xref: /openbsd/usr.sbin/cron/user.c (revision 8a6f8ff3)
1 /*	$OpenBSD: user.c,v 1.17 2015/11/09 01:12:27 millert Exp $	*/
2 
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 
22 #include <bitstring.h>		/* for structs.h */
23 #include <ctype.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>		/* for structs.h */
29 
30 #include "macros.h"
31 #include "structs.h"
32 #include "funcs.h"
33 
34 void
35 free_user(user *u)
36 {
37 	entry *e;
38 
39 	while ((e = SLIST_FIRST(&u->crontab))) {
40 		SLIST_REMOVE_HEAD(&u->crontab, entries);
41 		free_entry(e);
42 	}
43 	free(u->name);
44 	free(u);
45 }
46 
47 user *
48 load_user(int crontab_fd, struct passwd	*pw, const char *name)
49 {
50 	char envstr[MAX_ENVSTR];
51 	FILE *file;
52 	user *u;
53 	entry *e;
54 	int status, save_errno;
55 	char **envp, **tenvp;
56 
57 	if (!(file = fdopen(crontab_fd, "r"))) {
58 		perror("fdopen on crontab_fd in load_user");
59 		return (NULL);
60 	}
61 
62 	/* file is open.  build user entry, then read the crontab file.
63 	 */
64 	if ((u = malloc(sizeof(user))) == NULL)
65 		return (NULL);
66 	if ((u->name = strdup(name)) == NULL) {
67 		save_errno = errno;
68 		free(u);
69 		errno = save_errno;
70 		return (NULL);
71 	}
72 	SLIST_INIT(&u->crontab);
73 
74 	/* init environment.  this will be copied/augmented for each entry.
75 	 */
76 	if ((envp = env_init()) == NULL) {
77 		save_errno = errno;
78 		free(u->name);
79 		free(u);
80 		errno = save_errno;
81 		return (NULL);
82 	}
83 
84 	/* load the crontab
85 	 */
86 	while ((status = load_env(envstr, file)) >= 0) {
87 		switch (status) {
88 		case FALSE:
89 			/* Not an env variable, parse as crontab entry. */
90 			e = load_entry(file, NULL, pw, envp);
91 			if (e)
92 				SLIST_INSERT_HEAD(&u->crontab, e, entries);
93 			break;
94 		case TRUE:
95 			if ((tenvp = env_set(envp, envstr)) == NULL) {
96 				save_errno = errno;
97 				free_user(u);
98 				u = NULL;
99 				errno = save_errno;
100 				goto done;
101 			}
102 			envp = tenvp;
103 			break;
104 		}
105 	}
106 
107  done:
108 	env_free(envp);
109 	fclose(file);
110 	return (u);
111 }
112