2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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
41 * Mail -- a mail program
55 /* Same name, taking care for *allnet*? */
56 static int 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
);
59 /* Put another node onto a list of names and return the list */
60 static struct name
* put(struct name
*list
, struct name
*node
);
61 /* Grab a single name (liberal name) */
62 static char const * yankname(char const *ap
, char *wbuf
,
63 char const *separators
, int keepcomms
);
64 /* Extraction multiplexer that splits an input line to names */
65 static struct name
* extract1(char const *line
, enum gfield ntype
,
66 char const *separators
, int keepcomms
);
67 /* Recursively expand a group name. We limit the expansion to some fixed level
68 * to keep things from going haywire. Direct recursion is not expanded for
70 static struct name
* gexpand(struct name
*nlist
, struct grouphead
*gh
,
71 int metoo
, int ntype
);
74 same_name(char const *n1
, char const *n2
)
79 if (value("allnet") != NULL
) {
87 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
90 ret
= (asccasecmp(n1
, n2
) == 0);
96 delname(struct name
*np
, char const *name
)
100 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
101 if (same_name(p
->n_name
, name
)) {
102 if (p
->n_blink
== NULL
) {
103 if (p
->n_flink
!= NULL
)
104 p
->n_flink
->n_blink
= NULL
;
108 if (p
->n_flink
== NULL
) {
109 if (p
->n_blink
!= NULL
)
110 p
->n_blink
->n_flink
= NULL
;
113 p
->n_blink
->n_flink
= p
->n_flink
;
114 p
->n_flink
->n_blink
= p
->n_blink
;
120 put(struct name
*list
, struct name
*node
)
122 node
->n_flink
= list
;
123 node
->n_blink
= NULL
;
125 list
->n_blink
= node
;
130 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
133 char *wp
, c
, inquote
, lc
, lastsp
;
137 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
138 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
146 * Parse a full name: TODO RFC 5322
147 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
148 * - Skip entire (nested) comments
149 * - In non-quote, non-comment, join adjacent space to a single SP
150 * - Understand separators only in non-quote, non-comment context,
151 * and only if not part of a *quoted-pair* (XXX too liberal)
154 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
169 if (inquote
|| lc
== '\\') {
176 cp
= skip_comment(cp
+ 1);
184 if (strchr(separators
, c
) != NULL
)
188 lastsp
= blankchar(c
);
189 if (! lastsp
|| ! lc
)
201 extract1(char const *line
, enum gfield ntype
, char const *separators
,
204 struct name
*top
, *np
, *t
;
209 if (line
== NULL
|| *line
== '\0')
214 nbuf
= ac_alloc(strlen(line
) + 1);
215 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
216 t
= nalloc(nbuf
, ntype
);
231 gexpand(struct name
*nlist
, struct grouphead
*gh
, int metoo
, int ntype
)
234 struct grouphead
*ngh
;
239 if (depth
> MAXEXP
) {
240 printf(tr(150, "Expanding alias to depth larger than %d\n"),
246 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
250 if (strcmp(cp
, gh
->g_name
) == 0)
252 if ((ngh
= findgroup(cp
)) != NULL
) {
253 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
257 np
= nalloc(cp
, ntype
|GFULL
);
259 * At this point should allow to expand
260 * to self if only person in group
262 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
264 if (! metoo
&& same_name(cp
, myname
))
267 nlist
= put(nlist
, np
);
275 * Allocate a single element of a name list, initialize its name field to the
276 * passed name and return it.
279 nalloc(char *str
, enum gfield ntype
)
285 np
= (struct name
*)salloc(sizeof *np
);
291 (void)addrspec_with_guts((ntype
& (GFULL
|GSKIN
|GREF
)) != 0, str
, &ag
);
292 if ((ag
.ag_n_flags
& NAME_NAME_SALLOC
) == 0) {
293 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
294 ag
.ag_skinned
= savestrbuf(ag
.ag_skinned
, ag
.ag_slen
);
296 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
297 np
->n_flags
= ag
.ag_n_flags
;
300 if (ag
.ag_ilen
== ag
.ag_slen
302 && (ag
.ag_n_flags
& NAME_IDNA
) == 0
306 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
309 if ((ag
.ag_n_flags
& NAME_IDNA
) == 0) {
316 * The domain name was IDNA and has been converted.
317 * We also have to ensure that the domain name in
318 * .n_fullname is replaced with the converted version,
319 * since MIME doesn't perform encoding of addresses.
321 size_t l
= ag
.ag_iaddr_start
,
322 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
323 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+ 1);
324 memcpy(in
.s
, str
, l
);
325 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
327 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
333 mime_fromhdr(&in
, &out
, TD_ISPR
|TD_ICONV
);
334 np
->n_fullname
= savestr(out
.s
);
337 if (ag
.ag_n_flags
& NAME_IDNA
)
340 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
341 } else if (ntype
& GREF
) { /* TODO LEGACY */
342 /* TODO Unfortunately we had to skin GREFerences i.e. the
343 * TODO surrounding angle brackets have been stripped away.
344 * TODO Necessarily since otherwise the plain address check
345 * TODO fails due to them; insert them back so that valid
346 * TODO headers will be created */
347 np
->n_fullname
= np
->n_name
= str
= salloc(ag
.ag_slen
+ 2 + 1);
349 memcpy(str
, ag
.ag_skinned
, ag
.ag_slen
);
359 ndup(struct name
*np
, enum gfield ntype
)
363 if ((ntype
& (GFULL
|GSKIN
)) && (np
->n_flags
& NAME_SKINNED
) == 0) {
364 nnp
= nalloc(np
->n_name
, ntype
);
368 nnp
= (struct name
*)salloc(sizeof *np
);
369 nnp
->n_flink
= nnp
->n_blink
= NULL
;
371 nnp
->n_flags
= (np
->n_flags
&
372 ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
374 nnp
->n_name
= savestr(np
->n_name
);
375 if (np
->n_name
== np
->n_fullname
|| (ntype
& (GFULL
|GSKIN
)) == 0)
376 nnp
->n_fullname
= nnp
->n_name
;
378 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
379 nnp
->n_fullname
= savestr(np
->n_fullname
);
386 * Concatenate the two passed name lists, return the result.
389 cat(struct name
*n1
, struct name
*n2
)
399 while (tail
->n_flink
!= NULL
)
400 tail
= tail
->n_flink
;
407 * Determine the number of undeleted elements in
408 * a name list and return it.
411 count(struct name
const*np
)
415 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
416 if ((np
->n_type
& GDEL
) == 0)
422 * Extract a list of names from a line,
423 * and make a list of names from it.
424 * Return the list or NULL if none found.
427 extract(char const *line
, enum gfield ntype
)
429 return extract1(line
, ntype
, " \t,", 0);
433 lextract(char const *line
, enum gfield ntype
)
435 return (extract1(line
, ntype
, ",", 1));
439 * Turn a list of names into a string of the same names.
442 detract(struct name
*np
, enum gfield ntype
)
452 comma
= ntype
& GCOMMA
;
455 if ((debug
|| value("debug")) && comma
)
456 fprintf(stderr
, tr(145, "detract asked to insert commas\n"));
457 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
458 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
460 s
+= strlen(p
->n_fullname
) + 1;
470 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
471 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
473 cp
= sstpcpy(cp
, p
->n_fullname
);
474 if (comma
&& p
->n_flink
!= NULL
)
479 if (comma
&& *--cp
== ',')
486 * Check all addresses in np and delete invalid ones.
489 checkaddrs(struct name
*np
)
493 for (n
= np
; n
!= NULL
;) {
494 if (is_addr_invalid(n
, 1)) {
496 n
->n_blink
->n_flink
= n
->n_flink
;
498 n
->n_flink
->n_blink
= n
->n_blink
;
508 * Map all of the aliased users in the invoker's mailrc
509 * file and insert them into the list.
510 * Changed after all these months of service to recursively
511 * expand names (2/14/80).
514 usermap(struct name
*names
)
516 struct name
*new, *np
, *cp
;
517 struct grouphead
*gh
;
522 metoo
= (value("metoo") != NULL
);
524 if (np
->n_name
[0] == '\\') {
530 gh
= findgroup(np
->n_name
);
533 new = gexpand(new, gh
, metoo
, np
->n_type
);
542 * Remove all of the duplicates from the passed name list by
543 * insertion sorting them, then checking for dups.
544 * Return the head of the new list.
547 elide(struct name
*names
)
549 struct name
*np
, *t
, *newn
, *x
;
553 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
554 for (newn
= np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
555 if ((names
->n_type
& GDEL
) == 0) {
569 newn
->n_flink
= NULL
;
573 while (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
574 if (t
->n_flink
== NULL
)
580 * If we ran out of t's, put the new entry after
581 * the current value of t.
584 if (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
594 * Otherwise, put the new entry in front of the
595 * current t. If at the front of the list,
596 * the new guy becomes the new head of the list.
610 * The normal case -- we are inserting into the
611 * middle of the list.
617 x
->n_blink
= t
->n_blink
;
618 t
->n_blink
->n_flink
= x
;
623 * Now the list headed up by new is sorted.
624 * Go through it and remove duplicates.
630 while (t
->n_flink
!= NULL
&&
631 asccasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
633 if (t
== np
|| t
== NULL
) {
639 * Now t points to the last entry with the same name
640 * as np. Make np point beyond t.
643 np
->n_flink
= t
->n_flink
;
644 if (t
->n_flink
!= NULL
)
645 t
->n_flink
->n_blink
= np
;
652 delete_alternates(struct name
*np
)
657 np
= delname(np
, myname
);
659 for (ap
= altnames
; *ap
; ap
++)
660 np
= delname(np
, *ap
);
661 if ((xp
= lextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
663 np
= delname(np
, xp
->n_name
);
666 if ((xp
= lextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
668 np
= delname(np
, xp
->n_name
);
671 if ((xp
= extract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
673 np
= delname(np
, xp
->n_name
);
680 is_myname(char const *name
)
686 if (same_name(myname
, name
))
689 for (ap
= altnames
; *ap
; ap
++)
690 if (same_name(*ap
, name
))
692 if ((xp
= lextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
694 if (same_name(xp
->n_name
, name
))
698 if ((xp
= lextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
700 if (same_name(xp
->n_name
, name
))
704 if ((xp
= extract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
706 if (same_name(xp
->n_name
, name
))
716 * For each recipient in the passed name list with a /
717 * in the name, append the message to the end of the named file
718 * and remove him from the recipient list.
720 * Recipients whose name begins with | are piped through the given
721 * program and removed.
724 outof(struct name
*names
, FILE *fo
, struct header
*hp
)
726 int pipecnt
, xcnt
, *fda
, i
;
730 FILE *fin
= NULL
, *fout
;
734 * Look through all recipients and do a quick return if no file or pipe
735 * addressee is found.
737 fda
= NULL
; /* Silence cc */
738 for (pipecnt
= xcnt
= 0, np
= names
; np
!= NULL
; np
= np
->n_flink
)
739 switch (np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
) {
740 case NAME_ADDRSPEC_ISFILE
:
743 case NAME_ADDRSPEC_ISPIPE
:
747 if (pipecnt
== 0 && xcnt
== 0)
751 * Otherwise create an array of file descriptors for each found pipe
752 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
753 * each pipe subprocess needs its very own file descriptor, and we need
755 * To make our life a bit easier let's just use the auto-reclaimed
762 fda
= (int*)salloc(sizeof(int) * pipecnt
);
763 for (i
= 0; i
< pipecnt
; ++i
)
765 if ((shell
= value("SHELL")) == NULL
)
772 for (np
= names
; np
!= NULL
;) {
773 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
780 * See if we have copied the complete message out yet.
787 if ((fout
= Ftemp(&tempEdit
, "Re", "w", 0600, 1))
789 perror(tr(146, "Creation of temporary image"));
793 image
= open(tempEdit
, O_RDWR
);
795 for (i
= 0; i
< pipecnt
; ++i
) {
796 int fd
= open(tempEdit
, O_RDONLY
);
804 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
809 perror(tr(147, "Creating descriptor duplicate "
810 "of temporary image"));
815 fcntl(image
, F_SETFD
, FD_CLOEXEC
);
817 fprintf(fout
, "From %s %s", myname
, date
);
819 while (i
= c
, (c
= getc(fo
)) != EOF
)
827 perror(tr(148, "Finalizing write of temporary "
834 /* If we have to serve file addressees, open reader */
835 if (xcnt
!= 0 && (fin
= Fdopen(image
, "r")) == NULL
) {
836 perror(tr(149, "Failed to open a duplicate of "
837 "the temporary image"));
838 jcantfout
: ++senderr
;
844 /* From now on use xcnt as a counter for pipecnt */
849 * Now either copy "image" to the desired file
850 * or give it as the standard input to the desired
851 * program as appropriate.
854 if (np
->n_flags
& NAME_ADDRSPEC_ISPIPE
) {
859 sigaddset(&nset
, SIGHUP
);
860 sigaddset(&nset
, SIGINT
);
861 sigaddset(&nset
, SIGQUIT
);
862 pid
= start_command(shell
, &nset
,
863 fda
[xcnt
++], -1, "-c", np
->n_name
+ 1, NULL
);
865 fprintf(stderr
, tr(281,
866 "Message piping to <%s> failed\n"),
873 char *fname
= file_expand(np
->n_name
);
878 if ((fout
= Zopen(fname
, "a", NULL
)) == NULL
) {
879 fprintf(stderr
, tr(282,
880 "Message writing to <%s> failed: %s\n"),
881 fname
, strerror(errno
));
886 while ((i
= getc(fin
)) != EOF
)
889 fprintf(stderr
, tr(282,
890 "Message writing to <%s> failed: %s\n"),
891 fname
, tr(283, "write error"));
898 * In days of old we removed the entry from the
899 * the list; now for sake of header expansion
900 * we leave it in and mark it as deleted.
910 for (i
= 0; i
< pipecnt
; ++i
)
920 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))