s3:winbindd/idmap make idmap modules loadable again
[kai/samba.git] / source3 / winbindd / idmap_autorid.c
index 80d8ed1ec2d10c3c9bdd507d6458cea72a2654ff..04c64354b7bc07e4cdaf727080e46697f0f9cbc1 100644 (file)
@@ -25,7 +25,8 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include "winbindd.h"
-#include "dbwrap.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
 #include "idmap.h"
 #include "../libcli/security/dom_sid.h"
 #include "util_tdb.h"
@@ -63,13 +64,15 @@ static NTSTATUS idmap_autorid_get_domainrange(struct db_context *db,
        cfg = (struct autorid_domain_config *)private_data;
        dom_sid_string_buf(&(cfg->sid), sidstr, sizeof(sidstr));
 
-       if (!dbwrap_fetch_uint32(db, sidstr, &domainnum)) {
+       ret = dbwrap_fetch_uint32(db, sidstr, &domainnum);
+       if (!NT_STATUS_IS_OK(ret)) {
                DEBUG(10, ("Acquiring new range for domain %s\n", sidstr));
 
                /* fetch the current HWM */
-               if (!dbwrap_fetch_uint32(db, HWM, &hwm)) {
+               ret = dbwrap_fetch_uint32(db, HWM, &hwm);
+               if (!NT_STATUS_IS_OK(ret)) {
                        DEBUG(1, ("Fatal error while fetching current "
-                                 "HWM value!\n"));
+                                 "HWM value: %s\n", nt_errstr(ret)));
                        ret = NT_STATUS_INTERNAL_ERROR;
                        goto error;
                }
@@ -133,6 +136,7 @@ static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
        TDB_DATA data;
        char *keystr;
        struct dom_sid sid;
+       NTSTATUS status;
 
        /* can this be one of our ids? */
        if (map->xid.id < cfg->minvalue) {
@@ -157,10 +161,10 @@ static NTSTATUS idmap_autorid_id_to_sid(struct autorid_global_config *cfg,
                return NT_STATUS_NO_MEMORY;
        }
 
-       data = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr);
+       status = dbwrap_fetch_bystring(autorid_db, talloc_tos(), keystr, &data);
        TALLOC_FREE(keystr);
 
-       if (!data.dptr) {
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(4, ("id %d belongs to range %d which does not have "
                          "domain mapping, ignoring mapping request\n",
                          map->xid.id, range));
@@ -332,6 +336,7 @@ static NTSTATUS idmap_autorid_sids_to_unixids(struct idmap_domain *dom,
 static NTSTATUS idmap_autorid_db_init(void)
 {
        int32_t hwm;
+       NTSTATUS status;
 
        if (autorid_db) {
                /* its already open */
@@ -349,15 +354,21 @@ static NTSTATUS idmap_autorid_db_init(void)
        }
 
        /* Initialize high water mark for the currently used range to 0 */
-       hwm = dbwrap_fetch_int32(autorid_db, HWM);
-       if ((hwm < 0)) {
-               if (!NT_STATUS_IS_OK
-                   (dbwrap_trans_store_int32(autorid_db, HWM, 0))) {
+       status = dbwrap_fetch_int32(autorid_db, HWM, &hwm);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
+           (NT_STATUS_IS_OK(status) && (hwm < 0)))
+       {
+               status = dbwrap_trans_store_int32(autorid_db, HWM, 0);
+               if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,
                              ("Unable to initialise HWM in autorid "
-                              "database\n"));
+                              "database: %s\n", nt_errstr(status)));
                        return NT_STATUS_INTERNAL_DB_ERROR;
                }
+       } else if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("unable to fetch HWM from autorid database: %s\n",
+                         nt_errstr(status)));
+               return status;
        }
 
        return NT_STATUS_OK;
@@ -369,15 +380,16 @@ static struct autorid_global_config *idmap_autorid_loadconfig(TALLOC_CTX * ctx)
        TDB_DATA data;
        struct autorid_global_config *cfg;
        unsigned long minvalue, rangesize, maxranges;
+       NTSTATUS status;
 
-       data = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY);
+       status = dbwrap_fetch_bystring(autorid_db, ctx, CONFIGKEY, &data);
 
-       if (!data.dptr) {
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("No saved config found\n"));
                return NULL;
        }
 
-       cfg = TALLOC_ZERO_P(ctx, struct autorid_global_config);
+       cfg = talloc_zero(ctx, struct autorid_global_config);
        if (!cfg) {
                return NULL;
        }
@@ -435,22 +447,18 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
        struct autorid_global_config *storedconfig = NULL;
        NTSTATUS status;
        uint32_t hwm;
-       TALLOC_CTX *frame = talloc_stackframe();
-       char *config_option = NULL;
 
        if (!strequal(dom->name, "*")) {
                DEBUG(0, ("idmap_autorid_initialize: Error: autorid configured "
                          "for domain '%s'. But autorid can only be used for "
                          "the default idmap configuration.\n", dom->name));
-               status = NT_STATUS_INVALID_PARAMETER;
-               goto error;
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       config = TALLOC_ZERO_P(frame, struct autorid_global_config);
+       config = talloc_zero(dom, struct autorid_global_config);
        if (!config) {
                DEBUG(0, ("Out of memory!\n"));
-               status = NT_STATUS_NO_MEMORY;
-               goto error;
+               return NT_STATUS_NO_MEMORY;
        }
 
        status = idmap_autorid_db_init();
@@ -458,15 +466,8 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
                goto error;
        }
 
-       config_option = talloc_asprintf(frame, "idmap config %s", dom->name);
-       if (config_option == NULL) {
-               DEBUG(0, ("Out of memory!\n"));
-               status = NT_STATUS_NO_MEMORY;
-               goto error;
-       }
-
        config->minvalue = dom->low_id;
-       config->rangesize = lp_parm_int(-1, config_option, "rangesize", 100000);
+       config->rangesize = lp_parm_int(-1, "idmap config *", "rangesize", 100000);
 
        if (config->rangesize < 2000) {
                DEBUG(1, ("autorid rangesize must be at least 2000\n"));
@@ -498,11 +499,12 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
                   config->minvalue, config->rangesize, config->maxranges));
 
        /* read previously stored config and current HWM */
-       storedconfig = idmap_autorid_loadconfig(frame);
+       storedconfig = idmap_autorid_loadconfig(talloc_tos());
 
-       if (!dbwrap_fetch_uint32(autorid_db, HWM, &hwm)) {
+       status = dbwrap_fetch_uint32(autorid_db, HWM, &hwm);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Fatal error while fetching current "
-                         "HWM value!\n"));
+                         "HWM value: %s\n", nt_errstr(status)));
                status = NT_STATUS_INTERNAL_ERROR;
                goto error;
        }
@@ -541,14 +543,13 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
 
        dom->private_data = config;
 
-       if (!NT_STATUS_IS_OK(status)) {
-               goto error;
-       }
+       goto done;
 
-       return NT_STATUS_OK;
+error:
+       talloc_free(config);
 
-      error:
-       talloc_free(frame);
+done:
+       talloc_free(storedconfig);
 
        return status;
 }
@@ -562,7 +563,7 @@ static struct idmap_methods autorid_methods = {
        .sids_to_unixids = idmap_autorid_sids_to_unixids,
 };
 
-NTSTATUS idmap_autorid_init(void)
+NTSTATUS samba_init_module(void)
 {
        return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION,
                                  "autorid", &autorid_methods);