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"
27 /*******************************************************************
28 Create the share security tdb.
29 ********************************************************************/
31 static struct db_context
*share_db
; /* used for share security descriptors */
32 #define SHARE_DATABASE_VERSION_V1 1
33 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
34 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
36 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
37 /* Map generic permissions to file object specific permissions */
39 extern const struct generic_mapping file_generic_mapping
;
41 static int delete_fn(struct db_record
*rec
, void *priv
)
47 /*****************************************************
48 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
49 If we find one re-write it into a canonical case form.
50 *****************************************************/
52 static int upgrade_v2_to_v3(struct db_record
*rec
, void *priv
)
54 size_t prefix_len
= strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR
);
55 const char *servicename
= NULL
;
56 char *c_servicename
= NULL
;
58 bool *p_upgrade_ok
= (bool *)priv
;
61 /* Is there space for a one character sharename ? */
62 if (rec
->key
.dsize
<= prefix_len
+2) {
66 /* Does it start with the share key prefix ? */
67 if (memcmp(rec
->key
.dptr
, SHARE_SECURITY_DB_KEY_PREFIX_STR
,
72 /* Is it a null terminated string as a key ? */
73 if (rec
->key
.dptr
[rec
->key
.dsize
-1] != '\0') {
77 /* Bytes after the prefix are the sharename string. */
78 servicename
= (char *)&rec
->key
.dptr
[prefix_len
];
79 c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
81 smb_panic("out of memory upgrading share security db from v2 -> v3");
84 if (strcmp(servicename
, c_servicename
) == 0) {
85 /* Old and new names match. No canonicalization needed. */
86 TALLOC_FREE(c_servicename
);
90 /* Oops. Need to canonicalize name, delete old then store new. */
91 status
= rec
->delete_rec(rec
);
92 if (!NT_STATUS_IS_OK(status
)) {
93 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
94 "%s: %s\n", rec
->key
.dptr
, nt_errstr(status
)));
95 TALLOC_FREE(c_servicename
);
96 *p_upgrade_ok
= false;
99 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
100 "%s\n", rec
->key
.dptr
));
103 if (!(newkey
= talloc_asprintf(talloc_tos(),
104 SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
106 smb_panic("out of memory upgrading share security db from v2 -> v3");
109 status
= dbwrap_store(share_db
,
110 string_term_tdb_data(newkey
),
114 if (!NT_STATUS_IS_OK(status
)) {
115 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
116 "%s: %s\n", c_servicename
, nt_errstr(status
)));
117 TALLOC_FREE(c_servicename
);
119 *p_upgrade_ok
= false;
122 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
127 TALLOC_FREE(c_servicename
);
132 bool share_info_db_init(void)
134 const char *vstring
= "INFO/version";
137 bool upgrade_ok
= true;
139 if (share_db
!= NULL
) {
143 share_db
= db_open(NULL
, state_path("share_info.tdb"), 0,
144 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
145 if (share_db
== NULL
) {
146 DEBUG(0,("Failed to open share info database %s (%s)\n",
147 state_path("share_info.tdb"), strerror(errno
) ));
151 vers_id
= dbwrap_fetch_int32(share_db
, vstring
);
152 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
156 if (share_db
->transaction_start(share_db
) != 0) {
157 DEBUG(0, ("transaction_start failed\n"));
158 TALLOC_FREE(share_db
);
162 vers_id
= dbwrap_fetch_int32(share_db
, vstring
);
163 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
167 if (share_db
->transaction_cancel(share_db
)) {
168 smb_panic("transaction_cancel failed");
173 /* Move to at least V2. */
175 /* Cope with byte-reversed older versions of the db. */
176 if ((vers_id
== SHARE_DATABASE_VERSION_V1
) || (IREV(vers_id
) == SHARE_DATABASE_VERSION_V1
)) {
177 /* Written on a bigendian machine with old fetch_int code. Save as le. */
179 if (dbwrap_store_int32(share_db
, vstring
,
180 SHARE_DATABASE_VERSION_V2
) != 0) {
181 DEBUG(0, ("dbwrap_store_int32 failed\n"));
184 vers_id
= SHARE_DATABASE_VERSION_V2
;
187 if (vers_id
!= SHARE_DATABASE_VERSION_V2
) {
188 ret
= share_db
->traverse(share_db
, delete_fn
, NULL
);
190 DEBUG(0, ("traverse failed\n"));
193 if (dbwrap_store_int32(share_db
, vstring
,
194 SHARE_DATABASE_VERSION_V2
) != 0) {
195 DEBUG(0, ("dbwrap_store_int32 failed\n"));
200 /* Finally upgrade to version 3, with canonicalized sharenames. */
202 ret
= share_db
->traverse(share_db
, upgrade_v2_to_v3
, &upgrade_ok
);
203 if (ret
< 0 || upgrade_ok
== false) {
204 DEBUG(0, ("traverse failed\n"));
207 if (dbwrap_store_int32(share_db
, vstring
,
208 SHARE_DATABASE_VERSION_V3
) != 0) {
209 DEBUG(0, ("dbwrap_store_int32 failed\n"));
213 if (share_db
->transaction_commit(share_db
) != 0) {
214 DEBUG(0, ("transaction_commit failed\n"));
221 if (share_db
->transaction_cancel(share_db
)) {
222 smb_panic("transaction_cancel failed");
228 /*******************************************************************
229 Fake up a Everyone, default access as a default.
230 def_access is a GENERIC_XXX access mode.
231 ********************************************************************/
233 struct security_descriptor
*get_share_security_default( TALLOC_CTX
*ctx
, size_t *psize
, uint32 def_access
)
236 struct security_ace ace
;
237 struct security_acl
*psa
= NULL
;
238 struct security_descriptor
*psd
= NULL
;
239 uint32 spec_access
= def_access
;
241 se_map_generic(&spec_access
, &file_generic_mapping
);
243 sa
= (def_access
| spec_access
);
244 init_sec_ace(&ace
, &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, sa
, 0);
246 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, 1, &ace
)) != NULL
) {
247 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
248 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
253 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
260 /*******************************************************************
261 Pull a security descriptor from the share tdb.
262 ********************************************************************/
264 struct security_descriptor
*get_share_security( TALLOC_CTX
*ctx
, const char *servicename
,
268 struct security_descriptor
*psd
= NULL
;
270 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
273 if (!c_servicename
) {
277 if (!share_info_db_init()) {
278 TALLOC_FREE(c_servicename
);
282 if (!(key
= talloc_asprintf(ctx
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_servicename
))) {
283 TALLOC_FREE(c_servicename
);
284 DEBUG(0, ("talloc_asprintf failed\n"));
288 TALLOC_FREE(c_servicename
);
290 data
= dbwrap_fetch_bystring(share_db
, talloc_tos(), key
);
294 if (data
.dptr
== NULL
) {
295 return get_share_security_default(ctx
, psize
,
299 status
= unmarshall_sec_desc(ctx
, data
.dptr
, data
.dsize
, &psd
);
301 TALLOC_FREE(data
.dptr
);
303 if (!NT_STATUS_IS_OK(status
)) {
304 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
306 return get_share_security_default(ctx
, psize
,
311 *psize
= ndr_size_security_descriptor(psd
, 0);
313 return get_share_security_default(ctx
, psize
,
320 /*******************************************************************
321 Store a security descriptor in the share db.
322 ********************************************************************/
324 bool set_share_security(const char *share_name
, struct security_descriptor
*psd
)
326 TALLOC_CTX
*frame
= talloc_stackframe();
331 char *c_share_name
= canonicalize_servicename(frame
, share_name
);
337 if (!share_info_db_init()) {
341 status
= marshall_sec_desc(frame
, psd
, &blob
.dptr
, &blob
.dsize
);
343 if (!NT_STATUS_IS_OK(status
)) {
344 DEBUG(0, ("marshall_sec_desc failed: %s\n",
349 if (!(key
= talloc_asprintf(frame
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_share_name
))) {
350 DEBUG(0, ("talloc_asprintf failed\n"));
354 status
= dbwrap_trans_store(share_db
, string_term_tdb_data(key
), blob
,
356 if (!NT_STATUS_IS_OK(status
)) {
357 DEBUG(1, ("set_share_security: Failed to store secdesc for "
358 "%s: %s\n", share_name
, nt_errstr(status
)));
362 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name
));
370 /*******************************************************************
371 Delete a security descriptor.
372 ********************************************************************/
374 bool delete_share_security(const char *servicename
)
379 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
381 if (!c_servicename
) {
385 if (!share_info_db_init()) {
386 TALLOC_FREE(c_servicename
);
390 if (!(key
= talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
392 TALLOC_FREE(c_servicename
);
395 kbuf
= string_term_tdb_data(key
);
397 status
= dbwrap_trans_delete(share_db
, kbuf
);
398 if (!NT_STATUS_IS_OK(status
)) {
399 DEBUG(0, ("delete_share_security: Failed to delete entry for "
400 "share %s: %s\n", c_servicename
, nt_errstr(status
)));
401 TALLOC_FREE(c_servicename
);
405 TALLOC_FREE(c_servicename
);
409 /*******************************************************************
410 Can this user access with share with the required permissions ?
411 ********************************************************************/
413 bool share_access_check(const struct security_token
*token
,
414 const char *sharename
,
415 uint32 desired_access
,
420 struct security_descriptor
*psd
= NULL
;
423 psd
= get_share_security(talloc_tos(), sharename
, &sd_size
);
429 status
= se_access_check(psd
, token
, desired_access
, &granted
);
433 if (pgranted
!= NULL
) {
437 return NT_STATUS_IS_OK(status
);
440 /***************************************************************************
441 Parse the contents of an acl string from a usershare file.
442 ***************************************************************************/
444 bool parse_usershare_acl(TALLOC_CTX
*ctx
, const char *acl_str
, struct security_descriptor
**ppsd
)
447 const char *pacl
= acl_str
;
449 struct security_ace
*ace_list
= NULL
;
450 struct security_acl
*psa
= NULL
;
451 struct security_descriptor
*psd
= NULL
;
457 /* If the acl string is blank return "Everyone:R" */
459 struct security_descriptor
*default_psd
= get_share_security_default(ctx
, &s_size
, GENERIC_READ_ACCESS
);
469 /* Add the number of ',' characters to get the number of aces. */
470 num_aces
+= count_chars(pacl
,',');
472 ace_list
= TALLOC_ARRAY(ctx
, struct security_ace
, num_aces
);
477 for (i
= 0; i
< num_aces
; i
++) {
483 enum security_ace_type type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
485 if (!next_token_talloc(ctx
, &pacl
, &sidstr
, ":")) {
486 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
487 "for ':' in string '%s'\n", pacl
));
491 if (!string_to_sid(&sid
, sidstr
)) {
492 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
498 case 'F': /* Full Control, ie. R+W */
499 case 'f': /* Full Control, ie. R+W */
500 s_access
= g_access
= GENERIC_ALL_ACCESS
;
502 case 'R': /* Read only. */
503 case 'r': /* Read only. */
504 s_access
= g_access
= GENERIC_READ_ACCESS
;
506 case 'D': /* Deny all to this SID. */
507 case 'd': /* Deny all to this SID. */
508 type
= SEC_ACE_TYPE_ACCESS_DENIED
;
509 s_access
= g_access
= GENERIC_ALL_ACCESS
;
512 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
518 if (*pacl
&& *pacl
!= ',') {
519 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
523 pacl
++; /* Go past any ',' */
525 se_map_generic(&s_access
, &file_generic_mapping
);
526 sa
= (g_access
| s_access
);
527 init_sec_ace(&ace_list
[i
], &sid
, type
, sa
, 0);
530 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, num_aces
, ace_list
)) != NULL
) {
531 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
532 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
537 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));