GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/doas/doas.c Lines: 38 201 18.9 %
Date: 2017-11-13 Branches: 15 157 9.6 %

Line Branch Exec Source
1
/* $OpenBSD: doas.c,v 1.72 2017/05/27 09:51:07 tedu Exp $ */
2
/*
3
 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include <sys/types.h>
19
#include <sys/stat.h>
20
#include <sys/ioctl.h>
21
22
#include <limits.h>
23
#include <login_cap.h>
24
#include <bsd_auth.h>
25
#include <readpassphrase.h>
26
#include <string.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <err.h>
30
#include <unistd.h>
31
#include <pwd.h>
32
#include <grp.h>
33
#include <syslog.h>
34
#include <errno.h>
35
#include <fcntl.h>
36
37
#include "doas.h"
38
39
static void __dead
40
usage(void)
41
{
42
	fprintf(stderr, "usage: doas [-Lns] [-a style] [-C config] [-u user]"
43
	    " command [args]\n");
44
	exit(1);
45
}
46
47
static int
48
parseuid(const char *s, uid_t *uid)
49
{
50
	struct passwd *pw;
51
	const char *errstr;
52
53
	if ((pw = getpwnam(s)) != NULL) {
54
		*uid = pw->pw_uid;
55
		return 0;
56
	}
57
	*uid = strtonum(s, 0, UID_MAX, &errstr);
58
	if (errstr)
59
		return -1;
60
	return 0;
61
}
62
63
static int
64
uidcheck(const char *s, uid_t desired)
65
{
66
	uid_t uid;
67
68
	if (parseuid(s, &uid) != 0)
69
		return -1;
70
	if (uid != desired)
71
		return -1;
72
	return 0;
73
}
74
75
static int
76
parsegid(const char *s, gid_t *gid)
77
{
78
	struct group *gr;
79
	const char *errstr;
80
81
	if ((gr = getgrnam(s)) != NULL) {
82
		*gid = gr->gr_gid;
83
		return 0;
84
	}
85
	*gid = strtonum(s, 0, GID_MAX, &errstr);
86
	if (errstr)
87
		return -1;
88
	return 0;
89
}
90
91
static int
92
match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
93
    const char **cmdargs, struct rule *r)
94
{
95
	int i;
96
97
	if (r->ident[0] == ':') {
98
		gid_t rgid;
99
		if (parsegid(r->ident + 1, &rgid) == -1)
100
			return 0;
101
		for (i = 0; i < ngroups; i++) {
102
			if (rgid == groups[i])
103
				break;
104
		}
105
		if (i == ngroups)
106
			return 0;
107
	} else {
108
		if (uidcheck(r->ident, uid) != 0)
109
			return 0;
110
	}
111
	if (r->target && uidcheck(r->target, target) != 0)
112
		return 0;
113
	if (r->cmd) {
114
		if (strcmp(r->cmd, cmd))
115
			return 0;
116
		if (r->cmdargs) {
117
			/* if arguments were given, they should match explicitly */
118
			for (i = 0; r->cmdargs[i]; i++) {
119
				if (!cmdargs[i])
120
					return 0;
121
				if (strcmp(r->cmdargs[i], cmdargs[i]))
122
					return 0;
123
			}
124
			if (cmdargs[i])
125
				return 0;
126
		}
127
	}
128
	return 1;
129
}
130
131
static int
132
permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
133
    uid_t target, const char *cmd, const char **cmdargs)
134
{
135
	int i;
136
137
	*lastr = NULL;
138
	for (i = 0; i < nrules; i++) {
139
		if (match(uid, groups, ngroups, target, cmd,
140
		    cmdargs, rules[i]))
141
			*lastr = rules[i];
142
	}
143
	if (!*lastr)
144
		return 0;
145
	return (*lastr)->action == PERMIT;
146
}
147
148
static void
149
parseconfig(const char *filename, int checkperms)
150
{
151
	extern FILE *yyfp;
152
	extern int yyparse(void);
153
20
	struct stat sb;
154
155
10
	yyfp = fopen(filename, "r");
156
10
	if (!yyfp)
157
		err(1, checkperms ? "doas is not enabled, %s" :
158
		    "could not open config file %s", filename);
159
160
10
	if (checkperms) {
161
		if (fstat(fileno(yyfp), &sb) != 0)
162
			err(1, "fstat(\"%s\")", filename);
163
		if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
164
			errx(1, "%s is writable by group or other", filename);
165
		if (sb.st_uid != 0)
166
			errx(1, "%s is not owned by root", filename);
167
	}
168
169
10
	yyparse();
170
10
	fclose(yyfp);
171
10
	if (parse_errors)
172
		exit(1);
173
5
}
174
175
static void __dead
176
checkconfig(const char *confpath, int argc, char **argv,
177
    uid_t uid, gid_t *groups, int ngroups, uid_t target)
