lib/util: Standardize use of st_[acm]time ns
[samba.git] / source3 / lib / sharesec.c
index f6ff701d5b79ae587dcab9c19950d2dfed7ed675..acbdd8b5df918b9f1c687dd39e9f7d615c4f550b 100644 (file)
  */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "../libcli/security/security.h"
+#include "../librpc/gen_ndr/ndr_security.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
+#include "util_tdb.h"
+#include "libcli/util/ntstatus.h"
 
 /*******************************************************************
  Create the share security tdb.
  ********************************************************************/
 
-static TDB_CONTEXT *share_tdb; /* used for share security descriptors */
+static struct db_context *share_db; /* used for share security descriptors */
 #define SHARE_DATABASE_VERSION_V1 1
 #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
+#define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
 
+#define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
 /* Map generic permissions to file object specific permissions */
 
-static const struct generic_mapping file_generic_mapping = {
-        FILE_GENERIC_READ,
-        FILE_GENERIC_WRITE,
-        FILE_GENERIC_EXECUTE,
-        FILE_GENERIC_ALL
-};
+extern const struct generic_mapping file_generic_mapping;
 
+static int delete_fn(struct db_record *rec, void *priv)
+{
+       dbwrap_record_delete(rec);
+       return 0;
+}
 
-static bool share_info_db_init(void)
+/*****************************************************
+ Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
+ If we find one re-write it into a canonical case form.
+*****************************************************/
+
+static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
+{
+       size_t prefix_len = strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR);
+       const char *servicename = NULL;
+       char *c_servicename = NULL;
+       char *newkey = NULL;
+       bool *p_upgrade_ok = (bool *)priv;
+       NTSTATUS status;
+       TDB_DATA key;
+       TDB_DATA value;
+
+       key = dbwrap_record_get_key(rec);
+
+       /* Is there space for a one character sharename ? */
+       if (key.dsize <= prefix_len+2) {
+               return 0;
+       }
+
+       /* Does it start with the share key prefix ? */
+       if (memcmp(key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
+                       prefix_len) != 0) {
+               return 0;
+       }
+
+       /* Is it a null terminated string as a key ? */
+       if (key.dptr[key.dsize-1] != '\0') {
+               return 0;
+       }
+
+       /* Bytes after the prefix are the sharename string. */
+       servicename = (char *)&key.dptr[prefix_len];
+       c_servicename = canonicalize_servicename(talloc_tos(), servicename);
+       if (!c_servicename) {
+               smb_panic("out of memory upgrading share security db from v2 -> v3");
+       }
+
+       if (strcmp(servicename, c_servicename) == 0) {
+               /* Old and new names match. No canonicalization needed. */
+               TALLOC_FREE(c_servicename);
+               return 0;
+       }
+
+       /* Oops. Need to canonicalize name, delete old then store new. */
+       status = dbwrap_record_delete(rec);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
+                         "%s: %s\n", (const char *)key.dptr,
+                         nt_errstr(status)));
+               TALLOC_FREE(c_servicename);
+               *p_upgrade_ok = false;
+               return -1;
+       } else {
+               DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
+                          "%s\n", (const char *)key.dptr));
+       }
+
+       if (!(newkey = talloc_asprintf(talloc_tos(),
+                       SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
+                       c_servicename))) {
+               smb_panic("out of memory upgrading share security db from v2 -> v3");
+       }
+
+       value = dbwrap_record_get_value(rec);
+       status = dbwrap_store(share_db,
+                               string_term_tdb_data(newkey),
+                               value,
+                               TDB_REPLACE);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
+                         "%s: %s\n", c_servicename, nt_errstr(status)));
+               TALLOC_FREE(c_servicename);
+               TALLOC_FREE(newkey);
+               *p_upgrade_ok = false;
+               return -1;
+       } else {
+               DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
+                          "%s\n", newkey ));
+       }
+
+       TALLOC_FREE(newkey);
+       TALLOC_FREE(c_servicename);
+
+       return 0;
+}
+
+NTSTATUS share_info_db_init(void)
 {
        const char *vstring = "INFO/version";
-       int32 vers_id;
-       if (share_tdb) {
-               return True;
+       int32_t vers_id = 0;
+       bool upgrade_ok = true;
+       NTSTATUS status;
+       char *db_path;
+
+       if (share_db != NULL) {
+               return NT_STATUS_OK;
        }
 
-       share_tdb = tdb_open_log(state_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
-       if (!share_tdb) {
+       db_path = state_path(talloc_tos(), "share_info.tdb");
+       if (db_path == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       share_db = db_open(NULL, db_path, 0,
+                          TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
+                          DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
+       if (share_db == NULL) {
                DEBUG(0,("Failed to open share info database %s (%s)\n",
-                       state_path("share_info.tdb"), strerror(errno) ));
-               return False;
+                        db_path, strerror(errno)));
+               TALLOC_FREE(db_path);
+               return map_nt_error_from_unix_common(errno);
+       }
+       TALLOC_FREE(db_path);
+
+       status = dbwrap_fetch_int32_bystring(share_db, vstring, &vers_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               vers_id = 0;
        }
-       /* handle a Samba upgrade */
-       tdb_lock_bystring(share_tdb, vstring);
+
+       if (vers_id == SHARE_DATABASE_VERSION_V3) {
+               return NT_STATUS_OK;
+       }
+
+       if (dbwrap_transaction_start(share_db) != 0) {
+               DEBUG(0, ("transaction_start failed\n"));
+               TALLOC_FREE(share_db);
+               return NT_STATUS_INTERNAL_DB_ERROR;
+       }
+
+       status = dbwrap_fetch_int32_bystring(share_db, vstring, &vers_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               vers_id = 0;
+       }
+
+       if (vers_id == SHARE_DATABASE_VERSION_V3) {
+               /*
+                * Race condition
+                */
+               if (dbwrap_transaction_cancel(share_db)) {
+                       smb_panic("transaction_cancel failed");
+               }
+               return NT_STATUS_OK;
+       }
+
+       /* Move to at least V2. */
 
        /* Cope with byte-reversed older versions of the db. */
-       vers_id = tdb_fetch_int32(share_tdb, vstring);
        if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
                /* Written on a bigendian machine with old fetch_int code. Save as le. */
-               tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
+
+               status = dbwrap_store_int32_bystring(
+                       share_db, vstring, SHARE_DATABASE_VERSION_V2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
+                                 nt_errstr(status)));
+                       goto cancel;
+               }
                vers_id = SHARE_DATABASE_VERSION_V2;
        }
 
        if (vers_id != SHARE_DATABASE_VERSION_V2) {
-               tdb_traverse(share_tdb, tdb_traverse_delete_fn, NULL);
-               tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
+               status = dbwrap_traverse(share_db, delete_fn, NULL, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("traverse failed\n"));
+                       goto cancel;
+               }
+               status = dbwrap_store_int32_bystring(
+                       share_db, vstring, SHARE_DATABASE_VERSION_V2);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
+                                 nt_errstr(status)));
+                       goto cancel;
+               }
        }
