1*4fe1ef32Schristos /* $NetBSD: send.c,v 1.39 2017/11/09 20:27:50 christos Exp $ */
288b833a7Schristos
361f28255Scgd /*
42cb5542fSderaadt * Copyright (c) 1980, 1993
52cb5542fSderaadt * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
327c81c8f3Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3488b833a7Schristos #if 0
3588b833a7Schristos static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 6/6/93";
3688b833a7Schristos #else
37*4fe1ef32Schristos __RCSID("$NetBSD: send.c,v 1.39 2017/11/09 20:27:50 christos Exp $");
3888b833a7Schristos #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
41ca13337dSchristos #include <assert.h>
42ca13337dSchristos
4361f28255Scgd #include "rcv.h"
442cb5542fSderaadt #include "extern.h"
458207b28aSchristos #ifdef MIME_SUPPORT
468207b28aSchristos #include "mime.h"
478207b28aSchristos #endif
48ca13337dSchristos #include "sig.h"
49798fbc60Schristos
50f3098750Schristos /*
51f3098750Schristos * Mail -- a mail program
52f3098750Schristos *
53f3098750Schristos * Mail to others.
54f3098750Schristos */
55f3098750Schristos
56f3098750Schristos /*
57f3098750Schristos * compare twwo name structures. Support for get_smopts.
58f3098750Schristos */
59798fbc60Schristos static int
namecmp(struct name * np1,struct name * np2)60798fbc60Schristos namecmp(struct name *np1, struct name *np2)
61798fbc60Schristos {
62798fbc60Schristos for (/*EMPTY*/; np1 && np2; np1 = np1->n_flink, np2 = np2->n_flink) {
63798fbc60Schristos if (strcmp(np1->n_name, np2->n_name) != 0)
64798fbc60Schristos return 1;
65798fbc60Schristos }
66798fbc60Schristos return np1 || np2;
67798fbc60Schristos }
68798fbc60Schristos
69f3098750Schristos /*
70f3098750Schristos * Get the sendmail options from the smopts list.
71f3098750Schristos */
72798fbc60Schristos static struct name *
get_smopts(struct name * to)73798fbc60Schristos get_smopts(struct name *to)
74798fbc60Schristos {
75798fbc60Schristos struct smopts_s *sp;
76798fbc60Schristos struct name *smargs, *np;
77798fbc60Schristos smargs = NULL;
78798fbc60Schristos for (np = to; np; np = np->n_flink) {
79798fbc60Schristos if ((sp = findsmopts(np->n_name, 0)) == NULL)
80798fbc60Schristos continue;
81798fbc60Schristos if (smargs == NULL)
82798fbc60Schristos smargs = sp->s_smopts;
83798fbc60Schristos else if (namecmp(smargs, sp->s_smopts) != 0)
84798fbc60Schristos return NULL;
85798fbc60Schristos }
86798fbc60Schristos if (smargs &&
87798fbc60Schristos smargs->n_flink == NULL &&
88798fbc60Schristos (smargs->n_name == NULL || smargs->n_name[0] == '\0'))
89798fbc60Schristos return NULL;
90798fbc60Schristos return smargs;
91798fbc60Schristos }
9261f28255Scgd
9361f28255Scgd /*
94f3098750Schristos * Output a reasonable looking status field.
9561f28255Scgd */
96f3098750Schristos static void
statusput(struct message * mp,FILE * obuf,const char * prefix)97f3098750Schristos statusput(struct message *mp, FILE *obuf, const char *prefix)
98f3098750Schristos {
99f3098750Schristos char statout[3];
100f3098750Schristos char *cp = statout;
101f3098750Schristos
102f3098750Schristos if (mp->m_flag & MREAD)
103f3098750Schristos *cp++ = 'R';
104f3098750Schristos if ((mp->m_flag & MNEW) == 0)
105f3098750Schristos *cp++ = 'O';
106f3098750Schristos *cp = 0;
107f3098750Schristos if (statout[0])
108f3098750Schristos (void)fprintf(obuf, "%sStatus: %s\n",
109f3098750Schristos prefix == NULL ? "" : prefix, statout);
110f3098750Schristos }
11161f28255Scgd
11261f28255Scgd /*
11361f28255Scgd * Send message described by the passed pointer to the
11461f28255Scgd * passed output buffer. Return -1 on error.
11561f28255Scgd * Adjust the status: field if need be.
11661f28255Scgd * If doign is given, suppress ignored header fields.
11761f28255Scgd * prefix is a string to prepend to each output line.
11861f28255Scgd */
119f3098750Schristos PUBLIC int
sendmessage(struct message * mp,FILE * obuf,struct ignoretab * doign,const char * prefix,struct mime_info * mip)1208207b28aSchristos sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
1218207b28aSchristos const char *prefix, struct mime_info *mip)
12261f28255Scgd {
123ca286310Schristos off_t len;
1247c81c8f3Slukem FILE *ibuf;
12561f28255Scgd char line[LINESIZE];
126f3098750Schristos int isheadflag, infld, ignoring, dostat, firstline;
127d727506fSchristos char *cp;
128f3098750Schristos size_t prefixlen;
1294556f89aSchristos size_t linelen;
130ca13337dSchristos int rval;
131f3098750Schristos
132f3098750Schristos ignoring = 0;
133f3098750Schristos prefixlen = 0;
134ca13337dSchristos rval = -1;
13561f28255Scgd
13661f28255Scgd /*
13761f28255Scgd * Compute the prefix string, without trailing whitespace
13861f28255Scgd */
139ab850155Swiz if (prefix != NULL) {
140ece0fd5cSchristos const char *dp, *dp2 = NULL;
141ece0fd5cSchristos for (dp = prefix; *dp; dp++)
142d727506fSchristos if (!is_WSP(*dp))
143ece0fd5cSchristos dp2 = dp;
144ece0fd5cSchristos prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
14561f28255Scgd }
14661f28255Scgd ibuf = setinput(mp);
1474e972651Swiz len = mp->m_size;
1484e972651Swiz isheadflag = 1;
14961f28255Scgd dostat = doign == 0 || !isign("status", doign);
15061f28255Scgd infld = 0;
15161f28255Scgd firstline = 1;
152ca13337dSchristos
153ca13337dSchristos sig_check();
15461f28255Scgd /*
15561f28255Scgd * Process headers first
15661f28255Scgd */
1578207b28aSchristos #ifdef MIME_SUPPORT
1588207b28aSchristos if (mip)
1598207b28aSchristos obuf = mime_decode_header(mip);
1608207b28aSchristos #endif
1614e972651Swiz while (len > 0 && isheadflag) {
162ca13337dSchristos sig_check();
163ca13337dSchristos if (fgets(line, (int)sizeof(line), ibuf) == NULL)
16461f28255Scgd break;
1654556f89aSchristos len -= linelen = strlen(line);
16661f28255Scgd if (firstline) {
16761f28255Scgd /*
16861f28255Scgd * First line is the From line, so no headers
16961f28255Scgd * there to worry about
17061f28255Scgd */
17161f28255Scgd firstline = 0;
1724556f89aSchristos ignoring = doign == ignoreall || doign == bouncetab;
1738207b28aSchristos #ifdef MIME_SUPPORT
1748207b28aSchristos /* XXX - ignore multipart boundary lines! */
1758207b28aSchristos if (line[0] == '-' && line[1] == '-')
1768207b28aSchristos ignoring = 1;
1778207b28aSchristos #endif
17861f28255Scgd } else if (line[0] == '\n') {
17961f28255Scgd /*
18061f28255Scgd * If line is blank, we've reached end of
18161f28255Scgd * headers, so force out status: field
18261f28255Scgd * and note that we are no longer in header
18361f28255Scgd * fields
18461f28255Scgd */
18561f28255Scgd if (dostat) {
18661f28255Scgd statusput(mp, obuf, prefix);
18761f28255Scgd dostat = 0;
18861f28255Scgd }
1894e972651Swiz isheadflag = 0;
19061f28255Scgd ignoring = doign == ignoreall;
191d727506fSchristos } else if (infld && is_WSP(line[0])) {
19261f28255Scgd /*
19361f28255Scgd * If this line is a continuation (via space or tab)
19461f28255Scgd * of a previous header field, just echo it
19561f28255Scgd * (unless the field should be ignored).
19661f28255Scgd * In other words, nothing to do.
19761f28255Scgd */
19861f28255Scgd } else {
19961f28255Scgd /*
20061f28255Scgd * Pick up the header field if we have one.
20161f28255Scgd */
202d727506fSchristos char *save_cp;
203d727506fSchristos for (cp = line; *cp && *cp != ':' && !is_WSP(*cp); cp++)
204f3098750Schristos continue;
205d727506fSchristos save_cp = cp;
206d727506fSchristos cp = skip_WSP(cp);
207d727506fSchristos if (*cp++ != ':') {
20861f28255Scgd /*
20961f28255Scgd * Not a header line, force out status:
21061f28255Scgd * This happens in uucp style mail where
21161f28255Scgd * there are no headers at all.
21261f28255Scgd */
21361f28255Scgd if (dostat) {
21461f28255Scgd statusput(mp, obuf, prefix);
21561f28255Scgd dostat = 0;
21661f28255Scgd }
21761f28255Scgd if (doign != ignoreall)
21861f28255Scgd /* add blank line */
21961f28255Scgd (void)putc('\n', obuf);
2204e972651Swiz isheadflag = 0;
22161f28255Scgd ignoring = 0;
22261f28255Scgd } else {
22361f28255Scgd /*
22461f28255Scgd * If it is an ignored field and
22561f28255Scgd * we care about such things, skip it.
22661f28255Scgd */
227d727506fSchristos int save_c;
228d727506fSchristos save_c = *save_cp;
229d727506fSchristos *save_cp = 0; /* temporarily null terminate */
23061f28255Scgd if (doign && isign(line, doign))
23161f28255Scgd ignoring = 1;
23261f28255Scgd else if ((line[0] == 's' || line[0] == 'S') &&
23361f28255Scgd strcasecmp(line, "status") == 0) {
23461f28255Scgd /*
23561f28255Scgd * If the field is "status," go compute
23661f28255Scgd * and print the real Status: field
23761f28255Scgd */
23861f28255Scgd if (dostat) {
23961f28255Scgd statusput(mp, obuf, prefix);
24061f28255Scgd dostat = 0;
24161f28255Scgd }
24261f28255Scgd ignoring = 1;
24361f28255Scgd } else {
24461f28255Scgd ignoring = 0;
24561f28255Scgd }
24661f28255Scgd infld = 1;
2474556f89aSchristos *save_cp = save_c; /* restore the line */
24861f28255Scgd }
24961f28255Scgd }
25061f28255Scgd if (!ignoring) {
25161f28255Scgd /*
25261f28255Scgd * Strip trailing whitespace from prefix
25361f28255Scgd * if line is blank.
25461f28255Scgd */
255ab850155Swiz if (prefix != NULL) {
2564556f89aSchristos if (linelen > 1)
257ca286310Schristos (void)fputs(prefix, obuf);
25861f28255Scgd else
2594556f89aSchristos (void)fwrite(prefix, sizeof(*prefix),
26061f28255Scgd prefixlen, obuf);
261f670fa10Sross }
2624556f89aSchristos (void)fwrite(line, sizeof(*line), linelen, obuf);
26361f28255Scgd if (ferror(obuf))
264ca13337dSchristos goto out;
26561f28255Scgd }
26661f28255Scgd }
267ca13337dSchristos sig_check();
268ca13337dSchristos
26961f28255Scgd /*
27061f28255Scgd * Copy out message body
27161f28255Scgd */
2728207b28aSchristos #ifdef MIME_SUPPORT
2738207b28aSchristos if (mip) {
2748207b28aSchristos obuf = mime_decode_body(mip);
275ca13337dSchristos sig_check();
276ca13337dSchristos if (obuf == NULL) { /* XXX - early out */
277ca13337dSchristos rval = 0;
278ca13337dSchristos goto out;
279ca13337dSchristos }
2808207b28aSchristos }
2818207b28aSchristos #endif
28261f28255Scgd if (doign == ignoreall)
2834e972651Swiz len--; /* skip final blank line */
284f3098750Schristos
2854556f89aSchristos linelen = 0; /* needed for in case len == 0 */
286ca13337dSchristos if (prefix != NULL) {
2874e972651Swiz while (len > 0) {
288ca13337dSchristos sig_check();
289ca13337dSchristos if (fgets(line, (int)sizeof(line), ibuf) == NULL) {
2904556f89aSchristos linelen = 0;
29161f28255Scgd break;
29261f28255Scgd }
2934556f89aSchristos len -= linelen = strlen(line);
29461f28255Scgd /*
29561f28255Scgd * Strip trailing whitespace from prefix
29661f28255Scgd * if line is blank.
29761f28255Scgd */
2984556f89aSchristos if (linelen > 1)
299ca286310Schristos (void)fputs(prefix, obuf);
30061f28255Scgd else
3014556f89aSchristos (void)fwrite(prefix, sizeof(*prefix),
30261f28255Scgd prefixlen, obuf);
3032283d346Schristos (void)fwrite(line, sizeof(*line), linelen, obuf);
30461f28255Scgd if (ferror(obuf))
305ca13337dSchristos goto out;
30661f28255Scgd }
307ca13337dSchristos }
308ca13337dSchristos else {
3094e972651Swiz while (len > 0) {
310ca13337dSchristos sig_check();
3111742612dSlukem linelen = (size_t)len < sizeof(line) ? (size_t)len : sizeof(line);
312ca13337dSchristos if ((linelen = fread(line, sizeof(*line), linelen, ibuf)) == 0) {
31361f28255Scgd break;
314ca13337dSchristos }
3154556f89aSchristos len -= linelen;
3164556f89aSchristos if (fwrite(line, sizeof(*line), linelen, obuf) != linelen)
317ca13337dSchristos goto out;
31861f28255Scgd }
319ca13337dSchristos }
320ca13337dSchristos sig_check();
3214556f89aSchristos if (doign == ignoreall && linelen > 0 && line[linelen - 1] != '\n') {
3224556f89aSchristos int c;
323ca13337dSchristos
32461f28255Scgd /* no final blank line */
32561f28255Scgd if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
326ca13337dSchristos goto out;
3274556f89aSchristos }
328ca13337dSchristos rval = 0;
329ca13337dSchristos out:
330ca13337dSchristos sig_check();
331ca13337dSchristos return rval;
33261f28255Scgd }
33361f28255Scgd
33461f28255Scgd /*
335f3098750Schristos * Fix the header by glopping all of the expanded names from
336f3098750Schristos * the distribution list into the appropriate fields.
33761f28255Scgd */
338f3098750Schristos static void
fixhead(struct header * hp,struct name * tolist)339f3098750Schristos fixhead(struct header *hp, struct name *tolist)
34061f28255Scgd {
341f3098750Schristos struct name *np;
34261f28255Scgd
343f3098750Schristos hp->h_to = NULL;
344f3098750Schristos hp->h_cc = NULL;
345f3098750Schristos hp->h_bcc = NULL;
346f3098750Schristos for (np = tolist; np != NULL; np = np->n_flink) {
347f3098750Schristos if (np->n_type & GDEL)
348f3098750Schristos continue; /* Don't copy deleted addresses to the header */
349f3098750Schristos if ((np->n_type & GMASK) == GTO)
350f3098750Schristos hp->h_to =
351f3098750Schristos cat(hp->h_to, nalloc(np->n_name, np->n_type));
352f3098750Schristos else if ((np->n_type & GMASK) == GCC)
353f3098750Schristos hp->h_cc =
354f3098750Schristos cat(hp->h_cc, nalloc(np->n_name, np->n_type));
355f3098750Schristos else if ((np->n_type & GMASK) == GBCC)
356f3098750Schristos hp->h_bcc =
357f3098750Schristos cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
358f3098750Schristos }
35961f28255Scgd }
36061f28255Scgd
36161f28255Scgd /*
362f3098750Schristos * Format the given header line to not exceed 72 characters.
36361f28255Scgd */
364f3098750Schristos static void
fmt(const char * str,struct name * np,FILE * fo,size_t comma)365ca13337dSchristos fmt(const char *str, struct name *np, FILE *fo, size_t comma)
366f3098750Schristos {
367ca13337dSchristos size_t col;
368ca13337dSchristos size_t len;
369f3098750Schristos
370f3098750Schristos comma = comma ? 1 : 0;
371f3098750Schristos col = strlen(str);
372f3098750Schristos if (col)
373f3098750Schristos (void)fputs(str, fo);
374d727506fSchristos for (/*EMPTY*/; np != NULL; np = np->n_flink) {
375f3098750Schristos if (np->n_flink == NULL)
376f3098750Schristos comma = 0;
377f3098750Schristos len = strlen(np->n_name);
378f3098750Schristos col++; /* for the space */
379f3098750Schristos if (col + len + comma > 72 && col > 4) {
380f3098750Schristos (void)fputs("\n ", fo);
381f3098750Schristos col = 4;
382f3098750Schristos } else
383f3098750Schristos (void)putc(' ', fo);
384f3098750Schristos (void)fputs(np->n_name, fo);
385f3098750Schristos if (comma)
386f3098750Schristos (void)putc(',', fo);
387f3098750Schristos col += len + comma;
388f3098750Schristos }
389f3098750Schristos (void)putc('\n', fo);
390f3098750Schristos }
391f3098750Schristos
392f3098750Schristos /*
393ca13337dSchristos * Formatting for extra_header lines: try to wrap them before 72
394ca13337dSchristos * characters.
395ca13337dSchristos *
396ca13337dSchristos * XXX - should this allow for quoting?
397ca13337dSchristos */
398ca13337dSchristos static void
fmt2(const char * str,FILE * fo)399ca13337dSchristos fmt2(const char *str, FILE *fo)
400ca13337dSchristos {
401ca13337dSchristos const char *p;
402ca13337dSchristos size_t col;
403ca13337dSchristos
404ca13337dSchristos col = 0;
405ca13337dSchristos p = strchr(str, ':');
406ca13337dSchristos assert(p != NULL); /* this is a coding error */
407ca13337dSchristos
408ca13337dSchristos while (str <= p) { /* output the header name */
409ca13337dSchristos (void)putc(*str, fo);
410ca13337dSchristos str++;
411ca13337dSchristos col++;
412ca13337dSchristos }
413ca13337dSchristos assert(is_WSP(*str)); /* this is a coding error */
414ca13337dSchristos
415ca13337dSchristos (void)putc(' ', fo); /* output a single space after the ':' */
416ca13337dSchristos str = skip_WSP(str);
417ca13337dSchristos
418ca13337dSchristos while (*str != '\0') {
419ca13337dSchristos if (p <= str) {
420ca13337dSchristos p = strpbrk(str, " \t");
421ca13337dSchristos if (p == NULL)
422ca13337dSchristos p = str + strlen(str);
423ca13337dSchristos if (col + (p - str) > 72 && col > 4) {
424ca13337dSchristos (void)fputs("\n ", fo);
425ca13337dSchristos col = 4;
426ca13337dSchristos str = skip_WSP(str);
427ca13337dSchristos continue;
428ca13337dSchristos }
429ca13337dSchristos }
430ca13337dSchristos (void)putc(*str, fo);
431ca13337dSchristos str++;
432ca13337dSchristos col++;
433ca13337dSchristos }
434ca13337dSchristos (void)putc('\n', fo);
435ca13337dSchristos }
436ca13337dSchristos
437ca13337dSchristos /*
438f3098750Schristos * Dump the to, subject, cc header on the
439f3098750Schristos * passed file buffer.
440f3098750Schristos */
441f3098750Schristos PUBLIC int
puthead(struct header * hp,FILE * fo,int w)442f3098750Schristos puthead(struct header *hp, FILE *fo, int w)
443f3098750Schristos {
444ca13337dSchristos struct name *np;
445f3098750Schristos int gotcha;
446f3098750Schristos
447f3098750Schristos gotcha = 0;
448f3098750Schristos if (hp->h_to != NULL && w & GTO)
449f3098750Schristos fmt("To:", hp->h_to, fo, w & GCOMMA), gotcha++;
450f3098750Schristos if (hp->h_subject != NULL && w & GSUBJECT)
451f3098750Schristos (void)fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
452f3098750Schristos if (hp->h_cc != NULL && w & GCC)
453f3098750Schristos fmt("Cc:", hp->h_cc, fo, w & GCOMMA), gotcha++;
454f3098750Schristos if (hp->h_bcc != NULL && w & GBCC)
455f3098750Schristos fmt("Bcc:", hp->h_bcc, fo, w & GCOMMA), gotcha++;
456f3098750Schristos if (hp->h_in_reply_to != NULL && w & GMISC)
457f3098750Schristos (void)fprintf(fo, "In-Reply-To: %s\n", hp->h_in_reply_to), gotcha++;
458f3098750Schristos if (hp->h_references != NULL && w & GMISC)
459f3098750Schristos fmt("References:", hp->h_references, fo, w & GCOMMA), gotcha++;
460ca13337dSchristos if (hp->h_extra != NULL && w & GMISC) {
461ca13337dSchristos for (np = hp->h_extra; np != NULL; np = np->n_flink)
462ca13337dSchristos fmt2(np->n_name, fo);
463ca13337dSchristos gotcha++;
464ca13337dSchristos }
465ca13337dSchristos if (hp->h_smopts != NULL && w & GSMOPTS)
466ca13337dSchristos (void)fprintf(fo, "(sendmail options: %s)\n", detract(hp->h_smopts, GSMOPTS)), gotcha++;
4678207b28aSchristos #ifdef MIME_SUPPORT
468f3098750Schristos if (w & GMIME && (hp->h_attach || value(ENAME_MIME_ENCODE_MSG)))
469f3098750Schristos mime_putheader(fo, hp), gotcha++;
470f3098750Schristos #endif
471f3098750Schristos if (gotcha && w & GNL)
472f3098750Schristos (void)putc('\n', fo);
473f3098750Schristos return 0;
474f3098750Schristos }
475f3098750Schristos
476f3098750Schristos /*
477f3098750Schristos * Prepend a header in front of the collected stuff
478f3098750Schristos * and return the new file.
479f3098750Schristos */
480f3098750Schristos static FILE *
infix(struct header * hp,FILE * fi)481f3098750Schristos infix(struct header *hp, FILE *fi)
482f3098750Schristos {
483f3098750Schristos FILE *nfo, *nfi;
484f3098750Schristos int c, fd;
485f3098750Schristos char tempname[PATHSIZE];
486f3098750Schristos
487f3098750Schristos (void)snprintf(tempname, sizeof(tempname),
488f3098750Schristos "%s/mail.RsXXXXXXXXXX", tmpdir);
489f3098750Schristos if ((fd = mkstemp(tempname)) == -1 ||
490*4fe1ef32Schristos (nfo = Fdopen(fd, "wef")) == NULL) {
491f3098750Schristos if (fd != -1)
492f3098750Schristos (void)close(fd);
493f3098750Schristos warn("%s", tempname);
494f3098750Schristos return fi;
495f3098750Schristos }
496*4fe1ef32Schristos if ((nfi = Fopen(tempname, "ref")) == NULL) {
497f3098750Schristos warn("%s", tempname);
498f3098750Schristos (void)Fclose(nfo);
499f3098750Schristos (void)rm(tempname);
500f3098750Schristos return fi;
501f3098750Schristos }
502f3098750Schristos (void)rm(tempname);
503ca13337dSchristos (void)puthead(hp, nfo,
504ca13337dSchristos GTO | GSUBJECT | GCC | GBCC | GMISC | GNL | GCOMMA
505f3098750Schristos #ifdef MIME_SUPPORT
506ca13337dSchristos | GMIME
5078207b28aSchristos #endif
508ca13337dSchristos );
50961f28255Scgd
510f3098750Schristos c = getc(fi);
511f3098750Schristos while (c != EOF) {
512f3098750Schristos (void)putc(c, nfo);
513f3098750Schristos c = getc(fi);
514f3098750Schristos }
515f3098750Schristos if (ferror(fi)) {
516f3098750Schristos warn("read");
517f3098750Schristos rewind(fi);
518f3098750Schristos return fi;
519f3098750Schristos }
520f3098750Schristos (void)fflush(nfo);
521f3098750Schristos if (ferror(nfo)) {
522f3098750Schristos warn("%s", tempname);
523f3098750Schristos (void)Fclose(nfo);
524f3098750Schristos (void)Fclose(nfi);
525f3098750Schristos rewind(fi);
526f3098750Schristos return fi;
527f3098750Schristos }
528f3098750Schristos (void)Fclose(nfo);
529f3098750Schristos (void)Fclose(fi);
530f3098750Schristos rewind(nfi);
531f3098750Schristos return nfi;
53261f28255Scgd }
53361f28255Scgd
53461f28255Scgd /*
535f3098750Schristos * Save the outgoing mail on the passed file.
536349d9781Schristos *
537349d9781Schristos * Take care not to save a valid headline or the mbox file will be
538349d9781Schristos * broken. Prefix valid headlines with '>'.
539349d9781Schristos *
540349d9781Schristos * Note: most servers prefix any line starting with "From " in the
541349d9781Schristos * body of the message. We are more restrictive to avoid header/body
542349d9781Schristos * issues.
54361f28255Scgd */
544f3098750Schristos /*ARGSUSED*/
545f3098750Schristos static int
savemail(const char name[],FILE * fi)546f3098750Schristos savemail(const char name[], FILE *fi)
54761f28255Scgd {
548f3098750Schristos FILE *fo;
549f3098750Schristos time_t now;
550f3098750Schristos mode_t m;
551349d9781Schristos char *line;
552349d9781Schristos size_t linelen;
553349d9781Schristos int afterblank;
55461f28255Scgd
555f3098750Schristos m = umask(077);
556*4fe1ef32Schristos fo = Fopen(name, "aef");
557f3098750Schristos (void)umask(m);
558f3098750Schristos if (fo == NULL) {
559f3098750Schristos warn("%s", name);
560f3098750Schristos return -1;
561f3098750Schristos }
562f3098750Schristos (void)time(&now);
563f3098750Schristos (void)fprintf(fo, "From %s %s", myname, ctime(&now));
564349d9781Schristos afterblank = 0;
565349d9781Schristos while ((line = fgetln(fi, &linelen)) != NULL) {
566349d9781Schristos char c, *cp;
567349d9781Schristos cp = line + linelen - 1;
568349d9781Schristos if (afterblank &&
569349d9781Schristos linelen > sizeof("From . Aaa Aaa O0 00:00 0000") &&
570349d9781Schristos line[0] == 'F' &&
571349d9781Schristos line[1] == 'r' &&
572349d9781Schristos line[2] == 'o' &&
573349d9781Schristos line[3] == 'm' &&
574349d9781Schristos line[4] == ' ' &&
575349d9781Schristos *cp == '\n') {
576349d9781Schristos c = *cp;
577349d9781Schristos *cp = '\0';
578349d9781Schristos if (ishead(line))
579349d9781Schristos (void)fputc('>', fo);
580349d9781Schristos *cp = c;
581349d9781Schristos }
582349d9781Schristos (void)fwrite(line, 1, linelen, fo);
583349d9781Schristos afterblank = linelen == 1 && line[0] == '\n';
584349d9781Schristos }
585f3098750Schristos (void)putc('\n', fo);
586f3098750Schristos (void)fflush(fo);
587f3098750Schristos if (ferror(fo))
588f3098750Schristos warn("%s", name);
589f3098750Schristos (void)Fclose(fo);
590f3098750Schristos rewind(fi);
591f3098750Schristos return 0;
59261f28255Scgd }
59361f28255Scgd
59461f28255Scgd /*
5954556f89aSchristos * Mail a message that is already prepared in a file.
5964556f89aSchristos */
5974556f89aSchristos PUBLIC void
mail2(FILE * mtf,const char ** namelist)5984556f89aSchristos mail2(FILE *mtf, const char **namelist)
5994556f89aSchristos {
6004556f89aSchristos int pid;
6014556f89aSchristos const char *cp;
6024556f89aSchristos
6034556f89aSchristos if (debug) {
6044556f89aSchristos const char **t;
6054556f89aSchristos
6064556f89aSchristos (void)printf("Sendmail arguments:");
6074556f89aSchristos for (t = namelist; *t != NULL; t++)
6084556f89aSchristos (void)printf(" \"%s\"", *t);
6094556f89aSchristos (void)printf("\n");
6104556f89aSchristos return;
6114556f89aSchristos }
6124556f89aSchristos if ((cp = value(ENAME_RECORD)) != NULL)
6134556f89aSchristos (void)savemail(expand(cp), mtf);
6144556f89aSchristos /*
6154556f89aSchristos * Fork, set up the temporary mail file as standard
6164556f89aSchristos * input for "mail", and exec with the user list we generated
6174556f89aSchristos * far above.
6184556f89aSchristos */
6194556f89aSchristos pid = fork();
6204556f89aSchristos switch (pid) {
6214556f89aSchristos case -1:
6224556f89aSchristos warn("fork");
6234556f89aSchristos savedeadletter(mtf);
6244556f89aSchristos return;
6254556f89aSchristos case 0:
6264556f89aSchristos {
6274556f89aSchristos sigset_t nset;
6284556f89aSchristos (void)sigemptyset(&nset);
6294556f89aSchristos (void)sigaddset(&nset, SIGHUP);
6304556f89aSchristos (void)sigaddset(&nset, SIGINT);
6314556f89aSchristos (void)sigaddset(&nset, SIGQUIT);
6324556f89aSchristos (void)sigaddset(&nset, SIGTSTP);
6334556f89aSchristos (void)sigaddset(&nset, SIGTTIN);
6344556f89aSchristos (void)sigaddset(&nset, SIGTTOU);
6354556f89aSchristos prepare_child(&nset, fileno(mtf), -1);
6364556f89aSchristos if ((cp = value(ENAME_SENDMAIL)) != NULL)
6374556f89aSchristos cp = expand(cp);
6384556f89aSchristos else
6394556f89aSchristos cp = _PATH_SENDMAIL;
6404556f89aSchristos (void)execv(cp, (char *const *)__UNCONST(namelist));
6414556f89aSchristos warn("%s", cp);
6424556f89aSchristos _exit(1);
6434556f89aSchristos break; /* Appease GCC */
6444556f89aSchristos }
6454556f89aSchristos default:
6464556f89aSchristos if (value(ENAME_VERBOSE) != NULL)
6474556f89aSchristos (void)wait_child(pid);
6484556f89aSchristos else
6494556f89aSchristos free_child(pid);
6504556f89aSchristos break;
6514556f89aSchristos }
6524556f89aSchristos }
6534556f89aSchristos
654ca13337dSchristos static struct name *
ncopy(struct name * np)655ca13337dSchristos ncopy(struct name *np)
656ca13337dSchristos {
657ca13337dSchristos struct name *rv;
658ec30dc68She struct name *lp;
659ca13337dSchristos struct name *tp;
660ca13337dSchristos
661ec30dc68She lp = NULL; /* XXX gcc -Wuninitialized sh3 */
662ca13337dSchristos rv = NULL;
663ca13337dSchristos for (/*EMPTY*/; np; np = np->n_flink) {
664ca13337dSchristos tp = nalloc(np->n_name, np->n_type);
665ca13337dSchristos if (rv == NULL)
666ca13337dSchristos rv = tp;
667ca13337dSchristos else {
668ca13337dSchristos lp->n_flink = tp;
669ca13337dSchristos tp->n_blink = lp;
670ca13337dSchristos }
671ca13337dSchristos lp = tp;
672ca13337dSchristos }
673ca13337dSchristos return rv;
674ca13337dSchristos }
675ca13337dSchristos
6764556f89aSchristos /*
67761f28255Scgd * Mail a message on standard input to the people indicated
67861f28255Scgd * in the passed header. (Internal interface).
67961f28255Scgd */
680f3098750Schristos PUBLIC void
mail1(struct header * hp,int printheaders)681b127ccccSwiz mail1(struct header *hp, int printheaders)
68261f28255Scgd {
683ece0fd5cSchristos const char **namelist;
68461f28255Scgd struct name *to;
68561f28255Scgd FILE *mtf;
68661f28255Scgd
68761f28255Scgd /*
68861f28255Scgd * Collect user's mail from standard input.
68961f28255Scgd * Get the result as mtf.
69061f28255Scgd */
69161f28255Scgd if ((mtf = collect(hp, printheaders)) == NULL)
69261f28255Scgd return;
693349d9781Schristos
694ca13337dSchristos /*
695ca13337dSchristos * Grab any extra header lines. Do this after collect() so
696ca13337dSchristos * that we can add header lines while collecting.
697ca13337dSchristos */
698ca13337dSchristos hp->h_extra = ncopy(extra_headers);
699ca13337dSchristos
700f3098750Schristos if (value(ENAME_INTERACTIVE) != NULL) {
701f3098750Schristos if (value(ENAME_ASKCC) != NULL || value(ENAME_ASKBCC) != NULL) {
702f3098750Schristos if (value(ENAME_ASKCC) != NULL)
703ca286310Schristos (void)grabh(hp, GCC);
704f3098750Schristos if (value(ENAME_ASKBCC) != NULL)
705ca286310Schristos (void)grabh(hp, GBCC);
70610a20d06Sjtc } else {
707ca286310Schristos (void)printf("EOT\n");
70861f28255Scgd (void)fflush(stdout);
70961f28255Scgd }
710f670fa10Sross }
711f670fa10Sross if (fsize(mtf) == 0) {
712f3098750Schristos if (value(ENAME_DONTSENDEMPTY) != NULL)
713253750edSchristos goto out;
714ab850155Swiz if (hp->h_subject == NULL)
715ca286310Schristos (void)printf("No message, no subject; hope that's ok\n");
71661f28255Scgd else
717ca286310Schristos (void)printf("Null message body; hope that's ok\n");
718f670fa10Sross }
71961f28255Scgd /*
72061f28255Scgd * Now, take the user names from the combined
72161f28255Scgd * to and cc lists and do all the alias
72261f28255Scgd * processing.
72361f28255Scgd */
72461f28255Scgd senderr = 0;
72561f28255Scgd to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
726cb6786d4Swiz if (to == NULL) {
727ca286310Schristos (void)printf("No recipients specified\n");
72861f28255Scgd senderr++;
72961f28255Scgd }
7308207b28aSchristos #ifdef MIME_SUPPORT
7318207b28aSchristos /*
7328207b28aSchristos * If there are attachments, repackage the mail as a
7338207b28aSchristos * multi-part MIME message.
7348207b28aSchristos */
7358207b28aSchristos if (hp->h_attach || value(ENAME_MIME_ENCODE_MSG))
7368207b28aSchristos mtf = mime_encode(mtf, hp);
7378207b28aSchristos #endif
73861f28255Scgd /*
73961f28255Scgd * Look through the recipient list for names with /'s
74061f28255Scgd * in them which we write to as files directly.
74161f28255Scgd */
74261f28255Scgd to = outof(to, mtf, hp);
74361f28255Scgd if (senderr)
74461f28255Scgd savedeadletter(mtf);
74561f28255Scgd to = elide(to);
74661f28255Scgd if (count(to) == 0)
74761f28255Scgd goto out;
74861f28255Scgd fixhead(hp, to);
74961f28255Scgd if ((mtf = infix(hp, mtf)) == NULL) {
750ca286310Schristos (void)fprintf(stderr, ". . . message lost, sorry.\n");
75161f28255Scgd return;
75261f28255Scgd }
753798fbc60Schristos if (hp->h_smopts == NULL) {
754798fbc60Schristos hp->h_smopts = get_smopts(to);
755798fbc60Schristos if (hp->h_smopts != NULL &&
756798fbc60Schristos hp->h_smopts->n_name[0] != '\0' &&
757798fbc60Schristos value(ENAME_SMOPTS_VERIFY) != NULL)
758798fbc60Schristos if (grabh(hp, GSMOPTS)) {
759798fbc60Schristos (void)printf("mail aborted!\n");
760798fbc60Schristos savedeadletter(mtf);
761798fbc60Schristos goto out;
762798fbc60Schristos }
763798fbc60Schristos }
7642a8765d5Schristos namelist = unpack(hp->h_smopts, to);
7654556f89aSchristos mail2(mtf, namelist);
76661f28255Scgd out:
76761f28255Scgd (void)Fclose(mtf);
76861f28255Scgd }
76961f28255Scgd
77061f28255Scgd /*
771f3098750Schristos * Interface between the argument list and the mail1 routine
772f3098750Schristos * which does all the dirty work.
77361f28255Scgd */
774f3098750Schristos PUBLIC int
mail(struct name * to,struct name * cc,struct name * bcc,struct name * smopts,char * subject,struct attachment * attach)775f3098750Schristos mail(struct name *to, struct name *cc, struct name *bcc,
776f3098750Schristos struct name *smopts, char *subject, struct attachment *attach)
77761f28255Scgd {
778f3098750Schristos struct header head;
77961f28255Scgd
780f3098750Schristos /* ensure that all header fields are initially NULL */
781f3098750Schristos (void)memset(&head, 0, sizeof(head));
78261f28255Scgd
783f3098750Schristos head.h_to = to;
784f3098750Schristos head.h_subject = subject;
785f3098750Schristos head.h_cc = cc;
786f3098750Schristos head.h_bcc = bcc;
787f3098750Schristos head.h_smopts = smopts;
7888207b28aSchristos #ifdef MIME_SUPPORT
789f3098750Schristos head.h_attach = attach;
7908207b28aSchristos #endif
791f3098750Schristos mail1(&head, 0);
792f3098750Schristos return 0;
79361f28255Scgd }
79461f28255Scgd
79561f28255Scgd /*
796f3098750Schristos * Send mail to a bunch of user names. The interface is through
7974556f89aSchristos * the mail1 routine above.
79861f28255Scgd */
799f3098750Schristos PUBLIC int
sendmail(void * v)800f3098750Schristos sendmail(void *v)
80161f28255Scgd {
802f3098750Schristos char *str = v;
803f3098750Schristos struct header head;
80461f28255Scgd
805f3098750Schristos /* ensure that all header fields are initially NULL */
806f3098750Schristos (void)memset(&head, 0, sizeof(head));
80761f28255Scgd
808f3098750Schristos head.h_to = extract(str, GTO);
80961f28255Scgd
810f3098750Schristos mail1(&head, 0);
811f3098750Schristos return 0;
81261f28255Scgd }
813