1 /* aliasbr.c - new aliasing mechanism */
3 static char ident[] = "@(#)$Id: aliasbr.c,v 1.14 1993/02/26 21:58:38 jromine Exp $";
7 #include "../h/aliasbr.h"
19 struct aka *akahead = NULL;
20 struct aka *akatail = NULL;
22 struct home *homehead = NULL;
23 struct home *hometail = NULL;
25 static char *scanp (), *getp (), *seekp (), *akval (), *getalias ();
26 static struct aka *akalloc ();
27 static struct home *hmalloc ();
30 static int aleq(), addfile(), addgroup(), addmember(), addall();
34 struct passwd *getpwent ();
35 struct group *getgrnam (), *getgrgid ();
47 (void) alias (AliasFile);
50 v = akval (akahead, s);
64 register struct aka *ak;
66 register char *cp = NULL,
69 register struct adr *ad;
71 for (ad = ak -> ak_addr; ad; ad = ad -> ad_next) {
72 pp = ad -> ad_local ? akval (ak -> ak_next, ad -> ad_text)
73 : getcpy (ad -> ad_text);
76 cp = concat (cp, ",", pp, NULLCP);
85 akvis = ak -> ak_visible;
90 static char *akval (ak, s)
91 register struct aka *ak;
97 for (; ak; ak = ak -> ak_next)
98 if (aleq (s, ak -> ak_name))
105 static int aleq (string, aliasent)
106 register char *string,
111 while (c = *string++)
112 if (*aliasent == '*')
115 if ((c | 040) != (*aliasent | 040))
120 return (*aliasent == 0 || *aliasent == '*');
134 register struct aka *ak = NULL;
138 && (strncmp (file, "./", 2) && strncmp (file, "../", 3)))
139 file = libpath (file);
140 if ((fp = fopen (file, "r")) == NULL) {
145 while (vfgets (fp, &ap) == OK) {
147 switch (*(pp = scanp (bp))) {
148 case '<': /* recurse a level */
149 if (!*(cp = getp (pp + 1))) {
150 akerrst = "'<' without alias-file";
154 if ((i = alias (cp)) != AK_OK) {
159 case ':': /* comment */
167 if (!*(cp = seekp (pp, &lc, &ap))) {
171 if (!(ak = akalloc (cp))) {
177 ak -> ak_visible = 0;
181 ak -> ak_visible = 1;
189 switch (*(pp = scanp (ap))) {
194 case '<': /* read values from file */
195 if (!*(cp = getp (pp + 1))) {
199 if (!addfile (ak, cp)) {
205 case '=': /* UNIX group */
206 if (!*(cp = getp (pp + 1))) {
210 if (!addgroup (ak, cp)) {
216 case '+': /* UNIX group members */
217 if (!*(cp = getp (pp + 1))) {
221 if (!addmember (ak, cp)) {
227 case '*': /* Everyone */
232 while (cp = getalias (pp))
247 static char buffer[BUFSIZ];
251 (void) sprintf (buffer, "unable to read '%s'", akerrst);
255 (void) sprintf (buffer, "error in line '%s'", akerrst);
259 (void) sprintf (buffer, "out of memory while on '%s'", akerrst);
263 (void) sprintf (buffer, "no such group as '%s'", akerrst);
267 (void) sprintf (buffer, "unknown error (%d)", i);
276 static char *scanp (p)
285 static char *getp (p)
288 register char *cp = scanp (p);
291 while (!isspace (*cp) && *cp)
299 static char *seekp (p, c, a)
304 register char *cp = scanp (p);
307 while (!isspace (*cp) && *cp && *cp != ':' && *cp != ';')
318 static int addfile (ak, file)
319 register struct aka *ak;
326 if ((fp = fopen (libpath (file), "r")) == NULL) {
331 while (fgets (buffer, sizeof buffer, fp) != NULL)
332 while (cp = getalias (buffer))
341 static int addgroup (ak, grp)
342 register struct aka *ak;
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 static int addmember (ak, grp)
386 register struct aka *ak;
390 register struct group *gr = getgrnam (grp);
391 register struct home *hm = NULL;
405 if (homehead == NULL)
409 for (hm = homehead; hm; hm = hm -> h_next)
410 if (hm -> h_gid == gid)
411 add_aka (ak, hm -> h_name);
418 static int addall (ak)
419 register struct aka *ak;
421 int noshell = NoShell == NULLCP || *NoShell == 0;
422 register struct home *hm;
425 if (homehead == NULL)
431 for (hm = homehead; hm; hm = hm -> h_next)
432 if (hm -> h_uid > Everyone
433 && (noshell || strcmp (hm -> h_shell, NoShell)))
434 add_aka (ak, hm -> h_name);
436 return homehead != NULL;
441 static char *getalias (addrs)
442 register char *addrs;
446 static char *cp = NULL;
454 for (pp = cp; isspace (*pp); pp++)
458 for (qp = pp; *qp != 0 && *qp != ','; qp++)
462 for (cp = qp, qp--; qp > pp; qp--)
474 static add_aka (ak, pp)
475 register struct aka *ak;
478 register struct adr *ad,
481 for (ad = ak -> ak_addr, ld = NULL; ad; ld = ad, ad = ad -> ad_next)
482 if (!strcmp (pp, ad -> ad_text))
485 ad = (struct adr *) malloc (sizeof *ad);
488 ad -> ad_text = getcpy (pp);
489 ad -> ad_local = index (pp, '@') == NULL && index (pp, '!') == NULL;
490 ad -> ad_next = NULL;
499 register struct passwd *pw;
505 /* if the list has yet to be initialized */
506 /* zap the list, and rebuild from scratch */
514 while (pw = getpwent ())
526 static struct aka *akalloc (id)
529 register struct aka *p = (struct aka *) malloc (sizeof *p);
534 p -> ak_name = getcpy (id);
539 akatail -> ak_next = p;
548 static struct home *hmalloc (pw)
551 register struct home *p = (struct home *) malloc (sizeof *p);
556 p -> h_name = getcpy (pw -> pw_name);
557 p -> h_uid = pw -> pw_uid;
558 p -> h_gid = pw -> pw_gid;
559 p -> h_home = getcpy (pw -> pw_dir);
560 p -> h_shell = getcpy (pw -> pw_shell);
565 if (hometail != NULL)
566 hometail -> h_next = p;
567 if (homehead == NULL)
577 struct home *seek_home (name)
580 register struct home *hp;
587 if (homehead == NULL)
591 for (hp = homehead; hp; hp = hp -> h_next)
592 if (uleq (name, hp -> h_name))
595 #ifdef DBMPWD /* The only place where there might be problems */
596 /* This assumes that ALL usernames are kept in lowercase */
597 for (c = name,c1 = lname; *c; c++, c1++)
598 if (isalpha(*c) && isupper(*c))
603 if (pw = getpwnam(lname))