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