s3:dbwrap: convert dbwrap_fetch_int32() to NTSTATUS return code
authorMichael Adam <obnox@samba.org>
Thu, 6 Oct 2011 18:34:55 +0000 (20:34 +0200)
committerMichael Adam <obnox@samba.org>
Tue, 11 Oct 2011 12:17:58 +0000 (14:17 +0200)
Return the int32 value retrieved from the db by reference.
Before this, return value "-1" was used as a error indication,
but it could also be a valid value from the database.

source3/lib/dbwrap/dbwrap.h
source3/lib/dbwrap/dbwrap_util.c
source3/lib/sharesec.c
source3/passdb/pdb_tdb.c
source3/registry/reg_backend_db.c
source3/utils/dbwrap_tool.c
source3/winbindd/idmap_autorid.c
source3/winbindd/idmap_tdb.c

index 13565f8b33754a83282c40a5eb9b4e5ee0c69cda..a549c84d8e1f8e93e0b67f36d84c4d7d0f5f432f 100644 (file)
@@ -70,7 +70,8 @@ NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
 NTSTATUS dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
                               const char *key, TDB_DATA *value);
 
-int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr);
+NTSTATUS dbwrap_fetch_int32(struct db_context *db, const char *keystr,
+                           int32_t *result);
 int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v);
 bool dbwrap_fetch_uint32(struct db_context *db, const char *keystr,
                         uint32_t *val);
index 59985479af93f13f06abd4295ebf65e066a63eee..5c3940e97b8142008ce7a5c9e9319ef97872f265 100644 (file)
 #include "dbwrap.h"
 #include "util_tdb.h"
 
-int32_t dbwrap_fetch_int32(struct db_context *db, const char *keystr)
+NTSTATUS dbwrap_fetch_int32(struct db_context *db, const char *keystr,
+                           int32_t *result)
 {
        TDB_DATA dbuf;
-       int32 ret;
        NTSTATUS status;
 
+       if (result == NULL) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
        status = dbwrap_fetch_bystring(db, NULL, keystr, &dbuf);
        if (!NT_STATUS_IS_OK(status)) {
-               return -1;
+               return status;
        }
 
        if ((dbuf.dptr == NULL) || (dbuf.dsize != sizeof(int32_t))) {
                TALLOC_FREE(dbuf.dptr);
-               return -1;
+               return NT_STATUS_NOT_FOUND;
        }
 
-       ret = IVAL(dbuf.dptr, 0);
+       *result = IVAL(dbuf.dptr, 0);
        TALLOC_FREE(dbuf.dptr);
-       return ret;
+       return NT_STATUS_OK;
 }
 
 int dbwrap_store_int32(struct db_context *db, const char *keystr, int32_t v)
index 53187c04352621f982f44c9db66b6e35e0b3a317..9b3d5607fdec4635262decf8eabde68f538c7076 100644 (file)
@@ -139,7 +139,7 @@ static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
 bool share_info_db_init(void)
 {
        const char *vstring = "INFO/version";
-       int32 vers_id;
+       int32 vers_id = 0;
        bool upgrade_ok = true;
        NTSTATUS status;
 
@@ -155,7 +155,11 @@ bool share_info_db_init(void)
                return False;
        }
 
-       vers_id = dbwrap_fetch_int32(share_db, vstring);
+       status = dbwrap_fetch_int32(share_db, vstring, &vers_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               vers_id = 0;
+       }
+
        if (vers_id == SHARE_DATABASE_VERSION_V3) {
                return true;
        }
@@ -166,7 +170,11 @@ bool share_info_db_init(void)
                return false;
        }
 
-       vers_id = dbwrap_fetch_int32(share_db, vstring);
+       status = dbwrap_fetch_int32(share_db, vstring, &vers_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               vers_id = 0;
+       }
+
        if (vers_id == SHARE_DATABASE_VERSION_V3) {
                /*
                 * Race condition
index 8a9310cbdb043537288ada8c37e73433b7019cd8..565dd5d309826cdafa121a4ffac62ed7036a63a4 100644 (file)
@@ -424,6 +424,7 @@ static bool tdbsam_open( const char *name )
 {
        int32   version;
        int32   minor_version;
+       NTSTATUS status;
 
        /* check if we are already open */
 
@@ -441,14 +442,15 @@ static bool tdbsam_open( const char *name )
        }
 
        /* Check the version */
