database.c revision 320222
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#if !defined(lint) && !defined(LINT)
19static const char rcsid[] =
20  "$FreeBSD: stable/10/usr.sbin/cron/cron/database.c 320222 2017-06-22 07:08:18Z ngie $";
21#endif
22
23/* vix 26jan87 [RCS has the log]
24 */
25
26
27#include "cron.h"
28#include <fcntl.h>
29#include <sys/stat.h>
30#include <sys/file.h>
31
32
33#define TMAX(a,b) ((a)>(b)?(a):(b))
34
35
36static	void		process_crontab(char *, char *, char *,
37					     struct stat *,
38					     cron_db *, cron_db *);
39
40
41void
42load_database(old_db)
43	cron_db		*old_db;
44{
45	DIR		*dir;
46	struct stat	statbuf;
47	struct stat	syscron_stat;
48	time_t		maxmtime;
49	DIR_T   	*dp;
50	cron_db		new_db;
51	user		*u, *nu;
52	struct {
53		const char *name;
54		struct stat st;
55	} syscrontabs [] = {
56		{ SYSCRONTABS },
57		{ LOCALSYSCRONTABS }
58	};
59	int i;
60
61	Debug(DLOAD, ("[%d] load_database()\n", getpid()))
62
63	/* before we start loading any data, do a stat on SPOOL_DIR
64	 * so that if anything changes as of this moment (i.e., before we've
65	 * cached any of the database), we'll see the changes next time.
66	 */
67	if (stat(SPOOL_DIR, &statbuf) < OK) {
68		log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR);
69		(void) exit(ERROR_EXIT);
70	}
71
72	/* track system crontab file
73	 */
74	if (stat(SYSCRONTAB, &syscron_stat) < OK)
75		syscron_stat.st_mtime = 0;
76
77	maxmtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime);
78
79	for (i = 0; i < nitems(syscrontabs); i++) {
80		if (stat(syscrontabs[i].name, &syscrontabs[i].st) != -1) {
81			maxmtime = TMAX(syscrontabs[i].st.st_mtime, maxmtime);
82		} else {
83			syscrontabs[i].st.st_mtime = 0;
84		}
85	}
86
87	/* if spooldir's mtime has not changed, we don't need to fiddle with
88	 * the database.
89	 *
90	 * Note that old_db->mtime is initialized to 0 in main(), and
91	 * so is guaranteed to be different than the stat() mtime the first
92	 * time this function is called.
93	 */
94	if (old_db->mtime == maxmtime) {
95		Debug(DLOAD, ("[%d] spool dir mtime unch, no load needed.\n",
96			      getpid()))
97		return;
98	}
99
100	/* something's different.  make a new database, moving unchanged
101	 * elements from the old database, reloading elements that have
102	 * actually changed.  Whatever is left in the old database when
103	 * we're done is chaff -- crontabs that disappeared.
104	 */
105	new_db.mtime = maxmtime;
106	new_db.head = new_db.tail = NULL;
107
108	if (syscron_stat.st_mtime) {
109		process_crontab("root", SYS_NAME,
110				SYSCRONTAB, &syscron_stat,
111				&new_db, old_db);
112	}
113
114	for (i = 0; i < nitems(syscrontabs); i++) {
115		char tabname[MAXPATHLEN];
116		if (syscrontabs[i].st.st_mtime == 0)
117			continue;
118		if (!(dir = opendir(syscrontabs[i].name))) {
119			log_it("CRON", getpid(), "OPENDIR FAILED",
120			    syscrontabs[i].name);
121			(void) exit(ERROR_EXIT);
122		}
123
124		while (NULL != (dp = readdir(dir))) {
125			if (dp->d_name[0] == '.')
126				continue;
127			if (dp->d_type != DT_REG)
128				continue;
129			snprintf(tabname, sizeof(tabname), "%s/%s",
130			    syscrontabs[i].name, dp->d_name);
131			process_crontab("root", SYS_NAME, tabname,
132			    &syscrontabs[i].st, &new_db, old_db);
133		}
134		closedir(dir);
135	}
136
137	/* we used to keep this dir open all the time, for the sake of
138	 * efficiency.  however, we need to close it in every fork, and
139	 * we fork a lot more often than the mtime of the dir changes.
140	 */
141	if (!(dir = opendir(SPOOL_DIR))) {
142		log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR);
143		(void) exit(ERROR_EXIT);
144	}
145
146	while (NULL != (dp = readdir(dir))) {
147		char	fname[MAXNAMLEN+1],
148			tabname[MAXNAMLEN+1];
149
150		/* avoid file names beginning with ".".  this is good
151		 * because we would otherwise waste two guaranteed calls
152		 * to getpwnam() for . and .., and also because user names
153		 * starting with a period are just too nasty to consider.
154		 */
155		if (dp->d_name[0] == '.')
156			continue;
157
158		(void) strncpy(fname, dp->d_name, sizeof(fname));
159		fname[sizeof(fname)-1] = '\0';
160		(void) snprintf(tabname, sizeof tabname, CRON_TAB(fname));
161
162		process_crontab(fname, fname, tabname,
163				&statbuf, &new_db, old_db);
164	}
165	closedir(dir);
166
167	/* if we don't do this, then when our children eventually call
168	 * getpwnam() in do_command.c's child_process to verify MAILTO=,
169	 * they will screw us up (and v-v).
170	 */
171	endpwent();
172
173	/* whatever's left in the old database is now junk.
174	 */
175	Debug(DLOAD, ("unlinking old database:\n"))
176	for (u = old_db->head;  u != NULL;  u = nu) {
177		Debug(DLOAD, ("\t%s\n", u->name))
178		nu = u->next;
179		unlink_user(old_db, u);
180		free_user(u);
181	}
182
183	/* overwrite the database control block with the new one.
184	 */
185	*old_db = new_db;
186	Debug(DLOAD, ("load_database is done\n"))
187}
188
189
190void
191link_user(db, u)
192	cron_db	*db;
193	user	*u;
194{
195	if (db->head == NULL)
196		db->head = u;
197	if (db->tail)
198		db->tail->next = u;
199	u->prev = db->tail;
200	u->next = NULL;
201	db->tail = u;
202}
203
204
205void
206unlink_user(db, u)
207	cron_db	*db;
208	user	*u;
209{
210	if (u->prev == NULL)
211		db->head = u->next;
212	else
213		u->prev->next = u->next;
214
215	if (u->next == NULL)
216		db->tail = u->prev;
217	else
218		u->next->prev = u->prev;
219}
220
221
222user *
223find_user(db, name)
224	cron_db	*db;
225	char	*name;
226{
227	char	*env_get();
228	user	*u;
229
230	for (u = db->head;  u != NULL;  u = u->next)
231		if (!strcmp(u->name, name))
232			break;
233	return u;
234}
235
236
237static void
238process_crontab(uname, fname, tabname, statbuf, new_db, old_db)
239	char		*uname;
240	char		*fname;
241	char		*tabname;
242	struct stat	*statbuf;
243	cron_db		*new_db;
244	cron_db		*old_db;
245{
246	struct passwd	*pw = NULL;
247	int		crontab_fd = OK - 1;
248	user		*u;
249
250	if (strcmp(fname, SYS_NAME) && !(pw = getpwnam(uname))) {
251		/* file doesn't have a user in passwd file.
252		 */
253		log_it(fname, getpid(), "ORPHAN", "no passwd entry");
254		goto next_crontab;
255	}
256
257	if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
258		/* crontab not accessible?
259		 */
260		log_it(fname, getpid(), "CAN'T OPEN", tabname);
261		goto next_crontab;
262	}
263
264	if (fstat(crontab_fd, statbuf) < OK) {
265		log_it(fname, getpid(), "FSTAT FAILED", tabname);
266		goto next_crontab;
267	}
268
269	Debug(DLOAD, ("\t%s:", fname))
270	u = find_user(old_db, fname);
271	if (u != NULL) {
272		/* if crontab has not changed since we last read it
273		 * in, then we can just use our existing entry.
274		 */
275		if (u->mtime == statbuf->st_mtime) {
276			Debug(DLOAD, (" [no change, using old data]"))
277			unlink_user(old_db, u);
278			link_user(new_db, u);
279			goto next_crontab;
280		}
281
282		/* before we fall through to the code that will reload
283		 * the user, let's deallocate and unlink the user in
284		 * the old database.  This is more a point of memory
285		 * efficiency than anything else, since all leftover
286		 * users will be deleted from the old database when
287		 * we finish with the crontab...
288		 */
289		Debug(DLOAD, (" [delete old data]"))
290		unlink_user(old_db, u);
291		free_user(u);
292		log_it(fname, getpid(), "RELOAD", tabname);
293	}
294	u = load_user(crontab_fd, pw, fname);
295	if (u != NULL) {
296		u->mtime = statbuf->st_mtime;
297		link_user(new_db, u);
298	}
299
300next_crontab:
301	if (crontab_fd >= OK) {
302		Debug(DLOAD, (" [done]\n"))
303		close(crontab_fd);
304	}
305}
306