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"
28 /*******************************************************************
29 Create the share security tdb.
30 ********************************************************************/
32 static struct db_context
*share_db
; /* used for share security descriptors */
33 #define SHARE_DATABASE_VERSION_V1 1
34 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
35 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
37 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
38 /* Map generic permissions to file object specific permissions */
40 extern const struct generic_mapping file_generic_mapping
;
42 static int delete_fn(struct db_record
*rec
, void *priv
)
44 dbwrap_record_delete(rec
);
48 /*****************************************************
49 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
50 If we find one re-write it into a canonical case form.
51 *****************************************************/
53 static int upgrade_v2_to_v3(struct db_record
*rec
, void *priv
)
55 size_t prefix_len
= strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR
);
56 const char *servicename
= NULL
;
57 char *c_servicename
= NULL
;
59 bool *p_upgrade_ok
= (bool *)priv
;
64 key
= dbwrap_record_get_key(rec
);
66 /* Is there space for a one character sharename ? */
67 if (key
.dsize
<= prefix_len
+2) {
71 /* Does it start with the share key prefix ? */
72 if (memcmp(key
.dptr
, SHARE_SECURITY_DB_KEY_PREFIX_STR
,
77 /* Is it a null terminated string as a key ? */
78 if (key
.dptr
[key
.dsize
-1] != '\0') {
82 /* Bytes after the prefix are the sharename string. */
83 servicename
= (char *)&key
.dptr
[prefix_len
];
84 c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
86 smb_panic("out of memory upgrading share security db from v2 -> v3");
89 if (strcmp(servicename
, c_servicename
) == 0) {
90 /* Old and new names match. No canonicalization needed. */
91 TALLOC_FREE(c_servicename
);
95 /* Oops. Need to canonicalize name, delete old then store new. */
96 status
= dbwrap_record_delete(rec
);
97 if (!NT_STATUS_IS_OK(status
)) {
98 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
99 "%s: %s\n", (const char *)key
.dptr
,
101 TALLOC_FREE(c_servicename
);
102 *p_upgrade_ok
= false;
105 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
106 "%s\n", (const char *)key
.dptr
));
109 if (!(newkey
= talloc_asprintf(talloc_tos(),
110 SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
112 smb_panic("out of memory upgrading share security db from v2 -> v3");
115 value
= dbwrap_record_get_value(rec
);
116 status
= dbwrap_store(share_db
,
117 string_term_tdb_data(newkey
),
121 if (!NT_STATUS_IS_OK(status
)) {
122 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
123 "%s: %s\n", c_servicename
, nt_errstr(status
)));
124 TALLOC_FREE(c_servicename
);
126 *p_upgrade_ok
= false;
129 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
134 TALLOC_FREE(c_servicename
);
139 bool share_info_db_init(void)
141 const char *vstring
= "INFO/version";
143 bool upgrade_ok
= true;
146 if (share_db
!= NULL
) {
150 share_db
= db_open(NULL
, state_path("share_info.tdb"), 0,
151 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
152 if (share_db
== NULL
) {
153 DEBUG(0,("Failed to open share info database %s (%s)\n",
154 state_path("share_info.tdb"), strerror(errno
) ));
158 status
= dbwrap_fetch_int32(share_db
, vstring
, &vers_id
);
159 if (!NT_STATUS_IS_OK(status
)) {
163 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
167 if (dbwrap_transaction_start(share_db
) != 0) {
168 DEBUG(0, ("transaction_start failed\n"));
169 TALLOC_FREE(share_db
);
173 status
= dbwrap_fetch_int32(share_db
, vstring
, &vers_id
);
174 if (!NT_STATUS_IS_OK(status
)) {
178 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
182 if (dbwrap_transaction_cancel(share_db
)) {
183 smb_panic("transaction_cancel failed");
188 /* Move to at least V2. */
190 /* Cope with byte-reversed older versions of the db. */
191 if ((vers_id
== SHARE_DATABASE_VERSION_V1
) || (IREV(vers_id
) == SHARE_DATABASE_VERSION_V1
)) {
192 /* Written on a bigendian machine with old fetch_int code. Save as le. */
194 if (dbwrap_store_int32(share_db
, vstring
,
195 SHARE_DATABASE_VERSION_V2
) != 0) {
196 DEBUG(0, ("dbwrap_store_int32 failed\n"));
199 vers_id
= SHARE_DATABASE_VERSION_V2
;
202 if (vers_id
!= SHARE_DATABASE_VERSION_V2
) {
203 status
= dbwrap_traverse(share_db
, delete_fn
, NULL
, NULL
);
204 if (!NT_STATUS_IS_OK(status
)) {
205 DEBUG(0, ("traverse failed\n"));
208 if (dbwrap_store_int32(share_db
, vstring
,
209 SHARE_DATABASE_VERSION_V2
) != 0) {
210 DEBUG(0, ("dbwrap_store_int32 failed\n"));
215 /* Finally upgrade to version 3, with canonicalized sharenames. */
217 status
= dbwrap_traverse(share_db
, upgrade_v2_to_v3
, &upgrade_ok
, NULL
);
218 if (!NT_STATUS_IS_OK(status
) || upgrade_ok
== false) {
219 DEBUG(0, ("traverse failed\n"));
222 if (dbwrap_store_int32(share_db
, vstring
,
223 SHARE_DATABASE_VERSION_V3
) != 0) {
224 DEBUG(0, ("dbwrap_store_int32 failed\n"));
228 if (dbwrap_transaction_commit(share_db
) != 0) {
229 DEBUG(0, ("transaction_commit failed\n"));
236 if (dbwrap_transaction_cancel(share_db
)) {
237 smb_panic("transaction_cancel failed");
243 /*******************************************************************
244 Fake up a Everyone, default access as a default.
245 def_access is a GENERIC_XXX access mode.
246 ********************************************************************/
248 struct security_descriptor
*get_share_security_default( TALLOC_CTX
*ctx
, size_t *psize
, uint32 def_access
)
251 struct security_ace ace
;
252 struct security_acl
*psa
= NULL
;
253 struct security_descriptor
*psd
= NULL
;
254 uint32 spec_access
= def_access
;
256 se_map_generic(&spec_access
, &file_generic_mapping
);
258 sa
= (def_access
| spec_access
);
259 init_sec_ace(&ace
, &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, sa
, 0);
261 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, 1, &ace
)) != NULL
) {
262 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
263 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
268 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
275 /*******************************************************************
276 Pull a security descriptor from the share tdb.
277 ********************************************************************/
279 struct security_descriptor
*get_share_security( TALLOC_CTX
*ctx
, const char *servicename
,
283 struct security_descriptor
*psd
= NULL
;
285 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
288 if (!c_servicename
) {
292 if (!share_info_db_init()) {
293 TALLOC_FREE(c_servicename
);
297 if (!(key
= talloc_asprintf(ctx
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_servicename
))) {
298 TALLOC_FREE(c_servicename
);
299 DEBUG(0, ("talloc_asprintf failed\n"));
303 TALLOC_FREE(c_servicename
);
305 status
= dbwrap_fetch_bystring(share_db
, talloc_tos(), key
, &data
);
309 if (!NT_STATUS_IS_OK(status
)) {
310 return get_share_security_default(ctx
, psize
,
314 status
= unmarshall_sec_desc(ctx
, data
.dptr
, data
.dsize
, &psd
);
316 TALLOC_FREE(data
.dptr
);
318 if (!NT_STATUS_IS_OK(status
)) {
319 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
321 return get_share_security_default(ctx
, psize
,
326 *psize
= ndr_size_security_descriptor(psd
, 0);
328 return get_share_security_default(ctx
, psize
,
335 /*******************************************************************
336 Store a security descriptor in the share db.
337 ********************************************************************/
339 bool set_share_security(const char *share_name
, struct security_descriptor
*psd
)
341 TALLOC_CTX
*frame
= talloc_stackframe();
346 char *c_share_name
= canonicalize_servicename(frame
, share_name
);
352 if (!share_info_db_init()) {
356 status
= marshall_sec_desc(frame
, psd
, &blob
.dptr
, &blob
.dsize
);
358 if (!NT_STATUS_IS_OK(status
)) {
359 DEBUG(0, ("marshall_sec_desc failed: %s\n",
364 if (!(key
= talloc_asprintf(frame
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_share_name
))) {
365 DEBUG(0, ("talloc_asprintf failed\n"));
369 status
= dbwrap_trans_store(share_db
, string_term_tdb_data(key
), blob
,
371 if (!NT_STATUS_IS_OK(status
)) {
372 DEBUG(1, ("set_share_security: Failed to store secdesc for "
373 "%s: %s\n", share_name
, nt_errstr(status
)));
377 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name
));
385 /*******************************************************************
386 Delete a security descriptor.
387 ********************************************************************/
389 bool delete_share_security(const char *servicename
)
394 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
396 if (!c_servicename
) {
400 if (!share_info_db_init()) {
401 TALLOC_FREE(c_servicename
);
405 if (!(key
= talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
407 TALLOC_FREE(c_servicename
);
410 kbuf
= string_term_tdb_data(key
);
412 status
= dbwrap_trans_delete(share_db
, kbuf
);
413 if (!NT_STATUS_IS_OK(status
)) {
414 DEBUG(0, ("delete_share_security: Failed to delete entry for "
415 "share %s: %s\n", c_servicename
, nt_errstr(status
)));
416 TALLOC_FREE(c_servicename
);
420 TALLOC_FREE(c_servicename
);
424 /*******************************************************************
425 Can this user access with share with the required permissions ?
426 ********************************************************************/
428 bool share_access_check(const struct security_token
*token
,
429 const char *sharename
,
430 uint32 desired_access
,
435 struct security_descriptor
*psd
= NULL
;
438 psd
= get_share_security(talloc_tos(), sharename
, &sd_size
);
441 if (pgranted
!= NULL
) {
442 *pgranted
= desired_access
;
447 status
= se_access_check(psd
, token
, desired_access
, &granted
);
451 if (pgranted
!= NULL
) {
455 return NT_STATUS_IS_OK(status
);
458 /***************************************************************************
459 Parse the contents of an acl string from a usershare file.
460 ***************************************************************************/
462 bool parse_usershare_acl(TALLOC_CTX
*ctx
, const char *acl_str
, struct security_descriptor
**ppsd
)
465 const char *pacl
= acl_str
;
467 struct security_ace
*ace_list
= NULL
;
468 struct security_acl
*psa
= NULL
;
469 struct security_descriptor
*psd
= NULL
;
475 /* If the acl string is blank return "Everyone:R" */
477 struct security_descriptor
*default_psd
= get_share_security_default(ctx
, &s_size
, GENERIC_READ_ACCESS
);
487 /* Add the number of ',' characters to get the number of aces. */
488 num_aces
+= count_chars(pacl
,',');
490 ace_list
= talloc_array(ctx
, struct security_ace
, num_aces
);
495 for (i
= 0; i
< num_aces
; i
++) {
501 enum security_ace_type type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
503 if (!next_token_talloc(ctx
, &pacl
, &sidstr
, ":")) {
504 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
505 "for ':' in string '%s'\n", pacl
));
509 if (!string_to_sid(&sid
, sidstr
)) {
510 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
516 case 'F': /* Full Control, ie. R+W */
517 case 'f': /* Full Control, ie. R+W */
518 s_access
= g_access
= GENERIC_ALL_ACCESS
;
520 case 'R': /* Read only. */
521 case 'r': /* Read only. */
522 s_access
= g_access
= GENERIC_READ_ACCESS
;
524 case 'D': /* Deny all to this SID. */
525 case 'd': /* Deny all to this SID. */
526 type
= SEC_ACE_TYPE_ACCESS_DENIED
;
527 s_access
= g_access
= GENERIC_ALL_ACCESS
;
530 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
536 if (*pacl
&& *pacl
!= ',') {
537 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
541 pacl
++; /* Go past any ',' */
543 se_map_generic(&s_access
, &file_generic_mapping
);
544 sa
= (g_access
| s_access
);
545 init_sec_ace(&ace_list
[i
], &sid
, type
, sa
, 0);
548 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, num_aces
, ace_list
)) != NULL
) {
549 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
550 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
555 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));