-       version = dbwrap_fetch_int32(db_sam, TDBSAM_VERSION_STRING);
-       if (version == -1) {
+       status = dbwrap_fetch_int32(db_sam, TDBSAM_VERSION_STRING, &version);
+       if (!NT_STATUS_IS_OK(status)) {
                version = 0;    /* Version not found, assume version 0 */
        }
 
        /* Get the minor version */
-       minor_version = dbwrap_fetch_int32(db_sam, TDBSAM_MINOR_VERSION_STRING);
-       if (minor_version == -1) {
+       status = dbwrap_fetch_int32(db_sam, TDBSAM_MINOR_VERSION_STRING,
+                                   &minor_version);
+       if (!NT_STATUS_IS_OK(status)) {
                minor_version = 0; /* Minor version not found, assume 0 */
        }
 
@@ -482,14 +484,16 @@ static bool tdbsam_open( const char *name )
                }
 
                /* Re-check the version */
-               version = dbwrap_fetch_int32(db_sam, TDBSAM_VERSION_STRING);
-               if (version == -1) {
+               status = dbwrap_fetch_int32(db_sam, TDBSAM_VERSION_STRING,
+                                           &version);
+               if (!NT_STATUS_IS_OK(status)) {
                        version = 0;    /* Version not found, assume version 0 */
                }
 
                /* Re-check the minor version */
-               minor_version = dbwrap_fetch_int32(db_sam, TDBSAM_MINOR_VERSION_STRING);
-               if (minor_version == -1) {
+               status = dbwrap_fetch_int32(db_sam, TDBSAM_MINOR_VERSION_STRING,
+                                           &minor_version);
+               if (!NT_STATUS_IS_OK(status)) {
                        minor_version = 0; /* Minor version not found, assume 0 */
                }
 
index ecdf2d22f2ae36ec1fe6b4130c385f0509f59f30..64c466db81166cdb8eb73c1715fe642575a7c0dc 100644 (file)
@@ -71,7 +71,13 @@ static NTSTATUS regdb_trans_do_action(struct db_context *db, void *private_data)
        int32_t version_id;
        struct regdb_trans_ctx *ctx = (struct regdb_trans_ctx *)private_data;
 
-       version_id = dbwrap_fetch_int32(db, REGDB_VERSION_KEYNAME);
+       status = dbwrap_fetch_int32(db, REGDB_VERSION_KEYNAME, &version_id);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
+                         "Denying access.\n", nt_errstr(status)));
+               return NT_STATUS_ACCESS_DENIED;
+       }
 
        if (version_id != REGDB_CODE_VERSION) {
                DEBUG(0, ("ERROR: changed registry version %d found while "
@@ -627,8 +633,9 @@ done:
 
 WERROR regdb_init(void)
 {
-       uint32 vers_id;
+       int32_t vers_id;
        WERROR werr;
+       NTSTATUS status;
 
        if (regdb) {
                DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
@@ -656,8 +663,8 @@ WERROR regdb_init(void)
        DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
                   regdb_refcount));
 
-       vers_id = dbwrap_fetch_int32(regdb, REGDB_VERSION_KEYNAME);
-       if (vers_id == -1) {
+       status = dbwrap_fetch_int32(regdb, REGDB_VERSION_KEYNAME, &vers_id);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("regdb_init: registry version uninitialized "
                           "(got %d), initializing to version %d\n",
                           vers_id, REGDB_CODE_VERSION));
index d6aea126b8afeedb813e5c044ca2a281b88d2d3e..33ef94f6b95e7edefc3b208304645060dbdfa6dd 100644 (file)
@@ -35,8 +35,14 @@ static int dbwrap_tool_fetch_int32(struct db_context *db,
                                   void *data)
 {
        int32_t value;
+       NTSTATUS status;
 
-       value = dbwrap_fetch_int32(db, keyname);
+       status = dbwrap_fetch_int32(db, keyname, &value);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Error fetching int32 from key '%s': %s\n",
+                        keyname, nt_errstr(status));
+               return -1;
+       }
        d_printf("%d\n", value);
 
        return 0;
index 68115f4c3ef0eac6ca94c80491ad4bb200600538..ed00d3728d943b0b4c5692718a0d47a643150822 100644 (file)
@@ -354,8 +354,10 @@ 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)) {
+       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,
@@ -363,11 +365,17 @@ static NTSTATUS idmap_autorid_db_init(void)
                               "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;
        }
 
        /* Initialize high water mark for alloc pool to 0 */
-       hwm = dbwrap_fetch_int32(autorid_db, ALLOC_HWM);
-       if ((hwm < 0)) {
+       status = dbwrap_fetch_int32(autorid_db, ALLOC_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, ALLOC_HWM, 0);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,
@@ -375,7 +383,12 @@ static NTSTATUS idmap_autorid_db_init(void)
                               "database: %s\n", nt_errstr(status)));
                        return NT_STATUS_INTERNAL_DB_ERROR;
                }
+       } else if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("unable to fetch alloc HWM from autorid database: "
+                         "%s\n", nt_errstr(status)));
+               return status;
        }
+
        return NT_STATUS_OK;
 }
 
index ec6b0a8e9072f75d48c2e31c5b2e29193857f9a1..b520e091031b73841918a741839544d1463343e9 100644 (file)
@@ -172,7 +172,10 @@ static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
 #endif
        DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
 
-       vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
+       status = dbwrap_fetch_int32(db, "IDMAP_VERSION", &vers);
+       if (!NT_STATUS_IS_OK(status)) {
+               vers = -1;
+       }
 
        if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
                /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
@@ -183,7 +186,10 @@ static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
 
                int32 wm;
 
-               wm = dbwrap_fetch_int32(db, HWM_USER);
+               status = dbwrap_fetch_int32(db, HWM_USER, &wm);
+               if (!NT_STATUS_IS_OK(status)) {
+                       wm = -1;
+               }
 
                if (wm != -1) {
                        wm = IREV(wm);
@@ -196,7 +202,11 @@ static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
                        return False;
                }
 
-               wm = dbwrap_fetch_int32(db, HWM_GROUP);
+               status = dbwrap_fetch_int32(db, HWM_GROUP, &wm);
+               if (!NT_STATUS_IS_OK(status)) {
+                       wm = -1;
+               }
+
                if (wm != -1) {
                        wm = IREV(wm);
                } else {
@@ -331,7 +341,11 @@ static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
        }
 
        /* check against earlier versions */
-       version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
+       ret = dbwrap_fetch_int32(db, "IDMAP_VERSION", &version);
+       if (!NT_STATUS_IS_OK(ret)) {
+               version = -1;
+       }
+
        if (version != IDMAP_VERSION) {
                if (config_error) {
                        DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "