Пример #1
0
bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
{
	struct db_context *db;
	int i;

#ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
	if (lp_clustering()) {
		return ctdb_serverids_exist(messaging_ctdbd_connection(),
					    ids, num_ids, results);
	}
#endif
	if (!processes_exist(ids, num_ids, results)) {
		return false;
	}

	db = serverid_db();
	if (db == NULL) {
		return false;
	}

	for (i=0; i<num_ids; i++) {
		struct serverid_exists_state state;
		struct serverid_key key;
		TDB_DATA tdbkey;

		if (ids[i].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
			results[i] = true;
			continue;
		}
		if (!results[i]) {
			continue;
		}

		serverid_fill_key(&ids[i], &key);
		tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));

		state.id = &ids[i];
		state.exists = false;
		dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
		results[i] = state.exists;
	}
	return true;
}
struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
						  struct file_id id)
{
	struct share_mode_lock *lck;
	TDB_DATA key = locking_key(&id);
	NTSTATUS status;

	lck = talloc(mem_ctx, struct share_mode_lock);
	if (lck == NULL) {
		DEBUG(0, ("talloc failed\n"));
		return NULL;
	}
	status = dbwrap_parse_record(
		lock_db, key, fetch_share_mode_unlocked_parser, lck);
	if (!NT_STATUS_IS_OK(status) ||
	    (lck->data == NULL)) {
		TALLOC_FREE(lck);
		return NULL;
	}
	return lck;
}
Пример #3
0
static NTSTATUS dbwrap_cache_parse_record(
	struct db_context *db, TDB_DATA key,
	void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
	void *private_data)
{
	struct db_cache_ctx *ctx = talloc_get_type_abort(
		db->private_data, struct db_cache_ctx);
	TDB_DATA value;
	NTSTATUS status;

	if (!dbwrap_cache_validate(ctx)) {
		return NT_STATUS_NO_MEMORY;
	}

	if (dbwrap_exists(ctx->negative, key)) {
		return NT_STATUS_NOT_FOUND;
	}
	status = dbwrap_parse_record(ctx->positive, key, parser, private_data);
	if (NT_STATUS_IS_OK(status)) {
		return status;
	}

	status = dbwrap_fetch(ctx->backing, talloc_tos(), key, &value);

	if (NT_STATUS_IS_OK(status)) {
		dbwrap_store(ctx->positive, key, value, 0);
		parser(key, value, private_data);
		TALLOC_FREE(value.dptr);
		return NT_STATUS_OK;
	}

	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		char c = '\0';
		value.dptr = (uint8_t *)&c;
		value.dsize = sizeof(c);
		dbwrap_store(ctx->negative, key, value, 0);
		return NT_STATUS_NOT_FOUND;
	}
	return status;
}
Пример #4
0
NTSTATUS dbwrap_fetch_int32(struct db_context *db, TDB_DATA key,
			    int32_t *result)
{
	struct dbwrap_fetch_int32_state state;
	NTSTATUS status;

	if (result == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	state.status = NT_STATUS_INTERNAL_ERROR;

	status = dbwrap_parse_record(db, key, dbwrap_fetch_int32_parser, &state);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (NT_STATUS_IS_OK(state.status)) {
		*result = state.result;
	}
	return state.status;
}
Пример #5
0
NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
		      TDB_DATA key, TDB_DATA *value)
{
	struct dbwrap_fetch_state state;
	NTSTATUS status;

	if (value == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	state.mem_ctx = mem_ctx;

	status = dbwrap_parse_record(db, key, dbwrap_fetch_parser, &state);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	if ((state.data.dsize != 0) && (state.data.dptr == NULL)) {
		return NT_STATUS_NO_MEMORY;
	}
	*value = state.data;
	return NT_STATUS_OK;
}
Пример #6
0
NTSTATUS dbwrap_fetch_uint32_bystring(struct db_context *db,
                                      const char *keystr, uint32_t *val)
{
    struct dbwrap_fetch_uint32_state state;
    NTSTATUS status;

    if (val == NULL) {
        return NT_STATUS_INVALID_PARAMETER;
    }

    state.status = NT_STATUS_INTERNAL_ERROR;

    status = dbwrap_parse_record(db, string_term_tdb_data(keystr),
                                 dbwrap_fetch_uint32_parser, &state);
    if (!NT_STATUS_IS_OK(status)) {
        return status;
    }
    if (NT_STATUS_IS_OK(state.status)) {
        *val = state.result;
    }
    return state.status;
}
Пример #7
0
bool serverid_exists(const struct server_id *id)
{
	struct db_context *db;
	struct serverid_exists_state state;
	struct serverid_key key;
	TDB_DATA tdbkey;
	NTSTATUS status;

	if (procid_is_me(id)) {
		return true;
	}

	if (!process_exists(*id)) {
		return false;
	}

	if (id->unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
		return true;
	}

	db = serverid_db();
	if (db == NULL) {
		return false;
	}

	serverid_fill_key(id, &key);
	tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));

	state.id = id;
	state.exists = false;

	status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
	if (!NT_STATUS_IS_OK(status)) {
		return false;
	}
	return state.exists;
}
Пример #8
0
static NTSTATUS smb1srv_tcon_local_allocate_id(struct db_context *db,
					       uint32_t lowest_id,
					       uint32_t highest_id,
					       TALLOC_CTX *mem_ctx,
					       struct db_record **_rec,
					       uint32_t *_id)
{
	struct smb1srv_tcon_local_allocate_state state = {
		.lowest_id = lowest_id,
		.highest_id = highest_id,
		.last_id = 0,
		.useable_id = lowest_id,
		.status = NT_STATUS_INTERNAL_ERROR,
	};
	uint32_t i;
	uint32_t range;
	NTSTATUS status;
	int count = 0;

	*_rec = NULL;
	*_id = 0;

	if (lowest_id > highest_id) {
		return NT_STATUS_INSUFFICIENT_RESOURCES;
	}

	/*
	 * first we try randomly
	 */
	range = (highest_id - lowest_id) + 1;

	for (i = 0; i < (range / 2); i++) {
		uint32_t id;
		uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
		TDB_DATA key;
		TDB_DATA val;
		struct db_record *rec = NULL;

		id = generate_random() % range;
		id += lowest_id;

		if (id < lowest_id) {
			id = lowest_id;
		}
		if (id > highest_id) {
			id = highest_id;
		}

		key = smbXsrv_tcon_local_id_to_key(id, key_buf);

		rec = dbwrap_fetch_locked(db, mem_ctx, key);
		if (rec == NULL) {
			return NT_STATUS_INSUFFICIENT_RESOURCES;
		}

		val = dbwrap_record_get_value(rec);
		if (val.dsize != 0) {
			TALLOC_FREE(rec);
			continue;
		}

		*_rec = rec;
		*_id = id;
		return NT_STATUS_OK;
	}

	/*
	 * if the range is almost full,
	 * we traverse the whole table
	 * (this relies on sorted behavior of dbwrap_rbt)
	 */
	status = dbwrap_traverse_read(db, smb1srv_tcon_local_allocate_traverse,
				      &state, &count);
	if (NT_STATUS_IS_OK(status)) {
		if (NT_STATUS_IS_OK(state.status)) {
			return NT_STATUS_INTERNAL_ERROR;
		}

		if (!NT_STATUS_EQUAL(state.status, NT_STATUS_INTERNAL_ERROR)) {
			return state.status;
		}

		if (state.useable_id <= state.highest_id) {
			state.status = NT_STATUS_OK;
		} else {
			return NT_STATUS_INSUFFICIENT_RESOURCES;
		}
	} else if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
		/*
		 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
		 *
		 * If we get anything else it is an error, because it
		 * means we did not manage to find a free slot in
		 * the db.
		 */
		return NT_STATUS_INSUFFICIENT_RESOURCES;
	}

	if (NT_STATUS_IS_OK(state.status)) {
		uint32_t id;
		uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
		TDB_DATA key;
		TDB_DATA val;
		struct db_record *rec = NULL;

		id = state.useable_id;

		key = smbXsrv_tcon_local_id_to_key(id, key_buf);

		rec = dbwrap_fetch_locked(db, mem_ctx, key);
		if (rec == NULL) {
			return NT_STATUS_INSUFFICIENT_RESOURCES;
		}

		val = dbwrap_record_get_value(rec);
		if (val.dsize != 0) {
			TALLOC_FREE(rec);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		*_rec = rec;
		*_id = id;
		return NT_STATUS_OK;
	}

	return state.status;
}

