kamctl.base 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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.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 "$OSERBIN" ] ; then
  113. OSERBIN=$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. ##### ----------------------------------------------- #####
  135. #### Defined values
  136. ALL_METHODS=4294967295
  137. USERNAME_RE="[-a-zA-Z0-9&=\+\$,;\?/_\.\!~\*'\(\)]+"
  138. ##### ----------------------------------------------- #####
  139. #### database tables for SQL databases and DBTEXT
  140. SRDB_LOAD_SER=$((1 << 0)) # The row should be loaded by SER
  141. SRDB_DISABLED=$((1 << 1)) # The row is disabled
  142. SRDB_CANON=$((1 << 2)) # Canonical entry (domain or uri)
  143. SRDB_IS_TO=$((1 << 3)) # The URI can be used in To
  144. SRDB_IS_FROM=$((1 << 4)) # The URI can be used in From
  145. SRDB_FOR_SERWEB=$((1 << 5)) # Credentials instance can be used by serweb
  146. SRDB_PENDING=$((1 << 6))
  147. SRDB_DELETED=$((1 << 7))
  148. SRDB_CALLER_DELETED=$((1 << 8)) # Accounting table
  149. SRDB_CALLEE_DELETED=$((1 << 9)) # Accounting table
  150. SRDB_MULTIVALUE=$((1 << 10)) # Attr_types table
  151. SRDB_FILL_ON_REG=$((1 << 11)) # Attr_types table
  152. SRDB_REQUIRED=$((1 << 12)) # Attr_types table
  153. SRDB_DIR=$((1 << 13)) # Domain_settings table
  154. # UsrLoc Table
  155. if [ -z "$UL_TABLE" ] ; then
  156. UL_TABLE=location
  157. fi
  158. USER_COLUMN=username
  159. DOMAIN_COLUMN=domain
  160. CALLID_COLUMN=callid
  161. # subscriber table
  162. if [ -z "$SUB_TABLE" ] ; then
  163. SUB_TABLE=subscriber
  164. fi
  165. REALM_COLUMN=domain
  166. HA1_COLUMN=ha1
  167. HA1B_COLUMN=ha1b
  168. PASSWORD_COLUMN=password
  169. RPID_COLUMN=rpid
  170. SUBSCRIBER_COLUMN='username'
  171. PHP_LIB_COLUMN=phplib_id
  172. if [ -z "$STORE_PLAINTEXT_PW" ] ; then
  173. STORE_PLAINTEXT_PW=1
  174. fi
  175. # acl table
  176. if [ -z "$ACL_TABLE" ] ; then
  177. ACL_TABLE=grp
  178. fi
  179. ACL_USER_COLUMN=username
  180. ACL_DOMAIN_COLUMN=domain
  181. ACL_GROUP_COLUMN=grp
  182. ACL_MODIFIED_COLUMN=last_modified
  183. # aliases table
  184. if [ -z "$ALS_TABLE" ] ; then
  185. ALS_TABLE=aliases
  186. fi
  187. A_USER_COLUMN=username
  188. A_CONTACT_COLUMN=contact
  189. A_EXPIRES_COLUMN=expires
  190. A_Q_COLUMN=q
  191. A_CALLID_COLUMN=callid
  192. A_CSEQ_COLUMN=cseq
  193. A_LAST_MODIFIED_COLUMN=last_modified
  194. # domain table
  195. if [ -z "$DOMAIN_TABLE" ] ; then
  196. DOMAIN_TABLE=domain
  197. fi
  198. DO_DOMAIN_COLUMN=domain
  199. DO_LAST_MODIFIED_COLUMN=last_modified
  200. # uid_domain table
  201. if [ -z "$UID_DOMAIN_TABLE" ] ; then
  202. UID_DOMAIN_TABLE=uid_domain
  203. fi
  204. UID_DO_DOMAIN_COLUMN=domain
  205. UID_DO_DID_COLUMN=did
  206. UID_DO_FLAGS_COLUMN=flags
  207. # lcr tables
  208. if [ -z "$LCR_TABLE" ] ; then
  209. LCR_TABLE=lcr
  210. fi
  211. LCR_ID_COLUMN=lcr_id
  212. LCR_PREFIX_COLUMN=prefix
  213. LCR_GRPID_COLUMN=grp_id
  214. # gw table
  215. if [ -z "$GW_TABLE" ] ; then
  216. GW_TABLE=gw
  217. fi
  218. # carrier_name table
  219. if [ -z "$CARRIER_NAME_TABLE" ] ; then
  220. CARRIER_NAME_TABLE=carrier_name
  221. fi
  222. CARRIERROUTE_CARRIER_NAME_ID_COLUMN=id
  223. CARRIERROUTE_CARRIER_NAME_CARRIER_COLUMN=carrier
  224. # domain_name table
  225. if [ -z "$DOMAIN_NAME_TABLE" ] ; then
  226. DOMAIN_NAME_TABLE=domain_name
  227. fi
  228. CARRIERROUTE_DOMAIN_NAME_ID_COLUMN=id
  229. CARRIERROUTE_DOMAIN_NAME_DOMAIN_COLUMN=domain
  230. # carrierroute table
  231. if [ -z "$CARRIERROUTE_TABLE" ] ; then
  232. CARRIERROUTE_TABLE=carrierroute
  233. fi
  234. CARRIERROUTE_CARRIERROUTE_PREFIX_COLUMN=id
  235. CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN=carrier
  236. CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN=scan_prefix
  237. CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN=domain
  238. CARRIERROUTE_CARRIERROUTE_PROB_COLUMN=prob
  239. CARRIERROUTE_CARRIERROUTE_STRIP_COLUMN=strip
  240. CARRIERROUTE_CARRIERROUTE_REWRITE_HOST_COLUMN=rewrite_host
  241. CARRIERROUTE_CARRIERROUTE_REWRITE_PREFIX_COLUMN=rewrite_prefix
  242. CARRIERROUTE_CARRIERROUTE_REWRITE_SUFFIX_COLUMN=rewrite_suffix
  243. CARRIERROUTE_CARRIERROUTE_COMMENT_COLUMN=description
  244. CARRIERROUTE_CARRIERROUTE_FLAGS_COLUMN=flags
  245. CARRIERROUTE_CARRIERROUTE_MASK_COLUMN=mask
  246. # URI table
  247. if [ -z "$URI_TABLE" ] ; then
  248. URI_TABLE=uri
  249. fi
  250. URIUSER_COLUMN=uri_user
  251. MODIFIED_COLUMN=last_modified
  252. # dbaliases table
  253. if [ -z "$DA_TABLE" ] ; then
  254. DA_TABLE=dbaliases
  255. fi
  256. DA_USER_COLUMN=username
  257. DA_DOMAIN_COLUMN=domain
  258. DA_ALIAS_USER_COLUMN=alias_username
  259. DA_ALIAS_DOMAIN_COLUMN=alias_domain
  260. # speeddial table
  261. if [ -z "$SD_TABLE" ] ; then
  262. SD_TABLE=speed_dial
  263. fi
  264. SD_USER_COLUMN=username
  265. SD_DOMAIN_COLUMN=domain
  266. SD_SD_USER_COLUMN=sd_username
  267. SD_SD_DOMAIN_COLUMN=sd_domain
  268. SD_NEW_URI_COLUMN=new_uri
  269. SD_DESC_COLUMN=description
  270. # avp table
  271. if [ -z "$AVP_TABLE" ] ; then
  272. AVP_TABLE=usr_preferences
  273. fi
  274. AVP_UUID_COLUMN=uuid
  275. AVP_USER_COLUMN=username
  276. AVP_DOMAIN_COLUMN=domain
  277. AVP_ATTRIBUTE_COLUMN=attribute
  278. AVP_VALUE_COLUMN=value
  279. AVP_TYPE_COLUMN=type
  280. AVP_MODIFIED_COLUMN=last_modified
  281. # trusted table
  282. if [ -z "$TRUSTED_TABLE" ] ; then
  283. TRUSTED_TABLE=trusted
  284. fi
  285. TRUSTED_SRC_IP_COLUMN=src_ip
  286. TRUSTED_PROTO_COLUMN=proto
  287. TRUSTED_FROM_PATTERN_COLUMN=from_pattern
  288. TRUSTED_TAG_COLUMN=tag
  289. # address table
  290. if [ -z "$ADDRESS_TABLE" ] ; then
  291. ADDRESS_TABLE=address
  292. fi
  293. # dispatcher tables
  294. if [ -z "$DISPATCHER_TABLE" ] ; then
  295. DISPATCHER_TABLE=dispatcher
  296. fi
  297. DISPATCHER_ID_COLUMN=id
  298. DISPATCHER_SETID_COLUMN=setid
  299. DISPATCHER_DESTINATION_COLUMN=destination
  300. DISPATCHER_FLAGS_COLUMN=flags
  301. DISPATCHER_PRIORITY_COLUMN=priority
  302. DISPATCHER_ATTRS_COLUMN=attrs
  303. DISPATCHER_DESCRIPTION_COLUMN=description
  304. # dialog tables
  305. if [ -z "$DIALOG_TABLE" ] ; then
  306. DIALOG_TABLE=dialog
  307. fi
  308. # dialplan tables
  309. if [ -z "$DIALPLAN_TABLE" ] ; then
  310. DIALPLAN_TABLE=dialplan
  311. fi
  312. DIALPLAN_ID_COLUMN=id
  313. DIALPLAN_DPID_COLUMN=dpid
  314. DIALPLAN_PR_COLUMN=pr
  315. DIALPLAN_MATCH_OP_COLUMN=match_op
  316. DIALPLAN_MATCH_EXP_COLUMN=match_exp
  317. DIALPLAN_MATCH_LEN_COLUMN=match_len
  318. DIALPLAN_SUBST_EXP_COLUMN=subst_exp
  319. DIALPLAN_REPL_EXP_COLUMN=repl_exp
  320. DIALPLAN_ATTRS_COLUMN=attrs
  321. #
  322. ##### ------------------------------------------------ #####
  323. ### usage functions
  324. #
  325. usage_base() {
  326. echo
  327. mecho " -- command 'start|stop|restart|trap'"
  328. echo
  329. cat <<EOF
  330. trap ............................... trap with gdb Kamailio processes
  331. restart ............................ restart Kamailio
  332. start .............................. start Kamailio
  333. stop ............................... stop Kamailio
  334. EOF
  335. }
  336. usage_tls() {
  337. echo
  338. mecho " -- command 'tls'"
  339. echo
  340. cat <<EOF
  341. tls rootCA [<etcdir>] .......... creates new rootCA
  342. tls userCERT <user> [<etcdir>] ... creates user certificate
  343. default <etcdir> is $ETCDIR/tls
  344. EOF
  345. }
  346. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_base"
  347. usage_acl() {
  348. echo
  349. mecho " -- command 'acl' - manage access control lists (acl)"
  350. echo
  351. cat <<EOF
  352. acl show [<username>] .............. show user membership
  353. acl grant <username> <group> ....... grant user membership (*)
  354. acl revoke <username> [<group>] .... grant user membership(s) (*)
  355. EOF
  356. }
  357. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_acl"
  358. usage_lcr() {
  359. echo
  360. mecho " -- command 'lcr' - manage least cost routes (lcr)"
  361. echo
  362. cat <<EOF
  363. lcr show_gws....... show database gateways
  364. lcr show_routes.... show database routes
  365. lcr dump_gws....... show in memory gateways
  366. lcr dump_routes.... show in memory routes
  367. lcr reload ........ reload lcr gateways and routes
  368. lcr eval_weights .. evaluates probability for given GW's weights
  369. EOF
  370. }
  371. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_lcr"
  372. usage_cr() {
  373. echo
  374. mecho " -- command 'cr' - manage carrierroute tables"
  375. echo
  376. cat <<EOF
  377. cr show ....................................................... show tables
  378. cr reload ..................................................... reload tables
  379. cr dump ....................................................... show in memory tables
  380. cr addcn <carrier id> <carrier name> .......................... add a carrier name
  381. cr rmcn <carrier id> ......................................... rm a carrier name
  382. cr adddn <domain id> <domain name> ............................ add a domain name
  383. cr rmdn <domain id> .......................................... rm a domain name
  384. cr addcarrier <carrier> <scan_prefix> <domain> <rewrite_host> ................
  385. <prob> <strip> <rewrite_prefix> <rewrite_suffix> ...............
  386. <flags> <mask> <comment> .........................add a carrier
  387. (prob, strip, rewrite_prefix, rewrite_suffix,...................
  388. flags, mask and comment are optional arguments) ...............
  389. cr rmcarrier <carrier> <scan_prefix> <domain> ................ rm a carrier
  390. EOF
  391. }
  392. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_cr"
  393. usage_rpid() {
  394. echo
  395. mecho " -- command 'rpid' - manage Remote-Party-ID (RPID)"
  396. echo
  397. cat <<EOF
  398. rpid add <username> <rpid> ......... add rpid for a user (*)
  399. rpid rm <username> ................. set rpid to NULL for a user (*)
  400. rpid show <username> ............... show rpid of a user
  401. EOF
  402. }
  403. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_rpid"
  404. usage_subscriber() {
  405. echo
  406. mecho " -- command 'add|passwd|rm' - manage subscribers"
  407. echo
  408. cat <<EOF
  409. add <username> <password> .......... add a new subscriber (*)
  410. show <username> .................... show subscriber attributes (*)
  411. passwd <username> <passwd> ......... change user's password (*)
  412. rm <username> ...................... delete a user (*)
  413. sets <username> <attr> <val> ....... set string attribute (column value)
  414. setn <username> <attr> <val> ....... set numeric attribute (column value)
  415. EOF
  416. }
  417. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_subscriber"
  418. usage_trusted() {
  419. echo
  420. mecho " -- command 'add|dump|reload|rm|show' - manage trusted"
  421. echo
  422. cat <<EOF
  423. trusted show ...................... show db content
  424. trusted dump ...................... show cache content
  425. trusted reload .................... reload db table into cache
  426. trusted add <src_ip> <proto> <from_pattern> <tag>
  427. ....................... add a new entry
  428. ....................... (from_pattern and tag are optional arguments)
  429. trusted rm <src_ip> ............... remove all entries for the given src_ip
  430. EOF
  431. }
  432. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_trusted"
  433. usage_address() {
  434. echo
  435. mecho " -- command 'add|dump|reload|rm|show' - manage address"
  436. echo
  437. cat <<EOF
  438. address show ...................... show db content
  439. address dump ...................... show cache content
  440. address reload .................... reload db table into cache
  441. address add <grp> <ipaddr> <mask> <port> <tag>
  442. ....................... add a new entry
  443. ....................... (mask, port and tag are optional arguments)
  444. address rm <grp> <ipaddr> ......... remove entries for given grp and ipaddr
  445. EOF
  446. }
  447. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_address"
  448. usage_dispatcher() {
  449. echo
  450. mecho " -- command 'dispatcher' - manage dispatcher"
  451. echo
  452. cat <<EOF
  453. * Examples: dispatcher add 1 sip:1.2.3.1:5050 1 5 'prefix=123' 'gw one'
  454. * dispatcher add 2 sip:1.2.3.4:5050 3 0
  455. * dispatcher rm 4
  456. dispatcher show ..................... show dispatcher gateways
  457. dispatcher reload ................... reload dispatcher gateways
  458. dispatcher dump ..................... show in memory dispatcher gateways
  459. dispatcher add <setid> <destination> [flags] [priority] [attrs] [description]
  460. .......................... add gateway
  461. dispatcher rm <id> .................. delete gateway
  462. EOF
  463. }
  464. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_dispatcher"
  465. usage_dialog() {
  466. echo
  467. mecho " -- command 'dialog' - manage dialog records"
  468. echo
  469. cat <<EOF
  470. * Examples: dialog show
  471. * dialog showdb
  472. dialog show ..................... show in-memory dialog records
  473. dialog showdb ................... show database dialog records
  474. EOF
  475. }
  476. USAGE_FUNCTIONS="$USAGE_FUNCTIONS usage_dialog"
  477. usage_dialplan() {
  478. echo
  479. mecho " -- command 'dialplan' - manage dialplans"
  480. echo
  481. cat <<EOF
  482. dialplan show <dpid> .............. show dialplan tables
  483. dialplan reload ................... reload dialplan tables
  484. dialplan addrule <dpid> <prio> <match_op> <match_exp>
  485. <match_len> <subst_exp> <repl_exp> <attrs>
  486. .................... add a rule
  487. dialplan rm ....................... removes the entire dialplan table
  488. dialplan rmdpid <dpid> ............ removes all the gived dpid entries
  489. dialplan rmrule <dpid> <prio> ..... removes all the gived dpid/prio entries
  490. EOF
  491. }
  492. ##### ----------------------------------------------- #####
  493. #### Common functions
  494. mdbg() {
  495. if [ "0$VERBOSE" -ne 0 ] ; then
  496. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  497. echo -e "\033[1m$1\033[0m"
  498. else
  499. echo "$1"
  500. fi
  501. fi
  502. }
  503. mwarn() {
  504. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  505. echo -e '\E[37;32m'"\033[1mWARNING: $1\033[0m"
  506. else
  507. echo "** WARNING: $1"
  508. fi
  509. }
  510. minfo() {
  511. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  512. echo -e '\E[37;33m'"\033[1mINFO: $1\033[0m"
  513. else
  514. echo "** INFO: $1"
  515. fi
  516. }
  517. mecho() {
  518. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  519. echo -e "\033[1m$1\033[0m"
  520. else
  521. echo "$1"
  522. fi
  523. }
  524. merr() {
  525. if [ -t 1 -a -z "$NOHLPRINT" ] ; then
  526. echo -e '\E[37;31m'"\033[1mERROR: $1\033[0m"
  527. else
  528. echo "** ERROR: $1"
  529. fi
  530. }
  531. # determine host name, typically for use in printing UAC
  532. # messages; we use today a simplistic but portable uname -n way --
  533. # no domain name is displayed ; fifo_uac expands !! to host
  534. # address only for optional header fields; uname output without
  535. # domain is sufficient for informational header fields such as
  536. # From
  537. #
  538. get_my_host() {
  539. if [ -z "$SIP_DOMAIN" ]; then
  540. uname -n
  541. else
  542. echo "$SIP_DOMAIN"
  543. fi
  544. }
  545. # calculate name and domain of current user
  546. set_user() {
  547. OSERUSER=`echo $1|$AWK -F@ '{print $1}'`
  548. OSERDOMAIN=`echo $1|$AWK -F@ '{print $2}'`
  549. if [ -z "$OSERDOMAIN" ] ; then
  550. OSERDOMAIN="$SIP_DOMAIN"
  551. fi
  552. if [ -z "$OSERDOMAIN" ] ; then
  553. merr "domain unknown: use usernames with domain or set default domain \
  554. in SIP_DOMAIN"
  555. exit 1
  556. fi
  557. }
  558. # check the parameter if it is a valid address of record (user@domain)
  559. check_aor() {
  560. echo "$1" | $EGREP "^$USERNAME_RE@.*\..*" >/dev/null
  561. if [ $? -ne 0 ] ; then
  562. echo "error: invalid AoR: $1" > /dev/stderr
  563. exit 1
  564. fi
  565. }
  566. # check the parameter if it is a valid address of record (user@domain)
  567. is_aor() {
  568. echo "$1" | $EGREP "^$USERNAME_RE@.*\..*" >/dev/null
  569. if [ $? -ne 0 ] ; then
  570. false
  571. else
  572. true
  573. fi
  574. }
  575. # check the parameter if it is a valid SIP address of record (sip:user@domain)
  576. check_sipaor() {
  577. echo "$1" | $EGREP "^sip(s)?:$USERNAME_RE@.*\..*" >/dev/null
  578. if [ $? -ne 0 ] ; then
  579. echo "error: invalid SIP AoR: $1" > /dev/stderr
  580. exit 1
  581. fi
  582. }
  583. # check the parameter if it is a valid SIP URI
  584. # quite simplified now -- it captures just very basic
  585. # errors
  586. check_uri() {
  587. echo "$1" | $EGREP "^sip(s)?:($USERNAME_RE@)?.*\..*" > /dev/null
  588. if [ $? -ne 0 ] ; then
  589. echo "error: invalid SIP URI: $1" > /dev/stderr
  590. exit 1
  591. fi
  592. }
  593. print_status() {
  594. echo $1 | $EGREP "^[1-6][0-9][0-9]" > /dev/null
  595. if [ "$?" -eq 0 ] ; then
  596. echo $1
  597. else
  598. echo "200 OK"
  599. fi
  600. }
  601. # process output from FIFO/Unixsock server; if everything is ok
  602. # skip the first "ok" line and proceed to returned
  603. # parameters
  604. filter_fl()
  605. {
  606. # tail +2
  607. $AWK 'BEGIN {line=0;IGNORECASE=1;}
  608. {line++}
  609. NR == 1 && /^200 OK/ { next }
  610. /^$/ { next }
  611. { print }'
  612. }
  613. # process jsonrpc output
  614. filter_json()
  615. {
  616. $AWK 'function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
  617. BEGIN { line=0; IGNORECASE=1; }
  618. { line++; }
  619. NR == 1 && /^{.+"id"[ \t]*:[ \t]*[0-9]+[ \t]*}$/ { print; next; }
  620. NR == 1 && /^200 OK/ { next; }
  621. /^[ \t]*"jsonrpc":[ \t]*"2.0"/ { print; next; }
  622. /^[ \t]*"result":[ \t]*\[.+/ {
  623. n=split($0, a, ",");
  624. for (i=1; i<=n; i++) {
  625. if (i==1) {
  626. m=split(a[i], b, "[");
  627. print b[1] "[";
  628. if (substr(b[2], length(b[2]), 1)=="[" || substr(b[2], length(b[2]), 1)=="{") {
  629. print "\t\t" ltrim(b[2]);
  630. } else {
  631. print "\t\t" ltrim(b[2]) ",";
  632. }
  633. } else {
  634. if (match(a[i], ".+]$")) {
  635. print "\t\t" ltrim(substr(a[i], 1, length(a[i])-1));
  636. print "\t],";
  637. } else {
  638. if(length(a[i])>0) print "\t\t" ltrim(a[i]) ",";
  639. }
  640. }
  641. }
  642. next;
  643. }
  644. /[ \t]*}],/ {
  645. print substr($0, 1, length($0)-2);
  646. print "\t],";
  647. next;
  648. }
  649. /^$/ { next; }
  650. { print; }'
  651. }
  652. # params: user, realm, password
  653. # output: HA1
  654. _gen_ha1()
  655. {
  656. HA1=`echo -n "$1:$2:$3" | $MD5 | $AWK '{ print $1 }'`
  657. if [ $? -ne 0 ] ; then
  658. echo "HA1 calculation failed"
  659. exit 1
  660. fi
  661. }
  662. # params: user, realm, password
  663. # output: HA1B
  664. _gen_ha1b()
  665. {
  666. HA1B=`echo -n "$1@$2:$2:$3" | $MD5 | $AWK '{ print $1 }'`
  667. if [ $? -ne 0 ] ; then
  668. echo "HA1B calculation failed"
  669. exit 1
  670. fi
  671. }
  672. # params: user, realm, password
  673. # output: PHPLIB_ID
  674. _gen_phplib_id()
  675. {
  676. NOW=`date`;
  677. PHPLIB_ID=`echo -n "$1$2:$3:$NOW" | $MD5 | $AWK '{ print $1 }'`
  678. }
  679. # params: user, password
  680. # output: HA1, HA1B
  681. credentials()
  682. {
  683. set_user $1
  684. _gen_ha1 "$OSERUSER" "$OSERDOMAIN" "$2"
  685. _gen_ha1b "$OSERUSER" "$OSERDOMAIN" "$2"
  686. }