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/*
277c478bdstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
287c478bdstevel@tonic-gate * Use is subject to license terms.
297c478bdstevel@tonic-gate */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate /*
347c478bdstevel@tonic-gate  * All global externs defined in mail.h. All variables are initialized
357c478bdstevel@tonic-gate  * here!
367c478bdstevel@tonic-gate  *
377c478bdstevel@tonic-gate  * !!!!!IF YOU CHANGE (OR ADD) IT HERE, DO IT THERE ALSO !!!!!!!!
387c478bdstevel@tonic-gate  *
397c478bdstevel@tonic-gate  */
407c478bdstevel@tonic-gate#include	"mail.h"
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gateint		ac;		/* argument list count */
437c478bdstevel@tonic-gatechar		**av;		/* argument list */
447c478bdstevel@tonic-gateint		affbytecnt;     /* Total bytes of Auto-Fwd. info in msg. */
457c478bdstevel@tonic-gateint		affcnt;		/* Number of Auto-Fwd.-From: lines in msg. */
467c478bdstevel@tonic-gateint		Daffbytecnt;    /* Hold affbytecnt when sending Delivery Notification */
477c478bdstevel@tonic-gateint		Daffcnt;	/* Hold affcnt when sending Delivery Notification */
487c478bdstevel@tonic-gatechar		binmsg[] = "*** Message content is not printable: delete, write or save it to a file ***";
497c478bdstevel@tonic-gateint		changed;	/* > 0 says mailfile has changed */
507c478bdstevel@tonic-gatechar		datestring[60];	/* Today's date and time */
517c478bdstevel@tonic-gatechar		dbgfname[20];
527c478bdstevel@tonic-gateFILE		*dbgfp;
537c478bdstevel@tonic-gatechar		dead[] = "/dead.letter";	/* name of dead.letter */
547c478bdstevel@tonic-gateint		debug;	/* Controls debugging level. 0 ==> no debugging */
557c478bdstevel@tonic-gateint		delflg = 1;
567c478bdstevel@tonic-gateint		dflag = 0;	/* 1 says returning unsendable mail */
577c478bdstevel@tonic-gatechar		*errlist[]= {
587c478bdstevel@tonic-gate		"",
597c478bdstevel@tonic-gate		"Unknown system",
607c478bdstevel@tonic-gate		"Problem with mailfile",
617c478bdstevel@tonic-gate		"Space problem",
627c478bdstevel@tonic-gate		"Unable to forward mail, check permissions and group",
637c478bdstevel@tonic-gate		"Syntax error",
647c478bdstevel@tonic-gate		"Forwarding loop",
657c478bdstevel@tonic-gate		"Invalid sender",
667c478bdstevel@tonic-gate		"Invalid recipient",
677c478bdstevel@tonic-gate		"Too many From lines",
687c478bdstevel@tonic-gate		"Invalid permissions",
697c478bdstevel@tonic-gate		"Cannot open mbox",
707c478bdstevel@tonic-gate		"Temporary file problem",
717c478bdstevel@tonic-gate		"Cannot create dead.letter",
727c478bdstevel@tonic-gate		"Unbounded forwarding",
737c478bdstevel@tonic-gate		"Cannot create lock file",
747c478bdstevel@tonic-gate		"No group id of 'mail'",
757c478bdstevel@tonic-gate		"Problem allocating memory",
767c478bdstevel@tonic-gate		"Could not fork",
777c478bdstevel@tonic-gate		"Cannot pipe",
787c478bdstevel@tonic-gate		"Must be owner to modify mailfile",
797c478bdstevel@tonic-gate		"Permission denied by /etc/mail/mailsurr file",
807c478bdstevel@tonic-gate		"Surrogate command failed"
817c478bdstevel@tonic-gate};
827c478bdstevel@tonic-gateint		error = 0;	/* Local value for error */
837c478bdstevel@tonic-gatechar		*failsafe;	/* $FAILSAFE */
847c478bdstevel@tonic-gateint		file_size;
857c478bdstevel@tonic-gateint		flge = 0;	/* 1 ==> 'e' option specified */
867c478bdstevel@tonic-gateint		flgE = 0;	/* 1 ==> 'E' option specified */
877c478bdstevel@tonic-gateint		flgF = 0;	/* 1 ==> Installing/Removing  Forwarding */
887c478bdstevel@tonic-gateint		flgf = 0;	/* 1 ==> 'f' option specified */
897c478bdstevel@tonic-gateint		flgh = 0;	/* 1 ==> 'h' option specified */
907c478bdstevel@tonic-gateint		flgm;
917c478bdstevel@tonic-gateint		flgp = 0;	/* 1 ==> 'p' option specified */
927c478bdstevel@tonic-gateint		flgP = 0;	/* 1 ==> 'P' option specified */
937c478bdstevel@tonic-gateint		flgr = 0;	/* 1 ==> 'r' option -- print in fifo order */
947c478bdstevel@tonic-gateint		flgt = 0;	/* 1 ==> 't' option -- add To: line to letter */
957c478bdstevel@tonic-gateint		flgT = 0;	/* 1 ==> 'T' option specified */
967c478bdstevel@tonic-gateint		flgw = 0;	/* 1 ==> 'w' option specified */
977c478bdstevel@tonic-gateint		fnuhdrtype = 0;	/* type of first non-UNIX header line */
987c478bdstevel@tonic-gatechar		forwmsg[] = " forwarded by %s\n";
997c478bdstevel@tonic-gatechar		fromS[1024];	/* stored here by sendmail for sendsurg */
1007c478bdstevel@tonic-gatechar		fromU[1024];	/* stored here by sendmail for sendsurg */
1017c478bdstevel@tonic-gatechar		frwlmsg[] = "     %s: Forwarding loop detected in %s's mailfile.\n";
1027c478bdstevel@tonic-gatechar		frwrd[] = "Forward to ";	/* forwarding sentinel */
1037c478bdstevel@tonic-gatechar		fwdFrom[1024];
1047c478bdstevel@tonic-gateint		goerr = 0;	/* counts parsing errors */
1057c478bdstevel@tonic-gatestruct group	*grpptr;	/* pointer to struct group */
1067c478bdstevel@tonic-gatestruct hdrlines	hdrlines[H_CONT];
1077c478bdstevel@tonic-gate/* Default_display indicates whether to display this header line to the TTY */
1087c478bdstevel@tonic-gate/* when in default mode. Can be overridden via 'P' command at ? prompt */
1097c478bdstevel@tonic-gatestruct hdr	header[] = {
1107c478bdstevel@tonic-gate		"",				FALSE,
1117c478bdstevel@tonic-gate		"Auto-Forward-Count:",		FALSE,
1127c478bdstevel@tonic-gate		"Auto-Forwarded-From:",		FALSE,
1137c478bdstevel@tonic-gate		"Content-Length:",		TRUE,
1147c478bdstevel@tonic-gate		"Content-Type:",		FALSE,
1157c478bdstevel@tonic-gate		"Date:",			TRUE,
1167c478bdstevel@tonic-gate		"Default-Options:",		FALSE,
1177c478bdstevel@tonic-gate		"End-of-Header:",		FALSE,
1187c478bdstevel@tonic-gate		"From ",			TRUE,
1197c478bdstevel@tonic-gate		">From ",			TRUE,
1207c478bdstevel@tonic-gate		"From:",			TRUE,
1217c478bdstevel@tonic-gate		"MIME-Version:",		FALSE,
1227c478bdstevel@tonic-gate		"MTS-Message-ID:",		FALSE,
1237c478bdstevel@tonic-gate		"Message-Type:",		FALSE,
1247c478bdstevel@tonic-gate		"Message-Version:",		FALSE,
1257c478bdstevel@tonic-gate		"Message-Service:",		TRUE,
1267c478bdstevel@tonic-gate		"Received:",			FALSE,
1277c478bdstevel@tonic-gate		"Report-Version:",		FALSE,
1287c478bdstevel@tonic-gate		"Subject:",			TRUE,
1297c478bdstevel@tonic-gate		"To:",				TRUE,
1307c478bdstevel@tonic-gate		">To:",				FALSE,
1317c478bdstevel@tonic-gate		"Transport-Options:",		FALSE,
1327c478bdstevel@tonic-gate		"UA-Content-ID:",		FALSE,
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate		/*Dummy place holders for H_DAFWDFROM,*/
1357c478bdstevel@tonic-gate		/*H_DTCOPY and H_RECEIVED. Should */
1367c478bdstevel@tonic-gate		/* match above first...*/
1377c478bdstevel@tonic-gate		"Hold-Auto-Forwarded-From:",	FALSE,
1387c478bdstevel@tonic-gate		"Hold->To:",			FALSE,
1397c478bdstevel@tonic-gate		"Hold-Received:",		FALSE,
1407c478bdstevel@tonic-gate		"Continue:",			FALSE,
1417c478bdstevel@tonic-gate		"Name-Value:",			FALSE,
1427c478bdstevel@tonic-gate};
1437c478bdstevel@tonic-gatechar		*help[] = {
1447c478bdstevel@tonic-gate		"?\t\tprint this help message\n",
1457c478bdstevel@tonic-gate		"#\t\tdisplay message number #\n",
1467c478bdstevel@tonic-gate		"-\t\tprint previous\n",
1477c478bdstevel@tonic-gate		"+\t\tnext (no delete)\n",
1487c478bdstevel@tonic-gate		"! cmd\t\texecute cmd\n",
1497c478bdstevel@tonic-gate		"<CR>\t\tnext (no delete)\n",
1507c478bdstevel@tonic-gate		"a\t\tposition at and read newly arrived mail\n",
1517c478bdstevel@tonic-gate		"d [#]\t\tdelete message # (default current message)\n",
1527c478bdstevel@tonic-gate		"dp\t\tdelete current message and print the next\n",
1537c478bdstevel@tonic-gate		"dq\t\tdelete current message and exit\n",
1547c478bdstevel@tonic-gate		"h a\t\tdisplay all headers\n",
1557c478bdstevel@tonic-gate		"h d\t\tdisplay headers of letters scheduled for deletion\n",
1567c478bdstevel@tonic-gate		"h [#]\t\tdisplay headers around # (default current message)\n",
1577c478bdstevel@tonic-gate		"m user  \tmail (and delete) current message to user\n",
1587c478bdstevel@tonic-gate		"n\t\tnext (no delete)\n",
1597c478bdstevel@tonic-gate		"p\t\tprint (override any warnings of binary content)\n",
1607c478bdstevel@tonic-gate		"P\t\toverride default 'brief' mode and display ALL header lines\n",
1617c478bdstevel@tonic-gate		"q, ^D\t\tquit\n",
1627c478bdstevel@tonic-gate		"r [args]\treply to (and delete) current letter via mail [args]\n",
1637c478bdstevel@tonic-gate		"s [files]\tsave (and delete) current message (default mbox)\n",
1647c478bdstevel@tonic-gate		"u [#]\t\tundelete message # (default current message)\n",
1657c478bdstevel@tonic-gate		"w [files]\tsave (and delete) current message without header\n",
1667c478bdstevel@tonic-gate		"x\t\texit without changing mail\n",
1677c478bdstevel@tonic-gate		"y [files]\tsave (and delete) current message (default mbox)\n",
1687c478bdstevel@tonic-gate		0
1697c478bdstevel@tonic-gate};
1707c478bdstevel@tonic-gatechar		*hmbox;		/* pointer to $HOME/mbox */
1717c478bdstevel@tonic-gatechar		*hmdead;	/* pointer to $HOME/dead.letter */
1727c478bdstevel@tonic-gatechar		*home;		/* pointer to $HOME */
1737c478bdstevel@tonic-gatetime_t		iop;
1747c478bdstevel@tonic-gateint		interactive = 0;	/* 1 says user is interactive */
1757c478bdstevel@tonic-gateint		ismail = TRUE;		/* default to program=mail */
1767c478bdstevel@tonic-gateint             deliverflag = FALSE;    /* -d flag, skip sendmail
1777c478bdstevel@tonic-gate					 * deliver directly to mailbox
1787c478bdstevel@tonic-gate					 */
1797c478bdstevel@tonic-gateint             fromflag = FALSE;   /* -f from_user, set a user
1807c478bdstevel@tonic-gate					 * when going into a mailbox
1817c478bdstevel@tonic-gate					 */
1827c478bdstevel@tonic-gateint		keepdbgfile;
1837c478bdstevel@tonic-gatestruct let	let[MAXLET];
1847c478bdstevel@tonic-gatechar		*lettmp;		/* pointer to tmp filename */
1857c478bdstevel@tonic-gatechar		lfil[MAXFILENAME];
1867c478bdstevel@tonic-gatechar		line[LSIZE];	/* holds a line of a letter in many places */
1877c478bdstevel@tonic-gatechar		*mailfile;	/* pointer to mailfile */
1887c478bdstevel@tonic-gatechar		mailcnfg[] = MAILCNFG;	/* configuration file */
1897c478bdstevel@tonic-gatechar		maildir[] = MAILDIR;	/* directory for mail files */
1907c478bdstevel@tonic-gategid_t		mailgrp;	/* numeric id of group 'mail' */
1917c478bdstevel@tonic-gatechar		mailsave[] = SAVEDIR;	/* dir for save files */
1927c478bdstevel@tonic-gatechar		*mailsurr = MAILSURR;	/* surrogate file name */
1937c478bdstevel@tonic-gateFILE		*malf;		/* File pointer for mailfile */
1947c478bdstevel@tonic-gateint		maxerr = 0;	/* largest value of error */
1957c478bdstevel@tonic-gatechar		mbox[] = "/mbox";	/* name for mbox */
1967c478bdstevel@tonic-gateuid_t		mf_uid;		/* uid of users mailfile */
1977c478bdstevel@tonic-gategid_t		mf_gid;		/* gid of users mailfile */
1987c478bdstevel@tonic-gatechar		*msgtype;
1997c478bdstevel@tonic-gatechar		my_name[1024];	/* user's name who invoked this command */
2007c478bdstevel@tonic-gatechar		from_user[1024];	/* user's name specified w/ -f when sending */
2017c478bdstevel@tonic-gateuid_t		my_euid;	/* user's euid */
2027c478bdstevel@tonic-gategid_t		my_egid;	/* user's egid */
2037c478bdstevel@tonic-gateuid_t		my_uid;		/* user's uid */
2047c478bdstevel@tonic-gategid_t		my_gid;		/* user's gid */
2057c478bdstevel@tonic-gateint		nlet	= 0;	/* current number of letters in mailfile */
2067c478bdstevel@tonic-gateint		onlet	= 0;	/* number of letters in mailfile at startup*/
2077c478bdstevel@tonic-gateint		optcnt = 0;	/* Number of options specified */
2087c478bdstevel@tonic-gateint		orig_aff = 0;	/* orig. msg. contained H_AFWDFROM lines */
2097c478bdstevel@tonic-gateint		orig_dbglvl;	/* argument to -x invocation option */
2107c478bdstevel@tonic-gateint		orig_rcv = 0;	/* orig. msg. contained H_RECEIVED lines */
2117c478bdstevel@tonic-gateint		orig_tcopy = 0;	/* orig. msg. contained H_TCOPY lines */
2127c478bdstevel@tonic-gatestruct passwd	*pwd;		/* holds passwd entry for this user */
2137c478bdstevel@tonic-gateint		pflg = 0;	/* binary message display override flag */
2147c478bdstevel@tonic-gateint		Pflg = 0;	/* Selective display flag; 1 ==> display all */
2157c478bdstevel@tonic-gatechar		*program;	/* program name */
2167c478bdstevel@tonic-gateint		rcvbytecnt;     /* Total bytes of Received: info in msg. */
2177c478bdstevel@tonic-gateint		Drcvbytecnt;    /* Hold rcvbytecnt when sending Delivery Notification */
2187c478bdstevel@tonic-gatechar		*recipname;		/* full recipient name/address */
2197c478bdstevel@tonic-gateint		replying = 0;	/* 1 says we are replying to a letter */
2207c478bdstevel@tonic-gatechar		RFC822datestring[60];/* Date in RFC822 date format */
2217c478bdstevel@tonic-gatechar		Rpath[1024];	/* return path to sender of message */
2227c478bdstevel@tonic-gatechar		rmtmsg[] =	" remote from %s\n";
2237c478bdstevel@tonic-gatechar		rtrnmsg[] =	"***** UNDELIVERABLE MAIL sent to %s, being returned by %s *****\n";
2247c478bdstevel@tonic-gateint		sav_errno;
2257c478bdstevel@tonic-gatechar		savefile[MAXFILENAME];	/* holds filename of save file */
2267c478bdstevel@tonic-gatevoid		(*saveint)();
2277c478bdstevel@tonic-gate/* Any header line prefixes listed here WILL be displayed in default mode */
2287c478bdstevel@tonic-gate/* If it's not here, it won't be shown. Can be overridden via 'P' command */
2297c478bdstevel@tonic-gate/* at ? prompt */
2307c478bdstevel@tonic-gatechar		*seldisp[] = {
2317c478bdstevel@tonic-gate		"Cc:",
2327c478bdstevel@tonic-gate		"Bcc:",
2337c478bdstevel@tonic-gate		"Paper-",
2347c478bdstevel@tonic-gate		"Phone:",
2357c478bdstevel@tonic-gate		"Message-",
2367c478bdstevel@tonic-gate		"Original-",
2377c478bdstevel@tonic-gate		"Confirming-",
2387c478bdstevel@tonic-gate		"Delivered-",
2397c478bdstevel@tonic-gate		"Deliverable-",
2407c478bdstevel@tonic-gate		"Not-",
2417c478bdstevel@tonic-gate		"En-Route-To:",
2427c478bdstevel@tonic-gate		0
2437c478bdstevel@tonic-gate};
2447c478bdstevel@tonic-gateint		sending;	/* TRUE==>sending mail; FALSE==>printing mail */
2457c478bdstevel@tonic-gatechar		m_sendto[1024];
2467c478bdstevel@tonic-gatejmp_buf		sjbuf;
2477c478bdstevel@tonic-gateint		surg_rc = 0;	/* exit code of surrogate command */
2487c478bdstevel@tonic-gateint		surr_len = 0;
2497c478bdstevel@tonic-gatechar		*SURRcmdstr = (char *)NULL; /* save in case of FAILURE */
2507c478bdstevel@tonic-gateFILE		*SURRerrfile;	/* stderr from surrogate in case of FAILURE */
2517c478bdstevel@tonic-gatechar		*thissys;	/* Holds name of the system we are on */
2527c478bdstevel@tonic-gateFILE		*tmpf;		/* file pointer for temporary files */
2537c478bdstevel@tonic-gatemode_t		umsave;
2547c478bdstevel@tonic-gatestruct		utsname utsn;
2557c478bdstevel@tonic-gatestatic struct utimbuf	utims;
2567c478bdstevel@tonic-gatestruct utimbuf	*utimep = &utims;
2577c478bdstevel@tonic-gatechar		uval[1024];
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gateint init()
2607c478bdstevel@tonic-gate{
2617c478bdstevel@tonic-gate	utims.actime = utims.modtime = -1;
2627c478bdstevel@tonic-gate	return (xsetenv(mailcnfg));
2637c478bdstevel@tonic-gate}
264