Index: head/etc/mtree/BSD.root.dist =================================================================== --- head/etc/mtree/BSD.root.dist (revision 308138) +++ head/etc/mtree/BSD.root.dist (revision 308139) @@ -1,110 +1,112 @@ # $FreeBSD$ # # Please see the file src/etc/mtree/README before making changes to this file. # /set type=dir uname=root gname=wheel mode=0755 . bin .. boot defaults .. dtb .. firmware .. kernel .. modules .. zfs .. .. dev mode=0555 .. etc X11 .. autofs .. bluetooth .. casper .. + cron.d + .. defaults .. devd .. dma .. gss .. mail .. mtree .. newsyslog.conf.d .. ntp mode=0700 .. pam.d .. periodic daily .. monthly .. security .. weekly .. .. pkg .. ppp .. rc.conf.d .. rc.d .. security .. skel .. ssh .. ssl .. zfs .. .. lib casper .. geom .. .. libexec resolvconf .. .. media .. mnt .. proc mode=0555 .. rescue .. root .. sbin .. tmp mode=01777 .. usr .. var .. .. Index: head/usr.sbin/cron/cron/cron.8 =================================================================== --- head/usr.sbin/cron/cron/cron.8 (revision 308138) +++ head/usr.sbin/cron/cron/cron.8 (revision 308139) @@ -1,224 +1,228 @@ .\"/* Copyright 1988,1990,1993 by Paul Vixie .\" * All rights reserved .\" * .\" * Distribute freely, except: don't remove my name from the source or .\" * documentation (don't take credit for my work), mark your changes (don't .\" * get me blamed for your possible bugs), don't alter or remove this .\" * notice. May be sold if buildable source is provided to buyer. No .\" * warrantee of any kind, express or implied, is included with this .\" * software; use at your own risk, responsibility for damages (if any) to .\" * anyone resulting from the use of this software rests entirely with the .\" * user. .\" * .\" * Send bug reports, bug fixes, enhancements, requests, flames, etc., and .\" * I'll try to keep a version up to date. I can be reached as follows: .\" * Paul Vixie uunet!decwrl!vixie!paul .\" */ .\" .\" $FreeBSD$ .\" -.Dd August 21, 2016 +.Dd Octobre 31, 2016 .Dt CRON 8 .Os .Sh NAME .Nm cron .Nd daemon to execute scheduled commands (Vixie Cron) .Sh SYNOPSIS .Nm .Op Fl j Ar jitter .Op Fl J Ar rootjitter .Op Fl m Ar mailto .Op Fl n .Op Fl s .Op Fl o .Op Fl x Ar debugflag Ns Op , Ns Ar ... .Sh DESCRIPTION The .Nm utility should be started from .Pa /etc/rc or .Pa /etc/rc.local . It will return immediately, so you do not need to start it with '&'. .Pp The .Nm utility searches .Pa /var/cron/tabs for crontab files which are named after accounts in .Pa /etc/passwd ; crontabs found are loaded into memory. The .Nm utility also searches for .Pa /etc/crontab -which is in a different format (see +and files in +.Pa /etc/cron.d +and +.Pa /usr/local/etc/cron.d +which are in a different format (see .Xr crontab 5 ) . .Pp The .Nm utility then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. Before running a command from a per-account crontab file, .Nm checks the status of the account with .Xr pam 3 and skips the command if the account is unavailable, e.g., locked out or expired. Commands from .Pa /etc/crontab bypass this check. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the .Ev MAILTO environment variable in the crontab, if such exists). .Pp Additionally, .Nm checks each minute to see if its spool directory's modification time (or the modification time on .Pa /etc/crontab ) has changed, and if it has, .Nm will then examine the modification time on all crontabs and reload those which have changed. Thus .Nm need not be restarted whenever a crontab file is modified. Note that the .Xr crontab 1 command updates the modification time of the spool directory whenever it changes a crontab. .Pp Available options: .Bl -tag -width indent .It Fl j Ar jitter Enable time jitter. Prior to executing commands, .Nm will sleep a random number of seconds in the range from 0 to .Ar jitter . This will not affect superuser jobs (see .Fl J ) . A value for .Ar jitter must be between 0 and 60 inclusive. Default is 0, which effectively disables time jitter. .Pp This option can help to smooth down system load spikes during moments when a lot of jobs are likely to start at once, e.g., at the beginning of the first minute of each hour. .It Fl J Ar rootjitter Enable time jitter for superuser jobs. The same as .Fl j except that it will affect jobs run by the superuser only. .It Fl m Ar mailto Overrides the default recipient for .Nm mail. Each .Xr crontab 5 without .Ev MAILTO explicitly set will send mail to the .Ar mailto mailbox. Sending mail will be disabled by default if .Ar mailto set to a null string, usually specified in a shell as .Li '' or .Li \*q\*q . .It Fl n Don't daemonize, run in foreground instead. .It Fl s Enable special handling of situations when the GMT offset of the local timezone changes, such as the switches between the standard time and daylight saving time. .Pp The jobs run during the GMT offset changes time as intuitively expected. If a job falls into a time interval that disappears (for example, during the switch from standard time) to daylight saving time or is duplicated (for example, during the reverse switch), then it is handled in one of two ways: .Pp The first case is for the jobs that run every at hour of a time interval overlapping with the disappearing or duplicated interval. In other words, if the job had run within one hour before the GMT offset change (and cron was not restarted nor the .Xr crontab 5 changed after that) or would run after the change at the next hour. They work as always, skip the skipped time or run in the added time as usual. .Pp The second case is for the jobs that run less frequently. They are executed exactly once, they are not skipped nor executed twice (unless cron is restarted or the user's .Xr crontab 5 is changed during such a time interval). If an interval disappears due to the GMT offset change, such jobs are executed at the same absolute point of time as they would be in the old time zone. For example, if exactly one hour disappears, this point would be during the next hour at the first minute that is specified for them in .Xr crontab 5 . .It Fl o Disable the special handling of situations when the GMT offset of the local timezone changes, to be compatible with the old (default) behavior. If both options .Fl o and .Fl s are specified, the option specified last wins. .It Fl x Ar debugflag Ns Op , Ns Ar ... Enable writing of debugging information to standard output. One or more of the following comma separated .Ar debugflag identifiers must be specified: .Pp .Bl -tag -width ".Cm proc" -compact .It Cm bit currently not used .It Cm ext make the other debug flags more verbose .It Cm load be verbose when loading crontab files .It Cm misc be verbose about miscellaneous one-off events .It Cm pars be verbose about parsing individual crontab lines .It Cm proc be verbose about the state of the process, including all of its offspring .It Cm sch be verbose when iterating through the scheduling algorithms .It Cm test trace through the execution, but do not perform any actions .El .El .Sh FILES .Bl -tag -width /etc/pam.d/cron -compact .It Pa /etc/crontab System crontab file .It Pa /etc/pam.d/cron .Xr pam.conf 5 configuration file for .Nm .It Pa /var/cron/tabs Directory for personal crontab files .El .Sh SEE ALSO .Xr crontab 1 , .Xr pam 3 , .Xr crontab 5 , .Xr pam.conf 5 .Sh AUTHORS .An Paul Vixie Aq Mt paul@vix.com Index: head/usr.sbin/cron/cron/cron.h =================================================================== --- head/usr.sbin/cron/cron/cron.h (revision 308138) +++ head/usr.sbin/cron/cron/cron.h (revision 308139) @@ -1,306 +1,306 @@ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ /* cron.h - header for vixie's cron * * $FreeBSD$ * * vix 14nov88 [rest of log is in RCS] * vix 14jan87 [0 or 7 can be sunday; thanks, mwm@berkeley] * vix 30dec86 [written] */ /* reorder these #include's at your peril */ #include #include #include "compat.h" #include #include #include #include #include #include #include #include #include #include #include "pathnames.h" #include "config.h" #include "externs.h" /* these are really immutable, and are * defined for symbolic convenience only * TRUE, FALSE, and ERR must be distinct * ERR must be < OK. */ #define TRUE 1 #define FALSE 0 /* system calls return this on success */ #define OK 0 /* or this on error */ #define ERR (-1) /* turn this on to get '-x' code */ #ifndef DEBUGGING #define DEBUGGING FALSE #endif #define READ_PIPE 0 /* which end of a pipe pair do you read? */ #define WRITE_PIPE 1 /* or write to? */ #define STDIN 0 /* what is stdin's file descriptor? */ #define STDOUT 1 /* stdout's? */ #define STDERR 2 /* stderr's? */ #define ERROR_EXIT 1 /* exit() with this will scare the shell */ #define OK_EXIT 0 /* exit() with this is considered 'normal' */ #define MAX_FNAME 100 /* max length of internally generated fn */ #define MAX_COMMAND 1000 /* max length of internally generated cmd */ #define MAX_ENVSTR 1000 /* max length of envvar=value\0 strings */ #define MAX_TEMPSTR 100 /* obvious */ #define ROOT_UID 0 /* don't change this, it really must be root */ #define ROOT_USER "root" /* ditto */ #define SYS_NAME "*system*" /* magic owner name for system crontab */ /* NOTE: these correspond to DebugFlagNames, * defined below. */ #define DEXT 0x0001 /* extend flag for other debug masks */ #define DSCH 0x0002 /* scheduling debug mask */ #define DPROC 0x0004 /* process control debug mask */ #define DPARS 0x0008 /* parsing debug mask */ #define DLOAD 0x0010 /* database loading debug mask */ #define DMISC 0x0020 /* misc debug mask */ #define DTEST 0x0040 /* test mode: don't execute any commands */ #define DBIT 0x0080 /* bit twiddling shown (long) */ #define CRON_TAB(u) "%s/%s", SPOOL_DIR, u #define REG register #define PPC_NULL ((char **)NULL) #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 256 #endif #define Skip_Blanks(c, f) \ while (c == '\t' || c == ' ') \ c = get_char(f); #define Skip_Nonblanks(c, f) \ while (c!='\t' && c!=' ' && c!='\n' && c != EOF) \ c = get_char(f); #define Skip_Line(c, f) \ do {c = get_char(f);} while (c != '\n' && c != EOF); #if DEBUGGING # define Debug(mask, message) \ if ( (DebugFlags & (mask) ) == (mask) ) \ printf message; #else /* !DEBUGGING */ # define Debug(mask, message) \ ; #endif /* DEBUGGING */ #define MkLower(ch) (isupper(ch) ? tolower(ch) : ch) #define MkUpper(ch) (islower(ch) ? toupper(ch) : ch) #define Set_LineNum(ln) {Debug(DPARS|DEXT,("linenum=%d\n",ln)); \ LineNumber = ln; \ } #define FIRST_SECOND 0 #define LAST_SECOND 59 #define SECOND_COUNT (LAST_SECOND - FIRST_SECOND + 1) #define FIRST_MINUTE 0 #define LAST_MINUTE 59 #define MINUTE_COUNT (LAST_MINUTE - FIRST_MINUTE + 1) #define FIRST_HOUR 0 #define LAST_HOUR 23 #define HOUR_COUNT (LAST_HOUR - FIRST_HOUR + 1) #define FIRST_DOM 1 #define LAST_DOM 31 #define DOM_COUNT (LAST_DOM - FIRST_DOM + 1) #define FIRST_MONTH 1 #define LAST_MONTH 12 #define MONTH_COUNT (LAST_MONTH - FIRST_MONTH + 1) /* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */ #define FIRST_DOW 0 #define LAST_DOW 7 #define DOW_COUNT (LAST_DOW - FIRST_DOW + 1) #ifdef LOGIN_CAP /* see init.c */ #define RESOURCE_RC "daemon" #endif /* each user's crontab will be held as a list of * the following structure. * * These are the cron commands. */ typedef struct _entry { struct _entry *next; uid_t uid; gid_t gid; #ifdef LOGIN_CAP char *class; #endif char **envp; char *cmd; bitstr_t bit_decl(second, SECOND_COUNT); bitstr_t bit_decl(minute, MINUTE_COUNT); bitstr_t bit_decl(hour, HOUR_COUNT); bitstr_t bit_decl(dom, DOM_COUNT); bitstr_t bit_decl(month, MONTH_COUNT); bitstr_t bit_decl(dow, DOW_COUNT); int flags; #define DOM_STAR 0x01 #define DOW_STAR 0x02 #define WHEN_REBOOT 0x04 #define RUN_AT 0x08 #define NOT_UNTIL 0x10 #define SEC_RES 0x20 time_t lastrun; } entry; /* the crontab database will be a list of the * following structure, one element per user * plus one for the system. * * These are the crontabs. */ typedef struct _user { struct _user *next, *prev; /* links */ char *name; time_t mtime; /* last modtime of crontab */ entry *crontab; /* this person's crontab */ } user; typedef struct _cron_db { user *head, *tail; /* links */ time_t mtime; /* last modtime on spooldir */ } cron_db; void set_cron_uid(void), set_cron_cwd(void), load_database(cron_db *), open_logfile(void), sigpipe_func(void), job_add(entry *, user *), do_command(entry *, user *), link_user(cron_db *, user *), unlink_user(cron_db *, user *), free_user(user *), env_free(char **), unget_char(int, FILE *), free_entry(entry *), skip_comments(FILE *), - log_it(char *, int, char *, char *), + log_it(char *, int, char *, const char *), log_close(void); int job_runqueue(void), set_debug_flags(char *), get_char(FILE *), get_string(char *, int, FILE *, char *), swap_uids(void), swap_uids_back(void), load_env(char *, FILE *), cron_pclose(FILE *), strcmp_until(char *, char *, int), allowed(char *), strdtb(char *); char *env_get(char *, char **), *arpadate(time_t *), *mkprints(unsigned char *, unsigned int), *first_word(char *, char *), **env_init(void), **env_copy(char **), **env_set(char **, char *); user *load_user(int, struct passwd *, char *), *find_user(cron_db *, char *); entry *load_entry(FILE *, void (*)(char *), struct passwd *, char **); FILE *cron_popen(char *, char *, entry *); /* in the C tradition, we only create * variables for the main program, just * extern them elsewhere. */ #ifdef MAIN_PROGRAM # if !defined(LINT) && !defined(lint) char *copyright[] = { "@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie", "@(#) All rights reserved" }; # endif char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL }; char *DowNames[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", NULL }; char *ProgramName, *defmailto; int LineNumber; unsigned Jitter, RootJitter; time_t TargetTime; # if DEBUGGING int DebugFlags; char *DebugFlagNames[] = { /* sync with #defines */ "ext", "sch", "proc", "pars", "load", "misc", "test", "bit", NULL /* NULL must be last element */ }; # endif /* DEBUGGING */ #else /*MAIN_PROGRAM*/ extern char *copyright[], *MonthNames[], *DowNames[], *ProgramName, *defmailto; extern int LineNumber; extern unsigned Jitter, RootJitter; extern time_t TargetTime; extern struct pidfh *pfh; # if DEBUGGING extern int DebugFlags; extern char *DebugFlagNames[]; # endif /* DEBUGGING */ #endif /*MAIN_PROGRAM*/ Index: head/usr.sbin/cron/cron/database.c =================================================================== --- head/usr.sbin/cron/cron/database.c (revision 308138) +++ head/usr.sbin/cron/cron/database.c (revision 308139) @@ -1,263 +1,305 @@ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ #if !defined(lint) && !defined(LINT) static const char rcsid[] = "$FreeBSD$"; #endif /* vix 26jan87 [RCS has the log] */ #include "cron.h" #include #include #include #define TMAX(a,b) ((a)>(b)?(a):(b)) static void process_crontab(char *, char *, char *, struct stat *, cron_db *, cron_db *); void load_database(old_db) cron_db *old_db; { DIR *dir; struct stat statbuf; struct stat syscron_stat; + time_t maxmtime; DIR_T *dp; cron_db new_db; user *u, *nu; + struct { + const char *name; + struct stat st; + } syscrontabs [] = { + { SYSCRONTABS }, + { LOCALSYSCRONTABS } + }; + int i; Debug(DLOAD, ("[%d] load_database()\n", getpid())) /* before we start loading any data, do a stat on SPOOL_DIR * so that if anything changes as of this moment (i.e., before we've * cached any of the database), we'll see the changes next time. */ if (stat(SPOOL_DIR, &statbuf) < OK) { log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR); (void) exit(ERROR_EXIT); } /* track system crontab file */ if (stat(SYSCRONTAB, &syscron_stat) < OK) syscron_stat.st_mtime = 0; + maxmtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime); + + for (i = 0; i < nitems(syscrontabs); i++) { + if (stat(syscrontabs[i].name, &syscrontabs[i].st) != -1) { + maxmtime = TMAX(syscrontabs[i].st.st_mtime, maxmtime); + } else { + syscrontabs[i].st.st_mtime = 0; + } + } + /* if spooldir's mtime has not changed, we don't need to fiddle with * the database. * * Note that old_db->mtime is initialized to 0 in main(), and * so is guaranteed to be different than the stat() mtime the first * time this function is called. */ - if (old_db->mtime == TMAX(statbuf.st_mtime, syscron_stat.st_mtime)) { + if (old_db->mtime == maxmtime) { Debug(DLOAD, ("[%d] spool dir mtime unch, no load needed.\n", getpid())) return; } /* something's different. make a new database, moving unchanged * elements from the old database, reloading elements that have * actually changed. Whatever is left in the old database when * we're done is chaff -- crontabs that disappeared. */ - new_db.mtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime); + new_db.mtime = maxmtime; new_db.head = new_db.tail = NULL; if (syscron_stat.st_mtime) { process_crontab("root", SYS_NAME, SYSCRONTAB, &syscron_stat, &new_db, old_db); + } + + for (i = 0; i < nitems(syscrontabs); i++) { + char tabname[MAXPATHLEN]; + if (syscrontabs[i].st.st_mtime == 0) + continue; + if (!(dir = opendir(syscrontabs[i].name))) { + log_it("CRON", getpid(), "OPENDIR FAILED", + syscrontabs[i].name); + (void) exit(ERROR_EXIT); + } + + while (NULL != (dp = readdir(dir))) { + if (dp->d_name[0] == '.') + continue; + if (dp->d_type != DT_REG) + continue; + snprintf(tabname, sizeof(tabname), "%s/%s", + syscrontabs[i].name, dp->d_name); + process_crontab("root", SYS_NAME, tabname, + &syscrontabs[i].st, &new_db, old_db); + } + closedir(dir); } /* we used to keep this dir open all the time, for the sake of * efficiency. however, we need to close it in every fork, and * we fork a lot more often than the mtime of the dir changes. */ if (!(dir = opendir(SPOOL_DIR))) { log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR); (void) exit(ERROR_EXIT); } while (NULL != (dp = readdir(dir))) { char fname[MAXNAMLEN+1], tabname[MAXNAMLEN+1]; /* avoid file names beginning with ".". this is good * because we would otherwise waste two guaranteed calls * to getpwnam() for . and .., and also because user names * starting with a period are just too nasty to consider. */ if (dp->d_name[0] == '.') continue; (void) strncpy(fname, dp->d_name, sizeof(fname)); fname[sizeof(fname)-1] = '\0'; (void) snprintf(tabname, sizeof tabname, CRON_TAB(fname)); process_crontab(fname, fname, tabname, &statbuf, &new_db, old_db); } closedir(dir); /* if we don't do this, then when our children eventually call * getpwnam() in do_command.c's child_process to verify MAILTO=, * they will screw us up (and v-v). */ endpwent(); /* whatever's left in the old database is now junk. */ Debug(DLOAD, ("unlinking old database:\n")) for (u = old_db->head; u != NULL; u = nu) { Debug(DLOAD, ("\t%s\n", u->name)) nu = u->next; unlink_user(old_db, u); free_user(u); } /* overwrite the database control block with the new one. */ *old_db = new_db; Debug(DLOAD, ("load_database is done\n")) } void link_user(db, u) cron_db *db; user *u; { if (db->head == NULL) db->head = u; if (db->tail) db->tail->next = u; u->prev = db->tail; u->next = NULL; db->tail = u; } void unlink_user(db, u) cron_db *db; user *u; { if (u->prev == NULL) db->head = u->next; else u->prev->next = u->next; if (u->next == NULL) db->tail = u->prev; else u->next->prev = u->prev; } user * find_user(db, name) cron_db *db; char *name; { char *env_get(); user *u; for (u = db->head; u != NULL; u = u->next) if (!strcmp(u->name, name)) break; return u; } static void process_crontab(uname, fname, tabname, statbuf, new_db, old_db) char *uname; char *fname; char *tabname; struct stat *statbuf; cron_db *new_db; cron_db *old_db; { struct passwd *pw = NULL; int crontab_fd = OK - 1; user *u; if (strcmp(fname, SYS_NAME) && !(pw = getpwnam(uname))) { /* file doesn't have a user in passwd file. */ log_it(fname, getpid(), "ORPHAN", "no passwd entry"); goto next_crontab; } if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) { /* crontab not accessible? */ log_it(fname, getpid(), "CAN'T OPEN", tabname); goto next_crontab; } if (fstat(crontab_fd, statbuf) < OK) { log_it(fname, getpid(), "FSTAT FAILED", tabname); goto next_crontab; } Debug(DLOAD, ("\t%s:", fname)) u = find_user(old_db, fname); if (u != NULL) { /* if crontab has not changed since we last read it * in, then we can just use our existing entry. */ if (u->mtime == statbuf->st_mtime) { Debug(DLOAD, (" [no change, using old data]")) unlink_user(old_db, u); link_user(new_db, u); goto next_crontab; } /* before we fall through to the code that will reload * the user, let's deallocate and unlink the user in * the old database. This is more a point of memory * efficiency than anything else, since all leftover * users will be deleted from the old database when * we finish with the crontab... */ Debug(DLOAD, (" [delete old data]")) unlink_user(old_db, u); free_user(u); log_it(fname, getpid(), "RELOAD", tabname); } u = load_user(crontab_fd, pw, fname); if (u != NULL) { u->mtime = statbuf->st_mtime; link_user(new_db, u); } next_crontab: if (crontab_fd >= OK) { Debug(DLOAD, (" [done]\n")) close(crontab_fd); } } Index: head/usr.sbin/cron/cron/pathnames.h =================================================================== --- head/usr.sbin/cron/cron/pathnames.h (revision 308138) +++ head/usr.sbin/cron/cron/pathnames.h (revision 308139) @@ -1,81 +1,83 @@ /* Copyright 1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ /* * $FreeBSD$ */ #if (defined(BSD)) && (BSD >= 199103) || defined(__linux) || defined(AIX) # include #endif /*BSD*/ #ifndef CRONDIR /* CRONDIR is where crond(8) and crontab(1) both chdir * to; SPOOL_DIR, ALLOW_FILE, DENY_FILE, and LOG_FILE * are all relative to this directory. */ #define CRONDIR "/var/cron" #endif /* SPOOLDIR is where the crontabs live. * This directory will have its modtime updated * whenever crontab(1) changes a crontab; this is * the signal for crond(8) to look at each individual * crontab file and reload those whose modtimes are * newer than they were last time around (or which * didn't exist last time around...) */ #define SPOOL_DIR "tabs" /* undefining these turns off their features. note * that ALLOW_FILE and DENY_FILE must both be defined * in order to enable the allow/deny code. If neither * LOG_FILE or SYSLOG is defined, we don't log. If * both are defined, we log both ways. */ #define ALLOW_FILE "allow" /*-*/ #define DENY_FILE "deny" /*-*/ /*#define LOG_FILE "log"*/ /*-*/ /* where should the daemon stick its PID? */ #ifdef _PATH_VARRUN # define PIDDIR _PATH_VARRUN #else # define PIDDIR "/etc/" #endif #define PIDFILE "%scron.pid" /* 4.3BSD-style crontab */ #define SYSCRONTAB "/etc/crontab" +#define SYSCRONTABS "/etc/cron.d" +#define LOCALSYSCRONTABS "/usr/local/etc/cron.d" /* what editor to use if no EDITOR or VISUAL * environment variable specified. */ #if defined(_PATH_VI) # define EDITOR _PATH_VI #else # define EDITOR "/usr/ucb/vi" #endif #ifndef _PATH_BSHELL # define _PATH_BSHELL "/bin/sh" #endif #ifndef _PATH_DEFPATH # define _PATH_DEFPATH "/usr/bin:/bin" #endif Index: head/usr.sbin/cron/lib/misc.c =================================================================== --- head/usr.sbin/cron/lib/misc.c (revision 308138) +++ head/usr.sbin/cron/lib/misc.c (revision 308139) @@ -1,600 +1,596 @@ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ #if !defined(lint) && !defined(LINT) static const char rcsid[] = "$FreeBSD$"; #endif /* vix 26jan87 [RCS has the rest of the log] * vix 30dec86 [written] */ #include "cron.h" #if SYS_TIME_H # include #else # include #endif #include #include #include #include #include #include #if defined(SYSLOG) # include #endif #if defined(LOG_DAEMON) && !defined(LOG_CRON) #define LOG_CRON LOG_DAEMON #endif static int LogFD = ERR; int strcmp_until(left, right, until) char *left; char *right; int until; { register int diff; while (*left && *left != until && *left == *right) { left++; right++; } if ((*left=='\0' || *left == until) && (*right=='\0' || *right == until)) { diff = 0; } else { diff = *left - *right; } return diff; } /* strdtb(s) - delete trailing blanks in string 's' and return new length */ int strdtb(s) char *s; { char *x = s; /* scan forward to the null */ while (*x) x++; /* scan backward to either the first character before the string, * or the last non-blank in the string, whichever comes first. */ do {x--;} while (x >= s && isspace(*x)); /* one character beyond where we stopped above is where the null * goes. */ *++x = '\0'; /* the difference between the position of the null character and * the position of the first character of the string is the length. */ return x - s; } int set_debug_flags(flags) char *flags; { /* debug flags are of the form flag[,flag ...] * * if an error occurs, print a message to stdout and return FALSE. * otherwise return TRUE after setting ERROR_FLAGS. */ #if !DEBUGGING printf("this program was compiled without debugging enabled\n"); return FALSE; #else /* DEBUGGING */ char *pc = flags; DebugFlags = 0; while (*pc) { char **test; int mask; /* try to find debug flag name in our list. */ for ( test = DebugFlagNames, mask = 1; *test && strcmp_until(*test, pc, ','); test++, mask <<= 1 ) ; if (!*test) { fprintf(stderr, "unrecognized debug flag <%s> <%s>\n", flags, pc); return FALSE; } DebugFlags |= mask; /* skip to the next flag */ while (*pc && *pc != ',') pc++; if (*pc == ',') pc++; } if (DebugFlags) { int flag; fprintf(stderr, "debug flags enabled:"); for (flag = 0; DebugFlagNames[flag]; flag++) if (DebugFlags & (1 << flag)) fprintf(stderr, " %s", DebugFlagNames[flag]); fprintf(stderr, "\n"); } return TRUE; #endif /* DEBUGGING */ } void set_cron_uid() { #if defined(BSD) || defined(POSIX) if (seteuid(ROOT_UID) < OK) err(ERROR_EXIT, "seteuid"); #else if (setuid(ROOT_UID) < OK) err(ERROR_EXIT, "setuid"); #endif } void set_cron_cwd() { struct stat sb; /* first check for CRONDIR ("/var/cron" or some such) */ if (stat(CRONDIR, &sb) < OK && errno == ENOENT) { warn("%s", CRONDIR); if (OK == mkdir(CRONDIR, 0700)) { warnx("%s: created", CRONDIR); stat(CRONDIR, &sb); } else { err(ERROR_EXIT, "%s: mkdir", CRONDIR); } } if (!(sb.st_mode & S_IFDIR)) err(ERROR_EXIT, "'%s' is not a directory, bailing out", CRONDIR); if (chdir(CRONDIR) < OK) err(ERROR_EXIT, "cannot chdir(%s), bailing out", CRONDIR); /* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such) */ if (stat(SPOOL_DIR, &sb) < OK && errno == ENOENT) { warn("%s", SPOOL_DIR); if (OK == mkdir(SPOOL_DIR, 0700)) { warnx("%s: created", SPOOL_DIR); stat(SPOOL_DIR, &sb); } else { err(ERROR_EXIT, "%s: mkdir", SPOOL_DIR); } } if (!(sb.st_mode & S_IFDIR)) err(ERROR_EXIT, "'%s' is not a directory, bailing out", SPOOL_DIR); } /* get_char(file) : like getc() but increment LineNumber on newlines */ int get_char(file) FILE *file; { int ch; ch = getc(file); if (ch == '\n') Set_LineNum(LineNumber + 1) return ch; } /* unget_char(ch, file) : like ungetc but do LineNumber processing */ void unget_char(ch, file) int ch; FILE *file; { ungetc(ch, file); if (ch == '\n') Set_LineNum(LineNumber - 1) } /* get_string(str, max, file, termstr) : like fgets() but * (1) has terminator string which should include \n * (2) will always leave room for the null * (3) uses get_char() so LineNumber will be accurate * (4) returns EOF or terminating character, whichever */ int get_string(string, size, file, terms) char *string; int size; FILE *file; char *terms; { int ch; while (EOF != (ch = get_char(file)) && !strchr(terms, ch)) { if (size > 1) { *string++ = (char) ch; size--; } } if (size > 0) *string = '\0'; return ch; } /* skip_comments(file) : read past comment (if any) */ void skip_comments(file) FILE *file; { int ch; while (EOF != (ch = get_char(file))) { /* ch is now the first character of a line. */ while (ch == ' ' || ch == '\t') ch = get_char(file); if (ch == EOF) break; /* ch is now the first non-blank character of a line. */ if (ch != '\n' && ch != '#') break; /* ch must be a newline or comment as first non-blank * character on a line. */ while (ch != '\n' && ch != EOF) ch = get_char(file); /* ch is now the newline of a line which we're going to * ignore. */ } if (ch != EOF) unget_char(ch, file); } /* int in_file(char *string, FILE *file) * return TRUE if one of the lines in file matches string exactly, * FALSE otherwise. */ static int in_file(char *string, FILE *file) { char line[MAX_TEMPSTR]; rewind(file); while (fgets(line, MAX_TEMPSTR, file)) { if (line[0] != '\0') if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; if (0 == strcmp(line, string)) return TRUE; } return FALSE; } /* int allowed(char *username) * returns TRUE if (ALLOW_FILE exists and user is listed) * or (DENY_FILE exists and user is NOT listed) * or (neither file exists but user=="root" so it's okay) */ int allowed(username) char *username; { FILE *allow, *deny; int isallowed; isallowed = FALSE; deny = NULL; #if defined(ALLOW_FILE) && defined(DENY_FILE) if ((allow = fopen(ALLOW_FILE, "r")) == NULL && errno != ENOENT) goto out; if ((deny = fopen(DENY_FILE, "r")) == NULL && errno != ENOENT) goto out; Debug(DMISC, ("allow/deny enabled, %d/%d\n", !!allow, !!deny)) #else allow = NULL; #endif if (allow) isallowed = in_file(username, allow); else if (deny) isallowed = !in_file(username, deny); else { #if defined(ALLOW_ONLY_ROOT) isallowed = (strcmp(username, ROOT_USER) == 0); #else isallowed = TRUE; #endif } out: if (allow) fclose(allow); if (deny) fclose(deny); return (isallowed); } void -log_it(username, xpid, event, detail) - char *username; - int xpid; - char *event; - char *detail; +log_it(char *username, int xpid, char *event, const char *detail) { #if defined(LOG_FILE) || DEBUGGING PID_T pid = xpid; #endif #if defined(LOG_FILE) char *msg; TIME_T now = time((TIME_T) 0); register struct tm *t = localtime(&now); #endif /*LOG_FILE*/ #if defined(SYSLOG) static int syslog_open = 0; #endif #if defined(LOG_FILE) /* we assume that MAX_TEMPSTR will hold the date, time, &punctuation. */ msg = malloc(strlen(username) + strlen(event) + strlen(detail) + MAX_TEMPSTR); if (msg == NULL) warnx("failed to allocate memory for log message"); else { if (LogFD < OK) { LogFD = open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0600); if (LogFD < OK) { warn("can't open log file %s", LOG_FILE); } else { (void) fcntl(LogFD, F_SETFD, 1); } } /* we have to sprintf() it because fprintf() doesn't always * write everything out in one chunk and this has to be * atomically appended to the log file. */ sprintf(msg, "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)\n", username, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, pid, event, detail); /* we have to run strlen() because sprintf() returns (char*) * on old BSD. */ if (LogFD < OK || write(LogFD, msg, strlen(msg)) < OK) { if (LogFD >= OK) warn("%s", LOG_FILE); warnx("can't write to log file"); write(STDERR, msg, strlen(msg)); } free(msg); } #endif /*LOG_FILE*/ #if defined(SYSLOG) if (!syslog_open) { /* we don't use LOG_PID since the pid passed to us by * our client may not be our own. therefore we want to * print the pid ourselves. */ # ifdef LOG_DAEMON openlog(ProgramName, LOG_PID, LOG_CRON); # else openlog(ProgramName, LOG_PID); # endif syslog_open = TRUE; /* assume openlog success */ } syslog(LOG_INFO, "(%s) %s (%s)\n", username, event, detail); #endif /*SYSLOG*/ #if DEBUGGING if (DebugFlags) { fprintf(stderr, "log_it: (%s %d) %s (%s)\n", username, pid, event, detail); } #endif } void log_close() { if (LogFD != ERR) { close(LogFD); LogFD = ERR; } } /* two warnings: * (1) this routine is fairly slow * (2) it returns a pointer to static storage */ char * first_word(s, t) register char *s; /* string we want the first word of */ register char *t; /* terminators, implicitly including \0 */ { static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */ static int retsel = 0; register char *rb, *rp; /* select a return buffer */ retsel = 1-retsel; rb = &retbuf[retsel][0]; rp = rb; /* skip any leading terminators */ while (*s && (NULL != strchr(t, *s))) { s++; } /* copy until next terminator or full buffer */ while (*s && (NULL == strchr(t, *s)) && (rp < &rb[MAX_TEMPSTR])) { *rp++ = *s++; } /* finish the return-string and return it */ *rp = '\0'; return rb; } /* warning: * heavily ascii-dependent. */ static void mkprint(register char *dst, register unsigned char *src, register int len) { while (len-- > 0) { register unsigned char ch = *src++; if (ch < ' ') { /* control character */ *dst++ = '^'; *dst++ = ch + '@'; } else if (ch < 0177) { /* printable */ *dst++ = ch; } else if (ch == 0177) { /* delete/rubout */ *dst++ = '^'; *dst++ = '?'; } else { /* parity character */ sprintf(dst, "\\%03o", ch); dst += 4; } } *dst = '\0'; } /* warning: * returns a pointer to malloc'd storage, you must call free yourself. */ char * mkprints(src, len) register unsigned char *src; register unsigned int len; { register char *dst = malloc(len*4 + 1); if (dst != NULL) mkprint(dst, src, len); return dst; } #ifdef MAIL_DATE /* Sat, 27 Feb 93 11:44:51 CST * 123456789012345678901234567 */ char * arpadate(clock) time_t *clock; { time_t t = clock ?*clock :time(0L); struct tm *tm = localtime(&t); static char ret[32]; /* zone name might be >3 chars */ if (tm->tm_year >= 100) tm->tm_year += 1900; (void) snprintf(ret, sizeof(ret), "%s, %2d %s %d %02d:%02d:%02d %s", DowNames[tm->tm_wday], tm->tm_mday, MonthNames[tm->tm_mon], tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec, TZONE(*tm)); return ret; } #endif /*MAIL_DATE*/ #ifdef HAVE_SAVED_UIDS static int save_euid; int swap_uids() { save_euid = geteuid(); return seteuid(getuid()); } int swap_uids_back() { return seteuid(save_euid); } #else /*HAVE_SAVED_UIDS*/ int swap_uids() { return setreuid(geteuid(), getuid()); } int swap_uids_back() { return swap_uids(); } #endif /*HAVE_SAVED_UIDS*/