xref: /onnv-gate/usr/src/cmd/mail/main.c (revision 373:5de22f2b7283)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*373Sceastha /*
23*373Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*373Sceastha  * Use is subject to license terms.
25*373Sceastha  */
26*373Sceastha 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "mail.h"
330Sstevel@tonic-gate #ifdef SVR4
340Sstevel@tonic-gate #include <locale.h>
350Sstevel@tonic-gate #endif
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  *	mail [ -ehpPqrtw ] [-x debuglevel] [ -f file ] [ -F user(s) ]
380Sstevel@tonic-gate  *	mail -T file persons
390Sstevel@tonic-gate  *	mail [ -tw ] [ -m messagetype ] persons
400Sstevel@tonic-gate  *	rmail [ -tw ] persons
410Sstevel@tonic-gate  */
42*373Sceastha int
main(int argc,char ** argv)43*373Sceastha main(int argc, char **argv)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	register int i;
460Sstevel@tonic-gate 	char *cptr, *p;
470Sstevel@tonic-gate 	struct stat statb;
480Sstevel@tonic-gate 	static char pn[] = "main";
490Sstevel@tonic-gate 	extern char **environ;
500Sstevel@tonic-gate 	int env_var_idx, next_slot_idx;
510Sstevel@tonic-gate 	int tmpfd = -1;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #ifdef SVR4
540Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
550Sstevel@tonic-gate #endif
560Sstevel@tonic-gate 	/* fix here for bug #1086130 - security hole	*/
570Sstevel@tonic-gate 	/* skip over the LD_* env variable		*/
580Sstevel@tonic-gate 	env_var_idx = 0; next_slot_idx = 0;
590Sstevel@tonic-gate 	while (environ[env_var_idx] != NULL) {
600Sstevel@tonic-gate 			environ[next_slot_idx] = environ[env_var_idx];
610Sstevel@tonic-gate 		if (strncmp(environ[env_var_idx], "LD_", 3)) {
620Sstevel@tonic-gate 			next_slot_idx++;
630Sstevel@tonic-gate 		}
640Sstevel@tonic-gate 		env_var_idx++;
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 	environ[next_slot_idx] = NULL;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifdef SIGCONT
690Sstevel@tonic-gate #ifdef SVR4
700Sstevel@tonic-gate 	{
710Sstevel@tonic-gate 	struct sigaction nsig;
720Sstevel@tonic-gate 	nsig.sa_handler = SIG_DFL;
730Sstevel@tonic-gate 	sigemptyset(&nsig.sa_mask);
740Sstevel@tonic-gate 	nsig.sa_flags = SA_RESTART;
750Sstevel@tonic-gate 	(void) sigaction(SIGCONT, &nsig, (struct sigaction *)0);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate #else
780Sstevel@tonic-gate 	sigset(SIGCONT, SIG_DFL);
790Sstevel@tonic-gate #endif
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 *	Strip off path name of this command for use in messages
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 	if ((program = strrchr(argv[0], '/')) != NULL) {
860Sstevel@tonic-gate 		program++;
870Sstevel@tonic-gate 	} else {
880Sstevel@tonic-gate 		program = argv[0];
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/* Close all file descriptors except stdin, stdout & stderr */
920Sstevel@tonic-gate 	closefrom(STDERR_FILENO + 1);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 *	Get group id for mail, exit if none exists
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	if ((grpptr = getgrnam("mail")) == NULL) {
980Sstevel@tonic-gate 		errmsg(E_GROUP, "");
990Sstevel@tonic-gate 		exit(1);
1000Sstevel@tonic-gate 	} else {
1010Sstevel@tonic-gate 		mailgrp = grpptr->gr_gid;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/*
1050Sstevel@tonic-gate 	 *	Save the *id for later use.
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	my_uid = getuid();
1080Sstevel@tonic-gate 	my_gid = getgid();
1090Sstevel@tonic-gate 	my_euid = geteuid();
1100Sstevel@tonic-gate 	my_egid = getegid();
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 *	What command (rmail or mail)?
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	if (strcmp(program, "rmail") == SAME) {
1160Sstevel@tonic-gate 		ismail = FALSE;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 *	Parse the command line and adjust argc and argv
1210Sstevel@tonic-gate 	 *	to compensate for any options
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	i = parse(argc, argv);
1240Sstevel@tonic-gate 	argv += (i - 1);
1250Sstevel@tonic-gate 	argc -= (i - 1);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/* block a potential security hole */
1280Sstevel@tonic-gate 	if (flgT && (my_euid != 0)) {
1290Sstevel@tonic-gate 		setgid(my_gid);
1300Sstevel@tonic-gate 		Tout(pn, "Setgid unset\n");
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (debug == 0) {
1340Sstevel@tonic-gate 		/* If not set as an invocation option, check for system-wide */
1350Sstevel@tonic-gate 		/* global flag */
1360Sstevel@tonic-gate 		char *xp = xgetenv("DEBUG");
1370Sstevel@tonic-gate 		if (xp != (char *)NULL) {
1380Sstevel@tonic-gate 			debug = atoi(xp);
1390Sstevel@tonic-gate 			if (debug < 0) {
1400Sstevel@tonic-gate 				/* Keep trace file even if successful */
1410Sstevel@tonic-gate 				keepdbgfile = -1;
1420Sstevel@tonic-gate 				debug = -debug;
1430Sstevel@tonic-gate 			}
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	if (debug > 0) {
1470Sstevel@tonic-gate 		strcpy(dbgfname, "/tmp/MLDBGXXXXXX");
1480Sstevel@tonic-gate 		if ((tmpfd = mkstemp(dbgfname)) == -1) {
1490Sstevel@tonic-gate 			fprintf(stderr, "%s: can't open debugging file '%s'\n",
1500Sstevel@tonic-gate 				program, dbgfname);
1510Sstevel@tonic-gate 			exit(13);
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 		if ((dbgfp = fdopen(tmpfd, "w")) == (FILE *)NULL) {
1540Sstevel@tonic-gate 			fprintf(stderr, "%s: can't open debugging file '%s'\n",
1550Sstevel@tonic-gate 				program, dbgfname);
1560Sstevel@tonic-gate 			(void) close(tmpfd);
1570Sstevel@tonic-gate 			exit(13);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		setbuf(dbgfp, NULL);
1600Sstevel@tonic-gate 		fprintf(dbgfp, "main(): debugging level == %d\n", debug);
1610Sstevel@tonic-gate 		fprintf(dbgfp, "main(): trace file ='%s': kept %s\n", dbgfname,
1620Sstevel@tonic-gate 			((keepdbgfile < 0) ?
1630Sstevel@tonic-gate 				"on success or failure." : "only on failure."));
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (!ismail && (goerr > 0 || !i)) {
1670Sstevel@tonic-gate 		Dout(pn, 11, "!ismail, goerr=%d, i=%d\n", goerr, i);
1680Sstevel@tonic-gate 		if (goerr > 0) {
1690Sstevel@tonic-gate 			errmsg(E_SYNTAX, "Usage: rmail [-wt] person(s)");
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 		if (!i) {
1720Sstevel@tonic-gate 			errmsg(E_SYNTAX, "At least one user must be specified");
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 		Dout(pn, 11, "exiting!\n");
1750Sstevel@tonic-gate 		done(0);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	umsave = umask(7);
1790Sstevel@tonic-gate 	uname(&utsn);
1800Sstevel@tonic-gate 	if ((p = xgetenv("CLUSTER")) != (char *)NULL) {
1810Sstevel@tonic-gate 		/*
1820Sstevel@tonic-gate 		 * We are not who we appear...
1830Sstevel@tonic-gate 		 */
1840Sstevel@tonic-gate 		thissys = p;
1850Sstevel@tonic-gate 	} else {
1860Sstevel@tonic-gate 		thissys = utsn.nodename;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	Dout(pn, 11, "thissys = '%s', uname = '%s'\n", thissys, utsn.nodename);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	failsafe = xgetenv("FAILSAFE");
1910Sstevel@tonic-gate 	if (failsafe)
1920Sstevel@tonic-gate 		Dout(pn, 11, "failsafe processing enabled to %s\n", failsafe);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 *	Use environment variables
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	home = getenv("HOME");
1980Sstevel@tonic-gate 	if (!home || !*home) {
1990Sstevel@tonic-gate 		home = ".";
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	my_name[0] = '\0';
2030Sstevel@tonic-gate 	pwd = getpwuid(my_uid);
2040Sstevel@tonic-gate 	if (pwd)
2050Sstevel@tonic-gate 		(void) strlcpy(my_name, pwd->pw_name, sizeof (my_name));
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/* If root, use LOGNAME if set */
2080Sstevel@tonic-gate 	if (my_uid == 0) {
2090Sstevel@tonic-gate 		/* If root, use LOGNAME if set */
2100Sstevel@tonic-gate 		if (((cptr = getenv("LOGNAME")) != NULL) &&
2110Sstevel@tonic-gate 		    (strlen(cptr) != 0)) {
2120Sstevel@tonic-gate 			(void) strlcpy(my_name, cptr, sizeof (my_name));
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	Dout(pn, 11, "my_name = '%s'\n", my_name);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 *	Catch signals for cleanup
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (setjmp(sjbuf)) {
2210Sstevel@tonic-gate 		done(0);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	for (i = SIGINT; i < SIGCLD; i++) {
2240Sstevel@tonic-gate 		setsig(i, delete);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	setsig(SIGHUP, sig_done);
2270Sstevel@tonic-gate 	setsig(SIGTERM, sig_done);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	cksaved(my_name);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/*
2320Sstevel@tonic-gate 	 *	Rmail is always invoked to send mail
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 	Dout(pn, 11, "ismail=%d, argc=%d\n", ismail, argc);
2350Sstevel@tonic-gate 	if (ismail && (argc == 1)) {
2360Sstevel@tonic-gate 		sending = FALSE;
2370Sstevel@tonic-gate 		printmail();
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	} else {
2400Sstevel@tonic-gate 		sending = TRUE;
2410Sstevel@tonic-gate 		sendmail(argc, argv);
2420Sstevel@tonic-gate 	}
243*373Sceastha 	done(0);
2440Sstevel@tonic-gate }
245