-       tdb_unlock_bystring(share_tdb, vstring);
 
-       return True;
+       /* Finally upgrade to version 3, with canonicalized sharenames. */
+
+       status = dbwrap_traverse(share_db, upgrade_v2_to_v3, &upgrade_ok, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("traverse failed\n"));
+               goto cancel;
+       }
+       if (!upgrade_ok) {
+               DBG_ERR("upgrade failed.\n");
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto cancel;
+       }
+
+       status = dbwrap_store_int32_bystring(
+               share_db, vstring, SHARE_DATABASE_VERSION_V3);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("dbwrap_store_int32 failed: %s\n",
+                         nt_errstr(status)));
+               goto cancel;
+       }
+
+       if (dbwrap_transaction_commit(share_db) != 0) {
+               DEBUG(0, ("transaction_commit failed\n"));
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       return NT_STATUS_OK;
+
+ cancel:
+       if (dbwrap_transaction_cancel(share_db)) {
+               smb_panic("transaction_cancel failed");
+       }
+
+       return status;
 }
 
 /*******************************************************************
@@ -78,17 +267,17 @@ static bool share_info_db_init(void)
  def_access is a GENERIC_XXX access mode.
  ********************************************************************/
 
-SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
+struct security_descriptor *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32_t def_access)
 {
-       SEC_ACCESS sa;
-       SEC_ACE ace;
-       SEC_ACL *psa = NULL;
-       SEC_DESC *psd = NULL;
-       uint32 spec_access = def_access;
+       uint32_t sa;
+       struct security_ace ace;
+       struct security_acl *psa = NULL;
+       struct security_descriptor *psd = NULL;
+       uint32_t spec_access = def_access;
 
        se_map_generic(&spec_access, &file_generic_mapping);
 
-       init_sec_access(&sa, def_access | spec_access );
+       sa = (def_access | spec_access );
        init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
 
        if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
@@ -109,42 +298,57 @@ SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def
  Pull a security descriptor from the share tdb.
  ********************************************************************/
 
-SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
+struct security_descriptor *get_share_security( TALLOC_CTX *ctx, const char *servicename,
                              size_t *psize)
 {
        char *key;
-       SEC_DESC *psd = NULL;
+       struct security_descriptor *psd = NULL;
        TDB_DATA data;
+       char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
        NTSTATUS status;
 
-       if (!share_info_db_init()) {
+       if (!c_servicename) {
+               return NULL;
+       }
+
+       status = share_info_db_init();
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(c_servicename);
                return NULL;
        }
 
-       if (!(key = talloc_asprintf(ctx, "SECDESC/%s", servicename))) {
+       if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
+               TALLOC_FREE(c_servicename);
                DEBUG(0, ("talloc_asprintf failed\n"));
                return NULL;
        }
 
-       data = tdb_fetch_bystring(share_tdb, key);
+       TALLOC_FREE(c_servicename);
+
+       status = dbwrap_fetch_bystring(share_db, talloc_tos(), key, &data);
 
        TALLOC_FREE(key);
 
-       if (data.dptr == NULL) {
+       if (!NT_STATUS_IS_OK(status)) {
                return get_share_security_default(ctx, psize,
-                                                 GENERIC_ALL_ACCESS);
+                                                 SEC_RIGHTS_DIR_ALL);
        }
 
        status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
 
+       TALLOC_FREE(data.dptr);
+
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
-                         nt_errstr(status)));
-               return NULL;
+               return get_share_security_default(ctx, psize,
+                                                 SEC_RIGHTS_DIR_ALL);
        }
 
