2 * Copyright (c) 1997 - 2001 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 "kadm5_locl.h"
38 static struct units acl_units[] = {
39 { "all", KADM5_PRIV_ALL },
40 { "change-password",KADM5_PRIV_CPW },
41 { "cpw", KADM5_PRIV_CPW },
42 { "list", KADM5_PRIV_LIST },
43 { "delete", KADM5_PRIV_DELETE },
44 { "modify", KADM5_PRIV_MODIFY },
45 { "add", KADM5_PRIV_ADD },
46 { "get", KADM5_PRIV_GET },
47 { "get-keys", KADM5_PRIV_GET_KEYS },
52 _kadm5_string_to_privs(const char *s, uint32_t* privs)
55 flags = parse_flags(s, acl_units, 0);
63 _kadm5_privs_to_string(uint32_t privs, char *string, size_t len)
66 strlcpy(string, "none", len);
68 unparse_flags(privs, acl_units + 1, string, len);
73 * retrieve the right for the current caller on `princ' (NULL means all)
74 * and store them in `ret_flags'
75 * return 0 or an error.
79 fetch_acl (kadm5_server_context *context,
80 krb5_const_principal princ,
84 krb5_error_code ret = 0;
89 /* no acl file -> no rights */
90 f = fopen(context->config.acl_file, "r");
94 while(fgets(buf, sizeof(buf), f) != NULL) {
96 krb5_principal this_princ;
99 p = strtok_r(buf, " \t\n", &foo);
102 if (*p == '#') /* comment */
104 ret = krb5_parse_name(context->context, p, &this_princ);
107 if(!krb5_principal_compare(context->context,
108 context->caller, this_princ)) {
109 krb5_free_principal(context->context, this_princ);
112 krb5_free_principal(context->context, this_princ);
113 p = strtok_r(NULL, " \t\n", &foo);
116 ret = _kadm5_string_to_privs(p, &flags);
119 p = strtok_r(NULL, " \t\n", &foo);
125 krb5_principal pattern_princ;
127 const char *c0 = krb5_principal_get_comp_string(context->context,
131 ret = krb5_parse_name(context->context, p, &pattern_princ);
134 pat_c0 = krb5_principal_get_comp_string(context->context,
136 match = krb5_principal_match(context->context,
137 princ, pattern_princ);
140 * If `princ' is a WELLKNOWN name, then require the WELLKNOWN label
141 * be matched exactly.
143 * FIXME: We could do something similar for krbtgt and kadmin other
146 if (match && c0 && strcmp(c0, "WELLKNOWN") == 0 &&
147 (!pat_c0 || strcmp(pat_c0, "WELLKNOWN") != 0))
149 krb5_free_principal(context->context, pattern_princ);
161 _kadm5_is_kadmin_service_p(kadm5_server_context *context)
164 krb5_principal princ;
166 if (krb5_parse_name(context->context, KADM5_ADMIN_SERVICE, &princ) != 0)
169 ret = krb5_principal_compare(context->context, context->caller, princ);
170 krb5_free_principal(context->context, princ);
176 * set global acl flags in `context' for the current caller.
177 * return 0 on success or an error
181 _kadm5_acl_init(kadm5_server_context *context)
183 if (_kadm5_is_kadmin_service_p(context)) {
184 context->acl_flags = KADM5_PRIV_ALL;
188 return fetch_acl (context, NULL, &context->acl_flags);
192 * check if `flags' allows `op'
193 * return 0 if OK or an error
197 check_flags (unsigned op,
200 unsigned res = ~flags & op;
202 if(res & KADM5_PRIV_GET)
203 return KADM5_AUTH_GET;
204 if(res & KADM5_PRIV_GET_KEYS)
205 return KADM5_AUTH_GET_KEYS;
206 if(res & KADM5_PRIV_ADD)
207 return KADM5_AUTH_ADD;
208 if(res & KADM5_PRIV_MODIFY)
209 return KADM5_AUTH_MODIFY;
210 if(res & KADM5_PRIV_DELETE)
211 return KADM5_AUTH_DELETE;
212 if(res & KADM5_PRIV_CPW)
213 return KADM5_AUTH_CHANGEPW;
214 if(res & KADM5_PRIV_LIST)
215 return KADM5_AUTH_LIST;
217 return KADM5_AUTH_INSUFFICIENT;
222 * return 0 if the current caller in `context' is allowed to perform
223 * `op' on `princ' and otherwise an error
224 * princ == NULL if it's not relevant.
228 _kadm5_acl_check_permission(kadm5_server_context *context,
230 krb5_const_principal princ)
233 unsigned princ_flags;
235 ret = check_flags (op, context->acl_flags);
238 ret = fetch_acl (context, princ, &princ_flags);
241 return check_flags (op, princ_flags);