1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
24*7c478bd9Sstevel@tonic-gate
25*7c478bd9Sstevel@tonic-gate #include "mail.h"
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate * generate delivery notification if required.
28*7c478bd9Sstevel@tonic-gate */
gendeliv(fp,rc,name)29*7c478bd9Sstevel@tonic-gate void gendeliv(fp, rc, name)
30*7c478bd9Sstevel@tonic-gate FILE *fp;
31*7c478bd9Sstevel@tonic-gate int rc;
32*7c478bd9Sstevel@tonic-gate char *name;
33*7c478bd9Sstevel@tonic-gate {
34*7c478bd9Sstevel@tonic-gate static char pn[] = "gendeliv";
35*7c478bd9Sstevel@tonic-gate register char *p;
36*7c478bd9Sstevel@tonic-gate char buf[1024], cbuf[256], ybuf[10];
37*7c478bd9Sstevel@tonic-gate register int i;
38*7c478bd9Sstevel@tonic-gate int didafflines = 0, didrcvlines = 0, suppress = 0, svopts = 0;
39*7c478bd9Sstevel@tonic-gate time_t ltmp;
40*7c478bd9Sstevel@tonic-gate register struct hdrs *hptr;
41*7c478bd9Sstevel@tonic-gate FILE *outfile;
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate Dout(pn, 0, "at entry, fp = o%lo, rc = %d,name = '%s'\n", (long)fp, rc, name);
44*7c478bd9Sstevel@tonic-gate if (fp == (FILE *)NULL) {
45*7c478bd9Sstevel@tonic-gate /* Want to send Positive delivery notification. Need to */
46*7c478bd9Sstevel@tonic-gate /* put selected header info from orig. msg aside to */
47*7c478bd9Sstevel@tonic-gate /* avoid confusion with header info in Delivery Rpt. */
48*7c478bd9Sstevel@tonic-gate Daffbytecnt = affbytecnt; affbytecnt = 0;
49*7c478bd9Sstevel@tonic-gate Daffcnt = affcnt; affcnt = 0;
50*7c478bd9Sstevel@tonic-gate Drcvbytecnt = rcvbytecnt; rcvbytecnt = 0;
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate hdrlines[H_DAFWDFROM].head = hdrlines[H_AFWDFROM].head;
53*7c478bd9Sstevel@tonic-gate hdrlines[H_DAFWDFROM].tail = hdrlines[H_AFWDFROM].tail;
54*7c478bd9Sstevel@tonic-gate hdrlines[H_AFWDFROM].head = (struct hdrs *)NULL;
55*7c478bd9Sstevel@tonic-gate hdrlines[H_AFWDFROM].tail = (struct hdrs *)NULL;
56*7c478bd9Sstevel@tonic-gate hdrlines[H_DRECEIVED].head = hdrlines[H_RECEIVED].head;
57*7c478bd9Sstevel@tonic-gate hdrlines[H_DRECEIVED].tail = hdrlines[H_RECEIVED].tail;
58*7c478bd9Sstevel@tonic-gate hdrlines[H_RECEIVED].head = (struct hdrs *)NULL;
59*7c478bd9Sstevel@tonic-gate hdrlines[H_RECEIVED].tail = (struct hdrs *)NULL;
60*7c478bd9Sstevel@tonic-gate hdrlines[H_DTCOPY].head = hdrlines[H_TCOPY].head;
61*7c478bd9Sstevel@tonic-gate hdrlines[H_DTCOPY].tail = hdrlines[H_TCOPY].tail;
62*7c478bd9Sstevel@tonic-gate hdrlines[H_TCOPY].head = (struct hdrs *)NULL;
63*7c478bd9Sstevel@tonic-gate hdrlines[H_TCOPY].tail = (struct hdrs *)NULL;
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate pushlist (H_TCOPY, HEAD, Rpath, FALSE);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate if (rc == 0) {
68*7c478bd9Sstevel@tonic-gate /* Verify that positive delivery notification requested */
69*7c478bd9Sstevel@tonic-gate if (ckdlivopts(H_DTCOPY, &svopts) & NODELIVERY) {
70*7c478bd9Sstevel@tonic-gate Dout(pn, 0, "pos. notif. not requested\n");
71*7c478bd9Sstevel@tonic-gate goto rtrn;
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate } else {
74*7c478bd9Sstevel@tonic-gate /* Verify that negative delivery notification requested */
75*7c478bd9Sstevel@tonic-gate if (ckdlivopts(H_DTCOPY, &svopts) & IGNORE) {
76*7c478bd9Sstevel@tonic-gate Dout(pn, 0, "neg. notif. not requested\n");
77*7c478bd9Sstevel@tonic-gate goto rtrn;
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate if (fp == (FILE *)NULL) {
81*7c478bd9Sstevel@tonic-gate char *pargs[3];
82*7c478bd9Sstevel@tonic-gate pargs[0] = "mail";
83*7c478bd9Sstevel@tonic-gate pargs[1] = Rpath;
84*7c478bd9Sstevel@tonic-gate pargs[2] = 0;
85*7c478bd9Sstevel@tonic-gate if ((outfile = popenvp(pargs[0], pargs, "w", 1)) == (FILE *)NULL) {
86*7c478bd9Sstevel@tonic-gate /* Can't get pipe to mail. Just forget it..... */
87*7c478bd9Sstevel@tonic-gate Dout(pn, 0,"popenvp() failed\n");
88*7c478bd9Sstevel@tonic-gate goto rtrn;
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate } else {
91*7c478bd9Sstevel@tonic-gate outfile = fp;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate /* get date string into buf for later...*/
95*7c478bd9Sstevel@tonic-gate ltmp = time((time_t)0);
96*7c478bd9Sstevel@tonic-gate strcpy(buf, asctime(gmtime(<mp)));
97*7c478bd9Sstevel@tonic-gate /* strip year out of date string, insert 'GMT', and put year back... */
98*7c478bd9Sstevel@tonic-gate p = strrchr(buf,' ');
99*7c478bd9Sstevel@tonic-gate strcpy(ybuf,++p);
100*7c478bd9Sstevel@tonic-gate *p = '\0';
101*7c478bd9Sstevel@tonic-gate strcat(buf,"GMT ");
102*7c478bd9Sstevel@tonic-gate strcat(buf, ybuf);
103*7c478bd9Sstevel@tonic-gate trimnl(buf);
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate fprintf(outfile,"%s 2\n", header[H_RVERS].tag);
106*7c478bd9Sstevel@tonic-gate fprintf(outfile,"%s %s\n", header[H_TCOPY].tag,
107*7c478bd9Sstevel@tonic-gate hdrlines[H_TCOPY].head->value);
108*7c478bd9Sstevel@tonic-gate fprintf(outfile,"%s %s\n", header[H_DATE].tag, buf);
109*7c478bd9Sstevel@tonic-gate dumprcv(ORDINARY, -1,&didrcvlines,&suppress,outfile);
110*7c478bd9Sstevel@tonic-gate dumpaff(ORDINARY, -1,&didafflines,&suppress,outfile);
111*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Original-%s ", header[H_DATE].tag);
112*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_DATE].head) != (struct hdrs *)NULL) {
113*7c478bd9Sstevel@tonic-gate Dout(pn, 0,"date from H_DATE = '%s'\n", hptr->value);
114*7c478bd9Sstevel@tonic-gate fprintf(outfile,"%s\n", hptr->value);
115*7c478bd9Sstevel@tonic-gate } else {
116*7c478bd9Sstevel@tonic-gate /* If no H_DATE line in original message, use date */
117*7c478bd9Sstevel@tonic-gate /* in last UNIX H_FROM1 or H_FROM line */
118*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_FROM1].tail) == (struct hdrs *)NULL) {
119*7c478bd9Sstevel@tonic-gate hptr = hdrlines[H_FROM].tail;
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate Dout(pn, 0,"date from H_FROM = '%s'\n", hptr->value);
122*7c478bd9Sstevel@tonic-gate (void) strlcpy(buf, hptr->value, sizeof (buf));
123*7c478bd9Sstevel@tonic-gate /* Find date portion of line. */
124*7c478bd9Sstevel@tonic-gate /* Assumes line is of form - */
125*7c478bd9Sstevel@tonic-gate /* 'name_date_[remote_from_sys|forwarded_by_name]' */
126*7c478bd9Sstevel@tonic-gate if ((p = strchr(buf,' ')) == (char *)NULL) {
127*7c478bd9Sstevel@tonic-gate strcpy(buf, "No valid datestamp in original.");
128*7c478bd9Sstevel@tonic-gate } else {
129*7c478bd9Sstevel@tonic-gate (void) strlcpy(buf, p++, sizeof (buf));
130*7c478bd9Sstevel@tonic-gate /* Walk backwards from end of string to 3rd blank, */
131*7c478bd9Sstevel@tonic-gate /* and then check for 'remote from' or 'forwarded by' */
132*7c478bd9Sstevel@tonic-gate /* If either found, truncate there, else use entire */
133*7c478bd9Sstevel@tonic-gate /* string. */
134*7c478bd9Sstevel@tonic-gate p = buf + strlen(buf) - 1;
135*7c478bd9Sstevel@tonic-gate i = 0;
136*7c478bd9Sstevel@tonic-gate while (p > buf) {
137*7c478bd9Sstevel@tonic-gate if (*p == ' ') {
138*7c478bd9Sstevel@tonic-gate if (++i == 3) {
139*7c478bd9Sstevel@tonic-gate break;
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate p--;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate if ((i != 3) || (p <= buf)) {
145*7c478bd9Sstevel@tonic-gate strcpy(buf, "No valid datestamp in original.");
146*7c478bd9Sstevel@tonic-gate } else {
147*7c478bd9Sstevel@tonic-gate if ((strncmp((p+1),"remote from", 11) == 0) ||
148*7c478bd9Sstevel@tonic-gate (strncmp((p+1),"forwarded by", 12) == 0)) {
149*7c478bd9Sstevel@tonic-gate *p = '\0';
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate fprintf(outfile,"%s\n", buf);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_SUBJ].head) != (struct hdrs *)NULL) {
156*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Original-%s %s\n",
157*7c478bd9Sstevel@tonic-gate header[H_SUBJ].tag, hptr->value);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_MSVC].head) != (struct hdrs *)NULL) {
160*7c478bd9Sstevel@tonic-gate if ((strlen(hptr->value) != 4) ||
161*7c478bd9Sstevel@tonic-gate (casncmp("mail", hptr->value, 4) != 0)) {
162*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Original-%s %s\n",
163*7c478bd9Sstevel@tonic-gate header[H_MSVC].tag, hptr->value);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_MTSID].head) != (struct hdrs *)NULL) {
167*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Confirming-%s <%s>\n",
168*7c478bd9Sstevel@tonic-gate header[H_MTSID].tag, hptr->value);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_UAID].head) != (struct hdrs *)NULL) {
171*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Confirming-%s <%s>\n",
172*7c478bd9Sstevel@tonic-gate header[H_UAID].tag, hptr->value);
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate cbuf[0] = '\0';
175*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_DTCOPY].head) != (struct hdrs *)NULL) {
176*7c478bd9Sstevel@tonic-gate /* Pick comment field off of ">To:" line and put into cbuf */
177*7c478bd9Sstevel@tonic-gate getcomment(hptr->value, cbuf);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate if (rc == 0) {
180*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Delivered-To: %s!%s %s on %s\n",
181*7c478bd9Sstevel@tonic-gate thissys, name, cbuf, buf);
182*7c478bd9Sstevel@tonic-gate } else {
183*7c478bd9Sstevel@tonic-gate (void) strlcpy (buf, name, sizeof (buf));
184*7c478bd9Sstevel@tonic-gate if ((p = strchr(buf,'!')) != (char *)NULL) {
185*7c478bd9Sstevel@tonic-gate *p = '\0';
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Not-Delivered-To: %s!%s %s due to ",
188*7c478bd9Sstevel@tonic-gate thissys, buf,
189*7c478bd9Sstevel@tonic-gate /* if en-route-to, put comment there, else put it here*/
190*7c478bd9Sstevel@tonic-gate ((p == (char *)NULL) ? cbuf : ""));
191*7c478bd9Sstevel@tonic-gate mta_ercode(outfile);
192*7c478bd9Sstevel@tonic-gate if (ckdlivopts(H_DTCOPY, &svopts) & RETURN) {
193*7c478bd9Sstevel@tonic-gate fprintf(outfile," ORIGINAL MESSAGE ATTACHED\n");
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate
196*7c478bd9Sstevel@tonic-gate if (error == E_FRWL) {
197*7c478bd9Sstevel@tonic-gate fprintf(outfile, frwlmsg, program, uval);
198*7c478bd9Sstevel@tonic-gate } else {
199*7c478bd9Sstevel@tonic-gate fprintf(outfile, " (%s: Error # %d '%s'",
200*7c478bd9Sstevel@tonic-gate program,error,errlist[error]);
201*7c478bd9Sstevel@tonic-gate if (error == E_SURG) {
202*7c478bd9Sstevel@tonic-gate fprintf(outfile,", rc = %d)\n",surg_rc);
203*7c478bd9Sstevel@tonic-gate fprintf(outfile,
204*7c478bd9Sstevel@tonic-gate " ======= Surrogate command =======\n");
205*7c478bd9Sstevel@tonic-gate fprintf(outfile," %s\n",
206*7c478bd9Sstevel@tonic-gate ((SURRcmdstr == (char *)NULL) ?
207*7c478bd9Sstevel@tonic-gate "" : SURRcmdstr));
208*7c478bd9Sstevel@tonic-gate /* Include stderr from surrogate, if any */
209*7c478bd9Sstevel@tonic-gate if (SURRerrfile) {
210*7c478bd9Sstevel@tonic-gate fprintf(outfile,
211*7c478bd9Sstevel@tonic-gate " ==== Start of stdout & stderr ===\n");
212*7c478bd9Sstevel@tonic-gate rewind (SURRerrfile);
213*7c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof(buf), SURRerrfile) !=
214*7c478bd9Sstevel@tonic-gate (char *)NULL) {
215*7c478bd9Sstevel@tonic-gate fprintf(outfile," %s", buf);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate if (buf[strlen(buf)-1] != '\n') {
218*7c478bd9Sstevel@tonic-gate fprintf(outfile,"\n");
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate fprintf(outfile,
221*7c478bd9Sstevel@tonic-gate " ==== End of stdout & stderr ===\n");
222*7c478bd9Sstevel@tonic-gate } else
223*7c478bd9Sstevel@tonic-gate fprintf(outfile,
224*7c478bd9Sstevel@tonic-gate " ==== stdout & stderr unavailable ===\n");
225*7c478bd9Sstevel@tonic-gate } else {
226*7c478bd9Sstevel@tonic-gate fprintf(outfile,")\n");
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate if (p != (char *)NULL) {
230*7c478bd9Sstevel@tonic-gate fprintf(outfile, "En-Route-To: %s %s\n", name, cbuf);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate if ((hptr = hdrlines[H_DAFWDFROM].head) != (struct hdrs *)NULL) {
234*7c478bd9Sstevel@tonic-gate while (hptr != (struct hdrs *)NULL) {
235*7c478bd9Sstevel@tonic-gate fprintf(outfile,"Original-%s %s\n",
236*7c478bd9Sstevel@tonic-gate header[H_DAFWDFROM].tag, hptr->value);
237*7c478bd9Sstevel@tonic-gate hptr = hptr->next;
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate fprintf(outfile,"%s\n", header[H_EOH].tag);
241*7c478bd9Sstevel@tonic-gate if (fp == (FILE *)NULL) {
242*7c478bd9Sstevel@tonic-gate pclosevp(outfile);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate Dout(pn, 5, "notification sent.\n");
245*7c478bd9Sstevel@tonic-gate
246*7c478bd9Sstevel@tonic-gate rtrn:
247*7c478bd9Sstevel@tonic-gate /* Restore header info from original message. (see above and also */
248*7c478bd9Sstevel@tonic-gate /* goback()). */
249*7c478bd9Sstevel@tonic-gate clrhdr(H_TCOPY);
250*7c478bd9Sstevel@tonic-gate clrhdr(H_AFWDFROM);
251*7c478bd9Sstevel@tonic-gate clrhdr(H_RECEIVED);
252*7c478bd9Sstevel@tonic-gate affbytecnt = Daffbytecnt; Daffbytecnt = 0;
253*7c478bd9Sstevel@tonic-gate affcnt = Daffcnt; Daffcnt = 0;
254*7c478bd9Sstevel@tonic-gate rcvbytecnt = Drcvbytecnt; Drcvbytecnt = 0;
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate hdrlines[H_AFWDFROM].head = hdrlines[H_DAFWDFROM].head;
257*7c478bd9Sstevel@tonic-gate hdrlines[H_AFWDFROM].tail = hdrlines[H_DAFWDFROM].tail;
258*7c478bd9Sstevel@tonic-gate hdrlines[H_DAFWDFROM].head = (struct hdrs *)NULL;
259*7c478bd9Sstevel@tonic-gate hdrlines[H_DAFWDFROM].tail = (struct hdrs *)NULL;
260*7c478bd9Sstevel@tonic-gate hdrlines[H_RECEIVED].head = hdrlines[H_DRECEIVED].head;
261*7c478bd9Sstevel@tonic-gate hdrlines[H_RECEIVED].tail = hdrlines[H_DRECEIVED].tail;
262*7c478bd9Sstevel@tonic-gate hdrlines[H_DRECEIVED].head = (struct hdrs *)NULL;
263*7c478bd9Sstevel@tonic-gate hdrlines[H_DRECEIVED].tail = (struct hdrs *)NULL;
264*7c478bd9Sstevel@tonic-gate hdrlines[H_TCOPY].head = hdrlines[H_DTCOPY].head;
265*7c478bd9Sstevel@tonic-gate hdrlines[H_TCOPY].tail = hdrlines[H_DTCOPY].tail;
266*7c478bd9Sstevel@tonic-gate hdrlines[H_DTCOPY].head = (struct hdrs *)NULL;
267*7c478bd9Sstevel@tonic-gate hdrlines[H_DTCOPY].tail = (struct hdrs *)NULL;
268*7c478bd9Sstevel@tonic-gate
269*7c478bd9Sstevel@tonic-gate return;
270*7c478bd9Sstevel@tonic-gate }
271