xref: /illumos-gate/usr/src/cmd/mail/gendeliv.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "mail.h"
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * generate delivery notification if required.
287c478bd9Sstevel@tonic-gate  */
gendeliv(fp,rc,name)297c478bd9Sstevel@tonic-gate void gendeliv(fp, rc, name)
307c478bd9Sstevel@tonic-gate FILE	*fp;
317c478bd9Sstevel@tonic-gate int	rc;
327c478bd9Sstevel@tonic-gate char	*name;
337c478bd9Sstevel@tonic-gate {
347c478bd9Sstevel@tonic-gate 	static char pn[] = "gendeliv";
357c478bd9Sstevel@tonic-gate 	register char	*p;
367c478bd9Sstevel@tonic-gate 	char		buf[1024], cbuf[256], ybuf[10];
377c478bd9Sstevel@tonic-gate 	register int	i;
387c478bd9Sstevel@tonic-gate 	int		didafflines = 0, didrcvlines = 0, suppress = 0, svopts = 0;
397c478bd9Sstevel@tonic-gate 	time_t		ltmp;
407c478bd9Sstevel@tonic-gate 	register struct hdrs	*hptr;
417c478bd9Sstevel@tonic-gate 	FILE		*outfile;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	Dout(pn, 0, "at entry, fp = o%lo, rc = %d,name = '%s'\n", (long)fp, rc, name);
447c478bd9Sstevel@tonic-gate 	if (fp == (FILE *)NULL) {
457c478bd9Sstevel@tonic-gate 		/* Want to send Positive delivery notification. Need to */
467c478bd9Sstevel@tonic-gate 		/* put selected header info from orig. msg aside to */
477c478bd9Sstevel@tonic-gate 		/* avoid confusion with header info in Delivery Rpt. */
487c478bd9Sstevel@tonic-gate 		Daffbytecnt = affbytecnt; affbytecnt = 0;
497c478bd9Sstevel@tonic-gate 		Daffcnt = affcnt; affcnt = 0;
507c478bd9Sstevel@tonic-gate 		Drcvbytecnt = rcvbytecnt; rcvbytecnt = 0;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 		hdrlines[H_DAFWDFROM].head = hdrlines[H_AFWDFROM].head;
537c478bd9Sstevel@tonic-gate 		hdrlines[H_DAFWDFROM].tail = hdrlines[H_AFWDFROM].tail;
547c478bd9Sstevel@tonic-gate 		hdrlines[H_AFWDFROM].head = (struct hdrs *)NULL;
557c478bd9Sstevel@tonic-gate 		hdrlines[H_AFWDFROM].tail = (struct hdrs *)NULL;
567c478bd9Sstevel@tonic-gate 		hdrlines[H_DRECEIVED].head = hdrlines[H_RECEIVED].head;
577c478bd9Sstevel@tonic-gate 		hdrlines[H_DRECEIVED].tail = hdrlines[H_RECEIVED].tail;
587c478bd9Sstevel@tonic-gate 		hdrlines[H_RECEIVED].head = (struct hdrs *)NULL;
597c478bd9Sstevel@tonic-gate 		hdrlines[H_RECEIVED].tail = (struct hdrs *)NULL;
607c478bd9Sstevel@tonic-gate 		hdrlines[H_DTCOPY].head = hdrlines[H_TCOPY].head;
617c478bd9Sstevel@tonic-gate 		hdrlines[H_DTCOPY].tail = hdrlines[H_TCOPY].tail;
627c478bd9Sstevel@tonic-gate 		hdrlines[H_TCOPY].head = (struct hdrs *)NULL;
637c478bd9Sstevel@tonic-gate 		hdrlines[H_TCOPY].tail = (struct hdrs *)NULL;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 		pushlist (H_TCOPY, HEAD, Rpath, FALSE);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	if (rc == 0) {
687c478bd9Sstevel@tonic-gate 		/* Verify that positive delivery notification requested */
697c478bd9Sstevel@tonic-gate 		if (ckdlivopts(H_DTCOPY, &svopts) & NODELIVERY) {
707c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "pos. notif. not requested\n");
717c478bd9Sstevel@tonic-gate 			goto rtrn;
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 	} else {
747c478bd9Sstevel@tonic-gate 		/* Verify that negative delivery notification requested */
757c478bd9Sstevel@tonic-gate 		if (ckdlivopts(H_DTCOPY, &svopts) & IGNORE) {
767c478bd9Sstevel@tonic-gate 			Dout(pn, 0, "neg. notif. not requested\n");
777c478bd9Sstevel@tonic-gate 			goto rtrn;
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	if (fp == (FILE *)NULL) {
817c478bd9Sstevel@tonic-gate 		char *pargs[3];
827c478bd9Sstevel@tonic-gate 		pargs[0] = "mail";
837c478bd9Sstevel@tonic-gate 		pargs[1] = Rpath;
847c478bd9Sstevel@tonic-gate 		pargs[2] = 0;
857c478bd9Sstevel@tonic-gate 		if ((outfile = popenvp(pargs[0], pargs, "w", 1)) == (FILE *)NULL) {
867c478bd9Sstevel@tonic-gate 			/* Can't get pipe to mail. Just forget it..... */
877c478bd9Sstevel@tonic-gate 			Dout(pn, 0,"popenvp() failed\n");
887c478bd9Sstevel@tonic-gate 			goto rtrn;
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 	} else {
917c478bd9Sstevel@tonic-gate 		outfile = fp;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/* get date string into buf for later...*/
957c478bd9Sstevel@tonic-gate 	ltmp = time((time_t)0);
967c478bd9Sstevel@tonic-gate 	strcpy(buf, asctime(gmtime(&ltmp)));
977c478bd9Sstevel@tonic-gate 	/* strip year out of date string, insert 'GMT', and put year back... */
987c478bd9Sstevel@tonic-gate 	p = strrchr(buf,' ');
997c478bd9Sstevel@tonic-gate 	strcpy(ybuf,++p);
1007c478bd9Sstevel@tonic-gate 	*p = '\0';
1017c478bd9Sstevel@tonic-gate 	strcat(buf,"GMT ");
1027c478bd9Sstevel@tonic-gate 	strcat(buf, ybuf);
1037c478bd9Sstevel@tonic-gate 	trimnl(buf);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	fprintf(outfile,"%s 2\n", header[H_RVERS].tag);
1067c478bd9Sstevel@tonic-gate 	fprintf(outfile,"%s %s\n", header[H_TCOPY].tag,
1077c478bd9Sstevel@tonic-gate 					hdrlines[H_TCOPY].head->value);
1087c478bd9Sstevel@tonic-gate 	fprintf(outfile,"%s %s\n", header[H_DATE].tag, buf);
1097c478bd9Sstevel@tonic-gate 	dumprcv(ORDINARY, -1,&didrcvlines,&suppress,outfile);
1107c478bd9Sstevel@tonic-gate 	dumpaff(ORDINARY, -1,&didafflines,&suppress,outfile);
1117c478bd9Sstevel@tonic-gate 	fprintf(outfile,"Original-%s ", header[H_DATE].tag);
1127c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_DATE].head) != (struct hdrs *)NULL) {
1137c478bd9Sstevel@tonic-gate 		Dout(pn, 0,"date from H_DATE = '%s'\n", hptr->value);
1147c478bd9Sstevel@tonic-gate 		fprintf(outfile,"%s\n", hptr->value);
1157c478bd9Sstevel@tonic-gate 	} else {
1167c478bd9Sstevel@tonic-gate 		/* If no H_DATE line in original message, use date */
1177c478bd9Sstevel@tonic-gate 		/* in last UNIX H_FROM1 or H_FROM line */
1187c478bd9Sstevel@tonic-gate 		if ((hptr = hdrlines[H_FROM1].tail) == (struct hdrs *)NULL) {
1197c478bd9Sstevel@tonic-gate 			hptr = hdrlines[H_FROM].tail;
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		Dout(pn, 0,"date from H_FROM = '%s'\n", hptr->value);
1227c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, hptr->value, sizeof (buf));
1237c478bd9Sstevel@tonic-gate 		/* Find date portion of line. */
1247c478bd9Sstevel@tonic-gate 		/* Assumes line is of form - */
1257c478bd9Sstevel@tonic-gate 		/*       'name_date_[remote_from_sys|forwarded_by_name]' */
1267c478bd9Sstevel@tonic-gate 		if ((p = strchr(buf,' ')) == (char *)NULL) {
1277c478bd9Sstevel@tonic-gate 			strcpy(buf, "No valid datestamp in original.");
1287c478bd9Sstevel@tonic-gate 		} else {
1297c478bd9Sstevel@tonic-gate 			(void) strlcpy(buf, p++, sizeof (buf));
1307c478bd9Sstevel@tonic-gate 			/* Walk backwards from end of string to 3rd blank, */
1317c478bd9Sstevel@tonic-gate 			/* and then check for 'remote from' or 'forwarded by' */
1327c478bd9Sstevel@tonic-gate 			/* If either found, truncate there, else use entire */
1337c478bd9Sstevel@tonic-gate 			/* string. */
1347c478bd9Sstevel@tonic-gate 			p = buf + strlen(buf) - 1;
1357c478bd9Sstevel@tonic-gate 			i = 0;
1367c478bd9Sstevel@tonic-gate 			while (p > buf) {
1377c478bd9Sstevel@tonic-gate 				if (*p == ' ') {
1387c478bd9Sstevel@tonic-gate 					if (++i == 3) {
1397c478bd9Sstevel@tonic-gate 						break;
1407c478bd9Sstevel@tonic-gate 					}
1417c478bd9Sstevel@tonic-gate 				}
1427c478bd9Sstevel@tonic-gate 				p--;
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 			if ((i != 3) || (p <= buf)) {
1457c478bd9Sstevel@tonic-gate 				strcpy(buf, "No valid datestamp in original.");
1467c478bd9Sstevel@tonic-gate 			} else {
1477c478bd9Sstevel@tonic-gate 				if ((strncmp((p+1),"remote from", 11) == 0) ||
1487c478bd9Sstevel@tonic-gate 				    (strncmp((p+1),"forwarded by", 12) == 0)) {
1497c478bd9Sstevel@tonic-gate 					*p = '\0';
1507c478bd9Sstevel@tonic-gate 				}
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		fprintf(outfile,"%s\n", buf);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_SUBJ].head) != (struct hdrs *)NULL) {
1567c478bd9Sstevel@tonic-gate 		fprintf(outfile,"Original-%s %s\n",
1577c478bd9Sstevel@tonic-gate 				header[H_SUBJ].tag, hptr->value);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_MSVC].head) != (struct hdrs *)NULL) {
1607c478bd9Sstevel@tonic-gate 		if ((strlen(hptr->value) != 4) ||
1617c478bd9Sstevel@tonic-gate 		    (casncmp("mail", hptr->value, 4) != 0)) {
162*2a8bcb4eSToomas Soome 			fprintf(outfile,"Original-%s %s\n",
1637c478bd9Sstevel@tonic-gate 					header[H_MSVC].tag, hptr->value);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_MTSID].head) != (struct hdrs *)NULL) {
167*2a8bcb4eSToomas Soome 		fprintf(outfile,"Confirming-%s <%s>\n",
1687c478bd9Sstevel@tonic-gate 				header[H_MTSID].tag, hptr->value);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_UAID].head) != (struct hdrs *)NULL) {
1717c478bd9Sstevel@tonic-gate 		fprintf(outfile,"Confirming-%s <%s>\n",
1727c478bd9Sstevel@tonic-gate 				header[H_UAID].tag, hptr->value);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	cbuf[0] = '\0';
1757c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_DTCOPY].head) != (struct hdrs *)NULL) {
1767c478bd9Sstevel@tonic-gate 		/* Pick comment field off of ">To:" line and put into cbuf */
1777c478bd9Sstevel@tonic-gate 		getcomment(hptr->value, cbuf);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	if (rc == 0) {
1807c478bd9Sstevel@tonic-gate 		fprintf(outfile,"Delivered-To: %s!%s %s on %s\n",
1817c478bd9Sstevel@tonic-gate 						thissys, name, cbuf, buf);
1827c478bd9Sstevel@tonic-gate 	} else {
1837c478bd9Sstevel@tonic-gate 		(void) strlcpy (buf, name, sizeof (buf));
1847c478bd9Sstevel@tonic-gate 		if ((p = strchr(buf,'!')) != (char *)NULL) {
1857c478bd9Sstevel@tonic-gate 			*p = '\0';
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		fprintf(outfile,"Not-Delivered-To: %s!%s %s due to ",
1887c478bd9Sstevel@tonic-gate 			thissys, buf,
1897c478bd9Sstevel@tonic-gate 			/* if en-route-to, put comment there, else put it here*/
1907c478bd9Sstevel@tonic-gate 			((p == (char *)NULL) ? cbuf : ""));
1917c478bd9Sstevel@tonic-gate 		mta_ercode(outfile);
1927c478bd9Sstevel@tonic-gate 		if (ckdlivopts(H_DTCOPY, &svopts) & RETURN) {
1937c478bd9Sstevel@tonic-gate 			fprintf(outfile,"     ORIGINAL MESSAGE ATTACHED\n");
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		if (error == E_FRWL) {
1977c478bd9Sstevel@tonic-gate 			fprintf(outfile, frwlmsg, program, uval);
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			fprintf(outfile, "     (%s: Error # %d '%s'",
2007c478bd9Sstevel@tonic-gate 					program,error,errlist[error]);
2017c478bd9Sstevel@tonic-gate 			if (error == E_SURG) {
2027c478bd9Sstevel@tonic-gate 				fprintf(outfile,", rc = %d)\n",surg_rc);
2037c478bd9Sstevel@tonic-gate 				fprintf(outfile,
2047c478bd9Sstevel@tonic-gate 				    "     ======= Surrogate command =======\n");
2057c478bd9Sstevel@tonic-gate 				fprintf(outfile,"     %s\n",
2067c478bd9Sstevel@tonic-gate 					((SURRcmdstr == (char *)NULL) ?
2077c478bd9Sstevel@tonic-gate 							      "" : SURRcmdstr));
2087c478bd9Sstevel@tonic-gate 				/* Include stderr from surrogate, if any */
2097c478bd9Sstevel@tonic-gate 				if (SURRerrfile) {
2107c478bd9Sstevel@tonic-gate 					fprintf(outfile,
2117c478bd9Sstevel@tonic-gate 					    "     ==== Start of stdout & stderr ===\n");
2127c478bd9Sstevel@tonic-gate 					rewind (SURRerrfile);
2137c478bd9Sstevel@tonic-gate 					while (fgets(buf, sizeof(buf), SURRerrfile) !=
2147c478bd9Sstevel@tonic-gate 									(char *)NULL) {
2157c478bd9Sstevel@tonic-gate 						fprintf(outfile,"     %s", buf);
2167c478bd9Sstevel@tonic-gate 					}
2177c478bd9Sstevel@tonic-gate 					if (buf[strlen(buf)-1] != '\n') {
2187c478bd9Sstevel@tonic-gate 						fprintf(outfile,"\n");
2197c478bd9Sstevel@tonic-gate 					}
2207c478bd9Sstevel@tonic-gate 					fprintf(outfile,
2217c478bd9Sstevel@tonic-gate 					    "     ====  End of stdout & stderr  ===\n");
2227c478bd9Sstevel@tonic-gate 				} else
2237c478bd9Sstevel@tonic-gate 					fprintf(outfile,
2247c478bd9Sstevel@tonic-gate 					    "     ==== stdout & stderr unavailable ===\n");
2257c478bd9Sstevel@tonic-gate 			} else {
2267c478bd9Sstevel@tonic-gate 				fprintf(outfile,")\n");
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		if (p != (char *)NULL) {
2307c478bd9Sstevel@tonic-gate 			fprintf(outfile, "En-Route-To: %s %s\n", name, cbuf);
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	if ((hptr = hdrlines[H_DAFWDFROM].head) != (struct hdrs *)NULL) {
2347c478bd9Sstevel@tonic-gate 		while (hptr != (struct hdrs *)NULL) {
2357c478bd9Sstevel@tonic-gate 			fprintf(outfile,"Original-%s %s\n",
2367c478bd9Sstevel@tonic-gate 				header[H_DAFWDFROM].tag, hptr->value);
2377c478bd9Sstevel@tonic-gate 			hptr = hptr->next;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	fprintf(outfile,"%s\n", header[H_EOH].tag);
2417c478bd9Sstevel@tonic-gate 	if (fp == (FILE *)NULL) {
2427c478bd9Sstevel@tonic-gate 		pclosevp(outfile);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	Dout(pn, 5, "notification sent.\n");
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate     rtrn:
2477c478bd9Sstevel@tonic-gate 	/* Restore header info from original message. (see above and also */
2487c478bd9Sstevel@tonic-gate 	/* goback()). */
2497c478bd9Sstevel@tonic-gate 	clrhdr(H_TCOPY);
2507c478bd9Sstevel@tonic-gate 	clrhdr(H_AFWDFROM);
2517c478bd9Sstevel@tonic-gate 	clrhdr(H_RECEIVED);
2527c478bd9Sstevel@tonic-gate 	affbytecnt = Daffbytecnt; Daffbytecnt = 0;
2537c478bd9Sstevel@tonic-gate 	affcnt = Daffcnt; Daffcnt = 0;
2547c478bd9Sstevel@tonic-gate 	rcvbytecnt = Drcvbytecnt; Drcvbytecnt = 0;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	hdrlines[H_AFWDFROM].head = hdrlines[H_DAFWDFROM].head;
2577c478bd9Sstevel@tonic-gate 	hdrlines[H_AFWDFROM].tail = hdrlines[H_DAFWDFROM].tail;
2587c478bd9Sstevel@tonic-gate 	hdrlines[H_DAFWDFROM].head = (struct hdrs *)NULL;
2597c478bd9Sstevel@tonic-gate 	hdrlines[H_DAFWDFROM].tail = (struct hdrs *)NULL;
2607c478bd9Sstevel@tonic-gate 	hdrlines[H_RECEIVED].head = hdrlines[H_DRECEIVED].head;
2617c478bd9Sstevel@tonic-gate 	hdrlines[H_RECEIVED].tail = hdrlines[H_DRECEIVED].tail;
2627c478bd9Sstevel@tonic-gate 	hdrlines[H_DRECEIVED].head = (struct hdrs *)NULL;
2637c478bd9Sstevel@tonic-gate 	hdrlines[H_DRECEIVED].tail = (struct hdrs *)NULL;
2647c478bd9Sstevel@tonic-gate 	hdrlines[H_TCOPY].head = hdrlines[H_DTCOPY].head;
2657c478bd9Sstevel@tonic-gate 	hdrlines[H_TCOPY].tail = hdrlines[H_DTCOPY].tail;
2667c478bd9Sstevel@tonic-gate 	hdrlines[H_DTCOPY].head = (struct hdrs *)NULL;
2677c478bd9Sstevel@tonic-gate 	hdrlines[H_DTCOPY].tail = (struct hdrs *)NULL;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return;
2707c478bd9Sstevel@tonic-gate }
271