Browse code

- make db tables and colum names configurable with help of autogenerated files from XML, closed feature request #2019376 - adapt module to use this implementation - also use autogenerated documentation from XML source

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@4860 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 05/09/2008 09:06:59
Showing 9 changed files
... ...
@@ -29,9 +29,7 @@ Henning Westerholt
29 29
 
30 30
         1.3. Exported Parameters
31 31
 
32
-              1.3.1. db_url (string)
33
-              1.3.2. db_table (string)
34
-              1.3.3. use_domain (integer)
32
+              1.3.1. use_domain (integer)
35 33
 
36 34
         1.4. Exported Functions
37 35
 
... ...
@@ -48,16 +46,41 @@ Henning Westerholt
48 48
 
49 49
               1.6.1. Database setup
50 50
 
51
+   2. Module parameter for database access.
52
+
53
+        2.1. db_url (String)
54
+        2.2. userblacklist_table (String)
55
+        2.3. userblacklist_id_col (string)
56
+        2.4. userblacklist_username_col (string)
57
+        2.5. userblacklist_domain_col (string)
58
+        2.6. userblacklist_prefix_col (string)
59
+        2.7. userblacklist_whitelist_col (string)
60
+        2.8. globalblacklist_table (String)
61
+        2.9. globalblacklist_id_col (string)
62
+        2.10. globalblacklist_prefix_col (string)
63
+        2.11. globalblacklist_whitelist_col (string)
64
+        2.12. globalblacklist_description_col (string)
65
+
51 66
    List of Examples
52 67
 
53
-   1.1. Set db_url parameter
54
-   1.2. Set db_table parameter
55
-   1.3. Set use_domain parameter
56
-   1.4. check_user_blacklist usage
57
-   1.5. check_blacklist usage
58
-   1.6. reload_blacklists usage
59
-   1.7. Example database content - globalblacklist table
60
-   1.8. Example database content - userblacklist table
68
+   1.1. Set use_domain parameter
69
+   1.2. check_user_blacklist usage
70
+   1.3. check_blacklist usage
71
+   1.4. reload_blacklists usage
72
+   1.5. Example database content - globalblacklist table
73
+   1.6. Example database content - userblacklist table
74
+   2.1. Set db_url parameter
75
+   2.2. Set userblacklist_table parameter
76
+   2.3. Set userblacklist_id_col parameter
77
+   2.4. Set userblacklist_username_col parameter
78
+   2.5. Set userblacklist_domain_col parameter
79
+   2.6. Set userblacklist_prefix_col parameter
80
+   2.7. Set userblacklist_whitelist_col parameter
81
+   2.8. Set globalblacklist_table parameter
82
+   2.9. Set globalblacklist_id_col parameter
83
+   2.10. Set globalblacklist_prefix_col parameter
84
+   2.11. Set globalblacklist_whitelist_col parameter
85
+   2.12. Set globalblacklist_description_col parameter
61 86
 
62 87
 Chapter 1. Admin Guide
63 88
 
... ...
@@ -100,38 +123,14 @@ Chapter 1. Admin Guide
100 100
 
101 101
 1.3. Exported Parameters
102 102
 
103
-1.3.1. db_url (string)
104
-
105
-   Url to the database containing the routing data.
106
-
107
-   Default value is
108
-   "mysql://openserro:openserro@localhost/openser".
109
-
110
-   Example 1.1. Set db_url parameter
111
-...
112
-modparam("userblacklist", "db_url", "dbdriver://username:password@dbhost
113
-/dbname")
114
-...
115
-
116
-1.3.2. db_table (string)
117
-
118
-   Name of the table where the user blacklist data is stored.
119
-
120
-   Default value is "userblacklist".
121
-
122
-   Example 1.2. Set db_table parameter
123
-...
124
-modparam("userblacklist", "db_table", "userblacklist")
125
-...
126
-
127
-1.3.3. use_domain (integer)
103
+1.3.1. use_domain (integer)
128 104
 
129 105
    If set to non-zero value, the domain column in the
130 106
    userblacklist is used.
131 107
 
132 108
    Default value is "0".
133 109
 
134
-   Example 1.3. Set use_domain parameter
110
+   Example 1.1. Set use_domain parameter
135 111
 ...
136 112
 modparam("userblacklist", "use_domain", 0)
137 113
 ...
... ...
@@ -150,7 +149,7 @@ number, string table)
150 150
    the defaults are used if they are ommited. The number parameter
151 151
    can be used to check for example against the from URI user.
152 152
 
153
-   Example 1.4. check_user_blacklist usage
153
+   Example 1.2. check_user_blacklist usage
154 154
 ...
155 155
 $avp(i:80) = $rU;
156 156
 # rewrite the R-URI
... ...
@@ -166,7 +165,7 @@ if (!check_user_blacklist("$avp(i:80)", "$avp(i:82)"))
166 166
    given table. If a match is found and it is not set to
