Jelajahi Sumber

modules_k/userblacklist : extended userblacklist module so also alphanumerical matches are possible

Also updated doc and test.
Marius Zbihlei 15 tahun lalu
induk
melakukan
2f8f8e58a9

+ 31 - 11
modules_k/userblacklist/README

@@ -29,6 +29,7 @@ Henning Westerholt
         3. Exported Parameters
 
               3.1. use_domain (integer)
+              3.2. match_mode (integer)
 
         4. Exported Functions
 
@@ -66,12 +67,13 @@ Henning Westerholt
    List of Examples
 
    1.1. Set use_domain parameter
-   1.2. check_user_blacklist usage
+   1.2. Set match_mode parameter
    1.3. check_user_blacklist usage
-   1.4. check_blacklist usage
-   1.5. reload_blacklists usage
-   1.6. Example database content - globalblacklist table
-   1.7. Example database content - userblacklist table
+   1.4. check_user_blacklist usage
+   1.5. check_blacklist usage
+   1.6. reload_blacklists usage
+   1.7. Example database content - globalblacklist table
+   1.8. Example database content - userblacklist table
    2.1. Set db_url parameter
    2.2. Set userblacklist_table parameter
    2.3. Set userblacklist_id_col parameter
@@ -98,6 +100,7 @@ Chapter 1. Admin Guide
    3. Exported Parameters
 
         3.1. use_domain (integer)
+        3.2. match_mode (integer)
 
    4. Exported Functions
 
@@ -164,6 +167,7 @@ Chapter 1. Admin Guide
 3. Exported Parameters
 
    3.1. use_domain (integer)
+   3.2. match_mode (integer)
 
 3.1. use_domain (integer)
 
@@ -177,6 +181,22 @@ Chapter 1. Admin Guide
 modparam("userblacklist", "use_domain", 0)
 ...
 
+3.2. match_mode (integer)
+
+   The number of individual characters that are used for matching. Valid
+   values are 10 or 128. When you specifiy 10, only digits will be used
+   for matching, this operation mode is equivalent to the old behaviour.
+   When configured with 128, all standard ascii chars are available for
+   matching. Please be aware that memory requirements for storing the
+   routing tree in shared memory will also increase by a factor of 12.8.
+
+   Default value is “10”.
+
+   Example 1.2. Set match_mode parameter
+...
+modparam("userblacklist", "match_mode", 128)
+...
+
 4. Exported Functions
 
    4.1. check_user_blacklist (string user, string domain, string number,
@@ -198,7 +218,7 @@ table)
    are optional, the defaults are used if they are ommited. The number
    parameter can be used to check for example against the from URI user.
 
-   Example 1.2. check_user_blacklist usage
+   Example 1.3. check_user_blacklist usage
 ...
 $avp(i:80) = $rU;
 # rewrite the R-URI
@@ -219,7 +239,7 @@ table)
    are optional, the defaults are used if they are ommited. The number
    parameter can be used to check for example against the from URI user.
 
-   Example 1.3. check_user_blacklist usage
+   Example 1.4. check_user_blacklist usage
 ...
 $avp(i:80) = $rU;
 # rewrite the R-URI
@@ -236,7 +256,7 @@ if (!check_user_whitelist("$avp(i:80)", "$avp(i:82)"))
    returned. Otherwise, true is returned. If no table is given, then
    globalblacklist_table is used.
 
-   Example 1.4. check_blacklist usage
+   Example 1.5. check_blacklist usage
 ...
 if (!check_blacklist("global_blacklist")))
         sl_send_reply("403", "Forbidden");
@@ -253,7 +273,7 @@ if (!check_blacklist("global_blacklist")))
    Reload the internal global blacklist cache. This is necessary after the
    database tables for the global blacklist have been changed.
 
-   Example 1.5. reload_blacklists usage
+   Example 1.6. reload_blacklists usage
 ...
 kamctl fifo reload_blacklist
 ...
@@ -275,7 +295,7 @@ kamctl fifo reload_blacklist
    complete database documentation on the project webpage,
    http://www.kamailio.org/docs/db-tables/kamailio-db-devel.html.
 
-   Example 1.6. Example database content - globalblacklist table
+   Example 1.7. Example database content - globalblacklist table
 ...
 +----+-----------+-----------+
 | id | prefix    | whitelist |
@@ -292,7 +312,7 @@ kamctl fifo reload_blacklist
    “123455787” are also blacklisted, because the longest prefix will be
    matched.
 
-   Example 1.7. Example database content - userblacklist table
+   Example 1.8. Example database content - userblacklist table
 ...
 +----+----------------+-------------+-----------+-----------+
 | id | username       | domain      | prefix    | whitelist |

+ 8 - 4
modules_k/userblacklist/db.c

@@ -36,10 +36,14 @@
 #include "../../lib/trie/dtrie.h"
 
 
