Imported Upstream version 1.8.2
[debian/sudo] / plugins / sudoers / auth / kerb5.c
1 /*
2  * Copyright (c) 1999-2005, 2007-2008, 2010-2011
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 "sudoers.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(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(krb5_context context,
76     krb5_get_init_creds_opt **opts)
77 {
78     *opts = emalloc(sizeof(krb5_get_init_creds_opt));
79     krb5_get_init_creds_opt_init(*opts);
80     return 0;
81 }
82
83 static void
84 krb5_get_init_creds_opt_free(krb5_get_init_creds_opt *opts)
85 {
86     free(opts);
87 }
88 #endif
89
90 int
91 kerb5_init(struct passwd *pw, char **promptp, sudo_auth *auth)
92 {
93     krb5_context        sudo_context;
94     krb5_ccache         ccache;
95     krb5_principal      princ;
96     krb5_error_code     error;
97     char                cache_name[64];
98     char                *pname;
99
100     auth->data = (void *) &sudo_krb5_data; /* Stash all our data here */
101
102 #ifdef HAVE_KRB5_INIT_SECURE_CONTEXT
103     error = krb5_init_secure_context(&(sudo_krb5_data.sudo_context));
104 #else
105     error = krb5_init_context(&(sudo_krb5_data.sudo_context));
106 #endif
107     if (error)
108         return AUTH_FAILURE;
109     sudo_context = sudo_krb5_data.sudo_context;
110
111     if ((error = krb5_parse_name(sudo_context, pw->pw_name,
112         &(sudo_krb5_data.princ)))) {
113         log_error(NO_EXIT|NO_MAIL,
114                   _("%s: unable to parse '%s': %s"), auth->name, pw->pw_name,
115                   error_message(error));
116         return AUTH_FAILURE;
117     }
118     princ = sudo_krb5_data.princ;
119
120     /*
121      * Really, we need to tell the caller not to prompt for password.
122      * The API does not currently provide this unless the auth is standalone.
123      */
124 #if 1
125     if ((error = krb5_unparse_name(sudo_context, princ, &pname))) {
126         log_error(NO_EXIT|NO_MAIL,
127                   _("%s: unable to unparse princ ('%s'): %s"), auth->name,
128                   pw->pw_name, error_message(error));
129         return AUTH_FAILURE;
130     }
131
132     /* Only rewrite prompt if user didn't specify their own. */
133     /*if (!strcmp(prompt, PASSPROMPT)) { */
134         easprintf(promptp, "Password for %s: ", pname);
135     /*}*/
136     free(pname);
137 #endif
138
139     (void) snprintf(cache_name, sizeof(cache_name), "MEMORY:sudocc_%ld",
140                     (long) getpid());
141     if ((error = krb5_cc_resolve(sudo_context, cache_name,
142         &(sudo_krb5_data.ccache)))) {
143         log_error(NO_EXIT|NO_MAIL,
144                   _("%s: unable to resolve ccache: %s"), auth->name,
145                   error_message(error));
146         return AUTH_FAILURE;
147     }
148     ccache = sudo_krb5_data.ccache;
149
150     return AUTH_SUCCESS;
151 }
152
153 #ifdef HAVE_KRB5_VERIFY_USER
154 int
155 kerb5_verify(struct passwd *pw, char *pass, sudo_auth *auth)
156 {
157     krb5_context        sudo_context;
158     krb5_principal      princ;
159     krb5_ccache         ccache;
160     krb5_error_code     error;
161
162     sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
163     princ = ((sudo_krb5_datap) auth->data)->princ;
164     ccache = ((sudo_krb5_datap) auth->data)->ccache;
165
166     error = krb5_verify_user(sudo_context, princ, ccache, pass, 1, NULL);
167     return error ? AUTH_FAILURE : AUTH_SUCCESS;
168 }
169 #else
170 int
171 kerb5_verify(struct passwd *pw, char *pass, sudo_auth *auth)
172 {
173     krb5_context        sudo_context;
174     krb5_principal      princ;
175     krb5_creds          credbuf, *creds = NULL;
176     krb5_ccache         ccache;
177     krb5_error_code     error;
178     krb5_get_init_creds_opt *opts = NULL;
179
180     sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
181     princ = ((sudo_krb5_datap) auth->data)->princ;
182     ccache = ((sudo_krb5_datap) auth->data)->ccache;
183
184     /* Set default flags based on the local config file. */
185     error = krb5_get_init_creds_opt_alloc(sudo_context, &opts);
186     if (error) {
187         log_error(NO_EXIT|NO_MAIL,
188                   _("%s: unable to allocate options: %s"), auth->name,
189                   error_message(error));
190         goto done;
191     }
192 #ifdef HAVE_HEIMDAL
193     krb5_get_init_creds_opt_set_default_flags(sudo_context, NULL,
194         krb5_principal_get_realm(sudo_context, princ), opts);
195 #endif
196
197     /* Note that we always obtain a new TGT to verify the user */
198     if ((error = krb5_get_init_creds_password(sudo_context, &credbuf, princ,
199                                              pass, krb5_prompter_posix,
200                                              NULL, 0, NULL, opts))) {
201         /* Don't print error if just a bad password */
202         if (error != KRB5KRB_AP_ERR_BAD_INTEGRITY)
203             log_error(NO_EXIT|NO_MAIL,
204                       _("%s: unable to get credentials: %s"), auth->name,
205                       error_message(error));
206         goto done;
207     }
208     creds = &credbuf;
209
210     /* Verify the TGT to prevent spoof attacks. */
211     if ((error = verify_krb_v5_tgt(sudo_context, creds, auth->name)))
212         goto done;
213
214     /* Store cred in cred cache. */
215     if ((error = krb5_cc_initialize(sudo_context, ccache, princ))) {
216         log_error(NO_EXIT|NO_MAIL,
217                   _("%s: unable to initialize ccache: %s"), auth->name,
218                   error_message(error));
219     } else if ((error = krb5_cc_store_cred(sudo_context, ccache, creds))) {
220         log_error(NO_EXIT|NO_MAIL,
221                   _("%s: unable to store cred in ccache: %s"), auth->name,
222                   error_message(error));
223     }
224
225 done:
226     if (opts) {
227 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_TWO_ARGS
228         krb5_get_init_creds_opt_free(sudo_context, opts);
229 #else
230         krb5_get_init_creds_opt_free(opts);
231 #endif
232     }
233     if (creds)
234         krb5_free_cred_contents(sudo_context, creds);
235     return error ? AUTH_FAILURE : AUTH_SUCCESS;
236 }
237 #endif
238
239 int
240 kerb5_cleanup(struct passwd *pw, sudo_auth *auth)
241 {
242     krb5_context        sudo_context;
243     krb5_principal      princ;
244     krb5_ccache         ccache;
245
246     sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
247     princ = ((sudo_krb5_datap) auth->data)->princ;
248     ccache = ((sudo_krb5_datap) auth->data)->ccache;
249
250     if (sudo_context) {
251         if (ccache)
252             krb5_cc_destroy(sudo_context, ccache);
253         if (princ)
254             krb5_free_principal(sudo_context, princ);
255         krb5_free_context(sudo_context);
256     }
257
258     return AUTH_SUCCESS;
259 }
260
261 #ifndef HAVE_KRB5_VERIFY_USER
262 /*
263  * Verify the Kerberos ticket-granting ticket just retrieved for the
264  * user.  If the Kerberos server doesn't respond, assume the user is
265  * trying to fake us out (since we DID just get a TGT from what is
266  * supposedly our KDC).
267  *
268  * Returns 0 for successful authentication, non-zero for failure.
269  */
270 static int
271 verify_krb_v5_tgt(krb5_context sudo_context, krb5_creds *cred, char *auth_name)
272 {
273     krb5_error_code     error;
274     krb5_principal      server;
275     krb5_verify_init_creds_opt vopt;
276
277     /*
278      * Get the server principal for the local host.
279      * (Use defaults of "host" and canonicalized local name.)
280      */
281     if ((error = krb5_sname_to_principal(sudo_context, NULL, NULL,
282                                         KRB5_NT_SRV_HST, &server))) {
283         log_error(NO_EXIT|NO_MAIL,
284                   _("%s: unable to get host principal: %s"), auth_name,
285                   error_message(error));
286         return -1;
287     }
288
289     /* Initialize verify opts and set secure mode */
290     krb5_verify_init_creds_opt_init(&vopt);
291     krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, 1);
292
293     /* verify the Kerberos ticket-granting ticket we just retrieved */
294     error = krb5_verify_init_creds(sudo_context, cred, server, NULL,
295                                    NULL, &vopt);
296     krb5_free_principal(sudo_context, server);
297     if (error)
298         log_error(NO_EXIT|NO_MAIL,
299                   _("%s: Cannot verify TGT! Possible attack!: %s"),
300                   auth_name, error_message(error));
301     return error;
302 }
303 #endif