user.c revision 29452
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)
19static const char rcsid[] =
20	"$Id: user.c,v 1.5 1997/02/22 16:04:47 peter Exp $";
21#endif
22
23/* vix 26jan87 [log is in RCS file]
24 */
25
26
27#include "cron.h"
28
29
30void
31free_user(u)
32	user	*u;
33{
34	entry	*e, *ne;
35
36	free(u->name);
37	for (e = u->crontab;  e != NULL;  e = ne) {
38		ne = e->next;
39		free_entry(e);
40	}
41	free(u);
42}
43
44
45user *
46load_user(crontab_fd, pw, name)
47	int		crontab_fd;
48	struct passwd	*pw;		/* NULL implies syscrontab */
49	char		*name;
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			e = load_entry(file, NULL, pw, envp);
98			if (e) {
99				e->next = u->crontab;
100				u->crontab = e;
101			}
102			break;
103		case TRUE:
104			if ((tenvp = env_set(envp, envstr))) {
105				envp = tenvp;
106			} else {
107				free_user(u);
108				u = NULL;
109				goto done;
110			}
111			break;
112		}
113	}
114
115 done:
116	env_free(envp);
117	fclose(file);
118	Debug(DPARS, ("...load_user() done\n"))
119	return u;
120}
121