basitengr

midreg-1

Feb 15th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.88 KB | None | 0 0
  1. #!KAMAILIO
  2. #
  3. # Kamailio (OpenSER) SIP Server v5.1 - Push configuration script
  4. #!substdef "!ALIAS_IP!192.168.106.7!g"
  5. #!substdef "!LISTEN_PORT!9060!g"
  6.  
  7. #!substdef "!FS_IP!192.168.106.7!g"
  8. #!substdef "!FS_PORT!5060!g"
  9.  
  10. #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
  11. # - the value for 'use_domain' parameters
  12. #!define MULTIDOMAIN 1
  13.  
  14. #!define FLB_NATB 6
  15. #!define FLB_NATSIPPING 7
  16.  
  17.  
  18. memdbg=5
  19. memlog=5
  20. debug=2
  21.  
  22. # ----- log params -----
  23. log_stderror=no
  24. log_facility=LOG_LOCAL6
  25. log_name="SIPServer"
  26. log_prefix="{$hdr(CSeq) [$pr:$fU@$si:$sp]} " #prefix with Caller info
  27. log_prefix_mode=0
  28.  
  29. fork=yes
  30. children=4
  31. disable_tcp=yes
  32. /* add local domain aliases */
  33. alias=ALIAS_IP
  34. port=LISTEN_PORT
  35.  
  36. /* set paths to location of modules */
  37. mpath="/usr/local/lib64/kamailio/modules/"
  38.  
  39. # ------------------ Load Modules ----------------------------------
  40. loadmodule "db_mysql.so"
  41. loadmodule "jsonrpcs.so"
  42. loadmodule "tm.so"
  43. loadmodule "sl.so"
  44. loadmodule "rr.so"
  45. loadmodule "pv.so"
  46. loadmodule "maxfwd.so"
  47. loadmodule "textops.so"
  48. loadmodule "siputils.so"
  49. loadmodule "xlog.so"
  50. loadmodule "sqlops.so"
  51. loadmodule "jansson.so"
  52. loadmodule "auth.so"
  53. loadmodule "avpops.so"
  54. loadmodule "path.so"
  55. loadmodule "kex.so"
  56.  
  57. loadmodule "usrloc.so"
  58. loadmodule "registrar.so"
  59. loadmodule "nathelper.so"
  60. loadmodule "domain.so"
  61.  
  62. # ----------------- setting module-specific parameters ---------------
  63.  
  64. # ------- xlogl params -------
  65. modparam("xlog", "prefix", "")
  66. # ----- jsonrpcs params -----
  67. modparam("jsonrpcs", "pretty_format", 1)
  68.  
  69. modparam("sqlops","sqlcon","cb=>mysql://root:yahoo7@localhost/kamailio")
  70.  
  71. # ----- usrloc params -----
  72. /* enable DB persistency for location entries */
  73. #!ifdef WITH_USRLOCDB
  74.     modparam("usrloc", "db_url", DBURL)
  75.     modparam("usrloc", "db_mode", 2)
  76.     modparam("usrloc", "use_domain", MULTIDOMAIN)
  77. #!endif
  78.  
  79. # ----- nathelper params -----
  80. modparam("nathelper", "natping_interval", 30)
  81. modparam("nathelper", "ping_nated_only", 1)
  82. modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
  83. modparam("nathelper", "sipping_from", "sip:[email protected]")
  84.  
  85. # params needed for NAT traversal in other modules
  86. modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
  87. modparam("usrloc", "nat_bflag", FLB_NATB)
  88.  
  89. # -------------------------  request routing logic -------------------
  90. # main routing logic
  91.  
  92. request_route {
  93.     /* ********* ROUTINE CHECKS  ********************************** */
  94.     xlogl("L_INFO", "[$rm] received in request_route with r-uri ($ru) \n");
  95.  
  96.     # grant Route routing if route headers present
  97.     if (has_totag()) {
  98.         xlogl("L_INFO", "[$rm] received in request_route->has_totag() \n");
  99.         if (loose_route()) {
  100.             xlogl("L_INFO", "[$rm] received in request_route->has_totag->loose_route(): route RELAY \n");
  101.             route(RELAY);
  102.             break;
  103.             } else {
  104.                 if ( is_method("ACK") ) {
  105.                     xlogl("L_INFO", "[$rm] received in request_route->!has_totag->if_ACK \n");
  106.                     if ( t_check_trans() ) {
  107.                         xlogl("L_INFO", "[$rm] received in request_route->!has_totag->if_ACK->t_check_trans: route RELAY \n");
  108.                         route(RELAY);
  109.                         break;
  110.                     } else {
  111.                         exit;
  112.                     }
  113.                 }
  114.                 xlogl("L_ERROR", "[$rm] received in request_route->!has_totag: sl_send_reply(404) \n");
  115.                 sl_send_reply("404","Not here");
  116.             }
  117.             exit;
  118.         }
  119.  
  120.         # Process initial REGISTER|INVITE|OPTIONS
  121.         if ( !has_totag() ) {
  122.             xlogl("L_INFO", "[$rm] received in request_route->if_INVITE: record_route() \n");
  123.             record_route();
  124.             if ( is_method("REGISTER") ) {
  125.                 if ( search("expires=0") ) {
  126.                     xlogl("L_INFO", "----[UNREGISTER]---- SIP Un$rm request received from $fu with Contcat header: $hdr(Contact) \n");
  127.                 } else {
  128.                     xlogl("L_INFO", "----[$rm]---- SIP $rm request received from $fu with contact header [$ct] \n");
  129.                     route(AUTH);
  130.                     # add kamailio path header
  131.                     if ( !add_path_received() ) {
  132.                         xlogl("L_ERROR", "[$rm] --> Unable to add Path \n");
  133.                         sl_send_reply("503", "Internal Path Error");
  134.                     }
  135.                     setbflag(FLB_NATB);
  136.                     setbflag(FLB_NATSIPPING);
  137.                     if ( !save("location", "0x02") ) {
  138.                         xlogl("L_ERROR", "[$rm] --> Unable save location \n");
  139.                         sl_reply_error();
  140.                     }
  141.                 }
  142.             }
  143.         } else {
  144.         xlogl("L_ERROR", "[$rm] received in request_route->if_INVITE: sl_send_reply(404) \n");
  145.         sl_send_reply("404","Not here");
  146.         exit;
  147.         }
  148.  
  149.     xlogl("L_INFO", "[$rm] received in request_route: rewritehostport: 192.168.106.7:5060 \n");
  150.     $du = "sip:192.168.106.7:5060";
  151.     # forward the request now
  152.     route(RELAY);
  153. }
  154.  
  155. route[AUTH] {
  156.     #Step2: parse JSON using module-json and collect $avp(password) ($avp(password) = $json(property/user_secret);)
  157.     if (is_method("REGISTER") || from_uri==myself) {
  158.         # authenticate requests
  159.         sql_query("cb", "SELECT dir_params FROM astpp.sip_devices WHERE username = '$fU'", "ra");
  160.  
  161.         if ( $dbr(ra=>rows) > 0 ){
  162.             $var(i) = 0;
  163.             while( $var(i) < $dbr(ra=>rows) ) {
  164.                 $avp(password) = $dbr(ra=>[$var(i),0]);
  165.             }
  166.             $var(i) = $var(i) + 1;
  167.         }
  168.  
  169.         xlogl("Result string is $avp(password)");
  170.         jansson_get_field($avp(password), "password", "$avp(password)");
  171.         xlogl("[$rm] -> received Password: $avp(password) \n");
  172.  
  173.         sql_result_free("ra");
  174.  
  175.         #Step3: Auth module use pv_<auth-functions>
  176.         if (!pv_auth_check("$fU", "$avp(password)", 0, 1)) {
  177.             xlogl("[$rm] --> received pv_auth_check FAILED, sending auth_challenge \n");
  178.             auth_challenge("$fd", "0");
  179.             exit;
  180.         }
  181.         xlogl("[$rm] --> pv_auth_check SUCCESS, sending auth_challenge \n");
  182.         # user authenticated - remove auth header
  183.         if(!is_method("REGISTER|PUBLISH")) {
  184.             xlogl("[$rm] --> pv_auth_check SUCCESS, consume_credentials \n");
  185.             consume_credentials();
  186.         }
  187.     }
  188.     # if caller is not local subscriber, then check if it calls
  189.     # a local destination, otherwise deny, not an open relay here
  190.     if (from_uri!=myself && uri!=myself) {
  191.         xlogl("[$rm] --> 403 Not relaying for non-local subscriber \n");
  192.         sl_send_reply("403","Not relaying");
  193.         exit;
  194.     }
  195.     return;
  196. }
  197.  
  198. route[RELAY] {
  199.     if (!t_relay()) {
  200.         xlogl("L_ERROR", "Failed to RELAY transaction in route[RELAY] \n");
  201.         sl_reply_error();
  202.     }
  203. }
Add Comment
Please, Sign In to add comment