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