1 /* $NetBSD: user.c,v 1.3 2020/04/18 19:32:19 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.3 2020/04/18 19:32:19 christos Exp $");
29 #endif
30 #endif
31
32 /* vix 26jan87 [log is in RCS file]
33 */
34
35 #include "cron.h"
36
37 void
free_user(user * u)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 job_remove(e, u);
45 free_entry(e);
46 }
47 free(u);
48 }
49
50 user *
load_user(int crontab_fd,struct passwd * pw,const char * name)51 load_user(int crontab_fd, struct passwd *pw, const char *name) {
52 char envstr[MAX_ENVSTR];
53 FILE *file;
54 user *u;
55 entry *e;
56 int status, save_errno;
57 char **envp, **tenvp;
58
59 if (!(file = fdopen(crontab_fd, "r"))) {
60 perror("fdopen on crontab_fd in load_user");
61 return (NULL);
62 }
63
64 Debug(DPARS, ("load_user()\n"));
65
66 /* file is open. build user entry, then read the crontab file.
67 */
68 if ((u = malloc(sizeof(*u))) == NULL)
69 return (NULL);
70 if ((u->name = strdup(name)) == NULL) {
71 save_errno = errno;
72 free(u);
73 errno = save_errno;
74 return (NULL);
75 }
76 u->crontab = NULL;
77
78 /* init environment. this will be copied/augmented for each entry.
79 */
80 if ((envp = env_init()) == NULL) {
81 save_errno = errno;
82 free(u->name);
83 free(u);
84 errno = save_errno;
85 return (NULL);
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)) == NULL) {
105 save_errno = errno;
106 free_user(u);
107 u = NULL;
108 errno = save_errno;
109 goto done;
110 }
111 envp = tenvp;
112 break;
113 }
114 }
115
116 done:
117 env_free(envp);
118 (void)fclose(file);
119 Debug(DPARS, ("...load_user() done\n"));
120 return (u);
121 }
122