s3: Don't announce readraw and writeraw with the async echo responder
[Samba/ekacnet.git] / source3 / lib / sharesec.c
blobf84c8c5f815677d1cf08886afb11ee6f993ef5f3
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 "../librpc/gen_ndr/ndr_security.h"
23 /*******************************************************************
24 Create the share security tdb.
25 ********************************************************************/
27 static struct db_context *share_db; /* used for share security descriptors */
28 #define SHARE_DATABASE_VERSION_V1 1
29 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
30 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
32 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
33 /* Map generic permissions to file object specific permissions */
35 extern const struct generic_mapping file_generic_mapping;
37 static int delete_fn(struct db_record *rec, void *priv)
39 rec->delete_rec(rec);
40 return 0;
43 /*****************************************************
44 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
45 If we find one re-write it into a canonical case form.
46 *****************************************************/
48 static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
50 size_t prefix_len = strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR);
51 const char *servicename = NULL;
52 char *c_servicename = NULL;
53 char *newkey = NULL;
54 bool *p_upgrade_ok = (bool *)priv;
55 NTSTATUS status;
57 /* Is there space for a one character sharename ? */
58 if (rec->key.dsize <= prefix_len+2) {
59 return 0;
62 /* Does it start with the share key prefix ? */
63 if (memcmp(rec->key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
64 prefix_len) != 0) {
65 return 0;
68 /* Is it a null terminated string as a key ? */
69 if (rec->key.dptr[rec->key.dsize-1] != '\0') {
70 return 0;
73 /* Bytes after the prefix are the sharename string. */
74 servicename = (char *)&rec->key.dptr[prefix_len];
75 c_servicename = canonicalize_servicename(talloc_tos(), servicename);
76 if (!c_servicename) {
77 smb_panic("out of memory upgrading share security db from v2 -> v3");
80 if (strcmp(servicename, c_servicename) == 0) {
81 /* Old and new names match. No canonicalization needed. */
82 TALLOC_FREE(c_servicename);
83 return 0;
86 /* Oops. Need to canonicalize name, delete old then store new. */
87 status = rec->delete_rec(rec);
88 if (!NT_STATUS_IS_OK(status)) {
89 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
90 "%s: %s\n", rec->key.dptr, nt_errstr(status)));
91 TALLOC_FREE(c_servicename);
92 *p_upgrade_ok = false;
93 return -1;
94 } else {
95 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
96 "%s\n", rec->key.dptr ));
99 if (!(newkey = talloc_asprintf(talloc_tos(),
100 SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
101 c_servicename))) {
102 smb_panic("out of memory upgrading share security db from v2 -> v3");
105 status = dbwrap_store(share_db,
106 string_term_tdb_data(newkey),
107 rec->value,
108 TDB_REPLACE);
110 if (!NT_STATUS_IS_OK(status)) {
111 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
112 "%s: %s\n", c_servicename, nt_errstr(status)));
113 TALLOC_FREE(c_servicename);
114 TALLOC_FREE(newkey);
115 *p_upgrade_ok = false;
116 return -1;
117 } else {
118 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
119 "%s\n", newkey ));
122 TALLOC_FREE(newkey);
123 TALLOC_FREE(c_servicename);
125 return 0;
128 bool share_info_db_init(void)
130 const char *vstring = "INFO/version";
131 int32 vers_id;
132 int ret;
133 bool upgrade_ok = true;
135 if (share_db != NULL) {
136 return True;
139 share_db = db_open(NULL, state_path("share_info.tdb"), 0,
140 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
141 if (share_db == NULL) {
142 DEBUG(0,("Failed to open share info database %s (%s)\n",
143 state_path("share_info.tdb"), strerror(errno) ));
144 return False;
147 vers_id = dbwrap_fetch_int32(share_db, vstring);
148 if (vers_id == SHARE_DATABASE_VERSION_V3) {
149 return true;
152 if (share_db->transaction_start(share_db) != 0) {
153 DEBUG(0, ("transaction_start failed\n"));
154 TALLOC_FREE(share_db);
155 return false;
158 vers_id = dbwrap_fetch_int32(share_db, vstring);
159 if (vers_id == SHARE_DATABASE_VERSION_V3) {
161 * Race condition
163 if (share_db->transaction_cancel(share_db)) {
164 smb_panic("transaction_cancel failed");
166 return true;
169 /* Move to at least V2. */
171 /* Cope with byte-reversed older versions of the db. */
172 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
173 /* Written on a bigendian machine with old fetch_int code. Save as le. */
175 if (dbwrap_store_int32(share_db, vstring,
176 SHARE_DATABASE_VERSION_V2) != 0) {
177 DEBUG(0, ("dbwrap_store_int32 failed\n"));
178 goto cancel;
180 vers_id = SHARE_DATABASE_VERSION_V2;
183 if (vers_id != SHARE_DATABASE_VERSION_V2) {
184 ret = share_db->traverse(share_db, delete_fn, NULL);
185 if (ret < 0) {
186 DEBUG(0, ("traverse failed\n"));
187 goto cancel;
189 if (dbwrap_store_int32(share_db, vstring,
190 SHARE_DATABASE_VERSION_V2) != 0) {
191 DEBUG(0, ("dbwrap_store_int32 failed\n"));
192 goto cancel;
196 /* Finally upgrade to version 3, with canonicalized sharenames. */
198 ret = share_db->traverse(share_db, upgrade_v2_to_v3, &upgrade_ok);
199 if (ret < 0 || upgrade_ok == false) {
200 DEBUG(0, ("traverse failed\n"));
201 goto cancel;
203 if (dbwrap_store_int32(share_db, vstring,
204 SHARE_DATABASE_VERSION_V3) != 0) {
205 DEBUG(0, ("dbwrap_store_int32 failed\n"));
206 goto cancel;
209 if (share_db->transaction_commit(share_db) != 0) {
210 DEBUG(0, ("transaction_commit failed\n"));
211 return false;
214 return true;
216 cancel:
217 if (share_db->transaction_cancel(share_db)) {
218 smb_panic("transaction_cancel failed");
221 return false;
224 /*******************************************************************
225 Fake up a Everyone, default access as a default.
226 def_access is a GENERIC_XXX access mode.
227 ********************************************************************/
229 struct security_descriptor *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
231 uint32_t sa;
232 struct security_ace ace;
233 struct security_acl *psa = NULL;
234 struct security_descriptor *psd = NULL;
235 uint32 spec_access = def_access;
237 se_map_generic(&spec_access, &file_generic_mapping);
239 sa = (def_access | spec_access );
240 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
242 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
243 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
244 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
245 psa, psize);
248 if (!psd) {
249 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
250 return NULL;
253 return psd;
256 /*******************************************************************
257 Pull a security descriptor from the share tdb.
258 ********************************************************************/
260 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
261 size_t *psize)
263 char *key;
264 struct security_descriptor *psd = NULL;
265 TDB_DATA data;
266 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
267 NTSTATUS status;
269 if (!c_servicename) {
270 return NULL;
273 if (!share_info_db_init()) {
274 TALLOC_FREE(c_servicename);
275 return NULL;
278 if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
279 TALLOC_FREE(c_servicename);
280 DEBUG(0, ("talloc_asprintf failed\n"));
281 return NULL;
284 TALLOC_FREE(c_servicename);
286 data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
288 TALLOC_FREE(key);
290 if (data.dptr == NULL) {
291 return get_share_security_default(ctx, psize,
292 GENERIC_ALL_ACCESS);
295 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
297 TALLOC_FREE(data.dptr);
299 if (!NT_STATUS_IS_OK(status)) {
300 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
301 nt_errstr(status)));
302 return get_share_security_default(ctx, psize,
303 GENERIC_ALL_ACCESS);
306 if (psd) {
307 *psize = ndr_size_security_descriptor(psd, 0);
308 } else {
309 return get_share_security_default(ctx, psize,
310 GENERIC_ALL_ACCESS);
313 return psd;
316 /*******************************************************************
317 Store a security descriptor in the share db.
318 ********************************************************************/
320 bool set_share_security(const char *share_name, struct security_descriptor *psd)
322 TALLOC_CTX *frame = talloc_stackframe();
323 char *key;
324 bool ret = False;
325 TDB_DATA blob;
326 NTSTATUS status;
327 char *c_share_name = canonicalize_servicename(frame, share_name);
329 if (!c_share_name) {
330 goto out;
333 if (!share_info_db_init()) {
334 goto out;
337 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(0, ("marshall_sec_desc failed: %s\n",
341 nt_errstr(status)));
342 goto out;
345 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
346 DEBUG(0, ("talloc_asprintf failed\n"));
347 goto out;
350 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
351 TDB_REPLACE);
352 if (!NT_STATUS_IS_OK(status)) {
353 DEBUG(1, ("set_share_security: Failed to store secdesc for "
354 "%s: %s\n", share_name, nt_errstr(status)));
355 goto out;
358 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
359 ret = True;
361 out:
362 TALLOC_FREE(frame);
363 return ret;
366 /*******************************************************************
367 Delete a security descriptor.
368 ********************************************************************/
370 bool delete_share_security(const char *servicename)
372 TDB_DATA kbuf;
373 char *key;
374 NTSTATUS status;
375 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
377 if (!c_servicename) {
378 return NULL;
381 if (!share_info_db_init()) {
382 TALLOC_FREE(c_servicename);
383 return False;
386 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
387 c_servicename))) {
388 TALLOC_FREE(c_servicename);
389 return False;
391 kbuf = string_term_tdb_data(key);
393 status = dbwrap_trans_delete(share_db, kbuf);
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(0, ("delete_share_security: Failed to delete entry for "
396 "share %s: %s\n", c_servicename, nt_errstr(status)));
397 TALLOC_FREE(c_servicename);
398 return False;
401 TALLOC_FREE(c_servicename);
402 return True;
405 /*******************************************************************
406 Can this user access with share with the required permissions ?
407 ********************************************************************/
409 bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
410 uint32 desired_access)
412 uint32 granted;
413 NTSTATUS status;
414 struct security_descriptor *psd = NULL;
415 size_t sd_size;
417 psd = get_share_security(talloc_tos(), sharename, &sd_size);
419 if (!psd) {
420 return True;
423 status = se_access_check(psd, token, desired_access, &granted);
425 TALLOC_FREE(psd);
427 return NT_STATUS_IS_OK(status);
430 /***************************************************************************
431 Parse the contents of an acl string from a usershare file.
432 ***************************************************************************/
434 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
436 size_t s_size = 0;
437 const char *pacl = acl_str;
438 int num_aces = 0;
439 struct security_ace *ace_list = NULL;
440 struct security_acl *psa = NULL;
441 struct security_descriptor *psd = NULL;
442 size_t sd_size = 0;
443 int i;
445 *ppsd = NULL;
447 /* If the acl string is blank return "Everyone:R" */
448 if (!*acl_str) {
449 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
450 if (!default_psd) {
451 return False;
453 *ppsd = default_psd;
454 return True;
457 num_aces = 1;
459 /* Add the number of ',' characters to get the number of aces. */
460 num_aces += count_chars(pacl,',');
462 ace_list = TALLOC_ARRAY(ctx, struct security_ace, num_aces);
463 if (!ace_list) {
464 return False;
467 for (i = 0; i < num_aces; i++) {
468 uint32_t sa;
469 uint32 g_access;
470 uint32 s_access;
471 struct dom_sid sid;
472 char *sidstr;
473 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
475 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
476 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
477 "for ':' in string '%s'\n", pacl));
478 return False;
481 if (!string_to_sid(&sid, sidstr)) {
482 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
483 sidstr ));
484 return False;
487 switch (*pacl) {
488 case 'F': /* Full Control, ie. R+W */
489 case 'f': /* Full Control, ie. R+W */
490 s_access = g_access = GENERIC_ALL_ACCESS;
491 break;
492 case 'R': /* Read only. */
493 case 'r': /* Read only. */
494 s_access = g_access = GENERIC_READ_ACCESS;
495 break;
496 case 'D': /* Deny all to this SID. */
497 case 'd': /* Deny all to this SID. */
498 type = SEC_ACE_TYPE_ACCESS_DENIED;
499 s_access = g_access = GENERIC_ALL_ACCESS;
500 break;
501 default:
502 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
503 pacl ));
504 return False;
507 pacl++;
508 if (*pacl && *pacl != ',') {
509 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
510 pacl ));
511 return False;
513 pacl++; /* Go past any ',' */
515 se_map_generic(&s_access, &file_generic_mapping);
516 sa = (g_access | s_access);
517 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
520 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
521 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
522 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
523 psa, &sd_size);
526 if (!psd) {
527 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
528 return False;
531 *ppsd = psd;
532 return True;