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, pam_init, NULL, pam_verify, pam_cleanup, pam_begin_session, pam_end_session)
57 AUTH_ENTRY("SecurId", FLAG_STANDALONE, securid_init, securid_setup, securid_verify, NULL, NULL, NULL)
59 #ifdef HAVE_SIA_SES_INIT
60 AUTH_ENTRY("sia", FLAG_STANDALONE, NULL, sia_setup, sia_verify, sia_cleanup, NULL, NULL)
63 AUTH_ENTRY("aixauth", FLAG_STANDALONE, NULL, NULL, aixauth_verify, aixauth_cleanup, NULL, NULL)
66 AUTH_ENTRY("fwtk", FLAG_STANDALONE, fwtk_init, NULL, fwtk_verify, 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, passwd_init, NULL, passwd_verify, passwd_cleanup, NULL, NULL)
76 #if defined(HAVE_GETPRPWNAM) && !defined(WITHOUT_PASSWD)
77 AUTH_ENTRY("secureware", 0, secureware_init, NULL, secureware_verify, secureware_cleanup, NULL, NULL)
80 AUTH_ENTRY("afs", 0, NULL, NULL, afs_verify, NULL, NULL, NULL)
83 AUTH_ENTRY("dce", 0, NULL, NULL, dce_verify, NULL, NULL, NULL)
86 AUTH_ENTRY("kerb4", 0, kerb4_init, NULL, kerb4_verify, NULL, NULL, NULL)
89 AUTH_ENTRY("kerb5", 0, kerb5_init, NULL, kerb5_verify, kerb5_cleanup, NULL, NULL)
92 AUTH_ENTRY("S/Key", 0, NULL, rfc1938_setup, rfc1938_verify, NULL, NULL, NULL)
95 AUTH_ENTRY("OPIE", 0, NULL, rfc1938_setup, rfc1938_verify, NULL, NULL, NULL)
97 AUTH_ENTRY(NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL)
100 extern char **NewArgv; /* XXX - for auditing */
102 static void pass_warn(void);
105 verify_user(struct passwd *pw, char *prompt)
107 int counter = def_passwd_tries + 1;
108 int success = AUTH_FAILURE;
109 int flags, status, standalone, rval;
114 /* Enable suspend during password entry. */
115 sigemptyset(&sa.sa_mask);
116 sa.sa_flags = SA_RESTART;
117 sa.sa_handler = SIG_DFL;
118 (void) sigaction(SIGTSTP, &sa, &osa);
120 /* Make sure we have at least one auth method. */
121 if (auth_switch[0].name == NULL) {
122 audit_failure(NewArgv, "no authentication methods");
124 _("There are no authentication methods compiled into sudo! "
125 "If you want to turn off authentication, use the "
126 "--disable-authentication configure option."));
130 /* Make sure we haven't mixed standalone and shared auth methods. */
131 standalone = IS_STANDALONE(&auth_switch[0]);
132 if (standalone && auth_switch[1].name != NULL) {
133 audit_failure(NewArgv, "invalid authentication methods");
134 log_error(0, _("Invalid authentication methods compiled into sudo! "
135 "You may mix standalone and non-standalone authentication."));
139 /* Set FLAG_ONEANDONLY if there is only one auth method. */
140 if (auth_switch[1].name == NULL)
141 SET(auth_switch[0].flags, FLAG_ONEANDONLY);
143 /* Initialize auth methods and unconfigure the method if necessary. */
144 for (auth = auth_switch; auth->name; auth++) {
145 if (auth->init && !IS_DISABLED(auth)) {
146 if (NEEDS_USER(auth))
147 set_perms(PERM_USER);
149 status = (auth->init)(pw, &prompt, auth);
150 if (status == AUTH_FAILURE)
151 SET(auth->flags, FLAG_DISABLED);
152 else if (status == AUTH_FATAL) { /* XXX log */
153 audit_failure(NewArgv, "authentication failure");
154 return -1; /* assume error msg already printed */
157 if (NEEDS_USER(auth))
163 /* Do any per-method setup and unconfigure the method if needed */
164 for (auth = auth_switch; auth->name; auth++) {
165 if (auth->setup && !IS_DISABLED(auth)) {
166 if (NEEDS_USER(auth))
167 set_perms(PERM_USER);
169 status = (auth->setup)(pw, &prompt, auth);
170 if (status == AUTH_FAILURE)
171 SET(auth->flags, FLAG_DISABLED);
172 else if (status == AUTH_FATAL) {/* XXX log */
173 audit_failure(NewArgv, "authentication failure");
174 return -1; /* assume error msg already printed */
177 if (NEEDS_USER(auth))
182 /* Get the password unless the auth function will do it for us */
186 p = auth_getpass(prompt, def_passwd_timeout * 60,
187 SUDO_CONV_PROMPT_ECHO_OFF);
192 /* Call authentication functions. */
193 for (auth = auth_switch; auth->name; auth++) {
194 if (IS_DISABLED(auth))
197 if (NEEDS_USER(auth))
198 set_perms(PERM_USER);
200 success = auth->status = (auth->verify)(pw, p, auth);
202 if (NEEDS_USER(auth))
205 if (auth->status != AUTH_FAILURE)
209 zero_bytes(p, strlen(p));
214 /* Call cleanup routines. */
215 for (auth = auth_switch; auth->name; auth++) {
216 if (auth->cleanup && !IS_DISABLED(auth)) {
217 if (NEEDS_USER(auth))
218 set_perms(PERM_USER);
220 status = (auth->cleanup)(pw, auth);
221 if (status == AUTH_FATAL) { /* XXX log */
222 audit_failure(NewArgv, "authentication failure");
223 return -1; /* assume error msg already printed */
226 if (NEEDS_USER(auth))
233 (void) sigaction(SIGTSTP, &osa, NULL);
238 if (counter != def_passwd_tries) {
239 if (def_mail_badpass || def_mail_always)
243 log_error(flags, ngettext("%d incorrect password attempt",
244 "%d incorrect password attempts",
245 def_passwd_tries - counter), def_passwd_tries - counter);
247 audit_failure(NewArgv, "authentication failure");
252 audit_failure(NewArgv, "authentication failure");
260 int auth_begin_session(struct passwd *pw)
265 for (auth = auth_switch; auth->name; auth++) {
266 if (auth->begin_session && !IS_DISABLED(auth)) {
267 status = (auth->begin_session)(pw, auth);
268 if (status == AUTH_FATAL) { /* XXX log */
269 audit_failure(NewArgv, "authentication failure");
270 return -1; /* assume error msg already printed */
277 int auth_end_session(void)
282 for (auth = auth_switch; auth->name; auth++) {
283 if (auth->end_session && !IS_DISABLED(auth)) {
284 status = (auth->end_session)(auth);
285 if (status == AUTH_FATAL) { /* XXX log */
286 return -1; /* assume error msg already printed */
296 const char *warning = def_badpass_message;
302 sudo_printf(SUDO_CONV_ERROR_MSG, "%s\n", warning);
306 auth_getpass(const char *prompt, int timeout, int type)
308 struct sudo_conv_message msg;
309 struct sudo_conv_reply repl;
311 /* Mask user input if pwfeedback set and echo is off. */
312 if (type == SUDO_CONV_PROMPT_ECHO_OFF && def_pwfeedback)
313 type = SUDO_CONV_PROMPT_MASK;
315 /* If visiblepw set, do not error out if there is no tty. */
317 type |= SUDO_CONV_PROMPT_ECHO_OK;
319 /* Call conversation function */
320 memset(&msg, 0, sizeof(msg));
322 msg.timeout = def_passwd_timeout * 60;
324 memset(&repl, 0, sizeof(repl));
325 sudo_conv(1, &msg, &repl);
326 /* XXX - check for ENOTTY? */
331 dump_auth_methods(void)
335 sudo_printf(SUDO_CONV_INFO_MSG, _("Authentication methods:"));
336 for (auth = auth_switch; auth->name; auth++)
337 sudo_printf(SUDO_CONV_INFO_MSG, " '%s'", auth->name);
338 sudo_printf(SUDO_CONV_INFO_MSG, "\n");