]>
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 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 /* 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
);
115 } else if (strchr (s
, ':')) {
116 /* The first address in a blind list will contain the
117 alias name, so try to match, but just with just the
118 address (not including the list name). If there's a
119 match, then replace the alias part with its
122 char *name
= getname (s
);
126 /* s is of the form "Blind list: address". If address
127 is an alias, expand it. */
128 struct mailname
*mp
= getm (name
, NULL
, 0, NULL
, 0);
130 if (mp
&& mp
->m_ingrp
) {
131 char *gname
= add (mp
->m_gname
, NULL
);
133 if (gname
&& aleq (name
, ak
->ak_name
)) {
135 cp
= concat (gname
, akresult (ak
), NULL
);
143 /* Need to flush getname after use. */
144 while (getname ("")) continue;
157 aleq (char *string
, char *aliasent
)
161 while ((c
= *string
++))
162 if (*aliasent
== '*')
165 if ((c
| 040) != (*aliasent
| 040))
170 return (*aliasent
== 0 || *aliasent
== '*');
178 register char *bp
, *cp
, *pp
;
180 register struct aka
*ak
= NULL
;
184 && (strncmp (file
, "./", 2) && strncmp (file
, "../", 3)))
185 file
= etcpath (file
);
186 if ((fp
= fopen (file
, "r")) == NULL
) {
191 while (vfgets (fp
, &ap
) == OK
) {
193 switch (*(pp
= scanp (bp
))) {
194 case '<': /* recurse a level */
195 if (!*(cp
= getp (pp
+ 1))) {
196 akerrst
= "'<' without alias-file";
200 if ((i
= alias (cp
)) != AK_OK
) {
205 case ':': /* comment */
213 if (!*(cp
= seekp (pp
, &lc
, &ap
))) {
217 if (!(ak
= akalloc (cp
))) {
235 switch (*(pp
= scanp (ap
))) {
240 case '<': /* read values from file */
241 if (!*(cp
= getp (pp
+ 1))) {
245 if (!addfile (ak
, cp
)) {
251 case '=': /* UNIX group */
252 if (!*(cp
= getp (pp
+ 1))) {
256 if (!addgroup (ak
, cp
)) {
262 case '+': /* UNIX group members */
263 if (!*(cp
= getp (pp
+ 1))) {
267 if (!addmember (ak
, cp
)) {
274 while ((cp
= getalias (pp
)))
288 static char buffer
[BUFSIZ
];
292 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
296 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
300 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
304 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
308 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
319 while (isspace ((unsigned char) *p
))
328 char *cp
= scanp (p
);
331 while (!isspace ((unsigned char) *cp
) && *cp
)
340 seekp (char *p
, char *c
, char **a
)
345 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
356 addfile (struct aka
*ak
, char *file
)
362 if (!(fp
= fopen (etcpath (file
), "r"))) {
367 while (fgets (buffer
, sizeof buffer
, fp
))
368 while ((cp
= getalias (buffer
)))
377 addgroup (struct aka
*ak
, char *grp
)
380 register struct group
*gr
= getgrnam (grp
);
381 register struct home
*hm
= NULL
;
384 gr
= getgrgid (atoi (grp
));
390 while ((gp
= *gr
->gr_mem
++))
393 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
394 if (!strcmp (hm
->h_name
, gp
)) {
395 add_aka (ak
, hm
->h_name
);
398 if ((pw
= getpwnam(gp
)))
410 addmember (struct aka
*ak
, char *grp
)
413 register struct group
*gr
= getgrnam (grp
);
414 register struct home
*hm
= NULL
;
429 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
430 if (hm
->h_gid
== gid
)
431 add_aka (ak
, hm
->h_name
);
438 getalias (char *addrs
)
441 static char *cp
= NULL
;
449 /* Remove leading any space from the address. */
450 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
454 /* Find the end of the address. */
455 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
457 /* Set cp to point to the remainder of the addresses. */
460 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
462 if (isspace ((unsigned char) *qp
))
473 add_aka (struct aka
*ak
, char *pp
)
475 register struct adr
*ad
, *ld
;
477 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
478 if (!strcmp (pp
, ad
->ad_text
))
481 ad
= (struct adr
*) mh_xmalloc (sizeof(*ad
));
482 ad
->ad_text
= getcpy (pp
);
483 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
495 register struct passwd
*pw
;
500 /* if the list has yet to be initialized */
501 /* zap the list, and rebuild from scratch */
508 while ((pw
= getpwent ()))
520 register struct aka
*p
;
522 p
= (struct aka
*) mh_xmalloc (sizeof(*p
));
524 p
->ak_name
= getcpy (id
);
529 akatail
->ak_next
= p
;
539 hmalloc (struct passwd
*pw
)
541 register struct home
*p
;
543 p
= (struct home
*) mh_xmalloc (sizeof(*p
));
545 p
->h_name
= getcpy (pw
->pw_name
);
546 p
->h_uid
= pw
->pw_uid
;
547 p
->h_gid
= pw
->pw_gid
;
548 p
->h_home
= getcpy (pw
->pw_dir
);
549 p
->h_shell
= getcpy (pw
->pw_shell
);
552 if (hometail
!= NULL
)
553 hometail
->h_next
= p
;
554 if (homehead
== NULL
)