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
36 * Mail -- a mail program
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.
49 sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
54 char *cp, *cp2, line[LINESIZE];
55 int ishead, infld, ignoring, dostat, firstline;
56 int c = 0, length, prefixlen;
59 * Compute the prefix string, without trailing whitespace
63 for (cp = prefix; *cp != '\0'; cp++)
64 if (*cp != ' ' && *cp != '\t')
66 prefixlen = cp2 == NULL ? 0 : cp2 - prefix + 1;
71 dostat = doign == 0 || !isign("status", doign);
75 * Process headers first
77 while (count > 0 && ishead) {
78 if (fgets(line, sizeof(line), ibuf) == NULL)
80 count -= length = strlen(line);
83 * First line is the From line, so no headers
84 * there to worry about
87 ignoring = doign == ignoreall;
88 } else if (line[0] == '\n') {
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
96 statusput(mp, obuf, prefix);
100 ignoring = doign == ignoreall;
101 } else if (infld && (line[0] == ' ' || line[0] == '\t')) {
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.
110 * Pick up the header field if we have one.
112 for (cp = line; (c = *cp++) != '\0' && c != ':' &&
113 !isspace((unsigned char)c);)
116 while (isspace((unsigned char)*cp++))
120 * Not a header line, force out status:
121 * This happens in uucp style mail where
122 * there are no headers at all.
125 statusput(mp, obuf, prefix);
128 if (doign != ignoreall)
130 (void)putc('\n', obuf);
135 * If it is an ignored field and
136 * we care about such things, skip it.
138 *cp2 = '\0'; /* temporarily null terminate */
139 if (doign && isign(line, doign))
141 else if ((line[0] == 's' || line[0] == 'S') &&
142 strcasecmp(line, "status") == 0) {
144 * If the field is "status," go compute
145 * and print the real Status: field
148 statusput(mp, obuf, prefix);
154 *cp2 = c; /* restore */
161 * Strip trailing whitespace from prefix
164 if (prefix != NULL) {
168 (void)fwrite(prefix, sizeof(*prefix),
171 (void)fwrite(line, sizeof(*line), length, obuf);
177 * Copy out message body
179 if (doign == ignoreall)
180 count--; /* skip final blank line */
183 if (fgets(line, sizeof(line), ibuf) == NULL) {
187 count -= c = strlen(line);
189 * Strip trailing whitespace from prefix
195 (void)fwrite(prefix, sizeof(*prefix),
197 (void)fwrite(line, sizeof(*line), c, obuf);
203 c = count < LINESIZE ? count : LINESIZE;
204 if ((c = fread(line, sizeof(*line), c, ibuf)) <= 0)
207 if (fwrite(line, sizeof(*line), c, obuf) != c)
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)
218 * Output a reasonable looking status field.
221 statusput(struct message *mp, FILE *obuf, char *prefix)
226 if (mp->m_flag & MREAD)
228 if ((mp->m_flag & MNEW) == 0)
231 if (statout[0] != '\0')
232 fprintf(obuf, "%sStatus: %s\n",
233 prefix == NULL ? "" : prefix, statout);
237 * Interface between the argument list and the mail1 routine
238 * which does all the dirty work.
241 mail(struct name *to, struct name *cc, struct name *bcc, struct name *smopts,
242 char *subject, char *replyto)
247 head.h_subject = subject;
250 head.h_smopts = smopts;
251 head.h_replyto = replyto;
252 head.h_inreplyto = NULL;
259 * Send mail to a bunch of user names. The interface is through
260 * the mail routine below.
267 head.h_to = extract(str, GTO);
268 head.h_subject = NULL;
271 head.h_smopts = NULL;
272 head.h_replyto = value("REPLYTO");
273 head.h_inreplyto = NULL;
279 * Mail a message on standard input to the people indicated
280 * in the passed header. (Internal interface).
283 mail1(struct header *hp, int printheaders)
289 struct name *to, *nsto;
293 * Collect user's mail from standard input.
294 * Get the result as mtf.
296 if ((mtf = collect(hp, printheaders)) == NULL)
298 if (value("interactive") != NULL) {
299 if (value("askcc") != NULL || value("askbcc") != NULL) {
300 if (value("askcc") != NULL)
302 if (value("askbcc") != NULL)
306 (void)fflush(stdout);
309 if (fsize(mtf) == 0) {
310 if (value("dontsendempty") != NULL)
312 if (hp->h_subject == NULL)
313 printf("No message, no subject; hope that's ok\n");
315 printf("Null message body; hope that's ok\n");
318 * Now, take the user names from the combined
319 * to and cc lists and do all the alias
323 to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
325 printf("No recipients specified\n");
329 * Look through the recipient list for names with /'s
330 * in them which we write to as files directly.
332 to = outof(to, mtf, hp);
338 if (value("recordrecip") != NULL) {
340 * Before fixing the header, save old To:.
341 * We do this because elide above has sorted To: list, and
342 * we would like to save message in a file named by the first
343 * recipient the user has entered, not the one being the first
344 * after sorting happened.
346 if ((nsto = malloc(sizeof(struct name))) == NULL)
347 err(1, "Out of memory");
348 bcopy(hp->h_to, nsto, sizeof(struct name));
351 if ((mtf = infix(hp, mtf)) == NULL) {
352 fprintf(stderr, ". . . message lost, sorry.\n");
355 namelist = unpack(cat(hp->h_smopts, to));
359 printf("Sendmail arguments:");
360 for (t = namelist; *t != NULL; t++)
361 printf(" \"%s\"", *t);
365 if (value("recordrecip") != NULL) {
367 * Extract first recipient username from saved To: and use it
370 if ((nbuf = malloc(strlen(detract(nsto, 0)) + 1)) == NULL)
371 err(1, "Out of memory");
372 if ((cp = yanklogin(detract(nsto, 0), nbuf)) != NULL)
373 (void)savemail(expand(nbuf), mtf);
376 } else if ((cp = value("record")) != NULL)
377 (void)savemail(expand(cp), mtf);
379 * Fork, set up the temporary mail file as standard
380 * input for "mail", and exec with the user list we generated
391 (void)sigemptyset(&nset);
392 (void)sigaddset(&nset, SIGHUP);
393 (void)sigaddset(&nset, SIGINT);
394 (void)sigaddset(&nset, SIGQUIT);
395 (void)sigaddset(&nset, SIGTSTP);
396 (void)sigaddset(&nset, SIGTTIN);
397 (void)sigaddset(&nset, SIGTTOU);
398 prepare_child(&nset, fileno(mtf), -1);
399 if ((cp = value("sendmail")) != NULL)
407 if (value("verbose") != NULL)
408 (void)wait_child(pid);
416 * Fix the header by glopping all of the expanded names from
417 * the distribution list into the appropriate fields.
420 fixhead(struct header *hp, struct name *tolist)
427 for (np = tolist; np != NULL; np = np->n_flink) {
428 /* Don't copy deleted addresses to the header */
429 if (np->n_type & GDEL)
431 if ((np->n_type & GMASK) == GTO)
433 cat(hp->h_to, nalloc(np->n_name, np->n_type));
434 else if ((np->n_type & GMASK) == GCC)
436 cat(hp->h_cc, nalloc(np->n_name, np->n_type));
437 else if ((np->n_type & GMASK) == GBCC)
439 cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
444 * Prepend a header in front of the collected stuff
445 * and return the new file.
448 infix(struct header *hp, FILE *fi)
452 char tempname[PATHSIZE];
454 (void)snprintf(tempname, sizeof(tempname),
455 "%s/mail.RsXXXXXXXXXX", tmpdir);
456 if ((fd = mkstemp(tempname)) == -1 ||
457 (nfo = Fdopen(fd, "w")) == NULL) {
458 warn("%s", tempname);
461 if ((nfi = Fopen(tempname, "r")) == NULL) {
462 warn("%s", tempname);
468 (void)puthead(hp, nfo,
469 GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO|GNL|GCOMMA);
482 warn("%s", tempname);
495 * Dump the to, subject, cc header on the
496 * passed file buffer.
499 puthead(struct header *hp, FILE *fo, int w)
504 if (hp->h_to != NULL && w & GTO)
505 fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
506 if (hp->h_subject != NULL && w & GSUBJECT)
507 fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
508 if (hp->h_cc != NULL && w & GCC)
509 fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
510 if (hp->h_bcc != NULL && w & GBCC)
511 fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
512 if (hp->h_replyto != NULL && w & GREPLYTO)
513 fprintf(fo, "Reply-To: %s\n", hp->h_replyto), gotcha++;
514 if (hp->h_inreplyto != NULL && w & GINREPLYTO)
515 fprintf(fo, "In-Reply-To: <%s>\n", hp->h_inreplyto), gotcha++;
516 if (gotcha && w & GNL)
517 (void)putc('\n', fo);
522 * Format the given header line to not exceed 72 characters.
525 fmt(const char *str, struct name *np, FILE *fo, int comma)
529 comma = comma ? 1 : 0;
533 for (; np != NULL; np = np->n_flink) {
534 if (np->n_flink == NULL)
536 len = strlen(np->n_name);
537 col++; /* for the space */
538 if (col + len + comma > 72 && col > 4) {
543 fputs(np->n_name, fo);
552 * Save the outgoing mail on the passed file.
557 savemail(char name[], FILE *fi)
565 saved_umask = umask(077);
566 fo = Fopen(name, "a");
574 fprintf(fo, "From %s %s", myname, ctime(&now));
575 while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
576 (void)fwrite(buf, 1, i, fo);