1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice.  May be sold if buildable source is provided to buyer.  No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
12 *
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date.  I can be reached as follows:
15 * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
16 */
17
18/* cron.h - header for vixie's cron
19 *
20 * $FreeBSD: src/usr.sbin/cron/cron/cron.h,v 1.16 2005/08/24 17:51:36 pjd Exp $
21 *
22 * vix 14nov88 [rest of log is in RCS]
23 * vix 14jan87 [0 or 7 can be sunday; thanks, mwm@berkeley]
24 * vix 30dec86 [written]
25 */
26
27/* reorder these #include's at your peril */
28
29#include <sys/types.h>
30#include <sys/param.h>
31#include "compat.h"
32
33#include <bitstring.h>
34#include <ctype.h>
35#include <err.h>
36#include <errno.h>
37#include <libutil.h>
38#include <pwd.h>
39#ifdef __APPLE__
40#include <grp.h>
41#endif
42#include <signal.h>
43#include <stdio.h>
44#include <time.h>
45#include <sys/wait.h>
46
47#include "pathnames.h"
48#include "config.h"
49#include "externs.h"
50
51	/* these are really immutable, and are
52	 *   defined for symbolic convenience only
53	 * TRUE, FALSE, and ERR must be distinct
54	 * ERR must be < OK.
55	 */
56#define TRUE		1
57#define FALSE		0
58	/* system calls return this on success */
59#define OK		0
60	/*   or this on error */
61#define ERR		(-1)
62
63	/* turn this on to get '-x' code */
64#ifndef DEBUGGING
65#define DEBUGGING	FALSE
66#endif
67
68#define READ_PIPE	0	/* which end of a pipe pair do you read? */
69#define WRITE_PIPE	1	/*   or write to? */
70#define STDIN		0	/* what is stdin's file descriptor? */
71#define STDOUT		1	/*   stdout's? */
72#define STDERR		2	/*   stderr's? */
73#define ERROR_EXIT	1	/* exit() with this will scare the shell */
74#define	OK_EXIT		0	/* exit() with this is considered 'normal' */
75#define	MAX_FNAME	100	/* max length of internally generated fn */
76#define	MAX_COMMAND	1000	/* max length of internally generated cmd */
77#define	MAX_ENVSTR	1000	/* max length of envvar=value\0 strings */
78#define	MAX_TEMPSTR	100	/* obvious */
79#define	MAX_UNAME	256	/* max length of username, same as _UTX_USERSIZE */
80#define	ROOT_UID	0	/* don't change this, it really must be root */
81#define	ROOT_USER	"root"	/* ditto */
82
83				/* NOTE: these correspond to DebugFlagNames,
84				 *	defined below.
85				 */
86#define	DEXT		0x0001	/* extend flag for other debug masks */
87#define	DSCH		0x0002	/* scheduling debug mask */
88#define	DPROC		0x0004	/* process control debug mask */
89#define	DPARS		0x0008	/* parsing debug mask */
90#define	DLOAD		0x0010	/* database loading debug mask */
91#define	DMISC		0x0020	/* misc debug mask */
92#define	DTEST		0x0040	/* test mode: don't execute any commands */
93#define	DBIT		0x0080	/* bit twiddling shown (long) */
94
95#define	CRON_TAB(u)	"%s/%s", SPOOL_DIR, u
96#define	TMP_CRON_TAB(u)	"%s/%s", TMP_SPOOL_DIR, u
97#define	REG		register
98#define	PPC_NULL	((char **)NULL)
99
100#ifndef MAXHOSTNAMELEN
101#define MAXHOSTNAMELEN 256
102#endif
103
104#define	Skip_Blanks(c, f) \
105			while (c == '\t' || c == ' ') \
106				c = get_char(f);
107
108#define	Skip_Nonblanks(c, f) \
109			while (c!='\t' && c!=' ' && c!='\n' && c != EOF) \
110				c = get_char(f);
111
112#define	Skip_Line(c, f) \
113			do {c = get_char(f);} while (c != '\n' && c != EOF);
114
115#if DEBUGGING
116# define Debug(mask, message) \
117			if ( (DebugFlags & (mask) ) == (mask) ) \
118				printf message;
119#else /* !DEBUGGING */
120# define Debug(mask, message) \
121			;
122#endif /* DEBUGGING */
123
124#define	MkLower(ch)	(isupper(ch) ? tolower(ch) : ch)
125#define	MkUpper(ch)	(islower(ch) ? toupper(ch) : ch)
126#define	Set_LineNum(ln)	{Debug(DPARS|DEXT,("linenum=%d\n",ln)); \
127			 LineNumber = ln; \
128			}
129
130#define	FIRST_MINUTE	0
131#define	LAST_MINUTE	59
132#define	MINUTE_COUNT	(LAST_MINUTE - FIRST_MINUTE + 1)
133
134#define	FIRST_HOUR	0
135#define	LAST_HOUR	23
136#define	HOUR_COUNT	(LAST_HOUR - FIRST_HOUR + 1)
137
138#define	FIRST_DOM	1
139#define	LAST_DOM	31
140#define	DOM_COUNT	(LAST_DOM - FIRST_DOM + 1)
141
142#define	FIRST_MONTH	1
143#define	LAST_MONTH	12
144#define	MONTH_COUNT	(LAST_MONTH - FIRST_MONTH + 1)
145
146/* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */
147#define	FIRST_DOW	0
148#define	LAST_DOW	7
149#define	DOW_COUNT	(LAST_DOW - FIRST_DOW + 1)
150
151#ifdef LOGIN_CAP
152/* see init.c */
153#define RESOURCE_RC "daemon"
154#endif
155
156			/* each user's crontab will be held as a list of
157			 * the following structure.
158			 *
159			 * These are the cron commands.
160			 */
161
162typedef	struct _entry {
163	struct _entry	*next;
164#ifdef __APPLE__
165	char            uname[MAXLOGNAME];
166	char            gname[MAXLOGNAME]; /* there is no MAXGROUPNAME, but if there were, it'd probably be the same */
167#else
168	uid_t		uid;
169	gid_t		gid;
170#endif
171#ifdef LOGIN_CAP
172	char            *class;
173#endif
174	char		**envp;
175	char		*cmd;
176	bitstr_t	bit_decl(minute, MINUTE_COUNT);
177	bitstr_t	bit_decl(hour,   HOUR_COUNT);
178	bitstr_t	bit_decl(dom,    DOM_COUNT);
179	bitstr_t	bit_decl(month,  MONTH_COUNT);
180	bitstr_t	bit_decl(dow,    DOW_COUNT);
181	int		flags;
182#define	DOM_STAR	0x01
183#define	DOW_STAR	0x02
184#define	WHEN_REBOOT	0x04
185#define	RUN_AT	0x08
186#define	NOT_UNTIL	0x10
187#ifdef __APPLE__
188#define NOT_BATTERY	0x20
189#endif
190	time_t	lastrun;
191} entry;
192
193			/* the crontab database will be a list of the
194			 * following structure, one element per user
195			 * plus one for the system.
196			 *
197			 * These are the crontabs.
198			 */
199
200typedef	struct _user {
201	struct _user	*next, *prev;	/* links */
202	char		*name;
203	time_t		mtime;		/* last modtime of crontab */
204	entry		*crontab;	/* this person's crontab */
205} user;
206
207typedef	struct _cron_db {
208	user		*head, *tail;	/* links */
209	time_t		mtime;		/* last modtime on spooldir */
210} cron_db;
211
212
213void		set_cron_uid __P((void)),
214		set_cron_cwd __P((void)),
215		load_database __P((cron_db *)),
216		open_logfile __P((void)),
217		sigpipe_func __P((void)),
218		job_add __P((entry *, user *)),
219		do_command __P((entry *, user *)),
220		link_user __P((cron_db *, user *)),
221		unlink_user __P((cron_db *, user *)),
222		free_user __P((user *)),
223		env_free __P((char **)),
224		unget_char __P((int, FILE *)),
225		free_entry __P((entry *)),
226		skip_comments __P((FILE *)),
227		log_it __P((char *, int, char *, char *)),
228		log_close __P((void));
229
230int		job_runqueue __P((void)),
231		set_debug_flags __P((char *)),
232		get_char __P((FILE *)),
233		get_string __P((char *, int, FILE *, char *)),
234		swap_uids __P((void)),
235		load_env __P((char *, FILE *)),
236		cron_pclose __P((FILE *)),
237		strcmp_until __P((char *, char *, int)),
238#ifdef __APPLE__
239		allowed __P((char *, int)),
240#else
241		allowed __P((char *)),
242#endif
243		strdtb __P((char *));
244
245char		*env_get __P((char *, char **)),
246		*arpadate __P((time_t *)),
247		*mkprints __P((unsigned char *, unsigned int)),
248		*first_word __P((char *, char *)),
249		**env_init __P((void)),
250		**env_copy __P((char **)),
251		**env_set __P((char **, char *));
252
253user		*load_user __P((int, struct passwd *, char *)),
254		*find_user __P((cron_db *, char *));
255
256entry		*load_entry __P((FILE *, void (*)(),
257#ifdef __APPLE__
258				 char *, char **));
259#else
260				 struct passwd *, char **));
261#endif
262
263FILE		*cron_popen __P((char *, char *, entry *));
264
265
266				/* in the C tradition, we only create
267				 * variables for the main program, just
268				 * extern them elsewhere.
269				 */
270
271#ifdef MAIN_PROGRAM
272# if !defined(LINT) && !defined(lint)
273char	*copyright[] = {
274		"@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie",
275		"@(#) All rights reserved"
276	};
277# endif
278
279char	*MonthNames[] = {
280		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
281		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
282		NULL
283	};
284
285char	*DowNames[] = {
286		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
287		NULL
288	};
289
290char	*ProgramName;
291int	LineNumber;
292unsigned Jitter,
293	RootJitter;
294time_t	TargetTime;
295
296# if DEBUGGING
297int	DebugFlags;
298char	*DebugFlagNames[] = {	/* sync with #defines */
299		"ext", "sch", "proc", "pars", "load", "misc", "test", "bit",
300		NULL		/* NULL must be last element */
301	};
302# endif /* DEBUGGING */
303#else /*MAIN_PROGRAM*/
304extern	char	*copyright[],
305		*MonthNames[],
306		*DowNames[],
307		*ProgramName;
308extern	int	LineNumber;
309extern unsigned	Jitter,
310		RootJitter;
311extern	time_t	TargetTime;
312extern struct pidfh *pfh;
313# if DEBUGGING
314extern	int	DebugFlags;
315extern	char	*DebugFlagNames[];
316# endif /* DEBUGGING */
317#endif /*MAIN_PROGRAM*/
318