kamctl.base 17 KB

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