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 - 2014 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
40 #ifndef HAVE_AMALGAMATION
49 # define _SET_CLOEXEC(FD) fcntl((FD), F_SETFD, FD_CLOEXEC)
51 # define _SET_CLOEXEC(FD)
54 /* Same name, while taking care for *allnet*? */
55 static bool_t
_same_name(char const *n1
, char const *n2
);
57 /* Delete the given name from a namelist */
58 static struct name
* delname(struct name
*np
, char const *name
);
60 /* Put another node onto a list of names and return the list */
61 static struct name
* put(struct name
*list
, struct name
*node
);
63 /* Grab a single name (liberal name) */
64 static char const * yankname(char const *ap
, char *wbuf
,
65 char const *separators
, int keepcomms
);
67 /* Extraction multiplexer that splits an input line to names */
68 static struct name
* _extract1(char const *line
, enum gfield ntype
,
69 char const *separators
, bool_t keepcomms
);
71 /* Recursively expand a group name. Limit expansion to some fixed level.
72 * Direct recursion is not expanded for convenience */
73 static struct name
* _gexpand(size_t level
, struct name
*nlist
,
74 struct grouphead
*gh
, bool_t metoo
, int ntype
);
76 static void _remove_grouplist(struct grouphead
*gh
);
79 _same_name(char const *n1
, char const *n2
)
85 if (ok_blook(allnet
)) {
93 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
96 rv
= !asccasecmp(n1
, n2
);
103 delname(struct name
*np
, char const *name
)
108 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
109 if (_same_name(p
->n_name
, name
)) {
110 if (p
->n_blink
== NULL
) {
111 if (p
->n_flink
!= NULL
)
112 p
->n_flink
->n_blink
= NULL
;
116 if (p
->n_flink
== NULL
) {
117 if (p
->n_blink
!= NULL
)
118 p
->n_blink
->n_flink
= NULL
;
121 p
->n_blink
->n_flink
= p
->n_flink
;
122 p
->n_flink
->n_blink
= p
->n_blink
;
129 put(struct name
*list
, struct name
*node
)
132 node
->n_flink
= list
;
133 node
->n_blink
= NULL
;
135 list
->n_blink
= node
;
141 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
144 char *wp
, c
, inquote
, lc
, lastsp
;
149 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
150 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
157 /* Parse a full name: TODO RFC 5322
158 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
159 * - Skip entire (nested) comments
160 * - In non-quote, non-comment, join adjacent space to a single SP
161 * - Understand separators only in non-quote, non-comment context,
162 * and only if not part of a *quoted-pair* (XXX too liberal) */
164 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
175 #if 0 /* TODO when doing real RFC 5322 parsers - why have i done this? */
181 if (inquote
|| lc
== '\\') {
189 cp
= skip_comment(cp
+ 1);
197 if (strchr(separators
, c
) != NULL
)
201 lastsp
= blankchar(c
);
215 _extract1(char const *line
, enum gfield ntype
, char const *separators
,
218 struct name
*topp
, *np
, *t
;
224 if (line
== NULL
|| *line
== '\0')
229 nbuf
= ac_alloc(strlen(line
) +1);
230 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
231 t
= nalloc(nbuf
, ntype
);
246 _gexpand(size_t level
, struct name
*nlist
, struct grouphead
*gh
, bool_t metoo
,
250 struct grouphead
*ngh
;
255 if (UICMP(z
, level
++, >, MAXEXP
)) {
256 printf(tr(150, "Expanding alias to depth larger than %d\n"), MAXEXP
);
260 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
264 if (!strcmp(cp
, gh
->g_name
))
266 if ((ngh
= findgroup(cp
)) != NULL
) {
267 /* For S-nail(1), the "group" may *be* the sender in that a name maps
268 * to a full address specification */
269 if (!metoo
&& ngh
->g_list
->ge_link
== NULL
&& _same_name(cp
, myname
))
271 nlist
= _gexpand(level
, nlist
, ngh
, metoo
, ntype
);
275 np
= nalloc(cp
, ntype
| GFULL
);
276 /* At this point should allow to expand itself if only person in group */
277 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
279 if (!metoo
&& _same_name(cp
, myname
))
282 nlist
= put(nlist
, np
);
290 _remove_grouplist(struct grouphead
*gh
)
292 struct group
*gp
, *gq
;
295 if ((gp
= gh
->g_list
) != NULL
) {
296 for (; gp
; gp
= gq
) {
306 nalloc(char *str
, enum gfield ntype
)
313 np
= salloc(sizeof *np
);
319 addrspec_with_guts(((ntype
& (GFULL
| GSKIN
| GREF
)) != 0), str
, &ag
);
320 if (!(ag
.ag_n_flags
& NAME_NAME_SALLOC
)) {
321 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
322 ag
.ag_skinned
= savestrbuf(ag
.ag_skinned
, ag
.ag_slen
);
324 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
325 np
->n_flags
= ag
.ag_n_flags
;
328 if (ag
.ag_ilen
== ag
.ag_slen
330 && !(ag
.ag_n_flags
& NAME_IDNA
)
334 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
337 if (!(ag
.ag_n_flags
& NAME_IDNA
)) {
343 /* The domain name was IDNA and has been converted. We also have to
344 * ensure that the domain name in .n_fullname is replaced with the
345 * converted version, since MIME doesn't perform encoding of addrs */
346 size_t l
= ag
.ag_iaddr_start
,
347 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
348 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+1);
349 memcpy(in
.s
, str
, l
);
350 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
352 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
358 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
359 np
->n_fullname
= savestr(out
.s
);
362 if (ag
.ag_n_flags
& NAME_IDNA
)
365 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
366 } else if (ntype
& GREF
) { /* TODO LEGACY */
367 /* TODO Unfortunately we had to skin GREFerences i.e. the
368 * TODO surrounding angle brackets have been stripped away.
369 * TODO Necessarily since otherwise the plain address check
370 * TODO fails due to them; insert them back so that valid
371 * TODO headers will be created */
372 np
->n_fullname
= np
->n_name
= str
= salloc(ag
.ag_slen
+ 2 +1);
374 memcpy(str
, ag
.ag_skinned
, ag
.ag_slen
);
385 ndup(struct name
*np
, enum gfield ntype
)
390 if ((ntype
& (GFULL
| GSKIN
)) && !(np
->n_flags
& NAME_SKINNED
)) {
391 nnp
= nalloc(np
->n_name
, ntype
);
395 nnp
= salloc(sizeof *np
);
396 nnp
->n_flink
= nnp
->n_blink
= NULL
;
398 nnp
->n_flags
= (np
->n_flags
& ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
400 nnp
->n_name
= savestr(np
->n_name
);
401 if (np
->n_name
== np
->n_fullname
|| !(ntype
& (GFULL
| GSKIN
)))
402 nnp
->n_fullname
= nnp
->n_name
;
404 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
405 nnp
->n_fullname
= savestr(np
->n_fullname
);
413 cat(struct name
*n1
, struct name
*n2
)
425 while (tail
->n_flink
!= NULL
)
426 tail
= tail
->n_flink
;
436 count(struct name
const*np
)
441 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
442 if (!(np
->n_type
& GDEL
))
449 extract(char const *line
, enum gfield ntype
)
454 rv
= _extract1(line
, ntype
, " \t,", 0);
460 lextract(char const *line
, enum gfield ntype
)
465 rv
= ((line
!= NULL
&& strpbrk(line
, ",\"\\(<|"))
466 ? _extract1(line
, ntype
, ",", 1) : extract(line
, ntype
));
472 detract(struct name
*np
, enum gfield ntype
)
483 comma
= ntype
& GCOMMA
;
486 if ((options
& OPT_DEBUG
) && comma
)
487 fprintf(stderr
, tr(145, "detract asked to insert commas\n"));
488 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
489 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
491 s
+= strlen(p
->n_fullname
) +1;
501 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
502 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
504 cp
= sstpcpy(cp
, p
->n_fullname
);
505 if (comma
&& p
->n_flink
!= NULL
)
510 if (comma
&& *--cp
== ',')
518 grab_names(char const *field
, struct name
*np
, int comma
, enum gfield gflags
)
524 np
= lextract(readstr_input(field
, detract(np
, comma
)), gflags
);
525 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
526 if (is_addr_invalid(nq
, 1))
533 checkaddrs(struct name
*np
)
538 for (n
= np
; n
!= NULL
;) {
539 if (is_addr_invalid(n
, 1)) {
541 n
->n_blink
->n_flink
= n
->n_flink
;
543 n
->n_flink
->n_blink
= n
->n_blink
;
554 usermap(struct name
*names
, bool_t force_metoo
)
556 struct name
*new, *np
, *cp
;
557 struct grouphead
*gh
;
563 metoo
= (force_metoo
|| ok_blook(metoo
));
565 assert(!(np
->n_type
& GDEL
)); /* TODO legacy */
566 if (is_fileorpipe_addr(np
) || np
->n_name
[0] == '\\') {
572 gh
= findgroup(np
->n_name
);
575 new = _gexpand(0, new, gh
, metoo
, np
->n_type
);
585 elide(struct name
*names
)
587 struct name
*np
, *t
, *newn
, *x
;
594 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
595 for (np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
596 if (!(names
->n_type
& GDEL
)) {
610 newn
->n_flink
= NULL
;
616 while ((cmpres
= asccasecmp(t
->n_name
, np
->n_name
)) < 0) {
617 if (t
->n_flink
== NULL
)
622 /* If we ran out of t's, put new entry after the current value of t */
632 /* Otherwise, put the new entry in front of the current t. If at the
633 * front of the list, the new guy becomes the new head of the list */
644 /* The normal case -- we are inserting into the middle of the list */
648 x
->n_blink
= t
->n_blink
;
649 t
->n_blink
->n_flink
= x
;
653 /* Now the list headed up by new is sorted. Remove duplicates */
657 while (t
->n_flink
!= NULL
&& !asccasecmp(np
->n_name
, t
->n_flink
->n_name
))
664 /* Now t points to the last entry with the same name as np.
665 * Make np point beyond t */
666 np
->n_flink
= t
->n_flink
;
667 if (t
->n_flink
!= NULL
)
668 t
->n_flink
->n_blink
= np
;
677 delete_alternates(struct name
*np
)
683 np
= delname(np
, myname
);
685 for (ap
= altnames
; *ap
!= '\0'; ++ap
)
686 np
= delname(np
, *ap
);
688 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
690 np
= delname(np
, xp
->n_name
);
694 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
696 np
= delname(np
, xp
->n_name
);
700 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
702 np
= delname(np
, xp
->n_name
);
710 is_myname(char const *name
)
717 if (_same_name(myname
, name
))
720 for (ap
= altnames
; *ap
!= NULL
; ++ap
)
721 if (_same_name(*ap
, name
))
724 if ((xp
= lextract(ok_vlook(from
), GEXTRA
| GSKIN
)) != NULL
)
726 if (_same_name(xp
->n_name
, name
))
731 if ((xp
= lextract(ok_vlook(replyto
), GEXTRA
| GSKIN
)) != NULL
)
733 if (_same_name(xp
->n_name
, name
))
738 if ((xp
= extract(ok_vlook(sender
), GEXTRA
| GSKIN
)) != NULL
)
740 if (_same_name(xp
->n_name
, name
))
751 outof(struct name
*names
, FILE *fo
, struct header
*hp
, bool_t
*senderror
)
753 ui32_t pipecnt
, xcnt
, i
;
757 FILE *fin
= NULL
, *fout
;
761 /* Look through all recipients and do a quick return if no file or pipe
762 * addressee is found */
763 fda
= NULL
; /* Silence cc */
764 for (pipecnt
= xcnt
= 0, np
= names
; np
!= NULL
; np
= np
->n_flink
)
765 switch (np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
) {
766 case NAME_ADDRSPEC_ISFILE
:
769 case NAME_ADDRSPEC_ISPIPE
:
773 if (pipecnt
== 0 && xcnt
== 0)
776 /* Otherwise create an array of file descriptors for each found pipe
777 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
778 * each pipe subprocess needs its very own file descriptor, and we need
780 * To make our life a bit easier let's just use the auto-reclaimed
786 fda
= salloc(sizeof(int) * pipecnt
);
787 for (i
= 0; i
< pipecnt
; ++i
)
789 if ((sh
= ok_vlook(SHELL
)) == NULL
)
793 for (np
= names
; np
!= NULL
;) {
794 if (!(np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)) {
799 /* See if we have copied the complete message out yet. If not, do so */
804 if ((fout
= Ftmp(&tempEdit
, "outof",
805 OF_WRONLY
| OF_HOLDSIGS
| OF_REGISTER
, 0600)) == NULL
) {
806 perror(tr(146, "Creation of temporary image"));
810 if ((image
= open(tempEdit
, O_RDWR
| O_CLOEXEC
)) >= 0) {
812 for (i
= 0; i
< pipecnt
; ++i
) {
813 int fd
= open(tempEdit
, O_RDONLY
| O_CLOEXEC
);
824 Ftmp_release(&tempEdit
);
827 perror(tr(147, "Creating descriptor duplicate of temporary image"));
833 fprintf(fout
, "From %s %s", myname
, time_current
.tc_ctime
);
835 while (i
= c
, (c
= getc(fo
)) != EOF
)
843 perror(tr(148, "Finalizing write of temporary image"));
849 /* If we have to serve file addressees, open reader */
850 if (xcnt
!= 0 && (fin
= Fdopen(image
, "r")) == NULL
) {
852 "Failed to open a duplicate of the temporary image"));
860 /* From now on use xcnt as a counter for pipecnt */
864 /* Now either copy "image" to the desired file or give it as the standard
865 * input to the desired program as appropriate */
866 if (np
->n_flags
& NAME_ADDRSPEC_ISPIPE
) {
871 sigaddset(&nset
, SIGHUP
);
872 sigaddset(&nset
, SIGINT
);
873 sigaddset(&nset
, SIGQUIT
);
874 pid
= start_command(sh
, &nset
, fda
[xcnt
++], -1, "-c",
875 np
->n_name
+ 1, NULL
);
877 fprintf(stderr
, tr(281, "Message piping to <%s> failed\n"),
884 char c
, *fname
= file_expand(np
->n_name
);
890 if ((fout
= Zopen(fname
, "a", NULL
)) == NULL
) {
891 fprintf(stderr
, tr(282, "Message writing to <%s> failed: %s\n"),
892 fname
, strerror(errno
));
897 while ((c
= getc(fin
)) != EOF
)
900 fprintf(stderr
, tr(282, "Message writing to <%s> failed: %s\n"),
901 fname
, tr(283, "write error"));
907 /* In days of old we removed the entry from the the list; now for sake of
908 * header expansion we leave it in and mark it as deleted */
917 for (i
= 0; i
< pipecnt
; ++i
)
928 if (np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
935 FL
struct grouphead
*
936 findgroup(char *name
)
938 struct grouphead
*gh
;
941 for (gh
= groups
[hash(name
)]; gh
!= NULL
; gh
= gh
->g_link
)
942 if (*gh
->g_name
== *name
&& !strcmp(gh
->g_name
, name
))
949 printgroup(char *name
)
951 struct grouphead
*gh
;
955 if ((gh
= findgroup(name
)) == NULL
) {
956 fprintf(stderr
, tr(202, "\"%s\": no such alias\n"), name
);
960 printf("%s\t", gh
->g_name
);
961 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
)
962 printf(" %s", gp
->ge_name
);
969 remove_group(char const *name
)
972 struct grouphead
*gh
, *gp
;
977 for (gp
= NULL
, gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
) {
978 if (*gh
->g_name
== *name
&& !strcmp(gh
->g_name
, name
)) {
979 _remove_grouplist(gh
);
982 gp
->g_link
= gh
->g_link
;
999 /* vim:set fenc=utf-8:s-it-mode */