xref: /freebsd/crypto/heimdal/lib/krb5/acl.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1 /*
2  * Copyright (c) 2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "krb5_locl.h"
35 #include <fnmatch.h>
36 
37 RCSID("$Id: acl.c,v 1.1 2000/06/12 11:17:52 joda Exp $");
38 
39 struct acl_field {
40     enum { acl_string, acl_fnmatch, acl_retval } type;
41     union {
42 	const char *cstr;
43 	char **retv;
44     } u;
45     struct acl_field *next, **last;
46 };
47 
48 static void
49 acl_free_list(struct acl_field *acl)
50 {
51     struct acl_field *next;
52     while(acl != NULL) {
53 	next = acl->next;
54 	free(acl);
55 	acl = next;
56     }
57 }
58 
59 static krb5_error_code
60 acl_parse_format(krb5_context context,
61 		 struct acl_field **acl_ret,
62 		 const char *format,
63 		 va_list ap)
64 {
65     const char *p;
66     struct acl_field *acl = NULL, *tmp;
67 
68     for(p = format; *p != '\0'; p++) {
69 	tmp = malloc(sizeof(*tmp));
70 	if(tmp == NULL) {
71 	    acl_free_list(acl);
72 	    return ENOMEM;
73 	}
74 	if(*p == 's') {
75 	    tmp->type = acl_string;
76 	    tmp->u.cstr = va_arg(ap, const char*);
77 	} else if(*p == 'f') {
78 	    tmp->type = acl_fnmatch;
79 	    tmp->u.cstr = va_arg(ap, const char*);
80 	} else if(*p == 'r') {
81 	    tmp->type = acl_retval;
82 	    tmp->u.retv = va_arg(ap, char **);
83 	}
84 	tmp->next = NULL;
85 	if(acl == NULL)
86 	    acl = tmp;
87 	else
88 	    *acl->last = tmp;
89 	acl->last = &tmp->next;
90     }
91     *acl_ret = acl;
92     return 0;
93 }
94 
95 static krb5_boolean
96 acl_match_field(krb5_context context,
97 		const char *string,
98 		struct acl_field *field)
99 {
100     if(field->type == acl_string) {
101 	return !strcmp(string, field->u.cstr);
102     } else if(field->type == acl_fnmatch) {
103 	return !fnmatch(string, field->u.cstr, 0);
104     } else if(field->type == acl_retval) {
105 	*field->u.retv = strdup(string);
106 	return TRUE;
107     }
108     return FALSE;
109 }
110 
111 static krb5_boolean
112 acl_match_acl(krb5_context context,
113 	      struct acl_field *acl,
114 	      const char *string)
115 {
116     char buf[256];
117     for(;strsep_copy(&string, " \t", buf, sizeof(buf)) != -1;
118 	acl = acl->next) {
119 	if(buf[0] == '\0')
120 	    continue; /* skip ws */
121 	if(!acl_match_field(context, buf, acl)) {
122 	    return FALSE;
123 	}
124     }
125     return TRUE;
126 }
127 
128 
129 krb5_error_code
130 krb5_acl_match_string(krb5_context context,
131 		      const char *acl_string,
132 		      const char *format,
133 		      ...)
134 {
135     krb5_error_code ret;
136     struct acl_field *acl;
137 
138     va_list ap;
139     va_start(ap, format);
140     ret = acl_parse_format(context, &acl, format, ap);
141     va_end(ap);
142     if(ret)
143 	return ret;
144 
145     ret = acl_match_acl(context, acl, acl_string);
146 
147     acl_free_list(acl);
148     return ret ? 0 : EACCES;
149 }
150 
151 krb5_error_code
152 krb5_acl_match_file(krb5_context context,
153 		    const char *file,
154 		    const char *format,
155 		    ...)
156 {
157     krb5_error_code ret;
158     struct acl_field *acl;
159     char buf[256];
160     va_list ap;
161     FILE *f;
162 
163     f = fopen(file, "r");
164     if(f == NULL)
165 	return errno;
166 
167     va_start(ap, format);
168     ret = acl_parse_format(context, &acl, format, ap);
169     va_end(ap);
170     if(ret) {
171 	fclose(f);
172 	return ret;
173     }
174 
175     ret = EACCES; /* XXX */
176     while(fgets(buf, sizeof(buf), f)) {
177 	if(buf[0] == '#')
178 	    continue;
179 	if(acl_match_acl(context, acl, buf)) {
180 	    ret = 0;
181 	    goto out;
182 	}
183     }
184 
185   out:
186     fclose(f);
187     acl_free_list(acl);
188     return ret;
189 }
190