send.c revision 1.31 1 1.31 christos /* $NetBSD: send.c,v 1.31 2007/10/29 23:20:38 christos Exp $ */
2 1.6 christos
3 1.1 cgd /*
4 1.4 deraadt * Copyright (c) 1980, 1993
5 1.4 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.7 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.6 christos #if 0
35 1.6 christos static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 6/6/93";
36 1.6 christos #else
37 1.31 christos __RCSID("$NetBSD: send.c,v 1.31 2007/10/29 23:20:38 christos Exp $");
38 1.6 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "rcv.h"
42 1.4 deraadt #include "extern.h"
43 1.26 christos #ifdef MIME_SUPPORT
44 1.26 christos #include "mime.h"
45 1.26 christos #endif
46 1.26 christos
47 1.27 christos
48 1.28 christos /*
49 1.28 christos * Mail -- a mail program
50 1.28 christos *
51 1.28 christos * Mail to others.
52 1.28 christos */
53 1.28 christos
54 1.28 christos /*
55 1.28 christos * compare twwo name structures. Support for get_smopts.
56 1.28 christos */
57 1.27 christos static int
58 1.27 christos namecmp(struct name *np1, struct name *np2)
59 1.27 christos {
60 1.27 christos for (/*EMPTY*/; np1 && np2; np1 = np1->n_flink, np2 = np2->n_flink) {
61 1.27 christos if (strcmp(np1->n_name, np2->n_name) != 0)
62 1.27 christos return 1;
63 1.27 christos }
64 1.27 christos return np1 || np2;
65 1.27 christos }
66 1.27 christos
67 1.28 christos /*
68 1.28 christos * Get the sendmail options from the smopts list.
69 1.28 christos */
70 1.27 christos static struct name *
71 1.27 christos get_smopts(struct name *to)
72 1.27 christos {
73 1.27 christos struct smopts_s *sp;
74 1.27 christos struct name *smargs, *np;
75 1.27 christos smargs = NULL;
76 1.27 christos for (np = to; np; np = np->n_flink) {
77 1.27 christos if ((sp = findsmopts(np->n_name, 0)) == NULL)
78 1.27 christos continue;
79 1.27 christos if (smargs == NULL)
80 1.27 christos smargs = sp->s_smopts;
81 1.27 christos else if (namecmp(smargs, sp->s_smopts) != 0)
82 1.27 christos return NULL;
83 1.27 christos }
84 1.27 christos if (smargs &&
85 1.27 christos smargs->n_flink == NULL &&
86 1.27 christos (smargs->n_name == NULL || smargs->n_name[0] == '\0'))
87 1.27 christos return NULL;
88 1.27 christos return smargs;
89 1.27 christos }
90 1.1 cgd
91 1.1 cgd /*
92 1.28 christos * Output a reasonable looking status field.
93 1.1 cgd */
94 1.28 christos static void
95 1.28 christos statusput(struct message *mp, FILE *obuf, const char *prefix)
96 1.28 christos {
97 1.28 christos char statout[3];
98 1.28 christos char *cp = statout;
99 1.28 christos
100 1.28 christos if (mp->m_flag & MREAD)
101 1.28 christos *cp++ = 'R';
102 1.28 christos if ((mp->m_flag & MNEW) == 0)
103 1.28 christos *cp++ = 'O';
104 1.28 christos *cp = 0;
105 1.28 christos if (statout[0])
106 1.28 christos (void)fprintf(obuf, "%sStatus: %s\n",
107 1.28 christos prefix == NULL ? "" : prefix, statout);
108 1.28 christos }
109 1.1 cgd
110 1.1 cgd /*
111 1.1 cgd * Send message described by the passed pointer to the
112 1.1 cgd * passed output buffer. Return -1 on error.
113 1.1 cgd * Adjust the status: field if need be.
114 1.1 cgd * If doign is given, suppress ignored header fields.
115 1.1 cgd * prefix is a string to prepend to each output line.
116 1.1 cgd */
117 1.28 christos PUBLIC int
118 1.26 christos sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
119 1.26 christos const char *prefix, struct mime_info *mip)
120 1.1 cgd {
121 1.23 christos off_t len;
122 1.7 lukem FILE *ibuf;
123 1.1 cgd char line[LINESIZE];
124 1.28 christos int isheadflag, infld, ignoring, dostat, firstline;
125 1.29 christos char *cp;
126 1.28 christos size_t prefixlen;
127 1.30 christos size_t linelen;
128 1.28 christos
129 1.28 christos ignoring = 0;
130 1.28 christos prefixlen = 0;
131 1.1 cgd
132 1.1 cgd /*
133 1.1 cgd * Compute the prefix string, without trailing whitespace
134 1.1 cgd */
135 1.16 wiz if (prefix != NULL) {
136 1.22 christos const char *dp, *dp2 = NULL;
137 1.22 christos for (dp = prefix; *dp; dp++)
138 1.29 christos if (!is_WSP(*dp))
139 1.22 christos dp2 = dp;
140 1.22 christos prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
141 1.1 cgd }
142 1.1 cgd ibuf = setinput(mp);
143 1.15 wiz len = mp->m_size;
144 1.15 wiz isheadflag = 1;
145 1.1 cgd dostat = doign == 0 || !isign("status", doign);
146 1.1 cgd infld = 0;
147 1.1 cgd firstline = 1;
148 1.1 cgd /*
149 1.1 cgd * Process headers first
150 1.1 cgd */
151 1.26 christos #ifdef MIME_SUPPORT
152 1.26 christos if (mip)
153 1.26 christos obuf = mime_decode_header(mip);
154 1.26 christos #endif
155 1.15 wiz while (len > 0 && isheadflag) {
156 1.30 christos if (fgets(line, sizeof(line), ibuf) == NULL)
157 1.1 cgd break;
158 1.30 christos len -= linelen = strlen(line);
159 1.1 cgd if (firstline) {
160 1.29 christos /*
161 1.1 cgd * First line is the From line, so no headers
162 1.1 cgd * there to worry about
163 1.1 cgd */
164 1.1 cgd firstline = 0;
165 1.30 christos ignoring = doign == ignoreall || doign == bouncetab;
166 1.26 christos #ifdef MIME_SUPPORT
167 1.26 christos /* XXX - ignore multipart boundary lines! */
168 1.26 christos if (line[0] == '-' && line[1] == '-')
169 1.26 christos ignoring = 1;
170 1.26 christos #endif
171 1.1 cgd } else if (line[0] == '\n') {
172 1.1 cgd /*
173 1.1 cgd * If line is blank, we've reached end of
174 1.1 cgd * headers, so force out status: field
175 1.1 cgd * and note that we are no longer in header
176 1.1 cgd * fields
177 1.1 cgd */
178 1.1 cgd if (dostat) {
179 1.1 cgd statusput(mp, obuf, prefix);
180 1.1 cgd dostat = 0;
181 1.1 cgd }
182 1.15 wiz isheadflag = 0;
183 1.1 cgd ignoring = doign == ignoreall;
184 1.29 christos } else if (infld && is_WSP(line[0])) {
185 1.1 cgd /*
186 1.1 cgd * If this line is a continuation (via space or tab)
187 1.1 cgd * of a previous header field, just echo it
188 1.1 cgd * (unless the field should be ignored).
189 1.1 cgd * In other words, nothing to do.
190 1.1 cgd */
191 1.1 cgd } else {
192 1.1 cgd /*
193 1.1 cgd * Pick up the header field if we have one.
194 1.1 cgd */
195 1.29 christos char *save_cp;
196 1.29 christos for (cp = line; *cp && *cp != ':' && !is_WSP(*cp); cp++)
197 1.28 christos continue;
198 1.29 christos save_cp = cp;
199 1.29 christos cp = skip_WSP(cp);
200 1.29 christos if (*cp++ != ':') {
201 1.1 cgd /*
202 1.1 cgd * Not a header line, force out status:
203 1.1 cgd * This happens in uucp style mail where
204 1.1 cgd * there are no headers at all.
205 1.1 cgd */
206 1.1 cgd if (dostat) {
207 1.1 cgd statusput(mp, obuf, prefix);
208 1.1 cgd dostat = 0;
209 1.1 cgd }
210 1.1 cgd if (doign != ignoreall)
211 1.1 cgd /* add blank line */
212 1.18 wiz (void)putc('\n', obuf);
213 1.15 wiz isheadflag = 0;
214 1.1 cgd ignoring = 0;
215 1.1 cgd } else {
216 1.1 cgd /*
217 1.1 cgd * If it is an ignored field and
218 1.1 cgd * we care about such things, skip it.
219 1.1 cgd */
220 1.29 christos int save_c;
221 1.29 christos save_c = *save_cp;
222 1.29 christos *save_cp = 0; /* temporarily null terminate */
223 1.1 cgd if (doign && isign(line, doign))
224 1.1 cgd ignoring = 1;
225 1.1 cgd else if ((line[0] == 's' || line[0] == 'S') &&
226 1.1 cgd strcasecmp(line, "status") == 0) {
227 1.1 cgd /*
228 1.1 cgd * If the field is "status," go compute
229 1.1 cgd * and print the real Status: field
230 1.1 cgd */
231 1.1 cgd if (dostat) {
232 1.1 cgd statusput(mp, obuf, prefix);
233 1.1 cgd dostat = 0;
234 1.1 cgd }
235 1.1 cgd ignoring = 1;
236 1.1 cgd } else {
237 1.1 cgd ignoring = 0;
238 1.1 cgd }
239 1.1 cgd infld = 1;
240 1.30 christos *save_cp = save_c; /* restore the line */
241 1.1 cgd }
242 1.1 cgd }
243 1.1 cgd if (!ignoring) {
244 1.1 cgd /*
245 1.1 cgd * Strip trailing whitespace from prefix
246 1.1 cgd * if line is blank.
247 1.1 cgd */
248 1.16 wiz if (prefix != NULL) {
249 1.30 christos if (linelen > 1)
250 1.23 christos (void)fputs(prefix, obuf);
251 1.1 cgd else
252 1.30 christos (void)fwrite(prefix, sizeof(*prefix),
253 1.1 cgd prefixlen, obuf);
254 1.9 ross }
255 1.30 christos (void)fwrite(line, sizeof(*line), linelen, obuf);
256 1.1 cgd if (ferror(obuf))
257 1.1 cgd return -1;
258 1.1 cgd }
259 1.1 cgd }
260 1.1 cgd /*
261 1.1 cgd * Copy out message body
262 1.1 cgd */
263 1.26 christos #ifdef MIME_SUPPORT
264 1.26 christos if (mip) {
265 1.26 christos obuf = mime_decode_body(mip);
266 1.26 christos if (obuf == NULL) /* XXX - early out */
267 1.26 christos return 0;
268 1.26 christos }
269 1.26 christos #endif
270 1.1 cgd if (doign == ignoreall)
271 1.15 wiz len--; /* skip final blank line */
272 1.28 christos
273 1.30 christos linelen = 0; /* needed for in case len == 0 */
274 1.16 wiz if (prefix != NULL)
275 1.15 wiz while (len > 0) {
276 1.30 christos if (fgets(line, sizeof(line), ibuf) == NULL) {
277 1.30 christos linelen = 0;
278 1.1 cgd break;
279 1.1 cgd }
280 1.30 christos len -= linelen = strlen(line);
281 1.1 cgd /*
282 1.1 cgd * Strip trailing whitespace from prefix
283 1.1 cgd * if line is blank.
284 1.1 cgd */
285 1.30 christos if (linelen > 1)
286 1.23 christos (void)fputs(prefix, obuf);
287 1.1 cgd else
288 1.30 christos (void)fwrite(prefix, sizeof(*prefix),
289 1.1 cgd prefixlen, obuf);
290 1.31 christos (void)fwrite(line, sizeof(*line), linelen, obuf);
291 1.1 cgd if (ferror(obuf))
292 1.1 cgd return -1;
293 1.1 cgd }
294 1.1 cgd else
295 1.15 wiz while (len > 0) {
296 1.30 christos linelen = len < sizeof(line) ? (size_t)len : sizeof(line);
297 1.30 christos if ((linelen = fread(line, sizeof(*line), linelen, ibuf)) == 0)
298 1.1 cgd break;
299 1.30 christos len -= linelen;
300 1.30 christos if (fwrite(line, sizeof(*line), linelen, obuf) != linelen)
301 1.1 cgd return -1;
302 1.1 cgd }
303 1.30 christos if (doign == ignoreall && linelen > 0 && line[linelen - 1] != '\n') {
304 1.30 christos int c;
305 1.1 cgd /* no final blank line */
306 1.1 cgd if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
307 1.30 christos return -1;
308 1.30 christos }
309 1.1 cgd return 0;
310 1.1 cgd }
311 1.1 cgd
312 1.1 cgd /*
313 1.28 christos * Fix the header by glopping all of the expanded names from
314 1.28 christos * the distribution list into the appropriate fields.
315 1.28 christos */
316 1.28 christos static void
317 1.28 christos fixhead(struct header *hp, struct name *tolist)
318 1.28 christos {
319 1.28 christos struct name *np;
320 1.28 christos
321 1.28 christos hp->h_to = NULL;
322 1.28 christos hp->h_cc = NULL;
323 1.28 christos hp->h_bcc = NULL;
324 1.28 christos for (np = tolist; np != NULL; np = np->n_flink) {
325 1.28 christos if (np->n_type & GDEL)
326 1.28 christos continue; /* Don't copy deleted addresses to the header */
327 1.28 christos if ((np->n_type & GMASK) == GTO)
328 1.28 christos hp->h_to =
329 1.28 christos cat(hp->h_to, nalloc(np->n_name, np->n_type));
330 1.28 christos else if ((np->n_type & GMASK) == GCC)
331 1.28 christos hp->h_cc =
332 1.28 christos cat(hp->h_cc, nalloc(np->n_name, np->n_type));
333 1.28 christos else if ((np->n_type & GMASK) == GBCC)
334 1.28 christos hp->h_bcc =
335 1.28 christos cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
336 1.28 christos }
337 1.28 christos }
338 1.28 christos
339 1.28 christos /*
340 1.28 christos * Format the given header line to not exceed 72 characters.
341 1.1 cgd */
342 1.28 christos static void
343 1.28 christos fmt(const char *str, struct name *np, FILE *fo, int comma)
344 1.1 cgd {
345 1.28 christos int col, len;
346 1.1 cgd
347 1.28 christos comma = comma ? 1 : 0;
348 1.28 christos col = strlen(str);
349 1.28 christos if (col)
350 1.28 christos (void)fputs(str, fo);
351 1.29 christos for (/*EMPTY*/; np != NULL; np = np->n_flink) {
352 1.28 christos if (np->n_flink == NULL)
353 1.28 christos comma = 0;
354 1.28 christos len = strlen(np->n_name);
355 1.28 christos col++; /* for the space */
356 1.28 christos if (col + len + comma > 72 && col > 4) {
357 1.28 christos (void)fputs("\n ", fo);
358 1.28 christos col = 4;
359 1.28 christos } else
360 1.28 christos (void)putc(' ', fo);
361 1.28 christos (void)fputs(np->n_name, fo);
362 1.28 christos if (comma)
363 1.28 christos (void)putc(',', fo);
364 1.28 christos col += len + comma;
365 1.28 christos }
366 1.28 christos (void)putc('\n', fo);
367 1.1 cgd }
368 1.1 cgd
369 1.1 cgd /*
370 1.28 christos * Dump the to, subject, cc header on the
371 1.28 christos * passed file buffer.
372 1.1 cgd */
373 1.28 christos PUBLIC int
374 1.28 christos puthead(struct header *hp, FILE *fo, int w)
375 1.28 christos {
376 1.28 christos int gotcha;
377 1.28 christos
378 1.28 christos gotcha = 0;
379 1.28 christos if (hp->h_to != NULL && w & GTO)
380 1.28 christos fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
381 1.28 christos if (hp->h_subject != NULL && w & GSUBJECT)
382 1.28 christos (void)fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
383 1.28 christos if (hp->h_smopts != NULL && w & GSMOPTS)
384 1.28 christos (void)fprintf(fo, "(sendmail options: %s)\n", detract(hp->h_smopts, GSMOPTS)), gotcha++;
385 1.28 christos if (hp->h_cc != NULL && w & GCC)
386 1.28 christos fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
387 1.28 christos if (hp->h_bcc != NULL && w & GBCC)
388 1.28 christos fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
389 1.28 christos if (hp->h_in_reply_to != NULL && w & GMISC)
390 1.28 christos (void)fprintf(fo, "In-Reply-To: %s\n", hp->h_in_reply_to), gotcha++;
391 1.28 christos if (hp->h_references != NULL && w & GMISC)
392 1.28 christos fmt("References:", hp->h_references, fo, w&GCOMMA), gotcha++;
393 1.26 christos #ifdef MIME_SUPPORT
394 1.28 christos if (w & GMIME && (hp->h_attach || value(ENAME_MIME_ENCODE_MSG)))
395 1.28 christos mime_putheader(fo, hp), gotcha++;
396 1.26 christos #endif
397 1.28 christos if (gotcha && w & GNL)
398 1.28 christos (void)putc('\n', fo);
399 1.28 christos return 0;
400 1.28 christos }
401 1.28 christos
402 1.28 christos /*
403 1.28 christos * Prepend a header in front of the collected stuff
404 1.28 christos * and return the new file.
405 1.28 christos */
406 1.28 christos static FILE *
407 1.28 christos infix(struct header *hp, FILE *fi)
408 1.1 cgd {
409 1.28 christos FILE *nfo, *nfi;
410 1.28 christos int c, fd;
411 1.28 christos char tempname[PATHSIZE];
412 1.1 cgd
413 1.28 christos (void)snprintf(tempname, sizeof(tempname),
414 1.28 christos "%s/mail.RsXXXXXXXXXX", tmpdir);
415 1.28 christos if ((fd = mkstemp(tempname)) == -1 ||
416 1.28 christos (nfo = Fdopen(fd, "w")) == NULL) {
417 1.28 christos if (fd != -1)
418 1.28 christos (void)close(fd);
419 1.28 christos warn("%s", tempname);
420 1.28 christos return fi;
421 1.28 christos }
422 1.28 christos if ((nfi = Fopen(tempname, "r")) == NULL) {
423 1.28 christos warn("%s", tempname);
424 1.28 christos (void)Fclose(nfo);
425 1.28 christos (void)rm(tempname);
426 1.28 christos return fi;
427 1.28 christos }
428 1.28 christos (void)rm(tempname);
429 1.26 christos #ifdef MIME_SUPPORT
430 1.28 christos (void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMISC|GMIME|GNL|GCOMMA);
431 1.28 christos #else
432 1.28 christos (void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMISC|GNL|GCOMMA);
433 1.26 christos #endif
434 1.28 christos
435 1.28 christos c = getc(fi);
436 1.28 christos while (c != EOF) {
437 1.28 christos (void)putc(c, nfo);
438 1.28 christos c = getc(fi);
439 1.28 christos }
440 1.28 christos if (ferror(fi)) {
441 1.28 christos warn("read");
442 1.28 christos rewind(fi);
443 1.28 christos return fi;
444 1.28 christos }
445 1.28 christos (void)fflush(nfo);
446 1.28 christos if (ferror(nfo)) {
447 1.28 christos warn("%s", tempname);
448 1.28 christos (void)Fclose(nfo);
449 1.28 christos (void)Fclose(nfi);
450 1.28 christos rewind(fi);
451 1.28 christos return fi;
452 1.28 christos }
453 1.28 christos (void)Fclose(nfo);
454 1.28 christos (void)Fclose(fi);
455 1.28 christos rewind(nfi);
456 1.28 christos return nfi;
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd /*
460 1.28 christos * Save the outgoing mail on the passed file.
461 1.1 cgd */
462 1.28 christos /*ARGSUSED*/
463 1.28 christos static int
464 1.28 christos savemail(const char name[], FILE *fi)
465 1.1 cgd {
466 1.28 christos FILE *fo;
467 1.28 christos char buf[LINESIZE];
468 1.28 christos int i;
469 1.28 christos time_t now;
470 1.28 christos mode_t m;
471 1.1 cgd
472 1.28 christos m = umask(077);
473 1.28 christos fo = Fopen(name, "a");
474 1.28 christos (void)umask(m);
475 1.28 christos if (fo == NULL) {
476 1.28 christos warn("%s", name);
477 1.28 christos return -1;
478 1.28 christos }
479 1.28 christos (void)time(&now);
480 1.28 christos (void)fprintf(fo, "From %s %s", myname, ctime(&now));
481 1.31 christos while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
482 1.28 christos (void)fwrite(buf, 1, (size_t)i, fo);
483 1.28 christos (void)putc('\n', fo);
484 1.28 christos (void)fflush(fo);
485 1.28 christos if (ferror(fo))
486 1.28 christos warn("%s", name);
487 1.28 christos (void)Fclose(fo);
488 1.28 christos rewind(fi);
489 1.28 christos return 0;
490 1.1 cgd }
491 1.1 cgd
492 1.1 cgd /*
493 1.30 christos * Mail a message that is already prepared in a file.
494 1.30 christos */
495 1.30 christos PUBLIC void
496 1.30 christos mail2(FILE *mtf, const char **namelist)
497 1.30 christos {
498 1.30 christos int pid;
499 1.30 christos const char *cp;
500 1.30 christos
501 1.30 christos if (debug) {
502 1.30 christos const char **t;
503 1.30 christos
504 1.30 christos (void)printf("Sendmail arguments:");
505 1.30 christos for (t = namelist; *t != NULL; t++)
506 1.30 christos (void)printf(" \"%s\"", *t);
507 1.30 christos (void)printf("\n");
508 1.30 christos return;
509 1.30 christos }
510 1.30 christos if ((cp = value(ENAME_RECORD)) != NULL)
511 1.30 christos (void)savemail(expand(cp), mtf);
512 1.30 christos /*
513 1.30 christos * Fork, set up the temporary mail file as standard
514 1.30 christos * input for "mail", and exec with the user list we generated
515 1.30 christos * far above.
516 1.30 christos */
517 1.30 christos pid = fork();
518 1.30 christos switch (pid) {
519 1.30 christos case -1:
520 1.30 christos warn("fork");
521 1.30 christos savedeadletter(mtf);
522 1.30 christos return;
523 1.30 christos case 0:
524 1.30 christos {
525 1.30 christos sigset_t nset;
526 1.30 christos (void)sigemptyset(&nset);
527 1.30 christos (void)sigaddset(&nset, SIGHUP);
528 1.30 christos (void)sigaddset(&nset, SIGINT);
529 1.30 christos (void)sigaddset(&nset, SIGQUIT);
530 1.30 christos (void)sigaddset(&nset, SIGTSTP);
531 1.30 christos (void)sigaddset(&nset, SIGTTIN);
532 1.30 christos (void)sigaddset(&nset, SIGTTOU);
533 1.30 christos prepare_child(&nset, fileno(mtf), -1);
534 1.30 christos if ((cp = value(ENAME_SENDMAIL)) != NULL)
535 1.30 christos cp = expand(cp);
536 1.30 christos else
537 1.30 christos cp = _PATH_SENDMAIL;
538 1.30 christos (void)execv(cp, (char *const *)__UNCONST(namelist));
539 1.30 christos warn("%s", cp);
540 1.30 christos _exit(1);
541 1.30 christos break; /* Appease GCC */
542 1.30 christos }
543 1.30 christos default:
544 1.30 christos if (value(ENAME_VERBOSE) != NULL)
545 1.30 christos (void)wait_child(pid);
546 1.30 christos else
547 1.30 christos free_child(pid);
548 1.30 christos break;
549 1.30 christos }
550 1.30 christos }
551 1.30 christos
552 1.30 christos /*
553 1.1 cgd * Mail a message on standard input to the people indicated
554 1.1 cgd * in the passed header. (Internal interface).
555 1.1 cgd */
556 1.28 christos PUBLIC void
557 1.14 wiz mail1(struct header *hp, int printheaders)
558 1.1 cgd {
559 1.22 christos const char **namelist;
560 1.1 cgd struct name *to;
561 1.1 cgd FILE *mtf;
562 1.1 cgd
563 1.1 cgd /*
564 1.1 cgd * Collect user's mail from standard input.
565 1.1 cgd * Get the result as mtf.
566 1.1 cgd */
567 1.1 cgd if ((mtf = collect(hp, printheaders)) == NULL)
568 1.1 cgd return;
569 1.28 christos if (value(ENAME_INTERACTIVE) != NULL) {
570 1.28 christos if (value(ENAME_ASKCC) != NULL || value(ENAME_ASKBCC) != NULL) {
571 1.28 christos if (value(ENAME_ASKCC) != NULL)
572 1.23 christos (void)grabh(hp, GCC);
573 1.28 christos if (value(ENAME_ASKBCC) != NULL)
574 1.23 christos (void)grabh(hp, GBCC);
575 1.3 jtc } else {
576 1.23 christos (void)printf("EOT\n");
577 1.18 wiz (void)fflush(stdout);
578 1.1 cgd }
579 1.9 ross }
580 1.9 ross if (fsize(mtf) == 0) {
581 1.28 christos if (value(ENAME_DONTSENDEMPTY) != NULL)
582 1.12 christos goto out;
583 1.16 wiz if (hp->h_subject == NULL)
584 1.23 christos (void)printf("No message, no subject; hope that's ok\n");
585 1.1 cgd else
586 1.23 christos (void)printf("Null message body; hope that's ok\n");
587 1.9 ross }
588 1.1 cgd /*
589 1.1 cgd * Now, take the user names from the combined
590 1.1 cgd * to and cc lists and do all the alias
591 1.1 cgd * processing.
592 1.1 cgd */
593 1.1 cgd senderr = 0;
594 1.1 cgd to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
595 1.17 wiz if (to == NULL) {
596 1.23 christos (void)printf("No recipients specified\n");
597 1.1 cgd senderr++;
598 1.1 cgd }
599 1.26 christos #ifdef MIME_SUPPORT
600 1.26 christos /*
601 1.26 christos * If there are attachments, repackage the mail as a
602 1.26 christos * multi-part MIME message.
603 1.26 christos */
604 1.26 christos if (hp->h_attach || value(ENAME_MIME_ENCODE_MSG))
605 1.26 christos mtf = mime_encode(mtf, hp);
606 1.26 christos #endif
607 1.1 cgd /*
608 1.1 cgd * Look through the recipient list for names with /'s
609 1.1 cgd * in them which we write to as files directly.
610 1.1 cgd */
611 1.1 cgd to = outof(to, mtf, hp);
612 1.1 cgd if (senderr)
613 1.1 cgd savedeadletter(mtf);
614 1.1 cgd to = elide(to);
615 1.1 cgd if (count(to) == 0)
616 1.1 cgd goto out;
617 1.1 cgd fixhead(hp, to);
618 1.1 cgd if ((mtf = infix(hp, mtf)) == NULL) {
619 1.23 christos (void)fprintf(stderr, ". . . message lost, sorry.\n");
620 1.1 cgd return;
621 1.1 cgd }
622 1.27 christos if (hp->h_smopts == NULL) {
623 1.27 christos hp->h_smopts = get_smopts(to);
624 1.27 christos if (hp->h_smopts != NULL &&
625 1.27 christos hp->h_smopts->n_name[0] != '\0' &&
626 1.27 christos value(ENAME_SMOPTS_VERIFY) != NULL)
627 1.27 christos if (grabh(hp, GSMOPTS)) {
628 1.27 christos (void)printf("mail aborted!\n");
629 1.27 christos savedeadletter(mtf);
630 1.27 christos goto out;
631 1.27 christos }
632 1.27 christos }
633 1.1 cgd namelist = unpack(cat(hp->h_smopts, to));
634 1.30 christos mail2(mtf, namelist);
635 1.30 christos out:
636 1.18 wiz (void)Fclose(mtf);
637 1.1 cgd }
638 1.1 cgd
639 1.1 cgd /*
640 1.28 christos * Interface between the argument list and the mail1 routine
641 1.28 christos * which does all the dirty work.
642 1.1 cgd */
643 1.28 christos PUBLIC int
644 1.28 christos mail(struct name *to, struct name *cc, struct name *bcc,
645 1.28 christos struct name *smopts, char *subject, struct attachment *attach)
646 1.1 cgd {
647 1.28 christos struct header head;
648 1.1 cgd
649 1.28 christos /* ensure that all header fields are initially NULL */
650 1.28 christos (void)memset(&head, 0, sizeof(head));
651 1.1 cgd
652 1.28 christos head.h_to = to;
653 1.28 christos head.h_subject = subject;
654 1.28 christos head.h_cc = cc;
655 1.28 christos head.h_bcc = bcc;
656 1.28 christos head.h_smopts = smopts;
657 1.26 christos #ifdef MIME_SUPPORT
658 1.28 christos head.h_attach = attach;
659 1.26 christos #endif
660 1.28 christos mail1(&head, 0);
661 1.28 christos return 0;
662 1.1 cgd }
663 1.1 cgd
664 1.1 cgd /*
665 1.28 christos * Send mail to a bunch of user names. The interface is through
666 1.30 christos * the mail1 routine above.
667 1.1 cgd */
668 1.28 christos PUBLIC int
669 1.28 christos sendmail(void *v)
670 1.1 cgd {
671 1.28 christos char *str = v;
672 1.28 christos struct header head;
673 1.1 cgd
674 1.28 christos /* ensure that all header fields are initially NULL */
675 1.28 christos (void)memset(&head, 0, sizeof(head));
676 1.1 cgd
677 1.28 christos head.h_to = extract(str, GTO);
678 1.1 cgd
679 1.28 christos mail1(&head, 0);
680 1.28 christos return 0;
681 1.1 cgd }
682