2 * Copyright (c) 2000 - 2002 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,v 1.3 2002/04/18 16:16:24 joda Exp $");
40 enum { acl_string
, acl_fnmatch
, acl_retval
} type
;
45 struct acl_field
*next
, **last
;
49 acl_free_list(struct acl_field
*acl
)
51 struct acl_field
*next
;
59 static krb5_error_code
60 acl_parse_format(krb5_context context
,
61 struct acl_field
**acl_ret
,
66 struct acl_field
*acl
= NULL
, *tmp
;
68 for(p
= format
; *p
!= '\0'; p
++) {
69 tmp
= malloc(sizeof(*tmp
));
71 krb5_set_error_string(context
, "malloc: out of memory");
76 tmp
->type
= acl_string
;
77 tmp
->u
.cstr
= va_arg(ap
, const char*);
78 } else if(*p
== 'f') {
79 tmp
->type
= acl_fnmatch
;
80 tmp
->u
.cstr
= va_arg(ap
, const char*);
81 } else if(*p
== 'r') {
82 tmp
->type
= acl_retval
;
83 tmp
->u
.retv
= va_arg(ap
, char **);
90 acl
->last
= &tmp
->next
;
97 acl_match_field(krb5_context context
,
99 struct acl_field
*field
)
101 if(field
->type
== acl_string
) {
102 return !strcmp(string
, field
->u
.cstr
);
103 } else if(field
->type
== acl_fnmatch
) {
104 return !fnmatch(string
, field
->u
.cstr
, 0);
105 } else if(field
->type
== acl_retval
) {
106 *field
->u
.retv
= strdup(string
);
113 acl_match_acl(krb5_context context
,
114 struct acl_field
*acl
,
118 for(;strsep_copy(&string
, " \t", buf
, sizeof(buf
)) != -1;
121 continue; /* skip ws */
122 if(!acl_match_field(context
, buf
, acl
)) {
131 krb5_acl_match_string(krb5_context context
,
138 struct acl_field
*acl
;
141 va_start(ap
, format
);
142 ret
= acl_parse_format(context
, &acl
, format
, ap
);
147 found
= acl_match_acl(context
, acl
, string
);
152 krb5_set_error_string(context
, "ACL did not match");
158 krb5_acl_match_file(krb5_context context
,
164 struct acl_field
*acl
;
170 f
= fopen(file
, "r");
172 int save_errno
= errno
;
174 krb5_set_error_string(context
, "open(%s): %s", file
,
175 strerror(save_errno
));
179 va_start(ap
, format
);
180 ret
= acl_parse_format(context
, &acl
, format
, ap
);
188 while(fgets(buf
, sizeof(buf
), f
)) {
191 if(acl_match_acl(context
, acl
, buf
)) {
202 krb5_set_error_string(context
, "ACL did not match");