1 /* authz.h : authz parsing and searching, private to libsvn_repos 2 * 3 * ==================================================================== 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 * ==================================================================== 21 */ 22 23 #ifndef SVN_REPOS_AUTHZ_H 24 #define SVN_REPOS_AUTHZ_H 25 26 #include <apr_hash.h> 27 #include <apr_pools.h> 28 #include <apr_tables.h> 29 30 #include "svn_config.h" 31 #include "svn_error.h" 32 #include "svn_io.h" 33 #include "svn_repos.h" 34 35 #include "private/svn_string_private.h" 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif /* __cplusplus */ 40 41 42 /* 43 * Authz and global group file parsing 44 */ 45 46 /* A dictionary of rules that are specific to a particular 47 (user, repository) combination. */ 48 typedef struct authz_user_rules_t authz_user_rules_t; 49 50 51 /* Access rights in an ACL. 52 * 53 * This enum is different from and incompatible with 54 * svn_repos_authz_access_t, because it has different semantics and 55 * encodes rights that are not and should never be exposed in the 56 * public API. 57 */ 58 typedef enum authz_access_t 59 { 60 /* 61 * Individual access flags 62 */ 63 64 /* TODO: Future extension for lookup/traverse access. 65 authz_access_lookup_flag = 0x10, */ 66 67 /* Read access allows listing directory entries, reading file 68 contents and reading properties of files and directories. */ 69 authz_access_read_flag = 0x20, 70 71 /* Write access allows adding, removing and renaming directory 72 entries, modifying file contents and adding, removing and 73 modifying properties of files and directories. */ 74 authz_access_write_flag = 0x40, 75 76 /* 77 * Combined access flags 78 */ 79 80 /* No access. */ 81 authz_access_none = 0, 82 83 84 /* TODO: Lookup access is a synonym for the lookup flag. 85 authz_access_lookup = authz_access_lookup_flag, */ 86 87 /* Read access (TODO: implies lookup access). */ 88 authz_access_read = authz_access_read_flag /* TODO: | authz_access_lookup */, 89 90 /* Write access implies read (TODO: and lookup) access. */ 91 authz_access_write = authz_access_write_flag | authz_access_read 92 } authz_access_t; 93 94 95 /* Accumulated rights for (user, repository). */ 96 typedef struct authz_rights_t 97 { 98 /* The lowest level of access that the user has to every 99 path in the repository. */ 100 authz_access_t min_access; 101 102 /* The highest level of access that the user has to 103 any path in the repository. */ 104 authz_access_t max_access; 105 } authz_rights_t; 106 107 108 /* Accumulated global rights for a specific user. */ 109 typedef struct authz_global_rights_t 110 { 111 /* The user name. */ 112 const char *user; 113 114 /* Accumulated rights for this user from rules that are not 115 repository-specific. We use this to avoid a hash lookup for the 116 "any" repository rights. */ 117 authz_rights_t any_repos_rights; 118 119 /* Accumulated rights for this user across all repositories. */ 120 authz_rights_t all_repos_rights; 121 122 /* Accumulated rights for specific repositories. 123 The key is repository name, the value is an authz_rights_t*. */ 124 apr_hash_t *per_repos_rights; 125 } authz_global_rights_t; 126 127 128 /* Immutable authorization info */ 129 typedef struct authz_full_t 130 { 131 /* All ACLs from the authz file, in the order of definition. */ 132 apr_array_header_t *acls; 133 134 /* Globally accumulated rights for anonymous access. */ 135 svn_boolean_t has_anon_rights; 136 authz_global_rights_t anon_rights; 137 138 /* Globally accumulated rights for authenticated users. */ 139 svn_boolean_t has_authn_rights; 140 authz_global_rights_t authn_rights; 141 142 /* Globally accumulated rights, for all concrete users mentioned 143 in the authz file. The key is the user name, the value is 144 an authz_global_rights_t*. */ 145 apr_hash_t *user_rights; 146 147 /* The pool from which all the parsed authz data is allocated. 148 This is the RESULT_POOL passed to svn_authz__tng_parse. 149 150 It's a good idea to dedicate a pool for the authz structure, so 151 that the whole authz representation can be deallocated by 152 destroying the pool. */ 153 apr_pool_t *pool; 154 } authz_full_t; 155 156 157 /* Dynamic authorization info */ 158 struct svn_authz_t 159 { 160 /* The parsed and pre-processed contents of the authz file. */ 161 authz_full_t *full; 162 163 /* Identifies the authz model content 164 * (a hash value that can be used for e.g. cache lookups). */ 165 svn_membuf_t *authz_id; 166 167 /* Rules filtered for a particular user-repository combination. 168 * May be NULL. */ 169 authz_user_rules_t *filtered; 170 171 /* The pool from which all the parsed authz data is allocated. 172 This is the RESULT_POOL passed to svn_authz__tng_parse. 173 174 It's a good idea to dedicate a pool for the authz structure, so 175 that the whole authz representation can be deallocated by 176 destroying the pool. */ 177 apr_pool_t *pool; 178 }; 179 180 181 /* Rule path segment descriptor. */ 182 typedef struct authz_rule_segment_t 183 { 184 /* The segment type. */ 185 enum { 186 /* A literal string match. 187 The path segment must exactly match the pattern. 188 189 Note: Make sure this is always the first constant in the 190 enumeration, otherwise rules that match the repository 191 root will not sort first in the ACL list and the implicit 192 default no-access ACE will not be applied correctly. */ 193 authz_rule_literal, 194 195 /* A prefix match: a literal string followed by '*'. 196 The path segment must begin with the literal prefix. */ 197 authz_rule_prefix, 198 199 /* A suffix match: '*' followed by a literal string. 200 The path segment must end with the literal suffix. 201 The pattern is stored reversed, so that the matching code can 202 perform a prefix match on the reversed path segment. */ 203 authz_rule_suffix, 204 205 /* '*' 206 Matches any single non-empty path segment. 207 The pattern will be an empty string. */ 208 authz_rule_any_segment, 209 210 /* '**' 211 Matches any sequence of zero or more path segments. 212 The pattern will be an empty string. */ 213 authz_rule_any_recursive, 214 215 /* Any other glob/fnmatch pattern. */ 216 authz_rule_fnmatch 217 } kind; 218 219 /* The pattern for this path segment. 220 Any no-op fnmatch escape sequences (i.e., those that do not 221 escape a wildcard or character class) are stripped from the 222 string. 223 224 The pattern string will be interned; therefore, two identical 225 rule patterns will always contain the same pointer value and 226 equality can therefore be tested by comparing the pointer 227 values and segment kinds. */ 228 svn_string_t pattern; 229 } authz_rule_segment_t; 230 231 /* Rule path descriptor. */ 232 typedef struct authz_rule_t 233 { 234 /* The repository that this rule applies to. This will be the empty 235 string string if a the rule did not name a repository. The 236 repository name is interned. */ 237 const char *repos; 238 239 /* The number of segments in the rule path. */ 240 int len; 241 242 /* The array of path segments for this rule. Will be NULL for the 243 repository root. */ 244 authz_rule_segment_t *path; 245 } authz_rule_t; 246 247 248 /* An access control list defined by access rules. */ 249 typedef struct authz_acl_t 250 { 251 /* The sequence number of the ACL stores the order in which access 252 rules were defined in the authz file. The authz lookup code 253 selects the highest-numbered ACL from amongst a set of equivalent 254 matches. */ 255 int sequence_number; 256 257 /* The parsed rule. */ 258 authz_rule_t rule; 259 260 /* Access rights for anonymous users */ 261 svn_boolean_t has_anon_access; 262 authz_access_t anon_access; 263 264 /* Access rights for authenticated users */ 265 svn_boolean_t has_authn_access; 266 authz_access_t authn_access; 267 268 /* All other user- or group-specific access rights. 269 Aliases are replaced with their definitions, rules for the same 270 user or group are merged. */ 271 apr_array_header_t *user_access; 272 } authz_acl_t; 273 274 275 /* An access control entry in authz_acl_t::user_access. */ 276 typedef struct authz_ace_t 277 { 278 /* The name of the alias, user or group that this ACE applies to. */ 279 const char *name; 280 281 /* The set of group members, when NAME is the name of a group. 282 We store this reference in the ACE to save a hash lookup when 283 resolving access for group ACEs. 284 */ 285 apr_hash_t *members; 286 287 /* True if this is an inverse-match rule. */ 288 svn_boolean_t inverted; 289 290 /* The access rights defined by this ACE. */ 291 authz_access_t access; 292 } authz_ace_t; 293 294 295 /* Parse authz definitions from RULES and optional global group 296 * definitions from GROUPS, returning an immutable, in-memory 297 * representation of all the rules, groups and aliases. 298 * 299 * **AUTHZ and its contents will be allocated from RESULT_POOL. 300 * The function uses SCRATCH_POOL for temporary allocations. 301 */ 302 svn_error_t * 303 svn_authz__parse(authz_full_t **authz, 304 svn_stream_t *rules, 305 svn_stream_t *groups, 306 apr_pool_t *result_pool, 307 apr_pool_t *scratch_pool); 308 309 310 /* Reverse a STRING of length LEN in place. */ 311 void 312 svn_authz__reverse_string(char *string, apr_size_t len); 313 314 315 /* Compare two rules in lexical order by path only. */ 316 int 317 svn_authz__compare_paths(const authz_rule_t *a, const authz_rule_t *b); 318 319 /* Compare two rules in path lexical order, then repository lexical order. */ 320 int 321 svn_authz__compare_rules(const authz_rule_t *a, const authz_rule_t *b); 322 323 324 /* 325 * Authorization lookup 326 */ 327 328 /* The "anonymous" user for authz queries. */ 329 #define AUTHZ_ANONYMOUS_USER ((const char*)"") 330 331 /* Rules with this repository name apply to all repositories. */ 332 #define AUTHZ_ANY_REPOSITORY ((const char*)"") 333 334 /* Check if the ACL applies to the REPOS pair. */ 335 svn_boolean_t 336 svn_authz__acl_applies_to_repo(const authz_acl_t *acl, 337 const char *repos); 338 339 /* Check if the ACL applies to the (USER, REPOS) pair. If it does, 340 * and ACCESS is not NULL, set *ACCESS to the actual access rights for 341 * the user in this repository. 342 */ 343 svn_boolean_t 344 svn_authz__get_acl_access(authz_access_t *access, 345 const authz_acl_t *acl, 346 const char *user, const char *repos); 347 348 349 /* Set *RIGHTS to the accumulated global access rights calculated in 350 * AUTHZ for (USER, REPOS). 351 * Return TRUE if the rights are explicit (i.e., an ACL for REPOS 352 * applies to USER, or REPOS is AUTHZ_ANY_REPOSITORY). 353 */ 354 svn_boolean_t 355 svn_authz__get_global_rights(authz_rights_t *rights, 356 const authz_full_t *authz, 357 const char *user, const char *repos); 358 359 360 #ifdef __cplusplus 361 } 362 #endif /* __cplusplus */ 363 364 #endif /* SVN_REPOS_AUTHZ_H */ 365