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/>.
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)
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;
53 bool *p_upgrade_ok = (bool *)priv;
56 /* Is there space for a one character sharename ? */
57 if (rec->key.dsize <= prefix_len+2) {
61 /* Does it start with the share key prefix ? */
62 if (memcmp(rec->key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
67 /* Is it a null terminated string as a key ? */
68 if (rec->key.dptr[rec->key.dsize-1] != '\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);
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);
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;
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",
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),
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);
114 *p_upgrade_ok = false;
117 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
122 TALLOC_FREE(c_servicename);
127 bool share_info_db_init(void)
129 const char *vstring = "INFO/version";
132 bool upgrade_ok = true;
134 if (share_db != NULL) {
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) ));
146 vers_id = dbwrap_fetch_int32(share_db, vstring);
147 if (vers_id == SHARE_DATABASE_VERSION_V3) {
151 if (share_db->transaction_start(share_db) != 0) {
152 DEBUG(0, ("transaction_start failed\n"));
153 TALLOC_FREE(share_db);
157 vers_id = dbwrap_fetch_int32(share_db, vstring);
158 if (vers_id == SHARE_DATABASE_VERSION_V3) {
162 if (share_db->transaction_cancel(share_db)) {
163 smb_panic("transaction_cancel failed");
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"));
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);
185 DEBUG(0, ("traverse failed\n"));
188 if (dbwrap_store_int32(share_db, vstring,
189 SHARE_DATABASE_VERSION_V2) != 0) {
190 DEBUG(0, ("dbwrap_store_int32 failed\n"));
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"));
202 if (dbwrap_store_int32(share_db, vstring,
203 SHARE_DATABASE_VERSION_V3) != 0) {
204 DEBUG(0, ("dbwrap_store_int32 failed\n"));
208 if (share_db->transaction_commit(share_db) != 0) {
209 DEBUG(0, ("transaction_commit failed\n"));
216 if (share_db->transaction_cancel(share_db)) {
217 smb_panic("transaction_cancel failed");
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)
231 struct security_ace ace;
232 struct security_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,
248 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
255 /*******************************************************************
256 Pull a security descriptor from the share tdb.
257 ********************************************************************/
259 SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
263 SEC_DESC *psd = NULL;
265 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
268 if (!c_servicename) {
272 if (!share_info_db_init()) {
273 TALLOC_FREE(c_servicename);
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"));
283 TALLOC_FREE(c_servicename);
285 data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
289 if (data.dptr == NULL) {
290 return get_share_security_default(ctx, psize,
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",
301 return get_share_security_default(ctx, psize,
306 *psize = ndr_size_security_descriptor(psd, 0);
308 return get_share_security_default(ctx, psize,
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();
326 char *c_share_name = canonicalize_servicename(frame, share_name);
332 if (!share_info_db_init()) {
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",
344 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
345 DEBUG(0, ("talloc_asprintf failed\n"));
349 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
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)));
357 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
365 /*******************************************************************
366 Delete a security descriptor.
367 ********************************************************************/
369 bool delete_share_security(const char *servicename)
374 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
376 if (!c_servicename) {
380 if (!share_info_db_init()) {
381 TALLOC_FREE(c_servicename);
385 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
387 TALLOC_FREE(c_servicename);
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);
400 TALLOC_FREE(c_servicename);
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)
413 SEC_DESC *psd = NULL;
416 psd = get_share_security(talloc_tos(), sharename, &sd_size);
422 status = se_access_check(psd, token, desired_access, &granted);
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)
436 const char *pacl = acl_str;
438 struct security_ace *ace_list = NULL;
439 struct security_acl *psa = NULL;
440 SEC_DESC *psd = NULL;
446 /* If the acl string is blank return "Everyone:R" */
448 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
458 /* Add the number of ',' characters to get the number of aces. */
459 num_aces += count_chars(pacl,',');
461 ace_list = TALLOC_ARRAY(ctx, struct security_ace, num_aces);
466 for (i = 0; i < num_aces; i++) {
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));
480 if (!string_to_sid(&sid, sidstr)) {
481 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
487 case 'F': /* Full Control, ie. R+W */
488 case 'f': /* Full Control, ie. R+W */
489 s_access = g_access = GENERIC_ALL_ACCESS;
491 case 'R': /* Read only. */
492 case 'r': /* Read only. */
493 s_access = g_access = GENERIC_READ_ACCESS;
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;
501 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
507 if (*pacl && *pacl != ',') {
508 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
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,
526 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));