sip-router.cfg 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #
  2. # $Id$
  3. #
  4. # Example configuration file (simpler than ser-oob.cfg, but more
  5. # complex then ser-basic.cfg).
  6. #
  7. # First start SER sample config script with:
  8. # database, accounting, authentication, multi-domain support
  9. # PSTN GW section, named flags, named routes, global-,
  10. # domain- and user-preferences with AVPs
  11. # Several of these features are only here for demonstration purpose
  12. # what can be achieved with the SER config script language.
  13. #
  14. # If you look for a simpler version with a lot less dependencies
  15. # please refer to the ser-basic.cfg file in your SER distribution.
  16. #
  17. # If you look for documentation, try http://sip-router.org/wiki/.
  18. # The right mailing lists for questions about this file is
  19. # <[email protected]>.
  20. # To get this config running you need to execute the following commands
  21. # with the new serctl (the capital word are just place holders)
  22. # - ser_ctl domain add DOMAINNAME
  23. # - ser_ctl user add USERNAME@DOMAINNAME -p PASSWORD
  24. # ser_ctl can be obtained from
  25. # http://ftp.iptel.org/pub/serctl/daily-snapshots/.
  26. #
  27. # If you want to have PID header for your user
  28. # - ser_attr add uid=UID asserted_id="PID"
  29. # If you want to have gateway support
  30. # - ser_db add attr_types name=gw_ip rich_type=string raw_type=2 description="The gateway IP for the default ser.cfg" default_flags=33
  31. # - ser_attr add global gw_ip=GATEWAY-IP
  32. # ----------- Global Defines / Extra Features -------------------------------
  33. # (can be enabled either by uncommenting the corresponding #!define
  34. # statement or by starting with -A WITH_<FEATURE_NAME>, e.g.
  35. # ser -A WITH_TLS -f /etc/ser/ser-oob.cfg )
  36. # enable TLS
  37. ##!define WITH_TLS
  38. # started from compile directory (not installed)
  39. ##!define LOCAL_TEST_RUN
  40. # xmlrpc allowed subnets (if defined XMLRPC requests with source ip matching
  41. # this network addresses will be allowed, if no XMLRPC_ALLOWED_SUBNETx is
  42. # defined only requests coming from localhost will be allowed).
  43. # E.g.: ser -A XMLRPC_ALLOW_NET1=192.168.1.0/24 -f ser-oob.cfg
  44. ##!define XMLRPC_ALLOW_NET1 192.168.0.0/16
  45. ##!define XMLRPC_ALLOW_NET2 10.0.0.0/255.0.0.0
  46. ##!define XMLRPC_ALLOW_NET3 172.16.0.0/12
  47. # ----------- global configuration parameters ------------------------
  48. debug=2 # debug level (cmd line: -dddddddddd)
  49. #memdbg=10 # memory debug log level
  50. #memlog=10 # memory statistics log level
  51. #log_facility=LOG_LOCAL0 # sets the facility used for logging (see syslog(3))
  52. /* Uncomment these lines to enter debugging mode
  53. fork=no
  54. log_stderror=yes
  55. */
  56. check_via=no # (cmd. line: -v)
  57. dns=no # (cmd. line: -r)
  58. rev_dns=no # (cmd. line: -R)
  59. #port=5060
  60. #children=4
  61. #user=ser
  62. #group=ser
  63. #disable_core=yes #disables core dumping
  64. #open_fd_limit=1024 # sets the open file descriptors limit
  65. #mhomed=yes # useful for multihomed hosts, small performance penalty
  66. #disable_tcp=yes
  67. #tcp_accept_aliases=yes # accepts the tcp alias via option (see NEWS)
  68. sip_warning=yes
  69. #!ifdef WITH_TLS
  70. enable_tls=yes
  71. #!endif
  72. #
  73. # ------------------ module loading ----------------------------------
  74. #!ifdef LOCAL_TEST_RUN
  75. loadpath "modules:modules_s"
  76. #!else
  77. loadpath "/usr/lib/ser/modules:/usr/lib/ser/modules_s"
  78. #!endif
  79. # load a SQL database for authentication, domains, user AVPs etc.
  80. loadmodule "db_mysql"
  81. loadmodule "tm"
  82. loadmodule "sl"
  83. loadmodule "rr"
  84. loadmodule "maxfwd"
  85. loadmodule "usrloc"
  86. loadmodule "registrar"
  87. loadmodule "xlog"
  88. loadmodule "textops"
  89. loadmodule "ctl"
  90. loadmodule "cfg_rpc"
  91. loadmodule "auth"
  92. loadmodule "auth_db"
  93. loadmodule "gflags"
  94. loadmodule "domain"
  95. loadmodule "uri_db"
  96. loadmodule "avp"
  97. loadmodule "avp_db"
  98. loadmodule "acc_db"
  99. loadmodule "xmlrpc"
  100. #!ifdef WITH_TLS
  101. loadmodule "tls"
  102. #!endif
  103. # ----------------- setting script FLAGS -----------------------------
  104. flags
  105. FLAG_ACC : 1, # include message in accounting
  106. FLAG_FAILUREROUTE : 2; # we are operating from a failure route
  107. avpflags
  108. dialog_cookie; # handled by rr module
  109. # ----------------- setting module-specific parameters ---------------
  110. # specify the path to you database here
  111. modparam("acc_db|auth_db|avp_db|domain|gflags|usrloc|uri_db", "db_url", "mysql://ser:[email protected]/ser")
  112. # -- usrloc params --
  113. # as we use the database anyway we will use it for usrloc as well
  114. modparam("usrloc", "db_mode", 1)
  115. # -- auth params --
  116. modparam("auth_db", "calculate_ha1", yes)
  117. modparam("auth_db", "plain_password_column", "password")
  118. # -- rr params --
  119. # add value to ;lr param to make some broken UAs happy
  120. modparam("rr", "enable_full_lr", 1)
  121. #
  122. # limit the length of the AVP cookie to only necessary ones
  123. modparam("rr", "cookie_filter", "(account)")
  124. #
  125. # you probably do not want that someone can simply read and change
  126. # the AVP cookie in your Routes, thus should really change this
  127. # secret value below
  128. modparam("rr", "cookie_secret", "MyRRAVPcookiesecret")
  129. # -- gflags params --
  130. # load the global AVPs
  131. modparam("gflags", "load_global_attrs", 1)
  132. # -- domain params --
  133. # load the domain AVPs
  134. modparam("domain", "load_domain_attrs", 1)
  135. # -- ctl params --
  136. # by default ctl listens on unixs:/tmp/ser_ctl if no other address is
  137. # specified in modparams; this is also the default for sercmd
  138. modparam("ctl", "binrpc", "unixs:/tmp/ser_ctl")
  139. # listen on the "standard" fifo for backward compatibility
  140. modparam("ctl", "fifo", "fifo:/tmp/ser_fifo")
  141. # listen on tcp, localhost
  142. modparam("ctl", "binrpc", "tcp:127.0.0.1:2046")
  143. # -- acc_db params --
  144. # failed transactions (=negative responses) should be logged to
  145. modparam("acc_db", "failed_transactions", 1)
  146. # comment the next line if you don't want to have accounting to DB
  147. modparam("acc_db", "log_flag", "FLAG_ACC")
  148. # -- tm params --
  149. # uncomment the following line if you want to avoid that each new reply
  150. # restarts the resend timer (see INBOUND route below)
  151. #modparam("tm", "restart_fr_on_each_reply", "0")
  152. #!ifdef WITH_TLS
  153. # -- tls params --
  154. modparam("tls", "verify_certificate", 0)
  155. #!ifdef LOCAL_TEST_RUN
  156. modparam("tls", "certificate", "./modules/tls/sip-router-selfsigned.pem")
  157. modparam("tls", "private_key", "./modules/tls/sip-router-selfsigned.key")
  158. #separate TLS config file
  159. #modparam("tls", "config", "./modules/tls/tls.cfg")
  160. #!else
  161. modparam("tls", "certificate", "ser-selfsigned.pem")
  162. modparam("tls", "private_key", "ser-selfsigned.key")
  163. #separate TLS config file
  164. #modparam("tls", "config", "tls.cfg")
  165. #!endif
  166. # -- xmlrpc params --
  167. # using a sub-route from the module is a lot safer than relying on the
  168. # request method to distinguish HTTP from SIP
  169. modparam("xmlrpc", "route", "RPC");
  170. # ------------------------- request routing logic -------------------
  171. # main routing logic
  172. route{
  173. # if you have a PSTN gateway just un-comment the follwoing line and
  174. # specify the IP address of it to route calls to it
  175. #$gw_ip = "1.2.3.4"
  176. # first do some initial sanity checks
  177. route(INIT);
  178. # bypass the rest of the script for CANCELs if possible
  179. route(CATCH_CANCEL);
  180. # check if the request is routed via Route header or
  181. # needs a Record-Route header
  182. route(RR);
  183. # check if the request belongs to our proxy
  184. route(DOMAIN);
  185. # handle REGISTER requests
  186. route(REGISTRAR);
  187. # from here on we want to know you is calling
  188. route(AUTHENTICATION);
  189. # check if we should be outbound proxy for a local user
  190. route(OUTBOUND);
  191. # check if the request is for a local user
  192. route(INBOUND);
  193. # here you could for example try to do an ENUM lookup before
  194. # the call gets routed to the PSTN
  195. #route(ENUM);
  196. # lets see if someone wants to call a PSTN number
  197. route(PSTN);
  198. # nothing matched, reject it finally
  199. sl_reply("404", "No route matched");
  200. }
  201. route[FORWARD]
  202. {
  203. # here you could decide wether this call needs a RTP relay or not
  204. # if this is called from the failure route we need to open a new branch
  205. if (isflagset(FLAG_FAILUREROUTE)) {
  206. append_branch();
  207. }
  208. # if this is an initial INVITE (without a To-tag) we might try another
  209. # (forwarding or voicemail) target after receiving an error
  210. if (method=="INVITE" && strempty(@to.tag)) {
  211. t_on_failure("FAILURE_ROUTE");
  212. }
  213. # send it out now; use stateful forwarding as it works reliably
  214. # even for UDP2TCP
  215. if (!t_relay()) {
  216. sl_reply_error();
  217. }
  218. drop;
  219. }
  220. route[INIT]
  221. {
  222. # initial sanity checks -- messages with
  223. # max_forwards==0, or excessively long requests
  224. if (!mf_process_maxfwd_header("10")) {
  225. sl_reply("483", "Too Many Hops");
  226. drop;
  227. }
  228. if (msg:len >= 4096 ) {
  229. sl_reply("513", "Message too big");
  230. drop;
  231. }
  232. # you could add some NAT detection here for example
  233. # or you cuold call here some of the check from the sanity module
  234. # lets account all initial INVITEs
  235. # further in-dialog requests are accounted by a RR cookie (see below)
  236. if (method=="INVITE" && strempty(@to.tag)) {
  237. setflag(FLAG_ACC);
  238. }
  239. }
  240. route[RPC]
  241. {
  242. # allow XMLRPC from localhost
  243. if ((method=="POST" || method=="GET") &&
  244. (src_ip==127.0.0.1
  245. #!ifdef XMLRPC_ALLOW_NET1
  246. || src_ip == XMLRPC_ALLOW_NET1
  247. #!endif
  248. #!ifdef XMLRPC_ALLOW_NET2
  249. || src_ip == XMLRPC_ALLOW_NET2
  250. #!endif
  251. #!ifdef XMLRPC_ALLOW_NET3
  252. || src_ip == XMLRPC_ALLOW_NET3
  253. #!endif
  254. )) {
  255. if (msg:len >= 8192) {
  256. sl_reply("513", "Request to big");
  257. drop;
  258. }
  259. # close connection only for xmlrpclib user agents (there is a bug in
  260. # xmlrpclib: it waits for EOF before interpreting the response).
  261. if (search("^User-Agent:.*xmlrpclib"))
  262. set_reply_close();
  263. set_reply_no_connect(); # optional
  264. # lets see if a module wants to answer this
  265. dispatch_rpc();
  266. drop;
  267. }
  268. }
  269. route[RR]
  270. {
  271. # subsequent messages within a dialog should take the
  272. # path determined by record-routing
  273. if (loose_route()) {
  274. # mark routing logic in request
  275. append_hf("P-hint: rr-enforced\r\n");
  276. # if the Route contained the accounting AVP cookie we
  277. # set the accounting flag for the acc_db module.
  278. # this is more for demonstration purpose as this could
  279. # also be solved without RR cookies.
  280. # Note: this means all in-dialog request will show up in the
  281. # accounting tables, so prepare your accounting software for this ;-)
  282. if ($account == "yes") {
  283. setflag(FLAG_ACC);
  284. }
  285. # for broken devices which overwrite their Route's with each
  286. # (not present) RR from within dialog requests it is better
  287. # to repeat the RRing
  288. # and if we call rr after loose_route the AVP cookies are restored
  289. # automatically :)
  290. record_route();
  291. route(FORWARD);
  292. } else if (!method=="REGISTER") {
  293. # we record-route all messages -- to make sure that
  294. # subsequent messages will go through our proxy; that's
  295. # particularly good if upstream and downstream entities
  296. # use different transport protocol
  297. # if the initial INVITE got the ACC flag store this in
  298. # an RR AVP cookie. This is more for demonstration purpose
  299. if (isflagset(FLAG_ACC)) {
  300. $account = "yes";
  301. setavpflag($account, "dialog_cookie");
  302. }
  303. record_route();
  304. }
  305. }
  306. route[DOMAIN]
  307. {
  308. # check if the caller is from a local domain
  309. lookup_domain("$fd", "@from.uri.host");
  310. # check if the callee is at a local domain
  311. lookup_domain("$td", "@ruri.host");
  312. # we don't know the domain of the caller and also not
  313. # the domain of the callee -> somone uses our proxy as
  314. # a relay
  315. if (strempty($t.did) && strempty($f.did)) {
  316. sl_reply("403", "Relaying Forbidden");
  317. drop;
  318. }
  319. }
  320. route[REGISTRAR]
  321. {
  322. # if the request is a REGISTER lets take care of it
  323. if (method=="REGISTER") {
  324. # check if the REGISTER if for one of our local domains
  325. if (strempty($t.did)) {
  326. sl_reply("403", "Register forwarding forbidden");
  327. drop;
  328. }
  329. # we want only authenticated users to be registered
  330. if (!www_authenticate("$fd.digest_realm", "credentials")) {
  331. if ($? == -2) {
  332. sl_reply("500", "Internal Server Error");
  333. } else if ($? == -3) {
  334. sl_reply("400", "Bad Request");
  335. } else {
  336. if ($digest_challenge != "") {
  337. append_to_reply("%$digest_challenge");
  338. }
  339. sl_reply("401", "Unauthorized");
  340. }
  341. drop;
  342. }
  343. # check if the authenticated user is the same as the target user
  344. if (!lookup_user("$tu.uid", "@to.uri")) {
  345. sl_reply("404", "Unknown user in To");
  346. drop;
  347. }
  348. if ($f.uid != $t.uid) {
  349. sl_reply("403", "Authentication and To-Header mismatch");
  350. drop;
  351. }
  352. # check if the authenticated user is the same as the request originator
  353. # you may uncomment it if you care, what uri is in From header
  354. #if (!lookup_user("$fu.uid", "@from.uri")) {
  355. # sl_reply("404", "Unknown user in From");
  356. # drop;
  357. #}
  358. #if ($fu.uid != $tu.uid) {
  359. # sl_reply("403", "Authentication and From-Header mismatch");
  360. # drop;
  361. #}
  362. # everything is fine so lets store the binding
  363. if (!save_contacts("location")) {
  364. sl_reply("400", "Invalid REGISTER Request");
  365. drop;
  366. }
  367. drop;
  368. }
  369. }
  370. route[AUTHENTICATION]
  371. {
  372. if (method=="CANCEL" || method=="ACK") {
  373. # you are not allowed to challenge these methods
  374. break;
  375. }
  376. # requests from non-local to local domains should be permitted
  377. # remove this if you want a walled garden
  378. if (strempty($f.did)) {
  379. break;
  380. }
  381. # as gateways are usually not able to authenticate for their
  382. # requests you will have trust them base on some other information
  383. # like the source IP address. WARNING: if at all this is only safe
  384. # in a local network!!!
  385. #if (src_ip==a.b.c.d) {
  386. # break;
  387. #}
  388. if (!proxy_authenticate("$fd.digest_realm", "credentials")) {
  389. if ($? == -2) {
  390. sl_reply("500", "Internal Server Error");
  391. } else if ($? == -3) {
  392. sl_reply("400", "Bad Request");
  393. } else {
  394. if ($digest_challenge != "") {
  395. append_to_reply("%$digest_challenge");
  396. }
  397. sl_reply("407", "Proxy Authentication Required");
  398. }
  399. drop;
  400. }
  401. # check if the UID from the authentication meets the From header
  402. $authuid = $uid;
  403. if (!lookup_user("$fu.uid", "@from.uri")) {
  404. del_attr("$uid");
  405. }
  406. if ($fu.uid != $fr.authuid) {
  407. sl_reply("403", "Fake Identity");
  408. drop;
  409. }
  410. # load the user AVPs (preferences) of the caller, e.g. for RPID header
  411. load_attrs("$fu", "$f.uid");
  412. }
  413. route[OUTBOUND]
  414. {
  415. # if a local user calls to a foreign domain we play outbound proxy for him
  416. # comment this out if you want a walled garden
  417. if ($f.did != "" && $t.did == "") {
  418. append_hf("P-hint: outbound\r\n");
  419. route(FORWARD);
  420. }
  421. }
  422. route[INBOUND]
  423. {
  424. # lets see if know the callee
  425. if (lookup_user("$tu.uid", "@ruri")) {
  426. # load the preferences of the callee to have his timeout values loaded
  427. load_attrs("$tu", "$t.uid");
  428. # if you want to know if the callee username was an alias
  429. # check it like this
  430. #if (strempty($tu.uri_canonical)) {
  431. # if the alias URI has different AVPs/preferences
  432. # you can load them into the URI track like this
  433. #load_attrs("$tr", "@ruri");
  434. #}
  435. # check for call forwarding of the callee
  436. # Note: the forwarding target has to be full routable URI
  437. # in this example
  438. if ($tu.fwd_always_target != "") {
  439. attr2uri("$tu.fwd_always_target");
  440. route(FORWARD);
  441. }
  442. # native SIP destinations are handled using our USRLOC DB
  443. if (lookup_contacts("location")) {
  444. append_hf("P-hint: usrloc applied\r\n");
  445. # we set the TM module timers according to the preferences
  446. # of the callee (avoid too long ringing of his phones)
  447. # Note1: timer values have to be in ms now!
  448. # Note2: this makes even more sense if you switch to a voicemail
  449. # from the FAILURE_ROUTE below
  450. if ($t.fr_inv_timer != 0) {
  451. if ($t.fr_timer != 0) {
  452. t_set_fr("$t.fr_inv_timer", "$t.fr_timer");
  453. } else {
  454. t_set_fr("$t.fr_inv_timer");
  455. }
  456. }
  457. route(FORWARD);
  458. } else {
  459. sl_reply("480", "User temporarily not available");
  460. drop;
  461. }
  462. }
  463. }
  464. route[PSTN]
  465. {
  466. # Only if the AVP 'gw_ip' is set and the request URI contains
  467. # only a number we consider sending this to the PSTN GW.
  468. # Only users from a local domain are permitted to make calls.
  469. # Additionally you might want to check the acl AVP to verify
  470. # that the user is allowed to make such expensives calls.
  471. if ($f.did != "" && $gw_ip != "" &&
  472. uri=~"sips?:\+?[0-9]{3,18}@.*") {
  473. # probably you need to convert the number in the request
  474. # URI according to the requirements of your gateway here
  475. # if an AVP 'asserted_id' is set we insert an RPID header
  476. if ($asserted_id != "") {
  477. xlset_attr("$rpidheader", "<sip:%$asserted_id@%@ruri.host>;screen=yes");
  478. replace_attr_hf("Remote-Party-ID", "$rpidheader");
  479. }
  480. # just replace the domain part of the RURI with the
  481. # value from the AVP and send it out
  482. attr2uri("$gw_ip", "domain");
  483. route(FORWARD);
  484. }
  485. }
  486. route[CATCH_CANCEL] {
  487. # check whether there is a corresponding INVITE to the CANCEL,
  488. # and bypass the rest of the script if possible
  489. if (method == CANCEL) {
  490. if (!t_relay_cancel()) { # implicit drop if the INVITE was found
  491. # INVITE was found but some error occurred
  492. sl_reply("500", "Internal Server Error");
  493. drop;
  494. }
  495. # bad luck, no corresponding INVITE was found,
  496. # we have to continue with the script
  497. }
  498. }
  499. failure_route[FAILURE_ROUTE]
  500. {
  501. # mark for the other routes that we are operating from here on from a
  502. # failure route
  503. setflag(FLAG_FAILUREROUTE);
  504. if (t_check_status("486|600")) {
  505. # if we received a busy and a busy target is set, forward it there
  506. # Note: again the forwarding target has to be a routeable URI
  507. if ($tu.fwd_busy_target != "") {
  508. attr2uri("$tu.fwd_busy_target");
  509. route(FORWARD);
  510. }
  511. # alternatively you could forward the request to SEMS/voicemail here
  512. }
  513. else if (t_check_status("408|480")) {
  514. # if we received no answer and the noanswer target is set,
  515. # forward it there
  516. # Note: again the target has to be a routeable URI
  517. if ($tu.fwd_noanswer_target != "") {
  518. attr2uri("$tu.fwd_noanswer_target");
  519. route(FORWARD);
  520. }
  521. # alternatively you could forward the request to SEMS/voicemail here
  522. }
  523. }