2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static char sccsid[] = "@(#)send.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
44 * Mail -- a mail program
50 * Send message described by the passed pointer to the
51 * passed output buffer. Return -1 on error.
52 * Adjust the status: field if need be.
53 * If doign is given, suppress ignored header fields.
54 * prefix is a string to prepend to each output line.
57 sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
62 char *cp, *cp2, line[LINESIZE];
63 int ishead, infld, ignoring, dostat, firstline;
64 int c = 0, length, prefixlen;
67 * Compute the prefix string, without trailing whitespace
71 for (cp = prefix; *cp != '\0'; cp++)
72 if (*cp != ' ' && *cp != '\t')
74 prefixlen = cp2 == NULL ? 0 : cp2 - prefix + 1;
79 dostat = doign == 0 || !isign("status", doign);
83 * Process headers first
85 while (count > 0 && ishead) {
86 if (fgets(line, sizeof(line), ibuf) == NULL)
88 count -= length = strlen(line);
91 * First line is the From line, so no headers
92 * there to worry about
95 ignoring = doign == ignoreall;
96 } else if (line[0] == '\n') {
98 * If line is blank, we've reached end of
99 * headers, so force out status: field
100 * and note that we are no longer in header
104 statusput(mp, obuf, prefix);
108 ignoring = doign == ignoreall;
109 } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
111 * If this line is a continuation (via space or tab)
112 * of a previous header field, just echo it
113 * (unless the field should be ignored).
114 * In other words, nothing to do.
118 * Pick up the header field if we have one.
120 for (cp = line; (c = *cp++) != '\0' && c != ':' &&
121 !isspace((unsigned char)c);)
124 while (isspace((unsigned char)*cp++))
128 * Not a header line, force out status:
129 * This happens in uucp style mail where
130 * there are no headers at all.
133 statusput(mp, obuf, prefix);
136 if (doign != ignoreall)
138 (void)putc('\n', obuf);
143 * If it is an ignored field and
144 * we care about such things, skip it.
146 *cp2 = '\0'; /* temporarily null terminate */
147 if (doign && isign(line, doign))
149 else if ((line[0] == 's' || line[0] == 'S') &&
150 strcasecmp(line, "status") == 0) {
152 * If the field is "status," go compute
153 * and print the real Status: field
156 statusput(mp, obuf, prefix);
162 *cp2 = c; /* restore */
169 * Strip trailing whitespace from prefix
172 if (prefix != NULL) {
176 (void)fwrite(prefix, sizeof(*prefix),
179 (void)fwrite(line, sizeof(*line), length, obuf);
185 * Copy out message body
187 if (doign == ignoreall)
188 count--; /* skip final blank line */
191 if (fgets(line, sizeof(line), ibuf) == NULL) {
195 count -= c = strlen(line);
197 * Strip trailing whitespace from prefix
203 (void)fwrite(prefix, sizeof(*prefix),
205 (void)fwrite(line, sizeof(*line), c, obuf);
211 c = count < LINESIZE ? count : LINESIZE;
212 if ((c = fread(line, sizeof(*line), c, ibuf)) <= 0)
215 if (fwrite(line, sizeof(*line), c, obuf) != c)
218 if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
219 /* no final blank line */
220 if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
226 * Output a reasonable looking status field.
229 statusput(struct message *mp, FILE *obuf, char *prefix)
234 if (mp->m_flag & MREAD)
236 if ((mp->m_flag & MNEW) == 0)
239 if (statout[0] != '\0')
240 fprintf(obuf, "%sStatus: %s\n",
241 prefix == NULL ? "" : prefix, statout);
245 * Interface between the argument list and the mail1 routine
246 * which does all the dirty work.
249 mail(struct name *to, struct name *cc, struct name *bcc, struct name *smopts,
250 char *subject, char *replyto)
255 head.h_subject = subject;
258 head.h_smopts = smopts;
259 head.h_replyto = replyto;
260 head.h_inreplyto = NULL;
267 * Send mail to a bunch of user names. The interface is through
268 * the mail routine below.
275 head.h_to = extract(str, GTO);
276 head.h_subject = NULL;
279 head.h_smopts = NULL;
280 head.h_replyto = value("REPLYTO");
281 head.h_inreplyto = NULL;
287 * Mail a message on standard input to the people indicated
288 * in the passed header. (Internal interface).
291 mail1(struct header *hp, int printheaders)
297 struct name *to, *nsto;
301 * Collect user's mail from standard input.
302 * Get the result as mtf.
304 if ((mtf = collect(hp, printheaders)) == NULL)
306 if (value("interactive") != NULL) {
307 if (value("askcc") != NULL || value("askbcc") != NULL) {
308 if (value("askcc") != NULL)
310 if (value("askbcc") != NULL)
314 (void)fflush(stdout);
317 if (fsize(mtf) == 0) {
318 if (value("dontsendempty") != NULL)
320 if (hp->h_subject == NULL)
321 printf("No message, no subject; hope that's ok\n");
323 printf("Null message body; hope that's ok\n");
326 * Now, take the user names from the combined
327 * to and cc lists and do all the alias
331 to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
333 printf("No recipients specified\n");
337 * Look through the recipient list for names with /'s
338 * in them which we write to as files directly.
340 to = outof(to, mtf, hp);
346 if (value("recordrecip") != NULL) {
348 * Before fixing the header, save old To:.
349 * We do this because elide above has sorted To: list, and
350 * we would like to save message in a file named by the first
351 * recipient the user has entered, not the one being the first
352 * after sorting happened.
354 if ((nsto = malloc(sizeof(struct name))) == NULL)
355 err(1, "Out of memory");
356 bcopy(hp->h_to, nsto, sizeof(struct name));
359 if ((mtf = infix(hp, mtf)) == NULL) {
360 fprintf(stderr, ". . . message lost, sorry.\n");
363 namelist = unpack(cat(hp->h_smopts, to));
367 printf("Sendmail arguments:");
368 for (t = namelist; *t != NULL; t++)
369 printf(" \"%s\"", *t);
373 if (value("recordrecip") != NULL) {
375 * Extract first recipient username from saved To: and use it
378 if ((nbuf = malloc(strlen(detract(nsto, 0)) + 1)) == NULL)
379 err(1, "Out of memory");
380 if ((cp = yanklogin(detract(nsto, 0), nbuf)) != NULL)
381 (void)savemail(expand(nbuf), mtf);
384 } else if ((cp = value("record")) != NULL)
385 (void)savemail(expand(cp), mtf);
387 * Fork, set up the temporary mail file as standard
388 * input for "mail", and exec with the user list we generated
399 (void)sigemptyset(&nset);
400 (void)sigaddset(&nset, SIGHUP);
401 (void)sigaddset(&nset, SIGINT);
402 (void)sigaddset(&nset, SIGQUIT);
403 (void)sigaddset(&nset, SIGTSTP);
404 (void)sigaddset(&nset, SIGTTIN);
405 (void)sigaddset(&nset, SIGTTOU);
406 prepare_child(&nset, fileno(mtf), -1);
407 if ((cp = value("sendmail")) != NULL)
415 if (value("verbose") != NULL)
416 (void)wait_child(pid);
424 * Fix the header by glopping all of the expanded names from
425 * the distribution list into the appropriate fields.
428 fixhead(struct header *hp, struct name *tolist)
435 for (np = tolist; np != NULL; np = np->n_flink) {
436 /* Don't copy deleted addresses to the header */
437 if (np->n_type & GDEL)
439 if ((np->n_type & GMASK) == GTO)
441 cat(hp->h_to, nalloc(np->n_name, np->n_type));
442 else if ((np->n_type & GMASK) == GCC)
444 cat(hp->h_cc, nalloc(np->n_name, np->n_type));
445 else if ((np->n_type & GMASK) == GBCC)
447 cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
452 * Prepend a header in front of the collected stuff
453 * and return the new file.
456 infix(struct header *hp, FILE *fi)
460 char tempname[PATHSIZE];
462 (void)snprintf(tempname, sizeof(tempname),
463 "%s/mail.RsXXXXXXXXXX", tmpdir);
464 if ((fd = mkstemp(tempname)) == -1 ||
465 (nfo = Fdopen(fd, "w")) == NULL) {
466 warn("%s", tempname);
469 if ((nfi = Fopen(tempname, "r")) == NULL) {
470 warn("%s", tempname);
476 (void)puthead(hp, nfo,
477 GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO|GNL|GCOMMA);
490 warn("%s", tempname);
503 * Dump the to, subject, cc header on the
504 * passed file buffer.
507 puthead(struct header *hp, FILE *fo, int w)
512 if (hp->h_to != NULL && w & GTO)
513 fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
514 if (hp->h_subject != NULL && w & GSUBJECT)
515 fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
516 if (hp->h_cc != NULL && w & GCC)
517 fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
518 if (hp->h_bcc != NULL && w & GBCC)
519 fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
520 if (hp->h_replyto != NULL && w & GREPLYTO)
521 fprintf(fo, "Reply-To: %s\n", hp->h_replyto), gotcha++;
522 if (hp->h_inreplyto != NULL && w & GINREPLYTO)
523 fprintf(fo, "In-Reply-To: <%s>\n", hp->h_inreplyto), gotcha++;
524 if (gotcha && w & GNL)
525 (void)putc('\n', fo);
530 * Format the given header line to not exceed 72 characters.
533 fmt(const char *str, struct name *np, FILE *fo, int comma)
537 comma = comma ? 1 : 0;
541 for (; np != NULL; np = np->n_flink) {
542 if (np->n_flink == NULL)
544 len = strlen(np->n_name);
545 col++; /* for the space */
546 if (col + len + comma > 72 && col > 4) {
551 fputs(np->n_name, fo);
560 * Save the outgoing mail on the passed file.
565 savemail(char name[], FILE *fi)
573 saved_umask = umask(077);
574 fo = Fopen(name, "a");
582 fprintf(fo, "From %s %s", myname, ctime(&now));
583 while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
584 (void)fwrite(buf, 1, i, fo);