passdb-sql.c revision f8a86fdfb0048f9c87bf223373b35416ceb5856b
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (C) 2004 Timo Sirainen, Alex Howansky */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch lookup_credentials_callback_t *lookup_credentials;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void sql_query_save_results(struct sql_result *result,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch struct auth_request *auth_request = sql_request->auth_request;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch unsigned int i, fields_count;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch fields_count = sql_result_get_fields_count(result);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch for (i = 0; i < fields_count; i++) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch value = sql_result_get_field_value(result, i);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch auth_request_set_field(auth_request, name, value,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void sql_query_callback(struct sql_result *result, void *context)
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch struct passdb_sql_request *sql_request = context;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch struct auth_request *auth_request = sql_request->auth_request;
f883bf3eff62f5d27df5ee9ee664edc38a77937fStephan Bosch passdb_result = PASSDB_RESULT_INTERNAL_FAILURE;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch "Password query failed: %s",
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch } else if (ret == 0) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch auth_request_log_info(auth_request, "sql", "unknown user");
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* Note that we really want to check if the password field is
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch found. Just checking if password is set isn't enough,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch because with proxies we might want to return NULL as
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch if (sql_result_find_field(result, "password") < 0) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch "Password query must return a field named "
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch "'password'");
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch "Password query returned multiple matches");
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* auth_request_set_field() sets scheme */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch /* verify plain */
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch sql_request->callback.verify_plain(passdb_result, auth_request);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch ret = password_verify(auth_request->mech_password, password,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch } else if (ret == 0) {
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch auth_request_log_info(auth_request, "sql", "Password mismatch");
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch sql_request->callback.verify_plain(ret > 0 ? PASSDB_RESULT_OK :
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void sql_lookup_pass(struct passdb_sql_request *sql_request)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch var_expand(query, passdb_sql_conn->set.password_query,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch auth_request_get_var_expand_table(sql_request->auth_request,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch auth_request_log_debug(sql_request->auth_request, "sql",
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void sql_verify_plain(struct auth_request *request,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch sql_request = p_new(request->pool, struct passdb_sql_request, 1);
62aa68310d6f42467ca26880f678173bf1d26a83Stephan Bosch sql_request->callback.verify_plain = callback;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void sql_lookup_credentials(struct auth_request *request,
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch sql_request = p_new(request->pool, struct passdb_sql_request, 1);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch sql_request->callback.lookup_credentials = callback;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void passdb_sql_preinit(const char *args)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch auth_cache_parse_key(passdb_sql_conn->set.password_query);
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void passdb_sql_init(const char *args __attr_unused__)
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Bosch passdb_sql.blocking = (flags & SQL_DB_FLAG_BLOCKING) != 0;
6ae6496c225238a2c55a8cd96744ad976c44a726Stephan Boschstatic void passdb_sql_deinit(void)