kamctl.base 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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`
  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=/var/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
  338. restart ............................ restart Kamailio
  339. start .............................. start Kamailio
  340. stop ............................... stop Kamailio
  341. EOF
  342. }
  343. usage_tls() {
  344. echo
  345. mecho " -- command 'tls'"
  346. echo
  347. cat <<EOF
  348. tls rootCA [<etcdir>] .......... creates new rootCA
  349. tls userCERT <user> [<etcdir>] ... creates user certificate
  350. default <etcdir> is $ETCDIR/tls
  351. EOF
  352. }
  353. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_base"
  354. usage_acl() {
  355. echo
  356. mecho " -- command 'acl' - manage access control lists (acl)"
  357. echo
  358. cat <<EOF
  359. acl show [<username>] .............. show user membership
  360. acl grant <username> <group> ....... grant user membership (*)
  361. acl revoke <username> [<group>] .... grant user membership(s) (*)
  362. EOF
  363. }
  364. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_acl"
  365. usage_lcr() {
  366. echo
  367. mecho " -- command 'lcr' - manage least cost routes (lcr)"
  368. echo
  369. cat <<EOF
  370. lcr show_gws....... show database gateways
  371. lcr show_routes.... show database routes
  372. lcr dump_gws....... show in memory gateways
  373. lcr dump_routes.... show in memory routes
  374. lcr reload ........ reload lcr gateways and routes
  375. lcr eval_weights .. evaluates probability for given GW's weights
  376. EOF
  377. }
  378. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_lcr"
  379. usage_cr() {
  380. echo
  381. mecho " -- command 'cr' - manage carrierroute tables"
  382. echo
  383. cat <<EOF
  384. cr show ....................................................... show tables
  385. cr reload ..................................................... reload tables
  386. cr dump ....................................................... show in memory tables
  387. cr addcn <carrier id> <carrier name> .......................... add a carrier name
  388. cr rmcn <carrier id> ......................................... rm a carrier name
  389. cr adddn <domain id> <domain name> ............................ add a domain name
  390. cr rmdn <domain id> .......................................... rm a domain name
  391. cr addcarrier <carrier> <scan_prefix> <domain> <rewrite_host> ................
  392. <prob> <strip> <rewrite_prefix> <rewrite_suffix> ...............
  393. <flags> <mask> <comment> .........................add a carrier
  394. (prob, strip, rewrite_prefix, rewrite_suffix,...................
  395. flags, mask and comment are optional arguments) ...............
  396. cr rmcarrier <carrier> <scan_prefix> <domain> ................ rm a carrier
  397. EOF
  398. }
  399. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_cr"
  400. usage_rpid() {
  401. echo
  402. mecho " -- command 'rpid' - manage Remote-Party-ID (RPID)"
  403. echo
  404. cat <<EOF
  405. rpid add <username> <rpid> ......... add rpid for a user (*)
  406. rpid rm <username> ................. set rpid to NULL for a user (*)
  407. rpid show <username> ............... show rpid of a user
  408. EOF
  409. }
  410. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_rpid"
  411. usage_subscriber() {
  412. echo
  413. mecho " -- command 'add|passwd|rm' - manage subscribers"
  414. echo
  415. cat <<EOF
  416. add <username> <password> .......... add a new subscriber (*)
  417. show <username> .................... show subscriber attributes (*)
  418. passwd <username> <passwd> ......... change user's password (*)
  419. rm <username> ...................... delete a user (*)
  420. sets <username> <attr> <val> ....... set string attribute (column value)
  421. setn <username> <attr> <val> ....... set numeric attribute (column value)
  422. EOF
  423. }
  424. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_subscriber"
  425. usage_trusted() {
  426. echo
  427. mecho " -- command 'add|dump|reload|rm|show' - manage trusted"
  428. echo
  429. cat <<EOF
  430. trusted show ...................... show db content
  431. trusted dump ...................... show cache content
  432. trusted reload .................... reload db table into cache
  433. trusted add <src_ip> <proto> <from_pattern> <tag>
  434. ....................... add a new entry
  435. ....................... (from_pattern and tag are optional arguments)
  436. trusted rm <src_ip> ............... remove all entries for the given src_ip
  437. EOF
  438. }
  439. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_trusted"
  440. usage_address() {
  441. echo
  442. mecho " -- command 'add|dump|reload|rm|show' - manage address"
  443. echo
  444. cat <<EOF
  445. address show ...................... show db content
  446. address dump ...................... show cache content
  447. address reload .................... reload db table into cache
  448. address add <grp> <ipaddr> <mask> <port> <tag>
  449. ....................... add a new entry
  450. ....................... (mask, port and tag are optional arguments)
  451. address rm <grp> <ipaddr> ......... remove entries for given grp and ipaddr
  452. EOF
  453. }
  454. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_address"
  455. usage_dispatcher() {
  456. echo
  457. mecho " -- command 'dispatcher' - manage dispatcher"
  458. echo
  459. cat <<EOF
  460. * Examples: dispatcher add 1 sip:1.2.3.1:5050 1 5 'prefix=123' 'gw one'
  461. * dispatcher add 2 sip:1.2.3.4:5050 3 0
  462. * dispatcher rm 4
  463. dispatcher show ..................... show dispatcher gateways
  464. dispatcher reload ................... reload dispatcher gateways
  465. dispatcher dump ..................... show in memory dispatcher gateways
  466. dispatcher add <setid> <destination> [flags] [priority] [attrs] [description]
  467. .......................... add gateway
  468. dispatcher rm <id> .................. delete gateway
  469. EOF
  470. }
  471. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_dispatcher"
  472. usage_dialog() {
  473. echo
  474. mecho " -- command 'dialog' - manage dialog records"
  475. echo
  476. cat <<EOF
  477. * Examples: dialog show
  478. * dialog showdb
  479. dialog show ..................... show in-memory dialog records
  480. dialog showdb ................... show database dialog records
  481. EOF
  482. }
  483. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_dialog"
  484. usage_dialplan() {
  485. echo
  486. mecho " -- command 'dialplan' - manage dialplans"
  487. echo
  488. cat <<EOF
  489. dialplan show <dpid> .............. show dialplan tables
  490. dialplan reload ................... reload dialplan tables
  491. dialplan addrule <dpid> <prio> <match_op> <match_exp>
  492. <match_len> <subst_exp> <repl_exp> <attrs>
  493. .................... add a rule
  494. dialplan rm ....................... removes the entire dialplan table
  495. dialplan rmdpid <dpid> ............ removes all the gived dpid entries
  496. dialplan rmrule <dpid> <prio> ..... removes all the gived dpid/prio entries
  497. EOF
  498. }
  499. usage_ksr_srv() {
  500. echo
  501. mecho " -- command 'srv' - server management commands"
  502. echo
  503. cat <<EOF
  504. * Examples: srv sockets
  505. * srv rpclist
  506. srv sockets ................... show the list of listen sockets
  507. srv aliases ................... show the list of server aliases
  508. srv rpclist ................... show the list of server rpc commands
  509. srv version ................... show the server version
  510. EOF
  511. }
  512. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_ksr_srv"
  513. usage_mtree() {
  514. echo
  515. mecho " -- command 'add|dump|reload|rm|show' - manage mtree"
  516. echo
  517. cat <<EOF
  518. mtree show <tname> .................. show db content
  519. mtree dump [<tname>] ................ show cache content
  520. mtree reload [<tname>] .............. reload db table into cache
  521. mtree add <tname> <tprefix> <tvalue>
  522. ......................... add a new entry
  523. mtree rm <tname> <tprefix> .......... remove entries for given tname and tprefix
  524. EOF
  525. }
  526. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_mtree"
  527. usage_acc() {
  528. echo
  529. mecho " -- command 'acc' - manage accounding records"
  530. echo
  531. cat <<EOF
  532. acc initdb .................. init acc table by adding extra colums
  533. acc showdb .................. show content of acc table
  534. recent [<secs>] ............. show most recent records in acc (default 300s)
  535. EOF
  536. }
  537. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_acc"
  538. ##### ----------------------------------------------- #####
  539. #### Common functions
  540. mdbg() {
  541. if [ "0$VERBOSE" -ne 0 ] ; then
  542. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  543. echo -e "\033[1m$1\033[0m"
  544. else
  545. echo "$1"
  546. fi
  547. fi
  548. }
  549. mwarn() {
  550. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  551. echo -e '\E[37;32m'"\033[1mWARNING: $1\033[0m"
  552. else
  553. echo "** WARNING: $1"
  554. fi
  555. }
  556. minfo() {
  557. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  558. echo -e '\E[37;33m'"\033[1mINFO: $1\033[0m"
  559. else
  560. echo "** INFO: $1"
  561. fi
  562. }
  563. mecho() {
  564. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  565. echo -e "\033[1m$1\033[0m"
  566. else
  567. echo "$1"
  568. fi
  569. }
  570. merr() {
  571. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  572. echo -e '\E[37;31m'"\033[1mERROR: $1\033[0m"
  573. else
  574. echo "** ERROR: $1"
  575. fi
  576. }
  577. # determine host name, typically for use in printing UAC
  578. # messages; we use today a simplistic but portable uname -n way --
  579. # no domain name is displayed ; fifo_uac expands !! to host
  580. # address only for optional header fields; uname output without
  581. # domain is sufficient for informational header fields such as
  582. # From
  583. #
  584. get_my_host() {
  585. if [ -z "$SIP_DOMAIN" ]; then
  586. uname -n
  587. else
  588. echo "$SIP_DOMAIN"
  589. fi
  590. }
  591. # calculate name and domain of current user
  592. set_user() {
  593. OSERUSER=`echo $1|$AWK -F@ '{print $1}'`
  594. OSERDOMAIN=`echo $1|$AWK -F@ '{print $2}'`
  595. if [ -z "$OSERDOMAIN" ] ; then
  596. OSERDOMAIN="$SIP_DOMAIN"
  597. fi
  598. if [ -z "$OSERDOMAIN" ] ; then
  599. merr "domain unknown: use usernames with domain or set default domain \
  600. in SIP_DOMAIN"
  601. exit 1
  602. fi
  603. }
  604. # check the parameter if it is a valid address of record (user@domain)
  605. check_aor() {
  606. echo "$1" | $EGREP "^$USERNAME_RE@.+" >/dev/null
  607. if [ $? -ne 0 ] ; then
  608. echo "error: invalid AoR: $1" >&2
  609. exit 1
  610. fi
  611. }
  612. # check the parameter if it is a valid address of record (user@domain)
  613. is_aor() {
  614. echo "$1" | $EGREP "^$USERNAME_RE@.+" >/dev/null
  615. if [ $? -ne 0 ] ; then
  616. false
  617. else
  618. true
  619. fi
  620. }
  621. # check the parameter if it is a valid SIP address of record (sip:user@domain)
  622. check_sipaor() {
  623. echo "$1" | $EGREP "^sip(s)?:$USERNAME_RE@.+" >/dev/null
  624. if [ $? -ne 0 ] ; then
  625. echo "error: invalid SIP AoR: $1" >&2
  626. exit 1
  627. fi
  628. }
  629. # check the parameter if it is a valid SIP URI
  630. # quite simplified now -- it captures just very basic
  631. # errors
  632. check_uri() {
  633. echo "$1" | $EGREP "^sip(s)?:($USERNAME_RE@)?.+" > /dev/null
  634. if [ $? -ne 0 ] ; then
  635. echo "error: invalid SIP URI: $1" >&2
  636. exit 1
  637. fi
  638. }
  639. print_status() {
  640. echo $1 | $EGREP "^[1-6][0-9][0-9]" > /dev/null
  641. if [ "$?" -eq 0 ] ; then
  642. echo $1
  643. else
  644. echo "200 OK"
  645. fi
  646. }
  647. # process output from FIFO/Unixsock server; if everything is ok
  648. # skip the first "ok" line and proceed to returned
  649. # parameters
  650. filter_fl()
  651. {
  652. # tail +2
  653. $AWK 'BEGIN {line=0;IGNORECASE=1;}
  654. {line++}
  655. NR == 1 && /^200 OK/ { next }
  656. /^$/ { next }
  657. { print }'
  658. }
  659. # process jsonrpc output
  660. filter_json()
  661. {
  662. $AWK 'function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
  663. BEGIN { line=0; IGNORECASE=1; }
  664. { line++; }
  665. NR == 1 && /^[{].+"id"[ \t]*:[ \t]*[0-9]+[ \t]*}$/ { print; next; }
  666. NR == 1 && /^200 OK/ { next; }
  667. /^[ \t]*"jsonrpc":[ \t]*"2.0"/ { print; next; }
  668. /^[ \t]*"result":[ \t]*\[.+/ {
  669. n=split($0, a, ",");
  670. for (i=1; i<=n; i++) {
  671. if (i==1) {
  672. m=split(a[i], b, "[");
  673. print b[1] "[";
  674. if (substr(b[2], length(b[2]), 1)=="[" || substr(b[2], length(b[2]), 1)=="{") {
  675. print "\t\t" ltrim(b[2]);
  676. } else {
  677. print "\t\t" ltrim(b[2]) ",";
  678. }
  679. } else {
  680. if (match(a[i], ".+]$")) {
  681. print "\t\t" ltrim(substr(a[i], 1, length(a[i])-1));
  682. print "\t],";
  683. } else {
  684. if(length(a[i])>0) print "\t\t" ltrim(a[i]) ",";
  685. }
  686. }
  687. }
  688. next;
  689. }
  690. /[ \t]*}],/ {
  691. print substr($0, 1, length($0)-2);
  692. print "\t],";
  693. next;
  694. }
  695. /^$/ { next; }
  696. { print; }'
  697. }
  698. # params: user, realm, password
  699. # output: HA1
  700. _gen_ha1()
  701. {
  702. HA1=`echo -n "$1:$2:$3" | $MD5 | $AWK '{ print $1 }'`
  703. if [ $? -ne 0 ] ; then
  704. echo "HA1 calculation failed"
  705. exit 1
  706. fi
  707. }
  708. # params: user, realm, password
  709. # output: HA1B
  710. _gen_ha1b()
  711. {
  712. HA1B=`echo -n "$1@$2:$2:$3" | $MD5 | $AWK '{ print $1 }'`
  713. if [ $? -ne 0 ] ; then
  714. echo "HA1B calculation failed"
  715. exit 1
  716. fi
  717. }
  718. # params: user, realm, password
  719. # output: PHPLIB_ID
  720. _gen_phplib_id()
  721. {
  722. NOW=`date`;
  723. PHPLIB_ID=`echo -n "$1$2:$3:$NOW" | $MD5 | $AWK '{ print $1 }'`
  724. }
  725. # params: user, password
  726. # output: HA1, HA1B
  727. credentials()
  728. {
  729. set_user $1
  730. _gen_ha1 "$OSERUSER" "$OSERDOMAIN" "$2"
  731. _gen_ha1b "$OSERUSER" "$OSERDOMAIN" "$2"
  732. }