Skip to content

Commit 10101fa

Browse files
committed
dst_blacklists: provide more script functions
* add `add_blacklist_rule` function that adds a rule to a list * add `del_blacklist_rule` function that removes a rule from a list * add `check_blacklist` function that checks whether a pair matches a rule from a list, or from all the active lists
1 parent 9eb654e commit 10101fa

File tree

3 files changed

+217
-8
lines changed

3 files changed

+217
-8
lines changed

blacklists.c

+102
Original file line numberDiff line numberDiff line change
@@ -1174,3 +1174,105 @@ static mi_response_t *mi_del_blacklist_rule(const mi_params_t *params,
11741174

11751175
return init_mi_result_ok();
11761176
}
1177+
1178+
int w_check_blacklist(struct sip_msg *msg, struct bl_head *head,
1179+
struct ip_addr *ip, int *_port, unsigned short _proto, str *_pattern)
1180+
{
1181+
int ret, idx;
1182+
unsigned short port = (_port?*_port:0);
1183+
1184+
if (head) {
1185+
/* we need to check against a specific list */
1186+
idx = head - blst_heads;
1187+
ret = check_against_rule_list(ip, _pattern, port, _proto, idx);
1188+
} else {
1189+
/* if we do not have a head, we check against all enabled */
1190+
ret = check_against_blacklist(ip, _pattern, port, _proto);
1191+
}
1192+
return ret ?1:-1;
1193+
}
1194+
1195+
int fixup_blacklist_proto(void** param)
1196+
{
1197+
int proto = PROTO_NONE;
1198+
str *s = (str*)*param;
1199+
if (s && parse_proto((unsigned char *)s->s, s->len, &proto) < 0)
1200+
return E_BAD_PROTO;
1201+
1202+
*param = (void *)(unsigned long)proto;
1203+
return 0;
1204+
}
1205+
1206+
int fixup_blacklist_net(void** param)
1207+
{
1208+
str *s = (str*)*param;
1209+
str tmp = *s;
1210+
struct bl_net_flags *nf = pkg_malloc(sizeof *nf);
1211+
if (!nf)
1212+
return E_OUT_OF_MEM;
1213+
trim(&tmp);
1214+
if (tmp.s[0] == '!') {
1215+
nf->flags = BLR_APPLY_CONTRARY;
1216+
tmp.s++;
1217+
tmp.len--;
1218+
trim(&tmp);
1219+
}
1220+
1221+
if (parse_ip_net(tmp.s, tmp.len, &nf->ipnet) < 0) {
1222+
pkg_free(nf);
1223+
return E_BAD_ADDRESS;
1224+
}
1225+
*param = nf;
1226+
return 0;
1227+
}
1228+
1229+
int fixup_blacklist_net_free(void** param)
1230+
{
1231+
pkg_free(*param);
1232+
return 0;
1233+
}
1234+
1235+
int w_add_blacklist_rule(struct sip_msg *msg, struct bl_head *head,
1236+
struct bl_net_flags *nf, int *_port, unsigned short _proto,
1237+
str *_pattern, int *_exp)
1238+
{
1239+
struct bl_rule *list = NULL;
1240+
unsigned short port = (_port?*_port:0);
1241+
1242+
if (head->flags & BL_READONLY_LIST) {
1243+
LM_ERR("cannot modify read-only blacklist!\n");
1244+
return -1;
1245+
}
1246+
1247+
if (_exp && *_exp && !(head->flags & BL_DO_EXPIRE)) {
1248+
LM_ERR("blacklist does not support expiring rules!\n");
1249+
return -1;
1250+
}
1251+
1252+
if (add_rule_to_list(&list, &list, &nf->ipnet, _pattern,
1253+
port, _proto, nf->flags) != 0) {
1254+
LM_ERR("cannot build blacklist rule!\n");
1255+
return -1;
1256+
}
1257+
if (add_list_to_head(head, list, list, 0, (_exp?*_exp:0)) < 0) {
1258+
LM_ERR("cannot add blacklist rule!\n");
1259+
return -1;
1260+
}
1261+
return 1;
1262+
}
1263+
1264+
int w_del_blacklist_rule(struct sip_msg *msg, struct bl_head *head,
1265+
struct bl_net_flags *nf, int *_port, unsigned short _proto,
1266+
str *_pattern)
1267+
{
1268+
unsigned short port = (_port?*_port:0);
1269+
1270+
if (head->flags & BL_READONLY_LIST) {
1271+
LM_ERR("cannot modify read-only blacklist!\n");
1272+
return -1;
1273+
}
1274+
if (del_rule_from_list(head, &nf->ipnet,
1275+
_pattern, port, _proto, nf->flags) != 0)
1276+
return -1;
1277+
return 1;
1278+
}

