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/>.
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"
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
);
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
;
60 bool *p_upgrade_ok
= (bool *)priv
;
65 key
= dbwrap_record_get_key(rec
);
67 /* Is there space for a one character sharename ? */
68 if (key
.dsize
<= prefix_len
+2) {
72 /* Does it start with the share key prefix ? */
73 if (memcmp(key
.dptr
, SHARE_SECURITY_DB_KEY_PREFIX_STR
,
78 /* Is it a null terminated string as a key ? */
79 if (key
.dptr
[key
.dsize
-1] != '\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
);
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
);
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
,
102 TALLOC_FREE(c_servicename
);
103 *p_upgrade_ok
= false;
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",
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
),
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
);
127 *p_upgrade_ok
= false;
130 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
135 TALLOC_FREE(c_servicename
);
140 NTSTATUS
share_info_db_init(void)
142 const char *vstring
= "INFO/version";
144 bool upgrade_ok
= true;
148 if (share_db
!= NULL
) {
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
)) {
173 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
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
)) {
188 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
192 if (dbwrap_transaction_cancel(share_db
)) {
193 smb_panic("transaction_cancel failed");
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",
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"));
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",
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"));
237 DBG_ERR("upgrade failed.\n");
238 status
= NT_STATUS_INTERNAL_ERROR
;
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",
250 if (dbwrap_transaction_commit(share_db
) != 0) {
251 DEBUG(0, ("transaction_commit failed\n"));
252 return NT_STATUS_INTERNAL_ERROR
;
258 if (dbwrap_transaction_cancel(share_db
)) {
259 smb_panic("transaction_cancel failed");
265 /*******************************************************************
266 Fake up a Everyone, default access as a default.
267 def_access is a GENERIC_XXX access mode.
268 ********************************************************************/
270 struct security_descriptor
*get_share_security_default( TALLOC_CTX
*ctx
, size_t *psize
, uint32_t def_access
)
273 struct security_ace ace
;
274 struct security_acl
*psa
= NULL
;
275 struct security_descriptor
*psd
= NULL
;
276 uint32_t spec_access
= def_access
;
278 se_map_generic(&spec_access
, &file_generic_mapping
);
280 sa
= (def_access
| spec_access
);
281 init_sec_ace(&ace
, &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, sa
, 0);
283 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, 1, &ace
)) != NULL
) {
284 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
285 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
290 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
297 /*******************************************************************
298 Pull a security descriptor from the share tdb.
299 ********************************************************************/
301 struct security_descriptor
*get_share_security( TALLOC_CTX
*ctx
, const char *servicename
,
305 struct security_descriptor
*psd
= NULL
;
307 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
310 if (!c_servicename
) {
314 status
= share_info_db_init();
315 if (!NT_STATUS_IS_OK(status
)) {
316 TALLOC_FREE(c_servicename
);
320 if (!(key
= talloc_asprintf(ctx
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_servicename
))) {
321 TALLOC_FREE(c_servicename
);
322 DEBUG(0, ("talloc_asprintf failed\n"));
326 TALLOC_FREE(c_servicename
);
328 status
= dbwrap_fetch_bystring(share_db
, talloc_tos(), key
, &data
);
332 if (!NT_STATUS_IS_OK(status
)) {
333 return get_share_security_default(ctx
, psize
,
337 status
= unmarshall_sec_desc(ctx
, data
.dptr
, data
.dsize
, &psd
);
339 TALLOC_FREE(data
.dptr
);
341 if (!NT_STATUS_IS_OK(status
)) {
342 return get_share_security_default(ctx
, psize
,
347 *psize
= ndr_size_security_descriptor(psd
, 0);
349 return get_share_security_default(ctx
, psize
,
356 /*******************************************************************
357 Store a security descriptor in the share db.
358 ********************************************************************/
360 NTSTATUS
set_share_security(const char *share_name
,
361 struct security_descriptor
*psd
)
363 TALLOC_CTX
*frame
= talloc_stackframe();
367 char *c_share_name
= canonicalize_servicename(frame
, share_name
);
369 if (c_share_name
== NULL
) {
370 status
= NT_STATUS_INVALID_PARAMETER
;
374 status
= share_info_db_init();
375 if (!NT_STATUS_IS_OK(status
)) {
379 status
= marshall_sec_desc(frame
, psd
, &blob
.dptr
, &blob
.dsize
);
381 if (!NT_STATUS_IS_OK(status
)) {
382 DEBUG(0, ("marshall_sec_desc failed: %s\n",
387 if (!(key
= talloc_asprintf(frame
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_share_name
))) {
388 DEBUG(0, ("talloc_asprintf failed\n"));
389 status
= NT_STATUS_NO_MEMORY
;
393 status
= dbwrap_trans_store(share_db
, string_term_tdb_data(key
), blob
,
395 if (!NT_STATUS_IS_OK(status
)) {
396 DEBUG(1, ("set_share_security: Failed to store secdesc for "
397 "%s: %s\n", share_name
, nt_errstr(status
)));
401 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name
));
402 status
= NT_STATUS_OK
;
409 /*******************************************************************
410 Delete a security descriptor.
411 ********************************************************************/
413 NTSTATUS
delete_share_security(const char *servicename
)
418 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
420 if (c_servicename
== NULL
) {
421 return NT_STATUS_INVALID_PARAMETER
;
424 status
= share_info_db_init();
425 if (!NT_STATUS_IS_OK(status
)) {
426 TALLOC_FREE(c_servicename
);
430 if (!(key
= talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
432 TALLOC_FREE(c_servicename
);
433 return NT_STATUS_NO_MEMORY
;
435 kbuf
= string_term_tdb_data(key
);
437 status
= dbwrap_trans_delete(share_db
, kbuf
);
438 if (!NT_STATUS_IS_OK(status
)) {
439 DEBUG(0, ("delete_share_security: Failed to delete entry for "
440 "share %s: %s\n", c_servicename
, nt_errstr(status
)));
441 TALLOC_FREE(c_servicename
);
445 TALLOC_FREE(c_servicename
);
449 /*******************************************************************
450 Can this user access with share with the required permissions ?
451 ********************************************************************/
453 bool share_access_check(const struct security_token
*token
,
454 const char *sharename
,
455 uint32_t desired_access
,
460 struct security_descriptor
*psd
= NULL
;
463 psd
= get_share_security(talloc_tos(), sharename
, &sd_size
);
466 if (pgranted
!= NULL
) {
467 *pgranted
= desired_access
;
472 status
= se_file_access_check(psd
, token
, true, desired_access
, &granted
);
476 if (pgranted
!= NULL
) {
480 return NT_STATUS_IS_OK(status
);
483 /***************************************************************************
484 Parse the contents of an acl string from a usershare file.
485 ***************************************************************************/
487 bool parse_usershare_acl(TALLOC_CTX
*ctx
, const char *acl_str
, struct security_descriptor
**ppsd
)
490 const char *pacl
= acl_str
;
492 struct security_ace
*ace_list
= NULL
;
493 struct security_acl
*psa
= NULL
;
494 struct security_descriptor
*psd
= NULL
;
500 /* If the acl string is blank return "Everyone:R" */
502 struct security_descriptor
*default_psd
= get_share_security_default(ctx
, &s_size
, GENERIC_READ_ACCESS
);
512 /* Add the number of ',' characters to get the number of aces. */
513 num_aces
+= count_chars(pacl
,',');
515 ace_list
= talloc_array(ctx
, struct security_ace
, num_aces
);
520 for (i
= 0; i
< num_aces
; i
++) {
526 enum security_ace_type type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
528 if (!next_token_talloc(ctx
, &pacl
, &sidstr
, ":")) {
529 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
530 "for ':' in string '%s'\n", pacl
));
534 if (!string_to_sid(&sid
, sidstr
)) {
535 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
541 case 'F': /* Full Control, ie. R+W */
542 case 'f': /* Full Control, ie. R+W */
543 s_access
= g_access
= GENERIC_ALL_ACCESS
;
545 case 'R': /* Read only. */
546 case 'r': /* Read only. */
547 s_access
= g_access
= GENERIC_READ_ACCESS
;
549 case 'D': /* Deny all to this SID. */
550 case 'd': /* Deny all to this SID. */
551 type
= SEC_ACE_TYPE_ACCESS_DENIED
;
552 s_access
= g_access
= GENERIC_ALL_ACCESS
;
555 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
561 if (*pacl
&& *pacl
!= ',') {
562 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
566 pacl
++; /* Go past any ',' */
568 se_map_generic(&s_access
, &file_generic_mapping
);
569 sa
= (g_access
| s_access
);
570 init_sec_ace(&ace_list
[i
], &sid
, type
, sa
, 0);
573 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, num_aces
, ace_list
)) != NULL
) {
574 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
575 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
580 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));