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