Imported Upstream version 1.7.4p6
[debian/sudo] / auth / kerb5.c
1 /*
2  * Copyright (c) 1999-2005, 2007-2008, 2010
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
17  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
18  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  *
20  * Sponsored in part by the Defense Advanced Research Projects
21  * Agency (DARPA) and Air Force Research Laboratory, Air Force
22  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23  */
24
25 #include <config.h>
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <stdio.h>
30 #ifdef STDC_HEADERS
31 # include <stdlib.h>
32 # include <stddef.h>
33 #else
34 # ifdef HAVE_STDLIB_H
35 #  include <stdlib.h>
36 # endif
37 #endif /* STDC_HEADERS */
38 #ifdef HAVE_STRING_H
39 # include <string.h>
40 #endif /* HAVE_STRING_H */
41 #ifdef HAVE_STRINGS_H
42 # include <strings.h>
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 <krb5.h>
49 #ifdef HAVE_HEIMDAL
50 #include <com_err.h>
51 #endif
52
53 #include "sudo.h"
54 #include "sudo_auth.h"
55
56 #ifdef HAVE_HEIMDAL
57 # define extract_name(c, p)             krb5_principal_get_comp_string(c, p, 1)
58 # define krb5_free_data_contents(c, d)  krb5_data_free(d)
59 #else
60 # define extract_name(c, p)             (krb5_princ_component(c, p, 1)->data)
61 #endif
62
63 #ifndef HAVE_KRB5_VERIFY_USER
64 static int verify_krb_v5_tgt __P((krb5_context, krb5_creds *, char *));
65 #endif
66 static struct _sudo_krb5_data {
67     krb5_context        sudo_context;
68     krb5_principal      princ;
69     krb5_ccache         ccache;
70 } sudo_krb5_data = { NULL, NULL, NULL };
71 typedef struct _sudo_krb5_data *sudo_krb5_datap;
72
73 #ifndef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
74 static krb5_error_code
75 krb5_get_init_creds_opt_alloc(context, opts)
76     krb5_context                context;
77     krb5_get_init_creds_opt   **opts;
78 {
79     *opts = emalloc(sizeof(krb5_get_init_creds_opt));
80     krb5_get_init_creds_opt_init(*opts);
81     return 0;
82 }
83
84 static void
85 krb5_get_init_creds_opt_free(opts)
86     krb5_get_init_creds_opt *opts;
87 {
88     free(opts);
89 }
90 #endif
91
92 int
93 kerb5_init(pw, promptp, auth)
94     struct passwd *pw;
95     char **promptp;
96     sudo_auth *auth;
97 {
98     krb5_context        sudo_context;
99     krb5_ccache         ccache;
100     krb5_principal      princ;
101     krb5_error_code     error;
102     char                cache_name[64];
103     char                *pname;
104
105     auth->data = (void *) &sudo_krb5_data; /* Stash all our data here */
106
107 #ifdef HAVE_KRB5_INIT_SECURE_CONTEXT
108     error = krb5_init_secure_context(&(sudo_krb5_data.sudo_context));
109 #else
110     error = krb5_init_context(&(sudo_krb5_data.sudo_context));
111 #endif
112     if (error)
113         return(AUTH_FAILURE);
114     sudo_context = sudo_krb5_data.sudo_context;
115
116     if ((error = krb5_parse_name(sudo_context, pw->pw_name,
117         &(sudo_krb5_data.princ)))) {
118         log_error(NO_EXIT|NO_MAIL,
119                   "%s: unable to parse '%s': %s", auth->name, pw->pw_name,
120                   error_message(error));
121         return(AUTH_FAILURE);
122     }
123     princ = sudo_krb5_data.princ;
124
125     /*
126      * Really, we need to tell the caller not to prompt for password.
127      * The API does not currently provide this unless the auth is standalone.
128      */
129 #if 1
130     if ((error = krb5_unparse_name(sudo_context, princ, &pname))) {
131         log_error(NO_EXIT|NO_MAIL,
132                   "%s: unable to unparse princ ('%s'): %s", auth->name,
133                   pw->pw_name, error_message(error));
134         return(AUTH_FAILURE);
135     }
136
137     /* Only rewrite prompt if user didn't specify their own. */
138     /*if (!strcmp(prompt, PASSPROMPT)) { */
139         easprintf(promptp, "Password for %s: ", pname);
140     /*}*/
141     free(pname);
142 #endif
143
144     (void) snprintf(cache_name, sizeof(cache_name), "MEMORY:sudocc_%ld",
145                     (long) getpid());
146     if ((error = krb5_cc_resolve(sudo_context, cache_name,
147         &(sudo_krb5_data.ccache)))) {
148         log_error(NO_EXIT|NO_MAIL,
149                   "%s: unable to resolve ccache: %s", auth->name,
150                   error_message(error));
151         return(AUTH_FAILURE);
152     }
153     ccache = sudo_krb5_data.ccache;
154
155     return(AUTH_SUCCESS);
156 }
157
158 #ifdef HAVE_KRB5_VERIFY_USER
159 int
160 kerb5_verify(pw, pass, auth)
161     struct passwd *pw;
162     char *pass;
163     sudo_auth *auth;
164 {
165     krb5_context        sudo_context;
166     krb5_principal      princ;
167     krb5_ccache         ccache;
168     krb5_error_code     error;
169
170     sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
171     princ = ((sudo_krb5_datap) auth->data)->princ;
172     ccache = ((sudo_krb5_datap) auth->data)->ccache;
173
174     error = krb5_verify_user(sudo_context, princ, ccache, pass, 1, NULL);
175     return (error ? AUTH_FAILURE : AUTH_SUCCESS);
176 }
177 #else
178 int
179 kerb5_verify(pw, pass, auth)
180     struct passwd *pw;
181     char *pass;
182     sudo_auth *auth;
183 {
184     krb5_context        sudo_context;
185     krb5_principal      princ;
186     krb5_creds          credbuf, *creds = NULL;
187     krb5_ccache         ccache;
188     krb5_error_code     error;
189     krb5_get_init_creds_opt *opts = NULL;
190
191     sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
192     princ = ((sudo_krb5_datap) auth->data)->princ;
193     ccache = ((sudo_krb5_datap) auth->data)->ccache;
194
195     /* Set default flags based on the local config file. */
196     error = krb5_get_init_creds_opt_alloc(sudo_context, &opts);
197     if (error) {
198         log_error(NO_EXIT|NO_MAIL,
199                   "%s: unable to allocate options: %s", auth->name,
200                   error_message(error));
201         goto done;
202     }
203 #ifdef HAVE_HEIMDAL
204     krb5_get_init_creds_opt_set_default_flags(sudo_context, NULL,
205         krb5_principal_get_realm(sudo_context, princ), opts);
206 #endif
207
208     /* Note that we always obtain a new TGT to verify the user */
209     if ((error = krb5_get_init_creds_password(sudo_context, &credbuf, princ,
210                                              pass, krb5_prompter_posix,
211                                              NULL, 0, NULL, opts))) {
212         /* Don't print error if just a bad password */
213         if (error != KRB5KRB_AP_ERR_BAD_INTEGRITY)
214             log_error(NO_EXIT|NO_MAIL,
215                       "%s: unable to get credentials: %s", auth->name,
216                       error_message(error));
217         goto done;
218     }
219     creds = &credbuf;
220
221     /* Verify the TGT to prevent spoof attacks. */
222     if ((error = verify_krb_v5_tgt(sudo_context, creds, auth->name)))
223         goto done;
224
225     /* Store cred in cred cache. */
226     if ((error = krb5_cc_initialize(sudo_context, ccache, princ))) {
227         log_error(NO_EXIT|NO_MAIL,
228                   "%s: unable to initialize ccache: %s", auth->name,
229                   error_message(error));
230     } else if ((error = krb5_cc_store_cred(sudo_context, ccache, creds))) {
231         log_error(NO_EXIT|NO_MAIL,
232                   "%s: unable to store cred in ccache: %s", auth->name,
233                   error_message(error));
234     }
235
236 done:
237     if (opts) {
238 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_TWO_ARGS
239         krb5_get_init_creds_opt_free(sudo_context, opts);
240 #else
241         krb5_get_init_creds_opt_free(opts);
242 #endif
243     }
244     if (creds)
245         krb5_free_cred_contents(sudo_context, creds);
246     return (error ? AUTH_FAILURE : AUTH_SUCCESS);
247 }
248 #endif
249
250 int
251 kerb5_cleanup(pw, auth)
252     struct passwd *pw;
253     sudo_auth *auth;
254 {
255     krb5_context        sudo_context;
256     krb5_principal      princ;
257     krb5_ccache         ccache;
258
259     sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
260     princ = ((sudo_krb5_datap) auth->data)->princ;
261     ccache = ((sudo_krb5_datap) auth->data)->ccache;
262
263     if (sudo_context) {
264         if (ccache)
265             krb5_cc_destroy(sudo_context, ccache);
266         if (princ)
267             krb5_free_principal(sudo_context, princ);
268         krb5_free_context(sudo_context);
269     }
270
271     return(AUTH_SUCCESS);
272 }
273
274 #ifndef HAVE_KRB5_VERIFY_USER
275 /*
276  * Verify the Kerberos ticket-granting ticket just retrieved for the
277  * user.  If the Kerberos server doesn't respond, assume the user is
278  * trying to fake us out (since we DID just get a TGT from what is
279  * supposedly our KDC).
280  *
281  * Returns 0 for successful authentication, non-zero for failure.
282  */
283 static int
284 verify_krb_v5_tgt(sudo_context, cred, auth_name)
285     krb5_context        sudo_context;
286     krb5_creds          *cred;
287     char                *auth_name; /* For error reporting */
288 {
289     krb5_error_code     error;
290     krb5_principal      server;
291     krb5_verify_init_creds_opt vopt;
292
293     /*
294      * Get the server principal for the local host.
295      * (Use defaults of "host" and canonicalized local name.)
296      */
297     if ((error = krb5_sname_to_principal(sudo_context, NULL, NULL,
298                                         KRB5_NT_SRV_HST, &server))) {
299         log_error(NO_EXIT|NO_MAIL,
300                   "%s: unable to get host principal: %s", auth_name,
301                   error_message(error));
302         return(-1);
303     }
304
305     /* Initialize verify opts and set secure mode */
306     krb5_verify_init_creds_opt_init(&vopt);
307     krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, 1);
308
309     /* verify the Kerberos ticket-granting ticket we just retrieved */
310     error = krb5_verify_init_creds(sudo_context, cred, server, NULL,
311                                    NULL, &vopt);
312     krb5_free_principal(sudo_context, server);
313     if (error)
314         log_error(NO_EXIT|NO_MAIL,
315                   "%s: Cannot verify TGT! Possible attack!: %s", auth_name,
316                   error_message(error));
317     return(error);
318 }
319 #endif