kamctl.base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. #
  2. # control tool for maintaining Kamailio
  3. #
  4. #===================================================================
  5. ##### ----------------------------------------------- #####
  6. ### path to useful tools
  7. locate_tool() {
  8. TOOLPATH=""
  9. while [ -n "$1" ]
  10. do
  11. if [ -x /usr/bin/which ] ; then
  12. TOOLPATH=`which $1 2> /dev/null`
  13. if [ -n "$TOOLPATH" ]; then
  14. return
  15. fi
  16. fi
  17. # look in common locations
  18. if [ -x "/usr/bin/$1" ] ; then
  19. TOOLPATH="/usr/bin/$1"
  20. return
  21. fi
  22. if [ -x "/bin/$1" ] ; then
  23. TOOLPATH="/bin/$1"
  24. return
  25. fi
  26. if [ -x "/usr/local/bin/$1" ] ; then
  27. TOOLPATH="/usr/local/bin/$1"
  28. return
  29. fi
  30. shift
  31. done
  32. return
  33. }
  34. if [ -z "$EGREP" ] ; then
  35. locate_tool egrep
  36. if [ -z "$TOOLPATH" ] ; then
  37. # now error, but we can look for alternative names if it is the case
  38. echo "error: 'egrep' tool not found: set EGREP variable to correct tool path"
  39. exit
  40. fi
  41. EGREP="$TOOLPATH"
  42. fi
  43. if [ -z "$AWK" ] ; then
  44. locate_tool awk
  45. if [ -z "$TOOLPATH" ] ; then
  46. # now error, but we can look for alternative names if it is the case
  47. echo "error: 'awk' tool not found: set AWK variable to correct tool path"
  48. exit
  49. fi
  50. AWK="$TOOLPATH"
  51. fi
  52. if [ -z "$GDB" ] ; then
  53. locate_tool gdb
  54. if [ -z "$TOOLPATH" ] ; then
  55. # now error, but we can look for alternative names if it is the case
  56. GDB=""
  57. # echo "error: 'gdb' tool not found: set GDB variable to correct tool path"
  58. # exit
  59. else
  60. GDB="$TOOLPATH"
  61. fi
  62. fi
  63. if [ -z "$MD5" ]; then
  64. locate_tool md5sum md5
  65. if [ -z "$TOOLPATH" ] ; then
  66. # now error, but we can look for alternative names if it is the case
  67. echo "error: 'md5sum' or 'md5' tool not found: set MD5 variable to correct tool path"
  68. exit
  69. fi
  70. MD5="$TOOLPATH"
  71. fi
  72. if [ -z "$LAST_LINE" ] ; then
  73. locate_tool tail
  74. if [ -z "$TOOLPATH" ] ; then
  75. # now error, but we can look for alternative names if it is the case
  76. echo "error: 'tail' tool not found: set LAST_LINE variable to correct tool path"
  77. exit
  78. fi
  79. LAST_LINE="$TOOLPATH -n 1"
  80. fi
  81. if [ -z "$EXPR" ] ; then
  82. locate_tool expr
  83. if [ -z "$TOOLPATH" ] ; then
  84. # now error, but we can look for alternative names if it is the case
  85. echo "error: 'expr' tool not found: set EXPR variable to correct tool path"
  86. exit
  87. fi
  88. EXPR="$TOOLPATH"
  89. fi
  90. if [ -z "$EXPAND" ] ; then
  91. locate_tool expand
  92. if [ -z "$TOOLPATH" ] ; then
  93. EXPAND="cat"
  94. else
  95. EXPAND="$TOOLPATH -t 2"
  96. fi
  97. fi
  98. ##### ------------------------------------------------ #####
  99. ### configuration for starting/stopping kamailio
  100. if [ -z "$PID_FILE" ] ; then
  101. PID_FILE=/run/kamailio/kamailio.pid
  102. fi
  103. if [ -z "$SYSLOG" ] ; then
  104. SYSLOG=1 # 0=output to console, 1=output to syslog
  105. fi
  106. if [ -z "$STARTOPTIONS" ] ; then
  107. STARTOPTIONS= # for example -dddd
  108. fi
  109. if [ -z "$DIR" ] ; then
  110. DIR=`dirname $0`
  111. fi
  112. if [ -z "$KAMBIN" ] ; then
  113. KAMBIN=$DIR/kamailio
  114. fi
  115. ##### ------------------------------------------------ #####
  116. ### aliases configuration
  117. #
  118. ENABLE_ALIASES=0
  119. if [ "$ALIASES_TYPE" = "UL" ] ; then
  120. ENABLE_ALIASES=1
  121. else
  122. if [ "$ALIASES_TYPE" = "DB" ] ; then
  123. ENABLE_ALIASES=2
  124. fi
  125. fi
  126. ##### ------------------------------------------------ #####
  127. ### ACL name verification
  128. if [ -z "$VERIFY_ACL" ] ; then
  129. VERIFY_ACL=1
  130. fi
  131. if [ -z "$ACL_GROUPS" ] ; then
  132. ACL_GROUPS="local ld int voicemail free-pstn"
  133. fi
  134. if [ -z "$VERIFY_USER" ] ; then
  135. VERIFY_USER=1
  136. fi
  137. ##### ----------------------------------------------- #####
  138. #### Defined values
  139. ALL_METHODS=4294967295
  140. USERNAME_RE="[-a-zA-Z0-9&=\+\$,;\?/_\.\!~\*'\(\)]+"
  141. ##### ----------------------------------------------- #####
  142. #### database tables for SQL databases and DBTEXT
  143. SRDB_LOAD_SER=$((1 << 0)) # The row should be loaded by SER
  144. SRDB_DISABLED=$((1 << 1)) # The row is disabled
  145. SRDB_CANON=$((1 << 2)) # Canonical entry (domain or uri)
  146. SRDB_IS_TO=$((1 << 3)) # The URI can be used in To
  147. SRDB_IS_FROM=$((1 << 4)) # The URI can be used in From
  148. SRDB_FOR_SERWEB=$((1 << 5)) # Credentials instance can be used by serweb
  149. SRDB_PENDING=$((1 << 6))
  150. SRDB_DELETED=$((1 << 7))
  151. SRDB_CALLER_DELETED=$((1 << 8)) # Accounting table
  152. SRDB_CALLEE_DELETED=$((1 << 9)) # Accounting table
  153. SRDB_MULTIVALUE=$((1 << 10)) # Attr_types table
  154. SRDB_FILL_ON_REG=$((1 << 11)) # Attr_types table
  155. SRDB_REQUIRED=$((1 << 12)) # Attr_types table
  156. SRDB_DIR=$((1 << 13)) # Domain_settings table
  157. # UsrLoc Table
  158. if [ -z "$UL_TABLE" ] ; then
  159. UL_TABLE=location
  160. fi
  161. USER_COLUMN=username
  162. DOMAIN_COLUMN=domain
  163. CALLID_COLUMN=callid
  164. # subscriber table
  165. if [ -z "$SUB_TABLE" ] ; then
  166. SUB_TABLE=subscriber
  167. fi
  168. REALM_COLUMN=domain
  169. HA1_COLUMN=ha1
  170. HA1B_COLUMN=ha1b
  171. PASSWORD_COLUMN=password
  172. RPID_COLUMN=rpid
  173. SUBSCRIBER_COLUMN='username'
  174. PHP_LIB_COLUMN=phplib_id
  175. if [ -z "$STORE_PLAINTEXT_PW" ] ; then
  176. STORE_PLAINTEXT_PW=1
  177. fi
  178. # acl table
  179. if [ -z "$ACL_TABLE" ] ; then
  180. ACL_TABLE=grp
  181. fi
  182. ACL_USER_COLUMN=username
  183. ACL_DOMAIN_COLUMN=domain
  184. ACL_GROUP_COLUMN=grp
  185. ACL_MODIFIED_COLUMN=last_modified
  186. # aliases table
  187. if [ -z "$ALS_TABLE" ] ; then
  188. ALS_TABLE=aliases
  189. fi
  190. A_USER_COLUMN=username
  191. A_CONTACT_COLUMN=contact
  192. A_EXPIRES_COLUMN=expires
  193. A_Q_COLUMN=q
  194. A_CALLID_COLUMN=callid
  195. A_CSEQ_COLUMN=cseq
  196. A_LAST_MODIFIED_COLUMN=last_modified
  197. # domain table
  198. if [ -z "$DOMAIN_TABLE" ] ; then
  199. DOMAIN_TABLE=domain
  200. fi
  201. DO_DOMAIN_COLUMN=domain
  202. DO_LAST_MODIFIED_COLUMN=last_modified
  203. # uid_domain table
  204. if [ -z "$UID_DOMAIN_TABLE" ] ; then
  205. UID_DOMAIN_TABLE=uid_domain
  206. fi
  207. UID_DO_DOMAIN_COLUMN=domain
  208. UID_DO_DID_COLUMN=did
  209. UID_DO_FLAGS_COLUMN=flags
  210. # lcr tables
  211. if [ -z "$LCR_TABLE" ] ; then
  212. LCR_TABLE=lcr
  213. fi
  214. LCR_ID_COLUMN=lcr_id
  215. LCR_PREFIX_COLUMN=prefix
  216. LCR_GRPID_COLUMN=grp_id
  217. # gw table
  218. if [ -z "$GW_TABLE" ] ; then
  219. GW_TABLE=gw
  220. fi
  221. # carrier_name table
  222. if [ -z "$CARRIER_NAME_TABLE" ] ; then
  223. CARRIER_NAME_TABLE=carrier_name
  224. fi
  225. CARRIERROUTE_CARRIER_NAME_ID_COLUMN=id
  226. CARRIERROUTE_CARRIER_NAME_CARRIER_COLUMN=carrier
  227. # domain_name table
  228. if [ -z "$DOMAIN_NAME_TABLE" ] ; then
  229. DOMAIN_NAME_TABLE=domain_name
  230. fi
  231. CARRIERROUTE_DOMAIN_NAME_ID_COLUMN=id
  232. CARRIERROUTE_DOMAIN_NAME_DOMAIN_COLUMN=domain
  233. # carrierroute table
  234. if [ -z "$CARRIERROUTE_TABLE" ] ; then
  235. CARRIERROUTE_TABLE=carrierroute
  236. fi
  237. CARRIERROUTE_CARRIERROUTE_PREFIX_COLUMN=id
  238. CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN=carrier
  239. CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN=scan_prefix
  240. CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN=domain
  241. CARRIERROUTE_CARRIERROUTE_PROB_COLUMN=prob
  242. CARRIERROUTE_CARRIERROUTE_STRIP_COLUMN=strip
  243. CARRIERROUTE_CARRIERROUTE_REWRITE_HOST_COLUMN=rewrite_host
  244. CARRIERROUTE_CARRIERROUTE_REWRITE_PREFIX_COLUMN=rewrite_prefix
  245. CARRIERROUTE_CARRIERROUTE_REWRITE_SUFFIX_COLUMN=rewrite_suffix
  246. CARRIERROUTE_CARRIERROUTE_COMMENT_COLUMN=description
  247. CARRIERROUTE_CARRIERROUTE_FLAGS_COLUMN=flags
  248. CARRIERROUTE_CARRIERROUTE_MASK_COLUMN=mask
  249. # URI table
  250. if [ -z "$URI_TABLE" ] ; then
  251. URI_TABLE=uri
  252. fi
  253. URIUSER_COLUMN=uri_user
  254. MODIFIED_COLUMN=last_modified
  255. # dbaliases table
  256. if [ -z "$DA_TABLE" ] ; then
  257. DA_TABLE=dbaliases
  258. fi
  259. DA_USER_COLUMN=username
  260. DA_DOMAIN_COLUMN=domain
  261. DA_ALIAS_USER_COLUMN=alias_username
  262. DA_ALIAS_DOMAIN_COLUMN=alias_domain
  263. # speeddial table
  264. if [ -z "$SD_TABLE" ] ; then
  265. SD_TABLE=speed_dial
  266. fi
  267. SD_USER_COLUMN=username
  268. SD_DOMAIN_COLUMN=domain
  269. SD_SD_USER_COLUMN=sd_username
  270. SD_SD_DOMAIN_COLUMN=sd_domain
  271. SD_NEW_URI_COLUMN=new_uri
  272. SD_DESC_COLUMN=description
  273. # avp table
  274. if [ -z "$AVP_TABLE" ] ; then
  275. AVP_TABLE=usr_preferences
  276. fi
  277. AVP_UUID_COLUMN=uuid
  278. AVP_USER_COLUMN=username
  279. AVP_DOMAIN_COLUMN=domain
  280. AVP_ATTRIBUTE_COLUMN=attribute
  281. AVP_VALUE_COLUMN=value
  282. AVP_TYPE_COLUMN=type
  283. AVP_MODIFIED_COLUMN=last_modified
  284. # trusted table
  285. if [ -z "$TRUSTED_TABLE" ] ; then
  286. TRUSTED_TABLE=trusted
  287. fi
  288. TRUSTED_SRC_IP_COLUMN=src_ip
  289. TRUSTED_PROTO_COLUMN=proto
  290. TRUSTED_FROM_PATTERN_COLUMN=from_pattern
  291. TRUSTED_TAG_COLUMN=tag
  292. # address table
  293. if [ -z "$ADDRESS_TABLE" ] ; then
  294. ADDRESS_TABLE=address
  295. fi
  296. # dispatcher tables
  297. if [ -z "$DISPATCHER_TABLE" ] ; then
  298. DISPATCHER_TABLE=dispatcher
  299. fi
  300. DISPATCHER_ID_COLUMN=id
  301. DISPATCHER_SETID_COLUMN=setid
  302. DISPATCHER_DESTINATION_COLUMN=destination
  303. DISPATCHER_FLAGS_COLUMN=flags
  304. DISPATCHER_PRIORITY_COLUMN=priority
  305. DISPATCHER_ATTRS_COLUMN=attrs
  306. DISPATCHER_DESCRIPTION_COLUMN=description
  307. # dialog tables
  308. if [ -z "$DIALOG_TABLE" ] ; then
  309. DIALOG_TABLE=dialog
  310. fi
  311. # dialplan tables
  312. if [ -z "$DIALPLAN_TABLE" ] ; then
  313. DIALPLAN_TABLE=dialplan
  314. fi
  315. DIALPLAN_ID_COLUMN=id
  316. DIALPLAN_DPID_COLUMN=dpid
  317. DIALPLAN_PR_COLUMN=pr
  318. DIALPLAN_MATCH_OP_COLUMN=match_op
  319. DIALPLAN_MATCH_EXP_COLUMN=match_exp
  320. DIALPLAN_MATCH_LEN_COLUMN=match_len
  321. DIALPLAN_SUBST_EXP_COLUMN=subst_exp
  322. DIALPLAN_REPL_EXP_COLUMN=repl_exp
  323. DIALPLAN_ATTRS_COLUMN=attrs
  324. # ACC table
  325. if [ -z "$ACC_TABLE" ] ; then
  326. ACC_TABLE=acc
  327. fi
  328. #
  329. ##### ------------------------------------------------ #####
  330. ### usage functions
  331. #
  332. usage_base() {
  333. echo
  334. mecho " -- command 'start|stop|restart|trap'"
  335. echo
  336. cat <<EOF
  337. trap ............................... trap with gdb Kamailio processes using RPC
  338. pstrap ............................. trap with gdb Kamailio processes using ps
  339. restart ............................ restart Kamailio
  340. start .............................. start Kamailio
  341. stop ............................... stop Kamailio
  342. EOF
  343. }
  344. usage_tls() {
  345. echo
  346. mecho " -- command 'tls'"
  347. echo
  348. cat <<EOF
  349. tls rootCA [<etcdir>] ............ creates new rootCA
  350. tls userCERT <user> [<etcdir>] ... creates user certificate
  351. * default <etcdir> is $ETCDIR/tls
  352. tls gen-certs [<domain>] ......... generate self signed certificate
  353. EOF
  354. }
  355. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_base"
  356. usage_acl() {
  357. echo
  358. mecho " -- command 'acl' - manage access control lists (acl)"
  359. echo
  360. cat <<EOF
  361. acl show [<username>] .............. show user membership
  362. acl grant <username> <group> ....... grant user membership (*)
  363. acl revoke <username> [<group>] .... grant user membership(s) (*)
  364. EOF
  365. }
  366. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_acl"
  367. usage_lcr() {
  368. echo
  369. mecho " -- command 'lcr' - manage least cost routes (lcr)"
  370. echo
  371. cat <<EOF
  372. lcr show_gws....... show database gateways
  373. lcr show_routes.... show database routes
  374. lcr dump_gws....... show in memory gateways
  375. lcr dump_routes.... show in memory routes
  376. lcr reload ........ reload lcr gateways and routes
  377. lcr eval_weights .. evaluates probability for given GW's weights
  378. EOF
  379. }
  380. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_lcr"
  381. usage_cr() {
  382. echo
  383. mecho " -- command 'cr' - manage carrierroute tables"
  384. echo
  385. cat <<EOF
  386. cr show ....................................................... show tables
  387. cr reload ..................................................... reload tables
  388. cr dump ....................................................... show in memory tables
  389. cr addcn <carrier id> <carrier name> .......................... add a carrier name
  390. cr rmcn <carrier id> ......................................... rm a carrier name
  391. cr adddn <domain id> <domain name> ............................ add a domain name
  392. cr rmdn <domain id> .......................................... rm a domain name
  393. cr addcarrier <carrier> <scan_prefix> <domain> <rewrite_host> ................
  394. <prob> <strip> <rewrite_prefix> <rewrite_suffix> ...............
  395. <flags> <mask> <comment> .........................add a carrier
  396. (prob, strip, rewrite_prefix, rewrite_suffix,...................
  397. flags, mask and comment are optional arguments) ...............
  398. cr rmcarrier <carrier> <scan_prefix> <domain> ................ rm a carrier
  399. EOF
  400. }
  401. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_cr"
  402. usage_rpid() {
  403. echo
  404. mecho " -- command 'rpid' - manage Remote-Party-ID (RPID)"
  405. echo
  406. cat <<EOF
  407. rpid add <username> <rpid> ......... add rpid for a user (*)
  408. rpid rm <username> ................. set rpid to NULL for a user (*)
  409. rpid show <username> ............... show rpid of a user
  410. EOF
  411. }
  412. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_rpid"
  413. usage_subscriber() {
  414. echo
  415. mecho " -- command 'add|passwd|rm' - manage subscribers"
  416. echo
  417. cat <<EOF
  418. add <username> <password> .......... add a new subscriber (*)
  419. show <username> .................... show subscriber attributes (*)
  420. passwd <username> <passwd> ......... change user's password (*)
  421. rm <username> ...................... delete a user (*)
  422. sets <username> <attr> <val> ....... set string attribute (column value)
  423. setn <username> <attr> <val> ....... set numeric attribute (column value)
  424. EOF
  425. }
  426. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_subscriber"
  427. usage_trusted() {
  428. echo
  429. mecho " -- command 'add|dump|reload|rm|show' - manage trusted"
  430. echo
  431. cat <<EOF
  432. trusted show ...................... show db content
  433. trusted dump ...................... show cache content
  434. trusted reload .................... reload db table into cache
  435. trusted add <src_ip> <proto> <from_pattern> <tag>
  436. ....................... add a new entry
  437. ....................... (from_pattern and tag are optional arguments)
  438. trusted rm <src_ip> ............... remove all entries for the given src_ip
  439. EOF
  440. }
  441. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_trusted"
  442. usage_address() {
  443. echo
  444. mecho " -- command 'add|dump|reload|rm|show' - manage address"
  445. echo
  446. cat <<EOF
  447. address show ...................... show db content
  448. address dump ...................... show cache content
  449. address reload .................... reload db table into cache
  450. address add <grp> <ipaddr> <mask> <port> <tag>
  451. ....................... add a new entry
  452. ....................... (mask, port and tag are optional arguments)
  453. address rm <grp> <ipaddr> ......... remove entries for given grp and ipaddr
  454. EOF
  455. }
  456. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_address"
  457. usage_dispatcher() {
  458. echo
  459. mecho " -- command 'dispatcher' - manage dispatcher"
  460. echo
  461. cat <<EOF
  462. * Examples: dispatcher add 1 sip:1.2.3.1:5050 1 5 'prefix=123' 'gw one'
  463. * dispatcher add 2 sip:1.2.3.4:5050 3 0
  464. * dispatcher rm 4
  465. dispatcher show ..................... show dispatcher gateways
  466. dispatcher reload ................... reload dispatcher gateways
  467. dispatcher dump ..................... show in memory dispatcher gateways
  468. dispatcher add <setid> <destination> [flags] [priority] [attrs] [description]
  469. .......................... add gateway
  470. dispatcher rm <id> .................. delete gateway
  471. dispatcher rmip <ip> <setid>......... delete gateway <ip> in <setid>
  472. dispatcher rmset <setid> ............. delete all gateways in <setid>
  473. EOF
  474. }
  475. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_dispatcher"
  476. usage_dialog() {
  477. echo
  478. mecho " -- command 'dialog' - manage dialog records"
  479. echo
  480. cat <<EOF
  481. * Examples: dialog show
  482. * dialog showdb
  483. dialog show ..................... show in-memory dialog records
  484. dialog showdb ................... show database dialog records
  485. EOF
  486. }
  487. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_dialog"
  488. usage_dialplan() {
  489. echo
  490. mecho " -- command 'dialplan' - manage dialplans"
  491. echo
  492. cat <<EOF
  493. dialplan show <dpid> .............. show dialplan tables
  494. dialplan reload ................... reload dialplan tables
  495. dialplan addrule <dpid> <prio> <match_op> <match_exp>
  496. <match_len> <subst_exp> <repl_exp> <attrs>
  497. .................... add a rule
  498. dialplan rm ....................... removes the entire dialplan table
  499. dialplan rmdpid <dpid> ............ removes all the gived dpid entries
  500. dialplan rmrule <dpid> <prio> ..... removes all the gived dpid/prio entries
  501. EOF
  502. }
  503. usage_ksr_srv() {
  504. echo
  505. mecho " -- command 'srv' - server management commands"
  506. echo
  507. cat <<EOF
  508. * Examples: srv sockets
  509. * srv rpclist
  510. srv sockets ................... show the list of listen sockets
  511. srv aliases ................... show the list of server aliases
  512. srv rpclist ................... show the list of server rpc commands
  513. srv debug [<level>] ........... control the server debug level
  514. srv modules ................... show the list of loaded modules
  515. srv version ................... show the server version
  516. EOF
  517. }
  518. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_ksr_srv"
  519. usage_mtree() {
  520. echo
  521. mecho " -- command 'add|dump|reload|rm|show' - manage mtree"
  522. echo
  523. cat <<EOF
  524. mtree show <tname> .................. show db content
  525. mtree dump [<tname>] ................ show cache content
  526. mtree reload [<tname>] .............. reload db table into cache
  527. mtree add <tname> <tprefix> <tvalue>
  528. ......................... add a new entry
  529. mtree rm <tname> <tprefix> .......... remove entries for given tname and tprefix
  530. EOF
  531. }
  532. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_mtree"
  533. usage_acc() {
  534. echo
  535. mecho " -- command 'acc' - manage accounting records"
  536. echo
  537. cat <<EOF
  538. acc initdb .................. init acc table by adding extra columns
  539. acc showdb .................. show content of acc table
  540. recent [<secs>] ............. show most recent records in acc (default 300s)
  541. EOF
  542. }
  543. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_acc"
  544. ##### ----------------------------------------------- #####
  545. #### Common functions
  546. mdbg() {
  547. if [ "0$VERBOSE" -ne 0 ] ; then
  548. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  549. printf "\033[1m%s\033[0m\n" "$1"
  550. else
  551. echo "$1"
  552. fi
  553. fi
  554. }
  555. mwarn() {
  556. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  557. printf "\033[32;1mWARNING: %s\033[0m\n" "$1"
  558. else
  559. echo "** WARNING: $1"
  560. fi
  561. }
  562. minfo() {
  563. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  564. printf "\033[33;1mINFO: %s\033[0m\n" "$1"
  565. else
  566. echo "** INFO: $1"
  567. fi
  568. }
  569. mecho() {
  570. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  571. printf "\033[1m%s\033[0m\n" "$1"
  572. else
  573. echo "$1"
  574. fi
  575. }
  576. merr() {
  577. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  578. printf "\033[31;1mERROR: %s\033[0m\n" "$1"
  579. else
  580. echo "** ERROR: $1"
  581. fi
  582. }
  583. # determine host name, typically for use in printing UAC
  584. # messages; we use today a simplistic but portable uname -n way --
  585. # no domain name is displayed ; fifo_uac expands !! to host
  586. # address only for optional header fields; uname output without
  587. # domain is sufficient for informational header fields such as
  588. # From
  589. #
  590. get_my_host() {
  591. if [ -z "$SIP_DOMAIN" ]; then
  592. uname -n
  593. else
  594. echo "$SIP_DOMAIN"
  595. fi
  596. }
  597. # calculate name and domain of current user
  598. set_user() {
  599. OSERUSER=`echo $1|$AWK -F@ '{print $1}'`
  600. OSERDOMAIN=`echo $1|$AWK -F@ '{print $2}'`
  601. if [ -z "$OSERDOMAIN" ] ; then
  602. OSERDOMAIN="$SIP_DOMAIN"
  603. fi
  604. if [ -z "$OSERDOMAIN" ] ; then
  605. merr "domain unknown: use usernames with domain or set default domain \
  606. in SIP_DOMAIN"
  607. exit 1
  608. fi
  609. }
  610. # check the parameter if it is a valid address of record (user@domain)
  611. check_aor() {
  612. echo "$1" | $EGREP "^$USERNAME_RE@.+" >/dev/null
  613. if [ $? -ne 0 ] ; then
  614. echo "error: invalid AoR: $1" >&2
  615. exit 1
  616. fi
  617. }
  618. # check the parameter if it is a valid address of record (user@domain)
  619. is_aor() {
  620. echo "$1" | $EGREP "^$USERNAME_RE@.+" >/dev/null
  621. if [ $? -ne 0 ] ; then
  622. false
  623. else
  624. true
  625. fi
  626. }
  627. # check the parameter if it is a valid SIP address of record (sip:user@domain)
  628. check_sipaor() {
  629. echo "$1" | $EGREP "^sip(s)?:$USERNAME_RE@.+" >/dev/null
  630. if [ $? -ne 0 ] ; then
  631. echo "error: invalid SIP AoR: $1" >&2
  632. exit 1
  633. fi
  634. }
  635. # check the parameter if it is a valid SIP URI
  636. # quite simplified now -- it captures just very basic
  637. # errors
  638. check_uri() {
  639. echo "$1" | $EGREP "^sip(s)?:($USERNAME_RE@)?.+" > /dev/null
  640. if [ $? -ne 0 ] ; then
  641. echo "error: invalid SIP URI: $1" >&2
  642. exit 1
  643. fi
  644. }
  645. print_status() {
  646. echo $1 | $EGREP "^[1-6][0-9][0-9]" > /dev/null
  647. if [ "$?" -eq 0 ] ; then
  648. echo $1
  649. else
  650. echo "200 OK"
  651. fi
  652. }
  653. # process output from FIFO/Unixsock server; if everything is ok
  654. # skip the first "ok" line and proceed to returned
  655. # parameters
  656. filter_fl()
  657. {
  658. # tail +2
  659. $AWK 'BEGIN {line=0;IGNORECASE=1;}
  660. {line++}
  661. NR == 1 && /^200 OK/ { next }
  662. /^$/ { next }
  663. { print }'
  664. }
  665. # process jsonrpc output
  666. filter_json()
  667. {
  668. $AWK 'function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
  669. BEGIN { line=0; IGNORECASE=1; }
  670. { line++; }
  671. NR == 1 && /^[{].+"id"[ \t]*:[ \t]*[0-9]+[ \t]*}$/ { print; next; }
  672. NR == 1 && /^200 OK/ { next; }
  673. /^[ \t]*"jsonrpc":[ \t]*"2.0"/ { print; next; }
  674. /^[ \t]*"result":[ \t]*\[.+/ {
  675. n=split($0, a, ",");
  676. for (i=1; i<=n; i++) {
  677. if (i==1) {
  678. m=split(a[i], b, "[");
  679. print b[1] "[";
  680. if (substr(b[2], length(b[2]), 1)=="[" || substr(b[2], length(b[2]), 1)=="{") {
  681. print "\t\t" ltrim(b[2]);
  682. } else {
  683. print "\t\t" ltrim(b[2]) ",";
  684. }
  685. } else {
  686. if (match(a[i], ".+]$")) {
  687. print "\t\t" ltrim(substr(a[i], 1, length(a[i])-1));
  688. print "\t],";
  689. } else {
  690. if(length(a[i])>0) print "\t\t" ltrim(a[i]) ",";
  691. }
  692. }
  693. }
  694. next;
  695. }
  696. /[ \t]*}],/ {
  697. print substr($0, 1, length($0)-2);
  698. print "\t],";
  699. next;
  700. }
  701. /^$/ { next; }
  702. { print; }'
  703. }
  704. # params: user, realm, password
  705. # output: HA1
  706. _gen_ha1()
  707. {
  708. HA1=`echo -n "$1:$2:$3" | $MD5 | $AWK '{ print $1 }'`
  709. if [ $? -ne 0 ] ; then
  710. echo "HA1 calculation failed"
  711. exit 1
  712. fi
  713. }
  714. # params: user, realm, password
  715. # output: HA1B
  716. _gen_ha1b()
  717. {
  718. HA1B=`echo -n "$1@$2:$2:$3" | $MD5 | $AWK '{ print $1 }'`
  719. if [ $? -ne 0 ] ; then
  720. echo "HA1B calculation failed"
  721. exit 1
  722. fi
  723. }
  724. # params: user, realm, password
  725. # output: PHPLIB_ID
  726. _gen_phplib_id()
  727. {
  728. NOW=`date`;
  729. PHPLIB_ID=`echo -n "$1$2:$3:$NOW" | $MD5 | $AWK '{ print $1 }'`
  730. }
  731. # params: user, password
  732. # output: HA1, HA1B
  733. credentials()
  734. {
  735. set_user $1
  736. _gen_ha1 "$OSERUSER" "$OSERDOMAIN" "$2"
  737. _gen_ha1b "$OSERUSER" "$OSERDOMAIN" "$2"
  738. }