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