Browse code

modules_k/userblacklist : check_blacklist function uses globalblacklist_table as data source if no parameter is given.

Globalblacklist_table parameter was not used at all in the userblacklist module, the check_blacklist funtion always needing
one parameters and the db table. This is not mandatory anymore, as the check_blacklist function with no parameters will use
the globalblacklist_table

Marius Zbihlei authored on 05/03/2010 14:32:49
Showing 4 changed files
... ...
@@ -11,7 +11,7 @@ Henning Westerholt
11 11
    1&1 Internet AG
12 12
    <henning.westerholt@1und1.de>
13 13
 
14
-   Copyright � 2008 1&1 Internet AG
14
+   Copyright © 2008 1&1 Internet AG
15 15
    Revision History
16 16
    Revision $Revision$ $Date$
17 17
      __________________________________________________________________
... ...
@@ -38,7 +38,7 @@ Henning Westerholt
38 38
               4.2. check_user_whitelist (string user, string domain,
39 39
                       string number, string table)
40 40
 
41
-              4.3. check_blacklist (string table)
41
+              4.3. check_blacklist ([string table])
42 42
 
43 43
         5. MI Commands
44 44
 
... ...
@@ -107,7 +107,7 @@ Chapter 1. Admin Guide
107 107
         4.2. check_user_whitelist (string user, string domain, string
108 108
                 number, string table)
109 109
 
110
-        4.3. check_blacklist (string table)
110
+        4.3. check_blacklist ([string table])
111 111
 
112 112
    5. MI Commands
113 113
 
... ...
@@ -170,7 +170,7 @@ Chapter 1. Admin Guide
170 170
    If set to non-zero value, the domain column in the userblacklist is
171 171
    used.
172 172
 
173
-   Default value is "0".
173
+   Default value is “0”.
174 174
 
175 175
    Example 1.1. Set use_domain parameter
176 176
 ...
... ...
@@ -185,7 +185,7 @@ modparam("userblacklist", "use_domain", 0)
185 185
    4.2. check_user_whitelist (string user, string domain, string number,
186 186
           string table)
187 187
 
188
-   4.3. check_blacklist (string table)
188
+   4.3. check_blacklist ([string table])
189 189
 
190 190
 4.1.  check_user_blacklist (string user, string domain, string number, string
191 191
 table)
... ...
@@ -229,11 +229,12 @@ if (!check_user_whitelist("$avp(i:80)", "$avp(i:82)"))
229 229
 }
230 230
 ...
231 231
 
232
-4.3.  check_blacklist (string table)
232
+4.3.  check_blacklist ([string table])
233 233
 
234 234
    Finds the longest prefix that matches the request URI for the given
235 235
    table. If a match is found and it is not set to whitelist, false is
236
-   returned. Otherwise, true is returned.
236
+   returned. Otherwise, true is returned. If no table is given, then
237
+   globalblacklist_table is used.
237 238
 
238 239
    Example 1.4. check_blacklist usage
239 240
 ...
... ...
@@ -287,8 +288,8 @@ kamctl fifo reload_blacklist
287 288
 ...
288 289
 
289 290
    This table will setup a global blacklist for all numbers, only allowing
290
-   calls starting with "1". Numbers that starting with "123456" and
291
-   "123455787" are also blacklisted, because the longest prefix will be
291
+   calls starting with “1”. Numbers that starting with “123456” and
292
+   “123455787” are also blacklisted, because the longest prefix will be
292 293
    matched.
293 294
 
294 295
    Example 1.7. Example database content - userblacklist table
... ...
@@ -307,10 +308,10 @@ kamctl fifo reload_blacklist
307 308
 ...
308 309
 
309 310
    This table will setup user specific blacklists for certain usernames.
310
-   For example for user "49721123456788" the prefix "1234" will be not
311
-   allowed, but the number "123456788" is allowed. Additionally a domain
311
+   For example for user “49721123456788” the prefix “1234” will be not
312
+   allowed, but the number “123456788” is allowed. Additionally a domain
312 313
    could be specified that is used for username matching if the
313
-   "use_domain" parameter is set.
314
+   “use_domain” parameter is set.
314 315
 
315 316
 Chapter 2. Module parameter for database access.
316 317
 
... ...
@@ -333,7 +334,7 @@ Chapter 2. Module parameter for database access.
333 334
 
334 335
    URL to the database containing the data.
335 336
 
336
-   Default value is "mysql://openserro:openserro@localhost/openser".
337
+   Default value is “mysql://openserro:openserro@localhost/openser”.
337 338
 
338 339
    Example 2.1. Set db_url parameter
339 340
 ...
