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