167 167
    whitelist, false is returned. Otherwise, true is returned.
168 168
 
169
-   Example 1.5. check_blacklist usage
169
+   Example 1.3. check_blacklist usage
170 170
 ...
171 171
 if (!check_blacklist("global_blacklist")))
172 172
         sl_send_reply("403", "Forbidden");
... ...
@@ -182,7 +181,7 @@ if (!check_blacklist("global_blacklist")))
182 182
    after the database tables for the global blacklist have been
183 183
    changed.
184 184
 
185
-   Example 1.6. reload_blacklists usage
185
+   Example 1.4. reload_blacklists usage
186 186
 ...
187 187
 kamctl fifo reload_blacklist
188 188
 ...
... ...
@@ -203,7 +202,7 @@ kamctl fifo reload_blacklist
203 203
    complete database documentation on the project webpage,
204 204
    http://www.kamailio.org/docs/db-tables/kamailio-db-devel.html.
205 205
 
206
-   Example 1.7. Example database content - globalblacklist table
206
+   Example 1.5. Example database content - globalblacklist table
207 207
 ...
208 208
 +----+-----------+-----------+
209 209
 | id | prefix    | whitelist |
... ...
@@ -220,7 +219,7 @@ kamctl fifo reload_blacklist
220 220
    "123456" and "123455787" are also blacklisted, because the
221 221
    longest prefix will be matched.
222 222
 
223
-   Example 1.8. Example database content - userblacklist table
223
+   Example 1.6. Example database content - userblacklist table
224 224
 ...
225 225
 +----+----------------+-------------+-----------+-----------+
226 226
 | id | username       | domain      | prefix    | whitelist |
... ...
@@ -240,3 +239,122 @@ kamctl fifo reload_blacklist
240 240
    "1234" will be not allowed, but the number "123456788" is
241 241
    allowed. Additionally a domain could be specified that is used
242 242
    for username matching if the "use_domain" parameter is set.