struct smbXsrv_tcon_local_fetch_state {
	struct smbXsrv_tcon *tcon;
	NTSTATUS status;
};

static void smbXsrv_tcon_local_fetch_parser(TDB_DATA key, TDB_DATA data,
					    void *private_data)
{
	struct smbXsrv_tcon_local_fetch_state *state =
		(struct smbXsrv_tcon_local_fetch_state *)private_data;
	void *ptr;

	if (data.dsize != sizeof(ptr)) {
		state->status = NT_STATUS_INTERNAL_DB_ERROR;
		return;
	}

	memcpy(&ptr, data.dptr, data.dsize);
	state->tcon = talloc_get_type_abort(ptr, struct smbXsrv_tcon);
	state->status = NT_STATUS_OK;
}

static NTSTATUS smbXsrv_tcon_local_lookup(struct smbXsrv_tcon_table *table,
					  uint32_t tcon_local_id,
					  NTTIME now,
					  struct smbXsrv_tcon **_tcon)
{
	struct smbXsrv_tcon_local_fetch_state state = {
		.tcon = NULL,
		.status = NT_STATUS_INTERNAL_ERROR,
	};
	uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
	TDB_DATA key;
	NTSTATUS status;

	*_tcon = NULL;

	if (tcon_local_id == 0) {
		return NT_STATUS_NETWORK_NAME_DELETED;
	}