blacklists.h

+19
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,24 @@ static inline int check_blacklists( unsigned short proto,
9999
return check_against_blacklist(&ip, &body, port, proto);
100100
}
101101

102+
103+
struct bl_net_flags {
104+
struct net ipnet;
105+
unsigned int flags;
106+
};
107+
108+
int fixup_blacklist_proto(void** param);
109+
int fixup_blacklist_net(void** param);
110+
int fixup_blacklist_net_free(void** param);
111+
112+
int w_check_blacklist(struct sip_msg *msg, struct bl_head *head,
113+
struct ip_addr *ip, int *port, unsigned short _proto, str *_pattern);
114+
int w_add_blacklist_rule(struct sip_msg *msg, struct bl_head *head,
115+
struct bl_net_flags *_nf, int *_port, unsigned short _proto,
116+
str *_pattern, int *_exp);
117+
int w_del_blacklist_rule(struct sip_msg *msg, struct bl_head *head,
118+
struct bl_net_flags *_nf, int *_port, unsigned short _proto,
119+
str *_pattern);
120+
102121
#endif /* _BLACKLST_H */
103122

core_cmds.c

+96-8
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ static int fixup_mflag(void** param);
4949
static int fixup_bflag(void** param);
5050
static int fixup_qvalue(void** param);
5151
static int fixup_f_send_sock(void** param);
52+
static int fixup_blacklist_name(void** param);
5253
static int fixup_blacklist(void** param);
54+
static int fixup_blacklist_ip(void** param);
55+
static int fixup_blacklist_free(void** param);
5356
static int fixup_check_wrvar(void** param);
5457
static int fixup_avp_list(void** param);
5558
static int fixup_check_avp(void** param);
5659
static int fixup_event_name(void** param);
5760
static int fixup_format_string(void** param);
5861
static int fixup_nt_string(void** param);
62+
static int fixup_nt_str(void** param);
63+
static int fixup_nt_str_free(void** param);
5964

