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