db-sql.c revision a8c5a86d183db25a57bf193c06b41e092ec2e151
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen/* Copyright (c) 2003-2014 Dovecot authors, see the included COPYING file */
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen#define DEF_STR(name) DEF_STRUCT_STR(name, sql_settings)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen#define DEF_INT(name) DEF_STRUCT_INT(name, sql_settings)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen#define DEF_BOOL(name) DEF_STRUCT_BOOL(name, sql_settings)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenstatic struct sql_settings default_sql_settings = {
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen .password_query = "SELECT username, domain, password FROM users WHERE username = '%n' AND domain = '%d'",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen .user_query = "SELECT home, uid, gid FROM users WHERE username = '%n' AND domain = '%d'",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen .update_query = "UPDATE users SET password = '%w' WHERE username = '%n' AND domain = '%d'",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen .iterate_query = "SELECT username, domain FROM users",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenstatic struct sql_connection *connections = NULL;
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenstatic struct sql_connection *sql_conn_find(const char *config_path)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen for (conn = connections; conn != NULL; conn = conn->next) {
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen if (strcmp(conn->config_path, config_path) == 0)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenstatic const char *parse_setting(const char *key, const char *value,
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen return parse_setting_from_defs(conn->pool, setting_defs,
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenstruct sql_connection *db_sql_init(const char *config_path, bool userdb)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen i_fatal("sql: Configuration file path not given");
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen pool = pool_alloconly_create("sql_connection", 1024);
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen conn->config_path = p_strdup(pool, config_path);
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen if (!settings_read_nosection(config_path, parse_setting, conn, &error))
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen if (conn->set.password_query == default_sql_settings.password_query)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen if (conn->set.user_query == default_sql_settings.user_query)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen if (conn->set.update_query == default_sql_settings.update_query)
1c4f8e4c4e5f3a5f05692a2d5c57f96a5b612f3dTimo Sirainen if (conn->set.iterate_query == default_sql_settings.iterate_query)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen i_fatal("sql: driver not set in configuration file %s",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen i_fatal("sql: connect string not set in configuration file %s",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen conn->db = sql_init(conn->set.driver, conn->set.connect);
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenvoid db_sql_unref(struct sql_connection **_conn)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen /* abort all pending auth requests before setting conn to NULL,
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen so that callbacks can still access it */
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenvoid db_sql_connect(struct sql_connection *conn)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen /* auth worker's sql connection failed. we can't do anything
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen useful until the connection works. there's no point in
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen having tons of worker processes all logging failures,
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen so tell the auth master to stop creating new workers (and
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen maybe close old ones). this handling is especially useful if
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen we reach the max. number of connections for sql server. */
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenvoid db_sql_success(struct sql_connection *conn ATTR_UNUSED)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainenvoid db_sql_check_userdb_warning(struct sql_connection *conn)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen if (worker || conn->userdb_used || conn->set.userdb_warning_disable)
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen i_warning("sql: Ignoring changed user_query in %s, "
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen "because userdb sql not used. "
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen "(If this is intentional, set userdb_warning_disable=yes)",
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen i_warning("sql: Ignoring changed iterate_query in %s, "
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen "because userdb sql not used. "
e98de01b5644c88b6053e2921eb5e9a506fe263fTimo Sirainen "(If this is intentional, set userdb_warning_disable=yes)",