]>
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 if (gname
&& aleq (name
, ak
->ak_name
)) {
137 cp
= concat (gname
, akresult (ak
), NULL
);
145 /* Need to flush getname after use. */
146 while (getname ("")) continue;
154 return mh_xstrdup(s
);
159 aleq (char *string
, char *aliasent
)
163 while ((c
= *string
++)) {
164 if (*aliasent
== '*')
166 if ((c
| 040) != (*aliasent
| 040))
171 return (*aliasent
== 0 || *aliasent
== '*');
181 struct aka
*ak
= NULL
;
185 && !HasPrefix(file
, "./") && !HasPrefix(file
, "../"))
186 file
= etcpath (file
);
187 if ((fp
= fopen (file
, "r")) == NULL
) {
192 while (vfgets (fp
, &ap
) == OK
) {
194 switch (*(pp
= scanp (bp
))) {
195 case '<': /* recurse a level */
196 if (!*(cp
= getp (pp
+ 1))) {
197 akerrst
= "'<' without alias-file";
201 if ((i
= alias (cp
)) != AK_OK
) {
206 case ':': /* comment */
214 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
218 if (!(ak
= akalloc (cp
))) {
236 switch (*(pp
= scanp (ap
))) {
241 case '<': /* read values from file */
242 if (!*(cp
= getp (pp
+ 1))) {
246 if (!addfile (ak
, cp
)) {
252 case '=': /* UNIX group */
253 if (!*(cp
= getp (pp
+ 1))) {
257 if (!addgroup (ak
, cp
)) {
263 case '+': /* UNIX group members */
264 if (!*(cp
= getp (pp
+ 1))) {
268 if (!addmember (ak
, cp
)) {
275 while ((cp
= getalias (pp
)))
289 static char buffer
[BUFSIZ
];
293 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
297 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
301 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
305 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
309 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
320 while (isspace ((unsigned char) *p
))
329 char *cp
= scanp (p
);
332 while (!isspace ((unsigned char) *cp
) && *cp
)
341 seekp (char *p
, char *c
, char **a
)
346 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
357 addfile (struct aka
*ak
, char *file
)
363 if (!(fp
= fopen (etcpath (file
), "r"))) {
368 while (fgets (buffer
, sizeof buffer
, fp
))
369 while ((cp
= getalias (buffer
)))
378 addgroup (struct aka
*ak
, char *grp
)
381 struct group
*gr
= getgrnam (grp
);
382 struct home
*hm
= NULL
;
385 gr
= getgrgid (atoi (grp
));
391 while ((gp
= *gr
->gr_mem
++))
394 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
395 if (!strcmp (hm
->h_name
, gp
)) {
396 add_aka (ak
, hm
->h_name
);
399 if ((pw
= getpwnam(gp
)))
411 addmember (struct aka
*ak
, char *grp
)
414 struct group
*gr
= getgrnam (grp
);
415 struct home
*hm
= NULL
;
430 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
431 if (hm
->h_gid
== gid
)
432 add_aka (ak
, hm
->h_name
);
439 getalias (char *addrs
)
442 static char *cp
= NULL
;
450 /* Remove leading any space from the address. */
451 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
455 /* Find the end of the address. */
456 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
458 /* Set cp to point to the remainder of the addresses. */
461 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
463 if (isspace ((unsigned char) *qp
))
474 add_aka (struct aka
*ak
, char *pp
)
478 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
479 if (!strcmp (pp
, ad
->ad_text
))
483 ad
->ad_text
= mh_xstrdup(pp
);
484 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
501 /* if the list has yet to be initialized */
502 /* zap the list, and rebuild from scratch */
509 while ((pw
= getpwent ()))
524 p
->ak_name
= getcpy (id
);
529 akatail
->ak_next
= p
;
539 hmalloc (struct passwd
*pw
)
544 p
->h_name
= getcpy (pw
->pw_name
);
545 p
->h_uid
= pw
->pw_uid
;
546 p
->h_gid
= pw
->pw_gid
;
547 p
->h_home
= getcpy (pw
->pw_dir
);
548 p
->h_shell
= getcpy (pw
->pw_shell
);
551 if (hometail
!= NULL
)
552 hometail
->h_next
= p
;
553 if (homehead
== NULL
)