ctdb-daemon: Do not allow database detach if AllowClientDBAttach=1
[Samba.git] / source3 / lib / sharesec.c
blob095c851825ed83ef3bf8adae980746e81c1d1e96
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"
28 /*******************************************************************
29 Create the share security tdb.
30 ********************************************************************/
32 static struct db_context *share_db; /* used for share security descriptors */
33 #define SHARE_DATABASE_VERSION_V1 1
34 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
35 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
37 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
38 /* Map generic permissions to file object specific permissions */
40 extern const struct generic_mapping file_generic_mapping;
42 static int delete_fn(struct db_record *rec, void *priv)
44 dbwrap_record_delete(rec);
45 return 0;
48 /*****************************************************
49 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
50 If we find one re-write it into a canonical case form.
51 *****************************************************/
53 static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
55 size_t prefix_len = strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR);
56 const char *servicename = NULL;
57 char *c_servicename = NULL;
58 char *newkey = NULL;
59 bool *p_upgrade_ok = (bool *)priv;
60 NTSTATUS status;
61 TDB_DATA key;
62 TDB_DATA value;
64 key = dbwrap_record_get_key(rec);
66 /* Is there space for a one character sharename ? */
67 if (key.dsize <= prefix_len+2) {
68 return 0;
71 /* Does it start with the share key prefix ? */
72 if (memcmp(key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
73 prefix_len) != 0) {
74 return 0;
77 /* Is it a null terminated string as a key ? */
78 if (key.dptr[key.dsize-1] != '\0') {
79 return 0;
82 /* Bytes after the prefix are the sharename string. */
83 servicename = (char *)&key.dptr[prefix_len];
84 c_servicename = canonicalize_servicename(talloc_tos(), servicename);
85 if (!c_servicename) {
86 smb_panic("out of memory upgrading share security db from v2 -> v3");
89 if (strcmp(servicename, c_servicename) == 0) {
90 /* Old and new names match. No canonicalization needed. */
91 TALLOC_FREE(c_servicename);
92 return 0;
95 /* Oops. Need to canonicalize name, delete old then store new. */
96 status = dbwrap_record_delete(rec);
97 if (!NT_STATUS_IS_OK(status)) {
98 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
99 "%s: %s\n", (const char *)key.dptr,
100 nt_errstr(status)));
101 TALLOC_FREE(c_servicename);
102 *p_upgrade_ok = false;
103 return -1;
104 } else {
105 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
106 "%s\n", (const char *)key.dptr));
109 if (!(newkey = talloc_asprintf(talloc_tos(),
110 SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
111 c_servicename))) {
112 smb_panic("out of memory upgrading share security db from v2 -> v3");
115 value = dbwrap_record_get_value(rec);
116 status = dbwrap_store(share_db,
117 string_term_tdb_data(newkey),
118 value,
119 TDB_REPLACE);
121 if (!NT_STATUS_IS_OK(status)) {
122 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
123 "%s: %s\n", c_servicename, nt_errstr(status)));
124 TALLOC_FREE(c_servicename);
125 TALLOC_FREE(newkey);
126 *p_upgrade_ok = false;
127 return -1;
128 } else {
129 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
130 "%s\n", newkey ));
133 TALLOC_FREE(newkey);
134 TALLOC_FREE(c_servicename);
136 return 0;
139 bool share_info_db_init(void)
141 const char *vstring = "INFO/version";
142 int32 vers_id = 0;
143 bool upgrade_ok = true;
144 NTSTATUS status;
146 if (share_db != NULL) {
147 return True;
150 share_db = db_open(NULL, state_path("share_info.tdb"), 0,
151 TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
152 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
153 if (share_db == NULL) {
154 DEBUG(0,("Failed to open share info database %s (%s)\n",
155 state_path("share_info.tdb"), strerror(errno) ));
156 return False;
159 status = dbwrap_fetch_int32_bystring(share_db, vstring, &vers_id);
160 if (!NT_STATUS_IS_OK(status)) {
161 vers_id = 0;
164 if (vers_id == SHARE_DATABASE_VERSION_V3) {
165 return true;
168 if (dbwrap_transaction_start(share_db) != 0) {
169 DEBUG(0, ("transaction_start failed\n"));
170 TALLOC_FREE(share_db);
171 return false;
174 status = dbwrap_fetch_int32_bystring(share_db, vstring, &vers_id);
175 if (!NT_STATUS_IS_OK(status)) {
176 vers_id = 0;
179 if (vers_id == SHARE_DATABASE_VERSION_V3) {
181 * Race condition
183 if (dbwrap_transaction_cancel(share_db)) {
184 smb_panic("transaction_cancel failed");
186 return true;
189 /* Move to at least V2. */
191 /* Cope with byte-reversed older versions of the db. */
192 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
193 /* Written on a bigendian machine with old fetch_int code. Save as le. */
195 status = dbwrap_store_int32_bystring(
196 share_db, vstring, SHARE_DATABASE_VERSION_V2);
197 if (!NT_STATUS_IS_OK(status)) {
198 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
199 nt_errstr(status)));
200 goto cancel;
202 vers_id = SHARE_DATABASE_VERSION_V2;
205 if (vers_id != SHARE_DATABASE_VERSION_V2) {
206 status = dbwrap_traverse(share_db, delete_fn, NULL, NULL);
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(0, ("traverse failed\n"));
209 goto cancel;
211 status = dbwrap_store_int32_bystring(
212 share_db, vstring, SHARE_DATABASE_VERSION_V2);
213 if (!NT_STATUS_IS_OK(status)) {
214 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
215 nt_errstr(status)));
216 goto cancel;
220 /* Finally upgrade to version 3, with canonicalized sharenames. */
222 status = dbwrap_traverse(share_db, upgrade_v2_to_v3, &upgrade_ok, NULL);
223 if (!NT_STATUS_IS_OK(status) || upgrade_ok == false) {
224 DEBUG(0, ("traverse failed\n"));
225 goto cancel;
227 status = dbwrap_store_int32_bystring(
228 share_db, vstring, SHARE_DATABASE_VERSION_V3);
229 if (!NT_STATUS_IS_OK(status)) {
230 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
231 nt_errstr(status)));
232 goto cancel;
235 if (dbwrap_transaction_commit(share_db) != 0) {
236 DEBUG(0, ("transaction_commit failed\n"));
237 return false;
240 return true;
242 cancel:
243 if (dbwrap_transaction_cancel(share_db)) {
244 smb_panic("transaction_cancel failed");
247 return false;
250 /*******************************************************************
251 Fake up a Everyone, default access as a default.
252 def_access is a GENERIC_XXX access mode.
253 ********************************************************************/
255 struct security_descriptor *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
257 uint32_t sa;
258 struct security_ace ace;
259 struct security_acl *psa = NULL;
260 struct security_descriptor *psd = NULL;
261 uint32 spec_access = def_access;
263 se_map_generic(&spec_access, &file_generic_mapping);
265 sa = (def_access | spec_access );
266 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
268 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
269 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
270 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
271 psa, psize);
274 if (!psd) {
275 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
276 return NULL;
279 return psd;
282 /*******************************************************************
283 Pull a security descriptor from the share tdb.
284 ********************************************************************/
286 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
287 size_t *psize)
289 char *key;
290 struct security_descriptor *psd = NULL;
291 TDB_DATA data;
292 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
293 NTSTATUS status;
295 if (!c_servicename) {
296 return NULL;
299 if (!share_info_db_init()) {
300 TALLOC_FREE(c_servicename);
301 return NULL;
304 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
305 TALLOC_FREE(c_servicename);
306 DEBUG(0, ("talloc_asprintf failed\n"));
307 return NULL;
310 TALLOC_FREE(c_servicename);
312 status = dbwrap_fetch_bystring(share_db, talloc_tos(), key, &data);
314 TALLOC_FREE(key);
316 if (!NT_STATUS_IS_OK(status)) {
317 return get_share_security_default(ctx, psize,
318 SEC_RIGHTS_DIR_ALL);
321 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
323 TALLOC_FREE(data.dptr);
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
327 nt_errstr(status)));
328 return get_share_security_default(ctx, psize,
329 SEC_RIGHTS_DIR_ALL);
332 if (psd) {
333 *psize = ndr_size_security_descriptor(psd, 0);
334 } else {
335 return get_share_security_default(ctx, psize,
336 SEC_RIGHTS_DIR_ALL);
339 return psd;
342 /*******************************************************************
343 Store a security descriptor in the share db.
344 ********************************************************************/
346 bool set_share_security(const char *share_name, struct security_descriptor *psd)
348 TALLOC_CTX *frame = talloc_stackframe();
349 char *key;
350 bool ret = False;
351 TDB_DATA blob;
352 NTSTATUS status;
353 char *c_share_name = canonicalize_servicename(frame, share_name);
355 if (!c_share_name) {
356 goto out;
359 if (!share_info_db_init()) {
360 goto out;
363 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
365 if (!NT_STATUS_IS_OK(status)) {
366 DEBUG(0, ("marshall_sec_desc failed: %s\n",
367 nt_errstr(status)));
368 goto out;
371 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
372 DEBUG(0, ("talloc_asprintf failed\n"));
373 goto out;
376 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
377 TDB_REPLACE);
378 if (!NT_STATUS_IS_OK(status)) {
379 DEBUG(1, ("set_share_security: Failed to store secdesc for "
380 "%s: %s\n", share_name, nt_errstr(status)));
381 goto out;
384 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
385 ret = True;
387 out:
388 TALLOC_FREE(frame);
389 return ret;
392 /*******************************************************************
393 Delete a security descriptor.
394 ********************************************************************/
396 bool delete_share_security(const char *servicename)
398 TDB_DATA kbuf;
399 char *key;
400 NTSTATUS status;
401 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
403 if (!c_servicename) {
404 return NULL;
407 if (!share_info_db_init()) {
408 TALLOC_FREE(c_servicename);
409 return False;
412 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
413 c_servicename))) {
414 TALLOC_FREE(c_servicename);
415 return False;
417 kbuf = string_term_tdb_data(key);
419 status = dbwrap_trans_delete(share_db, kbuf);
420 if (!NT_STATUS_IS_OK(status)) {
421 DEBUG(0, ("delete_share_security: Failed to delete entry for "
422 "share %s: %s\n", c_servicename, nt_errstr(status)));
423 TALLOC_FREE(c_servicename);
424 return False;
427 TALLOC_FREE(c_servicename);
428 return True;
431 /*******************************************************************
432 Can this user access with share with the required permissions ?
433 ********************************************************************/
435 bool share_access_check(const struct security_token *token,
436 const char *sharename,
437 uint32 desired_access,
438 uint32_t *pgranted)
440 uint32 granted;
441 NTSTATUS status;
442 struct security_descriptor *psd = NULL;
443 size_t sd_size;
445 psd = get_share_security(talloc_tos(), sharename, &sd_size);
447 if (!psd) {
448 if (pgranted != NULL) {
449 *pgranted = desired_access;
451 return false;
454 status = se_file_access_check(psd, token, true, desired_access, &granted);
456 TALLOC_FREE(psd);
458 if (pgranted != NULL) {
459 *pgranted = granted;
462 return NT_STATUS_IS_OK(status);
465 /***************************************************************************
466 Parse the contents of an acl string from a usershare file.
467 ***************************************************************************/
469 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
471 size_t s_size = 0;
472 const char *pacl = acl_str;
473 int num_aces = 0;
474 struct security_ace *ace_list = NULL;
475 struct security_acl *psa = NULL;
476 struct security_descriptor *psd = NULL;
477 size_t sd_size = 0;
478 int i;
480 *ppsd = NULL;
482 /* If the acl string is blank return "Everyone:R" */
483 if (!*acl_str) {
484 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
485 if (!default_psd) {
486 return False;
488 *ppsd = default_psd;
489 return True;
492 num_aces = 1;
494 /* Add the number of ',' characters to get the number of aces. */
495 num_aces += count_chars(pacl,',');
497 ace_list = talloc_array(ctx, struct security_ace, num_aces);
498 if (!ace_list) {
499 return False;
502 for (i = 0; i < num_aces; i++) {
503 uint32_t sa;
504 uint32 g_access;
505 uint32 s_access;
506 struct dom_sid sid;
507 char *sidstr;
508 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
510 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
511 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
512 "for ':' in string '%s'\n", pacl));
513 return False;
516 if (!string_to_sid(&sid, sidstr)) {
517 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
518 sidstr ));
519 return False;
522 switch (*pacl) {
523 case 'F': /* Full Control, ie. R+W */
524 case 'f': /* Full Control, ie. R+W */
525 s_access = g_access = GENERIC_ALL_ACCESS;
526 break;
527 case 'R': /* Read only. */
528 case 'r': /* Read only. */
529 s_access = g_access = GENERIC_READ_ACCESS;
530 break;
531 case 'D': /* Deny all to this SID. */
532 case 'd': /* Deny all to this SID. */
533 type = SEC_ACE_TYPE_ACCESS_DENIED;
534 s_access = g_access = GENERIC_ALL_ACCESS;
535 break;
536 default:
537 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
538 pacl ));
539 return False;
542 pacl++;
543 if (*pacl && *pacl != ',') {
544 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
545 pacl ));
546 return False;
548 pacl++; /* Go past any ',' */
550 se_map_generic(&s_access, &file_generic_mapping);
551 sa = (g_access | s_access);
552 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
555 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
556 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
557 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
558 psa, &sd_size);
561 if (!psd) {
562 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
563 return False;
566 *ppsd = psd;
567 return True;