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