2 * Copyright (c) 2000 - 2002, 2004 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: acl.c 22119 2007-12-03 22:02:48Z lha $");
40 enum { acl_string, acl_fnmatch, acl_retval } type;
45 struct acl_field *next, **last;
49 free_retv(struct acl_field *acl)
52 if (acl->type == acl_retval) {
62 acl_free_list(struct acl_field *acl, int retv)
64 struct acl_field *next;
74 static krb5_error_code
75 acl_parse_format(krb5_context context,
76 struct acl_field **acl_ret,
81 struct acl_field *acl = NULL, *tmp;
83 for(p = format; *p != '\0'; p++) {
84 tmp = malloc(sizeof(*tmp));
86 krb5_set_error_string(context, "malloc: out of memory");
87 acl_free_list(acl, 0);
91 tmp->type = acl_string;
92 tmp->u.cstr = va_arg(ap, const char*);
93 } else if(*p == 'f') {
94 tmp->type = acl_fnmatch;
95 tmp->u.cstr = va_arg(ap, const char*);
96 } else if(*p == 'r') {
97 tmp->type = acl_retval;
98 tmp->u.retv = va_arg(ap, char **);
101 krb5_set_error_string(context, "acl_parse_format: "
102 "unknown format specifier %c", *p);
103 acl_free_list(acl, 0);
112 acl->last = &tmp->next;
119 acl_match_field(krb5_context context,
121 struct acl_field *field)
123 if(field->type == acl_string) {
124 return !strcmp(field->u.cstr, string);
125 } else if(field->type == acl_fnmatch) {
126 return !fnmatch(field->u.cstr, string, 0);
127 } else if(field->type == acl_retval) {
128 *field->u.retv = strdup(string);
135 acl_match_acl(krb5_context context,
136 struct acl_field *acl,
140 while(strsep_copy(&string, " \t", buf, sizeof(buf)) != -1) {
142 continue; /* skip ws */
145 if(!acl_match_field(context, buf, acl)) {
156 * krb5_acl_match_string matches ACL format against a string.
158 * The ACL format has three format specifiers: s, f, and r. Each
159 * specifier will retrieve one argument from the variable arguments
160 * for either matching or storing data. The input string is split up
161 * using " " (space) and "\t" (tab) as a delimiter; multiple and "\t"
162 * in a row are considered to be the same.
164 * List of format specifiers:
165 * - s Matches a string using strcmp(3) (case sensitive).
166 * - f Matches the string with fnmatch(3). Theflags
167 * argument (the last argument) passed to the fnmatch function is 0.
168 * - r Returns a copy of the string in the char ** passed in; the copy
169 * must be freed with free(3). There is no need to free(3) the
170 * string on error: the function will clean up and set the pointer
173 * @param context Kerberos 5 context
174 * @param string string to match with
175 * @param format format to match
176 * @param ... parameter to format string
178 * @return Return an error code or 0.
184 * ret = krb5_acl_match_string(context, "foo", "s", "foo");
186 * krb5_errx(context, 1, "acl didn't match");
187 * ret = krb5_acl_match_string(context, "foo foo baz/kaka",
188 * "ss", "foo", &s, "foo/\\*");
190 * // no need to free(s) on error
192 * krb5_errx(context, 1, "acl didn't match");
197 * @sa krb5_acl_match_file
198 * @ingroup krb5_support
201 krb5_error_code KRB5_LIB_FUNCTION
202 krb5_acl_match_string(krb5_context context,
209 struct acl_field *acl;
212 va_start(ap, format);
213 ret = acl_parse_format(context, &acl, format, ap);
218 found = acl_match_acl(context, acl, string);
219 acl_free_list(acl, !found);
223 krb5_set_error_string(context, "ACL did not match");
229 * krb5_acl_match_file matches ACL format against each line in a file
230 * using krb5_acl_match_string(). Lines starting with # are treated
231 * like comments and ignored.
233 * @param context Kerberos 5 context.
234 * @param file file with acl listed in the file.
235 * @param format format to match.
236 * @param ... parameter to format string.
238 * @return Return an error code or 0.
240 * @sa krb5_acl_match_string
241 * @ingroup krb5_support
244 krb5_error_code KRB5_LIB_FUNCTION
245 krb5_acl_match_file(krb5_context context,
251 struct acl_field *acl;
257 f = fopen(file, "r");
259 int save_errno = errno;
261 krb5_set_error_string(context, "open(%s): %s", file,
262 strerror(save_errno));
266 va_start(ap, format);
267 ret = acl_parse_format(context, &acl, format, ap);
275 while(fgets(buf, sizeof(buf), f)) {
278 if(acl_match_acl(context, acl, buf)) {
286 acl_free_list(acl, !found);
290 krb5_set_error_string(context, "ACL did not match");