	if (table == NULL) {
		/* this might happen before the end of negprot */
		return NT_STATUS_NETWORK_NAME_DELETED;
	}

	if (table->local.db_ctx == NULL) {
		return NT_STATUS_INTERNAL_ERROR;
	}

	key = smbXsrv_tcon_local_id_to_key(tcon_local_id, key_buf);

	status = dbwrap_parse_record(table->local.db_ctx, key,
				     smbXsrv_tcon_local_fetch_parser,
				     &state);
	if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		return NT_STATUS_NETWORK_NAME_DELETED;
	} else if (!NT_STATUS_IS_OK(status)) {
		return status;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		return state.status;
	}

	if (NT_STATUS_EQUAL(state.tcon->status, NT_STATUS_NETWORK_NAME_DELETED)) {
		return NT_STATUS_NETWORK_NAME_DELETED;
	}

	state.tcon->idle_time = now;

	*_tcon = state.tcon;
	return state.tcon->status;
}

static int smbXsrv_tcon_global_destructor(struct smbXsrv_tcon_global0 *global)
{
	return 0;
}

static void smbXsrv_tcon_global_verify_record(struct db_record *db_rec,
					bool *is_free,
					bool *was_free,
					TALLOC_CTX *mem_ctx,
					struct smbXsrv_tcon_global0 **_g);

static NTSTATUS smbXsrv_tcon_global_allocate(struct db_context *db,
					TALLOC_CTX *mem_ctx,
					struct smbXsrv_tcon_global0 **_global)
{
	uint32_t i;
	struct smbXsrv_tcon_global0 *global = NULL;
	uint32_t last_free = 0;
	const uint32_t min_tries = 3;

	*_global = NULL;

