s3: Fix some C++ warnings
[ira/wip.git] / source3 / groupdb / mapping_tdb.c
index 7c864e13401ac8c8fdd7e81a638ade7c2a99f2df..ee8d7526a0a49df423179bb467d42db50919502b 100644 (file)
 
 static struct db_context *db; /* used for driver files */
 
-static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
-                              size_t *p_num_entries, bool unix_only);
+static bool enum_group_mapping(const DOM_SID *domsid,
+                              enum lsa_SidType sid_name_use,
+                              GROUP_MAP **pp_rmap,
+                              size_t *p_num_entries,
+                              bool unix_only);
 static bool group_map_remove(const DOM_SID *sid);
-       
+
+static bool mapping_switch(const char *ldb_path);
+
 /****************************************************************************
  Open the group mapping tdb.
 ****************************************************************************/
 static bool init_group_mapping(void)
 {
+       const char *ldb_path;
+
        if (db != NULL) {
                return true;
        }
 
-       db = db_open_trans(NULL, state_path("group_mapping.tdb"), 0,
+       db = db_open(NULL, state_path("group_mapping.tdb"), 0,
                           TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
        if (db == NULL) {
                DEBUG(0, ("Failed to open group mapping database: %s\n",
@@ -46,13 +53,14 @@ static bool init_group_mapping(void)
                return false;
        }
 
-#if 0
-       /*
-        * This code was designed to handle a group mapping version
-        * upgrade. mapping_tdb is not active by default anymore, so ignore
-        * this here.
-        */
-       {
+       ldb_path = state_path("group_mapping.ldb");
+       if (file_exist(ldb_path) && !mapping_switch(ldb_path)) {
+               unlink(state_path("group_mapping.tdb"));
+               return false;
+
+       } else {
+               /* handle upgrade from old versions of the database */
+#if 0 /* -- Needs conversion to dbwrap -- */
                const char *vstring = "INFO/version";
                int32 vers_id;
                GROUP_MAP *map_table = NULL;
@@ -96,9 +104,8 @@ static bool init_group_mapping(void)
 
                        SAFE_FREE( map_table );
                }
-       }
 #endif
-
+       }
        return true;
 }
 
@@ -111,7 +118,7 @@ static char *group_mapping_key(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
                return NULL;
        }
 
-       result = talloc_asprintf(mem_ctx, "GROUP_PREFIX%s", sidstr);
+       result = talloc_asprintf(mem_ctx, "%s%s", GROUP_PREFIX, sidstr);
 
        TALLOC_FREE(sidstr);
        return result;
@@ -123,11 +130,11 @@ static bool add_mapping_entry(GROUP_MAP *map, int flag)
 {
        char *key, *buf;
        int len;
-       int res;
+       NTSTATUS status;
 
        key = group_mapping_key(talloc_tos(), &map->sid);
        if (key == NULL) {
-               return NULL;
+               return false;
        }
 
        len = tdb_pack(NULL, 0, "ddff",
@@ -141,13 +148,13 @@ static bool add_mapping_entry(GROUP_MAP *map, int flag)
        len = tdb_pack((uint8 *)buf, len, "ddff", map->gid,
                       map->sid_name_use, map->nt_name, map->comment);
 
-       res = dbwrap_trans_store(
+       status = dbwrap_trans_store(
                db, string_term_tdb_data(key),
-               make_tdb_data((uint8_t *)buf, len), flag);
+               make_tdb_data((uint8_t *)buf, len), TDB_REPLACE);
 
        TALLOC_FREE(key);
 
-       return (res == 0);
+       return NT_STATUS_IS_OK(status);
 }
 
 
@@ -163,7 +170,7 @@ static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
 
        /* the key is the SID, retrieving is direct */
 
-       key = group_mapping_key(talloc_tos(), &map->sid);
+       key = group_mapping_key(talloc_tos(), &sid);
        if (key == NULL) {
                return false;
        }
@@ -282,17 +289,17 @@ static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
 static bool group_map_remove(const DOM_SID *sid)
 {
        char *key;
-       int res;
+       NTSTATUS status;
 
        key = group_mapping_key(talloc_tos(), sid);
        if (key == NULL) {
                return false;
        }
 
-       res = dbwrap_trans_delete(db, string_term_tdb_data(key));
+       status = dbwrap_trans_delete(db, string_term_tdb_data(key));
 
        TALLOC_FREE(key);
-       return (res == 0);
+       return NT_STATUS_IS_OK(status);
 }
 
 /****************************************************************************
@@ -364,7 +371,7 @@ static bool enum_group_mapping(const DOM_SID *domsid,
        state.num_maps = 0;
        state.maps = NULL;
 
-       if (db->traverse_read(db, collect_map, (void *)&state) != 0) {
+       if (db->traverse_read(db, collect_map, (void *)&state) < 0) {
                return false;
        }
 
@@ -521,7 +528,7 @@ static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
        if (db->transaction_commit(db) != 0) {
                DEBUG(0, ("transaction_commit failed\n"));
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
-               goto cancel;
+               return status;
        }
 
        return NT_STATUS_OK;
@@ -535,6 +542,7 @@ static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
 }
 
 struct aliasmem_state {
+       TALLOC_CTX *mem_ctx;
        const DOM_SID *alias;
        DOM_SID **sids;
        size_t *num;
@@ -580,7 +588,7 @@ static int collect_aliasmem(struct db_record *rec, void *priv)
                if (!string_to_sid(&member, member_string))
                        continue;
 
-               if (!NT_STATUS_IS_OK(add_sid_to_array(NULL, &member,
+               if (!NT_STATUS_IS_OK(add_sid_to_array(state->mem_ctx, &member,
                                                      state->sids,
                                                      state->num)))
                {
@@ -593,7 +601,8 @@ static int collect_aliasmem(struct db_record *rec, void *priv)
        return 0;
 }
 
-static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
+static NTSTATUS enum_aliasmem(const DOM_SID *alias, TALLOC_CTX *mem_ctx,
+                             DOM_SID **sids, size_t *num)
 {
        GROUP_MAP map;
        struct aliasmem_state state;
@@ -611,6 +620,7 @@ static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
        state.alias = alias;
        state.sids = sids;
        state.num = num;
+       state.mem_ctx = mem_ctx;
 
        db->traverse_read(db, collect_aliasmem, &state);
        return NT_STATUS_OK;
@@ -666,8 +676,7 @@ static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
 
        if (num == 0) {
                status = dbwrap_delete_bystring(db, key);
-               TALLOC_FREE(sids);
-               goto cancel;
+               goto commit;
        }
 
        member_string = talloc_strdup(sids, "");
@@ -693,7 +702,7 @@ static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
 
        status = dbwrap_store_bystring(
                db, key, string_term_tdb_data(member_string), 0);
-
+ commit:
        TALLOC_FREE(sids);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -705,7 +714,7 @@ static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
        if (db->transaction_commit(db) != 0) {
                DEBUG(0, ("transaction_commit failed\n"));
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
-               goto cancel;
+               return status;
        }
 
        return NT_STATUS_OK;
@@ -717,6 +726,254 @@ static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
        return status;
 }
 
+
+/* -- ldb->tdb switching code -------------------------------------------- */
+
+/* change this if the data format ever changes */
+#define LTDB_PACKING_FORMAT 0x26011967
+
+/* old packing formats (not supported for now,
+ * it was never used for group mapping AFAIK) */
+#define LTDB_PACKING_FORMAT_NODN 0x26011966
+
+static unsigned int pull_uint32(uint8_t *p, int ofs)
+{
+       p += ofs;
+       return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
+}
+
+/*
+  unpack a ldb message from a linear buffer in TDB_DATA
+*/
+static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
+                             TDB_DATA data, void *ptr)
+{
+       TALLOC_CTX *tmp_ctx = talloc_tos();
+       GROUP_MAP map;
+       uint8_t *p;
+       uint32_t format;
+       uint32_t num_el;
+       unsigned int remaining;
+       unsigned int i, j;
+       size_t len;
+       char *name;
+       char *val;
+       char *q;
+       uint32_t num_mem = 0;
+       DOM_SID *members = NULL;
+
+       p = (uint8_t *)data.dptr;
+       if (data.dsize < 8) {
+               errno = EIO;
+               goto failed;
+       }
+
+       format = pull_uint32(p, 0);
+       num_el = pull_uint32(p, 4);
+       p += 8;
+
+       remaining = data.dsize - 8;
+
+       switch (format) {
+       case LTDB_PACKING_FORMAT:
+               len = strnlen((char *)p, remaining);
+               if (len == remaining) {
+                       errno = EIO;
+                       goto failed;
+               }
+
+               if (*p == '@') {
+                       /* ignore special LDB attributes */
+                       return 0;
+               }
+
+               if (strncmp((char *)p, "rid=", 4)) {
+                       /* unknown entry, ignore */
+                       DEBUG(3, ("Found unknown entry in group mapping "
+                                 "database named [%s]\n", (char *)p));
+                       return 0;
+               }
+
+               remaining -= len + 1;
+               p += len + 1;
+               break;
+
+       case LTDB_PACKING_FORMAT_NODN:
+       default:
+               errno = EIO;
+               goto failed;
+       }
+
+       if (num_el == 0) {
+               /* bad entry, ignore */
+               return 0;
+       }
+
+       if (num_el > remaining / 6) {
+               errno = EIO;
+               goto failed;
+       }
+
+       ZERO_STRUCT(map);
+
+       for (i = 0; i < num_el; i++) {
+               uint32_t num_vals;
+
+               if (remaining < 10) {
+                       errno = EIO;
+                       goto failed;
+               }
+               len = strnlen((char *)p, remaining - 6);
+               if (len == remaining - 6) {
+                       errno = EIO;
+                       goto failed;
+               }
+               name = talloc_strndup(tmp_ctx, (char *)p, len);
+               if (name == NULL) {
+                       errno = ENOMEM;
+                       goto failed;
+               }
+               remaining -= len + 1;
+               p += len + 1;
+
+               num_vals = pull_uint32(p, 0);
+               if (StrCaseCmp(name, "member") == 0) {
+                       num_mem = num_vals;
+                       members = talloc_array(tmp_ctx, DOM_SID, num_mem);
+                       if (members == NULL) {
+                               errno = ENOMEM;
+                               goto failed;
+                       }
+               } else if (num_vals != 1) {
+                       errno = EIO;
+                       goto failed;
+               }
+
+               p += 4;
+               remaining -= 4;
+
+               for (j = 0; j < num_vals; j++) {
+                       len = pull_uint32(p, 0);
+                       if (len > remaining-5) {
+                               errno = EIO;
+                               goto failed;
+                       }
+
+                       val = talloc_strndup(tmp_ctx, (char *)(p + 4), len);
+                       if (val == NULL) {
+                               errno = ENOMEM;
+                               goto failed;
+                       }
+
+                       remaining -= len+4+1;
+                       p += len+4+1;
+
+                       /* we ignore unknown or uninteresting attributes
+                        * (objectclass, etc.) */
+                       if (StrCaseCmp(name, "gidNumber") == 0) {
+                               map.gid = strtoul(val, &q, 10);
+                               if (*q) {
+                                       errno = EIO;
+                                       goto failed;
+                               }
+                       } else if (StrCaseCmp(name, "sid") == 0) {
+                               if (!string_to_sid(&map.sid, val)) {
+                                       errno = EIO;
+                                       goto failed;
+                               }
+                       } else if (StrCaseCmp(name, "sidNameUse") == 0) {
+                               map.sid_name_use = strtoul(val, &q, 10);
+                               if (*q) {
+                                       errno = EIO;
+                                       goto failed;
+                               }
+                       } else if (StrCaseCmp(name, "ntname") == 0) {
+                               strlcpy(map.nt_name, val,
+                                       sizeof(map.nt_name) -1);
+                       } else if (StrCaseCmp(name, "comment") == 0) {
+                               strlcpy(map.comment, val,
+                                       sizeof(map.comment) -1);
+                       } else if (StrCaseCmp(name, "member") == 0) {
+                               if (!string_to_sid(&members[j], val)) {
+                                       errno = EIO;
+                                       goto failed;
+                               }
+                       }
+
+                       TALLOC_FREE(val);
+               }
+
+               TALLOC_FREE(name);
+       }
+
+       if (!add_mapping_entry(&map, 0)) {
+               errno = EIO;
+               goto failed;
+       }
+
+       if (num_mem) {
+               for (j = 0; j < num_mem; j++) {
+                       NTSTATUS status;
+                       status = add_aliasmem(&map.sid, &members[j]);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               errno = EIO;
+                               goto failed;
+                       }
+               }
+       }
+
+       if (remaining != 0) {
+               DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
+                         remaining));
+       }
+
+       return 0;
+
+failed:
+       return -1;
+}
+
+static bool mapping_switch(const char *ldb_path)
+{
+       TDB_CONTEXT *ltdb;
+       TALLOC_CTX *frame;
+       char *new_path;
+       int ret;
+
+       frame = talloc_stackframe();
+
+       ltdb = tdb_open_log(ldb_path, 0, TDB_DEFAULT, O_RDONLY, 0600);
+       if (ltdb == NULL) goto failed;
+
+       /* ldb is just a very fancy tdb, read out raw data and perform
+        * conversion */
+       ret = tdb_traverse(ltdb, convert_ldb_record, NULL);
+       if (ret == -1) goto failed;
+
+       if (ltdb) {
+               tdb_close(ltdb);
+               ltdb = NULL;
+       }
+
+       /* now rename the old db out of the way */
+       new_path = state_path("group_mapping.ldb.replaced");
+       if (!new_path) {
+               goto failed;
+       }
+       if (rename(ldb_path, new_path) != 0) {
+               DEBUG(0,("Failed to rename old group mapping database\n"));
+               goto failed;
+       }
+       TALLOC_FREE(frame);
+       return True;
+
+failed:
+       DEBUG(0,("Failed to swith to tdb group mapping database\n"));
+       if (ltdb) tdb_close(ltdb);
+       TALLOC_FREE(frame);
+       return False;
+}
+
 static const struct mapping_backend tdb_backend = {
        .add_mapping_entry         = add_mapping_entry,
        .get_group_map_from_sid    = get_group_map_from_sid,