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("$Heimdal: acl.c 22119 2007-12-03 22:02:48Z lha $"
41 enum { acl_string
, acl_fnmatch
, acl_retval
} type
;
46 struct acl_field
*next
, **last
;
50 free_retv(struct acl_field
*acl
)
53 if (acl
->type
== acl_retval
) {
63 acl_free_list(struct acl_field
*acl
, int retv
)
65 struct acl_field
*next
;
75 static krb5_error_code
76 acl_parse_format(krb5_context context
,
77 struct acl_field
**acl_ret
,
82 struct acl_field
*acl
= NULL
, *tmp
;
84 for(p
= format
; *p
!= '\0'; p
++) {
85 tmp
= malloc(sizeof(*tmp
));
87 krb5_set_error_string(context
, "malloc: out of memory");
88 acl_free_list(acl
, 0);
92 tmp
->type
= acl_string
;
93 tmp
->u
.cstr
= va_arg(ap
, const char*);
94 } else if(*p
== 'f') {
95 tmp
->type
= acl_fnmatch
;
96 tmp
->u
.cstr
= va_arg(ap
, const char*);
97 } else if(*p
== 'r') {
98 tmp
->type
= acl_retval
;
99 tmp
->u
.retv
= va_arg(ap
, char **);
102 krb5_set_error_string(context
, "acl_parse_format: "
103 "unknown format specifier %c", *p
);
104 acl_free_list(acl
, 0);
113 acl
->last
= &tmp
->next
;
120 acl_match_field(krb5_context context
,
122 struct acl_field
*field
)
124 if(field
->type
== acl_string
) {
125 return !strcmp(field
->u
.cstr
, string
);
126 } else if(field
->type
== acl_fnmatch
) {
127 return !fnmatch(field
->u
.cstr
, string
, 0);
128 } else if(field
->type
== acl_retval
) {
129 *field
->u
.retv
= strdup(string
);
136 acl_match_acl(krb5_context context
,
137 struct acl_field
*acl
,
141 while(strsep_copy(&string
, " \t", buf
, sizeof(buf
)) != -1) {
143 continue; /* skip ws */
146 if(!acl_match_field(context
, buf
, acl
)) {
157 * krb5_acl_match_string matches ACL format against a string.
159 * The ACL format has three format specifiers: s, f, and r. Each
160 * specifier will retrieve one argument from the variable arguments
161 * for either matching or storing data. The input string is split up
162 * using " " (space) and "\t" (tab) as a delimiter; multiple and "\t"
163 * in a row are considered to be the same.
165 * List of format specifiers:
166 * - s Matches a string using strcmp(3) (case sensitive).
167 * - f Matches the string with fnmatch(3). Theflags
168 * argument (the last argument) passed to the fnmatch function is 0.
169 * - r Returns a copy of the string in the char ** passed in; the copy
170 * must be freed with free(3). There is no need to free(3) the
171 * string on error: the function will clean up and set the pointer
174 * @param context Kerberos 5 context
175 * @param string string to match with
176 * @param format format to match
177 * @param ... parameter to format string
179 * @return Return an error code or 0.
185 * ret = krb5_acl_match_string(context, "foo", "s", "foo");
187 * krb5_errx(context, 1, "acl didn't match");
188 * ret = krb5_acl_match_string(context, "foo foo baz/kaka",
189 * "ss", "foo", &s, "foo/\\*");
191 * // no need to free(s) on error
193 * krb5_errx(context, 1, "acl didn't match");
198 * @sa krb5_acl_match_file
199 * @ingroup krb5_support
202 krb5_error_code KRB5_LIB_FUNCTION
203 krb5_acl_match_string(krb5_context context
,
210 struct acl_field
*acl
;
213 va_start(ap
, format
);
214 ret
= acl_parse_format(context
, &acl
, format
, ap
);
219 found
= acl_match_acl(context
, acl
, string
);
220 acl_free_list(acl
, !found
);
224 krb5_set_error_string(context
, "ACL did not match");
230 * krb5_acl_match_file matches ACL format against each line in a file
231 * using krb5_acl_match_string(). Lines starting with # are treated
232 * like comments and ignored.
234 * @param context Kerberos 5 context.
235 * @param file file with acl listed in the file.
236 * @param format format to match.
237 * @param ... parameter to format string.
239 * @return Return an error code or 0.
241 * @sa krb5_acl_match_string
242 * @ingroup krb5_support
245 krb5_error_code KRB5_LIB_FUNCTION
246 krb5_acl_match_file(krb5_context context
,
252 struct acl_field
*acl
;
258 f
= fopen(file
, "r");
260 int save_errno
= errno
;
262 krb5_set_error_string(context
, "open(%s): %s", file
,
263 strerror(save_errno
));
267 va_start(ap
, format
);
268 ret
= acl_parse_format(context
, &acl
, format
, ap
);
276 while(fgets(buf
, sizeof(buf
), f
)) {
279 if(acl_match_acl(context
, acl
, buf
)) {
287 acl_free_list(acl
, !found
);
291 krb5_set_error_string(context
, "ACL did not match");