ldb: add tests for ldb_wildcard_compare
[Samba.git] / source3 / lib / sharesec.c
blobfbb2d4795fa5147b15fcefebbc13ccb22826cfd9
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 static struct security_descriptor *get_share_security_default(TALLOC_CTX *ctx,
271 size_t *psize,
272 uint32_t def_access)
274 uint32_t sa;
275 struct security_ace ace;
276 struct security_acl *psa = NULL;
277 struct security_descriptor *psd = NULL;
278 uint32_t spec_access = def_access;
280 se_map_generic(&spec_access, &file_generic_mapping);
282 sa = (def_access | spec_access );
283 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
285 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
286 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
287 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
288 psa, psize);
291 if (!psd) {
292 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
293 return NULL;
296 return psd;
299 /*******************************************************************
300 Pull a security descriptor from the share tdb.
301 ********************************************************************/
303 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
304 size_t *psize)
306 char *key;
307 struct security_descriptor *psd = NULL;
308 TDB_DATA data;
309 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
310 NTSTATUS status;
312 if (!c_servicename) {
313 return NULL;
316 status = share_info_db_init();
317 if (!NT_STATUS_IS_OK(status)) {
318 TALLOC_FREE(c_servicename);
319 return NULL;
322 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
323 TALLOC_FREE(c_servicename);
324 DEBUG(0, ("talloc_asprintf failed\n"));
325 return NULL;
328 TALLOC_FREE(c_servicename);
330 status = dbwrap_fetch_bystring(share_db, talloc_tos(), key, &data);
332 TALLOC_FREE(key);
334 if (!NT_STATUS_IS_OK(status)) {
335 return get_share_security_default(ctx, psize,
336 SEC_RIGHTS_DIR_ALL);
339 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
341 TALLOC_FREE(data.dptr);
343 if (!NT_STATUS_IS_OK(status)) {
344 return get_share_security_default(ctx, psize,
345 SEC_RIGHTS_DIR_ALL);
348 if (psd) {
349 *psize = ndr_size_security_descriptor(psd, 0);
350 } else {
351 return get_share_security_default(ctx, psize,
352 SEC_RIGHTS_DIR_ALL);
355 return psd;
358 /*******************************************************************
359 Store a security descriptor in the share db.
360 ********************************************************************/
362 NTSTATUS set_share_security(const char *share_name,
363 struct security_descriptor *psd)
365 TALLOC_CTX *frame = talloc_stackframe();
366 char *key;
367 TDB_DATA blob;
368 NTSTATUS status;
369 char *c_share_name = canonicalize_servicename(frame, share_name);
371 if (c_share_name == NULL) {
372 status = NT_STATUS_INVALID_PARAMETER;
373 goto out;
376 status = share_info_db_init();
377 if (!NT_STATUS_IS_OK(status)) {
378 goto out;
381 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
383 if (!NT_STATUS_IS_OK(status)) {
384 DEBUG(0, ("marshall_sec_desc failed: %s\n",
385 nt_errstr(status)));
386 goto out;
389 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
390 DEBUG(0, ("talloc_asprintf failed\n"));
391 status = NT_STATUS_NO_MEMORY;
392 goto out;
395 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
396 TDB_REPLACE);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(1, ("set_share_security: Failed to store secdesc for "
399 "%s: %s\n", share_name, nt_errstr(status)));
400 goto out;
403 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
404 status = NT_STATUS_OK;
406 out:
407 TALLOC_FREE(frame);
408 return status;
411 /*******************************************************************
412 Delete a security descriptor.
413 ********************************************************************/
415 NTSTATUS delete_share_security(const char *servicename)
417 TDB_DATA kbuf;
418 char *key;
419 NTSTATUS status;
420 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
422 if (c_servicename == NULL) {
423 return NT_STATUS_INVALID_PARAMETER;
426 status = share_info_db_init();
427 if (!NT_STATUS_IS_OK(status)) {
428 TALLOC_FREE(c_servicename);
429 return status;
432 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
433 c_servicename))) {
434 TALLOC_FREE(c_servicename);
435 return NT_STATUS_NO_MEMORY;
437 kbuf = string_term_tdb_data(key);
439 status = dbwrap_trans_delete(share_db, kbuf);
440 if (!NT_STATUS_IS_OK(status)) {
441 DEBUG(0, ("delete_share_security: Failed to delete entry for "
442 "share %s: %s\n", c_servicename, nt_errstr(status)));
443 TALLOC_FREE(c_servicename);
444 return status;
447 TALLOC_FREE(c_servicename);
448 return NT_STATUS_OK;
451 /*******************************************************************
452 Can this user access with share with the required permissions ?
453 ********************************************************************/
455 bool share_access_check(const struct security_token *token,
456 const char *sharename,
457 uint32_t desired_access,
458 uint32_t *pgranted)
460 uint32_t granted;
461 NTSTATUS status;
462 struct security_descriptor *psd = NULL;
463 size_t sd_size;
465 psd = get_share_security(talloc_tos(), sharename, &sd_size);
467 if (!psd) {
468 if (pgranted != NULL) {
469 *pgranted = desired_access;
471 return false;
474 status = se_file_access_check(psd, token, true, desired_access, &granted);
476 TALLOC_FREE(psd);
478 if (pgranted != NULL) {
479 *pgranted = granted;
482 return NT_STATUS_IS_OK(status);
485 /***************************************************************************
486 Parse the contents of an acl string from a usershare file.
487 ***************************************************************************/
489 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
491 size_t s_size = 0;
492 const char *pacl = acl_str;
493 int num_aces = 0;
494 struct security_ace *ace_list = NULL;
495 struct security_acl *psa = NULL;
496 struct security_descriptor *psd = NULL;
497 size_t sd_size = 0;
498 int i;
500 *ppsd = NULL;
502 /* If the acl string is blank return "Everyone:R" */
503 if (!*acl_str) {
504 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
505 if (!default_psd) {
506 return False;
508 *ppsd = default_psd;
509 return True;
512 num_aces = 1;
514 /* Add the number of ',' characters to get the number of aces. */
515 num_aces += count_chars(pacl,',');
517 ace_list = talloc_array(ctx, struct security_ace, num_aces);
518 if (!ace_list) {
519 return False;
522 for (i = 0; i < num_aces; i++) {
523 uint32_t sa;
524 uint32_t g_access;
525 uint32_t s_access;
526 struct dom_sid sid;
527 char *sidstr;
528 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
530 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
531 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
532 "for ':' in string '%s'\n", pacl));
533 return False;
536 if (!string_to_sid(&sid, sidstr)) {
537 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
538 sidstr ));
539 return False;
542 switch (*pacl) {
543 case 'F': /* Full Control, ie. R+W */
544 case 'f': /* Full Control, ie. R+W */
545 s_access = g_access = GENERIC_ALL_ACCESS;
546 break;
547 case 'R': /* Read only. */
548 case 'r': /* Read only. */
549 s_access = g_access = GENERIC_READ_ACCESS;
550 break;
551 case 'D': /* Deny all to this SID. */
552 case 'd': /* Deny all to this SID. */
553 type = SEC_ACE_TYPE_ACCESS_DENIED;
554 s_access = g_access = GENERIC_ALL_ACCESS;
555 break;
556 default:
557 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
558 pacl ));
559 return False;
562 pacl++;
563 if (*pacl && *pacl != ',') {
564 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
565 pacl ));
566 return False;
568 pacl++; /* Go past any ',' */
570 se_map_generic(&s_access, &file_generic_mapping);
571 sa = (g_access | s_access);
572 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
575 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
576 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
577 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
578 psa, &sd_size);
581 if (!psd) {
582 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
583 return False;
586 *ppsd = psd;
587 return True;