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 /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 317c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #ifdef lint 367c478bd9Sstevel@tonic-gate /* make lint happy */ 377c478bd9Sstevel@tonic-gate #define __EXTENSIONS__ 387c478bd9Sstevel@tonic-gate #endif 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include <sys/contract/process.h> 417c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 427c478bd9Sstevel@tonic-gate #include <sys/param.h> 437c478bd9Sstevel@tonic-gate #include <sys/resource.h> 447c478bd9Sstevel@tonic-gate #include <sys/stat.h> 457c478bd9Sstevel@tonic-gate #include <sys/task.h> 467c478bd9Sstevel@tonic-gate #include <sys/time.h> 477c478bd9Sstevel@tonic-gate #include <sys/types.h> 487c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 497c478bd9Sstevel@tonic-gate #include <sys/wait.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #include <security/pam_appl.h> 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #include <alloca.h> 547c478bd9Sstevel@tonic-gate #include <ctype.h> 557c478bd9Sstevel@tonic-gate #include <deflt.h> 567c478bd9Sstevel@tonic-gate #include <dirent.h> 577c478bd9Sstevel@tonic-gate #include <errno.h> 587c478bd9Sstevel@tonic-gate #include <fcntl.h> 597c478bd9Sstevel@tonic-gate #include <grp.h> 607c478bd9Sstevel@tonic-gate #include <libcontract.h> 617c478bd9Sstevel@tonic-gate #include <libcontract_priv.h> 627c478bd9Sstevel@tonic-gate #include <limits.h> 637c478bd9Sstevel@tonic-gate #include <locale.h> 647c478bd9Sstevel@tonic-gate #include <poll.h> 657c478bd9Sstevel@tonic-gate #include <project.h> 667c478bd9Sstevel@tonic-gate #include <pwd.h> 677c478bd9Sstevel@tonic-gate #include <signal.h> 687c478bd9Sstevel@tonic-gate #include <stdarg.h> 697c478bd9Sstevel@tonic-gate #include <stdio.h> 707c478bd9Sstevel@tonic-gate #include <stdlib.h> 717c478bd9Sstevel@tonic-gate #include <string.h> 727c478bd9Sstevel@tonic-gate #include <stropts.h> 737c478bd9Sstevel@tonic-gate #include <time.h> 747c478bd9Sstevel@tonic-gate #include <unistd.h> 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #include "cron.h" 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * #define DEBUG 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate #define MAIL "/usr/bin/mail" /* mail program to use */ 837c478bd9Sstevel@tonic-gate #define CONSOLE "/dev/console" /* where messages go when cron dies */ 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #define TMPINFILE "/tmp/crinXXXXXX" /* file to put stdin in for cmd */ 867c478bd9Sstevel@tonic-gate #define TMPDIR "/tmp" 877c478bd9Sstevel@tonic-gate #define PFX "crout" 887c478bd9Sstevel@tonic-gate #define TMPOUTFILE "/tmp/croutXXXXXX" /* file to place stdout, stderr */ 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate #define INMODE 00400 /* mode for stdin file */ 917c478bd9Sstevel@tonic-gate #define OUTMODE 00600 /* mode for stdout file */ 927c478bd9Sstevel@tonic-gate #define ISUID S_ISUID /* mode for verifing at jobs */ 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate #define INFINITY 2147483647L /* upper bound on time */ 957c478bd9Sstevel@tonic-gate #define CUSHION 180L 967c478bd9Sstevel@tonic-gate #define ZOMB 100 /* proc slot used for mailing output */ 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate #define JOBF 'j' 997c478bd9Sstevel@tonic-gate #define NICEF 'n' 1007c478bd9Sstevel@tonic-gate #define USERF 'u' 1017c478bd9Sstevel@tonic-gate #define WAITF 'w' 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate #define BCHAR '>' 1047c478bd9Sstevel@tonic-gate #define ECHAR '<' 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate #define DEFAULT 0 1077c478bd9Sstevel@tonic-gate #define LOAD 1 1087c478bd9Sstevel@tonic-gate #define QBUFSIZ 80 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate /* Defined actions for crabort() routine */ 1117c478bd9Sstevel@tonic-gate #define NO_ACTION 000 1127c478bd9Sstevel@tonic-gate #define REMOVE_FIFO 001 1137c478bd9Sstevel@tonic-gate #define CONSOLE_MSG 002 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate #define BADCD "can't change directory to the crontab directory." 1167c478bd9Sstevel@tonic-gate #define NOREADDIR "can't read the crontab directory." 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate #define BADJOBOPEN "unable to read your at job." 1197c478bd9Sstevel@tonic-gate #define BADSHELL "because your login shell \ 1207c478bd9Sstevel@tonic-gate isn't /usr/bin/sh, you can't use cron." 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate #define BADSTAT "can't access your crontab file. Resubmit it." 1237c478bd9Sstevel@tonic-gate #define BADPROJID "can't set project id for your job." 1247c478bd9Sstevel@tonic-gate #define CANTCDHOME "can't change directory to your home directory.\ 1257c478bd9Sstevel@tonic-gate \nYour commands will not be executed." 1267c478bd9Sstevel@tonic-gate #define CANTEXECSH "unable to exec the shell for one of your commands." 1277c478bd9Sstevel@tonic-gate #define NOREAD "can't read your crontab file. Resubmit it." 1287c478bd9Sstevel@tonic-gate #define BADTYPE "crontab is not a regular file.\n" 1297c478bd9Sstevel@tonic-gate #define NOSTDIN "unable to create a standard input file for \ 1307c478bd9Sstevel@tonic-gate one of your crontab commands. \ 1317c478bd9Sstevel@tonic-gate \nThat command was not executed." 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate #define NOTALLOWED "you are not authorized to use cron. Sorry." 1347c478bd9Sstevel@tonic-gate #define STDERRMSG "\n\n********************************************\ 1357c478bd9Sstevel@tonic-gate *****\nCron: The previous message is the \ 1367c478bd9Sstevel@tonic-gate standard output and standard error \ 1377c478bd9Sstevel@tonic-gate \nof one of your cron commands.\n" 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate #define STDOUTERR "one of your commands generated output or errors, \ 1407c478bd9Sstevel@tonic-gate but cron was unable to mail you this output.\ 1417c478bd9Sstevel@tonic-gate \nRemember to redirect standard output and standard \ 1427c478bd9Sstevel@tonic-gate error for each of your commands." 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate #define CLOCK_DRIFT "clock time drifted backwards after event!\n" 1457c478bd9Sstevel@tonic-gate #define PIDERR "unexpected pid returned %d (ignored)" 1467c478bd9Sstevel@tonic-gate #define CRONTABERR "Subject: Your crontab file has an error in it\n\n" 1477c478bd9Sstevel@tonic-gate #define CRONOUT "Subject: Output from \"cron\" command\n\n" 1487c478bd9Sstevel@tonic-gate #define MALLOCERR "out of space, cannot create new string\n" 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate #define DIDFORK didfork 1517c478bd9Sstevel@tonic-gate #define NOFORK !didfork 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate #define MAILBUFLEN (8*1024) 1547c478bd9Sstevel@tonic-gate #define LINELIMIT 80 1557c478bd9Sstevel@tonic-gate #define MAILBINITFREE (MAILBUFLEN - (sizeof (cte_intro) - 1) \ 1567c478bd9Sstevel@tonic-gate - (sizeof (cte_trail1) - 1) - (sizeof (cte_trail2) - 1) - 1) 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate #define ERR_CRONTABENT 0 /* error in crontab file entry */ 1597c478bd9Sstevel@tonic-gate #define ERR_UNIXERR 1 /* error in some system call */ 1607c478bd9Sstevel@tonic-gate #define ERR_CANTEXECCRON 2 /* error setting up "cron" job environment */ 1617c478bd9Sstevel@tonic-gate #define ERR_CANTEXECAT 3 /* error setting up "at" job environment */ 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate #define PROJECT "project=" 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate #define MAX_LOST_CONTRACTS 2048 /* reset if this many failed abandons */ 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate #define FORMAT "%a %b %e %H:%M:%S %Y" 1687c478bd9Sstevel@tonic-gate static char timebuf[80]; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate struct event { 1717c478bd9Sstevel@tonic-gate time_t time; /* time of the event */ 1727c478bd9Sstevel@tonic-gate short etype; /* what type of event; 0=cron, 1=at */ 1737c478bd9Sstevel@tonic-gate char *cmd; /* command for cron, job name for at */ 1747c478bd9Sstevel@tonic-gate struct usr *u; /* ptr to the owner (usr) of this event */ 1757c478bd9Sstevel@tonic-gate struct event *link; /* ptr to another event for this user */ 1767c478bd9Sstevel@tonic-gate union { 1777c478bd9Sstevel@tonic-gate struct { /* for crontab events */ 1787c478bd9Sstevel@tonic-gate char *minute; /* (these */ 1797c478bd9Sstevel@tonic-gate char *hour; /* fields */ 1807c478bd9Sstevel@tonic-gate char *daymon; /* are */ 1817c478bd9Sstevel@tonic-gate char *month; /* from */ 1827c478bd9Sstevel@tonic-gate char *dayweek; /* crontab) */ 1837c478bd9Sstevel@tonic-gate char *input; /* ptr to stdin */ 1847c478bd9Sstevel@tonic-gate } ct; 1857c478bd9Sstevel@tonic-gate struct { /* for at events */ 1867c478bd9Sstevel@tonic-gate short exists; /* for revising at events */ 1877c478bd9Sstevel@tonic-gate int eventid; /* for el_remove-ing at events */ 1887c478bd9Sstevel@tonic-gate } at; 1897c478bd9Sstevel@tonic-gate } of; 1907c478bd9Sstevel@tonic-gate }; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate struct usr { 1937c478bd9Sstevel@tonic-gate char *name; /* name of user (e.g. "root") */ 1947c478bd9Sstevel@tonic-gate char *home; /* home directory for user */ 1957c478bd9Sstevel@tonic-gate uid_t uid; /* user id */ 1967c478bd9Sstevel@tonic-gate gid_t gid; /* group id */ 1977c478bd9Sstevel@tonic-gate int aruncnt; /* counter for running jobs per uid */ 1987c478bd9Sstevel@tonic-gate int cruncnt; /* counter for running cron jobs per uid */ 1997c478bd9Sstevel@tonic-gate int ctid; /* for el_remove-ing crontab events */ 2007c478bd9Sstevel@tonic-gate short ctexists; /* for revising crontab events */ 2017c478bd9Sstevel@tonic-gate struct event *ctevents; /* list of this usr's crontab events */ 2027c478bd9Sstevel@tonic-gate struct event *atevents; /* list of this usr's at events */ 2037c478bd9Sstevel@tonic-gate struct usr *nextusr; 2047c478bd9Sstevel@tonic-gate }; /* ptr to next user */ 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate static struct queue 2077c478bd9Sstevel@tonic-gate { 2087c478bd9Sstevel@tonic-gate int njob; /* limit */ 2097c478bd9Sstevel@tonic-gate int nice; /* nice for execution */ 2107c478bd9Sstevel@tonic-gate int nwait; /* wait time to next execution attempt */ 2117c478bd9Sstevel@tonic-gate int nrun; /* number running */ 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate qd = {100, 2, 60}, /* default values for queue defs */ 2147c478bd9Sstevel@tonic-gate qt[NQUEUE]; 2157c478bd9Sstevel@tonic-gate static struct queue qq; 2167c478bd9Sstevel@tonic-gate 217*9f163834Sbasabi static struct runinfo 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate pid_t pid; 2207c478bd9Sstevel@tonic-gate short que; 2217c478bd9Sstevel@tonic-gate struct usr *rusr; /* pointer to usr struct */ 2227c478bd9Sstevel@tonic-gate char *outfile; /* file where stdout & stderr are trapped */ 2237c478bd9Sstevel@tonic-gate short jobtype; /* what type of event: 0=cron, 1=at */ 2247c478bd9Sstevel@tonic-gate char *jobname; /* command for "cron", jobname for "at" */ 2257c478bd9Sstevel@tonic-gate int mailwhendone; /* 1 = send mail even if no ouptut */ 226*9f163834Sbasabi struct runinfo *next; 227*9f163834Sbasabi } *rthead; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate static struct miscpid { 2307c478bd9Sstevel@tonic-gate pid_t pid; 2317c478bd9Sstevel@tonic-gate struct miscpid *next; 2327c478bd9Sstevel@tonic-gate } *miscpid_head; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static pid_t cron_pid; /* own pid */ 2357c478bd9Sstevel@tonic-gate static char didfork = 0; /* flag to see if I'm process group leader */ 2367c478bd9Sstevel@tonic-gate static int msgfd; /* file descriptor for fifo queue */ 2377c478bd9Sstevel@tonic-gate static int ecid = 1; /* event class id for el_remove(); MUST be set to 1 */ 2387c478bd9Sstevel@tonic-gate static int delayed; /* is job being rescheduled or did it run first time */ 2397c478bd9Sstevel@tonic-gate static int cwd; /* current working directory */ 2407c478bd9Sstevel@tonic-gate static struct event *next_event; /* the next event to execute */ 2417c478bd9Sstevel@tonic-gate static struct usr *uhead; /* ptr to the list of users */ 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* Variables for error handling at reading crontabs. */ 2447c478bd9Sstevel@tonic-gate static char cte_intro[] = "Line(s) with errors:\n\n"; 2457c478bd9Sstevel@tonic-gate static char cte_trail1[] = "\nMax number of errors encountered."; 2467c478bd9Sstevel@tonic-gate static char cte_trail2[] = " Evaluation of crontab aborted.\n"; 2477c478bd9Sstevel@tonic-gate static int cte_free = MAILBINITFREE; /* Free buffer space */ 2487c478bd9Sstevel@tonic-gate static char *cte_text = NULL; /* Text buffer pointer */ 2497c478bd9Sstevel@tonic-gate static char *cte_lp; /* Next free line in cte_text */ 2507c478bd9Sstevel@tonic-gate static int cte_nvalid; /* Valid lines found */ 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* user's default environment for the shell */ 2537c478bd9Sstevel@tonic-gate #define ROOTPATH "PATH=/usr/sbin:/usr/bin" 2547c478bd9Sstevel@tonic-gate #define NONROOTPATH "PATH=/usr/bin:" 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate static char *Def_supath = NULL; 2577c478bd9Sstevel@tonic-gate static char *Def_path = NULL; 2587c478bd9Sstevel@tonic-gate static char path[LINE_MAX] = "PATH="; 2597c478bd9Sstevel@tonic-gate static char supath[LINE_MAX] = "PATH="; 2607c478bd9Sstevel@tonic-gate static char homedir[LINE_MAX] = "HOME="; 2617c478bd9Sstevel@tonic-gate static char logname[LINE_MAX] = "LOGNAME="; 2627c478bd9Sstevel@tonic-gate static char tzone[LINE_MAX] = "TZ="; 2637c478bd9Sstevel@tonic-gate static char *envinit[] = { 2647c478bd9Sstevel@tonic-gate homedir, 2657c478bd9Sstevel@tonic-gate logname, 2667c478bd9Sstevel@tonic-gate ROOTPATH, 2677c478bd9Sstevel@tonic-gate "SHELL=/usr/bin/sh", 2687c478bd9Sstevel@tonic-gate tzone, 2697c478bd9Sstevel@tonic-gate NULL 2707c478bd9Sstevel@tonic-gate }; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate extern char **environ; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate #define DEFTZ "GMT" 2757c478bd9Sstevel@tonic-gate static int log = 0; 2767c478bd9Sstevel@tonic-gate static char hzname[10]; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate static void cronend(int); 2797c478bd9Sstevel@tonic-gate static void thaw_handler(int); 2807c478bd9Sstevel@tonic-gate static void child_handler(int); 2817c478bd9Sstevel@tonic-gate static void child_sigreset(void); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate static void dscan(DIR *dir, void (*fp)(char *, time_t)); 2847c478bd9Sstevel@tonic-gate static void mod_ctab(char *, time_t); 2857c478bd9Sstevel@tonic-gate static void mod_atjob(char *, time_t); 2867c478bd9Sstevel@tonic-gate static void add_atevent(struct usr *, char *, time_t, int); 2877c478bd9Sstevel@tonic-gate static void rm_ctevents(struct usr *); 2887c478bd9Sstevel@tonic-gate static void cleanup(struct runinfo *rn, int r); 2897c478bd9Sstevel@tonic-gate static void crabort(char *, int); 2907c478bd9Sstevel@tonic-gate static void msg(char *fmt, ...); 2917c478bd9Sstevel@tonic-gate static void logit(int, struct runinfo *, int); 2927c478bd9Sstevel@tonic-gate static void parsqdef(char *); 2937c478bd9Sstevel@tonic-gate static void defaults(); 2947c478bd9Sstevel@tonic-gate static void initialize(int); 2957c478bd9Sstevel@tonic-gate static void quedefs(int); 2967c478bd9Sstevel@tonic-gate static int idle(long); 2977c478bd9Sstevel@tonic-gate static struct usr *find_usr(char *); 2987c478bd9Sstevel@tonic-gate static int ex(struct event *e); 2997c478bd9Sstevel@tonic-gate static void read_dirs(void); 3007c478bd9Sstevel@tonic-gate static void mail(char *, char *, int); 3017c478bd9Sstevel@tonic-gate static char *next_field(int, int); 3027c478bd9Sstevel@tonic-gate static void readcron(struct usr *, time_t); 3037c478bd9Sstevel@tonic-gate static int next_ge(int, char *); 3047c478bd9Sstevel@tonic-gate static void free_if_unused(struct usr *); 3057c478bd9Sstevel@tonic-gate static void del_atjob(char *, char *); 3067c478bd9Sstevel@tonic-gate static void del_ctab(char *); 3077c478bd9Sstevel@tonic-gate static void resched(int); 3087c478bd9Sstevel@tonic-gate static int msg_wait(long); 3097c478bd9Sstevel@tonic-gate static struct runinfo *rinfo_get(pid_t); 3107c478bd9Sstevel@tonic-gate static void rinfo_free(struct runinfo *rp); 3117c478bd9Sstevel@tonic-gate static void mail_result(struct usr *p, struct runinfo *pr, size_t filesize); 3127c478bd9Sstevel@tonic-gate static time_t next_time(struct event *, time_t); 3137c478bd9Sstevel@tonic-gate static time_t get_switching_time(int, time_t); 3147c478bd9Sstevel@tonic-gate static time_t xmktime(struct tm *); 3157c478bd9Sstevel@tonic-gate static void process_msg(struct message *, time_t); 3167c478bd9Sstevel@tonic-gate static void reap_child(void); 3177c478bd9Sstevel@tonic-gate static void miscpid_insert(pid_t); 3187c478bd9Sstevel@tonic-gate static int miscpid_delete(pid_t); 319032624d5Sbasabi static void contract_set_template(void); 320032624d5Sbasabi static void contract_clear_template(void); 3217c478bd9Sstevel@tonic-gate static void contract_abandon_latest(pid_t); 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate static void cte_init(void); 3247c478bd9Sstevel@tonic-gate static void cte_add(int, char *); 3257c478bd9Sstevel@tonic-gate static void cte_valid(void); 3267c478bd9Sstevel@tonic-gate static int cte_istoomany(void); 3277c478bd9Sstevel@tonic-gate static void cte_sendmail(char *); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate static int set_user_cred(const struct usr *, struct project *); 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate /* 3327c478bd9Sstevel@tonic-gate * last_time is set immediately prior to exection of an event (via ex()) 3337c478bd9Sstevel@tonic-gate * to indicate the last time an event was executed. This was (surely) 3347c478bd9Sstevel@tonic-gate * it's original intended use. 3357c478bd9Sstevel@tonic-gate */ 3367c478bd9Sstevel@tonic-gate static time_t last_time, init_time, t_old; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate static int accept_sigcld, notifypipe[2]; 3397c478bd9Sstevel@tonic-gate static sigset_t defmask, childmask; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate /* 3427c478bd9Sstevel@tonic-gate * BSM hooks 3437c478bd9Sstevel@tonic-gate */ 3447c478bd9Sstevel@tonic-gate extern int audit_cron_session(char *, char *, uid_t, gid_t, char *); 3457c478bd9Sstevel@tonic-gate extern void audit_cron_new_job(char *, int, void *); 3467c478bd9Sstevel@tonic-gate extern void audit_cron_bad_user(char *); 3477c478bd9Sstevel@tonic-gate extern void audit_cron_user_acct_expired(char *); 3487c478bd9Sstevel@tonic-gate extern int audit_cron_create_anc_file(char *, char *, char *, uid_t); 3497c478bd9Sstevel@tonic-gate extern int audit_cron_delete_anc_file(char *, char *); 3507c478bd9Sstevel@tonic-gate extern int audit_cron_is_anc_name(char *); 3517c478bd9Sstevel@tonic-gate extern int audit_cron_mode(); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate static int cron_conv(int, struct pam_message **, 3547c478bd9Sstevel@tonic-gate struct pam_response **, void *); 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate static struct pam_conv pam_conv = {cron_conv, NULL}; 3577c478bd9Sstevel@tonic-gate static pam_handle_t *pamh; /* Authentication handle */ 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate /* 3607c478bd9Sstevel@tonic-gate * Function to help check a user's credentials. 3617c478bd9Sstevel@tonic-gate */ 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate static int verify_user_cred(const struct usr *u); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate /* 3667c478bd9Sstevel@tonic-gate * Values returned by verify_user_cred and set_user_cred: 3677c478bd9Sstevel@tonic-gate */ 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate #define VUC_OK 0 3707c478bd9Sstevel@tonic-gate #define VUC_BADUSER 1 3717c478bd9Sstevel@tonic-gate #define VUC_NOTINGROUP 2 3727c478bd9Sstevel@tonic-gate #define VUC_EXPIRED 3 3737c478bd9Sstevel@tonic-gate #define VUC_NEW_AUTH 4 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate /* 3767c478bd9Sstevel@tonic-gate * Modes of process_anc_files function 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate #define CRON_ANC_DELETE 1 3797c478bd9Sstevel@tonic-gate #define CRON_ANC_CREATE 0 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /* 3827c478bd9Sstevel@tonic-gate * Functions to remove a user or job completely from the running database. 3837c478bd9Sstevel@tonic-gate */ 3847c478bd9Sstevel@tonic-gate static void clean_out_atjobs(struct usr *u); 3857c478bd9Sstevel@tonic-gate static void clean_out_ctab(struct usr *u); 3867c478bd9Sstevel@tonic-gate static void clean_out_user(struct usr *u); 3877c478bd9Sstevel@tonic-gate static void cron_unlink(char *name); 3887c478bd9Sstevel@tonic-gate static void process_anc_files(int); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate /* 3917c478bd9Sstevel@tonic-gate * functions in elm.c 3927c478bd9Sstevel@tonic-gate */ 3937c478bd9Sstevel@tonic-gate extern void el_init(int, time_t, time_t, int); 3947c478bd9Sstevel@tonic-gate extern void el_add(void *, time_t, int); 3957c478bd9Sstevel@tonic-gate extern void el_remove(int, int); 3967c478bd9Sstevel@tonic-gate extern int el_empty(void); 3977c478bd9Sstevel@tonic-gate extern void *el_first(void); 3987c478bd9Sstevel@tonic-gate extern void el_delete(void); 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate int 4017c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate time_t t; 4047c478bd9Sstevel@tonic-gate time_t ne_time; /* amt of time until next event execution */ 4057c478bd9Sstevel@tonic-gate time_t newtime, lastmtime = 0L; 4067c478bd9Sstevel@tonic-gate struct usr *u; 4077c478bd9Sstevel@tonic-gate struct event *e, *e2, *eprev; 4087c478bd9Sstevel@tonic-gate struct stat buf; 4097c478bd9Sstevel@tonic-gate pid_t rfork; 4107c478bd9Sstevel@tonic-gate struct sigaction act; 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * reset is set to 1 via the return from ex() should ex() find 4147c478bd9Sstevel@tonic-gate * that the event to be executed is being run at the wrong time. 4157c478bd9Sstevel@tonic-gate * We immediately return to the top of the while (TRUE) loop in 4167c478bd9Sstevel@tonic-gate * main() where the event list is cleared and rebuilt, and reset 4177c478bd9Sstevel@tonic-gate * is set back to 0. 4187c478bd9Sstevel@tonic-gate */ 4197c478bd9Sstevel@tonic-gate int reset = 0; 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate /* 4227c478bd9Sstevel@tonic-gate * Only the privileged user can run this command. 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate if (getuid() != 0) 4257c478bd9Sstevel@tonic-gate crabort(NOTALLOWED, 0); 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate begin: 4287c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 4297c478bd9Sstevel@tonic-gate /* fork unless 'nofork' is specified */ 4307c478bd9Sstevel@tonic-gate if ((argc <= 1) || (strcmp(argv[1], "nofork"))) { 4317c478bd9Sstevel@tonic-gate if (rfork = fork()) { 4327c478bd9Sstevel@tonic-gate if (rfork == (pid_t)-1) { 4337c478bd9Sstevel@tonic-gate (void) sleep(30); 4347c478bd9Sstevel@tonic-gate goto begin; 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate return (0); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate didfork++; 4397c478bd9Sstevel@tonic-gate (void) setpgrp(); /* detach cron from console */ 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate (void) umask(022); 4437c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 4447c478bd9Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 4457c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN); 4467c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, cronend); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate defaults(); 4497c478bd9Sstevel@tonic-gate initialize(1); 4507c478bd9Sstevel@tonic-gate quedefs(DEFAULT); /* load default queue definitions */ 4517c478bd9Sstevel@tonic-gate cron_pid = getpid(); 4527c478bd9Sstevel@tonic-gate msg("*** cron started *** pid = %d", cron_pid); 4537c478bd9Sstevel@tonic-gate (void) sigset(SIGTHAW, thaw_handler); 4547c478bd9Sstevel@tonic-gate /* 4557c478bd9Sstevel@tonic-gate * setup SIGCLD handler/mask 4567c478bd9Sstevel@tonic-gate */ 4577c478bd9Sstevel@tonic-gate act.sa_handler = child_handler; 4587c478bd9Sstevel@tonic-gate act.sa_flags = 0; 4597c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 4607c478bd9Sstevel@tonic-gate (void) sigaddset(&act.sa_mask, SIGCLD); 4617c478bd9Sstevel@tonic-gate (void) sigaction(SIGCLD, &act, NULL); 4627c478bd9Sstevel@tonic-gate (void) sigemptyset(&childmask); 4637c478bd9Sstevel@tonic-gate (void) sigaddset(&childmask, SIGCLD); 4647c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &childmask, &defmask); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate if (pipe(notifypipe) != 0) { 4677c478bd9Sstevel@tonic-gate crabort("cannot create pipe", REMOVE_FIFO|CONSOLE_MSG); 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * will set O_NONBLOCK, so that the write() from child_handler 4717c478bd9Sstevel@tonic-gate * never be blocked. 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate (void) fcntl(notifypipe[0], F_SETFL, O_WRONLY|O_NONBLOCK); 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate t_old = time(NULL); 4767c478bd9Sstevel@tonic-gate last_time = t_old; 4777c478bd9Sstevel@tonic-gate for (;;) { /* MAIN LOOP */ 4787c478bd9Sstevel@tonic-gate t = time(NULL); 4797c478bd9Sstevel@tonic-gate if ((t_old > t) || (t-last_time > CUSHION) || reset) { 4807c478bd9Sstevel@tonic-gate reset = 0; 4817c478bd9Sstevel@tonic-gate /* the time was set backwards or forward */ 4827c478bd9Sstevel@tonic-gate el_delete(); 4837c478bd9Sstevel@tonic-gate u = uhead; 4847c478bd9Sstevel@tonic-gate while (u != NULL) { 4857c478bd9Sstevel@tonic-gate rm_ctevents(u); 4867c478bd9Sstevel@tonic-gate e = u->atevents; 4877c478bd9Sstevel@tonic-gate while (e != NULL) { 4887c478bd9Sstevel@tonic-gate free(e->cmd); 4897c478bd9Sstevel@tonic-gate e2 = e->link; 4907c478bd9Sstevel@tonic-gate free(e); 4917c478bd9Sstevel@tonic-gate e = e2; 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate u->atevents = NULL; 4947c478bd9Sstevel@tonic-gate u = u->nextusr; 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate (void) close(msgfd); 4977c478bd9Sstevel@tonic-gate initialize(0); 4987c478bd9Sstevel@tonic-gate t = time(NULL); 4997c478bd9Sstevel@tonic-gate last_time = t; 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate t_old = t; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate if (next_event == NULL && !el_empty()) { 5047c478bd9Sstevel@tonic-gate next_event = (struct event *)el_first(); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate if (next_event == NULL) { 5077c478bd9Sstevel@tonic-gate ne_time = INFINITY; 5087c478bd9Sstevel@tonic-gate } else { 5097c478bd9Sstevel@tonic-gate ne_time = next_event->time - t; 5107c478bd9Sstevel@tonic-gate #ifdef DEBUG 5117c478bd9Sstevel@tonic-gate cftime(timebuf, "%C", &next_event->time); 5127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "next_time=%ld %s\n", 5137c478bd9Sstevel@tonic-gate next_event->time, timebuf); 5147c478bd9Sstevel@tonic-gate #endif 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate if (ne_time > 0) { 5177c478bd9Sstevel@tonic-gate if ((reset = idle(ne_time)) != 0) 5187c478bd9Sstevel@tonic-gate continue; 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate if (stat(QUEDEFS, &buf)) { 5227c478bd9Sstevel@tonic-gate msg("cannot stat QUEDEFS file"); 5237c478bd9Sstevel@tonic-gate } else if (lastmtime != buf.st_mtime) { 5247c478bd9Sstevel@tonic-gate quedefs(LOAD); 5257c478bd9Sstevel@tonic-gate lastmtime = buf.st_mtime; 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate last_time = next_event->time; /* save execution time */ 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate if (reset = ex(next_event)) 5317c478bd9Sstevel@tonic-gate continue; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate switch (next_event->etype) { 5347c478bd9Sstevel@tonic-gate case CRONEVENT: 5357c478bd9Sstevel@tonic-gate /* add cronevent back into the main event list */ 5367c478bd9Sstevel@tonic-gate if (delayed) { 5377c478bd9Sstevel@tonic-gate delayed = 0; 5387c478bd9Sstevel@tonic-gate break; 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate /* 5427c478bd9Sstevel@tonic-gate * check if time(0)< last_time. if so, then the 5437c478bd9Sstevel@tonic-gate * system clock has gone backwards. to prevent this 5447c478bd9Sstevel@tonic-gate * job from being started twice, we reschedule this 5457c478bd9Sstevel@tonic-gate * job for the >>next time after last_time<<, and 5467c478bd9Sstevel@tonic-gate * then set next_event->time to this. note that 5477c478bd9Sstevel@tonic-gate * crontab's resolution is 1 minute. 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate if (last_time > time(NULL)) { 5517c478bd9Sstevel@tonic-gate msg(CLOCK_DRIFT); 5527c478bd9Sstevel@tonic-gate /* 5537c478bd9Sstevel@tonic-gate * bump up to next 30 second 5547c478bd9Sstevel@tonic-gate * increment 5557c478bd9Sstevel@tonic-gate * 1 <= newtime <= 30 5567c478bd9Sstevel@tonic-gate */ 5577c478bd9Sstevel@tonic-gate newtime = 30 - (last_time % 30); 5587c478bd9Sstevel@tonic-gate newtime += last_time; 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate /* 5617c478bd9Sstevel@tonic-gate * get the next scheduled event, 5627c478bd9Sstevel@tonic-gate * not the one that we just 5637c478bd9Sstevel@tonic-gate * kicked off! 5647c478bd9Sstevel@tonic-gate */ 5657c478bd9Sstevel@tonic-gate next_event->time = 5667c478bd9Sstevel@tonic-gate next_time(next_event, newtime); 5677c478bd9Sstevel@tonic-gate t_old = time(NULL); 5687c478bd9Sstevel@tonic-gate } else { 5697c478bd9Sstevel@tonic-gate next_event->time = 5707c478bd9Sstevel@tonic-gate next_time(next_event, (time_t)0); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate #ifdef DEBUG 5737c478bd9Sstevel@tonic-gate cftime(timebuf, "%C", &next_event->time); 5747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5757c478bd9Sstevel@tonic-gate "pushing back cron event %s at %ld (%s)\n", 5767c478bd9Sstevel@tonic-gate next_event->cmd, next_event->time, timebuf); 5777c478bd9Sstevel@tonic-gate #endif 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate el_add(next_event, next_event->time, 5807c478bd9Sstevel@tonic-gate (next_event->u)->ctid); 5817c478bd9Sstevel@tonic-gate break; 5827c478bd9Sstevel@tonic-gate default: 5837c478bd9Sstevel@tonic-gate /* remove at or batch job from system */ 5847c478bd9Sstevel@tonic-gate if (delayed) { 5857c478bd9Sstevel@tonic-gate delayed = 0; 5867c478bd9Sstevel@tonic-gate break; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate eprev = NULL; 5897c478bd9Sstevel@tonic-gate e = (next_event->u)->atevents; 5907c478bd9Sstevel@tonic-gate while (e != NULL) { 5917c478bd9Sstevel@tonic-gate if (e == next_event) { 5927c478bd9Sstevel@tonic-gate if (eprev == NULL) 5937c478bd9Sstevel@tonic-gate (e->u)->atevents = e->link; 5947c478bd9Sstevel@tonic-gate else 5957c478bd9Sstevel@tonic-gate eprev->link = e->link; 5967c478bd9Sstevel@tonic-gate free(e->cmd); 5977c478bd9Sstevel@tonic-gate free(e); 5987c478bd9Sstevel@tonic-gate break; 5997c478bd9Sstevel@tonic-gate } else { 6007c478bd9Sstevel@tonic-gate eprev = e; 6017c478bd9Sstevel@tonic-gate e = e->link; 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate break; 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate next_event = NULL; 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate static void 6137c478bd9Sstevel@tonic-gate initialize(int firstpass) 6147c478bd9Sstevel@tonic-gate { 6157c478bd9Sstevel@tonic-gate #ifdef DEBUG 6167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "in initialize\n"); 6177c478bd9Sstevel@tonic-gate #endif 6187c478bd9Sstevel@tonic-gate init_time = time(NULL); 6197c478bd9Sstevel@tonic-gate el_init(8, init_time, (time_t)(60*60*24), 10); 6207c478bd9Sstevel@tonic-gate if (firstpass) { 6217c478bd9Sstevel@tonic-gate /* for mail(1), make sure messages come from root */ 6227c478bd9Sstevel@tonic-gate if (putenv("LOGNAME=root") != 0) { 6237c478bd9Sstevel@tonic-gate crabort("cannot expand env variable", 6247c478bd9Sstevel@tonic-gate REMOVE_FIFO|CONSOLE_MSG); 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate if (access(FIFO, R_OK) == -1) { 6277c478bd9Sstevel@tonic-gate if (errno == ENOENT) { 6287c478bd9Sstevel@tonic-gate if (mknod(FIFO, S_IFIFO|0600, 0) != 0) 6297c478bd9Sstevel@tonic-gate crabort("cannot create fifo queue", 6307c478bd9Sstevel@tonic-gate REMOVE_FIFO|CONSOLE_MSG); 6317c478bd9Sstevel@tonic-gate } else { 6327c478bd9Sstevel@tonic-gate if (NOFORK) { 6337c478bd9Sstevel@tonic-gate /* didn't fork... init(1M) is waiting */ 6347c478bd9Sstevel@tonic-gate (void) sleep(60); 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate perror("FIFO"); 6377c478bd9Sstevel@tonic-gate crabort("cannot access fifo queue", 6387c478bd9Sstevel@tonic-gate REMOVE_FIFO|CONSOLE_MSG); 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate } else { 6417c478bd9Sstevel@tonic-gate if (NOFORK) { 6427c478bd9Sstevel@tonic-gate /* didn't fork... init(1M) is waiting */ 6437c478bd9Sstevel@tonic-gate (void) sleep(60); 6447c478bd9Sstevel@tonic-gate /* 6457c478bd9Sstevel@tonic-gate * the wait is painful, but we don't want 6467c478bd9Sstevel@tonic-gate * init respawning this quickly 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate crabort("cannot start cron; FIFO exists", CONSOLE_MSG); 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate if ((msgfd = open(FIFO, O_RDWR)) < 0) { 6547c478bd9Sstevel@tonic-gate perror("! open"); 6557c478bd9Sstevel@tonic-gate crabort("cannot open fifo queue", REMOVE_FIFO|CONSOLE_MSG); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * read directories, create users list, and add events to the 6607c478bd9Sstevel@tonic-gate * main event list. Only zero user list on firstpass. 6617c478bd9Sstevel@tonic-gate */ 6627c478bd9Sstevel@tonic-gate if (firstpass) 6637c478bd9Sstevel@tonic-gate uhead = NULL; 6647c478bd9Sstevel@tonic-gate read_dirs(); 6657c478bd9Sstevel@tonic-gate next_event = NULL; 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate if (!firstpass) 6687c478bd9Sstevel@tonic-gate return; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate /* stdout is log file */ 6717c478bd9Sstevel@tonic-gate if (freopen(ACCTFILE, "a", stdout) == NULL) 6727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "cannot open %s\n", ACCTFILE); 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate /* log should be root-only */ 6757c478bd9Sstevel@tonic-gate (void) fchmod(1, S_IRUSR|S_IWUSR); 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate /* stderr also goes to ACCTFILE */ 6787c478bd9Sstevel@tonic-gate (void) close(fileno(stderr)); 6797c478bd9Sstevel@tonic-gate (void) dup(1); 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate /* null for stdin */ 6827c478bd9Sstevel@tonic-gate (void) freopen("/dev/null", "r", stdin); 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate contract_set_template(); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate static void 6887c478bd9Sstevel@tonic-gate read_dirs() 6897c478bd9Sstevel@tonic-gate { 6907c478bd9Sstevel@tonic-gate DIR *dir; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate if (chdir(CRONDIR) == -1) 6937c478bd9Sstevel@tonic-gate crabort(BADCD, REMOVE_FIFO|CONSOLE_MSG); 6947c478bd9Sstevel@tonic-gate cwd = CRON; 6957c478bd9Sstevel@tonic-gate if ((dir = opendir(".")) == NULL) 6967c478bd9Sstevel@tonic-gate crabort(NOREADDIR, REMOVE_FIFO|CONSOLE_MSG); 6977c478bd9Sstevel@tonic-gate dscan(dir, mod_ctab); 6987c478bd9Sstevel@tonic-gate (void) closedir(dir); 6997c478bd9Sstevel@tonic-gate if (chdir(ATDIR) == -1) { 7007c478bd9Sstevel@tonic-gate msg("cannot chdir to at directory"); 7017c478bd9Sstevel@tonic-gate return; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate cwd = AT; 7047c478bd9Sstevel@tonic-gate if ((dir = opendir(".")) == NULL) { 7057c478bd9Sstevel@tonic-gate msg("cannot read at at directory"); 7067c478bd9Sstevel@tonic-gate return; 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate dscan(dir, mod_atjob); 7097c478bd9Sstevel@tonic-gate (void) closedir(dir); 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate static void 7137c478bd9Sstevel@tonic-gate dscan(DIR *df, void (*fp)(char *, time_t)) 7147c478bd9Sstevel@tonic-gate { 7157c478bd9Sstevel@tonic-gate struct dirent *dp; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate while ((dp = readdir(df)) != NULL) { 7187c478bd9Sstevel@tonic-gate if (strcmp(dp->d_name, ".") == 0 || 7197c478bd9Sstevel@tonic-gate strcmp(dp->d_name, "..") == 0) { 7207c478bd9Sstevel@tonic-gate continue; 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate (*fp)(dp->d_name, 0); 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate static void 7277c478bd9Sstevel@tonic-gate mod_ctab(char *name, time_t reftime) 7287c478bd9Sstevel@tonic-gate { 7297c478bd9Sstevel@tonic-gate struct passwd *pw; 7307c478bd9Sstevel@tonic-gate struct stat buf; 7317c478bd9Sstevel@tonic-gate struct usr *u; 7327c478bd9Sstevel@tonic-gate char namebuf[PATH_MAX]; 7337c478bd9Sstevel@tonic-gate char *pname; 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate /* skip over ancillary file names */ 7367c478bd9Sstevel@tonic-gate if (audit_cron_is_anc_name(name)) 7377c478bd9Sstevel@tonic-gate return; 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate if ((pw = getpwnam(name)) == NULL) { 7407c478bd9Sstevel@tonic-gate msg("No such user as %s - cron entries not created", name); 7417c478bd9Sstevel@tonic-gate return; 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate if (cwd != CRON) { 7447c478bd9Sstevel@tonic-gate if (snprintf(namebuf, sizeof (namebuf), "%s/%s", 7457c478bd9Sstevel@tonic-gate CRONDIR, name) >= sizeof (namebuf)) { 7467c478bd9Sstevel@tonic-gate msg("Too long path name %s - cron entries not created", 7477c478bd9Sstevel@tonic-gate namebuf); 7487c478bd9Sstevel@tonic-gate return; 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate pname = namebuf; 7517c478bd9Sstevel@tonic-gate } else { 7527c478bd9Sstevel@tonic-gate pname = name; 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate /* 7557c478bd9Sstevel@tonic-gate * a warning message is given by the crontab command so there is 7567c478bd9Sstevel@tonic-gate * no need to give one here...... use this code if you only want 7577c478bd9Sstevel@tonic-gate * users with a login shell of /usr/bin/sh to use cron 7587c478bd9Sstevel@tonic-gate */ 7597c478bd9Sstevel@tonic-gate #ifdef BOURNESHELLONLY 7607c478bd9Sstevel@tonic-gate if ((strcmp(pw->pw_shell, "") != 0) && 7617c478bd9Sstevel@tonic-gate (strcmp(pw->pw_shell, SHELL) != 0)) { 7627c478bd9Sstevel@tonic-gate mail(name, BADSHELL, ERR_CANTEXECCRON); 7637c478bd9Sstevel@tonic-gate cron_unlink(pname); 7647c478bd9Sstevel@tonic-gate return; 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate #endif 7677c478bd9Sstevel@tonic-gate if (stat(pname, &buf)) { 7687c478bd9Sstevel@tonic-gate mail(name, BADSTAT, ERR_UNIXERR); 7697c478bd9Sstevel@tonic-gate cron_unlink(pname); 7707c478bd9Sstevel@tonic-gate return; 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate if (!S_ISREG(buf.st_mode)) { 7737c478bd9Sstevel@tonic-gate mail(name, BADTYPE, ERR_CRONTABENT); 7747c478bd9Sstevel@tonic-gate return; 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate if ((u = find_usr(name)) == NULL) { 7777c478bd9Sstevel@tonic-gate #ifdef DEBUG 7787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "new user (%s) with a crontab\n", name); 7797c478bd9Sstevel@tonic-gate #endif 7807c478bd9Sstevel@tonic-gate u = xmalloc(sizeof (struct usr)); 7817c478bd9Sstevel@tonic-gate u->name = xmalloc(strlen(name)+1); 7827c478bd9Sstevel@tonic-gate (void) strcpy(u->name, name); 7837c478bd9Sstevel@tonic-gate u->home = xmalloc(strlen(pw->pw_dir)+1); 7847c478bd9Sstevel@tonic-gate (void) strcpy(u->home, pw->pw_dir); 7857c478bd9Sstevel@tonic-gate u->uid = pw->pw_uid; 7867c478bd9Sstevel@tonic-gate u->gid = pw->pw_gid; 7877c478bd9Sstevel@tonic-gate u->ctexists = TRUE; 7887c478bd9Sstevel@tonic-gate u->ctid = ecid++; 7897c478bd9Sstevel@tonic-gate u->ctevents = NULL; 7907c478bd9Sstevel@tonic-gate u->atevents = NULL; 7917c478bd9Sstevel@tonic-gate u->aruncnt = 0; 7927c478bd9Sstevel@tonic-gate u->cruncnt = 0; 7937c478bd9Sstevel@tonic-gate u->nextusr = uhead; 7947c478bd9Sstevel@tonic-gate uhead = u; 7957c478bd9Sstevel@tonic-gate readcron(u, reftime); 7967c478bd9Sstevel@tonic-gate } else { 7977c478bd9Sstevel@tonic-gate u->uid = pw->pw_uid; 7987c478bd9Sstevel@tonic-gate u->gid = pw->pw_gid; 7997c478bd9Sstevel@tonic-gate if (strcmp(u->home, pw->pw_dir) != 0) { 8007c478bd9Sstevel@tonic-gate free(u->home); 8017c478bd9Sstevel@tonic-gate u->home = xmalloc(strlen(pw->pw_dir)+1); 8027c478bd9Sstevel@tonic-gate (void) strcpy(u->home, pw->pw_dir); 8037c478bd9Sstevel@tonic-gate } 8047c478bd9Sstevel@tonic-gate u->ctexists = TRUE; 8057c478bd9Sstevel@tonic-gate if (u->ctid == 0) { 8067c478bd9Sstevel@tonic-gate #ifdef DEBUG 8077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s now has a crontab\n", 8087c478bd9Sstevel@tonic-gate u->name); 8097c478bd9Sstevel@tonic-gate #endif 8107c478bd9Sstevel@tonic-gate /* user didnt have a crontab last time */ 8117c478bd9Sstevel@tonic-gate u->ctid = ecid++; 8127c478bd9Sstevel@tonic-gate u->ctevents = NULL; 8137c478bd9Sstevel@tonic-gate readcron(u, reftime); 8147c478bd9Sstevel@tonic-gate return; 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate #ifdef DEBUG 8177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s has revised his crontab\n", u->name); 8187c478bd9Sstevel@tonic-gate #endif 8197c478bd9Sstevel@tonic-gate rm_ctevents(u); 8207c478bd9Sstevel@tonic-gate el_remove(u->ctid, 0); 8217c478bd9Sstevel@tonic-gate readcron(u, reftime); 8227c478bd9Sstevel@tonic-gate } 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8267c478bd9Sstevel@tonic-gate static void 8277c478bd9Sstevel@tonic-gate mod_atjob(char *name, time_t reftime) 8287c478bd9Sstevel@tonic-gate { 8297c478bd9Sstevel@tonic-gate char *ptr; 8307c478bd9Sstevel@tonic-gate time_t tim; 8317c478bd9Sstevel@tonic-gate struct passwd *pw; 8327c478bd9Sstevel@tonic-gate struct stat buf; 8337c478bd9Sstevel@tonic-gate struct usr *u; 8347c478bd9Sstevel@tonic-gate struct event *e; 8357c478bd9Sstevel@tonic-gate char namebuf[PATH_MAX]; 8367c478bd9Sstevel@tonic-gate char *pname; 8377c478bd9Sstevel@tonic-gate int jobtype; 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate ptr = name; 8407c478bd9Sstevel@tonic-gate if (((tim = num(&ptr)) == 0) || (*ptr != '.')) 8417c478bd9Sstevel@tonic-gate return; 8427c478bd9Sstevel@tonic-gate ptr++; 8437c478bd9Sstevel@tonic-gate if (!isalpha(*ptr)) 8447c478bd9Sstevel@tonic-gate return; 8457c478bd9Sstevel@tonic-gate jobtype = *ptr - 'a'; 8467c478bd9Sstevel@tonic-gate 8477c478bd9Sstevel@tonic-gate /* check for audit ancillary file */ 8487c478bd9Sstevel@tonic-gate if (audit_cron_is_anc_name(name)) 8497c478bd9Sstevel@tonic-gate return; 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if (cwd != AT) { 8527c478bd9Sstevel@tonic-gate if (snprintf(namebuf, sizeof (namebuf), "%s/%s", ATDIR, name) 8537c478bd9Sstevel@tonic-gate >= sizeof (namebuf)) { 8547c478bd9Sstevel@tonic-gate return; 8557c478bd9Sstevel@tonic-gate } 8567c478bd9Sstevel@tonic-gate pname = namebuf; 8577c478bd9Sstevel@tonic-gate } else { 8587c478bd9Sstevel@tonic-gate pname = name; 8597c478bd9Sstevel@tonic-gate } 8607c478bd9Sstevel@tonic-gate if (stat(pname, &buf) || jobtype >= NQUEUE) { 8617c478bd9Sstevel@tonic-gate cron_unlink(pname); 8627c478bd9Sstevel@tonic-gate return; 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate if (!(buf.st_mode & ISUID) || !S_ISREG(buf.st_mode)) { 8657c478bd9Sstevel@tonic-gate cron_unlink(pname); 8667c478bd9Sstevel@tonic-gate return; 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate if ((pw = getpwuid(buf.st_uid)) == NULL) { 8697c478bd9Sstevel@tonic-gate cron_unlink(pname); 8707c478bd9Sstevel@tonic-gate return; 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate /* 8737c478bd9Sstevel@tonic-gate * a warning message is given by the at command so there is no 8747c478bd9Sstevel@tonic-gate * need to give one here......use this code if you only want 8757c478bd9Sstevel@tonic-gate * users with a login shell of /usr/bin/sh to use cron 8767c478bd9Sstevel@tonic-gate */ 8777c478bd9Sstevel@tonic-gate #ifdef BOURNESHELLONLY 8787c478bd9Sstevel@tonic-gate if ((strcmp(pw->pw_shell, "") != 0) && 8797c478bd9Sstevel@tonic-gate (strcmp(pw->pw_shell, SHELL) != 0)) { 8807c478bd9Sstevel@tonic-gate mail(pw->pw_name, BADSHELL, ERR_CANTEXECAT); 8817c478bd9Sstevel@tonic-gate cron_unlink(pname); 8827c478bd9Sstevel@tonic-gate return; 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate #endif 8857c478bd9Sstevel@tonic-gate if ((u = find_usr(pw->pw_name)) == NULL) { 8867c478bd9Sstevel@tonic-gate #ifdef DEBUG 8877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "new user (%s) with an at job = %s\n", 8887c478bd9Sstevel@tonic-gate pw->pw_name, name); 8897c478bd9Sstevel@tonic-gate #endif 8907c478bd9Sstevel@tonic-gate u = xmalloc(sizeof (struct usr)); 8917c478bd9Sstevel@tonic-gate u->name = xmalloc(strlen(pw->pw_name)+1); 8927c478bd9Sstevel@tonic-gate (void) strcpy(u->name, pw->pw_name); 8937c478bd9Sstevel@tonic-gate u->home = xmalloc(strlen(pw->pw_dir)+1); 8947c478bd9Sstevel@tonic-gate (void) strcpy(u->home, pw->pw_dir); 8957c478bd9Sstevel@tonic-gate u->uid = pw->pw_uid; 8967c478bd9Sstevel@tonic-gate u->gid = pw->pw_gid; 8977c478bd9Sstevel@tonic-gate u->ctexists = FALSE; 8987c478bd9Sstevel@tonic-gate u->ctid = 0; 8997c478bd9Sstevel@tonic-gate u->ctevents = NULL; 9007c478bd9Sstevel@tonic-gate u->atevents = NULL; 9017c478bd9Sstevel@tonic-gate u->aruncnt = 0; 9027c478bd9Sstevel@tonic-gate u->cruncnt = 0; 9037c478bd9Sstevel@tonic-gate u->nextusr = uhead; 9047c478bd9Sstevel@tonic-gate uhead = u; 9057c478bd9Sstevel@tonic-gate add_atevent(u, name, tim, jobtype); 9067c478bd9Sstevel@tonic-gate } else { 9077c478bd9Sstevel@tonic-gate u->uid = pw->pw_uid; 9087c478bd9Sstevel@tonic-gate u->gid = pw->pw_gid; 9097c478bd9Sstevel@tonic-gate if (strcmp(u->home, pw->pw_dir) != 0) { 9107c478bd9Sstevel@tonic-gate free(u->home); 9117c478bd9Sstevel@tonic-gate u->home = xmalloc(strlen(pw->pw_dir)+1); 9127c478bd9Sstevel@tonic-gate (void) strcpy(u->home, pw->pw_dir); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate e = u->atevents; 9157c478bd9Sstevel@tonic-gate while (e != NULL) { 9167c478bd9Sstevel@tonic-gate if (strcmp(e->cmd, name) == 0) { 9177c478bd9Sstevel@tonic-gate e->of.at.exists = TRUE; 9187c478bd9Sstevel@tonic-gate break; 9197c478bd9Sstevel@tonic-gate } else { 9207c478bd9Sstevel@tonic-gate e = e->link; 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate if (e == NULL) { 9247c478bd9Sstevel@tonic-gate #ifdef DEBUG 9257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s has a new at job = %s\n", 9267c478bd9Sstevel@tonic-gate u->name, name); 9277c478bd9Sstevel@tonic-gate #endif 9287c478bd9Sstevel@tonic-gate add_atevent(u, name, tim, jobtype); 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate } 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate static void 9347c478bd9Sstevel@tonic-gate add_atevent(struct usr *u, char *job, time_t tim, int jobtype) 9357c478bd9Sstevel@tonic-gate { 9367c478bd9Sstevel@tonic-gate struct event *e; 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate e = xmalloc(sizeof (struct event)); 9397c478bd9Sstevel@tonic-gate e->etype = jobtype; 9407c478bd9Sstevel@tonic-gate e->cmd = xmalloc(strlen(job)+1); 9417c478bd9Sstevel@tonic-gate (void) strcpy(e->cmd, job); 9427c478bd9Sstevel@tonic-gate e->u = u; 9437c478bd9Sstevel@tonic-gate e->link = u->atevents; 9447c478bd9Sstevel@tonic-gate u->atevents = e; 9457c478bd9Sstevel@tonic-gate e->of.at.exists = TRUE; 9467c478bd9Sstevel@tonic-gate e->of.at.eventid = ecid++; 9477c478bd9Sstevel@tonic-gate if (tim < init_time) /* old job */ 9487c478bd9Sstevel@tonic-gate e->time = init_time; 9497c478bd9Sstevel@tonic-gate else 9507c478bd9Sstevel@tonic-gate e->time = tim; 9517c478bd9Sstevel@tonic-gate #ifdef DEBUG 9527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "add_atevent: user=%s, job=%s, time=%ld\n", 9537c478bd9Sstevel@tonic-gate u->name, e->cmd, e->time); 9547c478bd9Sstevel@tonic-gate #endif 9557c478bd9Sstevel@tonic-gate el_add(e, e->time, e->of.at.eventid); 9567c478bd9Sstevel@tonic-gate } 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate static char line[CTLINESIZE]; /* holds a line from a crontab file */ 9607c478bd9Sstevel@tonic-gate static int cursor; /* cursor for the above line */ 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate static void 9637c478bd9Sstevel@tonic-gate readcron(struct usr *u, time_t reftime) 9647c478bd9Sstevel@tonic-gate { 9657c478bd9Sstevel@tonic-gate /* 9667c478bd9Sstevel@tonic-gate * readcron reads in a crontab file for a user (u). The list of 9677c478bd9Sstevel@tonic-gate * events for user u is built, and u->events is made to point to 9687c478bd9Sstevel@tonic-gate * this list. Each event is also entered into the main event 9697c478bd9Sstevel@tonic-gate * list. 9707c478bd9Sstevel@tonic-gate */ 9717c478bd9Sstevel@tonic-gate FILE *cf; /* cf will be a user's crontab file */ 9727c478bd9Sstevel@tonic-gate struct event *e; 9737c478bd9Sstevel@tonic-gate int start; 9747c478bd9Sstevel@tonic-gate unsigned int i; 9757c478bd9Sstevel@tonic-gate char namebuf[PATH_MAX]; 9767c478bd9Sstevel@tonic-gate char *pname; 9777c478bd9Sstevel@tonic-gate int lineno = 0; 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate /* read the crontab file */ 9807c478bd9Sstevel@tonic-gate cte_init(); /* Init error handling */ 9817c478bd9Sstevel@tonic-gate if (cwd != CRON) { 9827c478bd9Sstevel@tonic-gate if (snprintf(namebuf, sizeof (namebuf), "%s/%s", 9837c478bd9Sstevel@tonic-gate CRONDIR, u->name) >= sizeof (namebuf)) { 9847c478bd9Sstevel@tonic-gate return; 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate pname = namebuf; 9877c478bd9Sstevel@tonic-gate } else { 9887c478bd9Sstevel@tonic-gate pname = u->name; 9897c478bd9Sstevel@tonic-gate } 9907c478bd9Sstevel@tonic-gate if ((cf = fopen(pname, "r")) == NULL) { 9917c478bd9Sstevel@tonic-gate mail(u->name, NOREAD, ERR_UNIXERR); 9927c478bd9Sstevel@tonic-gate return; 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate while (fgets(line, CTLINESIZE, cf) != NULL) { 9957c478bd9Sstevel@tonic-gate /* process a line of a crontab file */ 9967c478bd9Sstevel@tonic-gate lineno++; 9977c478bd9Sstevel@tonic-gate if (cte_istoomany()) 9987c478bd9Sstevel@tonic-gate break; 9997c478bd9Sstevel@tonic-gate cursor = 0; 10007c478bd9Sstevel@tonic-gate while (line[cursor] == ' ' || line[cursor] == '\t') 10017c478bd9Sstevel@tonic-gate cursor++; 10027c478bd9Sstevel@tonic-gate if (line[cursor] == '#' || line[cursor] == '\n') 10037c478bd9Sstevel@tonic-gate continue; 10047c478bd9Sstevel@tonic-gate e = xmalloc(sizeof (struct event)); 10057c478bd9Sstevel@tonic-gate e->etype = CRONEVENT; 10067c478bd9Sstevel@tonic-gate if (!(((e->of.ct.minute = next_field(0, 59)) != NULL) && 10077c478bd9Sstevel@tonic-gate ((e->of.ct.hour = next_field(0, 23)) != NULL) && 10087c478bd9Sstevel@tonic-gate ((e->of.ct.daymon = next_field(1, 31)) != NULL) && 10097c478bd9Sstevel@tonic-gate ((e->of.ct.month = next_field(1, 12)) != NULL) && 10107c478bd9Sstevel@tonic-gate ((e->of.ct.dayweek = next_field(0, 6)) != NULL))) { 10117c478bd9Sstevel@tonic-gate free(e); 10127c478bd9Sstevel@tonic-gate cte_add(lineno, line); 10137c478bd9Sstevel@tonic-gate continue; 10147c478bd9Sstevel@tonic-gate } 10157c478bd9Sstevel@tonic-gate while (line[cursor] == ' ' || line[cursor] == '\t') 10167c478bd9Sstevel@tonic-gate cursor++; 10177c478bd9Sstevel@tonic-gate if (line[cursor] == '\n' || line[cursor] == '\0') 10187c478bd9Sstevel@tonic-gate continue; 10197c478bd9Sstevel@tonic-gate /* get the command to execute */ 10207c478bd9Sstevel@tonic-gate start = cursor; 10217c478bd9Sstevel@tonic-gate again: 10227c478bd9Sstevel@tonic-gate while ((line[cursor] != '%') && 10237c478bd9Sstevel@tonic-gate (line[cursor] != '\n') && 10247c478bd9Sstevel@tonic-gate (line[cursor] != '\0') && 10257c478bd9Sstevel@tonic-gate (line[cursor] != '\\')) 10267c478bd9Sstevel@tonic-gate cursor++; 10277c478bd9Sstevel@tonic-gate if (line[cursor] == '\\') { 10287c478bd9Sstevel@tonic-gate cursor += 2; 10297c478bd9Sstevel@tonic-gate goto again; 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate e->cmd = xmalloc(cursor-start+1); 10327c478bd9Sstevel@tonic-gate (void) strncpy(e->cmd, line+start, cursor-start); 10337c478bd9Sstevel@tonic-gate e->cmd[cursor-start] = '\0'; 10347c478bd9Sstevel@tonic-gate /* see if there is any standard input */ 10357c478bd9Sstevel@tonic-gate if (line[cursor] == '%') { 10367c478bd9Sstevel@tonic-gate e->of.ct.input = xmalloc(strlen(line)-cursor+1); 10377c478bd9Sstevel@tonic-gate (void) strcpy(e->of.ct.input, line+cursor+1); 10387c478bd9Sstevel@tonic-gate for (i = 0; i < strlen(e->of.ct.input); i++) { 10397c478bd9Sstevel@tonic-gate if (e->of.ct.input[i] == '%') 10407c478bd9Sstevel@tonic-gate e->of.ct.input[i] = '\n'; 10417c478bd9Sstevel@tonic-gate } 10427c478bd9Sstevel@tonic-gate } else { 10437c478bd9Sstevel@tonic-gate e->of.ct.input = NULL; 10447c478bd9Sstevel@tonic-gate } 10457c478bd9Sstevel@tonic-gate /* have the event point to it's owner */ 10467c478bd9Sstevel@tonic-gate e->u = u; 10477c478bd9Sstevel@tonic-gate /* insert this event at the front of this user's event list */ 10487c478bd9Sstevel@tonic-gate e->link = u->ctevents; 10497c478bd9Sstevel@tonic-gate u->ctevents = e; 10507c478bd9Sstevel@tonic-gate /* set the time for the first occurance of this event */ 10517c478bd9Sstevel@tonic-gate e->time = next_time(e, reftime); 10527c478bd9Sstevel@tonic-gate /* finally, add this event to the main event list */ 10537c478bd9Sstevel@tonic-gate el_add(e, e->time, u->ctid); 10547c478bd9Sstevel@tonic-gate cte_valid(); 10557c478bd9Sstevel@tonic-gate #ifdef DEBUG 10567c478bd9Sstevel@tonic-gate cftime(timebuf, "%C", &e->time); 10577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "inserting cron event %s at %ld (%s)\n", 10587c478bd9Sstevel@tonic-gate e->cmd, e->time, timebuf); 10597c478bd9Sstevel@tonic-gate #endif 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate cte_sendmail(u->name); /* mail errors if any to user */ 10627c478bd9Sstevel@tonic-gate (void) fclose(cf); 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate /* 10667c478bd9Sstevel@tonic-gate * Below are the functions for handling of errors in crontabs. Concept is to 10677c478bd9Sstevel@tonic-gate * collect faulty lines and send one email at the end of the crontab 10687c478bd9Sstevel@tonic-gate * evaluation. If there are erroneous lines only ((cte_nvalid == 0), evaluation 10697c478bd9Sstevel@tonic-gate * of crontab is aborted. Otherwise reading of crontab is continued to the end 10707c478bd9Sstevel@tonic-gate * of the file but no further error logging appears. 10717c478bd9Sstevel@tonic-gate */ 10727c478bd9Sstevel@tonic-gate static void 10737c478bd9Sstevel@tonic-gate cte_init() 10747c478bd9Sstevel@tonic-gate { 10757c478bd9Sstevel@tonic-gate if (cte_text == NULL) 10767c478bd9Sstevel@tonic-gate cte_text = xmalloc(MAILBUFLEN); 10777c478bd9Sstevel@tonic-gate (void) strlcpy(cte_text, cte_intro, MAILBUFLEN); 10787c478bd9Sstevel@tonic-gate cte_lp = cte_text + sizeof (cte_intro) - 1; 10797c478bd9Sstevel@tonic-gate cte_free = MAILBINITFREE; 10807c478bd9Sstevel@tonic-gate cte_nvalid = 0; 10817c478bd9Sstevel@tonic-gate } 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate static void 10847c478bd9Sstevel@tonic-gate cte_add(int lineno, char *ctline) 10857c478bd9Sstevel@tonic-gate { 10867c478bd9Sstevel@tonic-gate int len; 10877c478bd9Sstevel@tonic-gate char *p; 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate if (cte_free >= LINELIMIT) { 10907c478bd9Sstevel@tonic-gate (void) sprintf(cte_lp, "%4d: ", lineno); 10917c478bd9Sstevel@tonic-gate (void) strlcat(cte_lp, ctline, LINELIMIT - 1); 10927c478bd9Sstevel@tonic-gate len = strlen(cte_lp); 10937c478bd9Sstevel@tonic-gate if (cte_lp[len - 1] != '\n') { 10947c478bd9Sstevel@tonic-gate cte_lp[len++] = '\n'; 10957c478bd9Sstevel@tonic-gate cte_lp[len] = '\0'; 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate for (p = cte_lp; *p; p++) { 10987c478bd9Sstevel@tonic-gate if (isprint(*p) || *p == '\n' || *p == '\t') 10997c478bd9Sstevel@tonic-gate continue; 11007c478bd9Sstevel@tonic-gate *p = '.'; 11017c478bd9Sstevel@tonic-gate } 11027c478bd9Sstevel@tonic-gate cte_lp += len; 11037c478bd9Sstevel@tonic-gate cte_free -= len; 11047c478bd9Sstevel@tonic-gate if (cte_free < LINELIMIT) { 11057c478bd9Sstevel@tonic-gate size_t buflen = MAILBUFLEN - (cte_lp - cte_text); 11067c478bd9Sstevel@tonic-gate (void) strlcpy(cte_lp, cte_trail1, buflen); 11077c478bd9Sstevel@tonic-gate if (cte_nvalid == 0) 11087c478bd9Sstevel@tonic-gate (void) strlcat(cte_lp, cte_trail2, buflen); 11097c478bd9Sstevel@tonic-gate } 11107c478bd9Sstevel@tonic-gate } 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate 11137c478bd9Sstevel@tonic-gate static void 11147c478bd9Sstevel@tonic-gate cte_valid() 11157c478bd9Sstevel@tonic-gate { 11167c478bd9Sstevel@tonic-gate cte_nvalid++; 11177c478bd9Sstevel@tonic-gate } 11187c478bd9Sstevel@tonic-gate 11197c478bd9Sstevel@tonic-gate static int 11207c478bd9Sstevel@tonic-gate cte_istoomany() 11217c478bd9Sstevel@tonic-gate { 11227c478bd9Sstevel@tonic-gate /* 11237c478bd9Sstevel@tonic-gate * Return TRUE only if all lines are faulty. So evaluation of 11247c478bd9Sstevel@tonic-gate * a crontab is not aborted if at least one valid line was found. 11257c478bd9Sstevel@tonic-gate */ 11267c478bd9Sstevel@tonic-gate return (cte_nvalid == 0 && cte_free < LINELIMIT); 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate static void 11307c478bd9Sstevel@tonic-gate cte_sendmail(char *username) 11317c478bd9Sstevel@tonic-gate { 11327c478bd9Sstevel@tonic-gate if (cte_free < MAILBINITFREE) 11337c478bd9Sstevel@tonic-gate mail(username, cte_text, ERR_CRONTABENT); 11347c478bd9Sstevel@tonic-gate } 11357c478bd9Sstevel@tonic-gate 11367c478bd9Sstevel@tonic-gate /* 11377c478bd9Sstevel@tonic-gate * Send mail with error message to a user 11387c478bd9Sstevel@tonic-gate */ 11397c478bd9Sstevel@tonic-gate static void 11407c478bd9Sstevel@tonic-gate mail(char *usrname, char *mesg, int format) 11417c478bd9Sstevel@tonic-gate { 11427c478bd9Sstevel@tonic-gate /* mail mails a user a message. */ 11437c478bd9Sstevel@tonic-gate FILE *pipe; 11447c478bd9Sstevel@tonic-gate char *temp; 11457c478bd9Sstevel@tonic-gate struct passwd *ruser_ids; 11467c478bd9Sstevel@tonic-gate pid_t fork_val; 11477c478bd9Sstevel@tonic-gate int saveerrno = errno; 11487c478bd9Sstevel@tonic-gate struct utsname name; 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate #ifdef TESTING 11517c478bd9Sstevel@tonic-gate return; 11527c478bd9Sstevel@tonic-gate #endif 11537c478bd9Sstevel@tonic-gate (void) uname(&name); 11547c478bd9Sstevel@tonic-gate if ((fork_val = fork()) == (pid_t)-1) { 11557c478bd9Sstevel@tonic-gate msg("cron cannot fork\n"); 11567c478bd9Sstevel@tonic-gate return; 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate if (fork_val == 0) { 11597c478bd9Sstevel@tonic-gate child_sigreset(); 11607c478bd9Sstevel@tonic-gate contract_clear_template(); 11617c478bd9Sstevel@tonic-gate if ((ruser_ids = getpwnam(usrname)) == NULL) 11627c478bd9Sstevel@tonic-gate exit(0); 11637c478bd9Sstevel@tonic-gate (void) setuid(ruser_ids->pw_uid); 11647c478bd9Sstevel@tonic-gate temp = xmalloc(strlen(MAIL)+strlen(usrname)+2); 11657c478bd9Sstevel@tonic-gate (void) sprintf(temp, "%s %s", MAIL, usrname); 11667c478bd9Sstevel@tonic-gate pipe = popen(temp, "w"); 11677c478bd9Sstevel@tonic-gate if (pipe != NULL) { 11687c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "To: %s\n", usrname); 11697c478bd9Sstevel@tonic-gate switch (format) { 11707c478bd9Sstevel@tonic-gate case ERR_CRONTABENT: 11717c478bd9Sstevel@tonic-gate (void) fprintf(pipe, CRONTABERR); 11727c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "Your \"crontab\" on %s\n", 11737c478bd9Sstevel@tonic-gate name.nodename); 11747c478bd9Sstevel@tonic-gate (void) fprintf(pipe, mesg); 11757c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 11767c478bd9Sstevel@tonic-gate "\nEntries or crontab have been ignored\n"); 11777c478bd9Sstevel@tonic-gate break; 11787c478bd9Sstevel@tonic-gate case ERR_UNIXERR: 11797c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "Subject: %s\n\n", mesg); 11807c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 11817c478bd9Sstevel@tonic-gate "The error on %s was \"%s\"\n", 11827c478bd9Sstevel@tonic-gate name.nodename, errmsg(saveerrno)); 11837c478bd9Sstevel@tonic-gate break; 11847c478bd9Sstevel@tonic-gate 11857c478bd9Sstevel@tonic-gate case ERR_CANTEXECCRON: 11867c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 11877c478bd9Sstevel@tonic-gate "Subject: Couldn't run your \"cron\" job\n\n"); 11887c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 11897c478bd9Sstevel@tonic-gate "Your \"cron\" job on %s ", name.nodename); 11907c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "couldn't be run\n"); 11917c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "%s\n", mesg); 11927c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 11937c478bd9Sstevel@tonic-gate "The error was \"%s\"\n", errmsg(saveerrno)); 11947c478bd9Sstevel@tonic-gate break; 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate case ERR_CANTEXECAT: 11977c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 11987c478bd9Sstevel@tonic-gate "Subject: Couldn't run your \"at\" job\n\n"); 11997c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "Your \"at\" job on %s ", 12007c478bd9Sstevel@tonic-gate name.nodename); 12017c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "couldn't be run\n"); 12027c478bd9Sstevel@tonic-gate (void) fprintf(pipe, "%s\n", mesg); 12037c478bd9Sstevel@tonic-gate (void) fprintf(pipe, 12047c478bd9Sstevel@tonic-gate "The error was \"%s\"\n", errmsg(saveerrno)); 12057c478bd9Sstevel@tonic-gate break; 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate default: 12087c478bd9Sstevel@tonic-gate break; 12097c478bd9Sstevel@tonic-gate } 12107c478bd9Sstevel@tonic-gate (void) pclose(pipe); 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate free(temp); 12137c478bd9Sstevel@tonic-gate exit(0); 12147c478bd9Sstevel@tonic-gate } 12157c478bd9Sstevel@tonic-gate 12167c478bd9Sstevel@tonic-gate contract_abandon_latest(fork_val); 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate if (cron_pid == getpid()) { 12197c478bd9Sstevel@tonic-gate miscpid_insert(fork_val); 12207c478bd9Sstevel@tonic-gate } 12217c478bd9Sstevel@tonic-gate } 12227c478bd9Sstevel@tonic-gate 12237c478bd9Sstevel@tonic-gate static char * 12247c478bd9Sstevel@tonic-gate next_field(int lower, int upper) 12257c478bd9Sstevel@tonic-gate { 12267c478bd9Sstevel@tonic-gate /* 12277c478bd9Sstevel@tonic-gate * next_field returns a pointer to a string which holds the next 12287c478bd9Sstevel@tonic-gate * field of a line of a crontab file. 12297c478bd9Sstevel@tonic-gate * if (numbers in this field are out of range (lower..upper), 12307c478bd9Sstevel@tonic-gate * or there is a syntax error) then 12317c478bd9Sstevel@tonic-gate * NULL is returned, and a mail message is sent to the 12327c478bd9Sstevel@tonic-gate * user telling him which line the error was in. 12337c478bd9Sstevel@tonic-gate */ 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate char *s; 12367c478bd9Sstevel@tonic-gate int num, num2, start; 12377c478bd9Sstevel@tonic-gate 12387c478bd9Sstevel@tonic-gate while ((line[cursor] == ' ') || (line[cursor] == '\t')) 12397c478bd9Sstevel@tonic-gate cursor++; 12407c478bd9Sstevel@tonic-gate start = cursor; 12417c478bd9Sstevel@tonic-gate if (line[cursor] == '\0') { 12427c478bd9Sstevel@tonic-gate return (NULL); 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate if (line[cursor] == '*') { 12457c478bd9Sstevel@tonic-gate cursor++; 12467c478bd9Sstevel@tonic-gate if ((line[cursor] != ' ') && (line[cursor] != '\t')) 12477c478bd9Sstevel@tonic-gate return (NULL); 12487c478bd9Sstevel@tonic-gate s = xmalloc(2); 12497c478bd9Sstevel@tonic-gate (void) strcpy(s, "*"); 12507c478bd9Sstevel@tonic-gate return (s); 12517c478bd9Sstevel@tonic-gate } 12527c478bd9Sstevel@tonic-gate for (;;) { 12537c478bd9Sstevel@tonic-gate if (!isdigit(line[cursor])) 12547c478bd9Sstevel@tonic-gate return (NULL); 12557c478bd9Sstevel@tonic-gate num = 0; 12567c478bd9Sstevel@tonic-gate do { 12577c478bd9Sstevel@tonic-gate num = num*10 + (line[cursor]-'0'); 12587c478bd9Sstevel@tonic-gate } while (isdigit(line[++cursor])); 12597c478bd9Sstevel@tonic-gate if ((num < lower) || (num > upper)) 12607c478bd9Sstevel@tonic-gate return (NULL); 12617c478bd9Sstevel@tonic-gate if (line[cursor] == '-') { 12627c478bd9Sstevel@tonic-gate if (!isdigit(line[++cursor])) 12637c478bd9Sstevel@tonic-gate return (NULL); 12647c478bd9Sstevel@tonic-gate num2 = 0; 12657c478bd9Sstevel@tonic-gate do { 12667c478bd9Sstevel@tonic-gate num2 = num2*10 + (line[cursor]-'0'); 12677c478bd9Sstevel@tonic-gate } while (isdigit(line[++cursor])); 12687c478bd9Sstevel@tonic-gate if ((num2 < lower) || (num2 > upper)) 12697c478bd9Sstevel@tonic-gate return (NULL); 12707c478bd9Sstevel@tonic-gate } 12717c478bd9Sstevel@tonic-gate if ((line[cursor] == ' ') || (line[cursor] == '\t')) 12727c478bd9Sstevel@tonic-gate break; 12737c478bd9Sstevel@tonic-gate if (line[cursor] == '\0') 12747c478bd9Sstevel@tonic-gate return (NULL); 12757c478bd9Sstevel@tonic-gate if (line[cursor++] != ',') 12767c478bd9Sstevel@tonic-gate return (NULL); 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate s = xmalloc(cursor-start+1); 12797c478bd9Sstevel@tonic-gate (void) strncpy(s, line+start, cursor-start); 12807c478bd9Sstevel@tonic-gate s[cursor-start] = '\0'; 12817c478bd9Sstevel@tonic-gate return (s); 12827c478bd9Sstevel@tonic-gate } 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate #define tm_cmp(t1, t2) (\ 12857c478bd9Sstevel@tonic-gate (t1)->tm_year == (t2)->tm_year && \ 12867c478bd9Sstevel@tonic-gate (t1)->tm_mon == (t2)->tm_mon && \ 12877c478bd9Sstevel@tonic-gate (t1)->tm_mday == (t2)->tm_mday && \ 12887c478bd9Sstevel@tonic-gate (t1)->tm_hour == (t2)->tm_hour && \ 12897c478bd9Sstevel@tonic-gate (t1)->tm_min == (t2)->tm_min) 12907c478bd9Sstevel@tonic-gate 12917c478bd9Sstevel@tonic-gate #define tm_setup(tp, yr, mon, dy, hr, min, dst) \ 12927c478bd9Sstevel@tonic-gate (tp)->tm_year = yr; \ 12937c478bd9Sstevel@tonic-gate (tp)->tm_mon = mon; \ 12947c478bd9Sstevel@tonic-gate (tp)->tm_mday = dy; \ 12957c478bd9Sstevel@tonic-gate (tp)->tm_hour = hr; \ 12967c478bd9Sstevel@tonic-gate (tp)->tm_min = min; \ 12977c478bd9Sstevel@tonic-gate (tp)->tm_isdst = dst; \ 12987c478bd9Sstevel@tonic-gate (tp)->tm_sec = 0; \ 12997c478bd9Sstevel@tonic-gate (tp)->tm_wday = 0; \ 13007c478bd9Sstevel@tonic-gate (tp)->tm_yday = 0; 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate /* 13037c478bd9Sstevel@tonic-gate * modification for bugid 1104537. the second argument to next_time is 13047c478bd9Sstevel@tonic-gate * now the value of time(2) to be used. if this is 0, then use the 13057c478bd9Sstevel@tonic-gate * current time. otherwise, the second argument is the time from which to 13067c478bd9Sstevel@tonic-gate * calculate things. this is useful to correct situations where you've 13077c478bd9Sstevel@tonic-gate * gone backwards in time (I.e. the system's internal clock is correcting 13087c478bd9Sstevel@tonic-gate * itself backwards). 13097c478bd9Sstevel@tonic-gate */ 13107c478bd9Sstevel@tonic-gate 13117c478bd9Sstevel@tonic-gate static time_t 13127c478bd9Sstevel@tonic-gate next_time(struct event *e, time_t tflag) 13137c478bd9Sstevel@tonic-gate { 13147c478bd9Sstevel@tonic-gate /* 13157c478bd9Sstevel@tonic-gate * returns the integer time for the next occurance of event e. 13167c478bd9Sstevel@tonic-gate * the following fields have ranges as indicated: 13177c478bd9Sstevel@tonic-gate * PRGM | min hour day of month mon day of week 13187c478bd9Sstevel@tonic-gate * ------|------------------------------------------------------- 13197c478bd9Sstevel@tonic-gate * cron | 0-59 0-23 1-31 1-12 0-6 (0=sunday) 13207c478bd9Sstevel@tonic-gate * time | 0-59 0-23 1-31 0-11 0-6 (0=sunday) 13217c478bd9Sstevel@tonic-gate * NOTE: this routine is hard to understand. 13227c478bd9Sstevel@tonic-gate */ 13237c478bd9Sstevel@tonic-gate 13247c478bd9Sstevel@tonic-gate struct tm *tm, ref_tm, tmp, tmp1, tmp2; 13257c478bd9Sstevel@tonic-gate int tm_mon, tm_mday, tm_wday, wday, m, min, h, hr, carry, day, days, 13267c478bd9Sstevel@tonic-gate d1, day1, carry1, d2, day2, carry2, daysahead, mon, yr, db, wd, today; 13277c478bd9Sstevel@tonic-gate time_t t, ref_t, t1, t2, zone_start; 13287c478bd9Sstevel@tonic-gate int fallback; 13297c478bd9Sstevel@tonic-gate extern int days_btwn(int, int, int, int, int, int); 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate if (tflag == 0) { 13327c478bd9Sstevel@tonic-gate t = time(NULL); /* original way of doing things */ 13337c478bd9Sstevel@tonic-gate } else { 13347c478bd9Sstevel@tonic-gate t = tflag; 13357c478bd9Sstevel@tonic-gate } 13367c478bd9Sstevel@tonic-gate 13377c478bd9Sstevel@tonic-gate tm = &ref_tm; /* use a local variable and call localtime_r() */ 13387c478bd9Sstevel@tonic-gate ref_t = t; /* keep a copy of the reference time */ 13397c478bd9Sstevel@tonic-gate 13407c478bd9Sstevel@tonic-gate recalc: 13417c478bd9Sstevel@tonic-gate fallback = 0; 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate (void) localtime_r(&t, tm); 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate if (daylight) { 13467c478bd9Sstevel@tonic-gate tmp = *tm; 13477c478bd9Sstevel@tonic-gate tmp.tm_isdst = (tm->tm_isdst > 0 ? 0 : 1); 13487c478bd9Sstevel@tonic-gate t1 = xmktime(&tmp); 13497c478bd9Sstevel@tonic-gate /* 13507c478bd9Sstevel@tonic-gate * see if we will have timezone switch over, and clock will 13517c478bd9Sstevel@tonic-gate * fall back. zone_start will hold the time when it happens 13527c478bd9Sstevel@tonic-gate * (ie time of PST -> PDT switch over). 13537c478bd9Sstevel@tonic-gate */ 13547c478bd9Sstevel@tonic-gate if (tm->tm_isdst != tmp.tm_isdst && 13557c478bd9Sstevel@tonic-gate (t1 - t) == (timezone - altzone) && 13567c478bd9Sstevel@tonic-gate tm_cmp(tm, &tmp)) { 13577c478bd9Sstevel@tonic-gate zone_start = get_switching_time(tmp.tm_isdst, t); 13587c478bd9Sstevel@tonic-gate fallback = 1; 13597c478bd9Sstevel@tonic-gate } 13607c478bd9Sstevel@tonic-gate } 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate tm_mon = next_ge(tm->tm_mon+1, e->of.ct.month) - 1; /* 0-11 */ 13637c478bd9Sstevel@tonic-gate tm_mday = next_ge(tm->tm_mday, e->of.ct.daymon); /* 1-31 */ 13647c478bd9Sstevel@tonic-gate tm_wday = next_ge(tm->tm_wday, e->of.ct.dayweek); /* 0-6 */ 13657c478bd9Sstevel@tonic-gate today = TRUE; 13667c478bd9Sstevel@tonic-gate if ((strcmp(e->of.ct.daymon, "*") == 0 && tm->tm_wday != tm_wday) || 13677c478bd9Sstevel@tonic-gate (strcmp(e->of.ct.dayweek, "*") == 0 && tm->tm_mday != tm_mday) || 13687c478bd9Sstevel@tonic-gate (tm->tm_mday != tm_mday && tm->tm_wday != tm_wday) || 13697c478bd9Sstevel@tonic-gate (tm->tm_mon != tm_mon)) { 13707c478bd9Sstevel@tonic-gate today = FALSE; 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate m = tm->tm_min + (t == ref_t ? 1 : 0); 13737c478bd9Sstevel@tonic-gate if ((tm->tm_hour + 1) <= next_ge(tm->tm_hour, e->of.ct.hour)) { 13747c478bd9Sstevel@tonic-gate m = 0; 13757c478bd9Sstevel@tonic-gate } 13767c478bd9Sstevel@tonic-gate min = next_ge(m%60, e->of.ct.minute); 13777c478bd9Sstevel@tonic-gate carry = (min < m) ? 1 : 0; 13787c478bd9Sstevel@tonic-gate h = tm->tm_hour + carry; 13797c478bd9Sstevel@tonic-gate hr = next_ge(h%24, e->of.ct.hour); 13807c478bd9Sstevel@tonic-gate carry = (hr < h) ? 1 : 0; 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate if (carry == 0 && today) { 13837c478bd9Sstevel@tonic-gate /* this event must occur today */ 13847c478bd9Sstevel@tonic-gate tm_setup(&tmp, tm->tm_year, tm->tm_mon, tm->tm_mday, 13857c478bd9Sstevel@tonic-gate hr, min, tm->tm_isdst); 13867c478bd9Sstevel@tonic-gate tmp1 = tmp; 13877c478bd9Sstevel@tonic-gate if ((t1 = xmktime(&tmp1)) == (time_t)-1) { 13887c478bd9Sstevel@tonic-gate return (0); 13897c478bd9Sstevel@tonic-gate } 13907c478bd9Sstevel@tonic-gate if (daylight && tmp.tm_isdst != tmp1.tm_isdst) { 13917c478bd9Sstevel@tonic-gate /* In case we are falling back */ 13927c478bd9Sstevel@tonic-gate if (fallback) { 13937c478bd9Sstevel@tonic-gate /* we may need to run the job once more. */ 13947c478bd9Sstevel@tonic-gate t = zone_start; 13957c478bd9Sstevel@tonic-gate goto recalc; 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate 13987c478bd9Sstevel@tonic-gate /* 13997c478bd9Sstevel@tonic-gate * In case we are not in falling back period, 14007c478bd9Sstevel@tonic-gate * calculate the time assuming the DST. If the 14017c478bd9Sstevel@tonic-gate * date/time is not altered by mktime, it is the 14027c478bd9Sstevel@tonic-gate * time to execute the job. 14037c478bd9Sstevel@tonic-gate */ 14047c478bd9Sstevel@tonic-gate tmp2 = tmp; 14057c478bd9Sstevel@tonic-gate tmp2.tm_isdst = tmp1.tm_isdst; 14067c478bd9Sstevel@tonic-gate if ((t1 = xmktime(&tmp2)) == (time_t)-1) { 14077c478bd9Sstevel@tonic-gate return (0); 14087c478bd9Sstevel@tonic-gate } 14097c478bd9Sstevel@tonic-gate if (tmp1.tm_isdst == tmp2.tm_isdst && 14107c478bd9Sstevel@tonic-gate tm_cmp(&tmp, &tmp2)) { 14117c478bd9Sstevel@tonic-gate /* 14127c478bd9Sstevel@tonic-gate * We got a valid time. 14137c478bd9Sstevel@tonic-gate */ 14147c478bd9Sstevel@tonic-gate return (t1); 14157c478bd9Sstevel@tonic-gate } else { 14167c478bd9Sstevel@tonic-gate /* 14177c478bd9Sstevel@tonic-gate * If the date does not match even if 14187c478bd9Sstevel@tonic-gate * we assume the alternate timezone, then 14197c478bd9Sstevel@tonic-gate * it must be the invalid time. eg 14207c478bd9Sstevel@tonic-gate * 2am while switching 1:59am to 3am. 14217c478bd9Sstevel@tonic-gate * t1 should point the time before the 14227c478bd9Sstevel@tonic-gate * switching over as we've calculate the 14237c478bd9Sstevel@tonic-gate * time with assuming alternate zone. 14247c478bd9Sstevel@tonic-gate */ 14257c478bd9Sstevel@tonic-gate if (tmp1.tm_isdst != tmp2.tm_isdst) { 14267c478bd9Sstevel@tonic-gate t = get_switching_time(tmp1.tm_isdst, 14277c478bd9Sstevel@tonic-gate t1); 14287c478bd9Sstevel@tonic-gate } else { 14297c478bd9Sstevel@tonic-gate /* does this really happen? */ 14307c478bd9Sstevel@tonic-gate t = get_switching_time(tmp1.tm_isdst, 14317c478bd9Sstevel@tonic-gate t1 - abs(timezone - altzone)); 14327c478bd9Sstevel@tonic-gate } 14337c478bd9Sstevel@tonic-gate if (t == (time_t)-1) 14347c478bd9Sstevel@tonic-gate return (0); 14357c478bd9Sstevel@tonic-gate } 14367c478bd9Sstevel@tonic-gate goto recalc; 14377c478bd9Sstevel@tonic-gate } 14387c478bd9Sstevel@tonic-gate if (tm_cmp(&tmp, &tmp1)) { 14397c478bd9Sstevel@tonic-gate /* got valid time */ 14407c478bd9Sstevel@tonic-gate return (t1); 14417c478bd9Sstevel@tonic-gate } else { 14427c478bd9Sstevel@tonic-gate /* 14437c478bd9Sstevel@tonic-gate * This should never happen, but just in 14447c478bd9Sstevel@tonic-gate * case, we fall back to the old code. 14457c478bd9Sstevel@tonic-gate */ 14467c478bd9Sstevel@tonic-gate if (tm->tm_min > min) { 14477c478bd9Sstevel@tonic-gate t += (time_t)(hr-tm->tm_hour-1) * HOUR + 14487c478bd9Sstevel@tonic-gate (time_t)(60-tm->tm_min+min) * MINUTE; 14497c478bd9Sstevel@tonic-gate } else { 14507c478bd9Sstevel@tonic-gate t += (time_t)(hr-tm->tm_hour) * HOUR + 14517c478bd9Sstevel@tonic-gate (time_t)(min-tm->tm_min) * MINUTE; 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate t1 = t; 14547c478bd9Sstevel@tonic-gate t -= (time_t)tm->tm_sec; 14557c478bd9Sstevel@tonic-gate (void) localtime_r(&t, &tmp); 14567c478bd9Sstevel@tonic-gate if ((tm->tm_isdst == 0) && (tmp.tm_isdst > 0)) 14577c478bd9Sstevel@tonic-gate t -= (timezone - altzone); 14587c478bd9Sstevel@tonic-gate return ((t <= ref_t) ? t1 : t); 14597c478bd9Sstevel@tonic-gate } 14607c478bd9Sstevel@tonic-gate } 14617c478bd9Sstevel@tonic-gate 14627c478bd9Sstevel@tonic-gate /* 14637c478bd9Sstevel@tonic-gate * Job won't run today, however if we have a switch over within 14647c478bd9Sstevel@tonic-gate * one hour and we will have one hour time drifting back in this 14657c478bd9Sstevel@tonic-gate * period, we may need to run the job one more time if the job was 14667c478bd9Sstevel@tonic-gate * set to run on this hour of clock. 14677c478bd9Sstevel@tonic-gate */ 14687c478bd9Sstevel@tonic-gate if (fallback) { 14697c478bd9Sstevel@tonic-gate t = zone_start; 14707c478bd9Sstevel@tonic-gate goto recalc; 14717c478bd9Sstevel@tonic-gate } 14727c478bd9Sstevel@tonic-gate 14737c478bd9Sstevel@tonic-gate min = next_ge(0, e->of.ct.minute); 14747c478bd9Sstevel@tonic-gate hr = next_ge(0, e->of.ct.hour); 14757c478bd9Sstevel@tonic-gate 14767c478bd9Sstevel@tonic-gate /* 14777c478bd9Sstevel@tonic-gate * calculate the date of the next occurance of this event, which 14787c478bd9Sstevel@tonic-gate * will be on a different day than the current 14797c478bd9Sstevel@tonic-gate */ 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate /* check monthly day specification */ 14827c478bd9Sstevel@tonic-gate d1 = tm->tm_mday+1; 14837c478bd9Sstevel@tonic-gate day1 = next_ge((d1-1)%days_in_mon(tm->tm_mon, tm->tm_year)+1, 14847c478bd9Sstevel@tonic-gate e->of.ct.daymon); 14857c478bd9Sstevel@tonic-gate carry1 = (day1 < d1) ? 1 : 0; 14867c478bd9Sstevel@tonic-gate 14877c478bd9Sstevel@tonic-gate /* check weekly day specification */ 14887c478bd9Sstevel@tonic-gate d2 = tm->tm_wday+1; 14897c478bd9Sstevel@tonic-gate wday = next_ge(d2%7, e->of.ct.dayweek); 14907c478bd9Sstevel@tonic-gate if (wday < d2) 14917c478bd9Sstevel@tonic-gate daysahead = 7 - d2 + wday; 14927c478bd9Sstevel@tonic-gate else 14937c478bd9Sstevel@tonic-gate daysahead = wday - d2; 14947c478bd9Sstevel@tonic-gate day2 = (d1+daysahead-1)%days_in_mon(tm->tm_mon, tm->tm_year)+1; 14957c478bd9Sstevel@tonic-gate carry2 = (day2 < d1) ? 1 : 0; 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate /* 14987c478bd9Sstevel@tonic-gate * based on their respective specifications, day1, and day2 give 14997c478bd9Sstevel@tonic-gate * the day of the month for the next occurance of this event. 15007c478bd9Sstevel@tonic-gate */ 15017c478bd9Sstevel@tonic-gate if ((strcmp(e->of.ct.daymon, "*") == 0) && 15027c478bd9Sstevel@tonic-gate (strcmp(e->of.ct.dayweek, "*") != 0)) { 15037c478bd9Sstevel@tonic-gate day1 = day2; 15047c478bd9Sstevel@tonic-gate carry1 = carry2; 15057c478bd9Sstevel@tonic-gate } 15067c478bd9Sstevel@tonic-gate if ((strcmp(e->of.ct.daymon, "*") != 0) && 15077c478bd9Sstevel@tonic-gate (strcmp(e->of.ct.dayweek, "*") == 0)) { 15087c478bd9Sstevel@tonic-gate day2 = day1; 15097c478bd9Sstevel@tonic-gate carry2 = carry1; 15107c478bd9Sstevel@tonic-gate } 15117c478bd9Sstevel@tonic-gate 15127c478bd9Sstevel@tonic-gate yr = tm->tm_year; 15137c478bd9Sstevel@tonic-gate if ((carry1 && carry2) || (tm->tm_mon != tm_mon)) { 15147c478bd9Sstevel@tonic-gate /* event does not occur in this month */ 15157c478bd9Sstevel@tonic-gate m = tm->tm_mon+1; 15167c478bd9Sstevel@tonic-gate mon = next_ge(m%12+1, e->of.ct.month) - 1; /* 0..11 */ 15177c478bd9Sstevel@tonic-gate carry = (mon < m) ? 1 : 0; 15187c478bd9Sstevel@tonic-gate yr += carry; 15197c478bd9Sstevel@tonic-gate /* recompute day1 and day2 */ 15207c478bd9Sstevel@tonic-gate day1 = next_ge(1, e->of.ct.daymon); 15217c478bd9Sstevel@tonic-gate db = days_btwn(tm->tm_mon, tm->tm_mday, tm->tm_year, mon, 15227c478bd9Sstevel@tonic-gate 1, yr) + 1; 15237c478bd9Sstevel@tonic-gate wd = (tm->tm_wday+db)%7; 15247c478bd9Sstevel@tonic-gate /* wd is the day of the week of the first of month mon */ 15257c478bd9Sstevel@tonic-gate wday = next_ge(wd, e->of.ct.dayweek); 15267c478bd9Sstevel@tonic-gate if (wday < wd) 15277c478bd9Sstevel@tonic-gate day2 = 1 + 7 - wd + wday; 15287c478bd9Sstevel@tonic-gate else 15297c478bd9Sstevel@tonic-gate day2 = 1 + wday - wd; 15307c478bd9Sstevel@tonic-gate if ((strcmp(e->of.ct.daymon, "*") != 0) && 15317c478bd9Sstevel@tonic-gate (strcmp(e->of.ct.dayweek, "*") == 0)) 15327c478bd9Sstevel@tonic-gate day2 = day1; 15337c478bd9Sstevel@tonic-gate if ((strcmp(e->of.ct.daymon, "*") == 0) && 15347c478bd9Sstevel@tonic-gate (strcmp(e->of.ct.dayweek, "*") != 0)) 15357c478bd9Sstevel@tonic-gate day1 = day2; 15367c478bd9Sstevel@tonic-gate day = (day1 < day2) ? day1 : day2; 15377c478bd9Sstevel@tonic-gate } else { /* event occurs in this month */ 15387c478bd9Sstevel@tonic-gate mon = tm->tm_mon; 15397c478bd9Sstevel@tonic-gate if (!carry1 && !carry2) 15407c478bd9Sstevel@tonic-gate day = (day1 < day2) ? day1 : day2; 15417c478bd9Sstevel@tonic-gate else if (!carry1) 15427c478bd9Sstevel@tonic-gate day = day1; 15437c478bd9Sstevel@tonic-gate else 15447c478bd9Sstevel@tonic-gate day = day2; 15457c478bd9Sstevel@tonic-gate } 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate /* 15487c478bd9Sstevel@tonic-gate * now that we have the min, hr, day, mon, yr of the next event, 15497c478bd9Sstevel@tonic-gate * figure out what time that turns out to be. 15507c478bd9Sstevel@tonic-gate */ 15517c478bd9Sstevel@tonic-gate tm_setup(&tmp, yr, mon, day, hr, min, -1); 15527c478bd9Sstevel@tonic-gate tmp2 = tmp; 15537c478bd9Sstevel@tonic-gate if ((t1 = xmktime(&tmp2)) == (time_t)-1) { 15547c478bd9Sstevel@tonic-gate return (0); 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate if (tm_cmp(&tmp, &tmp2)) { 15577c478bd9Sstevel@tonic-gate /* 15587c478bd9Sstevel@tonic-gate * mktime returns clock for the current time zone. If the 15597c478bd9Sstevel@tonic-gate * target date was in fallback period, it needs to be adjusted 15607c478bd9Sstevel@tonic-gate * to the time comes first. 15617c478bd9Sstevel@tonic-gate * Suppose, we are at Jan and scheduling job at 1:30am10/26/03. 15627c478bd9Sstevel@tonic-gate * mktime returns the time in PST, but 1:30am in PDT comes 15637c478bd9Sstevel@tonic-gate * first. So reverse the tm_isdst, and see if we have such 15647c478bd9Sstevel@tonic-gate * time/date. 15657c478bd9Sstevel@tonic-gate */ 15667c478bd9Sstevel@tonic-gate if (daylight) { 15677c478bd9Sstevel@tonic-gate int dst = tmp2.tm_isdst; 15687c478bd9Sstevel@tonic-gate 15697c478bd9Sstevel@tonic-gate tmp2 = tmp; 15707c478bd9Sstevel@tonic-gate tmp2.tm_isdst = (dst > 0 ? 0 : 1); 15717c478bd9Sstevel@tonic-gate if ((t2 = xmktime(&tmp2)) == (time_t)-1) { 15727c478bd9Sstevel@tonic-gate return (0); 15737c478bd9Sstevel@tonic-gate } 15747c478bd9Sstevel@tonic-gate if (tm_cmp(&tmp, &tmp2)) { 15757c478bd9Sstevel@tonic-gate /* 15767c478bd9Sstevel@tonic-gate * same time/date found in the opposite zone. 15777c478bd9Sstevel@tonic-gate * check the clock to see which comes early. 15787c478bd9Sstevel@tonic-gate */ 15797c478bd9Sstevel@tonic-gate if (t2 > ref_t && t2 < t1) { 15807c478bd9Sstevel@tonic-gate t1 = t2; 15817c478bd9Sstevel@tonic-gate } 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate } 15847c478bd9Sstevel@tonic-gate return (t1); 15857c478bd9Sstevel@tonic-gate } else { 15867c478bd9Sstevel@tonic-gate /* 15877c478bd9Sstevel@tonic-gate * mktime has set different time/date for the given date. 15887c478bd9Sstevel@tonic-gate * This means that the next job is scheduled to be run on the 15897c478bd9Sstevel@tonic-gate * invalid time. There are three possible invalid date/time. 15907c478bd9Sstevel@tonic-gate * 1. Non existing day of the month. such as April 31th. 15917c478bd9Sstevel@tonic-gate * 2. Feb 29th in the non-leap year. 15927c478bd9Sstevel@tonic-gate * 3. Time gap during the DST switch over. 15937c478bd9Sstevel@tonic-gate */ 15947c478bd9Sstevel@tonic-gate d1 = days_in_mon(mon, yr); 15957c478bd9Sstevel@tonic-gate if ((mon != 1 && day > d1) || (mon == 1 && day > 29)) { 15967c478bd9Sstevel@tonic-gate /* 15977c478bd9Sstevel@tonic-gate * see if we have got a specific date which 15987c478bd9Sstevel@tonic-gate * is invalid. 15997c478bd9Sstevel@tonic-gate */ 16007c478bd9Sstevel@tonic-gate if (strcmp(e->of.ct.dayweek, "*") == 0 && 16017c478bd9Sstevel@tonic-gate mon == (next_ge((mon+1)%12+1, e->of.ct.month)-1) && 16027c478bd9Sstevel@tonic-gate day <= next_ge(1, e->of.ct.daymon)) { 16037c478bd9Sstevel@tonic-gate /* job never run */ 16047c478bd9Sstevel@tonic-gate return (0); 16057c478bd9Sstevel@tonic-gate } 16067c478bd9Sstevel@tonic-gate /* 16077c478bd9Sstevel@tonic-gate * Since the day has gone invalid, we need to go to 16087c478bd9Sstevel@tonic-gate * next month, and recalcuate the first occurrence. 16097c478bd9Sstevel@tonic-gate * eg the cron tab such as: 16107c478bd9Sstevel@tonic-gate * 0 0 1,15,31 1,2,3,4,5 * /usr/bin.... 16117c478bd9Sstevel@tonic-gate * 2/31 is invalid, so the next job is 3/1. 16127c478bd9Sstevel@tonic-gate */ 16137c478bd9Sstevel@tonic-gate tmp2 = tmp; 16147c478bd9Sstevel@tonic-gate tmp2.tm_min = 0; 16157c478bd9Sstevel@tonic-gate tmp2.tm_hour = 0; 16167c478bd9Sstevel@tonic-gate tmp2.tm_mday = 1; /* 1st day of the month */ 16177c478bd9Sstevel@tonic-gate if (mon == 11) { 16187c478bd9Sstevel@tonic-gate tmp2.tm_mon = 0; 16197c478bd9Sstevel@tonic-gate tmp2.tm_year = yr + 1; 16207c478bd9Sstevel@tonic-gate } else { 16217c478bd9Sstevel@tonic-gate tmp2.tm_mon = mon + 1; 16227c478bd9Sstevel@tonic-gate } 16237c478bd9Sstevel@tonic-gate if ((t = xmktime(&tmp2)) == (time_t)-1) { 16247c478bd9Sstevel@tonic-gate return (0); 16257c478bd9Sstevel@tonic-gate } 16267c478bd9Sstevel@tonic-gate } else if (mon == 1 && day > d1) { 16277c478bd9Sstevel@tonic-gate /* 16287c478bd9Sstevel@tonic-gate * ie 29th in the non-leap year. Forwarding the 16297c478bd9Sstevel@tonic-gate * clock to Feb 29th 00:00 (March 1st), and recalculate 16307c478bd9Sstevel@tonic-gate * the next time. 16317c478bd9Sstevel@tonic-gate */ 16327c478bd9Sstevel@tonic-gate tmp2 = tmp; 16337c478bd9Sstevel@tonic-gate tmp2.tm_min = 0; 16347c478bd9Sstevel@tonic-gate tmp2.tm_hour = 0; 16357c478bd9Sstevel@tonic-gate if ((t = xmktime(&tmp2)) == (time_t)-1) { 16367c478bd9Sstevel@tonic-gate return (0); 16377c478bd9Sstevel@tonic-gate } 16387c478bd9Sstevel@tonic-gate } else if (daylight) { 16397c478bd9Sstevel@tonic-gate /* 16407c478bd9Sstevel@tonic-gate * Non existing time, eg 2am PST during summer time 16417c478bd9Sstevel@tonic-gate * switch. 16427c478bd9Sstevel@tonic-gate * We need to get the correct isdst which we are 16437c478bd9Sstevel@tonic-gate * swithing to, by adding time difference to make sure 16447c478bd9Sstevel@tonic-gate * that t2 is in the zone being switched. 16457c478bd9Sstevel@tonic-gate */ 16467c478bd9Sstevel@tonic-gate t2 = t1; 16477c478bd9Sstevel@tonic-gate t2 += abs(timezone - altzone); 16487c478bd9Sstevel@tonic-gate (void) localtime_r(&t2, &tmp2); 16497c478bd9Sstevel@tonic-gate zone_start = get_switching_time(tmp2.tm_isdst, 16507c478bd9Sstevel@tonic-gate t1 - abs(timezone - altzone)); 16517c478bd9Sstevel@tonic-gate if (zone_start == (time_t)-1) { 16527c478bd9Sstevel@tonic-gate return (0); 16537c478bd9Sstevel@tonic-gate } 16547c478bd9Sstevel@tonic-gate t = zone_start; 16557c478bd9Sstevel@tonic-gate } else { 16567c478bd9Sstevel@tonic-gate /* 16577c478bd9Sstevel@tonic-gate * This should never happen, but fall back to the 16587c478bd9Sstevel@tonic-gate * old code. 16597c478bd9Sstevel@tonic-gate */ 16607c478bd9Sstevel@tonic-gate days = days_btwn(tm->tm_mon, 16617c478bd9Sstevel@tonic-gate tm->tm_mday, tm->tm_year, mon, day, yr); 16627c478bd9Sstevel@tonic-gate t += (time_t)(23-tm->tm_hour)*HOUR 16637c478bd9Sstevel@tonic-gate + (time_t)(60-tm->tm_min)*MINUTE 16647c478bd9Sstevel@tonic-gate + (time_t)hr*HOUR + (time_t)min*MINUTE 16657c478bd9Sstevel@tonic-gate + (time_t)days*DAY; 16667c478bd9Sstevel@tonic-gate t1 = t; 16677c478bd9Sstevel@tonic-gate t -= (time_t)tm->tm_sec; 16687c478bd9Sstevel@tonic-gate (void) localtime_r(&t, &tmp); 16697c478bd9Sstevel@tonic-gate if ((tm->tm_isdst == 0) && (tmp.tm_isdst > 0)) 16707c478bd9Sstevel@tonic-gate t -= (timezone - altzone); 16717c478bd9Sstevel@tonic-gate return (t <= ref_t ? t1 : t); 16727c478bd9Sstevel@tonic-gate } 16737c478bd9Sstevel@tonic-gate goto recalc; 16747c478bd9Sstevel@tonic-gate } 16757c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate 16787c478bd9Sstevel@tonic-gate /* 16797c478bd9Sstevel@tonic-gate * This returns TOD in time_t that zone switch will happen, and this 16807c478bd9Sstevel@tonic-gate * will be called when clock fallback is about to happen. 16817c478bd9Sstevel@tonic-gate * (ie 30minutes before the time of PST -> PDT switch. 2:00 AM PST 16827c478bd9Sstevel@tonic-gate * will fall back to 1:00 PDT. So this function will be called only 16837c478bd9Sstevel@tonic-gate * for the time between 1:00 AM PST and 2:00 PST(1:00 PST)). 16847c478bd9Sstevel@tonic-gate * First goes through the common time differences to see if zone 16857c478bd9Sstevel@tonic-gate * switch happens at those minutes later. If not, check every minutes 16867c478bd9Sstevel@tonic-gate * until 6 hours ahead see if it happens(We might have 45minutes 16877c478bd9Sstevel@tonic-gate * fallback). 16887c478bd9Sstevel@tonic-gate */ 16897c478bd9Sstevel@tonic-gate static time_t 16907c478bd9Sstevel@tonic-gate get_switching_time(int to_dst, time_t t_ref) 16917c478bd9Sstevel@tonic-gate { 16927c478bd9Sstevel@tonic-gate time_t t, t1; 16937c478bd9Sstevel@tonic-gate struct tm tmp, tmp1; 16947c478bd9Sstevel@tonic-gate int hints[] = { 60, 120, 30, 90, 0}; /* minutes */ 16957c478bd9Sstevel@tonic-gate int i; 16967c478bd9Sstevel@tonic-gate 16977c478bd9Sstevel@tonic-gate (void) localtime_r(&t_ref, &tmp); 16987c478bd9Sstevel@tonic-gate tmp1 = tmp; 16997c478bd9Sstevel@tonic-gate tmp1.tm_sec = 0; 17007c478bd9Sstevel@tonic-gate tmp1.tm_min = 0; 17017c478bd9Sstevel@tonic-gate if ((t = xmktime(&tmp1)) == (time_t)-1) 17027c478bd9Sstevel@tonic-gate return ((time_t)-1); 17037c478bd9Sstevel@tonic-gate 17047c478bd9Sstevel@tonic-gate /* fast path */ 17057c478bd9Sstevel@tonic-gate for (i = 0; hints[i] != 0; i++) { 17067c478bd9Sstevel@tonic-gate t1 = t + hints[i] * 60; 17077c478bd9Sstevel@tonic-gate (void) localtime_r(&t1, &tmp1); 17087c478bd9Sstevel@tonic-gate if (tmp1.tm_isdst == to_dst) { 17097c478bd9Sstevel@tonic-gate t1--; 17107c478bd9Sstevel@tonic-gate (void) localtime_r(&t1, &tmp1); 17117c478bd9Sstevel@tonic-gate if (tmp1.tm_isdst != to_dst) { 17127c478bd9Sstevel@tonic-gate return (t1 + 1); 17137c478bd9Sstevel@tonic-gate } 17147c478bd9Sstevel@tonic-gate } 17157c478bd9Sstevel@tonic-gate } 17167c478bd9Sstevel@tonic-gate 17177c478bd9Sstevel@tonic-gate /* ugly, but don't know other than this. */ 17187c478bd9Sstevel@tonic-gate tmp1 = tmp; 17197c478bd9Sstevel@tonic-gate tmp1.tm_sec = 0; 17207c478bd9Sstevel@tonic-gate if ((t = xmktime(&tmp1)) == (time_t)-1) 17217c478bd9Sstevel@tonic-gate return ((time_t)-1); 17227c478bd9Sstevel@tonic-gate while (t < (t_ref + 6*60*60)) { /* 6 hours should be enough */ 17237c478bd9Sstevel@tonic-gate t += 60; /* at least one minute, I assume */ 17247c478bd9Sstevel@tonic-gate (void) localtime_r(&t, &tmp); 17257c478bd9Sstevel@tonic-gate if (tmp.tm_isdst == to_dst) 17267c478bd9Sstevel@tonic-gate return (t); 17277c478bd9Sstevel@tonic-gate } 17287c478bd9Sstevel@tonic-gate return ((time_t)-1); 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate 17317c478bd9Sstevel@tonic-gate static time_t 17327c478bd9Sstevel@tonic-gate xmktime(struct tm *tmp) 17337c478bd9Sstevel@tonic-gate { 17347c478bd9Sstevel@tonic-gate time_t ret; 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate if ((ret = mktime(tmp)) == (time_t)-1) { 17377c478bd9Sstevel@tonic-gate if (errno == EOVERFLOW) { 17387c478bd9Sstevel@tonic-gate return ((time_t)-1); 17397c478bd9Sstevel@tonic-gate } 17407c478bd9Sstevel@tonic-gate crabort("internal error: mktime failed", 17417c478bd9Sstevel@tonic-gate REMOVE_FIFO|CONSOLE_MSG); 17427c478bd9Sstevel@tonic-gate } 17437c478bd9Sstevel@tonic-gate return (ret); 17447c478bd9Sstevel@tonic-gate } 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate #define DUMMY 100 17477c478bd9Sstevel@tonic-gate 17487c478bd9Sstevel@tonic-gate static int 17497c478bd9Sstevel@tonic-gate next_ge(int current, char *list) 17507c478bd9Sstevel@tonic-gate { 17517c478bd9Sstevel@tonic-gate /* 17527c478bd9Sstevel@tonic-gate * list is a character field as in a crontab file; 17537c478bd9Sstevel@tonic-gate * for example: "40, 20, 50-10" 17547c478bd9Sstevel@tonic-gate * next_ge returns the next number in the list that is 17557c478bd9Sstevel@tonic-gate * greater than or equal to current. if no numbers of list 17567c478bd9Sstevel@tonic-gate * are >= current, the smallest element of list is returned. 17577c478bd9Sstevel@tonic-gate * NOTE: current must be in the appropriate range. 17587c478bd9Sstevel@tonic-gate */ 17597c478bd9Sstevel@tonic-gate 17607c478bd9Sstevel@tonic-gate char *ptr; 17617c478bd9Sstevel@tonic-gate int n, n2, min, min_gt; 17627c478bd9Sstevel@tonic-gate 17637c478bd9Sstevel@tonic-gate if (strcmp(list, "*") == 0) 17647c478bd9Sstevel@tonic-gate return (current); 17657c478bd9Sstevel@tonic-gate ptr = list; 17667c478bd9Sstevel@tonic-gate min = DUMMY; 17677c478bd9Sstevel@tonic-gate min_gt = DUMMY; 17687c478bd9Sstevel@tonic-gate for (;;) { 17697c478bd9Sstevel@tonic-gate if ((n = (int)num(&ptr)) == current) 17707c478bd9Sstevel@tonic-gate return (current); 17717c478bd9Sstevel@tonic-gate if (n < min) 17727c478bd9Sstevel@tonic-gate min = n; 17737c478bd9Sstevel@tonic-gate if ((n > current) && (n < min_gt)) 17747c478bd9Sstevel@tonic-gate min_gt = n; 17757c478bd9Sstevel@tonic-gate if (*ptr == '-') { 17767c478bd9Sstevel@tonic-gate ptr++; 17777c478bd9Sstevel@tonic-gate if ((n2 = (int)num(&ptr)) > n) { 17787c478bd9Sstevel@tonic-gate if ((current > n) && (current <= n2)) 17797c478bd9Sstevel@tonic-gate return (current); 17807c478bd9Sstevel@tonic-gate } else { /* range that wraps around */ 17817c478bd9Sstevel@tonic-gate if (current > n) 17827c478bd9Sstevel@tonic-gate return (current); 17837c478bd9Sstevel@tonic-gate if (current <= n2) 17847c478bd9Sstevel@tonic-gate return (current); 17857c478bd9Sstevel@tonic-gate } 17867c478bd9Sstevel@tonic-gate } 17877c478bd9Sstevel@tonic-gate if (*ptr == '\0') 17887c478bd9Sstevel@tonic-gate break; 17897c478bd9Sstevel@tonic-gate ptr += 1; 17907c478bd9Sstevel@tonic-gate } 17917c478bd9Sstevel@tonic-gate if (min_gt != DUMMY) 17927c478bd9Sstevel@tonic-gate return (min_gt); 17937c478bd9Sstevel@tonic-gate else 17947c478bd9Sstevel@tonic-gate return (min); 17957c478bd9Sstevel@tonic-gate } 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate static void 17987c478bd9Sstevel@tonic-gate free_if_unused(struct usr *u) 17997c478bd9Sstevel@tonic-gate { 18007c478bd9Sstevel@tonic-gate struct usr *cur, *prev; 18017c478bd9Sstevel@tonic-gate /* 18027c478bd9Sstevel@tonic-gate * To make sure a usr structure is idle we must check that 18037c478bd9Sstevel@tonic-gate * there are no at jobs queued for the user; the user does 18047c478bd9Sstevel@tonic-gate * not have a crontab, and also that there are no running at 18057c478bd9Sstevel@tonic-gate * or cron jobs (since the runinfo structure also has a 18067c478bd9Sstevel@tonic-gate * pointer to the usr structure). 18077c478bd9Sstevel@tonic-gate */ 18087c478bd9Sstevel@tonic-gate if (!u->ctexists && u->atevents == NULL && 18097c478bd9Sstevel@tonic-gate u->cruncnt == 0 && u->aruncnt == 0) { 18107c478bd9Sstevel@tonic-gate #ifdef DEBUG 18117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s removed from usr list\n", u->name); 18127c478bd9Sstevel@tonic-gate #endif 18137c478bd9Sstevel@tonic-gate for (cur = uhead, prev = NULL; 18147c478bd9Sstevel@tonic-gate cur != u; 18157c478bd9Sstevel@tonic-gate prev = cur, cur = cur->nextusr) { 18167c478bd9Sstevel@tonic-gate if (cur == NULL) { 18177c478bd9Sstevel@tonic-gate return; 18187c478bd9Sstevel@tonic-gate } 18197c478bd9Sstevel@tonic-gate } 18207c478bd9Sstevel@tonic-gate 18217c478bd9Sstevel@tonic-gate if (prev == NULL) 18227c478bd9Sstevel@tonic-gate uhead = u->nextusr; 18237c478bd9Sstevel@tonic-gate else 18247c478bd9Sstevel@tonic-gate prev->nextusr = u->nextusr; 18257c478bd9Sstevel@tonic-gate free(u->name); 18267c478bd9Sstevel@tonic-gate free(u->home); 18277c478bd9Sstevel@tonic-gate free(u); 18287c478bd9Sstevel@tonic-gate } 18297c478bd9Sstevel@tonic-gate } 18307c478bd9Sstevel@tonic-gate 18317c478bd9Sstevel@tonic-gate static void 18327c478bd9Sstevel@tonic-gate del_atjob(char *name, char *usrname) 18337c478bd9Sstevel@tonic-gate { 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate struct event *e, *eprev; 18367c478bd9Sstevel@tonic-gate struct usr *u; 18377c478bd9Sstevel@tonic-gate 18387c478bd9Sstevel@tonic-gate if ((u = find_usr(usrname)) == NULL) 18397c478bd9Sstevel@tonic-gate return; 18407c478bd9Sstevel@tonic-gate e = u->atevents; 18417c478bd9Sstevel@tonic-gate eprev = NULL; 18427c478bd9Sstevel@tonic-gate while (e != NULL) { 18437c478bd9Sstevel@tonic-gate if (strcmp(name, e->cmd) == 0) { 18447c478bd9Sstevel@tonic-gate if (next_event == e) 18457c478bd9Sstevel@tonic-gate next_event = NULL; 18467c478bd9Sstevel@tonic-gate if (eprev == NULL) 18477c478bd9Sstevel@tonic-gate u->atevents = e->link; 18487c478bd9Sstevel@tonic-gate else 18497c478bd9Sstevel@tonic-gate eprev->link = e->link; 18507c478bd9Sstevel@tonic-gate el_remove(e->of.at.eventid, 1); 18517c478bd9Sstevel@tonic-gate free(e->cmd); 18527c478bd9Sstevel@tonic-gate free(e); 18537c478bd9Sstevel@tonic-gate break; 18547c478bd9Sstevel@tonic-gate } else { 18557c478bd9Sstevel@tonic-gate eprev = e; 18567c478bd9Sstevel@tonic-gate e = e->link; 18577c478bd9Sstevel@tonic-gate } 18587c478bd9Sstevel@tonic-gate } 18597c478bd9Sstevel@tonic-gate 18607c478bd9Sstevel@tonic-gate free_if_unused(u); 18617c478bd9Sstevel@tonic-gate } 18627c478bd9Sstevel@tonic-gate 18637c478bd9Sstevel@tonic-gate static void 18647c478bd9Sstevel@tonic-gate del_ctab(char *name) 18657c478bd9Sstevel@tonic-gate { 18667c478bd9Sstevel@tonic-gate 18677c478bd9Sstevel@tonic-gate struct usr *u; 18687c478bd9Sstevel@tonic-gate 18697c478bd9Sstevel@tonic-gate if ((u = find_usr(name)) == NULL) 18707c478bd9Sstevel@tonic-gate return; 18717c478bd9Sstevel@tonic-gate rm_ctevents(u); 18727c478bd9Sstevel@tonic-gate el_remove(u->ctid, 0); 18737c478bd9Sstevel@tonic-gate u->ctid = 0; 18747c478bd9Sstevel@tonic-gate u->ctexists = 0; 18757c478bd9Sstevel@tonic-gate 18767c478bd9Sstevel@tonic-gate free_if_unused(u); 18777c478bd9Sstevel@tonic-gate } 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate static void 18807c478bd9Sstevel@tonic-gate rm_ctevents(struct usr *u) 18817c478bd9Sstevel@tonic-gate { 18827c478bd9Sstevel@tonic-gate struct event *e2, *e3; 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate /* 18857c478bd9Sstevel@tonic-gate * see if the next event (to be run by cron) is a cronevent 18867c478bd9Sstevel@tonic-gate * owned by this user. 18877c478bd9Sstevel@tonic-gate */ 18887c478bd9Sstevel@tonic-gate 18897c478bd9Sstevel@tonic-gate if ((next_event != NULL) && 18907c478bd9Sstevel@tonic-gate (next_event->etype == CRONEVENT) && 18917c478bd9Sstevel@tonic-gate (next_event->u == u)) { 18927c478bd9Sstevel@tonic-gate next_event = NULL; 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate e2 = u->ctevents; 18957c478bd9Sstevel@tonic-gate while (e2 != NULL) { 18967c478bd9Sstevel@tonic-gate free(e2->cmd); 18977c478bd9Sstevel@tonic-gate free(e2->of.ct.minute); 18987c478bd9Sstevel@tonic-gate free(e2->of.ct.hour); 18997c478bd9Sstevel@tonic-gate free(e2->of.ct.daymon); 19007c478bd9Sstevel@tonic-gate free(e2->of.ct.month); 19017c478bd9Sstevel@tonic-gate free(e2->of.ct.dayweek); 19027c478bd9Sstevel@tonic-gate if (e2->of.ct.input != NULL) 19037c478bd9Sstevel@tonic-gate free(e2->of.ct.input); 19047c478bd9Sstevel@tonic-gate e3 = e2->link; 19057c478bd9Sstevel@tonic-gate free(e2); 19067c478bd9Sstevel@tonic-gate e2 = e3; 19077c478bd9Sstevel@tonic-gate } 19087c478bd9Sstevel@tonic-gate u->ctevents = NULL; 19097c478bd9Sstevel@tonic-gate } 19107c478bd9Sstevel@tonic-gate 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate static struct usr * 19137c478bd9Sstevel@tonic-gate find_usr(char *uname) 19147c478bd9Sstevel@tonic-gate { 19157c478bd9Sstevel@tonic-gate struct usr *u; 19167c478bd9Sstevel@tonic-gate 19177c478bd9Sstevel@tonic-gate u = uhead; 19187c478bd9Sstevel@tonic-gate while (u != NULL) { 19197c478bd9Sstevel@tonic-gate if (strcmp(u->name, uname) == 0) 19207c478bd9Sstevel@tonic-gate return (u); 19217c478bd9Sstevel@tonic-gate u = u->nextusr; 19227c478bd9Sstevel@tonic-gate } 19237c478bd9Sstevel@tonic-gate return (NULL); 19247c478bd9Sstevel@tonic-gate } 19257c478bd9Sstevel@tonic-gate 19267c478bd9Sstevel@tonic-gate /* 19277c478bd9Sstevel@tonic-gate * Execute cron command or at/batch job. 19287c478bd9Sstevel@tonic-gate * If ever a premature return is added to this function pay attention to 19297c478bd9Sstevel@tonic-gate * free at_cmdfile and outfile plus jobname buffers of the runinfo structure. 19307c478bd9Sstevel@tonic-gate */ 19317c478bd9Sstevel@tonic-gate static int 19327c478bd9Sstevel@tonic-gate ex(struct event *e) 19337c478bd9Sstevel@tonic-gate { 19347c478bd9Sstevel@tonic-gate int r; 19357c478bd9Sstevel@tonic-gate int fd; 19367c478bd9Sstevel@tonic-gate pid_t rfork; 19377c478bd9Sstevel@tonic-gate FILE *atcmdfp; 19387c478bd9Sstevel@tonic-gate char mailvar[4]; 19397c478bd9Sstevel@tonic-gate char *at_cmdfile = NULL; 19407c478bd9Sstevel@tonic-gate struct stat buf; 19417c478bd9Sstevel@tonic-gate struct queue *qp; 19427c478bd9Sstevel@tonic-gate struct runinfo *rp; 19437c478bd9Sstevel@tonic-gate struct project proj, *pproj = NULL; 19447c478bd9Sstevel@tonic-gate char mybuf[PROJECT_BUFSZ]; 19457c478bd9Sstevel@tonic-gate char mybuf2[PROJECT_BUFSZ]; 19467c478bd9Sstevel@tonic-gate char *tmpfile; 19477c478bd9Sstevel@tonic-gate FILE *fptr; 19487c478bd9Sstevel@tonic-gate time_t dhltime; 19497c478bd9Sstevel@tonic-gate projid_t projid; 19507c478bd9Sstevel@tonic-gate int projflag = 0; 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate qp = &qt[e->etype]; /* set pointer to queue defs */ 19537c478bd9Sstevel@tonic-gate if (qp->nrun >= qp->njob) { 19547c478bd9Sstevel@tonic-gate msg("%c queue max run limit reached", e->etype+'a'); 19557c478bd9Sstevel@tonic-gate resched(qp->nwait); 19567c478bd9Sstevel@tonic-gate return (0); 19577c478bd9Sstevel@tonic-gate } 1958*9f163834Sbasabi rp = rinfo_get(0); /* allocating a new runinfo struct */ 19597c478bd9Sstevel@tonic-gate 19607c478bd9Sstevel@tonic-gate #ifdef ATLIMIT 19617c478bd9Sstevel@tonic-gate if ((e->u)->uid != 0 && (e->u)->aruncnt >= ATLIMIT) { 19627c478bd9Sstevel@tonic-gate msg("ATLIMIT (%d) reached for uid %d", 19637c478bd9Sstevel@tonic-gate ATLIMIT, (e->u)->uid); 19647c478bd9Sstevel@tonic-gate rinfo_free(rp); 19657c478bd9Sstevel@tonic-gate resched(qp->nwait); 19667c478bd9Sstevel@tonic-gate return (0); 19677c478bd9Sstevel@tonic-gate } 19687c478bd9Sstevel@tonic-gate #endif 19697c478bd9Sstevel@tonic-gate #ifdef CRONLIMIT 19707c478bd9Sstevel@tonic-gate if ((e->u)->uid != 0 && (e->u)->cruncnt >= CRONLIMIT) { 19717c478bd9Sstevel@tonic-gate msg("CRONLIMIT (%d) reached for uid %d", 19727c478bd9Sstevel@tonic-gate CRONLIMIT, (e->u)->uid); 19737c478bd9Sstevel@tonic-gate rinfo_free(rp); 19747c478bd9Sstevel@tonic-gate resched(qp->nwait); 19757c478bd9Sstevel@tonic-gate return (0); 19767c478bd9Sstevel@tonic-gate } 19777c478bd9Sstevel@tonic-gate #endif 19787c478bd9Sstevel@tonic-gate if ((e->u)->uid == 0) { /* set default path */ 19797c478bd9Sstevel@tonic-gate /* path settable in defaults file */ 19807c478bd9Sstevel@tonic-gate envinit[2] = supath; 19817c478bd9Sstevel@tonic-gate } else { 19827c478bd9Sstevel@tonic-gate envinit[2] = path; 19837c478bd9Sstevel@tonic-gate } 19847c478bd9Sstevel@tonic-gate 19857c478bd9Sstevel@tonic-gate /* 19867c478bd9Sstevel@tonic-gate * the tempnam() function uses malloc(3C) to allocate space for the 19877c478bd9Sstevel@tonic-gate * constructed file name, and returns a pointer to this area, which 19887c478bd9Sstevel@tonic-gate * is assigned to rp->outfile. Here rp->outfile is not overwritten. 19897c478bd9Sstevel@tonic-gate */ 19907c478bd9Sstevel@tonic-gate 19917c478bd9Sstevel@tonic-gate rp->outfile = tempnam(TMPDIR, PFX); 19927c478bd9Sstevel@tonic-gate rp->jobtype = e->etype; 19937c478bd9Sstevel@tonic-gate if (e->etype == CRONEVENT) { 19947c478bd9Sstevel@tonic-gate rp->jobname = xmalloc(strlen(e->cmd)+1); 19957c478bd9Sstevel@tonic-gate (void) strcpy(rp->jobname, e->cmd); 19967c478bd9Sstevel@tonic-gate /* "cron" jobs only produce mail if there's output */ 19977c478bd9Sstevel@tonic-gate rp->mailwhendone = 0; 19987c478bd9Sstevel@tonic-gate } else { 19997c478bd9Sstevel@tonic-gate at_cmdfile = xmalloc(strlen(ATDIR)+strlen(e->cmd)+2); 20007c478bd9Sstevel@tonic-gate (void) sprintf(at_cmdfile, "%s/%s", ATDIR, e->cmd); 20017c478bd9Sstevel@tonic-gate if ((atcmdfp = fopen(at_cmdfile, "r")) == NULL) { 20027c478bd9Sstevel@tonic-gate if (errno == ENAMETOOLONG) { 20037c478bd9Sstevel@tonic-gate if (chdir(ATDIR) == 0) 20047c478bd9Sstevel@tonic-gate cron_unlink(e->cmd); 20057c478bd9Sstevel@tonic-gate } else { 20067c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 20077c478bd9Sstevel@tonic-gate } 20087c478bd9Sstevel@tonic-gate mail((e->u)->name, BADJOBOPEN, ERR_CANTEXECAT); 20097c478bd9Sstevel@tonic-gate free(at_cmdfile); 20107c478bd9Sstevel@tonic-gate rinfo_free(rp); 20117c478bd9Sstevel@tonic-gate return (0); 20127c478bd9Sstevel@tonic-gate } 20137c478bd9Sstevel@tonic-gate rp->jobname = xmalloc(strlen(at_cmdfile)+1); 20147c478bd9Sstevel@tonic-gate (void) strcpy(rp->jobname, at_cmdfile); 20157c478bd9Sstevel@tonic-gate 20167c478bd9Sstevel@tonic-gate /* 20177c478bd9Sstevel@tonic-gate * Skip over the first two lines. 20187c478bd9Sstevel@tonic-gate */ 20197c478bd9Sstevel@tonic-gate (void) fscanf(atcmdfp, "%*[^\n]\n"); 20207c478bd9Sstevel@tonic-gate (void) fscanf(atcmdfp, "%*[^\n]\n"); 20217c478bd9Sstevel@tonic-gate if (fscanf(atcmdfp, ": notify by mail: %3s%*[^\n]\n", 20227c478bd9Sstevel@tonic-gate mailvar) == 1) { 20237c478bd9Sstevel@tonic-gate /* 20247c478bd9Sstevel@tonic-gate * Check to see if we should always send mail 20257c478bd9Sstevel@tonic-gate * to the owner. 20267c478bd9Sstevel@tonic-gate */ 20277c478bd9Sstevel@tonic-gate rp->mailwhendone = (strcmp(mailvar, "yes") == 0); 20287c478bd9Sstevel@tonic-gate } else { 20297c478bd9Sstevel@tonic-gate rp->mailwhendone = 0; 20307c478bd9Sstevel@tonic-gate } 20317c478bd9Sstevel@tonic-gate 20327c478bd9Sstevel@tonic-gate if (fscanf(atcmdfp, "\n: project: %d\n", &projid) == 1) { 20337c478bd9Sstevel@tonic-gate projflag = 1; 20347c478bd9Sstevel@tonic-gate } 20357c478bd9Sstevel@tonic-gate (void) fclose(atcmdfp); 20367c478bd9Sstevel@tonic-gate } 20377c478bd9Sstevel@tonic-gate 20387c478bd9Sstevel@tonic-gate /* 20397c478bd9Sstevel@tonic-gate * we make sure that the system time 20407c478bd9Sstevel@tonic-gate * hasn't drifted backwards. if it has, el_add() is now 20417c478bd9Sstevel@tonic-gate * called, to make sure that the event queue is back in order, 20427c478bd9Sstevel@tonic-gate * and we set the delayed flag. cron will pick up the request 20437c478bd9Sstevel@tonic-gate * later on at the proper time. 20447c478bd9Sstevel@tonic-gate */ 20457c478bd9Sstevel@tonic-gate dhltime = time(NULL); 20467c478bd9Sstevel@tonic-gate if ((dhltime - e->time) < 0) { 20477c478bd9Sstevel@tonic-gate msg("clock time drifted backwards!\n"); 20487c478bd9Sstevel@tonic-gate if (next_event->etype == CRONEVENT) { 20497c478bd9Sstevel@tonic-gate msg("correcting cron event\n"); 20507c478bd9Sstevel@tonic-gate next_event->time = next_time(next_event, dhltime); 20517c478bd9Sstevel@tonic-gate el_add(next_event, next_event->time, 20527c478bd9Sstevel@tonic-gate (next_event->u)->ctid); 20537c478bd9Sstevel@tonic-gate } else { /* etype == ATEVENT */ 20547c478bd9Sstevel@tonic-gate msg("correcting batch event\n"); 20557c478bd9Sstevel@tonic-gate el_add(next_event, next_event->time, 20567c478bd9Sstevel@tonic-gate next_event->of.at.eventid); 20577c478bd9Sstevel@tonic-gate } 20587c478bd9Sstevel@tonic-gate delayed++; 20597c478bd9Sstevel@tonic-gate t_old = time(NULL); 20607c478bd9Sstevel@tonic-gate free(at_cmdfile); 20617c478bd9Sstevel@tonic-gate rinfo_free(rp); 20627c478bd9Sstevel@tonic-gate return (0); 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate if ((rfork = fork()) == (pid_t)-1) { 20667c478bd9Sstevel@tonic-gate reap_child(); 20677c478bd9Sstevel@tonic-gate if ((rfork = fork()) == (pid_t)-1) { 20687c478bd9Sstevel@tonic-gate msg("cannot fork"); 20697c478bd9Sstevel@tonic-gate free(at_cmdfile); 20707c478bd9Sstevel@tonic-gate rinfo_free(rp); 20717c478bd9Sstevel@tonic-gate resched(60); 20727c478bd9Sstevel@tonic-gate (void) sleep(30); 20737c478bd9Sstevel@tonic-gate return (0); 20747c478bd9Sstevel@tonic-gate } 20757c478bd9Sstevel@tonic-gate } 20767c478bd9Sstevel@tonic-gate if (rfork) { /* parent process */ 20777c478bd9Sstevel@tonic-gate contract_abandon_latest(rfork); 20787c478bd9Sstevel@tonic-gate 20797c478bd9Sstevel@tonic-gate ++qp->nrun; 20807c478bd9Sstevel@tonic-gate rp->pid = rfork; 20817c478bd9Sstevel@tonic-gate rp->que = e->etype; 20827c478bd9Sstevel@tonic-gate if (e->etype != CRONEVENT) 20837c478bd9Sstevel@tonic-gate (e->u)->aruncnt++; 20847c478bd9Sstevel@tonic-gate else 20857c478bd9Sstevel@tonic-gate (e->u)->cruncnt++; 20867c478bd9Sstevel@tonic-gate rp->rusr = (e->u); 20877c478bd9Sstevel@tonic-gate logit(BCHAR, rp, 0); 20887c478bd9Sstevel@tonic-gate free(at_cmdfile); 20897c478bd9Sstevel@tonic-gate 20907c478bd9Sstevel@tonic-gate return (0); 20917c478bd9Sstevel@tonic-gate } 20927c478bd9Sstevel@tonic-gate 20937c478bd9Sstevel@tonic-gate child_sigreset(); 20947c478bd9Sstevel@tonic-gate contract_clear_template(); 20957c478bd9Sstevel@tonic-gate 20967c478bd9Sstevel@tonic-gate if (e->etype != CRONEVENT) { 20977c478bd9Sstevel@tonic-gate /* open jobfile as stdin to shell */ 20987c478bd9Sstevel@tonic-gate if (stat(at_cmdfile, &buf)) { 20997c478bd9Sstevel@tonic-gate if (errno == ENAMETOOLONG) { 21007c478bd9Sstevel@tonic-gate if (chdir(ATDIR) == 0) 21017c478bd9Sstevel@tonic-gate cron_unlink(e->cmd); 21027c478bd9Sstevel@tonic-gate } else 21037c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 21047c478bd9Sstevel@tonic-gate mail((e->u)->name, BADJOBOPEN, ERR_CANTEXECCRON); 21057c478bd9Sstevel@tonic-gate exit(1); 21067c478bd9Sstevel@tonic-gate } 21077c478bd9Sstevel@tonic-gate if (!(buf.st_mode&ISUID)) { 21087c478bd9Sstevel@tonic-gate /* 21097c478bd9Sstevel@tonic-gate * if setuid bit off, original owner has 21107c478bd9Sstevel@tonic-gate * given this file to someone else 21117c478bd9Sstevel@tonic-gate */ 21127c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 21137c478bd9Sstevel@tonic-gate exit(1); 21147c478bd9Sstevel@tonic-gate } 21157c478bd9Sstevel@tonic-gate if ((fd = open(at_cmdfile, O_RDONLY)) == -1) { 21167c478bd9Sstevel@tonic-gate mail((e->u)->name, BADJOBOPEN, ERR_CANTEXECCRON); 21177c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 21187c478bd9Sstevel@tonic-gate exit(1); 21197c478bd9Sstevel@tonic-gate } 21207c478bd9Sstevel@tonic-gate if (fd != 0) { 21217c478bd9Sstevel@tonic-gate (void) dup2(fd, 0); 21227c478bd9Sstevel@tonic-gate (void) close(fd); 21237c478bd9Sstevel@tonic-gate } 21247c478bd9Sstevel@tonic-gate /* 21257c478bd9Sstevel@tonic-gate * retrieve the project id of the at job and convert it 21267c478bd9Sstevel@tonic-gate * to a project name. fail if it's not a valid project 21277c478bd9Sstevel@tonic-gate * or if the user isn't a member of the project. 21287c478bd9Sstevel@tonic-gate */ 21297c478bd9Sstevel@tonic-gate if (projflag == 1) { 21307c478bd9Sstevel@tonic-gate if ((pproj = getprojbyid(projid, &proj, 21317c478bd9Sstevel@tonic-gate (void *)&mybuf, sizeof (mybuf))) == NULL || 21327c478bd9Sstevel@tonic-gate !inproj(e->u->name, pproj->pj_name, 21337c478bd9Sstevel@tonic-gate mybuf2, sizeof (mybuf2))) { 21347c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 21357c478bd9Sstevel@tonic-gate mail((e->u)->name, BADPROJID, ERR_CANTEXECAT); 21367c478bd9Sstevel@tonic-gate exit(1); 21377c478bd9Sstevel@tonic-gate } 21387c478bd9Sstevel@tonic-gate } 21397c478bd9Sstevel@tonic-gate } 21407c478bd9Sstevel@tonic-gate 21417c478bd9Sstevel@tonic-gate /* 21427c478bd9Sstevel@tonic-gate * Put process in a new session, and create a new task. 21437c478bd9Sstevel@tonic-gate */ 21447c478bd9Sstevel@tonic-gate if (setsid() < 0) { 21457c478bd9Sstevel@tonic-gate msg("setsid failed with errno = %d. job failed (%s)" 21467c478bd9Sstevel@tonic-gate " for user %s", errno, e->cmd, e->u->name); 21477c478bd9Sstevel@tonic-gate if (e->etype != CRONEVENT) 21487c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 21497c478bd9Sstevel@tonic-gate exit(1); 21507c478bd9Sstevel@tonic-gate } 21517c478bd9Sstevel@tonic-gate 21527c478bd9Sstevel@tonic-gate /* 21537c478bd9Sstevel@tonic-gate * set correct user identification and check his account 21547c478bd9Sstevel@tonic-gate */ 21557c478bd9Sstevel@tonic-gate r = set_user_cred(e->u, pproj); 21567c478bd9Sstevel@tonic-gate if (r == VUC_EXPIRED) { 21577c478bd9Sstevel@tonic-gate msg("user (%s) account is expired", e->u->name); 21587c478bd9Sstevel@tonic-gate audit_cron_user_acct_expired(e->u->name); 21597c478bd9Sstevel@tonic-gate clean_out_user(e->u); 21607c478bd9Sstevel@tonic-gate exit(1); 21617c478bd9Sstevel@tonic-gate } 21627c478bd9Sstevel@tonic-gate if (r == VUC_NEW_AUTH) { 21637c478bd9Sstevel@tonic-gate msg("user (%s) password has expired", e->u->name); 21647c478bd9Sstevel@tonic-gate audit_cron_user_acct_expired(e->u->name); 21657c478bd9Sstevel@tonic-gate clean_out_user(e->u); 21667c478bd9Sstevel@tonic-gate exit(1); 21677c478bd9Sstevel@tonic-gate } 21687c478bd9Sstevel@tonic-gate if (r != VUC_OK) { 21697c478bd9Sstevel@tonic-gate msg("bad user (%s)", e->u->name); 21707c478bd9Sstevel@tonic-gate audit_cron_bad_user(e->u->name); 21717c478bd9Sstevel@tonic-gate clean_out_user(e->u); 21727c478bd9Sstevel@tonic-gate exit(1); 21737c478bd9Sstevel@tonic-gate } 21747c478bd9Sstevel@tonic-gate /* 21757c478bd9Sstevel@tonic-gate * check user and initialize the supplementary group access list. 21767c478bd9Sstevel@tonic-gate * bugid 1230784: deleted from parent to avoid cron hang. Now 21777c478bd9Sstevel@tonic-gate * only child handles the call. 21787c478bd9Sstevel@tonic-gate */ 21797c478bd9Sstevel@tonic-gate 21807c478bd9Sstevel@tonic-gate if (verify_user_cred(e->u) != VUC_OK || 21817c478bd9Sstevel@tonic-gate setgid(e->u->gid) == -1 || 21827c478bd9Sstevel@tonic-gate initgroups(e->u->name, e->u->gid) == -1) { 21837c478bd9Sstevel@tonic-gate msg("bad user (%s) or setgid failed (%s)", 21847c478bd9Sstevel@tonic-gate e->u->name, e->u->name); 21857c478bd9Sstevel@tonic-gate audit_cron_bad_user(e->u->name); 21867c478bd9Sstevel@tonic-gate clean_out_user(e->u); 21877c478bd9Sstevel@tonic-gate exit(1); 21887c478bd9Sstevel@tonic-gate } 21897c478bd9Sstevel@tonic-gate 21907c478bd9Sstevel@tonic-gate if (e->etype != CRONEVENT) { 21917c478bd9Sstevel@tonic-gate r = audit_cron_session(e->u->name, NULL, 21927c478bd9Sstevel@tonic-gate e->u->uid, e->u->gid, 21937c478bd9Sstevel@tonic-gate at_cmdfile); 21947c478bd9Sstevel@tonic-gate cron_unlink(at_cmdfile); 21957c478bd9Sstevel@tonic-gate } else { 21967c478bd9Sstevel@tonic-gate r = audit_cron_session(e->u->name, CRONDIR, 21977c478bd9Sstevel@tonic-gate e->u->uid, e->u->gid, 21987c478bd9Sstevel@tonic-gate NULL); 21997c478bd9Sstevel@tonic-gate } 22007c478bd9Sstevel@tonic-gate if (r != 0) { 22017c478bd9Sstevel@tonic-gate msg("cron audit problem. job failed (%s) for user %s", 22027c478bd9Sstevel@tonic-gate e->cmd, e->u->name); 22037c478bd9Sstevel@tonic-gate exit(1); 22047c478bd9Sstevel@tonic-gate } 22057c478bd9Sstevel@tonic-gate 22067c478bd9Sstevel@tonic-gate audit_cron_new_job(e->cmd, e->etype, (void *)e); 22077c478bd9Sstevel@tonic-gate 22087c478bd9Sstevel@tonic-gate if (setuid(e->u->uid) == -1) { 22097c478bd9Sstevel@tonic-gate msg("setuid failed (%s)", e->u->name); 22107c478bd9Sstevel@tonic-gate clean_out_user(e->u); 22117c478bd9Sstevel@tonic-gate exit(1); 22127c478bd9Sstevel@tonic-gate } 22137c478bd9Sstevel@tonic-gate 22147c478bd9Sstevel@tonic-gate if (e->etype == CRONEVENT) { 22157c478bd9Sstevel@tonic-gate /* check for standard input to command */ 22167c478bd9Sstevel@tonic-gate if (e->of.ct.input != NULL) { 22177c478bd9Sstevel@tonic-gate if ((tmpfile = strdup(TMPINFILE)) == NULL) { 22187c478bd9Sstevel@tonic-gate mail((e->u)->name, MALLOCERR, 22197c478bd9Sstevel@tonic-gate ERR_CANTEXECCRON); 22207c478bd9Sstevel@tonic-gate exit(1); 22217c478bd9Sstevel@tonic-gate } 22227c478bd9Sstevel@tonic-gate if ((fd = mkstemp(tmpfile)) == -1 || 22237c478bd9Sstevel@tonic-gate (fptr = fdopen(fd, "w")) == NULL) { 22247c478bd9Sstevel@tonic-gate mail((e->u)->name, NOSTDIN, 22257c478bd9Sstevel@tonic-gate ERR_CANTEXECCRON); 22267c478bd9Sstevel@tonic-gate cron_unlink(tmpfile); 22277c478bd9Sstevel@tonic-gate free(tmpfile); 22287c478bd9Sstevel@tonic-gate exit(1); 22297c478bd9Sstevel@tonic-gate } 22307c478bd9Sstevel@tonic-gate if ((fwrite(e->of.ct.input, sizeof (char), 22317c478bd9Sstevel@tonic-gate strlen(e->of.ct.input), fptr)) != 22327c478bd9Sstevel@tonic-gate strlen(e->of.ct.input)) { 22337c478bd9Sstevel@tonic-gate mail((e->u)->name, NOSTDIN, ERR_CANTEXECCRON); 22347c478bd9Sstevel@tonic-gate cron_unlink(tmpfile); 22357c478bd9Sstevel@tonic-gate free(tmpfile); 22367c478bd9Sstevel@tonic-gate (void) close(fd); 22377c478bd9Sstevel@tonic-gate (void) fclose(fptr); 22387c478bd9Sstevel@tonic-gate exit(1); 22397c478bd9Sstevel@tonic-gate } 22407c478bd9Sstevel@tonic-gate if (fseek(fptr, (off_t)0, SEEK_SET) != -1) { 22417c478bd9Sstevel@tonic-gate if (fd != 0) { 22427c478bd9Sstevel@tonic-gate (void) dup2(fd, 0); 22437c478bd9Sstevel@tonic-gate (void) close(fd); 22447c478bd9Sstevel@tonic-gate } 22457c478bd9Sstevel@tonic-gate } 22467c478bd9Sstevel@tonic-gate cron_unlink(tmpfile); 22477c478bd9Sstevel@tonic-gate free(tmpfile); 22487c478bd9Sstevel@tonic-gate (void) fclose(fptr); 22497c478bd9Sstevel@tonic-gate } else if ((fd = open("/dev/null", O_RDONLY)) > 0) { 22507c478bd9Sstevel@tonic-gate (void) dup2(fd, 0); 22517c478bd9Sstevel@tonic-gate (void) close(fd); 22527c478bd9Sstevel@tonic-gate } 22537c478bd9Sstevel@tonic-gate } 22547c478bd9Sstevel@tonic-gate 22557c478bd9Sstevel@tonic-gate /* redirect stdout and stderr for the shell */ 22567c478bd9Sstevel@tonic-gate if ((fd = open(rp->outfile, O_WRONLY|O_CREAT|O_EXCL, OUTMODE)) == 1) 22577c478bd9Sstevel@tonic-gate fd = open("/dev/null", O_WRONLY); 22587c478bd9Sstevel@tonic-gate 22597c478bd9Sstevel@tonic-gate if (fd >= 0 && fd != 1) 22607c478bd9Sstevel@tonic-gate (void) dup2(fd, 1); 22617c478bd9Sstevel@tonic-gate 22627c478bd9Sstevel@tonic-gate if (fd >= 0 && fd != 2) { 22637c478bd9Sstevel@tonic-gate (void) dup2(fd, 2); 22647c478bd9Sstevel@tonic-gate if (fd != 1) 22657c478bd9Sstevel@tonic-gate (void) close(fd); 22667c478bd9Sstevel@tonic-gate } 22677c478bd9Sstevel@tonic-gate 22687c478bd9Sstevel@tonic-gate (void) strlcat(homedir, (e->u)->home, sizeof (homedir)); 22697c478bd9Sstevel@tonic-gate (void) strlcat(logname, (e->u)->name, sizeof (logname)); 22707c478bd9Sstevel@tonic-gate environ = envinit; 22717c478bd9Sstevel@tonic-gate if (chdir((e->u)->home) == -1) { 22727c478bd9Sstevel@tonic-gate mail((e->u)->name, CANTCDHOME, 22737c478bd9Sstevel@tonic-gate e->etype == CRONEVENT ? ERR_CANTEXECCRON : 22747c478bd9Sstevel@tonic-gate ERR_CANTEXECAT); 22757c478bd9Sstevel@tonic-gate exit(1); 22767c478bd9Sstevel@tonic-gate } 22777c478bd9Sstevel@tonic-gate #ifdef TESTING 22787c478bd9Sstevel@tonic-gate exit(1); 22797c478bd9Sstevel@tonic-gate #endif 22807c478bd9Sstevel@tonic-gate /* 22817c478bd9Sstevel@tonic-gate * make sure that all file descriptors EXCEPT 0, 1 and 2 22827c478bd9Sstevel@tonic-gate * will be closed. 22837c478bd9Sstevel@tonic-gate */ 22847c478bd9Sstevel@tonic-gate closefrom(3); 22857c478bd9Sstevel@tonic-gate 22867c478bd9Sstevel@tonic-gate if ((e->u)->uid != 0) 22877c478bd9Sstevel@tonic-gate (void) nice(qp->nice); 22887c478bd9Sstevel@tonic-gate if (e->etype == CRONEVENT) 22897c478bd9Sstevel@tonic-gate (void) execl(SHELL, "sh", "-c", e->cmd, 0); 22907c478bd9Sstevel@tonic-gate else /* type == ATEVENT */ 22917c478bd9Sstevel@tonic-gate (void) execl(SHELL, "sh", 0); 22927c478bd9Sstevel@tonic-gate mail((e->u)->name, CANTEXECSH, 22937c478bd9Sstevel@tonic-gate e->etype == CRONEVENT ? ERR_CANTEXECCRON : ERR_CANTEXECAT); 22947c478bd9Sstevel@tonic-gate exit(1); 22957c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 22967c478bd9Sstevel@tonic-gate } 22977c478bd9Sstevel@tonic-gate 22987c478bd9Sstevel@tonic-gate static int 22997c478bd9Sstevel@tonic-gate idle(long t) 23007c478bd9Sstevel@tonic-gate { 23017c478bd9Sstevel@tonic-gate time_t now; 23027c478bd9Sstevel@tonic-gate 23037c478bd9Sstevel@tonic-gate while (t > 0L) { 23047c478bd9Sstevel@tonic-gate 23057c478bd9Sstevel@tonic-gate if (msg_wait(t) != 0) { 23067c478bd9Sstevel@tonic-gate /* we need to run next job immediately */ 23077c478bd9Sstevel@tonic-gate return (0); 23087c478bd9Sstevel@tonic-gate } 23097c478bd9Sstevel@tonic-gate 23107c478bd9Sstevel@tonic-gate reap_child(); 23117c478bd9Sstevel@tonic-gate 23127c478bd9Sstevel@tonic-gate now = time(NULL); 23137c478bd9Sstevel@tonic-gate if (last_time > now) { 23147c478bd9Sstevel@tonic-gate /* clock has been reset */ 23157c478bd9Sstevel@tonic-gate return (1); 23167c478bd9Sstevel@tonic-gate } 23177c478bd9Sstevel@tonic-gate 23187c478bd9Sstevel@tonic-gate if (next_event == NULL && !el_empty()) { 23197c478bd9Sstevel@tonic-gate next_event = (struct event *)el_first(); 23207c478bd9Sstevel@tonic-gate } 23217c478bd9Sstevel@tonic-gate if (next_event == NULL) 23227c478bd9Sstevel@tonic-gate t = INFINITY; 23237c478bd9Sstevel@tonic-gate else 23247c478bd9Sstevel@tonic-gate t = (long)next_event->time - now; 23257c478bd9Sstevel@tonic-gate } 23267c478bd9Sstevel@tonic-gate return (0); 23277c478bd9Sstevel@tonic-gate } 23287c478bd9Sstevel@tonic-gate 23297c478bd9Sstevel@tonic-gate /* 23307c478bd9Sstevel@tonic-gate * This used to be in the idle(), but moved to the separate function. 23317c478bd9Sstevel@tonic-gate * This called from various place when cron needs to reap the 23327c478bd9Sstevel@tonic-gate * child. It includes the situation that cron hit maxrun, and needs 23337c478bd9Sstevel@tonic-gate * to reschedule the job. 23347c478bd9Sstevel@tonic-gate */ 23357c478bd9Sstevel@tonic-gate static void 23367c478bd9Sstevel@tonic-gate reap_child() 23377c478bd9Sstevel@tonic-gate { 23387c478bd9Sstevel@tonic-gate pid_t pid; 23397c478bd9Sstevel@tonic-gate int prc; 23407c478bd9Sstevel@tonic-gate struct runinfo *rp; 23417c478bd9Sstevel@tonic-gate 23427c478bd9Sstevel@tonic-gate for (;;) { 23437c478bd9Sstevel@tonic-gate pid = waitpid((pid_t)-1, &prc, WNOHANG); 23447c478bd9Sstevel@tonic-gate if (pid <= 0) 23457c478bd9Sstevel@tonic-gate break; 23467c478bd9Sstevel@tonic-gate #ifdef DEBUG 23477c478bd9Sstevel@tonic-gate fprintf(stderr, 23487c478bd9Sstevel@tonic-gate "wait returned %x for process %d\n", prc, pid); 23497c478bd9Sstevel@tonic-gate #endif 23507c478bd9Sstevel@tonic-gate if ((rp = rinfo_get(pid)) == NULL) { 23517c478bd9Sstevel@tonic-gate if (miscpid_delete(pid) == 0) { 23527c478bd9Sstevel@tonic-gate /* not found in anywhere */ 23537c478bd9Sstevel@tonic-gate msg(PIDERR, pid); 23547c478bd9Sstevel@tonic-gate } 23557c478bd9Sstevel@tonic-gate } else if (rp->que == ZOMB) { 23567c478bd9Sstevel@tonic-gate (void) unlink(rp->outfile); 23577c478bd9Sstevel@tonic-gate rinfo_free(rp); 23587c478bd9Sstevel@tonic-gate } else { 23597c478bd9Sstevel@tonic-gate cleanup(rp, prc); 23607c478bd9Sstevel@tonic-gate } 23617c478bd9Sstevel@tonic-gate } 23627c478bd9Sstevel@tonic-gate } 23637c478bd9Sstevel@tonic-gate 23647c478bd9Sstevel@tonic-gate static void 23657c478bd9Sstevel@tonic-gate cleanup(struct runinfo *pr, int rc) 23667c478bd9Sstevel@tonic-gate { 23677c478bd9Sstevel@tonic-gate int nextfork = 1; 23687c478bd9Sstevel@tonic-gate struct usr *p; 23697c478bd9Sstevel@tonic-gate struct stat buf; 23707c478bd9Sstevel@tonic-gate 23717c478bd9Sstevel@tonic-gate logit(ECHAR, pr, rc); 23727c478bd9Sstevel@tonic-gate --qt[pr->que].nrun; 23737c478bd9Sstevel@tonic-gate p = pr->rusr; 23747c478bd9Sstevel@tonic-gate if (pr->que != CRONEVENT) 23757c478bd9Sstevel@tonic-gate --p->aruncnt; 23767c478bd9Sstevel@tonic-gate else 23777c478bd9Sstevel@tonic-gate --p->cruncnt; 23787c478bd9Sstevel@tonic-gate 23797c478bd9Sstevel@tonic-gate if (!lstat(pr->outfile, &buf)) { 23807c478bd9Sstevel@tonic-gate if ((buf.st_mode != S_IFLNK) && 23817c478bd9Sstevel@tonic-gate (buf.st_size > 0 || pr->mailwhendone)) { 23827c478bd9Sstevel@tonic-gate /* mail user stdout and stderr */ 23837c478bd9Sstevel@tonic-gate for (;;) { 23847c478bd9Sstevel@tonic-gate if ((pr->pid = fork()) < 0) { 23857c478bd9Sstevel@tonic-gate /* 23867c478bd9Sstevel@tonic-gate * if fork fails try forever in doubling 23877c478bd9Sstevel@tonic-gate * retry times, up to 16 seconds 23887c478bd9Sstevel@tonic-gate */ 23897c478bd9Sstevel@tonic-gate (void) sleep(nextfork); 23907c478bd9Sstevel@tonic-gate if (nextfork < 16) 23917c478bd9Sstevel@tonic-gate nextfork += nextfork; 23927c478bd9Sstevel@tonic-gate continue; 23937c478bd9Sstevel@tonic-gate } else if (pr->pid == 0) { 23947c478bd9Sstevel@tonic-gate child_sigreset(); 23957c478bd9Sstevel@tonic-gate contract_clear_template(); 23967c478bd9Sstevel@tonic-gate 23977c478bd9Sstevel@tonic-gate mail_result(p, pr, buf.st_size); 23987c478bd9Sstevel@tonic-gate /* NOTREACHED */ 23997c478bd9Sstevel@tonic-gate } else { 24007c478bd9Sstevel@tonic-gate contract_abandon_latest(pr->pid); 24017c478bd9Sstevel@tonic-gate pr->que = ZOMB; 24027c478bd9Sstevel@tonic-gate break; 24037c478bd9Sstevel@tonic-gate } 24047c478bd9Sstevel@tonic-gate } 24057c478bd9Sstevel@tonic-gate } else { 24067c478bd9Sstevel@tonic-gate (void) unlink(pr->outfile); 24077c478bd9Sstevel@tonic-gate rinfo_free(pr); 24087c478bd9Sstevel@tonic-gate } 24097c478bd9Sstevel@tonic-gate } else { 24107c478bd9Sstevel@tonic-gate rinfo_free(pr); 24117c478bd9Sstevel@tonic-gate } 24127c478bd9Sstevel@tonic-gate 24137c478bd9Sstevel@tonic-gate free_if_unused(p); 24147c478bd9Sstevel@tonic-gate } 24157c478bd9Sstevel@tonic-gate 24167c478bd9Sstevel@tonic-gate /* 24177c478bd9Sstevel@tonic-gate * Mail stdout and stderr of a job to user. Get uid for real user and become 24187c478bd9Sstevel@tonic-gate * that person. We do this so that mail won't come from root since this 24197c478bd9Sstevel@tonic-gate * could be a security hole. If failure, quit - don't send mail as root. 24207c478bd9Sstevel@tonic-gate */ 24217c478bd9Sstevel@tonic-gate static void 24227c478bd9Sstevel@tonic-gate mail_result(struct usr *p, struct runinfo *pr, size_t filesize) 24237c478bd9Sstevel@tonic-gate { 24247c478bd9Sstevel@tonic-gate struct passwd *ruser_ids; 24257c478bd9Sstevel@tonic-gate FILE *mailpipe; 24267c478bd9Sstevel@tonic-gate FILE *st; 24277c478bd9Sstevel@tonic-gate struct utsname name; 24287c478bd9Sstevel@tonic-gate int nbytes; 24297c478bd9Sstevel@tonic-gate char iobuf[BUFSIZ]; 24307c478bd9Sstevel@tonic-gate char *cmd; 24317c478bd9Sstevel@tonic-gate 24327c478bd9Sstevel@tonic-gate (void) uname(&name); 24337c478bd9Sstevel@tonic-gate if ((ruser_ids = getpwnam(p->name)) == NULL) 24347c478bd9Sstevel@tonic-gate exit(0); 24357c478bd9Sstevel@tonic-gate (void) setuid(ruser_ids->pw_uid); 24367c478bd9Sstevel@tonic-gate 24377c478bd9Sstevel@tonic-gate cmd = xmalloc(strlen(MAIL)+strlen(p->name)+2); 24387c478bd9Sstevel@tonic-gate (void) sprintf(cmd, "%s %s", MAIL, p->name); 24397c478bd9Sstevel@tonic-gate mailpipe = popen(cmd, "w"); 24407c478bd9Sstevel@tonic-gate contract_abandon_latest(0); 24417c478bd9Sstevel@tonic-gate free(cmd); 24427c478bd9Sstevel@tonic-gate if (mailpipe == NULL) 24437c478bd9Sstevel@tonic-gate exit(127); 24447c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "To: %s\n", p->name); 24457c478bd9Sstevel@tonic-gate if (pr->jobtype == CRONEVENT) { 24467c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, CRONOUT); 24477c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "Your \"cron\" job on %s\n", 24487c478bd9Sstevel@tonic-gate name.nodename); 24497c478bd9Sstevel@tonic-gate if (pr->jobname != NULL) { 24507c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "%s\n\n", pr->jobname); 24517c478bd9Sstevel@tonic-gate } 24527c478bd9Sstevel@tonic-gate } else { 24537c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "Subject: Output from \"at\" job\n\n"); 24547c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "Your \"at\" job on %s\n", 24557c478bd9Sstevel@tonic-gate name.nodename); 24567c478bd9Sstevel@tonic-gate if (pr->jobname != NULL) { 24577c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "\"%s\"\n\n", pr->jobname); 24587c478bd9Sstevel@tonic-gate } 24597c478bd9Sstevel@tonic-gate } 24607c478bd9Sstevel@tonic-gate /* Tmp. file is fopen'ed w/ "r", secure open */ 24617c478bd9Sstevel@tonic-gate if (filesize > 0 && 24627c478bd9Sstevel@tonic-gate (st = fopen(pr->outfile, "r")) != NULL) { 24637c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, 24647c478bd9Sstevel@tonic-gate "produced the following output:\n\n"); 24657c478bd9Sstevel@tonic-gate while ((nbytes = fread(iobuf, sizeof (char), BUFSIZ, st)) != 0) 24667c478bd9Sstevel@tonic-gate (void) fwrite(iobuf, sizeof (char), nbytes, mailpipe); 24677c478bd9Sstevel@tonic-gate (void) fclose(st); 24687c478bd9Sstevel@tonic-gate } else { 24697c478bd9Sstevel@tonic-gate (void) fprintf(mailpipe, "completed.\n"); 24707c478bd9Sstevel@tonic-gate } 24717c478bd9Sstevel@tonic-gate (void) pclose(mailpipe); 24727c478bd9Sstevel@tonic-gate exit(0); 24737c478bd9Sstevel@tonic-gate } 24747c478bd9Sstevel@tonic-gate 24757c478bd9Sstevel@tonic-gate static int 24767c478bd9Sstevel@tonic-gate msg_wait(long tim) 24777c478bd9Sstevel@tonic-gate { 24787c478bd9Sstevel@tonic-gate struct message msg; 24797c478bd9Sstevel@tonic-gate int cnt; 24807c478bd9Sstevel@tonic-gate time_t reftime; 24817c478bd9Sstevel@tonic-gate struct pollfd pfd[2]; 24827c478bd9Sstevel@tonic-gate int64_t tl; 24837c478bd9Sstevel@tonic-gate int timeout; 24847c478bd9Sstevel@tonic-gate static int pending_msg; 24857c478bd9Sstevel@tonic-gate static time_t pending_reftime; 24867c478bd9Sstevel@tonic-gate 24877c478bd9Sstevel@tonic-gate if (pending_msg) { 24887c478bd9Sstevel@tonic-gate process_msg(&msgbuf, pending_reftime); 24897c478bd9Sstevel@tonic-gate pending_msg = 0; 24907c478bd9Sstevel@tonic-gate return (0); 24917c478bd9Sstevel@tonic-gate } 24927c478bd9Sstevel@tonic-gate 24937c478bd9Sstevel@tonic-gate /* 24947c478bd9Sstevel@tonic-gate * We are opening the signal mask to receive SIGCLD. The notifypipe 24957c478bd9Sstevel@tonic-gate * is used to avoid race condition between SIGCLD and poll system 24967c478bd9Sstevel@tonic-gate * call. 24977c478bd9Sstevel@tonic-gate * If SIGCLD is delivered in poll(), poll will be interrupted, and 24987c478bd9Sstevel@tonic-gate * we will return to idle() to reap the dead children. 24997c478bd9Sstevel@tonic-gate * If SIGCLD is delivered between sigprocmask() below and poll(), 25007c478bd9Sstevel@tonic-gate * there is no way we can detect the SIGCLD because poll() won't 25017c478bd9Sstevel@tonic-gate * be interrupted. In such case, the dead children can't be wait'ed 25027c478bd9Sstevel@tonic-gate * until poll returns by timeout or a new job. To avoid this race 25037c478bd9Sstevel@tonic-gate * condition, child_handler write to the notifypipe, so that 25047c478bd9Sstevel@tonic-gate * poll() will be able to return with POLLIN which indicates that 25057c478bd9Sstevel@tonic-gate * we have received SIGCLD. 25067c478bd9Sstevel@tonic-gate * 25077c478bd9Sstevel@tonic-gate * Since the notifypipe is used to just let poll return from 25087c478bd9Sstevel@tonic-gate * system call, the data in the pipe won't be read. Therefore, 25097c478bd9Sstevel@tonic-gate * any data in the pipe needs to be flushed before opening signal 25107c478bd9Sstevel@tonic-gate * mask. 25117c478bd9Sstevel@tonic-gate * 25127c478bd9Sstevel@tonic-gate * Note that we can probably re-write this code with pselect() 25137c478bd9Sstevel@tonic-gate * which can handle this situation easily. 25147c478bd9Sstevel@tonic-gate */ 25157c478bd9Sstevel@tonic-gate (void) ioctl(notifypipe[0], I_FLUSH, FLUSHW); 25167c478bd9Sstevel@tonic-gate 25177c478bd9Sstevel@tonic-gate pfd[0].fd = msgfd; 25187c478bd9Sstevel@tonic-gate pfd[0].events = POLLIN; 25197c478bd9Sstevel@tonic-gate pfd[1].fd = notifypipe[1]; 25207c478bd9Sstevel@tonic-gate pfd[1].events = POLLIN; 25217c478bd9Sstevel@tonic-gate 25227c478bd9Sstevel@tonic-gate #ifdef CRON_MAXSLEEP 25237c478bd9Sstevel@tonic-gate /* 25247c478bd9Sstevel@tonic-gate * CRON_MAXSLEEP can be defined to have cron periodically wake 25257c478bd9Sstevel@tonic-gate * up, so that cron can detect a change of TOD and adjust the 25267c478bd9Sstevel@tonic-gate * sleep time accordingly. 25277c478bd9Sstevel@tonic-gate */ 25287c478bd9Sstevel@tonic-gate tim = (tim > CRON_MAXSLEEP) ? CRON_MAXSLEEP : tim; 25297c478bd9Sstevel@tonic-gate #endif 25307c478bd9Sstevel@tonic-gate tl = (tim == INFINITY) ? -1ll : (int64_t)tim * 1000; 25317c478bd9Sstevel@tonic-gate 25327c478bd9Sstevel@tonic-gate accept_sigcld = 1; 25337c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &childmask, NULL); 25347c478bd9Sstevel@tonic-gate do { 25357c478bd9Sstevel@tonic-gate timeout = (tl > INT_MAX ? INT_MAX : (int)tl); 25367c478bd9Sstevel@tonic-gate tl -= timeout; 25377c478bd9Sstevel@tonic-gate cnt = poll(pfd, 2, timeout); 25387c478bd9Sstevel@tonic-gate if (cnt == -1 && errno != EINTR) { 25397c478bd9Sstevel@tonic-gate perror("! poll"); 25407c478bd9Sstevel@tonic-gate } 25417c478bd9Sstevel@tonic-gate } while (tl > 0 && cnt == 0); 25427c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &childmask, NULL); 25437c478bd9Sstevel@tonic-gate accept_sigcld = 0; 25447c478bd9Sstevel@tonic-gate 25457c478bd9Sstevel@tonic-gate /* 25467c478bd9Sstevel@tonic-gate * poll timeout or interrupted. 25477c478bd9Sstevel@tonic-gate */ 25487c478bd9Sstevel@tonic-gate if (cnt <= 0) 25497c478bd9Sstevel@tonic-gate return (0); 25507c478bd9Sstevel@tonic-gate 25517c478bd9Sstevel@tonic-gate /* 25527c478bd9Sstevel@tonic-gate * Not the timeout or new job, but a SIGCLD has been delivered. 25537c478bd9Sstevel@tonic-gate */ 25547c478bd9Sstevel@tonic-gate if ((pfd[0].revents & POLLIN) == 0) 25557c478bd9Sstevel@tonic-gate return (0); 25567c478bd9Sstevel@tonic-gate 25577c478bd9Sstevel@tonic-gate errno = 0; 25587c478bd9Sstevel@tonic-gate if ((cnt = read(msgfd, &msg, sizeof (msg))) != sizeof (msg)) { 25597c478bd9Sstevel@tonic-gate if (cnt != -1 || errno != EAGAIN) 25607c478bd9Sstevel@tonic-gate perror("! read"); 25617c478bd9Sstevel@tonic-gate return (0); 25627c478bd9Sstevel@tonic-gate } 25637c478bd9Sstevel@tonic-gate reftime = time(NULL); 25647c478bd9Sstevel@tonic-gate if (next_event != NULL && reftime >= next_event->time) { 25657c478bd9Sstevel@tonic-gate /* 25667c478bd9Sstevel@tonic-gate * we need to run the job before reloading crontab. 25677c478bd9Sstevel@tonic-gate */ 25687c478bd9Sstevel@tonic-gate (void) memcpy(&msgbuf, &msg, sizeof (msg)); 25697c478bd9Sstevel@tonic-gate pending_msg = 1; 25707c478bd9Sstevel@tonic-gate pending_reftime = reftime; 25717c478bd9Sstevel@tonic-gate return (1); 25727c478bd9Sstevel@tonic-gate } 25737c478bd9Sstevel@tonic-gate process_msg(&msg, reftime); 25747c478bd9Sstevel@tonic-gate return (0); 25757c478bd9Sstevel@tonic-gate } 25767c478bd9Sstevel@tonic-gate 25777c478bd9Sstevel@tonic-gate /* 25787c478bd9Sstevel@tonic-gate * process the message supplied via pipe. This will be called either 25797c478bd9Sstevel@tonic-gate * immediately after cron read the message from pipe, or idle time 25807c478bd9Sstevel@tonic-gate * if the message was pending due to the job execution. 25817c478bd9Sstevel@tonic-gate */ 25827c478bd9Sstevel@tonic-gate static void 25837c478bd9Sstevel@tonic-gate process_msg(struct message *pmsg, time_t reftime) 25847c478bd9Sstevel@tonic-gate { 25857c478bd9Sstevel@tonic-gate if (pmsg->etype == NULL) 25867c478bd9Sstevel@tonic-gate return; 25877c478bd9Sstevel@tonic-gate 25887c478bd9Sstevel@tonic-gate switch (pmsg->etype) { 25897c478bd9Sstevel@tonic-gate case AT: 25907c478bd9Sstevel@tonic-gate if (pmsg->action == DELETE) 25917c478bd9Sstevel@tonic-gate del_atjob(pmsg->fname, pmsg->logname); 25927c478bd9Sstevel@tonic-gate else 25937c478bd9Sstevel@tonic-gate mod_atjob(pmsg->fname, (time_t)0); 25947c478bd9Sstevel@tonic-gate break; 25957c478bd9Sstevel@tonic-gate case CRON: 25967c478bd9Sstevel@tonic-gate if (pmsg->action == DELETE) 25977c478bd9Sstevel@tonic-gate del_ctab(pmsg->fname); 25987c478bd9Sstevel@tonic-gate else 25997c478bd9Sstevel@tonic-gate mod_ctab(pmsg->fname, reftime); 26007c478bd9Sstevel@tonic-gate break; 26017c478bd9Sstevel@tonic-gate default: 26027c478bd9Sstevel@tonic-gate msg("message received - bad format"); 26037c478bd9Sstevel@tonic-gate break; 26047c478bd9Sstevel@tonic-gate } 26057c478bd9Sstevel@tonic-gate if (next_event != NULL) { 26067c478bd9Sstevel@tonic-gate if (next_event->etype == CRONEVENT) 26077c478bd9Sstevel@tonic-gate el_add(next_event, next_event->time, 26087c478bd9Sstevel@tonic-gate (next_event->u)->ctid); 26097c478bd9Sstevel@tonic-gate else /* etype == ATEVENT */ 26107c478bd9Sstevel@tonic-gate el_add(next_event, next_event->time, 26117c478bd9Sstevel@tonic-gate next_event->of.at.eventid); 26127c478bd9Sstevel@tonic-gate next_event = NULL; 26137c478bd9Sstevel@tonic-gate } 26147c478bd9Sstevel@tonic-gate (void) fflush(stdout); 26157c478bd9Sstevel@tonic-gate pmsg->etype = NULL; 26167c478bd9Sstevel@tonic-gate } 26177c478bd9Sstevel@tonic-gate 2618*9f163834Sbasabi /* 2619*9f163834Sbasabi * Allocate a new or find an existing runinfo structure 2620*9f163834Sbasabi */ 26217c478bd9Sstevel@tonic-gate static struct runinfo * 26227c478bd9Sstevel@tonic-gate rinfo_get(pid_t pid) 26237c478bd9Sstevel@tonic-gate { 26247c478bd9Sstevel@tonic-gate struct runinfo *rp; 26257c478bd9Sstevel@tonic-gate 2626*9f163834Sbasabi if (pid == 0) { /* allocate a new entry */ 2627*9f163834Sbasabi rp = xcalloc(1, sizeof (struct runinfo)); 2628*9f163834Sbasabi rp->next = rthead; /* link the entry into the list */ 2629*9f163834Sbasabi rthead = rp; 2630*9f163834Sbasabi return (rp); 2631*9f163834Sbasabi } 2632*9f163834Sbasabi /* search the list for an existing entry */ 2633*9f163834Sbasabi for (rp = rthead; rp != NULL; rp = rp->next) { 26347c478bd9Sstevel@tonic-gate if (rp->pid == pid) 26357c478bd9Sstevel@tonic-gate break; 26367c478bd9Sstevel@tonic-gate } 2637*9f163834Sbasabi return (rp); 26387c478bd9Sstevel@tonic-gate } 26397c478bd9Sstevel@tonic-gate 26407c478bd9Sstevel@tonic-gate /* 2641*9f163834Sbasabi * Free a runinfo structure and its associated memory 26427c478bd9Sstevel@tonic-gate */ 26437c478bd9Sstevel@tonic-gate static void 2644*9f163834Sbasabi rinfo_free(struct runinfo *entry) 26457c478bd9Sstevel@tonic-gate { 2646*9f163834Sbasabi struct runinfo **rpp; 2647*9f163834Sbasabi struct runinfo *rp; 2648*9f163834Sbasabi 2649*9f163834Sbasabi #ifdef DEBUG 2650*9f163834Sbasabi (void) fprintf(stderr, "freeing job %s\n", entry->jobname); 2651*9f163834Sbasabi #endif 2652*9f163834Sbasabi for (rpp = &rthead; (rp = *rpp) != NULL; rpp = &rp->next) { 2653*9f163834Sbasabi if (rp == entry) { 2654*9f163834Sbasabi *rpp = rp->next; /* unlink the entry */ 2655*9f163834Sbasabi free(rp->outfile); 2656*9f163834Sbasabi free(rp->jobname); 2657*9f163834Sbasabi free(rp); 2658*9f163834Sbasabi break; 2659*9f163834Sbasabi } 2660*9f163834Sbasabi } 26617c478bd9Sstevel@tonic-gate } 26627c478bd9Sstevel@tonic-gate 26637c478bd9Sstevel@tonic-gate /* ARGSUSED */ 26647c478bd9Sstevel@tonic-gate static void 26657c478bd9Sstevel@tonic-gate thaw_handler(int sig) 26667c478bd9Sstevel@tonic-gate { 26677c478bd9Sstevel@tonic-gate ; 26687c478bd9Sstevel@tonic-gate } 26697c478bd9Sstevel@tonic-gate 26707c478bd9Sstevel@tonic-gate 26717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 26727c478bd9Sstevel@tonic-gate static void 26737c478bd9Sstevel@tonic-gate cronend(int sig) 26747c478bd9Sstevel@tonic-gate { 26757c478bd9Sstevel@tonic-gate crabort("SIGTERM", REMOVE_FIFO); 26767c478bd9Sstevel@tonic-gate } 26777c478bd9Sstevel@tonic-gate 26787c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 26797c478bd9Sstevel@tonic-gate static void 26807c478bd9Sstevel@tonic-gate child_handler(int sig) 26817c478bd9Sstevel@tonic-gate { 26827c478bd9Sstevel@tonic-gate /* 26837c478bd9Sstevel@tonic-gate * Just in case someone changes the signal mask. 26847c478bd9Sstevel@tonic-gate * we don't want to notify the SIGCLD. 26857c478bd9Sstevel@tonic-gate */ 26867c478bd9Sstevel@tonic-gate if (accept_sigcld) { 26877c478bd9Sstevel@tonic-gate (void) write(notifypipe[0], &sig, 1); 26887c478bd9Sstevel@tonic-gate } 26897c478bd9Sstevel@tonic-gate } 26907c478bd9Sstevel@tonic-gate 26917c478bd9Sstevel@tonic-gate static void 26927c478bd9Sstevel@tonic-gate child_sigreset(void) 26937c478bd9Sstevel@tonic-gate { 26947c478bd9Sstevel@tonic-gate (void) signal(SIGCLD, SIG_DFL); 26957c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &defmask, NULL); 26967c478bd9Sstevel@tonic-gate } 26977c478bd9Sstevel@tonic-gate 26987c478bd9Sstevel@tonic-gate /* 26997c478bd9Sstevel@tonic-gate * crabort() - handle exits out of cron 27007c478bd9Sstevel@tonic-gate */ 27017c478bd9Sstevel@tonic-gate static void 27027c478bd9Sstevel@tonic-gate crabort(char *mssg, int action) 27037c478bd9Sstevel@tonic-gate { 27047c478bd9Sstevel@tonic-gate int c; 27057c478bd9Sstevel@tonic-gate 27067c478bd9Sstevel@tonic-gate if (action & REMOVE_FIFO) { 27077c478bd9Sstevel@tonic-gate /* FIFO vanishes when cron finishes */ 27087c478bd9Sstevel@tonic-gate if (unlink(FIFO) < 0) 27097c478bd9Sstevel@tonic-gate perror("cron could not unlink FIFO"); 27107c478bd9Sstevel@tonic-gate } 27117c478bd9Sstevel@tonic-gate 27127c478bd9Sstevel@tonic-gate if (action & CONSOLE_MSG) { 27137c478bd9Sstevel@tonic-gate /* write error msg to console */ 27147c478bd9Sstevel@tonic-gate if ((c = open(CONSOLE, O_WRONLY)) >= 0) { 27157c478bd9Sstevel@tonic-gate (void) write(c, "cron aborted: ", 14); 27167c478bd9Sstevel@tonic-gate (void) write(c, mssg, strlen(mssg)); 27177c478bd9Sstevel@tonic-gate (void) write(c, "\n", 1); 27187c478bd9Sstevel@tonic-gate (void) close(c); 27197c478bd9Sstevel@tonic-gate } 27207c478bd9Sstevel@tonic-gate } 27217c478bd9Sstevel@tonic-gate 27227c478bd9Sstevel@tonic-gate /* always log the message */ 27237c478bd9Sstevel@tonic-gate msg(mssg); 27247c478bd9Sstevel@tonic-gate msg("******* CRON ABORTED ********"); 27257c478bd9Sstevel@tonic-gate exit(1); 27267c478bd9Sstevel@tonic-gate } 27277c478bd9Sstevel@tonic-gate 27287c478bd9Sstevel@tonic-gate /* 27297c478bd9Sstevel@tonic-gate * msg() - time-stamped error reporting function 27307c478bd9Sstevel@tonic-gate */ 27317c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 27327c478bd9Sstevel@tonic-gate static void 27337c478bd9Sstevel@tonic-gate msg(char *fmt, ...) 27347c478bd9Sstevel@tonic-gate { 27357c478bd9Sstevel@tonic-gate va_list args; 27367c478bd9Sstevel@tonic-gate time_t t; 27377c478bd9Sstevel@tonic-gate 27387c478bd9Sstevel@tonic-gate t = time(NULL); 27397c478bd9Sstevel@tonic-gate 27407c478bd9Sstevel@tonic-gate (void) fflush(stdout); 27417c478bd9Sstevel@tonic-gate 27427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "! "); 27437c478bd9Sstevel@tonic-gate 27447c478bd9Sstevel@tonic-gate va_start(args, fmt); 27457c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, args); 27467c478bd9Sstevel@tonic-gate va_end(args); 27477c478bd9Sstevel@tonic-gate 27487c478bd9Sstevel@tonic-gate (void) strftime(timebuf, sizeof (timebuf), FORMAT, localtime(&t)); 27497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " %s\n", timebuf); 27507c478bd9Sstevel@tonic-gate 27517c478bd9Sstevel@tonic-gate (void) fflush(stderr); 27527c478bd9Sstevel@tonic-gate } 27537c478bd9Sstevel@tonic-gate 27547c478bd9Sstevel@tonic-gate static void 27557c478bd9Sstevel@tonic-gate logit(int cc, struct runinfo *rp, int rc) 27567c478bd9Sstevel@tonic-gate { 27577c478bd9Sstevel@tonic-gate time_t t; 27587c478bd9Sstevel@tonic-gate int ret; 27597c478bd9Sstevel@tonic-gate 27607c478bd9Sstevel@tonic-gate if (!log) 27617c478bd9Sstevel@tonic-gate return; 27627c478bd9Sstevel@tonic-gate 27637c478bd9Sstevel@tonic-gate t = time(NULL); 27647c478bd9Sstevel@tonic-gate if (cc == BCHAR) 27657c478bd9Sstevel@tonic-gate (void) printf("%c CMD: %s\n", cc, next_event->cmd); 27667c478bd9Sstevel@tonic-gate (void) strftime(timebuf, sizeof (timebuf), FORMAT, localtime(&t)); 27677c478bd9Sstevel@tonic-gate (void) printf("%c %.8s %u %c %s", 27687c478bd9Sstevel@tonic-gate cc, (rp->rusr)->name, rp->pid, QUE(rp->que), timebuf); 27697c478bd9Sstevel@tonic-gate if ((ret = TSTAT(rc)) != 0) 27707c478bd9Sstevel@tonic-gate (void) printf(" ts=%d", ret); 27717c478bd9Sstevel@tonic-gate if ((ret = RCODE(rc)) != 0) 27727c478bd9Sstevel@tonic-gate (void) printf(" rc=%d", ret); 27737c478bd9Sstevel@tonic-gate (void) putchar('\n'); 27747c478bd9Sstevel@tonic-gate (void) fflush(stdout); 27757c478bd9Sstevel@tonic-gate } 27767c478bd9Sstevel@tonic-gate 27777c478bd9Sstevel@tonic-gate static void 27787c478bd9Sstevel@tonic-gate resched(int delay) 27797c478bd9Sstevel@tonic-gate { 27807c478bd9Sstevel@tonic-gate time_t nt; 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate /* run job at a later time */ 27837c478bd9Sstevel@tonic-gate nt = next_event->time + delay; 27847c478bd9Sstevel@tonic-gate if (next_event->etype == CRONEVENT) { 27857c478bd9Sstevel@tonic-gate next_event->time = next_time(next_event, (time_t)0); 27867c478bd9Sstevel@tonic-gate if (nt < next_event->time) 27877c478bd9Sstevel@tonic-gate next_event->time = nt; 27887c478bd9Sstevel@tonic-gate el_add(next_event, next_event->time, (next_event->u)->ctid); 27897c478bd9Sstevel@tonic-gate delayed = 1; 27907c478bd9Sstevel@tonic-gate msg("rescheduling a cron job"); 27917c478bd9Sstevel@tonic-gate return; 27927c478bd9Sstevel@tonic-gate } 27937c478bd9Sstevel@tonic-gate add_atevent(next_event->u, next_event->cmd, nt, next_event->etype); 27947c478bd9Sstevel@tonic-gate msg("rescheduling at job"); 27957c478bd9Sstevel@tonic-gate } 27967c478bd9Sstevel@tonic-gate 27977c478bd9Sstevel@tonic-gate static void 27987c478bd9Sstevel@tonic-gate quedefs(int action) 27997c478bd9Sstevel@tonic-gate { 28007c478bd9Sstevel@tonic-gate int i; 28017c478bd9Sstevel@tonic-gate int j; 28027c478bd9Sstevel@tonic-gate char qbuf[QBUFSIZ]; 28037c478bd9Sstevel@tonic-gate FILE *fd; 28047c478bd9Sstevel@tonic-gate 28057c478bd9Sstevel@tonic-gate /* set up default queue definitions */ 28067c478bd9Sstevel@tonic-gate for (i = 0; i < NQUEUE; i++) { 28077c478bd9Sstevel@tonic-gate qt[i].njob = qd.njob; 28087c478bd9Sstevel@tonic-gate qt[i].nice = qd.nice; 28097c478bd9Sstevel@tonic-gate qt[i].nwait = qd.nwait; 28107c478bd9Sstevel@tonic-gate } 28117c478bd9Sstevel@tonic-gate if (action == DEFAULT) 28127c478bd9Sstevel@tonic-gate return; 28137c478bd9Sstevel@tonic-gate if ((fd = fopen(QUEDEFS, "r")) == NULL) { 28147c478bd9Sstevel@tonic-gate msg("cannot open quedefs file"); 28157c478bd9Sstevel@tonic-gate msg("using default queue definitions"); 28167c478bd9Sstevel@tonic-gate return; 28177c478bd9Sstevel@tonic-gate } 28187c478bd9Sstevel@tonic-gate while (fgets(qbuf, QBUFSIZ, fd) != NULL) { 28197c478bd9Sstevel@tonic-gate if ((j = qbuf[0]-'a') < 0 || j >= NQUEUE || qbuf[1] != '.') 28207c478bd9Sstevel@tonic-gate continue; 28217c478bd9Sstevel@tonic-gate parsqdef(&qbuf[2]); 28227c478bd9Sstevel@tonic-gate qt[j].njob = qq.njob; 28237c478bd9Sstevel@tonic-gate qt[j].nice = qq.nice; 28247c478bd9Sstevel@tonic-gate qt[j].nwait = qq.nwait; 28257c478bd9Sstevel@tonic-gate } 28267c478bd9Sstevel@tonic-gate (void) fclose(fd); 28277c478bd9Sstevel@tonic-gate } 28287c478bd9Sstevel@tonic-gate 28297c478bd9Sstevel@tonic-gate static void 28307c478bd9Sstevel@tonic-gate parsqdef(char *name) 28317c478bd9Sstevel@tonic-gate { 28327c478bd9Sstevel@tonic-gate int i; 28337c478bd9Sstevel@tonic-gate 28347c478bd9Sstevel@tonic-gate qq = qd; 28357c478bd9Sstevel@tonic-gate while (*name) { 28367c478bd9Sstevel@tonic-gate i = 0; 28377c478bd9Sstevel@tonic-gate while (isdigit(*name)) { 28387c478bd9Sstevel@tonic-gate i *= 10; 28397c478bd9Sstevel@tonic-gate i += *name++ - '0'; 28407c478bd9Sstevel@tonic-gate } 28417c478bd9Sstevel@tonic-gate switch (*name++) { 28427c478bd9Sstevel@tonic-gate case JOBF: 28437c478bd9Sstevel@tonic-gate qq.njob = i; 28447c478bd9Sstevel@tonic-gate break; 28457c478bd9Sstevel@tonic-gate case NICEF: 28467c478bd9Sstevel@tonic-gate qq.nice = i; 28477c478bd9Sstevel@tonic-gate break; 28487c478bd9Sstevel@tonic-gate case WAITF: 28497c478bd9Sstevel@tonic-gate qq.nwait = i; 28507c478bd9Sstevel@tonic-gate break; 28517c478bd9Sstevel@tonic-gate } 28527c478bd9Sstevel@tonic-gate } 28537c478bd9Sstevel@tonic-gate } 28547c478bd9Sstevel@tonic-gate 28557c478bd9Sstevel@tonic-gate /* 28567c478bd9Sstevel@tonic-gate * defaults - read defaults from /etc/default/cron 28577c478bd9Sstevel@tonic-gate */ 28587c478bd9Sstevel@tonic-gate static void 28597c478bd9Sstevel@tonic-gate defaults() 28607c478bd9Sstevel@tonic-gate { 28617c478bd9Sstevel@tonic-gate int flags; 28627c478bd9Sstevel@tonic-gate char *deflog; 28637c478bd9Sstevel@tonic-gate char *hz, *tz; 28647c478bd9Sstevel@tonic-gate 28657c478bd9Sstevel@tonic-gate /* 28667c478bd9Sstevel@tonic-gate * get HZ value for environment 28677c478bd9Sstevel@tonic-gate */ 28687c478bd9Sstevel@tonic-gate if ((hz = getenv("HZ")) == (char *)NULL) 28697c478bd9Sstevel@tonic-gate (void) sprintf(hzname, "HZ=%d", HZ); 28707c478bd9Sstevel@tonic-gate else 28717c478bd9Sstevel@tonic-gate (void) snprintf(hzname, sizeof (hzname), "HZ=%s", hz); 28727c478bd9Sstevel@tonic-gate /* 28737c478bd9Sstevel@tonic-gate * get TZ value for environment 28747c478bd9Sstevel@tonic-gate */ 28757c478bd9Sstevel@tonic-gate (void) snprintf(tzone, sizeof (tzone), "TZ=%s", 28767c478bd9Sstevel@tonic-gate ((tz = getenv("TZ")) != NULL) ? tz : DEFTZ); 28777c478bd9Sstevel@tonic-gate 28787c478bd9Sstevel@tonic-gate if (defopen(DEFFILE) == 0) { 28797c478bd9Sstevel@tonic-gate /* ignore case */ 28807c478bd9Sstevel@tonic-gate flags = defcntl(DC_GETFLAGS, 0); 28817c478bd9Sstevel@tonic-gate TURNOFF(flags, DC_CASE); 28827c478bd9Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags); 28837c478bd9Sstevel@tonic-gate 28847c478bd9Sstevel@tonic-gate if (((deflog = defread("CRONLOG=")) == NULL) || 28857c478bd9Sstevel@tonic-gate (*deflog == 'N') || (*deflog == 'n')) 28867c478bd9Sstevel@tonic-gate log = 0; 28877c478bd9Sstevel@tonic-gate else 28887c478bd9Sstevel@tonic-gate log = 1; 28897c478bd9Sstevel@tonic-gate /* fix for 1087611 - allow paths to be set in defaults file */ 28907c478bd9Sstevel@tonic-gate if ((Def_path = defread("PATH=")) != NULL) { 28917c478bd9Sstevel@tonic-gate (void) strlcat(path, Def_path, LINE_MAX); 28927c478bd9Sstevel@tonic-gate } else { 28937c478bd9Sstevel@tonic-gate (void) strlcpy(path, NONROOTPATH, LINE_MAX); 28947c478bd9Sstevel@tonic-gate } 28957c478bd9Sstevel@tonic-gate if ((Def_supath = defread("SUPATH=")) != NULL) { 28967c478bd9Sstevel@tonic-gate (void) strlcat(supath, Def_supath, LINE_MAX); 28977c478bd9Sstevel@tonic-gate } else { 28987c478bd9Sstevel@tonic-gate (void) strlcpy(supath, ROOTPATH, LINE_MAX); 28997c478bd9Sstevel@tonic-gate } 29007c478bd9Sstevel@tonic-gate (void) defopen(NULL); 29017c478bd9Sstevel@tonic-gate } 29027c478bd9Sstevel@tonic-gate } 29037c478bd9Sstevel@tonic-gate 29047c478bd9Sstevel@tonic-gate /* 29057c478bd9Sstevel@tonic-gate * Determine if a user entry for a job is still ok. The method used here 29067c478bd9Sstevel@tonic-gate * is a lot (about 75x) faster than using setgrent() / getgrent() 29077c478bd9Sstevel@tonic-gate * endgrent(). It should be safe because we use the sysconf to determine 29087c478bd9Sstevel@tonic-gate * the max, and it tolerates the max being 0. 29097c478bd9Sstevel@tonic-gate */ 29107c478bd9Sstevel@tonic-gate 29117c478bd9Sstevel@tonic-gate static int 29127c478bd9Sstevel@tonic-gate verify_user_cred(const struct usr *u) 29137c478bd9Sstevel@tonic-gate { 29147c478bd9Sstevel@tonic-gate struct passwd *pw; 29157c478bd9Sstevel@tonic-gate size_t numUsrGrps = 0; 29167c478bd9Sstevel@tonic-gate size_t numOrigGrps = 0; 29177c478bd9Sstevel@tonic-gate size_t i; 29187c478bd9Sstevel@tonic-gate int retval; 29197c478bd9Sstevel@tonic-gate 29207c478bd9Sstevel@tonic-gate /* 29217c478bd9Sstevel@tonic-gate * Maximum number of groups a user may be in concurrently. This 29227c478bd9Sstevel@tonic-gate * is a value which we obtain at runtime through a sysconf() 29237c478bd9Sstevel@tonic-gate * call. 29247c478bd9Sstevel@tonic-gate */ 29257c478bd9Sstevel@tonic-gate 29267c478bd9Sstevel@tonic-gate static size_t nGroupsMax = (size_t)-1; 29277c478bd9Sstevel@tonic-gate 29287c478bd9Sstevel@tonic-gate /* 29297c478bd9Sstevel@tonic-gate * Arrays for cron user's group list, constructed at startup to 29307c478bd9Sstevel@tonic-gate * be nGroupsMax elements long, used for verifying user 29317c478bd9Sstevel@tonic-gate * credentials prior to execution. 29327c478bd9Sstevel@tonic-gate */ 29337c478bd9Sstevel@tonic-gate 29347c478bd9Sstevel@tonic-gate static gid_t *UsrGrps; 29357c478bd9Sstevel@tonic-gate static gid_t *OrigGrps; 29367c478bd9Sstevel@tonic-gate 29377c478bd9Sstevel@tonic-gate if (((pw = getpwnam(u->name)) == NULL) || 29387c478bd9Sstevel@tonic-gate (pw->pw_uid != u->uid)) { 29397c478bd9Sstevel@tonic-gate return (VUC_BADUSER); 29407c478bd9Sstevel@tonic-gate } 29417c478bd9Sstevel@tonic-gate 29427c478bd9Sstevel@tonic-gate /* 29437c478bd9Sstevel@tonic-gate * Create the group id lists needed for job credential 29447c478bd9Sstevel@tonic-gate * verification. 29457c478bd9Sstevel@tonic-gate */ 29467c478bd9Sstevel@tonic-gate 29477c478bd9Sstevel@tonic-gate if (nGroupsMax == (size_t)-1) { 29487c478bd9Sstevel@tonic-gate if ((nGroupsMax = sysconf(_SC_NGROUPS_MAX)) > 0) { 29497c478bd9Sstevel@tonic-gate UsrGrps = xcalloc(nGroupsMax, sizeof (gid_t)); 29507c478bd9Sstevel@tonic-gate OrigGrps = xcalloc(nGroupsMax, sizeof (gid_t)); 29517c478bd9Sstevel@tonic-gate } 29527c478bd9Sstevel@tonic-gate 29537c478bd9Sstevel@tonic-gate #ifdef DEBUG 29547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "nGroupsMax = %ld\n", nGroupsMax); 29557c478bd9Sstevel@tonic-gate #endif 29567c478bd9Sstevel@tonic-gate } 29577c478bd9Sstevel@tonic-gate 29587c478bd9Sstevel@tonic-gate #ifdef DEBUG 29597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "verify_user_cred (%s-%d)\n", pw->pw_name, 29607c478bd9Sstevel@tonic-gate pw->pw_uid); 29617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "verify_user_cred: pw->pw_gid = %d, " 29627c478bd9Sstevel@tonic-gate "u->gid = %d\n", pw->pw_gid, u->gid); 29637c478bd9Sstevel@tonic-gate #endif 29647c478bd9Sstevel@tonic-gate 29657c478bd9Sstevel@tonic-gate retval = (u->gid == pw->pw_gid) ? VUC_OK : VUC_NOTINGROUP; 29667c478bd9Sstevel@tonic-gate 29677c478bd9Sstevel@tonic-gate if (nGroupsMax > 0) { 29687c478bd9Sstevel@tonic-gate numOrigGrps = getgroups(nGroupsMax, OrigGrps); 29697c478bd9Sstevel@tonic-gate 29707c478bd9Sstevel@tonic-gate (void) initgroups(pw->pw_name, pw->pw_gid); 29717c478bd9Sstevel@tonic-gate numUsrGrps = getgroups(nGroupsMax, UsrGrps); 29727c478bd9Sstevel@tonic-gate 29737c478bd9Sstevel@tonic-gate for (i = 0; i < numUsrGrps; i++) { 29747c478bd9Sstevel@tonic-gate if (UsrGrps[i] == u->gid) { 29757c478bd9Sstevel@tonic-gate retval = VUC_OK; 29767c478bd9Sstevel@tonic-gate break; 29777c478bd9Sstevel@tonic-gate } 29787c478bd9Sstevel@tonic-gate } 29797c478bd9Sstevel@tonic-gate 29807c478bd9Sstevel@tonic-gate if (OrigGrps) { 29817c478bd9Sstevel@tonic-gate (void) setgroups(numOrigGrps, OrigGrps); 29827c478bd9Sstevel@tonic-gate } 29837c478bd9Sstevel@tonic-gate } 29847c478bd9Sstevel@tonic-gate 29857c478bd9Sstevel@tonic-gate #ifdef DEBUG 29867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "verify_user_cred: VUC = %d\n", retval); 29877c478bd9Sstevel@tonic-gate #endif 29887c478bd9Sstevel@tonic-gate 29897c478bd9Sstevel@tonic-gate return (retval); 29907c478bd9Sstevel@tonic-gate } 29917c478bd9Sstevel@tonic-gate 29927c478bd9Sstevel@tonic-gate static int 29937c478bd9Sstevel@tonic-gate set_user_cred(const struct usr *u, struct project *pproj) 29947c478bd9Sstevel@tonic-gate { 29957c478bd9Sstevel@tonic-gate static char *progname = "cron"; 29967c478bd9Sstevel@tonic-gate int r = 0, rval = 0; 29977c478bd9Sstevel@tonic-gate 29987c478bd9Sstevel@tonic-gate if ((r = pam_start(progname, u->name, &pam_conv, &pamh)) 29997c478bd9Sstevel@tonic-gate != PAM_SUCCESS) { 30007c478bd9Sstevel@tonic-gate #ifdef DEBUG 30017c478bd9Sstevel@tonic-gate msg("pam_start returns %d\n", r); 30027c478bd9Sstevel@tonic-gate #endif 30037c478bd9Sstevel@tonic-gate rval = VUC_BADUSER; 30047c478bd9Sstevel@tonic-gate goto set_eser_cred_exit; 30057c478bd9Sstevel@tonic-gate } 30067c478bd9Sstevel@tonic-gate 30077c478bd9Sstevel@tonic-gate r = pam_acct_mgmt(pamh, 0); 30087c478bd9Sstevel@tonic-gate #ifdef DEBUG 30097c478bd9Sstevel@tonic-gate msg("pam_acc_mgmt returns %d\n", r); 30107c478bd9Sstevel@tonic-gate #endif 30117c478bd9Sstevel@tonic-gate if (r == PAM_ACCT_EXPIRED) { 30127c478bd9Sstevel@tonic-gate rval = VUC_EXPIRED; 30137c478bd9Sstevel@tonic-gate goto set_eser_cred_exit; 30147c478bd9Sstevel@tonic-gate } 30157c478bd9Sstevel@tonic-gate if (r == PAM_NEW_AUTHTOK_REQD) { 30167c478bd9Sstevel@tonic-gate rval = VUC_NEW_AUTH; 30177c478bd9Sstevel@tonic-gate goto set_eser_cred_exit; 30187c478bd9Sstevel@tonic-gate } 30197c478bd9Sstevel@tonic-gate if (r != PAM_SUCCESS) { 30207c478bd9Sstevel@tonic-gate rval = VUC_BADUSER; 30217c478bd9Sstevel@tonic-gate goto set_eser_cred_exit; 30227c478bd9Sstevel@tonic-gate } 30237c478bd9Sstevel@tonic-gate 30247c478bd9Sstevel@tonic-gate if (pproj != NULL) { 30257c478bd9Sstevel@tonic-gate size_t sz = sizeof (PROJECT) + strlen(pproj->pj_name); 30267c478bd9Sstevel@tonic-gate char *buf = alloca(sz); 30277c478bd9Sstevel@tonic-gate 30287c478bd9Sstevel@tonic-gate (void) snprintf(buf, sz, PROJECT "%s", pproj->pj_name); 30297c478bd9Sstevel@tonic-gate (void) pam_set_item(pamh, PAM_RESOURCE, buf); 30307c478bd9Sstevel@tonic-gate } 30317c478bd9Sstevel@tonic-gate 30327c478bd9Sstevel@tonic-gate r = pam_setcred(pamh, PAM_ESTABLISH_CRED); 30337c478bd9Sstevel@tonic-gate if (r != PAM_SUCCESS) 30347c478bd9Sstevel@tonic-gate rval = VUC_BADUSER; 30357c478bd9Sstevel@tonic-gate 30367c478bd9Sstevel@tonic-gate set_eser_cred_exit: 30377c478bd9Sstevel@tonic-gate (void) pam_end(pamh, r); 30387c478bd9Sstevel@tonic-gate return (rval); 30397c478bd9Sstevel@tonic-gate } 30407c478bd9Sstevel@tonic-gate 30417c478bd9Sstevel@tonic-gate static void 30427c478bd9Sstevel@tonic-gate clean_out_user(struct usr *u) 30437c478bd9Sstevel@tonic-gate { 30447c478bd9Sstevel@tonic-gate if (next_event->u == u) { 30457c478bd9Sstevel@tonic-gate next_event = NULL; 30467c478bd9Sstevel@tonic-gate } 30477c478bd9Sstevel@tonic-gate 30487c478bd9Sstevel@tonic-gate clean_out_ctab(u); 30497c478bd9Sstevel@tonic-gate clean_out_atjobs(u); 30507c478bd9Sstevel@tonic-gate free_if_unused(u); 30517c478bd9Sstevel@tonic-gate } 30527c478bd9Sstevel@tonic-gate 30537c478bd9Sstevel@tonic-gate static void 30547c478bd9Sstevel@tonic-gate clean_out_atjobs(struct usr *u) 30557c478bd9Sstevel@tonic-gate { 30567c478bd9Sstevel@tonic-gate struct event *ev, *pv; 30577c478bd9Sstevel@tonic-gate 30587c478bd9Sstevel@tonic-gate for (pv = NULL, ev = u->atevents; 30597c478bd9Sstevel@tonic-gate ev != NULL; 30607c478bd9Sstevel@tonic-gate pv = ev, ev = ev->link, free(pv)) { 30617c478bd9Sstevel@tonic-gate el_remove(ev->of.at.eventid, 1); 30627c478bd9Sstevel@tonic-gate if (cwd == AT) 30637c478bd9Sstevel@tonic-gate cron_unlink(ev->cmd); 30647c478bd9Sstevel@tonic-gate else { 30657c478bd9Sstevel@tonic-gate char buf[PATH_MAX]; 30667c478bd9Sstevel@tonic-gate if (strlen(ATDIR) + strlen(ev->cmd) + 2 30677c478bd9Sstevel@tonic-gate < PATH_MAX) { 30687c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", ATDIR, ev->cmd); 30697c478bd9Sstevel@tonic-gate cron_unlink(buf); 30707c478bd9Sstevel@tonic-gate } 30717c478bd9Sstevel@tonic-gate } 30727c478bd9Sstevel@tonic-gate free(ev->cmd); 30737c478bd9Sstevel@tonic-gate } 30747c478bd9Sstevel@tonic-gate 30757c478bd9Sstevel@tonic-gate u->atevents = NULL; 30767c478bd9Sstevel@tonic-gate } 30777c478bd9Sstevel@tonic-gate 30787c478bd9Sstevel@tonic-gate static void 30797c478bd9Sstevel@tonic-gate clean_out_ctab(struct usr *u) 30807c478bd9Sstevel@tonic-gate { 30817c478bd9Sstevel@tonic-gate rm_ctevents(u); 30827c478bd9Sstevel@tonic-gate el_remove(u->ctid, 0); 30837c478bd9Sstevel@tonic-gate u->ctid = 0; 30847c478bd9Sstevel@tonic-gate u->ctexists = 0; 30857c478bd9Sstevel@tonic-gate } 30867c478bd9Sstevel@tonic-gate 30877c478bd9Sstevel@tonic-gate static void 30887c478bd9Sstevel@tonic-gate cron_unlink(char *name) 30897c478bd9Sstevel@tonic-gate { 30907c478bd9Sstevel@tonic-gate int r; 30917c478bd9Sstevel@tonic-gate 30927c478bd9Sstevel@tonic-gate r = unlink(name); 30937c478bd9Sstevel@tonic-gate if (r == 0 || (r == -1 && errno == ENOENT)) { 30947c478bd9Sstevel@tonic-gate (void) audit_cron_delete_anc_file(name, NULL); 30957c478bd9Sstevel@tonic-gate } 30967c478bd9Sstevel@tonic-gate } 30977c478bd9Sstevel@tonic-gate 30987c478bd9Sstevel@tonic-gate static void 30997c478bd9Sstevel@tonic-gate create_anc_ctab(struct event *e) 31007c478bd9Sstevel@tonic-gate { 31017c478bd9Sstevel@tonic-gate if (audit_cron_create_anc_file(e->u->name, 31027c478bd9Sstevel@tonic-gate (cwd == CRON) ? NULL:CRONDIR, 31037c478bd9Sstevel@tonic-gate e->u->name, 31047c478bd9Sstevel@tonic-gate e->u->uid) == -1) { 31057c478bd9Sstevel@tonic-gate process_anc_files(CRON_ANC_DELETE); 31067c478bd9Sstevel@tonic-gate crabort("cannot create ancillary files for crontabs", 31077c478bd9Sstevel@tonic-gate REMOVE_FIFO|CONSOLE_MSG); 31087c478bd9Sstevel@tonic-gate } 31097c478bd9Sstevel@tonic-gate } 31107c478bd9Sstevel@tonic-gate 31117c478bd9Sstevel@tonic-gate static void 31127c478bd9Sstevel@tonic-gate delete_anc_ctab(struct event *e) 31137c478bd9Sstevel@tonic-gate { 31147c478bd9Sstevel@tonic-gate (void) audit_cron_delete_anc_file(e->u->name, 31157c478bd9Sstevel@tonic-gate (cwd == CRON) ? NULL:CRONDIR); 31167c478bd9Sstevel@tonic-gate } 31177c478bd9Sstevel@tonic-gate 31187c478bd9Sstevel@tonic-gate static void 31197c478bd9Sstevel@tonic-gate create_anc_atjob(struct event *e) 31207c478bd9Sstevel@tonic-gate { 31217c478bd9Sstevel@tonic-gate if (!e->of.at.exists) 31227c478bd9Sstevel@tonic-gate return; 31237c478bd9Sstevel@tonic-gate 31247c478bd9Sstevel@tonic-gate if (audit_cron_create_anc_file(e->cmd, 31257c478bd9Sstevel@tonic-gate (cwd == AT) ? NULL:ATDIR, 31267c478bd9Sstevel@tonic-gate e->u->name, 31277c478bd9Sstevel@tonic-gate e->u->uid) == -1) { 31287c478bd9Sstevel@tonic-gate process_anc_files(CRON_ANC_DELETE); 31297c478bd9Sstevel@tonic-gate crabort("cannot create ancillary files for atjobs", 31307c478bd9Sstevel@tonic-gate REMOVE_FIFO|CONSOLE_MSG); 31317c478bd9Sstevel@tonic-gate } 31327c478bd9Sstevel@tonic-gate } 31337c478bd9Sstevel@tonic-gate 31347c478bd9Sstevel@tonic-gate static void 31357c478bd9Sstevel@tonic-gate delete_anc_atjob(struct event *e) 31367c478bd9Sstevel@tonic-gate { 31377c478bd9Sstevel@tonic-gate if (!e->of.at.exists) 31387c478bd9Sstevel@tonic-gate return; 31397c478bd9Sstevel@tonic-gate 31407c478bd9Sstevel@tonic-gate (void) audit_cron_delete_anc_file(e->cmd, 31417c478bd9Sstevel@tonic-gate (cwd == AT) ? NULL:ATDIR); 31427c478bd9Sstevel@tonic-gate } 31437c478bd9Sstevel@tonic-gate 31447c478bd9Sstevel@tonic-gate 31457c478bd9Sstevel@tonic-gate static void 31467c478bd9Sstevel@tonic-gate process_anc_files(int del) 31477c478bd9Sstevel@tonic-gate { 31487c478bd9Sstevel@tonic-gate struct usr *u = uhead; 31497c478bd9Sstevel@tonic-gate struct event *e; 31507c478bd9Sstevel@tonic-gate 31517c478bd9Sstevel@tonic-gate if (!audit_cron_mode()) 31527c478bd9Sstevel@tonic-gate return; 31537c478bd9Sstevel@tonic-gate 31547c478bd9Sstevel@tonic-gate for (;;) { 31557c478bd9Sstevel@tonic-gate if (u->ctexists && u->ctevents != NULL) { 31567c478bd9Sstevel@tonic-gate e = u->ctevents; 31577c478bd9Sstevel@tonic-gate for (;;) { 31587c478bd9Sstevel@tonic-gate if (del) 31597c478bd9Sstevel@tonic-gate delete_anc_ctab(e); 31607c478bd9Sstevel@tonic-gate else 31617c478bd9Sstevel@tonic-gate create_anc_ctab(e); 31627c478bd9Sstevel@tonic-gate if ((e = e->link) == NULL) 31637c478bd9Sstevel@tonic-gate break; 31647c478bd9Sstevel@tonic-gate } 31657c478bd9Sstevel@tonic-gate } 31667c478bd9Sstevel@tonic-gate 31677c478bd9Sstevel@tonic-gate if (u->atevents != NULL) { 31687c478bd9Sstevel@tonic-gate e = u->atevents; 31697c478bd9Sstevel@tonic-gate for (;;) { 31707c478bd9Sstevel@tonic-gate if (del) 31717c478bd9Sstevel@tonic-gate delete_anc_atjob(e); 31727c478bd9Sstevel@tonic-gate else 31737c478bd9Sstevel@tonic-gate create_anc_atjob(e); 31747c478bd9Sstevel@tonic-gate if ((e = e->link) == NULL) 31757c478bd9Sstevel@tonic-gate break; 31767c478bd9Sstevel@tonic-gate } 31777c478bd9Sstevel@tonic-gate } 31787c478bd9Sstevel@tonic-gate 31797c478bd9Sstevel@tonic-gate if ((u = u->nextusr) == NULL) 31807c478bd9Sstevel@tonic-gate break; 31817c478bd9Sstevel@tonic-gate } 31827c478bd9Sstevel@tonic-gate } 31837c478bd9Sstevel@tonic-gate 31847c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 31857c478bd9Sstevel@tonic-gate static int 31867c478bd9Sstevel@tonic-gate cron_conv(int num_msg, struct pam_message **msgs, 31877c478bd9Sstevel@tonic-gate struct pam_response **response, void *appdata_ptr) 31887c478bd9Sstevel@tonic-gate { 31897c478bd9Sstevel@tonic-gate struct pam_message **m = msgs; 31907c478bd9Sstevel@tonic-gate int i; 31917c478bd9Sstevel@tonic-gate 31927c478bd9Sstevel@tonic-gate for (i = 0; i < num_msg; i++) { 31937c478bd9Sstevel@tonic-gate switch (m[i]->msg_style) { 31947c478bd9Sstevel@tonic-gate case PAM_ERROR_MSG: 31957c478bd9Sstevel@tonic-gate case PAM_TEXT_INFO: 31967c478bd9Sstevel@tonic-gate if (m[i]->msg != NULL) { 31977c478bd9Sstevel@tonic-gate (void) msg("%s\n", m[i]->msg); 31987c478bd9Sstevel@tonic-gate } 31997c478bd9Sstevel@tonic-gate break; 32007c478bd9Sstevel@tonic-gate 32017c478bd9Sstevel@tonic-gate default: 32027c478bd9Sstevel@tonic-gate break; 32037c478bd9Sstevel@tonic-gate } 32047c478bd9Sstevel@tonic-gate } 32057c478bd9Sstevel@tonic-gate return (0); 32067c478bd9Sstevel@tonic-gate } 32077c478bd9Sstevel@tonic-gate 32087c478bd9Sstevel@tonic-gate /* 32097c478bd9Sstevel@tonic-gate * Cron creates process for other than job. Mail process is the 32107c478bd9Sstevel@tonic-gate * one which rinfo does not cover. Therefore, miscpid will keep 32117c478bd9Sstevel@tonic-gate * track of the pids executed from cron. Otherwise, we will see 32127c478bd9Sstevel@tonic-gate * "unexpected pid returned.." messages appear in the log file. 32137c478bd9Sstevel@tonic-gate */ 32147c478bd9Sstevel@tonic-gate static void 32157c478bd9Sstevel@tonic-gate miscpid_insert(pid_t pid) 32167c478bd9Sstevel@tonic-gate { 32177c478bd9Sstevel@tonic-gate struct miscpid *mp; 32187c478bd9Sstevel@tonic-gate 32197c478bd9Sstevel@tonic-gate mp = xmalloc(sizeof (*mp)); 32207c478bd9Sstevel@tonic-gate mp->pid = pid; 32217c478bd9Sstevel@tonic-gate mp->next = miscpid_head; 32227c478bd9Sstevel@tonic-gate miscpid_head = mp; 32237c478bd9Sstevel@tonic-gate } 32247c478bd9Sstevel@tonic-gate 32257c478bd9Sstevel@tonic-gate static int 32267c478bd9Sstevel@tonic-gate miscpid_delete(pid_t pid) 32277c478bd9Sstevel@tonic-gate { 32287c478bd9Sstevel@tonic-gate struct miscpid *mp, *omp; 32297c478bd9Sstevel@tonic-gate int found = 0; 32307c478bd9Sstevel@tonic-gate 32317c478bd9Sstevel@tonic-gate omp = NULL; 32327c478bd9Sstevel@tonic-gate for (mp = miscpid_head; mp != NULL; mp = mp->next) { 32337c478bd9Sstevel@tonic-gate if (mp->pid == pid) { 32347c478bd9Sstevel@tonic-gate found = 1; 32357c478bd9Sstevel@tonic-gate break; 32367c478bd9Sstevel@tonic-gate } 32377c478bd9Sstevel@tonic-gate omp = mp; 32387c478bd9Sstevel@tonic-gate } 32397c478bd9Sstevel@tonic-gate if (found) { 32407c478bd9Sstevel@tonic-gate if (omp != NULL) 32417c478bd9Sstevel@tonic-gate omp->next = mp->next; 32427c478bd9Sstevel@tonic-gate else 32437c478bd9Sstevel@tonic-gate miscpid_head = NULL; 32447c478bd9Sstevel@tonic-gate free(mp); 32457c478bd9Sstevel@tonic-gate } 32467c478bd9Sstevel@tonic-gate return (found); 32477c478bd9Sstevel@tonic-gate } 32487c478bd9Sstevel@tonic-gate 32497c478bd9Sstevel@tonic-gate /* 32507c478bd9Sstevel@tonic-gate * Establish contract terms such that all children are in abandoned 32517c478bd9Sstevel@tonic-gate * process contracts. 32527c478bd9Sstevel@tonic-gate */ 3253032624d5Sbasabi static void 3254032624d5Sbasabi contract_set_template(void) 32557c478bd9Sstevel@tonic-gate { 32567c478bd9Sstevel@tonic-gate int fd; 32577c478bd9Sstevel@tonic-gate 32587c478bd9Sstevel@tonic-gate if ((fd = open64(CTFS_ROOT "/process/template", O_RDWR)) < 0) 32597c478bd9Sstevel@tonic-gate crabort("cannot open process contract template", 32607c478bd9Sstevel@tonic-gate REMOVE_FIFO | CONSOLE_MSG); 32617c478bd9Sstevel@tonic-gate 32627c478bd9Sstevel@tonic-gate if (ct_pr_tmpl_set_param(fd, 0) || 32637c478bd9Sstevel@tonic-gate ct_tmpl_set_informative(fd, 0) || 32647c478bd9Sstevel@tonic-gate ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR)) 32657c478bd9Sstevel@tonic-gate crabort("cannot establish contract template terms", 32667c478bd9Sstevel@tonic-gate REMOVE_FIFO | CONSOLE_MSG); 32677c478bd9Sstevel@tonic-gate 32687c478bd9Sstevel@tonic-gate if (ct_tmpl_activate(fd)) 32697c478bd9Sstevel@tonic-gate crabort("cannot activate contract template", 32707c478bd9Sstevel@tonic-gate REMOVE_FIFO | CONSOLE_MSG); 32717c478bd9Sstevel@tonic-gate 32727c478bd9Sstevel@tonic-gate (void) close(fd); 32737c478bd9Sstevel@tonic-gate } 32747c478bd9Sstevel@tonic-gate 32757c478bd9Sstevel@tonic-gate /* 32767c478bd9Sstevel@tonic-gate * Clear active process contract template. 32777c478bd9Sstevel@tonic-gate */ 3278032624d5Sbasabi static void 3279032624d5Sbasabi contract_clear_template(void) 32807c478bd9Sstevel@tonic-gate { 32817c478bd9Sstevel@tonic-gate int fd; 32827c478bd9Sstevel@tonic-gate 32837c478bd9Sstevel@tonic-gate if ((fd = open64(CTFS_ROOT "/process/template", O_RDWR)) < 0) 32847c478bd9Sstevel@tonic-gate crabort("cannot open process contract template", 32857c478bd9Sstevel@tonic-gate REMOVE_FIFO | CONSOLE_MSG); 32867c478bd9Sstevel@tonic-gate 32877c478bd9Sstevel@tonic-gate if (ct_tmpl_clear(fd)) 32887c478bd9Sstevel@tonic-gate crabort("cannot clear contract template", 32897c478bd9Sstevel@tonic-gate REMOVE_FIFO | CONSOLE_MSG); 32907c478bd9Sstevel@tonic-gate 32917c478bd9Sstevel@tonic-gate (void) close(fd); 32927c478bd9Sstevel@tonic-gate } 32937c478bd9Sstevel@tonic-gate 32947c478bd9Sstevel@tonic-gate /* 32957c478bd9Sstevel@tonic-gate * Abandon latest process contract unconditionally. If we have leaked [some 32967c478bd9Sstevel@tonic-gate * critical amount], exit such that the kernel reaps our contracts. 32977c478bd9Sstevel@tonic-gate */ 32987c478bd9Sstevel@tonic-gate static void 32997c478bd9Sstevel@tonic-gate contract_abandon_latest(pid_t pid) 33007c478bd9Sstevel@tonic-gate { 33017c478bd9Sstevel@tonic-gate int r; 33027c478bd9Sstevel@tonic-gate ctid_t id; 33037c478bd9Sstevel@tonic-gate static uint_t cts_lost; 33047c478bd9Sstevel@tonic-gate 33057c478bd9Sstevel@tonic-gate if (cts_lost > MAX_LOST_CONTRACTS) 33067c478bd9Sstevel@tonic-gate crabort("repeated failure to abandon contracts", 33077c478bd9Sstevel@tonic-gate REMOVE_FIFO | CONSOLE_MSG); 33087c478bd9Sstevel@tonic-gate 33097c478bd9Sstevel@tonic-gate if (r = contract_latest(&id)) { 33107c478bd9Sstevel@tonic-gate if (pid == 0) 33117c478bd9Sstevel@tonic-gate msg("could not obtain latest contract from " 33127c478bd9Sstevel@tonic-gate "popen(3C): %s", strerror(r)); 33137c478bd9Sstevel@tonic-gate else 33147c478bd9Sstevel@tonic-gate msg("could not obtain latest contract for " 33157c478bd9Sstevel@tonic-gate "PID %ld: %s", pid, strerror(r)); 33167c478bd9Sstevel@tonic-gate cts_lost++; 33177c478bd9Sstevel@tonic-gate return; 33187c478bd9Sstevel@tonic-gate } 33197c478bd9Sstevel@tonic-gate 33207c478bd9Sstevel@tonic-gate if (r = contract_abandon_id(id)) { 33217c478bd9Sstevel@tonic-gate msg("could not abandon latest contract %ld: %s", id, 33227c478bd9Sstevel@tonic-gate strerror(r)); 33237c478bd9Sstevel@tonic-gate cts_lost++; 33247c478bd9Sstevel@tonic-gate return; 33257c478bd9Sstevel@tonic-gate } 33267c478bd9Sstevel@tonic-gate } 3327