static int dbwrap_tool_store_int32(struct db_context *db, const char *keyname, const char *data) { NTSTATUS status; int32_t value = (int32_t)strtol(data, NULL, 10); if (dbwrap_is_persistent(db)) { status = dbwrap_trans_store_int32_bystring(db, keyname, value); } else { status = dbwrap_store_int32_bystring(db, keyname, value); } if (!NT_STATUS_IS_OK(status)) { d_fprintf(stderr, "ERROR: could not store int32 key '%s': %s\n", keyname, nt_errstr(status)); return -1; } return 0; }
static bool tdbsam_convert(struct db_context **pp_db, const char *name, int32_t from) { struct tdbsam_convert_state state; struct db_context *db = NULL; NTSTATUS status; /* We only need the update backup for local db's. */ if (db_is_local(name) && !tdbsam_convert_backup(name, pp_db)) { DEBUG(0, ("tdbsam_convert: Could not backup %s\n", name)); return false; } db = *pp_db; state.from = from; state.success = true; if (dbwrap_transaction_start(db) != 0) { DEBUG(0, ("tdbsam_convert: Could not start transaction\n")); return false; } if (!tdbsam_upgrade_next_rid(db)) { DEBUG(0, ("tdbsam_convert: tdbsam_upgrade_next_rid failed\n")); goto cancel; } status = dbwrap_traverse(db, tdbsam_convert_one, &state, NULL); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("tdbsam_convert: traverse failed\n")); goto cancel; } if (!state.success) { DEBUG(0, ("tdbsam_convert: Converting records failed\n")); goto cancel; } status = dbwrap_store_int32_bystring(db, TDBSAM_VERSION_STRING, TDBSAM_VERSION); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("tdbsam_convert: Could not store tdbsam version: " "%s\n", nt_errstr(status))); goto cancel; } status = dbwrap_store_int32_bystring(db, TDBSAM_MINOR_VERSION_STRING, TDBSAM_MINOR_VERSION); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("tdbsam_convert: Could not store tdbsam minor " "version: %s\n", nt_errstr(status))); goto cancel; } if (dbwrap_transaction_commit(db) != 0) { DEBUG(0, ("tdbsam_convert: Could not commit transaction\n")); return false; } return true; cancel: if (dbwrap_transaction_cancel(db) != 0) { smb_panic("tdbsam_convert: transaction_cancel failed"); } return false; }
static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db) { int32_t vers; struct convert_fn_state s; NTSTATUS status; status = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &vers); if (!NT_STATUS_IS_OK(status)) { vers = -1; } if (IREV(vers) == IDMAP_VERSION) { /* Arrggghh ! Bytereversed - make order independent ! */ /* * high and low records were created on a * big endian machine and will need byte-reversing. */ int32_t wm; status = dbwrap_fetch_int32_bystring(db, HWM_USER, &wm); if (!NT_STATUS_IS_OK(status)) { wm = -1; } if (wm != -1) { wm = IREV(wm); } else { wm = dom->low_id; } status = dbwrap_store_int32_bystring(db, HWM_USER, wm); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to byteswap user hwm in idmap " "database: %s\n", nt_errstr(status))); return False; } status = dbwrap_fetch_int32_bystring(db, HWM_GROUP, &wm); if (!NT_STATUS_IS_OK(status)) { wm = -1; } if (wm != -1) { wm = IREV(wm); } else { wm = dom->low_id; } status = dbwrap_store_int32_bystring(db, HWM_GROUP, wm); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to byteswap group hwm in idmap " "database: %s\n", nt_errstr(status))); return False; } } s.db = db; s.failed = false; /* the old format stored as DOMAIN/rid - now we store the SID direct */ status = dbwrap_traverse(db, convert_fn, &s, NULL); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Database traverse failed during conversion\n")); return false; } if (s.failed) { DEBUG(0, ("Problem during conversion\n")); return False; } status = dbwrap_store_int32_bystring(db, "IDMAP_VERSION", IDMAP_VERSION); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to store idmap version in database: %s\n", nt_errstr(status))); return False; } return True; }
/* * write out the current fsrvp server state to a TDB. This clears any content * currently written to the TDB. */ _PRIVATE_ NTSTATUS fss_state_store(TALLOC_CTX *mem_ctx, struct fss_sc_set *sc_sets, uint32_t sc_sets_count, const char *db_path) { TALLOC_CTX *tmp_ctx; struct db_context *db; NTSTATUS status; int ret; struct fss_sc_set *sc_set; tmp_ctx = talloc_new(mem_ctx); if (tmp_ctx == NULL) { return NT_STATUS_NO_MEMORY; } db = db_open(tmp_ctx, db_path, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE); if (db == NULL) { DEBUG(0, ("Failed to open fss state database %s\n", db_path)); status = NT_STATUS_ACCESS_DENIED; goto err_ctx_free; } ret = dbwrap_wipe(db); if (ret != 0) { status = NT_STATUS_UNSUCCESSFUL; goto err_db_free; } status = dbwrap_store_int32_bystring(db, FSS_DB_KEY_VERSION, FSRVP_STATE_DB_VERSION); if (!NT_STATUS_IS_OK(status)) { goto err_db_free; } ret = dbwrap_transaction_start(db); if (ret != 0) { status = NT_STATUS_UNSUCCESSFUL; goto err_db_free; } status = dbwrap_store_int32_bystring(db, FSS_DB_KEY_SC_SET_COUNT, sc_sets_count); if (!NT_STATUS_IS_OK(status)) { status = NT_STATUS_UNSUCCESSFUL; goto err_trans_cancel; } for (sc_set = sc_sets; sc_set; sc_set = sc_set->next) { status = fss_state_sc_set_store(tmp_ctx, db, sc_set); if (!NT_STATUS_IS_OK(status)) { goto err_trans_cancel; } } ret = dbwrap_transaction_commit(db); if (ret != 0) { status = NT_STATUS_UNSUCCESSFUL; goto err_trans_cancel; } talloc_free(db); talloc_free(tmp_ctx); return NT_STATUS_OK; err_trans_cancel: dbwrap_transaction_cancel(db); err_db_free: talloc_free(db); err_ctx_free: talloc_free(tmp_ctx); return status; }
bool run_dbwrap_watch1(int dummy) { struct tevent_context *ev = NULL; struct messaging_context *msg = NULL; struct db_context *db = NULL; const char *keystr = "key"; TDB_DATA key = string_term_tdb_data(keystr); struct db_record *rec = NULL; struct tevent_req *req = NULL; NTSTATUS status; bool ret = false; ev = samba_tevent_context_init(talloc_tos()); if (ev == NULL) { fprintf(stderr, "tevent_context_init failed\n"); goto fail; } msg = messaging_init(ev, ev); if (msg == NULL) { fprintf(stderr, "messaging_init failed\n"); goto fail; } db = db_open(msg, "test_watch.tdb", 0, TDB_DEFAULT, O_CREAT|O_RDWR, 0644, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE); if (db == NULL) { fprintf(stderr, "db_open failed: %s\n", strerror(errno)); goto fail; } dbwrap_watch_db(db, msg); rec = dbwrap_fetch_locked(db, db, key); if (rec == NULL) { fprintf(stderr, "dbwrap_fetch_locked failed\n"); goto fail; } req = dbwrap_record_watch_send(talloc_tos(), ev, rec, msg); if (req == NULL) { fprintf(stderr, "dbwrap_record_watch_send failed\n"); goto fail; } TALLOC_FREE(rec); status = dbwrap_store_int32_bystring(db, "different_key", 1); if (!NT_STATUS_IS_OK(status)) { fprintf(stderr, "dbwrap_store_int32 failed: %s\n", nt_errstr(status)); goto fail; } status = dbwrap_store_int32_bystring(db, keystr, 1); if (!NT_STATUS_IS_OK(status)) { fprintf(stderr, "dbwrap_store_int32 failed: %s\n", nt_errstr(status)); goto fail; } if (!tevent_req_poll(req, ev)) { fprintf(stderr, "tevent_req_poll failed\n"); goto fail; } status = dbwrap_record_watch_recv(req, talloc_tos(), &rec); if (!NT_STATUS_IS_OK(status)) { fprintf(stderr, "dbwrap_record_watch_recv failed: %s\n", nt_errstr(status)); goto fail; } ret = true; fail: TALLOC_FREE(req); TALLOC_FREE(rec); TALLOC_FREE(db); TALLOC_FREE(msg); TALLOC_FREE(ev); return ret; }