Make roken build on windows
[heimdal.git] / lib / krb5 / kuserok.c
blobccfe28130c9a247f84938c31489c94d3bce00f7e
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 #include <dirent.h>
37 /* see if principal is mentioned in the filename access file, return
38 TRUE (in result) if so, FALSE otherwise */
40 static krb5_error_code
41 check_one_file(krb5_context context,
42 const char *filename,
43 struct passwd *pwd,
44 krb5_principal principal,
45 krb5_boolean *result)
47 FILE *f;
48 char buf[BUFSIZ];
49 krb5_error_code ret;
50 struct stat st;
52 *result = FALSE;
54 f = fopen (filename, "r");
55 if (f == NULL)
56 return errno;
57 rk_cloexec_file(f);
59 /* check type and mode of file */
60 if (fstat(fileno(f), &st) != 0) {
61 fclose (f);
62 return errno;
64 if (S_ISDIR(st.st_mode)) {
65 fclose (f);
66 return EISDIR;
68 if (st.st_uid != pwd->pw_uid && st.st_uid != 0) {
69 fclose (f);
70 return EACCES;
72 if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
73 fclose (f);
74 return EACCES;
77 while (fgets (buf, sizeof(buf), f) != NULL) {
78 krb5_principal tmp;
79 char *newline = buf + strcspn(buf, "\n");
81 if(*newline != '\n') {
82 int c;
83 c = fgetc(f);
84 if(c != EOF) {
85 while(c != EOF && c != '\n')
86 c = fgetc(f);
87 /* line was too long, so ignore it */
88 continue;
91 *newline = '\0';
92 ret = krb5_parse_name (context, buf, &tmp);
93 if (ret)
94 continue;
95 *result = krb5_principal_compare (context, principal, tmp);
96 krb5_free_principal (context, tmp);
97 if (*result) {
98 fclose (f);
99 return 0;
102 fclose (f);
103 return 0;
106 static krb5_error_code
107 check_directory(krb5_context context,
108 const char *dirname,
109 struct passwd *pwd,
110 krb5_principal principal,
111 krb5_boolean *result)
113 DIR *d;
114 struct dirent *dent;
115 char filename[MAXPATHLEN];
116 krb5_error_code ret = 0;
117 struct stat st;
119 *result = FALSE;
121 if(lstat(dirname, &st) < 0)
122 return errno;
124 if (!S_ISDIR(st.st_mode))
125 return ENOTDIR;
127 if (st.st_uid != pwd->pw_uid && st.st_uid != 0)
128 return EACCES;
129 if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0)
130 return EACCES;
132 if((d = opendir(dirname)) == NULL)
133 return errno;
136 int fd;
137 struct stat st2;
139 fd = dirfd(d);
140 if(fstat(fd, &st2) < 0) {
141 closedir(d);
142 return errno;
144 if(st.st_dev != st2.st_dev || st.st_ino != st2.st_ino) {
145 closedir(d);
146 return EACCES;
150 while((dent = readdir(d)) != NULL) {
151 if(strcmp(dent->d_name, ".") == 0 ||
152 strcmp(dent->d_name, "..") == 0 ||
153 dent->d_name[0] == '#' || /* emacs autosave */
154 dent->d_name[strlen(dent->d_name) - 1] == '~') /* emacs backup */
155 continue;
156 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
157 ret = check_one_file(context, filename, pwd, principal, result);
158 if(ret == 0 && *result == TRUE)
159 break;
160 ret = 0; /* don't propagate errors upstream */
162 closedir(d);
163 return ret;
166 static krb5_boolean
167 match_local_principals(krb5_context context,
168 krb5_principal principal,
169 const char *luser)
171 krb5_error_code ret;
172 krb5_realm *realms, *r;
173 krb5_boolean result = FALSE;
175 /* multi-component principals can never match */
176 if(krb5_principal_get_comp_string(context, principal, 1) != NULL)
177 return FALSE;
179 ret = krb5_get_default_realms (context, &realms);
180 if (ret)
181 return FALSE;
183 for (r = realms; *r != NULL; ++r) {
184 if(strcmp(krb5_principal_get_realm(context, principal),
185 *r) != 0)
186 continue;
187 if(strcmp(krb5_principal_get_comp_string(context, principal, 0),
188 luser) == 0) {
189 result = TRUE;
190 break;
193 krb5_free_host_realm (context, realms);
194 return result;
198 * This function takes the name of a local user and checks if
199 * principal is allowed to log in as that user.
201 * The user may have a ~/.k5login file listing principals that are
202 * allowed to login as that user. If that file does not exist, all
203 * principals with a first component identical to the username, and a
204 * realm considered local, are allowed access.
206 * The .k5login file must contain one principal per line, be owned by
207 * user and not be writable by group or other (but must be readable by
208 * anyone).
210 * Note that if the file exists, no implicit access rights are given
211 * to user@@LOCALREALM.
213 * Optionally, a set of files may be put in ~/.k5login.d (a
214 * directory), in which case they will all be checked in the same
215 * manner as .k5login. The files may be called anything, but files
216 * starting with a hash (#) , or ending with a tilde (~) are
217 * ignored. Subdirectories are not traversed. Note that this directory
218 * may not be checked by other Kerberos implementations.
220 * @param context Kerberos 5 context.
221 * @param principal principal to check if allowed to login
222 * @param luser local user id
224 * @return returns TRUE if access should be granted, FALSE otherwise.
226 * @ingroup krb5_support
229 krb5_boolean KRB5_LIB_FUNCTION
230 krb5_kuserok (krb5_context context,
231 krb5_principal principal,
232 const char *luser)
234 char *buf;
235 size_t buflen;
236 struct passwd *pwd;
237 krb5_error_code ret;
238 krb5_boolean result = FALSE;
240 krb5_boolean found_file = FALSE;
242 #ifdef POSIX_GETPWNAM_R
243 char pwbuf[2048];
244 struct passwd pw;
246 if(getpwnam_r(luser, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0)
247 return FALSE;
248 #else
249 pwd = getpwnam (luser);
250 #endif
251 if (pwd == NULL)
252 return FALSE;
254 #define KLOGIN "/.k5login"
255 buflen = strlen(pwd->pw_dir) + sizeof(KLOGIN) + 2; /* 2 for .d */
256 buf = malloc(buflen);
257 if(buf == NULL)
258 return FALSE;
259 /* check user's ~/.k5login */
260 strlcpy(buf, pwd->pw_dir, buflen);
261 strlcat(buf, KLOGIN, buflen);
262 ret = check_one_file(context, buf, pwd, principal, &result);
264 if(ret == 0 && result == TRUE) {
265 free(buf);
266 return TRUE;
269 if(ret != ENOENT)
270 found_file = TRUE;
272 strlcat(buf, ".d", buflen);
273 ret = check_directory(context, buf, pwd, principal, &result);
274 free(buf);
275 if(ret == 0 && result == TRUE)
276 return TRUE;
278 if(ret != ENOENT && ret != ENOTDIR)
279 found_file = TRUE;
281 /* finally if no files exist, allow all principals matching
282 <localuser>@<LOCALREALM> */
283 if(found_file == FALSE)
284 return match_local_principals(context, principal, luser);
286 return FALSE;