1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 "rcv.h"
33 #include "extern.h"
34
35 /*
36 * Mail -- a mail program
37 *
38 * Mail to others.
39 */
40
41 /*
42 * Send message described by the passed pointer to the
43 * passed output buffer. Return -1 on error.
44 * Adjust the status: field if need be.
45 * If doign is given, suppress ignored header fields.
46 * prefix is a string to prepend to each output line.
47 */
48 int
sendmessage(struct message * mp,FILE * obuf,struct ignoretab * doign,char * prefix)49 sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
50 char *prefix)
51 {
52 long count;
53 FILE *ibuf;
54 char *cp, *cp2, line[LINESIZE];
55 int ishead, infld, ignoring, dostat, firstline;
56 int c = 0, length, prefixlen;
57
58 /*
59 * Compute the prefix string, without trailing whitespace
60 */
61 if (prefix != NULL) {
62 cp2 = 0;
63 for (cp = prefix; *cp != '\0'; cp++)
64 if (*cp != ' ' && *cp != '\t')
65 cp2 = cp;
66 prefixlen = cp2 == NULL ? 0 : cp2 - prefix + 1;
67 }
68 ibuf = setinput(mp);
69 count = mp->m_size;
70 ishead = 1;
71 dostat = doign == 0 || !isign("status", doign);
72 infld = 0;
73 firstline = 1;
74 /*
75 * Process headers first
76 */
77 while (count > 0 && ishead) {
78 if (fgets(line, sizeof(line), ibuf) == NULL)
79 break;
80 count -= length = strlen(line);
81 if (firstline) {
82 /*
83 * First line is the From line, so no headers
84 * there to worry about
85 */
86 firstline = 0;
87 ignoring = doign == ignoreall;
88 } else if (line[0] == '\n') {
89 /*
90 * If line is blank, we've reached end of
91 * headers, so force out status: field
92 * and note that we are no longer in header
93 * fields
94 */
95 if (dostat) {
96 statusput(mp, obuf, prefix);
97 dostat = 0;
98 }
99 ishead = 0;
100 ignoring = doign == ignoreall;
101 } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
102 /*
103 * If this line is a continuation (via space or tab)
104 * of a previous header field, just echo it
105 * (unless the field should be ignored).
106 * In other words, nothing to do.
107 */
108 } else {
109 /*
110 * Pick up the header field if we have one.
111 */
112 for (cp = line; (c = *cp++) != '\0' && c != ':' &&
113 !isspace((unsigned char)c);)
114 ;
115 cp2 = --cp;
116 while (isspace((unsigned char)*cp++))
117 ;
118 if (cp[-1] != ':') {
119 /*
120 * Not a header line, force out status:
121 * This happens in uucp style mail where
122 * there are no headers at all.
123 */
124 if (dostat) {
125 statusput(mp, obuf, prefix);
126 dostat = 0;
127 }
128 if (doign != ignoreall)
129 /* add blank line */
130 (void)putc('\n', obuf);
131 ishead = 0;
132 ignoring = 0;
133 } else {
134 /*
135 * If it is an ignored field and
136 * we care about such things, skip it.
137 */
138 *cp2 = '\0'; /* temporarily null terminate */
139 if (doign && isign(line, doign))
140 ignoring = 1;
141 else if ((line[0] == 's' || line[0] == 'S') &&
142 strcasecmp(line, "status") == 0) {
143 /*
144 * If the field is "status," go compute
145 * and print the real Status: field
146 */
147 if (dostat) {
148 statusput(mp, obuf, prefix);
149 dostat = 0;
150 }
151 ignoring = 1;
152 } else {
153 ignoring = 0;
154 *cp2 = c; /* restore */
155 }
156 infld = 1;
157 }
158 }
159 if (!ignoring) {
160 /*
161 * Strip trailing whitespace from prefix
162 * if line is blank.
163 */
164 if (prefix != NULL) {
165 if (length > 1)
166 fputs(prefix, obuf);
167 else
168 (void)fwrite(prefix, sizeof(*prefix),
169 prefixlen, obuf);
170 }
171 (void)fwrite(line, sizeof(*line), length, obuf);
172 if (ferror(obuf))
173 return (-1);
174 }
175 }
176 /*
177 * Copy out message body
178 */
179 if (doign == ignoreall)
180 count--; /* skip final blank line */
181 if (prefix != NULL)
182 while (count > 0) {
183 if (fgets(line, sizeof(line), ibuf) == NULL) {
184 c = 0;
185 break;
186 }
187 count -= c = strlen(line);
188 /*
189 * Strip trailing whitespace from prefix
190 * if line is blank.
191 */
192 if (c > 1)
193 fputs(prefix, obuf);
194 else
195 (void)fwrite(prefix, sizeof(*prefix),
196 prefixlen, obuf);
197 (void)fwrite(line, sizeof(*line), c, obuf);
198 if (ferror(obuf))
199 return (-1);
200 }
201 else
202 while (count > 0) {
203 c = count < LINESIZE ? count : LINESIZE;
204 if ((c = fread(line, sizeof(*line), c, ibuf)) <= 0)
205 break;
206 count -= c;
207 if (fwrite(line, sizeof(*line), c, obuf) != c)
208 return (-1);
209 }
210 if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
211 /* no final blank line */
212 if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
213 return (-1);
214 return (0);
215 }
216
217 /*
218 * Output a reasonable looking status field.
219 */
220 void
statusput(struct message * mp,FILE * obuf,char * prefix)221 statusput(struct message *mp, FILE *obuf, char *prefix)
222 {
223 char statout[3];
224 char *cp = statout;
225
226 if (mp->m_flag & MREAD)
227 *cp++ = 'R';
228 if ((mp->m_flag & MNEW) == 0)
229 *cp++ = 'O';
230 *cp = '\0';
231 if (statout[0] != '\0')
232 fprintf(obuf, "%sStatus: %s\n",
233 prefix == NULL ? "" : prefix, statout);
234 }
235
236 /*
237 * Interface between the argument list and the mail1 routine
238 * which does all the dirty work.
239 */
240 int
mail(struct name * to,struct name * cc,struct name * bcc,struct name * smopts,char * subject,char * replyto)241 mail(struct name *to, struct name *cc, struct name *bcc, struct name *smopts,
242 char *subject, char *replyto)
243 {
244 struct header head;
245
246 head.h_to = to;
247 head.h_subject = subject;
248 head.h_cc = cc;
249 head.h_bcc = bcc;
250 head.h_smopts = smopts;
251 head.h_replyto = replyto;
252 head.h_inreplyto = NULL;
253 mail1(&head, 0);
254 return (0);
255 }
256
257
258 /*
259 * Send mail to a bunch of user names. The interface is through
260 * the mail routine below.
261 */
262 int
sendmail(void * str)263 sendmail(void *str)
264 {
265 struct header head;
266
267 head.h_to = extract(str, GTO);
268 head.h_subject = NULL;
269 head.h_cc = NULL;
270 head.h_bcc = NULL;
271 head.h_smopts = NULL;
272 head.h_replyto = value("REPLYTO");
273 head.h_inreplyto = NULL;
274 mail1(&head, 0);
275 return (0);
276 }
277
278 /*
279 * Mail a message on standard input to the people indicated
280 * in the passed header. (Internal interface).
281 */
282 void
mail1(struct header * hp,int printheaders)283 mail1(struct header *hp, int printheaders)
284 {
285 char *cp;
286 char *nbuf;
287 int pid;
288 char **namelist;
289 struct name *to, *nsto;
290 FILE *mtf;
291
292 /*
293 * Collect user's mail from standard input.
294 * Get the result as mtf.
295 */
296 if ((mtf = collect(hp, printheaders)) == NULL) {
297 senderr++;
298 return;
299 }
300 if (value("interactive") != NULL) {
301 if (value("askcc") != NULL || value("askbcc") != NULL) {
302 if (value("askcc") != NULL)
303 grabh(hp, GCC);
304 if (value("askbcc") != NULL)
305 grabh(hp, GBCC);
306 } else {
307 printf("EOT\n");
308 (void)fflush(stdout);
309 }
310 }
311 if (fsize(mtf) == 0) {
312 if (value("dontsendempty") != NULL)
313 goto out;
314 if (hp->h_subject == NULL)
315 printf("No message, no subject; hope that's ok\n");
316 else
317 printf("Null message body; hope that's ok\n");
318 }
319 /*
320 * Now, take the user names from the combined
321 * to and cc lists and do all the alias
322 * processing.
323 */
324 senderr = 0;
325 to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
326 if (to == NULL) {
327 printf("No recipients specified\n");
328 senderr++;
329 }
330 /*
331 * Look through the recipient list for names with /'s
332 * in them which we write to as files directly.
333 */
334 to = outof(to, mtf, hp);
335 if (senderr)
336 savedeadletter(mtf);
337 to = elide(to);
338 if (count(to) == 0)
339 goto out;
340 if (value("recordrecip") != NULL) {
341 /*
342 * Before fixing the header, save old To:.
343 * We do this because elide above has sorted To: list, and
344 * we would like to save message in a file named by the first
345 * recipient the user has entered, not the one being the first
346 * after sorting happened.
347 */
348 if ((nsto = malloc(sizeof(struct name))) == NULL)
349 err(1, "Out of memory");
350 bcopy(hp->h_to, nsto, sizeof(struct name));
351 }
352 fixhead(hp, to);
353 if ((mtf = infix(hp, mtf)) == NULL) {
354 fprintf(stderr, ". . . message lost, sorry.\n");
355 return;
356 }
357 namelist = unpack(cat(hp->h_smopts, to));
358 if (debug) {
359 char **t;
360
361 printf("Sendmail arguments:");
362 for (t = namelist; *t != NULL; t++)
363 printf(" \"%s\"", *t);
364 printf("\n");
365 goto out;
366 }
367 if (value("recordrecip") != NULL) {
368 /*
369 * Extract first recipient username from saved To: and use it
370 * as a filename.
371 */
372 if ((nbuf = malloc(strlen(detract(nsto, 0)) + 1)) == NULL)
373 err(1, "Out of memory");
374 if ((cp = yanklogin(detract(nsto, 0), nbuf)) != NULL)
375 (void)savemail(expand(nbuf), mtf);
376 free(nbuf);
377 free(nsto);
378 } else if ((cp = value("record")) != NULL)
379 (void)savemail(expand(cp), mtf);
380 /*
381 * Fork, set up the temporary mail file as standard
382 * input for "mail", and exec with the user list we generated
383 * far above.
384 */
385 pid = fork();
386 if (pid == -1) {
387 warn("fork");
388 savedeadletter(mtf);
389 goto out;
390 }
391 if (pid == 0) {
392 sigset_t nset;
393 (void)sigemptyset(&nset);
394 (void)sigaddset(&nset, SIGHUP);
395 (void)sigaddset(&nset, SIGINT);
396 (void)sigaddset(&nset, SIGQUIT);
397 (void)sigaddset(&nset, SIGTSTP);
398 (void)sigaddset(&nset, SIGTTIN);
399 (void)sigaddset(&nset, SIGTTOU);
400 prepare_child(&nset, fileno(mtf), -1);
401 if ((cp = value("sendmail")) != NULL)
402 cp = expand(cp);
403 else
404 cp = _PATH_SENDMAIL;
405 execv(cp, namelist);
406 warn("%s", cp);
407 _exit(1);
408 }
409 if (value("verbose") != NULL)
410 (void)wait_child(pid);
411 else
412 free_child(pid);
413 out:
414 (void)Fclose(mtf);
415 }
416
417 /*
418 * Fix the header by glopping all of the expanded names from
419 * the distribution list into the appropriate fields.
420 */
421 void
fixhead(struct header * hp,struct name * tolist)422 fixhead(struct header *hp, struct name *tolist)
423 {
424 struct name *np;
425
426 hp->h_to = NULL;
427 hp->h_cc = NULL;
428 hp->h_bcc = NULL;
429 for (np = tolist; np != NULL; np = np->n_flink) {
430 /* Don't copy deleted addresses to the header */
431 if (np->n_type & GDEL)
432 continue;
433 if ((np->n_type & GMASK) == GTO)
434 hp->h_to =
435 cat(hp->h_to, nalloc(np->n_name, np->n_type));
436 else if ((np->n_type & GMASK) == GCC)
437 hp->h_cc =
438 cat(hp->h_cc, nalloc(np->n_name, np->n_type));
439 else if ((np->n_type & GMASK) == GBCC)
440 hp->h_bcc =
441 cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
442 }
443 }
444
445 /*
446 * Prepend a header in front of the collected stuff
447 * and return the new file.
448 */
449 FILE *
infix(struct header * hp,FILE * fi)450 infix(struct header *hp, FILE *fi)
451 {
452 FILE *nfo, *nfi;
453 int c, fd;
454 char tempname[PATHSIZE];
455
456 (void)snprintf(tempname, sizeof(tempname),
457 "%s/mail.RsXXXXXXXXXX", tmpdir);
458 if ((fd = mkstemp(tempname)) == -1 ||
459 (nfo = Fdopen(fd, "w")) == NULL) {
460 warn("%s", tempname);
461 return (fi);
462 }
463 if ((nfi = Fopen(tempname, "r")) == NULL) {
464 warn("%s", tempname);
465 (void)Fclose(nfo);
466 (void)rm(tempname);
467 return (fi);
468 }
469 (void)rm(tempname);
470 (void)puthead(hp, nfo,
471 GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO|GNL|GCOMMA);
472 c = getc(fi);
473 while (c != EOF) {
474 (void)putc(c, nfo);
475 c = getc(fi);
476 }
477 if (ferror(fi)) {
478 warnx("read");
479 rewind(fi);
480 return (fi);
481 }
482 (void)fflush(nfo);
483 if (ferror(nfo)) {
484 warn("%s", tempname);
485 (void)Fclose(nfo);
486 (void)Fclose(nfi);
487 rewind(fi);
488 return (fi);
489 }
490 (void)Fclose(nfo);
491 (void)Fclose(fi);
492 rewind(nfi);
493 return (nfi);
494 }
495
496 /*
497 * Dump the to, subject, cc header on the
498 * passed file buffer.
499 */
500 int
puthead(struct header * hp,FILE * fo,int w)501 puthead(struct header *hp, FILE *fo, int w)
502 {
503 int gotcha;
504
505 gotcha = 0;
506 if (hp->h_to != NULL && w & GTO)
507 fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
508 if (hp->h_subject != NULL && w & GSUBJECT)
509 fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
510 if (hp->h_cc != NULL && w & GCC)
511 fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
512 if (hp->h_bcc != NULL && w & GBCC)
513 fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
514 if (hp->h_replyto != NULL && w & GREPLYTO)
515 fprintf(fo, "Reply-To: %s\n", hp->h_replyto), gotcha++;
516 if (hp->h_inreplyto != NULL && w & GINREPLYTO)
517 fprintf(fo, "In-Reply-To: <%s>\n", hp->h_inreplyto), gotcha++;
518 if (gotcha && w & GNL)
519 (void)putc('\n', fo);
520 return (0);
521 }
522
523 /*
524 * Format the given header line to not exceed 72 characters.
525 */
526 void
fmt(const char * str,struct name * np,FILE * fo,int comma)527 fmt(const char *str, struct name *np, FILE *fo, int comma)
528 {
529 int col, len;
530
531 comma = comma ? 1 : 0;
532 col = strlen(str);
533 if (col)
534 fputs(str, fo);
535 for (; np != NULL; np = np->n_flink) {
536 if (np->n_flink == NULL)
537 comma = 0;
538 len = strlen(np->n_name);
539 col++; /* for the space */
540 if (col + len + comma > 72 && col > 4) {
541 fprintf(fo, "\n ");
542 col = 4;
543 } else
544 fprintf(fo, " ");
545 fputs(np->n_name, fo);
546 if (comma)
547 fprintf(fo, ",");
548 col += len + comma;
549 }
550 fprintf(fo, "\n");
551 }
552
553 /*
554 * Save the outgoing mail on the passed file.
555 */
556
557 /*ARGSUSED*/
558 int
savemail(char name[],FILE * fi)559 savemail(char name[], FILE *fi)
560 {
561 FILE *fo;
562 char buf[BUFSIZ];
563 int i;
564 time_t now;
565 mode_t saved_umask;
566
567 saved_umask = umask(077);
568 fo = Fopen(name, "a");
569 umask(saved_umask);
570
571 if (fo == NULL) {
572 warn("%s", name);
573 return (-1);
574 }
575 (void)time(&now);
576 fprintf(fo, "From %s %s", myname, ctime(&now));
577 while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
578 (void)fwrite(buf, 1, i, fo);
579 fprintf(fo, "\n");
580 (void)fflush(fo);
581 if (ferror(fo))
582 warn("%s", name);
583 (void)Fclose(fo);
584 rewind(fi);
585 return (0);
586 }
587