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