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