xref: /minix3/crypto/external/bsd/heimdal/dist/lib/kadm5/acl.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: acl.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadm5_locl.h"
37ebfedea0SLionel Sambuc 
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc static struct units acl_units[] = {
41ebfedea0SLionel Sambuc     { "all",		KADM5_PRIV_ALL },
42ebfedea0SLionel Sambuc     { "change-password",KADM5_PRIV_CPW },
43ebfedea0SLionel Sambuc     { "cpw",		KADM5_PRIV_CPW },
44ebfedea0SLionel Sambuc     { "list",		KADM5_PRIV_LIST },
45ebfedea0SLionel Sambuc     { "delete",		KADM5_PRIV_DELETE },
46ebfedea0SLionel Sambuc     { "modify",		KADM5_PRIV_MODIFY },
47ebfedea0SLionel Sambuc     { "add",		KADM5_PRIV_ADD },
48ebfedea0SLionel Sambuc     { "get", 		KADM5_PRIV_GET },
49*0a6a1f1dSLionel Sambuc     { NULL,		0 }
50ebfedea0SLionel Sambuc };
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_string_to_privs(const char * s,uint32_t * privs)53ebfedea0SLionel Sambuc _kadm5_string_to_privs(const char *s, uint32_t* privs)
54ebfedea0SLionel Sambuc {
55ebfedea0SLionel Sambuc     int flags;
56ebfedea0SLionel Sambuc     flags = parse_flags(s, acl_units, 0);
57ebfedea0SLionel Sambuc     if(flags < 0)
58ebfedea0SLionel Sambuc 	return KADM5_FAILURE;
59ebfedea0SLionel Sambuc     *privs = flags;
60ebfedea0SLionel Sambuc     return 0;
61ebfedea0SLionel Sambuc }
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_privs_to_string(uint32_t privs,char * string,size_t len)64ebfedea0SLionel Sambuc _kadm5_privs_to_string(uint32_t privs, char *string, size_t len)
65ebfedea0SLionel Sambuc {
66ebfedea0SLionel Sambuc     if(privs == 0)
67ebfedea0SLionel Sambuc 	strlcpy(string, "none", len);
68ebfedea0SLionel Sambuc     else
69ebfedea0SLionel Sambuc 	unparse_flags(privs, acl_units + 1, string, len);
70ebfedea0SLionel Sambuc     return 0;
71ebfedea0SLionel Sambuc }
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc /*
74ebfedea0SLionel Sambuc  * retrieve the right for the current caller on `princ' (NULL means all)
75ebfedea0SLionel Sambuc  * and store them in `ret_flags'
76ebfedea0SLionel Sambuc  * return 0 or an error.
77ebfedea0SLionel Sambuc  */
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc static kadm5_ret_t
fetch_acl(kadm5_server_context * context,krb5_const_principal princ,unsigned * ret_flags)80ebfedea0SLionel Sambuc fetch_acl (kadm5_server_context *context,
81ebfedea0SLionel Sambuc 	   krb5_const_principal princ,
82ebfedea0SLionel Sambuc 	   unsigned *ret_flags)
83ebfedea0SLionel Sambuc {
84ebfedea0SLionel Sambuc     FILE *f;
85ebfedea0SLionel Sambuc     krb5_error_code ret = 0;
86ebfedea0SLionel Sambuc     char buf[256];
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc     *ret_flags = 0;
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc     /* no acl file -> no rights */
91ebfedea0SLionel Sambuc     f = fopen(context->config.acl_file, "r");
92ebfedea0SLionel Sambuc     if (f == NULL)
93ebfedea0SLionel Sambuc 	return 0;
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc     while(fgets(buf, sizeof(buf), f) != NULL) {
96ebfedea0SLionel Sambuc 	char *foo = NULL, *p;
97ebfedea0SLionel Sambuc 	krb5_principal this_princ;
98ebfedea0SLionel Sambuc 	unsigned flags = 0;
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc 	p = strtok_r(buf, " \t\n", &foo);
101ebfedea0SLionel Sambuc 	if(p == NULL)
102ebfedea0SLionel Sambuc 	    continue;
103ebfedea0SLionel Sambuc 	if (*p == '#')		/* comment */
104ebfedea0SLionel Sambuc 	    continue;
105ebfedea0SLionel Sambuc 	ret = krb5_parse_name(context->context, p, &this_princ);
106ebfedea0SLionel Sambuc 	if(ret)
107ebfedea0SLionel Sambuc 	    break;
108ebfedea0SLionel Sambuc 	if(!krb5_principal_compare(context->context,
109ebfedea0SLionel Sambuc 				   context->caller, this_princ)) {
110ebfedea0SLionel Sambuc 	    krb5_free_principal(context->context, this_princ);
111ebfedea0SLionel Sambuc 	    continue;
112ebfedea0SLionel Sambuc 	}
113ebfedea0SLionel Sambuc 	krb5_free_principal(context->context, this_princ);
114ebfedea0SLionel Sambuc 	p = strtok_r(NULL, " \t\n", &foo);
115ebfedea0SLionel Sambuc 	if(p == NULL)
116ebfedea0SLionel Sambuc 	    continue;
117ebfedea0SLionel Sambuc 	ret = _kadm5_string_to_privs(p, &flags);
118ebfedea0SLionel Sambuc 	if (ret)
119ebfedea0SLionel Sambuc 	    break;
120ebfedea0SLionel Sambuc 	p = strtok_r(NULL, " \t\n", &foo);
121ebfedea0SLionel Sambuc 	if (p == NULL) {
122ebfedea0SLionel Sambuc 	    *ret_flags = flags;
123ebfedea0SLionel Sambuc 	    break;
124ebfedea0SLionel Sambuc 	}
125ebfedea0SLionel Sambuc 	if (princ != NULL) {
126ebfedea0SLionel Sambuc 	    krb5_principal pattern_princ;
127ebfedea0SLionel Sambuc 	    krb5_boolean match;
128ebfedea0SLionel Sambuc 
129ebfedea0SLionel Sambuc 	    ret = krb5_parse_name (context->context, p, &pattern_princ);
130ebfedea0SLionel Sambuc 	    if (ret)
131ebfedea0SLionel Sambuc 		break;
132ebfedea0SLionel Sambuc 	    match = krb5_principal_match (context->context,
133ebfedea0SLionel Sambuc 					  princ, pattern_princ);
134ebfedea0SLionel Sambuc 	    krb5_free_principal (context->context, pattern_princ);
135ebfedea0SLionel Sambuc 	    if (match) {
136ebfedea0SLionel Sambuc 		*ret_flags = flags;
137ebfedea0SLionel Sambuc 		break;
138ebfedea0SLionel Sambuc 	    }
139ebfedea0SLionel Sambuc 	}
140ebfedea0SLionel Sambuc     }
141ebfedea0SLionel Sambuc     fclose(f);
142ebfedea0SLionel Sambuc     return ret;
143ebfedea0SLionel Sambuc }
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc /*
146ebfedea0SLionel Sambuc  * set global acl flags in `context' for the current caller.
147ebfedea0SLionel Sambuc  * return 0 on success or an error
148ebfedea0SLionel Sambuc  */
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_acl_init(kadm5_server_context * context)151ebfedea0SLionel Sambuc _kadm5_acl_init(kadm5_server_context *context)
152ebfedea0SLionel Sambuc {
153ebfedea0SLionel Sambuc     krb5_principal princ;
154ebfedea0SLionel Sambuc     krb5_error_code ret;
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc     ret = krb5_parse_name(context->context, KADM5_ADMIN_SERVICE, &princ);
157ebfedea0SLionel Sambuc     if (ret)
158ebfedea0SLionel Sambuc 	return ret;
159ebfedea0SLionel Sambuc     ret = krb5_principal_compare(context->context, context->caller, princ);
160ebfedea0SLionel Sambuc     krb5_free_principal(context->context, princ);
161ebfedea0SLionel Sambuc     if(ret != 0) {
162ebfedea0SLionel Sambuc 	context->acl_flags = KADM5_PRIV_ALL;
163ebfedea0SLionel Sambuc 	return 0;
164ebfedea0SLionel Sambuc     }
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc     return fetch_acl (context, NULL, &context->acl_flags);
167ebfedea0SLionel Sambuc }
168ebfedea0SLionel Sambuc 
169ebfedea0SLionel Sambuc /*
170ebfedea0SLionel Sambuc  * check if `flags' allows `op'
171ebfedea0SLionel Sambuc  * return 0 if OK or an error
172ebfedea0SLionel Sambuc  */
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc static kadm5_ret_t
check_flags(unsigned op,unsigned flags)175ebfedea0SLionel Sambuc check_flags (unsigned op,
176ebfedea0SLionel Sambuc 	     unsigned flags)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc     unsigned res = ~flags & op;
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc     if(res & KADM5_PRIV_GET)
181ebfedea0SLionel Sambuc 	return KADM5_AUTH_GET;
182ebfedea0SLionel Sambuc     if(res & KADM5_PRIV_ADD)
183ebfedea0SLionel Sambuc 	return KADM5_AUTH_ADD;
184ebfedea0SLionel Sambuc     if(res & KADM5_PRIV_MODIFY)
185ebfedea0SLionel Sambuc 	return KADM5_AUTH_MODIFY;
186ebfedea0SLionel Sambuc     if(res & KADM5_PRIV_DELETE)
187ebfedea0SLionel Sambuc 	return KADM5_AUTH_DELETE;
188ebfedea0SLionel Sambuc     if(res & KADM5_PRIV_CPW)
189ebfedea0SLionel Sambuc 	return KADM5_AUTH_CHANGEPW;
190ebfedea0SLionel Sambuc     if(res & KADM5_PRIV_LIST)
191ebfedea0SLionel Sambuc 	return KADM5_AUTH_LIST;
192ebfedea0SLionel Sambuc     if(res)
193ebfedea0SLionel Sambuc 	return KADM5_AUTH_INSUFFICIENT;
194ebfedea0SLionel Sambuc     return 0;
195ebfedea0SLionel Sambuc }
196ebfedea0SLionel Sambuc 
197ebfedea0SLionel Sambuc /*
198ebfedea0SLionel Sambuc  * return 0 if the current caller in `context' is allowed to perform
199ebfedea0SLionel Sambuc  * `op' on `princ' and otherwise an error
200ebfedea0SLionel Sambuc  * princ == NULL if it's not relevant.
201ebfedea0SLionel Sambuc  */
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_acl_check_permission(kadm5_server_context * context,unsigned op,krb5_const_principal princ)204ebfedea0SLionel Sambuc _kadm5_acl_check_permission(kadm5_server_context *context,
205ebfedea0SLionel Sambuc 			    unsigned op,
206ebfedea0SLionel Sambuc 			    krb5_const_principal princ)
207ebfedea0SLionel Sambuc {
208ebfedea0SLionel Sambuc     kadm5_ret_t ret;
209ebfedea0SLionel Sambuc     unsigned princ_flags;
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     ret = check_flags (op, context->acl_flags);
212ebfedea0SLionel Sambuc     if (ret == 0)
213ebfedea0SLionel Sambuc 	return ret;
214ebfedea0SLionel Sambuc     ret = fetch_acl (context, princ, &princ_flags);
215ebfedea0SLionel Sambuc     if (ret)
216ebfedea0SLionel Sambuc 	return ret;
217ebfedea0SLionel Sambuc     return check_flags (op, princ_flags);
218ebfedea0SLionel Sambuc }
219