178
{
179
15
	const struct rule *rule;
180
181
5
	setresuid(uid, uid, uid);
182
5
	parseconfig(confpath, 0);
183
5
	if (!argc)
184
		exit(0);
185
186
	if (permit(uid, groups, ngroups, &rule, target, argv[0],
187
	    (const char **)argv + 1)) {
188
		printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
189
		exit(0);
190
	} else {
191
		printf("deny\n");
192
		exit(1);
193
	}
194
}
195
196
static void
197
authuser(char *myname, char *login_style, int persist)
198
{
199
	char *challenge = NULL, *response, rbuf[1024], cbuf[128];
200
	auth_session_t *as;
201
	int fd = -1;
202
203
	if (persist)
204
		fd = open("/dev/tty", O_RDWR);
205
	if (fd != -1) {
206
		if (ioctl(fd, TIOCCHKVERAUTH) == 0)
207
			goto good;
208
	}
209
210
	if (!(as = auth_userchallenge(myname, login_style, "auth-doas",
211
	    &challenge)))
212
		errx(1, "Authorization failed");
213
	if (!challenge) {
214
		char host[HOST_NAME_MAX + 1];
215
		if (gethostname(host, sizeof(host)))
216
			snprintf(host, sizeof(host), "?");
217
		snprintf(cbuf, sizeof(cbuf),
218
		    "\rdoas (%.32s@%.32s) password: ", myname, host);
219
		challenge = cbuf;
220
	}
221
	response = readpassphrase(challenge, rbuf, sizeof(rbuf),
222
	    RPP_REQUIRE_TTY);
223
	if (response == NULL && errno == ENOTTY) {
224
		syslog(LOG_AUTHPRIV | LOG_NOTICE,
225
		    "tty required for %s", myname);
226
		errx(1, "a tty is required");
227
	}
228
	if (!auth_userresponse(as, response, 0)) {
229
		syslog(LOG_AUTHPRIV | LOG_NOTICE,
230
		    "failed auth for %s", myname);
231
		errx(1, "Authorization failed");
232
	}
233
	explicit_bzero(rbuf, sizeof(rbuf));
234
good:
235
	if (fd != -1) {
236
		int secs = 5 * 60;
237
		ioctl(fd, TIOCSETVERAUTH, &secs);
238
		close(fd);
239
	}
240
}
241
242
int
243
main(int argc, char **argv)
244
{
245
	const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
246
	    "/usr/local/bin:/usr/local/sbin";
247
	const char *confpath = NULL;
248
20
	char *shargv[] = { NULL, NULL };
249
	char *sh;
250
	const char *cmd;
251
10
	char cmdline[LINE_MAX];
252
10
	char myname[_PW_NAME_LEN + 1];
253
	struct passwd *pw;
254
10
	const struct rule *rule;
255
	uid_t uid;
256
10
	uid_t target = 0;
257
10
	gid_t groups[NGROUPS_MAX + 1];
258
	int ngroups;
259
	int i, ch;
260
	int sflag = 0;
261
	int nflag = 0;
262
10
	char cwdpath[PATH_MAX];
263
	const char *cwd;
264
	char *login_style = NULL;
265
	char **envp;
266
267
10
	setprogname("doas");
268
269
10
	closefrom(STDERR_FILENO + 1);
270
271
10
	uid = getuid();
272
273
40
	while ((ch = getopt(argc, argv, "a:C:Lnsu:")) != -1) {
274

10
		switch (ch) {
275
		case 'a':
276
			login_style = optarg;
277
			break;
278
		case 'C':
279
10
			confpath = optarg;
280
10
			break;
281
		case 'L':
282
			i = open("/dev/tty", O_RDWR);
283
			if (i != -1)
284
				ioctl(i, TIOCCLRVERAUTH);
285
			exit(i == -1);
286
		case 'u':
287
			if (parseuid(optarg, &target) != 0)
288
				errx(1, "unknown user");
289
			break;
290
		case 'n':
291
			nflag = 1;
292
			break;
293
		case 's':
294
			sflag = 1;
295
			break;
296
		default:
297
			usage();
298
			break;
299
		}
300
	}
301
10
	argv += optind;
302
10
	argc -= optind;
303
304
10
	if (confpath) {
305
10
		if (sflag)
306
			usage();
307
	} else if ((!sflag && !argc) || (sflag && argc))
308
		usage();
309
310
10
	pw = getpwuid(uid);
311
10
	if (!pw)
312
		err(1, "getpwuid failed");
313
10
	if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
314
		errx(1, "pw_name too long");
315
10
	ngroups = getgroups(NGROUPS_MAX, groups);
316
10
	if (ngroups == -1)
317
		err(1, "can't get groups");
318
10
	groups[ngroups++] = getgid();
319
320
10
	if (sflag) {
321
		sh = getenv("SHELL");
322
		if (sh == NULL || *sh == '\0') {
323
			shargv[0] = strdup(pw->pw_shell);
324
			if (shargv[0] == NULL)
325
				err(1, NULL);
326
		} else
327
			shargv[0] = sh;
328
		argv = shargv;
329
		argc = 1;
330
	}
331
332
10
	if (confpath) {
333
		checkconfig(confpath, argc, argv, uid, groups, ngroups,
334
		    target);
335
		exit(1);	/* fail safe */
336
	}
337
338
	if (geteuid())
339
		errx(1, "not installed setuid");
340
341
	parseconfig("/etc/doas.conf", 1);
342
343
	/* cmdline is used only for logging, no need to abort on truncate */
344
	(void)strlcpy(cmdline, argv[0], sizeof(cmdline));
345
	for (i = 1; i < argc; i++) {
346
		if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
347
			break;
348
		if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
349
			break;
350
	}
351
352
	cmd = argv[0];
353
	if (!permit(uid, groups, ngroups, &rule, target, cmd,
354
	    (const char **)argv + 1)) {
355
		syslog(LOG_AUTHPRIV | LOG_NOTICE,
356
		    "failed command for %s: %s", myname, cmdline);
357
		errc(1, EPERM, NULL);
358
	}
359
360
	if (!(rule->options & NOPASS)) {
361
		if (nflag)
362
			errx(1, "Authorization required");
363
364
		authuser(myname, login_style, rule->options & PERSIST);
365
	}
366
367
	if (pledge("stdio rpath getpw exec id flock cpath wpath", NULL) == -1)
368
		err(1, "pledge");
369
370
	pw = getpwuid(target);
371
	if (!pw)
372
		errx(1, "no passwd entry for target");
373
374
	if (setusercontext(NULL, pw, target, LOGIN_SETGROUP |
375
	    LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
376
	    LOGIN_SETUSER) != 0)
377
		errx(1, "failed to set user context for target");
378
379
	if (pledge("stdio rpath exec flock cpath wpath", NULL) == -1)
380
		err(1, "pledge");
381
382
	if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
383
		cwd = "(failed)";
384
	else
385
		cwd = cwdpath;
386
387
	if (pledge("stdio exec flock rpath cpath wpath", NULL) == -1)
388
		err(1, "pledge");
389
390
	syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command %s as %s from %s",
391
	    myname, cmdline, pw->pw_name, cwd);
392
393
	envp = prepenv(rule);
394
395
	if (rule->cmd) {
396
		if (setenv("PATH", safepath, 1) == -1)
397
			err(1, "failed to set PATH '%s'", safepath);
398
	}
399
	execvpe(cmd, argv, envp);
400
	if (errno == ENOENT)
401
		errx(1, "%s: command not found", cmd);
402
	err(1, "%s", cmd);
403
}