Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / kcm / acl.c
blob377e17656513f71809c9e24b9f1c9b251cc8abb9
1 /* $NetBSD: acl.c,v 1.1.1.1 2011/04/13 18:14:35 elric Exp $ */
3 /*
4 * Copyright (c) 2005, PADL Software Pty Ltd.
5 * All rights reserved.
7 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of PADL Software nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #include "kcm_locl.h"
39 krb5_error_code
40 kcm_access(krb5_context context,
41 kcm_client *client,
42 kcm_operation opcode,
43 kcm_ccache ccache)
45 int read_p = 0;
46 int write_p = 0;
47 uint16_t mask;
48 krb5_error_code ret;
50 KCM_ASSERT_VALID(ccache);
52 switch (opcode) {
53 case KCM_OP_INITIALIZE:
54 case KCM_OP_DESTROY:
55 case KCM_OP_STORE:
56 case KCM_OP_REMOVE_CRED:
57 case KCM_OP_SET_FLAGS:
58 case KCM_OP_CHOWN:
59 case KCM_OP_CHMOD:
60 case KCM_OP_GET_INITIAL_TICKET:
61 case KCM_OP_GET_TICKET:
62 case KCM_OP_MOVE_CACHE:
63 case KCM_OP_SET_DEFAULT_CACHE:
64 case KCM_OP_SET_KDC_OFFSET:
65 write_p = 1;
66 read_p = 0;
67 break;
68 case KCM_OP_NOOP:
69 case KCM_OP_GET_NAME:
70 case KCM_OP_RESOLVE:
71 case KCM_OP_GEN_NEW:
72 case KCM_OP_RETRIEVE:
73 case KCM_OP_GET_PRINCIPAL:
74 case KCM_OP_GET_CRED_UUID_LIST:
75 case KCM_OP_GET_CRED_BY_UUID:
76 case KCM_OP_GET_CACHE_UUID_LIST:
77 case KCM_OP_GET_CACHE_BY_UUID:
78 case KCM_OP_GET_DEFAULT_CACHE:
79 case KCM_OP_GET_KDC_OFFSET:
80 write_p = 0;
81 read_p = 1;
82 break;
83 default:
84 ret = KRB5_FCC_PERM;
85 goto out;
88 if (ccache->flags & KCM_FLAGS_OWNER_IS_SYSTEM) {
89 /* System caches cannot be reinitialized or destroyed by users */
90 if (opcode == KCM_OP_INITIALIZE ||
91 opcode == KCM_OP_DESTROY ||
92 opcode == KCM_OP_REMOVE_CRED ||
93 opcode == KCM_OP_MOVE_CACHE) {
94 ret = KRB5_FCC_PERM;
95 goto out;
98 /* Let root always read system caches */
99 if (CLIENT_IS_ROOT(client)) {
100 ret = 0;
101 goto out;
105 /* start out with "other" mask */
106 mask = S_IROTH|S_IWOTH;
108 /* root can do anything */
109 if (CLIENT_IS_ROOT(client)) {
110 if (read_p)
111 mask |= S_IRUSR|S_IRGRP|S_IROTH;
112 if (write_p)
113 mask |= S_IWUSR|S_IWGRP|S_IWOTH;
115 /* same session same as owner */
116 if (kcm_is_same_session(client, ccache->uid, ccache->session)) {
117 if (read_p)
118 mask |= S_IROTH;
119 if (write_p)
120 mask |= S_IWOTH;
122 /* owner */
123 if (client->uid == ccache->uid) {
124 if (read_p)
125 mask |= S_IRUSR;
126 if (write_p)
127 mask |= S_IWUSR;
129 /* group */
130 if (client->gid == ccache->gid) {
131 if (read_p)
132 mask |= S_IRGRP;
133 if (write_p)
134 mask |= S_IWGRP;
137 ret = (ccache->mode & mask) ? 0 : KRB5_FCC_PERM;
139 out:
140 if (ret) {
141 kcm_log(2, "Process %d is not permitted to call %s on cache %s",
142 client->pid, kcm_op2string(opcode), ccache->name);
145 return ret;
148 krb5_error_code
149 kcm_chmod(krb5_context context,
150 kcm_client *client,
151 kcm_ccache ccache,
152 uint16_t mode)
154 KCM_ASSERT_VALID(ccache);
156 /* System cache mode can only be set at startup */
157 if (ccache->flags & KCM_FLAGS_OWNER_IS_SYSTEM)
158 return KRB5_FCC_PERM;
160 if (ccache->uid != client->uid)
161 return KRB5_FCC_PERM;
163 if (ccache->gid != client->gid)
164 return KRB5_FCC_PERM;
166 HEIMDAL_MUTEX_lock(&ccache->mutex);
168 ccache->mode = mode;
170 HEIMDAL_MUTEX_unlock(&ccache->mutex);
172 return 0;
175 krb5_error_code
176 kcm_chown(krb5_context context,
177 kcm_client *client,
178 kcm_ccache ccache,
179 uid_t uid,
180 gid_t gid)
182 KCM_ASSERT_VALID(ccache);
184 /* System cache owner can only be set at startup */
185 if (ccache->flags & KCM_FLAGS_OWNER_IS_SYSTEM)
186 return KRB5_FCC_PERM;
188 if (ccache->uid != client->uid)
189 return KRB5_FCC_PERM;
191 if (ccache->gid != client->gid)
192 return KRB5_FCC_PERM;
194 HEIMDAL_MUTEX_lock(&ccache->mutex);
196 ccache->uid = uid;
197 ccache->gid = gid;
199 HEIMDAL_MUTEX_unlock(&ccache->mutex);
201 return 0;