pam_krb5.c revision 1.9 1 /* $NetBSD: pam_krb5.c,v 1.9 2005/04/19 03:38:47 lukem Exp $ */
2
3 /*-
4 * This pam_krb5 module contains code that is:
5 * Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
6 * Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
7 * Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
8 * Copyright (c) Nicolas Williams, 2001. All rights reserved.
9 * Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
10 * Copyright (c) Mark R V Murray, 2001. All rights reserved.
11 * Copyright (c) Networks Associates Technology, Inc., 2002-2005.
12 * All rights reserved.
13 *
14 * Portions of this software were developed for the FreeBSD Project by
15 * ThinkSec AS and NAI Labs, the Security Research Division of Network
16 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
17 * ("CBOSS"), as part of the DARPA CHATS research program.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notices, and the entire permission notice in its entirety,
24 * including the disclaimer of warranties.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote
29 * products derived from this software without specific prior
30 * written permission.
31 *
32 * ALTERNATIVELY, this product may be distributed under the terms of
33 * the GNU Public License, in which case the provisions of the GPL are
34 * required INSTEAD OF the above restrictions. (This clause is
35 * necessary due to a potential bad interaction between the GPL and
36 * the restrictions contained in a BSD-style copyright.)
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 *
50 */
51
52 #include <sys/cdefs.h>
53 #ifdef __FreeBSD__
54 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_krb5/pam_krb5.c,v 1.22 2005/01/24 16:49:50 rwatson Exp $");
55 #else
56 __RCSID("$NetBSD: pam_krb5.c,v 1.9 2005/04/19 03:38:47 lukem Exp $");
57 #endif
58
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <errno.h>
62 #include <limits.h>
63 #include <pwd.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <syslog.h>
68 #include <unistd.h>
69
70 #include <krb5/krb5.h>
71 #include <krb5/com_err.h>
72
73 #define PAM_SM_AUTH
74 #define PAM_SM_ACCOUNT
75 #define PAM_SM_PASSWORD
76
77 #include <security/pam_appl.h>
78 #include <security/pam_modules.h>
79 #include <security/pam_mod_misc.h>
80 #include <security/openpam.h>
81
82 #define COMPAT_HEIMDAL
83 /* #define COMPAT_MIT */
84
85 static int verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int);
86 static void cleanup_cache(pam_handle_t *, void *, int);
87 static const char *compat_princ_component(krb5_context, krb5_principal, int);
88 static void compat_free_data_contents(krb5_context, krb5_data *);
89
90 #define USER_PROMPT "Username: "
91 #define PASSWORD_PROMPT "%s's Password:"
92 #define NEW_PASSWORD_PROMPT "New Password:"
93
94 #define PAM_OPT_CCACHE "ccache"
95 #define PAM_OPT_DEBUG "debug"
96 #define PAM_OPT_FORWARDABLE "forwardable"
97 #define PAM_OPT_NO_CCACHE "no_ccache"
98 #define PAM_OPT_REUSE_CCACHE "reuse_ccache"
99
100 /*
101 * authentication management
102 */
103 PAM_EXTERN int
104 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
105 int argc __unused, const char *argv[] __unused)
106 {
107 krb5_error_code krbret;
108 krb5_context pam_context;
109 krb5_creds creds;
110 krb5_principal princ;
111 krb5_ccache ccache;
112 krb5_get_init_creds_opt opts;
113 struct passwd *pwd, pwres;
114 int retval;
115 void *ccache_data;
116 const char *user, *pass;
117 const void *sourceuser, *service;
118 char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
119 char password_prompt[80];
120 char pwbuf[1024];
121
122 retval = pam_get_user(pamh, &user, USER_PROMPT);
123 if (retval != PAM_SUCCESS)
124 return (retval);
125
126 PAM_LOG("Got user: %s", user);
127
128 retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
129 if (retval != PAM_SUCCESS)
130 return (retval);
131
132 PAM_LOG("Got ruser: %s", (const char *)sourceuser);
133
134 service = NULL;
135 pam_get_item(pamh, PAM_SERVICE, &service);
136 if (service == NULL)
137 service = "unknown";
138
139 PAM_LOG("Got service: %s", (const char *)service);
140
141 krbret = krb5_init_context(&pam_context);
142 if (krbret != 0) {
143 PAM_VERBOSE_ERROR("Kerberos 5 error");
144 return (PAM_SERVICE_ERR);
145 }
146
147 PAM_LOG("Context initialised");
148
149 krb5_get_init_creds_opt_init(&opts);
150
151 if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
152 krb5_get_init_creds_opt_set_forwardable(&opts, 1);
153
154 PAM_LOG("Credentials initialised");
155
156 krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
157 if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
158 PAM_VERBOSE_ERROR("Kerberos 5 error");
159 retval = PAM_SERVICE_ERR;
160 goto cleanup3;
161 }
162
163 PAM_LOG("Done krb5_cc_register()");
164
165 /* Get principal name */
166 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
167 asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
168 else
169 principal = strdup(user);
170
171 PAM_LOG("Created principal: %s", principal);
172
173 krbret = krb5_parse_name(pam_context, principal, &princ);
174 free(principal);
175 if (krbret != 0) {
176 PAM_LOG("Error krb5_parse_name(): %s",
177 krb5_get_err_text(pam_context, krbret));
178 PAM_VERBOSE_ERROR("Kerberos 5 error");
179 retval = PAM_SERVICE_ERR;
180 goto cleanup3;
181 }
182
183 PAM_LOG("Done krb5_parse_name()");
184
185 /* Now convert the principal name into something human readable */
186 princ_name = NULL;
187 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
188 if (krbret != 0) {
189 PAM_LOG("Error krb5_unparse_name(): %s",
190 krb5_get_err_text(pam_context, krbret));
191 PAM_VERBOSE_ERROR("Kerberos 5 error");
192 retval = PAM_SERVICE_ERR;
193 goto cleanup2;
194 }
195
196 PAM_LOG("Got principal: %s", princ_name);
197
198 /* Get password */
199 (void) snprintf(password_prompt, sizeof(password_prompt),
200 PASSWORD_PROMPT, princ_name);
201 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, password_prompt);
202 if (retval != PAM_SUCCESS)
203 goto cleanup2;
204
205 PAM_LOG("Got password");
206
207 /* Verify the local user exists (AFTER getting the password) */
208 if (strchr(user, '@')) {
209 /* get a local account name for this principal */
210 krbret = krb5_aname_to_localname(pam_context, princ,
211 sizeof(luser), luser);
212 if (krbret != 0) {
213 PAM_VERBOSE_ERROR("Kerberos 5 error");
214 PAM_LOG("Error krb5_aname_to_localname(): %s",
215 krb5_get_err_text(pam_context, krbret));
216 retval = PAM_USER_UNKNOWN;
217 goto cleanup2;
218 }
219
220 retval = pam_set_item(pamh, PAM_USER, luser);
221 if (retval != PAM_SUCCESS)
222 goto cleanup2;
223
224 PAM_LOG("PAM_USER Redone");
225 }
226
227 if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
228 pwd == NULL) {
229 retval = PAM_USER_UNKNOWN;
230 goto cleanup2;
231 }
232
233 PAM_LOG("Done getpwnam_r()");
234
235 /* Get a TGT */
236 memset(&creds, 0, sizeof(krb5_creds));
237 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
238 pass, NULL, pamh, 0, NULL, &opts);
239 if (krbret != 0) {
240 PAM_VERBOSE_ERROR("Kerberos 5 error");
241 PAM_LOG("Error krb5_get_init_creds_password(): %s",
242 krb5_get_err_text(pam_context, krbret));
243 retval = PAM_AUTH_ERR;
244 goto cleanup2;
245 }
246
247 PAM_LOG("Got TGT");
248
249 /* Generate a temporary cache */
250 krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache);
251 if (krbret != 0) {
252 PAM_VERBOSE_ERROR("Kerberos 5 error");
253 PAM_LOG("Error krb5_cc_gen_new(): %s",
254 krb5_get_err_text(pam_context, krbret));
255 retval = PAM_SERVICE_ERR;
256 goto cleanup;
257 }
258 krbret = krb5_cc_initialize(pam_context, ccache, princ);
259 if (krbret != 0) {
260 PAM_VERBOSE_ERROR("Kerberos 5 error");
261 PAM_LOG("Error krb5_cc_initialize(): %s",
262 krb5_get_err_text(pam_context, krbret));
263 retval = PAM_SERVICE_ERR;
264 goto cleanup;
265 }
266 krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
267 if (krbret != 0) {
268 PAM_VERBOSE_ERROR("Kerberos 5 error");
269 PAM_LOG("Error krb5_cc_store_cred(): %s",
270 krb5_get_err_text(pam_context, krbret));
271 krb5_cc_destroy(pam_context, ccache);
272 retval = PAM_SERVICE_ERR;
273 goto cleanup;
274 }
275
276 PAM_LOG("Credentials stashed");
277
278 /* Verify them */
279 if ((srvdup = strdup(service)) == NULL) {
280 retval = PAM_BUF_ERR;
281 goto cleanup;
282 }
283 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
284 openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
285 free(srvdup);
286 if (krbret == -1) {
287 PAM_VERBOSE_ERROR("Kerberos 5 error");
288 krb5_cc_destroy(pam_context, ccache);
289 retval = PAM_AUTH_ERR;
290 goto cleanup;
291 }
292
293 PAM_LOG("Credentials stash verified");
294
295 retval = pam_get_data(pamh, "ccache", &ccache_data);
296 if (retval == PAM_SUCCESS) {
297 krb5_cc_destroy(pam_context, ccache);
298 PAM_VERBOSE_ERROR("Kerberos 5 error");
299 retval = PAM_AUTH_ERR;
300 goto cleanup;
301 }
302
303 PAM_LOG("Credentials stash not pre-existing");
304
305 asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
306 ccache), krb5_cc_get_name(pam_context, ccache));
307 if (ccache_name == NULL) {
308 PAM_VERBOSE_ERROR("Kerberos 5 error");
309 retval = PAM_BUF_ERR;
310 goto cleanup;
311 }
312 retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
313 if (retval != 0) {
314 krb5_cc_destroy(pam_context, ccache);
315 PAM_VERBOSE_ERROR("Kerberos 5 error");
316 retval = PAM_SERVICE_ERR;
317 goto cleanup;
318 }
319
320 PAM_LOG("Credentials stash saved");
321
322 cleanup:
323 krb5_free_cred_contents(pam_context, &creds);
324 PAM_LOG("Done cleanup");
325 cleanup2:
326 krb5_free_principal(pam_context, princ);
327 PAM_LOG("Done cleanup2");
328 cleanup3:
329 if (princ_name)
330 free(princ_name);
331
332 krb5_free_context(pam_context);
333
334 PAM_LOG("Done cleanup3");
335
336 if (retval != PAM_SUCCESS)
337 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
338
339 return (retval);
340 }
341
342 PAM_EXTERN int
343 pam_sm_setcred(pam_handle_t *pamh, int flags,
344 int argc __unused, const char *argv[] __unused)
345 {
346
347 krb5_error_code krbret;
348 krb5_context pam_context;
349 krb5_principal princ;
350 krb5_creds creds;
351 krb5_ccache ccache_temp, ccache_perm;
352 krb5_cc_cursor cursor;
353 struct passwd *pwd = NULL, pwres;
354 int retval;
355 const char *cache_name, *q;
356 const void *user;
357 void *cache_data;
358 char *cache_name_buf = NULL, *p;
359 char pwbuf[1024];
360
361 uid_t euid;
362 gid_t egid;
363
364 if (flags & PAM_DELETE_CRED)
365 return (PAM_SUCCESS);
366
367 if (flags & PAM_REFRESH_CRED)
368 return (PAM_SUCCESS);
369
370 if (flags & PAM_REINITIALIZE_CRED)
371 return (PAM_SUCCESS);
372
373 if (!(flags & PAM_ESTABLISH_CRED))
374 return (PAM_SERVICE_ERR);
375
376 /* If a persistent cache isn't desired, stop now. */
377 if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE))
378 return (PAM_SUCCESS);
379
380 PAM_LOG("Establishing credentials");
381
382 /* Get username */
383 retval = pam_get_item(pamh, PAM_USER, &user);
384 if (retval != PAM_SUCCESS)
385 return (retval);
386
387 PAM_LOG("Got user: %s", (const char *)user);
388
389 krbret = krb5_init_context(&pam_context);
390 if (krbret != 0) {
391 PAM_LOG("Error krb5_init_context() failed");
392 return (PAM_SERVICE_ERR);
393 }
394
395 PAM_LOG("Context initialised");
396
397 euid = geteuid(); /* Usually 0 */
398 egid = getegid();
399
400 PAM_LOG("Got euid, egid: %d %d", euid, egid);
401
402 /* Retrieve the temporary cache */
403 retval = pam_get_data(pamh, "ccache", &cache_data);
404 if (retval != PAM_SUCCESS) {
405 retval = PAM_CRED_UNAVAIL;
406 goto cleanup3;
407 }
408 krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
409 if (krbret != 0) {
410 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data,
411 krb5_get_err_text(pam_context, krbret));
412 retval = PAM_SERVICE_ERR;
413 goto cleanup3;
414 }
415
416 /* Get the uid. This should exist. */
417 if (getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
418 pwd == NULL) {
419 retval = PAM_USER_UNKNOWN;
420 goto cleanup3;
421 }
422
423 PAM_LOG("Done getpwnam_r()");
424
425 /* Avoid following a symlink as root */
426 if (setegid(pwd->pw_gid)) {
427 retval = PAM_SERVICE_ERR;
428 goto cleanup3;
429 }
430 if (seteuid(pwd->pw_uid)) {
431 retval = PAM_SERVICE_ERR;
432 goto cleanup3;
433 }
434
435 PAM_LOG("Done setegid() & seteuid()");
436
437 /* Get the cache name */
438 cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
439 if (cache_name == NULL) {
440 asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
441 cache_name = cache_name_buf;
442 }
443
444 p = calloc(PATH_MAX + 16, sizeof(char));
445 q = cache_name;
446
447 if (p == NULL) {
448 PAM_LOG("Error malloc(): failure");
449 retval = PAM_BUF_ERR;
450 goto cleanup3;
451 }
452 cache_name = p;
453
454 /* convert %u and %p */
455 while (*q) {
456 if (*q == '%') {
457 q++;
458 if (*q == 'u') {
459 sprintf(p, "%d", pwd->pw_uid);
460 p += strlen(p);
461 }
462 else if (*q == 'p') {
463 sprintf(p, "%d", getpid());
464 p += strlen(p);
465 }
466 else {
467 /* Not a special token */
468 *p++ = '%';
469 q--;
470 }
471 q++;
472 }
473 else {
474 *p++ = *q++;
475 }
476 }
477
478 PAM_LOG("Got cache_name: %s", cache_name);
479
480 /* Initialize the new ccache */
481 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
482 if (krbret != 0) {
483 PAM_LOG("Error krb5_cc_get_principal(): %s",
484 krb5_get_err_text(pam_context, krbret));
485 retval = PAM_SERVICE_ERR;
486 goto cleanup3;
487 }
488 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
489 if (krbret != 0) {
490 PAM_LOG("Error krb5_cc_resolve(): %s",
491 krb5_get_err_text(pam_context, krbret));
492 retval = PAM_SERVICE_ERR;
493 goto cleanup2;
494 }
495 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
496 if (krbret != 0) {
497 PAM_LOG("Error krb5_cc_initialize(): %s",
498 krb5_get_err_text(pam_context, krbret));
499 retval = PAM_SERVICE_ERR;
500 goto cleanup2;
501 }
502
503 PAM_LOG("Cache initialised");
504
505 /* Prepare for iteration over creds */
506 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
507 if (krbret != 0) {
508 PAM_LOG("Error krb5_cc_start_seq_get(): %s",
509 krb5_get_err_text(pam_context, krbret));
510 krb5_cc_destroy(pam_context, ccache_perm);
511 retval = PAM_SERVICE_ERR;
512 goto cleanup2;
513 }
514
515 PAM_LOG("Prepared for iteration");
516
517 /* Copy the creds (should be two of them) */
518 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
519 &cursor, &creds) == 0)) {
520 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
521 if (krbret != 0) {
522 PAM_LOG("Error krb5_cc_store_cred(): %s",
523 krb5_get_err_text(pam_context, krbret));
524 krb5_cc_destroy(pam_context, ccache_perm);
525 krb5_free_cred_contents(pam_context, &creds);
526 retval = PAM_SERVICE_ERR;
527 goto cleanup2;
528 }
529 krb5_free_cred_contents(pam_context, &creds);
530 PAM_LOG("Iteration");
531 }
532 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
533
534 PAM_LOG("Done iterating");
535
536 if (strstr(cache_name, "FILE:") == cache_name) {
537 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
538 PAM_LOG("Error chown(): %s", strerror(errno));
539 krb5_cc_destroy(pam_context, ccache_perm);
540 retval = PAM_SERVICE_ERR;
541 goto cleanup2;
542 }
543 PAM_LOG("Done chown()");
544
545 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
546 PAM_LOG("Error chmod(): %s", strerror(errno));
547 krb5_cc_destroy(pam_context, ccache_perm);
548 retval = PAM_SERVICE_ERR;
549 goto cleanup2;
550 }
551 PAM_LOG("Done chmod()");
552 }
553
554 krb5_cc_close(pam_context, ccache_perm);
555
556 PAM_LOG("Cache closed");
557
558 retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
559 if (retval != PAM_SUCCESS) {
560 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
561 krb5_cc_destroy(pam_context, ccache_perm);
562 retval = PAM_SERVICE_ERR;
563 goto cleanup2;
564 }
565
566 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
567
568 cleanup2:
569 krb5_free_principal(pam_context, princ);
570 PAM_LOG("Done cleanup2");
571 cleanup3:
572 krb5_free_context(pam_context);
573 PAM_LOG("Done cleanup3");
574
575 seteuid(euid);
576 setegid(egid);
577
578 PAM_LOG("Done seteuid() & setegid()");
579
580 if (cache_name_buf != NULL)
581 free(cache_name_buf);
582
583 return (retval);
584 }
585
586 /*
587 * account management
588 */
589 PAM_EXTERN int
590 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
591 int argc __unused, const char *argv[] __unused)
592 {
593 krb5_error_code krbret;
594 krb5_context pam_context;
595 krb5_ccache ccache;
596 krb5_principal princ;
597 int retval;
598 const void *user;
599 void *ccache_name;
600
601 retval = pam_get_item(pamh, PAM_USER, &user);
602 if (retval != PAM_SUCCESS)
603 return (retval);
604
605 PAM_LOG("Got user: %s", (const char *)user);
606
607 retval = pam_get_data(pamh, "ccache", &ccache_name);
608 if (retval != PAM_SUCCESS)
609 return (PAM_SUCCESS);
610
611 PAM_LOG("Got credentials");
612
613 krbret = krb5_init_context(&pam_context);
614 if (krbret != 0) {
615 PAM_LOG("Error krb5_init_context() failed");
616 return (PAM_PERM_DENIED);
617 }
618
619 PAM_LOG("Context initialised");
620
621 krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
622 if (krbret != 0) {
623 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name,
624 krb5_get_err_text(pam_context, krbret));
625 krb5_free_context(pam_context);
626 return (PAM_PERM_DENIED);
627 }
628
629 PAM_LOG("Got ccache %s", (const char *)ccache_name);
630
631
632 krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
633 if (krbret != 0) {
634 PAM_LOG("Error krb5_cc_get_principal(): %s",
635 krb5_get_err_text(pam_context, krbret));
636 retval = PAM_PERM_DENIED;;
637 goto cleanup;
638 }
639
640 PAM_LOG("Got principal");
641
642 if (krb5_kuserok(pam_context, princ, (const char *)user))
643 retval = PAM_SUCCESS;
644 else
645 retval = PAM_PERM_DENIED;
646 krb5_free_principal(pam_context, princ);
647
648 PAM_LOG("Done kuserok()");
649
650 cleanup:
651 krb5_free_context(pam_context);
652 PAM_LOG("Done cleanup");
653
654 return (retval);
655
656 }
657
658 /*
659 * password management
660 */
661 PAM_EXTERN int
662 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
663 int argc __unused, const char *argv[] __unused)
664 {
665 krb5_error_code krbret;
666 krb5_context pam_context;
667 krb5_creds creds;
668 krb5_principal princ;
669 krb5_get_init_creds_opt opts;
670 krb5_data result_code_string, result_string;
671 int result_code, retval;
672 const char *pass;
673 const void *user;
674 char *princ_name, *passdup;
675 char password_prompt[80];
676
677 if (flags & PAM_PRELIM_CHECK) {
678 /* Nothing to do here. */
679 return (PAM_SUCCESS);
680 }
681
682 if (!(flags & PAM_UPDATE_AUTHTOK)) {
683 PAM_LOG("Illegal flags argument");
684 return (PAM_ABORT);
685 }
686
687 retval = pam_get_item(pamh, PAM_USER, &user);
688 if (retval != PAM_SUCCESS)
689 return (retval);
690
691 PAM_LOG("Got user: %s", (const char *)user);
692
693 krbret = krb5_init_context(&pam_context);
694 if (krbret != 0) {
695 PAM_LOG("Error krb5_init_context() failed");
696 return (PAM_SERVICE_ERR);
697 }
698
699 PAM_LOG("Context initialised");
700
701 krb5_get_init_creds_opt_init(&opts);
702
703 krb5_get_init_creds_opt_set_tkt_life(&opts, 300);
704 krb5_get_init_creds_opt_set_forwardable(&opts, FALSE);
705 krb5_get_init_creds_opt_set_proxiable(&opts, FALSE);
706
707 PAM_LOG("Credentials options initialised");
708
709 /* Get principal name */
710 krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
711 if (krbret != 0) {
712 PAM_LOG("Error krb5_parse_name(): %s",
713 krb5_get_err_text(pam_context, krbret));
714 retval = PAM_USER_UNKNOWN;
715 goto cleanup3;
716 }
717
718 /* Now convert the principal name into something human readable */
719 princ_name = NULL;
720 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
721 if (krbret != 0) {
722 PAM_LOG("Error krb5_unparse_name(): %s",
723 krb5_get_err_text(pam_context, krbret));
724 retval = PAM_SERVICE_ERR;
725 goto cleanup2;
726 }
727
728 PAM_LOG("Got principal: %s", princ_name);
729
730 /* Get password */
731 (void) snprintf(password_prompt, sizeof(password_prompt),
732 PASSWORD_PROMPT, princ_name);
733 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, password_prompt);
734 if (retval != PAM_SUCCESS)
735 goto cleanup2;
736
737 PAM_LOG("Got password");
738
739 memset(&creds, 0, sizeof(krb5_creds));
740 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
741 pass, NULL, pamh, 0, "kadmin/changepw", &opts);
742 if (krbret != 0) {
743 PAM_LOG("Error krb5_get_init_creds_password(): %s",
744 krb5_get_err_text(pam_context, krbret));
745 retval = PAM_AUTH_ERR;
746 goto cleanup2;
747 }
748
749 PAM_LOG("Credentials established");
750
751 /* Now get the new password */
752 for (;;) {
753 retval = pam_get_authtok(pamh,
754 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
755 if (retval != PAM_TRY_AGAIN)
756 break;
757 pam_error(pamh, "Mismatch; try again, EOF to quit.");
758 }
759 if (retval != PAM_SUCCESS)
760 goto cleanup;
761
762 PAM_LOG("Got new password");
763
764 /* Change it */
765 if ((passdup = strdup(pass)) == NULL) {
766 retval = PAM_BUF_ERR;
767 goto cleanup;
768 }
769
770 krb5_data_zero(&result_code_string);
771 krb5_data_zero(&result_string);
772
773 krbret = krb5_change_password(pam_context, &creds, passdup,
774 &result_code, &result_code_string, &result_string);
775 free(passdup);
776 if (krbret != 0) {
777 pam_error(pamh, "Unable to set password: %s",
778 krb5_get_err_text(pam_context, krbret));
779 retval = PAM_AUTHTOK_ERR;
780 goto cleanup;
781 }
782 if (result_code) {
783 pam_info(pamh, "%s%s%.*s",
784 krb5_passwd_result_to_string(pam_context, result_code),
785 result_string.length > 0 ? ": " : "",
786 (int)result_string.length,
787 result_string.length > 0 ? (char *)result_string.data : "");
788 retval = PAM_AUTHTOK_ERR;
789 } else {
790 PAM_LOG("Password changed");
791 }
792
793 krb5_data_free(&result_string);
794 krb5_data_free(&result_code_string);
795
796 cleanup:
797 krb5_free_cred_contents(pam_context, &creds);
798 PAM_LOG("Done cleanup");
799 cleanup2:
800 krb5_free_principal(pam_context, princ);
801 PAM_LOG("Done cleanup2");
802 cleanup3:
803 if (princ_name)
804 free(princ_name);
805
806 krb5_free_context(pam_context);
807
808 PAM_LOG("Done cleanup3");
809
810 return (retval);
811 }
812
813 PAM_MODULE_ENTRY("pam_krb5");
814
815 /*
816 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
817 * Modified by Sam Hartman <hartmans (at) mit.edu> to support PAM services
818 * for Debian.
819 *
820 * Verify the Kerberos ticket-granting ticket just retrieved for the
821 * user. If the Kerberos server doesn't respond, assume the user is
822 * trying to fake us out (since we DID just get a TGT from what is
823 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
824 * the local keytab doesn't have it), and we cannot find another
825 * service we do have, let her in.
826 *
827 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
828 */
829 /* ARGSUSED */
830 static int
831 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
832 char *pam_service, int debug)
833 {
834 krb5_error_code retval;
835 krb5_principal princ;
836 krb5_keyblock *keyblock;
837 krb5_data packet;
838 krb5_auth_context auth_context;
839 char phost[BUFSIZ];
840 const char *services[3], **service;
841
842 packet.data = 0;
843
844 /* If possible we want to try and verify the ticket we have
845 * received against a keytab. We will try multiple service
846 * principals, including at least the host principal and the PAM
847 * service principal. The host principal is preferred because access
848 * to that key is generally sufficient to compromise root, while the
849 * service key for this PAM service may be less carefully guarded.
850 * It is important to check the keytab first before the KDC so we do
851 * not get spoofed by a fake KDC.
852 */
853 services[0] = "host";
854 services[1] = pam_service;
855 services[2] = NULL;
856 keyblock = 0;
857 retval = -1;
858 for (service = &services[0]; *service != NULL; service++) {
859 retval = krb5_sname_to_principal(context, NULL, *service,
860 KRB5_NT_SRV_HST, &princ);
861 if (retval != 0) {
862 if (debug)
863 syslog(LOG_DEBUG,
864 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
865 "krb5_sname_to_principal()",
866 krb5_get_err_text(context, retval));
867 return -1;
868 }
869
870 /* Extract the name directly. */
871 strncpy(phost, compat_princ_component(context, princ, 1),
872 BUFSIZ);
873 phost[BUFSIZ - 1] = '\0';
874
875 /*
876 * Do we have service/<host> keys?
877 * (use default/configured keytab, kvno IGNORE_VNO to get the
878 * first match, and ignore enctype.)
879 */
880 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
881 &keyblock);
882 if (retval != 0)
883 continue;
884 break;
885 }
886 if (retval != 0) { /* failed to find key */
887 /* Keytab or service key does not exist */
888 if (debug)
889 syslog(LOG_DEBUG,
890 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
891 "krb5_kt_read_service_key()",
892 krb5_get_err_text(context, retval));
893 retval = 0;
894 goto cleanup;
895 }
896 if (keyblock)
897 krb5_free_keyblock(context, keyblock);
898
899 /* Talk to the kdc and construct the ticket. */
900 auth_context = NULL;
901 retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
902 NULL, ccache, &packet);
903 if (auth_context) {
904 krb5_auth_con_free(context, auth_context);
905 auth_context = NULL; /* setup for rd_req */
906 }
907 if (retval) {
908 if (debug)
909 syslog(LOG_DEBUG,
910 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
911 "krb5_mk_req()",
912 krb5_get_err_text(context, retval));
913 retval = -1;
914 goto cleanup;
915 }
916
917 /* Try to use the ticket. */
918 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
919 NULL, NULL);
920 if (retval) {
921 if (debug)
922 syslog(LOG_DEBUG,
923 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
924 "krb5_rd_req()",
925 krb5_get_err_text(context, retval));
926 retval = -1;
927 }
928 else
929 retval = 1;
930
931 cleanup:
932 if (packet.data)
933 compat_free_data_contents(context, &packet);
934 krb5_free_principal(context, princ);
935 return retval;
936 }
937
938 /* Free the memory for cache_name. Called by pam_end() */
939 /* ARGSUSED */
940 static void
941 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
942 {
943 krb5_context pam_context;
944 krb5_ccache ccache;
945 krb5_error_code krbret;
946
947 if (krb5_init_context(&pam_context))
948 return;
949
950 krbret = krb5_cc_resolve(pam_context, data, &ccache);
951 if (krbret == 0)
952 krb5_cc_destroy(pam_context, ccache);
953 krb5_free_context(pam_context);
954 free(data);
955 }
956
957 #ifdef COMPAT_HEIMDAL
958 #ifdef COMPAT_MIT
959 #error This cannot be MIT and Heimdal compatible!
960 #endif
961 #endif
962
963 #ifndef COMPAT_HEIMDAL
964 #ifndef COMPAT_MIT
965 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
966 #endif
967 #endif
968
969 #ifdef COMPAT_HEIMDAL
970 /* ARGSUSED */
971 static const char *
972 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
973 {
974 return princ->name.name_string.val[n];
975 }
976
977 /* ARGSUSED */
978 static void
979 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
980 {
981 krb5_xfree(data->data);
982 }
983 #endif
984
985 #ifdef COMPAT_MIT
986 static const char *
987 compat_princ_component(krb5_context context, krb5_principal princ, int n)
988 {
989 return krb5_princ_component(context, princ, n)->data;
990 }
991
992 static void
993 compat_free_data_contents(krb5_context context, krb5_data * data)
994 {
995 krb5_free_data_contents(context, data);
996 }
997 #endif
998