From 603c3e1bcb7b4106afe4aefdfed43e7832ede956 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Thu, 6 Oct 2011 20:34:55 +0200 Subject: s3:dbwrap: convert dbwrap_fetch_int32() to NTSTATUS return code 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 | 3 ++- source3/lib/dbwrap/dbwrap_util.c | 16 ++++++++++------ source3/lib/sharesec.c | 14 +++++++++++--- source3/passdb/pdb_tdb.c | 20 ++++++++++++-------- source3/registry/reg_backend_db.c | 15 +++++++++++---- source3/utils/dbwrap_tool.c | 8 +++++++- source3/winbindd/idmap_autorid.c | 21 +++++++++++++++++---- source3/winbindd/idmap_tdb.c | 22 ++++++++++++++++++---- 8 files changed, 88 insertions(+), 31 deletions(-) diff --git a/source3/lib/dbwrap/dbwrap.h b/source3/lib/dbwrap/dbwrap.h index 13565f8b33..a549c84d8e 100644 --- a/source3/lib/dbwrap/dbwrap.h +++ b/source3/lib/dbwrap/dbwrap.h @@ -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); diff --git a/source3/lib/dbwrap/dbwrap_util.c b/source3/lib/dbwrap/dbwrap_util.c index 59985479af..5c3940e97b 100644 --- a/source3/lib/dbwrap/dbwrap_util.c +++ b/source3/lib/dbwrap/dbwrap_util.c @@ -26,25 +26,29 @@ #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) diff --git a/source3/lib/sharesec.c b/source3/lib/sharesec.c index 53187c0435..9b3d5607fd 100644 --- a/source3/lib/sharesec.c +++ b/source3/lib/sharesec.c @@ -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 diff --git a/source3/passdb/pdb_tdb.c b/source3/passdb/pdb_tdb.c index 8a9310cbdb..565dd5d309 100644 --- a/source3/passdb/pdb_tdb.c +++ b/source3/passdb/pdb_tdb.c @@ -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 */ } diff --git a/source3/registry/reg_backend_db.c b/source3/registry/reg_backend_db.c index ecdf2d22f2..64c466db81 100644 --- a/source3/registry/reg_backend_db.c +++ b/source3/registry/reg_backend_db.c @@ -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)); diff --git a/source3/utils/dbwrap_tool.c b/source3/utils/dbwrap_tool.c index d6aea126b8..33ef94f6b9 100644 --- a/source3/utils/dbwrap_tool.c +++ b/source3/utils/dbwrap_tool.c @@ -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; diff --git a/source3/winbindd/idmap_autorid.c b/source3/winbindd/idmap_autorid.c index 68115f4c3e..ed00d3728d 100644 --- a/source3/winbindd/idmap_autorid.c +++ b/source3/winbindd/idmap_autorid.c @@ -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; } diff --git a/source3/winbindd/idmap_tdb.c b/source3/winbindd/idmap_tdb.c index ec6b0a8e90..b520e09103 100644 --- a/source3/winbindd/idmap_tdb.c +++ b/source3/winbindd/idmap_tdb.c @@ -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 " -- cgit