s4-waf: merge in the latest changes from master
[Samba/ekacnet.git] / source3 / lib / sharesec.c
blobeef5f39fc090405bb6da3f4f07af9a2791d60829
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"
22 /*******************************************************************
23 Create the share security tdb.
24 ********************************************************************/
26 static struct db_context *share_db; /* used for share security descriptors */
27 #define SHARE_DATABASE_VERSION_V1 1
28 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
29 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
31 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
32 /* Map generic permissions to file object specific permissions */
34 extern const struct generic_mapping file_generic_mapping;
36 static int delete_fn(struct db_record *rec, void *priv)
38 rec->delete_rec(rec);
39 return 0;
42 /*****************************************************
43 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
44 If we find one re-write it into a canonical case form.
45 *****************************************************/
47 static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
49 size_t prefix_len = strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR);
50 const char *servicename = NULL;
51 char *c_servicename = NULL;
52 char *newkey = NULL;
53 bool *p_upgrade_ok = (bool *)priv;
54 NTSTATUS status;
56 /* Is there space for a one character sharename ? */
57 if (rec->key.dsize <= prefix_len+2) {
58 return 0;
61 /* Does it start with the share key prefix ? */
62 if (memcmp(rec->key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
63 prefix_len) != 0) {
64 return 0;
67 /* Is it a null terminated string as a key ? */
68 if (rec->key.dptr[rec->key.dsize-1] != '\0') {
69 return 0;
72 /* Bytes after the prefix are the sharename string. */
73 servicename = (char *)&rec->key.dptr[prefix_len];
74 c_servicename = canonicalize_servicename(talloc_tos(), servicename);
75 if (!c_servicename) {
76 smb_panic("out of memory upgrading share security db from v2 -> v3");
79 if (strcmp(servicename, c_servicename) == 0) {
80 /* Old and new names match. No canonicalization needed. */
81 TALLOC_FREE(c_servicename);
82 return 0;
85 /* Oops. Need to canonicalize name, delete old then store new. */
86 status = rec->delete_rec(rec);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
89 "%s: %s\n", rec->key.dptr, nt_errstr(status)));
90 TALLOC_FREE(c_servicename);
91 *p_upgrade_ok = false;
92 return -1;
93 } else {
94 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
95 "%s\n", rec->key.dptr ));
98 if (!(newkey = talloc_asprintf(talloc_tos(),
99 SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
100 c_servicename))) {
101 smb_panic("out of memory upgrading share security db from v2 -> v3");
104 status = dbwrap_store(share_db,
105 string_term_tdb_data(newkey),
106 rec->value,
107 TDB_REPLACE);
109 if (!NT_STATUS_IS_OK(status)) {
110 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
111 "%s: %s\n", c_servicename, nt_errstr(status)));
112 TALLOC_FREE(c_servicename);
113 TALLOC_FREE(newkey);
114 *p_upgrade_ok = false;
115 return -1;
116 } else {
117 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
118 "%s\n", newkey ));
121 TALLOC_FREE(newkey);
122 TALLOC_FREE(c_servicename);
124 return 0;
127 bool share_info_db_init(void)
129 const char *vstring = "INFO/version";
130 int32 vers_id;
131 int ret;
132 bool upgrade_ok = true;
134 if (share_db != NULL) {
135 return True;
138 share_db = db_open(NULL, state_path("share_info.tdb"), 0,
139 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
140 if (share_db == NULL) {
141 DEBUG(0,("Failed to open share info database %s (%s)\n",
142 state_path("share_info.tdb"), strerror(errno) ));
143 return False;
146 vers_id = dbwrap_fetch_int32(share_db, vstring);
147 if (vers_id == SHARE_DATABASE_VERSION_V3) {
148 return true;
151 if (share_db->transaction_start(share_db) != 0) {
152 DEBUG(0, ("transaction_start failed\n"));
153 TALLOC_FREE(share_db);
154 return false;
157 vers_id = dbwrap_fetch_int32(share_db, vstring);
158 if (vers_id == SHARE_DATABASE_VERSION_V3) {
160 * Race condition
162 if (share_db->transaction_cancel(share_db)) {
163 smb_panic("transaction_cancel failed");
165 return true;
168 /* Move to at least V2. */
170 /* Cope with byte-reversed older versions of the db. */
171 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
172 /* Written on a bigendian machine with old fetch_int code. Save as le. */
174 if (dbwrap_store_int32(share_db, vstring,
175 SHARE_DATABASE_VERSION_V2) != 0) {
176 DEBUG(0, ("dbwrap_store_int32 failed\n"));
177 goto cancel;
179 vers_id = SHARE_DATABASE_VERSION_V2;
182 if (vers_id != SHARE_DATABASE_VERSION_V2) {
183 ret = share_db->traverse(share_db, delete_fn, NULL);
184 if (ret < 0) {
185 DEBUG(0, ("traverse failed\n"));
186 goto cancel;
188 if (dbwrap_store_int32(share_db, vstring,
189 SHARE_DATABASE_VERSION_V2) != 0) {
190 DEBUG(0, ("dbwrap_store_int32 failed\n"));
191 goto cancel;
195 /* Finally upgrade to version 3, with canonicalized sharenames. */
197 ret = share_db->traverse(share_db, upgrade_v2_to_v3, &upgrade_ok);
198 if (ret < 0 || upgrade_ok == false) {
199 DEBUG(0, ("traverse failed\n"));
200 goto cancel;
202 if (dbwrap_store_int32(share_db, vstring,
203 SHARE_DATABASE_VERSION_V3) != 0) {
204 DEBUG(0, ("dbwrap_store_int32 failed\n"));
205 goto cancel;
208 if (share_db->transaction_commit(share_db) != 0) {
209 DEBUG(0, ("transaction_commit failed\n"));
210 return false;
213 return true;
215 cancel:
216 if (share_db->transaction_cancel(share_db)) {
217 smb_panic("transaction_cancel failed");
220 return false;
223 /*******************************************************************
224 Fake up a Everyone, default access as a default.
225 def_access is a GENERIC_XXX access mode.
226 ********************************************************************/
228 SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
230 uint32_t sa;
231 SEC_ACE ace;
232 SEC_ACL *psa = NULL;
233 SEC_DESC *psd = NULL;
234 uint32 spec_access = def_access;
236 se_map_generic(&spec_access, &file_generic_mapping);
238 sa = (def_access | spec_access );
239 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
241 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
242 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
243 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
244 psa, psize);
247 if (!psd) {
248 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
249 return NULL;
252 return psd;
255 /*******************************************************************
256 Pull a security descriptor from the share tdb.
257 ********************************************************************/
259 SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
260 size_t *psize)
262 char *key;
263 SEC_DESC *psd = NULL;
264 TDB_DATA data;
265 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
266 NTSTATUS status;
268 if (!c_servicename) {
269 return NULL;
272 if (!share_info_db_init()) {
273 TALLOC_FREE(c_servicename);
274 return NULL;
277 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
278 TALLOC_FREE(c_servicename);
279 DEBUG(0, ("talloc_asprintf failed\n"));
280 return NULL;
283 TALLOC_FREE(c_servicename);
285 data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
287 TALLOC_FREE(key);
289 if (data.dptr == NULL) {
290 return get_share_security_default(ctx, psize,
291 GENERIC_ALL_ACCESS);
294 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
296 TALLOC_FREE(data.dptr);
298 if (!NT_STATUS_IS_OK(status)) {
299 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
300 nt_errstr(status)));
301 return get_share_security_default(ctx, psize,
302 GENERIC_ALL_ACCESS);
305 if (psd) {
306 *psize = ndr_size_security_descriptor(psd, NULL, 0);
307 } else {
308 return get_share_security_default(ctx, psize,
309 GENERIC_ALL_ACCESS);
312 return psd;
315 /*******************************************************************
316 Store a security descriptor in the share db.
317 ********************************************************************/
319 bool set_share_security(const char *share_name, SEC_DESC *psd)
321 TALLOC_CTX *frame = talloc_stackframe();
322 char *key;
323 bool ret = False;
324 TDB_DATA blob;
325 NTSTATUS status;
326 char *c_share_name = canonicalize_servicename(frame, share_name);
328 if (!c_share_name) {
329 goto out;
332 if (!share_info_db_init()) {
333 goto out;
336 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
338 if (!NT_STATUS_IS_OK(status)) {
339 DEBUG(0, ("marshall_sec_desc failed: %s\n",
340 nt_errstr(status)));
341 goto out;
344 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
345 DEBUG(0, ("talloc_asprintf failed\n"));
346 goto out;
349 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
350 TDB_REPLACE);
351 if (!NT_STATUS_IS_OK(status)) {
352 DEBUG(1, ("set_share_security: Failed to store secdesc for "
353 "%s: %s\n", share_name, nt_errstr(status)));
354 goto out;
357 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
358 ret = True;
360 out:
361 TALLOC_FREE(frame);
362 return ret;
365 /*******************************************************************
366 Delete a security descriptor.
367 ********************************************************************/
369 bool delete_share_security(const char *servicename)
371 TDB_DATA kbuf;
372 char *key;
373 NTSTATUS status;
374 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
376 if (!c_servicename) {
377 return NULL;
380 if (!share_info_db_init()) {
381 TALLOC_FREE(c_servicename);
382 return False;
385 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
386 c_servicename))) {
387 TALLOC_FREE(c_servicename);
388 return False;
390 kbuf = string_term_tdb_data(key);
392 status = dbwrap_trans_delete(share_db, kbuf);
393 if (!NT_STATUS_IS_OK(status)) {
394 DEBUG(0, ("delete_share_security: Failed to delete entry for "
395 "share %s: %s\n", c_servicename, nt_errstr(status)));
396 TALLOC_FREE(c_servicename);
397 return False;
400 TALLOC_FREE(c_servicename);
401 return True;
404 /*******************************************************************
405 Can this user access with share with the required permissions ?
406 ********************************************************************/
408 bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
409 uint32 desired_access)
411 uint32 granted;
412 NTSTATUS status;
413 SEC_DESC *psd = NULL;
414 size_t sd_size;
416 psd = get_share_security(talloc_tos(), sharename, &sd_size);
418 if (!psd) {
419 return True;
422 status = se_access_check(psd, token, desired_access, &granted);
424 TALLOC_FREE(psd);
426 return NT_STATUS_IS_OK(status);
429 /***************************************************************************
430 Parse the contents of an acl string from a usershare file.
431 ***************************************************************************/
433 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
435 size_t s_size = 0;
436 const char *pacl = acl_str;
437 int num_aces = 0;
438 SEC_ACE *ace_list = NULL;
439 SEC_ACL *psa = NULL;
440 SEC_DESC *psd = NULL;
441 size_t sd_size = 0;
442 int i;
444 *ppsd = NULL;
446 /* If the acl string is blank return "Everyone:R" */
447 if (!*acl_str) {
448 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
449 if (!default_psd) {
450 return False;
452 *ppsd = default_psd;
453 return True;
456 num_aces = 1;
458 /* Add the number of ',' characters to get the number of aces. */
459 num_aces += count_chars(pacl,',');
461 ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
462 if (!ace_list) {
463 return False;
466 for (i = 0; i < num_aces; i++) {
467 uint32_t sa;
468 uint32 g_access;
469 uint32 s_access;
470 DOM_SID sid;
471 char *sidstr;
472 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
474 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
475 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
476 "for ':' in string '%s'\n", pacl));
477 return False;
480 if (!string_to_sid(&sid, sidstr)) {
481 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
482 sidstr ));
483 return False;
486 switch (*pacl) {
487 case 'F': /* Full Control, ie. R+W */
488 case 'f': /* Full Control, ie. R+W */
489 s_access = g_access = GENERIC_ALL_ACCESS;
490 break;
491 case 'R': /* Read only. */
492 case 'r': /* Read only. */
493 s_access = g_access = GENERIC_READ_ACCESS;
494 break;
495 case 'D': /* Deny all to this SID. */
496 case 'd': /* Deny all to this SID. */
497 type = SEC_ACE_TYPE_ACCESS_DENIED;
498 s_access = g_access = GENERIC_ALL_ACCESS;
499 break;
500 default:
501 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
502 pacl ));
503 return False;
506 pacl++;
507 if (*pacl && *pacl != ',') {
508 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
509 pacl ));
510 return False;
512 pacl++; /* Go past any ',' */
514 se_map_generic(&s_access, &file_generic_mapping);
515 sa = (g_access | s_access);
516 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
519 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
520 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
521 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
522 psa, &sd_size);
525 if (!psd) {
526 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
527 return False;
530 *ppsd = psd;
531 return True;