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: stable/10/usr.sbin/cron/cron/cron.h 321242 2017-07-19 20:24:38Z ngie $
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	ROOT_UID	0	/* don't change this, it really must be root */
772311Sjkh#define	ROOT_USER	"root"	/* ditto */
78170890Syar#define	SYS_NAME	"*system*" /* magic owner name for system crontab */
792311Sjkh
802311Sjkh				/* NOTE: these correspond to DebugFlagNames,
812311Sjkh				 *	defined below.
822311Sjkh				 */
832311Sjkh#define	DEXT		0x0001	/* extend flag for other debug masks */
842311Sjkh#define	DSCH		0x0002	/* scheduling debug mask */
852311Sjkh#define	DPROC		0x0004	/* process control debug mask */
862311Sjkh#define	DPARS		0x0008	/* parsing debug mask */
872311Sjkh#define	DLOAD		0x0010	/* database loading debug mask */
882311Sjkh#define	DMISC		0x0020	/* misc debug mask */
892311Sjkh#define	DTEST		0x0040	/* test mode: don't execute any commands */
902311Sjkh#define	DBIT		0x0080	/* bit twiddling shown (long) */
912311Sjkh
922311Sjkh#define	CRON_TAB(u)	"%s/%s", SPOOL_DIR, u
932311Sjkh#define	REG		register
942311Sjkh#define	PPC_NULL	((char **)NULL)
952311Sjkh
962311Sjkh#ifndef MAXHOSTNAMELEN
9769230Skris#define MAXHOSTNAMELEN 256
982311Sjkh#endif
992311Sjkh
1002311Sjkh#define	Skip_Blanks(c, f) \
1012311Sjkh			while (c == '\t' || c == ' ') \
1022311Sjkh				c = get_char(f);
1032311Sjkh
1042311Sjkh#define	Skip_Nonblanks(c, f) \
1052311Sjkh			while (c!='\t' && c!=' ' && c!='\n' && c != EOF) \
1062311Sjkh				c = get_char(f);
1072311Sjkh
1082311Sjkh#define	Skip_Line(c, f) \
1092311Sjkh			do {c = get_char(f);} while (c != '\n' && c != EOF);
1102311Sjkh
1112311Sjkh#if DEBUGGING
1122311Sjkh# define Debug(mask, message) \
1132311Sjkh			if ( (DebugFlags & (mask) ) == (mask) ) \
1142311Sjkh				printf message;
1152311Sjkh#else /* !DEBUGGING */
1162311Sjkh# define Debug(mask, message) \
1172311Sjkh			;
1182311Sjkh#endif /* DEBUGGING */
1192311Sjkh
1202311Sjkh#define	MkLower(ch)	(isupper(ch) ? tolower(ch) : ch)
1212311Sjkh#define	MkUpper(ch)	(islower(ch) ? toupper(ch) : ch)
1222311Sjkh#define	Set_LineNum(ln)	{Debug(DPARS|DEXT,("linenum=%d\n",ln)); \
1232311Sjkh			 LineNumber = ln; \
1242311Sjkh			}
1252311Sjkh
126242101Ssobomax#define	FIRST_SECOND	0
127242101Ssobomax#define	LAST_SECOND	59
128242101Ssobomax#define	SECOND_COUNT	(LAST_SECOND - FIRST_SECOND + 1)
129242101Ssobomax
1302311Sjkh#define	FIRST_MINUTE	0
1312311Sjkh#define	LAST_MINUTE	59
1322311Sjkh#define	MINUTE_COUNT	(LAST_MINUTE - FIRST_MINUTE + 1)
1332311Sjkh
1342311Sjkh#define	FIRST_HOUR	0
1352311Sjkh#define	LAST_HOUR	23
1362311Sjkh#define	HOUR_COUNT	(LAST_HOUR - FIRST_HOUR + 1)
1372311Sjkh
1382311Sjkh#define	FIRST_DOM	1
1392311Sjkh#define	LAST_DOM	31
1402311Sjkh#define	DOM_COUNT	(LAST_DOM - FIRST_DOM + 1)
1412311Sjkh
1422311Sjkh#define	FIRST_MONTH	1
1432311Sjkh#define	LAST_MONTH	12
1442311Sjkh#define	MONTH_COUNT	(LAST_MONTH - FIRST_MONTH + 1)
1452311Sjkh
1462311Sjkh/* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */
1472311Sjkh#define	FIRST_DOW	0
1482311Sjkh#define	LAST_DOW	7
1492311Sjkh#define	DOW_COUNT	(LAST_DOW - FIRST_DOW + 1)
1502311Sjkh
15130895Sache#ifdef LOGIN_CAP
15230895Sache/* see init.c */
15330895Sache#define RESOURCE_RC "daemon"
15430895Sache#endif
15530895Sache
1562311Sjkh			/* each user's crontab will be held as a list of
1572311Sjkh			 * the following structure.
1582311Sjkh			 *
1592311Sjkh			 * These are the cron commands.
1602311Sjkh			 */
1612311Sjkh
1622311Sjkhtypedef	struct _entry {
1632311Sjkh	struct _entry	*next;
1648857Srgrimes	uid_t		uid;
1652311Sjkh	gid_t		gid;
16630895Sache#ifdef LOGIN_CAP
16730895Sache	char            *class;
16830895Sache#endif
1692311Sjkh	char		**envp;
1702311Sjkh	char		*cmd;
171242101Ssobomax	bitstr_t	bit_decl(second, SECOND_COUNT);
1722311Sjkh	bitstr_t	bit_decl(minute, MINUTE_COUNT);
1732311Sjkh	bitstr_t	bit_decl(hour,   HOUR_COUNT);
1742311Sjkh	bitstr_t	bit_decl(dom,    DOM_COUNT);
1752311Sjkh	bitstr_t	bit_decl(month,  MONTH_COUNT);
1762311Sjkh	bitstr_t	bit_decl(dow,    DOW_COUNT);
1772311Sjkh	int		flags;
1782311Sjkh#define	DOM_STAR	0x01
1792311Sjkh#define	DOW_STAR	0x02
1802311Sjkh#define	WHEN_REBOOT	0x04
18174010Sbabkin#define	RUN_AT	0x08
18274010Sbabkin#define	NOT_UNTIL	0x10
183242101Ssobomax#define	SEC_RES		0x20
18474010Sbabkin	time_t	lastrun;
1852311Sjkh} entry;
1862311Sjkh
1872311Sjkh			/* the crontab database will be a list of the
1882311Sjkh			 * following structure, one element per user
1892311Sjkh			 * plus one for the system.
1902311Sjkh			 *
1912311Sjkh			 * These are the crontabs.
1922311Sjkh			 */
1932311Sjkh
1942311Sjkhtypedef	struct _user {
1952311Sjkh	struct _user	*next, *prev;	/* links */
1962311Sjkh	char		*name;
1972311Sjkh	time_t		mtime;		/* last modtime of crontab */
1982311Sjkh	entry		*crontab;	/* this person's crontab */
1992311Sjkh} user;
2002311Sjkh
2012311Sjkhtypedef	struct _cron_db {
2022311Sjkh	user		*head, *tail;	/* links */
2032311Sjkh	time_t		mtime;		/* last modtime on spooldir */
2042311Sjkh} cron_db;
2052311Sjkh
2062311Sjkh
207173412Skevlovoid		set_cron_uid(void),
208173412Skevlo		set_cron_cwd(void),
209173412Skevlo		load_database(cron_db *),
210173412Skevlo		open_logfile(void),
211173412Skevlo		sigpipe_func(void),
212173412Skevlo		job_add(entry *, user *),
213173412Skevlo		do_command(entry *, user *),
214173412Skevlo		link_user(cron_db *, user *),
215173412Skevlo		unlink_user(cron_db *, user *),
216173412Skevlo		free_user(user *),
217173412Skevlo		env_free(char **),
218173412Skevlo		unget_char(int, FILE *),
219173412Skevlo		free_entry(entry *),
220173412Skevlo		skip_comments(FILE *),
221321242Sngie		log_it(char *, int, char *, const char *),
222173412Skevlo		log_close(void);
2232311Sjkh
224173412Skevloint		job_runqueue(void),
225173412Skevlo		set_debug_flags(char *),
226173412Skevlo		get_char(FILE *),
227173412Skevlo		get_string(char *, int, FILE *, char *),
228173412Skevlo		swap_uids(void),
229184809Smatteo		swap_uids_back(void),
230173412Skevlo		load_env(char *, FILE *),
231173412Skevlo		cron_pclose(FILE *),
232173412Skevlo		strcmp_until(char *, char *, int),
233173412Skevlo		allowed(char *),
234173412Skevlo		strdtb(char *);
2352311Sjkh
236173412Skevlochar		*env_get(char *, char **),
237173412Skevlo		*arpadate(time_t *),
238173412Skevlo		*mkprints(unsigned char *, unsigned int),
239173412Skevlo		*first_word(char *, char *),
240173412Skevlo		**env_init(void),
241173412Skevlo		**env_copy(char **),
242173412Skevlo		**env_set(char **, char *);
2432311Sjkh
244173412Skevlouser		*load_user(int, struct passwd *, char *),
245173412Skevlo		*find_user(cron_db *, char *);
2462311Sjkh
247184809Smatteoentry		*load_entry(FILE *, void (*)(char *),
248173412Skevlo				 struct passwd *, char **);
2492311Sjkh
250173412SkevloFILE		*cron_popen(char *, char *, entry *);
2512311Sjkh
2522311Sjkh
2532311Sjkh				/* in the C tradition, we only create
2542311Sjkh				 * variables for the main program, just
2552311Sjkh				 * extern them elsewhere.
2562311Sjkh				 */
2572311Sjkh
2582311Sjkh#ifdef MAIN_PROGRAM
2592311Sjkh# if !defined(LINT) && !defined(lint)
2602311Sjkhchar	*copyright[] = {
2612311Sjkh		"@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie",
2622311Sjkh		"@(#) All rights reserved"
2632311Sjkh	};
2642311Sjkh# endif
2652311Sjkh
2662311Sjkhchar	*MonthNames[] = {
2672311Sjkh		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2682311Sjkh		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2692311Sjkh		NULL
2702311Sjkh	};
2712311Sjkh
2722311Sjkhchar	*DowNames[] = {
2732311Sjkh		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
2742311Sjkh		NULL
2752311Sjkh	};
2762311Sjkh
277180096Smarckchar	*ProgramName,
278180096Smarck	*defmailto;
2792311Sjkhint	LineNumber;
280129280Syarunsigned Jitter,
281129280Syar	RootJitter;
2822311Sjkhtime_t	TargetTime;
2832311Sjkh
2842311Sjkh# if DEBUGGING
2852311Sjkhint	DebugFlags;
2862311Sjkhchar	*DebugFlagNames[] = {	/* sync with #defines */
2872311Sjkh		"ext", "sch", "proc", "pars", "load", "misc", "test", "bit",
2882311Sjkh		NULL		/* NULL must be last element */
2892311Sjkh	};
2902311Sjkh# endif /* DEBUGGING */
2912311Sjkh#else /*MAIN_PROGRAM*/
2922311Sjkhextern	char	*copyright[],
2932311Sjkh		*MonthNames[],
2942311Sjkh		*DowNames[],
295180096Smarck		*ProgramName,
296180096Smarck		*defmailto;
2972311Sjkhextern	int	LineNumber;
298129280Syarextern unsigned	Jitter,
299129280Syar		RootJitter;
3002311Sjkhextern	time_t	TargetTime;
301149430Spjdextern struct pidfh *pfh;
3022311Sjkh# if DEBUGGING
3032311Sjkhextern	int	DebugFlags;
3042311Sjkhextern	char	*DebugFlagNames[];
3052311Sjkh# endif /* DEBUGGING */
3062311Sjkh#endif /*MAIN_PROGRAM*/
307