]>
diplodocus.org Git - nmh/blob - uip/aliasbr.c
3 * aliasbr.c -- new aliasing mechanism
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/aliasbr.h>
21 struct aka
*akahead
= NULL
;
22 struct aka
*akatail
= NULL
;
24 struct home
*homehead
= NULL
;
25 struct home
*hometail
= NULL
;
33 char *akresult (struct aka
*);
34 char *akvalue (char *);
37 static char *akval (struct aka
*, char *);
38 static int aleq (char *, char *);
39 static char *scanp (unsigned char *);
40 static char *getp (char *);
41 static char *seekp (char *, char *, char **);
42 static int addfile (struct aka
*, char *);
43 static int addgroup (struct aka
*, char *);
44 static int addmember (struct aka
*, char *);
45 static int addall (struct aka
*);
46 static char *getalias (char *);
47 static void add_aka (struct aka
*, char *);
48 static struct aka
*akalloc (char *);
49 static struct home
*hmalloc (struct passwd
*);
50 struct home
*seek_home (char *);
53 /* Do mh alias substitution on 's' and return the results. */
63 v
= akval (akahead
, s
);
78 akresult (struct aka
*ak
)
80 register char *cp
= NULL
, *dp
, *pp
;
81 register struct adr
*ad
;
83 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
84 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
85 : getcpy (ad
->ad_text
);
89 cp
= concat (cp
, ",", pp
, NULL
);
98 akvis
= ak
->ak_visible
;
104 akval (struct aka
*ak
, char *s
)
109 for (; ak
; ak
= ak
->ak_next
)
110 if (aleq (s
, ak
->ak_name
))
111 return akresult (ak
);
118 aleq (char *string
, char *aliasent
)
122 while ((c
= *string
++))
123 if (*aliasent
== '*')
126 if ((c
| 040) != (*aliasent
| 040))
131 return (*aliasent
== 0 || *aliasent
== '*');
139 register char *bp
, *cp
, *pp
;
141 register struct aka
*ak
= NULL
;
145 && (strncmp (file
, "./", 2) && strncmp (file
, "../", 3)))
146 file
= etcpath (file
);
147 if ((fp
= fopen (file
, "r")) == NULL
) {
152 while (vfgets (fp
, &ap
) == OK
) {
154 switch (*(pp
= scanp (bp
))) {
155 case '<': /* recurse a level */
156 if (!*(cp
= getp (pp
+ 1))) {
157 akerrst
= "'<' without alias-file";
161 if ((i
= alias (cp
)) != AK_OK
) {
166 case ':': /* comment */
174 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
178 if (!(ak
= akalloc (cp
))) {
196 switch (*(pp
= scanp (ap
))) {
201 case '<': /* read values from file */
202 if (!*(cp
= getp (pp
+ 1))) {
206 if (!addfile (ak
, cp
)) {
212 case '=': /* UNIX group */
213 if (!*(cp
= getp (pp
+ 1))) {
217 if (!addgroup (ak
, cp
)) {
223 case '+': /* UNIX group members */
224 if (!*(cp
= getp (pp
+ 1))) {
228 if (!addmember (ak
, cp
)) {
234 case '*': /* Everyone */
239 while ((cp
= getalias (pp
)))
253 static char buffer
[BUFSIZ
];
257 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
261 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
265 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
269 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
273 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
282 scanp (unsigned char *p
)
293 register unsigned char *cp
= scanp (p
);
296 while (!isspace (*cp
) && *cp
)
305 seekp (char *p
, char *c
, char **a
)
307 register unsigned char *cp
;
310 while (!isspace (*cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
321 addfile (struct aka
*ak
, char *file
)
327 if (!(fp
= fopen (etcpath (file
), "r"))) {
332 while (fgets (buffer
, sizeof buffer
, fp
))
333 while ((cp
= getalias (buffer
)))
342 addgroup (struct aka
*ak
, char *grp
)
345 register struct group
*gr
= getgrnam (grp
);
346 register struct home
*hm
= NULL
;
349 gr
= getgrgid (atoi (grp
));
356 if (homehead
== NULL
)
360 while ((gp
= *gr
->gr_mem
++))
365 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
366 if (!strcmp (hm
->h_name
, gp
)) {
367 add_aka (ak
, hm
->h_name
);
371 if ((pw
= getpwnam(gp
)))
384 addmember (struct aka
*ak
, char *grp
)
387 register struct group
*gr
= getgrnam (grp
);
388 register struct home
*hm
= NULL
;
402 if (homehead
== NULL
)
406 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
407 if (hm
->h_gid
== gid
)
408 add_aka (ak
, hm
->h_name
);
415 addall (struct aka
*ak
)
417 int noshell
= NoShell
== NULL
|| *NoShell
== 0;
418 register struct home
*hm
;
421 if (homehead
== NULL
)
427 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
428 if (hm
->h_uid
> Everyone
429 && (noshell
|| strcmp (hm
->h_shell
, NoShell
)))
430 add_aka (ak
, hm
->h_name
);
432 return homehead
!= NULL
;
437 getalias (char *addrs
)
439 register unsigned char *pp
, *qp
;
440 static char *cp
= NULL
;
448 for (pp
= cp
; isspace (*pp
); pp
++)
452 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
456 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
469 add_aka (struct aka
*ak
, char *pp
)
471 register struct adr
*ad
, *ld
;
473 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
474 if (!strcmp (pp
, ad
->ad_text
))
477 ad
= (struct adr
*) mh_xmalloc (sizeof(*ad
));
478 ad
->ad_text
= getcpy (pp
);
479 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
491 register struct passwd
*pw
;
497 /* if the list has yet to be initialized */
498 /* zap the list, and rebuild from scratch */
506 while ((pw
= getpwent ()))
520 register struct aka
*p
;
522 p
= (struct aka
*) mh_xmalloc (sizeof(*p
));
524 p
->ak_name
= getcpy (id
);
529 akatail
->ak_next
= p
;
539 hmalloc (struct passwd
*pw
)
541 register struct home
*p
;
543 p
= (struct home
*) mh_xmalloc (sizeof(*p
));
545 p
->h_name
= getcpy (pw
->pw_name
);
546 p
->h_uid
= pw
->pw_uid
;
547 p
->h_gid
= pw
->pw_gid
;
548 p
->h_home
= getcpy (pw
->pw_dir
);
549 p
->h_shell
= getcpy (pw
->pw_shell
);
552 if (hometail
!= NULL
)
553 hometail
->h_next
= p
;
554 if (homehead
== NULL
)
563 seek_home (char *name
)
565 register struct home
*hp
;
573 if (homehead
== NULL
)
577 for (hp
= homehead
; hp
; hp
= hp
->h_next
)
578 if (!mh_strcasecmp (name
, hp
->h_name
))
583 * The only place where there might be problems.
584 * This assumes that ALL usernames are kept in lowercase.
586 for (c
= name
, c1
= lname
; *c
&& (c1
- lname
< sizeof(lname
) - 1); c
++, c1
++) {
587 if (isalpha(*c
) && isupper(*c
))
593 if ((pw
= getpwnam(lname
)))