Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/auth/db-sql.c @ 9575:0a00dcc4f0ea HEAD
lib-storage: Allow shared namespace prefix to use %variable modifiers.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 26 May 2010 17:07:51 +0100 |
parents | 00cd9aacd03c |
children |
rev | line source |
---|---|
9532
00cd9aacd03c
Updated copyright notices to include year 2010.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1 /* Copyright (c) 2003-2010 Dovecot authors, see the included COPYING file */ |
2754
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 |
5474
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
14 #define DEF_STR(name) DEF_STRUCT_STR(name, sql_settings) |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
15 #define DEF_INT(name) DEF_STRUCT_INT(name, sql_settings) |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
16 #define DEF_BOOL(name) DEF_STRUCT_BOOL(name, sql_settings) |
2754
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 static struct setting_def setting_defs[] = { |
5474
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
19 DEF_STR(driver), |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
20 DEF_STR(connect), |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
21 DEF_STR(password_query), |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
22 DEF_STR(user_query), |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
23 DEF_STR(update_query), |
331337b735c9
Added type checks to setting defines.
Timo Sirainen <tss@iki.fi>
parents:
5462
diff
changeset
|
24 DEF_STR(default_pass_scheme), |
3913
af15aab60ff1
Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents:
3908
diff
changeset
|
25 |
af15aab60ff1
Settings' default listing wasn't ended properly, which could have caused
Timo Sirainen <tss@iki.fi>
parents:
3908
diff
changeset
|
26 { 0, NULL, 0 } |
2754
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 }; |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 |
8027
62cf70991cf2
extern/static fixes (from a sparse check by Diego Liziero)
Timo Sirainen <tss@iki.fi>
parents:
7986
diff
changeset
|
29 static struct sql_settings default_sql_settings = { |
2754
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 MEMBER(driver) NULL, |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 MEMBER(connect) NULL, |
7110
c3365e886746
Updated comments and defaults to SQL queries. SQL is typically used with
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
32 MEMBER(password_query) "SELECT username, domain, password FROM users WHERE username = '%n' AND domain = '%d'", |
c3365e886746
Updated comments and defaults to SQL queries. SQL is typically used with
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
33 MEMBER(user_query) "SELECT home, uid, gid FROM users WHERE username = '%n' AND domain = '%d'", |
c3365e886746
Updated comments and defaults to SQL queries. SQL is typically used with
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
34 MEMBER(update_query) "UPDATE users SET password = '%w' WHERE username = '%n' AND domain = '%d'", |
7986
bcd70b69c7e5
Changed SQL's default password scheme to MD5.
Timo Sirainen <tss@iki.fi>
parents:
7110
diff
changeset
|
35 MEMBER(default_pass_scheme) "MD5" |
2754
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 }; |
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 *connections = NULL; |
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 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
|
41 { |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 struct sql_connection *conn; |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 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
|
45 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
|
46 return conn; |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
47 } |
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 return NULL; |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 } |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 |
3c3ac12be307
Created generic asynchronous SQL API and implemented MySQL and PostgreSQL
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 static const char *parse_setting(const char *key, const char *value, |
4907
5b4c9b20eba0
Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
53 struct sql_connection *conn) |
2754
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 | 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); |
6428
7cad076906eb
pool_unref() now takes ** pointer.
Timo Sirainen <tss@iki.fi>
parents:
5475
diff
changeset
|
109 pool_unref(&conn->pool); |
2754
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 |