]>
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 int addall (struct aka
*);
45 static char *getalias (char *);
46 static void add_aka (struct aka
*, char *);
47 static struct aka
*akalloc (char *);
48 static struct home
*hmalloc (struct passwd
*);
51 /* Do mh alias substitution on 's' and return the results. */
61 v
= akval (akahead
, s
);
76 akresult (struct aka
*ak
)
78 register char *cp
= NULL
, *dp
, *pp
;
79 register struct adr
*ad
;
81 for (ad
= ak
->ak_addr
; ad
; ad
= ad
->ad_next
) {
82 pp
= ad
->ad_local
? akval (ak
->ak_next
, ad
->ad_text
)
83 : getcpy (ad
->ad_text
);
87 cp
= concat (cp
, ",", pp
, NULL
);
96 akvis
= ak
->ak_visible
;
102 akval (struct aka
*ak
, char *s
)
107 /* It'd be tempting to check for a trailing semicolon and remove
108 it. But that would break the EXMH alias parser on what would
109 then be valid expressions:
110 http://lists.gnu.org/archive/html/nmh-workers/2012-10/msg00039.html
113 for (; ak
; ak
= ak
->ak_next
) {
114 if (aleq (s
, ak
->ak_name
)) {
115 return akresult (ak
);
116 } else if (strchr (s
, ':')) {
117 /* The first address in a blind list will contain the
118 alias name, so try to match, but just with just the
119 address (not including the list name). If there's a
120 match, then replace the alias part with its
123 char *name
= getname (s
);
127 /* s is of the form "Blind list: address". If address
128 is an alias, expand it. */
129 struct mailname
*mp
= getm (name
, NULL
, 0, NULL
, 0);
131 if (mp
&& mp
->m_ingrp
) {
132 char *gname
= add (mp
->m_gname
, NULL
);
134 if (gname
&& aleq (name
, ak
->ak_name
)) {
136 cp
= concat (gname
, akresult (ak
), NULL
);
144 /* Need to flush getname after use. */
145 while (getname ("")) continue;
158 aleq (char *string
, char *aliasent
)
162 while ((c
= *string
++))
163 if (*aliasent
== '*')
166 if ((c
| 040) != (*aliasent
| 040))
171 return (*aliasent
== 0 || *aliasent
== '*');
179 register char *bp
, *cp
, *pp
;
181 register struct aka
*ak
= NULL
;
185 && (strncmp (file
, "./", 2) && strncmp (file
, "../", 3)))
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
)) {
274 case '*': /* Everyone */
279 while ((cp
= getalias (pp
)))
293 static char buffer
[BUFSIZ
];
297 snprintf (buffer
, sizeof(buffer
), "unable to read '%s'", akerrst
);
301 snprintf (buffer
, sizeof(buffer
), "error in line '%s'", akerrst
);
305 snprintf (buffer
, sizeof(buffer
), "out of memory while on '%s'", akerrst
);
309 snprintf (buffer
, sizeof(buffer
), "no such group as '%s'", akerrst
);
313 snprintf (buffer
, sizeof(buffer
), "unknown error (%d)", i
);
324 while (isspace ((unsigned char) *p
))
333 char *cp
= scanp (p
);
336 while (!isspace ((unsigned char) *cp
) && *cp
)
345 seekp (char *p
, char *c
, char **a
)
350 while (!isspace ((unsigned char) *cp
) && *cp
&& *cp
!= ':' && *cp
!= ';')
361 addfile (struct aka
*ak
, char *file
)
367 if (!(fp
= fopen (etcpath (file
), "r"))) {
372 while (fgets (buffer
, sizeof buffer
, fp
))
373 while ((cp
= getalias (buffer
)))
382 addgroup (struct aka
*ak
, char *grp
)
385 register struct group
*gr
= getgrnam (grp
);
386 register struct home
*hm
= NULL
;
389 gr
= getgrgid (atoi (grp
));
395 while ((gp
= *gr
->gr_mem
++))
398 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
399 if (!strcmp (hm
->h_name
, gp
)) {
400 add_aka (ak
, hm
->h_name
);
403 if ((pw
= getpwnam(gp
)))
415 addmember (struct aka
*ak
, char *grp
)
418 register struct group
*gr
= getgrnam (grp
);
419 register struct home
*hm
= NULL
;
434 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
435 if (hm
->h_gid
== gid
)
436 add_aka (ak
, hm
->h_name
);
443 addall (struct aka
*ak
)
445 int noshell
= NoShell
== NULL
|| *NoShell
== 0;
446 register struct home
*hm
;
453 for (hm
= homehead
; hm
; hm
= hm
->h_next
)
454 if ((int) hm
->h_uid
> Everyone
455 && (noshell
|| strcmp (hm
->h_shell
, NoShell
)))
456 add_aka (ak
, hm
->h_name
);
458 return homehead
!= NULL
;
463 getalias (char *addrs
)
466 static char *cp
= NULL
;
474 /* Remove leading any space from the address. */
475 for (pp
= cp
; isspace ((unsigned char) *pp
); pp
++)
479 /* Find the end of the address. */
480 for (qp
= pp
; *qp
!= 0 && *qp
!= ','; qp
++)
482 /* Set cp to point to the remainder of the addresses. */
485 for (cp
= qp
, qp
--; qp
> pp
; qp
--)
487 if (isspace ((unsigned char) *qp
))
498 add_aka (struct aka
*ak
, char *pp
)
500 register struct adr
*ad
, *ld
;
502 for (ad
= ak
->ak_addr
, ld
= NULL
; ad
; ld
= ad
, ad
= ad
->ad_next
)
503 if (!strcmp (pp
, ad
->ad_text
))
506 ad
= (struct adr
*) mh_xmalloc (sizeof(*ad
));
507 ad
->ad_text
= getcpy (pp
);
508 ad
->ad_local
= strchr(pp
, '@') == NULL
&& strchr(pp
, '!') == NULL
;
520 register struct passwd
*pw
;
525 /* if the list has yet to be initialized */
526 /* zap the list, and rebuild from scratch */
533 while ((pw
= getpwent ()))
545 register struct aka
*p
;
547 p
= (struct aka
*) mh_xmalloc (sizeof(*p
));
549 p
->ak_name
= getcpy (id
);
554 akatail
->ak_next
= p
;
564 hmalloc (struct passwd
*pw
)
566 register struct home
*p
;
568 p
= (struct home
*) mh_xmalloc (sizeof(*p
));
570 p
->h_name
= getcpy (pw
->pw_name
);
571 p
->h_uid
= pw
->pw_uid
;
572 p
->h_gid
= pw
->pw_gid
;
573 p
->h_home
= getcpy (pw
->pw_dir
);
574 p
->h_shell
= getcpy (pw
->pw_shell
);
577 if (hometail
!= NULL
)
578 hometail
->h_next
= p
;
579 if (homehead
== NULL
)