]>
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>
20 struct aka
*akahead
= NULL
;
21 struct aka
*akatail
= NULL
;
23 struct home
*homehead
= NULL
;
24 struct home
*hometail
= NULL
;
32 char *akresult (struct aka
*);
33 char *akvalue (char *);
36 static char *akval (struct aka
*, char *);
37 static int aleq (char *, char *);
38 static char *scanp (char *);
39 static char *getp (char *);
40 static char *seekp (char *, char *, char **);
41 static int addfile (struct aka
*, char *);
42 static int addgroup (struct aka
*, char *);
43 static int addmember (struct aka
*, char *);
44 static int addall (struct aka
*);
45 static char *getalias (char *);
46 static void add_aka (struct aka
*, char *);
47 static struct aka
*akalloc (char *);
48 static struct home
*hmalloc (struct passwd
*);
50 struct home
*seek_home (char *);
54 /* Do mh alias substitution on 's' and return the results. */
64 v
= akval (akahead
, s
);
79 akresult (struct aka
*ak
)
81 register char *cp
= NULL
, *dp
, *pp
;
82 register struct adr
*ad
;
84 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
85 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
86 : getcpy (ad
->ad_text
);
90 cp
= concat (cp
, ",", pp
, NULL
);
99 akvis
= ak
->ak_visible
;
105 akval (struct aka
*ak
, char *s
)
110 for (; ak
; ak
= ak
->ak_next
)
111 if (aleq (s
, ak
->ak_name
))
112 return akresult (ak
);
119 aleq (char *string
, char *aliasent
)
123 while ((c
= *string
++))
124 if (*aliasent
== '*')
127 if ((c
| 040) != (*aliasent
| 040))
132 return (*aliasent
== 0 || *aliasent
== '*');
140 register char *bp
, *cp
, *pp
;
142 register struct aka
*ak
= NULL
;
146 && (strncmp (file
, "./", 2) && strncmp (file
, "../", 3)))
147 file
= etcpath (file
);
148 if ((fp
= fopen (file
, "r")) == NULL
) {
153 while (vfgets (fp
, &ap
) == OK
) {
155 switch (*(pp
= scanp (bp
))) {
156 case '<': /* recurse a level */
157 if (!*(cp
= getp (pp
+ 1))) {
158 akerrst
= "'<' without alias-file";
162 if ((i
= alias (cp
)) != AK_OK
) {
167 case ':': /* comment */
175 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
179 if (!(ak
= akalloc (cp
))) {
197 switch (*(pp
= scanp (ap
))) {
202 case '<': /* read values from file */
203 if (!*(cp
= getp (pp
+ 1))) {
207 if (!addfile (ak
, cp
)) {
213 case '=': /* UNIX group */
214 if (!*(cp
= getp (pp
+ 1))) {
218 if (!addgroup (ak
, cp
)) {
224 case '+': /* UNIX group members */
225 if (!*(cp
= getp (pp
+ 1))) {
229 if (!addmember (ak
, cp
)) {
235 case '*': /* Everyone */
240 while ((cp
= getalias (pp
)))
254 static char buffer
[BUFSIZ
];
258 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
262 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
266 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
270 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
274 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
294 register char *cp
= scanp (p
);
297 while (!isspace (*cp
) && *cp
)
306 seekp (char *p
, char *c
, char **a
)
311 while (!isspace (*cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
322 addfile (struct aka
*ak
, char *file
)
328 if (!(fp
= fopen (etcpath (file
), "r"))) {
333 while (fgets (buffer
, sizeof buffer
, fp
))
334 while ((cp
= getalias (buffer
)))
343 addgroup (struct aka
*ak
, char *grp
)
346 register struct group
*gr
= getgrnam (grp
);
347 register struct home
*hm
= NULL
;
350 gr
= getgrgid (atoi (grp
));
357 if (homehead
== NULL
)
361 while ((gp
= *gr
->gr_mem
++))
366 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
367 if (!strcmp (hm
->h_name
, gp
)) {
368 add_aka (ak
, hm
->h_name
);
372 if ((pw
= getpwnam(gp
)))
385 addmember (struct aka
*ak
, char *grp
)
388 register struct group
*gr
= getgrnam (grp
);
389 register struct home
*hm
= NULL
;
403 if (homehead
== NULL
)
407 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
408 if (hm
->h_gid
== gid
)
409 add_aka (ak
, hm
->h_name
);
416 addall (struct aka
*ak
)
418 int noshell
= NoShell
== NULL
|| *NoShell
== 0;
419 register struct home
*hm
;
422 if (homehead
== NULL
)
428 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
429 if (hm
->h_uid
> Everyone
430 && (noshell
|| strcmp (hm
->h_shell
, NoShell
)))
431 add_aka (ak
, hm
->h_name
);
433 return homehead
!= NULL
;
438 getalias (char *addrs
)
440 register char *pp
, *qp
;
441 static char *cp
= NULL
;
449 for (pp
= cp
; isspace (*pp
); pp
++)
453 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
457 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
470 add_aka (struct aka
*ak
, char *pp
)
472 register struct adr
*ad
, *ld
;
474 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
475 if (!strcmp (pp
, ad
->ad_text
))
478 ad
= (struct adr
*) malloc (sizeof(*ad
));
481 ad
->ad_text
= getcpy (pp
);
482 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
494 register struct passwd
*pw
;
500 /* if the list has yet to be initialized */
501 /* zap the list, and rebuild from scratch */
509 while ((pw
= getpwent ()))
523 register struct aka
*p
;
525 if (!(p
= (struct aka
*) malloc (sizeof(*p
))))
528 p
->ak_name
= getcpy (id
);
533 akatail
->ak_next
= p
;
543 hmalloc (struct passwd
*pw
)
545 register struct home
*p
;
547 if (!(p
= (struct home
*) malloc (sizeof(*p
))))
550 p
->h_name
= getcpy (pw
->pw_name
);
551 p
->h_uid
= pw
->pw_uid
;
552 p
->h_gid
= pw
->pw_gid
;
553 p
->h_home
= getcpy (pw
->pw_dir
);
554 p
->h_shell
= getcpy (pw
->pw_shell
);
557 if (hometail
!= NULL
)
558 hometail
->h_next
= p
;
559 if (homehead
== NULL
)
569 seek_home (char *name
)
571 register struct home
*hp
;
578 if (homehead
== NULL
)
582 for (hp
= homehead
; hp
; hp
= hp
->h_next
)
583 if (!strcasecmp (name
, hp
->h_name
))
588 * The only place where there might be problems.
589 * This assumes that ALL usernames are kept in lowercase.
591 for (c
= name
, c1
= lname
; *c
&& (c1
- lname
< sizeof(lname
) - 1); c
++, c1
++) {
592 if (isalpha(*c
) && isupper(*c
))
598 if ((pw
= getpwnam(lname
)))