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