+
 /**
  * Builds a d-tree using database entries.
  * \return negative on failure, postive on success, indicating the number of d-tree entries
  */
+
+extern int match_mode;
+
 int db_build_userbl_tree(const str *username, const str *domain, const str *table, struct dtrie_node_t *root, int use_domain)
 {
 	db_key_t columns[2] = { &userblacklist_prefix_col, &userblacklist_whitelist_col };
@@ -67,7 +71,7 @@ int db_build_userbl_tree(const str *username, const str *domain, const str *tabl
 		return -1;
 	}
 
-	dtrie_clear(root, NULL, 10);
+	dtrie_clear(root, NULL, match_mode);
 
 	if (RES_COL_N(res) > 1) {
 		for(i = 0; i < RES_ROW_N(res); i++) {
@@ -84,7 +88,7 @@ int db_build_userbl_tree(const str *username, const str *domain, const str *tabl
 						nodeflags=(void *)MARK_WHITELIST;
 					}
 					if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
-						nodeflags, 10) < 0) LM_ERR("could not insert values into trie.\n");
+						nodeflags, match_mode) < 0) LM_ERR("could not insert values into trie.\n");
 					n++;
 				}
 				else {
@@ -120,7 +124,7 @@ int db_reload_source(const str *table, struct dtrie_node_t *root)
 		return -1;
 	}
 
