xref: /netbsd/external/bsd/cron/dist/user.c (revision 6550d01e)
1 /*	$NetBSD: user.c,v 1.2 2010/05/06 18:53:17 christos Exp $	*/
2 
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4  * All rights reserved
5  */
6 
7 /*
8  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23 #include <sys/cdefs.h>
24 #if !defined(lint) && !defined(LINT)
25 #if 0
26 static char rcsid[] = "Id: user.c,v 1.5 2004/01/23 18:56:43 vixie Exp";
27 +#else
28 +__RCSID("$NetBSD: user.c,v 1.2 2010/05/06 18:53:17 christos Exp $");
29 #endif
30 #endif
31 
32 /* vix 26jan87 [log is in RCS file]
33  */
34 
35 #include "cron.h"
36 
37 void
38 free_user(user *u) {
39 	entry *e, *ne;
40 
41 	free(u->name);
42 	for (e = u->crontab;  e != NULL;  e = ne) {
43 		ne = e->next;
44 		free_entry(e);
45 	}
46 	free(u);
47 }
48 
49 user *
50 load_user(int crontab_fd, struct passwd	*pw, const char *name) {
51 	char envstr[MAX_ENVSTR];
52 	FILE *file;
53 	user *u;
54 	entry *e;
55 	int status, save_errno;
56 	char **envp, **tenvp;
57 
58 	if (!(file = fdopen(crontab_fd, "r"))) {
59 		perror("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 = malloc(sizeof(*u))) == NULL)
68 		return (NULL);
69 	if ((u->name = strdup(name)) == NULL) {
70 		save_errno = errno;
71 		free(u);
72 		errno = save_errno;
73 		return (NULL);
74 	}
75 	u->crontab = NULL;
76 
77 	/* init environment.  this will be copied/augmented for each entry.
78 	 */
79 	if ((envp = env_init()) == NULL) {
80 		save_errno = errno;
81 		free(u->name);
82 		free(u);
83 		errno = save_errno;
84 		return (NULL);
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)) == NULL) {
104 				save_errno = errno;
105 				free_user(u);
106 				u = NULL;
107 				errno = save_errno;
108 				goto done;
109 			}
110 			envp = tenvp;
111 			break;
112 		}
113 	}
114 
115  done:
116 	env_free(envp);
117 	(void)fclose(file);
118 	Debug(DPARS, ("...load_user() done\n"));
119 	return (u);
120 }
121