passdb-sql.c revision 4042b17026279498eb37922ee3316a50144c51a4
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (C) 2004 Timo Sirainen, Alex Howansky */
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen lookup_credentials_callback_t *lookup_credentials;
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainenstatic void result_save_extra_fields(struct sql_result *result,
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen unsigned int i, fields_count;
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen fields_count = sql_result_get_fields_count(result);
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen for (i = 0; i < fields_count; i++) {
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen value = sql_result_get_field_value(result, i);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen /* don't delay replying to client of the failure */
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen /* user can't actually login - don't keep this
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen reply for master */
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen /* we're proxying authentication for this user. send
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen password back if using plaintext authentication. */
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainenstatic void sql_query_callback(struct sql_result *result, void *context)
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen struct passdb_sql_request *sql_request = context;
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen struct auth_request *auth_request = sql_request->auth_request;
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen } else if (ret == 0) {
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen } else if ((idx = sql_result_find_field(result, "password")) < 0) {
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen i_error("sql(%s): Password query didn't return password",
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen password = t_strdup(sql_result_get_field_value(result, idx));
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen result_save_extra_fields(result, sql_request, auth_request);
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen /* make sure there was only one row returned */
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen i_error("sql(%s): Password query returned multiple "
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen scheme = passdb_sql_conn->set.default_pass_scheme;
3d2d1ba0a14521c7320370d8cafb9a95b49d333dTimo Sirainen passdb_handle_credentials(sql_request->credentials,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen /* verify plain */
4d6c99647852146fec4f2663767b17593ef73a47Timo Sirainen sql_request->callback.verify_plain(PASSDB_RESULT_USER_UNKNOWN,
4d6c99647852146fec4f2663767b17593ef73a47Timo Sirainen ret = password_verify(sql_request->password, password, scheme, user);
4d6c99647852146fec4f2663767b17593ef73a47Timo Sirainen i_error("sql(%s): Unknown password scheme %s",
4d6c99647852146fec4f2663767b17593ef73a47Timo Sirainen } else if (ret == 0) {
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen sql_request->callback.verify_plain(ret > 0 ? PASSDB_RESULT_OK :
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainenstatic void sql_lookup_pass(struct passdb_sql_request *sql_request)
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen var_expand(query, passdb_sql_conn->set.password_query,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen auth_request_get_var_expand_table(sql_request->auth_request,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainenstatic void sql_verify_plain(struct auth_request *request, const char *password,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen sql_request = i_malloc(sizeof(struct passdb_sql_request) +
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen sql_request->callback.verify_plain = callback;
9511a40d933181045343110c8101b75887062aaeTimo Sirainenstatic void sql_lookup_credentials(struct auth_request *request,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen sql_request = i_new(struct passdb_sql_request, 1);
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen sql_request->callback.lookup_credentials = callback;
static void passdb_sql_deinit(void)