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