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