]>
diplodocus.org Git - nmh/blob - uip/aliasbr.c
3 * aliasbr.c -- new aliasing mechanism
16 struct aka
*akahead
= NULL
;
17 struct aka
*akatail
= NULL
;
19 struct home
*homehead
= NULL
;
20 struct home
*hometail
= NULL
;
28 char *akresult (struct aka
*);
29 char *akvalue (char *);
32 static char *akval (struct aka
*, char *);
33 static int aleq (char *, char *);
34 static char *scanp (char *);
35 static char *getp (char *);
36 static char *seekp (char *, char *, char **);
37 static int addfile (struct aka
*, char *);
38 static int addgroup (struct aka
*, char *);
39 static int addmember (struct aka
*, char *);
40 static int addall (struct aka
*);
41 static char *getalias (char *);
42 static void add_aka (struct aka
*, char *);
43 static struct aka
*akalloc (char *);
44 static struct home
*hmalloc (struct passwd
*);
46 struct home
*seek_home (char *);
50 /* Do mh alias substitution on 's' and return the results. */
60 v
= akval (akahead
, s
);
75 akresult (struct aka
*ak
)
77 register char *cp
= NULL
, *dp
, *pp
;
78 register struct adr
*ad
;
80 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
81 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
82 : getcpy (ad
->ad_text
);
86 cp
= concat (cp
, ",", pp
, NULL
);
95 akvis
= ak
->ak_visible
;
101 akval (struct aka
*ak
, char *s
)
106 for (; ak
; ak
= ak
->ak_next
)
107 if (aleq (s
, ak
->ak_name
))
108 return akresult (ak
);
115 aleq (char *string
, char *aliasent
)
119 while ((c
= *string
++))
120 if (*aliasent
== '*')
123 if ((c
| 040) != (*aliasent
| 040))
128 return (*aliasent
== 0 || *aliasent
== '*');
136 register char *bp
, *cp
, *pp
;
138 register struct aka
*ak
= NULL
;
142 && (strncmp (file
, "./", 2) && strncmp (file
, "../", 3)))
143 file
= etcpath (file
);
144 if ((fp
= fopen (file
, "r")) == NULL
) {
149 while (vfgets (fp
, &ap
) == OK
) {
151 switch (*(pp
= scanp (bp
))) {
152 case '<': /* recurse a level */
153 if (!*(cp
= getp (pp
+ 1))) {
154 akerrst
= "'<' without alias-file";
158 if ((i
= alias (cp
)) != AK_OK
) {
163 case ':': /* comment */
171 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
175 if (!(ak
= akalloc (cp
))) {
193 switch (*(pp
= scanp (ap
))) {
198 case '<': /* read values from file */
199 if (!*(cp
= getp (pp
+ 1))) {
203 if (!addfile (ak
, cp
)) {
209 case '=': /* UNIX group */
210 if (!*(cp
= getp (pp
+ 1))) {
214 if (!addgroup (ak
, cp
)) {
220 case '+': /* UNIX group members */
221 if (!*(cp
= getp (pp
+ 1))) {
225 if (!addmember (ak
, cp
)) {
231 case '*': /* Everyone */
236 while ((cp
= getalias (pp
)))
250 static char buffer
[BUFSIZ
];
254 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
258 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
262 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
266 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
270 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
290 register char *cp
= scanp (p
);
293 while (!isspace (*cp
) && *cp
)
302 seekp (char *p
, char *c
, char **a
)
307 while (!isspace (*cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
318 addfile (struct aka
*ak
, char *file
)
324 if (!(fp
= fopen (etcpath (file
), "r"))) {
329 while (fgets (buffer
, sizeof buffer
, fp
))
330 while ((cp
= getalias (buffer
)))
339 addgroup (struct aka
*ak
, char *grp
)
342 register struct group
*gr
= getgrnam (grp
);
343 register struct home
*hm
= NULL
;
346 gr
= getgrgid (atoi (grp
));
353 if (homehead
== NULL
)
357 while ((gp
= *gr
->gr_mem
++))
362 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
363 if (!strcmp (hm
->h_name
, gp
)) {
364 add_aka (ak
, hm
->h_name
);
368 if ((pw
= getpwnam(gp
)))
381 addmember (struct aka
*ak
, char *grp
)
384 register struct group
*gr
= getgrnam (grp
);
385 register struct home
*hm
= NULL
;
399 if (homehead
== NULL
)
403 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
404 if (hm
->h_gid
== gid
)
405 add_aka (ak
, hm
->h_name
);
412 addall (struct aka
*ak
)
414 int noshell
= NoShell
== NULL
|| *NoShell
== 0;
415 register struct home
*hm
;
418 if (homehead
== NULL
)
424 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
425 if (hm
->h_uid
> Everyone
426 && (noshell
|| strcmp (hm
->h_shell
, NoShell
)))
427 add_aka (ak
, hm
->h_name
);
429 return homehead
!= NULL
;
434 getalias (char *addrs
)
436 register char *pp
, *qp
;
437 static char *cp
= NULL
;
445 for (pp
= cp
; isspace (*pp
); pp
++)
449 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
453 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
466 add_aka (struct aka
*ak
, char *pp
)
468 register struct adr
*ad
, *ld
;
470 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
471 if (!strcmp (pp
, ad
->ad_text
))
474 ad
= (struct adr
*) malloc (sizeof(*ad
));
477 ad
->ad_text
= getcpy (pp
);
478 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
490 register struct passwd
*pw
;
496 /* if the list has yet to be initialized */
497 /* zap the list, and rebuild from scratch */
505 while ((pw
= getpwent ()))
519 register struct aka
*p
;
521 if (!(p
= (struct aka
*) malloc (sizeof(*p
))))
524 p
->ak_name
= getcpy (id
);
529 akatail
->ak_next
= p
;
539 hmalloc (struct passwd
*pw
)
541 register struct home
*p
;
543 if (!(p
= (struct home
*) malloc (sizeof(*p
))))
546 p
->h_name
= getcpy (pw
->pw_name
);
547 p
->h_uid
= pw
->pw_uid
;
548 p
->h_gid
= pw
->pw_gid
;
549 p
->h_home
= getcpy (pw
->pw_dir
);
550 p
->h_shell
= getcpy (pw
->pw_shell
);
553 if (hometail
!= NULL
)
554 hometail
->h_next
= p
;
555 if (homehead
== NULL
)
565 seek_home (char *name
)
567 register struct home
*hp
;
574 if (homehead
== NULL
)
578 for (hp
= homehead
; hp
; hp
= hp
->h_next
)
579 if (!strcasecmp (name
, hp
->h_name
))
584 * The only place where there might be problems.
585 * This assumes that ALL usernames are kept in lowercase.
587 for (c
= name
, c1
= lname
; *c
&& (c1
- lname
< sizeof(lname
) - 1); c
++, c1
++) {
588 if (isalpha(*c
) && isupper(*c
))
594 if ((pw
= getpwnam(lname
)))