2 * Copyright (c) 1999-2005, 2008-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
32 #endif /* STDC_HEADERS */
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRINGS_H */
41 #endif /* HAVE_UNISTD_H */
47 #include "sudo_auth.h"
50 static sudo_auth auth_switch[] = {
51 /* Standalone entries first */
53 AUTH_ENTRY("pam", FLAG_STANDALONE, sudo_pam_init, NULL, sudo_pam_verify, sudo_pam_cleanup, sudo_pam_begin_session, sudo_pam_end_session)
56 AUTH_ENTRY("SecurId", FLAG_STANDALONE, sudo_securid_init, sudo_securid_setup, sudo_securid_verify, NULL, NULL, NULL)
58 #ifdef HAVE_SIA_SES_INIT
59 AUTH_ENTRY("sia", FLAG_STANDALONE, NULL, sudo_sia_setup, sudo_sia_verify, sudo_sia_cleanup, NULL, NULL)
62 AUTH_ENTRY("aixauth", FLAG_STANDALONE, NULL, NULL, sudo_aix_verify, sudo_aix_cleanup, NULL, NULL)
65 AUTH_ENTRY("fwtk", FLAG_STANDALONE, sudo_fwtk_init, NULL, sudo_fwtk_verify, sudo_fwtk_cleanup, NULL, NULL)
67 #ifdef HAVE_BSD_AUTH_H
68 AUTH_ENTRY("bsdauth", FLAG_STANDALONE, bsdauth_init, NULL, bsdauth_verify, bsdauth_cleanup, NULL, NULL)
71 /* Non-standalone entries */
72 #ifndef WITHOUT_PASSWD
73 AUTH_ENTRY("passwd", 0, sudo_passwd_init, NULL, sudo_passwd_verify, sudo_passwd_cleanup, NULL, NULL)
75 #if defined(HAVE_GETPRPWNAM) && !defined(WITHOUT_PASSWD)
76 AUTH_ENTRY("secureware", 0, sudo_secureware_init, NULL, sudo_secureware_verify, sudo_secureware_cleanup, NULL, NULL)
79 AUTH_ENTRY("afs", 0, NULL, NULL, sudo_afs_verify, NULL, NULL, NULL)
82 AUTH_ENTRY("dce", 0, NULL, NULL, sudo_dce_verify, NULL, NULL, NULL)
85 AUTH_ENTRY("kerb5", 0, sudo_krb5_init, sudo_krb5_setup, sudo_krb5_verify, sudo_krb5_cleanup, NULL, NULL)
88 AUTH_ENTRY("S/Key", 0, NULL, sudo_rfc1938_setup, sudo_rfc1938_verify, NULL, NULL, NULL)
91 AUTH_ENTRY("OPIE", 0, NULL, sudo_rfc1938_setup, sudo_rfc1938_verify, NULL, NULL, NULL)
93 AUTH_ENTRY(NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL)
96 static int standalone;
98 extern char **NewArgv; /* XXX - for auditing */
100 static void pass_warn(void);
103 * Initialize sudoers authentication method(s).
104 * Returns 0 on success and -1 on error.
107 sudo_auth_init(struct passwd *pw)
110 int status = AUTH_SUCCESS;
111 debug_decl(sudo_auth_init, SUDO_DEBUG_AUTH)
113 if (auth_switch[0].name == NULL)
116 /* Make sure we haven't mixed standalone and shared auth methods. */
117 standalone = IS_STANDALONE(&auth_switch[0]);
118 if (standalone && auth_switch[1].name != NULL) {
119 audit_failure(NewArgv, N_("invalid authentication methods"));
120 log_fatal(0, N_("Invalid authentication methods compiled into sudo! "
121 "You may not mix standalone and non-standalone authentication."));
122 debug_return_int(-1);
125 /* Set FLAG_ONEANDONLY if there is only one auth method. */
126 if (auth_switch[1].name == NULL)
127 SET(auth_switch[0].flags, FLAG_ONEANDONLY);
129 /* Initialize auth methods and unconfigure the method if necessary. */
130 for (auth = auth_switch; auth->name; auth++) {
131 if (auth->init && !IS_DISABLED(auth)) {
132 if (NEEDS_USER(auth))
133 set_perms(PERM_USER);
135 status = (auth->init)(pw, auth);
137 if (NEEDS_USER(auth))
140 /* Disable if it failed to init unless there was a fatal error. */
141 if (status == AUTH_FAILURE)
142 SET(auth->flags, FLAG_DISABLED);
143 else if (status == AUTH_FATAL)
144 break; /* assume error msg already printed */
147 debug_return_int(status == AUTH_FATAL ? -1 : 0);
151 * Cleanup all authentication methods.
152 * Returns 0 on success and -1 on error.
155 sudo_auth_cleanup(struct passwd *pw)
158 int status = AUTH_SUCCESS;
159 debug_decl(sudo_auth_cleanup, SUDO_DEBUG_AUTH)
161 /* Call cleanup routines. */
162 for (auth = auth_switch; auth->name; auth++) {
163 if (auth->cleanup && !IS_DISABLED(auth)) {
164 if (NEEDS_USER(auth))
165 set_perms(PERM_USER);
167 status = (auth->cleanup)(pw, auth);
169 if (NEEDS_USER(auth))
172 if (status == AUTH_FATAL)
173 break; /* assume error msg already printed */
176 debug_return_int(status == AUTH_FATAL ? -1 : 0);
180 * Verify the specified user.
181 * Returns true if verified, false if not or -1 on error.
184 verify_user(struct passwd *pw, char *prompt, int validated)
186 int counter = def_passwd_tries + 1;
187 int success = AUTH_FAILURE;
192 debug_decl(verify_user, SUDO_DEBUG_AUTH)
194 /* Enable suspend during password entry. */
195 sigemptyset(&sa.sa_mask);
196 sa.sa_flags = SA_RESTART;
197 sa.sa_handler = SIG_DFL;
198 (void) sigaction(SIGTSTP, &sa, &osa);
200 /* Make sure we have at least one auth method. */
201 /* XXX - check FLAG_DISABLED too */
202 if (auth_switch[0].name == NULL) {
203 audit_failure(NewArgv, N_("no authentication methods"));
205 N_("There are no authentication methods compiled into sudo! "
206 "If you want to turn off authentication, use the "
207 "--disable-authentication configure option."));
208 debug_return_int(-1);
212 /* Do any per-method setup and unconfigure the method if needed */
213 for (auth = auth_switch; auth->name; auth++) {
214 if (auth->setup && !IS_DISABLED(auth)) {
215 if (NEEDS_USER(auth))
216 set_perms(PERM_USER);
218 status = (auth->setup)(pw, &prompt, auth);
220 if (NEEDS_USER(auth))
223 if (status == AUTH_FAILURE)
224 SET(auth->flags, FLAG_DISABLED);
225 else if (status == AUTH_FATAL)
226 goto done; /* assume error msg already printed */
230 /* Get the password unless the auth function will do it for us */
234 p = auth_getpass(prompt, def_passwd_timeout * 60,
235 SUDO_CONV_PROMPT_ECHO_OFF);
240 /* Call authentication functions. */
241 for (auth = auth_switch; auth->name; auth++) {
242 if (IS_DISABLED(auth))
245 if (NEEDS_USER(auth))
246 set_perms(PERM_USER);
248 success = auth->status = (auth->verify)(pw, p, auth);
250 if (NEEDS_USER(auth))
253 if (auth->status != AUTH_FAILURE)
257 zero_bytes(p, strlen(p));
264 (void) sigaction(SIGTSTP, &osa, NULL);
269 if (counter != def_passwd_tries)
270 validated |= FLAG_BAD_PASSWORD;
271 log_auth_failure(validated, def_passwd_tries - counter);
276 log_auth_failure(validated | FLAG_AUTH_ERROR, 0);
281 debug_return_int(rval);
285 * Call authentication method begin session hooks.
286 * Returns 1 on success and -1 on error.
289 sudo_auth_begin_session(struct passwd *pw, char **user_env[])
292 int status = AUTH_SUCCESS;
293 debug_decl(sudo_auth_begin_session, SUDO_DEBUG_AUTH)
295 for (auth = auth_switch; auth->name; auth++) {
296 if (auth->begin_session && !IS_DISABLED(auth)) {
297 status = (auth->begin_session)(pw, user_env, auth);
298 if (status == AUTH_FATAL)
299 break; /* assume error msg already printed */
302 debug_return_int(status == AUTH_FATAL ? -1 : 1);
306 sudo_auth_needs_end_session(void)
310 debug_decl(sudo_auth_needs_end_session, SUDO_DEBUG_AUTH)
312 for (auth = auth_switch; auth->name; auth++) {
313 if (auth->end_session && !IS_DISABLED(auth)) {
318 debug_return_bool(needed);
322 * Call authentication method end session hooks.
323 * Returns 1 on success and -1 on error.
326 sudo_auth_end_session(struct passwd *pw)
329 int status = AUTH_SUCCESS;
330 debug_decl(sudo_auth_end_session, SUDO_DEBUG_AUTH)
332 for (auth = auth_switch; auth->name; auth++) {
333 if (auth->end_session && !IS_DISABLED(auth)) {
334 status = (auth->end_session)(pw, auth);
335 if (status == AUTH_FATAL)
336 break; /* assume error msg already printed */
339 debug_return_int(status == AUTH_FATAL ? -1 : 1);
345 const char *warning = def_badpass_message;
346 debug_decl(pass_warn, SUDO_DEBUG_AUTH)
352 sudo_printf(SUDO_CONV_ERROR_MSG, "%s\n", warning);
358 auth_getpass(const char *prompt, int timeout, int type)
360 struct sudo_conv_message msg;
361 struct sudo_conv_reply repl;
362 debug_decl(auth_getpass, SUDO_DEBUG_AUTH)
364 /* Mask user input if pwfeedback set and echo is off. */
365 if (type == SUDO_CONV_PROMPT_ECHO_OFF && def_pwfeedback)
366 type = SUDO_CONV_PROMPT_MASK;
368 /* If visiblepw set, do not error out if there is no tty. */
370 type |= SUDO_CONV_PROMPT_ECHO_OK;
372 /* Call conversation function */
373 memset(&msg, 0, sizeof(msg));
375 msg.timeout = def_passwd_timeout * 60;
377 memset(&repl, 0, sizeof(repl));
378 sudo_conv(1, &msg, &repl);
379 /* XXX - check for ENOTTY? */
380 debug_return_str_masked(repl.reply);
384 dump_auth_methods(void)
387 debug_decl(dump_auth_methods, SUDO_DEBUG_AUTH)
389 sudo_printf(SUDO_CONV_INFO_MSG, _("Authentication methods:"));
390 for (auth = auth_switch; auth->name; auth++)
391 sudo_printf(SUDO_CONV_INFO_MSG, " '%s'", auth->name);
392 sudo_printf(SUDO_CONV_INFO_MSG, "\n");