	global = talloc_zero(mem_ctx, struct smbXsrv_tcon_global0);
	if (global == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	talloc_set_destructor(global, smbXsrv_tcon_global_destructor);

	/*
	 * Here we just randomly try the whole 32-bit space
	 *
	 * We use just 32-bit, because we want to reuse the
	 * ID for SRVSVC.
	 */
	for (i = 0; i < UINT32_MAX; i++) {
		bool is_free = false;
		bool was_free = false;
		uint32_t id;
		uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
		TDB_DATA key;

		if (i >= min_tries && last_free != 0) {
			id = last_free;
		} else {
			id = generate_random();
		}
		if (id == 0) {
			id++;
		}
		if (id == UINT32_MAX) {
			id--;
		}

		key = smbXsrv_tcon_global_id_to_key(id, key_buf);

		global->db_rec = dbwrap_fetch_locked(db, mem_ctx, key);
		if (global->db_rec == NULL) {
			talloc_free(global);
			return NT_STATUS_INSUFFICIENT_RESOURCES;
		}

		smbXsrv_tcon_global_verify_record(global->db_rec,
						  &is_free,
						  &was_free,
						  NULL, NULL);

		if (!is_free) {
			TALLOC_FREE(global->db_rec);
			continue;
		}

		if (!was_free && i < min_tries) {
			/*
			 * The session_id is free now,
			 * but was not free before.
			 *
			 * This happens if a smbd crashed
			 * and did not cleanup the record.
			 *
			 * If this is one of our first tries,
			 * then we try to find a real free one.
			 */
			if (last_free == 0) {
				last_free = id;
			}
			TALLOC_FREE(global->db_rec);
			continue;
		}

		global->tcon_global_id = id;

		*_global = global;
		return NT_STATUS_OK;
	}

	/* should not be reached */
	talloc_free(global);
	return NT_STATUS_INTERNAL_ERROR;
}
Пример #9
0
static int dbwrap_fallback_exists(struct db_context *db, TDB_DATA key)
{
	NTSTATUS status = dbwrap_parse_record(db, key, NULL, NULL);
	return NT_STATUS_IS_OK(status) ? 1 : 0;
}
Пример #10
0
bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
{
	int *todo_idx = NULL;
	struct server_id *todo_ids = NULL;
	bool *todo_results = NULL;
	int todo_num = 0;
	int *remote_idx = NULL;
	int remote_num = 0;
	int *verify_idx = NULL;
	int verify_num = 0;
	int t, idx;
	bool result = false;
	struct db_context *db;

	db = serverid_db();
	if (db == NULL) {
		return false;
	}

	todo_idx = talloc_array(talloc_tos(), int, num_ids);
	if (todo_idx == NULL) {
		goto fail;
	}
	todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
	if (todo_ids == NULL) {
		goto fail;
	}
	todo_results = talloc_array(talloc_tos(), bool, num_ids);
	if (todo_results == NULL) {
		goto fail;
	}

	remote_idx = talloc_array(talloc_tos(), int, num_ids);
	if (remote_idx == NULL) {
		goto fail;
	}
	verify_idx = talloc_array(talloc_tos(), int, num_ids);
	if (verify_idx == NULL) {
		goto fail;
	}

	for (idx=0; idx<num_ids; idx++) {
		results[idx] = false;

		if (server_id_is_disconnected(&ids[idx])) {
			continue;
		}

		if (procid_is_me(&ids[idx])) {
			results[idx] = true;
			continue;
		}

		if (procid_is_local(&ids[idx])) {
			bool exists = process_exists_by_pid(ids[idx].pid);

			if (!exists) {
				continue;
			}

			if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
				results[idx] = true;
				continue;
			}

			verify_idx[verify_num] = idx;
			verify_num += 1;
			continue;
		}

		if (!lp_clustering()) {
			continue;
		}

		remote_idx[remote_num] = idx;
		remote_num += 1;
	}

