12311Sjkh/* Copyright 1988,1990,1993,1994 by Paul Vixie
22311Sjkh * All rights reserved
32311Sjkh *
42311Sjkh * Distribute freely, except: don't remove my name from the source or
52311Sjkh * documentation (don't take credit for my work), mark your changes (don't
62311Sjkh * get me blamed for your possible bugs), don't alter or remove this
72311Sjkh * notice.  May be sold if buildable source is provided to buyer.  No
82311Sjkh * warrantee of any kind, express or implied, is included with this
92311Sjkh * software; use at your own risk, responsibility for damages (if any) to
102311Sjkh * anyone resulting from the use of this software rests entirely with the
112311Sjkh * user.
122311Sjkh *
132311Sjkh * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
142311Sjkh * I'll try to keep a version up to date.  I can be reached as follows:
152311Sjkh * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
162311Sjkh */
172311Sjkh
182311Sjkh/* cron.h - header for vixie's cron
192311Sjkh *
2050479Speter * $FreeBSD$
212311Sjkh *
222311Sjkh * vix 14nov88 [rest of log is in RCS]
232311Sjkh * vix 14jan87 [0 or 7 can be sunday; thanks, mwm@berkeley]
242311Sjkh * vix 30dec86 [written]
252311Sjkh */
262311Sjkh
272311Sjkh/* reorder these #include's at your peril */
282311Sjkh
292311Sjkh#include <sys/types.h>
302311Sjkh#include <sys/param.h>
312311Sjkh#include "compat.h"
322311Sjkh
3329452Scharnier#include <bitstring.h>
3429452Scharnier#include <ctype.h>
3529452Scharnier#include <err.h>
3620573Spst#include <errno.h>
37149430Spjd#include <libutil.h>
382311Sjkh#include <pwd.h>
3929452Scharnier#include <signal.h>
4029452Scharnier#include <stdio.h>
4129452Scharnier#include <time.h>
422311Sjkh#include <sys/wait.h>
432311Sjkh
442311Sjkh#include "pathnames.h"
452311Sjkh#include "config.h"
462311Sjkh#include "externs.h"
472311Sjkh
482311Sjkh	/* these are really immutable, and are
492311Sjkh	 *   defined for symbolic convenience only
502311Sjkh	 * TRUE, FALSE, and ERR must be distinct
512311Sjkh	 * ERR must be < OK.
522311Sjkh	 */
532311Sjkh#define TRUE		1
542311Sjkh#define FALSE		0
552311Sjkh	/* system calls return this on success */
562311Sjkh#define OK		0
572311Sjkh	/*   or this on error */
582311Sjkh#define ERR		(-1)
592311Sjkh
602311Sjkh	/* turn this on to get '-x' code */
612311Sjkh#ifndef DEBUGGING
622311Sjkh#define DEBUGGING	FALSE
632311Sjkh#endif
642311Sjkh
652311Sjkh#define READ_PIPE	0	/* which end of a pipe pair do you read? */
662311Sjkh#define WRITE_PIPE	1	/*   or write to? */
672311Sjkh#define STDIN		0	/* what is stdin's file descriptor? */
682311Sjkh#define STDOUT		1	/*   stdout's? */
692311Sjkh#define STDERR		2	/*   stderr's? */
702311Sjkh#define ERROR_EXIT	1	/* exit() with this will scare the shell */
712311Sjkh#define	OK_EXIT		0	/* exit() with this is considered 'normal' */
722311Sjkh#define	MAX_FNAME	100	/* max length of internally generated fn */
732311Sjkh#define	MAX_COMMAND	1000	/* max length of internally generated cmd */
742311Sjkh#define	MAX_ENVSTR	1000	/* max length of envvar=value\0 strings */
752311Sjkh#define	MAX_TEMPSTR	100	/* obvious */
762311Sjkh#define	MAX_UNAME	20	/* max length of username, should be overkill */
772311Sjkh#define	ROOT_UID	0	/* don't change this, it really must be root */
782311Sjkh#define	ROOT_USER	"root"	/* ditto */
79170890Syar#define	SYS_NAME	"*system*" /* magic owner name for system crontab */
802311Sjkh
812311Sjkh				/* NOTE: these correspond to DebugFlagNames,
822311Sjkh				 *	defined below.
832311Sjkh				 */
842311Sjkh#define	DEXT		0x0001	/* extend flag for other debug masks */
852311Sjkh#define	DSCH		0x0002	/* scheduling debug mask */
862311Sjkh#define	DPROC		0x0004	/* process control debug mask */
872311Sjkh#define	DPARS		0x0008	/* parsing debug mask */
882311Sjkh#define	DLOAD		0x0010	/* database loading debug mask */
892311Sjkh#define	DMISC		0x0020	/* misc debug mask */
902311Sjkh#define	DTEST		0x0040	/* test mode: don't execute any commands */
912311Sjkh#define	DBIT		0x0080	/* bit twiddling shown (long) */
922311Sjkh
932311Sjkh#define	CRON_TAB(u)	"%s/%s", SPOOL_DIR, u
942311Sjkh#define	REG		register
952311Sjkh#define	PPC_NULL	((char **)NULL)
962311Sjkh
972311Sjkh#ifndef MAXHOSTNAMELEN
9869230Skris#define MAXHOSTNAMELEN 256
992311Sjkh#endif
1002311Sjkh
1012311Sjkh#define	Skip_Blanks(c, f) \
1022311Sjkh			while (c == '\t' || c == ' ') \
1032311Sjkh				c = get_char(f);
1042311Sjkh
1052311Sjkh#define	Skip_Nonblanks(c, f) \
1062311Sjkh			while (c!='\t' && c!=' ' && c!='\n' && c != EOF) \
1072311Sjkh				c = get_char(f);
1082311Sjkh
1092311Sjkh#define	Skip_Line(c, f) \
1102311Sjkh			do {c = get_char(f);} while (c != '\n' && c != EOF);
1112311Sjkh
1122311Sjkh#if DEBUGGING
1132311Sjkh# define Debug(mask, message) \
1142311Sjkh			if ( (DebugFlags & (mask) ) == (mask) ) \
1152311Sjkh				printf message;
1162311Sjkh#else /* !DEBUGGING */
1172311Sjkh# define Debug(mask, message) \
1182311Sjkh			;
1192311Sjkh#endif /* DEBUGGING */
1202311Sjkh
1212311Sjkh#define	MkLower(ch)	(isupper(ch) ? tolower(ch) : ch)
1222311Sjkh#define	MkUpper(ch)	(islower(ch) ? toupper(ch) : ch)
1232311Sjkh#define	Set_LineNum(ln)	{Debug(DPARS|DEXT,("linenum=%d\n",ln)); \
1242311Sjkh			 LineNumber = ln; \
1252311Sjkh			}
1262311Sjkh
127242101Ssobomax#define	FIRST_SECOND	0
128242101Ssobomax#define	LAST_SECOND	59
129242101Ssobomax#define	SECOND_COUNT	(LAST_SECOND - FIRST_SECOND + 1)
130242101Ssobomax
1312311Sjkh#define	FIRST_MINUTE	0
1322311Sjkh#define	LAST_MINUTE	59
1332311Sjkh#define	MINUTE_COUNT	(LAST_MINUTE - FIRST_MINUTE + 1)
1342311Sjkh
1352311Sjkh#define	FIRST_HOUR	0
1362311Sjkh#define	LAST_HOUR	23
1372311Sjkh#define	HOUR_COUNT	(LAST_HOUR - FIRST_HOUR + 1)
1382311Sjkh
1392311Sjkh#define	FIRST_DOM	1
1402311Sjkh#define	LAST_DOM	31
1412311Sjkh#define	DOM_COUNT	(LAST_DOM - FIRST_DOM + 1)
1422311Sjkh
1432311Sjkh#define	FIRST_MONTH	1
1442311Sjkh#define	LAST_MONTH	12
1452311Sjkh#define	MONTH_COUNT	(LAST_MONTH - FIRST_MONTH + 1)
1462311Sjkh
1472311Sjkh/* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */
1482311Sjkh#define	FIRST_DOW	0
1492311Sjkh#define	LAST_DOW	7
1502311Sjkh#define	DOW_COUNT	(LAST_DOW - FIRST_DOW + 1)
1512311Sjkh
15230895Sache#ifdef LOGIN_CAP
15330895Sache/* see init.c */
15430895Sache#define RESOURCE_RC "daemon"
15530895Sache#endif
15630895Sache
1572311Sjkh			/* each user's crontab will be held as a list of
1582311Sjkh			 * the following structure.
1592311Sjkh			 *
1602311Sjkh			 * These are the cron commands.
1612311Sjkh			 */
1622311Sjkh
1632311Sjkhtypedef	struct _entry {
1642311Sjkh	struct _entry	*next;
1658857Srgrimes	uid_t		uid;
1662311Sjkh	gid_t		gid;
16730895Sache#ifdef LOGIN_CAP
16830895Sache	char            *class;
16930895Sache#endif
1702311Sjkh	char		**envp;
1712311Sjkh	char		*cmd;
172242101Ssobomax	bitstr_t	bit_decl(second, SECOND_COUNT);
1732311Sjkh	bitstr_t	bit_decl(minute, MINUTE_COUNT);
1742311Sjkh	bitstr_t	bit_decl(hour,   HOUR_COUNT);
1752311Sjkh	bitstr_t	bit_decl(dom,    DOM_COUNT);
1762311Sjkh	bitstr_t	bit_decl(month,  MONTH_COUNT);
1772311Sjkh	bitstr_t	bit_decl(dow,    DOW_COUNT);
1782311Sjkh	int		flags;
1792311Sjkh#define	DOM_STAR	0x01
1802311Sjkh#define	DOW_STAR	0x02
1812311Sjkh#define	WHEN_REBOOT	0x04
18274010Sbabkin#define	RUN_AT	0x08
18374010Sbabkin#define	NOT_UNTIL	0x10
184242101Ssobomax#define	SEC_RES		0x20
18574010Sbabkin	time_t	lastrun;
1862311Sjkh} entry;
1872311Sjkh
1882311Sjkh			/* the crontab database will be a list of the
1892311Sjkh			 * following structure, one element per user
1902311Sjkh			 * plus one for the system.
1912311Sjkh			 *
1922311Sjkh			 * These are the crontabs.
1932311Sjkh			 */
1942311Sjkh
1952311Sjkhtypedef	struct _user {
1962311Sjkh	struct _user	*next, *prev;	/* links */
1972311Sjkh	char		*name;
1982311Sjkh	time_t		mtime;		/* last modtime of crontab */
1992311Sjkh	entry		*crontab;	/* this person's crontab */
2002311Sjkh} user;
2012311Sjkh
2022311Sjkhtypedef	struct _cron_db {
2032311Sjkh	user		*head, *tail;	/* links */
2042311Sjkh	time_t		mtime;		/* last modtime on spooldir */
2052311Sjkh} cron_db;
2062311Sjkh
2072311Sjkh
208173412Skevlovoid		set_cron_uid(void),
209173412Skevlo		set_cron_cwd(void),
210173412Skevlo		load_database(cron_db *),
211173412Skevlo		open_logfile(void),
212173412Skevlo		sigpipe_func(void),
213173412Skevlo		job_add(entry *, user *),
214173412Skevlo		do_command(entry *, user *),
215173412Skevlo		link_user(cron_db *, user *),
216173412Skevlo		unlink_user(cron_db *, user *),
217173412Skevlo		free_user(user *),
218173412Skevlo		env_free(char **),
219173412Skevlo		unget_char(int, FILE *),
220173412Skevlo		free_entry(entry *),
221173412Skevlo		skip_comments(FILE *),
222173412Skevlo		log_it(char *, int, char *, char *),
223173412Skevlo		log_close(void);
2242311Sjkh
225173412Skevloint		job_runqueue(void),
226173412Skevlo		set_debug_flags(char *),
227173412Skevlo		get_char(FILE *),
228173412Skevlo		get_string(char *, int, FILE *, char *),
229173412Skevlo		swap_uids(void),
230184809Smatteo		swap_uids_back(void),
231173412Skevlo		load_env(char *, FILE *),
232173412Skevlo		cron_pclose(FILE *),
233173412Skevlo		strcmp_until(char *, char *, int),
234173412Skevlo		allowed(char *),
235173412Skevlo		strdtb(char *);
2362311Sjkh
237173412Skevlochar		*env_get(char *, char **),
238173412Skevlo		*arpadate(time_t *),
239173412Skevlo		*mkprints(unsigned char *, unsigned int),
240173412Skevlo		*first_word(char *, char *),
241173412Skevlo		**env_init(void),
242173412Skevlo		**env_copy(char **),
243173412Skevlo		**env_set(char **, char *);
2442311Sjkh
245173412Skevlouser		*load_user(int, struct passwd *, char *),
246173412Skevlo		*find_user(cron_db *, char *);
2472311Sjkh
248184809Smatteoentry		*load_entry(FILE *, void (*)(char *),
249173412Skevlo				 struct passwd *, char **);
2502311Sjkh
251173412SkevloFILE		*cron_popen(char *, char *, entry *);
2522311Sjkh
2532311Sjkh
2542311Sjkh				/* in the C tradition, we only create
2552311Sjkh				 * variables for the main program, just
2562311Sjkh				 * extern them elsewhere.
2572311Sjkh				 */
2582311Sjkh
2592311Sjkh#ifdef MAIN_PROGRAM
2602311Sjkh# if !defined(LINT) && !defined(lint)
2612311Sjkhchar	*copyright[] = {
2622311Sjkh		"@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie",
2632311Sjkh		"@(#) All rights reserved"
2642311Sjkh	};
2652311Sjkh# endif
2662311Sjkh
2672311Sjkhchar	*MonthNames[] = {
2682311Sjkh		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2692311Sjkh		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2702311Sjkh		NULL
2712311Sjkh	};
2722311Sjkh
2732311Sjkhchar	*DowNames[] = {
2742311Sjkh		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
2752311Sjkh		NULL
2762311Sjkh	};
2772311Sjkh
278180096Smarckchar	*ProgramName,
279180096Smarck	*defmailto;
2802311Sjkhint	LineNumber;
281129280Syarunsigned Jitter,
282129280Syar	RootJitter;
2832311Sjkhtime_t	TargetTime;
2842311Sjkh
2852311Sjkh# if DEBUGGING
2862311Sjkhint	DebugFlags;
2872311Sjkhchar	*DebugFlagNames[] = {	/* sync with #defines */
2882311Sjkh		"ext", "sch", "proc", "pars", "load", "misc", "test", "bit",
2892311Sjkh		NULL		/* NULL must be last element */
2902311Sjkh	};
2912311Sjkh# endif /* DEBUGGING */
2922311Sjkh#else /*MAIN_PROGRAM*/
2932311Sjkhextern	char	*copyright[],
2942311Sjkh		*MonthNames[],
2952311Sjkh		*DowNames[],
296180096Smarck		*ProgramName,
297180096Smarck		*defmailto;
2982311Sjkhextern	int	LineNumber;
299129280Syarextern unsigned	Jitter,
300129280Syar		RootJitter;
3012311Sjkhextern	time_t	TargetTime;
302149430Spjdextern struct pidfh *pfh;
3032311Sjkh# if DEBUGGING
3042311Sjkhextern	int	DebugFlags;
3052311Sjkhextern	char	*DebugFlagNames[];
3062311Sjkh# endif /* DEBUGGING */
3072311Sjkh#endif /*MAIN_PROGRAM*/
308