GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/cron/user.c Lines: 0 46 0.0 %
Date: 2017-11-13 Branches: 0 23 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: user.c,v 1.20 2017/06/07 23:36:43 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(int crontab_fd, struct passwd	*pw, const char *name)
62
{
63
	char envstr[MAX_ENVSTR];
64
	FILE *file;
65
	user *u;
66
	entry *e;
67
	int status, save_errno;
68
	char **envp = NULL, **tenvp;
69
70
	if (!(file = fdopen(crontab_fd, "r"))) {
71
		syslog(LOG_ERR, "(%s) FDOPEN (%m)", name);
72
		return (NULL);
73
	}
74
	CrontabFilename = name;
75
	LineNumber = 0;
76
77
	/* file is open.  build user entry, then read the crontab file.
78
	 */
79
	if ((u = malloc(sizeof(user))) == NULL)
80
		goto done;
81
	if ((u->name = strdup(name)) == NULL) {
82
		save_errno = errno;
83
		free(u);
84
		u = NULL;
85
		errno = save_errno;
86
		goto done;
87
	}
88
	SLIST_INIT(&u->crontab);
89
90
	/* init environment.  this will be copied/augmented for each entry.
91
	 */
92
	if ((envp = env_init()) == NULL) {
93
		save_errno = errno;
94
		free_user(u);
95
		u = NULL;
96
		errno = save_errno;
97
		goto done;
98
	}
99
100
	/* load the crontab
101
	 */
102
	ParseErrorCount = 0;
103
	while ((status = load_env(envstr, file)) >= 0) {
104
		switch (status) {
105
		case FALSE:
106
			/* Not an env variable, parse as crontab entry. */
107
			e = load_entry(file, parse_error, pw, envp);
108
			if (e == NULL) {
109
				/* Parse error, ignore for non-root entries */
110
				if (pw != NULL) {
111
					save_errno = errno;
112
					free_user(u);
113
					u = NULL;
114
					errno = save_errno;
115
					goto done;
116
				}
117
			} else {
118
				SLIST_INSERT_HEAD(&u->crontab, e, entries);
119
			}
120
			break;
121
		case TRUE:
122
			if ((tenvp = env_set(envp, envstr)) == NULL) {
123
				save_errno = errno;
124
				free_user(u);
125
				u = NULL;
126
				errno = save_errno;
127
				goto done;
128
			}
129
			envp = tenvp;
130
			break;
131
		}
132
	}
133
134
 done:
135
	if (envp != NULL)
136
		env_free(envp);
137
	fclose(file);
138
	return (u);
139
}