ctdb-server: Clean up connection tracking functions
[samba4-gss.git] / source3 / lib / sharesec.c
blobda1d5e89e5484a894d7227f5b9a14b11e7591786
1 /*
2 * Unix SMB/Netbios implementation.
3 * SEC_DESC handling functions
4 * Copyright (C) Jeremy R. Allison 1995-2003.
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 .size = 0,
278 struct security_acl *psa = NULL;
279 struct security_descriptor *psd = NULL;
280 uint32_t spec_access = def_access;
282 se_map_generic(&spec_access, &file_generic_mapping);
284 sa = (def_access | spec_access );
285 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
287 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
288 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
289 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
290 psa, psize);
293 if (!psd) {
294 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
295 return NULL;
298 return psd;
301 /*******************************************************************
302 Pull a security descriptor from the share tdb.
303 ********************************************************************/
305 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
306 size_t *psize)
308 char *key;
309 struct security_descriptor *psd = NULL;
310 TDB_DATA data;
311 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
312 NTSTATUS status;
314 if (!c_servicename) {
315 return NULL;
318 status = share_info_db_init();
319 if (!NT_STATUS_IS_OK(status)) {
320 TALLOC_FREE(c_servicename);
321 return NULL;
324 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
325 TALLOC_FREE(c_servicename);
326 DEBUG(0, ("talloc_asprintf failed\n"));
327 return NULL;
330 TALLOC_FREE(c_servicename);
332 status = dbwrap_fetch_bystring(share_db, talloc_tos(), key, &data);
334 TALLOC_FREE(key);
336 if (!NT_STATUS_IS_OK(status)) {
337 return get_share_security_default(ctx, psize,
338 SEC_RIGHTS_DIR_ALL);
341 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
343 TALLOC_FREE(data.dptr);
345 if (!NT_STATUS_IS_OK(status)) {
346 return get_share_security_default(ctx, psize,
347 SEC_RIGHTS_DIR_ALL);
350 if (psd) {
351 *psize = ndr_size_security_descriptor(psd, 0);
352 } else {
353 return get_share_security_default(ctx, psize,
354 SEC_RIGHTS_DIR_ALL);
357 return psd;
360 /*******************************************************************
361 Store a security descriptor in the share db.
362 ********************************************************************/
364 NTSTATUS set_share_security(const char *share_name,
365 struct security_descriptor *psd)
367 TALLOC_CTX *frame = talloc_stackframe();
368 char *key;
369 TDB_DATA blob;
370 NTSTATUS status;
371 char *c_share_name = canonicalize_servicename(frame, share_name);
373 if (c_share_name == NULL) {
374 status = NT_STATUS_INVALID_PARAMETER;
375 goto out;
378 status = share_info_db_init();
379 if (!NT_STATUS_IS_OK(status)) {
380 goto out;
383 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(0, ("marshall_sec_desc failed: %s\n",
387 nt_errstr(status)));
388 goto out;
391 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
392 DEBUG(0, ("talloc_asprintf failed\n"));
393 status = NT_STATUS_NO_MEMORY;
394 goto out;
397 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
398 TDB_REPLACE);
399 if (!NT_STATUS_IS_OK(status)) {
400 DEBUG(1, ("set_share_security: Failed to store secdesc for "
401 "%s: %s\n", share_name, nt_errstr(status)));
402 goto out;
405 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
406 status = NT_STATUS_OK;
408 out:
409 TALLOC_FREE(frame);
410 return status;
413 /*******************************************************************
414 Delete a security descriptor.
415 ********************************************************************/
417 NTSTATUS delete_share_security(const char *servicename)
419 TDB_DATA kbuf;
420 char *key;
421 NTSTATUS status;
422 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
424 if (c_servicename == NULL) {
425 return NT_STATUS_INVALID_PARAMETER;
428 status = share_info_db_init();
429 if (!NT_STATUS_IS_OK(status)) {
430 TALLOC_FREE(c_servicename);
431 return status;
434 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
435 c_servicename))) {
436 TALLOC_FREE(c_servicename);
437 return NT_STATUS_NO_MEMORY;
439 kbuf = string_term_tdb_data(key);
441 status = dbwrap_trans_delete(share_db, kbuf);
442 if (!NT_STATUS_IS_OK(status)) {
443 DEBUG(0, ("delete_share_security: Failed to delete entry for "
444 "share %s: %s\n", c_servicename, nt_errstr(status)));
445 TALLOC_FREE(c_servicename);
446 return status;
449 TALLOC_FREE(c_servicename);
450 return NT_STATUS_OK;
453 /*******************************************************************
454 Can this user access with share with the required permissions ?
455 ********************************************************************/
457 bool share_access_check(const struct security_token *token,
458 const char *sharename,
459 uint32_t desired_access,
460 uint32_t *pgranted)
462 uint32_t granted;
463 NTSTATUS status;
464 struct security_descriptor *psd = NULL;
465 size_t sd_size;
467 psd = get_share_security(talloc_tos(), sharename, &sd_size);
469 if (!psd) {
470 if (pgranted != NULL) {
471 *pgranted = desired_access;
473 return false;
476 status = se_file_access_check(psd, token, true, desired_access, &granted);
478 TALLOC_FREE(psd);
480 if (pgranted != NULL) {
481 *pgranted = granted;
484 return NT_STATUS_IS_OK(status);
487 /***************************************************************************
488 Parse the contents of an acl string from a usershare file.
489 ***************************************************************************/
491 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
493 size_t s_size = 0;
494 const char *pacl = acl_str;
495 int num_aces = 0;
496 struct security_ace *ace_list = NULL;
497 struct security_acl *psa = NULL;
498 struct security_descriptor *psd = NULL;
499 size_t sd_size = 0;
500 int i;
502 *ppsd = NULL;
504 /* If the acl string is blank return "Everyone:R" */
505 if (!*acl_str) {
506 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
507 if (!default_psd) {
508 return False;
510 *ppsd = default_psd;
511 return True;
514 num_aces = 1;
516 /* Add the number of ',' characters to get the number of aces. */
517 num_aces += count_chars(pacl,',');
519 ace_list = talloc_array(ctx, struct security_ace, num_aces);
520 if (!ace_list) {
521 return False;
524 for (i = 0; i < num_aces; i++) {
525 uint32_t sa;
526 uint32_t g_access;
527 uint32_t s_access;
528 struct dom_sid sid;
529 char *sidstr;
530 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
532 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
533 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
534 "for ':' in string '%s'\n", pacl));
535 return False;
538 if (!string_to_sid(&sid, sidstr)) {
539 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
540 sidstr ));
541 return False;
544 switch (*pacl) {
545 case 'F': /* Full Control, ie. R+W */
546 case 'f': /* Full Control, ie. R+W */
547 s_access = g_access = GENERIC_ALL_ACCESS;
548 break;
549 case 'R': /* Read only. */
550 case 'r': /* Read only. */
551 s_access = g_access = GENERIC_READ_ACCESS;
552 break;
553 case 'D': /* Deny all to this SID. */
554 case 'd': /* Deny all to this SID. */
555 type = SEC_ACE_TYPE_ACCESS_DENIED;
556 s_access = g_access = GENERIC_ALL_ACCESS;
557 break;
558 default:
559 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
560 pacl ));
561 return False;
564 pacl++;
565 if (*pacl && *pacl != ',') {
566 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
567 pacl ));
568 return False;
570 pacl++; /* Go past any ',' */
572 se_map_generic(&s_access, &file_generic_mapping);
573 sa = (g_access | s_access);
574 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
577 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
578 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
579 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
580 psa, &sd_size);
583 if (!psd) {
584 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
585 return False;
588 *ppsd = psd;
589 return True;