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