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 53010f05bSdp * Common Development and Distribution License (the "License"). 63010f05bSdp * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*b0d0a1c8SViswanathan Kannappan * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/stat.h> 317c478bd9Sstevel@tonic-gate #include <sys/types.h> 327c478bd9Sstevel@tonic-gate #include <sys/wait.h> 337c478bd9Sstevel@tonic-gate #include <errno.h> 347c478bd9Sstevel@tonic-gate #include <signal.h> 357c478bd9Sstevel@tonic-gate #include <stdio.h> 367c478bd9Sstevel@tonic-gate #include <stdlib.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate #include <fcntl.h> 397c478bd9Sstevel@tonic-gate #include <ctype.h> 407c478bd9Sstevel@tonic-gate #include <pwd.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <locale.h> 437c478bd9Sstevel@tonic-gate #include <nl_types.h> 447c478bd9Sstevel@tonic-gate #include <langinfo.h> 457c478bd9Sstevel@tonic-gate #include <libintl.h> 461c0be37bSsn199410 #include <spawn.h> 477c478bd9Sstevel@tonic-gate #include <security/pam_appl.h> 485b08e637SChris Gerhard #include <limits.h> 495b08e637SChris Gerhard #include <libzoneinfo.h> 507c478bd9Sstevel@tonic-gate #include "cron.h" 513d63ea05Sas145665 #include "getresponse.h" 527c478bd9Sstevel@tonic-gate 53d61d085dScf46844 #if defined(XPG4) 54d61d085dScf46844 #define VIPATH "/usr/xpg4/bin/vi" 55d61d085dScf46844 #elif defined(XPG6) 56d61d085dScf46844 #define VIPATH "/usr/xpg6/bin/vi" 57d61d085dScf46844 #else 58d61d085dScf46844 #define _XPG_NOTDEFINED 59d61d085dScf46844 #define VIPATH "vi" 60d61d085dScf46844 #endif 61d61d085dScf46844 627c478bd9Sstevel@tonic-gate #define TMPFILE "_cron" /* prefix for tmp file */ 637c478bd9Sstevel@tonic-gate #define CRMODE 0600 /* mode for creating crontabs */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #define BADCREATE \ 667c478bd9Sstevel@tonic-gate "can't create your crontab file in the crontab directory." 677c478bd9Sstevel@tonic-gate #define BADOPEN "can't open your crontab file." 687c478bd9Sstevel@tonic-gate #define BADSHELL \ 697c478bd9Sstevel@tonic-gate "because your login shell isn't /usr/bin/sh, you can't use cron." 707c478bd9Sstevel@tonic-gate #define WARNSHELL "warning: commands will be executed using /usr/bin/sh\n" 717c478bd9Sstevel@tonic-gate #define BADUSAGE \ 723010f05bSdp "usage:\n" \ 733010f05bSdp "\tcrontab [file]\n" \ 743010f05bSdp "\tcrontab -e [username]\n" \ 753010f05bSdp "\tcrontab -l [username]\n" \ 763010f05bSdp "\tcrontab -r [username]" 777c478bd9Sstevel@tonic-gate #define INVALIDUSER "you are not a valid user (no entry in /etc/passwd)." 787c478bd9Sstevel@tonic-gate #define NOTALLOWED "you are not authorized to use cron. Sorry." 797c478bd9Sstevel@tonic-gate #define NOTROOT \ 807c478bd9Sstevel@tonic-gate "you must be super-user to access another user's crontab file" 817c478bd9Sstevel@tonic-gate #define AUDITREJECT "The audit context for your shell has not been set." 827c478bd9Sstevel@tonic-gate #define EOLN "unexpected end of line." 837c478bd9Sstevel@tonic-gate #define UNEXPECT "unexpected character found in line." 847c478bd9Sstevel@tonic-gate #define OUTOFBOUND "number out of bounds." 857c478bd9Sstevel@tonic-gate #define ERRSFND "errors detected in input, no crontab file generated." 867c478bd9Sstevel@tonic-gate #define ED_ERROR \ 877c478bd9Sstevel@tonic-gate " The editor indicates that an error occurred while you were\n"\ 887c478bd9Sstevel@tonic-gate " editing the crontab data - usually a minor typing error.\n\n" 897c478bd9Sstevel@tonic-gate #define BADREAD "error reading your crontab file" 907c478bd9Sstevel@tonic-gate #define ED_PROMPT \ 913d63ea05Sas145665 " Edit again, to ensure crontab information is intact (%s/%s)?\n"\ 923d63ea05Sas145665 " ('%s' will discard edits.)" 937c478bd9Sstevel@tonic-gate #define NAMETOOLONG "login name too long" 945b08e637SChris Gerhard #define BAD_TZ "Timezone unrecognized in: %s" 955b08e637SChris Gerhard #define BAD_SHELL "Invalid shell specified: %s" 965b08e637SChris Gerhard #define BAD_HOME "Unable to access directory: %s\t%s\n" 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate extern int per_errno; 991c0be37bSsn199410 extern char **environ; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate extern int audit_crontab_modify(char *, char *, int); 1027c478bd9Sstevel@tonic-gate extern int audit_crontab_delete(char *, int); 1037c478bd9Sstevel@tonic-gate extern int audit_crontab_not_allowed(uid_t, char *); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate int err; 1067c478bd9Sstevel@tonic-gate int cursor; 1077c478bd9Sstevel@tonic-gate char *cf; 1087c478bd9Sstevel@tonic-gate char *tnam; 1097c478bd9Sstevel@tonic-gate char edtemp[5+13+1]; 1107c478bd9Sstevel@tonic-gate char line[CTLINESIZE]; 1117c478bd9Sstevel@tonic-gate static char login[UNAMESIZE]; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate static int next_field(int, int); 1147c478bd9Sstevel@tonic-gate static void catch(int); 1157c478bd9Sstevel@tonic-gate static void crabort(char *); 1167c478bd9Sstevel@tonic-gate static void cerror(char *); 1177c478bd9Sstevel@tonic-gate static void copycron(FILE *); 1187c478bd9Sstevel@tonic-gate 119032624d5Sbasabi int 120032624d5Sbasabi main(int argc, char **argv) 1217c478bd9Sstevel@tonic-gate { 1227c478bd9Sstevel@tonic-gate int c, r; 1237c478bd9Sstevel@tonic-gate int rflag = 0; 1247c478bd9Sstevel@tonic-gate int lflag = 0; 1257c478bd9Sstevel@tonic-gate int eflag = 0; 1267c478bd9Sstevel@tonic-gate int errflg = 0; 1277c478bd9Sstevel@tonic-gate char *pp; 1287c478bd9Sstevel@tonic-gate FILE *fp, *tmpfp; 1297c478bd9Sstevel@tonic-gate struct stat stbuf; 1307c478bd9Sstevel@tonic-gate struct passwd *pwp; 1317c478bd9Sstevel@tonic-gate time_t omodtime; 1327c478bd9Sstevel@tonic-gate char *editor; 1337c478bd9Sstevel@tonic-gate uid_t ruid; 1347c478bd9Sstevel@tonic-gate pid_t pid; 1357c478bd9Sstevel@tonic-gate int stat_loc; 1367c478bd9Sstevel@tonic-gate int ret; 1377c478bd9Sstevel@tonic-gate char real_login[UNAMESIZE]; 1387c478bd9Sstevel@tonic-gate int tmpfd = -1; 1397c478bd9Sstevel@tonic-gate pam_handle_t *pamh; 1407c478bd9Sstevel@tonic-gate int pam_error; 1411c0be37bSsn199410 pid_t cpid; 1421c0be37bSsn199410 int cstatus; 1431c0be37bSsn199410 char *argvec[3]; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1467c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1477c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1487c478bd9Sstevel@tonic-gate #endif 1497c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1503d63ea05Sas145665 1513d63ea05Sas145665 if (init_yes() < 0) { 1523d63ea05Sas145665 (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES), 1533d63ea05Sas145665 strerror(errno)); 1543d63ea05Sas145665 exit(1); 1553d63ea05Sas145665 } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "elr")) != EOF) 1587c478bd9Sstevel@tonic-gate switch (c) { 1597c478bd9Sstevel@tonic-gate case 'e': 1607c478bd9Sstevel@tonic-gate eflag++; 1617c478bd9Sstevel@tonic-gate break; 1627c478bd9Sstevel@tonic-gate case 'l': 1637c478bd9Sstevel@tonic-gate lflag++; 1647c478bd9Sstevel@tonic-gate break; 1657c478bd9Sstevel@tonic-gate case 'r': 1667c478bd9Sstevel@tonic-gate rflag++; 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate case '?': 1697c478bd9Sstevel@tonic-gate errflg++; 1707c478bd9Sstevel@tonic-gate break; 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate if (eflag + lflag + rflag > 1) 1747c478bd9Sstevel@tonic-gate errflg++; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate argc -= optind; 1777c478bd9Sstevel@tonic-gate argv += optind; 1787c478bd9Sstevel@tonic-gate if (errflg || argc > 1) 1797c478bd9Sstevel@tonic-gate crabort(BADUSAGE); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate ruid = getuid(); 1827c478bd9Sstevel@tonic-gate if ((pwp = getpwuid(ruid)) == NULL) 1837c478bd9Sstevel@tonic-gate crabort(INVALIDUSER); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate if (strlcpy(real_login, pwp->pw_name, sizeof (real_login)) 1867c478bd9Sstevel@tonic-gate >= sizeof (real_login)) 1877c478bd9Sstevel@tonic-gate crabort(NAMETOOLONG); 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if ((eflag || lflag || rflag) && argc == 1) { 1907c478bd9Sstevel@tonic-gate if ((pwp = getpwnam(*argv)) == NULL) 1917c478bd9Sstevel@tonic-gate crabort(INVALIDUSER); 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (!chkauthattr(CRONADMIN_AUTH, real_login)) { 1947c478bd9Sstevel@tonic-gate if (pwp->pw_uid != ruid) 1957c478bd9Sstevel@tonic-gate crabort(NOTROOT); 1967c478bd9Sstevel@tonic-gate else 1977c478bd9Sstevel@tonic-gate pp = getuser(ruid); 1987c478bd9Sstevel@tonic-gate } else 1997c478bd9Sstevel@tonic-gate pp = *argv++; 2007c478bd9Sstevel@tonic-gate } else { 2017c478bd9Sstevel@tonic-gate pp = getuser(ruid); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if (pp == NULL) { 2057c478bd9Sstevel@tonic-gate if (per_errno == 2) 2067c478bd9Sstevel@tonic-gate crabort(BADSHELL); 2077c478bd9Sstevel@tonic-gate else 2087c478bd9Sstevel@tonic-gate crabort(INVALIDUSER); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (strlcpy(login, pp, sizeof (login)) >= sizeof (login)) 2117c478bd9Sstevel@tonic-gate crabort(NAMETOOLONG); 2127c478bd9Sstevel@tonic-gate if (!allowed(login, CRONALLOW, CRONDENY)) 2137c478bd9Sstevel@tonic-gate crabort(NOTALLOWED); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* Do account validation check */ 2167c478bd9Sstevel@tonic-gate pam_error = pam_start("cron", pp, NULL, &pamh); 2177c478bd9Sstevel@tonic-gate if (pam_error != PAM_SUCCESS) { 2187c478bd9Sstevel@tonic-gate crabort((char *)pam_strerror(pamh, pam_error)); 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate pam_error = pam_acct_mgmt(pamh, PAM_SILENT); 2217c478bd9Sstevel@tonic-gate if (pam_error != PAM_SUCCESS) { 2227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning - Invalid account: " 2237c478bd9Sstevel@tonic-gate "'%s' not allowed to execute cronjobs\n"), pp); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate (void) pam_end(pamh, PAM_SUCCESS); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* check for unaudited shell */ 2297c478bd9Sstevel@tonic-gate if (audit_crontab_not_allowed(ruid, pp)) 2307c478bd9Sstevel@tonic-gate crabort(AUDITREJECT); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate cf = xmalloc(strlen(CRONDIR)+strlen(login)+2); 2337c478bd9Sstevel@tonic-gate strcat(strcat(strcpy(cf, CRONDIR), "/"), login); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (rflag) { 2367c478bd9Sstevel@tonic-gate r = unlink(cf); 2377c478bd9Sstevel@tonic-gate cron_sendmsg(DELETE, login, login, CRON); 2387c478bd9Sstevel@tonic-gate audit_crontab_delete(cf, r); 2397c478bd9Sstevel@tonic-gate exit(0); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate if (lflag) { 2427c478bd9Sstevel@tonic-gate if ((fp = fopen(cf, "r")) == NULL) 2437c478bd9Sstevel@tonic-gate crabort(BADOPEN); 2447c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, fp) != NULL) 2457c478bd9Sstevel@tonic-gate fputs(line, stdout); 2467c478bd9Sstevel@tonic-gate fclose(fp); 2477c478bd9Sstevel@tonic-gate exit(0); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate if (eflag) { 2507c478bd9Sstevel@tonic-gate if ((fp = fopen(cf, "r")) == NULL) { 2517c478bd9Sstevel@tonic-gate if (errno != ENOENT) 2527c478bd9Sstevel@tonic-gate crabort(BADOPEN); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate (void) strcpy(edtemp, "/tmp/crontabXXXXXX"); 2557c478bd9Sstevel@tonic-gate tmpfd = mkstemp(edtemp); 2567c478bd9Sstevel@tonic-gate if (fchown(tmpfd, ruid, -1) == -1) { 2577c478bd9Sstevel@tonic-gate (void) close(tmpfd); 2587c478bd9Sstevel@tonic-gate crabort("fchown of temporary file failed"); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate (void) close(tmpfd); 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * Fork off a child with user's permissions, 2637c478bd9Sstevel@tonic-gate * to edit the crontab file 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1) 2667c478bd9Sstevel@tonic-gate crabort("fork failed"); 2677c478bd9Sstevel@tonic-gate if (pid == 0) { /* child process */ 2687c478bd9Sstevel@tonic-gate /* give up super-user privileges. */ 2697c478bd9Sstevel@tonic-gate setuid(ruid); 2707c478bd9Sstevel@tonic-gate if ((tmpfp = fopen(edtemp, "w")) == NULL) 2717c478bd9Sstevel@tonic-gate crabort("can't create temporary file"); 2727c478bd9Sstevel@tonic-gate if (fp != NULL) { 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * Copy user's crontab file to temporary file. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, fp) != NULL) { 2777c478bd9Sstevel@tonic-gate fputs(line, tmpfp); 2787c478bd9Sstevel@tonic-gate if (ferror(tmpfp)) { 2797c478bd9Sstevel@tonic-gate fclose(fp); 2807c478bd9Sstevel@tonic-gate fclose(tmpfp); 2817c478bd9Sstevel@tonic-gate crabort("write error on" 2827c478bd9Sstevel@tonic-gate "temporary file"); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate if (ferror(fp)) { 2867c478bd9Sstevel@tonic-gate fclose(fp); 2877c478bd9Sstevel@tonic-gate fclose(tmpfp); 2887c478bd9Sstevel@tonic-gate crabort(BADREAD); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate fclose(fp); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate if (fclose(tmpfp) == EOF) 2937c478bd9Sstevel@tonic-gate crabort("write error on temporary file"); 2947c478bd9Sstevel@tonic-gate if (stat(edtemp, &stbuf) < 0) 2957c478bd9Sstevel@tonic-gate crabort("can't stat temporary file"); 2967c478bd9Sstevel@tonic-gate omodtime = stbuf.st_mtime; 297d61d085dScf46844 #ifdef _XPG_NOTDEFINED 2987c478bd9Sstevel@tonic-gate editor = getenv("VISUAL"); 299d61d085dScf46844 if (editor == NULL) { 300d61d085dScf46844 #endif 3017c478bd9Sstevel@tonic-gate editor = getenv("EDITOR"); 3027c478bd9Sstevel@tonic-gate if (editor == NULL) 303d61d085dScf46844 editor = VIPATH; 304d61d085dScf46844 #ifdef _XPG_NOTDEFINED 305d61d085dScf46844 } 306d61d085dScf46844 #endif 3071c0be37bSsn199410 argvec[0] = strdup(editor); 3081c0be37bSsn199410 argvec[1] = strdup(edtemp); 3091c0be37bSsn199410 argvec[2] = NULL; 3101c0be37bSsn199410 3111c0be37bSsn199410 if (argvec[0] == NULL || argvec[1] == NULL) 3121c0be37bSsn199410 crabort("Insufficient memory"); 3131c0be37bSsn199410 3147c478bd9Sstevel@tonic-gate sleep(1); 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate while (1) { 3171c0be37bSsn199410 /* 3181c0be37bSsn199410 * posix_spawnp() allows the file pointed to 3191c0be37bSsn199410 * by the 'EDITOR' variable to be searched in 3201c0be37bSsn199410 * the PATH environment variable 3211c0be37bSsn199410 */ 3221c0be37bSsn199410 3231c0be37bSsn199410 ret = posix_spawnp(&cpid, editor, NULL, NULL, 3241c0be37bSsn199410 (char *const *)argvec, 3251c0be37bSsn199410 (char *const *)environ); 3261c0be37bSsn199410 if (ret) { 3271c0be37bSsn199410 (void) fprintf(stderr, 3281c0be37bSsn199410 gettext("crontab: %s: %s\n"), 3291c0be37bSsn199410 editor, strerror(errno)); 3301c0be37bSsn199410 cstatus = -1; 3311c0be37bSsn199410 } else { 3321c0be37bSsn199410 pid_t wpid = 0; 3331c0be37bSsn199410 while ((wpid = waitpid(cpid, &cstatus, 3341c0be37bSsn199410 0)) == -1 && errno == EINTR) 3351c0be37bSsn199410 ; 3361c0be37bSsn199410 if (wpid == -1) 3371c0be37bSsn199410 cstatus = -1; 3381c0be37bSsn199410 } 3391c0be37bSsn199410 3407c478bd9Sstevel@tonic-gate /* sanity checks */ 3417c478bd9Sstevel@tonic-gate if ((tmpfp = fopen(edtemp, "r")) == NULL) 3427c478bd9Sstevel@tonic-gate crabort("can't open temporary file"); 3437c478bd9Sstevel@tonic-gate if (fstat(fileno(tmpfp), &stbuf) < 0) 3447c478bd9Sstevel@tonic-gate crabort("can't stat temporary file"); 3457c478bd9Sstevel@tonic-gate if (stbuf.st_size == 0) 3467c478bd9Sstevel@tonic-gate crabort("temporary file empty"); 3477c478bd9Sstevel@tonic-gate if (omodtime == stbuf.st_mtime) { 3487c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 3497c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 3503d63ea05Sas145665 "The crontab file was not" 3513d63ea05Sas145665 " changed.\n")); 3527c478bd9Sstevel@tonic-gate exit(1); 3537c478bd9Sstevel@tonic-gate } 3541c0be37bSsn199410 if ((cstatus) && (errno != EINTR)) { 3557c478bd9Sstevel@tonic-gate /* 3567c478bd9Sstevel@tonic-gate * Some editors (like 'vi') can return 3577c478bd9Sstevel@tonic-gate * a non-zero exit status even though 3587c478bd9Sstevel@tonic-gate * everything is okay. Need to check. 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ED_ERROR)); 3617c478bd9Sstevel@tonic-gate fflush(stderr); 3627c478bd9Sstevel@tonic-gate if (isatty(fileno(stdin))) { 3637c478bd9Sstevel@tonic-gate /* Interactive */ 3641c0be37bSsn199410 fprintf(stdout, 3651c0be37bSsn199410 gettext(ED_PROMPT), 3663d63ea05Sas145665 yesstr, nostr, nostr); 3677c478bd9Sstevel@tonic-gate fflush(stdout); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate if (yes()) { 3707c478bd9Sstevel@tonic-gate /* Edit again */ 3717c478bd9Sstevel@tonic-gate continue; 3727c478bd9Sstevel@tonic-gate } else { 3737c478bd9Sstevel@tonic-gate /* Dump changes */ 3747c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 3757c478bd9Sstevel@tonic-gate exit(1); 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate } else { 3781c0be37bSsn199410 /* 3791c0be37bSsn199410 * Non-interactive, dump changes 3801c0be37bSsn199410 */ 3817c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 3827c478bd9Sstevel@tonic-gate exit(1); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate exit(0); 3867c478bd9Sstevel@tonic-gate } /* while (1) */ 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* fix for 1125555 - ignore common signals while waiting */ 3907c478bd9Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 3917c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 3927c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN); 3937c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, SIG_IGN); 3947c478bd9Sstevel@tonic-gate wait(&stat_loc); 3957c478bd9Sstevel@tonic-gate if ((stat_loc & 0xFF00) != 0) 3967c478bd9Sstevel@tonic-gate exit(1); 3977c478bd9Sstevel@tonic-gate 3981c0be37bSsn199410 /* 3991c0be37bSsn199410 * unlink edtemp as 'ruid'. The file contents will be held 4001c0be37bSsn199410 * since we open the file descriptor 'tmpfp' before calling 4011c0be37bSsn199410 * unlink. 4021c0be37bSsn199410 */ 4031c0be37bSsn199410 if (((ret = seteuid(ruid)) < 0) || 4041c0be37bSsn199410 ((tmpfp = fopen(edtemp, "r")) == NULL) || 4051c0be37bSsn199410 (unlink(edtemp) == -1)) { 4067c478bd9Sstevel@tonic-gate fprintf(stderr, "crontab: %s: %s\n", 4077c478bd9Sstevel@tonic-gate edtemp, errmsg(errno)); 4081c0be37bSsn199410 if ((ret < 0) || (tmpfp == NULL)) 4097c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 4107c478bd9Sstevel@tonic-gate exit(1); 4117c478bd9Sstevel@tonic-gate } else 4127c478bd9Sstevel@tonic-gate seteuid(0); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate copycron(tmpfp); 4157c478bd9Sstevel@tonic-gate } else { 4167c478bd9Sstevel@tonic-gate if (argc == 0) 4177c478bd9Sstevel@tonic-gate copycron(stdin); 4187c478bd9Sstevel@tonic-gate else if (seteuid(getuid()) != 0 || (fp = fopen(argv[0], "r")) 4197c478bd9Sstevel@tonic-gate == NULL) 4207c478bd9Sstevel@tonic-gate crabort(BADOPEN); 4217c478bd9Sstevel@tonic-gate else { 4227c478bd9Sstevel@tonic-gate seteuid(0); 4237c478bd9Sstevel@tonic-gate copycron(fp); 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate cron_sendmsg(ADD, login, login, CRON); 4277c478bd9Sstevel@tonic-gate /* 4287c478bd9Sstevel@tonic-gate * if (per_errno == 2) 4297c478bd9Sstevel@tonic-gate * fprintf(stderr, gettext(WARNSHELL)); 4307c478bd9Sstevel@tonic-gate */ 4317c478bd9Sstevel@tonic-gate return (0); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate static void 4357c478bd9Sstevel@tonic-gate copycron(fp) 4367c478bd9Sstevel@tonic-gate FILE *fp; 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate FILE *tfp; 4397c478bd9Sstevel@tonic-gate char pid[6], *tnam_end; 4407c478bd9Sstevel@tonic-gate int t; 4415b08e637SChris Gerhard char buf[LINE_MAX]; 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate sprintf(pid, "%-5d", getpid()); 4447c478bd9Sstevel@tonic-gate tnam = xmalloc(strlen(CRONDIR)+strlen(TMPFILE)+7); 4457c478bd9Sstevel@tonic-gate strcat(strcat(strcat(strcpy(tnam, CRONDIR), "/"), TMPFILE), pid); 4467c478bd9Sstevel@tonic-gate /* cut trailing blanks */ 4477c478bd9Sstevel@tonic-gate tnam_end = strchr(tnam, ' '); 4487c478bd9Sstevel@tonic-gate if (tnam_end != NULL) 4497c478bd9Sstevel@tonic-gate *tnam_end = 0; 4507c478bd9Sstevel@tonic-gate /* catch SIGINT, SIGHUP, SIGQUIT signals */ 4517c478bd9Sstevel@tonic-gate if (signal(SIGINT, catch) == SIG_IGN) 4527c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 4537c478bd9Sstevel@tonic-gate if (signal(SIGHUP, catch) == SIG_IGN) signal(SIGHUP, SIG_IGN); 4547c478bd9Sstevel@tonic-gate if (signal(SIGQUIT, catch) == SIG_IGN) signal(SIGQUIT, SIG_IGN); 4557c478bd9Sstevel@tonic-gate if (signal(SIGTERM, catch) == SIG_IGN) signal(SIGTERM, SIG_IGN); 4567c478bd9Sstevel@tonic-gate if ((t = creat(tnam, CRMODE)) == -1) crabort(BADCREATE); 4577c478bd9Sstevel@tonic-gate if ((tfp = fdopen(t, "w")) == NULL) { 4587c478bd9Sstevel@tonic-gate unlink(tnam); 4597c478bd9Sstevel@tonic-gate crabort(BADCREATE); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate err = 0; /* if errors found, err set to 1 */ 4627c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, fp) != NULL) { 4637c478bd9Sstevel@tonic-gate cursor = 0; 4647c478bd9Sstevel@tonic-gate while (line[cursor] == ' ' || line[cursor] == '\t') 4657c478bd9Sstevel@tonic-gate cursor++; 4667c478bd9Sstevel@tonic-gate /* fix for 1039689 - treat blank line like a comment */ 4677c478bd9Sstevel@tonic-gate if (line[cursor] == '#' || line[cursor] == '\n') 4687c478bd9Sstevel@tonic-gate goto cont; 4695b08e637SChris Gerhard 4705b08e637SChris Gerhard if (strncmp(&line[cursor], ENV_TZ, strlen(ENV_TZ)) == 0) { 4715b08e637SChris Gerhard char *x; 4725b08e637SChris Gerhard 4735b08e637SChris Gerhard strncpy(buf, &line[cursor + strlen(ENV_TZ)], 4745b08e637SChris Gerhard sizeof (buf)); 4755b08e637SChris Gerhard if ((x = strchr(buf, '\n')) != NULL) 4765b08e637SChris Gerhard *x = NULL; 4775b08e637SChris Gerhard 4785b08e637SChris Gerhard if (isvalid_tz(buf, NULL, _VTZ_ALL)) { 4795b08e637SChris Gerhard goto cont; 4805b08e637SChris Gerhard } else { 4815b08e637SChris Gerhard err = 1; 4825b08e637SChris Gerhard fprintf(stderr, BAD_TZ, &line[cursor]); 4835b08e637SChris Gerhard continue; 4845b08e637SChris Gerhard } 4855b08e637SChris Gerhard } else if (strncmp(&line[cursor], ENV_SHELL, 4865b08e637SChris Gerhard strlen(ENV_SHELL)) == 0) { 4875b08e637SChris Gerhard char *x; 4885b08e637SChris Gerhard 4895b08e637SChris Gerhard strncpy(buf, &line[cursor + strlen(ENV_SHELL)], 4905b08e637SChris Gerhard sizeof (buf)); 4915b08e637SChris Gerhard if ((x = strchr(buf, '\n')) != NULL) 4925b08e637SChris Gerhard *x = NULL; 4935b08e637SChris Gerhard 4945b08e637SChris Gerhard if (isvalid_shell(buf)) { 4955b08e637SChris Gerhard goto cont; 4965b08e637SChris Gerhard } else { 4975b08e637SChris Gerhard err = 1; 4985b08e637SChris Gerhard fprintf(stderr, BAD_SHELL, &line[cursor]); 4995b08e637SChris Gerhard continue; 5005b08e637SChris Gerhard } 5015b08e637SChris Gerhard } else if (strncmp(&line[cursor], ENV_HOME, 5025b08e637SChris Gerhard strlen(ENV_HOME)) == 0) { 5035b08e637SChris Gerhard char *x; 5045b08e637SChris Gerhard 5055b08e637SChris Gerhard strncpy(buf, &line[cursor + strlen(ENV_HOME)], 5065b08e637SChris Gerhard sizeof (buf)); 5075b08e637SChris Gerhard if ((x = strchr(buf, '\n')) != NULL) 5085b08e637SChris Gerhard *x = NULL; 5095b08e637SChris Gerhard if (chdir(buf) == 0) { 5105b08e637SChris Gerhard goto cont; 5115b08e637SChris Gerhard } else { 5125b08e637SChris Gerhard err = 1; 5135b08e637SChris Gerhard fprintf(stderr, BAD_HOME, &line[cursor], 5145b08e637SChris Gerhard strerror(errno)); 5155b08e637SChris Gerhard continue; 5165b08e637SChris Gerhard } 5175b08e637SChris Gerhard } 5185b08e637SChris Gerhard 5197c478bd9Sstevel@tonic-gate if (next_field(0, 59)) continue; 5207c478bd9Sstevel@tonic-gate if (next_field(0, 23)) continue; 5217c478bd9Sstevel@tonic-gate if (next_field(1, 31)) continue; 5227c478bd9Sstevel@tonic-gate if (next_field(1, 12)) continue; 5237c478bd9Sstevel@tonic-gate if (next_field(0, 06)) continue; 5247c478bd9Sstevel@tonic-gate if (line[++cursor] == '\0') { 5257c478bd9Sstevel@tonic-gate cerror(EOLN); 5267c478bd9Sstevel@tonic-gate continue; 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate cont: 5297c478bd9Sstevel@tonic-gate if (fputs(line, tfp) == EOF) { 5307c478bd9Sstevel@tonic-gate unlink(tnam); 5317c478bd9Sstevel@tonic-gate crabort(BADCREATE); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate fclose(fp); 5357c478bd9Sstevel@tonic-gate fclose(tfp); 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate /* audit differences between old and new crontabs */ 5387c478bd9Sstevel@tonic-gate audit_crontab_modify(cf, tnam, err); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (!err) { 5417c478bd9Sstevel@tonic-gate /* make file tfp the new crontab */ 5427c478bd9Sstevel@tonic-gate unlink(cf); 5437c478bd9Sstevel@tonic-gate if (link(tnam, cf) == -1) { 5447c478bd9Sstevel@tonic-gate unlink(tnam); 5457c478bd9Sstevel@tonic-gate crabort(BADCREATE); 5467c478bd9Sstevel@tonic-gate } 547*b0d0a1c8SViswanathan Kannappan } else { 548*b0d0a1c8SViswanathan Kannappan crabort(ERRSFND); 549*b0d0a1c8SViswanathan Kannappan } 5507c478bd9Sstevel@tonic-gate unlink(tnam); 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate static int 5547c478bd9Sstevel@tonic-gate next_field(lower, upper) 5557c478bd9Sstevel@tonic-gate int lower, upper; 5567c478bd9Sstevel@tonic-gate { 5577c478bd9Sstevel@tonic-gate int num, num2; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate while ((line[cursor] == ' ') || (line[cursor] == '\t')) cursor++; 5607c478bd9Sstevel@tonic-gate if (line[cursor] == '\0') { 5617c478bd9Sstevel@tonic-gate cerror(EOLN); 5627c478bd9Sstevel@tonic-gate return (1); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate if (line[cursor] == '*') { 5657c478bd9Sstevel@tonic-gate cursor++; 5667c478bd9Sstevel@tonic-gate if ((line[cursor] != ' ') && (line[cursor] != '\t')) { 5677c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 5687c478bd9Sstevel@tonic-gate return (1); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate return (0); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate while (TRUE) { 5737c478bd9Sstevel@tonic-gate if (!isdigit(line[cursor])) { 5747c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 5757c478bd9Sstevel@tonic-gate return (1); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate num = 0; 5787c478bd9Sstevel@tonic-gate do { 5797c478bd9Sstevel@tonic-gate num = num*10 + (line[cursor]-'0'); 5807c478bd9Sstevel@tonic-gate } while (isdigit(line[++cursor])); 5817c478bd9Sstevel@tonic-gate if ((num < lower) || (num > upper)) { 5827c478bd9Sstevel@tonic-gate cerror(OUTOFBOUND); 5837c478bd9Sstevel@tonic-gate return (1); 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate if (line[cursor] == '-') { 5867c478bd9Sstevel@tonic-gate if (!isdigit(line[++cursor])) { 5877c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 5887c478bd9Sstevel@tonic-gate return (1); 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate num2 = 0; 5917c478bd9Sstevel@tonic-gate do { 5927c478bd9Sstevel@tonic-gate num2 = num2*10 + (line[cursor]-'0'); 5937c478bd9Sstevel@tonic-gate } while (isdigit(line[++cursor])); 5947c478bd9Sstevel@tonic-gate if ((num2 < lower) || (num2 > upper)) { 5957c478bd9Sstevel@tonic-gate cerror(OUTOFBOUND); 5967c478bd9Sstevel@tonic-gate return (1); 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate if ((line[cursor] == ' ') || (line[cursor] == '\t')) break; 6007c478bd9Sstevel@tonic-gate if (line[cursor] == '\0') { 6017c478bd9Sstevel@tonic-gate cerror(EOLN); 6027c478bd9Sstevel@tonic-gate return (1); 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate if (line[cursor++] != ',') { 6057c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 6067c478bd9Sstevel@tonic-gate return (1); 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate return (0); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate static void 6137c478bd9Sstevel@tonic-gate cerror(msg) 6147c478bd9Sstevel@tonic-gate char *msg; 6157c478bd9Sstevel@tonic-gate { 6167c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%scrontab: error on previous line; %s\n"), 6177c478bd9Sstevel@tonic-gate line, msg); 6187c478bd9Sstevel@tonic-gate err = 1; 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate static void 6237c478bd9Sstevel@tonic-gate catch(int x) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate unlink(tnam); 6267c478bd9Sstevel@tonic-gate exit(1); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate static void 6307c478bd9Sstevel@tonic-gate crabort(msg) 6317c478bd9Sstevel@tonic-gate char *msg; 6327c478bd9Sstevel@tonic-gate { 6337c478bd9Sstevel@tonic-gate int sverrno; 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate if (strcmp(edtemp, "") != 0) { 6367c478bd9Sstevel@tonic-gate sverrno = errno; 6377c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 6387c478bd9Sstevel@tonic-gate errno = sverrno; 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate if (tnam != NULL) { 6417c478bd9Sstevel@tonic-gate sverrno = errno; 6427c478bd9Sstevel@tonic-gate (void) unlink(tnam); 6437c478bd9Sstevel@tonic-gate errno = sverrno; 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate fprintf(stderr, "crontab: %s\n", gettext(msg)); 6467c478bd9Sstevel@tonic-gate exit(1); 6477c478bd9Sstevel@tonic-gate } 648