6065
static int w_forward(struct sip_msg *msg, struct proxy_l *dest);
6166
static int w_send(struct sip_msg *msg, struct proxy_l *dest, str *headers);
@@ -229,6 +234,37 @@ static cmd_export_t core_cmds[]={
229234
{"unuse_blacklist", (cmd_function)w_unuse_blacklist, {
230235
{CMD_PARAM_STR, fixup_blacklist, 0}, {0,0,0}},
231236
ALL_ROUTES},
237+
{"check_blacklist", (cmd_function)w_check_blacklist, {
238+
{CMD_PARAM_STR, fixup_blacklist, 0},
239+
{CMD_PARAM_STR, fixup_blacklist_ip, fixup_blacklist_free}, /* ip */
240+
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, /* port */
241+
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
242+
fixup_blacklist_proto, 0}, /* proto */
243+
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
244+
fixup_nt_str, fixup_nt_str_free}, /* pattern */
245+
{0,0,0}},
246+
ALL_ROUTES},
247+
{"add_blacklist_rule", (cmd_function)w_add_blacklist_rule, {
248+
{CMD_PARAM_STR, fixup_blacklist_name, 0},
249+
{CMD_PARAM_STR, fixup_blacklist_net, fixup_blacklist_net_free}, /* ip */
250+
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, /* port */
251+
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
252+
fixup_blacklist_proto, 0}, /* proto */
253+
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
254+
fixup_nt_str, fixup_nt_str_free}, /* pattern */
255+
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, /* expire */
256+
{0,0,0}},
257+
ALL_ROUTES},
258+
{"del_blacklist_rule", (cmd_function)w_del_blacklist_rule, {
259+
{CMD_PARAM_STR, fixup_blacklist_name, 0},
260+
{CMD_PARAM_STR, fixup_blacklist_net, fixup_blacklist_net_free}, /* ip */
261+
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, /* port */
262+
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
263+
fixup_blacklist_proto, 0}, /* proto */
264+
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
265+
fixup_nt_str, fixup_nt_str_free}, /* pattern */
266+
{0,0,0}},
267+
ALL_ROUTES},
232268
{"cache_store", (cmd_function)w_cache_store, {
233269
{CMD_PARAM_STR, 0, 0},
234270
{CMD_PARAM_STR, 0, 0},
@@ -449,21 +485,47 @@ static int fixup_f_send_sock(void** param)
449485
return E_BAD_ADDRESS;
450486
}
451487

488+
static int fixup_blacklist_name(void** param)
489+
{
490+
str *s = (str*)*param;
491+
*param = get_bl_head_by_name(s);
492+
if (!*param) {
493+
LM_ERR("blacklist %.*s not configured\n", s->len, s->s);
494+
return E_CFG;
495+
}
496+
return 0;
497+
}
498+
452499
static int fixup_blacklist(void** param)
453500
{
454501
str *s = (str*)*param;
455502

456-
if (!str_strcasecmp(s, _str("all")))
503+
if (!str_strcasecmp(s, _str("all"))) {
457504
*param = NULL;
458-
else {
459-
*param = get_bl_head_by_name(s);
460-
if (!*param) {
461-
LM_ERR("[UN]USE_BLACKLIST - list "
462-
"%.*s not configured\n", s->len, s->s);
463-
return E_CFG;
464-
}
505+
return 0;
506+
} else {
507+
return fixup_blacklist_name(param);
465508
}
509+
}
466510

511+
static int fixup_blacklist_free(void** param)
512+
{
513+
pkg_free(*param);
514+
return 0;
515+
}
516+
517+
static int fixup_blacklist_ip(void** param)
518+
{
519+
str *s = (str*)*param;
520+
struct ip_addr *ip_pkg;
521+
struct ip_addr *ip = str2ip(s);
522+
if (!ip)
523+
return E_BAD_ADDRESS;
524+
ip_pkg = pkg_malloc(sizeof *ip_pkg);
525+
if (!ip_pkg)
526+
return E_OUT_OF_MEM;
527+
memcpy(ip_pkg, ip, sizeof *ip_pkg);
528+
*param = ip_pkg;
467529
return 0;
468530
}
469531

@@ -544,6 +606,32 @@ static int fixup_nt_string(void** param)
544606
return 0;
545607
}
546608

609+
static int fixup_nt_str(void** param)
610+
{
611+
str *s = (str*)*param;
612+
str *s_nt;
613+
614+
s_nt = pkg_malloc(sizeof *s_nt + (s?s->len:0) + 1);
615+
if (!s_nt)
616+
return E_OUT_OF_MEM;
617+
s_nt->s = (char *)(s_nt + 1);
618+
if (s) {
619+
s_nt->len = s->len;
620+
memcpy(s_nt->s, s->s, s->len);
621+
} else {
622+
s_nt->len = 0;
623+
}
624+
s_nt->s[s_nt->len] = '\0';
625+
626+
*param = s_nt;
627+
return 0;
628+
}
629+
630+
static int fixup_nt_str_free(void** param)
631+
{
632+
pkg_free(((str *)*param)->s);
633+
return 0;
634+
}
547635

548636
static int w_forward(struct sip_msg *msg, struct proxy_l *dest)
549637
{

0 commit comments

Comments
 (0)