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 /* 23*b7d62af5Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27*b7d62af5Sceastha /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*b7d62af5Sceastha /* All Rights Reserved */ 29*b7d62af5Sceastha 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include "mail.h" 337c478bd9Sstevel@tonic-gate #ifdef SVR4 347c478bd9Sstevel@tonic-gate #include <locale.h> 357c478bd9Sstevel@tonic-gate #endif 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * mail [ -ehpPqrtw ] [-x debuglevel] [ -f file ] [ -F user(s) ] 387c478bd9Sstevel@tonic-gate * mail -T file persons 397c478bd9Sstevel@tonic-gate * mail [ -tw ] [ -m messagetype ] persons 407c478bd9Sstevel@tonic-gate * rmail [ -tw ] persons 417c478bd9Sstevel@tonic-gate */ 42*b7d62af5Sceastha int 43*b7d62af5Sceastha main(int argc, char **argv) 447c478bd9Sstevel@tonic-gate { 457c478bd9Sstevel@tonic-gate register int i; 467c478bd9Sstevel@tonic-gate char *cptr, *p; 477c478bd9Sstevel@tonic-gate struct stat statb; 487c478bd9Sstevel@tonic-gate static char pn[] = "main"; 497c478bd9Sstevel@tonic-gate extern char **environ; 507c478bd9Sstevel@tonic-gate int env_var_idx, next_slot_idx; 517c478bd9Sstevel@tonic-gate int tmpfd = -1; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifdef SVR4 547c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 557c478bd9Sstevel@tonic-gate #endif 567c478bd9Sstevel@tonic-gate /* fix here for bug #1086130 - security hole */ 577c478bd9Sstevel@tonic-gate /* skip over the LD_* env variable */ 587c478bd9Sstevel@tonic-gate env_var_idx = 0; next_slot_idx = 0; 597c478bd9Sstevel@tonic-gate while (environ[env_var_idx] != NULL) { 607c478bd9Sstevel@tonic-gate environ[next_slot_idx] = environ[env_var_idx]; 617c478bd9Sstevel@tonic-gate if (strncmp(environ[env_var_idx], "LD_", 3)) { 627c478bd9Sstevel@tonic-gate next_slot_idx++; 637c478bd9Sstevel@tonic-gate } 647c478bd9Sstevel@tonic-gate env_var_idx++; 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate environ[next_slot_idx] = NULL; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #ifdef SIGCONT 697c478bd9Sstevel@tonic-gate #ifdef SVR4 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate struct sigaction nsig; 727c478bd9Sstevel@tonic-gate nsig.sa_handler = SIG_DFL; 737c478bd9Sstevel@tonic-gate sigemptyset(&nsig.sa_mask); 747c478bd9Sstevel@tonic-gate nsig.sa_flags = SA_RESTART; 757c478bd9Sstevel@tonic-gate (void) sigaction(SIGCONT, &nsig, (struct sigaction *)0); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate #else 787c478bd9Sstevel@tonic-gate sigset(SIGCONT, SIG_DFL); 797c478bd9Sstevel@tonic-gate #endif 807c478bd9Sstevel@tonic-gate #endif 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* 837c478bd9Sstevel@tonic-gate * Strip off path name of this command for use in messages 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate if ((program = strrchr(argv[0], '/')) != NULL) { 867c478bd9Sstevel@tonic-gate program++; 877c478bd9Sstevel@tonic-gate } else { 887c478bd9Sstevel@tonic-gate program = argv[0]; 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* Close all file descriptors except stdin, stdout & stderr */ 927c478bd9Sstevel@tonic-gate closefrom(STDERR_FILENO + 1); 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * Get group id for mail, exit if none exists 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate if ((grpptr = getgrnam("mail")) == NULL) { 987c478bd9Sstevel@tonic-gate errmsg(E_GROUP, ""); 997c478bd9Sstevel@tonic-gate exit(1); 1007c478bd9Sstevel@tonic-gate } else { 1017c478bd9Sstevel@tonic-gate mailgrp = grpptr->gr_gid; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * Save the *id for later use. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate my_uid = getuid(); 1087c478bd9Sstevel@tonic-gate my_gid = getgid(); 1097c478bd9Sstevel@tonic-gate my_euid = geteuid(); 1107c478bd9Sstevel@tonic-gate my_egid = getegid(); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate * What command (rmail or mail)? 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate if (strcmp(program, "rmail") == SAME) { 1167c478bd9Sstevel@tonic-gate ismail = FALSE; 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Parse the command line and adjust argc and argv 1217c478bd9Sstevel@tonic-gate * to compensate for any options 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate i = parse(argc, argv); 1247c478bd9Sstevel@tonic-gate argv += (i - 1); 1257c478bd9Sstevel@tonic-gate argc -= (i - 1); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* block a potential security hole */ 1287c478bd9Sstevel@tonic-gate if (flgT && (my_euid != 0)) { 1297c478bd9Sstevel@tonic-gate setgid(my_gid); 1307c478bd9Sstevel@tonic-gate Tout(pn, "Setgid unset\n"); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (debug == 0) { 1347c478bd9Sstevel@tonic-gate /* If not set as an invocation option, check for system-wide */ 1357c478bd9Sstevel@tonic-gate /* global flag */ 1367c478bd9Sstevel@tonic-gate char *xp = xgetenv("DEBUG"); 1377c478bd9Sstevel@tonic-gate if (xp != (char *)NULL) { 1387c478bd9Sstevel@tonic-gate debug = atoi(xp); 1397c478bd9Sstevel@tonic-gate if (debug < 0) { 1407c478bd9Sstevel@tonic-gate /* Keep trace file even if successful */ 1417c478bd9Sstevel@tonic-gate keepdbgfile = -1; 1427c478bd9Sstevel@tonic-gate debug = -debug; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate if (debug > 0) { 1477c478bd9Sstevel@tonic-gate strcpy(dbgfname, "/tmp/MLDBGXXXXXX"); 1487c478bd9Sstevel@tonic-gate if ((tmpfd = mkstemp(dbgfname)) == -1) { 1497c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: can't open debugging file '%s'\n", 1507c478bd9Sstevel@tonic-gate program, dbgfname); 1517c478bd9Sstevel@tonic-gate exit(13); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate if ((dbgfp = fdopen(tmpfd, "w")) == (FILE *)NULL) { 1547c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: can't open debugging file '%s'\n", 1557c478bd9Sstevel@tonic-gate program, dbgfname); 1567c478bd9Sstevel@tonic-gate (void) close(tmpfd); 1577c478bd9Sstevel@tonic-gate exit(13); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate setbuf(dbgfp, NULL); 1607c478bd9Sstevel@tonic-gate fprintf(dbgfp, "main(): debugging level == %d\n", debug); 1617c478bd9Sstevel@tonic-gate fprintf(dbgfp, "main(): trace file ='%s': kept %s\n", dbgfname, 1627c478bd9Sstevel@tonic-gate ((keepdbgfile < 0) ? 1637c478bd9Sstevel@tonic-gate "on success or failure." : "only on failure.")); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate if (!ismail && (goerr > 0 || !i)) { 1677c478bd9Sstevel@tonic-gate Dout(pn, 11, "!ismail, goerr=%d, i=%d\n", goerr, i); 1687c478bd9Sstevel@tonic-gate if (goerr > 0) { 1697c478bd9Sstevel@tonic-gate errmsg(E_SYNTAX, "Usage: rmail [-wt] person(s)"); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate if (!i) { 1727c478bd9Sstevel@tonic-gate errmsg(E_SYNTAX, "At least one user must be specified"); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate Dout(pn, 11, "exiting!\n"); 1757c478bd9Sstevel@tonic-gate done(0); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate umsave = umask(7); 1797c478bd9Sstevel@tonic-gate uname(&utsn); 1807c478bd9Sstevel@tonic-gate if ((p = xgetenv("CLUSTER")) != (char *)NULL) { 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * We are not who we appear... 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate thissys = p; 1857c478bd9Sstevel@tonic-gate } else { 1867c478bd9Sstevel@tonic-gate thissys = utsn.nodename; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate Dout(pn, 11, "thissys = '%s', uname = '%s'\n", thissys, utsn.nodename); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate failsafe = xgetenv("FAILSAFE"); 1917c478bd9Sstevel@tonic-gate if (failsafe) 1927c478bd9Sstevel@tonic-gate Dout(pn, 11, "failsafe processing enabled to %s\n", failsafe); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * Use environment variables 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate home = getenv("HOME"); 1987c478bd9Sstevel@tonic-gate if (!home || !*home) { 1997c478bd9Sstevel@tonic-gate home = "."; 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate my_name[0] = '\0'; 2037c478bd9Sstevel@tonic-gate pwd = getpwuid(my_uid); 2047c478bd9Sstevel@tonic-gate if (pwd) 2057c478bd9Sstevel@tonic-gate (void) strlcpy(my_name, pwd->pw_name, sizeof (my_name)); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* If root, use LOGNAME if set */ 2087c478bd9Sstevel@tonic-gate if (my_uid == 0) { 2097c478bd9Sstevel@tonic-gate /* If root, use LOGNAME if set */ 2107c478bd9Sstevel@tonic-gate if (((cptr = getenv("LOGNAME")) != NULL) && 2117c478bd9Sstevel@tonic-gate (strlen(cptr) != 0)) { 2127c478bd9Sstevel@tonic-gate (void) strlcpy(my_name, cptr, sizeof (my_name)); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate Dout(pn, 11, "my_name = '%s'\n", my_name); 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* 2187c478bd9Sstevel@tonic-gate * Catch signals for cleanup 2197c478bd9Sstevel@tonic-gate */ 2207c478bd9Sstevel@tonic-gate if (setjmp(sjbuf)) { 2217c478bd9Sstevel@tonic-gate done(0); 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate for (i = SIGINT; i < SIGCLD; i++) { 2247c478bd9Sstevel@tonic-gate setsig(i, delete); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate setsig(SIGHUP, sig_done); 2277c478bd9Sstevel@tonic-gate setsig(SIGTERM, sig_done); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate cksaved(my_name); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Rmail is always invoked to send mail 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate Dout(pn, 11, "ismail=%d, argc=%d\n", ismail, argc); 2357c478bd9Sstevel@tonic-gate if (ismail && (argc == 1)) { 2367c478bd9Sstevel@tonic-gate sending = FALSE; 2377c478bd9Sstevel@tonic-gate printmail(); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate } else { 2407c478bd9Sstevel@tonic-gate sending = TRUE; 2417c478bd9Sstevel@tonic-gate sendmail(argc, argv); 2427c478bd9Sstevel@tonic-gate } 243*b7d62af5Sceastha done(0); 2447c478bd9Sstevel@tonic-gate } 245