]>
diplodocus.org Git - nmh/blob - uip/aliasbr.c
3 * aliasbr.c -- new aliasing mechanism
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/aliasbr.h>
19 struct aka
*akahead
= NULL
;
20 struct aka
*akatail
= NULL
;
22 struct home
*homehead
= NULL
;
23 struct home
*hometail
= NULL
;
31 char *akresult (struct aka
*);
32 char *akvalue (char *);
35 static char *akval (struct aka
*, char *);
36 static int aleq (char *, char *);
37 static char *scanp (unsigned char *);
38 static char *getp (char *);
39 static char *seekp (char *, char *, char **);
40 static int addfile (struct aka
*, char *);
41 static int addgroup (struct aka
*, char *);
42 static int addmember (struct aka
*, char *);
43 static int addall (struct aka
*);
44 static char *getalias (char *);
45 static void add_aka (struct aka
*, char *);
46 static struct aka
*akalloc (char *);
47 static struct home
*hmalloc (struct passwd
*);
48 struct home
*seek_home (char *);
51 /* Do mh alias substitution on 's' and return the results. */
61 v
= akval (akahead
, s
);
76 akresult (struct aka
*ak
)
78 register char *cp
= NULL
, *dp
, *pp
;
79 register struct adr
*ad
;
81 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
82 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
83 : getcpy (ad
->ad_text
);
87 cp
= concat (cp
, ",", pp
, NULL
);
96 akvis
= ak
->ak_visible
;
102 akval (struct aka
*ak
, char *s
)
107 for (; ak
; ak
= ak
->ak_next
)
108 if (aleq (s
, ak
->ak_name
))
109 return akresult (ak
);
116 aleq (char *string
, char *aliasent
)
120 while ((c
= *string
++))
121 if (*aliasent
== '*')
124 if ((c
| 040) != (*aliasent
| 040))
129 return (*aliasent
== 0 || *aliasent
== '*');
137 register char *bp
, *cp
, *pp
;
139 register struct aka
*ak
= NULL
;
143 && (strncmp (file
, "./", 2) && strncmp (file
, "../", 3)))
144 file
= etcpath (file
);
145 if ((fp
= fopen (file
, "r")) == NULL
) {
150 while (vfgets (fp
, &ap
) == OK
) {
152 switch (*(pp
= scanp (bp
))) {
153 case '<': /* recurse a level */
154 if (!*(cp
= getp (pp
+ 1))) {
155 akerrst
= "'<' without alias-file";
159 if ((i
= alias (cp
)) != AK_OK
) {
164 case ':': /* comment */
172 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
176 if (!(ak
= akalloc (cp
))) {
194 switch (*(pp
= scanp (ap
))) {
199 case '<': /* read values from file */
200 if (!*(cp
= getp (pp
+ 1))) {
204 if (!addfile (ak
, cp
)) {
210 case '=': /* UNIX group */
211 if (!*(cp
= getp (pp
+ 1))) {
215 if (!addgroup (ak
, cp
)) {
221 case '+': /* UNIX group members */
222 if (!*(cp
= getp (pp
+ 1))) {
226 if (!addmember (ak
, cp
)) {
232 case '*': /* Everyone */
237 while ((cp
= getalias (pp
)))
251 static char buffer
[BUFSIZ
];
255 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
259 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
263 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
267 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
271 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
280 scanp (unsigned char *p
)
291 register unsigned char *cp
= scanp (p
);
294 while (!isspace (*cp
) && *cp
)
303 seekp (char *p
, char *c
, char **a
)
305 register unsigned char *cp
;
308 while (!isspace (*cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
319 addfile (struct aka
*ak
, char *file
)
325 if (!(fp
= fopen (etcpath (file
), "r"))) {
330 while (fgets (buffer
, sizeof buffer
, fp
))
331 while ((cp
= getalias (buffer
)))
340 addgroup (struct aka
*ak
, char *grp
)
343 register struct group
*gr
= getgrnam (grp
);
344 register struct home
*hm
= NULL
;
347 gr
= getgrgid (atoi (grp
));
354 if (homehead
== NULL
)
358 while ((gp
= *gr
->gr_mem
++))
363 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
364 if (!strcmp (hm
->h_name
, gp
)) {
365 add_aka (ak
, hm
->h_name
);
369 if ((pw
= getpwnam(gp
)))
382 addmember (struct aka
*ak
, char *grp
)
385 register struct group
*gr
= getgrnam (grp
);
386 register struct home
*hm
= NULL
;
400 if (homehead
== NULL
)
404 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
405 if (hm
->h_gid
== gid
)
406 add_aka (ak
, hm
->h_name
);
413 addall (struct aka
*ak
)
415 int noshell
= NoShell
== NULL
|| *NoShell
== 0;
416 register struct home
*hm
;
419 if (homehead
== NULL
)
425 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
426 if (hm
->h_uid
> Everyone
427 && (noshell
|| strcmp (hm
->h_shell
, NoShell
)))
428 add_aka (ak
, hm
->h_name
);
430 return homehead
!= NULL
;
435 getalias (char *addrs
)
437 register unsigned char *pp
, *qp
;
438 static char *cp
= NULL
;
446 for (pp
= cp
; isspace (*pp
); pp
++)
450 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
454 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
467 add_aka (struct aka
*ak
, char *pp
)
469 register struct adr
*ad
, *ld
;
471 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
472 if (!strcmp (pp
, ad
->ad_text
))
475 ad
= (struct adr
*) mh_xmalloc (sizeof(*ad
));
476 ad
->ad_text
= getcpy (pp
);
477 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
489 register struct passwd
*pw
;
495 /* if the list has yet to be initialized */
496 /* zap the list, and rebuild from scratch */
504 while ((pw
= getpwent ()))
518 register struct aka
*p
;
520 p
= (struct aka
*) mh_xmalloc (sizeof(*p
));
522 p
->ak_name
= getcpy (id
);
527 akatail
->ak_next
= p
;
537 hmalloc (struct passwd
*pw
)
539 register struct home
*p
;
541 p
= (struct home
*) mh_xmalloc (sizeof(*p
));
543 p
->h_name
= getcpy (pw
->pw_name
);
544 p
->h_uid
= pw
->pw_uid
;
545 p
->h_gid
= pw
->pw_gid
;
546 p
->h_home
= getcpy (pw
->pw_dir
);
547 p
->h_shell
= getcpy (pw
->pw_shell
);
550 if (hometail
!= NULL
)
551 hometail
->h_next
= p
;
552 if (homehead
== NULL
)
561 seek_home (char *name
)
563 register struct home
*hp
;
571 if (homehead
== NULL
)
575 for (hp
= homehead
; hp
; hp
= hp
->h_next
)
576 if (!mh_strcasecmp (name
, hp
->h_name
))
581 * The only place where there might be problems.
582 * This assumes that ALL usernames are kept in lowercase.
584 for (c
= name
, c1
= lname
; *c
&& (c1
- lname
< sizeof(lname
) - 1); c
++, c1
++) {
585 if (isalpha(*c
) && isupper(*c
))
591 if ((pw
= getpwnam(lname
)))