1. #!KAMAILIO
  2. #!define WITH_MYSQL
  3. #!define WITH_AUTH
  4. #!define WITH_USRLOCDB
  5. #!define WITH_FREESWITCH
  6. #!define WITH_DEBUG
  7. # Kamailio (OpenSER) SIP Server v4.3 - default configuration script
  8. # - web: http://www.kamailio.org
  9. # - git: http://sip-router.org
  10. #
  11. # Direct your questions about this file to: <[email protected]>
  12. #
  13. # Refer to the Core CookBook at http://www.kamailio.org/wiki/
  14. # for an explanation of possible statements, functions and parameters.
  15. #
  16. # Several features can be enabled using '#!define WITH_FEATURE' directives:
  17. #
  18. # *** To run in debug mode:
  19. # - define WITH_DEBUG
  20. #
  21. # *** To enable mysql:
  22. # - define WITH_MYSQL
  23. #
  24. # *** To enable authentication execute:
  25. # - enable mysql
  26. # - define WITH_AUTH
  27. # - add users using 'kamctl'
  28. #
  29. # *** To enable IP authentication execute:
  30. # - enable mysql
  31. # - enable authentication
  32. # - define WITH_IPAUTH
  33. # - add IP addresses with group id '1' to 'address' table
  34. #
  35. # *** To enable persistent user location execute:
  36. # - enable mysql
  37. # - define WITH_USRLOCDB
  38. debug=2
  39. # *** To enable presence server execute:
  40. # - enable mysql
  41. # - define WITH_PRESENCE
  42. #
  43. # *** To enable nat traversal execute:
  44. # - define WITH_NAT
  45. # - install RTPProxy: http://www.rtpproxy.org
  46. # - start RTPProxy:
  47. # rtpproxy -l _your_public_ip_ -s udp:localhost:7722
  48. # - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING
  49. #
  50. # *** To enable PSTN gateway routing execute:
  51. # - define WITH_PSTN
  52. # - set the value of pstn.gw_ip
  53. # - check route[PSTN] for regexp routing condition
  54. #
  55. # *** To enable database aliases lookup execute:
  56. # - enable mysql
  57. # - define WITH_ALIASDB
  58. #
  59. # *** To enable speed dial lookup execute:
  60. # - enable mysql
  61. # - define WITH_SPEEDDIAL
  62. #
  63. # *** To enable multi-domain support execute:
  64. # - enable mysql
  65. # - define WITH_MULTIDOMAIN
  66. #
  67. # *** To enable TLS support execute:
  68. # - adjust CFGDIR/tls.cfg as needed
  69. # - define WITH_TLS
  70. #
  71. # *** To enable XMLRPC support execute:
  72. # - define WITH_XMLRPC
  73. # - adjust route[XMLRPC] for access policy
  74. #
  75. # *** To enable anti-flood detection execute:
  76. # - adjust pike and htable=>ipban settings as needed (default is
  77. # block if more than 16 requests in 2 seconds and ban for 300 seconds)
  78. # - define WITH_ANTIFLOOD
  79. #
  80. # *** To block 3XX redirect replies execute:
  81. # - define WITH_BLOCK3XX
  82. #
  83. # *** To enable VoiceMail routing execute:
  84. # - define WITH_VOICEMAIL
  85. # - set the value of voicemail.srv_ip
  86. # - adjust the value of voicemail.srv_port
  87. #
  88. # *** To enhance accounting execute:
  89. # - enable mysql
  90. # - define WITH_ACCDB
  91. # - add following columns to database
  92. #!ifdef ACCDB_COMMENT
  93. ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  94. ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  95. ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
  96. ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  97. ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  98. ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  99. ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
  100. ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
  101. ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
  102. ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
  103. ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
  104. ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
  105. #!endif
  106. ####### Include Local Config If Exists #########
  107. import_file "kamailio-local.cfg"
  108. ####### Defined Values #########
  109. # *** Value defines - IDs used later in config
  110. #!ifdef WITH_MYSQL
  111. # - database URL - used to connect to database server by modules such
  112. # as: auth_db, acc, usrloc, a.s.o.
  113. #!ifndef DBURL
  114. #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
  115. #!endif
  116. #!endif
  117. #!ifdef WITH_MULTIDOMAIN
  118. # - the value for 'use_domain' parameters
  119. #!define MULTIDOMAIN 1
  120. #!else
  121. #!define MULTIDOMAIN 0
  122. #!endif
  123. # - flags
  124. # FLT_ - per transaction (message) flags
  125. # FLB_ - per branch flags
  126. #!define FLT_ACC 1
  127. #!define FLT_ACCMISSED 2
  128. #!define FLT_ACCFAILED 3
  129. #!define FLT_NATS 5
  130. #!define FLB_NATB 6
  131. #!define FLB_NATSIPPING 7
  132. ####### Global Parameters #########
  133. ### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR
  134. #!ifdef WITH_DEBUG
  135. debug=4
  136. log_stderror=yes
  137. #!else
  138. debug=2
  139. log_stderror=no
  140. #!endif
  141. memdbg=5
  142. memlog=5
  143. log_facility=LOG_LOCAL0
  144. fork=yes
  145. children=4
  146. /* uncomment the next line to disable TCP (default on) */
  147. #disable_tcp=yes
  148. /* uncomment the next line to disable the auto discovery of local aliases
  149. based on reverse DNS on IPs (default on) */
  150. #auto_aliases=no
  151. /* add local domain aliases */
  152. #alias="sip.mydomain.com"
  153. /* uncomment and configure the following line if you want Kamailio to
  154. bind on a specific interface/port/proto (default bind on all available) */
  155. #listen=udp:10.0.0.10:5060
  156. /* port to listen to
  157. * - can be specified more than once if needed to listen on many ports */
  158. port=5060
  159. #!ifdef WITH_TLS
  160. enable_tls=yes
  161. #!endif
  162. # life time of TCP connection when there is no traffic
  163. # - a bit higher than registration expires to cope with UA behind NAT
  164. tcp_connection_lifetime=3605
  165. ####### Custom Parameters #########
  166. #!ifdef WITH_FREESWITCH
  167. freeswitch.bindip = "192.168.2.131" desc "FreeSWITCH IP Address"
  168. freeswitch.bindport = "5090" desc "FreeSWITCH Port"
  169. #!endif
  170. # These parameters can be modified runtime via RPC interface
  171. # - see the documentation of 'cfg_rpc' module.
  172. #
  173. # Format: group.id = value 'desc' description
  174. # Access: $sel(cfg_get.group.id) or @cfg_get.group.id
  175. #
  176. #!ifdef WITH_PSTN
  177. # PSTN GW Routing
  178. #
  179. # - pstn.gw_ip: valid IP or hostname as string value, example:
  180. # pstn.gw_ip = "10.0.0.101" desc "My PSTN GW Address"
  181. #
  182. # - by default is empty to avoid misrouting
  183. pstn.gw_ip = "" desc "PSTN GW Address"
  184. pstn.gw_port = "" desc "PSTN GW Port"
  185. #!endif
  186. #!ifdef WITH_VOICEMAIL
  187. # VoiceMail Routing on offline, busy or no answer
  188. #
  189. # - by default Voicemail server IP is empty to avoid misrouting
  190. voicemail.srv_ip = "" desc "VoiceMail IP Address"
  191. voicemail.srv_port = "5060" desc "VoiceMail Port"
  192. #!endif
  193. ####### Modules Section ########
  194. # set paths to location of modules (to sources or installation folders)
  195. #!ifdef WITH_SRCPATH
  196. mpath="modules/"
  197. #!else
  198. mpath="/usr/lib/x86_64-linux-gnu/kamailio/modules/"
  199. #!endif
  200. #!ifdef WITH_MYSQL
  201. loadmodule "db_mysql.so"
  202. #!endif
  203. loadmodule "mi_fifo.so"
  204. loadmodule "kex.so"
  205. loadmodule "corex.so"
  206. loadmodule "tm.so"
  207. loadmodule "tmx.so"
  208. loadmodule "sl.so"
  209. loadmodule "rr.so"
  210. loadmodule "pv.so"
  211. loadmodule "maxfwd.so"
  212. loadmodule "usrloc.so"
  213. loadmodule "registrar.so"
  214. loadmodule "textops.so"
  215. loadmodule "siputils.so"
  216. loadmodule "xlog.so"
  217. loadmodule "sanity.so"
  218. loadmodule "ctl.so"
  219. loadmodule "cfg_rpc.so"
  220. loadmodule "mi_rpc.so"
  221. loadmodule "acc.so"
  222. #!ifdef WITH_AUTH
  223. loadmodule "auth.so"
  224. loadmodule "auth_db.so"
  225. #!ifdef WITH_IPAUTH
  226. loadmodule "permissions.so"
  227. #!endif
  228. #!endif
  229. #!ifdef WITH_ALIASDB
  230. loadmodule "alias_db.so"
  231. #!endif
  232. #!ifdef WITH_SPEEDDIAL
  233. loadmodule "speeddial.so"
  234. #!endif
  235. #!ifdef WITH_MULTIDOMAIN
  236. loadmodule "domain.so"
  237. #!endif
  238. #!ifdef WITH_PRESENCE
  239. loadmodule "presence.so"
  240. loadmodule "presence_xml.so"
  241. #!endif
  242. #!ifdef WITH_NAT
  243. loadmodule "nathelper.so"
  244. loadmodule "rtpproxy.so"
  245. #!endif
  246. #!ifdef WITH_TLS
  247. loadmodule "tls.so"
  248. #!endif
  249. #!ifdef WITH_ANTIFLOOD
  250. loadmodule "htable.so"
  251. loadmodule "pike.so"
  252. #!endif
  253. #!ifdef WITH_XMLRPC
  254. loadmodule "xmlrpc.so"
  255. #!endif
  256. #!ifdef WITH_DEBUG
  257. loadmodule "debugger.so"
  258. #!endif
  259. # ----------------- setting module-specific parameters ---------------
  260. # ----- mi_fifo params -----
  261. #modparam("mi_fifo", "fifo_name", "/var/run/kamailio/kamailio_fifo")
  262. # ----- ctl params -----
  263. #modparam("ctl", "binrpc", "unix:/var/run/kamailio/kamailio_ctl")
  264. # ----- tm params -----
  265. # auto-discard branches from previous serial forking leg
  266. modparam("tm", "failure_reply_mode", 3)
  267. # default retransmission timeout: 30sec
  268. modparam("tm", "fr_timer", 30000)
  269. # default invite retransmission timeout after 1xx: 120sec
  270. modparam("tm", "fr_inv_timer", 120000)
  271. # ----- rr params -----
  272. # set next param to 1 to add value to ;lr param (helps with some UAs)
  273. modparam("rr", "enable_full_lr", 0)
  274. # do not append from tag to the RR (no need for this script)
  275. modparam("rr", "append_fromtag", 0)
  276. # ----- registrar params -----
  277. modparam("registrar", "method_filtering", 1)
  278. /* uncomment the next line to disable parallel forking via location */
  279. # modparam("registrar", "append_branches", 0)
  280. /* uncomment the next line not to allow more than 10 contacts per AOR */
  281. #modparam("registrar", "max_contacts", 10)
  282. # max value for expires of registrations
  283. modparam("registrar", "max_expires", 3600)
  284. # set it to 1 to enable GRUU
  285. modparam("registrar", "gruu_enabled", 0)
  286. # ----- acc params -----
  287. /* what special events should be accounted ? */
  288. modparam("acc", "early_media", 0)
  289. modparam("acc", "report_ack", 0)
  290. modparam("acc", "report_cancels", 0)
  291. /* by default ww do not adjust the direct of the sequential requests.
  292. if you enable this parameter, be sure the enable "append_fromtag"
  293. in "rr" module */
  294. modparam("acc", "detect_direction", 0)
  295. /* account triggers (flags) */
  296. modparam("acc", "log_flag", FLT_ACC)
  297. modparam("acc", "log_missed_flag", FLT_ACCMISSED)
  298. modparam("acc", "log_extra",
  299. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  300. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  301. modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
  302. /* enhanced DB accounting */
  303. #!ifdef WITH_ACCDB
  304. modparam("acc", "db_flag", FLT_ACC)
  305. modparam("acc", "db_missed_flag", FLT_ACCMISSED)
  306. modparam("acc", "db_url", DBURL)
  307. modparam("acc", "db_extra",
  308. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  309. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  310. #!endif
  311. # ----- usrloc params -----
  312. /* enable DB persistency for location entries */
  313. #!ifdef WITH_USRLOCDB
  314. modparam("usrloc", "db_url", DBURL)
  315. modparam("usrloc", "db_mode", 2)
  316. modparam("usrloc", "use_domain", MULTIDOMAIN)
  317. #!endif
  318. # ----- auth_db params -----
  319. #!ifdef WITH_AUTH
  320. modparam("auth_db", "db_url", DBURL)
  321. modparam("auth_db", "calculate_ha1", yes)
  322. modparam("auth_db", "password_column", "password")
  323. modparam("auth_db", "load_credentials", "")
  324. modparam("auth_db", "use_domain", MULTIDOMAIN)
  325. # ----- permissions params -----
  326. #!ifdef WITH_IPAUTH
  327. modparam("permissions", "db_url", DBURL)
  328. modparam("permissions", "db_mode", 1)
  329. #!endif
  330. #!endif
  331. # ----- alias_db params -----
  332. #!ifdef WITH_ALIASDB
  333. modparam("alias_db", "db_url", DBURL)
  334. modparam("alias_db", "use_domain", MULTIDOMAIN)
  335. #!endif
  336. # ----- speeddial params -----
  337. #!ifdef WITH_SPEEDDIAL
  338. modparam("speeddial", "db_url", DBURL)
  339. modparam("speeddial", "use_domain", MULTIDOMAIN)
  340. #!endif
  341. # ----- domain params -----
  342. #!ifdef WITH_MULTIDOMAIN
  343. modparam("domain", "db_url", DBURL)
  344. # register callback to match myself condition with domains list
  345. modparam("domain", "register_myself", 1)
  346. #!endif
  347. #!ifdef WITH_PRESENCE
  348. # ----- presence params -----
  349. modparam("presence", "db_url", DBURL)
  350. # ----- presence_xml params -----
  351. modparam("presence_xml", "db_url", DBURL)
  352. modparam("presence_xml", "force_active", 1)
  353. #!endif
  354. #!ifdef WITH_NAT
  355. # ----- rtpproxy params -----
  356. modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
  357. # ----- nathelper params -----
  358. modparam("nathelper", "natping_interval", 30)
  359. modparam("nathelper", "ping_nated_only", 1)
  360. modparam("nathelper", "sipping_bflag", FLB_NATSIPPING)
  361. modparam("nathelper", "sipping_from", "sip:[email protected]")
  362. # params needed for NAT traversal in other modules
  363. modparam("nathelper|registrar", "received_avp", "$avp(RECEIVED)")
  364. modparam("usrloc", "nat_bflag", FLB_NATB)
  365. #!endif
  366. #!ifdef WITH_TLS
  367. # ----- tls params -----
  368. modparam("tls", "config", "/etc/kamailio/tls.cfg")
  369. #!endif
  370. #!ifdef WITH_ANTIFLOOD
  371. # ----- pike params -----
  372. modparam("pike", "sampling_time_unit", 2)
  373. modparam("pike", "reqs_density_per_unit", 16)
  374. modparam("pike", "remove_latency", 4)
  375. # ----- htable params -----
  376. # ip ban htable with autoexpire after 5 minutes
  377. modparam("htable", "htable", "ipban=>size=8;autoexpire=300;")
  378. #!endif
  379. #!ifdef WITH_XMLRPC
  380. # ----- xmlrpc params -----
  381. modparam("xmlrpc", "route", "XMLRPC");
  382. modparam("xmlrpc", "url_match", "^/RPC")
  383. #!endif
  384. #!ifdef WITH_DEBUG
  385. # ----- debugger params -----
  386. modparam("debugger", "cfgtrace", 1)
  387. modparam("debugger", "log_level_name", "exec")
  388. #!endif
  389. ####### Routing Logic ########
  390. # Main SIP request routing logic
  391. # - processing of any incoming SIP request starts with this route
  392. # - note: this is the same as route { ... }
  393. request_route {
  394. # per request initial checks
  395. route(REQINIT);
  396. # NAT detection
  397. route(NATDETECT);
  398. # CANCEL processing
  399. if (is_method("CANCEL")) {
  400. if (t_check_trans()) {
  401. route(RELAY);
  402. }
  403. exit;
  404. }
  405. # handle requests within SIP dialogs
  406. route(WITHINDLG);
  407. ### only initial requests (no To tag)
  408. # handle retransmissions
  409. if(t_precheck_trans()) {
  410. t_check_trans();
  411. exit;
  412. }
  413. t_check_trans();
  414. # authentication
  415. route(AUTH);
  416. # record routing for dialog forming requests (in case they are routed)
  417. # - remove preloaded route headers
  418. remove_hf("Route");
  419. if (is_method("INVITE|SUBSCRIBE"))
  420. record_route();
  421. # account only INVITEs
  422. if (is_method("INVITE")) {
  423. setflag(FLT_ACC); # do accounting
  424. }
  425. # dispatch requests to foreign domains
  426. route(SIPOUT);
  427. ### requests for my local domains
  428. # handle presence related requests
  429. route(PRESENCE);
  430. # handle registrations
  431. route(REGISTRAR);
  432. if ($rU==$null) {
  433. # request with no Username in RURI
  434. sl_send_reply("484","Address Incomplete");
  435. exit;
  436. }
  437. # dispatch destinations to PSTN
  438. route(PSTN);
  439. # user location service
  440. route(LOCATION);
  441. }
  442. # Wrapper for relaying requests
  443. route[RELAY] {
  444. # enable additional event routes for forwarded requests
  445. # - serial forking, RTP relaying handling, a.s.o.
  446. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  447. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  448. }
  449. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  450. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  451. }
  452. if (is_method("INVITE")) {
  453. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  454. }
  455. if (!t_relay()) {
  456. sl_reply_error();
  457. }
  458. exit;
  459. }
  460. # Per SIP request initial checks
  461. route[REQINIT] {
  462. #!ifdef WITH_ANTIFLOOD
  463. # flood detection from same IP and traffic ban for a while
  464. # be sure you exclude checking trusted peers, such as pstn gateways
  465. # - local host excluded (e.g., loop to self)
  466. if(src_ip!=myself) {
  467. if($sht(ipban=>$si)!=$null) {
  468. # ip is already blocked
  469. xdbg("request from blocked IP - $rm from $fu (IP:$si:$sp)\n");
  470. exit;
  471. }
  472. if (!pike_check_req()) {
  473. xlog("L_ALERT","ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n");
  474. $sht(ipban=>$si) = 1;
  475. exit;
  476. }
  477. }
  478. if($ua =~ "friendly-scanner") {
  479. sl_send_reply("200", "OK");
  480. exit;
  481. }
  482. #!endif
  483. if (!mf_process_maxfwd_header("10")) {
  484. sl_send_reply("483","Too Many Hops");
  485. exit;
  486. }
  487. if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  488. sl_send_reply("200","Keepalive");
  489. exit;
  490. }
  491. if(!sanity_check("1511", "7")) {
  492. xlog("Malformed SIP message from $si:$sp\n");
  493. exit;
  494. }
  495. }
  496. # Handle requests within SIP dialogs
  497. route[WITHINDLG] {
  498. if (!has_totag()) return;
  499. # sequential request withing a dialog should
  500. # take the path determined by record-routing
  501. if (loose_route()) {
  502. route(DLGURI);
  503. if (is_method("BYE")) {
  504. setflag(FLT_ACC); # do accounting ...
  505. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  506. }
  507. else if ( is_method("ACK") ) {
  508. # ACK is forwarded statelessy
  509. route(NATMANAGE);
  510. }
  511. else if ( is_method("NOTIFY") ) {
  512. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  513. record_route();
  514. }
  515. route(RELAY);
  516. exit;
  517. }
  518. if (is_method("SUBSCRIBE") && uri == myself) {
  519. # in-dialog subscribe requests
  520. route(PRESENCE);
  521. exit;
  522. }
  523. if ( is_method("ACK") ) {
  524. if ( t_check_trans() ) {
  525. # no loose-route, but stateful ACK;
  526. # must be an ACK after a 487
  527. # or e.g. 404 from upstream server
  528. route(RELAY);
  529. exit;
  530. } else {
  531. # ACK without matching transaction ... ignore and discard
  532. exit;
  533. }
  534. }
  535. sl_send_reply("404","Not here");
  536. exit;
  537. }
  538. # Handle SIP registrations
  539. route[REGISTRAR] {
  540. if (!is_method("REGISTER")) return;
  541. if(isflagset(FLT_NATS)) {
  542. setbflag(FLB_NATB);
  543. #!ifdef WITH_NATSIPPING
  544. # do SIP NAT pinging
  545. setbflag(FLB_NATSIPPING);
  546. #!endif
  547. }
  548. if (!save("location"))
  549. sl_reply_error();
  550. exit;
  551. }
  552. # User location service
  553. route[LOCATION] {
  554. #!ifdef WITH_SPEEDDIAL
  555. # search for short dialing - 2-digit extension
  556. if($rU=~"^[0-9][0-9]$")
  557. if(sd_lookup("speed_dial"))
  558. route(SIPOUT);
  559. #!endif
  560. #!ifdef WITH_ALIASDB
  561. # search in DB-based aliases
  562. if(alias_db_lookup("dbaliases"))
  563. route(SIPOUT);
  564. #!endif
  565. $avp(oexten) = $rU;
  566. if (!lookup("location")) {
  567. $var(rc) = $rc;
  568. route(TOVOICEMAIL);
  569. t_newtran();
  570. switch ($var(rc)) {
  571. case -1:
  572. case -3:
  573. send_reply("404", "Not Found");
  574. exit;
  575. case -2:
  576. send_reply("405", "Method Not Allowed");
  577. exit;
  578. }
  579. }
  580. # when routing via usrloc, log the missed calls also
  581. if (is_method("INVITE")) {
  582. setflag(FLT_ACCMISSED);
  583. }
  584. route(RELAY);
  585. exit;
  586. }
  587. # Presence server processing
  588. route[PRESENCE] {
  589. if(!is_method("PUBLISH|SUBSCRIBE"))
  590. return;
  591. if(is_method("SUBSCRIBE") && $hdr(Event)=="message-summary") {
  592. route(TOVOICEMAIL);
  593. # returns here if no voicemail server is configured
  594. sl_send_reply("404", "No voicemail service");
  595. exit;
  596. }
  597. #!ifdef WITH_PRESENCE
  598. if (!t_newtran()) {
  599. sl_reply_error();
  600. exit;
  601. }
  602. if(is_method("PUBLISH")) {
  603. handle_publish();
  604. t_release();
  605. } else if(is_method("SUBSCRIBE")) {
  606. handle_subscribe();
  607. t_release();
  608. }
  609. exit;
  610. #!endif
  611. # if presence enabled, this part will not be executed
  612. if (is_method("PUBLISH") || $rU==$null) {
  613. sl_send_reply("404", "Not here");
  614. exit;
  615. }
  616. return;
  617. }
  618. # IP authorization and user authentication
  619. route[AUTH] {
  620. #!ifdef WITH_AUTH
  621. #!ifdef WITH_IPAUTH
  622. if((!is_method("REGISTER")) && allow_source_address()) {
  623. # source IP allowed
  624. return;
  625. }
  626. #!endif
  627. if (is_method("REGISTER") || from_uri==myself)
  628. {
  629. # authenticate requests
  630. if (!auth_check("$fd", "subscriber", "1")) {
  631. auth_challenge("$fd", "0");
  632. exit;
  633. }
  634. # user authenticated - remove auth header
  635. if(!is_method("REGISTER|PUBLISH"))
  636. consume_credentials();
  637. }
  638. # if caller is not local subscriber, then check if it calls
  639. # a local destination, otherwise deny, not an open relay here
  640. if (from_uri!=myself && uri!=myself) {
  641. sl_send_reply("403","Not relaying");
  642. exit;
  643. }
  644. #!endif
  645. return;
  646. }
  647. # Caller NAT detection
  648. route[NATDETECT] {
  649. #!ifdef WITH_NAT
  650. force_rport();
  651. if (nat_uac_test("19")) {
  652. if (is_method("REGISTER")) {
  653. fix_nated_register();
  654. } else {
  655. if(is_first_hop())
  656. set_contact_alias();
  657. }
  658. setflag(FLT_NATS);
  659. }
  660. #!endif
  661. return;
  662. }
  663. # RTPProxy control and signaling updates for NAT traversal
  664. route[NATMANAGE] {
  665. #!ifdef WITH_NAT
  666. if (is_request()) {
  667. if(has_totag()) {
  668. if(check_route_param("nat=yes")) {
  669. setbflag(FLB_NATB);
  670. }
  671. }
  672. }
  673. if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB)))
  674. return;
  675. rtpproxy_manage("co");
  676. if (is_request()) {
  677. if (!has_totag()) {
  678. if(t_is_branch_route()) {
  679. add_rr_param(";nat=yes");
  680. }
  681. }
  682. }
  683. if (is_reply()) {
  684. if(isbflagset(FLB_NATB)) {
  685. if(is_first_hop())
  686. set_contact_alias();
  687. }
  688. }
  689. #!endif
  690. return;
  691. }
  692. # URI update for dialog requests
  693. route[DLGURI] {
  694. #!ifdef WITH_NAT
  695. if(!isdsturiset()) {
  696. handle_ruri_alias();
  697. }
  698. #!endif
  699. return;
  700. }
  701. # Routing to foreign domains
  702. route[SIPOUT] {
  703. if (uri==myself) return;
  704. append_hf("P-hint: outbound\r\n");
  705. route(RELAY);
  706. exit;
  707. }
  708. # PSTN GW routing
  709. route[PSTN] {
  710. #!ifdef WITH_PSTN
  711. # check if PSTN GW IP is defined
  712. if (strempty($sel(cfg_get.pstn.gw_ip))) {
  713. xlog("SCRIPT: PSTN routing enabled but pstn.gw_ip not defined\n");
  714. return;
  715. }
  716. # route to PSTN dialed numbers starting with '+' or '00'
  717. # (international format)
  718. # - update the condition to match your dialing rules for PSTN routing
  719. if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$"))
  720. return;
  721. # only local users allowed to call
  722. if(from_uri!=myself) {
  723. sl_send_reply("403", "Not Allowed");
  724. exit;
  725. }
  726. if (strempty($sel(cfg_get.pstn.gw_port))) {
  727. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);
  728. } else {
  729. $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip) + ":"
  730. + $sel(cfg_get.pstn.gw_port);
  731. }
  732. route(RELAY);
  733. exit;
  734. #!endif
  735. return;
  736. }
  737. # XMLRPC routing
  738. #!ifdef WITH_XMLRPC
  739. route[XMLRPC] {
  740. # allow XMLRPC from localhost
  741. if ((method=="POST" || method=="GET")
  742. && (src_ip==127.0.0.1)) {
  743. # close connection only for xmlrpclib user agents (there is a bug in
  744. # xmlrpclib: it waits for EOF before interpreting the response).
  745. if ($hdr(User-Agent) =~ "xmlrpclib")
  746. set_reply_close();
  747. set_reply_no_connect();
  748. dispatch_rpc();
  749. exit;
  750. }
  751. send_reply("403", "Forbidden");
  752. exit;
  753. }
  754. #!endif
  755. # Routing to voicemail server
  756. route[TOVOICEMAIL] {
  757. #!ifdef WITH_VOICEMAIL
  758. if(!is_method("INVITE|SUBSCRIBE"))
  759. return;
  760. # check if VoiceMail server IP is defined
  761. if (strempty($sel(cfg_get.voicemail.srv_ip))) {
  762. xlog("SCRIPT: VoiceMail routing enabled but IP not defined\n");
  763. return;
  764. }
  765. if(is_method("INVITE")) {
  766. if($avp(oexten)==$null)
  767. return;
  768. $ru = "sip:" + $avp(oexten) + "@" + $sel(cfg_get.voicemail.srv_ip)
  769. + ":" + $sel(cfg_get.voicemail.srv_port);
  770. } else {
  771. if($rU==$null)
  772. return;
  773. $ru = "sip:" + $rU + "@" + $sel(cfg_get.voicemail.srv_ip)
  774. + ":" + $sel(cfg_get.voicemail.srv_port);
  775. }
  776. route(RELAY);
  777. exit;
  778. #!endif
  779. return;
  780. }
  781. # Manage outgoing branches
  782. branch_route[MANAGE_BRANCH] {
  783. xdbg("new branch [$T_branch_idx] to $ru\n");
  784. route(NATMANAGE);
  785. }
  786. # Manage incoming replies
  787. onreply_route[MANAGE_REPLY] {
  788. xdbg("incoming reply\n");
  789. if(status=~"[12][0-9][0-9]")
  790. route(NATMANAGE);
  791. }
  792. # Manage failure routing cases
  793. failure_route[MANAGE_FAILURE] {
  794. route(NATMANAGE);
  795. if (t_is_canceled()) {
  796. exit;
  797. }
  798. #!ifdef WITH_BLOCK3XX
  799. # block call redirect based on 3xx replies.
  800. if (t_check_status("3[0-9][0-9]")) {
  801. t_reply("404","Not found");
  802. exit;
  803. }
  804. #!endif
  805. #!ifdef WITH_VOICEMAIL
  806. # serial forking
  807. # - route to voicemail on busy or no answer (timeout)
  808. if (t_check_status("486|408")) {
  809. $du = $null;
  810. route(TOVOICEMAIL);
  811. exit;
  812. }
  813. #!endif
  814. }