-       if (psd)
+       if (psd) {
                *psize = ndr_size_security_descriptor(psd, 0);
+       } else {
+               return get_share_security_default(ctx, psize,
+                                                 SEC_RIGHTS_DIR_ALL);
+       }
 
        return psd;
 }
@@ -153,19 +357,24 @@ SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
  Store a security descriptor in the share db.
  ********************************************************************/
 
-bool set_share_security(const char *share_name, SEC_DESC *psd)
+NTSTATUS set_share_security(const char *share_name,
+                           struct security_descriptor *psd)
 {
-       TALLOC_CTX *frame;
+       TALLOC_CTX *frame = talloc_stackframe();
        char *key;
-       bool ret = False;
        TDB_DATA blob;
        NTSTATUS status;
+       char *c_share_name = canonicalize_servicename(frame, share_name);
 
-       if (!share_info_db_init()) {
-               return False;
+       if (c_share_name == NULL) {
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto out;
        }
 
-       frame = talloc_stackframe();
+       status = share_info_db_init();
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
 
        status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
 
@@ -175,88 +384,114 @@ bool set_share_security(const char *share_name, SEC_DESC *psd)
                goto out;
        }
 
-       if (!(key = talloc_asprintf(frame, "SECDESC/%s", share_name))) {
+       if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
                DEBUG(0, ("talloc_asprintf failed\n"));
+               status = NT_STATUS_NO_MEMORY;
                goto out;
        }
 
-       if (tdb_trans_store_bystring(share_tdb, key, blob,
-                                    TDB_REPLACE) == -1) {
-               DEBUG(1,("set_share_security: Failed to store secdesc for "
-                        "%s\n", share_name ));
+       status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
+                                   TDB_REPLACE);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("set_share_security: Failed to store secdesc for "
+                         "%s: %s\n", share_name, nt_errstr(status)));
                goto out;
        }
 
        DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
-       ret = True;
+       status = NT_STATUS_OK;
 
  out:
        TALLOC_FREE(frame);
