15e9cd1aeSAssar Westerlund /*
2ae771770SStanislav Sedov * Copyright (c) 2000 - 2002, 2004 Kungliga Tekniska Högskolan
35e9cd1aeSAssar Westerlund * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund * All rights reserved.
55e9cd1aeSAssar Westerlund *
65e9cd1aeSAssar Westerlund * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund * are met:
95e9cd1aeSAssar Westerlund *
105e9cd1aeSAssar Westerlund * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund * notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund *
135e9cd1aeSAssar Westerlund * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund * notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund * documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund *
175e9cd1aeSAssar Westerlund * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund * may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund * without specific prior written permission.
205e9cd1aeSAssar Westerlund *
215e9cd1aeSAssar Westerlund * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund */
335e9cd1aeSAssar Westerlund
345e9cd1aeSAssar Westerlund #include "krb5_locl.h"
355e9cd1aeSAssar Westerlund #include <fnmatch.h>
365e9cd1aeSAssar Westerlund
375e9cd1aeSAssar Westerlund struct acl_field {
385e9cd1aeSAssar Westerlund enum { acl_string, acl_fnmatch, acl_retval } type;
395e9cd1aeSAssar Westerlund union {
405e9cd1aeSAssar Westerlund const char *cstr;
415e9cd1aeSAssar Westerlund char **retv;
425e9cd1aeSAssar Westerlund } u;
435e9cd1aeSAssar Westerlund struct acl_field *next, **last;
445e9cd1aeSAssar Westerlund };
455e9cd1aeSAssar Westerlund
465e9cd1aeSAssar Westerlund static void
free_retv(struct acl_field * acl)47c19800e8SDoug Rabson free_retv(struct acl_field *acl)
48c19800e8SDoug Rabson {
49c19800e8SDoug Rabson while(acl != NULL) {
50c19800e8SDoug Rabson if (acl->type == acl_retval) {
51c19800e8SDoug Rabson if (*acl->u.retv)
52c19800e8SDoug Rabson free(*acl->u.retv);
53c19800e8SDoug Rabson *acl->u.retv = NULL;
54c19800e8SDoug Rabson }
55c19800e8SDoug Rabson acl = acl->next;
56c19800e8SDoug Rabson }
57c19800e8SDoug Rabson }
58c19800e8SDoug Rabson
59c19800e8SDoug Rabson static void
acl_free_list(struct acl_field * acl,int retv)60c19800e8SDoug Rabson acl_free_list(struct acl_field *acl, int retv)
615e9cd1aeSAssar Westerlund {
625e9cd1aeSAssar Westerlund struct acl_field *next;
63c19800e8SDoug Rabson if (retv)
64c19800e8SDoug Rabson free_retv(acl);
655e9cd1aeSAssar Westerlund while(acl != NULL) {
665e9cd1aeSAssar Westerlund next = acl->next;
675e9cd1aeSAssar Westerlund free(acl);
685e9cd1aeSAssar Westerlund acl = next;
695e9cd1aeSAssar Westerlund }
705e9cd1aeSAssar Westerlund }
715e9cd1aeSAssar Westerlund
725e9cd1aeSAssar Westerlund static krb5_error_code
acl_parse_format(krb5_context context,struct acl_field ** acl_ret,const char * format,va_list ap)735e9cd1aeSAssar Westerlund acl_parse_format(krb5_context context,
745e9cd1aeSAssar Westerlund struct acl_field **acl_ret,
755e9cd1aeSAssar Westerlund const char *format,
765e9cd1aeSAssar Westerlund va_list ap)
775e9cd1aeSAssar Westerlund {
785e9cd1aeSAssar Westerlund const char *p;
795e9cd1aeSAssar Westerlund struct acl_field *acl = NULL, *tmp;
805e9cd1aeSAssar Westerlund
815e9cd1aeSAssar Westerlund for(p = format; *p != '\0'; p++) {
825e9cd1aeSAssar Westerlund tmp = malloc(sizeof(*tmp));
835e9cd1aeSAssar Westerlund if(tmp == NULL) {
84ae771770SStanislav Sedov krb5_set_error_message(context, ENOMEM,
85ae771770SStanislav Sedov N_("malloc: out of memory", ""));
86c19800e8SDoug Rabson acl_free_list(acl, 0);
875e9cd1aeSAssar Westerlund return ENOMEM;
885e9cd1aeSAssar Westerlund }
895e9cd1aeSAssar Westerlund if(*p == 's') {
905e9cd1aeSAssar Westerlund tmp->type = acl_string;
915e9cd1aeSAssar Westerlund tmp->u.cstr = va_arg(ap, const char*);
925e9cd1aeSAssar Westerlund } else if(*p == 'f') {
935e9cd1aeSAssar Westerlund tmp->type = acl_fnmatch;
945e9cd1aeSAssar Westerlund tmp->u.cstr = va_arg(ap, const char*);
955e9cd1aeSAssar Westerlund } else if(*p == 'r') {
965e9cd1aeSAssar Westerlund tmp->type = acl_retval;
975e9cd1aeSAssar Westerlund tmp->u.retv = va_arg(ap, char **);
98c19800e8SDoug Rabson *tmp->u.retv = NULL;
99c19800e8SDoug Rabson } else {
100ae771770SStanislav Sedov krb5_set_error_message(context, EINVAL,
101ae771770SStanislav Sedov N_("Unknown format specifier %c while "
102ae771770SStanislav Sedov "parsing ACL", "specifier"), *p);
103c19800e8SDoug Rabson acl_free_list(acl, 0);
104c19800e8SDoug Rabson free(tmp);
105c19800e8SDoug Rabson return EINVAL;
1065e9cd1aeSAssar Westerlund }
1075e9cd1aeSAssar Westerlund tmp->next = NULL;
1085e9cd1aeSAssar Westerlund if(acl == NULL)
1095e9cd1aeSAssar Westerlund acl = tmp;
1105e9cd1aeSAssar Westerlund else
1115e9cd1aeSAssar Westerlund *acl->last = tmp;
1125e9cd1aeSAssar Westerlund acl->last = &tmp->next;
1135e9cd1aeSAssar Westerlund }
1145e9cd1aeSAssar Westerlund *acl_ret = acl;
1155e9cd1aeSAssar Westerlund return 0;
1165e9cd1aeSAssar Westerlund }
1175e9cd1aeSAssar Westerlund
1185e9cd1aeSAssar Westerlund static krb5_boolean
acl_match_field(krb5_context context,const char * string,struct acl_field * field)1195e9cd1aeSAssar Westerlund acl_match_field(krb5_context context,
1205e9cd1aeSAssar Westerlund const char *string,
1215e9cd1aeSAssar Westerlund struct acl_field *field)
1225e9cd1aeSAssar Westerlund {
1235e9cd1aeSAssar Westerlund if(field->type == acl_string) {
124c19800e8SDoug Rabson return !strcmp(field->u.cstr, string);
1255e9cd1aeSAssar Westerlund } else if(field->type == acl_fnmatch) {
126c19800e8SDoug Rabson return !fnmatch(field->u.cstr, string, 0);
1275e9cd1aeSAssar Westerlund } else if(field->type == acl_retval) {
1285e9cd1aeSAssar Westerlund *field->u.retv = strdup(string);
1295e9cd1aeSAssar Westerlund return TRUE;
1305e9cd1aeSAssar Westerlund }
1315e9cd1aeSAssar Westerlund return FALSE;
1325e9cd1aeSAssar Westerlund }
1335e9cd1aeSAssar Westerlund
1345e9cd1aeSAssar Westerlund static krb5_boolean
acl_match_acl(krb5_context context,struct acl_field * acl,const char * string)1355e9cd1aeSAssar Westerlund acl_match_acl(krb5_context context,
1365e9cd1aeSAssar Westerlund struct acl_field *acl,
1375e9cd1aeSAssar Westerlund const char *string)
1385e9cd1aeSAssar Westerlund {
1395e9cd1aeSAssar Westerlund char buf[256];
140c19800e8SDoug Rabson while(strsep_copy(&string, " \t", buf, sizeof(buf)) != -1) {
1415e9cd1aeSAssar Westerlund if(buf[0] == '\0')
1425e9cd1aeSAssar Westerlund continue; /* skip ws */
143c19800e8SDoug Rabson if (acl == NULL)
144c19800e8SDoug Rabson return FALSE;
1455e9cd1aeSAssar Westerlund if(!acl_match_field(context, buf, acl)) {
1465e9cd1aeSAssar Westerlund return FALSE;
1475e9cd1aeSAssar Westerlund }
148c19800e8SDoug Rabson acl = acl->next;
1495e9cd1aeSAssar Westerlund }
150c19800e8SDoug Rabson if (acl)
151c19800e8SDoug Rabson return FALSE;
1525e9cd1aeSAssar Westerlund return TRUE;
1535e9cd1aeSAssar Westerlund }
1545e9cd1aeSAssar Westerlund
155c19800e8SDoug Rabson /**
156c19800e8SDoug Rabson * krb5_acl_match_string matches ACL format against a string.
157c19800e8SDoug Rabson *
158c19800e8SDoug Rabson * The ACL format has three format specifiers: s, f, and r. Each
159c19800e8SDoug Rabson * specifier will retrieve one argument from the variable arguments
160c19800e8SDoug Rabson * for either matching or storing data. The input string is split up
161c19800e8SDoug Rabson * using " " (space) and "\t" (tab) as a delimiter; multiple and "\t"
162c19800e8SDoug Rabson * in a row are considered to be the same.
163c19800e8SDoug Rabson *
164c19800e8SDoug Rabson * List of format specifiers:
165c19800e8SDoug Rabson * - s Matches a string using strcmp(3) (case sensitive).
166c19800e8SDoug Rabson * - f Matches the string with fnmatch(3). Theflags
167c19800e8SDoug Rabson * argument (the last argument) passed to the fnmatch function is 0.
168c19800e8SDoug Rabson * - r Returns a copy of the string in the char ** passed in; the copy
169c19800e8SDoug Rabson * must be freed with free(3). There is no need to free(3) the
170c19800e8SDoug Rabson * string on error: the function will clean up and set the pointer
171c19800e8SDoug Rabson * to NULL.
172c19800e8SDoug Rabson *
173c19800e8SDoug Rabson * @param context Kerberos 5 context
174c19800e8SDoug Rabson * @param string string to match with
175c19800e8SDoug Rabson * @param format format to match
176c19800e8SDoug Rabson * @param ... parameter to format string
177c19800e8SDoug Rabson *
178c19800e8SDoug Rabson * @return Return an error code or 0.
179c19800e8SDoug Rabson *
180c19800e8SDoug Rabson *
181c19800e8SDoug Rabson * @code
182c19800e8SDoug Rabson * char *s;
183c19800e8SDoug Rabson *
184c19800e8SDoug Rabson * ret = krb5_acl_match_string(context, "foo", "s", "foo");
185c19800e8SDoug Rabson * if (ret)
186c19800e8SDoug Rabson * krb5_errx(context, 1, "acl didn't match");
187c19800e8SDoug Rabson * ret = krb5_acl_match_string(context, "foo foo baz/kaka",
188c19800e8SDoug Rabson * "ss", "foo", &s, "foo/\\*");
189c19800e8SDoug Rabson * if (ret) {
190c19800e8SDoug Rabson * // no need to free(s) on error
191c19800e8SDoug Rabson * assert(s == NULL);
192c19800e8SDoug Rabson * krb5_errx(context, 1, "acl didn't match");
193c19800e8SDoug Rabson * }
194c19800e8SDoug Rabson * free(s);
195c19800e8SDoug Rabson * @endcode
196c19800e8SDoug Rabson *
197c19800e8SDoug Rabson * @sa krb5_acl_match_file
198c19800e8SDoug Rabson * @ingroup krb5_support
199c19800e8SDoug Rabson */
2005e9cd1aeSAssar Westerlund
201ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_acl_match_string(krb5_context context,const char * string,const char * format,...)2025e9cd1aeSAssar Westerlund krb5_acl_match_string(krb5_context context,
2038373020dSJacques Vidrine const char *string,
2045e9cd1aeSAssar Westerlund const char *format,
2055e9cd1aeSAssar Westerlund ...)
2065e9cd1aeSAssar Westerlund {
2075e9cd1aeSAssar Westerlund krb5_error_code ret;
208adb0ddaeSAssar Westerlund krb5_boolean found;
2095e9cd1aeSAssar Westerlund struct acl_field *acl;
2105e9cd1aeSAssar Westerlund
2115e9cd1aeSAssar Westerlund va_list ap;
2125e9cd1aeSAssar Westerlund va_start(ap, format);
2135e9cd1aeSAssar Westerlund ret = acl_parse_format(context, &acl, format, ap);
2145e9cd1aeSAssar Westerlund va_end(ap);
2155e9cd1aeSAssar Westerlund if(ret)
2165e9cd1aeSAssar Westerlund return ret;
2175e9cd1aeSAssar Westerlund
2188373020dSJacques Vidrine found = acl_match_acl(context, acl, string);
219c19800e8SDoug Rabson acl_free_list(acl, !found);
220adb0ddaeSAssar Westerlund if (found) {
221adb0ddaeSAssar Westerlund return 0;
222adb0ddaeSAssar Westerlund } else {
223ae771770SStanislav Sedov krb5_set_error_message(context, EACCES, N_("ACL did not match", ""));
224adb0ddaeSAssar Westerlund return EACCES;
225adb0ddaeSAssar Westerlund }
2265e9cd1aeSAssar Westerlund }
2275e9cd1aeSAssar Westerlund
228c19800e8SDoug Rabson /**
229c19800e8SDoug Rabson * krb5_acl_match_file matches ACL format against each line in a file
230c19800e8SDoug Rabson * using krb5_acl_match_string(). Lines starting with # are treated
231c19800e8SDoug Rabson * like comments and ignored.
232c19800e8SDoug Rabson *
233c19800e8SDoug Rabson * @param context Kerberos 5 context.
234c19800e8SDoug Rabson * @param file file with acl listed in the file.
235c19800e8SDoug Rabson * @param format format to match.
236c19800e8SDoug Rabson * @param ... parameter to format string.
237c19800e8SDoug Rabson *
238c19800e8SDoug Rabson * @return Return an error code or 0.
239c19800e8SDoug Rabson *
240c19800e8SDoug Rabson * @sa krb5_acl_match_string
241c19800e8SDoug Rabson * @ingroup krb5_support
242c19800e8SDoug Rabson */
243c19800e8SDoug Rabson
244ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_acl_match_file(krb5_context context,const char * file,const char * format,...)2455e9cd1aeSAssar Westerlund krb5_acl_match_file(krb5_context context,
2465e9cd1aeSAssar Westerlund const char *file,
2475e9cd1aeSAssar Westerlund const char *format,
2485e9cd1aeSAssar Westerlund ...)
2495e9cd1aeSAssar Westerlund {
2505e9cd1aeSAssar Westerlund krb5_error_code ret;
251*ed549cb0SCy Schubert struct acl_field *acl = NULL;
2525e9cd1aeSAssar Westerlund char buf[256];
2535e9cd1aeSAssar Westerlund va_list ap;
2545e9cd1aeSAssar Westerlund FILE *f;
255adb0ddaeSAssar Westerlund krb5_boolean found;
2565e9cd1aeSAssar Westerlund
2575e9cd1aeSAssar Westerlund f = fopen(file, "r");
258adb0ddaeSAssar Westerlund if(f == NULL) {
259adb0ddaeSAssar Westerlund int save_errno = errno;
260ae771770SStanislav Sedov rk_strerror_r(save_errno, buf, sizeof(buf));
261ae771770SStanislav Sedov krb5_set_error_message(context, save_errno,
262ae771770SStanislav Sedov N_("open(%s): %s", "file, errno"),
263ae771770SStanislav Sedov file, buf);
264adb0ddaeSAssar Westerlund return save_errno;
265adb0ddaeSAssar Westerlund }
266ae771770SStanislav Sedov rk_cloexec_file(f);
2675e9cd1aeSAssar Westerlund
2685e9cd1aeSAssar Westerlund va_start(ap, format);
2695e9cd1aeSAssar Westerlund ret = acl_parse_format(context, &acl, format, ap);
2705e9cd1aeSAssar Westerlund va_end(ap);
2715e9cd1aeSAssar Westerlund if(ret) {
2725e9cd1aeSAssar Westerlund fclose(f);
2735e9cd1aeSAssar Westerlund return ret;
2745e9cd1aeSAssar Westerlund }
2755e9cd1aeSAssar Westerlund
276adb0ddaeSAssar Westerlund found = FALSE;
2775e9cd1aeSAssar Westerlund while(fgets(buf, sizeof(buf), f)) {
2785e9cd1aeSAssar Westerlund if(buf[0] == '#')
2795e9cd1aeSAssar Westerlund continue;
2805e9cd1aeSAssar Westerlund if(acl_match_acl(context, acl, buf)) {
281adb0ddaeSAssar Westerlund found = TRUE;
282adb0ddaeSAssar Westerlund break;
2835e9cd1aeSAssar Westerlund }
284c19800e8SDoug Rabson free_retv(acl);
2855e9cd1aeSAssar Westerlund }
2865e9cd1aeSAssar Westerlund
2875e9cd1aeSAssar Westerlund fclose(f);
288c19800e8SDoug Rabson acl_free_list(acl, !found);
289adb0ddaeSAssar Westerlund if (found) {
290adb0ddaeSAssar Westerlund return 0;
291adb0ddaeSAssar Westerlund } else {
292ae771770SStanislav Sedov krb5_set_error_message(context, EACCES, N_("ACL did not match", ""));
293adb0ddaeSAssar Westerlund return EACCES;
294adb0ddaeSAssar Westerlund }
2955e9cd1aeSAssar Westerlund }
296