pam_krb5.c revision 1.2 1 /* $NetBSD: pam_krb5.c,v 1.2 2004/12/12 08:18:45 christos 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-2003.
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.20 2004/02/10 10:13:20 des Exp $");
55 #else
56 __RCSID("$NetBSD: pam_krb5.c,v 1.2 2004/12/12 08:18:45 christos 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 "Password:"
92 #define NEW_PASSWORD_PROMPT "New Password:"
93
94 #define PAM_OPT_CCACHE "ccache"
95 #define PAM_OPT_FORWARDABLE "forwardable"
96 #define PAM_OPT_NO_CCACHE "no_ccache"
97 #define PAM_OPT_REUSE_CCACHE "reuse_ccache"
98
99 /*
100 * authentication management
101 */
102 PAM_EXTERN int
103 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
104 int argc __unused, const char *argv[] __unused)
105 {
106 krb5_error_code krbret;
107 krb5_context pam_context;
108 krb5_creds creds;
109 krb5_principal princ;
110 krb5_ccache ccache;
111 krb5_get_init_creds_opt opts;
112 struct passwd *pwd;
113 int retval;
114 void *ccache_data;
115 const char *user, *pass;
116 const void *sourceuser, *service;
117 char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
118
119 retval = pam_get_user(pamh, &user, USER_PROMPT);
120 if (retval != PAM_SUCCESS)
121 return (retval);
122
123 PAM_LOG("Got user: %s", user);
124
125 retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
126 if (retval != PAM_SUCCESS)
127 return (retval);
128
129 PAM_LOG("Got ruser: %s", (const char *)sourceuser);
130
131 service = NULL;
132 pam_get_item(pamh, PAM_SERVICE, &service);
133 if (service == NULL)
134 service = "unknown";
135
136 PAM_LOG("Got service: %s", (const char *)service);
137
138 krbret = krb5_init_context(&pam_context);
139 if (krbret != 0) {
140 PAM_VERBOSE_ERROR("Kerberos 5 error");
141 return (PAM_SERVICE_ERR);
142 }
143
144 PAM_LOG("Context initialised");
145
146 krb5_get_init_creds_opt_init(&opts);
147
148 if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
149 krb5_get_init_creds_opt_set_forwardable(&opts, 1);
150
151 PAM_LOG("Credentials initialised");
152
153 krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
154 if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
155 PAM_VERBOSE_ERROR("Kerberos 5 error");
156 retval = PAM_SERVICE_ERR;
157 goto cleanup3;
158 }
159
160 PAM_LOG("Done krb5_cc_register()");
161
162 /* Get principal name */
163 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
164 asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
165 else
166 principal = strdup(user);
167
168 PAM_LOG("Created principal: %s", principal);
169
170 krbret = krb5_parse_name(pam_context, principal, &princ);
171 free(principal);
172 if (krbret != 0) {
173 PAM_LOG("Error krb5_parse_name(): %s",
174 krb5_get_err_text(pam_context, krbret));
175 PAM_VERBOSE_ERROR("Kerberos 5 error");
176 retval = PAM_SERVICE_ERR;
177 goto cleanup3;
178 }
179
180 PAM_LOG("Done krb5_parse_name()");
181
182 /* Now convert the principal name into something human readable */
183 princ_name = NULL;
184 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
185 if (krbret != 0) {
186 PAM_LOG("Error krb5_unparse_name(): %s",
187 krb5_get_err_text(pam_context, krbret));
188 PAM_VERBOSE_ERROR("Kerberos 5 error");
189 retval = PAM_SERVICE_ERR;
190 goto cleanup2;
191 }
192
193 PAM_LOG("Got principal: %s", princ_name);
194
195 /* Get password */
196 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
197 if (retval != PAM_SUCCESS)
198 goto cleanup2;
199
200 PAM_LOG("Got password");
201
202 /* Verify the local user exists (AFTER getting the password) */
203 if (strchr(user, '@')) {
204 /* get a local account name for this principal */
205 krbret = krb5_aname_to_localname(pam_context, princ,
206 sizeof(luser), luser);
207 if (krbret != 0) {
208 PAM_VERBOSE_ERROR("Kerberos 5 error");
209 PAM_LOG("Error krb5_aname_to_localname(): %s",
210 krb5_get_err_text(pam_context, krbret));
211 retval = PAM_USER_UNKNOWN;
212 goto cleanup2;
213 }
214
215 retval = pam_set_item(pamh, PAM_USER, luser);
216 if (retval != PAM_SUCCESS)
217 goto cleanup2;
218
219 PAM_LOG("PAM_USER Redone");
220 }
221
222 pwd = getpwnam(user);
223 if (pwd == NULL) {
224 retval = PAM_USER_UNKNOWN;
225 goto cleanup2;
226 }
227
228 PAM_LOG("Done getpwnam()");
229
230 /* Get a TGT */
231 memset(&creds, 0, sizeof(krb5_creds));
232 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
233 pass, NULL, pamh, 0, NULL, &opts);
234 if (krbret != 0) {
235 PAM_VERBOSE_ERROR("Kerberos 5 error");
236 PAM_LOG("Error krb5_get_init_creds_password(): %s",
237 krb5_get_err_text(pam_context, krbret));
238 retval = PAM_AUTH_ERR;
239 goto cleanup2;
240 }
241
242 PAM_LOG("Got TGT");
243
244 /* Generate a temporary cache */
245 krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache);
246 if (krbret != 0) {
247 PAM_VERBOSE_ERROR("Kerberos 5 error");
248 PAM_LOG("Error krb5_cc_gen_new(): %s",
249 krb5_get_err_text(pam_context, krbret));
250 retval = PAM_SERVICE_ERR;
251 goto cleanup;
252 }
253 krbret = krb5_cc_initialize(pam_context, ccache, princ);
254 if (krbret != 0) {
255 PAM_VERBOSE_ERROR("Kerberos 5 error");
256 PAM_LOG("Error krb5_cc_initialize(): %s",
257 krb5_get_err_text(pam_context, krbret));
258 retval = PAM_SERVICE_ERR;
259 goto cleanup;
260 }
261 krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
262 if (krbret != 0) {
263 PAM_VERBOSE_ERROR("Kerberos 5 error");
264 PAM_LOG("Error krb5_cc_store_cred(): %s",
265 krb5_get_err_text(pam_context, krbret));
266 krb5_cc_destroy(pam_context, ccache);
267 retval = PAM_SERVICE_ERR;
268 goto cleanup;
269 }
270
271 PAM_LOG("Credentials stashed");
272
273 /* Verify them */
274 if ((srvdup = strdup(service)) == NULL) {
275 retval = PAM_BUF_ERR;
276 goto cleanup;
277 }
278 krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
279 openpam_get_option(pamh, PAM_OPT_FORWARDABLE) ? 1 : 0);
280 free(srvdup);
281 if (krbret == -1) {
282 PAM_VERBOSE_ERROR("Kerberos 5 error");
283 krb5_cc_destroy(pam_context, ccache);
284 retval = PAM_AUTH_ERR;
285 goto cleanup;
286 }
287
288 PAM_LOG("Credentials stash verified");
289
290 retval = pam_get_data(pamh, "ccache", &ccache_data);
291 if (retval == PAM_SUCCESS) {
292 krb5_cc_destroy(pam_context, ccache);
293 PAM_VERBOSE_ERROR("Kerberos 5 error");
294 retval = PAM_AUTH_ERR;
295 goto cleanup;
296 }
297
298 PAM_LOG("Credentials stash not pre-existing");
299
300 asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
301 ccache), krb5_cc_get_name(pam_context, ccache));
302 if (ccache_name == NULL) {
303 PAM_VERBOSE_ERROR("Kerberos 5 error");
304 retval = PAM_BUF_ERR;
305 goto cleanup;
306 }
307 retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
308 if (retval != 0) {
309 krb5_cc_destroy(pam_context, ccache);
310 PAM_VERBOSE_ERROR("Kerberos 5 error");
311 retval = PAM_SERVICE_ERR;
312 goto cleanup;
313 }
314
315 PAM_LOG("Credentials stash saved");
316
317 cleanup:
318 krb5_free_cred_contents(pam_context, &creds);
319 PAM_LOG("Done cleanup");
320 cleanup2:
321 krb5_free_principal(pam_context, princ);
322 PAM_LOG("Done cleanup2");
323 cleanup3:
324 if (princ_name)
325 free(princ_name);
326
327 krb5_free_context(pam_context);
328
329 PAM_LOG("Done cleanup3");
330
331 if (retval != PAM_SUCCESS)
332 PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
333
334 return (retval);
335 }
336
337 PAM_EXTERN int
338 pam_sm_setcred(pam_handle_t *pamh, int flags,
339 int argc __unused, const char *argv[] __unused)
340 {
341
342 krb5_error_code krbret;
343 krb5_context pam_context;
344 krb5_principal princ;
345 krb5_creds creds;
346 krb5_ccache ccache_temp, ccache_perm;
347 krb5_cc_cursor cursor;
348 struct passwd *pwd = NULL;
349 int retval;
350 const char *cache_name, *q;
351 const void *user;
352 void *cache_data;
353 char *cache_name_buf = NULL, *p;
354
355 uid_t euid;
356 gid_t egid;
357
358 if (flags & PAM_DELETE_CRED)
359 return (PAM_SUCCESS);
360
361 if (flags & PAM_REFRESH_CRED)
362 return (PAM_SUCCESS);
363
364 if (flags & PAM_REINITIALIZE_CRED)
365 return (PAM_SUCCESS);
366
367 if (!(flags & PAM_ESTABLISH_CRED))
368 return (PAM_SERVICE_ERR);
369
370 PAM_LOG("Establishing credentials");
371
372 /* Get username */
373 retval = pam_get_item(pamh, PAM_USER, &user);
374 if (retval != PAM_SUCCESS)
375 return (retval);
376
377 PAM_LOG("Got user: %s", (const char *)user);
378
379 krbret = krb5_init_context(&pam_context);
380 if (krbret != 0) {
381 PAM_LOG("Error krb5_init_context() failed");
382 return (PAM_SERVICE_ERR);
383 }
384
385 PAM_LOG("Context initialised");
386
387 euid = geteuid(); /* Usually 0 */
388 egid = getegid();
389
390 PAM_LOG("Got euid, egid: %d %d", euid, egid);
391
392 /* Retrieve the temporary cache */
393 retval = pam_get_data(pamh, "ccache", &cache_data);
394 if (retval != PAM_SUCCESS) {
395 retval = PAM_CRED_UNAVAIL;
396 goto cleanup3;
397 }
398 krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
399 if (krbret != 0) {
400 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data,
401 krb5_get_err_text(pam_context, krbret));
402 retval = PAM_SERVICE_ERR;
403 goto cleanup3;
404 }
405
406 /* Get the uid. This should exist. */
407 pwd = getpwnam(user);
408 if (pwd == NULL) {
409 retval = PAM_USER_UNKNOWN;
410 goto cleanup3;
411 }
412
413 PAM_LOG("Done getpwnam()");
414
415 /* Avoid following a symlink as root */
416 if (setegid(pwd->pw_gid)) {
417 retval = PAM_SERVICE_ERR;
418 goto cleanup3;
419 }
420 if (seteuid(pwd->pw_uid)) {
421 retval = PAM_SERVICE_ERR;
422 goto cleanup3;
423 }
424
425 PAM_LOG("Done setegid() & seteuid()");
426
427 /* Get the cache name */
428 cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
429 if (cache_name == NULL) {
430 asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
431 cache_name = cache_name_buf;
432 }
433
434 p = calloc(PATH_MAX + 16, sizeof(char));
435 q = cache_name;
436
437 if (p == NULL) {
438 PAM_LOG("Error malloc(): failure");
439 retval = PAM_BUF_ERR;
440 goto cleanup3;
441 }
442 cache_name = p;
443
444 /* convert %u and %p */
445 while (*q) {
446 if (*q == '%') {
447 q++;
448 if (*q == 'u') {
449 sprintf(p, "%d", pwd->pw_uid);
450 p += strlen(p);
451 }
452 else if (*q == 'p') {
453 sprintf(p, "%d", getpid());
454 p += strlen(p);
455 }
456 else {
457 /* Not a special token */
458 *p++ = '%';
459 q--;
460 }
461 q++;
462 }
463 else {
464 *p++ = *q++;
465 }
466 }
467
468 PAM_LOG("Got cache_name: %s", cache_name);
469
470 /* Initialize the new ccache */
471 krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
472 if (krbret != 0) {
473 PAM_LOG("Error krb5_cc_get_principal(): %s",
474 krb5_get_err_text(pam_context, krbret));
475 retval = PAM_SERVICE_ERR;
476 goto cleanup3;
477 }
478 krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
479 if (krbret != 0) {
480 PAM_LOG("Error krb5_cc_resolve(): %s",
481 krb5_get_err_text(pam_context, krbret));
482 retval = PAM_SERVICE_ERR;
483 goto cleanup2;
484 }
485 krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
486 if (krbret != 0) {
487 PAM_LOG("Error krb5_cc_initialize(): %s",
488 krb5_get_err_text(pam_context, krbret));
489 retval = PAM_SERVICE_ERR;
490 goto cleanup2;
491 }
492
493 PAM_LOG("Cache initialised");
494
495 /* Prepare for iteration over creds */
496 krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
497 if (krbret != 0) {
498 PAM_LOG("Error krb5_cc_start_seq_get(): %s",
499 krb5_get_err_text(pam_context, krbret));
500 krb5_cc_destroy(pam_context, ccache_perm);
501 retval = PAM_SERVICE_ERR;
502 goto cleanup2;
503 }
504
505 PAM_LOG("Prepared for iteration");
506
507 /* Copy the creds (should be two of them) */
508 while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
509 &cursor, &creds) == 0)) {
510 krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
511 if (krbret != 0) {
512 PAM_LOG("Error krb5_cc_store_cred(): %s",
513 krb5_get_err_text(pam_context, krbret));
514 krb5_cc_destroy(pam_context, ccache_perm);
515 krb5_free_cred_contents(pam_context, &creds);
516 retval = PAM_SERVICE_ERR;
517 goto cleanup2;
518 }
519 krb5_free_cred_contents(pam_context, &creds);
520 PAM_LOG("Iteration");
521 }
522 krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
523
524 PAM_LOG("Done iterating");
525
526 if (strstr(cache_name, "FILE:") == cache_name) {
527 if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
528 PAM_LOG("Error chown(): %s", strerror(errno));
529 krb5_cc_destroy(pam_context, ccache_perm);
530 retval = PAM_SERVICE_ERR;
531 goto cleanup2;
532 }
533 PAM_LOG("Done chown()");
534
535 if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
536 PAM_LOG("Error chmod(): %s", strerror(errno));
537 krb5_cc_destroy(pam_context, ccache_perm);
538 retval = PAM_SERVICE_ERR;
539 goto cleanup2;
540 }
541 PAM_LOG("Done chmod()");
542 }
543
544 krb5_cc_close(pam_context, ccache_perm);
545
546 PAM_LOG("Cache closed");
547
548 retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
549 if (retval != PAM_SUCCESS) {
550 PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
551 krb5_cc_destroy(pam_context, ccache_perm);
552 retval = PAM_SERVICE_ERR;
553 goto cleanup2;
554 }
555
556 PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
557
558 cleanup2:
559 krb5_free_principal(pam_context, princ);
560 PAM_LOG("Done cleanup2");
561 cleanup3:
562 krb5_free_context(pam_context);
563 PAM_LOG("Done cleanup3");
564
565 seteuid(euid);
566 setegid(egid);
567
568 PAM_LOG("Done seteuid() & setegid()");
569
570 if (cache_name_buf != NULL)
571 free(cache_name_buf);
572
573 return (retval);
574 }
575
576 /*
577 * account management
578 */
579 PAM_EXTERN int
580 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
581 int argc __unused, const char *argv[] __unused)
582 {
583 krb5_error_code krbret;
584 krb5_context pam_context;
585 krb5_ccache ccache;
586 krb5_principal princ;
587 int retval;
588 const void *user;
589 void *ccache_name;
590
591 retval = pam_get_item(pamh, PAM_USER, &user);
592 if (retval != PAM_SUCCESS)
593 return (retval);
594
595 PAM_LOG("Got user: %s", (const char *)user);
596
597 retval = pam_get_data(pamh, "ccache", &ccache_name);
598 if (retval != PAM_SUCCESS)
599 return (PAM_SUCCESS);
600
601 PAM_LOG("Got credentials");
602
603 krbret = krb5_init_context(&pam_context);
604 if (krbret != 0) {
605 PAM_LOG("Error krb5_init_context() failed");
606 return (PAM_PERM_DENIED);
607 }
608
609 PAM_LOG("Context initialised");
610
611 krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
612 if (krbret != 0) {
613 PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name,
614 krb5_get_err_text(pam_context, krbret));
615 krb5_free_context(pam_context);
616 return (PAM_PERM_DENIED);
617 }
618
619 PAM_LOG("Got ccache %s", (const char *)ccache_name);
620
621
622 krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
623 if (krbret != 0) {
624 PAM_LOG("Error krb5_cc_get_principal(): %s",
625 krb5_get_err_text(pam_context, krbret));
626 retval = PAM_PERM_DENIED;;
627 goto cleanup;
628 }
629
630 PAM_LOG("Got principal");
631
632 if (krb5_kuserok(pam_context, princ, (const char *)user))
633 retval = PAM_SUCCESS;
634 else
635 retval = PAM_PERM_DENIED;
636 krb5_free_principal(pam_context, princ);
637
638 PAM_LOG("Done kuserok()");
639
640 cleanup:
641 krb5_free_context(pam_context);
642 PAM_LOG("Done cleanup");
643
644 return (retval);
645
646 }
647
648 /*
649 * password management
650 */
651 PAM_EXTERN int
652 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
653 int argc __unused, const char *argv[] __unused)
654 {
655 krb5_error_code krbret;
656 krb5_context pam_context;
657 krb5_creds creds;
658 krb5_principal princ;
659 krb5_get_init_creds_opt opts;
660 krb5_data result_code_string, result_string;
661 int result_code, retval;
662 const char *pass;
663 const void *user;
664 char *princ_name, *passdup;
665
666 if (!(flags & PAM_UPDATE_AUTHTOK))
667 return (PAM_AUTHTOK_ERR);
668
669 retval = pam_get_item(pamh, PAM_USER, &user);
670 if (retval != PAM_SUCCESS)
671 return (retval);
672
673 PAM_LOG("Got user: %s", (const char *)user);
674
675 krbret = krb5_init_context(&pam_context);
676 if (krbret != 0) {
677 PAM_LOG("Error krb5_init_context() failed");
678 return (PAM_SERVICE_ERR);
679 }
680
681 PAM_LOG("Context initialised");
682
683 krb5_get_init_creds_opt_init(&opts);
684
685 PAM_LOG("Credentials options initialised");
686
687 /* Get principal name */
688 krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
689 if (krbret != 0) {
690 PAM_LOG("Error krb5_parse_name(): %s",
691 krb5_get_err_text(pam_context, krbret));
692 retval = PAM_USER_UNKNOWN;
693 goto cleanup3;
694 }
695
696 /* Now convert the principal name into something human readable */
697 princ_name = NULL;
698 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
699 if (krbret != 0) {
700 PAM_LOG("Error krb5_unparse_name(): %s",
701 krb5_get_err_text(pam_context, krbret));
702 retval = PAM_SERVICE_ERR;
703 goto cleanup2;
704 }
705
706 PAM_LOG("Got principal: %s", princ_name);
707
708 /* Get password */
709 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
710 if (retval != PAM_SUCCESS)
711 goto cleanup2;
712
713 PAM_LOG("Got password");
714
715 memset(&creds, 0, sizeof(krb5_creds));
716 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
717 pass, NULL, pamh, 0, "kadmin/changepw", &opts);
718 if (krbret != 0) {
719 PAM_LOG("Error krb5_get_init_creds_password(): %s",
720 krb5_get_err_text(pam_context, krbret));
721 retval = PAM_AUTH_ERR;
722 goto cleanup2;
723 }
724
725 PAM_LOG("Credentials established");
726
727 /* Now get the new password */
728 for (;;) {
729 retval = pam_get_authtok(pamh,
730 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
731 if (retval != PAM_TRY_AGAIN)
732 break;
733 pam_error(pamh, "Mismatch; try again, EOF to quit.");
734 }
735 if (retval != PAM_SUCCESS)
736 goto cleanup;
737
738 PAM_LOG("Got new password");
739
740 /* Change it */
741 if ((passdup = strdup(pass)) == NULL) {
742 retval = PAM_BUF_ERR;
743 goto cleanup;
744 }
745 krbret = krb5_change_password(pam_context, &creds, passdup,
746 &result_code, &result_code_string, &result_string);
747 free(passdup);
748 if (krbret != 0) {
749 PAM_LOG("Error krb5_change_password(): %s",
750 krb5_get_err_text(pam_context, krbret));
751 retval = PAM_AUTHTOK_ERR;
752 goto cleanup;
753 }
754 if (result_code) {
755 PAM_LOG("Error krb5_change_password(): (result_code)");
756 retval = PAM_AUTHTOK_ERR;
757 goto cleanup;
758 }
759
760 PAM_LOG("Password changed");
761
762 if (result_string.data)
763 free(result_string.data);
764 if (result_code_string.data)
765 free(result_code_string.data);
766
767 cleanup:
768 krb5_free_cred_contents(pam_context, &creds);
769 PAM_LOG("Done cleanup");
770 cleanup2:
771 krb5_free_principal(pam_context, princ);
772 PAM_LOG("Done cleanup2");
773 cleanup3:
774 if (princ_name)
775 free(princ_name);
776
777 krb5_free_context(pam_context);
778
779 PAM_LOG("Done cleanup3");
780
781 return (retval);
782 }
783
784 PAM_MODULE_ENTRY("pam_krb5");
785
786 /*
787 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
788 * Modified by Sam Hartman <hartmans (at) mit.edu> to support PAM services
789 * for Debian.
790 *
791 * Verify the Kerberos ticket-granting ticket just retrieved for the
792 * user. If the Kerberos server doesn't respond, assume the user is
793 * trying to fake us out (since we DID just get a TGT from what is
794 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
795 * the local keytab doesn't have it), and we cannot find another
796 * service we do have, let her in.
797 *
798 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
799 */
800 /* ARGSUSED */
801 static int
802 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
803 char *pam_service, int debug)
804 {
805 krb5_error_code retval;
806 krb5_principal princ;
807 krb5_keyblock *keyblock;
808 krb5_data packet;
809 krb5_auth_context auth_context;
810 char phost[BUFSIZ];
811 const char *services[3], **service;
812
813 packet.data = 0;
814
815 /* If possible we want to try and verify the ticket we have
816 * received against a keytab. We will try multiple service
817 * principals, including at least the host principal and the PAM
818 * service principal. The host principal is preferred because access
819 * to that key is generally sufficient to compromise root, while the
820 * service key for this PAM service may be less carefully guarded.
821 * It is important to check the keytab first before the KDC so we do
822 * not get spoofed by a fake KDC.
823 */
824 services[0] = "host";
825 services[1] = pam_service;
826 services[2] = NULL;
827 keyblock = 0;
828 retval = -1;
829 for (service = &services[0]; *service != NULL; service++) {
830 retval = krb5_sname_to_principal(context, NULL, *service,
831 KRB5_NT_SRV_HST, &princ);
832 if (retval != 0) {
833 if (debug)
834 syslog(LOG_DEBUG,
835 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
836 "krb5_sname_to_principal()",
837 krb5_get_err_text(context, retval));
838 return -1;
839 }
840
841 /* Extract the name directly. */
842 strncpy(phost, compat_princ_component(context, princ, 1),
843 BUFSIZ);
844 phost[BUFSIZ - 1] = '\0';
845
846 /*
847 * Do we have service/<host> keys?
848 * (use default/configured keytab, kvno IGNORE_VNO to get the
849 * first match, and ignore enctype.)
850 */
851 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
852 &keyblock);
853 if (retval != 0)
854 continue;
855 break;
856 }
857 if (retval != 0) { /* failed to find key */
858 /* Keytab or service key does not exist */
859 if (debug)
860 syslog(LOG_DEBUG,
861 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
862 "krb5_kt_read_service_key()",
863 krb5_get_err_text(context, retval));
864 retval = 0;
865 goto cleanup;
866 }
867 if (keyblock)
868 krb5_free_keyblock(context, keyblock);
869
870 /* Talk to the kdc and construct the ticket. */
871 auth_context = NULL;
872 retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
873 NULL, ccache, &packet);
874 if (auth_context) {
875 krb5_auth_con_free(context, auth_context);
876 auth_context = NULL; /* setup for rd_req */
877 }
878 if (retval) {
879 if (debug)
880 syslog(LOG_DEBUG,
881 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
882 "krb5_mk_req()",
883 krb5_get_err_text(context, retval));
884 retval = -1;
885 goto cleanup;
886 }
887
888 /* Try to use the ticket. */
889 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
890 NULL, NULL);
891 if (retval) {
892 if (debug)
893 syslog(LOG_DEBUG,
894 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
895 "krb5_rd_req()",
896 krb5_get_err_text(context, retval));
897 retval = -1;
898 }
899 else
900 retval = 1;
901
902 cleanup:
903 if (packet.data)
904 compat_free_data_contents(context, &packet);
905 krb5_free_principal(context, princ);
906 return retval;
907 }
908
909 /* Free the memory for cache_name. Called by pam_end() */
910 /* ARGSUSED */
911 static void
912 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
913 {
914 krb5_context pam_context;
915 krb5_ccache ccache;
916 krb5_error_code krbret;
917
918 if (krb5_init_context(&pam_context))
919 return;
920
921 krbret = krb5_cc_resolve(pam_context, data, &ccache);
922 if (krbret == 0)
923 krb5_cc_destroy(pam_context, ccache);
924 krb5_free_context(pam_context);
925 free(data);
926 }
927
928 #ifdef COMPAT_HEIMDAL
929 #ifdef COMPAT_MIT
930 #error This cannot be MIT and Heimdal compatible!
931 #endif
932 #endif
933
934 #ifndef COMPAT_HEIMDAL
935 #ifndef COMPAT_MIT
936 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
937 #endif
938 #endif
939
940 #ifdef COMPAT_HEIMDAL
941 /* ARGSUSED */
942 static const char *
943 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
944 {
945 return princ->name.name_string.val[n];
946 }
947
948 /* ARGSUSED */
949 static void
950 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
951 {
952 krb5_xfree(data->data);
953 }
954 #endif
955
956 #ifdef COMPAT_MIT
957 static const char *
958 compat_princ_component(krb5_context context, krb5_principal princ, int n)
959 {
960 return krb5_princ_component(context, princ, n)->data;
961 }
962
963 static void
964 compat_free_data_contents(krb5_context context, krb5_data * data)
965 {
966 krb5_free_data_contents(context, data);
967 }
968 #endif
969