	if (remote_num != 0 &&
	    ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
	{
		int old_remote_num = remote_num;

		remote_num = 0;
		todo_num = 0;

		for (t=0; t<old_remote_num; t++) {
			idx = remote_idx[t];

			if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
				remote_idx[remote_num] = idx;
				remote_num += 1;
				continue;
			}

			todo_idx[todo_num] = idx;
			todo_ids[todo_num] = ids[idx];
			todo_results[todo_num] = false;
			todo_num += 1;
		}

		/*
		 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
		 * to verify that the server_id still exists,
		 * which means only the server_id.unique_id and
		 * server_id.vnn are verified, while server_id.pid
		 * is not verified at all.
		 *
		 * TODO: do we want to verify server_id.pid somehow?
		 */
		if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
					  todo_ids, todo_num, todo_results))
		{
			goto fail;
		}

		for (t=0; t<todo_num; t++) {
			idx = todo_idx[t];

			results[idx] = todo_results[t];
		}
	}

	if (remote_num != 0) {
		todo_num = 0;

		for (t=0; t<remote_num; t++) {
			idx = remote_idx[t];
			todo_idx[todo_num] = idx;
			todo_ids[todo_num] = ids[idx];
			todo_results[todo_num] = false;
			todo_num += 1;
		}

		if (!ctdb_processes_exist(messaging_ctdbd_connection(),
					  todo_ids, todo_num,
					  todo_results)) {
			goto fail;
		}

		for (t=0; t<todo_num; t++) {
			idx = todo_idx[t];

			if (!todo_results[t]) {
				continue;
			}

			if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
				results[idx] = true;
				continue;
			}

			verify_idx[verify_num] = idx;
			verify_num += 1;
		}
	}

	for (t=0; t<verify_num; t++) {
		struct serverid_exists_state state;
		struct serverid_key key;
		TDB_DATA tdbkey;
		NTSTATUS status;

		idx = verify_idx[t];

		serverid_fill_key(&ids[idx], &key);
		tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));

		state.id = &ids[idx];
		state.exists = false;
		status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
		if (!NT_STATUS_IS_OK(status)) {
			results[idx] = false;
			continue;
		}
		results[idx] = state.exists;
	}

	result = true;
fail:
	TALLOC_FREE(verify_idx);
	TALLOC_FREE(remote_idx);
	TALLOC_FREE(todo_results);
	TALLOC_FREE(todo_ids);
	TALLOC_FREE(todo_idx);
	return result;
}
Пример #11
0
bool run_dbwrap_do_locked1(int dummy)
{
	struct tevent_context *ev;
	struct messaging_context *msg;
	struct db_context *backend;
	struct db_context *db;
	const char *dbname = "test_do_locked.tdb";
	const char *keystr = "key";
	TDB_DATA key = string_term_tdb_data(keystr);
	const char *valuestr = "value";
	TDB_DATA value = string_term_tdb_data(valuestr);
	struct do_locked1_state state = { .value = value };
	int ret = false;
	NTSTATUS status;

	ev = server_event_context();
	if (ev == NULL) {
		fprintf(stderr, "server_event_context() failed\n");
		return false;
	}
	msg = server_messaging_context();
	if (msg == NULL) {
		fprintf(stderr, "server_messaging_context() failed\n");
		return false;
	}

	backend = db_open(talloc_tos(), dbname, 0,
			  TDB_CLEAR_IF_FIRST, O_CREAT|O_RDWR, 0644,
			  DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
	if (backend == NULL) {
		fprintf(stderr, "db_open failed: %s\n", strerror(errno));
		return false;
	}

	db = db_open_watched(talloc_tos(), backend, msg);
	if (db == NULL) {
		fprintf(stderr, "db_open_watched failed: %s\n",
			strerror(errno));
		return false;
	}

	status = dbwrap_do_locked(db, key, do_locked1_cb, &state);
	if (!NT_STATUS_IS_OK(status)) {
		fprintf(stderr, "dbwrap_do_locked failed: %s\n",
			nt_errstr(status));
		goto fail;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		fprintf(stderr, "store returned %s\n",
			nt_errstr(state.status));
		goto fail;
	}

	status = dbwrap_parse_record(db, key, do_locked1_check, &state);
	if (!NT_STATUS_IS_OK(status)) {
		fprintf(stderr, "dbwrap_parse_record failed: %s\n",
			nt_errstr(status));
		goto fail;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		fprintf(stderr, "data compare returned %s\n",
			nt_errstr(status));
		goto fail;
	}

	status = dbwrap_do_locked(db, key, do_locked1_del, &state);
	if (!NT_STATUS_IS_OK(status)) {
		fprintf(stderr, "dbwrap_do_locked failed: %s\n",
			nt_errstr(status));
		goto fail;
	}
	if (!NT_STATUS_IS_OK(state.status)) {
		fprintf(stderr, "delete returned %s\n", nt_errstr(status));
		goto fail;
	}

	status = dbwrap_parse_record(db, key, do_locked1_check, &state);
	if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		fprintf(stderr, "parse_record returned %s, "
			"expected NOT_FOUND\n", nt_errstr(status));
		goto fail;
	}

	ret = true;
fail:
	TALLOC_FREE(db);
	unlink(dbname);
	return ret;
}