diff --git a/modules/userblacklist/userblacklist.c b/modules/userblacklist/userblacklist.c index 58d7a372c21..3ca85b7b237 100644 --- a/modules/userblacklist/userblacklist.c +++ b/modules/userblacklist/userblacklist.c @@ -62,12 +62,11 @@ static str db_table = str_init("userblacklist"); static int use_domain = 0; /* ---- fixup functions: */ -static int check_blacklist_fixup(void** param, int param_no); -static int check_user_blacklist_fixup(void** param, int param_no); +static int check_blacklist_fixup(void** param); /* ---- exported commands: */ -static int check_user_blacklist(struct sip_msg *msg, char* str1, char* str2, char* str3, char* str4); -static int check_blacklist(struct sip_msg *msg, struct check_blacklist_fs_t *arg1); +static int check_user_blacklist(struct sip_msg *msg, str *user, str *domain, str *number, str *table); +static int check_blacklist(struct sip_msg *msg, struct dt_node_t *root); /* ---- module init functions: */ static int mod_init(void); @@ -80,14 +79,18 @@ mi_response_t *mi_reload_blacklist(const mi_params_t *params, static cmd_export_t cmds[]={ - { "check_user_blacklist", (cmd_function)check_user_blacklist, 2, check_user_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE }, - { "check_user_blacklist", (cmd_function)check_user_blacklist, 3, check_user_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE }, - { "check_user_blacklist", (cmd_function)check_user_blacklist, 4, check_user_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE }, - { "check_blacklist", (cmd_function)check_blacklist, 1, check_blacklist_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE }, - { 0, 0, 0, 0, 0, 0} + {"check_user_blacklist", (cmd_function)check_user_blacklist, { + {CMD_PARAM_STR,0,0}, + {CMD_PARAM_STR,0,0}, + {CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, + {CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, {0,0,0}}, + REQUEST_ROUTE | FAILURE_ROUTE}, + {"check_blacklist", (cmd_function)check_blacklist, { + {CMD_PARAM_STR, check_blacklist_fixup, 0}, {0,0,0}}, + REQUEST_ROUTE | FAILURE_ROUTE}, + {0,0,{{0,0,0}},0} }; - static param_export_t params[] = { { "db_url", STR_PARAM, &db_url.s }, { "db_table", STR_PARAM, &db_table.s }, @@ -155,101 +158,22 @@ static struct source_list_t *sources = NULL; static struct dt_node_t *dt_root; -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) { - LM_ERR("no parameter %d\n", param_no); - return E_UNSPEC; - } - - 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; - } - - if(!model->spec.getf) { - if(param_no == 1) { - if(str2int(&s, (unsigned int*)&model->spec.pvp.pvn.u.isname.name.n) != 0) { - LM_ERR("wrong value [%.*s] for parameter %d\n", s.len, s.s, param_no); - return E_UNSPEC; - } - } else { - if(param_no == 2 || param_no == 3) { - LM_ERR("wrong value [%.*s] for parameter %d\n", s.len, s.s, param_no); - return E_UNSPEC; - } else { - // only a string - return 0; - } - } - } - *param = (void*)model; - } else { - LM_ERR("wrong number of parameters\n"); - } - - return 0; -} - - -static int check_user_blacklist(struct sip_msg *msg, char* str1, char* str2, char* str3, char* str4) +static int check_user_blacklist(struct sip_msg *msg, str *user, str *domain, str *number, str *table) { - str user = { .len = 0, .s = NULL }; - str domain = { .len = 0, .s = NULL}; - str table = { .len = 0, .s = NULL}; - str number = { .len = 0, .s = NULL}; - char whitelist; char *src; char *dst; char req_number[MAXNUMBERLEN+1]; - /* user */ - if(((pv_elem_p)str1)->spec.getf) { - if(pv_printf_s(msg, (pv_elem_p)str1, &user) != 0) { - LM_ERR("cannot print user pseudo-variable\n"); - return -1; - } - } - /* domain */ - if(((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; - } - } - /* source number */ - if(str3 != NULL && ((pv_elem_p)str3)->spec.getf) { - if(pv_printf_s(msg, (pv_elem_p)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 { - /* use default table name */ - table.len=db_table.len; - table.s=db_table.s; - } + if (!table) + table = &db_table; if (msg->first_line.type != SIP_REQUEST) { LM_ERR("SIP msg is not a request\n"); return -1; } - if(number.s == NULL) { + if(number == NULL) { /* use R-URI */ 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"); @@ -258,17 +182,17 @@ static int check_user_blacklist(struct sip_msg *msg, char* str1, char* str2, cha strncpy(req_number, msg->parsed_uri.user.s, msg->parsed_uri.user.len); req_number[msg->parsed_uri.user.len] = '\0'; } else { - if (number.len > MAXNUMBERLEN) { + if (number->len > MAXNUMBERLEN) { LM_ERR("number to long\n"); return -1; } - strncpy(req_number, number.s, number.len); - req_number[number.len] = '\0'; + strncpy(req_number, number->s, number->len); + req_number[number->len] = '\0'; } LM_DBG("check entry %s for user %.*s on domain %.*s in table %.*s\n", req_number, - user.len, user.s, domain.len, domain.s, table.len, table.s); - if (db_build_userbl_tree(&user, &domain, &table, dt_root, use_domain) < 0) { + user->len, user->s, domain->len, domain->s, table->len, table->s); + if (db_build_userbl_tree(user, domain, table, dt_root, use_domain) < 0) { LM_ERR("cannot build d-tree\n"); return -1; } @@ -302,15 +226,15 @@ static int check_user_blacklist(struct sip_msg *msg, char* str1, char* str2, cha * Finds d-tree root for given table. * \return pointer to d-tree root on success, NULL otherwise */ -static struct dt_node_t *table2dt(const char *table) +static struct dt_node_t *table2dt(str *table) { struct source_t *src = sources->head; while (src) { - if (strcmp(table, src->table) == 0) return src->dt_root; + if (strncmp(table->s, src->table, table->len) == 0) return src->dt_root; src = src->next; } - LM_ERR("invalid table '%s'.\n", table); + LM_ERR("invalid table '%.*s'.\n", table->len, table->s); return NULL; } @@ -320,12 +244,12 @@ static struct dt_node_t *table2dt(const char *table) * already present, nothing will be done. * \return zero on success, negative on errors */ -static int add_source(const char *table) +static int add_source(str *table) { /* check if the table is already present */ struct source_t *src = sources->head; while (src) { - if (strcmp(table, src->table) == 0) return 0; + if (strncmp(table->s, src->table, table->len) == 0) return 0; src = src->next; } @@ -339,59 +263,44 @@ static int add_source(const char *table) src->next = sources->head; sources->head = src; - src->table = shm_malloc(strlen(table)+1); + src->table = shm_malloc(table->len+1); if (!src->table) { LM_ERR("out of shared memory.\n"); shm_free(src); return -1; } - strcpy(src->table, table); - LM_DBG("add table %s", table); + strncpy(src->table, table->s, table->len); + src->table[table->len] = 0; + LM_DBG("add table %.*s", table->len, table->s); return dt_init(&(src->dt_root)); } -static int check_blacklist_fixup(void **arg, int arg_no) -{ - char *table = (char *)(*arg); - struct dt_node_t *node = NULL; - if (arg_no != 1) { - LM_ERR("wrong number of parameters\n"); - return -1; - } +static int check_blacklist_fixup(void **arg) +{ + struct dt_node_t *node; - if (!table) { - LM_ERR("no table name\n"); - return -1; - } /* try to add the table */ - if (add_source(table) != 0) { + if (add_source((str*)*arg) != 0) { LM_ERR("could not add table\n"); return -1; } /* get the node that belongs to the table */ - node = table2dt(table); + node = table2dt((str*)*arg); if (!node) { - LM_ERR("invalid table '%s'\n", table); + LM_ERR("invalid table '%.*s'\n", ((str*)*arg)->len, ((str*)*arg)->s); return -1; } - struct check_blacklist_fs_t *new_arg = (struct check_blacklist_fs_t*)pkg_malloc(sizeof(struct check_blacklist_fs_t)); - if (!new_arg) { - LM_ERR("out of private memory\n"); - return -1; - } - memset(new_arg, 0, sizeof(struct check_blacklist_fs_t)); - new_arg->dt_root = node; - *arg=(void*)new_arg; + *arg = node; return 0; } -static int check_blacklist(struct sip_msg *msg, struct check_blacklist_fs_t *arg1) +static int check_blacklist(struct sip_msg *msg, struct dt_node_t *root) { char whitelist; char *src; @@ -429,7 +338,7 @@ static int check_blacklist(struct sip_msg *msg, struct check_blacklist_fs_t *arg /* we guard the dt_root */ lock_get(lock); LM_DBG("check entry %s\n", req_number); - if (dt_longest_match(arg1->dt_root, req_number, &whitelist) >= 0) { + if (dt_longest_match(root, req_number, &whitelist) >= 0) { if (whitelist) { /* LM_DBG("whitelisted\n"); */ lock_release(lock);