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