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 /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 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 <sys/types.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/wait.h> 36*7c478bd9Sstevel@tonic-gate #include <errno.h> 37*7c478bd9Sstevel@tonic-gate #include <signal.h> 38*7c478bd9Sstevel@tonic-gate #include <stdio.h> 39*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 40*7c478bd9Sstevel@tonic-gate #include <string.h> 41*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 42*7c478bd9Sstevel@tonic-gate #include <ctype.h> 43*7c478bd9Sstevel@tonic-gate #include <pwd.h> 44*7c478bd9Sstevel@tonic-gate #include <unistd.h> 45*7c478bd9Sstevel@tonic-gate #include <locale.h> 46*7c478bd9Sstevel@tonic-gate #include <nl_types.h> 47*7c478bd9Sstevel@tonic-gate #include <langinfo.h> 48*7c478bd9Sstevel@tonic-gate #include <libintl.h> 49*7c478bd9Sstevel@tonic-gate #include <security/pam_appl.h> 50*7c478bd9Sstevel@tonic-gate #include "cron.h" 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate #define TMPFILE "_cron" /* prefix for tmp file */ 53*7c478bd9Sstevel@tonic-gate #define CRMODE 0600 /* mode for creating crontabs */ 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate #define BADCREATE \ 56*7c478bd9Sstevel@tonic-gate "can't create your crontab file in the crontab directory." 57*7c478bd9Sstevel@tonic-gate #define BADOPEN "can't open your crontab file." 58*7c478bd9Sstevel@tonic-gate #define BADSHELL \ 59*7c478bd9Sstevel@tonic-gate "because your login shell isn't /usr/bin/sh, you can't use cron." 60*7c478bd9Sstevel@tonic-gate #define WARNSHELL "warning: commands will be executed using /usr/bin/sh\n" 61*7c478bd9Sstevel@tonic-gate #define BADUSAGE \ 62*7c478bd9Sstevel@tonic-gate "proper usage is: \n crontab [file | -e | -l | -r ] [user]" 63*7c478bd9Sstevel@tonic-gate #define INVALIDUSER "you are not a valid user (no entry in /etc/passwd)." 64*7c478bd9Sstevel@tonic-gate #define NOTALLOWED "you are not authorized to use cron. Sorry." 65*7c478bd9Sstevel@tonic-gate #define NOTROOT \ 66*7c478bd9Sstevel@tonic-gate "you must be super-user to access another user's crontab file" 67*7c478bd9Sstevel@tonic-gate #define AUDITREJECT "The audit context for your shell has not been set." 68*7c478bd9Sstevel@tonic-gate #define EOLN "unexpected end of line." 69*7c478bd9Sstevel@tonic-gate #define UNEXPECT "unexpected character found in line." 70*7c478bd9Sstevel@tonic-gate #define OUTOFBOUND "number out of bounds." 71*7c478bd9Sstevel@tonic-gate #define ERRSFND "errors detected in input, no crontab file generated." 72*7c478bd9Sstevel@tonic-gate #define ED_ERROR \ 73*7c478bd9Sstevel@tonic-gate " The editor indicates that an error occurred while you were\n"\ 74*7c478bd9Sstevel@tonic-gate " editing the crontab data - usually a minor typing error.\n\n" 75*7c478bd9Sstevel@tonic-gate #define BADREAD "error reading your crontab file" 76*7c478bd9Sstevel@tonic-gate #define ED_PROMPT \ 77*7c478bd9Sstevel@tonic-gate " Edit again, to ensure crontab information is intact (%c/%c)?\n"\ 78*7c478bd9Sstevel@tonic-gate " ('%c' will discard edits.)" 79*7c478bd9Sstevel@tonic-gate #define NAMETOOLONG "login name too long" 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate extern int per_errno; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate extern int audit_crontab_modify(char *, char *, int); 84*7c478bd9Sstevel@tonic-gate extern int audit_crontab_delete(char *, int); 85*7c478bd9Sstevel@tonic-gate extern int audit_crontab_not_allowed(uid_t, char *); 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate int err; 88*7c478bd9Sstevel@tonic-gate int cursor; 89*7c478bd9Sstevel@tonic-gate char *cf; 90*7c478bd9Sstevel@tonic-gate char *tnam; 91*7c478bd9Sstevel@tonic-gate char edtemp[5+13+1]; 92*7c478bd9Sstevel@tonic-gate char line[CTLINESIZE]; 93*7c478bd9Sstevel@tonic-gate static char login[UNAMESIZE]; 94*7c478bd9Sstevel@tonic-gate static char yeschr; 95*7c478bd9Sstevel@tonic-gate static char nochr; 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate static int yes(void); 98*7c478bd9Sstevel@tonic-gate static int next_field(int, int); 99*7c478bd9Sstevel@tonic-gate static void catch(int); 100*7c478bd9Sstevel@tonic-gate static void crabort(char *); 101*7c478bd9Sstevel@tonic-gate static void cerror(char *); 102*7c478bd9Sstevel@tonic-gate static void copycron(FILE *); 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate main(argc, argv) 105*7c478bd9Sstevel@tonic-gate int argc; 106*7c478bd9Sstevel@tonic-gate char **argv; 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate int c, r; 109*7c478bd9Sstevel@tonic-gate int rflag = 0; 110*7c478bd9Sstevel@tonic-gate int lflag = 0; 111*7c478bd9Sstevel@tonic-gate int eflag = 0; 112*7c478bd9Sstevel@tonic-gate int errflg = 0; 113*7c478bd9Sstevel@tonic-gate char *pp; 114*7c478bd9Sstevel@tonic-gate FILE *fp, *tmpfp; 115*7c478bd9Sstevel@tonic-gate struct stat stbuf; 116*7c478bd9Sstevel@tonic-gate struct passwd *pwp; 117*7c478bd9Sstevel@tonic-gate time_t omodtime; 118*7c478bd9Sstevel@tonic-gate char *editor; 119*7c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 120*7c478bd9Sstevel@tonic-gate uid_t ruid; 121*7c478bd9Sstevel@tonic-gate pid_t pid; 122*7c478bd9Sstevel@tonic-gate int stat_loc; 123*7c478bd9Sstevel@tonic-gate int ret; 124*7c478bd9Sstevel@tonic-gate char real_login[UNAMESIZE]; 125*7c478bd9Sstevel@tonic-gate int tmpfd = -1; 126*7c478bd9Sstevel@tonic-gate pam_handle_t *pamh; 127*7c478bd9Sstevel@tonic-gate int pam_error; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 130*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 131*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 132*7c478bd9Sstevel@tonic-gate #endif 133*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 134*7c478bd9Sstevel@tonic-gate yeschr = *nl_langinfo(YESSTR); 135*7c478bd9Sstevel@tonic-gate nochr = *nl_langinfo(NOSTR); 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "elr")) != EOF) 138*7c478bd9Sstevel@tonic-gate switch (c) { 139*7c478bd9Sstevel@tonic-gate case 'e': 140*7c478bd9Sstevel@tonic-gate eflag++; 141*7c478bd9Sstevel@tonic-gate break; 142*7c478bd9Sstevel@tonic-gate case 'l': 143*7c478bd9Sstevel@tonic-gate lflag++; 144*7c478bd9Sstevel@tonic-gate break; 145*7c478bd9Sstevel@tonic-gate case 'r': 146*7c478bd9Sstevel@tonic-gate rflag++; 147*7c478bd9Sstevel@tonic-gate break; 148*7c478bd9Sstevel@tonic-gate case '?': 149*7c478bd9Sstevel@tonic-gate errflg++; 150*7c478bd9Sstevel@tonic-gate break; 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate if (eflag + lflag + rflag > 1) 154*7c478bd9Sstevel@tonic-gate errflg++; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate argc -= optind; 157*7c478bd9Sstevel@tonic-gate argv += optind; 158*7c478bd9Sstevel@tonic-gate if (errflg || argc > 1) 159*7c478bd9Sstevel@tonic-gate crabort(BADUSAGE); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate ruid = getuid(); 162*7c478bd9Sstevel@tonic-gate if ((pwp = getpwuid(ruid)) == NULL) 163*7c478bd9Sstevel@tonic-gate crabort(INVALIDUSER); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate if (strlcpy(real_login, pwp->pw_name, sizeof (real_login)) 166*7c478bd9Sstevel@tonic-gate >= sizeof (real_login)) 167*7c478bd9Sstevel@tonic-gate crabort(NAMETOOLONG); 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate if ((eflag || lflag || rflag) && argc == 1) { 170*7c478bd9Sstevel@tonic-gate if ((pwp = getpwnam(*argv)) == NULL) 171*7c478bd9Sstevel@tonic-gate crabort(INVALIDUSER); 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate if (!chkauthattr(CRONADMIN_AUTH, real_login)) { 174*7c478bd9Sstevel@tonic-gate if (pwp->pw_uid != ruid) 175*7c478bd9Sstevel@tonic-gate crabort(NOTROOT); 176*7c478bd9Sstevel@tonic-gate else 177*7c478bd9Sstevel@tonic-gate pp = getuser(ruid); 178*7c478bd9Sstevel@tonic-gate } else 179*7c478bd9Sstevel@tonic-gate pp = *argv++; 180*7c478bd9Sstevel@tonic-gate } else { 181*7c478bd9Sstevel@tonic-gate pp = getuser(ruid); 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate if (pp == NULL) { 185*7c478bd9Sstevel@tonic-gate if (per_errno == 2) 186*7c478bd9Sstevel@tonic-gate crabort(BADSHELL); 187*7c478bd9Sstevel@tonic-gate else 188*7c478bd9Sstevel@tonic-gate crabort(INVALIDUSER); 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate if (strlcpy(login, pp, sizeof (login)) >= sizeof (login)) 191*7c478bd9Sstevel@tonic-gate crabort(NAMETOOLONG); 192*7c478bd9Sstevel@tonic-gate if (!allowed(login, CRONALLOW, CRONDENY)) 193*7c478bd9Sstevel@tonic-gate crabort(NOTALLOWED); 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate /* Do account validation check */ 196*7c478bd9Sstevel@tonic-gate pam_error = pam_start("cron", pp, NULL, &pamh); 197*7c478bd9Sstevel@tonic-gate if (pam_error != PAM_SUCCESS) { 198*7c478bd9Sstevel@tonic-gate crabort((char *)pam_strerror(pamh, pam_error)); 199*7c478bd9Sstevel@tonic-gate } 200*7c478bd9Sstevel@tonic-gate pam_error = pam_acct_mgmt(pamh, PAM_SILENT); 201*7c478bd9Sstevel@tonic-gate if (pam_error != PAM_SUCCESS) { 202*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning - Invalid account: " 203*7c478bd9Sstevel@tonic-gate "'%s' not allowed to execute cronjobs\n"), pp); 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate (void) pam_end(pamh, PAM_SUCCESS); 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate /* check for unaudited shell */ 209*7c478bd9Sstevel@tonic-gate if (audit_crontab_not_allowed(ruid, pp)) 210*7c478bd9Sstevel@tonic-gate crabort(AUDITREJECT); 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate cf = xmalloc(strlen(CRONDIR)+strlen(login)+2); 213*7c478bd9Sstevel@tonic-gate strcat(strcat(strcpy(cf, CRONDIR), "/"), login); 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate if (rflag) { 216*7c478bd9Sstevel@tonic-gate r = unlink(cf); 217*7c478bd9Sstevel@tonic-gate cron_sendmsg(DELETE, login, login, CRON); 218*7c478bd9Sstevel@tonic-gate audit_crontab_delete(cf, r); 219*7c478bd9Sstevel@tonic-gate exit(0); 220*7c478bd9Sstevel@tonic-gate } 221*7c478bd9Sstevel@tonic-gate if (lflag) { 222*7c478bd9Sstevel@tonic-gate if ((fp = fopen(cf, "r")) == NULL) 223*7c478bd9Sstevel@tonic-gate crabort(BADOPEN); 224*7c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, fp) != NULL) 225*7c478bd9Sstevel@tonic-gate fputs(line, stdout); 226*7c478bd9Sstevel@tonic-gate fclose(fp); 227*7c478bd9Sstevel@tonic-gate exit(0); 228*7c478bd9Sstevel@tonic-gate } 229*7c478bd9Sstevel@tonic-gate if (eflag) { 230*7c478bd9Sstevel@tonic-gate if ((fp = fopen(cf, "r")) == NULL) { 231*7c478bd9Sstevel@tonic-gate if (errno != ENOENT) 232*7c478bd9Sstevel@tonic-gate crabort(BADOPEN); 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate (void) strcpy(edtemp, "/tmp/crontabXXXXXX"); 235*7c478bd9Sstevel@tonic-gate tmpfd = mkstemp(edtemp); 236*7c478bd9Sstevel@tonic-gate if (fchown(tmpfd, ruid, -1) == -1) { 237*7c478bd9Sstevel@tonic-gate (void) close(tmpfd); 238*7c478bd9Sstevel@tonic-gate crabort("fchown of temporary file failed"); 239*7c478bd9Sstevel@tonic-gate } 240*7c478bd9Sstevel@tonic-gate (void) close(tmpfd); 241*7c478bd9Sstevel@tonic-gate /* 242*7c478bd9Sstevel@tonic-gate * Fork off a child with user's permissions, 243*7c478bd9Sstevel@tonic-gate * to edit the crontab file 244*7c478bd9Sstevel@tonic-gate */ 245*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)-1) 246*7c478bd9Sstevel@tonic-gate crabort("fork failed"); 247*7c478bd9Sstevel@tonic-gate if (pid == 0) { /* child process */ 248*7c478bd9Sstevel@tonic-gate /* give up super-user privileges. */ 249*7c478bd9Sstevel@tonic-gate setuid(ruid); 250*7c478bd9Sstevel@tonic-gate if ((tmpfp = fopen(edtemp, "w")) == NULL) 251*7c478bd9Sstevel@tonic-gate crabort("can't create temporary file"); 252*7c478bd9Sstevel@tonic-gate if (fp != NULL) { 253*7c478bd9Sstevel@tonic-gate /* 254*7c478bd9Sstevel@tonic-gate * Copy user's crontab file to temporary file. 255*7c478bd9Sstevel@tonic-gate */ 256*7c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, fp) != NULL) { 257*7c478bd9Sstevel@tonic-gate fputs(line, tmpfp); 258*7c478bd9Sstevel@tonic-gate if (ferror(tmpfp)) { 259*7c478bd9Sstevel@tonic-gate fclose(fp); 260*7c478bd9Sstevel@tonic-gate fclose(tmpfp); 261*7c478bd9Sstevel@tonic-gate crabort("write error on" 262*7c478bd9Sstevel@tonic-gate "temporary file"); 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate if (ferror(fp)) { 266*7c478bd9Sstevel@tonic-gate fclose(fp); 267*7c478bd9Sstevel@tonic-gate fclose(tmpfp); 268*7c478bd9Sstevel@tonic-gate crabort(BADREAD); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate fclose(fp); 271*7c478bd9Sstevel@tonic-gate } 272*7c478bd9Sstevel@tonic-gate if (fclose(tmpfp) == EOF) 273*7c478bd9Sstevel@tonic-gate crabort("write error on temporary file"); 274*7c478bd9Sstevel@tonic-gate if (stat(edtemp, &stbuf) < 0) 275*7c478bd9Sstevel@tonic-gate crabort("can't stat temporary file"); 276*7c478bd9Sstevel@tonic-gate omodtime = stbuf.st_mtime; 277*7c478bd9Sstevel@tonic-gate editor = getenv("VISUAL"); 278*7c478bd9Sstevel@tonic-gate if (editor == NULL) 279*7c478bd9Sstevel@tonic-gate editor = getenv("EDITOR"); 280*7c478bd9Sstevel@tonic-gate if (editor == NULL) 281*7c478bd9Sstevel@tonic-gate editor = "ed"; 282*7c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), 283*7c478bd9Sstevel@tonic-gate "%s %s", editor, edtemp); 284*7c478bd9Sstevel@tonic-gate sleep(1); 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate while (1) { 287*7c478bd9Sstevel@tonic-gate ret = system(buf); 288*7c478bd9Sstevel@tonic-gate /* sanity checks */ 289*7c478bd9Sstevel@tonic-gate if ((tmpfp = fopen(edtemp, "r")) == NULL) 290*7c478bd9Sstevel@tonic-gate crabort("can't open temporary file"); 291*7c478bd9Sstevel@tonic-gate if (fstat(fileno(tmpfp), &stbuf) < 0) 292*7c478bd9Sstevel@tonic-gate crabort("can't stat temporary file"); 293*7c478bd9Sstevel@tonic-gate if (stbuf.st_size == 0) 294*7c478bd9Sstevel@tonic-gate crabort("temporary file empty"); 295*7c478bd9Sstevel@tonic-gate if (omodtime == stbuf.st_mtime) { 296*7c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 297*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext( 298*7c478bd9Sstevel@tonic-gate "The crontab file was not changed.\n")); 299*7c478bd9Sstevel@tonic-gate exit(1); 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate if ((ret) && (errno != EINTR)) { 302*7c478bd9Sstevel@tonic-gate /* 303*7c478bd9Sstevel@tonic-gate * Some editors (like 'vi') can return 304*7c478bd9Sstevel@tonic-gate * a non-zero exit status even though 305*7c478bd9Sstevel@tonic-gate * everything is okay. Need to check. 306*7c478bd9Sstevel@tonic-gate */ 307*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ED_ERROR)); 308*7c478bd9Sstevel@tonic-gate fflush(stderr); 309*7c478bd9Sstevel@tonic-gate if (isatty(fileno(stdin))) { 310*7c478bd9Sstevel@tonic-gate /* Interactive */ 311*7c478bd9Sstevel@tonic-gate fprintf(stdout, gettext(ED_PROMPT), 312*7c478bd9Sstevel@tonic-gate yeschr, nochr, nochr); 313*7c478bd9Sstevel@tonic-gate fflush(stdout); 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate if (yes()) { 316*7c478bd9Sstevel@tonic-gate /* Edit again */ 317*7c478bd9Sstevel@tonic-gate continue; 318*7c478bd9Sstevel@tonic-gate } else { 319*7c478bd9Sstevel@tonic-gate /* Dump changes */ 320*7c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 321*7c478bd9Sstevel@tonic-gate exit(1); 322*7c478bd9Sstevel@tonic-gate } 323*7c478bd9Sstevel@tonic-gate } else { 324*7c478bd9Sstevel@tonic-gate /* Non-interactive, dump changes */ 325*7c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 326*7c478bd9Sstevel@tonic-gate exit(1); 327*7c478bd9Sstevel@tonic-gate } 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate exit(0); 330*7c478bd9Sstevel@tonic-gate } /* while (1) */ 331*7c478bd9Sstevel@tonic-gate } 332*7c478bd9Sstevel@tonic-gate 333*7c478bd9Sstevel@tonic-gate /* fix for 1125555 - ignore common signals while waiting */ 334*7c478bd9Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 335*7c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 336*7c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN); 337*7c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, SIG_IGN); 338*7c478bd9Sstevel@tonic-gate wait(&stat_loc); 339*7c478bd9Sstevel@tonic-gate if ((stat_loc & 0xFF00) != 0) 340*7c478bd9Sstevel@tonic-gate exit(1); 341*7c478bd9Sstevel@tonic-gate 342*7c478bd9Sstevel@tonic-gate if ((seteuid(ruid) < 0) || 343*7c478bd9Sstevel@tonic-gate ((tmpfp = fopen(edtemp, "r")) == NULL)) { 344*7c478bd9Sstevel@tonic-gate fprintf(stderr, "crontab: %s: %s\n", 345*7c478bd9Sstevel@tonic-gate edtemp, errmsg(errno)); 346*7c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 347*7c478bd9Sstevel@tonic-gate exit(1); 348*7c478bd9Sstevel@tonic-gate } else 349*7c478bd9Sstevel@tonic-gate seteuid(0); 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate copycron(tmpfp); 352*7c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 353*7c478bd9Sstevel@tonic-gate } else { 354*7c478bd9Sstevel@tonic-gate if (argc == 0) 355*7c478bd9Sstevel@tonic-gate copycron(stdin); 356*7c478bd9Sstevel@tonic-gate else if (seteuid(getuid()) != 0 || (fp = fopen(argv[0], "r")) 357*7c478bd9Sstevel@tonic-gate == NULL) 358*7c478bd9Sstevel@tonic-gate crabort(BADOPEN); 359*7c478bd9Sstevel@tonic-gate else { 360*7c478bd9Sstevel@tonic-gate seteuid(0); 361*7c478bd9Sstevel@tonic-gate copycron(fp); 362*7c478bd9Sstevel@tonic-gate } 363*7c478bd9Sstevel@tonic-gate } 364*7c478bd9Sstevel@tonic-gate cron_sendmsg(ADD, login, login, CRON); 365*7c478bd9Sstevel@tonic-gate /* 366*7c478bd9Sstevel@tonic-gate * if (per_errno == 2) 367*7c478bd9Sstevel@tonic-gate * fprintf(stderr, gettext(WARNSHELL)); 368*7c478bd9Sstevel@tonic-gate */ 369*7c478bd9Sstevel@tonic-gate return (0); 370*7c478bd9Sstevel@tonic-gate } 371*7c478bd9Sstevel@tonic-gate 372*7c478bd9Sstevel@tonic-gate static void 373*7c478bd9Sstevel@tonic-gate copycron(fp) 374*7c478bd9Sstevel@tonic-gate FILE *fp; 375*7c478bd9Sstevel@tonic-gate { 376*7c478bd9Sstevel@tonic-gate FILE *tfp; 377*7c478bd9Sstevel@tonic-gate char pid[6], *tnam_end; 378*7c478bd9Sstevel@tonic-gate int t; 379*7c478bd9Sstevel@tonic-gate 380*7c478bd9Sstevel@tonic-gate sprintf(pid, "%-5d", getpid()); 381*7c478bd9Sstevel@tonic-gate tnam = xmalloc(strlen(CRONDIR)+strlen(TMPFILE)+7); 382*7c478bd9Sstevel@tonic-gate strcat(strcat(strcat(strcpy(tnam, CRONDIR), "/"), TMPFILE), pid); 383*7c478bd9Sstevel@tonic-gate /* cut trailing blanks */ 384*7c478bd9Sstevel@tonic-gate tnam_end = strchr(tnam, ' '); 385*7c478bd9Sstevel@tonic-gate if (tnam_end != NULL) 386*7c478bd9Sstevel@tonic-gate *tnam_end = 0; 387*7c478bd9Sstevel@tonic-gate /* catch SIGINT, SIGHUP, SIGQUIT signals */ 388*7c478bd9Sstevel@tonic-gate if (signal(SIGINT, catch) == SIG_IGN) 389*7c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 390*7c478bd9Sstevel@tonic-gate if (signal(SIGHUP, catch) == SIG_IGN) signal(SIGHUP, SIG_IGN); 391*7c478bd9Sstevel@tonic-gate if (signal(SIGQUIT, catch) == SIG_IGN) signal(SIGQUIT, SIG_IGN); 392*7c478bd9Sstevel@tonic-gate if (signal(SIGTERM, catch) == SIG_IGN) signal(SIGTERM, SIG_IGN); 393*7c478bd9Sstevel@tonic-gate if ((t = creat(tnam, CRMODE)) == -1) crabort(BADCREATE); 394*7c478bd9Sstevel@tonic-gate if ((tfp = fdopen(t, "w")) == NULL) { 395*7c478bd9Sstevel@tonic-gate unlink(tnam); 396*7c478bd9Sstevel@tonic-gate crabort(BADCREATE); 397*7c478bd9Sstevel@tonic-gate } 398*7c478bd9Sstevel@tonic-gate err = 0; /* if errors found, err set to 1 */ 399*7c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, fp) != NULL) { 400*7c478bd9Sstevel@tonic-gate cursor = 0; 401*7c478bd9Sstevel@tonic-gate while (line[cursor] == ' ' || line[cursor] == '\t') 402*7c478bd9Sstevel@tonic-gate cursor++; 403*7c478bd9Sstevel@tonic-gate /* fix for 1039689 - treat blank line like a comment */ 404*7c478bd9Sstevel@tonic-gate if (line[cursor] == '#' || line[cursor] == '\n') 405*7c478bd9Sstevel@tonic-gate goto cont; 406*7c478bd9Sstevel@tonic-gate if (next_field(0, 59)) continue; 407*7c478bd9Sstevel@tonic-gate if (next_field(0, 23)) continue; 408*7c478bd9Sstevel@tonic-gate if (next_field(1, 31)) continue; 409*7c478bd9Sstevel@tonic-gate if (next_field(1, 12)) continue; 410*7c478bd9Sstevel@tonic-gate if (next_field(0, 06)) continue; 411*7c478bd9Sstevel@tonic-gate if (line[++cursor] == '\0') { 412*7c478bd9Sstevel@tonic-gate cerror(EOLN); 413*7c478bd9Sstevel@tonic-gate continue; 414*7c478bd9Sstevel@tonic-gate } 415*7c478bd9Sstevel@tonic-gate cont: 416*7c478bd9Sstevel@tonic-gate if (fputs(line, tfp) == EOF) { 417*7c478bd9Sstevel@tonic-gate unlink(tnam); 418*7c478bd9Sstevel@tonic-gate crabort(BADCREATE); 419*7c478bd9Sstevel@tonic-gate } 420*7c478bd9Sstevel@tonic-gate } 421*7c478bd9Sstevel@tonic-gate fclose(fp); 422*7c478bd9Sstevel@tonic-gate fclose(tfp); 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate /* audit differences between old and new crontabs */ 425*7c478bd9Sstevel@tonic-gate audit_crontab_modify(cf, tnam, err); 426*7c478bd9Sstevel@tonic-gate 427*7c478bd9Sstevel@tonic-gate if (!err) { 428*7c478bd9Sstevel@tonic-gate /* make file tfp the new crontab */ 429*7c478bd9Sstevel@tonic-gate unlink(cf); 430*7c478bd9Sstevel@tonic-gate if (link(tnam, cf) == -1) { 431*7c478bd9Sstevel@tonic-gate unlink(tnam); 432*7c478bd9Sstevel@tonic-gate crabort(BADCREATE); 433*7c478bd9Sstevel@tonic-gate } 434*7c478bd9Sstevel@tonic-gate } else 435*7c478bd9Sstevel@tonic-gate fprintf(stderr, "crontab: %s\n", gettext(ERRSFND)); 436*7c478bd9Sstevel@tonic-gate unlink(tnam); 437*7c478bd9Sstevel@tonic-gate } 438*7c478bd9Sstevel@tonic-gate 439*7c478bd9Sstevel@tonic-gate static int 440*7c478bd9Sstevel@tonic-gate next_field(lower, upper) 441*7c478bd9Sstevel@tonic-gate int lower, upper; 442*7c478bd9Sstevel@tonic-gate { 443*7c478bd9Sstevel@tonic-gate int num, num2; 444*7c478bd9Sstevel@tonic-gate 445*7c478bd9Sstevel@tonic-gate while ((line[cursor] == ' ') || (line[cursor] == '\t')) cursor++; 446*7c478bd9Sstevel@tonic-gate if (line[cursor] == '\0') { 447*7c478bd9Sstevel@tonic-gate cerror(EOLN); 448*7c478bd9Sstevel@tonic-gate return (1); 449*7c478bd9Sstevel@tonic-gate } 450*7c478bd9Sstevel@tonic-gate if (line[cursor] == '*') { 451*7c478bd9Sstevel@tonic-gate cursor++; 452*7c478bd9Sstevel@tonic-gate if ((line[cursor] != ' ') && (line[cursor] != '\t')) { 453*7c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 454*7c478bd9Sstevel@tonic-gate return (1); 455*7c478bd9Sstevel@tonic-gate } 456*7c478bd9Sstevel@tonic-gate return (0); 457*7c478bd9Sstevel@tonic-gate } 458*7c478bd9Sstevel@tonic-gate while (TRUE) { 459*7c478bd9Sstevel@tonic-gate if (!isdigit(line[cursor])) { 460*7c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 461*7c478bd9Sstevel@tonic-gate return (1); 462*7c478bd9Sstevel@tonic-gate } 463*7c478bd9Sstevel@tonic-gate num = 0; 464*7c478bd9Sstevel@tonic-gate do { 465*7c478bd9Sstevel@tonic-gate num = num*10 + (line[cursor]-'0'); 466*7c478bd9Sstevel@tonic-gate } while (isdigit(line[++cursor])); 467*7c478bd9Sstevel@tonic-gate if ((num < lower) || (num > upper)) { 468*7c478bd9Sstevel@tonic-gate cerror(OUTOFBOUND); 469*7c478bd9Sstevel@tonic-gate return (1); 470*7c478bd9Sstevel@tonic-gate } 471*7c478bd9Sstevel@tonic-gate if (line[cursor] == '-') { 472*7c478bd9Sstevel@tonic-gate if (!isdigit(line[++cursor])) { 473*7c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 474*7c478bd9Sstevel@tonic-gate return (1); 475*7c478bd9Sstevel@tonic-gate } 476*7c478bd9Sstevel@tonic-gate num2 = 0; 477*7c478bd9Sstevel@tonic-gate do { 478*7c478bd9Sstevel@tonic-gate num2 = num2*10 + (line[cursor]-'0'); 479*7c478bd9Sstevel@tonic-gate } while (isdigit(line[++cursor])); 480*7c478bd9Sstevel@tonic-gate if ((num2 < lower) || (num2 > upper)) { 481*7c478bd9Sstevel@tonic-gate cerror(OUTOFBOUND); 482*7c478bd9Sstevel@tonic-gate return (1); 483*7c478bd9Sstevel@tonic-gate } 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate if ((line[cursor] == ' ') || (line[cursor] == '\t')) break; 486*7c478bd9Sstevel@tonic-gate if (line[cursor] == '\0') { 487*7c478bd9Sstevel@tonic-gate cerror(EOLN); 488*7c478bd9Sstevel@tonic-gate return (1); 489*7c478bd9Sstevel@tonic-gate } 490*7c478bd9Sstevel@tonic-gate if (line[cursor++] != ',') { 491*7c478bd9Sstevel@tonic-gate cerror(UNEXPECT); 492*7c478bd9Sstevel@tonic-gate return (1); 493*7c478bd9Sstevel@tonic-gate } 494*7c478bd9Sstevel@tonic-gate } 495*7c478bd9Sstevel@tonic-gate return (0); 496*7c478bd9Sstevel@tonic-gate } 497*7c478bd9Sstevel@tonic-gate 498*7c478bd9Sstevel@tonic-gate static void 499*7c478bd9Sstevel@tonic-gate cerror(msg) 500*7c478bd9Sstevel@tonic-gate char *msg; 501*7c478bd9Sstevel@tonic-gate { 502*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%scrontab: error on previous line; %s\n"), 503*7c478bd9Sstevel@tonic-gate line, msg); 504*7c478bd9Sstevel@tonic-gate err = 1; 505*7c478bd9Sstevel@tonic-gate } 506*7c478bd9Sstevel@tonic-gate 507*7c478bd9Sstevel@tonic-gate 508*7c478bd9Sstevel@tonic-gate static void 509*7c478bd9Sstevel@tonic-gate catch(int x) 510*7c478bd9Sstevel@tonic-gate { 511*7c478bd9Sstevel@tonic-gate unlink(tnam); 512*7c478bd9Sstevel@tonic-gate exit(1); 513*7c478bd9Sstevel@tonic-gate } 514*7c478bd9Sstevel@tonic-gate 515*7c478bd9Sstevel@tonic-gate static void 516*7c478bd9Sstevel@tonic-gate crabort(msg) 517*7c478bd9Sstevel@tonic-gate char *msg; 518*7c478bd9Sstevel@tonic-gate { 519*7c478bd9Sstevel@tonic-gate int sverrno; 520*7c478bd9Sstevel@tonic-gate 521*7c478bd9Sstevel@tonic-gate if (strcmp(edtemp, "") != 0) { 522*7c478bd9Sstevel@tonic-gate sverrno = errno; 523*7c478bd9Sstevel@tonic-gate (void) unlink(edtemp); 524*7c478bd9Sstevel@tonic-gate errno = sverrno; 525*7c478bd9Sstevel@tonic-gate } 526*7c478bd9Sstevel@tonic-gate if (tnam != NULL) { 527*7c478bd9Sstevel@tonic-gate sverrno = errno; 528*7c478bd9Sstevel@tonic-gate (void) unlink(tnam); 529*7c478bd9Sstevel@tonic-gate errno = sverrno; 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate fprintf(stderr, "crontab: %s\n", gettext(msg)); 532*7c478bd9Sstevel@tonic-gate exit(1); 533*7c478bd9Sstevel@tonic-gate } 534*7c478bd9Sstevel@tonic-gate 535*7c478bd9Sstevel@tonic-gate static int 536*7c478bd9Sstevel@tonic-gate yes(void) 537*7c478bd9Sstevel@tonic-gate { 538*7c478bd9Sstevel@tonic-gate int first_char; 539*7c478bd9Sstevel@tonic-gate int dummy_char; 540*7c478bd9Sstevel@tonic-gate 541*7c478bd9Sstevel@tonic-gate first_char = dummy_char = getchar(); 542*7c478bd9Sstevel@tonic-gate while ((dummy_char != '\n') && 543*7c478bd9Sstevel@tonic-gate (dummy_char != '\0') && 544*7c478bd9Sstevel@tonic-gate (dummy_char != EOF)) 545*7c478bd9Sstevel@tonic-gate dummy_char = getchar(); 546*7c478bd9Sstevel@tonic-gate return (first_char == yeschr); 547*7c478bd9Sstevel@tonic-gate } 548