s3:idmap: remove unused method dump_data() from the idmap API
[amitay/samba.git] / source3 / winbindd / idmap_tdb.c
index 22c17578e6b8a99f839fec72c8c6855e942a4d83..6acf1e4573348cea0135dd48484d3a534a37a829 100644 (file)
@@ -60,7 +60,7 @@ static int convert_fn(struct db_record *rec, void *private_data)
        struct winbindd_domain *domain;
        char *p;
        NTSTATUS status;
-       DOM_SID sid;
+       struct dom_sid sid;
        uint32 rid;
        fstring keystr;
        fstring dom_name;
@@ -97,8 +97,7 @@ static int convert_fn(struct db_record *rec, void *private_data)
 
        rid = atoi(p);
 
-       sid_copy(&sid, &domain->sid);
-       sid_append_rid(&sid, rid);
+       sid_compose(&sid, &domain->sid, rid);
 
        sid_to_fstring(keystr, &sid);
        key2 = string_term_tdb_data(keystr);
@@ -196,7 +195,7 @@ static bool idmap_tdb_upgrade(struct db_context *db)
        }
 
        if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
-               DEBUG(0, ("Unable to dtore idmap version in databse\n"));
+               DEBUG(0, ("Unable to store idmap version in databse\n"));
                return False;
        }
 
@@ -296,7 +295,7 @@ static NTSTATUS idmap_tdb_open_db(TALLOC_CTX *memctx,
 
                if (!idmap_tdb_upgrade(db)) {
                        db->transaction_cancel(db);
-                       DEBUG(0, ("Unable to open idmap database, it's in an old formati, and upgrade failed!\n"));
+                       DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
                        ret = NT_STATUS_INTERNAL_DB_ERROR;
                        goto done;
                }
@@ -401,7 +400,7 @@ static NTSTATUS idmap_tdb_alloc_init( const char *params )
 
 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
 {
-       bool ret;
+       NTSTATUS ret;
        const char *hwmkey;
        const char *hwmtype;
        uint32_t high_hwm;
@@ -449,10 +448,11 @@ static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
 
        /* fetch a new id and increment it */
        ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
-       if (ret != 0) {
-               DEBUG(0, ("Fatal error while fetching a new %s value\n!", hwmtype));
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
+                         hwmtype, nt_errstr(ret)));
                idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_UNSUCCESSFUL;
+               return ret;
        }
 
        /* recheck it is in the range */
@@ -530,6 +530,7 @@ static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
        const char *hwmtype;
        uint32_t hwm;
        uint32_t high_hwm;
+       NTSTATUS ret;
 
        /* Get current high water mark */
        switch (xid->type) {
@@ -552,17 +553,15 @@ static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
 
        hwm = xid->id;
 
-       if ((hwm = dbwrap_store_uint32(idmap_alloc_db, hwmkey, hwm)) == -1) {
-               return NT_STATUS_INTERNAL_DB_ERROR;
-       }
-
        /* Warn if it is out of range */
        if (hwm >= high_hwm) {
                DEBUG(0, ("Warning: %s range full!! (max: %lu)\n", 
                          hwmtype, (unsigned long)high_hwm));
        }
 
-       return NT_STATUS_OK;
+       ret = dbwrap_trans_store_uint32(idmap_alloc_db, hwmkey, hwm);
+
+       return ret;
 }
 
 /**********************************
@@ -593,8 +592,8 @@ static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
 {
        NTSTATUS ret;
        struct idmap_tdb_context *ctx;
-       char *config_option = NULL;
-       const char *range;
+
+       DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
 
        ctx = talloc(dom, struct idmap_tdb_context);
        if ( ! ctx) {
@@ -602,29 +601,72 @@ static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
                return NT_STATUS_NO_MEMORY;
        }
 
-       config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
-       if ( ! config_option) {
-               DEBUG(0, ("Out of memory!\n"));
-               ret = NT_STATUS_NO_MEMORY;
-               goto failed;
-       }
+       if (strequal(dom->name, "*")) {
+               uid_t low_uid = 0;
+               uid_t high_uid = 0;
+               gid_t low_gid = 0;
+               gid_t high_gid = 0;
 
-       ret = idmap_tdb_open_db(ctx, false, &ctx->db);
-       if ( ! NT_STATUS_IS_OK(ret)) {
-               goto failed;
+               ctx->filter_low_id = 0;
+               ctx->filter_high_id = 0;
+
+               if (lp_idmap_uid(&low_uid, &high_uid)) {
+                       ctx->filter_low_id = low_uid;
+                       ctx->filter_high_id = high_uid;
+               } else {
+                       DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
+               }
+
+               if (lp_idmap_gid(&low_gid, &high_gid)) {
+                       if ((low_gid != low_uid) || (high_gid != high_uid)) {
+                               DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
+                                     " ranges do not agree -- building "
+                                     "intersection\n"));
+                               ctx->filter_low_id = MAX(ctx->filter_low_id,
+                                                        low_gid);
+                               ctx->filter_high_id = MIN(ctx->filter_high_id,
+                                                         high_gid);
+                       }
+               } else {
+                       DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
+               }
+       } else {
+               char *config_option = NULL;
+               const char *range;
+
+               config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
+               if ( ! config_option) {
+                       DEBUG(0, ("Out of memory!\n"));
+                       ret = NT_STATUS_NO_MEMORY;
+                       goto failed;
+               }
+
+               range = lp_parm_const_string(-1, config_option, "range", NULL);
+               if (( ! range) ||
+                   (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
+               {
+                       ctx->filter_low_id = 0;
+                       ctx->filter_high_id = 0;
+               }
+
+               talloc_free(config_option);
        }
 
-       range = lp_parm_const_string(-1, config_option, "range", NULL);
-       if (( ! range) ||
-           (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
-           (ctx->filter_low_id > ctx->filter_high_id)) {
+       if (ctx->filter_low_id > ctx->filter_high_id) {
                ctx->filter_low_id = 0;
                ctx->filter_high_id = 0;
        }
 
+       DEBUG(10, ("idmap_tdb_db_init: filter range %u-%u loaded for domain "
+             "'%s'\n", ctx->filter_low_id, ctx->filter_high_id, dom->name));
+
+       ret = idmap_tdb_open_db(ctx, false, &ctx->db);
+       if ( ! NT_STATUS_IS_OK(ret)) {
+               goto failed;
+       }
+
        dom->private_data = ctx;
 
-       talloc_free(config_option);
        return NT_STATUS_OK;
 
 failed:
@@ -750,6 +792,7 @@ static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map
        } else { /* Unknown record type ! */
                DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
                ret = NT_STATUS_INTERNAL_DB_ERROR;
