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 static struct security_descriptor
*get_share_security_default(TALLOC_CTX
*ctx
,
275 struct security_ace ace
= {
278 struct security_acl
*psa
= NULL
;
279 struct security_descriptor
*psd
= NULL
;
280 uint32_t spec_access
= def_access
;
282 se_map_generic(&spec_access
, &file_generic_mapping
);
284 sa
= (def_access
| spec_access
);
285 init_sec_ace(&ace
, &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, sa
, 0);
287 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, 1, &ace
)) != NULL
) {
288 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
289 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
294 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
301 /*******************************************************************
302 Pull a security descriptor from the share tdb.
303 ********************************************************************/
305 struct security_descriptor
*get_share_security( TALLOC_CTX
*ctx
, const char *servicename
,
309 struct security_descriptor
*psd
= NULL
;
311 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
314 if (!c_servicename
) {
318 status
= share_info_db_init();
319 if (!NT_STATUS_IS_OK(status
)) {
320 TALLOC_FREE(c_servicename
);
324 if (!(key
= talloc_asprintf(ctx
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_servicename
))) {
325 TALLOC_FREE(c_servicename
);
326 DEBUG(0, ("talloc_asprintf failed\n"));
330 TALLOC_FREE(c_servicename
);
332 status
= dbwrap_fetch_bystring(share_db
, talloc_tos(), key
, &data
);
336 if (!NT_STATUS_IS_OK(status
)) {
337 return get_share_security_default(ctx
, psize
,
341 status
= unmarshall_sec_desc(ctx
, data
.dptr
, data
.dsize
, &psd
);
343 TALLOC_FREE(data
.dptr
);
345 if (!NT_STATUS_IS_OK(status
)) {
346 return get_share_security_default(ctx
, psize
,
351 *psize
= ndr_size_security_descriptor(psd
, 0);
353 return get_share_security_default(ctx
, psize
,
360 /*******************************************************************
361 Store a security descriptor in the share db.
362 ********************************************************************/
364 NTSTATUS
set_share_security(const char *share_name
,
365 struct security_descriptor
*psd
)
367 TALLOC_CTX
*frame
= talloc_stackframe();
371 char *c_share_name
= canonicalize_servicename(frame
, share_name
);
373 if (c_share_name
== NULL
) {
374 status
= NT_STATUS_INVALID_PARAMETER
;
378 status
= share_info_db_init();
379 if (!NT_STATUS_IS_OK(status
)) {
383 status
= marshall_sec_desc(frame
, psd
, &blob
.dptr
, &blob
.dsize
);
385 if (!NT_STATUS_IS_OK(status
)) {
386 DEBUG(0, ("marshall_sec_desc failed: %s\n",
391 if (!(key
= talloc_asprintf(frame
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_share_name
))) {
392 DEBUG(0, ("talloc_asprintf failed\n"));
393 status
= NT_STATUS_NO_MEMORY
;
397 status
= dbwrap_trans_store(share_db
, string_term_tdb_data(key
), blob
,
399 if (!NT_STATUS_IS_OK(status
)) {
400 DEBUG(1, ("set_share_security: Failed to store secdesc for "
401 "%s: %s\n", share_name
, nt_errstr(status
)));
405 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name
));
406 status
= NT_STATUS_OK
;
413 /*******************************************************************
414 Delete a security descriptor.
415 ********************************************************************/
417 NTSTATUS
delete_share_security(const char *servicename
)
422 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
424 if (c_servicename
== NULL
) {
425 return NT_STATUS_INVALID_PARAMETER
;
428 status
= share_info_db_init();
429 if (!NT_STATUS_IS_OK(status
)) {
430 TALLOC_FREE(c_servicename
);
434 if (!(key
= talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
436 TALLOC_FREE(c_servicename
);
437 return NT_STATUS_NO_MEMORY
;
439 kbuf
= string_term_tdb_data(key
);
441 status
= dbwrap_trans_delete(share_db
, kbuf
);
442 if (!NT_STATUS_IS_OK(status
)) {
443 DEBUG(0, ("delete_share_security: Failed to delete entry for "
444 "share %s: %s\n", c_servicename
, nt_errstr(status
)));
445 TALLOC_FREE(c_servicename
);
449 TALLOC_FREE(c_servicename
);
453 /*******************************************************************
454 Can this user access with share with the required permissions ?
455 ********************************************************************/
457 bool share_access_check(const struct security_token
*token
,
458 const char *sharename
,
459 uint32_t desired_access
,
464 struct security_descriptor
*psd
= NULL
;
467 psd
= get_share_security(talloc_tos(), sharename
, &sd_size
);
470 if (pgranted
!= NULL
) {
471 *pgranted
= desired_access
;
476 status
= se_file_access_check(psd
, token
, true, desired_access
, &granted
);
480 if (pgranted
!= NULL
) {
484 return NT_STATUS_IS_OK(status
);
487 /***************************************************************************
488 Parse the contents of an acl string from a usershare file.
489 ***************************************************************************/
491 bool parse_usershare_acl(TALLOC_CTX
*ctx
, const char *acl_str
, struct security_descriptor
**ppsd
)
494 const char *pacl
= acl_str
;
496 struct security_ace
*ace_list
= NULL
;
497 struct security_acl
*psa
= NULL
;
498 struct security_descriptor
*psd
= NULL
;
504 /* If the acl string is blank return "Everyone:R" */
506 struct security_descriptor
*default_psd
= get_share_security_default(ctx
, &s_size
, GENERIC_READ_ACCESS
);
516 /* Add the number of ',' characters to get the number of aces. */
517 num_aces
+= count_chars(pacl
,',');
519 ace_list
= talloc_array(ctx
, struct security_ace
, num_aces
);
524 for (i
= 0; i
< num_aces
; i
++) {
530 enum security_ace_type type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
532 if (!next_token_talloc(ctx
, &pacl
, &sidstr
, ":")) {
533 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
534 "for ':' in string '%s'\n", pacl
));
538 if (!string_to_sid(&sid
, sidstr
)) {
539 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
545 case 'F': /* Full Control, ie. R+W */
546 case 'f': /* Full Control, ie. R+W */
547 s_access
= g_access
= GENERIC_ALL_ACCESS
;
549 case 'R': /* Read only. */
550 case 'r': /* Read only. */
551 s_access
= g_access
= GENERIC_READ_ACCESS
;
553 case 'D': /* Deny all to this SID. */
554 case 'd': /* Deny all to this SID. */
555 type
= SEC_ACE_TYPE_ACCESS_DENIED
;
556 s_access
= g_access
= GENERIC_ALL_ACCESS
;
559 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
565 if (*pacl
&& *pacl
!= ',') {
566 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
570 pacl
++; /* Go past any ',' */
572 se_map_generic(&s_access
, &file_generic_mapping
);
573 sa
= (g_access
| s_access
);
574 init_sec_ace(&ace_list
[i
], &sid
, type
, sa
, 0);
577 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, num_aces
, ace_list
)) != NULL
) {
578 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
579 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
584 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));