243
+
244
+Chapter 2. Module parameter for database access.
245
+
246
+2.1. db_url (String)
247
+
248
+   URL to the database containing the data.
249
+
250
+   Default value is
251
+   "mysql://openserro:openserro@localhost/openser".
252
+
253
+   Example 2.1. Set db_url parameter
254
+...
255
+modparam("userblacklist", "db_url", "dbdriver://username:password@dbhost
256
+/dbname")
257
+...
258
+
259
+2.2. userblacklist_table (String)
260
+
261
+   Name of the userblacklist table for the userblacklist module.
262
+
263
+   Default value is "userblacklist".
264
+
265
+   Example 2.2. Set userblacklist_table parameter
266
+...
267
+modparam("userblacklist", "userblacklist_table", "userblacklist")
268
+...
269
+
270
+2.3. userblacklist_id_col (string)
271
+
272
+   unique ID
273
+
274
+   Example 2.3. Set userblacklist_id_col parameter
275
+...
276
+modparam("userblacklist", "userblacklist_id_col", "id")
277
+...
278
+
279
+2.4. userblacklist_username_col (string)
280
+
281
+   The user that is used for the blacklist lookup.
282
+
283
+   Example 2.4. Set userblacklist_username_col parameter
284
+...
285
+modparam("userblacklist", "userblacklist_username_col", "username")
286
+...
287
+
288
+2.5. userblacklist_domain_col (string)
289
+
290
+   The domain that is used for the blacklist lookup.
291
+
292
+   Example 2.5. Set userblacklist_domain_col parameter
293
+...
294
+modparam("userblacklist", "userblacklist_domain_col", "domain")
295
+...
296
+
297
+2.6. userblacklist_prefix_col (string)
298
+
299
+   The prefix that is matched for the blacklist.
300
+
301
+   Example 2.6. Set userblacklist_prefix_col parameter
302
+...
303
+modparam("userblacklist", "userblacklist_prefix_col", "prefix")
304
+...
305
+
306
+2.7. userblacklist_whitelist_col (string)
307
+
308
+   Specify if this a blacklist (0) or a whitelist (1) entry.
309
+
310
+   Example 2.7. Set userblacklist_whitelist_col parameter
311
+...
312
+modparam("userblacklist", "userblacklist_whitelist_col", "whitelist")
313
+...
314
+
315
+2.8. globalblacklist_table (String)
316
+
317
+   Name of the globalblacklist table for the userblacklist module.
318
+
319
+   Default value is "globalblacklist".
320
+
321
+   Example 2.8. Set globalblacklist_table parameter
322
+...
323
+modparam("userblacklist", "globalblacklist_table", "globalblacklist")
324
+...
325
+
326
+2.9. globalblacklist_id_col (string)
327
+
328
+   unique ID
329
+
330
+   Example 2.9. Set globalblacklist_id_col parameter
331
+...
332
+modparam("userblacklist", "globalblacklist_id_col", "id")
333
+...
334
+
335
+2.10. globalblacklist_prefix_col (string)
336
+
337
+   The prefix that is matched for the blacklist.
338
+
339
+   Example 2.10. Set globalblacklist_prefix_col parameter
340
+...
341
+modparam("userblacklist", "globalblacklist_prefix_col", "prefix")
342
+...
343
+
344
+2.11. globalblacklist_whitelist_col (string)
345
+
346
+   Specify if this a blacklist (0) or a whitelist (1) entry.
347
+
348
+   Example 2.11. Set globalblacklist_whitelist_col parameter
349
+...
350
+modparam("userblacklist", "globalblacklist_whitelist_col", "whitelist")
351
+...
352
+
353
+2.12. globalblacklist_description_col (string)
354
+
355
+   A comment for the entry.
356
+
357
+   Example 2.12. Set globalblacklist_description_col parameter
358
+...
359
+modparam("userblacklist", "globalblacklist_description_col", "descriptio
360
+n")
361
+...
... ...
@@ -29,64 +29,21 @@
29 29
 
30 30
 #include "db.h"
31 31
 #include "dt.h"
32
+#include "db_userblacklist.h"
32 33
 
33 34
 #include "../../db/db.h"
34 35
 #include "../../mem/mem.h"
35 36
 #include "../../ut.h"
36 37
 
37 38
 
38
-static db_con_t *dbc;
39
-static db_func_t dbf;
40
-
41
-static str prefix_col = str_init("prefix");
42
-static str whitelist_col = str_init("whitelist");
43
-static str username_key = str_init("username");
44
-static str domain_key = str_init("domain");
45
-
46
-
47
-int db_bind(const str *url)
48
-{
49
-	if (db_bind_mod(url, &dbf) < 0) {
50
-		LM_ERR("can't bind to database module.\n");
51
-		return -1;
52
-	}
53
-
54
-	return 0;
55
-}
56
-
57
-
58
-int db_init(const str *url, const str *table)
59
-{
60
-	dbc = dbf.init(url);
61
-	if (!dbc) {
62
-		LM_ERR("child can't connect to database.\n");
63
-		return -1;
64
-	}
65
-	if(db_check_table_version(&dbf, dbc, table, 1) < 0) {
66
-		LM_ERR("during table version check.\n");
67
-		return -1;
68
-	}
69
-
70
-	return 0;
71
-}
72
-
73
-
74
-void db_destroy(void)
75
-{
76
-	if (dbc) {
77
-		dbf.close(dbc);
78
-	}
79
-}
80
-
81
-
82 39
 /**
83 40
  * Builds a d-tree using database entries.
84 41
  * \return negative on failure, postive on success, indicating the number of d-tree entries
85 42
  */
86 43
 int db_build_userbl_tree(const str *username, const str *domain, const str *table, struct dt_node_t *root, int use_domain)
87 44
 {
88
-	db_key_t columns[2] = { &prefix_col, &whitelist_col };
89
-	db_key_t key[2] = { &username_key, &domain_key };
45
+	db_key_t columns[2] = { &userblacklist_prefix_col, &userblacklist_whitelist_col };
46
+	db_key_t key[2] = { &userblacklist_username_col, &userblacklist_domain_col };
90 47
 
91 48
 	db_val_t val[2];
92 49
 	VAL_TYPE(val) = VAL_TYPE(val + 1) = DB_STR;
... ...
@@ -100,11 +57,11 @@ int db_build_userbl_tree(const str *username, const str *domain, const str *tabl
100 100
 	int i;
101 101
 	int n = 0;
102 102
 	
103
-	if (dbf.use_table(dbc, table) < 0) {
103
+	if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
104 104
 		LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
105 105
 		return -1;
106 106
 	}
107
-	if (dbf.query(dbc, key, 0, val, columns, (!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
107
+	if (userblacklist_dbf.query(userblacklist_dbh, key, 0, val, columns, (!use_domain) ? (1) : (2), 2, 0, &res) < 0) {
108 108
 		LM_ERR("error while executing query.\n");
109 109
 		return -1;
110 110
 	}
... ...
@@ -130,7 +87,7 @@ int db_build_userbl_tree(const str *username, const str *domain, const str *tabl
130 130
 			}
131 131
 		}
132 132
 	}
133
-	dbf.free_result(dbc, res);
133
+	userblacklist_dbf.free_result(userblacklist_dbh, res);
134 134
 
135 135
 	return n;
136 136
 }
... ...
@@ -142,16 +99,16 @@ int db_build_userbl_tree(const str *username, const str *domain, const str *tabl
142 142
  */
143 143
 int db_reload_source(const str *table, struct dt_node_t *root)
144 144
 {
145
-	db_key_t columns[2] = { &prefix_col, &whitelist_col };
145
+	db_key_t columns[2] = { &globalblacklist_prefix_col, &globalblacklist_whitelist_col };
146 146
 	db_res_t *res;
147 147
 	int i;
148 148
 	int n = 0;
149 149
 	
150
-	if (dbf.use_table(dbc, table) < 0) {
150
+	if (userblacklist_dbf.use_table(userblacklist_dbh, table) < 0) {
151 151
 		LM_ERR("cannot use table '%.*s'.\n", table->len, table->s);
152 152
 		return -1;
153 153
 	}
154
-	if (dbf.query(dbc, NULL, NULL, NULL, columns, 0, 2, NULL, &res) < 0) {
154
+	if (userblacklist_dbf.query(userblacklist_dbh, NULL, NULL, NULL, columns, 0, 2, NULL, &res) < 0) {
155 155
 		LM_ERR("error while executing query.\n");
156 156
 		return -1;
157 157
 	}
... ...
@@ -177,7 +134,7 @@ int db_reload_source(const str *table, struct dt_node_t *root)
177 177
 			}
178 178
 		}
179 179
 	}
180
-	dbf.free_result(dbc, res);
180
+	userblacklist_dbf.free_result(userblacklist_dbh, res);
181 181
 
182 182
 	return n;
183 183
 }
... ...
@@ -33,10 +33,6 @@
33 33
 #include "../../sr_module.h"
34 34
 #include "dt.h"
35 35
 
36
-int db_bind(const str *url);
37
-int db_init(const str *url, const str *table);
38
-void db_destroy(void);
39
-
40 36
 int db_build_userbl_tree(const str *user, const str *domain, const str *table, struct dt_node_t *root, int use_domain);
41 37
 int db_reload_source(const str *table, struct dt_node_t *root);
42 38
 
43 39
new file mode 100644
... ...
@@ -0,0 +1,131 @@
0
+
1
+/*!
2
+ * \file
3
+ * \ingroup db
4
+ * \brief Database support for modules.
5
+ *
6
+ * Database support functions for modules.
7
+ *
8
+ * @cond
9
+ * WARNING:
10
+ * This file was autogenerated from the XML source file
11
+ * ../../modules/userblacklist/kamailio-userblacklist.xml.
12
+ * It can be regenerated by running 'make dbschema' in the top level
13
+ * directory of the source code. You need to have xsltproc and
14
+ * docbook-xsl stylesheets installed.
15
+ * ALL CHANGES DONE HERE WILL BE LOST IF THE FILE IS REGENERATED
16
+ * @endcond
17
+ */
18
+
19
+#include "db_userblacklist.h"
20
+
21
+/* database variables */
22
+/* TODO assign read-write or read-only URI, introduce a parameter in XML */
23
+
24
+//extern str userblacklist_db_url;
25
+db_con_t * userblacklist_dbh = NULL;
26
+
27
+str userblacklist_table = str_init("userblacklist");
28
+
29
+/* column names */
30
+str userblacklist_id_col = str_init("id");
31
+str userblacklist_username_col = str_init("username");
32
+str userblacklist_domain_col = str_init("domain");
33
+str userblacklist_prefix_col = str_init("prefix");
34
+str userblacklist_whitelist_col = str_init("whitelist");
35
+
36
+/* table version */
37
+const unsigned int userblacklist_version = 1;
38
+
39
+str globalblacklist_table = str_init("globalblacklist");
40
+
41
+/* column names */
42
+str globalblacklist_id_col = str_init("id");
43
+str globalblacklist_prefix_col = str_init("prefix");
44
+str globalblacklist_whitelist_col = str_init("whitelist");
45
+str globalblacklist_description_col = str_init("description");
46
+
47
+/* table version */
48
+const unsigned int globalblacklist_version = 1;
49
+
50
+
51
+/*
52
+ * Closes the DB connection.
53
+ */
54
+void userblacklist_db_close(void) {
55
+	if (userblacklist_dbh) {
56
+		userblacklist_dbf.close(userblacklist_dbh);
57
+		userblacklist_dbh = NULL;
58
+	}
59
+}
60
+
61
+
62
+/*!
63
+ * Initialises the DB API, check the table version and closes the connection.
64
+ * This should be called from the mod_init function.
65
+ *
66
+ * \return 0 means ok, -1 means an error occured.
67
+ */
68
+int userblacklist_db_init(void) {
69
+	if (!userblacklist_db_url.s || !userblacklist_db_url.len) {
70
+		LM_ERR("you have to set the db_url module parameter.\n");
71
+		return -1;
72
+	}
73
+	if (db_bind_mod(&userblacklist_db_url, &userblacklist_dbf) < 0) {
74
+		LM_ERR("can't bind database module.\n");
75
+		return -1;
76
+	}
77
+	if ((userblacklist_dbh = userblacklist_dbf.init(&userblacklist_db_url)) == NULL) {
78
+		LM_ERR("can't connect to database.\n");
79
+		return -1;
80
+	}
81
+	if (
82
+	(db_check_table_version(&userblacklist_dbf, userblacklist_dbh, &userblacklist_table, userblacklist_version) < 0) ||
83
+	(db_check_table_version(&userblacklist_dbf, userblacklist_dbh, &globalblacklist_table, globalblacklist_version) < 0)
84
+	) {
85
+		LM_ERR("during table version check.\n");
86
+		userblacklist_db_close();
87
+		return -1;
88
+	}
89
+	userblacklist_db_close();
90
+	return 0;
91
+}
92
+
93
+
94
+/*!
95
+ * Initialize the DB connection without checking the table version and DB URL.
96
+ * This should be called from child_init. An already existing database
97
+ * connection will be closed, and a new one created.
98
+ *
99
+ * \return 0 means ok, -1 means an error occured.
100
+ */
101
+int userblacklist_db_open(void) {
102
+	if (userblacklist_dbh) {
103
+		userblacklist_dbf.close(userblacklist_dbh);
104
+	}
105
+	if ((userblacklist_dbh = userblacklist_dbf.init(&userblacklist_db_url)) == NULL) {
106
+		LM_ERR("can't connect to database.\n");
107
+		return -1;
108
+	}
109
+	return 0;
110
+}
111
+
112
+
113
+/*!
114
+ * Update the variable length after eventual assignments from the config script.
115
+ * This is necessary because we're using the 'str' type.
116
+ */
117
+void userblacklist_db_vars(void) {
118
+	userblacklist_table.len = strlen(userblacklist_table.s);
119
+	userblacklist_id_col.len = strlen(userblacklist_id_col.s);
120
+	userblacklist_username_col.len = strlen(userblacklist_username_col.s);
121
+	userblacklist_domain_col.len = strlen(userblacklist_domain_col.s);
122
+	userblacklist_prefix_col.len = strlen(userblacklist_prefix_col.s);
123
+	userblacklist_whitelist_col.len = strlen(userblacklist_whitelist_col.s);
124
+	globalblacklist_table.len = strlen(globalblacklist_table.s);
125
+	globalblacklist_id_col.len = strlen(globalblacklist_id_col.s);
126
+	globalblacklist_prefix_col.len = strlen(globalblacklist_prefix_col.s);
127
+	globalblacklist_whitelist_col.len = strlen(globalblacklist_whitelist_col.s);
128
+	globalblacklist_description_col.len = strlen(globalblacklist_description_col.s);
129
+}
130
+
0 131
new file mode 100644
... ...
@@ -0,0 +1,107 @@
0
+
1
+/*!
2
+ * \file
3
+ * \ingroup db
4
+ * \brief Database support for modules.
5
+ *
6
+ * Database support functions for modules.
7
+ *
8
+ * @cond
9
+ * WARNING:
10
+ * This file was autogenerated from the XML source file
11
+ * ../../modules/userblacklist/kamailio-userblacklist.xml.
12
+ * It can be regenerated by running 'make dbschema' in the top level
13
+ * directory of the source code. You need to have xsltproc and
14
+ * docbook-xsl stylesheets installed.
15
+ * ALL CHANGES DONE HERE WILL BE LOST IF THE FILE IS REGENERATED
16
+ * @endcond
17
+ */
18
+
19
+#ifndef db_userblacklist_h
20
+#define db_userblacklist_h
21
+
22
+
23
+/* necessary includes */
24
+#include "../../db/db.h"
25
+#include "../../str.h"
26
+#include "../../ut.h"
27
+
28
+#include <string.h>
29
+
30
+
31
+/* database variables */
32
+
33
+extern str userblacklist_db_url;
34
+extern db_con_t * userblacklist_dbh;
35
+db_func_t userblacklist_dbf;
36
+
37
+#define userblacklist_DB_URL { "db_url", STR_PARAM, &userblacklist_db_url.s },
38
+
39
+#define userblacklist_DB_TABLE { "userblacklist_table", STR_PARAM, &userblacklist_table.s },
40
+
41
+extern str userblacklist_table;
42
+
43
+/* column names */
44
+extern str userblacklist_id_col;
45
+extern str userblacklist_username_col;
46
+extern str userblacklist_domain_col;
47
+extern str userblacklist_prefix_col;
48
+extern str userblacklist_whitelist_col;
49
+#define userblacklist_DB_COLS \
50
+{ "userblacklist_id_col", STR_PARAM, &userblacklist_id_col.s }, \
51
+{ "userblacklist_username_col", STR_PARAM, &userblacklist_username_col.s }, \
52
+{ "userblacklist_domain_col", STR_PARAM, &userblacklist_domain_col.s }, \
53
+{ "userblacklist_prefix_col", STR_PARAM, &userblacklist_prefix_col.s }, \
54
+{ "userblacklist_whitelist_col", STR_PARAM, &userblacklist_whitelist_col.s }, \
55
+
56
+/* table version */
57
+extern const unsigned int userblacklist_version;
58
+
59
+#define globalblacklist_DB_TABLE { "globalblacklist_table", STR_PARAM, &userblacklist_table.s },
60
+
61
+extern str globalblacklist_table;
62
+
63
+/* column names */
64
+extern str globalblacklist_id_col;
65
+extern str globalblacklist_prefix_col;
66
+extern str globalblacklist_whitelist_col;
67
+extern str globalblacklist_description_col;
68
+#define globalblacklist_DB_COLS \
69
+{ "globalblacklist_id_col", STR_PARAM, &globalblacklist_id_col.s }, \
70
+{ "globalblacklist_prefix_col", STR_PARAM, &globalblacklist_prefix_col.s }, \
71
+{ "globalblacklist_whitelist_col", STR_PARAM, &globalblacklist_whitelist_col.s }, \
72
+{ "globalblacklist_description_col", STR_PARAM, &globalblacklist_description_col.s }, \
73
+
74
+/* table version */
75
+extern const unsigned int globalblacklist_version;
76
+
77
+
78
+/*
79
+ * Closes the DB connection.
80
+ */
81
+void userblacklist_db_close(void);
82
+
83
+/*!
84
+ * Initialises the DB API, check the table version and closes the connection.
85
+ * This should be called from the mod_init function.
86
+ *
87
+ * \return 0 means ok, -1 means an error occured.
88
+ */
89
+int userblacklist_db_init(void);
90
+
91
+/*!
92
+ * Initialize the DB connection without checking the table version and DB URL.
93
+ * This should be called from child_init. An already existing database
94
+ * connection will be closed, and a new one created.
95
+ *
96
+ * \return 0 means ok, -1 means an error occured.
97
+ */
98
+int userblacklist_db_open(void);
99
+
100
+/*!
101
+ * Update the variable length after eventual assignments from the config script.
102
+ * This is necessary because we're using the 'str' type.
103
+ */
104
+void userblacklist_db_vars(void);
105
+
106
+#endif
... ...
@@ -3,6 +3,7 @@
3 3
 "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
4 4
 
5 5
 <!ENTITY admin SYSTEM "userblacklist_admin.xml">
6
+<!ENTITY db    SYSTEM "userblacklist_db.xml">
6 7
 <!ENTITY faq SYSTEM "../../../doc/module_faq.xml">
7 8
 
8 9
 <!-- Include general documentation entities -->
... ...
@@ -40,5 +41,6 @@
40 40
 	</bookinfo>
41 41
 	<toc></toc>
42 42
 	&admin;
43
+	&db;
43 44
 	&faq;
44 45
 </book>
... ...
@@ -60,46 +60,6 @@
60 60
 	<section>
61 61
 	<title>Exported Parameters</title>
62 62
     <section>
63
-	    <title><varname>db_url</varname> (string)</title>
64
-	    <para>
65
-		Url to the database containing the routing data.
66
-	    </para>
67
-	    <para>
68
-		<emphasis>
69
-		Default value is <quote>&defaultrodb;</quote>.
70
-		</emphasis>
71
-	    </para>
72
-	    <example>
73
-		<title>Set <varname>db_url</varname> parameter</title>
74
-		<programlisting format="linespecific">
75
-...
76
-modparam("userblacklist", "db_url", "&exampledb;")
77
-...
78
-		</programlisting>
79
-	    </example>
80
-    </section>
81
-
82
-    <section>
83
-	    <title><varname>db_table</varname> (string)</title>
84
-	    <para>
85
-		Name of the table where the user blacklist data is stored.
86
-	    </para>
87
-	    <para>
88
-		    <emphasis>
89
-			    Default value is <quote>userblacklist</quote>.
90
-		    </emphasis>
91
-	    </para>
92
-	    <example>
93
-		    <title>Set <varname>db_table</varname> parameter</title>
94
-		    <programlisting format="linespecific">
95
-...
96
-modparam("userblacklist", "db_table", "userblacklist")
97
-...
98
-		    </programlisting>
99
-	    </example>
100
-    </section>
101
-	
102
-    <section>
103 63
 	    <title><varname>use_domain</varname> (integer)</title>
104 64
 	    <para>
105 65
 			If set to non-zero value, the domain column in the userblacklist is used.
106 66
new file mode 100644
... ...
@@ -0,0 +1,166 @@
0
+<!--
1
+WARNING:
2
+This file was autogenerated from the XML source file
3
+../../modules/userblacklist/doc/kamailio-userblacklist.xml.
4
+It can be regenerated by running 'make dbschema' in the top level
5
+directory of the source code. You need to have xsltproc and
6
+docbook-xsl stylesheets installed.
7
+ALL CHANGES DONE HERE WILL BE LOST IF THE FILE IS REGENERATED
8
+-->
9
+
10
+<chapter>
11
+  <title>Module parameter for database access.</title>
12
+  <section>
13
+    <title><varname>db_url</varname> (String)</title>
14
+    <para>URL to the database containing the data.</para>
15
+    <para>
16
+      <emphasis>Default value is <quote>mysql://openserro:openserro@localhost/openser</quote>.</emphasis>
17
+    </para>
18
+    <example>
19
+      <title>Set <varname>db_url</varname> parameter</title>
20
+      <programlisting format="linespecific">
21
+...
22
+modparam("userblacklist", "db_url", "dbdriver://username:password@dbhost/dbname")
23
+...
24
+</programlisting>
25
+    </example>
26
+  </section>
27
+  <section>
28
+    <title><varname>userblacklist_table</varname> (String)</title>
29
+    <para>Name of the userblacklist table for the userblacklist module.</para>
30
+    <para>
31
+      <emphasis>Default value is <quote>userblacklist</quote>.</emphasis>
32
+    </para>
33
+    <example>
34
+      <title>Set <varname>userblacklist_table</varname> parameter</title>
35
+      <programlisting format="linespecific">
36
+...
37
+modparam("userblacklist", "userblacklist_table", "userblacklist")
38
+...
39
+</programlisting>
40
+    </example>
41
+  </section>
42
+  <section>
43
+    <title><varname>userblacklist_id_col</varname> (string)</title>
44
+    <para>unique ID</para>
45
+    <example>
46
+      <title>Set <varname>userblacklist_id_col</varname> parameter</title>
47
+      <programlisting format="linespecific">
48
+...
49
+modparam("userblacklist", "userblacklist_id_col", "id")
50
+...
51
+</programlisting>
52
+    </example>
53
+  </section>
54
+  <section>
55
+    <title><varname>userblacklist_username_col</varname> (string)</title>
56
+    <para>The user that is used for the blacklist lookup.</para>
57
+    <example>
58
+      <title>Set <varname>userblacklist_username_col</varname> parameter</title>
59
+      <programlisting format="linespecific">
60
+...
61
+modparam("userblacklist", "userblacklist_username_col", "username")
62
+...
63
+</programlisting>
64
+    </example>
65
+  </section>
66
+  <section>
67
+    <title><varname>userblacklist_domain_col</varname> (string)</title>
68
+    <para>The domain that is used for the blacklist lookup.</para>
69
+    <example>
70
+      <title>Set <varname>userblacklist_domain_col</varname> parameter</title>
71
+      <programlisting format="linespecific">
72
+...
73
+modparam("userblacklist", "userblacklist_domain_col", "domain")
74
+...
75
+</programlisting>
76
+    </example>
77
+  </section>
78
+  <section>
79
+    <title><varname>userblacklist_prefix_col</varname> (string)</title>
80
+    <para>The prefix that is matched for the blacklist.</para>
81
+    <example>
82
+      <title>Set <varname>userblacklist_prefix_col</varname> parameter</title>
83
+      <programlisting format="linespecific">
84
+...
85
+modparam("userblacklist", "userblacklist_prefix_col", "prefix")
86
+...
87
+</programlisting>
88
+    </example>
89
+  </section>
90
+  <section>
91
+    <title><varname>userblacklist_whitelist_col</varname> (string)</title>
92
+    <para>Specify if this a blacklist (0) or a whitelist (1) entry.</para>
93
+    <example>
94
+      <title>Set <varname>userblacklist_whitelist_col</varname> parameter</title>
95
+      <programlisting format="linespecific">
96
+...
97
+modparam("userblacklist", "userblacklist_whitelist_col", "whitelist")
98
+...
99
+</programlisting>
100
+    </example>
101
+  </section>
102
+  <section>
103
+    <title><varname>globalblacklist_table</varname> (String)</title>
104
+    <para>Name of the globalblacklist table for the userblacklist module.</para>
105
+    <para>
106
+      <emphasis>Default value is <quote>globalblacklist</quote>.</emphasis>
107
+    </para>
108
+    <example>
109
+      <title>Set <varname>globalblacklist_table</varname> parameter</title>
110
+      <programlisting format="linespecific">
111
+...
112
+modparam("userblacklist", "globalblacklist_table", "globalblacklist")
113
+...
114
+</programlisting>
115
+    </example>
116
+  </section>
117
+  <section>
118
+    <title><varname>globalblacklist_id_col</varname> (string)</title>
119
+    <para>unique ID</para>
120
+    <example>
121
+      <title>Set <varname>globalblacklist_id_col</varname> parameter</title>
122
+      <programlisting format="linespecific">
123
+...
124
+modparam("userblacklist", "globalblacklist_id_col", "id")
125
+...
126
+</programlisting>
127
+    </example>
128
+  </section>
129
+  <section>
130
+    <title><varname>globalblacklist_prefix_col</varname> (string)</title>
131
+    <para>The prefix that is matched for the blacklist.</para>
132
+    <example>
133
+      <title>Set <varname>globalblacklist_prefix_col</varname> parameter</title>
134
+      <programlisting format="linespecific">
135
+...
136
+modparam("userblacklist", "globalblacklist_prefix_col", "prefix")
137
+...
138
+</programlisting>
139
+    </example>
140
+  </section>
141
+  <section>
142
+    <title><varname>globalblacklist_whitelist_col</varname> (string)</title>
143
+    <para>Specify if this a blacklist (0) or a whitelist (1) entry.</para>
144
+    <example>
145
+      <title>Set <varname>globalblacklist_whitelist_col</varname> parameter</title>
146
+      <programlisting format="linespecific">
147
+...
148
+modparam("userblacklist", "globalblacklist_whitelist_col", "whitelist")
149
+...
150
+</programlisting>
151
+    </example>
152
+  </section>
153
+  <section>
154
+    <title><varname>globalblacklist_description_col</varname> (string)</title>
155
+    <para>A comment for the entry.</para>
156
+    <example>
157
+      <title>Set <varname>globalblacklist_description_col</varname> parameter</title>
158
+      <programlisting format="linespecific">
159
+...
160
+modparam("userblacklist", "globalblacklist_description_col", "description")
161
+...
162
+</programlisting>
163
+    </example>
164
+  </section>
165
+</chapter>
... ...
@@ -53,6 +53,7 @@
53 53
 
54 54
 #include "dt.h"
55 55
 #include "db.h"
56
+#include "db_userblacklist.h"
56 57
 
57 58
 MODULE_VERSION
58 59
 
... ...
@@ -72,8 +73,7 @@ struct check_blacklist_fs_t {
72 72
 };
73 73
 
74 74
 
75
-static str db_url       = str_init(DEFAULT_RODB_URL);
76
-static str db_table     = str_init("userblacklist");
75
+str userblacklist_db_url = str_init(DEFAULT_RODB_URL);
77 76
 static int use_domain   = 0;
78 77
 
79 78
 /* ---- fixup functions: */
... ...
@@ -104,8 +104,11 @@ static cmd_export_t cmds[]={
104 104
 
105 105
 
106 106
 static param_export_t params[] = {
107
-	{ "db_url",          STR_PARAM, &db_url.s },
108
-	{ "db_table",        STR_PARAM, &db_table.s },
107
+	userblacklist_DB_URL
108
+	userblacklist_DB_TABLE
109
+	globalblacklist_DB_TABLE
110
+	userblacklist_DB_COLS
111
+	globalblacklist_DB_COLS
109 112
 	{ "use_domain",      INT_PARAM, &use_domain },
110 113
 	{ 0, 0, 0}
111 114
 };
... ...
@@ -237,8 +240,8 @@ static int check_user_blacklist(struct sip_msg *msg, char* str1, char* str2, cha
237 237
 		table.len=strlen(str4);
238 238
 	} else {
239 239
 		/* use default table name */
240
-		table.len=db_table.len;
241
-		table.s=db_table.s;
240
+		table.len=userblacklist_table.len;
241
+		table.s=userblacklist_table.s;
242 242
 	}
243 243
 
244 244
 	if (msg->first_line.type != SIP_REQUEST) {
... ...
@@ -535,10 +538,9 @@ struct mi_root * mi_reload_blacklist(struct mi_root* cmd, void* param)
535 535
 
536 536
 static int mod_init(void)
537 537
 {
538
-	db_url.len = strlen(db_url.s);
539
-	db_table.len = strlen(db_table.s);
538
+	userblacklist_db_vars();
540 539
 
541
-	if (db_bind(&db_url) != 0) return -1;
540
+	if (userblacklist_db_init() != 0) return -1;
542 541
 	if (init_shmlock() != 0) return -1;
543 542
 	if (init_source_list() != 0) return -1;
544 543
 	return 0;
... ...
@@ -547,7 +549,7 @@ static int mod_init(void)
547 547
 
548 548
 static int child_init(int rank)
549 549
 {
550
-	if (db_init(&db_url, &db_table) != 0) return -1;
550
+	if (userblacklist_db_open() != 0) return -1;
551 551
 	if (dt_init(&dt_root) != 0) return -1;
552 552
 	/* because we've added new sources during the fixup */
553 553
 	if (reload_sources() != 0) return -1;
... ...
@@ -558,7 +560,7 @@ static int child_init(int rank)
558 558
 
559 559
 static int mi_child_init(void)
560 560
 {
561
-	if (db_init(&db_url, &db_table) != 0) return -1;
561
+	if (userblacklist_db_open() != 0) return -1;
562 562
 	if (dt_init(&dt_root) != 0) return -1;
563 563
 	/* because we've added new sources during the fixup */
564 564
 	if (reload_sources() != 0) return -1;
... ...
@@ -571,6 +573,6 @@ static void mod_destroy(void)
571 571
 {
572 572
 	destroy_source_list();
573 573
 	destroy_shmlock();
574
-	db_destroy();
574
+	userblacklist_db_close();
575 575
 	dt_destroy(&dt_root);
576 576
 }