s3:winbindd/idmap make idmap modules loadable again
[kai/samba.git] / source3 / winbindd / idmap_autorid.c
index f4ab754654355f473640df66dcf5acc3a1a6f3fe..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,10 +380,11 @@ 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;
        }
@@ -455,7 +467,7 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
        }
 
        config->minvalue = dom->low_id;
-       config->rangesize = lp_parm_int(-1, "autorid", "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"));
@@ -489,9 +501,10 @@ static NTSTATUS idmap_autorid_initialize(struct idmap_domain *dom)
        /* read previously stored config and current HWM */
        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;
        }
@@ -530,14 +543,12 @@ 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:
+error:
        talloc_free(config);
+
+done:
        talloc_free(storedconfig);
 
        return status;
@@ -552,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);