Imported Upstream version 1.8.2
[debian/sudo] / plugins / sudoers / auth / sudo_auth.c
1 /*
2  * Copyright (c) 1999-2005, 2008-2010 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
15  *
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.
19  */
20
21 #include <config.h>
22
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <stdio.h>
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 # include <stddef.h>
29 #else
30 # ifdef HAVE_STDLIB_H
31 #  include <stdlib.h>
32 # endif
33 #endif /* STDC_HEADERS */
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #endif /* HAVE_STRING_H */
37 #ifdef HAVE_STRINGS_H
38 # include <strings.h>
39 #endif /* HAVE_STRINGS_H */
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif /* HAVE_UNISTD_H */
43 #include <pwd.h>
44 #include <time.h>
45 #include <signal.h>
46
47 #include "sudoers.h"
48 #include "sudo_auth.h"
49 #include "insults.h"
50
51 static sudo_auth auth_switch[] = {
52 /* Standalone entries first */
53 #ifdef HAVE_PAM
54     AUTH_ENTRY("pam", FLAG_STANDALONE, pam_init, NULL, pam_verify, pam_cleanup, pam_begin_session, pam_end_session)
55 #endif
56 #ifdef HAVE_SECURID
57     AUTH_ENTRY("SecurId", FLAG_STANDALONE, securid_init, securid_setup, securid_verify, NULL, NULL, NULL)
58 #endif
59 #ifdef HAVE_SIA_SES_INIT
60     AUTH_ENTRY("sia", FLAG_STANDALONE, NULL, sia_setup, sia_verify, sia_cleanup, NULL, NULL)
61 #endif
62 #ifdef HAVE_AIXAUTH
63     AUTH_ENTRY("aixauth", FLAG_STANDALONE, NULL, NULL, aixauth_verify, aixauth_cleanup, NULL, NULL)
64 #endif
65 #ifdef HAVE_FWTK
66     AUTH_ENTRY("fwtk", FLAG_STANDALONE, fwtk_init, NULL, fwtk_verify, fwtk_cleanup, NULL, NULL)
67 #endif
68 #ifdef HAVE_BSD_AUTH_H
69     AUTH_ENTRY("bsdauth", FLAG_STANDALONE, bsdauth_init, NULL, bsdauth_verify, bsdauth_cleanup, NULL, NULL)
70 #endif
71
72 /* Non-standalone entries */
73 #ifndef WITHOUT_PASSWD
74     AUTH_ENTRY("passwd", 0, passwd_init, NULL, passwd_verify, passwd_cleanup, NULL, NULL)
75 #endif
76 #if defined(HAVE_GETPRPWNAM) && !defined(WITHOUT_PASSWD)
77     AUTH_ENTRY("secureware", 0, secureware_init, NULL, secureware_verify, secureware_cleanup, NULL, NULL)
78 #endif
79 #ifdef HAVE_AFS
80     AUTH_ENTRY("afs", 0, NULL, NULL, afs_verify, NULL, NULL, NULL)
81 #endif
82 #ifdef HAVE_DCE
83     AUTH_ENTRY("dce", 0, NULL, NULL, dce_verify, NULL, NULL, NULL)
84 #endif
85 #ifdef HAVE_KERB4
86     AUTH_ENTRY("kerb4", 0, kerb4_init, NULL, kerb4_verify, NULL, NULL, NULL)
87 #endif
88 #ifdef HAVE_KERB5
89     AUTH_ENTRY("kerb5", 0, kerb5_init, NULL, kerb5_verify, kerb5_cleanup, NULL, NULL)
90 #endif
91 #ifdef HAVE_SKEY
92     AUTH_ENTRY("S/Key", 0, NULL, rfc1938_setup, rfc1938_verify, NULL, NULL, NULL)
93 #endif
94 #ifdef HAVE_OPIE
95     AUTH_ENTRY("OPIE", 0, NULL, rfc1938_setup, rfc1938_verify, NULL, NULL, NULL)
96 #endif
97     AUTH_ENTRY(NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL)
98 };
99
100 extern char **NewArgv; /* XXX - for auditing */
101
102 static void pass_warn(void);
103
104 int
105 verify_user(struct passwd *pw, char *prompt)
106 {
107     int counter = def_passwd_tries + 1;
108     int success = AUTH_FAILURE;
109     int flags, status, standalone, rval;
110     char *p;
111     sudo_auth *auth;
112     sigaction_t sa, osa;
113
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);
119
120     /* Make sure we have at least one auth method. */
121     if (auth_switch[0].name == NULL) {
122         audit_failure(NewArgv, "no authentication methods");
123         log_error(0,
124             _("There are no authentication methods compiled into sudo!  "
125             "If you want to turn off authentication, use the "
126             "--disable-authentication configure option."));
127         return -1;
128     }
129
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."));
136         return -1;
137     }
138
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);
142
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);
148
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 */
155             }
156
157             if (NEEDS_USER(auth))
158                 restore_perms();
159         }
160     }
161
162     while (--counter) {
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);
168
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 */
175                 }
176
177                 if (NEEDS_USER(auth))
178                     restore_perms();
179             }
180         }
181
182         /* Get the password unless the auth function will do it for us */
183         if (standalone) {
184             p = prompt;
185         } else {
186             p = auth_getpass(prompt, def_passwd_timeout * 60,
187                 SUDO_CONV_PROMPT_ECHO_OFF);
188             if (p == NULL)
189                 break;
190         }
191
192         /* Call authentication functions. */
193         for (auth = auth_switch; auth->name; auth++) {
194             if (IS_DISABLED(auth))
195                 continue;
196
197             if (NEEDS_USER(auth))
198                 set_perms(PERM_USER);
199
200             success = auth->status = (auth->verify)(pw, p, auth);
201
202             if (NEEDS_USER(auth))
203                 restore_perms();
204
205             if (auth->status != AUTH_FAILURE)
206                 goto cleanup;
207         }
208         if (!standalone)
209             zero_bytes(p, strlen(p));
210         pass_warn();
211     }
212
213 cleanup:
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);
219
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 */
224             }
225
226             if (NEEDS_USER(auth))
227                 restore_perms();
228         }
229     }
230
231     switch (success) {
232         case AUTH_SUCCESS:
233             (void) sigaction(SIGTSTP, &osa, NULL);
234             rval = TRUE;
235             break;
236         case AUTH_INTR:
237         case AUTH_FAILURE:
238             if (counter != def_passwd_tries) {
239                 if (def_mail_badpass || def_mail_always)
240                     flags = 0;
241                 else
242                     flags = NO_MAIL;
243                 log_error(flags, ngettext("%d incorrect password attempt",
244                     "%d incorrect password attempts",
245                     def_passwd_tries - counter), def_passwd_tries - counter);
246             }
247             audit_failure(NewArgv, "authentication failure");
248             rval = FALSE;
249             break;
250         case AUTH_FATAL:
251         default:
252             audit_failure(NewArgv, "authentication failure");
253             rval = -1;
254             break;
255     }
256
257     return rval;
258 }
259
260 int auth_begin_session(struct passwd *pw)
261 {
262     sudo_auth *auth;
263     int status;
264
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 */
271             }
272         }
273     }
274     return TRUE;
275 }
276
277 int auth_end_session(void)
278 {
279     sudo_auth *auth;
280     int status;
281
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 */
287             }
288         }
289     }
290     return TRUE;
291 }
292
293 static void
294 pass_warn(void)
295 {
296     const char *warning = def_badpass_message;
297
298 #ifdef INSULT
299     if (def_insults)
300         warning = INSULT;
301 #endif
302     sudo_printf(SUDO_CONV_ERROR_MSG, "%s\n", warning);
303 }
304
305 char *
306 auth_getpass(const char *prompt, int timeout, int type)
307 {
308     struct sudo_conv_message msg;
309     struct sudo_conv_reply repl;
310
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;
314
315     /* If visiblepw set, do not error out if there is no tty. */
316     if (def_visiblepw)
317         type |= SUDO_CONV_PROMPT_ECHO_OK;
318
319     /* Call conversation function */
320     memset(&msg, 0, sizeof(msg));
321     msg.msg_type = type;
322     msg.timeout = def_passwd_timeout * 60;
323     msg.msg = prompt;
324     memset(&repl, 0, sizeof(repl));
325     sudo_conv(1, &msg, &repl);
326     /* XXX - check for ENOTTY? */
327     return repl.reply;
328 }
329
330 void
331 dump_auth_methods(void)
332 {
333     sudo_auth *auth;
334
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");
339 }