2
0

blst.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*$Id$
  2. *
  3. * Copyright (C) 2007 iptelorg GmbH
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. *
  19. * Blacklist related script functions
  20. *
  21. * History:
  22. * -------
  23. * 2007-07-30 created by andrei
  24. */
  25. #include "../../modparam.h"
  26. #include "../../dprint.h"
  27. #include "../../parser/msg_parser.h"
  28. #include "../../parser/hf.h"
  29. #include "../../dst_blacklist.h"
  30. #include "../../timer_ticks.h"
  31. #include "../../ip_addr.h"
  32. #include "../../compiler_opt.h"
  33. #include "../../ut.h"
  34. #include "../../globals.h"
  35. #include "../../cfg_core.h"
  36. MODULE_VERSION
  37. static int blst_add_f(struct sip_msg*, char*, char*);
  38. static int blst_add_retry_after_f(struct sip_msg*, char*, char*);
  39. static int blst_del_f(struct sip_msg*, char*, char*);
  40. static int blst_is_blacklisted_f(struct sip_msg*, char*, char*);
  41. static int blst_set_ignore_f(struct sip_msg*, char*, char*);
  42. static int blst_clear_ignore_f(struct sip_msg*, char*, char*);
  43. static int blst_rpl_set_ignore_f(struct sip_msg*, char*, char*);
  44. static int blst_rpl_clear_ignore_f(struct sip_msg*, char*, char*);
  45. static cmd_export_t cmds[]={
  46. {"blst_add", blst_add_f, 0, 0,
  47. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
  48. {"blst_add", blst_add_f, 1, fixup_var_int_1,
  49. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
  50. {"blst_add_retry_after", blst_add_retry_after_f, 2, fixup_var_int_12,
  51. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
  52. {"blst_del", blst_del_f, 0, 0,
  53. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
  54. {"blst_is_blacklisted", blst_is_blacklisted_f, 0, 0,
  55. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
  56. {"blst_set_ignore", blst_set_ignore_f, 0, 0,
  57. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
  58. {"blst_set_ignore", blst_set_ignore_f, 1, fixup_var_int_1,
  59. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
  60. {"blst_clear_ignore", blst_clear_ignore_f, 0, 0,
  61. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
  62. {"blst_clear_ignore", blst_clear_ignore_f, 1, fixup_var_int_1,
  63. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
  64. {"blst_rpl_set_ignore", blst_rpl_set_ignore_f, 0, 0,
  65. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  66. {"blst_rpl_set_ignore", blst_rpl_set_ignore_f, 1, fixup_var_int_1,
  67. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  68. {"blst_rpl_clear_ignore", blst_rpl_clear_ignore_f, 0, 0,
  69. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  70. {"blst_rpl_clear_ignore", blst_rpl_clear_ignore_f, 1, fixup_var_int_1,
  71. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  72. {0,0,0,0,0}
  73. };
  74. static param_export_t params[]={
  75. {0,0,0}
  76. }; /* no params */
  77. struct module_exports exports= {
  78. "blst",
  79. cmds,
  80. 0, /* RPC methods */
  81. params,
  82. 0, /* module initialization function */
  83. 0, /* response function */
  84. 0, /* destroy function */
  85. 0, /* on_cancel function */
  86. 0, /* per-child init function */
  87. };
  88. static int blst_add_f(struct sip_msg* msg, char* to, char* foo)
  89. {
  90. #ifdef USE_DST_BLACKLIST
  91. int t;
  92. struct dest_info src;
  93. if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
  94. t=0;
  95. if (unlikely( to && (get_int_fparam(&t, msg, (fparam_t*)to)<0)))
  96. return -1;
  97. if (t==0)
  98. t=cfg_get(core, core_cfg, blst_timeout);
  99. init_dest_info(&src);
  100. src.send_sock=0;
  101. src.to=msg->rcv.src_su;
  102. src.id=msg->rcv.proto_reserved1;
  103. src.proto=msg->rcv.proto;
  104. dst_blacklist_force_add_to(BLST_ADM_PROHIBITED, &src, msg,
  105. S_TO_TICKS(t));
  106. return 1;
  107. }else{
  108. LOG(L_WARN, "WARNING: blst: blst_add: blacklist support disabled\n");
  109. }
  110. #else /* USE_DST_BLACKLIST */
  111. LOG(L_WARN, "WARNING: blst: blst_add: blacklist support not compiled-in"
  112. " - no effect -\n");
  113. #endif /* USE_DST_BLACKLIST */
  114. return 1;
  115. }
  116. /* returns error if no retry_after hdr field is present */
  117. static int blst_add_retry_after_f(struct sip_msg* msg, char* min, char* max)
  118. {
  119. #ifdef USE_DST_BLACKLIST
  120. int t_min, t_max, t;
  121. struct dest_info src;
  122. struct hdr_field* hf;
  123. if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
  124. if (unlikely(get_int_fparam(&t_min, msg, (fparam_t*)min)<0)) return -1;
  125. if (likely(max)){
  126. if (unlikely(get_int_fparam(&t_max, msg, (fparam_t*)max)<0))
  127. return -1;
  128. }else{
  129. t_max=0;
  130. }
  131. init_dest_info(&src);
  132. src.send_sock=0;
  133. src.to=msg->rcv.src_su;
  134. src.id=msg->rcv.proto_reserved1;
  135. src.proto=msg->rcv.proto;
  136. t=-1;
  137. if ((parse_headers(msg, HDR_RETRY_AFTER_F, 0)==0) &&
  138. (msg->parsed_flag & HDR_RETRY_AFTER_F)){
  139. for (hf=msg->headers; hf; hf=hf->next)
  140. if (hf->type==HDR_RETRY_AFTER_T){
  141. /* found */
  142. t=(unsigned)(unsigned long)hf->parsed;
  143. break;
  144. }
  145. }
  146. if (t<0)
  147. return -1;
  148. t=MAX_unsigned(t, t_min);
  149. t=MIN_unsigned(t, t_max);
  150. if (likely(t))
  151. dst_blacklist_force_add_to(BLST_ADM_PROHIBITED, &src, msg,
  152. S_TO_TICKS(t));
  153. return 1;
  154. }else{
  155. LOG(L_WARN, "WARNING: blst: blst_add_retry_after:"
  156. " blacklist support disabled\n");
  157. }
  158. #else /* USE_DST_BLACKLIST */
  159. LOG(L_WARN, "WARNING: blst: blst_add_retry_after:"
  160. " blacklist support not compiled-in - no effect -\n");
  161. #endif /* USE_DST_BLACKLIST */
  162. return 1;
  163. }
  164. static int blst_del_f(struct sip_msg* msg, char* foo, char* bar)
  165. {
  166. #ifdef USE_DST_BLACKLIST
  167. struct dest_info src;
  168. if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
  169. init_dest_info(&src);
  170. src.send_sock=0;
  171. src.to=msg->rcv.src_su;
  172. src.id=msg->rcv.proto_reserved1;
  173. src.proto=msg->rcv.proto;
  174. if (dst_blacklist_del(&src, msg))
  175. return 1;
  176. }else{
  177. LOG(L_WARN, "WARNING: blst: blst_del: blacklist support disabled\n");
  178. }
  179. #else /* USE_DST_BLACKLIST */
  180. LOG(L_WARN, "WARNING: blst: blst_del: blacklist support not compiled-in"
  181. " - no effect -\n");
  182. #endif /* USE_DST_BLACKLIST */
  183. return -1;
  184. }
  185. static int blst_is_blacklisted_f(struct sip_msg* msg, char* foo, char* bar)
  186. {
  187. #ifdef USE_DST_BLACKLIST
  188. struct dest_info src;
  189. if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
  190. init_dest_info(&src);
  191. src.send_sock=0;
  192. src.to=msg->rcv.src_su;
  193. src.id=msg->rcv.proto_reserved1;
  194. src.proto=msg->rcv.proto;
  195. if (dst_is_blacklisted(&src, msg))
  196. return 1;
  197. }else{
  198. LOG(L_WARN, "WARNING: blst: blst_is_blacklisted:"
  199. " blacklist support disabled\n");
  200. }
  201. #else /* USE_DST_BLACKLIST */
  202. LOG(L_WARN, "WARNING: blst: blst_is_blacklisted:"
  203. " blacklist support not compiled-in - no effect -\n");
  204. #endif /* USE_DST_BLACKLIST */
  205. return -1;
  206. }
  207. static int blst_set_ignore_f(struct sip_msg* msg, char* flags, char* foo)
  208. {
  209. #ifdef USE_DST_BLACKLIST
  210. unsigned char blst_imask;
  211. int mask;
  212. if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
  213. return -1;
  214. blst_imask=flags?mask:0xff;
  215. msg->fwd_send_flags.blst_imask|=blst_imask;
  216. return 1;
  217. #else /* USE_DST_BLACKLIST */
  218. LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
  219. " not compiled-in - no effect -\n");
  220. #endif /* USE_DST_BLACKLIST */
  221. return 1;
  222. }
  223. static int blst_clear_ignore_f(struct sip_msg* msg, char* flags, char* foo)
  224. {
  225. #ifdef USE_DST_BLACKLIST
  226. unsigned char blst_imask;
  227. int mask;
  228. if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
  229. return -1;
  230. blst_imask=flags?mask:0xff;
  231. msg->fwd_send_flags.blst_imask&=~blst_imask;
  232. return 1;
  233. #else /* USE_DST_BLACKLIST */
  234. LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
  235. " not compiled-in - no effect -\n");
  236. #endif /* USE_DST_BLACKLIST */
  237. return 1;
  238. }
  239. static int blst_rpl_set_ignore_f(struct sip_msg* msg, char* flags, char* foo)
  240. {
  241. #ifdef USE_DST_BLACKLIST
  242. unsigned char blst_imask;
  243. int mask;
  244. if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
  245. return -1;
  246. blst_imask=flags?mask:0xff;
  247. msg->rpl_send_flags.blst_imask|=blst_imask;
  248. return 1;
  249. #else /* USE_DST_BLACKLIST */
  250. LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
  251. " not compiled-in - no effect -\n");
  252. #endif /* USE_DST_BLACKLIST */
  253. return 1;
  254. }
  255. static int blst_rpl_clear_ignore_f(struct sip_msg* msg, char* flags, char* foo)
  256. {
  257. #ifdef USE_DST_BLACKLIST
  258. unsigned char blst_imask;
  259. int mask;
  260. if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
  261. return -1;
  262. blst_imask=flags?mask:0xff;
  263. msg->rpl_send_flags.blst_imask&=~blst_imask;
  264. return 1;
  265. #else /* USE_DST_BLACKLIST */
  266. LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
  267. " not compiled-in - no effect -\n");
  268. #endif /* USE_DST_BLACKLIST */
  269. return 1;
  270. }