s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / lib / sharesec.c
blob2f625351b22a3f55948ae2fc49027eda18e1bec2
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"
21 #include "system/filesys.h"
22 #include "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
24 #include "dbwrap.h"
25 #include "util_tdb.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)
43 rec->delete_rec(rec);
44 return 0;
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;
57 char *newkey = NULL;
58 bool *p_upgrade_ok = (bool *)priv;
59 NTSTATUS status;
61 /* Is there space for a one character sharename ? */
62 if (rec->key.dsize <= prefix_len+2) {
63 return 0;
66 /* Does it start with the share key prefix ? */
67 if (memcmp(rec->key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
68 prefix_len) != 0) {
69 return 0;
72 /* Is it a null terminated string as a key ? */
73 if (rec->key.dptr[rec->key.dsize-1] != '\0') {
74 return 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);
80 if (!c_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);
87 return 0;
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;
97 return -1;
98 } else {
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",
105 c_servicename))) {
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),
111 rec->value,
112 TDB_REPLACE);
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);
118 TALLOC_FREE(newkey);
119 *p_upgrade_ok = false;
120 return -1;
121 } else {
122 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
123 "%s\n", newkey ));
126 TALLOC_FREE(newkey);
127 TALLOC_FREE(c_servicename);
129 return 0;
132 bool share_info_db_init(void)
134 const char *vstring = "INFO/version";
135 int32 vers_id;
136 int ret;
137 bool upgrade_ok = true;
139 if (share_db != NULL) {
140 return True;
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) ));
148 return False;
151 vers_id = dbwrap_fetch_int32(share_db, vstring);
152 if (vers_id == SHARE_DATABASE_VERSION_V3) {
153 return true;
156 if (share_db->transaction_start(share_db) != 0) {
157 DEBUG(0, ("transaction_start failed\n"));
158 TALLOC_FREE(share_db);
159 return false;
162 vers_id = dbwrap_fetch_int32(share_db, vstring);
163 if (vers_id == SHARE_DATABASE_VERSION_V3) {
165 * Race condition
167 if (share_db->transaction_cancel(share_db)) {
168 smb_panic("transaction_cancel failed");
170 return true;
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"));
182 goto cancel;
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);
189 if (ret < 0) {
190 DEBUG(0, ("traverse failed\n"));
191 goto cancel;
193 if (dbwrap_store_int32(share_db, vstring,
194 SHARE_DATABASE_VERSION_V2) != 0) {
195 DEBUG(0, ("dbwrap_store_int32 failed\n"));
196 goto cancel;
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"));
205 goto cancel;
207 if (dbwrap_store_int32(share_db, vstring,
208 SHARE_DATABASE_VERSION_V3) != 0) {
209 DEBUG(0, ("dbwrap_store_int32 failed\n"));
210 goto cancel;
213 if (share_db->transaction_commit(share_db) != 0) {
214 DEBUG(0, ("transaction_commit failed\n"));
215 return false;
218 return true;
220 cancel:
221 if (share_db->transaction_cancel(share_db)) {
222 smb_panic("transaction_cancel failed");
225 return false;
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)
235 uint32_t sa;
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,
249 psa, psize);
252 if (!psd) {
253 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
254 return NULL;
257 return psd;
260 /*******************************************************************
261 Pull a security descriptor from the share tdb.
262 ********************************************************************/
264 struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
265 size_t *psize)
267 char *key;
268 struct security_descriptor *psd = NULL;
269 TDB_DATA data;
270 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
271 NTSTATUS status;
273 if (!c_servicename) {
274 return NULL;
277 if (!share_info_db_init()) {
278 TALLOC_FREE(c_servicename);
279 return NULL;
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"));
285 return NULL;
288 TALLOC_FREE(c_servicename);
290 data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
292 TALLOC_FREE(key);
294 if (data.dptr == NULL) {
295 return get_share_security_default(ctx, psize,
296 GENERIC_ALL_ACCESS);
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",
305 nt_errstr(status)));
306 return get_share_security_default(ctx, psize,
307 GENERIC_ALL_ACCESS);
310 if (psd) {
311 *psize = ndr_size_security_descriptor(psd, 0);
312 } else {
313 return get_share_security_default(ctx, psize,
314 GENERIC_ALL_ACCESS);
317 return psd;
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();
327 char *key;
328 bool ret = False;
329 TDB_DATA blob;
330 NTSTATUS status;
331 char *c_share_name = canonicalize_servicename(frame, share_name);
333 if (!c_share_name) {
334 goto out;
337 if (!share_info_db_init()) {
338 goto out;
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",
345 nt_errstr(status)));
346 goto out;
349 if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
350 DEBUG(0, ("talloc_asprintf failed\n"));
351 goto out;
354 status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
355 TDB_REPLACE);
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)));
359 goto out;
362 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
363 ret = True;
365 out:
366 TALLOC_FREE(frame);
367 return ret;
370 /*******************************************************************
371 Delete a security descriptor.
372 ********************************************************************/
374 bool delete_share_security(const char *servicename)
376 TDB_DATA kbuf;
377 char *key;
378 NTSTATUS status;
379 char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
381 if (!c_servicename) {
382 return NULL;
385 if (!share_info_db_init()) {
386 TALLOC_FREE(c_servicename);
387 return False;
390 if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
391 c_servicename))) {
392 TALLOC_FREE(c_servicename);
393 return False;
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);
402 return False;
405 TALLOC_FREE(c_servicename);
406 return True;
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,
416 uint32_t *pgranted)
418 uint32 granted;
419 NTSTATUS status;
420 struct security_descriptor *psd = NULL;
421 size_t sd_size;
423 psd = get_share_security(talloc_tos(), sharename, &sd_size);
425 if (!psd) {
426 if (pgranted != NULL) {
427 *pgranted = desired_access;
429 return false;
432 status = se_access_check(psd, token, desired_access, &granted);
434 TALLOC_FREE(psd);
436 if (pgranted != NULL) {
437 *pgranted = granted;
440 return NT_STATUS_IS_OK(status);
443 /***************************************************************************
444 Parse the contents of an acl string from a usershare file.
445 ***************************************************************************/
447 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
449 size_t s_size = 0;
450 const char *pacl = acl_str;
451 int num_aces = 0;
452 struct security_ace *ace_list = NULL;
453 struct security_acl *psa = NULL;
454 struct security_descriptor *psd = NULL;
455 size_t sd_size = 0;
456 int i;
458 *ppsd = NULL;
460 /* If the acl string is blank return "Everyone:R" */
461 if (!*acl_str) {
462 struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
463 if (!default_psd) {
464 return False;
466 *ppsd = default_psd;
467 return True;
470 num_aces = 1;
472 /* Add the number of ',' characters to get the number of aces. */
473 num_aces += count_chars(pacl,',');
475 ace_list = talloc_array(ctx, struct security_ace, num_aces);
476 if (!ace_list) {
477 return False;
480 for (i = 0; i < num_aces; i++) {
481 uint32_t sa;
482 uint32 g_access;
483 uint32 s_access;
484 struct dom_sid sid;
485 char *sidstr;
486 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
488 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
489 DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
490 "for ':' in string '%s'\n", pacl));
491 return False;
494 if (!string_to_sid(&sid, sidstr)) {
495 DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
496 sidstr ));
497 return False;
500 switch (*pacl) {
501 case 'F': /* Full Control, ie. R+W */
502 case 'f': /* Full Control, ie. R+W */
503 s_access = g_access = GENERIC_ALL_ACCESS;
504 break;
505 case 'R': /* Read only. */
506 case 'r': /* Read only. */
507 s_access = g_access = GENERIC_READ_ACCESS;
508 break;
509 case 'D': /* Deny all to this SID. */
510 case 'd': /* Deny all to this SID. */
511 type = SEC_ACE_TYPE_ACCESS_DENIED;
512 s_access = g_access = GENERIC_ALL_ACCESS;
513 break;
514 default:
515 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
516 pacl ));
517 return False;
520 pacl++;
521 if (*pacl && *pacl != ',') {
522 DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
523 pacl ));
524 return False;
526 pacl++; /* Go past any ',' */
528 se_map_generic(&s_access, &file_generic_mapping);
529 sa = (g_access | s_access);
530 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
533 if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
534 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
535 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
536 psa, &sd_size);
539 if (!psd) {
540 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
541 return False;
544 *ppsd = psd;
545 return True;