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.
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);
#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)
bool share_info_db_init(void)
{
const char *vstring = "INFO/version";
- int32 vers_id;
+ int32 vers_id = 0;
bool upgrade_ok = true;
NTSTATUS status;
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;
}
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
{
int32 version;
int32 minor_version;
+ NTSTATUS status;
/* check if we are already open */
}
/* 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 */
}
}
/* 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 */
}
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 "
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",
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));
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;
}
/* 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,
"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,
"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;
}
#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 ! */
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);
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 {
}
/* 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 "