1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23/*	  All Rights Reserved  	*/
24
25
26/*
27 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30
31#pragma ident	"%Z%%M%	%I%	%E% SMI"
32
33 /*
34  * All global externs defined in mail.h. All variables are initialized
35  * here!
36  *
37  * !!!!!IF YOU CHANGE (OR ADD) IT HERE, DO IT THERE ALSO !!!!!!!!
38  *
39  */
40#include	"mail.h"
41
42int		ac;		/* argument list count */
43char		**av;		/* argument list */
44int		affbytecnt;     /* Total bytes of Auto-Fwd. info in msg. */
45int		affcnt;		/* Number of Auto-Fwd.-From: lines in msg. */
46int		Daffbytecnt;    /* Hold affbytecnt when sending Delivery Notification */
47int		Daffcnt;	/* Hold affcnt when sending Delivery Notification */
48char		binmsg[] = "*** Message content is not printable: delete, write or save it to a file ***";
49int		changed;	/* > 0 says mailfile has changed */
50char		datestring[60];	/* Today's date and time */
51char		dbgfname[20];
52FILE		*dbgfp;
53char		dead[] = "/dead.letter";	/* name of dead.letter */
54int		debug;	/* Controls debugging level. 0 ==> no debugging */
55int		delflg = 1;
56int		dflag = 0;	/* 1 says returning unsendable mail */
57char		*errlist[]= {
58		"",
59		"Unknown system",
60		"Problem with mailfile",
61		"Space problem",
62		"Unable to forward mail, check permissions and group",
63		"Syntax error",
64		"Forwarding loop",
65		"Invalid sender",
66		"Invalid recipient",
67		"Too many From lines",
68		"Invalid permissions",
69		"Cannot open mbox",
70		"Temporary file problem",
71		"Cannot create dead.letter",
72		"Unbounded forwarding",
73		"Cannot create lock file",
74		"No group id of 'mail'",
75		"Problem allocating memory",
76		"Could not fork",
77		"Cannot pipe",
78		"Must be owner to modify mailfile",
79		"Permission denied by /etc/mail/mailsurr file",
80		"Surrogate command failed"
81};
82int		error = 0;	/* Local value for error */
83char		*failsafe;	/* $FAILSAFE */
84int		file_size;
85int		flge = 0;	/* 1 ==> 'e' option specified */
86int		flgE = 0;	/* 1 ==> 'E' option specified */
87int		flgF = 0;	/* 1 ==> Installing/Removing  Forwarding */
88int		flgf = 0;	/* 1 ==> 'f' option specified */
89int		flgh = 0;	/* 1 ==> 'h' option specified */
90int		flgm;
91int		flgp = 0;	/* 1 ==> 'p' option specified */
92int		flgP = 0;	/* 1 ==> 'P' option specified */
93int		flgr = 0;	/* 1 ==> 'r' option -- print in fifo order */
94int		flgt = 0;	/* 1 ==> 't' option -- add To: line to letter */
95int		flgT = 0;	/* 1 ==> 'T' option specified */
96int		flgw = 0;	/* 1 ==> 'w' option specified */
97int		fnuhdrtype = 0;	/* type of first non-UNIX header line */
98char		forwmsg[] = " forwarded by %s\n";
99char		fromS[1024];	/* stored here by sendmail for sendsurg */
100char		fromU[1024];	/* stored here by sendmail for sendsurg */
101char		frwlmsg[] = "     %s: Forwarding loop detected in %s's mailfile.\n";
102char		frwrd[] = "Forward to ";	/* forwarding sentinel */
103char		fwdFrom[1024];
104int		goerr = 0;	/* counts parsing errors */
105struct group	*grpptr;	/* pointer to struct group */
106struct hdrlines	hdrlines[H_CONT];
107/* Default_display indicates whether to display this header line to the TTY */
108/* when in default mode. Can be overridden via 'P' command at ? prompt */
109struct hdr	header[] = {
110		"",				FALSE,
111		"Auto-Forward-Count:",		FALSE,
112		"Auto-Forwarded-From:",		FALSE,
113		"Content-Length:",		TRUE,
114		"Content-Type:",		FALSE,
115		"Date:",			TRUE,
116		"Default-Options:",		FALSE,
117		"End-of-Header:",		FALSE,
118		"From ",			TRUE,
119		">From ",			TRUE,
120		"From:",			TRUE,
121		"MIME-Version:",		FALSE,
122		"MTS-Message-ID:",		FALSE,
123		"Message-Type:",		FALSE,
124		"Message-Version:",		FALSE,
125		"Message-Service:",		TRUE,
126		"Received:",			FALSE,
127		"Report-Version:",		FALSE,
128		"Subject:",			TRUE,
129		"To:",				TRUE,
130		">To:",				FALSE,
131		"Transport-Options:",		FALSE,
132		"UA-Content-ID:",		FALSE,
133
134		/*Dummy place holders for H_DAFWDFROM,*/
135		/*H_DTCOPY and H_RECEIVED. Should */
136		/* match above first...*/
137		"Hold-Auto-Forwarded-From:",	FALSE,
138		"Hold->To:",			FALSE,
139		"Hold-Received:",		FALSE,
140		"Continue:",			FALSE,
141		"Name-Value:",			FALSE,
142};
143char		*help[] = {
144		"?\t\tprint this help message\n",
145		"#\t\tdisplay message number #\n",
146		"-\t\tprint previous\n",
147		"+\t\tnext (no delete)\n",
148		"! cmd\t\texecute cmd\n",
149		"<CR>\t\tnext (no delete)\n",
150		"a\t\tposition at and read newly arrived mail\n",
151		"d [#]\t\tdelete message # (default current message)\n",
152		"dp\t\tdelete current message and print the next\n",
153		"dq\t\tdelete current message and exit\n",
154		"h a\t\tdisplay all headers\n",
155		"h d\t\tdisplay headers of letters scheduled for deletion\n",
156		"h [#]\t\tdisplay headers around # (default current message)\n",
157		"m user  \tmail (and delete) current message to user\n",
158		"n\t\tnext (no delete)\n",
159		"p\t\tprint (override any warnings of binary content)\n",
160		"P\t\toverride default 'brief' mode and display ALL header lines\n",
161		"q, ^D\t\tquit\n",
162		"r [args]\treply to (and delete) current letter via mail [args]\n",
163		"s [files]\tsave (and delete) current message (default mbox)\n",
164		"u [#]\t\tundelete message # (default current message)\n",
165		"w [files]\tsave (and delete) current message without header\n",
166		"x\t\texit without changing mail\n",
167		"y [files]\tsave (and delete) current message (default mbox)\n",
168		0
169};
170char		*hmbox;		/* pointer to $HOME/mbox */
171char		*hmdead;	/* pointer to $HOME/dead.letter */
172char		*home;		/* pointer to $HOME */
173time_t		iop;
174int		interactive = 0;	/* 1 says user is interactive */
175int		ismail = TRUE;		/* default to program=mail */
176int             deliverflag = FALSE;    /* -d flag, skip sendmail
177					 * deliver directly to mailbox
178					 */
179int             fromflag = FALSE;   /* -f from_user, set a user
180					 * when going into a mailbox
181					 */
182int		keepdbgfile;
183struct let	let[MAXLET];
184char		*lettmp;		/* pointer to tmp filename */
185char		lfil[MAXFILENAME];
186char		line[LSIZE];	/* holds a line of a letter in many places */
187char		*mailfile;	/* pointer to mailfile */
188char		mailcnfg[] = MAILCNFG;	/* configuration file */
189char		maildir[] = MAILDIR;	/* directory for mail files */
190gid_t		mailgrp;	/* numeric id of group 'mail' */
191char		mailsave[] = SAVEDIR;	/* dir for save files */
192char		*mailsurr = MAILSURR;	/* surrogate file name */
193FILE		*malf;		/* File pointer for mailfile */
194int		maxerr = 0;	/* largest value of error */
195char		mbox[] = "/mbox";	/* name for mbox */
196uid_t		mf_uid;		/* uid of users mailfile */
197gid_t		mf_gid;		/* gid of users mailfile */
198char		*msgtype;
199char		my_name[1024];	/* user's name who invoked this command */
200char		from_user[1024];	/* user's name specified w/ -f when sending */
201uid_t		my_euid;	/* user's euid */
202gid_t		my_egid;	/* user's egid */
203uid_t		my_uid;		/* user's uid */
204gid_t		my_gid;		/* user's gid */
205int		nlet	= 0;	/* current number of letters in mailfile */
206int		onlet	= 0;	/* number of letters in mailfile at startup*/
207int		optcnt = 0;	/* Number of options specified */
208int		orig_aff = 0;	/* orig. msg. contained H_AFWDFROM lines */
209int		orig_dbglvl;	/* argument to -x invocation option */
210int		orig_rcv = 0;	/* orig. msg. contained H_RECEIVED lines */
211int		orig_tcopy = 0;	/* orig. msg. contained H_TCOPY lines */
212struct passwd	*pwd;		/* holds passwd entry for this user */
213int		pflg = 0;	/* binary message display override flag */
214int		Pflg = 0;	/* Selective display flag; 1 ==> display all */
215char		*program;	/* program name */
216int		rcvbytecnt;     /* Total bytes of Received: info in msg. */
217int		Drcvbytecnt;    /* Hold rcvbytecnt when sending Delivery Notification */
218char		*recipname;		/* full recipient name/address */
219int		replying = 0;	/* 1 says we are replying to a letter */
220char		RFC822datestring[60];/* Date in RFC822 date format */
221char		Rpath[1024];	/* return path to sender of message */
222char		rmtmsg[] =	" remote from %s\n";
223char		rtrnmsg[] =	"***** UNDELIVERABLE MAIL sent to %s, being returned by %s *****\n";
224int		sav_errno;
225char		savefile[MAXFILENAME];	/* holds filename of save file */
226void		(*saveint)();
227/* Any header line prefixes listed here WILL be displayed in default mode */
228/* If it's not here, it won't be shown. Can be overridden via 'P' command */
229/* at ? prompt */
230char		*seldisp[] = {
231		"Cc:",
232		"Bcc:",
233		"Paper-",
234		"Phone:",
235		"Message-",
236		"Original-",
237		"Confirming-",
238		"Delivered-",
239		"Deliverable-",
240		"Not-",
241		"En-Route-To:",
242		0
243};
244int		sending;	/* TRUE==>sending mail; FALSE==>printing mail */
245char		m_sendto[1024];
246jmp_buf		sjbuf;
247int		surg_rc = 0;	/* exit code of surrogate command */
248int		surr_len = 0;
249char		*SURRcmdstr = (char *)NULL; /* save in case of FAILURE */
250FILE		*SURRerrfile;	/* stderr from surrogate in case of FAILURE */
251char		*thissys;	/* Holds name of the system we are on */
252FILE		*tmpf;		/* file pointer for temporary files */
253mode_t		umsave;
254struct		utsname utsn;
255static struct utimbuf	utims;
256struct utimbuf	*utimep = &utims;
257char		uval[1024];
258
259int init()
260{
261	utims.actime = utims.modtime = -1;
262	return (xsetenv(mailcnfg));
263}
264