瀏覽代碼

userblacklist: user core fixup helpers for string parameters

- it allows both static and dynamic string parameters
- should fix the issue of using static strings, the parameters were no
  longer evaluated and empty values were used
Daniel-Constantin Mierla 9 年之前
父節點
當前提交
54562b873c
共有 1 個文件被更改,包括 57 次插入68 次删除
  1. 57 68
      modules/userblacklist/userblacklist.c

+ 57 - 68
modules/userblacklist/userblacklist.c

@@ -13,8 +13,8 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
@@ -75,7 +75,7 @@ typedef struct _avp_check
 
 
 struct check_blacklist_fs_t {
-  struct dtrie_node_t *dtrie_root;
+	struct dtrie_node_t *dtrie_root;
 };
 
 str userblacklist_db_url = str_init(DEFAULT_RODB_URL);
@@ -179,7 +179,7 @@ struct source_t {
 
 
 struct source_list_t {
-  struct source_t *head;
+	struct source_t *head;
 };
 
 
@@ -190,25 +190,13 @@ static struct dtrie_node_t *dtrie_root = NULL;
 
 static int check_user_blacklist_fixup(void** param, int param_no)
 {
-	pv_elem_t *model=NULL;
-	str s;
-
-	/* convert to str */
-	s.s = (char*)*param;
-	s.len = strlen(s.s);
-
 	if (param_no > 0 && param_no <= 4) {
-		if(s.len == 0 && param_no != 4) {
+		if(strlen((char*)*param) == 0 && param_no != 4) {
 			LM_ERR("no parameter %d\n", param_no);
 			return E_UNSPEC;
 		}
+		return fixup_spve_null(param, 1);
 
-		if(pv_parse_format(&s, &model) < 0 || !model) {
-			LM_ERR("wrong format [%.*s] for parameter %d\n", s.len, s.s, param_no);
-			return E_UNSPEC;
-		}
-
-		*param = (void*)model;
 	} else {
 		LM_ERR("wrong number of parameters\n");
 	}
@@ -217,7 +205,8 @@ static int check_user_blacklist_fixup(void** param, int param_no)
 }
 
 
-static int check_user_list(struct sip_msg *msg, char* str1, char* str2, char* str3, char* str4, int listtype)
+static int check_user_list(sip_msg_t *msg, char* str1, char* str2,
+		char* str3, char* str4, int listtype)
 {
 	str user = { .len = 0, .s = NULL };
 	str domain = { .len = 0, .s = NULL};
@@ -229,32 +218,31 @@ static int check_user_list(struct sip_msg *msg, char* str1, char* str2, char* st
 	char req_number[MAXNUMBERLEN+1];
 
 	/* user */
-	if(((pv_elem_p)str1)->spec!=NULL && ((pv_elem_p)str1)->spec->getf!=NULL) {
-		if(pv_printf_s(msg, (pv_elem_p)str1, &user) != 0) {
-			LM_ERR("cannot print user pseudo-variable\n");
-			return -1;
-		}
+	if(fixup_get_svalue(msg, (gparam_t*)str1, &user)!=0) {
+		LM_ERR("cannot print user pseudo-variable\n");
+		return -1;
 	}
 	/* domain */
-	if(((pv_elem_p)str2)->spec!=NULL && ((pv_elem_p)str2)->spec->getf) {
-		if(pv_printf_s(msg, (pv_elem_p)str2, &domain) != 0) {
-			LM_ERR("cannot print domain pseudo-variable\n");
-			return -1;
-		}
+	if(fixup_get_svalue(msg, (gparam_t*)str2, &domain)!=0) {
+		LM_ERR("cannot print domain pseudo-variable\n");
+		return -1;
 	}
 	/* source number */
-	if(str3 != NULL && ((pv_elem_p)str3)->spec!=NULL && ((pv_elem_p)str3)->spec->getf!=NULL) {
-		if(pv_printf_s(msg, (pv_elem_p)str3, &number) != 0) {
+	if(str3 != NULL) {
+		if(fixup_get_svalue(msg, (gparam_t*)str3, &number)!=0) {
 			LM_ERR("cannot print number pseudo-variable\n");
 			return -1;
 		}
 	}
 	/* table name */
-	if(str4 != NULL && strlen(str4) > 0) {
-		/* string */
-		table.s=str4;
-		table.len=strlen(str4);
-	} else {
+	if(str4 != NULL) {
+		if(fixup_get_svalue(msg, (gparam_t*)str3, &table)!=0) {
+			LM_ERR("cannot print number pseudo-variable\n");
+			return -1;
+		}
+	}
+
+	if(table.len<=0) {
 		/* use default table name */
 		table.len=userblacklist_table.len;
 		table.s=userblacklist_table.s;
@@ -267,7 +255,8 @@ static int check_user_list(struct sip_msg *msg, char* str1, char* str2, char* st
 
 	if(number.s == NULL) {
 		/* use R-URI */
-		if ((parse_sip_msg_uri(msg) < 0) || (!msg->parsed_uri.user.s) || (msg->parsed_uri.user.len > MAXNUMBERLEN)) {
+		if ((parse_sip_msg_uri(msg) < 0) || (!msg->parsed_uri.user.s)
+				|| (msg->parsed_uri.user.len > MAXNUMBERLEN)) {
 			LM_ERR("cannot parse msg URI\n");
 			return -1;
 		}
@@ -457,7 +446,7 @@ static int check_blacklist_fixup(void **arg, int arg_no)
 	char *table = (char *)(*arg);
 	struct dtrie_node_t *node = NULL;
 	struct check_blacklist_fs_t *new_arg;
-	
+
 	if (arg_no != 1) {
 		LM_ERR("wrong number of parameters\n");
 		return -1;
@@ -471,7 +460,7 @@ static int check_blacklist_fixup(void **arg, int arg_no)
 	if (add_source(table) != 0) {
 		LM_ERR("could not add table");
 		return -1;
-	}	
+	}
 
 	/* get the node that belongs to the table */
 	node = table2dt(table);
@@ -689,8 +678,8 @@ static void dump_dtrie_mi(const struct dtrie_node_t *root,
 		return ;
 	}
 
-        /* If data found, add a new node to the reply tree */
-        if (root->data) {
+	/* If data found, add a new node to the reply tree */
+	if (root->data) {
 		/* Create new node and add it to the roots's kids */
 		if(!(crt_node = add_mi_node_child(&reply->node, MI_DUP_NAME, prefix,
 				*length, 0, 0)) ) {
@@ -713,34 +702,34 @@ static void dump_dtrie_mi(const struct dtrie_node_t *root,
 			LM_ERR("cannot add attributes to the node\n");
 			return ;
 		}
-        }
+	}
 
 	/* Perform a DFS search */
-        for (i = 0; i < branches; i++) {
-                /* If child branch found, traverse it */
-                if (root->child[i]) {
-                        if (branches == 10) {
-                                digit = i + '0';
-                        } else {
-                                digit = i;
-                        }
-
-                        /* Push digit in prefix stack */
+	for (i = 0; i < branches; i++) {
+		/* If child branch found, traverse it */
+		if (root->child[i]) {
+			if (branches == 10) {
+				digit = i + '0';
+			} else {
+				digit = i;
+			}
+
+			/* Push digit in prefix stack */
 			if (*length >= MAXNUMBERLEN + 1) {
 				LM_ERR("prefix length exceeds %d\n", MAXNUMBERLEN + 1);
 				return ;
 			}
-                        prefix[(*length)++] = digit;
+			prefix[(*length)++] = digit;
 
-                        /* Recursive DFS call */
-	                dump_dtrie_mi(root->child[i], branches, prefix, length, reply);
+			/* Recursive DFS call */
+			dump_dtrie_mi(root->child[i], branches, prefix, length, reply);
 
-                        /* Pop digit from prefix stack */
-                        (*length)--;
-                }
-        }
+			/* Pop digit from prefix stack */
+			(*length)--;
+		}
+	}
 
-        return ;
+	return ;
 }
 
 
@@ -824,8 +813,8 @@ static struct mi_root * check_list_mi(struct mi_root* cmd, int list_type)
 
 	switch (list_type) {
 		case MARK_WHITELIST:
-                        attr.s = WHITELISTED_S;
-                        attr.len = WHITELISTED_LEN;
+			attr.s = WHITELISTED_S;
+			attr.len = WHITELISTED_LEN;
 
 			if (ret == MARK_WHITELIST) {
 				val.s = TRUE_S;
@@ -834,8 +823,8 @@ static struct mi_root * check_list_mi(struct mi_root* cmd, int list_type)
 
 			break;
 		case MARK_BLACKLIST:
-                        attr.s = BLACKLISTED_S;
-                        attr.len = BLACKLISTED_LEN;
+			attr.s = BLACKLISTED_S;
+			attr.len = BLACKLISTED_LEN;
 
 			if (ret == MARK_BLACKLIST) {
 				val.s = TRUE_S;
@@ -967,8 +956,8 @@ static struct mi_root * check_userlist_mi(struct mi_root* cmd, int list_type)
 
 	switch (list_type) {
 		case MARK_WHITELIST:
-                        attr.s = WHITELISTED_S;
-                        attr.len = WHITELISTED_LEN;
+			attr.s = WHITELISTED_S;
+			attr.len = WHITELISTED_LEN;
 
 			if (ret == MARK_WHITELIST) {
 				val.s = TRUE_S;
@@ -977,8 +966,8 @@ static struct mi_root * check_userlist_mi(struct mi_root* cmd, int list_type)
 
 			break;
 		case MARK_BLACKLIST:
-                        attr.s = BLACKLISTED_S;
-                        attr.len = BLACKLISTED_LEN;
+			attr.s = BLACKLISTED_S;
+			attr.len = BLACKLISTED_LEN;
 
 			if (ret == MARK_BLACKLIST) {
 				val.s = TRUE_S;