... ...
@@ -345,7 +346,7 @@ modparam("userblacklist", "db_url", "dbdriver://username:password@dbhost/dbname"
345 346
 
346 347
    Name of the userblacklist table for the userblacklist module.
347 348
 
348
-   Default value is "userblacklist".
349
+   Default value is “userblacklist”.
349 350
 
350 351
    Example 2.2. Set userblacklist_table parameter
351 352
 ...
... ...
@@ -400,10 +401,10 @@ modparam("userblacklist", "userblacklist_whitelist_col", "whitelist")
400 401
 8. globalblacklist_table (String)
401 402
 
402 403
    Name of the globalblacklist table for the userblacklist module. Please
403
-   not that this table is currently ignored, the table needs to be given
404
-   as a parameter for the check_blacklist function.
404
+   note that this table is used when the check_blacklist function is
405
+   called with no parameters.
405 406
 
406
-   Default value is "globalblacklist".
407
+   Default value is “globalblacklist”.
407 408
 
408 409
    Example 2.8. Set globalblacklist_table parameter
409 410
 ...
... ...
@@ -152,12 +152,13 @@ if (!check_user_whitelist("$avp(i:80)", "$avp(i:82)"))
152 152
 	</section>
153 153
 	<section>
154 154
 	    <title>
155
-		<function moreinfo="none">check_blacklist (string table)</function>
155
+		<function moreinfo="none">check_blacklist ([string table])</function>
156 156
 	    </title>
157 157
 	    <para>
158 158
 		Finds the longest prefix that matches the request URI for the
159 159
 		given table. If a match is found and it is not set to whitelist,
160
-		false is returned. Otherwise, true is returned.
160
+		false is returned. Otherwise, true is returned. If no table is given, 
161
+		then globalblacklist_table is used.
161 162
 	    </para>
162 163
 	<example>
163 164
 		<title><function>check_blacklist</function> usage</title>
... ...
@@ -112,8 +112,7 @@ modparam("userblacklist", "userblacklist_whitelist_col", "whitelist")
112 112
   <section>
113 113
     <title><varname>globalblacklist_table</varname> (String)</title>
114 114
     <para>Name of the globalblacklist table for the userblacklist module.
115
-    Please not that this table is currently ignored, the table needs to be
116
-    given as a parameter for the check_blacklist function.</para>
115
+    Please note that this table is used when the check_blacklist function is called with no parameters.</para>
117 116
     <para>
118 117
       <emphasis>Default value is <quote>globalblacklist</quote>.</emphasis>
119 118
     </para>
... ...
@@ -74,10 +74,12 @@ struct check_blacklist_fs_t {
74 74
 
75 75
 str userblacklist_db_url = str_init(DEFAULT_RODB_URL);
76 76
 static int use_domain   = 0;
77
+static struct dtrie_node_t *gnode = NULL;
77 78
 
78 79
 /* ---- fixup functions: */
79 80
 static int check_blacklist_fixup(void** param, int param_no);
80 81
 static int check_user_blacklist_fixup(void** param, int param_no);
82
+static int check_globalblacklist_fixup(void** param, int param_no);
81 83
 
82 84
 /* ---- exported commands: */
83 85
 static int check_user_blacklist(struct sip_msg *msg, char* str1, char* str2, char* str3, char* str4);
... ...
@@ -87,6 +89,8 @@ static int check_user_whitelist2(struct sip_msg *msg, char* str1, char* str2);
87 89
 static int check_user_blacklist3(struct sip_msg *msg, char* str1, char* str2, char* str3);
88 90
 static int check_user_whitelist3(struct sip_msg *msg, char* str1, char* str2, char* str3);
89 91
 static int check_blacklist(struct sip_msg *msg, struct check_blacklist_fs_t *arg1);
92
+static int check_globalblacklist(struct sip_msg *msg);
93
+
90 94
 
91 95
 /* ---- module init functions: */
92 96
 static int mod_init(void);
... ...
@@ -106,6 +110,7 @@ static cmd_export_t cmds[]={
106 110
 	{ "check_user_blacklist", (cmd_function)check_user_blacklist, 4, check_user_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
107 111
 	{ "check_user_whitelist", (cmd_function)check_user_whitelist, 4, check_user_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
108 112
 	{ "check_blacklist", (cmd_function)check_blacklist, 1, check_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
113
+	{ "check_blacklist", (cmd_function)check_globalblacklist, 0, check_globalblacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
109 114
 	{ 0, 0, 0, 0, 0, 0}
110 115
 };
111 116
 
... ...
@@ -401,6 +406,48 @@ static int add_source(const char *table)
401 406
 }
402 407
 
403 408
 
409
+static int check_globalblacklist_fixup(void** param, int param_no)
410
+{
411
+	char * table = globalblacklist_table.s;
412
+	if(param_no > 0){
413
+		LM_ERR("Wrong number of parameters\n");
414
+		return -1;
415
+	}
416
+
417
+	if (!table) {
418
+		LM_ERR("no table name\n");
419
+		return -1;
420
+	}
421
+	/* try to add the table */
422
+	if (add_source(table) != 0) {
423
+		LM_ERR("could not add table");
424
+		return -1;
425
+	}
426
+
427
+	gnode = table2dt(table);
428
+	if (!gnode) {
429
+		LM_ERR("invalid table '%s'\n", table);
430
+		return -1;
431
+	}
432
+
433
+	return 0;
434
+}
435
+
436
+static int check_globalblacklist(struct sip_msg* msg)
437
+{
438
+	static struct check_blacklist_fs_t* arg = NULL;
439
+	if(!arg){
440
+		arg = pkg_malloc(sizeof(struct check_blacklist_fs_t));
441
+		if (!arg) {
442
+			PKG_MEM_ERROR;
443
+			return -1;
444
+		}
445
+		memset(arg, 0, sizeof(struct check_blacklist_fs_t));
446
+		arg->dtrie_root = gnode;
447
+	}
448
+	return check_blacklist(msg, arg);
449
+}
450
+
404 451
 static int check_blacklist_fixup(void **arg, int arg_no)
405 452
 {
406 453
 	char *table = (char *)(*arg);
... ...
@@ -454,11 +501,6 @@ static int check_blacklist(struct sip_msg *msg, struct check_blacklist_fs_t *arg
454 501
 		return -1;
455 502
 	}
456 503
 
457
-	if (parse_sip_msg_uri(msg) < 0) {
458
-		LM_ERR("cannot parse msg URI\n");
459
-		return -1;
460
-	}
461
-
462 504
 	if ((parse_sip_msg_uri(msg) < 0) || (!msg->parsed_uri.user.s) || (msg->parsed_uri.user.len > MAXNUMBERLEN)) {
463 505
 		LM_ERR("cannot parse msg URI\n");
464 506
 		return -1;