summaryrefslogtreecommitdiffstats
path: root/plugins/sudoers/check.c
blob: 7a148021bbdc024f935b04d9c188722e84e2514d (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 1993-1996,1998-2005, 2007-2018
 *	Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * 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 THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
 *
 * Sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <sys/types.h>			/* for ssize_t */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>

#include "sudoers.h"
#include "check.h"

struct getpass_closure {
    int tstat;
    int lectured;
    void *cookie;
    struct passwd *auth_pw;
};

static struct passwd *get_authpw(int);

/*
 * Called when getpass is suspended so we can drop the lock.
 */
static int
getpass_suspend(int signo, void *vclosure)
{
    struct getpass_closure *closure = vclosure;

    timestamp_close(closure->cookie);
    closure->cookie = NULL;
    return 0;
}

/*
 * Called when getpass is resumed so we can reacquire the lock.
 */
static int
getpass_resume(int signo, void *vclosure)
{
    struct getpass_closure *closure = vclosure;

    closure->cookie = timestamp_open(user_name, user_sid);
    if (closure->cookie == NULL)
	return -1;
    if (!timestamp_lock(closure->cookie, closure->auth_pw))
	return -1;
    return 0;
}

/*
 * Returns true if the user successfully authenticates, false if not
 * or -1 on fatal error.
 */
static int
check_user_interactive(int validated, int mode, struct getpass_closure *closure)
{
    struct sudo_conv_callback callback;
    int ret = -1;
    char *prompt;
    debug_decl(check_user_interactive, SUDOERS_DEBUG_AUTH);

    /* Construct callback for getpass function. */
    memset(&callback, 0, sizeof(callback));
    callback.version = SUDO_CONV_CALLBACK_VERSION;
    callback.closure = closure;
    callback.on_suspend = getpass_suspend;
    callback.on_resume = getpass_resume;

    /* Open, lock and read time stamp file if we are using it. */
    if (!ISSET(mode, MODE_IGNORE_TICKET)) {
	/* Open time stamp file and check its status. */
	closure->cookie = timestamp_open(user_name, user_sid);
	if (closure->cookie != NULL) {
	    if (timestamp_lock(closure->cookie, closure->auth_pw)) {
		closure->tstat = timestamp_status(closure->cookie,
		    closure->auth_pw);
	    }
	    callback.on_suspend = getpass_suspend;
	    callback.on_resume = getpass_resume;
	}
    }

    switch (closure->tstat) {
    case TS_FATAL:
	/* Fatal error (usually setuid failure), unsafe to proceed. */
	goto done;

    case TS_CURRENT:
	/* Time stamp file is valid and current. */
	if (!ISSET(validated, FLAG_CHECK_USER)) {
	    ret = true;
	    break;
	}
	sudo_debug_printf(SUDO_DEBUG_INFO,
	    "%s: check user flag overrides time stamp", __func__);
	FALLTHROUGH;

    default:
	if (ISSET(mode, MODE_NONINTERACTIVE) && !def_noninteractive_auth) {
	    validated |= FLAG_NO_USER_INPUT;
	    log_auth_failure(validated, 0);
	    goto done;
	}

	/* Expand any escapes in the prompt. */
	prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
	    closure->auth_pw->pw_name);
	if (prompt == NULL)
	    goto done;

	ret = verify_user(closure->auth_pw, prompt, validated, &callback);
	if (ret == true && closure->lectured)
	    (void)set_lectured();	/* lecture error not fatal */
	free(prompt);
	break;
    }

done:
    debug_return_int(ret);
}

/*
 * Returns true if the user successfully authenticates, false if not
 * or -1 on error.
 */
