1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Handle name lists, alias expansion, outof(): serve file / pipe addresses
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 /* Same name, taking care for *allnet*? */
50 static int same_name(char const *n1
, char const *n2
);
51 /* Delete the given name from a namelist */
52 static struct name
* delname(struct name
*np
, char const *name
);
53 /* Put another node onto a list of names and return the list */
54 static struct name
* put(struct name
*list
, struct name
*node
);
55 /* Grab a single name (liberal name) */
56 static char const * yankname(char const *ap
, char *wbuf
,
57 char const *separators
, int keepcomms
);
58 /* Extraction multiplexer that splits an input line to names */
59 static struct name
* extract1(char const *line
, enum gfield ntype
,
60 char const *separators
, int keepcomms
);
61 /* Recursively expand a group name. We limit the expansion to some fixed level
62 * to keep things from going haywire. Direct recursion is not expanded for
64 static struct name
* gexpand(struct name
*nlist
, struct grouphead
*gh
,
65 int metoo
, int ntype
);
67 static void _remove_grouplist(struct grouphead
*gh
);
70 same_name(char const *n1
, char const *n2
)
75 if (value("allnet") != NULL
) {
83 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
86 ret
= (asccasecmp(n1
, n2
) == 0);
92 delname(struct name
*np
, char const *name
)
96 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
97 if (same_name(p
->n_name
, name
)) {
98 if (p
->n_blink
== NULL
) {
99 if (p
->n_flink
!= NULL
)
100 p
->n_flink
->n_blink
= NULL
;
104 if (p
->n_flink
== NULL
) {
105 if (p
->n_blink
!= NULL
)
106 p
->n_blink
->n_flink
= NULL
;
109 p
->n_blink
->n_flink
= p
->n_flink
;
110 p
->n_flink
->n_blink
= p
->n_blink
;
116 put(struct name
*list
, struct name
*node
)
118 node
->n_flink
= list
;
119 node
->n_blink
= NULL
;
121 list
->n_blink
= node
;
126 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
129 char *wp
, c
, inquote
, lc
, lastsp
;
133 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
134 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
142 * Parse a full name: TODO RFC 5322
143 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
144 * - Skip entire (nested) comments
145 * - In non-quote, non-comment, join adjacent space to a single SP
146 * - Understand separators only in non-quote, non-comment context,
147 * and only if not part of a *quoted-pair* (XXX too liberal)
150 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
161 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
167 if (inquote
|| lc
== '\\') {
174 cp
= skip_comment(cp
+ 1);
182 if (strchr(separators
, c
) != NULL
)
186 lastsp
= blankchar(c
);
187 if (! lastsp
|| ! lc
)
199 extract1(char const *line
, enum gfield ntype
, char const *separators
,
202 struct name
*topp
, *np
, *t
;
207 if (line
== NULL
|| *line
== '\0')
212 nbuf
= ac_alloc(strlen(line
) + 1);
213 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
214 t
= nalloc(nbuf
, ntype
);
229 gexpand(struct name
*nlist
, struct grouphead
*gh
, int metoo
, int ntype
)
232 struct grouphead
*ngh
;
237 if (depth
> MAXEXP
) {
238 printf(tr(150, "Expanding alias to depth larger than %d\n"),
244 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
248 if (strcmp(cp
, gh
->g_name
) == 0)
250 if ((ngh
= findgroup(cp
)) != NULL
) {
251 /* For S-nail(1), the "group" may *be* the sender in
252 * that a name maps to a full address specification */
253 if (! metoo
&& ngh
->g_list
->ge_link
== NULL
&&
254 same_name(cp
, myname
))
256 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
260 np
= nalloc(cp
, ntype
|GFULL
);
262 * At this point should allow to expand
263 * to self if only person in group
265 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
267 if (! metoo
&& same_name(cp
, myname
))
270 nlist
= put(nlist
, np
);
278 _remove_grouplist(struct grouphead
*gh
)
280 struct group
*gp
, *gq
;
282 if ((gp
= gh
->g_list
) != NULL
) {
283 for (; gp
; gp
= gq
) {
292 * Allocate a single element of a name list, initialize its name field to the
293 * passed name and return it.
296 nalloc(char *str
, enum gfield ntype
)
302 np
= (struct name
*)salloc(sizeof *np
);
308 (void)addrspec_with_guts((ntype
& (GFULL
|GSKIN
|GREF
)) != 0, str
, &ag
);
309 if ((ag
.ag_n_flags
& NAME_NAME_SALLOC
) == 0) {
310 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
311 ag
.ag_skinned
= savestrbuf(ag
.ag_skinned
, ag
.ag_slen
);
313 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
314 np
->n_flags
= ag
.ag_n_flags
;
317 if (ag
.ag_ilen
== ag
.ag_slen
319 && (ag
.ag_n_flags
& NAME_IDNA
) == 0
323 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
326 if ((ag
.ag_n_flags
& NAME_IDNA
) == 0) {
333 * The domain name was IDNA and has been converted.
334 * We also have to ensure that the domain name in
335 * .n_fullname is replaced with the converted version,
336 * since MIME doesn't perform encoding of addresses.
338 size_t l
= ag
.ag_iaddr_start
,
339 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
340 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+ 1);
341 memcpy(in
.s
, str
, l
);
342 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
344 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
350 mime_fromhdr(&in
, &out
, TD_ISPR
|TD_ICONV
);
351 np
->n_fullname
= savestr(out
.s
);
354 if (ag
.ag_n_flags
& NAME_IDNA
)
357 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
358 } else if (ntype
& GREF
) { /* TODO LEGACY */
359 /* TODO Unfortunately we had to skin GREFerences i.e. the
360 * TODO surrounding angle brackets have been stripped away.
361 * TODO Necessarily since otherwise the plain address check
362 * TODO fails due to them; insert them back so that valid
363 * TODO headers will be created */
364 np
->n_fullname
= np
->n_name
= str
= salloc(ag
.ag_slen
+ 2 + 1);
366 memcpy(str
, ag
.ag_skinned
, ag
.ag_slen
);
376 ndup(struct name
*np
, enum gfield ntype
)
380 if ((ntype
& (GFULL
|GSKIN
)) && (np
->n_flags
& NAME_SKINNED
) == 0) {
381 nnp
= nalloc(np
->n_name
, ntype
);
385 nnp
= (struct name
*)salloc(sizeof *np
);
386 nnp
->n_flink
= nnp
->n_blink
= NULL
;
388 nnp
->n_flags
= (np
->n_flags
&
389 ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
391 nnp
->n_name
= savestr(np
->n_name
);
392 if (np
->n_name
== np
->n_fullname
|| (ntype
& (GFULL
|GSKIN
)) == 0)
393 nnp
->n_fullname
= nnp
->n_name
;
395 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
396 nnp
->n_fullname
= savestr(np
->n_fullname
);
403 * Concatenate the two passed name lists, return the result.
406 cat(struct name
*n1
, struct name
*n2
)
416 while (tail
->n_flink
!= NULL
)
417 tail
= tail
->n_flink
;
424 * Determine the number of undeleted elements in
425 * a name list and return it.
428 count(struct name
const*np
)
432 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
433 if ((np
->n_type
& GDEL
) == 0)
439 * Extract a list of names from a line,
440 * and make a list of names from it.
441 * Return the list or NULL if none found.
444 extract(char const *line
, enum gfield ntype
)
446 return extract1(line
, ntype
, " \t,", 0);
450 lextract(char const *line
, enum gfield ntype
)
452 return ((line
&& strpbrk(line
, ",\"\\(<|")) ?
453 extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
457 * Turn a list of names into a string of the same names.
460 detract(struct name
*np
, enum gfield ntype
)
470 comma
= ntype
& GCOMMA
;
473 if ((options
& OPT_DEBUG
) && comma
)
474 fprintf(stderr
, tr(145, "detract asked to insert commas\n"));
475 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
476 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
478 s
+= strlen(p
->n_fullname
) + 1;
488 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
489 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
491 cp
= sstpcpy(cp
, p
->n_fullname
);
492 if (comma
&& p
->n_flink
!= NULL
)
497 if (comma
&& *--cp
== ',')
504 grab_names(const char *field
, struct name
*np
, int comma
, enum gfield gflags
)
508 np
= lextract(readstr_input(field
, detract(np
, comma
)), gflags
);
509 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
510 if (is_addr_invalid(nq
, 1))
516 * Check all addresses in np and delete invalid ones.
519 checkaddrs(struct name
*np
)
523 for (n
= np
; n
!= NULL
;) {
524 if (is_addr_invalid(n
, 1)) {
526 n
->n_blink
->n_flink
= n
->n_flink
;
528 n
->n_flink
->n_blink
= n
->n_blink
;
538 * Map all of the aliased users in the invoker's mailrc
539 * file and insert them into the list.
540 * Changed after all these months of service to recursively
541 * expand names (2/14/80).
544 usermap(struct name
*names
, bool_t force_metoo
)
546 struct name
*new, *np
, *cp
;
547 struct grouphead
*gh
;
552 metoo
= (force_metoo
|| value("metoo") != NULL
);
554 assert((np
->n_type
& GDEL
) == 0); /* TODO legacy */
555 if (is_fileorpipe_addr(np
) || np
->n_name
[0] == '\\') {
561 gh
= findgroup(np
->n_name
);
564 new = gexpand(new, gh
, metoo
, np
->n_type
);
573 * Remove all of the duplicates from the passed name list by
574 * insertion sorting them, then checking for dups.
575 * Return the head of the new list.
578 elide(struct name
*names
)
580 struct name
*np
, *t
, *newn
, *x
;
584 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
585 for (newn
= np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
586 if ((names
->n_type
& GDEL
) == 0) {
600 newn
->n_flink
= NULL
;
604 while (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
605 if (t
->n_flink
== NULL
)
611 * If we ran out of t's, put the new entry after
612 * the current value of t.
615 if (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
625 * Otherwise, put the new entry in front of the
626 * current t. If at the front of the list,
627 * the new guy becomes the new head of the list.
641 * The normal case -- we are inserting into the
642 * middle of the list.
648 x
->n_blink
= t
->n_blink
;
649 t
->n_blink
->n_flink
= x
;
654 * Now the list headed up by new is sorted.
655 * Go through it and remove duplicates.
661 while (t
->n_flink
!= NULL
&&
662 asccasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
670 * Now t points to the last entry with the same name
671 * as np. Make np point beyond t.
674 np
->n_flink
= t
->n_flink
;
675 if (t
->n_flink
!= NULL
)
676 t
->n_flink
->n_blink
= np
;
683 delete_alternates(struct name
*np
)
688 np
= delname(np
, myname
);
690 for (ap
= altnames
; *ap
; ap
++)
691 np
= delname(np
, *ap
);
692 if ((xp
= lextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
694 np
= delname(np
, xp
->n_name
);
697 if ((xp
= lextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
699 np
= delname(np
, xp
->n_name
);
702 if ((xp
= extract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
704 np
= delname(np
, xp
->n_name
);
711 is_myname(char const *name
)
717 if (same_name(myname
, name
))
720 for (ap
= altnames
; *ap
; ap
++)
721 if (same_name(*ap
, name
))
723 if ((xp
= lextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
725 if (same_name(xp
->n_name
, name
))
729 if ((xp
= lextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
731 if (same_name(xp
->n_name
, name
))
735 if ((xp
= extract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
737 if (same_name(xp
->n_name
, name
))
747 * For each recipient in the passed name list with a /
748 * in the name, append the message to the end of the named file
749 * and remove him from the recipient list.
751 * Recipients whose name begins with | are piped through the given
752 * program and removed.
755 outof(struct name
*names
, FILE *fo
, struct header
*hp
, bool_t
*senderror
)
757 int pipecnt
, xcnt
, *fda
, i
;
760 FILE *fin
= NULL
, *fout
;
764 * Look through all recipients and do a quick return if no file or pipe
765 * addressee is found.
767 fda
= NULL
; /* Silence cc */
768 for (pipecnt
= xcnt
= 0, np
= names
; np
!= NULL
; np
= np
->n_flink
)
769 switch (np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
) {
770 case NAME_ADDRSPEC_ISFILE
:
773 case NAME_ADDRSPEC_ISPIPE
:
777 if (pipecnt
== 0 && xcnt
== 0)
781 * Otherwise create an array of file descriptors for each found pipe
782 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
783 * each pipe subprocess needs its very own file descriptor, and we need
785 * To make our life a bit easier let's just use the auto-reclaimed
792 fda
= (int*)salloc(sizeof(int) * pipecnt
);
793 for (i
= 0; i
< pipecnt
; ++i
)
795 if ((sh
= value("SHELL")) == NULL
)
799 for (np
= names
; np
!= NULL
;) {
800 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
807 * See if we have copied the complete message out yet.
814 /* XXX tempEdit unlink racy - block signals, at least */
815 if ((fout
= Ftemp(&tempEdit
, "Re", "w", 0600, 1))
817 perror(tr(146, "Creation of temporary image"));
821 image
= open(tempEdit
, O_RDWR
);
823 for (i
= 0; i
< pipecnt
; ++i
) {
824 int fd
= open(tempEdit
, O_RDONLY
);
832 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
837 perror(tr(147, "Creating descriptor duplicate "
838 "of temporary image"));
843 (void)fcntl(image
, F_SETFD
, FD_CLOEXEC
);
845 fprintf(fout
, "From %s %s",
846 myname
, time_current
.tc_ctime
);
848 while (i
= c
, (c
= getc(fo
)) != EOF
)
856 perror(tr(148, "Finalizing write of temporary "
863 /* If we have to serve file addressees, open reader */
864 if (xcnt
!= 0 && (fin
= Fdopen(image
, "r")) == NULL
) {
865 perror(tr(149, "Failed to open a duplicate of "
866 "the temporary image"));
867 jcantfout
: *senderror
= TRU1
;
873 /* From now on use xcnt as a counter for pipecnt */
878 * Now either copy "image" to the desired file
879 * or give it as the standard input to the desired
880 * program as appropriate.
883 if (np
->n_flags
& NAME_ADDRSPEC_ISPIPE
) {
888 sigaddset(&nset
, SIGHUP
);
889 sigaddset(&nset
, SIGINT
);
890 sigaddset(&nset
, SIGQUIT
);
891 pid
= start_command(sh
, &nset
,
892 fda
[xcnt
++], -1, "-c", np
->n_name
+ 1, NULL
);
894 fprintf(stderr
, tr(281,
895 "Message piping to <%s> failed\n"),
902 char *fname
= file_expand(np
->n_name
);
907 if ((fout
= Zopen(fname
, "a", NULL
)) == NULL
) {
908 fprintf(stderr
, tr(282,
909 "Message writing to <%s> failed: %s\n"),
910 fname
, strerror(errno
));
915 while ((i
= getc(fin
)) != EOF
)
918 fprintf(stderr
, tr(282,
919 "Message writing to <%s> failed: %s\n"),
920 fname
, tr(283, "write error"));
927 * In days of old we removed the entry from the
928 * the list; now for sake of header expansion
929 * we leave it in and mark it as deleted.
939 for (i
= 0; i
< pipecnt
; ++i
)
949 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
958 findgroup(char *name
)
960 struct grouphead
*gh
;
962 for (gh
= groups
[hash(name
)]; gh
!= NULL
; gh
= gh
->g_link
)
963 if (*gh
->g_name
== *name
&& strcmp(gh
->g_name
, name
) == 0)
969 printgroup(char *name
)
971 struct grouphead
*gh
;
974 if ((gh
= findgroup(name
)) == NULL
) {
975 fprintf(stderr
, tr(202, "\"%s\": no such alias\n"), name
);
978 printf("%s\t", gh
->g_name
);
979 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
)
980 printf(" %s", gp
->ge_name
);
985 remove_group(const char *name
)
987 struct grouphead
*gh
, *gp
= NULL
;
990 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
) {
991 if (*gh
->g_name
== *name
&& strcmp(gh
->g_name
, name
) == 0) {
992 _remove_grouplist(gh
);
995 gp
->g_link
= gh
->g_link
;