xref: /titanic_50/usr/src/cmd/mail/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate /*
26*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
27*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "mail.h"
33*7c478bd9Sstevel@tonic-gate #ifdef SVR4
34*7c478bd9Sstevel@tonic-gate #include <locale.h>
35*7c478bd9Sstevel@tonic-gate #endif
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  *	mail [ -ehpPqrtw ] [-x debuglevel] [ -f file ] [ -F user(s) ]
38*7c478bd9Sstevel@tonic-gate  *	mail -T file persons
39*7c478bd9Sstevel@tonic-gate  *	mail [ -tw ] [ -m messagetype ] persons
40*7c478bd9Sstevel@tonic-gate  *	rmail [ -tw ] persons
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate main(argc, argv)
43*7c478bd9Sstevel@tonic-gate char	**argv;
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	register int i;
46*7c478bd9Sstevel@tonic-gate 	char *cptr, *p;
47*7c478bd9Sstevel@tonic-gate 	struct stat statb;
48*7c478bd9Sstevel@tonic-gate 	static char pn[] = "main";
49*7c478bd9Sstevel@tonic-gate 	extern char **environ;
50*7c478bd9Sstevel@tonic-gate 	int env_var_idx, next_slot_idx;
51*7c478bd9Sstevel@tonic-gate 	int tmpfd = -1;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #ifdef SVR4
54*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate 	/* fix here for bug #1086130 - security hole	*/
57*7c478bd9Sstevel@tonic-gate 	/* skip over the LD_* env variable		*/
58*7c478bd9Sstevel@tonic-gate 	env_var_idx = 0; next_slot_idx = 0;
59*7c478bd9Sstevel@tonic-gate 	while (environ[env_var_idx] != NULL) {
60*7c478bd9Sstevel@tonic-gate 			environ[next_slot_idx] = environ[env_var_idx];
61*7c478bd9Sstevel@tonic-gate 		if (strncmp(environ[env_var_idx], "LD_", 3)) {
62*7c478bd9Sstevel@tonic-gate 			next_slot_idx++;
63*7c478bd9Sstevel@tonic-gate 		}
64*7c478bd9Sstevel@tonic-gate 		env_var_idx++;
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 	environ[next_slot_idx] = NULL;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate #ifdef SIGCONT
69*7c478bd9Sstevel@tonic-gate #ifdef SVR4
70*7c478bd9Sstevel@tonic-gate 	{
71*7c478bd9Sstevel@tonic-gate 	struct sigaction nsig;
72*7c478bd9Sstevel@tonic-gate 	nsig.sa_handler = SIG_DFL;
73*7c478bd9Sstevel@tonic-gate 	sigemptyset(&nsig.sa_mask);
74*7c478bd9Sstevel@tonic-gate 	nsig.sa_flags = SA_RESTART;
75*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGCONT, &nsig, (struct sigaction *)0);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate #else
78*7c478bd9Sstevel@tonic-gate 	sigset(SIGCONT, SIG_DFL);
79*7c478bd9Sstevel@tonic-gate #endif
80*7c478bd9Sstevel@tonic-gate #endif
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	/*
83*7c478bd9Sstevel@tonic-gate 	 *	Strip off path name of this command for use in messages
84*7c478bd9Sstevel@tonic-gate 	 */
85*7c478bd9Sstevel@tonic-gate 	if ((program = strrchr(argv[0], '/')) != NULL) {
86*7c478bd9Sstevel@tonic-gate 		program++;
87*7c478bd9Sstevel@tonic-gate 	} else {
88*7c478bd9Sstevel@tonic-gate 		program = argv[0];
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	/* Close all file descriptors except stdin, stdout & stderr */
92*7c478bd9Sstevel@tonic-gate 	closefrom(STDERR_FILENO + 1);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	/*
95*7c478bd9Sstevel@tonic-gate 	 *	Get group id for mail, exit if none exists
96*7c478bd9Sstevel@tonic-gate 	 */
97*7c478bd9Sstevel@tonic-gate 	if ((grpptr = getgrnam("mail")) == NULL) {
98*7c478bd9Sstevel@tonic-gate 		errmsg(E_GROUP, "");
99*7c478bd9Sstevel@tonic-gate 		exit(1);
100*7c478bd9Sstevel@tonic-gate 	} else {
101*7c478bd9Sstevel@tonic-gate 		mailgrp = grpptr->gr_gid;
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	/*
105*7c478bd9Sstevel@tonic-gate 	 *	Save the *id for later use.
106*7c478bd9Sstevel@tonic-gate 	 */
107*7c478bd9Sstevel@tonic-gate 	my_uid = getuid();
108*7c478bd9Sstevel@tonic-gate 	my_gid = getgid();
109*7c478bd9Sstevel@tonic-gate 	my_euid = geteuid();
110*7c478bd9Sstevel@tonic-gate 	my_egid = getegid();
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	/*
113*7c478bd9Sstevel@tonic-gate 	 *	What command (rmail or mail)?
114*7c478bd9Sstevel@tonic-gate 	 */
115*7c478bd9Sstevel@tonic-gate 	if (strcmp(program, "rmail") == SAME) {
116*7c478bd9Sstevel@tonic-gate 		ismail = FALSE;
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	/*
120*7c478bd9Sstevel@tonic-gate 	 *	Parse the command line and adjust argc and argv
121*7c478bd9Sstevel@tonic-gate 	 *	to compensate for any options
122*7c478bd9Sstevel@tonic-gate 	 */
123*7c478bd9Sstevel@tonic-gate 	i = parse(argc, argv);
124*7c478bd9Sstevel@tonic-gate 	argv += (i - 1);
125*7c478bd9Sstevel@tonic-gate 	argc -= (i - 1);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	/* block a potential security hole */
128*7c478bd9Sstevel@tonic-gate 	if (flgT && (my_euid != 0)) {
129*7c478bd9Sstevel@tonic-gate 		setgid(my_gid);
130*7c478bd9Sstevel@tonic-gate 		Tout(pn, "Setgid unset\n");
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (debug == 0) {
134*7c478bd9Sstevel@tonic-gate 		/* If not set as an invocation option, check for system-wide */
135*7c478bd9Sstevel@tonic-gate 		/* global flag */
136*7c478bd9Sstevel@tonic-gate 		char *xp = xgetenv("DEBUG");
137*7c478bd9Sstevel@tonic-gate 		if (xp != (char *)NULL) {
138*7c478bd9Sstevel@tonic-gate 			debug = atoi(xp);
139*7c478bd9Sstevel@tonic-gate 			if (debug < 0) {
140*7c478bd9Sstevel@tonic-gate 				/* Keep trace file even if successful */
141*7c478bd9Sstevel@tonic-gate 				keepdbgfile = -1;
142*7c478bd9Sstevel@tonic-gate 				debug = -debug;
143*7c478bd9Sstevel@tonic-gate 			}
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 	if (debug > 0) {
147*7c478bd9Sstevel@tonic-gate 		strcpy(dbgfname, "/tmp/MLDBGXXXXXX");
148*7c478bd9Sstevel@tonic-gate 		if ((tmpfd = mkstemp(dbgfname)) == -1) {
149*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: can't open debugging file '%s'\n",
150*7c478bd9Sstevel@tonic-gate 				program, dbgfname);
151*7c478bd9Sstevel@tonic-gate 			exit(13);
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		if ((dbgfp = fdopen(tmpfd, "w")) == (FILE *)NULL) {
154*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: can't open debugging file '%s'\n",
155*7c478bd9Sstevel@tonic-gate 				program, dbgfname);
156*7c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
157*7c478bd9Sstevel@tonic-gate 			exit(13);
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 		setbuf(dbgfp, NULL);
160*7c478bd9Sstevel@tonic-gate 		fprintf(dbgfp, "main(): debugging level == %d\n", debug);
161*7c478bd9Sstevel@tonic-gate 		fprintf(dbgfp, "main(): trace file ='%s': kept %s\n", dbgfname,
162*7c478bd9Sstevel@tonic-gate 			((keepdbgfile < 0) ?
163*7c478bd9Sstevel@tonic-gate 				"on success or failure." : "only on failure."));
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if (!ismail && (goerr > 0 || !i)) {
167*7c478bd9Sstevel@tonic-gate 		Dout(pn, 11, "!ismail, goerr=%d, i=%d\n", goerr, i);
168*7c478bd9Sstevel@tonic-gate 		if (goerr > 0) {
169*7c478bd9Sstevel@tonic-gate 			errmsg(E_SYNTAX, "Usage: rmail [-wt] person(s)");
170*7c478bd9Sstevel@tonic-gate 		}
171*7c478bd9Sstevel@tonic-gate 		if (!i) {
172*7c478bd9Sstevel@tonic-gate 			errmsg(E_SYNTAX, "At least one user must be specified");
173*7c478bd9Sstevel@tonic-gate 		}
174*7c478bd9Sstevel@tonic-gate 		Dout(pn, 11, "exiting!\n");
175*7c478bd9Sstevel@tonic-gate 		done(0);
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	umsave = umask(7);
179*7c478bd9Sstevel@tonic-gate 	uname(&utsn);
180*7c478bd9Sstevel@tonic-gate 	if ((p = xgetenv("CLUSTER")) != (char *)NULL) {
181*7c478bd9Sstevel@tonic-gate 		/*
182*7c478bd9Sstevel@tonic-gate 		 * We are not who we appear...
183*7c478bd9Sstevel@tonic-gate 		 */
184*7c478bd9Sstevel@tonic-gate 		thissys = p;
185*7c478bd9Sstevel@tonic-gate 	} else {
186*7c478bd9Sstevel@tonic-gate 		thissys = utsn.nodename;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	Dout(pn, 11, "thissys = '%s', uname = '%s'\n", thissys, utsn.nodename);
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	failsafe = xgetenv("FAILSAFE");
191*7c478bd9Sstevel@tonic-gate 	if (failsafe)
192*7c478bd9Sstevel@tonic-gate 		Dout(pn, 11, "failsafe processing enabled to %s\n", failsafe);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	/*
195*7c478bd9Sstevel@tonic-gate 	 *	Use environment variables
196*7c478bd9Sstevel@tonic-gate 	 */
197*7c478bd9Sstevel@tonic-gate 	home = getenv("HOME");
198*7c478bd9Sstevel@tonic-gate 	if (!home || !*home) {
199*7c478bd9Sstevel@tonic-gate 		home = ".";
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	my_name[0] = '\0';
203*7c478bd9Sstevel@tonic-gate 	pwd = getpwuid(my_uid);
204*7c478bd9Sstevel@tonic-gate 	if (pwd)
205*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(my_name, pwd->pw_name, sizeof (my_name));
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	/* If root, use LOGNAME if set */
208*7c478bd9Sstevel@tonic-gate 	if (my_uid == 0) {
209*7c478bd9Sstevel@tonic-gate 		/* If root, use LOGNAME if set */
210*7c478bd9Sstevel@tonic-gate 		if (((cptr = getenv("LOGNAME")) != NULL) &&
211*7c478bd9Sstevel@tonic-gate 		    (strlen(cptr) != 0)) {
212*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(my_name, cptr, sizeof (my_name));
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 	}
215*7c478bd9Sstevel@tonic-gate 	Dout(pn, 11, "my_name = '%s'\n", my_name);
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	/*
218*7c478bd9Sstevel@tonic-gate 	 *	Catch signals for cleanup
219*7c478bd9Sstevel@tonic-gate 	 */
220*7c478bd9Sstevel@tonic-gate 	if (setjmp(sjbuf)) {
221*7c478bd9Sstevel@tonic-gate 		done(0);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	for (i = SIGINT; i < SIGCLD; i++) {
224*7c478bd9Sstevel@tonic-gate 		setsig(i, delete);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate 	setsig(SIGHUP, sig_done);
227*7c478bd9Sstevel@tonic-gate 	setsig(SIGTERM, sig_done);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	cksaved(my_name);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	/*
232*7c478bd9Sstevel@tonic-gate 	 *	Rmail is always invoked to send mail
233*7c478bd9Sstevel@tonic-gate 	 */
234*7c478bd9Sstevel@tonic-gate 	Dout(pn, 11, "ismail=%d, argc=%d\n", ismail, argc);
235*7c478bd9Sstevel@tonic-gate 	if (ismail && (argc == 1)) {
236*7c478bd9Sstevel@tonic-gate 		sending = FALSE;
237*7c478bd9Sstevel@tonic-gate 		printmail();
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	} else {
240*7c478bd9Sstevel@tonic-gate 		sending = TRUE;
241*7c478bd9Sstevel@tonic-gate 		sendmail(argc, argv);
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 	done(0); /*NOTREACHED*/
244*7c478bd9Sstevel@tonic-gate }
245