]>
diplodocus.org Git - nmh/blob - uip/aliasbr.c
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>
12 #include <h/addrsbr.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 char *getalias (char *);
45 static void add_aka (struct aka
*, char *);
46 static struct aka
*akalloc (char *);
47 static struct home
*hmalloc (struct passwd
*);
50 /* Do mh alias substitution on 's' and return the results. */
60 v
= akval (akahead
, s
);
75 akresult (struct aka
*ak
)
77 char *cp
= NULL
, *dp
, *pp
;
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 /* It'd be tempting to check for a trailing semicolon and remove
107 it. But that would break the EXMH alias parser on what would
108 then be valid expressions:
109 http://lists.gnu.org/archive/html/nmh-workers/2012-10/msg00039.html
112 for (; ak
; ak
= ak
->ak_next
) {
113 if (aleq (s
, ak
->ak_name
)) {
114 return akresult (ak
);
117 if (strchr (s
, ':')) {
118 /* The first address in a blind list will contain the
119 alias name, so try to match, but just with just the
120 address (not including the list name). If there's a
121 match, then replace the alias part with its
124 char *name
= getname (s
);
128 /* s is of the form "Blind list: address". If address
129 is an alias, expand it. */
130 struct mailname
*mp
= getm (name
, NULL
, 0, NULL
, 0);
132 if (mp
&& mp
->m_ingrp
) {
133 char *gname
= add (mp
->m_gname
, NULL
);
135 /* FIXME: gname must be true; add() never returns NULL.
136 * Is some other test required? */
137 if (gname
&& aleq (name
, ak
->ak_name
)) {
139 cp
= concat (gname
, akresult (ak
), NULL
);
147 /* Need to flush getname after use. */
148 while (getname ("")) continue;
156 return mh_xstrdup(s
);
161 aleq (char *string
, char *aliasent
)
165 while ((c
= *string
++)) {
166 if (*aliasent
== '*')
168 if (tolower((unsigned char)c
) != tolower((unsigned char)*aliasent
))
173 return (*aliasent
== 0 || *aliasent
== '*');
183 struct aka
*ak
= NULL
;
187 && !has_prefix(file
, "./") && !has_prefix(file
, "../"))
188 file
= etcpath (file
);
189 if ((fp
= fopen (file
, "r")) == NULL
) {
194 while (vfgets (fp
, &ap
) == OK
) {
196 switch (*(pp
= scanp (bp
))) {
197 case '<': /* recurse a level */
198 if (!*(cp
= getp (pp
+ 1))) {
199 akerrst
= "'<' without alias-file";
203 if ((i
= alias (cp
)) != AK_OK
) {
208 case ':': /* comment */
216 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
220 if (!(ak
= akalloc (cp
))) {
238 switch (*(pp
= scanp (ap
))) {
243 case '<': /* read values from file */
244 if (!*(cp
= getp (pp
+ 1))) {
248 if (!addfile (ak
, cp
)) {
254 case '=': /* UNIX group */
255 if (!*(cp
= getp (pp
+ 1))) {
259 if (!addgroup (ak
, cp
)) {
265 case '+': /* UNIX group members */
266 if (!*(cp
= getp (pp
+ 1))) {
270 if (!addmember (ak
, cp
)) {
277 while ((cp
= getalias (pp
)))
291 static char buffer
[BUFSIZ
];
295 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
299 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
303 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
307 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
311 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
322 while (isspace ((unsigned char) *p
))
331 char *cp
= scanp (p
);
334 while (!isspace ((unsigned char) *cp
) && *cp
)
343 seekp (char *p
, char *c
, char **a
)
348 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
359 addfile (struct aka
*ak
, char *file
)
365 if (!(fp
= fopen (etcpath (file
), "r"))) {
370 while (fgets (buffer
, sizeof buffer
, fp
))
371 while ((cp
= getalias (buffer
)))
380 addgroup (struct aka
*ak
, char *grp
)
383 struct group
*gr
= getgrnam (grp
);
384 struct home
*hm
= NULL
;
387 gr
= getgrgid (atoi (grp
));
393 while ((gp
= *gr
->gr_mem
++))
396 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
397 if (!strcmp (hm
->h_name
, gp
)) {
398 add_aka (ak
, hm
->h_name
);
401 if ((pw
= getpwnam(gp
)))
413 addmember (struct aka
*ak
, char *grp
)
416 struct group
*gr
= getgrnam (grp
);
417 struct home
*hm
= NULL
;
432 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
433 if (hm
->h_gid
== gid
)
434 add_aka (ak
, hm
->h_name
);
441 getalias (char *addrs
)
444 static char *cp
= NULL
;
452 /* Remove leading any space from the address. */
453 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
457 /* Find the end of the address. */
458 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
460 /* Set cp to point to the remainder of the addresses. */
463 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
465 if (isspace ((unsigned char) *qp
))
476 add_aka (struct aka
*ak
, char *pp
)
480 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
481 if (!strcmp (pp
, ad
->ad_text
))
485 ad
->ad_text
= mh_xstrdup(pp
);
486 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
503 /* if the list has yet to be initialized */
504 /* zap the list, and rebuild from scratch */
511 while ((pw
= getpwent ()))
526 p
->ak_name
= getcpy (id
);
531 akatail
->ak_next
= p
;
541 hmalloc (struct passwd
*pw
)
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
)