-	dtrie_clear(root, NULL, 10);
+	dtrie_clear(root, NULL, match_mode);
 
 	if (RES_COL_N(res) > 1) {
 		for(i = 0; i < RES_ROW_N(res); i++) {
@@ -134,7 +138,7 @@ int db_reload_source(const str *table, struct dtrie_node_t *root)
 					if (RES_ROWS(res)[i].values[1].val.int_val == 0) nodeflags=(void *) MARK_BLACKLIST;
 					else nodeflags=(void *)MARK_WHITELIST;
 					if (dtrie_insert(root, RES_ROWS(res)[i].values[0].val.string_val, strlen(RES_ROWS(res)[i].values[0].val.string_val),
-						nodeflags, 10) < 0) LM_ERR("could not insert values into trie.\n");
+						nodeflags, match_mode) < 0) LM_ERR("could not insert values into trie.\n");
 					n++;
 				}
 				else {

+ 25 - 0
modules_k/userblacklist/doc/userblacklist_admin.xml

@@ -93,6 +93,31 @@ modparam("userblacklist", "use_domain", 0)
 		    </programlisting>
 	    </example>
     </section>
+    <section>
+    	    <title><varname>match_mode</varname> (integer)</title>
+	    <para>
+The number of individual characters that are used for matching. 
+Valid values are 10 or 128. When you specifiy 10, only digits will be used for matching, 
+this operation mode is equivalent to the old behaviour. When configured with 128, 
+all standard ascii chars are available for matching. Please be aware that memory 
+requirements for storing the routing tree in shared memory will also increase by a factor of 12.8.
+	    </para>
+	    <para>
+		    <emphasis>
+			    Default value is <quote>10</quote>.
+		    </emphasis>
+	    </para>
+	    <example>
+		    <title>Set <varname>match_mode</varname> parameter</title>
+		    <programlisting format="linespecific">
+...
+modparam("userblacklist", "match_mode", 128)
+...
+		    </programlisting>
+	    </example>
+    </section>
+    
+
 </section>
 <section>
 	<title>Exported Functions</title>

+ 11 - 9
modules_k/userblacklist/userblacklist.c

@@ -73,7 +73,8 @@ struct check_blacklist_fs_t {
 };
 
 str userblacklist_db_url = str_init(DEFAULT_RODB_URL);
-static int use_domain   = 0;
+int use_domain   = 0;
+int match_mode = 10; /* numeric */
 static struct dtrie_node_t *gnode = NULL;
 
 /* ---- fixup functions: */
@@ -122,6 +123,7 @@ static param_export_t params[] = {
 	userblacklist_DB_COLS
 	globalblacklist_DB_COLS
 	{ "use_domain",      INT_PARAM, &use_domain },
+	{ "match_mode",	     INT_PARAM, &match_mode},
 	{ 0, 0, 0}
 };
 
@@ -291,7 +293,7 @@ static int check_user_list(struct sip_msg *msg, char* str1, char* str2, char* st
 		ptr = ptr + 1;
 	}
 
-	nodeflags = dtrie_longest_match(dtrie_root, ptr, strlen(ptr), NULL, 10);
+	nodeflags = dtrie_longest_match(dtrie_root, ptr, strlen(ptr), NULL, match_mode);
 	if (nodeflags) {
 		if (*nodeflags == (void *)MARK_WHITELIST) {
 			/* LM_ERR("whitelisted"); */
@@ -395,7 +397,7 @@ static int add_source(const char *table)
 	strcpy(src->table, table);
 	LM_DBG("add table %s", table);
 
-	src->dtrie_root = dtrie_init(10);
+	src->dtrie_root = dtrie_init(match_mode);
 
 	if (src->dtrie_root == NULL) {
 		LM_ERR("could not initialize data");
@@ -510,15 +512,15 @@ static int check_blacklist(struct sip_msg *msg, struct check_blacklist_fs_t *arg
 
 	ptr = req_number;
 	/* Skip over non-digits.  */
-	while (strlen(ptr) > 0 && !isdigit(*ptr)) {
-		ptr = ptr + 1;
+	while (match_mode == 10 && strlen(ptr) > 0 && !isdigit(*ptr)) {
+			ptr = ptr + 1;
 	}
 
 	LM_DBG("check entry %s\n", req_number);
 
 	/* avoids dirty reads when updating d-tree */
 	lock_get(lock);
-	nodeflags = dtrie_longest_match(arg1->dtrie_root, ptr, strlen(ptr), NULL, 10);
+	nodeflags = dtrie_longest_match(arg1->dtrie_root, ptr, strlen(ptr), NULL, match_mode);
 	if (nodeflags) {
 		if (*nodeflags == (void *)MARK_WHITELIST) {
 			/* LM_DBG("whitelisted"); */
@@ -591,7 +593,7 @@ static void destroy_source_list(void)
 			sources->head = src->next;
 
 			if (src->table) shm_free(src->table);
-			dtrie_destroy(&(src->dtrie_root), NULL, 10);
+			dtrie_destroy(&(src->dtrie_root), NULL, match_mode);
 			shm_free(src);
 		}
 
@@ -673,7 +675,7 @@ static int mi_child_init(void)
 	if(userblacklist_child_initialized)
 		return 0;
 	if (userblacklist_db_open() != 0) return -1;
-	dtrie_root=dtrie_init(10);
+	dtrie_root=dtrie_init(match_mode);
 	if (dtrie_root == NULL) {
 		LM_ERR("could not initialize data");
 		return -1;
@@ -692,5 +694,5 @@ static void mod_destroy(void)
 	destroy_source_list();
 	destroy_shmlock();
 	userblacklist_db_close();
-	dtrie_destroy(&dtrie_root, NULL, 10);
+	dtrie_destroy(&dtrie_root, NULL, match_mode);
 }

+ 11 - 3
test/unit/25.sh

@@ -34,6 +34,7 @@ function cleanup() {
 	$MYSQL "delete from userblacklist where username='494675231';"
 	$MYSQL "delete from userblacklist where username='494675453';"
 	$MYSQL "delete from userblacklist where username='494675454';"
+	$MYSQL "delete from userblacklist where username='user4946';"
 	$MYSQL "delete from globalblacklist where description='_test_';"
 	exit $1;
 }
@@ -58,6 +59,7 @@ $MYSQL "insert into location (username,contact,socket,user_agent,cseq,q) values
 
 $MYSQL "insert into location (username,contact,socket,user_agent,cseq,q) values (\"49721123456784\",\"sip:[email protected]\",\"udp:127.0.0.1:5060\",\"ser_test\",1,-1);"
 
+$MYSQL "insert into location (username,contact,socket,user_agent,cseq,q) values (\"user4946\",\"sip:[email protected]\",\"udp:127.0.0.1:5060\",\"ser_test\",1,-1);"
 
 # setup userblacklist, first some dummy data
 $MYSQL "insert into userblacklist (username, domain, prefix, whitelist) values ('494675454','','49900','0');"
@@ -133,17 +135,23 @@ $MYSQL "insert into globalblacklist (prefix, whitelist, description) values ('2'
 
 $CTL fifo reload_blacklist
 
-if [ "$ret" -ne 1 ] ; then
+sipp -sn uac -s 49721123456785 127.0.0.1:5059 -i 127.0.0.1 -m 1 -f 2 -p 5061 &> /dev/null
+ret=$?
+
+if [ "$ret" -ne 0 ] ; then
 	cleanup 1
 fi;
 
-sipp -sn uac -s 49721123456785 127.0.0.1:5059 -i 127.0.0.1 -m 1 -f 2 -p 5061 &> /dev/null
+$MYSQL "insert into globalblacklist (prefix, whitelist, description) values ('user4946','0','_test_');"
+
+sipp -sn uac -s user4946 127.0.0.1:5059 -i 127.0.0.1 -m 1 -f 2 -p 5061 &> /dev/null
 ret=$?
 
-if [ "$ret" -ne 0 ] ; then
+if [ "$ret" -ne 1 ] ; then
 	cleanup 1
 fi;
 
+
 sipp -sn uac -s 49721123456784 127.0.0.1:5059 -i 127.0.0.1 -m 1 -f 2 -p 5061 &> /dev/null
 ret=$?