xref: /freebsd/crypto/heimdal/lib/krb5/acl.c (revision 8373020d34ceb1ac55d8f43333c1ca3680185b39)
15e9cd1aeSAssar Westerlund /*
28373020dSJacques Vidrine  * Copyright (c) 2000 - 2002 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 
378373020dSJacques Vidrine RCSID("$Id: acl.c,v 1.3 2002/04/18 16:16:24 joda Exp $");
385e9cd1aeSAssar Westerlund 
395e9cd1aeSAssar Westerlund struct acl_field {
405e9cd1aeSAssar Westerlund     enum { acl_string, acl_fnmatch, acl_retval } type;
415e9cd1aeSAssar Westerlund     union {
425e9cd1aeSAssar Westerlund 	const char *cstr;
435e9cd1aeSAssar Westerlund 	char **retv;
445e9cd1aeSAssar Westerlund     } u;
455e9cd1aeSAssar Westerlund     struct acl_field *next, **last;
465e9cd1aeSAssar Westerlund };
475e9cd1aeSAssar Westerlund 
485e9cd1aeSAssar Westerlund static void
495e9cd1aeSAssar Westerlund acl_free_list(struct acl_field *acl)
505e9cd1aeSAssar Westerlund {
515e9cd1aeSAssar Westerlund     struct acl_field *next;
525e9cd1aeSAssar Westerlund     while(acl != NULL) {
535e9cd1aeSAssar Westerlund 	next = acl->next;
545e9cd1aeSAssar Westerlund 	free(acl);
555e9cd1aeSAssar Westerlund 	acl = next;
565e9cd1aeSAssar Westerlund     }
575e9cd1aeSAssar Westerlund }
585e9cd1aeSAssar Westerlund 
595e9cd1aeSAssar Westerlund static krb5_error_code
605e9cd1aeSAssar Westerlund acl_parse_format(krb5_context context,
615e9cd1aeSAssar Westerlund 		 struct acl_field **acl_ret,
625e9cd1aeSAssar Westerlund 		 const char *format,
635e9cd1aeSAssar Westerlund 		 va_list ap)
645e9cd1aeSAssar Westerlund {
655e9cd1aeSAssar Westerlund     const char *p;
665e9cd1aeSAssar Westerlund     struct acl_field *acl = NULL, *tmp;
675e9cd1aeSAssar Westerlund 
685e9cd1aeSAssar Westerlund     for(p = format; *p != '\0'; p++) {
695e9cd1aeSAssar Westerlund 	tmp = malloc(sizeof(*tmp));
705e9cd1aeSAssar Westerlund 	if(tmp == NULL) {
71adb0ddaeSAssar Westerlund 	    krb5_set_error_string(context, "malloc: out of memory");
725e9cd1aeSAssar Westerlund 	    acl_free_list(acl);
735e9cd1aeSAssar Westerlund 	    return ENOMEM;
745e9cd1aeSAssar Westerlund 	}
755e9cd1aeSAssar Westerlund 	if(*p == 's') {
765e9cd1aeSAssar Westerlund 	    tmp->type = acl_string;
775e9cd1aeSAssar Westerlund 	    tmp->u.cstr = va_arg(ap, const char*);
785e9cd1aeSAssar Westerlund 	} else if(*p == 'f') {
795e9cd1aeSAssar Westerlund 	    tmp->type = acl_fnmatch;
805e9cd1aeSAssar Westerlund 	    tmp->u.cstr = va_arg(ap, const char*);
815e9cd1aeSAssar Westerlund 	} else if(*p == 'r') {
825e9cd1aeSAssar Westerlund 	    tmp->type = acl_retval;
835e9cd1aeSAssar Westerlund 	    tmp->u.retv = va_arg(ap, char **);
845e9cd1aeSAssar Westerlund 	}
855e9cd1aeSAssar Westerlund 	tmp->next = NULL;
865e9cd1aeSAssar Westerlund 	if(acl == NULL)
875e9cd1aeSAssar Westerlund 	    acl = tmp;
885e9cd1aeSAssar Westerlund 	else
895e9cd1aeSAssar Westerlund 	    *acl->last = tmp;
905e9cd1aeSAssar Westerlund 	acl->last = &tmp->next;
915e9cd1aeSAssar Westerlund     }
925e9cd1aeSAssar Westerlund     *acl_ret = acl;
935e9cd1aeSAssar Westerlund     return 0;
945e9cd1aeSAssar Westerlund }
955e9cd1aeSAssar Westerlund 
965e9cd1aeSAssar Westerlund static krb5_boolean
975e9cd1aeSAssar Westerlund acl_match_field(krb5_context context,
985e9cd1aeSAssar Westerlund 		const char *string,
995e9cd1aeSAssar Westerlund 		struct acl_field *field)
1005e9cd1aeSAssar Westerlund {
1015e9cd1aeSAssar Westerlund     if(field->type == acl_string) {
1025e9cd1aeSAssar Westerlund 	return !strcmp(string, field->u.cstr);
1035e9cd1aeSAssar Westerlund     } else if(field->type == acl_fnmatch) {
1045e9cd1aeSAssar Westerlund 	return !fnmatch(string, field->u.cstr, 0);
1055e9cd1aeSAssar Westerlund     } else if(field->type == acl_retval) {
1065e9cd1aeSAssar Westerlund 	*field->u.retv = strdup(string);
1075e9cd1aeSAssar Westerlund 	return TRUE;
1085e9cd1aeSAssar Westerlund     }
1095e9cd1aeSAssar Westerlund     return FALSE;
1105e9cd1aeSAssar Westerlund }
1115e9cd1aeSAssar Westerlund 
1125e9cd1aeSAssar Westerlund static krb5_boolean
1135e9cd1aeSAssar Westerlund acl_match_acl(krb5_context context,
1145e9cd1aeSAssar Westerlund 	      struct acl_field *acl,
1155e9cd1aeSAssar Westerlund 	      const char *string)
1165e9cd1aeSAssar Westerlund {
1175e9cd1aeSAssar Westerlund     char buf[256];
1185e9cd1aeSAssar Westerlund     for(;strsep_copy(&string, " \t", buf, sizeof(buf)) != -1;
1195e9cd1aeSAssar Westerlund 	acl = acl->next) {
1205e9cd1aeSAssar Westerlund 	if(buf[0] == '\0')
1215e9cd1aeSAssar Westerlund 	    continue; /* skip ws */
1225e9cd1aeSAssar Westerlund 	if(!acl_match_field(context, buf, acl)) {
1235e9cd1aeSAssar Westerlund 	    return FALSE;
1245e9cd1aeSAssar Westerlund 	}
1255e9cd1aeSAssar Westerlund     }
1265e9cd1aeSAssar Westerlund     return TRUE;
1275e9cd1aeSAssar Westerlund }
1285e9cd1aeSAssar Westerlund 
1295e9cd1aeSAssar Westerlund 
1305e9cd1aeSAssar Westerlund krb5_error_code
1315e9cd1aeSAssar Westerlund krb5_acl_match_string(krb5_context context,
1328373020dSJacques Vidrine 		      const char *string,
1335e9cd1aeSAssar Westerlund 		      const char *format,
1345e9cd1aeSAssar Westerlund 		      ...)
1355e9cd1aeSAssar Westerlund {
1365e9cd1aeSAssar Westerlund     krb5_error_code ret;
137adb0ddaeSAssar Westerlund     krb5_boolean found;
1385e9cd1aeSAssar Westerlund     struct acl_field *acl;
1395e9cd1aeSAssar Westerlund 
1405e9cd1aeSAssar Westerlund     va_list ap;
1415e9cd1aeSAssar Westerlund     va_start(ap, format);
1425e9cd1aeSAssar Westerlund     ret = acl_parse_format(context, &acl, format, ap);
1435e9cd1aeSAssar Westerlund     va_end(ap);
1445e9cd1aeSAssar Westerlund     if(ret)
1455e9cd1aeSAssar Westerlund 	return ret;
1465e9cd1aeSAssar Westerlund 
1478373020dSJacques Vidrine     found = acl_match_acl(context, acl, string);
1485e9cd1aeSAssar Westerlund     acl_free_list(acl);
149adb0ddaeSAssar Westerlund     if (found) {
150adb0ddaeSAssar Westerlund 	return 0;
151adb0ddaeSAssar Westerlund     } else {
152adb0ddaeSAssar Westerlund 	krb5_set_error_string(context, "ACL did not match");
153adb0ddaeSAssar Westerlund 	return EACCES;
154adb0ddaeSAssar Westerlund     }
1555e9cd1aeSAssar Westerlund }
1565e9cd1aeSAssar Westerlund 
1575e9cd1aeSAssar Westerlund krb5_error_code
1585e9cd1aeSAssar Westerlund krb5_acl_match_file(krb5_context context,
1595e9cd1aeSAssar Westerlund 		    const char *file,
1605e9cd1aeSAssar Westerlund 		    const char *format,
1615e9cd1aeSAssar Westerlund 		    ...)
1625e9cd1aeSAssar Westerlund {
1635e9cd1aeSAssar Westerlund     krb5_error_code ret;
1645e9cd1aeSAssar Westerlund     struct acl_field *acl;
1655e9cd1aeSAssar Westerlund     char buf[256];
1665e9cd1aeSAssar Westerlund     va_list ap;
1675e9cd1aeSAssar Westerlund     FILE *f;
168adb0ddaeSAssar Westerlund     krb5_boolean found;
1695e9cd1aeSAssar Westerlund 
1705e9cd1aeSAssar Westerlund     f = fopen(file, "r");
171adb0ddaeSAssar Westerlund     if(f == NULL) {
172adb0ddaeSAssar Westerlund 	int save_errno = errno;
173adb0ddaeSAssar Westerlund 
174adb0ddaeSAssar Westerlund 	krb5_set_error_string(context, "open(%s): %s", file,
175adb0ddaeSAssar Westerlund 			      strerror(save_errno));
176adb0ddaeSAssar Westerlund 	return save_errno;
177adb0ddaeSAssar Westerlund     }
1785e9cd1aeSAssar Westerlund 
1795e9cd1aeSAssar Westerlund     va_start(ap, format);
1805e9cd1aeSAssar Westerlund     ret = acl_parse_format(context, &acl, format, ap);
1815e9cd1aeSAssar Westerlund     va_end(ap);
1825e9cd1aeSAssar Westerlund     if(ret) {
1835e9cd1aeSAssar Westerlund 	fclose(f);
1845e9cd1aeSAssar Westerlund 	return ret;
1855e9cd1aeSAssar Westerlund     }
1865e9cd1aeSAssar Westerlund 
187adb0ddaeSAssar Westerlund     found = FALSE;
1885e9cd1aeSAssar Westerlund     while(fgets(buf, sizeof(buf), f)) {
1895e9cd1aeSAssar Westerlund 	if(buf[0] == '#')
1905e9cd1aeSAssar Westerlund 	    continue;
1915e9cd1aeSAssar Westerlund 	if(acl_match_acl(context, acl, buf)) {
192adb0ddaeSAssar Westerlund 	    found = TRUE;
193adb0ddaeSAssar Westerlund 	    break;
1945e9cd1aeSAssar Westerlund 	}
1955e9cd1aeSAssar Westerlund     }
1965e9cd1aeSAssar Westerlund 
1975e9cd1aeSAssar Westerlund     fclose(f);
1985e9cd1aeSAssar Westerlund     acl_free_list(acl);
199adb0ddaeSAssar Westerlund     if (found) {
200adb0ddaeSAssar Westerlund 	return 0;
201adb0ddaeSAssar Westerlund     } else {
202adb0ddaeSAssar Westerlund 	krb5_set_error_string(context, "ACL did not match");
203adb0ddaeSAssar Westerlund 	return EACCES;
204adb0ddaeSAssar Westerlund     }
2055e9cd1aeSAssar Westerlund }
206