2 * Copyright (c) 1999-2005, 2008-2010 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>
24 #include <sys/param.h>
33 #endif /* STDC_HEADERS */
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
48 #include "sudo_auth.h"
51 static sudo_auth auth_switch[] = {
52 /* Standalone entries first */
54 AUTH_ENTRY("pam", FLAG_STANDALONE, sudo_pam_init, NULL, sudo_pam_verify, sudo_pam_cleanup, sudo_pam_begin_session, sudo_pam_end_session)
57 AUTH_ENTRY("SecurId", FLAG_STANDALONE, sudo_securid_init, sudo_securid_setup, sudo_securid_verify, NULL, NULL, NULL)
59 #ifdef HAVE_SIA_SES_INIT
60 AUTH_ENTRY("sia", FLAG_STANDALONE, NULL, sudo_sia_setup, sudo_sia_verify, sudo_sia_cleanup, NULL, NULL)
63 AUTH_ENTRY("aixauth", FLAG_STANDALONE, NULL, NULL, sudo_aix_verify, sudo_aix_cleanup, NULL, NULL)
66 AUTH_ENTRY("fwtk", FLAG_STANDALONE, sudo_fwtk_init, NULL, sudo_fwtk_verify, sudo_fwtk_cleanup, NULL, NULL)
68 #ifdef HAVE_BSD_AUTH_H
69 AUTH_ENTRY("bsdauth", FLAG_STANDALONE, bsdauth_init, NULL, bsdauth_verify, bsdauth_cleanup, NULL, NULL)
72 /* Non-standalone entries */
73 #ifndef WITHOUT_PASSWD
74 AUTH_ENTRY("passwd", 0, sudo_passwd_init, NULL, sudo_passwd_verify, sudo_passwd_cleanup, NULL, NULL)
76 #if defined(HAVE_GETPRPWNAM) && !defined(WITHOUT_PASSWD)
77 AUTH_ENTRY("secureware", 0, sudo_secureware_init, NULL, sudo_secureware_verify, sudo_secureware_cleanup, NULL, NULL)
80 AUTH_ENTRY("afs", 0, NULL, NULL, sudo_afs_verify, NULL, NULL, NULL)
83 AUTH_ENTRY("dce", 0, NULL, NULL, sudo_dce_verify, NULL, NULL, NULL)
86 AUTH_ENTRY("kerb5", 0, sudo_krb5_init, sudo_krb5_setup, sudo_krb5_verify, sudo_krb5_cleanup, NULL, NULL)
89 AUTH_ENTRY("S/Key", 0, NULL, sudo_rfc1938_setup, sudo_rfc1938_verify, NULL, NULL, NULL)
92 AUTH_ENTRY("OPIE", 0, NULL, sudo_rfc1938_setup, sudo_rfc1938_verify, NULL, NULL, NULL)
94 AUTH_ENTRY(NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL)
97 static int standalone;
99 extern char **NewArgv; /* XXX - for auditing */
101 static void pass_warn(void);
104 sudo_auth_init(struct passwd *pw)
107 int status = AUTH_SUCCESS;
108 debug_decl(sudo_auth_init, SUDO_DEBUG_AUTH)
110 if (auth_switch[0].name == NULL)
111 debug_return_int(true);
113 /* Make sure we haven't mixed standalone and shared auth methods. */
114 standalone = IS_STANDALONE(&auth_switch[0]);
115 if (standalone && auth_switch[1].name != NULL) {
116 audit_failure(NewArgv, "invalid authentication methods");
117 log_fatal(0, _("Invalid authentication methods compiled into sudo! "
118 "You may mix standalone and non-standalone authentication."));
119 debug_return_int(-1);
122 /* Set FLAG_ONEANDONLY if there is only one auth method. */
123 if (auth_switch[1].name == NULL)
124 SET(auth_switch[0].flags, FLAG_ONEANDONLY);
126 /* Initialize auth methods and unconfigure the method if necessary. */
127 for (auth = auth_switch; auth->name; auth++) {
128 if (auth->init && !IS_DISABLED(auth)) {
129 if (NEEDS_USER(auth))
130 set_perms(PERM_USER);
132 status = (auth->init)(pw, auth);
134 if (NEEDS_USER(auth))
137 if (status == AUTH_FAILURE)
138 SET(auth->flags, FLAG_DISABLED);
139 else if (status == AUTH_FATAL) {
141 audit_failure(NewArgv, "authentication failure");
142 break; /* assume error msg already printed */
146 debug_return_int(status == AUTH_FATAL ? -1 : true);
150 sudo_auth_cleanup(struct passwd *pw)
153 int status = AUTH_SUCCESS;
154 debug_decl(sudo_auth_cleanup, SUDO_DEBUG_AUTH)
156 /* Call cleanup routines. */
157 for (auth = auth_switch; auth->name; auth++) {
158 if (auth->cleanup && !IS_DISABLED(auth)) {
159 if (NEEDS_USER(auth))
160 set_perms(PERM_USER);
162 status = (auth->cleanup)(pw, auth);
164 if (NEEDS_USER(auth))
167 if (status == AUTH_FATAL) {
169 audit_failure(NewArgv, "authentication failure");
170 break; /* assume error msg already printed */
174 debug_return_int(status == AUTH_FATAL ? -1 : true);
178 verify_user(struct passwd *pw, char *prompt)
180 int counter = def_passwd_tries + 1;
181 int success = AUTH_FAILURE;
182 int flags, status, rval;
186 debug_decl(verify_user, SUDO_DEBUG_AUTH)
188 /* Enable suspend during password entry. */
189 sigemptyset(&sa.sa_mask);
190 sa.sa_flags = SA_RESTART;
191 sa.sa_handler = SIG_DFL;
192 (void) sigaction(SIGTSTP, &sa, &osa);
194 /* Make sure we have at least one auth method. */
195 /* XXX - check FLAG_DISABLED too */
196 if (auth_switch[0].name == NULL) {
197 audit_failure(NewArgv, "no authentication methods");
199 _("There are no authentication methods compiled into sudo! "
200 "If you want to turn off authentication, use the "
201 "--disable-authentication configure option."));
202 debug_return_int(-1);
206 /* Do any per-method setup and unconfigure the method if needed */
207 for (auth = auth_switch; auth->name; auth++) {
208 if (auth->setup && !IS_DISABLED(auth)) {
209 if (NEEDS_USER(auth))
210 set_perms(PERM_USER);
212 status = (auth->setup)(pw, &prompt, auth);
214 if (NEEDS_USER(auth))
217 if (status == AUTH_FAILURE)
218 SET(auth->flags, FLAG_DISABLED);
219 else if (status == AUTH_FATAL) {
221 audit_failure(NewArgv, "authentication failure");
222 debug_return_int(-1);/* assume error msg already printed */
227 /* Get the password unless the auth function will do it for us */
231 p = auth_getpass(prompt, def_passwd_timeout * 60,
232 SUDO_CONV_PROMPT_ECHO_OFF);
237 /* Call authentication functions. */
238 for (auth = auth_switch; auth->name; auth++) {
239 if (IS_DISABLED(auth))
242 if (NEEDS_USER(auth))
243 set_perms(PERM_USER);
245 success = auth->status = (auth->verify)(pw, p, auth);
247 if (NEEDS_USER(auth))
250 if (auth->status != AUTH_FAILURE)
254 zero_bytes(p, strlen(p));
261 (void) sigaction(SIGTSTP, &osa, NULL);
266 if (counter != def_passwd_tries) {
267 if (def_mail_badpass || def_mail_always)
271 log_fatal(flags, ngettext("%d incorrect password attempt",
272 "%d incorrect password attempts",
273 def_passwd_tries - counter), def_passwd_tries - counter);
275 audit_failure(NewArgv, "authentication failure");
280 audit_failure(NewArgv, "authentication failure");
285 debug_return_int(rval);
289 sudo_auth_begin_session(struct passwd *pw, char **user_env[])
293 debug_decl(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) {
300 audit_failure(NewArgv, "authentication failure");
301 debug_return_bool(-1); /* assume error msg already printed */
305 debug_return_bool(true);
309 sudo_auth_end_session(struct passwd *pw)
313 debug_decl(auth_end_session, SUDO_DEBUG_AUTH)
315 for (auth = auth_switch; auth->name; auth++) {
316 if (auth->end_session && !IS_DISABLED(auth)) {
317 status = (auth->end_session)(pw, auth);
318 if (status == AUTH_FATAL) {
320 debug_return_bool(-1); /* assume error msg already printed */
324 debug_return_bool(true);
330 const char *warning = def_badpass_message;
331 debug_decl(pass_warn, SUDO_DEBUG_AUTH)
337 sudo_printf(SUDO_CONV_ERROR_MSG, "%s\n", warning);
343 auth_getpass(const char *prompt, int timeout, int type)
345 struct sudo_conv_message msg;
346 struct sudo_conv_reply repl;
347 debug_decl(auth_getpass, SUDO_DEBUG_AUTH)
349 /* Mask user input if pwfeedback set and echo is off. */
350 if (type == SUDO_CONV_PROMPT_ECHO_OFF && def_pwfeedback)
351 type = SUDO_CONV_PROMPT_MASK;
353 /* If visiblepw set, do not error out if there is no tty. */
355 type |= SUDO_CONV_PROMPT_ECHO_OK;
357 /* Call conversation function */
358 memset(&msg, 0, sizeof(msg));
360 msg.timeout = def_passwd_timeout * 60;
362 memset(&repl, 0, sizeof(repl));
363 sudo_conv(1, &msg, &repl);
364 /* XXX - check for ENOTTY? */
365 debug_return_str_masked(repl.reply);
369 dump_auth_methods(void)
372 debug_decl(dump_auth_methods, SUDO_DEBUG_AUTH)
374 sudo_printf(SUDO_CONV_INFO_MSG, _("Authentication methods:"));
375 for (auth = auth_switch; auth->name; auth++)
376 sudo_printf(SUDO_CONV_INFO_MSG, " '%s'", auth->name);
377 sudo_printf(SUDO_CONV_INFO_MSG, "\n");