pam_krb5.c revision 115470
1/*-
2 * This pam_krb5 module contains code that is:
3 *   Copyright (c) Derrick J. Brashear, 1996. All rights reserved.
4 *   Copyright (c) Frank Cusack, 1999-2001. All rights reserved.
5 *   Copyright (c) Jacques A. Vidrine, 2000-2001. All rights reserved.
6 *   Copyright (c) Nicolas Williams, 2001. All rights reserved.
7 *   Copyright (c) Perot Systems Corporation, 2001. All rights reserved.
8 *   Copyright (c) Mark R V Murray, 2001.  All rights reserved.
9 *   Copyright (c) Networks Associates Technology, Inc., 2002-2003.
10 *       All rights reserved.
11 *
12 * Portions of this software were developed for the FreeBSD Project by
13 * ThinkSec AS and NAI Labs, the Security Research Division of Network
14 * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
15 * ("CBOSS"), as part of the DARPA CHATS research program.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notices, and the entire permission notice in its entirety,
22 *    including the disclaimer of warranties.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote
27 *    products derived from this software without specific prior
28 *    written permission.
29 *
30 * ALTERNATIVELY, this product may be distributed under the terms of
31 * the GNU Public License, in which case the provisions of the GPL are
32 * required INSTEAD OF the above restrictions.  (This clause is
33 * necessary due to a potential bad interaction between the GPL and
34 * the restrictions contained in a BSD-style copyright.)
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
40 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 */
49
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_krb5/pam_krb5.c 115470 2003-05-31 17:19:03Z des $");
52
53#include <sys/types.h>
54#include <sys/stat.h>
55#include <errno.h>
56#include <limits.h>
57#include <pwd.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <syslog.h>
62#include <unistd.h>
63
64#include <krb5.h>
65#include <com_err.h>
66
67#define	PAM_SM_AUTH
68#define	PAM_SM_ACCOUNT
69#define	PAM_SM_PASSWORD
70
71#include <security/pam_appl.h>
72#include <security/pam_modules.h>
73#include <security/pam_mod_misc.h>
74#include <security/openpam.h>
75
76#define	COMPAT_HEIMDAL
77/* #define	COMPAT_MIT */
78
79static int	verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int);
80static void	cleanup_cache(pam_handle_t *, void *, int);
81static const	char *compat_princ_component(krb5_context, krb5_principal, int);
82static void	compat_free_data_contents(krb5_context, krb5_data *);
83
84#define USER_PROMPT		"Username: "
85#define PASSWORD_PROMPT		"Password:"
86#define NEW_PASSWORD_PROMPT	"New Password:"
87
88#define PAM_OPT_CCACHE		"ccache"
89#define PAM_OPT_FORWARDABLE	"forwardable"
90#define PAM_OPT_NO_CCACHE	"no_ccache"
91#define PAM_OPT_REUSE_CCACHE	"reuse_ccache"
92
93/*
94 * authentication management
95 */
96PAM_EXTERN int
97pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
98    int argc __unused, const char *argv[] __unused)
99{
100	krb5_error_code krbret;
101	krb5_context pam_context;
102	krb5_creds creds;
103	krb5_principal princ;
104	krb5_ccache ccache;
105	krb5_get_init_creds_opt opts;
106	struct passwd *pwd;
107	int retval;
108	const char *sourceuser, *user, *pass, *service;
109	char *principal, *princ_name, *ccache_name, luser[32], *srvdup;
110
111	retval = pam_get_user(pamh, &user, USER_PROMPT);
112	if (retval != PAM_SUCCESS)
113		return (retval);
114
115	PAM_LOG("Got user: %s", user);
116
117	retval = pam_get_item(pamh, PAM_RUSER, (const void **)&sourceuser);
118	if (retval != PAM_SUCCESS)
119		return (retval);
120
121	PAM_LOG("Got ruser: %s", sourceuser);
122
123	service = NULL;
124	pam_get_item(pamh, PAM_SERVICE, (const void **)&service);
125	if (service == NULL)
126		service = "unknown";
127
128	PAM_LOG("Got service: %s", service);
129
130	krbret = krb5_init_context(&pam_context);
131	if (krbret != 0) {
132		PAM_VERBOSE_ERROR("Kerberos 5 error");
133		return (PAM_SERVICE_ERR);
134	}
135
136	PAM_LOG("Context initialised");
137
138	krb5_get_init_creds_opt_init(&opts);
139
140	if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
141		krb5_get_init_creds_opt_set_forwardable(&opts, 1);
142
143	PAM_LOG("Credentials initialised");
144
145	krbret = krb5_cc_register(pam_context, &krb5_mcc_ops, FALSE);
146	if (krbret != 0 && krbret != KRB5_CC_TYPE_EXISTS) {
147		PAM_VERBOSE_ERROR("Kerberos 5 error");
148		retval = PAM_SERVICE_ERR;
149		goto cleanup3;
150	}
151
152	PAM_LOG("Done krb5_cc_register()");
153
154	/* Get principal name */
155	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
156		asprintf(&principal, "%s/%s", sourceuser, user);
157	else
158		principal = strdup(user);
159
160	PAM_LOG("Created principal: %s", principal);
161
162	krbret = krb5_parse_name(pam_context, principal, &princ);
163	free(principal);
164	if (krbret != 0) {
165		PAM_LOG("Error krb5_parse_name(): %s",
166		    krb5_get_err_text(pam_context, krbret));
167		PAM_VERBOSE_ERROR("Kerberos 5 error");
168		retval = PAM_SERVICE_ERR;
169		goto cleanup3;
170	}
171
172	PAM_LOG("Done krb5_parse_name()");
173
174	/* Now convert the principal name into something human readable */
175	princ_name = NULL;
176	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
177	if (krbret != 0) {
178		PAM_LOG("Error krb5_unparse_name(): %s",
179		    krb5_get_err_text(pam_context, krbret));
180		PAM_VERBOSE_ERROR("Kerberos 5 error");
181		retval = PAM_SERVICE_ERR;
182		goto cleanup2;
183	}
184
185	PAM_LOG("Got principal: %s", princ_name);
186
187	/* Get password */
188	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, PASSWORD_PROMPT);
189	if (retval != PAM_SUCCESS)
190		goto cleanup2;
191
192	PAM_LOG("Got password");
193
194	/* Verify the local user exists (AFTER getting the password) */
195	if (strchr(user, '@')) {
196		/* get a local account name for this principal */
197		krbret = krb5_aname_to_localname(pam_context, princ,
198		    sizeof(luser), luser);
199		if (krbret != 0) {
200			PAM_VERBOSE_ERROR("Kerberos 5 error");
201			PAM_LOG("Error krb5_aname_to_localname(): %s",
202			    krb5_get_err_text(pam_context, krbret));
203			retval = PAM_USER_UNKNOWN;
204			goto cleanup2;
205		}
206
207		retval = pam_set_item(pamh, PAM_USER, luser);
208		if (retval != PAM_SUCCESS)
209			goto cleanup2;
210
211		retval = pam_get_item(pamh, PAM_USER, (const void **)&user);
212		if (retval != PAM_SUCCESS)
213			goto cleanup2;
214
215		PAM_LOG("PAM_USER Redone");
216	}
217
218	pwd = getpwnam(user);
219	if (pwd == NULL) {
220		retval = PAM_USER_UNKNOWN;
221		goto cleanup2;
222	}
223
224	PAM_LOG("Done getpwnam()");
225
226	/* Get a TGT */
227	memset(&creds, 0, sizeof(krb5_creds));
228	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
229	    pass, NULL, pamh, 0, NULL, &opts);
230	if (krbret != 0) {
231		PAM_VERBOSE_ERROR("Kerberos 5 error");
232		PAM_LOG("Error krb5_get_init_creds_password(): %s",
233		    krb5_get_err_text(pam_context, krbret));
234		retval = PAM_AUTH_ERR;
235		goto cleanup2;
236	}
237
238	PAM_LOG("Got TGT");
239
240	/* Generate a temporary cache */
241	krbret = krb5_cc_gen_new(pam_context, &krb5_mcc_ops, &ccache);
242	if (krbret != 0) {
243		PAM_VERBOSE_ERROR("Kerberos 5 error");
244		PAM_LOG("Error krb5_cc_gen_new(): %s",
245		    krb5_get_err_text(pam_context, krbret));
246		retval = PAM_SERVICE_ERR;
247		goto cleanup;
248	}
249	krbret = krb5_cc_initialize(pam_context, ccache, princ);
250	if (krbret != 0) {
251		PAM_VERBOSE_ERROR("Kerberos 5 error");
252		PAM_LOG("Error krb5_cc_initialize(): %s",
253		    krb5_get_err_text(pam_context, krbret));
254		retval = PAM_SERVICE_ERR;
255		goto cleanup;
256	}
257	krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
258	if (krbret != 0) {
259		PAM_VERBOSE_ERROR("Kerberos 5 error");
260		PAM_LOG("Error krb5_cc_store_cred(): %s",
261		    krb5_get_err_text(pam_context, krbret));
262		krb5_cc_destroy(pam_context, ccache);
263		retval = PAM_SERVICE_ERR;
264		goto cleanup;
265	}
266
267	PAM_LOG("Credentials stashed");
268
269	/* Verify them */
270	if ((srvdup = strdup(service)) == NULL) {
271		retval = PAM_BUF_ERR;
272		goto cleanup;
273	}
274	krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
275	    openpam_get_option(pamh, PAM_OPT_FORWARDABLE) ? 1 : 0);
276	free(srvdup);
277	if (krbret == -1) {
278		PAM_VERBOSE_ERROR("Kerberos 5 error");
279		krb5_cc_destroy(pam_context, ccache);
280		retval = PAM_AUTH_ERR;
281		goto cleanup;
282	}
283
284	PAM_LOG("Credentials stash verified");
285
286	retval = pam_get_data(pamh, "ccache", (const void **)&ccache_name);
287	if (retval == PAM_SUCCESS) {
288		krb5_cc_destroy(pam_context, ccache);
289		PAM_VERBOSE_ERROR("Kerberos 5 error");
290		retval = PAM_AUTH_ERR;
291		goto cleanup;
292	}
293
294	PAM_LOG("Credentials stash not pre-existing");
295
296	asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
297		ccache), krb5_cc_get_name(pam_context, ccache));
298	if (ccache_name == NULL) {
299		PAM_VERBOSE_ERROR("Kerberos 5 error");
300		retval = PAM_BUF_ERR;
301		goto cleanup;
302	}
303	retval = pam_set_data(pamh, "ccache", ccache_name, cleanup_cache);
304	if (retval != 0) {
305		krb5_cc_destroy(pam_context, ccache);
306		PAM_VERBOSE_ERROR("Kerberos 5 error");
307		retval = PAM_SERVICE_ERR;
308		goto cleanup;
309	}
310
311	PAM_LOG("Credentials stash saved");
312
313cleanup:
314	krb5_free_cred_contents(pam_context, &creds);
315	PAM_LOG("Done cleanup");
316cleanup2:
317	krb5_free_principal(pam_context, princ);
318	PAM_LOG("Done cleanup2");
319cleanup3:
320	if (princ_name)
321		free(princ_name);
322
323	krb5_free_context(pam_context);
324
325	PAM_LOG("Done cleanup3");
326
327	if (retval != PAM_SUCCESS)
328		PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
329
330	return (retval);
331}
332
333PAM_EXTERN int
334pam_sm_setcred(pam_handle_t *pamh, int flags,
335    int argc __unused, const char *argv[] __unused)
336{
337
338	krb5_error_code krbret;
339	krb5_context pam_context;
340	krb5_principal princ;
341	krb5_creds creds;
342	krb5_ccache ccache_temp, ccache_perm;
343	krb5_cc_cursor cursor;
344	struct passwd *pwd = NULL;
345	int retval;
346	const char *cache_name, *q, *user;
347	char *cache_name_buf = NULL, *p;
348
349	uid_t euid;
350	gid_t egid;
351
352	if (flags & PAM_DELETE_CRED)
353		return (PAM_SUCCESS);
354
355	if (flags & PAM_REFRESH_CRED)
356		return (PAM_SUCCESS);
357
358	if (flags & PAM_REINITIALIZE_CRED)
359		return (PAM_SUCCESS);
360
361	if (!(flags & PAM_ESTABLISH_CRED))
362		return (PAM_SERVICE_ERR);
363
364	PAM_LOG("Establishing credentials");
365
366	/* Get username */
367	retval = pam_get_item(pamh, PAM_USER, (const void **)&user);
368	if (retval != PAM_SUCCESS)
369		return (retval);
370
371	PAM_LOG("Got user: %s", user);
372
373	krbret = krb5_init_context(&pam_context);
374	if (krbret != 0) {
375		PAM_LOG("Error krb5_init_context() failed");
376		return (PAM_SERVICE_ERR);
377	}
378
379	PAM_LOG("Context initialised");
380
381	euid = geteuid();	/* Usually 0 */
382	egid = getegid();
383
384	PAM_LOG("Got euid, egid: %d %d", euid, egid);
385
386	/* Retrieve the temporary cache */
387	retval = pam_get_data(pamh, "ccache", (const void **)&cache_name);
388	if (retval != PAM_SUCCESS) {
389		retval = PAM_CRED_UNAVAIL;
390		goto cleanup3;
391	}
392	krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_temp);
393	if (krbret != 0) {
394		PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", cache_name,
395		    krb5_get_err_text(pam_context, krbret));
396		retval = PAM_SERVICE_ERR;
397		goto cleanup3;
398	}
399
400	/* Get the uid. This should exist. */
401	pwd = getpwnam(user);
402	if (pwd == NULL) {
403		retval = PAM_USER_UNKNOWN;
404		goto cleanup3;
405	}
406
407	PAM_LOG("Done getpwnam()");
408
409	/* Avoid following a symlink as root */
410	if (setegid(pwd->pw_gid)) {
411		retval = PAM_SERVICE_ERR;
412		goto cleanup3;
413	}
414	if (seteuid(pwd->pw_uid)) {
415		retval = PAM_SERVICE_ERR;
416		goto cleanup3;
417	}
418
419	PAM_LOG("Done setegid() & seteuid()");
420
421	/* Get the cache name */
422	cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
423	if (cache_name == NULL) {
424		asprintf(&cache_name_buf, "FILE:/tmp/krb5cc_%d", pwd->pw_uid);
425		cache_name = cache_name_buf;
426	}
427
428	p = calloc(PATH_MAX + 16, sizeof(char));
429	q = cache_name;
430
431	if (p == NULL) {
432		PAM_LOG("Error malloc(): failure");
433		retval = PAM_BUF_ERR;
434		goto cleanup3;
435	}
436	cache_name = p;
437
438	/* convert %u and %p */
439	while (*q) {
440		if (*q == '%') {
441			q++;
442			if (*q == 'u') {
443				sprintf(p, "%d", pwd->pw_uid);
444				p += strlen(p);
445			}
446			else if (*q == 'p') {
447				sprintf(p, "%d", getpid());
448				p += strlen(p);
449			}
450			else {
451				/* Not a special token */
452				*p++ = '%';
453				q--;
454			}
455			q++;
456		}
457		else {
458			*p++ = *q++;
459		}
460	}
461
462	PAM_LOG("Got cache_name: %s", cache_name);
463
464	/* Initialize the new ccache */
465	krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
466	if (krbret != 0) {
467		PAM_LOG("Error krb5_cc_get_principal(): %s",
468		    krb5_get_err_text(pam_context, krbret));
469		retval = PAM_SERVICE_ERR;
470		goto cleanup3;
471	}
472	krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
473	if (krbret != 0) {
474		PAM_LOG("Error krb5_cc_resolve(): %s",
475		    krb5_get_err_text(pam_context, krbret));
476		retval = PAM_SERVICE_ERR;
477		goto cleanup2;
478	}
479	krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
480	if (krbret != 0) {
481		PAM_LOG("Error krb5_cc_initialize(): %s",
482		    krb5_get_err_text(pam_context, krbret));
483		retval = PAM_SERVICE_ERR;
484		goto cleanup2;
485	}
486
487	PAM_LOG("Cache initialised");
488
489	/* Prepare for iteration over creds */
490	krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
491	if (krbret != 0) {
492		PAM_LOG("Error krb5_cc_start_seq_get(): %s",
493		    krb5_get_err_text(pam_context, krbret));
494		krb5_cc_destroy(pam_context, ccache_perm);
495		retval = PAM_SERVICE_ERR;
496		goto cleanup2;
497	}
498
499	PAM_LOG("Prepared for iteration");
500
501	/* Copy the creds (should be two of them) */
502	while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
503				&cursor, &creds) == 0)) {
504		krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
505		if (krbret != 0) {
506			PAM_LOG("Error krb5_cc_store_cred(): %s",
507			    krb5_get_err_text(pam_context, krbret));
508			krb5_cc_destroy(pam_context, ccache_perm);
509			krb5_free_cred_contents(pam_context, &creds);
510			retval = PAM_SERVICE_ERR;
511			goto cleanup2;
512		}
513		krb5_free_cred_contents(pam_context, &creds);
514		PAM_LOG("Iteration");
515	}
516	krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
517
518	PAM_LOG("Done iterating");
519
520	if (strstr(cache_name, "FILE:") == cache_name) {
521		if (chown(&cache_name[5], pwd->pw_uid, pwd->pw_gid) == -1) {
522			PAM_LOG("Error chown(): %s", strerror(errno));
523			krb5_cc_destroy(pam_context, ccache_perm);
524			retval = PAM_SERVICE_ERR;
525			goto cleanup2;
526		}
527		PAM_LOG("Done chown()");
528
529		if (chmod(&cache_name[5], (S_IRUSR | S_IWUSR)) == -1) {
530			PAM_LOG("Error chmod(): %s", strerror(errno));
531			krb5_cc_destroy(pam_context, ccache_perm);
532			retval = PAM_SERVICE_ERR;
533			goto cleanup2;
534		}
535		PAM_LOG("Done chmod()");
536	}
537
538	krb5_cc_close(pam_context, ccache_perm);
539
540	PAM_LOG("Cache closed");
541
542	retval = pam_setenv(pamh, "KRB5CCNAME", cache_name, 1);
543	if (retval != PAM_SUCCESS) {
544		PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
545		krb5_cc_destroy(pam_context, ccache_perm);
546		retval = PAM_SERVICE_ERR;
547		goto cleanup2;
548	}
549
550	PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
551
552cleanup2:
553	krb5_free_principal(pam_context, princ);
554	PAM_LOG("Done cleanup2");
555cleanup3:
556	krb5_free_context(pam_context);
557	PAM_LOG("Done cleanup3");
558
559	seteuid(euid);
560	setegid(egid);
561
562	PAM_LOG("Done seteuid() & setegid()");
563
564	if (cache_name_buf != NULL)
565		free(cache_name_buf);
566
567	return (retval);
568}
569
570/*
571 * account management
572 */
573PAM_EXTERN int
574pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
575    int argc __unused, const char *argv[] __unused)
576{
577	krb5_error_code krbret;
578	krb5_context pam_context;
579	krb5_ccache ccache;
580	krb5_principal princ;
581	int retval;
582	const char *user, *ccache_name;
583
584	retval = pam_get_item(pamh, PAM_USER, (const void **)&user);
585	if (retval != PAM_SUCCESS)
586		return (retval);
587
588	PAM_LOG("Got user: %s", user);
589
590	retval = pam_get_data(pamh, "ccache", (const void **)&ccache_name);
591	if (retval != PAM_SUCCESS)
592		return (PAM_SUCCESS);
593
594	PAM_LOG("Got credentials");
595
596	krbret = krb5_init_context(&pam_context);
597	if (krbret != 0) {
598		PAM_LOG("Error krb5_init_context() failed");
599		return (PAM_PERM_DENIED);
600	}
601
602	PAM_LOG("Context initialised");
603
604	krbret = krb5_cc_resolve(pam_context, ccache_name, &ccache);
605	if (krbret != 0) {
606		PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", ccache_name,
607		    krb5_get_err_text(pam_context, krbret));
608		krb5_free_context(pam_context);
609		return (PAM_PERM_DENIED);
610	}
611
612	PAM_LOG("Got ccache %s", ccache_name);
613
614
615	krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
616	if (krbret != 0) {
617		PAM_LOG("Error krb5_cc_get_principal(): %s",
618		    krb5_get_err_text(pam_context, krbret));
619		retval = PAM_PERM_DENIED;;
620		goto cleanup;
621	}
622
623	PAM_LOG("Got principal");
624
625	if (krb5_kuserok(pam_context, princ, user))
626		retval = PAM_SUCCESS;
627	else
628		retval = PAM_PERM_DENIED;
629	krb5_free_principal(pam_context, princ);
630
631	PAM_LOG("Done kuserok()");
632
633cleanup:
634	krb5_free_context(pam_context);
635	PAM_LOG("Done cleanup");
636
637	return (retval);
638
639}
640
641/*
642 * password management
643 */
644PAM_EXTERN int
645pam_sm_chauthtok(pam_handle_t *pamh, int flags,
646    int argc __unused, const char *argv[] __unused)
647{
648	krb5_error_code krbret;
649	krb5_context pam_context;
650	krb5_creds creds;
651	krb5_principal princ;
652	krb5_get_init_creds_opt opts;
653	krb5_data result_code_string, result_string;
654	int result_code, retval;
655	const char *user, *pass;
656	char *princ_name, *passdup;
657
658	if (!(flags & PAM_UPDATE_AUTHTOK))
659		return (PAM_AUTHTOK_ERR);
660
661	retval = pam_get_item(pamh, PAM_USER, (const void **)&user);
662	if (retval != PAM_SUCCESS)
663		return (retval);
664
665	PAM_LOG("Got user: %s", user);
666
667	krbret = krb5_init_context(&pam_context);
668	if (krbret != 0) {
669		PAM_LOG("Error krb5_init_context() failed");
670		return (PAM_SERVICE_ERR);
671	}
672
673	PAM_LOG("Context initialised");
674
675	krb5_get_init_creds_opt_init(&opts);
676
677	PAM_LOG("Credentials options initialised");
678
679	/* Get principal name */
680	krbret = krb5_parse_name(pam_context, user, &princ);
681	if (krbret != 0) {
682		PAM_LOG("Error krb5_parse_name(): %s",
683		    krb5_get_err_text(pam_context, krbret));
684		retval = PAM_USER_UNKNOWN;
685		goto cleanup3;
686	}
687
688	/* Now convert the principal name into something human readable */
689	princ_name = NULL;
690	krbret = krb5_unparse_name(pam_context, princ, &princ_name);
691	if (krbret != 0) {
692		PAM_LOG("Error krb5_unparse_name(): %s",
693		    krb5_get_err_text(pam_context, krbret));
694		retval = PAM_SERVICE_ERR;
695		goto cleanup2;
696	}
697
698	PAM_LOG("Got principal: %s", princ_name);
699
700	/* Get password */
701	retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass, PASSWORD_PROMPT);
702	if (retval != PAM_SUCCESS)
703		goto cleanup2;
704
705	PAM_LOG("Got password");
706
707	memset(&creds, 0, sizeof(krb5_creds));
708	krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
709	    pass, NULL, pamh, 0, "kadmin/changepw", &opts);
710	if (krbret != 0) {
711		PAM_LOG("Error krb5_get_init_creds_password()",
712		    krb5_get_err_text(pam_context, krbret));
713		retval = PAM_AUTH_ERR;
714		goto cleanup2;
715	}
716
717	PAM_LOG("Credentials established");
718
719	/* Now get the new password */
720	for (;;) {
721		retval = pam_get_authtok(pamh,
722		    PAM_AUTHTOK, &pass, NEW_PASSWORD_PROMPT);
723		if (retval != PAM_TRY_AGAIN)
724			break;
725		pam_error(pamh, "Mismatch; try again, EOF to quit.");
726	}
727	if (retval != PAM_SUCCESS)
728		goto cleanup;
729
730	PAM_LOG("Got new password");
731
732	/* Change it */
733	if ((passdup = strdup(pass)) == NULL) {
734		retval = PAM_BUF_ERR;
735		goto cleanup;
736	}
737	krbret = krb5_change_password(pam_context, &creds, passdup,
738	    &result_code, &result_code_string, &result_string);
739	free(passdup);
740	if (krbret != 0) {
741		PAM_LOG("Error krb5_change_password(): %s",
742		    krb5_get_err_text(pam_context, krbret));
743		retval = PAM_AUTHTOK_ERR;
744		goto cleanup;
745	}
746	if (result_code) {
747		PAM_LOG("Error krb5_change_password(): (result_code)");
748		retval = PAM_AUTHTOK_ERR;
749		goto cleanup;
750	}
751
752	PAM_LOG("Password changed");
753
754	if (result_string.data)
755		free(result_string.data);
756	if (result_code_string.data)
757		free(result_code_string.data);
758
759cleanup:
760	krb5_free_cred_contents(pam_context, &creds);
761	PAM_LOG("Done cleanup");
762cleanup2:
763	krb5_free_principal(pam_context, princ);
764	PAM_LOG("Done cleanup2");
765cleanup3:
766	if (princ_name)
767		free(princ_name);
768
769	krb5_free_context(pam_context);
770
771	PAM_LOG("Done cleanup3");
772
773	return (retval);
774}
775
776PAM_MODULE_ENTRY("pam_krb5");
777
778/*
779 * This routine with some modification is from the MIT V5B6 appl/bsd/login.c
780 * Modified by Sam Hartman <hartmans@mit.edu> to support PAM services
781 * for Debian.
782 *
783 * Verify the Kerberos ticket-granting ticket just retrieved for the
784 * user.  If the Kerberos server doesn't respond, assume the user is
785 * trying to fake us out (since we DID just get a TGT from what is
786 * supposedly our KDC).  If the host/<host> service is unknown (i.e.,
787 * the local keytab doesn't have it), and we cannot find another
788 * service we do have, let her in.
789 *
790 * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
791 */
792/* ARGSUSED */
793static int
794verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
795    char *pam_service, int debug)
796{
797	krb5_error_code retval;
798	krb5_principal princ;
799	krb5_keyblock *keyblock;
800	krb5_data packet;
801	krb5_auth_context auth_context;
802	char phost[BUFSIZ];
803	const char *services[3], **service;
804
805	packet.data = 0;
806
807	/* If possible we want to try and verify the ticket we have
808	 * received against a keytab.  We will try multiple service
809	 * principals, including at least the host principal and the PAM
810	 * service principal.  The host principal is preferred because access
811	 * to that key is generally sufficient to compromise root, while the
812	 * service key for this PAM service may be less carefully guarded.
813	 * It is important to check the keytab first before the KDC so we do
814	 * not get spoofed by a fake KDC.
815	 */
816	services[0] = "host";
817	services[1] = pam_service;
818	services[2] = NULL;
819	keyblock = 0;
820	retval = -1;
821	for (service = &services[0]; *service != NULL; service++) {
822		retval = krb5_sname_to_principal(context, NULL, *service,
823		    KRB5_NT_SRV_HST, &princ);
824		if (retval != 0) {
825			if (debug)
826				syslog(LOG_DEBUG,
827				    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
828				    "krb5_sname_to_principal()",
829				    krb5_get_err_text(context, retval));
830			return -1;
831		}
832
833		/* Extract the name directly. */
834		strncpy(phost, compat_princ_component(context, princ, 1),
835		    BUFSIZ);
836		phost[BUFSIZ - 1] = '\0';
837
838		/*
839		 * Do we have service/<host> keys?
840		 * (use default/configured keytab, kvno IGNORE_VNO to get the
841		 * first match, and ignore enctype.)
842		 */
843		retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
844		    &keyblock);
845		if (retval != 0)
846			continue;
847		break;
848	}
849	if (retval != 0) {	/* failed to find key */
850		/* Keytab or service key does not exist */
851		if (debug)
852			syslog(LOG_DEBUG,
853			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
854			    "krb5_kt_read_service_key()",
855			    krb5_get_err_text(context, retval));
856		retval = 0;
857		goto cleanup;
858	}
859	if (keyblock)
860		krb5_free_keyblock(context, keyblock);
861
862	/* Talk to the kdc and construct the ticket. */
863	auth_context = NULL;
864	retval = krb5_mk_req(context, &auth_context, 0, *service, phost,
865		NULL, ccache, &packet);
866	if (auth_context) {
867		krb5_auth_con_free(context, auth_context);
868		auth_context = NULL;	/* setup for rd_req */
869	}
870	if (retval) {
871		if (debug)
872			syslog(LOG_DEBUG,
873			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
874			    "krb5_mk_req()",
875			    krb5_get_err_text(context, retval));
876		retval = -1;
877		goto cleanup;
878	}
879
880	/* Try to use the ticket. */
881	retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
882	    NULL, NULL);
883	if (retval) {
884		if (debug)
885			syslog(LOG_DEBUG,
886			    "pam_krb5: verify_krb_v5_tgt(): %s: %s",
887			    "krb5_rd_req()",
888			    krb5_get_err_text(context, retval));
889		retval = -1;
890	}
891	else
892		retval = 1;
893
894cleanup:
895	if (packet.data)
896		compat_free_data_contents(context, &packet);
897	krb5_free_principal(context, princ);
898	return retval;
899}
900
901/* Free the memory for cache_name. Called by pam_end() */
902/* ARGSUSED */
903static void
904cleanup_cache(pam_handle_t *pamh __unused, void *data, int pam_end_status __unused)
905{
906	krb5_context pam_context;
907	krb5_ccache ccache;
908	krb5_error_code krbret;
909
910	if (krb5_init_context(&pam_context))
911		return;
912
913	krbret = krb5_cc_resolve(pam_context, data, &ccache);
914	if (krbret == 0)
915		krb5_cc_destroy(pam_context, ccache);
916	krb5_free_context(pam_context);
917	free(data);
918}
919
920#ifdef COMPAT_HEIMDAL
921#ifdef COMPAT_MIT
922#error This cannot be MIT and Heimdal compatible!
923#endif
924#endif
925
926#ifndef COMPAT_HEIMDAL
927#ifndef COMPAT_MIT
928#error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
929#endif
930#endif
931
932#ifdef COMPAT_HEIMDAL
933/* ARGSUSED */
934static const char *
935compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
936{
937	return princ->name.name_string.val[n];
938}
939
940/* ARGSUSED */
941static void
942compat_free_data_contents(krb5_context context __unused, krb5_data * data)
943{
944	krb5_xfree(data->data);
945}
946#endif
947
948#ifdef COMPAT_MIT
949static const char *
950compat_princ_component(krb5_context context, krb5_principal princ, int n)
951{
952	return krb5_princ_component(context, princ, n)->data;
953}
954
955static void
956compat_free_data_contents(krb5_context context, krb5_data * data)
957{
958	krb5_free_data_contents(context, data);
959}
960#endif
961