2 ** aliasbr.c -- new aliasing mechanism
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/aliasbr.h>
18 struct aka *akahead = NULL;
19 struct aka *akatail = NULL;
21 struct home *homehead = NULL;
22 struct home *hometail = NULL;
30 char *akresult(struct aka *);
31 char *akvalue(char *);
34 static char *akval(struct aka *, char *);
35 static int aleq(char *, char *);
36 static char *scanp(unsigned char *);
37 static char *getp(char *);
38 static char *seekp(char *, char *, char **);
39 static int addfile(struct aka *, char *);
40 static int addgroup(struct aka *, char *);
41 static int addmember(struct aka *, char *);
42 static char *getalias(char *);
43 static void add_aka(struct aka *, char *);
44 static struct aka *akalloc(char *);
45 static struct home *hmalloc(struct passwd *);
46 struct home *seek_home(char *);
49 /* Do mh alias substitution on 's' and return the results. */
56 v = akval(akahead, s);
71 akresult(struct aka *ak)
73 register char *cp = NULL, *dp, *pp;
74 register struct adr *ad;
76 for (ad = ak->ak_addr; ad; ad = ad->ad_next) {
77 pp = ad->ad_local ? akval(ak->ak_next, ad->ad_text)
78 : getcpy(ad->ad_text);
82 cp = concat(cp, ",", pp, NULL);
90 akvis = ak->ak_visible;
96 akval(struct aka *ak, char *s)
101 for (; ak; ak = ak->ak_next)
102 if (aleq(s, ak->ak_name))
110 aleq(char *string, char *aliasent)
114 while ((c = *string++))
115 if (*aliasent == '*')
117 else if ((c | 040) != (*aliasent | 040))
122 return (*aliasent == 0 || *aliasent == '*');
127 ** file needs to be absolute or relative to cwd
133 register char *bp, *cp, *pp;
135 register struct aka *ak = NULL;
138 if ((fp = fopen(file, "r")) == NULL) {
143 while (vfgets(fp, &ap) == OK) {
145 switch (*(pp = scanp(bp))) {
146 case '<': /* recurse a level */
147 if (!*(cp = getp(pp + 1))) {
148 akerrst = "'<' without alias-file";
152 if ((i = alias(cp)) != AK_OK) {
157 case ':': /* comment */
165 if (!*(cp = seekp(pp, &lc, &ap))) {
169 if (!(ak = akalloc(cp))) {
187 switch (*(pp = scanp(ap))) {
192 case '<': /* read values from file */
193 if (!*(cp = getp(pp + 1))) {
197 if (!addfile(ak, cp)) {
203 case '=': /* UNIX group */
204 if (!*(cp = getp(pp + 1))) {
208 if (!addgroup(ak, cp)) {
214 case '+': /* UNIX group members */
215 if (!*(cp = getp(pp + 1))) {
219 if (!addmember(ak, cp)) {
226 while ((cp = getalias(pp)))
240 static char buffer[BUFSIZ];
244 snprintf(buffer, sizeof(buffer), "unable to read '%s'",
249 snprintf(buffer, sizeof(buffer), "error in line '%s'",
254 snprintf(buffer, sizeof(buffer), "out of memory while on '%s'",
259 snprintf(buffer, sizeof(buffer), "no such group as '%s'",
264 snprintf(buffer, sizeof(buffer), "unknown error (%d)", i);
273 scanp(unsigned char *p)
284 register unsigned char *cp = scanp(p);
287 while (!isspace(*cp) && *cp)
296 seekp(char *p, char *c, char **a)
298 register unsigned char *cp;
301 while (!isspace(*cp) && *cp && *cp != ':' && *cp != ';')
312 addfile(struct aka *ak, char *file)
318 if (!(fp = fopen(etcpath(file), "r"))) {
323 while (fgets(buffer, sizeof buffer, fp))
324 while ((cp = getalias(buffer)))
333 addgroup(struct aka *ak, char *grp)
336 register struct group *gr = getgrnam(grp);
337 register struct home *hm = NULL;
340 gr = getgrgid(atoi(grp));
346 while ((gp = *gr->gr_mem++))
349 for (hm = homehead; hm; hm = hm->h_next)
350 if (strcmp(hm->h_name, gp)==0) {
351 add_aka(ak, hm->h_name);
354 if ((pw = getpwnam(gp))) {
365 addmember(struct aka *ak, char *grp)
368 register struct group *gr = getgrnam(grp);
369 register struct home *hm = NULL;
384 for (hm = homehead; hm; hm = hm->h_next)
385 if (hm->h_gid == gid)
386 add_aka(ak, hm->h_name);
393 getalias(char *addrs)
395 register unsigned char *pp, *qp;
396 static char *cp = NULL;
404 for (pp = cp; isspace(*pp); pp++)
408 for (qp = pp; *qp != 0 && *qp != ','; qp++)
412 for (cp = qp, qp--; qp > pp; qp--)
425 add_aka(struct aka *ak, char *pp)
427 register struct adr *ad, *ld;
429 for (ad = ak->ak_addr, ld = NULL; ad; ld = ad, ad = ad->ad_next)
430 if (strcmp(pp, ad->ad_text)==0)
433 ad = (struct adr *) mh_xmalloc(sizeof(*ad));
434 ad->ad_text = getcpy(pp);
435 ad->ad_local = strchr(pp, '@') == NULL;
447 register struct passwd *pw;
451 /* read the passwd database and build a list */
453 while ((pw = getpwent())) {
460 init++; /* now we're initialized */
468 register struct aka *p;
470 p = (struct aka *) mh_xmalloc(sizeof(*p));
472 p->ak_name = getcpy(id);
477 akatail->ak_next = p;
487 hmalloc(struct passwd *pw)
489 register struct home *p;
491 p = (struct home *) mh_xmalloc(sizeof(*p));
493 p->h_name = getcpy(pw->pw_name);
494 p->h_uid = pw->pw_uid;
495 p->h_gid = pw->pw_gid;
496 p->h_home = getcpy(pw->pw_dir);
497 p->h_shell = getcpy(pw->pw_shell);
504 hometail->h_next = p;
512 seek_home(char *name)
514 register struct home *hp;
520 for (hp = homehead; hp; hp = hp->h_next)
521 if (!mh_strcasecmp(name, hp->h_name))
525 ** The only place where there might be problems.
526 ** This assumes that ALL usernames are kept in lowercase.
528 for (c = name, c1 = lname; *c && (c1 - lname < sizeof(lname) - 1);
530 if (isalpha(*c) && isupper(*c))
536 if ((pw = getpwnam(lname)))