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"
37 RCSID("$Id: kuserok.c 16048 2005-09-09 10:33:33Z lha $");
39 /* see if principal is mentioned in the filename access file, return
40 TRUE (in result) if so, FALSE otherwise */
42 static krb5_error_code
43 check_one_file(krb5_context context,
46 krb5_principal principal,
56 f = fopen (filename, "r");
60 /* check type and mode of file */
61 if (fstat(fileno(f), &st) != 0) {
65 if (S_ISDIR(st.st_mode)) {
69 if (st.st_uid != pwd->pw_uid && st.st_uid != 0) {
73 if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
78 while (fgets (buf, sizeof(buf), f) != NULL) {
80 char *newline = buf + strcspn(buf, "\n");
82 if(*newline != '\n') {
86 while(c != EOF && c != '\n')
88 /* line was too long, so ignore it */
93 ret = krb5_parse_name (context, buf, &tmp);
96 *result = krb5_principal_compare (context, principal, tmp);
97 krb5_free_principal (context, tmp);
107 static krb5_error_code
108 check_directory(krb5_context context,
111 krb5_principal principal,
112 krb5_boolean *result)
116 char filename[MAXPATHLEN];
117 krb5_error_code ret = 0;
122 if(lstat(dirname, &st) < 0)
125 if (!S_ISDIR(st.st_mode))
128 if (st.st_uid != pwd->pw_uid && st.st_uid != 0)
130 if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0)
133 if((d = opendir(dirname)) == NULL)
142 if(fstat(fd, &st2) < 0) {
146 if(st.st_dev != st2.st_dev || st.st_ino != st2.st_ino) {
153 while((dent = readdir(d)) != NULL) {
154 if(strcmp(dent->d_name, ".") == 0 ||
155 strcmp(dent->d_name, "..") == 0 ||
156 dent->d_name[0] == '#' || /* emacs autosave */
157 dent->d_name[strlen(dent->d_name) - 1] == '~') /* emacs backup */
159 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
160 ret = check_one_file(context, filename, pwd, principal, result);
161 if(ret == 0 && *result == TRUE)
163 ret = 0; /* don't propagate errors upstream */
170 match_local_principals(krb5_context context,
171 krb5_principal principal,
175 krb5_realm *realms, *r;
176 krb5_boolean result = FALSE;
178 /* multi-component principals can never match */
179 if(krb5_principal_get_comp_string(context, principal, 1) != NULL)
182 ret = krb5_get_default_realms (context, &realms);
186 for (r = realms; *r != NULL; ++r) {
187 if(strcmp(krb5_principal_get_realm(context, principal),
190 if(strcmp(krb5_principal_get_comp_string(context, principal, 0),
196 krb5_free_host_realm (context, realms);
201 * Return TRUE iff `principal' is allowed to login as `luser'.
204 krb5_boolean KRB5_LIB_FUNCTION
205 krb5_kuserok (krb5_context context,
206 krb5_principal principal,
213 krb5_boolean result = FALSE;
215 krb5_boolean found_file = FALSE;
217 #ifdef POSIX_GETPWNAM_R
221 if(getpwnam_r(luser, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0)
224 pwd = getpwnam (luser);
229 #define KLOGIN "/.k5login"
230 buflen = strlen(pwd->pw_dir) + sizeof(KLOGIN) + 2; /* 2 for .d */
231 buf = malloc(buflen);
234 /* check user's ~/.k5login */
235 strlcpy(buf, pwd->pw_dir, buflen);
236 strlcat(buf, KLOGIN, buflen);
237 ret = check_one_file(context, buf, pwd, principal, &result);
239 if(ret == 0 && result == TRUE) {
247 strlcat(buf, ".d", buflen);
248 ret = check_directory(context, buf, pwd, principal, &result);
250 if(ret == 0 && result == TRUE)
253 if(ret != ENOENT && ret != ENOTDIR)
256 /* finally if no files exist, allow all principals matching
257 <localuser>@<LOCALREALM> */
258 if(found_file == FALSE)
259 return match_local_principals(context, principal, luser);