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
56 /* Grab a single name (liberal name) */
57 static char const * yankname(char const *ap
, char *wbuf
,
58 char const *separators
, int keepcomms
);
59 /* Extraction multiplexer that splits an input line to names */
60 static struct name
* extract1(char const *line
, enum gfield ntype
,
61 char const *separators
, int keepcomms
);
63 static struct name
* tailof(struct name
*name
);
64 static int same_name(char *n1
, char *n2
);
65 static struct name
* gexpand(struct name
*nlist
, struct grouphead
*gh
,
66 int metoo
, int ntype
);
67 static struct name
* put(struct name
*list
, struct name
*node
);
68 static struct name
* delname(struct name
*np
, char *name
);
71 yankname(char const *ap
, char *wbuf
, char const *separators
, int keepcomms
)
74 char *wp
, c
, inquote
, lc
, lastsp
;
78 /* Skip over intermediate list trash, as in ".org> , <xy@zz.org>" */
79 for (c
= *ap
; blankchar(c
) || c
== ','; c
= *++ap
)
87 * Parse a full name: TODO RFC 5322
88 * - Keep everything in quotes, liberal handle *quoted-pair*s therein
89 * - Skip entire (nested) comments
90 * - In non-quote, non-comment, join adjacent space to a single SP
91 * - Understand separators only in non-quote, non-comment context,
92 * and only if not part of a *quoted-pair* (XXX too liberal)
95 for (inquote
= lc
= lastsp
= 0;; lc
= c
, ++cp
) {
110 if (inquote
|| lc
== '\\') {
117 cp
= skip_comment(cp
+ 1);
125 if (strchr(separators
, c
) != NULL
)
129 lastsp
= blankchar(c
);
130 if (! lastsp
|| ! lc
)
142 extract1(char const *line
, enum gfield ntype
, char const *separators
,
145 struct name
*top
, *np
, *t
;
150 if (line
== NULL
|| *line
== '\0')
154 nbuf
= ac_alloc(strlen(line
) + 1);
155 while ((cp
= yankname(cp
, nbuf
, separators
, keepcomms
)) != NULL
) {
156 t
= nalloc(nbuf
, ntype
);
170 * Allocate a single element of a name list, initialize its name field to the
171 * passed name and return it.
174 nalloc(char *str
, enum gfield ntype
)
180 np
= (struct name
*)salloc(sizeof *np
);
186 (void)addrspec_with_guts((ntype
& (GFULL
|GSKIN
|GREF
)) != 0, str
, &ag
);
187 if ((ag
.ag_n_flags
& NAME_NAME_SALLOC
) == 0) {
188 ag
.ag_n_flags
|= NAME_NAME_SALLOC
;
189 ag
.ag_skinned
= savestrbuf(ag
.ag_skinned
, ag
.ag_slen
);
191 np
->n_fullname
= np
->n_name
= ag
.ag_skinned
;
192 np
->n_flags
= ag
.ag_n_flags
;
195 if (ag
.ag_ilen
== ag
.ag_slen
197 && (ag
.ag_n_flags
& NAME_IDNA
) == 0
201 if (ag
.ag_n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
)
204 if ((ag
.ag_n_flags
& NAME_IDNA
) == 0) {
211 * The domain name was IDNA and has been converted.
212 * We also have to ensure that the domain name in
213 * .n_fullname is replaced with the converted version,
214 * since MIME doesn't perform encoding of addresses.
216 size_t l
= ag
.ag_iaddr_start
,
217 lsuff
= ag
.ag_ilen
- ag
.ag_iaddr_aend
;
218 in
.s
= ac_alloc(l
+ ag
.ag_slen
+ lsuff
+ 1);
219 memcpy(in
.s
, str
, l
);
220 memcpy(in
.s
+ l
, ag
.ag_skinned
, ag
.ag_slen
);
222 memcpy(in
.s
+ l
, str
+ ag
.ag_iaddr_aend
, lsuff
);
228 mime_fromhdr(&in
, &out
, TD_ISPR
|TD_ICONV
);
229 np
->n_fullname
= savestr(out
.s
);
232 if (ag
.ag_n_flags
& NAME_IDNA
)
235 np
->n_flags
|= NAME_FULLNAME_SALLOC
;
236 } else if (ntype
& GREF
) { /* TODO LEGACY */
237 /* TODO Unfortunately we had to skin GREFerences i.e. the
238 * TODO surrounding angle brackets have been stripped away.
239 * TODO Necessarily since otherwise the plain address check
240 * TODO fails due to them; insert them back so that valid
241 * TODO headers will be created */
242 np
->n_fullname
= np
->n_name
= str
= salloc(ag
.ag_slen
+ 2 + 1);
244 memcpy(str
, ag
.ag_skinned
, ag
.ag_slen
);
254 ndup(struct name
*np
, enum gfield ntype
)
258 if ((ntype
& (GFULL
|GSKIN
)) && (np
->n_flags
& NAME_SKINNED
) == 0) {
259 nnp
= nalloc(np
->n_name
, ntype
);
263 nnp
= (struct name
*)salloc(sizeof *np
);
264 nnp
->n_flink
= nnp
->n_blink
= NULL
;
266 nnp
->n_flags
= (np
->n_flags
&
267 ~(NAME_NAME_SALLOC
| NAME_FULLNAME_SALLOC
)) |
269 nnp
->n_name
= savestr(np
->n_name
);
270 if (np
->n_name
== np
->n_fullname
|| (ntype
& (GFULL
|GSKIN
)) == 0)
271 nnp
->n_fullname
= nnp
->n_name
;
273 nnp
->n_flags
|= NAME_FULLNAME_SALLOC
;
274 nnp
->n_fullname
= savestr(np
->n_fullname
);
281 * Find the tail of a list and return it.
284 tailof(struct name
*name
)
291 while (np
->n_flink
!= NULL
)
297 * Extract a list of names from a line,
298 * and make a list of names from it.
299 * Return the list or NULL if none found.
302 extract(char const *line
, enum gfield ntype
)
304 return extract1(line
, ntype
, " \t,(", 0);
308 sextract(char const *line
, enum gfield ntype
)
310 if (line
&& strpbrk(line
, ",\"\\(<"))
311 return extract1(line
, ntype
, ",", 1);
313 return extract(line
, ntype
);
317 lextract(char const *line
, enum gfield ntype
)
319 return (extract1(line
, ntype
, ",", 1));
323 * Turn a list of names into a string of the same names.
326 detract(struct name
*np
, enum gfield ntype
)
333 comma
= ntype
& GCOMMA
;
338 if ((debug
|| value("debug")) && comma
)
339 fprintf(stderr
, catgets(catd
, CATSET
, 145,
340 "detract asked to insert commas\n"));
341 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
342 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
344 s
+= strlen(p
->n_fullname
) + 1;
353 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
354 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
356 cp
= sstpcpy(cp
, p
->n_fullname
);
357 if (comma
&& p
->n_flink
!= NULL
)
362 if (comma
&& *--cp
== ',')
368 * Check all addresses in np and delete invalid ones.
371 checkaddrs(struct name
*np
)
375 for (n
= np
; n
!= NULL
;) {
376 if (is_addr_invalid(n
, 1)) {
378 n
->n_blink
->n_flink
= n
->n_flink
;
380 n
->n_flink
->n_blink
= n
->n_blink
;
390 * For each recipient in the passed name list with a /
391 * in the name, append the message to the end of the named file
392 * and remove him from the recipient list.
394 * Recipients whose name begins with | are piped through the given
395 * program and removed.
398 outof(struct name
*names
, FILE *fo
, struct header
*hp
)
400 int pipecnt
, xcnt
, *fda
, i
;
404 FILE *fin
= NULL
, *fout
;
408 * Look through all recipients and do a quick return if no file or pipe
409 * addressee is found.
411 fda
= NULL
; /* Silence cc */
412 for (pipecnt
= xcnt
= 0, np
= names
; np
!= NULL
; np
= np
->n_flink
)
413 switch (np
->n_flags
& NAME_ADDRSPEC_ISFILEORPIPE
) {
414 case NAME_ADDRSPEC_ISFILE
:
417 case NAME_ADDRSPEC_ISPIPE
:
421 if (pipecnt
== 0 && xcnt
== 0)
425 * Otherwise create an array of file descriptors for each found pipe
426 * addressee to get around the dup(2)-shared-file-offset problem, i.e.,
427 * each pipe subprocess needs its very own file descriptor, and we need
429 * To make our life a bit easier let's just use the auto-reclaimed
436 fda
= (int*)salloc(sizeof(int) * pipecnt
);
437 for (i
= 0; i
< pipecnt
; ++i
)
439 if ((shell
= value("SHELL")) == NULL
)
446 for (np
= names
; np
!= NULL
;) {
447 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
454 * See if we have copied the complete message out yet.
461 if ((fout
= Ftemp(&tempEdit
, "Re", "w", 0600, 1))
463 perror(tr(146, "Creation of temporary image"));
467 image
= open(tempEdit
, O_RDWR
);
469 for (i
= 0; i
< pipecnt
; ++i
) {
470 int fd
= open(tempEdit
, O_RDONLY
);
478 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
483 perror(tr(147, "Creating descriptor duplicate "
484 "of temporary image"));
489 fcntl(image
, F_SETFD
, FD_CLOEXEC
);
491 fprintf(fout
, "From %s %s", myname
, date
);
493 while (i
= c
, (c
= getc(fo
)) != EOF
)
501 perror(tr(148, "Finalizing write of temporary "
508 /* If we have to serve file addressees, open reader */
509 if (xcnt
!= 0 && (fin
= Fdopen(image
, "r")) == NULL
) {
510 perror(tr(149, "Failed to open a duplicate of "
511 "the temporary image"));
512 jcantfout
: ++senderr
;
518 /* From now on use xcnt as a counter for pipecnt */
523 * Now either copy "image" to the desired file
524 * or give it as the standard input to the desired
525 * program as appropriate.
528 if (np
->n_flags
& NAME_ADDRSPEC_ISPIPE
) {
533 sigaddset(&nset
, SIGHUP
);
534 sigaddset(&nset
, SIGINT
);
535 sigaddset(&nset
, SIGQUIT
);
536 pid
= start_command(shell
, &nset
,
537 fda
[xcnt
++], -1, "-c", np
->n_name
+ 1, NULL
);
539 fprintf(stderr
, tr(281,
540 "Message piping to <%s> failed\n"),
547 char *fname
= file_expand(np
->n_name
);
549 fprintf(stderr
, tr(81,
550 "\"%s\": Expansion failed.\n"),
555 if ((fout
= Zopen(fname
, "a", NULL
)) == NULL
) {
556 fprintf(stderr
, tr(282,
557 "Message writing to <%s> failed: %s\n"),
558 fname
, strerror(errno
));
563 while ((i
= getc(fin
)) != EOF
)
566 fprintf(stderr
, tr(282,
567 "Message writing to <%s> failed: %s\n"),
568 fname
, tr(283, "write error"));
575 * In days of old we removed the entry from the
576 * the list; now for sake of header expansion
577 * we leave it in and mark it as deleted.
587 for (i
= 0; i
< pipecnt
; ++i
)
597 if ((np
->n_flags
& (NAME_ADDRSPEC_ISFILE
|NAME_ADDRSPEC_ISPIPE
))
606 same_name(char *n1
, char *n2
)
610 if (value("allnet") != NULL
) {
618 } while (c1
!= '\0' && c2
!= '\0' && c1
!= '@' && c2
!= '@');
621 return asccasecmp(n1
, n2
) == 0;
625 * Map all of the aliased users in the invoker's mailrc
626 * file and insert them into the list.
627 * Changed after all these months of service to recursively
628 * expand names (2/14/80).
632 usermap(struct name
*names
)
634 struct name
*new, *np
, *cp
;
635 struct grouphead
*gh
;
640 metoo
= (value("metoo") != NULL
);
642 if (np
->n_name
[0] == '\\') {
648 gh
= findgroup(np
->n_name
);
651 new = gexpand(new, gh
, metoo
, np
->n_type
);
660 * Recursively expand a group name. We limit the expansion to some
661 * fixed level to keep things from going haywire.
662 * Direct recursion is not expanded for convenience.
666 gexpand(struct name
*nlist
, struct grouphead
*gh
, int metoo
, int ntype
)
669 struct grouphead
*ngh
;
674 if (depth
> MAXEXP
) {
675 printf(catgets(catd
, CATSET
, 150,
676 "Expanding alias to depth larger than %d\n"), MAXEXP
);
680 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
684 if (strcmp(cp
, gh
->g_name
) == 0)
686 if ((ngh
= findgroup(cp
)) != NULL
) {
687 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
691 np
= nalloc(cp
, ntype
|GFULL
);
693 * At this point should allow to expand
694 * to self if only person in group
696 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
698 if (!metoo
&& same_name(cp
, myname
))
701 nlist
= put(nlist
, np
);
708 * Concatenate the two passed name lists, return the result.
711 cat(struct name
*n1
, struct name
*n2
)
726 * Unpack the name list onto a vector of strings.
727 * Return an error if the name list won't fit.
730 unpack(struct name
*np
)
734 int t
, extra
, metoo
, verbose
;
737 if ((t
= count(n
)) == 0)
738 panic(catgets(catd
, CATSET
, 151, "No names to unpack"));
740 * Compute the number of extra arguments we will need.
741 * We need at least two extra -- one for "mail" and one for
742 * the terminating 0 pointer. Additional spots may be needed
743 * to pass along -f to the host mailer.
747 metoo
= value("metoo") != NULL
;
750 verbose
= value("verbose") != NULL
;
754 top
= (char **)salloc((t
+ extra
) * sizeof *top
);
762 for (; n
!= NULL
; n
= n
->n_flink
)
763 if ((n
->n_type
& GDEL
) == 0)
770 * Remove all of the duplicates from the passed name list by
771 * insertion sorting them, then checking for dups.
772 * Return the head of the new list.
775 elide(struct name
*names
)
777 struct name
*np
, *t
, *newn
, *x
;
781 /* Throw away all deleted nodes (XXX merge with plain sort below?) */
782 for (newn
= np
= NULL
; names
!= NULL
; names
= names
->n_flink
)
783 if ((names
->n_type
& GDEL
) == 0) {
797 newn
->n_flink
= NULL
;
801 while (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
802 if (t
->n_flink
== NULL
)
808 * If we ran out of t's, put the new entry after
809 * the current value of t.
812 if (asccasecmp(t
->n_name
, np
->n_name
) < 0) {
822 * Otherwise, put the new entry in front of the
823 * current t. If at the front of the list,
824 * the new guy becomes the new head of the list.
838 * The normal case -- we are inserting into the
839 * middle of the list.
845 x
->n_blink
= t
->n_blink
;
846 t
->n_blink
->n_flink
= x
;
851 * Now the list headed up by new is sorted.
852 * Go through it and remove duplicates.
858 while (t
->n_flink
!= NULL
&&
859 asccasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
861 if (t
== np
|| t
== NULL
) {
867 * Now t points to the last entry with the same name
868 * as np. Make np point beyond t.
871 np
->n_flink
= t
->n_flink
;
872 if (t
->n_flink
!= NULL
)
873 t
->n_flink
->n_blink
= np
;
880 * Put another node onto a list of names and return
884 put(struct name
*list
, struct name
*node
)
886 node
->n_flink
= list
;
887 node
->n_blink
= NULL
;
889 list
->n_blink
= node
;
894 * Determine the number of undeleted elements in
895 * a name list and return it.
898 count(struct name
*np
)
902 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
903 if ((np
->n_type
& GDEL
) == 0)
909 * Delete the given name from a namelist.
912 delname(struct name
*np
, char *name
)
916 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
917 if (same_name(p
->n_name
, name
)) {
918 if (p
->n_blink
== NULL
) {
919 if (p
->n_flink
!= NULL
)
920 p
->n_flink
->n_blink
= NULL
;
924 if (p
->n_flink
== NULL
) {
925 if (p
->n_blink
!= NULL
)
926 p
->n_blink
->n_flink
= NULL
;
929 p
->n_blink
->n_flink
= p
->n_flink
;
930 p
->n_flink
->n_blink
= p
->n_blink
;
936 * Pretty print a name list
937 * Uncomment it if you need it.
942 prettyprint(struct name *name)
948 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
951 fprintf(stderr, "\n");
956 delete_alternates(struct name
*np
)
961 np
= delname(np
, myname
);
963 for (ap
= altnames
; *ap
; ap
++)
964 np
= delname(np
, *ap
);
965 if ((xp
= sextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
967 np
= delname(np
, xp
->n_name
);
970 if ((xp
= sextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
972 np
= delname(np
, xp
->n_name
);
975 if ((xp
= sextract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
977 np
= delname(np
, xp
->n_name
);
984 is_myname(char *name
)
989 if (same_name(myname
, name
))
992 for (ap
= altnames
; *ap
; ap
++)
993 if (same_name(*ap
, name
))
995 if ((xp
= sextract(value("from"), GEXTRA
|GSKIN
)) != NULL
)
997 if (same_name(xp
->n_name
, name
))
1001 if ((xp
= sextract(value("replyto"), GEXTRA
|GSKIN
)) != NULL
)
1003 if (same_name(xp
->n_name
, name
))
1007 if ((xp
= sextract(value("sender"), GEXTRA
|GSKIN
)) != NULL
)
1009 if (same_name(xp
->n_name
, name
))