-       return ret;
+       return status;
 }
 
 /*******************************************************************
  Delete a security descriptor.
 ********************************************************************/
 
-bool delete_share_security(const char *servicename)
+NTSTATUS delete_share_security(const char *servicename)
 {
        TDB_DATA kbuf;
        char *key;
+       NTSTATUS status;
+       char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
 
-       if (!(key = talloc_asprintf(talloc_tos(), "SECDESC/%s",
-                                   servicename))) {
-               return False;
+       if (c_servicename == NULL) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       status = share_info_db_init();
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(c_servicename);
+               return status;
+       }
+
+       if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
+                                   c_servicename))) {
+               TALLOC_FREE(c_servicename);
+               return NT_STATUS_NO_MEMORY;
        }
        kbuf = string_term_tdb_data(key);
 
-       if (tdb_trans_delete(share_tdb, kbuf) != 0) {
+       status = dbwrap_trans_delete(share_db, kbuf);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("delete_share_security: Failed to delete entry for "
-                         "share %s\n", servicename));
-               return False;
+                         "share %s: %s\n", c_servicename, nt_errstr(status)));
+               TALLOC_FREE(c_servicename);
+               return status;
        }
 
-       return True;
+       TALLOC_FREE(c_servicename);
+       return NT_STATUS_OK;
 }
 
 /*******************************************************************
  Can this user access with share with the required permissions ?
 ********************************************************************/
 
-bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
-                       uint32 desired_access)
+bool share_access_check(const struct security_token *token,
+                       const char *sharename,
+                       uint32_t desired_access,
+                       uint32_t *pgranted)
 {
-       uint32 granted;
+       uint32_t granted;
        NTSTATUS status;
-       SEC_DESC *psd = NULL;
+       struct security_descriptor *psd = NULL;
        size_t sd_size;
-       bool ret = True;
 
        psd = get_share_security(talloc_tos(), sharename, &sd_size);
 
        if (!psd) {
-               return True;
+               if (pgranted != NULL) {
+                       *pgranted = desired_access;
+               }
+               return false;
        }
 
-       ret = se_access_check(psd, token, desired_access, &granted, &status);
+       status = se_file_access_check(psd, token, true, desired_access, &granted);
 
        TALLOC_FREE(psd);
 
-       return ret;
+       if (pgranted != NULL) {
+               *pgranted = granted;
+       }
+
+       return NT_STATUS_IS_OK(status);
 }
 
 /***************************************************************************
  Parse the contents of an acl string from a usershare file.
 ***************************************************************************/
 
-bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
+bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, struct security_descriptor **ppsd)
 {
        size_t s_size = 0;
        const char *pacl = acl_str;
        int num_aces = 0;
-       SEC_ACE *ace_list = NULL;
-       SEC_ACL *psa = NULL;
-       SEC_DESC *psd = NULL;
+       struct security_ace *ace_list = NULL;
+       struct security_acl *psa = NULL;
+       struct security_descriptor *psd = NULL;
        size_t sd_size = 0;
        int i;
 
@@ -264,7 +499,7 @@ bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
 
        /* If the acl string is blank return "Everyone:R" */
        if (!*acl_str) {
-               SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
+               struct security_descriptor *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
                if (!default_psd) {
                        return False;
                }
@@ -277,16 +512,16 @@ bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
        /* Add the number of ',' characters to get the number of aces. */
        num_aces += count_chars(pacl,',');
 
-       ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
+       ace_list = talloc_array(ctx, struct security_ace, num_aces);
        if (!ace_list) {
                return False;
        }
 
        for (i = 0; i < num_aces; i++) {
-               SEC_ACCESS sa;
-               uint32 g_access;
-               uint32 s_access;
-               DOM_SID sid;
+               uint32_t sa;
+               uint32_t g_access;
+               uint32_t s_access;
+               struct dom_sid sid;
                char *sidstr;
                enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
 
@@ -331,7 +566,7 @@ bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
                pacl++; /* Go past any ',' */
 
                se_map_generic(&s_access, &file_generic_mapping);
-               init_sec_access(&sa, g_access | s_access );
+               sa = (g_access | s_access);
                init_sec_ace(&ace_list[i], &sid, type, sa, 0);
        }