s3:idmap_tdb: use filter from idmap_domain rather than from idmap_tdb_context
[amitay/samba.git] / source3 / winbindd / idmap_tdb.c
index 095889f2550179f2fcd46c25c71f817718318e81..5f769206f5cbe49bdab52bf069f92ecc99758ba2 100644 (file)
@@ -398,14 +398,67 @@ static NTSTATUS idmap_tdb_alloc_init( const char *params )
  Allocate a new id. 
 **********************************/
 
-static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
+struct idmap_tdb_allocate_id_context {
+       const char *hwmkey;
+       const char *hwmtype;
+       uint32_t high_hwm;
+       uint32_t hwm;
+};
+
+static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
+                                            void *private_data)
 {
        NTSTATUS ret;
+       struct idmap_tdb_allocate_id_context *state;
+       uint32_t hwm;
+
+       state = (struct idmap_tdb_allocate_id_context *)private_data;
+
+       hwm = dbwrap_fetch_int32(db, state->hwmkey);
+       if (hwm == -1) {
+               ret = NT_STATUS_INTERNAL_DB_ERROR;
+               goto done;
+       }
+
+       /* check it is in the range */
+       if (hwm > state->high_hwm) {
+               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
+                         state->hwmtype, (unsigned long)state->high_hwm));
+               ret = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
+       /* fetch a new id and increment it */
+       ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
+                         state->hwmtype, nt_errstr(ret)));
+               goto done;
+       }
+
+       /* recheck it is in the range */
+       if (hwm > state->high_hwm) {
+               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
+                         state->hwmtype, (unsigned long)state->high_hwm));
+               ret = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
+       ret = NT_STATUS_OK;
+       state->hwm = hwm;
+
+done:
+       return ret;
+}
+
+static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
+{
        const char *hwmkey;
        const char *hwmtype;
        uint32_t high_hwm;
-       uint32_t hwm;
-       int res;
+       uint32_t hwm = 0;
+       NTSTATUS status;
+       struct idmap_tdb_allocate_id_context state;
 
        /* Get current high water mark */
        switch (xid->type) {
@@ -427,52 +480,46 @@ static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       res = idmap_alloc_db->transaction_start(idmap_alloc_db);
-       if (res != 0) {
-               DEBUG(1, (__location__ " Failed to start transaction.\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
+       state.hwm = hwm;
+       state.high_hwm = high_hwm;
+       state.hwmtype = hwmtype;
+       state.hwmkey = hwmkey;
 
-       if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_INTERNAL_DB_ERROR;
-       }
+       status = dbwrap_trans_do(idmap_alloc_db, idmap_tdb_allocate_id_action,
+                                &state);
 
-       /* check it is in the range */
-       if (hwm > high_hwm) {
-               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n", 
-                         hwmtype, (unsigned long)high_hwm));
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_UNSUCCESSFUL;
+       if (NT_STATUS_IS_OK(status)) {
+               xid->id = state.hwm;
+               DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
+       } else {
+               DEBUG(1, ("Error allocating a new %s\n", hwmtype));
        }
 
-       /* fetch a new id and increment it */
-       ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
-       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 ret;
-       }
+       return status;
+}
 
-       /* recheck it is in the range */
-       if (hwm > high_hwm) {
-               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n", 
-                         hwmtype, (unsigned long)high_hwm));
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
+/**
+ * Allocate a new unix-ID.
+ * For now this is for the default idmap domain only.
+ * Should be extended later on.
+ */
+static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
+                                    struct unixid *id)
+{
+       NTSTATUS ret;
 
-       res = idmap_alloc_db->transaction_commit(idmap_alloc_db);
-       if (res != 0) {
-               DEBUG(1, (__location__ " Failed to commit transaction.\n"));
-               return NT_STATUS_UNSUCCESSFUL;
+       if (!strequal(dom->name, "*")) {
+               DEBUG(3, ("idmap_tdb_get_new_id: "
+                         "Refusing allocation of a new unixid for domain'%s'. "
+                         "Currently only supported for the default "
+                         "domain \"*\".\n",
+                          dom->name));
+               return NT_STATUS_NOT_IMPLEMENTED;
        }
 
-       xid->id = hwm;
-       DEBUG(10,("New %s = %d\n", hwmtype, hwm));
+       ret = idmap_tdb_allocate_id(id);
 
-       return NT_STATUS_OK;
+       return ret;
 }
 
 /**********************************
@@ -589,21 +636,23 @@ failed:
  Single id to sid lookup function. 
 **********************************/
 
-static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
+static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
 {
        NTSTATUS ret;
        TDB_DATA data;
        char *keystr;
+       struct idmap_tdb_context *ctx;
 
-       if (!ctx || !map) {
+       if (!dom || !map) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
+
        /* apply filters before checking */
-       if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
-           (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
+       if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
                DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
-                               map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
+                               map->xid.id, dom->low_id, dom->high_id));
                return NT_STATUS_NONE_MAPPED;
        }
 
@@ -662,14 +711,17 @@ done:
  Single sid to id lookup function. 
 **********************************/
 
-static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
+static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
 {
        NTSTATUS ret;
        TDB_DATA data;
        char *keystr;
        unsigned long rec_id = 0;
+       struct idmap_tdb_context *ctx;
        TALLOC_CTX *tmp_ctx = talloc_stackframe();
 
+       ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
+
        keystr = sid_string_talloc(tmp_ctx, map->sid);
        if (keystr == NULL) {
                DEBUG(0, ("Out of memory!\n"));
@@ -707,10 +759,9 @@ static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map
        }
 
        /* apply filters before returning result */
-       if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
-           (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
+       if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
                DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
-                               map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
+                               map->xid.id, dom->low_id, dom->high_id));
                ret = NT_STATUS_NONE_MAPPED;
        }
 
@@ -737,7 +788,7 @@ static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_ma
        ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
 
        for (i = 0; ids[i]; i++) {
-               ret = idmap_tdb_id_to_sid(ctx, ids[i]);
+               ret = idmap_tdb_id_to_sid(dom, ids[i]);
                if ( ! NT_STATUS_IS_OK(ret)) {
 
                        /* if it is just a failed mapping continue */
@@ -780,7 +831,7 @@ static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_ma
        ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
 
        for (i = 0; ids[i]; i++) {
-               ret = idmap_tdb_sid_to_id(ctx, ids[i]);
+               ret = idmap_tdb_sid_to_id(dom, ids[i]);
                if ( ! NT_STATUS_IS_OK(ret)) {
 
                        /* if it is just a failed mapping continue */
@@ -917,36 +968,16 @@ static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
 }
 
 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,
+       .allocate_id = idmap_tdb_get_new_id,
        .close_fn = idmap_tdb_close
 };
 
-static struct idmap_alloc_methods db_alloc_methods = {
-
-       .init = idmap_tdb_alloc_init,
-       .allocate_id = idmap_tdb_allocate_id,
-       .close_fn = idmap_tdb_alloc_close
-};
-
-NTSTATUS idmap_alloc_tdb_init(void)
-{
-       return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_alloc_methods);
-}
-
 NTSTATUS idmap_tdb_init(void)
 {
-       NTSTATUS ret;
-
        DEBUG(10, ("calling idmap_tdb_init\n"));
 
-       /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
-       ret = idmap_alloc_tdb_init();
-       if (! NT_STATUS_IS_OK(ret)) {
-               return ret;
-       }
        return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
 }