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));
353 while ((gp = *gr->gr_mem++))
356 for (hm = homehead; hm; hm = hm->h_next)
357 if (!strcmp (hm->h_name, gp)) {
358 add_aka (ak, hm->h_name);
361 if ((pw = getpwnam(gp)))
373 addmember (struct aka *ak, char *grp)
376 register struct group *gr = getgrnam (grp);
377 register struct home *hm = NULL;
392 for (hm = homehead; hm; hm = hm->h_next)
393 if (hm->h_gid == gid)
394 add_aka (ak, hm->h_name);
401 addall (struct aka *ak)
403 int noshell = NoShell == NULL || *NoShell == 0;
404 register struct home *hm;
411 for (hm = homehead; hm; hm = hm->h_next)
412 if ((int) hm->h_uid > Everyone
413 && (noshell || strcmp (hm->h_shell, NoShell)))
414 add_aka (ak, hm->h_name);
416 return homehead != NULL;
421 getalias (char *addrs)
423 register unsigned char *pp, *qp;
424 static char *cp = NULL;
432 for (pp = cp; isspace (*pp); pp++)
436 for (qp = pp; *qp != 0 && *qp != ','; qp++)
440 for (cp = qp, qp--; qp > pp; qp--)
453 add_aka (struct aka *ak, char *pp)
455 register struct adr *ad, *ld;
457 for (ad = ak->ak_addr, ld = NULL; ad; ld = ad, ad = ad->ad_next)
458 if (!strcmp (pp, ad->ad_text))
461 ad = (struct adr *) mh_xmalloc (sizeof(*ad));
462 ad->ad_text = getcpy (pp);
463 ad->ad_local = strchr(pp, '@') == NULL && strchr(pp, '!') == NULL;
475 register struct passwd *pw;
480 /* if the list has yet to be initialized */
481 /* zap the list, and rebuild from scratch */
488 while ((pw = getpwent ()))
500 register struct aka *p;
502 p = (struct aka *) mh_xmalloc (sizeof(*p));
504 p->ak_name = getcpy (id);
509 akatail->ak_next = p;
519 hmalloc (struct passwd *pw)
521 register struct home *p;
523 p = (struct home *) mh_xmalloc (sizeof(*p));
525 p->h_name = getcpy (pw->pw_name);
526 p->h_uid = pw->pw_uid;
527 p->h_gid = pw->pw_gid;
528 p->h_home = getcpy (pw->pw_dir);
529 p->h_shell = getcpy (pw->pw_shell);
532 if (hometail != NULL)
533 hometail->h_next = p;
534 if (homehead == NULL)
543 seek_home (char *name)
545 register struct home *hp;
551 for (hp = homehead; hp; hp = hp->h_next)
552 if (!mh_strcasecmp (name, hp->h_name))
556 * The only place where there might be problems.
557 * This assumes that ALL usernames are kept in lowercase.
559 for (c = name, c1 = lname;
560 *c && (c1 - lname < (int) sizeof(lname) - 1);
562 if (isalpha(*c) && isupper(*c))
568 if ((pw = getpwnam(lname)))