objectclass -> objectClass
[Samba/gebeck_regimport.git] / source3 / lib / sharesec.c
blob471363b4bedb42297555300dd348c38d6985d967
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 struct db_context *share_db; /* 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
39 static int delete_fn(struct db_record *rec, void *priv)
41 rec->delete_rec(rec);
42 return 0;
45 static bool share_info_db_init(void)
47 const char *vstring = "INFO/version";
48 int32 vers_id;
50 if (share_db != NULL) {
51 return True;
54 share_db = db_open_trans(NULL, state_path("share_info.tdb"), 0,
55 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
56 if (share_db == NULL) {
57 DEBUG(0,("Failed to open share info database %s (%s)\n",
58 state_path("share_info.tdb"), strerror(errno) ));
59 return False;
62 vers_id = dbwrap_fetch_int32(share_db, vstring);
63 if (vers_id == SHARE_DATABASE_VERSION_V2) {
64 return true;
67 if (share_db->transaction_start(share_db) != 0) {
68 DEBUG(0, ("transaction_start failed\n"));
69 TALLOC_FREE(share_db);
70 return false;
73 vers_id = dbwrap_fetch_int32(share_db, vstring);
74 if (vers_id == SHARE_DATABASE_VERSION_V2) {
76 * Race condition
78 if (share_db->transaction_cancel(share_db)) {
79 smb_panic("transaction_cancel failed");
81 return true;
84 /* Cope with byte-reversed older versions of the db. */
85 if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
86 /* Written on a bigendian machine with old fetch_int code. Save as le. */
88 if (dbwrap_store_int32(share_db, vstring,
89 SHARE_DATABASE_VERSION_V2) != 0) {
90 DEBUG(0, ("dbwrap_store_int32 failed\n"));
91 goto cancel;
93 vers_id = SHARE_DATABASE_VERSION_V2;
96 if (vers_id != SHARE_DATABASE_VERSION_V2) {
97 if (share_db->traverse(share_db, delete_fn, NULL) != 0) {
98 DEBUG(0, ("wipe_all failed\n"));
99 goto cancel;
101 if (dbwrap_store_int32(share_db, vstring,
102 SHARE_DATABASE_VERSION_V2) != 0) {
103 DEBUG(0, ("dbwrap_store_int32 failed\n"));
104 goto cancel;
108 if (share_db->transaction_commit(share_db) != 0) {
109 DEBUG(0, ("transaction_commit failed\n"));
110 goto cancel;
113 return true;
115 cancel:
116 if (share_db->transaction_cancel(share_db)) {
117 smb_panic("transaction_cancel failed");
120 return false;
123 /*******************************************************************
124 Fake up a Everyone, default access as a default.
125 def_access is a GENERIC_XXX access mode.
126 ********************************************************************/
128 SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
130 SEC_ACCESS sa;
131 SEC_ACE ace;
132 SEC_ACL *psa = NULL;
133 SEC_DESC *psd = NULL;
134 uint32 spec_access = def_access;
136 se_map_generic(&spec_access, &file_generic_mapping);
138 init_sec_access(&sa, def_access | spec_access );
139 init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
141 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
142 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
143 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
144 psa, psize);
147 if (!psd) {
148 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
149 return NULL;
152 return psd;
155 /*******************************************************************
156 Pull a security descriptor from the share tdb.
157 ********************************************************************/
159 SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
160 size_t *psize)
162 char *key;
163 SEC_DESC *psd = NULL;
164 TDB_DATA data;
165 NTSTATUS status;
167 if (!share_info_db_init()) {
168 return NULL;
171 if (!(key = talloc_asprintf(ctx, "SECDESC/%s", servicename))) {
172 DEBUG(0, ("talloc_asprintf failed\n"));
173 return NULL;
176 data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
178 TALLOC_FREE(key);
180 if (data.dptr == NULL) {
181 return get_share_security_default(ctx, psize,
182 GENERIC_ALL_ACCESS);
185 status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
187 TALLOC_FREE(data.dptr);
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
191 nt_errstr(status)));
192 return NULL;
195 if (psd)
196 *psize = ndr_size_security_descriptor(psd, 0);
198 return psd;
201 /*******************************************************************
202 Store a security descriptor in the share db.
203 ********************************************************************/
205 bool set_share_security(const char *share_name, SEC_DESC *psd)
207 TALLOC_CTX *frame;
208 char *key;
209 bool ret = False;
210 TDB_DATA blob;
211 NTSTATUS status;
213 if (!share_info_db_init()) {
214 return False;
217 frame = talloc_stackframe();
219 status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
221 if (!NT_STATUS_IS_OK(status)) {
222 DEBUG(0, ("marshall_sec_desc failed: %s\n",
223 nt_errstr(status)));
224 goto out;
227 if (!(key = talloc_asprintf(frame, "SECDESC/%s", share_name))) {
228 DEBUG(0, ("talloc_asprintf failed\n"));
229 goto out;
232 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
233 TDB_REPLACE);
234 if (!NT_STATUS_IS_OK(status)) {
235 DEBUG(1, ("set_share_security: Failed to store secdesc for "
236 "%s: %s\n", share_name, nt_errstr(status)));
237 goto out;
240 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
241 ret = True;
243 out:
244 TALLOC_FREE(frame);
245 return ret;
248 /*******************************************************************
249 Delete a security descriptor.
250 ********************************************************************/
252 bool delete_share_security(const char *servicename)
254 TDB_DATA kbuf;
255 char *key;
256 NTSTATUS status;
258 if (!(key = talloc_asprintf(talloc_tos(), "SECDESC/%s",
259 servicename))) {
260 return False;
262 kbuf = string_term_tdb_data(key);
264 status = dbwrap_trans_delete(share_db, kbuf);
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(0, ("delete_share_security: Failed to delete entry for "
267 "share %s: %s\n", servicename, nt_errstr(status)));
268 return False;
271 return True;
274 /*******************************************************************
275 Can this user access with share with the required permissions ?
276 ********************************************************************/
278 bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
279 uint32 desired_access)
281 uint32 granted;
282 NTSTATUS status;
283 SEC_DESC *psd = NULL;
284 size_t sd_size;
285 bool ret = True;
287 psd = get_share_security(talloc_tos(), sharename, &sd_size);
289 if (!psd) {
290 return True;
293 ret = se_access_check(psd, token, desired_access, &granted, &status);
295 TALLOC_FREE(psd);
297 return ret;
300 /***************************************************************************
301 Parse the contents of an acl string from a usershare file.
302 ***************************************************************************/
304 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
306 size_t s_size = 0;
307 const char *pacl = acl_str;
308 int num_aces = 0;
309 SEC_ACE *ace_list = NULL;
310 SEC_ACL *psa = NULL;
311 SEC_DESC *psd = NULL;
312 size_t sd_size = 0;
313 int i;
315 *ppsd = NULL;
317 /* If the acl string is blank return "Everyone:R" */
318 if (!*acl_str) {
319 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
320 if (!default_psd) {
321 return False;
323 *ppsd = default_psd;
324 return True;
327 num_aces = 1;
329 /* Add the number of ',' characters to get the number of aces. */
330 num_aces += count_chars(pacl,',');
332 ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
333 if (!ace_list) {
334 return False;
337 for (i = 0; i < num_aces; i++) {
338 SEC_ACCESS sa;
339 uint32 g_access;
340 uint32 s_access;
341 DOM_SID sid;
342 char *sidstr;
343 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
345 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
346 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
347 "for ':' in string '%s'\n", pacl));
348 return False;
351 if (!string_to_sid(&sid, sidstr)) {
352 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
353 sidstr ));
354 return False;
357 switch (*pacl) {
358 case 'F': /* Full Control, ie. R+W */
359 case 'f': /* Full Control, ie. R+W */
360 s_access = g_access = GENERIC_ALL_ACCESS;
361 break;
362 case 'R': /* Read only. */
363 case 'r': /* Read only. */
364 s_access = g_access = GENERIC_READ_ACCESS;
365 break;
366 case 'D': /* Deny all to this SID. */
367 case 'd': /* Deny all to this SID. */
368 type = SEC_ACE_TYPE_ACCESS_DENIED;
369 s_access = g_access = GENERIC_ALL_ACCESS;
370 break;
371 default:
372 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
373 pacl ));
374 return False;
377 pacl++;
378 if (*pacl && *pacl != ',') {
379 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
380 pacl ));
381 return False;
383 pacl++; /* Go past any ',' */
385 se_map_generic(&s_access, &file_generic_mapping);
386 init_sec_access(&sa, g_access | s_access );
387 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
390 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
391 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
392 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
393 psa, &sd_size);
396 if (!psd) {
397 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
398 return False;
401 *ppsd = psd;
402 return True;