summaryrefslogtreecommitdiff
path: root/usr.sbin/cron/user.c
blob: 0c0d25642da14c8c04b7b108b72a01b5e2a0829d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*	$OpenBSD: user.c,v 1.21 2018/02/05 03:52:37 millert Exp $	*/

/* Copyright 1988,1990,1993,1994 by Paul Vixie
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <bitstring.h>		/* for structs.h */
#include <ctype.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>		/* for structs.h */

#include "macros.h"
#include "structs.h"
#include "funcs.h"
#include "globals.h"

void
free_user(user *u)
{
	entry *e;

	while ((e = SLIST_FIRST(&u->crontab))) {
		SLIST_REMOVE_HEAD(&u->crontab, entries);
		free_entry(e);
	}
	free(u->name);
	free(u);
}

static int ParseErrorCount;
static const char *CrontabFilename;

static void
parse_error(const char *msg)
{
	ParseErrorCount++;
	syslog(LOG_ERR, "(CRON) %s:%d (%s)", CrontabFilename, LineNumber, msg);
}

user *
load_user(FILE *file, struct passwd *pw, const char *name)
{
	char envstr[MAX_ENVSTR];
	user *u;
	entry *e;
	int status, save_errno;
	char **envp = NULL, **tenvp;

	CrontabFilename = name;
	LineNumber = 0;

	/* file is open.  build user entry, then read the crontab file.
	 */
	if ((u = malloc(sizeof(user))) == NULL)
		goto done;
	if ((u->name = strdup(name)) == NULL) {
		save_errno = errno;
		free(u);
		u = NULL;
		errno = save_errno;
		goto done;
	}
	SLIST_INIT(&u->crontab);

	/* init environment.  this will be copied/augmented for each entry.
	 */
	if ((envp = env_init()) == NULL) {
		save_errno = errno;
		free_user(u);
		u = NULL;
		errno = save_errno;
		goto done;
	}

	/* load the crontab
	 */
	ParseErrorCount = 0;
	while ((status = load_env(envstr, file)) >= 0) {
		switch (status) {
		case FALSE:
			/* Not an env variable, parse as crontab entry. */
			e = load_entry(file, parse_error, pw, envp);
			if (e == NULL) {
				/* Parse error, ignore for non-root entries */
				if (pw != NULL) {
					save_errno = errno;
					free_user(u);
					u = NULL;
					errno = save_errno;
					goto done;
				}
			} else {
				SLIST_INSERT_HEAD(&u->crontab, e, entries);
			}
			break;
		case TRUE:
			if ((tenvp = env_set(envp, envstr)) == NULL) {
				save_errno = errno;
				free_user(u);
				u = NULL;
				errno = save_errno;
				goto done;
			}
			envp = tenvp;
			break;
		}
	}

 done:
	if (envp != NULL)
		env_free(envp);
	return (u);
}