Fix a memory leak
[Samba.git] / source / lib / sharesec.c
blob2338cca591bc72afea56cd0ce9f67c005725e2a2
1 /*
2 * Unix SMB/Netbios implementation.
3 * SEC_DESC handling functions
4 * Copyright (C) Jeremy R. Allison 1995-2003.
5 *
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/>.
20 #include "includes.h"
22 /*******************************************************************
23 Create the share security tdb.
24 ********************************************************************/
26 static TDB_CONTEXT *share_tdb; /* used for share security descriptors */
27 #define SHARE_DATABASE_VERSION_V1 1
28 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
30 /* Map generic permissions to file object specific permissions */
32 static const struct generic_mapping file_generic_mapping = {
33 FILE_GENERIC_READ,
34 FILE_GENERIC_WRITE,
35 FILE_GENERIC_EXECUTE,
36 FILE_GENERIC_ALL
40 static bool share_info_db_init(void)
42 const char *vstring = "INFO/version";
43 int32 vers_id;
45 if (share_tdb) {
46 return True;
49 share_tdb = tdb_open_log(state_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
50 if (!share_tdb) {
51 DEBUG(0,("Failed to open share info database %s (%s)\n",
52 state_path("share_info.tdb"), strerror(errno) ));
53 return False;
56 /* handle a Samba upgrade */
57 tdb_lock_bystring(share_tdb, vstring);
59 /* Cope with byte-reversed older versions of the db. */
60 vers_id = tdb_fetch_int32(share_tdb, vstring);
61 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
62 /* Written on a bigendian machine with old fetch_int code. Save as le. */
63 tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
64 vers_id = SHARE_DATABASE_VERSION_V2;
67 if (vers_id != SHARE_DATABASE_VERSION_V2) {
68 tdb_wipe_all(share_tdb);
69 tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
71 tdb_unlock_bystring(share_tdb, vstring);
73 return True;
76 /*******************************************************************
77 Fake up a Everyone, default access as a default.
78 def_access is a GENERIC_XXX access mode.
79 ********************************************************************/
81 SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
83 SEC_ACCESS sa;
84 SEC_ACE ace;
85 SEC_ACL *psa = NULL;
86 SEC_DESC *psd = NULL;
87 uint32 spec_access = def_access;
89 se_map_generic(&spec_access, &file_generic_mapping);
91 init_sec_access(&sa, def_access | spec_access );
92 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
94 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
95 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
96 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
97 psa, psize);
100 if (!psd) {
101 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
102 return NULL;
105 return psd;
108 /*******************************************************************
109 Pull a security descriptor from the share tdb.
110 ********************************************************************/
112 SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
113 size_t *psize)
115 char *key;
116 SEC_DESC *psd = NULL;
117 TDB_DATA data;
118 NTSTATUS status;
120 if (!share_info_db_init()) {
121 return NULL;
124 if (!(key = talloc_asprintf(ctx, "SECDESC/%s", servicename))) {
125 DEBUG(0, ("talloc_asprintf failed\n"));
126 return NULL;
129 data = tdb_fetch_bystring(share_tdb, key);
131 TALLOC_FREE(key);
133 if (data.dptr == NULL) {
134 return get_share_security_default(ctx, psize,
135 GENERIC_ALL_ACCESS);
138 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
140 SAFE_FREE(data.dptr);
142 if (!NT_STATUS_IS_OK(status)) {
143 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
144 nt_errstr(status)));
145 return NULL;
148 if (psd)
149 *psize = ndr_size_security_descriptor(psd, 0);
151 return psd;
154 /*******************************************************************
155 Store a security descriptor in the share db.
156 ********************************************************************/
158 bool set_share_security(const char *share_name, SEC_DESC *psd)
160 TALLOC_CTX *frame;
161 char *key;
162 bool ret = False;
163 TDB_DATA blob;
164 NTSTATUS status;
166 if (!share_info_db_init()) {
167 return False;
170 frame = talloc_stackframe();
172 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0, ("marshall_sec_desc failed: %s\n",
176 nt_errstr(status)));
177 goto out;
180 if (!(key = talloc_asprintf(frame, "SECDESC/%s", share_name))) {
181 DEBUG(0, ("talloc_asprintf failed\n"));
182 goto out;
185 if (tdb_trans_store_bystring(share_tdb, key, blob,
186 TDB_REPLACE) == -1) {
187 DEBUG(1,("set_share_security: Failed to store secdesc for "
188 "%s\n", share_name ));
189 goto out;
192 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
193 ret = True;
195 out:
196 TALLOC_FREE(frame);
197 return ret;
200 /*******************************************************************
201 Delete a security descriptor.
202 ********************************************************************/
204 bool delete_share_security(const char *servicename)
206 TDB_DATA kbuf;
207 char *key;
209 if (!(key = talloc_asprintf(talloc_tos(), "SECDESC/%s",
210 servicename))) {
211 return False;
213 kbuf = string_term_tdb_data(key);
215 if (tdb_trans_delete(share_tdb, kbuf) != 0) {
216 DEBUG(0, ("delete_share_security: Failed to delete entry for "
217 "share %s\n", servicename));
218 return False;
221 return True;
224 /*******************************************************************
225 Can this user access with share with the required permissions ?
226 ********************************************************************/
228 bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
229 uint32 desired_access)
231 uint32 granted;
232 NTSTATUS status;
233 SEC_DESC *psd = NULL;
234 size_t sd_size;
235 bool ret = True;
237 psd = get_share_security(talloc_tos(), sharename, &sd_size);
239 if (!psd) {
240 return True;
243 ret = se_access_check(psd, token, desired_access, &granted, &status);
245 TALLOC_FREE(psd);
247 return ret;
250 /***************************************************************************
251 Parse the contents of an acl string from a usershare file.
252 ***************************************************************************/
254 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
256 size_t s_size = 0;
257 const char *pacl = acl_str;
258 int num_aces = 0;
259 SEC_ACE *ace_list = NULL;
260 SEC_ACL *psa = NULL;
261 SEC_DESC *psd = NULL;
262 size_t sd_size = 0;
263 int i;
265 *ppsd = NULL;
267 /* If the acl string is blank return "Everyone:R" */
268 if (!*acl_str) {
269 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
270 if (!default_psd) {
271 return False;
273 *ppsd = default_psd;
274 return True;
277 num_aces = 1;
279 /* Add the number of ',' characters to get the number of aces. */
280 num_aces += count_chars(pacl,',');
282 ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
283 if (!ace_list) {
284 return False;
287 for (i = 0; i < num_aces; i++) {
288 SEC_ACCESS sa;
289 uint32 g_access;
290 uint32 s_access;
291 DOM_SID sid;
292 char *sidstr;
293 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
295 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
296 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
297 "for ':' in string '%s'\n", pacl));
298 return False;
301 if (!string_to_sid(&sid, sidstr)) {
302 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
303 sidstr ));
304 return False;
307 switch (*pacl) {
308 case 'F': /* Full Control, ie. R+W */
309 case 'f': /* Full Control, ie. R+W */
310 s_access = g_access = GENERIC_ALL_ACCESS;
311 break;
312 case 'R': /* Read only. */
313 case 'r': /* Read only. */
314 s_access = g_access = GENERIC_READ_ACCESS;
315 break;
316 case 'D': /* Deny all to this SID. */
317 case 'd': /* Deny all to this SID. */
318 type = SEC_ACE_TYPE_ACCESS_DENIED;
319 s_access = g_access = GENERIC_ALL_ACCESS;
320 break;
321 default:
322 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
323 pacl ));
324 return False;
327 pacl++;
328 if (*pacl && *pacl != ',') {
329 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
330 pacl ));
331 return False;
333 pacl++; /* Go past any ',' */
335 se_map_generic(&s_access, &file_generic_mapping);
336 init_sec_access(&sa, g_access | s_access );
337 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
340 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
341 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
342 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
343 psa, &sd_size);
346 if (!psd) {
347 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
348 return False;
351 *ppsd = psd;
352 return True;