--- /etc/kamailio/kamailio.cfg 2018-12-13 20:25:12.117585656 +0200 +++ /tmp/kamailio.cfg 2018-12-13 20:41:22.000000000 +0200 @@ -2,6 +2,7 @@ #!define WITH_MYSQL #!define WITH_AUTH #!define WITH_USRLOCDB +#!define WITH_ASTERISK # # Kamailio (OpenSER) SIP Server v5.2 - default configuration script # - web: https://www.kamailio.org @@ -122,8 +123,9 @@ #!ifdef WITH_MYSQL # - database URL - used to connect to database server by modules such # as: auth_db, acc, usrloc, a.s.o. -#!ifndef DBURL -#!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio" +#!define DBURL "mysql://openser:openserrw@localhost/openser" +#!ifdef WITH_ASTERISK +#!define DBASTURL "mysql://asterisk:asterisk_password@localhost/asterisk" #!endif #!endif #!ifdef WITH_MULTIDOMAIN @@ -176,7 +178,7 @@ /* uncomment and configure the following line if you want Kamailio to * bind on a specific interface/port/proto (default bind on all available) */ -# listen=udp:10.0.0.10:5060 + listen=udp:192.168.1.230:5060 #!ifdef WITH_TLS enable_tls=yes @@ -213,6 +215,15 @@ voicemail.srv_port = "5060" desc "VoiceMail Port" #!endif + +#!ifdef WITH_ASTERISK +asterisk.bindip = "192.168.1.230" desc "Asterisk IP Address" +asterisk.bindport = "5080" desc "Asterisk Port" +kamailio.bindip = "192.168.178.230" desc "Kamailio IP Address" +kamailio.bindport = "5060" desc "Kamailio Port" +#!endif + + ####### Modules Section ######## /* set paths to location of modules */ @@ -316,7 +327,16 @@ modparam("rr", "enable_full_lr", 0) # do not append from tag to the RR (no need for this script) modparam("rr", "append_fromtag", 0) - +#!ifdef WITH_ASTERISK +modparam("rr", "append_fromtag", 1) +#!else +modparam("rr", "append_fromtag", 0) +#!endif +#!ifdef WITH_ASTERISK +modparam("rr", "append_fromtag", 1) +#!else +modparam("rr", "append_fromtag", 0) +#!endif # ----- registrar params ----- modparam("registrar", "method_filtering", 1) /* uncomment the next line to disable parallel forking via location */ @@ -369,6 +389,17 @@ modparam("auth_db", "password_column", "password") modparam("auth_db", "load_credentials", "") modparam("auth_db", "use_domain", MULTIDOMAIN) +#!ifdef WITH_ASTERISK +modparam("auth_db", "user_column", "name") +modparam("auth_db", "password_column", "sippasswd") +modparam("auth_db", "db_url", DBASTURL) +modparam("auth_db", "version_table", 0) +#!else +modparam("auth_db", "db_url", DBURL) +modparam("auth_db", "password_column", "password") +modparam("auth_db", "use_domain", MULTIDOMAIN) +#!endif + # ----- permissions params ----- #!ifdef WITH_IPAUTH @@ -643,6 +674,9 @@ if (!save("location")) { sl_reply_error(); } +#!ifdef WITH_ASTERISK + route(REGFWD); +#!endif exit; } @@ -664,6 +698,15 @@ route(SIPOUT); } #!endif +#!ifdef WITH_ASTERISK + if(is_method("INVITE") && (!route(FROMASTERISK))) { + # if new call from out there - send to Asterisk + # - non-INVITE request are routed directly by Kamailio + # - traffic from Asterisk is routed also directy by Kamailio + route(TOASTERISK); + exit; + } +#!endif $avp(oexten) = $rU; if (!lookup("location")) { @@ -728,6 +771,11 @@ # IP authorization and user authentication route[AUTH] { #!ifdef WITH_AUTH +#!ifdef WITH_ASTERISK + # do not auth traffic from Asterisk - trusted! + if(route(FROMASTERISK)) + return; +#!endif #!ifdef WITH_IPAUTH if((!is_method("REGISTER")) && allow_source_address()) { @@ -760,6 +808,18 @@ sl_send_reply("403","Not relaying"); exit; } +#!ifdef WITH_ASTERISK + if (!auth_check("$fd", "sipusers", "1")) { +#!else + if (!auth_check("$fd", "subscriber", "1")) { +#!endif + auth_challenge("$fd", "0"); + exit; + } + # user authenticated - remove auth header + if(!is_method("REGISTER|PUBLISH")) + consume_credentials(); + } #!endif return; @@ -971,3 +1031,40 @@ } #!endif } +#!ifdef WITH_ASTERISK +# Test if coming from Asterisk +route[FROMASTERISK] { + if($si==$sel(cfg_get.asterisk.bindip) + && $sp==$sel(cfg_get.asterisk.bindport)) + return 1; + return -1; +} + +# Send to Asterisk +route[TOASTERISK] { + $du = "sip:" + $sel(cfg_get.asterisk.bindip) + ":" + + $sel(cfg_get.asterisk.bindport); + route(RELAY); + exit; +} + +# Forward REGISTER to Asterisk +route[REGFWD] { + if(!is_method("REGISTER")) + { + return; + } + $var(rip) = $sel(cfg_get.asterisk.bindip); + $uac_req(method)="REGISTER"; + $uac_req(ruri)="sip:" + $var(rip) + ":" + $sel(cfg_get.asterisk.bindport); + $uac_req(furi)="sip:" + $au + "@" + $var(rip); + $uac_req(turi)="sip:" + $au + "@" + $var(rip); + $uac_req(hdrs)="Contact: \r\n"; + if($sel(contact.expires) != $null) + $uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $sel(contact.expires) + "\r\n"; + else + $uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $hdr(Expires) + "\r\n"; + uac_req_send(); +} +#!endif +