From: Siavash Tavakoli Date: Mon, 3 May 2021 12:42:56 +0000 (+0100) Subject: lib-dict: Add dict_op_settings X-Git-Tag: 2.3.17~392 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c84087024980d47771ab9e43edda377b810fd9ff;p=thirdparty%2Fdovecot%2Fcore.git lib-dict: Add dict_op_settings Used for dict operations. Currently username & home_dir are used. Also, change dict_lookup() to accept a parameter of this type but don't actually use it. --- diff --git a/src/auth/db-dict.c b/src/auth/db-dict.c index e86f0c444d..09c357d9a4 100644 --- a/src/auth/db-dict.c +++ b/src/auth/db-dict.c @@ -415,7 +415,7 @@ static int db_dict_iter_lookup_key_values(struct db_dict_value_iter *iter) str_truncate(path, strlen(DICT_PATH_SHARED)); str_append(path, key->key->key); - ret = dict_lookup(iter->conn->dict, iter->pool, + ret = dict_lookup(iter->conn->dict, NULL, iter->pool, str_c(path), &key->value, &error); if (ret > 0) { e_debug(authdb_event(iter->auth_request), diff --git a/src/dict/dict-commands.c b/src/dict/dict-commands.c index 85d2eec70b..bdb7c40be3 100644 --- a/src/dict/dict-commands.c +++ b/src/dict/dict-commands.c @@ -224,7 +224,7 @@ static int cmd_lookup(struct dict_connection_cmd *cmd, const char *line) /* */ dict_connection_cmd_async(cmd); event_add_str(cmd->event, "key", line); - dict_lookup_async(cmd->conn->dict, line, cmd_lookup_callback, cmd); + dict_lookup_async(cmd->conn->dict, NULL, line, cmd_lookup_callback, cmd); return 1; } diff --git a/src/doveadm/doveadm-dict.c b/src/doveadm/doveadm-dict.c index dcede04848..134e762acd 100644 --- a/src/doveadm/doveadm-dict.c +++ b/src/doveadm/doveadm-dict.c @@ -108,7 +108,7 @@ static void cmd_dict_get(struct doveadm_cmd_context *cctx) i_zero(&ctx); ctx.pool = pool_alloconly_create("doveadm dict lookup", 512); ctx.ret = -2; - dict_lookup_async(dict, key, dict_lookup_callback, &ctx); + dict_lookup_async(dict, NULL, key, dict_lookup_callback, &ctx); while (ctx.ret == -2) dict_wait(dict); if (ctx.ret < 0) { diff --git a/src/lib-dict-backend/test-dict-sql.c b/src/lib-dict-backend/test-dict-sql.c index 1d16728e36..ffaa9c6a8e 100644 --- a/src/lib-dict-backend/test-dict-sql.c +++ b/src/lib-dict-backend/test-dict-sql.c @@ -64,7 +64,7 @@ static void test_lookup_one(void) test_set_expected(dict, &res); - test_assert(dict_lookup(dict, pool, "shared/dictmap/hello/world", &value, &error) == 1); + test_assert(dict_lookup(dict, NULL, pool, "shared/dictmap/hello/world", &value, &error) == 1); test_assert_strcmp(value, "one"); if (error != NULL) i_error("dict_lookup failed: %s", error); diff --git a/src/lib-dict-extra/test-dict-fs.c b/src/lib-dict-extra/test-dict-fs.c index b95a59f794..aa7f67491e 100644 --- a/src/lib-dict-extra/test-dict-fs.c +++ b/src/lib-dict-extra/test-dict-fs.c @@ -13,7 +13,7 @@ static void test_dict_set_get(struct dict *dict, const char *key, dict_set(t, key, value); if (dict_transaction_commit(&t, &error) < 0) i_fatal("dict_transaction_commit(%s) failed: %s", key, error); - if (dict_lookup(dict, pool_datastack_create(), key, &got_value, + if (dict_lookup(dict, NULL, pool_datastack_create(), key, &got_value, &error) < 0) i_fatal("dict_lookup(%s) failed: %s", key, error); test_assert_strcmp(got_value, value); diff --git a/src/lib-dict/dict-client.c b/src/lib-dict/dict-client.c index 3539e6b920..d16ddb4a9a 100644 --- a/src/lib-dict/dict-client.c +++ b/src/lib-dict/dict-client.c @@ -1006,7 +1006,7 @@ static int client_dict_lookup(struct dict *_dict, pool_t pool, const char *key, i_zero(&lookup); lookup.ret = -2; - dict_lookup_async(_dict, key, client_dict_lookup_callback, &lookup); + dict_lookup_async(_dict, NULL, key, client_dict_lookup_callback, &lookup); if (lookup.ret == -2) client_dict_wait(_dict); diff --git a/src/lib-dict/dict-lua.c b/src/lib-dict/dict-lua.c index 1955722396..bfd2b5bbbe 100644 --- a/src/lib-dict/dict-lua.c +++ b/src/lib-dict/dict-lua.c @@ -77,7 +77,7 @@ static int lua_dict_lookup(lua_State *L) dict = xlua_dict_getptr(L, 1, NULL); key = luaL_checkstring(L, 2); - dict_lookup_async(dict, key, lua_dict_lookup_callback, L); + dict_lookup_async(dict, NULL, key, lua_dict_lookup_callback, L); return lua_dict_async_continue(L, lua_yieldk(L, 0, 0, lua_dict_async_continue), 0); diff --git a/src/lib-dict/dict.c b/src/lib-dict/dict.c index 48f709dd61..250dacb48e 100644 --- a/src/lib-dict/dict.c +++ b/src/lib-dict/dict.c @@ -294,7 +294,8 @@ static void dict_commit_callback(const struct dict_commit_result *result, } } -int dict_lookup(struct dict *dict, pool_t pool, const char *key, +int dict_lookup(struct dict *dict, const struct dict_op_settings *set ATTR_UNUSED, + pool_t pool, const char *key, const char **value_r, const char **error_r) { struct event *event = event_create(dict->event); @@ -310,15 +311,16 @@ int dict_lookup(struct dict *dict, pool_t pool, const char *key, } #undef dict_lookup_async -void dict_lookup_async(struct dict *dict, const char *key, - dict_lookup_callback_t *callback, void *context) +void dict_lookup_async(struct dict *dict, const struct dict_op_settings *set ATTR_UNUSED, + const char *key, dict_lookup_callback_t *callback, + void *context) { if (dict->v.lookup_async == NULL) { struct dict_lookup_result result; i_zero(&result); /* event is going to be sent by dict_lookup */ - result.ret = dict_lookup(dict, pool_datastack_create(), + result.ret = dict_lookup(dict, NULL, pool_datastack_create(), key, &result.value, &result.error); const char *const values[] = { result.value, NULL }; result.values = values; diff --git a/src/lib-dict/dict.h b/src/lib-dict/dict.h index 194bcb4758..9b48376c57 100644 --- a/src/lib-dict/dict.h +++ b/src/lib-dict/dict.h @@ -40,6 +40,12 @@ struct dict_settings { struct event *event_parent; }; +struct dict_op_settings { + const char *username; + /* home directory for the user, if known */ + const char *home_dir; +}; + struct dict_lookup_result { int ret; @@ -97,12 +103,13 @@ bool dict_switch_ioloop(struct dict *dict) ATTR_NOWARN_UNUSED_RESULT; /* Lookup value for key. Set it to NULL if it's not found. Returns 1 if found, 0 if not found and -1 if lookup failed. */ -int dict_lookup(struct dict *dict, pool_t pool, +int dict_lookup(struct dict *dict, const struct dict_op_settings *set, pool_t pool, const char *key, const char **value_r, const char **error_r); -void dict_lookup_async(struct dict *dict, const char *key, - dict_lookup_callback_t *callback, void *context); -#define dict_lookup_async(dict, key, callback, context) \ - dict_lookup_async(dict, key, (dict_lookup_callback_t *)(callback), \ +void dict_lookup_async(struct dict *dict, const struct dict_op_settings *set, + const char *key, dict_lookup_callback_t *callback, + void *context); +#define dict_lookup_async(dict, set, key, callback, context) \ + dict_lookup_async(dict, set, key, (dict_lookup_callback_t *)(callback), \ 1 ? (context) : \ CALLBACK_TYPECHECK(callback, \ void (*)(const struct dict_lookup_result *, typeof(context)))) diff --git a/src/lib-dict/test-dict-client.c b/src/lib-dict/test-dict-client.c index 80607ed1d7..74e58c066d 100644 --- a/src/lib-dict/test-dict-client.c +++ b/src/lib-dict/test-dict-client.c @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) switch (i_rand_limit(4)) { case 0: pending++; - dict_lookup_async(dict, key, lookup_callback, NULL); + dict_lookup_async(dict, NULL, key, lookup_callback, NULL); break; case 1: { struct dict_transaction_context *trans; diff --git a/src/lib-fs/fs-dict.c b/src/lib-fs/fs-dict.c index 6a0a8bd026..172236ffbe 100644 --- a/src/lib-fs/fs-dict.c +++ b/src/lib-fs/fs-dict.c @@ -157,7 +157,7 @@ static int fs_dict_lookup(struct dict_fs_file *file) if (file->value != NULL) return 0; - ret = dict_lookup(fs->dict, file->pool, file->key, &file->value, &error); + ret = dict_lookup(fs->dict, NULL, file->pool, file->key, &file->value, &error); if (ret > 0) return 0; else if (ret < 0) { diff --git a/src/lib-oauth2/oauth2-jwt.c b/src/lib-oauth2/oauth2-jwt.c index 6dd09be4d3..6e850c6937 100644 --- a/src/lib-oauth2/oauth2-jwt.c +++ b/src/lib-oauth2/oauth2-jwt.c @@ -98,7 +98,7 @@ oauth2_lookup_hmac_key(const struct oauth2_settings *set, const char *azp, /* do a synchronous dict lookup */ lookup_key = t_strconcat(DICT_PATH_SHARED, azp, "/", alg, "/", key_id, NULL); - if ((ret = dict_lookup(set->key_dict, pool_datastack_create(), + if ((ret = dict_lookup(set->key_dict, NULL, pool_datastack_create(), lookup_key, &base64_key, error_r)) < 0) { return -1; } else if (ret == 0) { @@ -178,7 +178,7 @@ oauth2_lookup_pubkey(const struct oauth2_settings *set, const char *azp, /* do a synchronous dict lookup */ lookup_key = t_strconcat(DICT_PATH_SHARED, azp, "/", alg, "/", key_id, NULL); - if ((ret = dict_lookup(set->key_dict, pool_datastack_create(), + if ((ret = dict_lookup(set->key_dict, NULL, pool_datastack_create(), lookup_key, &key_str, error_r)) < 0) { return -1; } else if (ret == 0) { diff --git a/src/lib-storage/index/index-attribute.c b/src/lib-storage/index/index-attribute.c index 8aef8c89c7..e7d20888d6 100644 --- a/src/lib-storage/index/index-attribute.c +++ b/src/lib-storage/index/index-attribute.c @@ -257,7 +257,7 @@ int index_storage_attribute_get(struct mailbox *box, if (index_storage_get_dict(box, type_flags, &dict, &mailbox_prefix) < 0) return -1; - ret = dict_lookup(dict, pool_datastack_create(), + ret = dict_lookup(dict, NULL, pool_datastack_create(), key_get_prefixed(type_flags, mailbox_prefix, key), &value_r->value, &error); if (ret < 0) { diff --git a/src/plugins/quota/quota-dict.c b/src/plugins/quota/quota-dict.c index 10f608f09f..e4a9729ae8 100644 --- a/src/plugins/quota/quota-dict.c +++ b/src/plugins/quota/quota-dict.c @@ -160,7 +160,7 @@ dict_quota_get_resource(struct quota_root *_root, const char *key, *value, *error; key = want_bytes ? DICT_QUOTA_CURRENT_BYTES_PATH : DICT_QUOTA_CURRENT_COUNT_PATH; - ret = dict_lookup(root->dict, unsafe_data_stack_pool, + ret = dict_lookup(root->dict, NULL, unsafe_data_stack_pool, key, &value, &error); if (ret < 0) { *error_r = t_strdup_printf(