xref: /openbsd/usr.sbin/cron/user.c (revision 274d7c50)
1 /*	$OpenBSD: user.c,v 1.21 2018/02/05 03:52:37 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 #include "globals.h"
36 
37 void
38 free_user(user *u)
39 {
40 	entry *e;
41 
42 	while ((e = SLIST_FIRST(&u->crontab))) {
43 		SLIST_REMOVE_HEAD(&u->crontab, entries);
44 		free_entry(e);
45 	}
46 	free(u->name);
47 	free(u);
48 }
49 
50 static int ParseErrorCount;
51 static const char *CrontabFilename;
52 
53 static void
54 parse_error(const char *msg)
55 {
56 	ParseErrorCount++;
57 	syslog(LOG_ERR, "(CRON) %s:%d (%s)", CrontabFilename, LineNumber, msg);
58 }
59 
60 user *
61 load_user(FILE *file, struct passwd *pw, const char *name)
62 {
63 	char envstr[MAX_ENVSTR];
64 	user *u;
65 	entry *e;
66 	int status, save_errno;
67 	char **envp = NULL, **tenvp;
68 
69 	CrontabFilename = name;
70 	LineNumber = 0;
71 
72 	/* file is open.  build user entry, then read the crontab file.
73 	 */
74 	if ((u = malloc(sizeof(user))) == NULL)
75 		goto done;
76 	if ((u->name = strdup(name)) == NULL) {
77 		save_errno = errno;
78 		free(u);
79 		u = NULL;
80 		errno = save_errno;
81 		goto done;
82 	}
83 	SLIST_INIT(&u->crontab);
84 
85 	/* init environment.  this will be copied/augmented for each entry.
86 	 */
87 	if ((envp = env_init()) == NULL) {
88 		save_errno = errno;
89 		free_user(u);
90 		u = NULL;
91 		errno = save_errno;
92 		goto done;
93 	}
94 
95 	/* load the crontab
96 	 */
97 	ParseErrorCount = 0;
98 	while ((status = load_env(envstr, file)) >= 0) {
99 		switch (status) {
100 		case FALSE:
101 			/* Not an env variable, parse as crontab entry. */
102 			e = load_entry(file, parse_error, pw, envp);
103 			if (e == NULL) {
104 				/* Parse error, ignore for non-root entries */
105 				if (pw != NULL) {
106 					save_errno = errno;
107 					free_user(u);
108 					u = NULL;
109 					errno = save_errno;
110 					goto done;
111 				}
112 			} else {
113 				SLIST_INSERT_HEAD(&u->crontab, e, entries);
114 			}
115 			break;
116 		case TRUE:
117 			if ((tenvp = env_set(envp, envstr)) == NULL) {
118 				save_errno = errno;
119 				free_user(u);
120 				u = NULL;
121 				errno = save_errno;
122 				goto done;
123 			}
124 			envp = tenvp;
125 			break;
126 		}
127 	}
128 
129  done:
130 	if (envp != NULL)
131 		env_free(envp);
132 	return (u);
133 }
134