No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / kuserok.c
blob599ac8ebb63bbb000b4379588357e869060bf4ce
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 __RCSID("$Heimdal: kuserok.c 16048 2005-09-09 10:33:33Z lha $"
38 "$NetBSD$");
40 /* see if principal is mentioned in the filename access file, return
41 TRUE (in result) if so, FALSE otherwise */
43 static krb5_error_code
44 check_one_file(krb5_context context,
45 const char *filename,
46 struct passwd *pwd,
47 krb5_principal principal,
48 krb5_boolean *result)
50 FILE *f;
51 char buf[BUFSIZ];
52 krb5_error_code ret;
53 struct stat st;
55 *result = FALSE;
57 f = fopen (filename, "r");
58 if (f == NULL)
59 return errno;
61 /* check type and mode of file */
62 if (fstat(fileno(f), &st) != 0) {
63 fclose (f);
64 return errno;
66 if (S_ISDIR(st.st_mode)) {
67 fclose (f);
68 return EISDIR;
70 if (st.st_uid != pwd->pw_uid && st.st_uid != 0) {
71 fclose (f);
72 return EACCES;
74 if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
75 fclose (f);
76 return EACCES;
79 while (fgets (buf, sizeof(buf), f) != NULL) {
80 krb5_principal tmp;
81 char *newline = buf + strcspn(buf, "\n");
83 if(*newline != '\n') {
84 int c;
85 c = fgetc(f);
86 if(c != EOF) {
87 while(c != EOF && c != '\n')
88 c = fgetc(f);
89 /* line was too long, so ignore it */
90 continue;
93 *newline = '\0';
94 ret = krb5_parse_name (context, buf, &tmp);
95 if (ret)
96 continue;
97 *result = krb5_principal_compare (context, principal, tmp);
98 krb5_free_principal (context, tmp);
99 if (*result) {
100 fclose (f);
101 return 0;
104 fclose (f);
105 return 0;
108 static krb5_error_code
109 check_directory(krb5_context context,
110 const char *dirname,
111 struct passwd *pwd,
112 krb5_principal principal,
113 krb5_boolean *result)
115 DIR *d;
116 struct dirent *dent;
117 char filename[MAXPATHLEN];
118 krb5_error_code ret = 0;
119 struct stat st;
121 *result = FALSE;
123 if(lstat(dirname, &st) < 0)
124 return errno;
126 if (!S_ISDIR(st.st_mode))
127 return ENOTDIR;
129 if (st.st_uid != pwd->pw_uid && st.st_uid != 0)
130 return EACCES;
131 if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0)
132 return EACCES;
134 if((d = opendir(dirname)) == NULL)
135 return errno;
137 #ifdef HAVE_DIRFD
139 int fd;
140 struct stat st2;
142 fd = dirfd(d);
143 if(fstat(fd, &st2) < 0) {
144 closedir(d);
145 return errno;
147 if(st.st_dev != st2.st_dev || st.st_ino != st2.st_ino) {
148 closedir(d);
149 return EACCES;
152 #endif
154 while((dent = readdir(d)) != NULL) {
155 if(strcmp(dent->d_name, ".") == 0 ||
156 strcmp(dent->d_name, "..") == 0 ||
157 dent->d_name[0] == '#' || /* emacs autosave */
158 dent->d_name[strlen(dent->d_name) - 1] == '~') /* emacs backup */
159 continue;
160 snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
161 ret = check_one_file(context, filename, pwd, principal, result);
162 if(ret == 0 && *result == TRUE)
163 break;
164 ret = 0; /* don't propagate errors upstream */
166 closedir(d);
167 return ret;
170 static krb5_boolean
171 match_local_principals(krb5_context context,
172 krb5_principal principal,
173 const char *luser)
175 krb5_error_code ret;
176 krb5_realm *realms, *r;
177 krb5_boolean result = FALSE;
179 /* multi-component principals can never match */
180 if(krb5_principal_get_comp_string(context, principal, 1) != NULL)
181 return FALSE;
183 ret = krb5_get_default_realms (context, &realms);
184 if (ret)
185 return FALSE;
187 for (r = realms; *r != NULL; ++r) {
188 if(strcmp(krb5_principal_get_realm(context, principal),
189 *r) != 0)
190 continue;
191 if(strcmp(krb5_principal_get_comp_string(context, principal, 0),
192 luser) == 0) {
193 result = TRUE;
194 break;
197 krb5_free_host_realm (context, realms);
198 return result;
202 * Return TRUE iff `principal' is allowed to login as `luser'.
205 krb5_boolean KRB5_LIB_FUNCTION
206 krb5_kuserok (krb5_context context,
207 krb5_principal principal,
208 const char *luser)
210 char *buf;
211 size_t buflen;
212 struct passwd *pwd;
213 krb5_error_code ret;
214 krb5_boolean result = FALSE;
216 krb5_boolean found_file = FALSE;
218 #ifdef POSIX_GETPWNAM_R
219 char pwbuf[2048];
220 struct passwd pw;
222 if(getpwnam_r(luser, &pw, pwbuf, sizeof(pwbuf), &pwd) != 0)
223 return FALSE;
224 #else
225 pwd = getpwnam (luser);
226 #endif
227 if (pwd == NULL)
228 return FALSE;
230 #define KLOGIN "/.k5login"
231 buflen = strlen(pwd->pw_dir) + sizeof(KLOGIN) + 2; /* 2 for .d */
232 buf = malloc(buflen);
233 if(buf == NULL)
234 return FALSE;
235 /* check user's ~/.k5login */
236 strlcpy(buf, pwd->pw_dir, buflen);
237 strlcat(buf, KLOGIN, buflen);
238 ret = check_one_file(context, buf, pwd, principal, &result);
240 if(ret == 0 && result == TRUE) {
241 free(buf);
242 return TRUE;
245 if(ret != ENOENT)
246 found_file = TRUE;
248 strlcat(buf, ".d", buflen);
249 ret = check_directory(context, buf, pwd, principal, &result);
250 free(buf);
251 if(ret == 0 && result == TRUE)
252 return TRUE;
254 if(ret != ENOENT && ret != ENOTDIR)
255 found_file = TRUE;
257 /* finally if no files exist, allow all principals matching
258 <localuser>@<LOCALREALM> */
259 if(found_file == FALSE)
260 return match_local_principals(context, principal, luser);
262 return FALSE;