2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
35 #include "kuserok_plugin.h"
38 #ifndef SYSTEM_K5LOGIN_DIR
40 * System k5login location. File namess in this directory are expected
41 * to be usernames and to contain a list of principals allowed to login
42 * as the user named the same as the file.
44 #define SYSTEM_K5LOGIN_DIR SYSCONFDIR "/k5login.d"
47 /* Plugin framework bits */
51 const char *k5login_dir
;
53 krb5_const_principal principal
;
58 static krb5_error_code KRB5_LIB_CALL
59 plcallback(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
61 const krb5plugin_kuserok_ftable
*locate
= plug
;
62 struct plctx
*plctx
= userctx
;
64 return locate
->kuserok(plugctx
, context
, plctx
->rule
, plctx
->flags
,
65 plctx
->k5login_dir
, plctx
->luser
, plctx
->principal
,
69 static krb5_error_code plugin_reg_ret
;
70 static krb5plugin_kuserok_ftable kuserok_simple_plug
;
71 static krb5plugin_kuserok_ftable kuserok_sys_k5login_plug
;
72 static krb5plugin_kuserok_ftable kuserok_user_k5login_plug
;
73 static krb5plugin_kuserok_ftable kuserok_deny_plug
;
76 reg_def_plugins_once(void *ctx
)
79 krb5_context context
= ctx
;
81 plugin_reg_ret
= krb5_plugin_register(context
, PLUGIN_TYPE_DATA
,
83 &kuserok_simple_plug
);
84 ret
= krb5_plugin_register(context
, PLUGIN_TYPE_DATA
,
85 KRB5_PLUGIN_KUSEROK
, &kuserok_sys_k5login_plug
);
88 ret
= krb5_plugin_register(context
, PLUGIN_TYPE_DATA
,
89 KRB5_PLUGIN_KUSEROK
, &kuserok_user_k5login_plug
);
92 ret
= krb5_plugin_register(context
, PLUGIN_TYPE_DATA
,
93 KRB5_PLUGIN_KUSEROK
, &kuserok_deny_plug
);
99 * This function is designed to be portable for Win32 and POSIX. The
100 * design does lead to multiple getpwnam_r() calls, but this is probably
105 * @param context A krb5_context
106 * @param filename Name of item to introspection
107 * @param is_system_location TRUE if the dir/file are system locations or
108 * FALSE if they are user home directory locations
109 * @param dir Directory (optional)
110 * @param dirlstat A pointer to struct stat for the directory (optional)
111 * @param file File (optional)
112 * @param owner Name of user that is expected to own the file
115 static krb5_error_code
116 check_owner_dir(krb5_context context
,
117 const char *filename
,
118 krb5_boolean is_system_location
,
120 struct stat
*dirlstat
,
125 * XXX Implement this!
127 * The thing to do is to call _get_osfhandle() on fileno(file) and
128 * dirfd(dir) to get HANDLEs to the same, then call
129 * GetSecurityInfo() on those HANDLEs to get the security descriptor
130 * (SD), then check the owner and DACL. Checking the DACL sounds
131 * like a lot of work (what, derive a mode from the ACL the way
132 * NFSv4 servers do?). Checking the owner means doing an LSARPC
133 * lookup at least (to get the user's SID).
135 if (is_system_location
|| owner
== NULL
)
137 krb5_set_error_message(context
, EACCES
,
138 "User k5login files not supported on Windows");
141 struct passwd pw
, *pwd
= NULL
;
145 heim_assert(owner
!= NULL
, "no directory owner ?");
147 if (rk_getpwnam_r(owner
, &pw
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0) {
148 krb5_set_error_message(context
, errno
,
149 "User unknown %s (getpwnam_r())", owner
);
153 krb5_set_error_message(context
, EACCES
, "no user %s", owner
);
157 if (fstat(dirfd(dir
), &st
) == -1) {
158 krb5_set_error_message(context
, EACCES
,
159 "fstat(%s) of k5login.d failed",
163 if (!S_ISDIR(st
.st_mode
)) {
164 krb5_set_error_message(context
, ENOTDIR
, "%s not a directory",
168 if (st
.st_dev
!= dirlstat
->st_dev
|| st
.st_ino
!= dirlstat
->st_ino
) {
169 krb5_set_error_message(context
, EACCES
,
170 "%s was renamed during kuserok "
171 "operation", filename
);
174 if ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) != 0) {
175 krb5_set_error_message(context
, EACCES
,
176 "%s has world and/or group write "
177 "permissions", filename
);
180 if (pwd
->pw_uid
!= st
.st_uid
&& st
.st_uid
!= 0) {
181 krb5_set_error_message(context
, EACCES
,
182 "%s not owned by the user (%s) or root",
191 static krb5_error_code
192 check_owner_file(krb5_context context
,
193 const char *filename
,
194 FILE *file
, const char *owner
)
198 * XXX Implement this!
200 * The thing to do is to call _get_osfhandle() on fileno(file) and
201 * dirfd(dir) to get HANDLEs to the same, then call
202 * GetSecurityInfo() on those HANDLEs to get the security descriptor
203 * (SD), then check the owner and DACL. Checking the DACL sounds
204 * like a lot of work (what, derive a mode from the ACL the way
205 * NFSv4 servers do?). Checking the owner means doing an LSARPC
206 * lookup at least (to get the user's SID).
211 krb5_set_error_message(context
, EACCES
,
212 "User k5login files not supported on Windows");
215 struct passwd pw
, *pwd
= NULL
;
222 if (rk_getpwnam_r(owner
, &pw
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0) {
223 krb5_set_error_message(context
, errno
,
224 "User unknown %s (getpwnam_r())", owner
);
228 krb5_set_error_message(context
, EACCES
, "no user %s", owner
);
232 if (fstat(fileno(file
), &st
) == -1) {
233 krb5_set_error_message(context
, EACCES
, "fstat(%s) of k5login failed",
237 if (S_ISDIR(st
.st_mode
)) {
238 krb5_set_error_message(context
, EISDIR
, "k5login: %s is a directory",
242 if ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) != 0) {
243 krb5_set_error_message(context
, EISDIR
,
244 "k5login %s has world and/or group write "
245 "permissions", filename
);
248 if (pwd
->pw_uid
!= st
.st_uid
&& st
.st_uid
!= 0) {
249 krb5_set_error_message(context
, EACCES
,
250 "k5login %s not owned by the user or root",
260 /* see if principal is mentioned in the filename access file, return
261 TRUE (in result) if so, FALSE otherwise */
263 static krb5_error_code
264 check_one_file(krb5_context context
,
265 const char *filename
,
267 krb5_boolean is_system_location
,
268 krb5_const_principal principal
,
269 krb5_boolean
*result
)
277 f
= fopen(filename
, "r");
282 ret
= check_owner_file(context
, filename
, f
, owner
);
286 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
288 char *newline
= buf
+ strcspn(buf
, "\n");
290 if (*newline
!= '\n') {
294 while (c
!= EOF
&& c
!= '\n')
296 /* line was too long, so ignore it */
301 ret
= krb5_parse_name(context
, buf
, &tmp
);
304 *result
= krb5_principal_compare(context
, principal
, tmp
);
305 krb5_free_principal(context
, tmp
);
317 static krb5_error_code
318 check_directory(krb5_context context
,
321 krb5_boolean is_system_location
,
322 krb5_const_principal principal
,
323 krb5_boolean
*result
)
327 char filename
[MAXPATHLEN
];
329 krb5_error_code ret
= 0;
334 if (lstat(dirname
, &st
) < 0)
337 if (!S_ISDIR(st
.st_mode
)) {
338 krb5_set_error_message(context
, ENOTDIR
, "k5login.d not a directory");
342 if ((d
= opendir(dirname
)) == NULL
) {
343 krb5_set_error_message(context
, ENOTDIR
, "Could not open k5login.d");
347 ret
= check_owner_dir(context
, dirname
, is_system_location
, d
, &st
, owner
);
351 while ((dent
= readdir(d
)) != NULL
) {
353 * XXX: Should we also skip files whose names start with "."?
354 * Vim ".filename.swp" files are also good candidates to skip.
355 * Once we ignore "#*" and "*~", it is not clear what other
356 * heuristics to apply.
358 if (strcmp(dent
->d_name
, ".") == 0 ||
359 strcmp(dent
->d_name
, "..") == 0 ||
360 dent
->d_name
[0] == '#' || /* emacs autosave */
361 dent
->d_name
[strlen(dent
->d_name
) - 1] == '~') /* emacs backup */
363 len
= snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, dent
->d_name
);
364 /* Skip too-long filenames that got truncated by snprintf() */
365 if (len
< sizeof(filename
)) {
366 ret
= check_one_file(context
, filename
, owner
, is_system_location
,
368 if (ret
== 0 && *result
== TRUE
)
371 ret
= 0; /* don't propagate errors upstream */
379 static krb5_error_code
380 check_an2ln(krb5_context context
,
381 krb5_const_principal principal
,
383 krb5_boolean
*result
)
389 /* XXX Should we make this an option? */
390 /* multi-component principals can never match */
391 if (krb5_principal_get_comp_string(context
, principal
, 1) != NULL
) {
397 lname
= malloc(strlen(luser
) + 1);
399 return krb5_enomem(context
);
400 ret
= krb5_aname_to_localname(context
, principal
, strlen(luser
)+1, lname
);
403 if (strcmp(lname
, luser
) == 0)
415 * This function takes the name of a local user and checks if
416 * principal is allowed to log in as that user.
418 * The user may have a ~/.k5login file listing principals that are
419 * allowed to login as that user. If that file does not exist, all
420 * principals with a only one component that is identical to the
421 * username, and a realm considered local, are allowed access.
423 * The .k5login file must contain one principal per line, be owned by
424 * user and not be writable by group or other (but must be readable by
427 * Note that if the file exists, no implicit access rights are given
428 * to user@@LOCALREALM.
430 * Optionally, a set of files may be put in ~/.k5login.d (a
431 * directory), in which case they will all be checked in the same
432 * manner as .k5login. The files may be called anything, but files
433 * starting with a hash (#) , or ending with a tilde (~) are
434 * ignored. Subdirectories are not traversed. Note that this directory
435 * may not be checked by other Kerberos implementations.
437 * If no configuration file exists, match user against local domains,
438 * ie luser@@LOCAL-REALMS-IN-CONFIGURATION-FILES.
440 * @param context Kerberos 5 context.
441 * @param principal principal to check if allowed to login
442 * @param luser local user id
444 * @return returns TRUE if access should be granted, FALSE otherwise.
446 * @ingroup krb5_support
449 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
450 krb5_kuserok(krb5_context context
,
451 krb5_principal principal
,
454 return _krb5_kuserok(context
, principal
, luser
, TRUE
);
458 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
459 _krb5_kuserok(krb5_context context
,
460 krb5_principal principal
,
462 krb5_boolean an2ln_ok
)
464 static heim_base_once_t reg_def_plugins
= HEIM_BASE_ONCE_INIT
;
470 * XXX we should have a struct with a krb5_context field and a
471 * krb5_error_code fied and pass the address of that as the ctx
472 * argument of heim_base_once_f(). For now we use a static to
473 * communicate failures. Actually, we ignore failures anyways,
474 * since we can't return them.
476 heim_base_once_f(®_def_plugins
, context
, reg_def_plugins_once
);
480 ctx
.principal
= principal
;
483 ctx
.k5login_dir
= krb5_config_get_string(context
, NULL
, "libdefaults",
484 "k5login_directory", NULL
);
487 ctx
.flags
|= KUSEROK_ANAME_TO_LNAME_OK
;
489 if (krb5_config_get_bool_default(context
, NULL
, FALSE
, "libdefaults",
490 "k5login_authoritative", NULL
))
491 ctx
.flags
|= KUSEROK_K5LOGIN_IS_AUTHORITATIVE
;
493 if ((ctx
.flags
& KUSEROK_K5LOGIN_IS_AUTHORITATIVE
) && plugin_reg_ret
)
494 return plugin_reg_ret
; /* fail safe */
496 rules
= krb5_config_get_strings(context
, NULL
, "libdefaults",
499 /* Default: check ~/.k5login */
500 ctx
.rule
= "USER-K5LOGIN";
502 ret
= plcallback(context
, &kuserok_user_k5login_plug
, NULL
, &ctx
);
507 ret
= plcallback(context
, &kuserok_simple_plug
, NULL
, &ctx
);
515 for (n
= 0; rules
[n
]; n
++) {
518 ret
= _krb5_plugin_run_f(context
, "krb5", KRB5_PLUGIN_KUSEROK
,
519 KRB5_PLUGIN_KUSEROK_VERSION_0
, 0,
521 if (ret
!= KRB5_PLUGIN_NO_HANDLE
)
527 krb5_config_free_strings(rules
);
533 * Simple kuserok: check that the lname for the aname matches luser.
536 static krb5_error_code KRB5_LIB_CALL
537 kuserok_simple_plug_f(void *plug_ctx
, krb5_context context
, const char *rule
,
538 unsigned int flags
, const char *k5login_dir
,
539 const char *luser
, krb5_const_principal principal
,
540 krb5_boolean
*result
)
544 if (strcmp(rule
, "SIMPLE") != 0 || (flags
& KUSEROK_ANAME_TO_LNAME_OK
) == 0)
545 return KRB5_PLUGIN_NO_HANDLE
;
547 ret
= check_an2ln(context
, principal
, luser
, result
);
548 if (ret
== 0 && *result
== FALSE
)
549 return KRB5_PLUGIN_NO_HANDLE
;
555 * Check k5login files in a system location, rather than in home
559 static krb5_error_code KRB5_LIB_CALL
560 kuserok_sys_k5login_plug_f(void *plug_ctx
, krb5_context context
,
561 const char *rule
, unsigned int flags
,
562 const char *k5login_dir
, const char *luser
,
563 krb5_const_principal principal
, krb5_boolean
*result
)
565 char filename
[MAXPATHLEN
];
567 const char *profile_dir
= NULL
;
572 if (strcmp(rule
, "SYSTEM-K5LOGIN") != 0 &&
573 strncmp(rule
, "SYSTEM-K5LOGIN:", strlen("SYSTEM-K5LOGIN:")) != 0)
574 return KRB5_PLUGIN_NO_HANDLE
;
576 profile_dir
= strchr(rule
, ':');
577 if (profile_dir
== NULL
)
578 profile_dir
= k5login_dir
? k5login_dir
: SYSTEM_K5LOGIN_DIR
;
582 len
= snprintf(filename
, sizeof(filename
), "%s/%s", profile_dir
, luser
);
583 if (len
< sizeof(filename
)) {
584 ret
= check_one_file(context
, filename
, NULL
, TRUE
, principal
, result
);
587 ((flags
& KUSEROK_K5LOGIN_IS_AUTHORITATIVE
) || *result
== TRUE
))
592 return KRB5_PLUGIN_NO_HANDLE
;
596 * Check ~luser/.k5login and/or ~/luser/.k5login.d
599 static krb5_error_code KRB5_LIB_CALL
600 kuserok_user_k5login_plug_f(void *plug_ctx
, krb5_context context
,
601 const char *rule
, unsigned int flags
,
602 const char *k5login_dir
, const char *luser
,
603 krb5_const_principal principal
,
604 krb5_boolean
*result
)
607 return KRB5_PLUGIN_NO_HANDLE
;
611 const char *profile_dir
= NULL
;
613 krb5_boolean found_file
= FALSE
;
614 struct passwd pw
, *pwd
= NULL
;
617 if (strcmp(rule
, "USER-K5LOGIN") != 0)
618 return KRB5_PLUGIN_NO_HANDLE
;
620 profile_dir
= k5login_dir
;
621 if (profile_dir
== NULL
) {
622 /* Don't deadlock with gssd or anything of the sort */
623 if (!_krb5_homedir_access(context
))
624 return KRB5_PLUGIN_NO_HANDLE
;
626 if (getpwnam_r(luser
, &pw
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0) {
627 krb5_set_error_message(context
, errno
, "User unknown (getpwnam_r())");
628 return KRB5_PLUGIN_NO_HANDLE
;
631 krb5_set_error_message(context
, errno
, "User unknown (getpwnam())");
632 return KRB5_PLUGIN_NO_HANDLE
;
634 profile_dir
= pwd
->pw_dir
;
637 #define KLOGIN "/.k5login"
639 if (asprintf(&path
, "%s/.k5login.d", profile_dir
) == -1)
640 return krb5_enomem(context
);
642 ret
= _krb5_expand_path_tokensv(context
, path
, 1, &path_exp
,
643 "luser", luser
, NULL
);
649 /* check user's ~/.k5login */
650 path
[strlen(path
) - strlen(".d")] = '\0';
651 ret
= check_one_file(context
, path
, luser
, FALSE
, principal
, result
);
654 * A match in ~/.k5login is sufficient. A non-match, falls through to the
655 * .k5login.d code below.
657 if (ret
== 0 && *result
== TRUE
) {
665 * A match in ~/.k5login.d/somefile is sufficient. A non-match, falls
666 * through to the code below that handles negative results.
668 * XXX: put back the .d; clever|hackish? you decide
670 path
[strlen(path
)] = '.';
671 ret
= check_directory(context
, path
, luser
, FALSE
, principal
, result
);
673 if (ret
== 0 && *result
== TRUE
)
675 if (ret
!= ENOENT
&& ret
!= ENOTDIR
)
679 * When either ~/.k5login or ~/.k5login.d/ exists, but neither matches
680 * and we're authoritative, we're done. Otherwise, give other plugins
684 if (found_file
&& (flags
& KUSEROK_K5LOGIN_IS_AUTHORITATIVE
))
686 return KRB5_PLUGIN_NO_HANDLE
;
690 static krb5_error_code KRB5_LIB_CALL
691 kuserok_deny_plug_f(void *plug_ctx
, krb5_context context
, const char *rule
,
692 unsigned int flags
, const char *k5login_dir
,
693 const char *luser
, krb5_const_principal principal
,
694 krb5_boolean
*result
)
696 if (strcmp(rule
, "DENY") != 0)
697 return KRB5_PLUGIN_NO_HANDLE
;
703 static krb5_error_code KRB5_LIB_CALL
704 kuser_ok_null_plugin_init(krb5_context context
, void **ctx
)
710 static void KRB5_LIB_CALL
711 kuser_ok_null_plugin_fini(void *ctx
)
716 static krb5plugin_kuserok_ftable kuserok_simple_plug
= {
717 KRB5_PLUGIN_KUSEROK_VERSION_0
,
718 kuser_ok_null_plugin_init
,
719 kuser_ok_null_plugin_fini
,
720 kuserok_simple_plug_f
,
723 static krb5plugin_kuserok_ftable kuserok_sys_k5login_plug
= {
724 KRB5_PLUGIN_KUSEROK_VERSION_0
,
725 kuser_ok_null_plugin_init
,
726 kuser_ok_null_plugin_fini
,
727 kuserok_sys_k5login_plug_f
,
730 static krb5plugin_kuserok_ftable kuserok_user_k5login_plug
= {
731 KRB5_PLUGIN_KUSEROK_VERSION_0
,
732 kuser_ok_null_plugin_init
,
733 kuser_ok_null_plugin_fini
,
734 kuserok_user_k5login_plug_f
,
737 static krb5plugin_kuserok_ftable kuserok_deny_plug
= {
738 KRB5_PLUGIN_KUSEROK_VERSION_0
,
739 kuser_ok_null_plugin_init
,
740 kuser_ok_null_plugin_fini
,