int
check_user(int validated, int mode)
{
    struct getpass_closure closure = { TS_ERROR };
    int ret = -1;
    bool exempt = false;
    debug_decl(check_user, SUDOERS_DEBUG_AUTH);

    /*
     * In intercept mode, only check the user if configured to do so.
     * We already have a session so no need to init the auth subsystem.
     */
    if (ISSET(sudo_mode, MODE_POLICY_INTERCEPTED)) {
	if (!def_intercept_authenticate) {
	    debug_return_int(true);
	}
    }

    /*
     * Init authentication system regardless of whether we need a password.
     * Required for proper PAM session support.
     */
    if ((closure.auth_pw = get_authpw(mode)) == NULL)
	goto done;
    if (sudo_auth_init(closure.auth_pw, mode) == -1)
	goto done;

    /*
     * Don't prompt for the root passwd or if the user is exempt.
     * If the user is not changing uid/gid, no need for a password.
     */
    if (!def_authenticate || user_is_exempt()) {
	sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %s", __func__,
	    !def_authenticate ? "authentication disabled" :
	    "user exempt from authentication");
	exempt = true;
	ret = true;
	goto done;
    }
    if (user_uid == 0 || (user_uid == runas_pw->pw_uid &&
	(!runas_gr || user_in_group(sudo_user.pw, runas_gr->gr_name)))) {
#ifdef HAVE_SELINUX
	if (user_role == NULL && user_type == NULL)
#endif
#ifdef HAVE_APPARMOR
	if (user_apparmor_profile == NULL)
#endif
#ifdef HAVE_PRIV_SET
	if (runas_privs == NULL && runas_limitprivs == NULL)
#endif
	{
	    sudo_debug_printf(SUDO_DEBUG_INFO,
		"%s: user running command as self", __func__);
	    ret = true;
	    goto done;
	}
    }

    ret = check_user_interactive(validated, mode, &closure);

done:
    if (ret == true) {
	/* The approval function may disallow a user post-authentication. */
	ret = sudo_auth_approval(closure.auth_pw, validated, exempt);

	/*
	 * Only update time stamp if user validated and was approved.
	 * Failure to update the time stamp is not a fatal error.
	 */
	if (ret == true && ISSET(validated, VALIDATE_SUCCESS)) {
	    if (ISSET(mode, MODE_UPDATE_TICKET) && closure.tstat != TS_ERROR)
		(void)timestamp_update(closure.cookie, closure.auth_pw);
	}
    }
    timestamp_close(closure.cookie);
    sudo_auth_cleanup(closure.auth_pw, !ISSET(validated, VALIDATE_SUCCESS));
    if (closure.auth_pw != NULL)
	sudo_pw_delref(closure.auth_pw);

    debug_return_int(ret);
}

/*
 * Display sudo lecture (standard or custom).
 * Returns true if the user was lectured, else false.
 */
void
display_lecture(struct sudo_conv_callback *callback)
{
    struct getpass_closure *closure;
    struct sudo_conv_message msg[2];
    struct sudo_conv_reply repl[2];
    char buf[BUFSIZ];
    struct stat sb;
    ssize_t nread;
    int fd, msgcount = 1;
    debug_decl(lecture, SUDOERS_DEBUG_AUTH);

    if (callback == NULL || (closure = callback->closure) == NULL)
	debug_return;

    if (closure->lectured)
	debug_return;

    if (def_lecture == never || (def_lecture == once && already_lectured()))
	debug_return;

    memset(&msg, 0, sizeof(msg));
    memset(&repl, 0, sizeof(repl));

    if (def_lecture_file) {
	fd = open(def_lecture_file, O_RDONLY|O_NONBLOCK);
	if (fd != -1 && fstat(fd, &sb) == 0) {
	    if (S_ISREG(sb.st_mode)) {
		(void) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
		while ((nread = read(fd, buf, sizeof(buf) - 1)) > 0) {
		    buf[nread] = '\0';
		    msg[0].msg_type = SUDO_CONV_ERROR_MSG|SUDO_CONV_PREFER_TTY;
		    msg[0].msg = buf;
		    sudo_conv(1, msg, repl, NULL);
		}
		if (nread == 0) {
		    close(fd);
		    goto done;
		}
		log_warning(SLOG_RAW_MSG,
		    N_("error reading lecture file %s"), def_lecture_file);
	    } else {
		log_warningx(SLOG_RAW_MSG,
		    N_("ignoring lecture file %s: not a regular file"),
		    def_lecture_file);
	    }
	} else {
	    log_warning(SLOG_RAW_MSG|SLOG_NO_STDERR, N_("unable to open %s"),
		def_lecture_file);
	}
	if (fd != -1)
	    close(fd);
    }

    /* Default sudo lecture. */
    msg[0].msg_type = SUDO_CONV_ERROR_MSG|SUDO_CONV_PREFER_TTY;
    msg[0].msg = _("\n"
	"We trust you have received the usual lecture from the local System\n"
	"Administrator. It usually boils down to these three things:\n\n"
	"    #1) Respect the privacy of others.\n"
	"    #2) Think before you type.\n"
	"    #3) With great power comes great responsibility.\n\n");
    if (!def_pwfeedback) {
	msg[1].msg_type = SUDO_CONV_ERROR_MSG|SUDO_CONV_PREFER_TTY;
	msg[1].msg = _("For security reasons, the password you type will not be visible.\n\n");
	msgcount++;
    }
    sudo_conv(msgcount, msg, repl, NULL);

done:
    closure->lectured = true;
    debug_return;
}

