2 * Copyright (c) 1999-2005, 2007-2008, 2010-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
27 #include <sys/types.h>
28 #include <sys/param.h>
37 #endif /* STDC_HEADERS */
40 #endif /* HAVE_STRING_H */
43 #endif /* HAVE_STRING_H */
46 #endif /* HAVE_UNISTD_H */
54 #include "sudo_auth.h"
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)
60 # define extract_name(c, p) (krb5_princ_component(c, p, 1)->data)
63 #ifndef HAVE_KRB5_VERIFY_USER
64 static int verify_krb_v5_tgt(krb5_context, krb5_creds *, char *);
66 static struct _sudo_krb5_data {
67 krb5_context sudo_context;
70 } sudo_krb5_data = { NULL, NULL, NULL };
71 typedef struct _sudo_krb5_data *sudo_krb5_datap;
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)
78 *opts = emalloc(sizeof(krb5_get_init_creds_opt));
79 krb5_get_init_creds_opt_init(*opts);
84 krb5_get_init_creds_opt_free(krb5_get_init_creds_opt *opts)
91 kerb5_setup(struct passwd *pw, char **promptp, sudo_auth *auth)
93 static char *krb5_prompt;
95 if (krb5_prompt == NULL) {
96 krb5_context sudo_context;
99 krb5_error_code error;
101 sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
102 princ = ((sudo_krb5_datap) auth->data)->princ;
105 * Really, we need to tell the caller not to prompt for password. The
106 * API does not currently provide this unless the auth is standalone.
108 if ((error = krb5_unparse_name(sudo_context, princ, &pname))) {
109 log_error(NO_EXIT|NO_MAIL,
110 _("%s: unable to unparse princ ('%s'): %s"), auth->name,
111 pw->pw_name, error_message(error));
115 /* Only rewrite prompt if user didn't specify their own. */
116 /*if (!strcmp(prompt, PASSPROMPT)) { */
117 easprintf(&krb5_prompt, "Password for %s: ", pname);
121 *promptp = krb5_prompt;
127 kerb5_init(struct passwd *pw, sudo_auth *auth)
129 krb5_context sudo_context;
131 krb5_principal princ;
132 krb5_error_code error;
135 auth->data = (void *) &sudo_krb5_data; /* Stash all our data here */
137 #ifdef HAVE_KRB5_INIT_SECURE_CONTEXT
138 error = krb5_init_secure_context(&(sudo_krb5_data.sudo_context));
140 error = krb5_init_context(&(sudo_krb5_data.sudo_context));
144 sudo_context = sudo_krb5_data.sudo_context;
146 if ((error = krb5_parse_name(sudo_context, pw->pw_name,
147 &(sudo_krb5_data.princ)))) {
148 log_error(NO_EXIT|NO_MAIL,
149 _("%s: unable to parse '%s': %s"), auth->name, pw->pw_name,
150 error_message(error));
153 princ = sudo_krb5_data.princ;
155 (void) snprintf(cache_name, sizeof(cache_name), "MEMORY:sudocc_%ld",
157 if ((error = krb5_cc_resolve(sudo_context, cache_name,
158 &(sudo_krb5_data.ccache)))) {
159 log_error(NO_EXIT|NO_MAIL,
160 _("%s: unable to resolve ccache: %s"), auth->name,
161 error_message(error));
164 ccache = sudo_krb5_data.ccache;
169 #ifdef HAVE_KRB5_VERIFY_USER
171 kerb5_verify(struct passwd *pw, char *pass, sudo_auth *auth)
173 krb5_context sudo_context;
174 krb5_principal princ;
176 krb5_error_code error;
178 sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
179 princ = ((sudo_krb5_datap) auth->data)->princ;
180 ccache = ((sudo_krb5_datap) auth->data)->ccache;
182 error = krb5_verify_user(sudo_context, princ, ccache, pass, 1, NULL);
183 return error ? AUTH_FAILURE : AUTH_SUCCESS;
187 kerb5_verify(struct passwd *pw, char *pass, sudo_auth *auth)
189 krb5_context sudo_context;
190 krb5_principal princ;
191 krb5_creds credbuf, *creds = NULL;
193 krb5_error_code error;
194 krb5_get_init_creds_opt *opts = NULL;
196 sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
197 princ = ((sudo_krb5_datap) auth->data)->princ;
198 ccache = ((sudo_krb5_datap) auth->data)->ccache;
200 /* Set default flags based on the local config file. */
201 error = krb5_get_init_creds_opt_alloc(sudo_context, &opts);
203 log_error(NO_EXIT|NO_MAIL,
204 _("%s: unable to allocate options: %s"), auth->name,
205 error_message(error));
209 krb5_get_init_creds_opt_set_default_flags(sudo_context, NULL,
210 krb5_principal_get_realm(sudo_context, princ), opts);
213 /* Note that we always obtain a new TGT to verify the user */
214 if ((error = krb5_get_init_creds_password(sudo_context, &credbuf, princ,
215 pass, krb5_prompter_posix,
216 NULL, 0, NULL, opts))) {
217 /* Don't print error if just a bad password */
218 if (error != KRB5KRB_AP_ERR_BAD_INTEGRITY)
219 log_error(NO_EXIT|NO_MAIL,
220 _("%s: unable to get credentials: %s"), auth->name,
221 error_message(error));
226 /* Verify the TGT to prevent spoof attacks. */
227 if ((error = verify_krb_v5_tgt(sudo_context, creds, auth->name)))
230 /* Store cred in cred cache. */
231 if ((error = krb5_cc_initialize(sudo_context, ccache, princ))) {
232 log_error(NO_EXIT|NO_MAIL,
233 _("%s: unable to initialize ccache: %s"), auth->name,
234 error_message(error));
235 } else if ((error = krb5_cc_store_cred(sudo_context, ccache, creds))) {
236 log_error(NO_EXIT|NO_MAIL,
237 _("%s: unable to store cred in ccache: %s"), auth->name,
238 error_message(error));
243 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_TWO_ARGS
244 krb5_get_init_creds_opt_free(sudo_context, opts);
246 krb5_get_init_creds_opt_free(opts);
250 krb5_free_cred_contents(sudo_context, creds);
251 return error ? AUTH_FAILURE : AUTH_SUCCESS;
256 kerb5_cleanup(struct passwd *pw, sudo_auth *auth)
258 krb5_context sudo_context;
259 krb5_principal princ;
262 sudo_context = ((sudo_krb5_datap) auth->data)->sudo_context;
263 princ = ((sudo_krb5_datap) auth->data)->princ;
264 ccache = ((sudo_krb5_datap) auth->data)->ccache;
268 krb5_cc_destroy(sudo_context, ccache);
270 krb5_free_principal(sudo_context, princ);
271 krb5_free_context(sudo_context);
277 #ifndef HAVE_KRB5_VERIFY_USER
279 * Verify the Kerberos ticket-granting ticket just retrieved for the
280 * user. If the Kerberos server doesn't respond, assume the user is
281 * trying to fake us out (since we DID just get a TGT from what is
282 * supposedly our KDC).
284 * Returns 0 for successful authentication, non-zero for failure.
287 verify_krb_v5_tgt(krb5_context sudo_context, krb5_creds *cred, char *auth_name)
289 krb5_error_code error;
290 krb5_principal server;
291 krb5_verify_init_creds_opt vopt;
294 * Get the server principal for the local host.
295 * (Use defaults of "host" and canonicalized local name.)
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));
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);
309 /* verify the Kerberos ticket-granting ticket we just retrieved */
310 error = krb5_verify_init_creds(sudo_context, cred, server, NULL,
312 krb5_free_principal(sudo_context, server);
314 log_error(NO_EXIT|NO_MAIL,
315 _("%s: Cannot verify TGT! Possible attack!: %s"),
316 auth_name, error_message(error));