17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
247c478bdstevel@tonic-gate
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
307c478bdstevel@tonic-gate * Use is subject to license terms.
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include "mail.h"
347c478bdstevel@tonic-gate/*
357c478bdstevel@tonic-gate	This routine returns undeliverable mail as well as handles
367c478bdstevel@tonic-gate	replying to letters
377c478bdstevel@tonic-gate*/
387c478bdstevel@tonic-gatevoid
397c478bdstevel@tonic-gategoback(letnum)
407c478bdstevel@tonic-gateint	letnum;
417c478bdstevel@tonic-gate{
427c478bdstevel@tonic-gate	static char pn[] = "goback";
437c478bdstevel@tonic-gate	int	i, w;
447c478bdstevel@tonic-gate	char	buf[1024], *cp, work[1024], wuser[1024];
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate	/*
477c478bdstevel@tonic-gate	 * If dflag already == 1, then been here already and
487c478bdstevel@tonic-gate	 * having a problem delivering failure notification.
497c478bdstevel@tonic-gate	 * Reset dflag to 9 to avoid endless loop.....
507c478bdstevel@tonic-gate	 */
517c478bdstevel@tonic-gate	if (dflag == 1) {
527c478bdstevel@tonic-gate		dflag = 9;
537c478bdstevel@tonic-gate		Dout(pn, 0, "dflag = %d\n", dflag);
547c478bdstevel@tonic-gate		if (!error) {
557c478bdstevel@tonic-gate			error = E_REMOTE;
567c478bdstevel@tonic-gate			Dout(pn, 0, "error = %d\n", error);
577c478bdstevel@tonic-gate		}
587c478bdstevel@tonic-gate	}
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	if (dflag < 2) {
617c478bdstevel@tonic-gate		work[0] = '\0';
627c478bdstevel@tonic-gate		wuser[0] = '\0';
637c478bdstevel@tonic-gate		fclose(tmpf);
647c478bdstevel@tonic-gate		if (!replying) {
657c478bdstevel@tonic-gate			dflag = 1;
667c478bdstevel@tonic-gate			Dout(pn, 0, "dflag = 1\n");
677c478bdstevel@tonic-gate			if ((debug > 0) && (keepdbgfile == 0)) {
687c478bdstevel@tonic-gate				keepdbgfile++;
697c478bdstevel@tonic-gate			}
707c478bdstevel@tonic-gate			if (ckdlivopts(H_TCOPY, (int *)0) & IGNORE) {
717c478bdstevel@tonic-gate				goto skipsend;
727c478bdstevel@tonic-gate			}
737c478bdstevel@tonic-gate		}
747c478bdstevel@tonic-gate		tmpf = doopen(lettmp, "r+", E_TMP);
757c478bdstevel@tonic-gate		if (replying) {
767c478bdstevel@tonic-gate			fseek(tmpf, let[letnum].adr, 0);
777c478bdstevel@tonic-gate		}
787c478bdstevel@tonic-gate		for (fgets(line, LSIZE, tmpf);
797c478bdstevel@tonic-gate		strncmp(line, header[H_FROM].tag, strlen(header[H_FROM].tag))
807c478bdstevel@tonic-gate		    == SAME ||
817c478bdstevel@tonic-gate		    strncmp(line, header[H_FROM1].tag,
827c478bdstevel@tonic-gate			strlen(header[H_FROM1].tag)) == SAME; ) {
837c478bdstevel@tonic-gate			if ((i = substr(line, "remote from")) != -1) {
847c478bdstevel@tonic-gate				for (i = 0, cp = strrchr(line, ' ') + 1;
857c478bdstevel@tonic-gate					*cp != '\n';
867c478bdstevel@tonic-gate					cp++) {
877c478bdstevel@tonic-gate					buf[i++] = *cp;
887c478bdstevel@tonic-gate				}
897c478bdstevel@tonic-gate				buf[i++] = '!';
907c478bdstevel@tonic-gate				buf[i] = '\0';
917c478bdstevel@tonic-gate				strcat(work, buf);
927c478bdstevel@tonic-gate				if (line[0] == '>') {
937c478bdstevel@tonic-gate					i = 6;
947c478bdstevel@tonic-gate				} else {
957c478bdstevel@tonic-gate					i = 5;
967c478bdstevel@tonic-gate				}
977c478bdstevel@tonic-gate				for (w = i; line[w] != ' '; w++) {
987c478bdstevel@tonic-gate					wuser[w-i] = line[w];
997c478bdstevel@tonic-gate				}
1007c478bdstevel@tonic-gate				wuser[w-i] = '\0';
1017c478bdstevel@tonic-gate			} else if ((i = substr(line, "forwarded by")) == -1) {
1027c478bdstevel@tonic-gate				if (line[0] == '>') {
1037c478bdstevel@tonic-gate					break;
1047c478bdstevel@tonic-gate				} else {
1057c478bdstevel@tonic-gate					i = 5;
1067c478bdstevel@tonic-gate				}
1077c478bdstevel@tonic-gate				for (w = i; line[w] != ' '; w++) {
1087c478bdstevel@tonic-gate					wuser[w-i] = line[w];
1097c478bdstevel@tonic-gate				}
1107c478bdstevel@tonic-gate				wuser[w-i] = '\0';
1117c478bdstevel@tonic-gate			} else if ((i = substr(line, "forwarded by")) > -1) {
1127c478bdstevel@tonic-gate				break;
1137c478bdstevel@tonic-gate			}
1147c478bdstevel@tonic-gate			fgets(line, LSIZE, tmpf);
1157c478bdstevel@tonic-gate		}
1167c478bdstevel@tonic-gate		strcat(work, wuser);
1177c478bdstevel@tonic-gate		fclose(tmpf);
1187c478bdstevel@tonic-gate		tmpf = doopen(lettmp, "r+", E_TMP);
1197c478bdstevel@tonic-gate		if (work[0] != '\0') {
1207c478bdstevel@tonic-gate			reciplist list;
1217c478bdstevel@tonic-gate			if (replying) {
1227c478bdstevel@tonic-gate				(void) snprintf(buf, sizeof (buf),
1237c478bdstevel@tonic-gate				    "mail %s %s", m_sendto, work);
1247c478bdstevel@tonic-gate				printf("%s\n", buf);
1257c478bdstevel@tonic-gate				systm(buf);
1267c478bdstevel@tonic-gate				return;
1277c478bdstevel@tonic-gate			}
1287c478bdstevel@tonic-gate			if (interactive) {
1297c478bdstevel@tonic-gate				(void) strlcpy(work, my_name, sizeof (work));
1307c478bdstevel@tonic-gate			}
1317c478bdstevel@tonic-gate			fprintf(stderr, "%s: Return to %s\n", program, work);
1327c478bdstevel@tonic-gate			/* Put header info from message aside so it won't */
1337c478bdstevel@tonic-gate			/* get confused with the Delivery Notification info */
1347c478bdstevel@tonic-gate			Daffbytecnt = affbytecnt; affbytecnt = 0;
1357c478bdstevel@tonic-gate			Daffcnt = affcnt; affcnt = 0;
1367c478bdstevel@tonic-gate			Drcvbytecnt = rcvbytecnt; rcvbytecnt = 0;
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate			hdrlines[H_DAFWDFROM].head = hdrlines[H_AFWDFROM].head;
1397c478bdstevel@tonic-gate			hdrlines[H_DAFWDFROM].tail = hdrlines[H_AFWDFROM].tail;
1407c478bdstevel@tonic-gate			hdrlines[H_AFWDFROM].head = (struct hdrs *)NULL;
1417c478bdstevel@tonic-gate			hdrlines[H_AFWDFROM].tail = (struct hdrs *)NULL;
1427c478bdstevel@tonic-gate			hdrlines[H_DRECEIVED].head = hdrlines[H_RECEIVED].head;
1437c478bdstevel@tonic-gate			hdrlines[H_DRECEIVED].tail = hdrlines[H_RECEIVED].tail;
1447c478bdstevel@tonic-gate			hdrlines[H_RECEIVED].head = (struct hdrs *)NULL;
1457c478bdstevel@tonic-gate			hdrlines[H_RECEIVED].tail = (struct hdrs *)NULL;
1467c478bdstevel@tonic-gate			hdrlines[H_DTCOPY].head = hdrlines[H_TCOPY].head;
1477c478bdstevel@tonic-gate			hdrlines[H_DTCOPY].tail = hdrlines[H_TCOPY].tail;
1487c478bdstevel@tonic-gate			hdrlines[H_TCOPY].head = (struct hdrs *)NULL;
1497c478bdstevel@tonic-gate			hdrlines[H_TCOPY].tail = (struct hdrs *)NULL;
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gate			pushlist(H_TCOPY, HEAD, work, FALSE);
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate			new_reciplist(&list);
1547c478bdstevel@tonic-gate			add_recip(&list, work, FALSE);
1557c478bdstevel@tonic-gate			sendlist(&list, 0, 0);
1567c478bdstevel@tonic-gate			del_reciplist(&list);
1577c478bdstevel@tonic-gate		}
1587c478bdstevel@tonic-gate	}
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate	skipsend:
1617c478bdstevel@tonic-gate	if (dflag == 9) {
1627c478bdstevel@tonic-gate		fprintf(stderr,
1637c478bdstevel@tonic-gate			"%s: Cannot return mail.\n",
1647c478bdstevel@tonic-gate			program);
1657c478bdstevel@tonic-gate		mkdead();
1667c478bdstevel@tonic-gate	}
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate	else if (dflag < 2) {
1697c478bdstevel@tonic-gate		if (!maxerr && (dflag != 1)) {
1707c478bdstevel@tonic-gate			maxerr = error;
1717c478bdstevel@tonic-gate			Dout(pn, 0, "maxerr = %d\n", maxerr);
1727c478bdstevel@tonic-gate		}
1737c478bdstevel@tonic-gate		dflag = 0;
1747c478bdstevel@tonic-gate		error = 0;
1757c478bdstevel@tonic-gate		Dout(pn, 0, "before return,  dflag = %d, error = %d\n",
1767c478bdstevel@tonic-gate			dflag, error);
1777c478bdstevel@tonic-gate	}
1787c478bdstevel@tonic-gate}
179