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