ser-oob.cfg 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. #
  2. # $Id$
  3. #
  4. #
  5. # Applicability of this Configuration File
  6. # ----------------------------------------
  7. #
  8. # This is default SER script as used for example at the iptel.org
  9. # SIP service; it can deal with NATs, terminate calls to a PSTN
  10. # gateway, and it implements a couple of basic signaling features
  11. # (few types of call forwarding). In this scenario you may have
  12. # multiple SIP proxies sharing one database for accessing provisioned
  13. # data, which are maintained for example using serweb. The proxy
  14. # servers also share write-access to user location database (and
  15. # keeps a full cache of all usrloc entries synchronized using
  16. # multicast).
  17. #
  18. # If you look for a simpler version with a lot less dependencies
  19. # please refer to the ser-basic.cfg file in your SER distribution.
  20. #
  21. # Requirements:
  22. # ---------------
  23. # running DB, running RTP proxy, one public IP address
  24. # for SIP service, one private IP address for administrative purposes;
  25. # optional: IP address of a PSTN gateway
  26. #
  27. # HOWTOs:
  28. # ---------
  29. # To get this config running you need to execute the following commands
  30. # with the new serctl (the capital word are just place holders):
  31. #
  32. # $ ser_ctl domain add DOMAINNAME
  33. # $ ser_ctl user add USERNAME@DOMAINNAME -p PASSWORD
  34. #
  35. # If you want to have P-Asserted-ID header for your user
  36. #
  37. # $ ser_attr add uid=UID asserted_id="PID"
  38. #
  39. # If you want to have (PSTN) gateway support:
  40. #
  41. # $ ser_db add attr_types name=gw_ip rich_type=string raw_type=2 \
  42. # description="The gateway IP for the default ser.cfg" default_flags=33
  43. # $ ser_attr add global gw_ip=GATEWAY-IP
  44. #
  45. # Alternatively, you can simple uncomment the relevant line in this file
  46. # right at the beginning of the main route.
  47. #
  48. # You can also use serweb to set all the values above.
  49. #
  50. # Users with permission to call PSTN using this script must have
  51. # the $gw_acl attribute set properly, and shall have $asserted_id
  52. # set to indicate their caller-id for calls to PSTN. For inbound
  53. # calls from PSTN, additional aliases may be also set.
  54. #
  55. # Warning:
  56. # -----------
  57. # If this file is installed on Debian from package 'ser-oob' then some
  58. # options in this configuration file may be set by post-installation
  59. # script, according to values entered by user at installation time in
  60. # debconf configuration. These values are then applied automatically to
  61. # this file each time the 'ser-oob' package is upgraded or reconfigured by
  62. # calling 'dpkg-reconfigure ser-oob'.
  63. #
  64. # The parts of this configuration file that may be altered by debconf are
  65. # enclosed between '#DEBCONF-something-START' and '#DEBCONF-something-END'
  66. # comment marks. Please do not remove them.
  67. #
  68. #
  69. # TODO (Future possible improvements):
  70. # ---------------------------------------
  71. # * protocol tuning
  72. # - AVP-based diversion for call-forwarding (as opposed to specialized
  73. # module)
  74. # - add Date header in 200s to REGISTERs (to be packaged with NTP!)
  75. # * more security:
  76. # - pike/rate-limit
  77. # - identity
  78. # - TLS
  79. # - permissions
  80. # - Re-name all internal headers so that they start with a common prefix,
  81. # such as P-SER and then wipe all such headers from requests received
  82. # from untrusted sources, such as the user agents or foreign proxy
  83. # servers
  84. # * refined DB use (e.g., flatstore for acc)
  85. # * miscellanous:
  86. # - dialog module for monitoring purposes
  87. # - more extensive logging using xlog (controlled by gflags/gAVPs)
  88. # * leveraging 2.1 features:
  89. # - removal of private IP address (it takes a multicast-specific
  90. # command which will allow OS to determine source IP address)
  91. # - timer route:
  92. # * don't use exec (it takes domain.reload as script command)
  93. # * compare last-stored timestamp with current timestamp (it takes
  94. # assignment of gAVPs)
  95. # * check multicast REGISTERs for their TTL (this is a simple and
  96. # effective security check to prevent remote multicast messages
  97. # to damage our traffic)
  98. # - numerous fine-tuning parameters which are only available in 2.1
  99. # (mlock_pages, dns_try_naptr, etc.)
  100. # - better support for preloaded routes with domain name
  101. #
  102. # Security considerations:
  103. # ------------------------
  104. # The script has been tested against security leaks, but it comes
  105. # under terms of GPL "as is" without any warranties; better check
  106. # yourself that:
  107. # - IP based authentication of PSTN gateway and multicast REGISTERs
  108. # is compliant to your network setup and security policy.
  109. # - Multiple gateway IPs can't be provisioned as security checks
  110. # are applied only to one.
  111. #
  112. # Licensing
  113. # ----------
  114. # Copyright (C) 2005-2008 iptelorg GmbH
  115. # This file is part of SER, a free SIP server. It is available under the
  116. # terms of the GNU General Public License.
  117. # Numerous folks have contributed to this file, including but not limited
  118. # to Andrei, Jan, Jiri, Michal, Miklos, Nils.
  119. #
  120. #
  121. # .... that's it, enough of yadiyada, here the real config begins!
  122. # ----------- Global Configuration Parameters -------------------------------
  123. #debug=3 # debug level (cmd line: -ddd)
  124. #memdbg=10 # memory debug log level
  125. #memlog=10 # memory statistics log level
  126. #log_facility=LOG_LOCAL0 # the facility used for logging (see syslog(3))
  127. #DEBCONF-SERVERID-START
  128. server_id=0
  129. #DEBCONF-SERVERID-END
  130. # Uncomment these lines to enter debugging mode or start SER with
  131. # ser -ED
  132. #
  133. #fork=no
  134. #log_stderror=yes
  135. check_via=no # (cmd. line: -v)
  136. dns=no # (cmd. line: -r)
  137. rev_dns=no # (cmd. line: -R)
  138. #port=5060
  139. #children=4
  140. #user=ser
  141. #group=ser
  142. #disable_core=yes # disables core dumping
  143. open_files_limit=20480 # sets the open file descriptors limit
  144. #mhomed=yes # usefull for multihomed hosts, small performance
  145. # penalty
  146. disable_tcp=no # be conservative about enabling TCP -- it can
  147. # degrade performance a lot
  148. #tcp_accept_aliases=yes # accepts the tcp alias via option
  149. phone2tel=no # ignore user=phone in request-URIs -- otherwise
  150. # these URIs would be interpreted as equivalent
  151. # to TEL URIs, and their lookup would fail in URI
  152. # database
  153. reply_to_via=no
  154. # public IP address
  155. #DEBCONF-LISTEN-START
  156. listen=127.0.0.1
  157. #DEBCONF-LISTEN-END
  158. # sip.mcast.net for REGISTER replication
  159. #DEBCONF-LISTEN_REPL-START
  160. listen=224.0.1.75
  161. #DEBCONF-LISTEN_REPL-END
  162. # administrative interface -- needed for example for multicast source
  163. # or XML-RPC
  164. #DEBCONF-LISTEN_ADMIN-START
  165. listen=udp:127.0.0.1
  166. #DEBCONF-LISTEN_ADMIN-END
  167. #listen=tls:127.0.0.1:5061
  168. mlock_pages=yes
  169. shm_force_alloc=yes
  170. real_time=7
  171. # ------------------- DNS Parameters ----------------------------------------
  172. # (see doc/dns.txt for more details)
  173. #
  174. # minimum timeouts
  175. dns_retr_time=1
  176. dns_retr_no=1
  177. dns_servers_no=1
  178. dns_use_search_list=no
  179. dns_try_ipv6=no
  180. # dns cache & failover
  181. use_dns_cache=on
  182. use_dns_failover=on
  183. # dns_cache_flags=0
  184. dns_cache_negative_ttl=300
  185. dns_cache_min_ttl=60
  186. dns_cache_max_ttl=86400 # 1 day
  187. dns_cache_mem=2048 # 2 MB
  188. dns_cache_gc_interval=60 # garbage collection every minute
  189. # ser 2.1 specific options
  190. # dns_try_naptr=yes
  191. # dns_srv_lb=yes # srv based load balancing
  192. # dns_udp_pref=3 # prefer udp (when resolving naptr record)
  193. # dns_tcp_pref=2 # if no udp availbale accept tcp (for naptr)
  194. # dns_tls_pref=-1 # ignore / don't accept tls (for naptr)
  195. # dns_cache_delete_nonexpired=no
  196. # ------------------- Blacklist Parameters ----------------------------------
  197. # (see doc/dst_blacklist.txt for more details)
  198. #
  199. use_dst_blacklist=on
  200. dst_blacklist_mem=1024 # 1 MB
  201. dst_blacklist_expire=300 # blacklist default time
  202. dst_blacklist_gc_interval=150 # 2.5 min
  203. # for ser 2.1 to the above add tm blst_503* parameters and/or use the
  204. # blst module (see NEWS)
  205. # ------------------- TCP Parameters ----------------------------------------
  206. # (see NEWS for more details)
  207. tcp_connection_lifetime=3600
  208. #tcp_max_connections=10240 # default is 2048
  209. tcp_connect_timeout=1
  210. # ------------------- TLS Parameters ----------------------------------------
  211. # Enable TLS hooks so that the TLS module can be used
  212. tls_enable=yes
  213. # -------------------- Custom Parameters ------------------------------------
  214. # These parameters can be modified runtime via RPC interface,
  215. # read the documentation of cfg_rpc module.
  216. # Session Timer parameters, RFC 4028
  217. #
  218. # Default session interval used by the proxy if the UAC does not support
  219. # session timer. Set it to "0" to disable session timer proxy support.
  220. #
  221. session_timer.default = "1800" desc "default session interval (in s)"
  222. #
  223. # Minimum session interval accepted by the proxy, it must not be less
  224. # than 90 seconds.
  225. #
  226. session_timer.min_se = "90" desc "minimum session interval (in s)"
  227. # RTP Proxy options
  228. #
  229. # Whether to enable or disable the rtp proxy. Possible values are:
  230. # "0" -- always disable
  231. # "1" -- always enable regardless of whether UAC or UAS is behind NAT
  232. # "detect" -- detect whether the UAC or the UAS is behind NAT,
  233. # and enable the rtp proxy when necessary
  234. #
  235. #DEBCONF-RTP_ENABLE-START
  236. rtp_proxy.enabled = "detect" desc "indicates whether the RTP Proxy is enabled or not (0/1/detect)"
  237. #DEBCONF-RTP_ENABLE-END
  238. # ------------------ Module Loading -----------------------------------------
  239. loadpath "/usr/lib/ser/modules"
  240. # load a SQL database for authentication, domains, user AVPs etc.
  241. loadmodule "mysql"
  242. #loadmodule "postgres"
  243. loadmodule "sl"
  244. loadmodule "tm"
  245. loadmodule "rr"
  246. loadmodule "maxfwd"
  247. loadmodule "usrloc"
  248. loadmodule "registrar"
  249. loadmodule "xlog"
  250. loadmodule "textops"
  251. loadmodule "ctl"
  252. loadmodule "auth"
  253. loadmodule "auth_db"
  254. loadmodule "gflags"
  255. loadmodule "domain"
  256. loadmodule "uri_db"
  257. loadmodule "avp"
  258. loadmodule "avp_db"
  259. loadmodule "acc_db"
  260. #loadmodule "xmlrpc"
  261. loadmodule "options"
  262. loadmodule "sanity"
  263. loadmodule "nathelper"
  264. loadmodule "uri"
  265. loadmodule "speeddial"
  266. loadmodule "timer"
  267. loadmodule "db_ops"
  268. loadmodule "exec"
  269. loadmodule "cfg_rpc"
  270. loadmodule "eval"
  271. #loadmodule "tls"
  272. # ----------------- Declaration of Script Flags -----------------------------
  273. flags
  274. FLAG_ACC : 1, # the request will be recorded by ACC
  275. FLAG_FAILUREROUTE : 2, # we are operating from the failure route
  276. FLAG_NAT : 3, # the UAC is behind a NAT
  277. FLAG_REPL_ENABLED : 4, # REGISTER replication is enabled if set
  278. FLAG_TOTAG : 5, # request has a To tag
  279. FLAG_PSTN_ALLOWED : 6, # the user is allowed to use the PSTN
  280. FLAG_DONT_RM_CRED : 7, # do not remove the credentials
  281. FLAG_AUTH_OK : 8, # authentication succeeded
  282. FLAG_SERWEB_RSVD1 : 9, # bit reserved for use with serweb
  283. FLAG_SERWEB_RSVD2 : 10, # bit reserved for use with serweb
  284. FLAG_SESSIONTIMER : 11, # indicates that the UAC supports Session Timer
  285. FLAG_RR_DONE : 12, # the request got already one RR header
  286. FLAG_RTP_PROXY : 13; # the RTP proxy is turned on
  287. avpflags
  288. dialog_cookie; # attribute will be stored in Route headers
  289. # ----------------- Module-specific Parameters ------------------------------
  290. # path to the database
  291. #
  292. #DEBCONF-DBURL-START
  293. modparam("speeddial|auth_db|usrloc|domain|uri_db|gflags|avp_db|db_ops",
  294. "db_url", "mysql://ser:[email protected]/ser")
  295. #DEBCONF-DBURL-END
  296. # specify the path to your database for accounting
  297. #DEBCONF-DBURLACC-START
  298. modparam("acc_db", "db_url", "mysql://ser:[email protected]/ser")
  299. #DEBCONF-DBURLACC-END
  300. # -- usrloc --
  301. # Database access mode: 0 -- memory cached, 1 -- write through,
  302. # 2 -- delayed write. 1 is generally safer than 2. 2 can help
  303. # to survive peaks in load. However, it creates delayed peaks that can
  304. # impair request-processing latency later (usrloc would have to be
  305. # re-redesigned more lock-free to avoid it).
  306. #DEBCONF-DBMODE-START
  307. modparam("usrloc", "db_mode", 1)
  308. #DEBCONF-DBMODE-END
  309. # Don't delete expired records from database on a per-contact basis -- that
  310. # results in bulky DB operations and can lead to synchronization issues
  311. # in server farm when for a time a server doesn't obtain re-reregistrations
  312. modparam("usrloc","db_skip_delete",1)
  313. # -- registrar --
  314. # Maximum expires time. Forces users to re-register every 10 min.
  315. modparam("registrar", "max_expires", 600)
  316. # Minimum expires time. Even if they try, clients cannot register
  317. # for a shorter time than this.
  318. modparam("registrar", "min_expires", 240)
  319. # Identify natted contacts using a flag.
  320. modparam("registrar", "load_nat_flag", "FLAG_NAT")
  321. modparam("registrar", "save_nat_flag", "FLAG_NAT")
  322. # Maximum number of contacts.
  323. modparam("registrar", "max_contacts", 10)
  324. # -- auth --
  325. #modparam("auth_db", "calculate_ha1", yes)
  326. #modparam("auth_db", "password_column", "password")
  327. # Minimize replay-attack window.
  328. modparam("auth", "nonce_expire", 10)
  329. # Enable/disable extra authentication checks using the following modparams.
  330. # The values are: 1 -- Request-URI, 2 -- Call-ID, 4 -- From tag,
  331. # 8 -- source IP. The options are disabled by default.
  332. # For REGISTER requests we hash the Request-URI, Call-ID, and source IP of the
  333. # request into the nonce string. This ensures that the generated credentials
  334. # cannot be used with another registrar, user agent with another source IP
  335. # address or Call-ID. Note that user agents that change Call-ID with every
  336. # REGISTER message will not be able to register if you enable this.
  337. #modparam("auth", "auth_checks_register", 11)
  338. # For dialog-establishing requests (such as the original INVITE, OPTIONS, etc)
  339. # we hash the Request-URI and source IP. Hashing Call-ID and From tags takes
  340. # some extra precaution, because these checks could render some UA unusable.
  341. #modparam("auth", "auth_checks_no_dlg", 9)
  342. # For mid-dialog requests, such as re-INVITE, we can hash source IP and
  343. # Request-URI just like in the previous case. In addition to that we can hash
  344. # Call-ID and From tag because these are fixed within a dialog and are
  345. # guaranteed not to change. This settings effectively restrict the usage of
  346. # generated credentials to a single user agent within a single dialog.
  347. #modparam("auth", "auth_checks_in_dlg", 15)
  348. # Deal with clients who can't do qop properly
  349. modparam("auth", "qop", "")
  350. #DEBCONF-AUTHSECRET-START
  351. modparam("auth", "secret", "aqwedrftredswqwddcft")
  352. #DEBCONF-AUTHSECRET-END
  353. # -- rr --
  354. # Add value to lr param to make some broken UAs happy.
  355. modparam("rr", "enable_full_lr", 1)
  356. # Limit the length of the AVP cookie to necessary attributes only
  357. modparam("rr", "cookie_filter", "(account|uac_nat|stimer)")
  358. # You probably do not want that someone can simply read and change
  359. # the AVP cookie in your Routes, thus should really change this
  360. # secret value below
  361. modparam("rr", "cookie_secret", "sgsatewgdbsnmpoiewh")
  362. # The ftag Route parameter may be used to easily determine if a BYE
  363. # is coming from caller or callee, but we prefer shorter messages
  364. modparam("rr", "append_fromtag", 0)
  365. # -- gflags --
  366. # Load global attributes.
  367. modparam("gflags", "load_global_attrs", 1)
  368. # -- domain --
  369. # Load domain attributes.
  370. modparam("domain", "load_domain_attrs", 1)
  371. # -- ctl --
  372. # By default, ctl listens on unixs:/tmp/ser_ctl if no other address is
  373. # specified in modparams; this is also the default for sercmd.
  374. modparam("ctl", "binrpc", "unixs:/tmp/ser_ctl")
  375. # Listen on the "standard" fifo for backward compatibility.
  376. modparam("ctl", "fifo", "fifo:/tmp/ser_fifo")
  377. # Listen on tcp on localhost.
  378. #modparam("ctl", "binrpc", "tcp:localhost:2046")
  379. # -- acc_db --
  380. # Failed transactions (those with negative responses) should be logged, too.
  381. modparam("acc_db", "failed_transactions", 1)
  382. # If you don't want to have accounting entries written into the database,
  383. # comment the next line out.
  384. modparam("acc_db", "log_flag", "FLAG_ACC")
  385. # if you would like to customize your CDRs, do it here....
  386. #modparam("acc_db", "attrs",
  387. # "$f.sop_billing_category,$f.isPrepaidCustomer,$f.sop_cf_orig_uid")
  388. # -- tm --
  389. # Do not restart the resend timer with each reply. (See INBOUND route
  390. # below.)
  391. modparam("tm", "restart_fr_on_each_reply", 0)
  392. # -- xmlrpc --
  393. # Use a sub-route. This is a lot safer then relying on the request method
  394. # to distinguish HTTP from SIP
  395. #modparam("xmlrpc", "route", "RPC");
  396. # -- nathelper --
  397. # RTP Proxy address
  398. #DEBCONF-RTTPPROXY-START
  399. modparam("nathelper", "rtpproxy_sock", "udp:127.0.0.1:22222")
  400. #DEBCONF-RTTPPROXY-END
  401. # TCP keepalives as simple as CRLF
  402. modparam("nathelper", "natping_crlf", 0)
  403. # How often to send a NAT ping. Set this to 0 to turn NAT ping off.
  404. #DEBCONF-NATPING_INTERVAL-START
  405. modparam("nathelper", "natping_interval", 15)
  406. #DEBCONF-NATPING_INTERVAL-END
  407. # Only ping contacts that have the NAT flag set.
  408. modparam("nathelper", "ping_nated_only", 1)
  409. # Send an OPTIONS SIP request as NAT ping. If this is not set, a simple
  410. # 4-byte ping is used.
  411. modparam("nathelper", "natping_method", "OPTIONS")
  412. # Temporary statefull natping test (only in future versions)
  413. #modparam("nathelper", "natping_stateful", 1)
  414. # -- exec --
  415. modparam("exec", "time_to_kill", 200);
  416. modparam("exec", "setvars", 0);
  417. # -- timer --
  418. # Register route ON_1MIN_TIMER to be called every minute.
  419. modparam("timer", "declare_timer",
  420. "ON_1MIN_TIMER=ON_1MIN_TIMER,60000,slow,enable");
  421. # -- tls --
  422. #modparam("tls", "config", "tls.cfg");
  423. # -- db_ops --
  424. modparam("db_ops", "declare_handle", "reload")
  425. # ------------------------- Request Routing Logic --------------------------
  426. # Main request route.
  427. #
  428. # Each request starts here.
  429. #
  430. route
  431. {
  432. # if you have a PSTN gateway just un-comment the follwoing line and
  433. # specify the IP address of it to route calls to it.
  434. #$gw_ip = "1.2.3.4"
  435. # Alternatively (even better), set it as global persistent parameter
  436. # using serweb or ser_attrs). If using a PSTN GW, per-subscriber
  437. # options must ($gw_acl) or may (asserted_id) be set to enable calls
  438. # to PSTN. If email-like URIs are used, having a URI alias for
  439. # processing incoming PSTN-to-ip requests may be useful, too.
  440. # Important: the script is assuming one global pstn-gw for all
  441. # domains! Failure to allow gw_ip to be a domain-specic attribute
  442. # would result in security gaps (onsend_route checks only for one
  443. # gateway).
  444. # First, do some initial sanity checks.
  445. route(INIT);
  446. # Bypass the rest of the script for CANCELs if possible.
  447. route(CATCH_CANCEL);
  448. # Check if the request is routed via Route header.
  449. route(LOOSE_ROUTE);
  450. # Look up domain IDs
  451. route(DOMAIN);
  452. # Answer OPTIONS requests to our system.
  453. route(OPTIONS_REPLY);
  454. # Enforce domain policy.
  455. route(DOMAIN_POLICY);
  456. # Handle REGISTER requests.
  457. route(REGISTRAR);
  458. # From here on we want to know who is calling.
  459. route(AUTHENTICATION);
  460. # We are finished with all the precaution work -- let's
  461. # try to locate the callee. The first route that matches
  462. # "wins" and relays the request. If none matches, SER will
  463. # send a 404.
  464. # Check if we should be outbound proxy for a local user.
  465. route(OUTBOUND);
  466. # Redirect in case user dialed a speed dial entry.
  467. route(SPEEDDIAL);
  468. # Place various site-specific routes here.
  469. route(SITE_SPECIFIC);
  470. # Check if the request is for a local user.
  471. route(INBOUND);
  472. # There is SIP user for the called address. Before trying PSTN,
  473. # you may have to convert the adress, for instance by using
  474. # ENUM.
  475. #route(ENUM);
  476. # Last resort: if none of the previous route has found
  477. # the recepient, try PSTN.
  478. route(PSTN);
  479. # nothing matched
  480. sl_reply("404", "No route matched");
  481. }
  482. # Forward a request to the destination set.
  483. #
  484. route[FORWARD]
  485. {
  486. # If this is called from the failure route we need to add a new
  487. # branch.
  488. if (isflagset(FLAG_FAILUREROUTE)) {
  489. if (!append_branch()) {
  490. t_reply("500", "Too many branches");
  491. drop;
  492. }
  493. }
  494. # If this is an initial INVITE (without a To-tag) we might try
  495. # another target (call forwarding or voicemail) after receiving
  496. # an error.
  497. if (method=="INVITE" && [email protected]) {
  498. t_on_failure("FAILURE_ROUTE");
  499. }
  500. # Always use the reply route to check for NATed UAS.
  501. t_on_reply("REPLY_ROUTE");
  502. # Insert a Record-Route header into all requests.
  503. # This has to be done as one of the last steps to include all the
  504. # RR cookies which might have been created during the script run.
  505. route(RR);
  506. # Activate the RTP proxy as the last step because it modifies the
  507. # body.
  508. route(RTPPROXY);
  509. # Remove credentials to keep requests shorter
  510. if (isflagset(FLAG_AUTH_OK) && !isflagset(FLAG_DONT_RM_CRED) ) {
  511. consume_credentials();
  512. }
  513. # Send it out now.
  514. if (!t_relay()) {
  515. if (isflagset(FLAG_FAILUREROUTE)) {
  516. # XXX This should be replaced with
  517. # t_reply_error() similar to sl_reply_error()
  518. # in order to return the proper failure code.
  519. # Only, there is no such function yet.
  520. t_reply("500", "Request cannot be forwarded");
  521. }
  522. else {
  523. sl_reply_error();
  524. }
  525. }
  526. drop;
  527. }
  528. # Perform initial checks on an incoming request.
  529. #
  530. # Rejects the request if it fails any of the checks.
  531. #
  532. route[INIT]
  533. {
  534. # Messages with a Max-Forwards header of zero.
  535. if (!mf_process_maxfwd_header("10")) {
  536. sl_reply("483","Too Many Hops");
  537. drop;
  538. }
  539. # Set flag for use in the onsend route
  540. if (@to.tag) {
  541. setflag(FLAG_TOTAG);
  542. }
  543. # Check if the UAC is NATed and fix the message accordingly
  544. route(NAT_DETECTION);
  545. # Activate accounting for all initial INVITEs. In-dialog requests
  546. # are accounted by a RR cookie (see below).
  547. if (method == "INVITE" && [email protected]) {
  548. setflag(FLAG_ACC);
  549. }
  550. # Set flag and use it instead of the attribute.
  551. if ($replicate==1) {
  552. setflag(FLAG_REPL_ENABLED);
  553. }
  554. }
  555. # Reply OPTIONS requests sent to the proxy itself.
  556. #
  557. route[OPTIONS_REPLY]
  558. {
  559. # OPTIONS requests without a username in the Request-URI but one
  560. # of our domains or IPs are addressed to the proxy itself and
  561. # can be answered statelessly.
  562. if (method == "OPTIONS" && [email protected] && (uri == myself || $t.did))
  563. {
  564. options_reply();
  565. drop;
  566. }
  567. }
  568. # Check if the sender of the request is behind a NAT device. If so,
  569. # fix the request so that other devices can talk to the sender nonetheless.
  570. #
  571. route[NAT_DETECTION]
  572. {
  573. # Lots of UAs do not include the rport parameter in there Via
  574. # header, so we put it there regardless.
  575. force_rport();
  576. force_tcp_alias();
  577. # Check if the request contains hints for a NATed UAC. Also, try to
  578. # rewrite contacts using maddr. Using maddr is a really dubious
  579. # technique and we better replace such with transport address.
  580. # Downside: it fails for clients fronted by another server, in
  581. # which case a valid contact we dislike because of maddr will be
  582. # substituted inapproprietely (e.g., WM from other domains will
  583. # fail). If you are worried about that, remove tests for maddr and
  584. # recompile SER using HONOR_MADDR. Also note that rewriting
  585. # contacts may possibly lead to client denying subseqent requests
  586. # to them because they don't recognized fixed contacts as their
  587. # own. Should you encounter such a case, a possible solution
  588. # would be to store the original information as a contact parameter
  589. # and restore it on its way back.
  590. if (nat_uac_test("19")
  591. || (@hf_value["contact"] && @contact.uri.params.maddr))
  592. {
  593. setflag(FLAG_NAT);
  594. $uac_nat = 1;
  595. setavpflag($uac_nat, "dialog_cookie");
  596. if (method == "REGISTER") {
  597. # Prepare the Contact so that the registrar module
  598. # saves the source address and port as well.
  599. fix_nated_register();
  600. }
  601. else {
  602. # Overwrite the Contact to allow proper in-dialog
  603. # routing.
  604. fix_nated_contact();
  605. }
  606. }
  607. }
  608. # Activates RTP proxy if necessary.
  609. #
  610. route[RTPPROXY]
  611. {
  612. if (@cfg_get.rtp_proxy.enabled == "0") {
  613. # RTP Proxy is disabled
  614. break;
  615. } else if (@cfg_get.rtp_proxy.enabled == "detect") {
  616. if (!isflagset(FLAG_NAT)) {
  617. # If no NAT is involved we don't have to do here anything.
  618. break;
  619. }
  620. } else if (@cfg_get.rtp_proxy.enabled != "1") {
  621. # This is not a valid setting
  622. xlog("L_ERR", "Unknown option for rtp_proxy.enabled: %@cfg_get.rtp_proxy.enabled\n");
  623. break;
  624. } # else rtp proxy is permanently enabled
  625. # If the message terminates a dialog turn RTP proxy off.
  626. if (method == "BYE" || method == "CANCEL") {
  627. unforce_rtp_proxy();
  628. append_hf("P-RTP-Proxy: UNFORCED\r\n");
  629. break;
  630. }
  631. # Turn the RTP proxy on for INVITEs and UPDATEs.
  632. if (((method=="INVITE" || method == "UPDATE") && @msg.body)
  633. && !isflagset(FLAG_RTP_PROXY))
  634. {
  635. force_rtp_proxy('r');
  636. append_hf("P-RTP-Proxy: YES\r\n");
  637. setflag(FLAG_RTP_PROXY);
  638. }
  639. }
  640. # Handling of loose routed requests
  641. #
  642. # XXX Isn't the proper term "record routed"? This route also handles
  643. # strict routed requests, doesn't it? -- martinh
  644. route[LOOSE_ROUTE]
  645. {
  646. # subsequent messages withing a dialog should take the
  647. # path determined by the Route headers.
  648. if (loose_route()) {
  649. # Mark routing logic in request.
  650. append_hf("P-hint: rr-enforced\r\n");
  651. # If the Route contained the accounting AVP cookie we
  652. # set the accounting flag for the acc_db module.
  653. # This is more for demonstration purpose as this could
  654. # also be solved without RR cookies.
  655. # Note: this means all in-dialog request will show up in
  656. # the accounting tables, so prepare your accounting software
  657. # for this.
  658. if ($account == "yes") {
  659. setflag(FLAG_ACC);
  660. }
  661. # Restore the NAT flag if present
  662. if ($uac_nat == 1) {
  663. setflag(FLAG_NAT);
  664. }
  665. # Restore Session Timer flag and headers.
  666. if ($stimer && ($stimer != "0")) {
  667. route(SESSION_TIMER);
  668. }
  669. # Some broken devices overide the dialog route set with the
  670. # Record-Route headers from each in-dialog request. So, we
  671. # better add Record-Route headers again. If we call
  672. # record_route() after loose_route(), the AVP cookies are
  673. # restored automatically. Additionally, there is a scenario
  674. # where Record-Route headers are necessary if an initial
  675. # SUBSCRIBE is forked.
  676. #
  677. # Note that here we forward before authentication checks
  678. # are executed. Generally, we only authenticate
  679. # out-of-dialog requests. Some in-dialog requests can't be
  680. # authenticated at all, see the call-forwarding example in
  681. # route[DOMAIN].
  682. route(RR);
  683. route(FORWARD);
  684. }
  685. }
  686. # Add a Record-Route header
  687. #
  688. route[RR]
  689. {
  690. if (!isflagset(FLAG_RR_DONE) && method != "REGISTER") {
  691. # We record-route all messages to make sure that
  692. # subsequent messages will go through our proxy. This is
  693. # particularly good if upstream and downstream entities
  694. # use different transport protocols.
  695. # If the ACC flag is set, store this in a Record-Route
  696. # AVP cookie. This is more for demonstration purposes.
  697. if (isflagset(FLAG_ACC)) {
  698. $account = "yes";
  699. setavpflag($account, "dialog_cookie");
  700. }
  701. # Insert the RR header.
  702. record_route();
  703. # This flag allows to call this route several times
  704. # without inserting several RR headers.
  705. setflag(FLAG_RR_DONE);
  706. }
  707. }
  708. # Look up the domains of the caller and the callee.
  709. #
  710. route[DOMAIN]
  711. {
  712. # Check whether the caller is from a local domain.
  713. lookup_domain("$fd", "@from.uri.host");
  714. # Check whether the callee is at a local domain
  715. lookup_domain("$td", "@ruri.host");
  716. }
  717. # Check domain usage policies and reject illegal requests.
  718. #
  719. route[DOMAIN_POLICY]
  720. {
  721. # If we don't know the domain of the caller nor the domain of the
  722. # callee, somone tries to use our proxy as a relay. However, we
  723. # can only apply this check out-of-dialog requests without a To
  724. # tag. In some cases such as call-forwarding, subsequent requests
  725. # may not include served domain neither as origination nor
  726. # destination (a@A calls b@B who forwards to c@C. A BYE by c@C is
  727. # then From b@B and To a@A. There is no mentioning of c@C despite
  728. # legitimate behaviour of c@C).
  729. if (!isflagset(FLAG_TOTAG) && !$t.did && !$f.did) {
  730. sl_reply("403", "Relaying Forbidden");
  731. drop;
  732. }
  733. }
  734. # The Registrar
  735. #
  736. route[REGISTRAR]
  737. {
  738. # Process only REGISTERs here.
  739. if (method != "REGISTER") {
  740. break;
  741. }
  742. # If this is a replica (sent to the multicast address), trust it to
  743. # be secure and store it in usrloc
  744. if (dst_ip==224.0.1.75) {
  745. if (!isflagset(FLAG_REPL_ENABLED)) {
  746. # Multicast replication administratively disabled.
  747. # Ignore.
  748. drop;
  749. }
  750. # Read marker from master
  751. if (search("^Repl-Marker: nated")) {
  752. setflag(FLAG_NAT);
  753. $uac_nat = 1;
  754. }
  755. # If the replicating server added its own server id to the
  756. # request, obtain the value and store it in an attribute.
  757. # This is used by registrar.
  758. $server_id = @msg.header["SER-Server-ID"];
  759. # Assume URI in form of UID@mydomain and store contacts
  760. # under this UID. Note that this only works if local policy
  761. # causes UIDs to have form compliant to RFC3261 URI
  762. # usernames.
  763. $tu.uid = @ruri.user;
  764. if (!save_mem_nr("location")) {
  765. log(1, "Error while saving replicated REGISTER.\n");
  766. }
  767. drop;
  768. }
  769. else {
  770. # This is a REGISTER request received from the UA. Remove
  771. # our internal header fields if they are present. The may
  772. # have been added maliciously.
  773. remove_hf("SER-Server-ID");
  774. remove_hf("Repl-Marker");
  775. }
  776. # Check if the REGISTER if for one of our local domains.
  777. if (!$t.did) {
  778. sl_reply("403", "Register Forwarding Forbidden");
  779. drop;
  780. }
  781. # The REGISTER target is in the To header, so reload the domain.
  782. if (!lookup_domain("$td", "@to.uri.host")) {
  783. sl_reply("404", "Unknown Domain");
  784. drop;
  785. }
  786. # Useful for clients that ignore expires in 200 (OK). This is an
  787. # attempt to keep them sticking to our value of 600.
  788. append_to_reply("Expires: 600\r\n");
  789. append_to_reply("Min-Expires: 240\r\n");
  790. # We want only authenticated users to be registered.
  791. if (!www_authenticate("$fd.digest_realm", "credentials")) {
  792. if ($? == -2) {
  793. sl_reply("500", "Internal Server Error");
  794. }
  795. else if ($? == -3) {
  796. sl_reply("400", "Bad Request");
  797. }
  798. else {
  799. if ($digest_challenge) {
  800. append_to_reply("%$digest_challenge");
  801. }
  802. sl_reply("401", "Unauthorized");
  803. }
  804. drop;
  805. }
  806. # Check if the authenticated user is the same as the target user.
  807. if (!lookup_user("$tu.uid", "@to.uri")) {
  808. sl_reply("404", "Unknown user in To");
  809. drop;
  810. }
  811. # the authentication ID does not match the ID in the To header
  812. if ($f.uid != $t.uid) {
  813. sl_reply("403", "Authentication and To-Header mismatch");
  814. drop;
  815. }
  816. # Check if the authenticated user is the same as the request
  817. # originator. You may uncomment it if you care, which URI is in
  818. # the From header.
  819. #if (!lookup_user("$fr.uid", "@from.uri")) {
  820. # sl_reply("404", "Unknown user in From");
  821. # drop;
  822. #}
  823. #if ($fu.uid != $fr.uid) {
  824. # sl_reply("403", "Authentication and From-Header mismatch");
  825. # drop;
  826. #}
  827. # Everything is fine. Store the binding.
  828. if (!save_contacts("location")) {
  829. sl_reply("400", "Invalid REGISTER Request");
  830. drop;
  831. }
  832. # do not delete the following 3 lines, they are used by debconf
  833. #DEBCONF-REPLICATION1-START
  834. #
  835. #DEBCONF-REPLICATION1-END
  836. if (isflagset(FLAG_REPL_ENABLED)) {
  837. if (isflagset(FLAG_NAT)) {
  838. append_hf("Repl-Marker: nated\r\n");
  839. }
  840. # Append this server's unique ID to the request
  841. append_hf_value("SER-Server-ID", "%@sys.server_id");
  842. # We are multicasting a successful REGISTER to all proxies
  843. # on the multicast network to replicate the contact
  844. # addresses to all of them. In case they share the same IP
  845. # address (VIP) it is important to set the sending IP
  846. # address to an unshared one (in the future a special mcast
  847. # module may use unbound sockets for sending and leave
  848. # the source IP address decision up to kernel routing
  849. # tables).
  850. #DEBCONF-REPL_SEND_ADDR-START
  851. force_send_socket(udp:127.0.0.1);
  852. #DEBCONF-REPL_SEND_ADDR-END
  853. # Put the UID in the Request-URI so that it doesn't have to
  854. # be looked up in the database by all multicast receivers.
  855. attr2uri("$tu.uid","user");
  856. forward_udp(224.0.1.75,5060);
  857. }
  858. #DEBCONF-REPLICATION2-START
  859. #
  860. #DEBCONF-REPLICATION2-END
  861. drop;
  862. }
  863. # Authentication of request originators claiming to belong to one of our
  864. # domains.
  865. #
  866. route[AUTHENTICATION]
  867. {
  868. # CANCELs and ACKs cannot be challenged.
  869. if (method=="CANCEL" || method=="ACK") {
  870. break;
  871. }
  872. # Requests from non-local to local domains should be permitted.
  873. # Remove this if you want a walled garden.
  874. if (!$f.did) {
  875. break;
  876. }
  877. # Gateways are usually not able to authenticate for their requests.
  878. # You have to trust them base on some other information such as the
  879. # source IP address.
  880. # WARNING: If at all this is only safe in a local network!
  881. if (@src.ip == $gw_ip) {
  882. break;
  883. }
  884. if (!proxy_authenticate("$fd.digest_realm", "credentials")) {
  885. if ($? == -2) {
  886. sl_reply("500", "Internal Server Error");
  887. }
  888. else if ($? == -3) {
  889. sl_reply("400", "Bad Request");
  890. }
  891. else {
  892. if ($digest_challenge) {
  893. append_to_reply("%$digest_challenge");
  894. }
  895. sl_reply("407", "Proxy Authentication Required");
  896. }
  897. drop;
  898. }
  899. # Check if the UID derived from authentication matches that from
  900. # the From header.
  901. if (!lookup_user("$fr.uid", "@from.uri")) {
  902. sl_reply("403", "Fake Identity");
  903. drop;
  904. }
  905. if ($fu.uid != $fr.uid) {
  906. sl_reply("403", "Fake Identity");
  907. drop;
  908. }
  909. setflag(FLAG_AUTH_OK);
  910. # Load the user attributes of the caller.
  911. load_attrs("$fu", "$f.uid");
  912. }
  913. # Process request targeted to non-local domains.
  914. #
  915. route[OUTBOUND]
  916. {
  917. # If a local user calls to a foreign domain we play outbound
  918. # proxy for them.
  919. # Comment this out if you want a walled garden.
  920. if ($f.did && !$t.did) {
  921. append_hf("P-hint: outbound\r\n");
  922. route(FORWARD);
  923. }
  924. }
  925. # Process speeddial addresses.
  926. #
  927. route[SPEEDDIAL]
  928. {
  929. # If the caller is local and uses two digits only, we redirect the
  930. # UA to the real target.
  931. if ($fd.did && uri =~ "sip:[0-9][0-9]@") {
  932. if (sd_lookup("speed_dial")) {
  933. sl_reply("302", "Speed Dial Redirect");
  934. }
  935. else {
  936. sl_reply("404", "Speed Dial Not Found");
  937. }
  938. drop;
  939. }
  940. }
  941. # Process requests targeted to a local user.
  942. #
  943. route[INBOUND]
  944. {
  945. # lets see if know the callee
  946. if (!lookup_user("$tu.uid", "@ruri")) {
  947. break;
  948. }
  949. # Load the attributes of the callee.
  950. load_attrs("$tu", "$t.uid");
  951. # You can check if the called URI is in fact an alias like this.
  952. #if (! $tu.uri_canonical) {
  953. # # If the alias URI has different attributes, you can load
  954. # # them into the URI track like this.
  955. # load_attrs("$tr", "@ruri");
  956. #}
  957. # Check for call forwarding of the callee.
  958. # Note: The forwarding target has to be full routable URI
  959. # in this example.
  960. if ($tu.fwd_always_target) {
  961. attr2uri("$tu.fwd_always_target");
  962. # If we are forwarding to ourselves, don't remove
  963. # credentials. Otherwise the request would be challenged
  964. # again.
  965. # Note: This doesn't apply to failure_route which may
  966. # still be problematic -- credentials are already
  967. # removed when we forward. Consider using a 3xx.
  968. lookup_domain("$td", "@ruri.host");
  969. if ($t.did) {
  970. setflag(FLAG_DONT_RM_CRED);
  971. }
  972. route(FORWARD);
  973. }
  974. # Native SIP destinations are handled using the usrloc database.
  975. if (lookup_contacts("location")) {
  976. append_hf("P-hint: usrloc applied\r\n");
  977. # We set the tm module timers according to the prefences
  978. # of the callee (avoid too long ringing of his phones).
  979. # Note1: Timer values have to be in ms now!
  980. # Note2: This makes even more sense if you switch to a
  981. # voicemail from the FAILURE_ROUTE below.
  982. if ($t.fr_inv_timer) {
  983. if ($t.fr_timer) {
  984. t_set_fr("$t.fr_inv_timer", "$t.fr_timer");
  985. }
  986. else {
  987. t_set_fr("$t.fr_inv_timer");
  988. }
  989. }
  990. # This enables session timer support as long as one side
  991. # supports it. If you want to have session timmer support
  992. # only for calls from your PSTN gateway but not between pure
  993. # VoIP calls you can remove the comment marks from the if
  994. # clause in the next line and closing bracket below.
  995. # WARNING: If at all you should trust IP addresses only in
  996. # your local network!
  997. #if (@src.ip == $gw_ip) {
  998. route(SESSION_TIMER);
  999. #}
  1000. route(FORWARD);
  1001. }
  1002. else {
  1003. sl_reply("480", "Temporarily unavailable");
  1004. drop;
  1005. }
  1006. }
  1007. # Process calls for PSTN.
  1008. #
  1009. route[PSTN]
  1010. {
  1011. # Check some conditions first:
  1012. # PSTN is available for our own users only.
  1013. if (!$f.did) {
  1014. break;
  1015. }
  1016. # If the attribute $gw_ip isn't set, there is no PSTN service
  1017. # active.
  1018. if (!$gw_ip) {
  1019. break;
  1020. }
  1021. # And finally, the username of the Request-URI must look like
  1022. # a phone number.
  1023. if (!uri =~ "^sips?:\+?[0-9]{3,18}@") {
  1024. break;
  1025. }
  1026. # You may have to convert the number in the Request-URI into a
  1027. # format that is accepted by your gateway here.
  1028. # Check permissions of the caller for initial INVITEs.
  1029. if (method == "INVITE" && [email protected]) {
  1030. if (!$f.gw_acl == "1") {
  1031. sl_reply("403", "PSTN Not Permitted");
  1032. drop;
  1033. }
  1034. }
  1035. # If the attribute $asserted_id is set, we add its contents as a
  1036. # Remote-Party-ID header.
  1037. # Depending on your gateway, you may have to add a
  1038. # P-Asserted-Identity header here instead.
  1039. if ($asserted_id) {
  1040. xlset_attr("$rpidheader",
  1041. "<sip:%$asserted_id@%@ruri.host>;screen=yes");
  1042. replace_attr_hf("Remote-Party-ID", "$rpidheader");
  1043. }
  1044. # Enable Session Timer support with the gateway.
  1045. route(SESSION_TIMER);
  1046. # Replace the domain part of the Request-URI with the value from
  1047. # the attribute and send it out.
  1048. attr2uri("$gw_ip", "domain");
  1049. # Set the PSTN_ALLOWED flag. This will be checked on the
  1050. # onsend_route.
  1051. setflag(FLAG_PSTN_ALLOWED);
  1052. route(FORWARD);
  1053. }
  1054. # Try to process CANCEL requests quickly.
  1055. #
  1056. route[CATCH_CANCEL]
  1057. {
  1058. if (method == CANCEL) {
  1059. # t_relay_cancel() will stop processing if a matching
  1060. # INVITE was found.
  1061. if (!t_relay_cancel()) {
  1062. # An INVITE was found but some error occurred.
  1063. sl_reply("500", "Internal Server Error");
  1064. drop;
  1065. }
  1066. # Bad luck, no corresponding INVITE was found, we have to
  1067. # continue with the script.
  1068. }
  1069. }
  1070. # Site specific policy.
  1071. #
  1072. route[SITE_SPECIFIC]
  1073. {
  1074. # This is only relevant for requests for one of our domains.
  1075. if (!$t.did) {
  1076. break;
  1077. }
  1078. # Do site specific routing such as peering.
  1079. # For example:
  1080. if (uri=~"^sip:000777") {
  1081. rewritehostport("sems01.iptel.org:5074");
  1082. route(FORWARD);
  1083. }
  1084. }
  1085. # Process Session-Timer.
  1086. #
  1087. route[SESSION_TIMER]
  1088. {
  1089. # We are only interested in session establishment or session
  1090. # refreshing.
  1091. #
  1092. if (method != "INVITE" && method != "UPDATE") {
  1093. break;
  1094. }
  1095. # Let's check if the Session-Expires header is already present.
  1096. if (@hf_value.session_expires) {
  1097. # Compare the Session-Expires header value with the
  1098. # configured Min-SE.
  1099. eval_push("x:%@hf_value.session_expires.uri");
  1100. eval_oper("(int)", -1);
  1101. eval_push("x:%@cfg_get.session_timer.min_se");
  1102. eval_oper("(int)", -1);
  1103. eval_oper(">=", -2);
  1104. # Let's check for the Suported header.
  1105. if (hf_value_exists("Supported", "timer")) {
  1106. # The UAC supports Session-Timer, so we
  1107. # only need to take a look at the values
  1108. if (@eval.pop[-1] == "0") {
  1109. # Session interval is lower than the
  1110. # configured Min-SE
  1111. append_to_reply("Min-SE: %@cfg_get.session_timer.min_se\r\n");
  1112. sl_reply("422", "Session Interval Too Small");
  1113. drop;
  1114. }
  1115. # We store the session expires value for the reply
  1116. # route and mark the attribute for inserting as
  1117. # Record-Route cookie.
  1118. $stimer = @hf_value.session_expires.uri;
  1119. setavpflag($stimer, "dialog_cookie");
  1120. # Set the session timer flag that indicates the
  1121. # UAC supports the extension.
  1122. setflag(FLAG_SESSIONTIMER);
  1123. }
  1124. else {
  1125. # Session epxires was already inserted by some other
  1126. # proxy
  1127. if (@eval.pop[-1] == "0") {
  1128. # Session interval is lower than the
  1129. # configured Min-SE. There is no point in
  1130. # sending 422 response, because the UAC
  1131. # does not support the extension, the values
  1132. # can be corrected instead.
  1133. assign_hf_value("Session-Expires",
  1134. "%@cfg_get.session_timer.min_se");
  1135. remove_hf_value("Min-SE");
  1136. append_hf_value("Min-SE",
  1137. "%@cfg_get.session_timer.min_se");
  1138. }
  1139. }
  1140. }
  1141. else {
  1142. # No Session Timer is requested yet, neither by UAC nor by
  1143. # proxy
  1144. if (@cfg_get.session_timer.default != "0") {
  1145. # Add a Session Expires header to see if the UAS
  1146. # supports Session Timer. We do not insert a
  1147. # Required header because then the call might fail.
  1148. append_hf_value("Session-Expires",
  1149. "%@cfg_get.session_timer.default");
  1150. if (@cfg_get.session_timer.min_se != "90") {
  1151. append_hf_value("Min-SE",
  1152. "%@cfg_get.session_timer.min_se");
  1153. }
  1154. # Mark the attribute to be inserted as a
  1155. # Record-Route cookie
  1156. $stimer = @cfg_get.session_timer.default;
  1157. setavpflag($stimer, "dialog_cookie");
  1158. }
  1159. }
  1160. }
  1161. # Failure route for initial INVITEs.
  1162. #
  1163. failure_route[FAILURE_ROUTE]
  1164. {
  1165. # Mark that we are operating from a failure route.
  1166. setflag(FLAG_FAILUREROUTE);
  1167. if (t_check_status("486|600")) {
  1168. # If we received a busy and a busy target is set, forward
  1169. # it there.
  1170. # Note: Again, the forwarding target has to be a routeable
  1171. # URI. We redirect using 3xx to avoid possible issues with
  1172. # credentials (if we consumed them, they may be missing in
  1173. # a loop, if we don't consume them, messages are bigger and
  1174. # more vulnerable)
  1175. if ($tu.fwd_busy_target) {
  1176. #attr2uri("$tu.fwd_busy_target");
  1177. #route(FORWARD);
  1178. attr_destination("$tu.fwd_busy_target");
  1179. t_reply("302", "Redirect On Busy");
  1180. }
  1181. # Alternatively, you could forward the request to
  1182. # SEMS/voicemail here
  1183. }
  1184. else if (t_check_status("408|480")) {
  1185. # If we received no answer and the noanswer target is set,
  1186. # forward it there.
  1187. # Note: See above.
  1188. if ($tu.fwd_noanswer_target) {
  1189. #attr2uri("$tu.fwd_noanswer_target");
  1190. #route(FORWARD);
  1191. attr_destination("$tu.fwd_noanswer_target");
  1192. t_reply("302", "Redirect On No Answer");
  1193. }
  1194. }
  1195. }
  1196. # Onreply route that fixes NAT in responses.
  1197. #
  1198. onreply_route[REPLY_ROUTE]
  1199. {
  1200. # Fix the Contact in the reply if it contains a private IP to
  1201. # allow proper routing of in-dialog messages.
  1202. # Do the same if the contact is maddred. See the notes in route
  1203. # [NAT_DETECTION] for more information.
  1204. if (nat_uac_test("1") ||
  1205. (@hf_value["contact"] && @contact.uri.params.maddr))
  1206. {
  1207. fix_nated_contact();
  1208. }
  1209. # If RTP proxy was activated and this is a 18x or 2xx reply with a
  1210. # body, inform RTP proxy.
  1211. if (isflagset(FLAG_RTP_PROXY)
  1212. && status=~"(18[03])|(2[0-9][0-9])"
  1213. && @msg.body)
  1214. {
  1215. force_rtp_proxy('r');
  1216. }
  1217. # Let's check for session timer support.
  1218. if (isflagset(FLAG_SESSIONTIMER) && status =~ "2[0-9][0-9]") {
  1219. # The UAC wanted to have a session timer.
  1220. if (!@hf_value.session_expires) {
  1221. # But the UAS does not support it, so we will try
  1222. # to convince the UAC to do it.
  1223. append_hf_value("Session-Expires",
  1224. "%$stimer;refresher=uac");
  1225. if (!hf_value_exists("Require", "timer")) {
  1226. include_hf_value("Require", "timer");
  1227. }
  1228. }
  1229. }
  1230. }
  1231. # Do some final checks before a request is sent out.
  1232. onsend_route
  1233. {
  1234. # Bypass check: Eliminate requests to the PSTN gateway if they have
  1235. # not passed ACL checks and are not marked with FLAG_PSTN_ALLOWED
  1236. # but are dialog-initiating requests (no to-tag, no CANCEL, no ACK).
  1237. # This helps to stop policy bypasses (gateway IP uploaded as a
  1238. # forked contact, or a call-forwarding destination, or a DNS name,
  1239. # or a preloaded route, or something else possibly)
  1240. if (to_ip==$g.gw_ip && !isflagset(FLAG_PSTN_ALLOWED)
  1241. && !isflagset(FLAG_TOTAG)
  1242. && method != "ACK" && method != "CANCEL")
  1243. {
  1244. log(1, "ALERT: non authorized packet for PSTN, dropping...\n%mb\n");
  1245. # You can't use advanced features from onsend_route.
  1246. # xlog("L_ALERT", "non authorized packet for PSTN, dropping...\n%mb\n");
  1247. drop;
  1248. }
  1249. # RFC 1918 relay protection: Useful if SER is attached to an
  1250. # administrative network using private IP address space and you
  1251. # wish to prevent UACs from relaying their packets there.
  1252. #
  1253. # You will have to comment this out, if you are regularly serving
  1254. # an RFC 1918 address space.
  1255. if (to_ip==10.0.0.0/8 || to_ip==172.16.0.0/12
  1256. || to_ip==192.168.0.0/16)
  1257. {
  1258. log(1, "ALERT: Packet targeted to an RFC1918 address dropped\n");
  1259. drop;
  1260. }
  1261. }
  1262. # Run every minute by the timer module.
  1263. #
  1264. route[ON_1MIN_TIMER] {
  1265. # Cleanup expired location records
  1266. # MySQL version:
  1267. db_query("delete from location where expires<utc_timestamp()");
  1268. # PostgreSQL version:
  1269. #db_query("delete from location where expires<now()");
  1270. # Reload domains if domain table has been changed recently.
  1271. # Note: because global attributes are read-only and we can't
  1272. # easily remember the "last" status, we check for changed
  1273. # timestamp in 2 minute time-interval.
  1274. # MySQL version:
  1275. db_query("select value from global_attrs where name='domain_data_version' and type=0 and cast(value as unsigned int) between unix_timestamp(now())-120 and unix_timestamp(now())", "reload");
  1276. # PostgreSQL version:
  1277. #db_query("select value from global_attrs where name='domain_data_version' and type=0 and cast(value as integer) between date_part('epoch', now() - interval '120 seconds') and date_part('epoch', now())", "reload");
  1278. if (@db.fetch.reload.count=="1") {
  1279. # Domain reload only available as fifo command.
  1280. exec_msg("sercmd domain.reload");
  1281. }
  1282. db_close("reload");
  1283. }