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) 19 static char rcsid[] = "$Id: user.c,v 1.2 1996/11/01 23:27:39 millert Exp $"; 20 #endif 21 22 /* vix 26jan87 [log is in RCS file] 23 */ 24 25 26 #include "cron.h" 27 28 29 void 30 free_user(u) 31 user *u; 32 { 33 entry *e, *ne; 34 35 free(u->name); 36 for (e = u->crontab; e != NULL; e = ne) { 37 ne = e->next; 38 free_entry(e); 39 } 40 free(u); 41 } 42 43 44 user * 45 load_user(crontab_fd, pw, name) 46 int crontab_fd; 47 struct passwd *pw; /* NULL implies syscrontab */ 48 char *name; 49 { 50 char envstr[MAX_ENVSTR]; 51 FILE *file; 52 user *u; 53 entry *e; 54 int status; 55 char **envp, **tenvp; 56 57 if (!(file = fdopen(crontab_fd, "r"))) { 58 perror("fdopen on crontab_fd in load_user"); 59 return NULL; 60 } 61 62 Debug(DPARS, ("load_user()\n")) 63 64 /* file is open. build user entry, then read the crontab file. 65 */ 66 if ((u = (user *) malloc(sizeof(user))) == NULL) { 67 errno = ENOMEM; 68 return NULL; 69 } 70 if ((u->name = strdup(name)) == NULL) { 71 free(u); 72 errno = ENOMEM; 73 return NULL; 74 } 75 u->crontab = NULL; 76 77 /* 78 * init environment. this will be copied/augmented for each entry. 79 */ 80 if ((envp = env_init()) == NULL) { 81 free(u->name); 82 free(u); 83 return NULL; 84 } 85 86 /* 87 * load the crontab 88 */ 89 while ((status = load_env(envstr, file)) >= OK) { 90 switch (status) { 91 case ERR: 92 free_user(u); 93 u = NULL; 94 goto done; 95 case FALSE: 96 e = load_entry(file, NULL, pw, envp); 97 if (e) { 98 e->next = u->crontab; 99 u->crontab = e; 100 } 101 break; 102 case TRUE: 103 if ((tenvp = env_set(envp, envstr))) { 104 envp = tenvp; 105 } else { 106 free_user(u); 107 u = NULL; 108 goto done; 109 } 110 break; 111 } 112 } 113 114 done: 115 env_free(envp); 116 fclose(file); 117 Debug(DPARS, ("...load_user() done\n")) 118 return u; 119 } 120