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