+               goto done;
        }
 
        /* apply filters before returning result */
@@ -946,124 +989,6 @@ done:
        return ret;
 }
 
-/**********************************
- remove a mapping.
-**********************************/
-
-static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom,
-                                        const struct id_map *map)
-{
-       struct idmap_tdb_context *ctx;
-       NTSTATUS ret;
-       TDB_DATA ksid, kid, data;
-       char *ksidstr, *kidstr;
-       fstring tmp;
-
-       if (!map || !map->sid) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       ksidstr = kidstr = NULL;
-       data.dptr = NULL;
-
-       /* TODO: should we filter a remove_mapping using low/high filters ? */
-
-       ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
-
-       switch (map->xid.type) {
-
-       case ID_TYPE_UID:
-               kidstr = talloc_asprintf(ctx, "UID %lu",
-                                        (unsigned long)map->xid.id);
-               break;
-
-       case ID_TYPE_GID:
-               kidstr = talloc_asprintf(ctx, "GID %lu",
-                                        (unsigned long)map->xid.id);
-               break;
-
-       default:
-               DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       if (kidstr == NULL) {
-               DEBUG(0, ("ERROR: Out of memory!\n"));
-               ret = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       if ((ksidstr = talloc_asprintf(
-                    ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
-               DEBUG(0, ("Out of memory!\n"));
-               ret = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
-       ksid = string_term_tdb_data(ksidstr);
-       kid = string_term_tdb_data(kidstr);
-
-       if (ctx->db->transaction_start(ctx->db) != 0) {
-               DEBUG(0, ("Failed to start transaction for %s\n",
-                         ksidstr));
-               return NT_STATUS_INTERNAL_DB_ERROR;
-       }
-
-       /* Check if sid is present in database */
-       data = dbwrap_fetch(ctx->db, NULL, ksid);
-       if (!data.dptr) {
-               ctx->db->transaction_cancel(ctx->db);
-               DEBUG(10,("Record %s not found\n", ksidstr));
-               ret = NT_STATUS_NONE_MAPPED;
-               goto done;
-       }
-
-       /* Check if sid is mapped to the specified ID */
-       if ((data.dsize != kid.dsize) ||
-           (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
-               ctx->db->transaction_cancel(ctx->db);
-               DEBUG(10,("Specified SID does not map to specified ID\n"));
-               DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr,
-                        (const char *)data.dptr));
-               ret = NT_STATUS_NONE_MAPPED;
-               goto done;
-       }
-
-       DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
-
-       /* Delete previous mappings. */
-
-       DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
-       ret = dbwrap_delete(ctx->db, ksid);
-       if (!NT_STATUS_IS_OK(ret)) {
-               DEBUG(0,("Warning: Failed to delete %s: %s\n",
-                        ksidstr, nt_errstr(ret)));
-       }
-
-       DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
-       ret = dbwrap_delete(ctx->db, kid);
-       if (!NT_STATUS_IS_OK(ret)) {
-               DEBUG(0,("Warning: Failed to delete %s: %s\n",
-                        kidstr, nt_errstr(ret)));
-       }
-
-       if (ctx->db->transaction_commit(ctx->db) != 0) {
-               DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
-                         ksidstr, kidstr));
-               ret = NT_STATUS_INTERNAL_DB_ERROR;
-               goto done;
-       }
-
-       ret = NT_STATUS_OK;
-
-done:
-       talloc_free(ksidstr);
-       talloc_free(kidstr);
-       talloc_free(data.dptr);
-       return ret;
-}
-
 /**********************************
  Close the idmap tdb instance
 **********************************/
@@ -1080,107 +1005,12 @@ static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
        return NT_STATUS_OK;
 }
 
-struct dump_data {
-       TALLOC_CTX *memctx;
-       struct id_map **maps;
-       int *num_maps;
-       NTSTATUS ret;
-};
-
-static int idmap_tdb_dump_one_entry(struct db_record *rec, void *pdata)
-{
-       struct dump_data *data = talloc_get_type(pdata, struct dump_data);
-       struct id_map *maps;
-       int num_maps = *data->num_maps;
-
-       /* ignore any record but the ones with a SID as key */
-       if (strncmp((const char *)rec->key.dptr, "S-", 2) == 0) {
-
-               maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
-               if ( ! maps) {
-                       DEBUG(0, ("Out of memory!\n"));
-                       data->ret = NT_STATUS_NO_MEMORY;
-                       return -1;
-               }
-                       *data->maps = maps;
-               maps[num_maps].sid = talloc(maps, DOM_SID);
-               if ( ! maps[num_maps].sid) {
-                       DEBUG(0, ("Out of memory!\n"));
-                       data->ret = NT_STATUS_NO_MEMORY;
-                       return -1;
-               }
-
-               if (!string_to_sid(maps[num_maps].sid, (const char *)rec->key.dptr)) {
-                       DEBUG(10,("INVALID record %s\n", (const char *)rec->key.dptr));
-                       /* continue even with errors */
-                       return 0;
-               }
-
-               /* Try a UID record. */
-               if (sscanf((const char *)rec->value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
-                       maps[num_maps].xid.type = ID_TYPE_UID;
-                       maps[num_maps].status = ID_MAPPED;
-                       *data->num_maps = num_maps + 1;
-
-               /* Try a GID record. */
-               } else
-               if (sscanf((const char *)rec->value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
-                       maps[num_maps].xid.type = ID_TYPE_GID;
-                       maps[num_maps].status = ID_MAPPED;
-                       *data->num_maps = num_maps + 1;
-
-               /* Unknown record type ! */
-               } else {
-                       maps[num_maps].status = ID_UNKNOWN;
-                       DEBUG(2, ("Found INVALID record %s -> %s\n",
-                               (const char *)rec->key.dptr,
-                               (const char *)rec->value.dptr));
-                       /* do not increment num_maps */
-               }
-       }
-
-       return 0;
-}
-
-/**********************************
- Dump all mappings out
-**********************************/
-
-static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
-{
-       struct idmap_tdb_context *ctx;
-       struct dump_data *data;
-       NTSTATUS ret = NT_STATUS_OK;
-
-       ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
-
-       data = TALLOC_ZERO_P(ctx, struct dump_data);
-       if ( ! data) {
-               DEBUG(0, ("Out of memory!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-       data->maps = maps;
-       data->num_maps = num_maps;
-       data->ret = NT_STATUS_OK;
-
-       ctx->db->traverse_read(ctx->db, idmap_tdb_dump_one_entry, data);
-
-       if ( ! NT_STATUS_IS_OK(data->ret)) {
-               ret = data->ret;
-       }
-
-       talloc_free(data);
-       return ret;
-}
-
 static struct idmap_methods db_methods = {
 
        .init = idmap_tdb_db_init,
        .unixids_to_sids = idmap_tdb_unixids_to_sids,
        .sids_to_unixids = idmap_tdb_sids_to_unixids,
        .set_mapping = idmap_tdb_set_mapping,
-       .remove_mapping = idmap_tdb_remove_mapping,
-       .dump_data = idmap_tdb_dump_data,
        .close_fn = idmap_tdb_close
 };