pam_krb5.c revision 1.6 1 /* $NetBSD: pam_krb5.c,v 1.6 2005/02/26 18:25:28 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.6 2005/02/26 18:25:28 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 krb5_get_init_creds_opt_set_tkt_life(&opts, 300);
702 krb5_get_init_creds_opt_set_forwardable(&opts, FALSE);
703 krb5_get_init_creds_opt_set_proxiable(&opts, FALSE);
704
705 PAM_LOG("Credentials options initialised");
706
707 /* Get principal name */
708 krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
709 if (krbret != 0) {
710 PAM_LOG("Error krb5_parse_name(): %s",
711 krb5_get_err_text(pam_context, krbret));
712 retval = PAM_USER_UNKNOWN;
713 goto cleanup3;
714 }
715
716 /* Now convert the principal name into something human readable */
717 princ_name = NULL;
718 krbret = krb5_unparse_name(pam_context, princ, &princ_name);
719 if (krbret != 0) {
720 PAM_LOG("Error krb5_unparse_name(): %s",
721 krb5_get_err_text(pam_context, krbret));
722 retval = PAM_SERVICE_ERR;
723 goto cleanup2;
724 }
725
726 PAM_LOG("Got principal: %s", princ_name);
727
728 /* Get password */
729 (void) snprintf(password_prompt, sizeof(password_prompt),
730 PASSWORD_PROMPT, princ_name);
731 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, password_prompt);
732 if (retval != PAM_SUCCESS)
733 goto cleanup2;
734
735 PAM_LOG("Got password");
736
737 memset(&creds, 0, sizeof(krb5_creds));
738 krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
739 pass, NULL, pamh, 0, "kadmin/changepw", &opts);
740 if (krbret != 0) {
741 PAM_LOG("Error krb5_get_init_creds_password(): %s",
742 krb5_get_err_text(pam_context, krbret));
743 retval = PAM_AUTH_ERR;
744 goto cleanup2;
745 }
746
747 PAM_LOG("Credentials established");
748
749 /* Now get the new password */
750 for (;;) {
751 retval = pam_get_authtok(pamh,
752 PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
753 if (retval != PAM_TRY_AGAIN)
754 break;
755 pam_error(pamh, "Mismatch; try again, EOF to quit.");
756 }
757 if (retval != PAM_SUCCESS)
758 goto cleanup;
759
760 PAM_LOG("Got new password");
761
762 /* Change it */
763 if ((passdup = strdup(pass)) == NULL) {
764 retval = PAM_BUF_ERR;
765 goto cleanup;
766 }
767
768 krb5_data_zero(&result_code_string);
769 krb5_data_zero(&result_string);
770
771 krbret = krb5_change_password(pam_context, &creds, passdup,
772 &result_code, &result_code_string, &result_string);
773 free(passdup);
774 if (krbret != 0) {
775 pam_error(pamh, "Unable to set password: %s",
776 krb5_get_err_text(pam_context, krbret));
777 retval = PAM_AUTHTOK_ERR;
778 goto cleanup;
779 }
780 if (result_code) {
781 pam_info(pamh, "%s%s%.*s",
782 krb5_passwd_result_to_string(pam_context, result_code),
783 result_string.length > 0 ? ": " : "",
784 (int)result_string.length,
785 result_string.length > 0 ? (char *)result_string.data : "");
786 retval = PAM_AUTHTOK_ERR;
787 } else {
788 PAM_LOG("Password changed");
789 }
790
791 krb5_data_free(&result_string);
792 krb5_data_free(&result_code_string);
793
794 cleanup:
795 krb5_free_cred_contents(pam_context, &creds);
796 PAM_LOG("Done cleanup");
797 cleanup2:
798 krb5_free_principal(pam_context, princ);
799 PAM_LOG("Done cleanup2");
800 cleanup3:
801 if (princ_name)
802 free(princ_name);
803
804 krb5_free_context(pam_context);
805
806 PAM_LOG("Done cleanup3");
807
808 return (retval);
809 }
810
811 PAM_MODULE_ENTRY("pam_krb5");
812
813 /*
814 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
815 * Modified by Sam Hartman <hartmans (at) mit.edu> to support PAM services
816 * for Debian.
817 *
818 * Verify the Kerberos ticket-granting ticket just retrieved for the
819 * user. If the Kerberos server doesn't respond, assume the user is
820 * trying to fake us out (since we DID just get a TGT from what is
821 * supposedly our KDC). If the host/<host> service is unknown (i.e.,
822 * the local keytab doesn't have it), and we cannot find another
823 * service we do have, let her in.
824 *
825 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
826 */
827 /* ARGSUSED */
828 static int
829 verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
830 char *pam_service, int debug)
831 {
832 krb5_error_code retval;
833 krb5_principal princ;
834 krb5_keyblock *keyblock;
835 krb5_data packet;
836 krb5_auth_context auth_context;
837 char phost[BUFSIZ];
838 const char *services[3], **service;
839
840 packet.data = 0;
841
842 /* If possible we want to try and verify the ticket we have
843 * received against a keytab. We will try multiple service
844 * principals, including at least the host principal and the PAM
845 * service principal. The host principal is preferred because access
846 * to that key is generally sufficient to compromise root, while the
847 * service key for this PAM service may be less carefully guarded.
848 * It is important to check the keytab first before the KDC so we do
849 * not get spoofed by a fake KDC.
850 */
851 services[0] = "host";
852 services[1] = pam_service;
853 services[2] = NULL;
854 keyblock = 0;
855 retval = -1;
856 for (service = &services[0]; *service != NULL; service++) {
857 retval = krb5_sname_to_principal(context, NULL, *service,
858 KRB5_NT_SRV_HST, &princ);
859 if (retval != 0) {
860 if (debug)
861 syslog(LOG_DEBUG,
862 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
863 "krb5_sname_to_principal()",
864 krb5_get_err_text(context, retval));
865 return -1;
866 }
867
868 /* Extract the name directly. */
869 strncpy(phost, compat_princ_component(context, princ, 1),
870 BUFSIZ);
871 phost[BUFSIZ - 1] = '\0';
872
873 /*
874 * Do we have service/<host> keys?
875 * (use default/configured keytab, kvno IGNORE_VNO to get the
876 * first match, and ignore enctype.)
877 */
878 retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
879 &keyblock);
880 if (retval != 0)
881 continue;
882 break;
883 }
884 if (retval != 0) { /* failed to find key */
885 /* Keytab or service key does not exist */
886 if (debug)
887 syslog(LOG_DEBUG,
888 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
889 "krb5_kt_read_service_key()",
890 krb5_get_err_text(context, retval));
891 retval = 0;
892 goto cleanup;
893 }
894 if (keyblock)
895 krb5_free_keyblock(context, keyblock);
896
897 /* Talk to the kdc and construct the ticket. */
898 auth_context = NULL;
899 retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
900 NULL, ccache, &packet);
901 if (auth_context) {
902 krb5_auth_con_free(context, auth_context);
903 auth_context = NULL; /* setup for rd_req */
904 }
905 if (retval) {
906 if (debug)
907 syslog(LOG_DEBUG,
908 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
909 "krb5_mk_req()",
910 krb5_get_err_text(context, retval));
911 retval = -1;
912 goto cleanup;
913 }
914
915 /* Try to use the ticket. */
916 retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
917 NULL, NULL);
918 if (retval) {
919 if (debug)
920 syslog(LOG_DEBUG,
921 "pam_krb5: verify_krb_v5_tgt(): %s: %s",
922 "krb5_rd_req()",
923 krb5_get_err_text(context, retval));
924 retval = -1;
925 }
926 else
927 retval = 1;
928
929 cleanup:
930 if (packet.data)
931 compat_free_data_contents(context, &packet);
932 krb5_free_principal(context, princ);
933 return retval;
934 }
935
936 /* Free the memory for cache_name. Called by pam_end() */
937 /* ARGSUSED */
938 static void
939 cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
940 {
941 krb5_context pam_context;
942 krb5_ccache ccache;
943 krb5_error_code krbret;
944
945 if (krb5_init_context(&pam_context))
946 return;
947
948 krbret = krb5_cc_resolve(pam_context, data, &ccache);
949 if (krbret == 0)
950 krb5_cc_destroy(pam_context, ccache);
951 krb5_free_context(pam_context);
952 free(data);
953 }
954
955 #ifdef COMPAT_HEIMDAL
956 #ifdef COMPAT_MIT
957 #error This cannot be MIT and Heimdal compatible!
958 #endif
959 #endif
960
961 #ifndef COMPAT_HEIMDAL
962 #ifndef COMPAT_MIT
963 #error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
964 #endif
965 #endif
966
967 #ifdef COMPAT_HEIMDAL
968 /* ARGSUSED */
969 static const char *
970 compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
971 {
972 return princ->name.name_string.val[n];
973 }
974
975 /* ARGSUSED */
976 static void
977 compat_free_data_contents(krb5_context context __unused, krb5_data * data)
978 {
979 krb5_xfree(data->data);
980 }
981 #endif
982
983 #ifdef COMPAT_MIT
984 static const char *
985 compat_princ_component(krb5_context context, krb5_principal princ, int n)
986 {
987 return krb5_princ_component(context, princ, n)->data;
988 }
989
990 static void
991 compat_free_data_contents(krb5_context context, krb5_data * data)
992 {
993 krb5_free_data_contents(context, data);
994 }
995 #endif
996