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