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 "../libcli/security/security.h"
22 #include "../librpc/gen_ndr/ndr_security.h"
25 /*******************************************************************
26 Create the share security tdb.
27 ********************************************************************/
29 static struct db_context
*share_db
; /* used for share security descriptors */
30 #define SHARE_DATABASE_VERSION_V1 1
31 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
32 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
34 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
35 /* Map generic permissions to file object specific permissions */
37 extern const struct generic_mapping file_generic_mapping
;
39 static int delete_fn(struct db_record
*rec
, void *priv
)
45 /*****************************************************
46 Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
47 If we find one re-write it into a canonical case form.
48 *****************************************************/
50 static int upgrade_v2_to_v3(struct db_record
*rec
, void *priv
)
52 size_t prefix_len
= strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR
);
53 const char *servicename
= NULL
;
54 char *c_servicename
= NULL
;
56 bool *p_upgrade_ok
= (bool *)priv
;
59 /* Is there space for a one character sharename ? */
60 if (rec
->key
.dsize
<= prefix_len
+2) {
64 /* Does it start with the share key prefix ? */
65 if (memcmp(rec
->key
.dptr
, SHARE_SECURITY_DB_KEY_PREFIX_STR
,
70 /* Is it a null terminated string as a key ? */
71 if (rec
->key
.dptr
[rec
->key
.dsize
-1] != '\0') {
75 /* Bytes after the prefix are the sharename string. */
76 servicename
= (char *)&rec
->key
.dptr
[prefix_len
];
77 c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
79 smb_panic("out of memory upgrading share security db from v2 -> v3");
82 if (strcmp(servicename
, c_servicename
) == 0) {
83 /* Old and new names match. No canonicalization needed. */
84 TALLOC_FREE(c_servicename
);
88 /* Oops. Need to canonicalize name, delete old then store new. */
89 status
= rec
->delete_rec(rec
);
90 if (!NT_STATUS_IS_OK(status
)) {
91 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
92 "%s: %s\n", rec
->key
.dptr
, nt_errstr(status
)));
93 TALLOC_FREE(c_servicename
);
94 *p_upgrade_ok
= false;
97 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
98 "%s\n", rec
->key
.dptr
));
101 if (!(newkey
= talloc_asprintf(talloc_tos(),
102 SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
104 smb_panic("out of memory upgrading share security db from v2 -> v3");
107 status
= dbwrap_store(share_db
,
108 string_term_tdb_data(newkey
),
112 if (!NT_STATUS_IS_OK(status
)) {
113 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
114 "%s: %s\n", c_servicename
, nt_errstr(status
)));
115 TALLOC_FREE(c_servicename
);
117 *p_upgrade_ok
= false;
120 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
125 TALLOC_FREE(c_servicename
);
130 bool share_info_db_init(void)
132 const char *vstring
= "INFO/version";
135 bool upgrade_ok
= true;
137 if (share_db
!= NULL
) {
141 share_db
= db_open(NULL
, state_path("share_info.tdb"), 0,
142 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
143 if (share_db
== NULL
) {
144 DEBUG(0,("Failed to open share info database %s (%s)\n",
145 state_path("share_info.tdb"), strerror(errno
) ));
149 vers_id
= dbwrap_fetch_int32(share_db
, vstring
);
150 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
154 if (share_db
->transaction_start(share_db
) != 0) {
155 DEBUG(0, ("transaction_start failed\n"));
156 TALLOC_FREE(share_db
);
160 vers_id
= dbwrap_fetch_int32(share_db
, vstring
);
161 if (vers_id
== SHARE_DATABASE_VERSION_V3
) {
165 if (share_db
->transaction_cancel(share_db
)) {
166 smb_panic("transaction_cancel failed");
171 /* Move to at least V2. */
173 /* Cope with byte-reversed older versions of the db. */
174 if ((vers_id
== SHARE_DATABASE_VERSION_V1
) || (IREV(vers_id
) == SHARE_DATABASE_VERSION_V1
)) {
175 /* Written on a bigendian machine with old fetch_int code. Save as le. */
177 if (dbwrap_store_int32(share_db
, vstring
,
178 SHARE_DATABASE_VERSION_V2
) != 0) {
179 DEBUG(0, ("dbwrap_store_int32 failed\n"));
182 vers_id
= SHARE_DATABASE_VERSION_V2
;
185 if (vers_id
!= SHARE_DATABASE_VERSION_V2
) {
186 ret
= share_db
->traverse(share_db
, delete_fn
, NULL
);
188 DEBUG(0, ("traverse failed\n"));
191 if (dbwrap_store_int32(share_db
, vstring
,
192 SHARE_DATABASE_VERSION_V2
) != 0) {
193 DEBUG(0, ("dbwrap_store_int32 failed\n"));
198 /* Finally upgrade to version 3, with canonicalized sharenames. */
200 ret
= share_db
->traverse(share_db
, upgrade_v2_to_v3
, &upgrade_ok
);
201 if (ret
< 0 || upgrade_ok
== false) {
202 DEBUG(0, ("traverse failed\n"));
205 if (dbwrap_store_int32(share_db
, vstring
,
206 SHARE_DATABASE_VERSION_V3
) != 0) {
207 DEBUG(0, ("dbwrap_store_int32 failed\n"));
211 if (share_db
->transaction_commit(share_db
) != 0) {
212 DEBUG(0, ("transaction_commit failed\n"));
219 if (share_db
->transaction_cancel(share_db
)) {
220 smb_panic("transaction_cancel failed");
226 /*******************************************************************
227 Fake up a Everyone, default access as a default.
228 def_access is a GENERIC_XXX access mode.
229 ********************************************************************/
231 struct security_descriptor
*get_share_security_default( TALLOC_CTX
*ctx
, size_t *psize
, uint32 def_access
)
234 struct security_ace ace
;
235 struct security_acl
*psa
= NULL
;
236 struct security_descriptor
*psd
= NULL
;
237 uint32 spec_access
= def_access
;
239 se_map_generic(&spec_access
, &file_generic_mapping
);
241 sa
= (def_access
| spec_access
);
242 init_sec_ace(&ace
, &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, sa
, 0);
244 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, 1, &ace
)) != NULL
) {
245 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
246 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
251 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
258 /*******************************************************************
259 Pull a security descriptor from the share tdb.
260 ********************************************************************/
262 struct security_descriptor
*get_share_security( TALLOC_CTX
*ctx
, const char *servicename
,
266 struct security_descriptor
*psd
= NULL
;
268 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
271 if (!c_servicename
) {
275 if (!share_info_db_init()) {
276 TALLOC_FREE(c_servicename
);
280 if (!(key
= talloc_asprintf(ctx
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_servicename
))) {
281 TALLOC_FREE(c_servicename
);
282 DEBUG(0, ("talloc_asprintf failed\n"));
286 TALLOC_FREE(c_servicename
);
288 data
= dbwrap_fetch_bystring(share_db
, talloc_tos(), key
);
292 if (data
.dptr
== NULL
) {
293 return get_share_security_default(ctx
, psize
,
297 status
= unmarshall_sec_desc(ctx
, data
.dptr
, data
.dsize
, &psd
);
299 TALLOC_FREE(data
.dptr
);
301 if (!NT_STATUS_IS_OK(status
)) {
302 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
304 return get_share_security_default(ctx
, psize
,
309 *psize
= ndr_size_security_descriptor(psd
, 0);
311 return get_share_security_default(ctx
, psize
,
318 /*******************************************************************
319 Store a security descriptor in the share db.
320 ********************************************************************/
322 bool set_share_security(const char *share_name
, struct security_descriptor
*psd
)
324 TALLOC_CTX
*frame
= talloc_stackframe();
329 char *c_share_name
= canonicalize_servicename(frame
, share_name
);
335 if (!share_info_db_init()) {
339 status
= marshall_sec_desc(frame
, psd
, &blob
.dptr
, &blob
.dsize
);
341 if (!NT_STATUS_IS_OK(status
)) {
342 DEBUG(0, ("marshall_sec_desc failed: %s\n",
347 if (!(key
= talloc_asprintf(frame
, SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s", c_share_name
))) {
348 DEBUG(0, ("talloc_asprintf failed\n"));
352 status
= dbwrap_trans_store(share_db
, string_term_tdb_data(key
), blob
,
354 if (!NT_STATUS_IS_OK(status
)) {
355 DEBUG(1, ("set_share_security: Failed to store secdesc for "
356 "%s: %s\n", share_name
, nt_errstr(status
)));
360 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name
));
368 /*******************************************************************
369 Delete a security descriptor.
370 ********************************************************************/
372 bool delete_share_security(const char *servicename
)
377 char *c_servicename
= canonicalize_servicename(talloc_tos(), servicename
);
379 if (!c_servicename
) {
383 if (!share_info_db_init()) {
384 TALLOC_FREE(c_servicename
);
388 if (!(key
= talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR
"%s",
390 TALLOC_FREE(c_servicename
);
393 kbuf
= string_term_tdb_data(key
);
395 status
= dbwrap_trans_delete(share_db
, kbuf
);
396 if (!NT_STATUS_IS_OK(status
)) {
397 DEBUG(0, ("delete_share_security: Failed to delete entry for "
398 "share %s: %s\n", c_servicename
, nt_errstr(status
)));
399 TALLOC_FREE(c_servicename
);
403 TALLOC_FREE(c_servicename
);
407 /*******************************************************************
408 Can this user access with share with the required permissions ?
409 ********************************************************************/
411 bool share_access_check(const struct security_token
*token
, const char *sharename
,
412 uint32 desired_access
)
416 struct security_descriptor
*psd
= NULL
;
419 psd
= get_share_security(talloc_tos(), sharename
, &sd_size
);
425 status
= se_access_check(psd
, token
, desired_access
, &granted
);
429 return NT_STATUS_IS_OK(status
);
432 /***************************************************************************
433 Parse the contents of an acl string from a usershare file.
434 ***************************************************************************/
436 bool parse_usershare_acl(TALLOC_CTX
*ctx
, const char *acl_str
, struct security_descriptor
**ppsd
)
439 const char *pacl
= acl_str
;
441 struct security_ace
*ace_list
= NULL
;
442 struct security_acl
*psa
= NULL
;
443 struct security_descriptor
*psd
= NULL
;
449 /* If the acl string is blank return "Everyone:R" */
451 struct security_descriptor
*default_psd
= get_share_security_default(ctx
, &s_size
, GENERIC_READ_ACCESS
);
461 /* Add the number of ',' characters to get the number of aces. */
462 num_aces
+= count_chars(pacl
,',');
464 ace_list
= TALLOC_ARRAY(ctx
, struct security_ace
, num_aces
);
469 for (i
= 0; i
< num_aces
; i
++) {
475 enum security_ace_type type
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
477 if (!next_token_talloc(ctx
, &pacl
, &sidstr
, ":")) {
478 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
479 "for ':' in string '%s'\n", pacl
));
483 if (!string_to_sid(&sid
, sidstr
)) {
484 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
490 case 'F': /* Full Control, ie. R+W */
491 case 'f': /* Full Control, ie. R+W */
492 s_access
= g_access
= GENERIC_ALL_ACCESS
;
494 case 'R': /* Read only. */
495 case 'r': /* Read only. */
496 s_access
= g_access
= GENERIC_READ_ACCESS
;
498 case 'D': /* Deny all to this SID. */
499 case 'd': /* Deny all to this SID. */
500 type
= SEC_ACE_TYPE_ACCESS_DENIED
;
501 s_access
= g_access
= GENERIC_ALL_ACCESS
;
504 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
510 if (*pacl
&& *pacl
!= ',') {
511 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
515 pacl
++; /* Go past any ',' */
517 se_map_generic(&s_access
, &file_generic_mapping
);
518 sa
= (g_access
| s_access
);
519 init_sec_ace(&ace_list
[i
], &sid
, type
, sa
, 0);
522 if ((psa
= make_sec_acl(ctx
, NT4_ACL_REVISION
, num_aces
, ace_list
)) != NULL
) {
523 psd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
524 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
529 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));