1 /* $NetBSD: database.c,v 1.3 2010/07/15 20:04:14 christos Exp $ */ 2 3 /* Copyright 1988,1990,1993,1994 by Paul Vixie 4 * All rights reserved 5 */ 6 7 /* 8 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 9 * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. 10 * 11 * Permission to use, copy, modify, and distribute this software for any 12 * purpose with or without fee is hereby granted, provided that the above 13 * copyright notice and this permission notice appear in all copies. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 */ 23 #include <sys/cdefs.h> 24 #if !defined(lint) && !defined(LINT) 25 #if 0 26 static char rcsid[] = "Id: database.c,v 1.7 2004/01/23 18:56:42 vixie Exp"; 27 #else 28 __RCSID("$NetBSD: database.c,v 1.3 2010/07/15 20:04:14 christos Exp $"); 29 #endif 30 #endif 31 32 /* vix 26jan87 [RCS has the log] 33 */ 34 35 #include "cron.h" 36 37 #define TMAX(a,b) ((a)>(b)?(a):(b)) 38 39 static void process_crontab(const char *, const char *, 40 const char *, struct stat *, 41 cron_db *, cron_db *); 42 43 void 44 load_database(cron_db *old_db) { 45 struct stat statbuf, syscron_stat; 46 cron_db new_db; 47 DIR_T *dp; 48 DIR *dir; 49 user *u, *nu; 50 51 Debug(DLOAD, ("[%ld] load_database()\n", (long)getpid())); 52 53 /* before we start loading any data, do a stat on SPOOL_DIR 54 * so that if anything changes as of this moment (i.e., before we've 55 * cached any of the database), we'll see the changes next time. 56 */ 57 if (stat(SPOOL_DIR, &statbuf) < OK) { 58 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR); 59 (void) exit(ERROR_EXIT); 60 } 61 62 /* track system crontab file 63 */ 64 if (stat(SYSCRONTAB, &syscron_stat) < OK) 65 syscron_stat.st_mtime = 0; 66 67 /* if spooldir's mtime has not changed, we don't need to fiddle with 68 * the database. 69 * 70 * Note that old_db->mtime is initialized to 0 in main(), and 71 * so is guaranteed to be different than the stat() mtime the first 72 * time this function is called. 73 */ 74 if (old_db->mtime == TMAX(statbuf.st_mtime, syscron_stat.st_mtime)) { 75 Debug(DLOAD, ("[%ld] spool dir mtime unch, no load needed.\n", 76 (long)getpid())); 77 return; 78 } 79 80 /* something's different. make a new database, moving unchanged 81 * elements from the old database, reloading elements that have 82 * actually changed. Whatever is left in the old database when 83 * we're done is chaff -- crontabs that disappeared. 84 */ 85 new_db.mtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime); 86 new_db.head = new_db.tail = NULL; 87 88 if (syscron_stat.st_mtime) 89 process_crontab("root", NULL, SYSCRONTAB, &syscron_stat, 90 &new_db, old_db); 91 92 /* we used to keep this dir open all the time, for the sake of 93 * efficiency. however, we need to close it in every fork, and 94 * we fork a lot more often than the mtime of the dir changes. 95 */ 96 if (!(dir = opendir(SPOOL_DIR))) { 97 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR); 98 (void) exit(ERROR_EXIT); 99 } 100 101 while (NULL != (dp = readdir(dir))) { 102 char fname[MAXNAMLEN+1], tabname[MAXNAMLEN+1]; 103 104 /* avoid file names beginning with ".". this is good 105 * because we would otherwise waste two guaranteed calls 106 * to getpwnam() for . and .., and also because user names 107 * starting with a period are just too nasty to consider. 108 */ 109 if (dp->d_name[0] == '.') 110 continue; 111 112 if (strlen(dp->d_name) >= sizeof fname) 113 continue; /* XXX log? */ 114 (void)strlcpy(fname, dp->d_name, sizeof(fname)); 115 116 if (!glue_strings(tabname, sizeof tabname, SPOOL_DIR, 117 fname, '/')) 118 continue; /* XXX log? */ 119 120 process_crontab(fname, fname, tabname, 121 &statbuf, &new_db, old_db); 122 } 123 (void)closedir(dir); 124 125 /* if we don't do this, then when our children eventually call 126 * getpwnam() in do_command.c's child_process to verify MAILTO=, 127 * they will screw us up (and v-v). 128 */ 129 endpwent(); 130 131 /* whatever's left in the old database is now junk. 132 */ 133 Debug(DLOAD, ("unlinking old database:\n")); 134 for (u = old_db->head; u != NULL; u = nu) { 135 Debug(DLOAD, ("\t%s\n", u->name)); 136 nu = u->next; 137 unlink_user(old_db, u); 138 free_user(u); 139 } 140 141 /* overwrite the database control block with the new one. 142 */ 143 *old_db = new_db; 144 Debug(DLOAD, ("load_database is done\n")); 145 } 146 147 void 148 link_user(cron_db *db, user *u) { 149 if (db->head == NULL) 150 db->head = u; 151 if (db->tail) 152 db->tail->next = u; 153 u->prev = db->tail; 154 u->next = NULL; 155 db->tail = u; 156 } 157 158 void 159 unlink_user(cron_db *db, user *u) { 160 if (u->prev == NULL) 161 db->head = u->next; 162 else 163 u->prev->next = u->next; 164 165 if (u->next == NULL) 166 db->tail = u->prev; 167 else 168 u->next->prev = u->prev; 169 } 170 171 user * 172 find_user(cron_db *db, const char *name) { 173 user *u; 174 175 for (u = db->head; u != NULL; u = u->next) 176 if (strcmp(u->name, name) == 0) 177 break; 178 return (u); 179 } 180 181 static void 182 process_crontab(const char *uname, const char *fname, const char *tabname, 183 struct stat *statbuf, cron_db *new_db, cron_db *old_db) 184 { 185 struct passwd *pw = NULL; 186 int crontab_fd = OK - 1; 187 mode_t eqmode = 0600, badmode = 0; 188 user *u; 189 190 if (fname == NULL) { 191 /* 192 * SYSCRONTAB: 193 * set fname to something for logging purposes. 194 * Allow it to become readable by group and others, but 195 * not writable. 196 */ 197 fname = "*system*"; 198 eqmode = 0; 199 badmode = 022; 200 } else if ((pw = getpwnam(uname)) == NULL) { 201 /* file doesn't have a user in passwd file. 202 */ 203 log_it(fname, getpid(), "ORPHAN", "no passwd entry"); 204 goto next_crontab; 205 } 206 207 if ((crontab_fd = open(tabname, O_RDONLY|O_NONBLOCK|O_NOFOLLOW, 0)) < OK) { 208 /* crontab not accessible? 209 */ 210 log_it(fname, getpid(), "CAN'T OPEN", tabname); 211 goto next_crontab; 212 } 213 214 if (fstat(crontab_fd, statbuf) < OK) { 215 log_it(fname, getpid(), "FSTAT FAILED", tabname); 216 goto next_crontab; 217 } 218 if (!S_ISREG(statbuf->st_mode)) { 219 log_it(fname, getpid(), "NOT REGULAR", tabname); 220 goto next_crontab; 221 } 222 if ((eqmode && (statbuf->st_mode & 07777) != eqmode) || 223 (badmode && (statbuf->st_mode & badmode) != 0)) { 224 log_it(fname, getpid(), "BAD FILE MODE", tabname); 225 goto next_crontab; 226 } 227 if (statbuf->st_uid != ROOT_UID && (pw == NULL || 228 statbuf->st_uid != pw->pw_uid || strcmp(uname, pw->pw_name) != 0)) { 229 log_it(fname, getpid(), "WRONG FILE OWNER", tabname); 230 goto next_crontab; 231 } 232 if (statbuf->st_nlink != 1) { 233 log_it(fname, getpid(), "BAD LINK COUNT", tabname); 234 goto next_crontab; 235 } 236 237 Debug(DLOAD, ("\t%s:", fname)); 238 u = find_user(old_db, fname); 239 if (u != NULL) { 240 /* if crontab has not changed since we last read it 241 * in, then we can just use our existing entry. 242 */ 243 if (u->mtime == statbuf->st_mtime) { 244 Debug(DLOAD, (" [no change, using old data]")); 245 unlink_user(old_db, u); 246 link_user(new_db, u); 247 goto next_crontab; 248 } 249 250 /* before we fall through to the code that will reload 251 * the user, let's deallocate and unlink the user in 252 * the old database. This is more a point of memory 253 * efficiency than anything else, since all leftover 254 * users will be deleted from the old database when 255 * we finish with the crontab... 256 */ 257 Debug(DLOAD, (" [delete old data]")); 258 unlink_user(old_db, u); 259 free_user(u); 260 log_it(fname, getpid(), "RELOAD", tabname); 261 } 262 u = load_user(crontab_fd, pw, fname); 263 if (u != NULL) { 264 u->mtime = statbuf->st_mtime; 265 link_user(new_db, u); 266 } 267 268 next_crontab: 269 if (crontab_fd >= OK) { 270 Debug(DLOAD, (" [done]\n")); 271 (void)close(crontab_fd); 272 } 273 } 274