xref: /openbsd-src/usr.sbin/cron/user.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: user.c,v 1.19 2016/08/30 14:08:16 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 <pwd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <time.h>		/* for structs.h */
31 
32 #include "macros.h"
33 #include "structs.h"
34 #include "funcs.h"
35 
36 void
37 free_user(user *u)
38 {
39 	entry *e;
40 
41 	while ((e = SLIST_FIRST(&u->crontab))) {
42 		SLIST_REMOVE_HEAD(&u->crontab, entries);
43 		free_entry(e);
44 	}
45 	free(u->name);
46 	free(u);
47 }
48 
49 user *
50 load_user(int crontab_fd, struct passwd	*pw, const char *name)
51 {
52 	char envstr[MAX_ENVSTR];
53 	FILE *file;
54 	user *u;
55 	entry *e;
56 	int status, save_errno;
57 	char **envp = NULL, **tenvp;
58 
59 	if (!(file = fdopen(crontab_fd, "r"))) {
60 		syslog(LOG_ERR, "(%s) FDOPEN (%m)", pw->pw_name);
61 		return (NULL);
62 	}
63 
64 	/* file is open.  build user entry, then read the crontab file.
65 	 */
66 	if ((u = malloc(sizeof(user))) == NULL)
67 		goto done;
68 	if ((u->name = strdup(name)) == NULL) {
69 		save_errno = errno;
70 		free(u);
71 		u = NULL;
72 		errno = save_errno;
73 		goto done;
74 	}
75 	SLIST_INIT(&u->crontab);
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_user(u);
82 		u = NULL;
83 		errno = save_errno;
84 		goto done;
85 	}
86 
87 	/* load the crontab
88 	 */
89 	while ((status = load_env(envstr, file)) >= 0) {
90 		switch (status) {
91 		case FALSE:
92 			/* Not an env variable, parse as crontab entry. */
93 			e = load_entry(file, NULL, pw, envp);
94 			if (e)
95 				SLIST_INSERT_HEAD(&u->crontab, e, entries);
96 			break;
97 		case TRUE:
98 			if ((tenvp = env_set(envp, envstr)) == NULL) {
99 				save_errno = errno;
100 				free_user(u);
101 				u = NULL;
102 				errno = save_errno;
103 				goto done;
104 			}
105 			envp = tenvp;
106 			break;
107 		}
108 	}
109 
110  done:
111 	if (envp != NULL)
112 		env_free(envp);
113 	fclose(file);
114 	return (u);
115 }
116