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