s3-waf: create a smbldap.so library.
[Samba/id10ts.git] / source3 / lib / sharesec.c
blob2c324cf971c920a6c2f963698f05263ae852b7c8
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 if (share_db == NULL) {
153 DEBUG(0,("Failed to open share info database %s (%s)\n",
154 state_path("share_info.tdb"), strerror(errno) ));
155 return False;
158 status = dbwrap_fetch_int32(share_db, vstring, &vers_id);
159 if (!NT_STATUS_IS_OK(status)) {
160 vers_id = 0;
163 if (vers_id == SHARE_DATABASE_VERSION_V3) {
164 return true;
167 if (dbwrap_transaction_start(share_db) != 0) {
168 DEBUG(0, ("transaction_start failed\n"));
169 TALLOC_FREE(share_db);
170 return false;
173 status = dbwrap_fetch_int32(share_db, vstring, &vers_id);
174 if (!NT_STATUS_IS_OK(status)) {
175 vers_id = 0;
178 if (vers_id == SHARE_DATABASE_VERSION_V3) {
180 * Race condition
182 if (dbwrap_transaction_cancel(share_db)) {
183 smb_panic("transaction_cancel failed");
185 return true;
188 /* Move to at least V2. */
190 /* Cope with byte-reversed older versions of the db. */
191 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
192 /* Written on a bigendian machine with old fetch_int code. Save as le. */
194 status = dbwrap_store_int32(share_db, vstring,
195 SHARE_DATABASE_VERSION_V2);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
198 nt_errstr(status)));
199 goto cancel;
201 vers_id = SHARE_DATABASE_VERSION_V2;
204 if (vers_id != SHARE_DATABASE_VERSION_V2) {
205 status = dbwrap_traverse(share_db, delete_fn, NULL, NULL);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(0, ("traverse failed\n"));
208 goto cancel;
210 status = dbwrap_store_int32(share_db, vstring,
211 SHARE_DATABASE_VERSION_V2);
212 if (!NT_STATUS_IS_OK(status)) {
213 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
214 nt_errstr(status)));
215 goto cancel;
219 /* Finally upgrade to version 3, with canonicalized sharenames. */
221 status = dbwrap_traverse(share_db, upgrade_v2_to_v3, &upgrade_ok, NULL);
222 if (!NT_STATUS_IS_OK(status) || upgrade_ok == false) {
223 DEBUG(0, ("traverse failed\n"));
224 goto cancel;
226 status = dbwrap_store_int32(share_db, vstring,
227 SHARE_DATABASE_VERSION_V3);
228 if (!NT_STATUS_IS_OK(status)) {
229 DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
230 nt_errstr(status)));
231 goto cancel;
234 if (dbwrap_transaction_commit(share_db) != 0) {
235 DEBUG(0, ("transaction_commit failed\n"));
236 return false;
239 return true;
241 cancel:
242 if (dbwrap_transaction_cancel(share_db)) {
243 smb_panic("transaction_cancel failed");
246 return false;
249 /*******************************************************************
250 Fake up a Everyone, default access as a default.
251 def_access is a GENERIC_XXX access mode.
252 ********************************************************************/
254 struct security_descriptor *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
256 uint32_t sa;
257 struct security_ace ace;
258 struct security_acl *psa = NULL;
259 struct security_descriptor *psd = NULL;
260 uint32 spec_access = def_access;
262 se_map_generic(&spec_access, &file_generic_mapping);
264 sa = (def_access | spec_access );
265 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
267 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
268 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
269 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
270 psa, psize);
273 if (!psd) {
274 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
275 return NULL;
278 return psd;
281 /*******************************************************************
282 Pull a security descriptor from the share tdb.
283 ********************************************************************/
285 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
286 size_t *psize)
288 char *key;
289 struct security_descriptor *psd = NULL;
290 TDB_DATA data;
291 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
292 NTSTATUS status;
294 if (!c_servicename) {
295 return NULL;
298 if (!share_info_db_init()) {
299 TALLOC_FREE(c_servicename);
300 return NULL;
303 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
304 TALLOC_FREE(c_servicename);
305 DEBUG(0, ("talloc_asprintf failed\n"));
306 return NULL;
309 TALLOC_FREE(c_servicename);
311 status = dbwrap_fetch_bystring(share_db, talloc_tos(), key, &data);
313 TALLOC_FREE(key);
315 if (!NT_STATUS_IS_OK(status)) {
316 return get_share_security_default(ctx, psize,
317 SEC_RIGHTS_DIR_ALL);
320 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
322 TALLOC_FREE(data.dptr);
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
326 nt_errstr(status)));
327 return get_share_security_default(ctx, psize,
328 SEC_RIGHTS_DIR_ALL);
331 if (psd) {
332 *psize = ndr_size_security_descriptor(psd, 0);
333 } else {
334 return get_share_security_default(ctx, psize,
335 SEC_RIGHTS_DIR_ALL);
338 return psd;
341 /*******************************************************************
342 Store a security descriptor in the share db.
343 ********************************************************************/
345 bool set_share_security(const char *share_name, struct security_descriptor *psd)
347 TALLOC_CTX *frame = talloc_stackframe();
348 char *key;
349 bool ret = False;
350 TDB_DATA blob;
351 NTSTATUS status;
352 char *c_share_name = canonicalize_servicename(frame, share_name);
354 if (!c_share_name) {
355 goto out;
358 if (!share_info_db_init()) {
359 goto out;
362 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
364 if (!NT_STATUS_IS_OK(status)) {
365 DEBUG(0, ("marshall_sec_desc failed: %s\n",
366 nt_errstr(status)));
367 goto out;
370 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
371 DEBUG(0, ("talloc_asprintf failed\n"));
372 goto out;
375 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
376 TDB_REPLACE);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(1, ("set_share_security: Failed to store secdesc for "
379 "%s: %s\n", share_name, nt_errstr(status)));
380 goto out;
383 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
384 ret = True;
386 out:
387 TALLOC_FREE(frame);
388 return ret;
391 /*******************************************************************
392 Delete a security descriptor.
393 ********************************************************************/
395 bool delete_share_security(const char *servicename)
397 TDB_DATA kbuf;
398 char *key;
399 NTSTATUS status;
400 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
402 if (!c_servicename) {
403 return NULL;
406 if (!share_info_db_init()) {
407 TALLOC_FREE(c_servicename);
408 return False;
411 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
412 c_servicename))) {
413 TALLOC_FREE(c_servicename);
414 return False;
416 kbuf = string_term_tdb_data(key);
418 status = dbwrap_trans_delete(share_db, kbuf);
419 if (!NT_STATUS_IS_OK(status)) {
420 DEBUG(0, ("delete_share_security: Failed to delete entry for "
421 "share %s: %s\n", c_servicename, nt_errstr(status)));
422 TALLOC_FREE(c_servicename);
423 return False;
426 TALLOC_FREE(c_servicename);
427 return True;
430 /*******************************************************************
431 Can this user access with share with the required permissions ?
432 ********************************************************************/
434 bool share_access_check(const struct security_token *token,
435 const char *sharename,
436 uint32 desired_access,
437 uint32_t *pgranted)
439 uint32 granted;
440 NTSTATUS status;
441 struct security_descriptor *psd = NULL;
442 size_t sd_size;
444 psd = get_share_security(talloc_tos(), sharename, &sd_size);
446 if (!psd) {
447 if (pgranted != NULL) {
448 *pgranted = desired_access;
450 return false;
453 status = se_access_check(psd, token, desired_access, &granted);
455 TALLOC_FREE(psd);
457 if (pgranted != NULL) {
458 *pgranted = granted;
461 return NT_STATUS_IS_OK(status);
464 /***************************************************************************
465 Parse the contents of an acl string from a usershare file.
466 ***************************************************************************/
468 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
470 size_t s_size = 0;
471 const char *pacl = acl_str;
472 int num_aces = 0;
473 struct security_ace *ace_list = NULL;
474 struct security_acl *psa = NULL;
475 struct security_descriptor *psd = NULL;
476 size_t sd_size = 0;
477 int i;
479 *ppsd = NULL;
481 /* If the acl string is blank return "Everyone:R" */
482 if (!*acl_str) {
483 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
484 if (!default_psd) {
485 return False;
487 *ppsd = default_psd;
488 return True;
491 num_aces = 1;
493 /* Add the number of ',' characters to get the number of aces. */
494 num_aces += count_chars(pacl,',');
496 ace_list = talloc_array(ctx, struct security_ace, num_aces);
497 if (!ace_list) {
498 return False;
501 for (i = 0; i < num_aces; i++) {
502 uint32_t sa;
503 uint32 g_access;
504 uint32 s_access;
505 struct dom_sid sid;
506 char *sidstr;
507 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
509 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
510 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
511 "for ':' in string '%s'\n", pacl));
512 return False;
515 if (!string_to_sid(&sid, sidstr)) {
516 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
517 sidstr ));
518 return False;
521 switch (*pacl) {
522 case 'F': /* Full Control, ie. R+W */
523 case 'f': /* Full Control, ie. R+W */
524 s_access = g_access = GENERIC_ALL_ACCESS;
525 break;
526 case 'R': /* Read only. */
527 case 'r': /* Read only. */
528 s_access = g_access = GENERIC_READ_ACCESS;
529 break;
530 case 'D': /* Deny all to this SID. */
531 case 'd': /* Deny all to this SID. */
532 type = SEC_ACE_TYPE_ACCESS_DENIED;
533 s_access = g_access = GENERIC_ALL_ACCESS;
534 break;
535 default:
536 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
537 pacl ));
538 return False;
541 pacl++;
542 if (*pacl && *pacl != ',') {
543 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
544 pacl ));
545 return False;
547 pacl++; /* Go past any ',' */
549 se_map_generic(&s_access, &file_generic_mapping);
550 sa = (g_access | s_access);
551 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
554 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
555 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
556 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
557 psa, &sd_size);
560 if (!psd) {
561 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
562 return False;
565 *ppsd = psd;
566 return True;