userdb-sql.c revision e665999b757e60bfb98e5a84a78b05f061453140
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen/* Copyright (c) 2004-2011 Dovecot authors, see the included COPYING file */
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstatic void userdb_sql_iterate_next(struct userdb_iterate_context *_ctx);
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainenstatic int userdb_sql_iterate_deinit(struct userdb_iterate_context *_ctx);
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainensql_query_get_result(struct sql_result *result,
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen unsigned int i, fields_count;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen fields_count = sql_result_get_fields_count(result);
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen for (i = 0; i < fields_count; i++) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen value = sql_result_get_field_value(result, i);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic void sql_query_callback(struct sql_result *sql_result,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct auth_request *auth_request = sql_request->auth_request;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct userdb_module *_module = auth_request->userdb->userdb;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen enum userdb_result result = USERDB_RESULT_INTERNAL_FAILURE;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen "User query failed: %s",
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen "User query failed: %s "
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen "(using built-in default user_query: %s)",
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen } else if (ret == 0) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen auth_request_log_info(auth_request, "sql", "Unknown user");
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen sql_query_get_result(sql_result, auth_request);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic const char *
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenuserdb_sql_escape(const char *str, const struct auth_request *auth_request)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct userdb_module *_module = auth_request->userdb->userdb;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen return sql_escape_string(module->conn->db, str);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void userdb_sql_lookup(struct auth_request *auth_request,
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen struct userdb_module *_module = auth_request->userdb->userdb;
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen var_expand(query, module->conn->set.user_query,
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen auth_request_get_var_expand_table(auth_request,
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen sql_request = i_new(struct userdb_sql_request, 1);
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen auth_request_log_debug(auth_request, "sql", "%s", str_c(query));
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainenstatic void sql_iter_query_callback(struct sql_result *sql_result,
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainenuserdb_sql_iterate_init(struct auth_request *auth_request,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen userdb_iter_callback_t *callback, void *context)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct userdb_module *_module = auth_request->userdb->userdb;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen var_expand(query, module->conn->set.iterate_query,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen auth_request_get_var_expand_table(auth_request,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen ctx = i_new(struct sql_userdb_iterate_context, 1);
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen auth_request_log_debug(auth_request, "sql", "%s", str_c(query));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int userdb_sql_iterate_get_user(struct sql_userdb_iterate_context *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char **user_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* try user first */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen idx = sql_result_find_field(ctx->result, "user");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *user_r = sql_result_get_field_value(ctx->result, idx);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* username [+ domain]? */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen idx = sql_result_find_field(ctx->result, "username");
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* no user or username, fail */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *user_r = sql_result_get_field_value(ctx->result, idx);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen domain = sql_result_find_field_value(ctx->result, "domain");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *user_r = t_strconcat(*user_r, "@", domain, NULL);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstatic void userdb_sql_iterate_next(struct userdb_iterate_context *_ctx)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen struct userdb_module *_module = _ctx->auth_request->userdb->userdb;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen struct sql_userdb_module *module = (struct sql_userdb_module *)_module;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen /* query not finished yet */
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if (userdb_sql_iterate_get_user(ctx, &user) < 0)
a60c1c1fca85402e6fccbf3ae0784b7179ae186cTimo Sirainen i_error("sql: Iterate query didn't return 'user' field");
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen i_error("sql: Iterate query returned NULL user");
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen } else if (ret < 0) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen "(using built-in default iterate_query: %s)",
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstatic int userdb_sql_iterate_deinit(struct userdb_iterate_context *_ctx)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen /* sql query hasn't finished yet */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenuserdb_sql_preinit(pool_t pool, const char *args)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen module = p_new(pool, struct sql_userdb_module, 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen auth_cache_parse_key(pool, module->conn->set.user_query);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic void userdb_sql_init(struct userdb_module *_module)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen _module->blocking = (flags & SQL_DB_FLAG_BLOCKING) != 0;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic void userdb_sql_deinit(struct userdb_module *_module)