/*
 * Checks if the user is exempt from supplying a password.
 */
bool
user_is_exempt(void)
{
    bool ret = false;
    debug_decl(user_is_exempt, SUDOERS_DEBUG_AUTH);

    if (def_exempt_group) {
	if (user_in_group(sudo_user.pw, def_exempt_group))
	    ret = true;
    }
    debug_return_bool(ret);
}

/*
 * Get passwd entry for the user we are going to authenticate as.
 * By default, this is the user invoking sudo.  In the most common
 * case, this matches sudo_user.pw or runas_pw.
 */
static struct passwd *
get_authpw(int mode)
{
    struct passwd *pw = NULL;
    debug_decl(get_authpw, SUDOERS_DEBUG_AUTH);

    if (ISSET(mode, (MODE_CHECK|MODE_LIST))) {
	/* In list mode we always prompt for the user's password. */
	sudo_pw_addref(sudo_user.pw);
	pw = sudo_user.pw;
    } else {
	if (def_rootpw) {
	    if ((pw = sudo_getpwuid(ROOT_UID)) == NULL) {
		log_warningx(SLOG_SEND_MAIL, N_("unknown uid %u"), ROOT_UID);
	    }
	} else if (def_runaspw) {
	    if ((pw = sudo_getpwnam(def_runas_default)) == NULL) {
		log_warningx(SLOG_SEND_MAIL,
		    N_("unknown user %s"), def_runas_default);
	    }
	} else if (def_targetpw) {
	    if (runas_pw->pw_name == NULL) {
		/* This should never be NULL as we fake up the passwd struct */
		log_warningx(SLOG_RAW_MSG, N_("unknown uid %u"),
		    (unsigned int) runas_pw->pw_uid);
	    } else {
		sudo_pw_addref(runas_pw);
		pw = runas_pw;
	    }
	} else {
	    sudo_pw_addref(sudo_user.pw);
	    pw = sudo_user.pw;
	}
    }

    debug_return_ptr(pw);
}

/*
 * Returns true if the specified shell is allowed by /etc/shells, else false.
 */
bool
check_user_shell(const struct passwd *pw)
{
    const char *shell;
    debug_decl(check_user_shell, SUDOERS_DEBUG_AUTH);

    if (!def_runas_check_shell)
	debug_return_bool(true);

    sudo_debug_printf(SUDO_DEBUG_INFO,
	"%s: checking /etc/shells for %s", __func__, pw->pw_shell);

    setusershell();
    while ((shell = getusershell()) != NULL) {
	if (strcmp(shell, pw->pw_shell) == 0)
	    debug_return_bool(true);
    }
    endusershell();

    debug_return_bool(false);
}