| 1 | /* | 
|---|
| 2 | *  Unix SMB/Netbios implementation. | 
|---|
| 3 | *  SEC_DESC handling functions | 
|---|
| 4 | *  Copyright (C) Jeremy R. Allison            1995-2003. | 
|---|
| 5 | * | 
|---|
| 6 | *  This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | *  it under the terms of the GNU General Public License as published by | 
|---|
| 8 | *  the Free Software Foundation; either version 2 of the License, or | 
|---|
| 9 | *  (at your option) any later version. | 
|---|
| 10 | * | 
|---|
| 11 | *  This program is distributed in the hope that it will be useful, | 
|---|
| 12 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | *  GNU General Public License for more details. | 
|---|
| 15 | * | 
|---|
| 16 | *  You should have received a copy of the GNU General Public License | 
|---|
| 17 | *  along with this program; if not, write to the Free Software | 
|---|
| 18 | *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | #include "includes.h" | 
|---|
| 22 |  | 
|---|
| 23 | /******************************************************************* | 
|---|
| 24 | Create the share security tdb. | 
|---|
| 25 | ********************************************************************/ | 
|---|
| 26 |  | 
|---|
| 27 | static TDB_CONTEXT *share_tdb; /* used for share security descriptors */ | 
|---|
| 28 | #define SHARE_DATABASE_VERSION_V1 1 | 
|---|
| 29 | #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */ | 
|---|
| 30 |  | 
|---|
| 31 | /* Map generic permissions to file object specific permissions */ | 
|---|
| 32 |  | 
|---|
| 33 | static struct generic_mapping file_generic_mapping = { | 
|---|
| 34 | FILE_GENERIC_READ, | 
|---|
| 35 | FILE_GENERIC_WRITE, | 
|---|
| 36 | FILE_GENERIC_EXECUTE, | 
|---|
| 37 | FILE_GENERIC_ALL | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | BOOL share_info_db_init(void) | 
|---|
| 42 | { | 
|---|
| 43 | const char *vstring = "INFO/version"; | 
|---|
| 44 | int32 vers_id; | 
|---|
| 45 |  | 
|---|
| 46 | if (share_tdb) { | 
|---|
| 47 | return True; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | share_tdb = tdb_open_log(lock_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); | 
|---|
| 51 | if (!share_tdb) { | 
|---|
| 52 | DEBUG(0,("Failed to open share info database %s (%s)\n", | 
|---|
| 53 | lock_path("share_info.tdb"), strerror(errno) )); | 
|---|
| 54 | return False; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | /* handle a Samba upgrade */ | 
|---|
| 58 | tdb_lock_bystring(share_tdb, vstring); | 
|---|
| 59 |  | 
|---|
| 60 | /* Cope with byte-reversed older versions of the db. */ | 
|---|
| 61 | vers_id = tdb_fetch_int32(share_tdb, vstring); | 
|---|
| 62 | if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) { | 
|---|
| 63 | /* Written on a bigendian machine with old fetch_int code. Save as le. */ | 
|---|
| 64 | tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2); | 
|---|
| 65 | vers_id = SHARE_DATABASE_VERSION_V2; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | if (vers_id != SHARE_DATABASE_VERSION_V2) { | 
|---|
| 69 | tdb_traverse(share_tdb, tdb_traverse_delete_fn, NULL); | 
|---|
| 70 | tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2); | 
|---|
| 71 | } | 
|---|
| 72 | tdb_unlock_bystring(share_tdb, vstring); | 
|---|
| 73 |  | 
|---|
| 74 | return True; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | /******************************************************************* | 
|---|
| 78 | Fake up a Everyone, default access as a default. | 
|---|
| 79 | def_access is a GENERIC_XXX access mode. | 
|---|
| 80 | ********************************************************************/ | 
|---|
| 81 |  | 
|---|
| 82 | SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access) | 
|---|
| 83 | { | 
|---|
| 84 | SEC_ACCESS sa; | 
|---|
| 85 | SEC_ACE ace; | 
|---|
| 86 | SEC_ACL *psa = NULL; | 
|---|
| 87 | SEC_DESC *psd = NULL; | 
|---|
| 88 | uint32 spec_access = def_access; | 
|---|
| 89 |  | 
|---|
| 90 | se_map_generic(&spec_access, &file_generic_mapping); | 
|---|
| 91 |  | 
|---|
| 92 | init_sec_access(&sa, def_access | spec_access ); | 
|---|
| 93 | init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0); | 
|---|
| 94 |  | 
|---|
| 95 | if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) { | 
|---|
| 96 | psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, psize); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | if (!psd) { | 
|---|
| 100 | DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n")); | 
|---|
| 101 | return NULL; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | return psd; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | /******************************************************************* | 
|---|
| 108 | Pull a security descriptor from the share tdb. | 
|---|
| 109 | ********************************************************************/ | 
|---|
| 110 |  | 
|---|
| 111 | SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename, | 
|---|
| 112 | size_t *psize) | 
|---|
| 113 | { | 
|---|
| 114 | prs_struct ps; | 
|---|
| 115 | fstring key; | 
|---|
| 116 | SEC_DESC *psd = NULL; | 
|---|
| 117 |  | 
|---|
| 118 | if (!share_info_db_init()) { | 
|---|
| 119 | return NULL; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | *psize = 0; | 
|---|
| 123 |  | 
|---|
| 124 | /* Fetch security descriptor from tdb */ | 
|---|
| 125 |  | 
|---|
| 126 | slprintf(key, sizeof(key)-1, "SECDESC/%s", servicename); | 
|---|
| 127 |  | 
|---|
| 128 | if (tdb_prs_fetch(share_tdb, key, &ps, ctx)!=0 || | 
|---|
| 129 | !sec_io_desc("get_share_security", &psd, &ps, 1)) { | 
|---|
| 130 |  | 
|---|
| 131 | DEBUG(4, ("get_share_security: using default secdesc for %s\n", | 
|---|
| 132 | servicename)); | 
|---|
| 133 |  | 
|---|
| 134 | return get_share_security_default(ctx, psize, GENERIC_ALL_ACCESS); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | if (psd) | 
|---|
| 138 | *psize = sec_desc_size(psd); | 
|---|
| 139 |  | 
|---|
| 140 | prs_mem_free(&ps); | 
|---|
| 141 | return psd; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /******************************************************************* | 
|---|
| 145 | Store a security descriptor in the share db. | 
|---|
| 146 | ********************************************************************/ | 
|---|
| 147 |  | 
|---|
| 148 | BOOL set_share_security(const char *share_name, SEC_DESC *psd) | 
|---|
| 149 | { | 
|---|
| 150 | prs_struct ps; | 
|---|
| 151 | TALLOC_CTX *mem_ctx = NULL; | 
|---|
| 152 | fstring key; | 
|---|
| 153 | BOOL ret = False; | 
|---|
| 154 |  | 
|---|
| 155 | if (!share_info_db_init()) { | 
|---|
| 156 | return False; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | mem_ctx = talloc_init("set_share_security"); | 
|---|
| 160 | if (mem_ctx == NULL) | 
|---|
| 161 | return False; | 
|---|
| 162 |  | 
|---|
| 163 | prs_init(&ps, (uint32)sec_desc_size(psd), mem_ctx, MARSHALL); | 
|---|
| 164 |  | 
|---|
| 165 | if (!sec_io_desc("share_security", &psd, &ps, 1)) | 
|---|
| 166 | goto out; | 
|---|
| 167 |  | 
|---|
| 168 | slprintf(key, sizeof(key)-1, "SECDESC/%s", share_name); | 
|---|
| 169 |  | 
|---|
| 170 | if (tdb_prs_store(share_tdb, key, &ps)==0) { | 
|---|
| 171 | ret = True; | 
|---|
| 172 | DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name )); | 
|---|
| 173 | } else { | 
|---|
| 174 | DEBUG(1,("set_share_security: Failed to store secdesc for %s\n", share_name )); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /* Free malloc'ed memory */ | 
|---|
| 178 |  | 
|---|
| 179 | out: | 
|---|
| 180 |  | 
|---|
| 181 | prs_mem_free(&ps); | 
|---|
| 182 | if (mem_ctx) | 
|---|
| 183 | talloc_destroy(mem_ctx); | 
|---|
| 184 | return ret; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /******************************************************************* | 
|---|
| 188 | Delete a security descriptor. | 
|---|
| 189 | ********************************************************************/ | 
|---|
| 190 |  | 
|---|
| 191 | BOOL delete_share_security(const struct share_params *params) | 
|---|
| 192 | { | 
|---|
| 193 | TDB_DATA kbuf; | 
|---|
| 194 | fstring key; | 
|---|
| 195 |  | 
|---|
| 196 | slprintf(key, sizeof(key)-1, "SECDESC/%s", | 
|---|
| 197 | lp_servicename(params->service)); | 
|---|
| 198 | kbuf.dptr = key; | 
|---|
| 199 | kbuf.dsize = strlen(key)+1; | 
|---|
| 200 |  | 
|---|
| 201 | if (tdb_trans_delete(share_tdb, kbuf) != 0) { | 
|---|
| 202 | DEBUG(0,("delete_share_security: Failed to delete entry for share %s\n", | 
|---|
| 203 | lp_servicename(params->service) )); | 
|---|
| 204 | return False; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | return True; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | /******************************************************************* | 
|---|
| 211 | Can this user access with share with the required permissions ? | 
|---|
| 212 | ********************************************************************/ | 
|---|
| 213 |  | 
|---|
| 214 | BOOL share_access_check(const NT_USER_TOKEN *token, const char *sharename, | 
|---|
| 215 | uint32 desired_access) | 
|---|
| 216 | { | 
|---|
| 217 | uint32 granted; | 
|---|
| 218 | NTSTATUS status; | 
|---|
| 219 | TALLOC_CTX *mem_ctx = NULL; | 
|---|
| 220 | SEC_DESC *psd = NULL; | 
|---|
| 221 | size_t sd_size; | 
|---|
| 222 | BOOL ret = True; | 
|---|
| 223 |  | 
|---|
| 224 | if (!(mem_ctx = talloc_init("share_access_check"))) { | 
|---|
| 225 | return False; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | psd = get_share_security(mem_ctx, sharename, &sd_size); | 
|---|
| 229 |  | 
|---|
| 230 | if (!psd) { | 
|---|
| 231 | TALLOC_FREE(mem_ctx); | 
|---|
| 232 | return True; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | ret = se_access_check(psd, token, desired_access, &granted, &status); | 
|---|
| 236 |  | 
|---|
| 237 | talloc_destroy(mem_ctx); | 
|---|
| 238 | return ret; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /*************************************************************************** | 
|---|
| 242 | Parse the contents of an acl string from a usershare file. | 
|---|
| 243 | ***************************************************************************/ | 
|---|
| 244 |  | 
|---|
| 245 | BOOL parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd) | 
|---|
| 246 | { | 
|---|
| 247 | size_t s_size = 0; | 
|---|
| 248 | const char *pacl = acl_str; | 
|---|
| 249 | int num_aces = 0; | 
|---|
| 250 | SEC_ACE *ace_list = NULL; | 
|---|
| 251 | SEC_ACL *psa = NULL; | 
|---|
| 252 | SEC_DESC *psd = NULL; | 
|---|
| 253 | size_t sd_size = 0; | 
|---|
| 254 | int i; | 
|---|
| 255 |  | 
|---|
| 256 | *ppsd = NULL; | 
|---|
| 257 |  | 
|---|
| 258 | /* If the acl string is blank return "Everyone:R" */ | 
|---|
| 259 | if (!*acl_str) { | 
|---|
| 260 | SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS); | 
|---|
| 261 | if (!default_psd) { | 
|---|
| 262 | return False; | 
|---|
| 263 | } | 
|---|
| 264 | *ppsd = default_psd; | 
|---|
| 265 | return True; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | num_aces = 1; | 
|---|
| 269 |  | 
|---|
| 270 | /* Add the number of ',' characters to get the number of aces. */ | 
|---|
| 271 | num_aces += count_chars(pacl,','); | 
|---|
| 272 |  | 
|---|
| 273 | ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces); | 
|---|
| 274 | if (!ace_list) { | 
|---|
| 275 | return False; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | for (i = 0; i < num_aces; i++) { | 
|---|
| 279 | SEC_ACCESS sa; | 
|---|
| 280 | uint32 g_access; | 
|---|
| 281 | uint32 s_access; | 
|---|
| 282 | DOM_SID sid; | 
|---|
| 283 | fstring sidstr; | 
|---|
| 284 | uint8 type = SEC_ACE_TYPE_ACCESS_ALLOWED; | 
|---|
| 285 |  | 
|---|
| 286 | if (!next_token(&pacl, sidstr, ":", sizeof(sidstr))) { | 
|---|
| 287 | DEBUG(0,("parse_usershare_acl: malformed usershare acl looking " | 
|---|
| 288 | "for ':' in string '%s'\n", pacl)); | 
|---|
| 289 | return False; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | if (!string_to_sid(&sid, sidstr)) { | 
|---|
| 293 | DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n", | 
|---|
| 294 | sidstr )); | 
|---|
| 295 | return False; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | switch (*pacl) { | 
|---|
| 299 | case 'F': /* Full Control, ie. R+W */ | 
|---|
| 300 | case 'f': /* Full Control, ie. R+W */ | 
|---|
| 301 | s_access = g_access = GENERIC_ALL_ACCESS; | 
|---|
| 302 | break; | 
|---|
| 303 | case 'R': /* Read only. */ | 
|---|
| 304 | case 'r': /* Read only. */ | 
|---|
| 305 | s_access = g_access = GENERIC_READ_ACCESS; | 
|---|
| 306 | break; | 
|---|
| 307 | case 'D': /* Deny all to this SID. */ | 
|---|
| 308 | case 'd': /* Deny all to this SID. */ | 
|---|
| 309 | type = SEC_ACE_TYPE_ACCESS_DENIED; | 
|---|
| 310 | s_access = g_access = GENERIC_ALL_ACCESS; | 
|---|
| 311 | break; | 
|---|
| 312 | default: | 
|---|
| 313 | DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n", | 
|---|
| 314 | pacl )); | 
|---|
| 315 | return False; | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | pacl++; | 
|---|
| 319 | if (*pacl && *pacl != ',') { | 
|---|
| 320 | DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n", | 
|---|
| 321 | pacl )); | 
|---|
| 322 | return False; | 
|---|
| 323 | } | 
|---|
| 324 | pacl++; /* Go past any ',' */ | 
|---|
| 325 |  | 
|---|
| 326 | se_map_generic(&s_access, &file_generic_mapping); | 
|---|
| 327 | init_sec_access(&sa, g_access | s_access ); | 
|---|
| 328 | init_sec_ace(&ace_list[i], &sid, type, sa, 0); | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) { | 
|---|
| 332 | psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, &sd_size); | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | if (!psd) { | 
|---|
| 336 | DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n")); | 
|---|
| 337 | return False; | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | *ppsd = psd; | 
|---|
| 341 | return True; | 
|---|
| 342 | } | 
|---|