static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom) { uint32_t low_uid; uint32_t low_gid; bool update_uid = false; bool update_gid = false; struct idmap_tdb_common_context *ctx; NTSTATUS status; ctx = talloc_get_type(dom->private_data, struct idmap_tdb_common_context); status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_USER, &low_uid); if (!NT_STATUS_IS_OK(status) || low_uid < dom->low_id) { update_uid = true; } status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_GROUP, &low_gid); if (!NT_STATUS_IS_OK(status) || low_gid < dom->low_id) { update_gid = true; } if (!update_uid && !update_gid) { return NT_STATUS_OK; } if (dbwrap_transaction_start(ctx->db) != 0) { DEBUG(0, ("Unable to start upgrade transaction!\n")); return NT_STATUS_INTERNAL_DB_ERROR; } if (update_uid) { status = dbwrap_store_uint32_bystring(ctx->db, HWM_USER, dom->low_id); if (!NT_STATUS_IS_OK(status)) { dbwrap_transaction_cancel(ctx->db); DEBUG(0, ("Unable to initialise user hwm in idmap " "database: %s\n", nt_errstr(status))); return NT_STATUS_INTERNAL_DB_ERROR; } } if (update_gid) { status = dbwrap_store_uint32_bystring(ctx->db, HWM_GROUP, dom->low_id); if (!NT_STATUS_IS_OK(status)) { dbwrap_transaction_cancel(ctx->db); DEBUG(0, ("Unable to initialise group hwm in idmap " "database: %s\n", nt_errstr(status))); return NT_STATUS_INTERNAL_DB_ERROR; } } if (dbwrap_transaction_commit(ctx->db) != 0) { DEBUG(0, ("Unable to commit upgrade transaction!\n")); return NT_STATUS_INTERNAL_DB_ERROR; } return NT_STATUS_OK; }
static bool open_db(struct idmap_tdb_common_context *ctx) { NTSTATUS status; char *db_path; if(ctx->db) { /* already open */ return true; } db_path = talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb", lp_private_dir()); if(!db_path) { DEBUG(0, ("Out of memory!\n")); return false; } ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE); if(!ctx->db) { DEBUG(0, ("Failed to open database: %s\n", strerror(errno))); return false; } if(dbwrap_transaction_start(ctx->db) != 0) { DEBUG(0, ("Failed to start transaction!\n")); return false; } status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_uid, LOW_ID); if(!NT_STATUS_IS_OK(status)) { dbwrap_transaction_cancel(ctx->db); return false; } status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_gid, LOW_ID); if(!NT_STATUS_IS_OK(status)) { dbwrap_transaction_cancel(ctx->db); return false; } if(dbwrap_transaction_commit(ctx->db) != 0) { DEBUG(0, ("Failed to commit transaction!\n")); return false; } return true; }
static bool tdbsam_upgrade_next_rid(struct db_context *db) { TDB_CONTEXT *tdb; uint32 rid; bool ok = false; NTSTATUS status; status = dbwrap_fetch_uint32_bystring(db, NEXT_RID_STRING, &rid); if (NT_STATUS_IS_OK(status)) { return true; } tdb = tdb_open_log(state_path("winbindd_idmap.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0644); if (tdb) { ok = tdb_fetch_uint32(tdb, "RID_COUNTER", &rid); if (!ok) { rid = BASE_RID; } tdb_close(tdb); } else { rid = BASE_RID; } status = dbwrap_store_uint32_bystring(db, NEXT_RID_STRING, rid); if (!NT_STATUS_IS_OK(status)) { return false; } return true; }
static int dbwrap_tool_store_uint32(struct db_context *db, const char *keyname, const char *data) { NTSTATUS status; uint32_t value = (uint32_t)strtol(data, NULL, 10); if (dbwrap_is_persistent(db)) { status = dbwrap_trans_store_uint32_bystring(db, keyname, value); } else { status = dbwrap_store_uint32_bystring(db, keyname, value); } if (!NT_STATUS_IS_OK(status)) { d_fprintf(stderr, "ERROR: could not store uint32 key '%s': %s\n", keyname, nt_errstr(status)); return -1; } return 0; }
uint32_t smbXsrv_open_hash(struct smbXsrv_open *_open) { uint8_t buf[8+8+8]; uint32_t ret; TDB_DATA key; SBVAL(buf, 0, _open->global->open_persistent_id); SBVAL(buf, 8, _open->global->open_volatile_id); SBVAL(buf, 16, _open->global->open_time); key = (TDB_DATA) { .dptr = buf, .dsize = sizeof(buf) }; ret = tdb_jenkins_hash(&key); if (ret == 0) { ret = 1; } return ret; } static NTSTATUS smbXsrv_open_set_replay_cache(struct smbXsrv_open *op) { struct GUID *create_guid; struct GUID_txt_buf buf; char *guid_string; struct db_context *db = op->table->local.replay_cache_db_ctx; NTSTATUS status; if (!(op->flags & SMBXSRV_OPEN_NEED_REPLAY_CACHE)) { return NT_STATUS_OK; } if (op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE) { return NT_STATUS_OK; } create_guid = &op->global->create_guid; if (GUID_all_zero(create_guid)) { return NT_STATUS_OK; } guid_string = GUID_buf_string(create_guid, &buf); if (guid_string == NULL) { return NT_STATUS_INVALID_PARAMETER; } status = dbwrap_store_uint32_bystring(db, guid_string, op->local_id); if (NT_STATUS_IS_OK(status)) { op->flags |= SMBXSRV_OPEN_HAVE_REPLAY_CACHE; op->flags &= ~SMBXSRV_OPEN_NEED_REPLAY_CACHE; } return status; } static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op) { struct GUID *create_guid; struct GUID_txt_buf buf; char *guid_string; struct db_context *db; NTSTATUS status; if (op->table == NULL) { return NT_STATUS_OK; } db = op->table->local.replay_cache_db_ctx; if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) { return NT_STATUS_OK; } create_guid = &op->global->create_guid; if (GUID_all_zero(create_guid)) { return NT_STATUS_OK; } guid_string = GUID_buf_string(create_guid, &buf); if (guid_string == NULL) { return NT_STATUS_INVALID_PARAMETER; } status = dbwrap_purge_bystring(db, guid_string); if (NT_STATUS_IS_OK(status)) { op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE; } return status; } NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op) { struct smbXsrv_open_table *table = op->table; NTSTATUS status; if (op->global->db_rec != NULL) { DEBUG(0, ("smbXsrv_open_update(0x%08x): " "Called with db_rec != NULL'\n", op->global->open_global_id)); return NT_STATUS_INTERNAL_ERROR; } op->global->db_rec = smbXsrv_open_global_fetch_locked( table->global.db_ctx, op->global->open_global_id, op->global /* TALLOC_CTX */); if (op->global->db_rec == NULL) { return NT_STATUS_INTERNAL_DB_ERROR; } status = smbXsrv_open_global_store(op->global); if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("smbXsrv_open_update: " "global_id (0x%08x) store failed - %s\n", op->global->open_global_id, nt_errstr(status))); return status; } status = smbXsrv_open_set_replay_cache(op); if (!NT_STATUS_IS_OK(status)) { DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n", nt_errstr(status)); return status; } if (CHECK_DEBUGLVL(10)) { struct smbXsrv_openB open_blob; ZERO_STRUCT(open_blob); open_blob.version = SMBXSRV_VERSION_0; open_blob.info.info0 = op; DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n", op->global->open_global_id)); NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob); } return NT_STATUS_OK; } NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now) { struct smbXsrv_open_table *table; struct db_record *local_rec = NULL; struct db_record *global_rec = NULL; NTSTATUS status; NTSTATUS error = NT_STATUS_OK; error = smbXsrv_open_clear_replay_cache(op); if (!NT_STATUS_IS_OK(error)) { DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n", nt_errstr(error)); } if (op->table == NULL) { return error; } table = op->table; op->table = NULL; op->status = NT_STATUS_FILE_CLOSED; op->global->disconnect_time = now; server_id_set_disconnected(&op->global->server_id); global_rec = op->global->db_rec; op->global->db_rec = NULL; if (global_rec == NULL) { global_rec = smbXsrv_open_global_fetch_locked( table->global.db_ctx, op->global->open_global_id, op->global /* TALLOC_CTX */); if (global_rec == NULL) { error = NT_STATUS_INTERNAL_ERROR; } } if (global_rec != NULL && op->global->durable) { /* * If it is a durable open we need to update the global part * instead of deleting it */ op->global->db_rec = global_rec; status = smbXsrv_open_global_store(op->global); if (NT_STATUS_IS_OK(status)) { /* * smbXsrv_open_global_store does the free * of op->global->db_rec */ global_rec = NULL; } if (!NT_STATUS_IS_OK(status)) { DEBUG(0,("smbXsrv_open_close(0x%08x)" "smbXsrv_open_global_store() failed - %s\n", op->global->open_global_id, nt_errstr(status))); error = status; } if (NT_STATUS_IS_OK(status) && CHECK_DEBUGLVL(10)) { struct smbXsrv_openB open_blob; ZERO_STRUCT(open_blob); open_blob.version = SMBXSRV_VERSION_0; open_blob.info.info0 = op; DEBUG(10,("smbXsrv_open_close(0x%08x): " "stored disconnect\n", op->global->open_global_id)); NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob); } } if (global_rec != NULL) { status = dbwrap_record_delete(global_rec); if (!NT_STATUS_IS_OK(status)) { TDB_DATA key = dbwrap_record_get_key(global_rec); DEBUG(0, ("smbXsrv_open_close(0x%08x): " "failed to delete global key '%s': %s\n", op->global->open_global_id, hex_encode_talloc(global_rec, key.dptr, key.dsize), nt_errstr(status))); error = status; } } TALLOC_FREE(global_rec); local_rec = op->db_rec; if (local_rec == NULL) { local_rec = smbXsrv_open_local_fetch_locked(table->local.db_ctx, op->local_id, op /* TALLOC_CTX*/); if (local_rec == NULL) { error = NT_STATUS_INTERNAL_ERROR; } } if (local_rec != NULL) { status = dbwrap_record_delete(local_rec); if (!NT_STATUS_IS_OK(status)) { TDB_DATA key = dbwrap_record_get_key(local_rec); DEBUG(0, ("smbXsrv_open_close(0x%08x): " "failed to delete local key '%s': %s\n", op->global->open_global_id, hex_encode_talloc(local_rec, key.dptr, key.dsize), nt_errstr(status))); error = status; } table->local.num_opens -= 1; } if (op->db_rec == NULL) { TALLOC_FREE(local_rec); } op->db_rec = NULL; if (op->compat) { op->compat->op = NULL; file_free(NULL, op->compat); op->compat = NULL; } return error; } NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn) { uint32_t max_opens; /* * Allow a range from 1..65534. * * With real_max_open_files possible ids, * truncated to the SMB1 limit of 16-bit. * * 0 and 0xFFFF are no valid ids. */ max_opens = conn->client->sconn->real_max_open_files; max_opens = MIN(max_opens, UINT16_MAX - 1); return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens); }