bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainendict_query_save_results(struct auth_request *auth_request,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen while (db_dict_value_iter_next(iter, &key, &value)) {
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen auth_request_set_userdb_field(auth_request, key, value);
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen if (db_dict_value_iter_deinit(&iter, &error) < 0) {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_error(auth_request, AUTH_SUBSYS_DB, "%s", error);
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenstatic void userdb_dict_lookup(struct auth_request *auth_request,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen struct userdb_module *_module = auth_request->userdb->userdb;
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen if (array_count(&module->conn->set.userdb_fields) == 0 &&
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen array_count(&module->conn->set.parsed_userdb_objects) == 0) {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_error(auth_request, AUTH_SUBSYS_DB,
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen "No userdb_objects or userdb_fields specified");
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen callback(USERDB_RESULT_INTERNAL_FAILURE, auth_request);
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen ret = db_dict_value_iter_init(module->conn, auth_request,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen userdb_result = USERDB_RESULT_INTERNAL_FAILURE;
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen else if (ret == 0) {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_unknown_user(auth_request, AUTH_SUBSYS_DB);
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen if (dict_query_save_results(auth_request, iter) < 0)
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen userdb_result = USERDB_RESULT_INTERNAL_FAILURE;
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenuserdb_dict_iterate_init(struct auth_request *auth_request,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen userdb_iter_callback_t *callback, void *context)
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen struct userdb_module *_module = auth_request->userdb->userdb;
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen ctx = i_new(struct dict_userdb_iterate_context, 1);
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen if (*module->conn->set.iterate_prefix == '\0') {
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_error(auth_request, AUTH_SUBSYS_DB,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen "iterate: iterate_prefix not set");
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (auth_request_var_expand(path, module->conn->set.iterate_prefix,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen auth_request_log_error(auth_request, AUTH_SUBSYS_DB,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen "Failed to expand iterate_prefix=%s: %s",
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen ctx->key_prefix = p_strdup(auth_request->pool, str_c(path));
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen ctx->key_prefix_len = strlen(ctx->key_prefix);
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen ctx->iter = dict_iterate_init(module->conn->dict, ctx->key_prefix, 0);
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen auth_request_log_debug(auth_request, AUTH_SUBSYS_DB,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenstatic const char *
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenuserdb_dict_get_user(struct dict_userdb_iterate_context *ctx, const char *key)
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen i_assert(strncmp(key, ctx->key_prefix, ctx->key_prefix_len) == 0);
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenstatic void userdb_dict_iterate_next(struct userdb_iterate_context *_ctx)
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen if (ctx->iter != NULL && dict_iterate(ctx->iter, &key, &value))
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen _ctx->callback(userdb_dict_get_user(ctx, key), _ctx->context);
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenstatic int userdb_dict_iterate_deinit(struct userdb_iterate_context *_ctx)
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainen if (dict_iterate_deinit(&ctx->iter, &error) < 0) {
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenuserdb_dict_preinit(pool_t pool, const char *args)
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainen module = p_new(pool, struct dict_userdb_module, 1);
74674a53a72dab535c61f455b2246ef2797844eaTimo Sirainen module->module.default_cache_key = auth_cache_parse_key(pool,
79042f8c2ec1778528584c064b164d1ebcdde16bTimo Sirainen db_dict_parse_cache_key(&conn->set.keys, &conn->set.userdb_fields,
2028d80c2704bbf62b29b2c624b0ee3c3a03c462Timo Sirainenstatic void userdb_dict_deinit(struct userdb_module *_module)