1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 */
4
5 /*
6 * Copyright (c) 1997 by Internet Software Consortium
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 * SOFTWARE.
20 */
21
22 #if !defined(lint) && !defined(LINT)
23 static const char rcsid[] = "$Id: user.c,v 1.2 1998/08/14 00:32:41 vixie Exp $";
24 #endif
25
26 /* vix 26jan87 [log is in RCS file]
27 */
28
29 #include "cron.h"
30
31 static char *User_name;
32
33 void
free_user(user * u)34 free_user(user *u)
35 {
36 entry *e, *ne;
37
38 free(u->name);
39 for (e = u->crontab; e != NULL; e = ne) {
40 ne = e->next;
41 free_entry(e);
42 }
43 free(u);
44 }
45
46 static void
log_error(const char * msg)47 log_error(const char *msg)
48 {
49 log_it(User_name, getpid(), "PARSE", msg);
50 }
51
52 /* NULL pw implies syscrontab */
53 user *
load_user(int crontab_fd,struct passwd * pw,const char * name)54 load_user(int crontab_fd, struct passwd *pw, const char *name)
55 {
56 char envstr[MAX_ENVSTR];
57 FILE *file;
58 user *u;
59 entry *e;
60 int status;
61 char **envp, **tenvp;
62
63 if (!(file = fdopen(crontab_fd, "r"))) {
64 warn("fdopen on crontab_fd in load_user");
65 return (NULL);
66 }
67
68 Debug(DPARS, ("load_user()\n"))
69
70 /* file is open. build user entry, then read the crontab file.
71 */
72 if ((u = (user *) malloc(sizeof(user))) == NULL) {
73 errno = ENOMEM;
74 return (NULL);
75 }
76 if ((u->name = strdup(name)) == NULL) {
77 free(u);
78 errno = ENOMEM;
79 return (NULL);
80 }
81 u->crontab = NULL;
82
83 /*
84 * init environment. this will be copied/augmented for each entry.
85 */
86 if ((envp = env_init()) == NULL) {
87 free(u->name);
88 free(u);
89 return (NULL);
90 }
91
92 /*
93 * load the crontab
94 */
95 while ((status = load_env(envstr, file)) >= OK) {
96 switch (status) {
97 case ERR:
98 free_user(u);
99 u = NULL;
100 goto done;
101 case FALSE:
102 User_name = u->name; /* for log_error */
103 e = load_entry(file, log_error, pw, envp);
104 if (e) {
105 e->next = u->crontab;
106 u->crontab = e;
107 }
108 break;
109 case TRUE:
110 if ((tenvp = env_set(envp, envstr))) {
111 envp = tenvp;
112 } else {
113 free_user(u);
114 u = NULL;
115 goto done;
116 }
117 break;
118 }
119 }
120
121 done:
122 env_free(envp);
123 fclose(file);
124 Debug(DPARS, ("...load_user() done\n"))
125 return (u);
126 }
127