s3/torture: use stack buffer for rbtree loop
[Samba.git] / source3 / lib / sharesec.c
blobacbdd8b5df918b9f1c687dd39e9f7d615c4f550b
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
24 #include "dbwrap/dbwrap.h"
25 #include "dbwrap/dbwrap_open.h"
26 #include "util_tdb.h"
27 #include "libcli/util/ntstatus.h"
29 /*******************************************************************
30 Create the share security tdb.
31 ********************************************************************/
33 static struct db_context *share_db; /* used for share security descriptors */
34 #define SHARE_DATABASE_VERSION_V1 1
35 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
36 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
38 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
39 /* Map generic permissions to file object specific permissions */
41 extern const struct generic_mapping file_generic_mapping;
43 static int delete_fn(struct db_record *rec, void *priv)
45 dbwrap_record_delete(rec);
46 return 0;
49 /*****************************************************
50 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
51 If we find one re-write it into a canonical case form.
52 *****************************************************/
54 static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
56 size_t prefix_len = strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR);
57 const char *servicename = NULL;
58 char *c_servicename = NULL;
59 char *newkey = NULL;
60 bool *p_upgrade_ok = (bool *)priv;
61 NTSTATUS status;
62 TDB_DATA key;
63 TDB_DATA value;
65 key = dbwrap_record_get_key(rec);
67 /* Is there space for a one character sharename ? */
68 if (key.dsize <= prefix_len+2) {
69 return 0;
72 /* Does it start with the share key prefix ? */
73 if (memcmp(key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
74 prefix_len) != 0) {
75 return 0;
78 /* Is it a null terminated string as a key ? */
79 if (key.dptr[key.dsize-1] != '\0') {
80 return 0;
83 /* Bytes after the prefix are the sharename string. */
84 servicename = (char *)&key.dptr[prefix_len];
85 c_servicename = canonicalize_servicename(talloc_tos(), servicename);
86 if (!c_servicename) {
87 smb_panic("out of memory upgrading share security db from v2 -> v3");
90 if (strcmp(servicename, c_servicename) == 0) {
91 /* Old and new names match. No canonicalization needed. */
92 TALLOC_FREE(c_servicename);
93 return 0;
96 /* Oops. Need to canonicalize name, delete old then store new. */
97 status = dbwrap_record_delete(rec);
98 if (!NT_STATUS_IS_OK(status)) {
99 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
100 "%s: %s\n", (const char *)key.dptr,
101 nt_errstr(status)));
102 TALLOC_FREE(c_servicename);
103 *p_upgrade_ok = false;
104 return -1;
105 } else {
106 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
107 "%s\n", (const char *)key.dptr));
110 if (!(newkey = talloc_asprintf(talloc_tos(),
111 SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
112 c_servicename))) {
113 smb_panic("out of memory upgrading share security db from v2 -> v3");
116 value = dbwrap_record_get_value(rec);
117 status = dbwrap_store(share_db,
118 string_term_tdb_data(newkey),
119 value,
120 TDB_REPLACE);
122 if (!NT_STATUS_IS_OK(status)) {
123 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
124 "%s: %s\n", c_servicename, nt_errstr(status)));
125 TALLOC_FREE(c_servicename);
126 TALLOC_FREE(newkey);
127 *p_upgrade_ok = false;
128 return -1;
129 } else {
130 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
131 "%s\n", newkey ));
134 TALLOC_FREE(newkey);
135 TALLOC_FREE(c_servicename);
137 return 0;
140 NTSTATUS share_info_db_init(void)
142 const char *vstring = "INFO/version";
143 int32_t vers_id = 0;
144 bool upgrade_ok = true;
145 NTSTATUS status;
146 char *db_path;
148 if (share_db != NULL) {
149 return NT_STATUS_OK;
152 db_path = state_path(talloc_tos(), "share_info.tdb");
153 if (db_path == NULL) {
154 return NT_STATUS_NO_MEMORY;
157 share_db = db_open(NULL, db_path, 0,
158 TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
159 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
160 if (share_db == NULL) {
161 DEBUG(0,("Failed to open share info database %s (%s)\n",
162 db_path, strerror(errno)));
163 TALLOC_FREE(db_path);
164 return map_nt_error_from_unix_common(errno);
166 TALLOC_FREE(db_path);
168 status = dbwrap_fetch_int32_bystring(share_db, vstring, &vers_id);
169 if (!NT_STATUS_IS_OK(status)) {
170 vers_id = 0;
173 if (vers_id == SHARE_DATABASE_VERSION_V3) {
174 return NT_STATUS_OK;
177 if (dbwrap_transaction_start(share_db) != 0) {
178 DEBUG(0, ("transaction_start failed\n"));
179 TALLOC_FREE(share_db);
180 return NT_STATUS_INTERNAL_DB_ERROR;
183 status = dbwrap_fetch_int32_bystring(share_db, vstring, &vers_id);
184 if (!NT_STATUS_IS_OK(status)) {
185 vers_id = 0;
188 if (vers_id == SHARE_DATABASE_VERSION_V3) {
190 * Race condition
192 if (dbwrap_transaction_cancel(share_db)) {
193 smb_panic("transaction_cancel failed");
195 return NT_STATUS_OK;
198 /* Move to at least V2. */
200 /* Cope with byte-reversed older versions of the db. */
201 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
202 /* Written on a bigendian machine with old fetch_int code. Save as le. */
204 status = dbwrap_store_int32_bystring(
205 share_db, vstring, SHARE_DATABASE_VERSION_V2);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
208 nt_errstr(status)));
209 goto cancel;
211 vers_id = SHARE_DATABASE_VERSION_V2;
214 if (vers_id != SHARE_DATABASE_VERSION_V2) {
215 status = dbwrap_traverse(share_db, delete_fn, NULL, NULL);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(0, ("traverse failed\n"));
218 goto cancel;
220 status = dbwrap_store_int32_bystring(
221 share_db, vstring, SHARE_DATABASE_VERSION_V2);
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
224 nt_errstr(status)));
225 goto cancel;
229 /* Finally upgrade to version 3, with canonicalized sharenames. */
231 status = dbwrap_traverse(share_db, upgrade_v2_to_v3, &upgrade_ok, NULL);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(0, ("traverse failed\n"));
234 goto cancel;
236 if (!upgrade_ok) {
237 DBG_ERR("upgrade failed.\n");
238 status = NT_STATUS_INTERNAL_ERROR;
239 goto cancel;
242 status = dbwrap_store_int32_bystring(
243 share_db, vstring, SHARE_DATABASE_VERSION_V3);
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
246 nt_errstr(status)));
247 goto cancel;
250 if (dbwrap_transaction_commit(share_db) != 0) {
251 DEBUG(0, ("transaction_commit failed\n"));
252 return NT_STATUS_INTERNAL_ERROR;
255 return NT_STATUS_OK;
257 cancel:
258 if (dbwrap_transaction_cancel(share_db)) {
259 smb_panic("transaction_cancel failed");
262 return status;
265 /*******************************************************************
266 Fake up a Everyone, default access as a default.
267 def_access is a GENERIC_XXX access mode.
268 ********************************************************************/
270 struct security_descriptor *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32_t def_access)
272 uint32_t sa;
273 struct security_ace ace;
274 struct security_acl *psa = NULL;
275 struct security_descriptor *psd = NULL;
276 uint32_t spec_access = def_access;
278 se_map_generic(&spec_access, &file_generic_mapping);
280 sa = (def_access | spec_access );
281 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
283 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
284 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
285 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
286 psa, psize);
289 if (!psd) {
290 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
291 return NULL;
294 return psd;
297 /*******************************************************************
298 Pull a security descriptor from the share tdb.
299 ********************************************************************/
301 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
302 size_t *psize)
304 char *key;
305 struct security_descriptor *psd = NULL;
306 TDB_DATA data;
307 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
308 NTSTATUS status;
310 if (!c_servicename) {
311 return NULL;
314 status = share_info_db_init();
315 if (!NT_STATUS_IS_OK(status)) {
316 TALLOC_FREE(c_servicename);
317 return NULL;
320 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
321 TALLOC_FREE(c_servicename);
322 DEBUG(0, ("talloc_asprintf failed\n"));
323 return NULL;
326 TALLOC_FREE(c_servicename);
328 status = dbwrap_fetch_bystring(share_db, talloc_tos(), key, &data);
330 TALLOC_FREE(key);
332 if (!NT_STATUS_IS_OK(status)) {
333 return get_share_security_default(ctx, psize,
334 SEC_RIGHTS_DIR_ALL);
337 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
339 TALLOC_FREE(data.dptr);
341 if (!NT_STATUS_IS_OK(status)) {
342 return get_share_security_default(ctx, psize,
343 SEC_RIGHTS_DIR_ALL);
346 if (psd) {
347 *psize = ndr_size_security_descriptor(psd, 0);
348 } else {
349 return get_share_security_default(ctx, psize,
350 SEC_RIGHTS_DIR_ALL);
353 return psd;
356 /*******************************************************************
357 Store a security descriptor in the share db.
358 ********************************************************************/
360 NTSTATUS set_share_security(const char *share_name,
361 struct security_descriptor *psd)
363 TALLOC_CTX *frame = talloc_stackframe();
364 char *key;
365 TDB_DATA blob;
366 NTSTATUS status;
367 char *c_share_name = canonicalize_servicename(frame, share_name);
369 if (c_share_name == NULL) {
370 status = NT_STATUS_INVALID_PARAMETER;
371 goto out;
374 status = share_info_db_init();
375 if (!NT_STATUS_IS_OK(status)) {
376 goto out;
379 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
381 if (!NT_STATUS_IS_OK(status)) {
382 DEBUG(0, ("marshall_sec_desc failed: %s\n",
383 nt_errstr(status)));
384 goto out;
387 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
388 DEBUG(0, ("talloc_asprintf failed\n"));
389 status = NT_STATUS_NO_MEMORY;
390 goto out;
393 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
394 TDB_REPLACE);
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(1, ("set_share_security: Failed to store secdesc for "
397 "%s: %s\n", share_name, nt_errstr(status)));
398 goto out;
401 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
402 status = NT_STATUS_OK;
404 out:
405 TALLOC_FREE(frame);
406 return status;
409 /*******************************************************************
410 Delete a security descriptor.
411 ********************************************************************/
413 NTSTATUS delete_share_security(const char *servicename)
415 TDB_DATA kbuf;
416 char *key;
417 NTSTATUS status;
418 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
420 if (c_servicename == NULL) {
421 return NT_STATUS_INVALID_PARAMETER;
424 status = share_info_db_init();
425 if (!NT_STATUS_IS_OK(status)) {
426 TALLOC_FREE(c_servicename);
427 return status;
430 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
431 c_servicename))) {
432 TALLOC_FREE(c_servicename);
433 return NT_STATUS_NO_MEMORY;
435 kbuf = string_term_tdb_data(key);
437 status = dbwrap_trans_delete(share_db, kbuf);
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(0, ("delete_share_security: Failed to delete entry for "
440 "share %s: %s\n", c_servicename, nt_errstr(status)));
441 TALLOC_FREE(c_servicename);
442 return status;
445 TALLOC_FREE(c_servicename);
446 return NT_STATUS_OK;
449 /*******************************************************************
450 Can this user access with share with the required permissions ?
451 ********************************************************************/
453 bool share_access_check(const struct security_token *token,
454 const char *sharename,
455 uint32_t desired_access,
456 uint32_t *pgranted)
458 uint32_t granted;
459 NTSTATUS status;
460 struct security_descriptor *psd = NULL;
461 size_t sd_size;
463 psd = get_share_security(talloc_tos(), sharename, &sd_size);
465 if (!psd) {
466 if (pgranted != NULL) {
467 *pgranted = desired_access;
469 return false;
472 status = se_file_access_check(psd, token, true, desired_access, &granted);
474 TALLOC_FREE(psd);
476 if (pgranted != NULL) {
477 *pgranted = granted;
480 return NT_STATUS_IS_OK(status);
483 /***************************************************************************
484 Parse the contents of an acl string from a usershare file.
485 ***************************************************************************/
487 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
489 size_t s_size = 0;
490 const char *pacl = acl_str;
491 int num_aces = 0;
492 struct security_ace *ace_list = NULL;
493 struct security_acl *psa = NULL;
494 struct security_descriptor *psd = NULL;
495 size_t sd_size = 0;
496 int i;
498 *ppsd = NULL;
500 /* If the acl string is blank return "Everyone:R" */
501 if (!*acl_str) {
502 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
503 if (!default_psd) {
504 return False;
506 *ppsd = default_psd;
507 return True;
510 num_aces = 1;
512 /* Add the number of ',' characters to get the number of aces. */
513 num_aces += count_chars(pacl,',');
515 ace_list = talloc_array(ctx, struct security_ace, num_aces);
516 if (!ace_list) {
517 return False;
520 for (i = 0; i < num_aces; i++) {
521 uint32_t sa;
522 uint32_t g_access;
523 uint32_t s_access;
524 struct dom_sid sid;
525 char *sidstr;
526 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
528 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
529 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
530 "for ':' in string '%s'\n", pacl));
531 return False;
534 if (!string_to_sid(&sid, sidstr)) {
535 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
536 sidstr ));
537 return False;
540 switch (*pacl) {
541 case 'F': /* Full Control, ie. R+W */
542 case 'f': /* Full Control, ie. R+W */
543 s_access = g_access = GENERIC_ALL_ACCESS;
544 break;
545 case 'R': /* Read only. */
546 case 'r': /* Read only. */
547 s_access = g_access = GENERIC_READ_ACCESS;
548 break;
549 case 'D': /* Deny all to this SID. */
550 case 'd': /* Deny all to this SID. */
551 type = SEC_ACE_TYPE_ACCESS_DENIED;
552 s_access = g_access = GENERIC_ALL_ACCESS;
553 break;
554 default:
555 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
556 pacl ));
557 return False;
560 pacl++;
561 if (*pacl && *pacl != ',') {
562 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
563 pacl ));
564 return False;
566 pacl++; /* Go past any ',' */
568 se_map_generic(&s_access, &file_generic_mapping);
569 sa = (g_access | s_access);
570 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
573 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
574 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
575 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
576 psa, &sd_size);
579 if (!psd) {
580 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
581 return False;
584 *ppsd = psd;
585 return True;