Imported Upstream version 1.7.2
[debian/sudo] / auth / sudo_auth.c
1 /*
2  * Copyright (c) 1999-2005, 2008-2009 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 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 #  include <memory.h>
37 # endif
38 # include <string.h>
39 #else
40 # ifdef HAVE_STRINGS_H
41 #  include <strings.h>
42 # endif
43 #endif /* HAVE_STRING_H */
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif /* HAVE_UNISTD_H */
47 #include <pwd.h>
48 #include <time.h>
49 #include <signal.h>
50
51 #include "sudo.h"
52 #include "sudo_auth.h"
53 #include "insults.h"
54
55 #ifndef lint
56 __unused static const char rcsid[] = "$Sudo: sudo_auth.c,v 1.40 2009/05/25 12:02:42 millert Exp $";
57 #endif /* lint */
58
59 sudo_auth auth_switch[] = {
60 #ifdef AUTH_STANDALONE
61     AUTH_STANDALONE
62 #else
63 #  ifndef WITHOUT_PASSWD
64     AUTH_ENTRY(0, "passwd", passwd_init, NULL, passwd_verify, NULL)
65 #  endif
66 #  if defined(HAVE_GETPRPWNAM) && !defined(WITHOUT_PASSWD)
67     AUTH_ENTRY(0, "secureware", secureware_init, NULL, secureware_verify, NULL)
68 #  endif
69 #  ifdef HAVE_AFS
70     AUTH_ENTRY(0, "afs", NULL, NULL, afs_verify, NULL)
71 #  endif
72 #  ifdef HAVE_DCE
73     AUTH_ENTRY(0, "dce", NULL, NULL, dce_verify, NULL)
74 #  endif
75 #  ifdef HAVE_KERB4
76     AUTH_ENTRY(0, "kerb4", kerb4_init, NULL, kerb4_verify, NULL)
77 #  endif
78 #  ifdef HAVE_KERB5
79     AUTH_ENTRY(0, "kerb5", kerb5_init, NULL, kerb5_verify, kerb5_cleanup)
80 #  endif
81 #  ifdef HAVE_SKEY
82     AUTH_ENTRY(0, "S/Key", NULL, rfc1938_setup, rfc1938_verify, NULL)
83 #  endif
84 #  ifdef HAVE_OPIE
85     AUTH_ENTRY(0, "OPIE", NULL, rfc1938_setup, rfc1938_verify, NULL)
86 #  endif
87 #endif /* AUTH_STANDALONE */
88     AUTH_ENTRY(0, NULL, NULL, NULL, NULL, NULL)
89 };
90
91 void
92 verify_user(pw, prompt)
93     struct passwd *pw;
94     char *prompt;
95 {
96     int counter = def_passwd_tries + 1;
97     int success = AUTH_FAILURE;
98     int status;
99     int flags;
100     char *p;
101     sudo_auth *auth;
102     sigaction_t sa, osa;
103 #ifdef HAVE_BSM_AUDIT
104     extern char **NewArgv;
105 #endif
106
107     /* Enable suspend during password entry. */
108     sigemptyset(&sa.sa_mask);
109     sa.sa_flags = SA_RESTART;
110     sa.sa_handler = SIG_DFL;
111     (void) sigaction(SIGTSTP, &sa, &osa);
112
113     /* Make sure we have at least one auth method. */
114     if (auth_switch[0].name == NULL) {
115 #ifdef HAVE_BSM_AUDIT
116         audit_failure(NewArgv, "no authentication methods");
117 #endif
118         log_error(0, "%s  %s %s",
119             "There are no authentication methods compiled into sudo!",
120             "If you want to turn off authentication, use the",
121             "--disable-authentication configure option.");
122     }
123
124     /* Set FLAG_ONEANDONLY if there is only one auth method. */
125     if (auth_switch[1].name == NULL)
126         SET(auth_switch[0].flags, FLAG_ONEANDONLY);
127
128     /* Initialize auth methods and unconfigure the method if necessary. */
129     for (auth = auth_switch; auth->name; auth++) {
130         if (auth->init && IS_CONFIGURED(auth)) {
131             if (NEEDS_USER(auth))
132                 set_perms(PERM_USER);
133
134             status = (auth->init)(pw, &prompt, auth);
135             if (status == AUTH_FAILURE)
136                 CLR(auth->flags, FLAG_CONFIGURED);
137             else if (status == AUTH_FATAL) {    /* XXX log */
138 #ifdef HAVE_BSM_AUDIT
139                 audit_failure(NewArgv, "authentication failure");
140 #endif
141                 exit(1);                /* assume error msg already printed */
142             }
143
144             if (NEEDS_USER(auth))
145                 set_perms(PERM_ROOT);
146         }
147     }
148
149     while (--counter) {
150         /* Do any per-method setup and unconfigure the method if needed */
151         for (auth = auth_switch; auth->name; auth++) {
152             if (auth->setup && IS_CONFIGURED(auth)) {
153                 if (NEEDS_USER(auth))
154                     set_perms(PERM_USER);
155
156                 status = (auth->setup)(pw, &prompt, auth);
157                 if (status == AUTH_FAILURE)
158                     CLR(auth->flags, FLAG_CONFIGURED);
159                 else if (status == AUTH_FATAL) {/* XXX log */
160 #ifdef HAVE_BSM_AUDIT
161                     audit_failure(NewArgv, "authentication failure");
162 #endif
163                     exit(1);            /* assume error msg already printed */
164                 }
165
166                 if (NEEDS_USER(auth))
167                     set_perms(PERM_ROOT);
168             }
169         }
170
171         /* Get the password unless the auth function will do it for us */
172 #ifdef AUTH_STANDALONE
173         p = prompt;
174 #else
175         p = (char *) tgetpass(prompt, def_passwd_timeout * 60,
176             tgetpass_flags);
177 #endif /* AUTH_STANDALONE */
178
179         /* Call authentication functions. */
180         for (auth = auth_switch; p && auth->name; auth++) {
181             if (!IS_CONFIGURED(auth))
182                 continue;
183
184             if (NEEDS_USER(auth))
185                 set_perms(PERM_USER);
186
187             success = auth->status = (auth->verify)(pw, (char *)p, auth);
188
189             if (NEEDS_USER(auth))
190                 set_perms(PERM_ROOT);
191
192             if (auth->status != AUTH_FAILURE)
193                 goto cleanup;
194         }
195 #ifndef AUTH_STANDALONE
196         if (p)
197             zero_bytes(p, strlen(p));
198 #endif
199         if (!ISSET(tgetpass_flags, TGP_ASKPASS))
200             pass_warn(stderr);
201     }
202
203 cleanup:
204     /* Call cleanup routines. */
205     for (auth = auth_switch; auth->name; auth++) {
206         if (auth->cleanup && IS_CONFIGURED(auth)) {
207             if (NEEDS_USER(auth))
208                 set_perms(PERM_USER);
209
210             status = (auth->cleanup)(pw, auth);
211             if (status == AUTH_FATAL) { /* XXX log */
212 #ifdef HAVE_BSM_AUDIT
213                 audit_failure(NewArgv, "authentication failure");
214 #endif
215                 exit(1);                /* assume error msg already printed */
216             }
217
218             if (NEEDS_USER(auth))
219                 set_perms(PERM_ROOT);
220         }
221     }
222
223     switch (success) {
224         case AUTH_SUCCESS:
225             (void) sigaction(SIGTSTP, &osa, NULL);
226             return;
227         case AUTH_INTR:
228         case AUTH_FAILURE:
229             if (counter != def_passwd_tries) {
230                 if (def_mail_badpass || def_mail_always)
231                     flags = 0;
232                 else
233                     flags = NO_MAIL;
234 #ifdef HAVE_BSM_AUDIT
235                 audit_failure(NewArgv, "authentication failure");
236 #endif
237                 log_error(flags, "%d incorrect password attempt%s",
238                     def_passwd_tries - counter,
239                     (def_passwd_tries - counter == 1) ? "" : "s");
240             }
241             /* FALLTHROUGH */
242         case AUTH_FATAL:
243 #ifdef HAVE_BSM_AUDIT
244             audit_failure(NewArgv, "authentication failure");
245 #endif
246             exit(1);
247     }
248     /* NOTREACHED */
249 }
250
251 void
252 pass_warn(fp)
253     FILE *fp;
254 {
255
256 #ifdef INSULT
257     if (def_insults)
258         (void) fprintf(fp, "%s\n", INSULT);
259     else
260 #endif
261         (void) fprintf(fp, "%s\n", def_badpass_message);
262 }
263
264 void
265 dump_auth_methods()
266 {
267     sudo_auth *auth;
268
269     (void) fputs("Authentication methods:", stdout);
270     for (auth = auth_switch; auth->name; auth++)
271         (void) printf(" '%s'", auth->name);
272     (void) putchar('\n');
273 }