annotate src/auth/db-sql.c @ 4903:204d7edc7cdc HEAD

Added context parameter type safety checks for most callback APIs.
author Timo Sirainen <tss@iki.fi>
date Fri, 15 Dec 2006 18:55:29 +0200
parents 2c1cc5bbc260
children 5b4c9b20eba0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2003-2004 Timo Sirainen */
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
3474
9096b7957413 Removed direct config.h including. I'm not sure why it was done before,
Timo Sirainen <tss@iki.fi>
parents: 3422
diff changeset
3 #include "common.h"
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #if defined(PASSDB_SQL) || defined(USERDB_SQL)
3474
9096b7957413 Removed direct config.h including. I'm not sure why it was done before,
Timo Sirainen <tss@iki.fi>
parents: 3422
diff changeset
6
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "settings.h"
4295
4fc637010202 Escape SQL strings using sql_escape_string(). Fixes the problems with
Timo Sirainen <tss@iki.fi>
parents: 3913
diff changeset
8 #include "auth-request.h"
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "db-sql.h"
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include <stddef.h>
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include <stdlib.h>
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #define DEF(type, name) { type, #name, offsetof(struct sql_settings, name) }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 static struct setting_def setting_defs[] = {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 DEF(SET_STR, driver),
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 DEF(SET_STR, connect),
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 DEF(SET_STR, password_query),
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 DEF(SET_STR, user_query),
4782
2c1cc5bbc260 Added auth_request_set_credentials() to modify credentials in passdb and
Timo Sirainen <tss@iki.fi>
parents: 4350
diff changeset
21 DEF(SET_STR, update_query),
3913
af15aab60ff1 Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents: 3908
diff changeset
22 DEF(SET_STR, default_pass_scheme),
af15aab60ff1 Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents: 3908
diff changeset
23
af15aab60ff1 Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents: 3908
diff changeset
24 { 0, NULL, 0 }
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 };
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 struct sql_settings default_sql_settings = {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 MEMBER(driver) NULL,
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 MEMBER(connect) NULL,
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 MEMBER(password_query) "SELECT password FROM users WHERE userid = '%u'",
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 MEMBER(user_query) "SELECT home, uid, gid FROM users WHERE userid = '%u'",
4782
2c1cc5bbc260 Added auth_request_set_credentials() to modify credentials in passdb and
Timo Sirainen <tss@iki.fi>
parents: 4350
diff changeset
32 MEMBER(update_query) "UPDATE users SET password = '%w' WHERE userid = '%u'",
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 MEMBER(default_pass_scheme) "PLAIN-MD5"
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 };
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 static struct sql_connection *connections = NULL;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 static struct sql_connection *sql_conn_find(const char *config_path)
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 struct sql_connection *conn;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 for (conn = connections; conn != NULL; conn = conn->next) {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 if (strcmp(conn->config_path, config_path) == 0)
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 return conn;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 return NULL;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 static const char *parse_setting(const char *key, const char *value,
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 void *context)
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 struct sql_connection *conn = context;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 return parse_setting_from_defs(conn->pool, setting_defs,
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 &conn->set, key, value);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 struct sql_connection *db_sql_init(const char *config_path)
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 struct sql_connection *conn;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 pool_t pool;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 conn = sql_conn_find(config_path);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 if (conn != NULL) {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 conn->refcount++;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 return conn;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69
3908
afe21b6d4b68 Give a clear error message if SQL/LDAP configuration file path was left
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
70 if (*config_path == '\0')
4350
a916c98dc910 s/ldap/sql/ in error message
Timo Sirainen <tss@iki.fi>
parents: 4295
diff changeset
71 i_fatal("sql: Configuration file path not given");
3908
afe21b6d4b68 Give a clear error message if SQL/LDAP configuration file path was left
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
72
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 pool = pool_alloconly_create("sql_connection", 1024);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 conn = p_new(pool, struct sql_connection, 1);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 conn->pool = pool;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 conn->refcount = 1;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 conn->config_path = p_strdup(pool, config_path);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 conn->set = default_sql_settings;
4903
204d7edc7cdc Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents: 4782
diff changeset
81 if (!settings_read(config_path, NULL, parse_setting,
204d7edc7cdc Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents: 4782
diff changeset
82 null_settings_section_callback, conn))
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 exit(FATAL_DEFAULT);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 if (conn->set.driver == NULL) {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 i_fatal("sql: driver not set in configuration file %s",
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 config_path);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 if (conn->set.connect == NULL) {
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 i_fatal("sql: connect string not set in configuration file %s",
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 config_path);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 }
3422
28be9a80908e Don't connect to SQL database in main dovecot-auth process if we're using
Timo Sirainen <tss@iki.fi>
parents: 2754
diff changeset
93 conn->db = sql_init(conn->set.driver, conn->set.connect);
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95 conn->next = connections;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 connections = conn;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 return conn;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
100 void db_sql_unref(struct sql_connection **_conn)
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 {
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
102 struct sql_connection *conn = *_conn;
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
103
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
104 *_conn = NULL;
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 if (--conn->refcount > 0)
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 return;
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3474
diff changeset
108 sql_deinit(&conn->db);
2754
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 pool_unref(conn->pool);
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 }
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111
3c3ac12be307 Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 #endif