pv: added $msg(lpart) - return last part of the message
[sip-router] / utils / kamctl / kamctl
1 #!/bin/sh
2 #
3 # control tool for maintaining Kamailio
4 #
5 #===================================================================
6
7 ### version for this script
8 VERSION='5.4.0'
9
10 PATH=$PATH:/usr/local/sbin/
11
12 # for testing only, please don't enable this in production environments
13 # as this introduce security risks
14 if [ -z "$KAMCTL_TEST" ]; then
15         TEST="false"
16 else
17         TEST="true"
18 fi
19
20 ### include config files
21
22 # check for rc file at same location with kamctl
23 which greadlink >/dev/null 2>&1
24 ret=$?
25 if [ $ret -eq 0 ] ; then
26         KAMCTLFULLPATH=$(greadlink -f "$0")
27 else
28         which readlink >/dev/null 2>&1
29         ret=$?
30         if [ $ret -eq 0 ] ; then
31                 KAMCTLFULLPATH=$(readlink -f "$0")
32         fi
33 fi
34 if [ -n "$KAMCTLFULLPATH" ] ; then
35         KAMCTLDIRPATH=$(dirname "$KAMCTLFULLPATH")
36         if [ -f $KAMCTLDIRPATH/kamctlrc ]; then
37                 . $KAMCTLDIRPATH/kamctlrc
38         fi
39 fi
40
41 # check for rc file at standard locations
42 if [ -f /etc/kamailio/kamctlrc ]; then
43         . /etc/kamailio/kamctlrc
44 fi
45 if [ -f /usr/local/etc/kamailio/kamctlrc ]; then
46         . /usr/local/etc/kamailio/kamctlrc
47 fi
48 if [ -f ~/.kamctlrc ]; then
49         . ~/.kamctlrc
50 fi
51
52 if [ $TEST = "true" ]; then
53         if [ -f ./kamctlrc ]; then
54                 . ./kamctlrc
55         fi
56 fi
57
58
59 ### force values for variables in this section
60 # you better set the variables in ~/.kamctlrc
61 if [ -z "$ETCDIR" ] ; then
62         ETCDIR="/usr/local/etc/kamailio"
63 fi
64
65 if [ -z "$MYDIR" ] ; then
66         MYDIR=`dirname $0`
67 fi
68
69 if [ -z "$MYLIBDIR" ] ; then
70         MYLIBDIR="/usr/local/lib/kamailio/kamctl"
71         if [ ! -d "$MYLIBDIR" ]; then
72                 MYLIBDIR=$MYDIR
73         fi
74 fi
75
76 ##### ------------------------------------------------ #####
77 ### load base functions
78 #
79 if [ -f "$MYLIBDIR/kamctl.base" ]; then
80         . "$MYLIBDIR/kamctl.base"
81 else
82         echo -e "Cannot load core functions '$MYLIBDIR/kamctl.base' - exiting ...\n"
83         exit -1
84 fi
85
86 # locate kamcmd
87 if [ -z "$SERCMD" ] ; then
88         # try same dir as kamctl
89         SERCMD="$MYDIR/kamcmd"
90         if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
91                 # try standard location installed from sources
92                 SERCMD="/usr/local/sbin/kamcmd"
93                 if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
94                         # try source tree location
95                         SERCMD="$MYDIR/../sercmd/kamcmd"
96                         if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
97                                 # try locate it with which
98                                 SERCMD=`which kamcmd`
99                                 if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
100                                         mdbg "kamcmd tool not found"
101                                 fi
102                         fi
103                 fi
104         fi
105 else
106         if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
107                 merr "SERCMD does not point to an executable file"
108                 exit -1;
109         fi
110 fi
111
112 #
113 ##### ------------------------------------------------ #####
114 ### DBENGINE
115 #
116 DBENGINELOADED=0
117 case $DBENGINE in
118         MYSQL|mysql|MySQL)
119                 if [ -f "$MYLIBDIR/kamctl.mysql" ]; then
120                         . "$MYLIBDIR/kamctl.mysql"
121                         DBENGINELOADED=1
122                 fi
123                 ;;
124         PGSQL|pgsql|postgres|postgresql|POSTGRESQL)
125                 if [ -f "$MYLIBDIR/kamctl.pgsql" ]; then
126                         . "$MYLIBDIR/kamctl.pgsql"
127                         DBENGINELOADED=1
128                 fi
129                 ;;
130         ORACLE|oracle|Oracle)
131                 if [ -f "$MYLIBDIR/kamctl.oracle" ]; then
132                         . "$MYLIBDIR/kamctl.oracle"
133                         DBENGINELOADED=1
134                 fi
135                 ;;
136
137         DBTEXT|dbtext|textdb)
138                 if [ -f "$MYLIBDIR/kamctl.dbtext" ]; then
139                         . "$MYLIBDIR/kamctl.dbtext"
140                         DBENGINELOADED=1
141                 fi
142                 ;;
143         DB_BERKELEY|db_berkeley|BERKELEY|berkeley)
144                 if [ -f "$MYLIBDIR/kamctl.db_berkeley" ]; then
145                         . "$MYLIBDIR/kamctl.db_berkeley"
146                         DBENGINELOADED=1
147                 fi
148                 ;;
149         SQLITE|sqlite)
150                 if [ -f "$MYLIBDIR/kamctl.sqlite" ]; then
151                         . "$MYLIBDIR/kamctl.sqlite"
152                         DBENGINELOADED=1
153                 fi
154                 ;;
155
156 esac
157
158 if [ $DBENGINELOADED -eq 1 ] ; then
159         mdbg "database engine '$DBENGINE' loaded"
160 elif [ -n "$DBENGINE" ] ; then
161         mwarn "database engine not found - tried '$DBENGINE'"
162 fi
163
164 #
165 ##### ------------------------------------------------ #####
166 ### CTLENGINE
167 #
168
169 require_kamcmd() {
170         if [ -z "$SERCMD" ] ; then
171                 merr "kamcmd tool is missing"
172                 exit -1
173         fi
174 }
175
176 CTLENGINELOADED=0
177 CTLENGINETYPE=0
178
179 if [ -z "$CTLENGINE" ] ; then
180         CTLENGINE="RPCFIFO"
181 fi
182 case $CTLENGINE in
183         RPCFIFO|rpcfifo)
184                 if [ -f "$MYLIBDIR/kamctl.rpcfifo" ]; then
185                         . "$MYLIBDIR/kamctl.rpcfifo"
186                         CTLENGINELOADED=1
187                         CTLENGINETYPE=1
188                 fi
189                 ;;
190 esac
191
192 #### ------------------------------------------------- #####
193 ### Load kamcmd interface
194 #
195 if [ -f "$MYLIBDIR/kamctl.ser" ]; then
196         . "$MYLIBDIR/kamctl.ser"
197 fi
198
199 if [ $CTLENGINELOADED -eq 1 ] ; then
200         mdbg "Control engine '$CTLENGINE' loaded"
201 else
202         mwarn "no control engine found - tried '$CTLENGINE'"
203 fi
204
205 #
206 ##### ------------------------------------------------ #####
207 ### common functions
208 #
209 usage() {
210         CMD=`basename $0`
211         if [ "0$VERIFY_ACL" -eq 1 ] ; then
212                 EXTRA_TEXT="ACL privileges are: $ACL_GROUPS"
213         fi
214         cat <<EOF
215 $0 $VERSION
216
217 Existing commands:
218 EOF
219 for f in $USAGE_FUNCTIONS
220 do
221         $f
222 done
223         echo
224
225 }
226
227 require_dbengine() {
228         if [ $DBENGINELOADED -eq 0 ] ; then
229                 merr "This command requires a database engine - none was loaded"
230                 exit -1
231         fi
232 }
233
234 require_ctlengine() {
235         if [ $CTLENGINELOADED -eq 0 ] ; then
236                 merr "This command requires a control engine - none was loaded"
237                 exit -1
238         fi
239 }
240
241 #
242 ##### ------------------------------------------------ #####
243 ### combined functions (require db and/or ctl)
244 #
245 #
246
247 #
248 ##### ------------------------------------------------ #####
249 ### helper functions (require db and/or ctl)
250 #
251
252 ##
253 # wrapper to run RPC command based on configured engine
254 # - first parameter is the RPC command
255 # - the rest are the parameters for the RPC command
256 ctl_cmd_run () {
257         VCMDRPC="$1"
258         shift
259
260         if [ $CTLENGINETYPE -eq 1 ] ; then
261                 $CTLCMD $VCMDRPC $@
262         else
263                 merr "ctl engine not configured"
264                 exit 1
265         fi
266 }
267
268 lower() {
269         echo $1 | tr [A-Z] [a-z]
270 }
271
272 # params: user
273 # output: false if exists, true otherwise
274 is_user() {
275         set_user $1
276
277         QUERY="select count(*) from $SUB_TABLE where \
278 $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
279
280         CNT=`$DBROCMD "$QUERY" "$DBRAWPARAMS" | $EGREP -v ERROR | $LAST_LINE`
281         mdbg "is_user: user counter=$CNT"
282         if [ "$CNT" = "0" ] ; then
283                 false
284         else
285                 true
286         fi
287 }
288
289
290 # params: table, column, value
291 # output: false if exists, true otherwise
292 is_value_in_db() {
293         TABLE=$1
294         COL=$2
295         VALUE=$3
296
297         QUERY="select count(*) from $TABLE where $COL='$VALUE';"
298         CNT=`$DBROCMD "$QUERY" "$DBRAWPARAMS" | $EGREP -v ERROR | $LAST_LINE`
299         mdbg "is_value_in_db: counter=$CNT"
300         if [ "$CNT" = "0" ] ; then
301                 false
302         else
303                 true
304         fi
305 }
306
307 #
308 ##### ------------------------------------------------ #####
309 ### ACL Management
310 #
311 acl() {
312         require_dbengine
313         case $1 in
314                 show)
315                         if [ $# -eq 2 ] ; then
316                                 if [ $VERIFY_USER -eq 1 ] ; then
317                                         is_user $2
318                                         if [ $? -ne 0 ] ; then
319                                                 mecho "Non-existent user '$2'. Still proceeding? [Y|N] "
320                                                 read answer
321                                                 if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
322                                                         minfo "Proceeding with non-local user"
323                                                 else
324                                                         exit 1
325                                                 fi
326                                         fi
327                                 fi
328                                 set_user $2
329                                 CLAUSE=" WHERE $ACL_USER_COLUMN='$OSERUSER' AND \
330                                         $ACL_DOMAIN_COLUMN='$OSERDOMAIN' "
331                         elif [ $# -ne 1 ] ; then
332                                 usage_acl
333                                 exit 1
334                         fi
335                         QUERY="select * FROM $ACL_TABLE $CLAUSE ; "
336                         $DBROCMD "$QUERY"
337
338                         ;;
339
340                 grant)
341                         if [ $# -lt 3 ] ; then
342                                 usage
343                                 exit 1
344                         fi
345                         if [ $VERIFY_USER -eq 1 ] ; then
346                                 is_user $2
347                                 if [ $? -ne 0 ] ; then
348                                         mecho "Non-existent user '$2'. Still proceeding? [Y|N] "
349                                         read answer
350                                         if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
351                                                 minfo "Proceeding with non-local user"
352                                         else
353                                                 exit 1
354                                         fi
355                                 fi
356                         fi
357                         set_user $2
358                         shift 2
359                         acl_inserted=0
360                         while [ $# -gt 0 ] ; do
361
362                                 if [ $VERIFY_ACL -eq 1 ] ; then
363                                         found=0
364                                         for i in $ACL_GROUPS ; do
365                                                 if [ "$1" = "$i" ] ; then
366                                                         found=1
367                                                         break
368                                                 fi
369                                         done
370                                         if [ $found -eq 0 ] ; then
371                                                 mwarn "Invalid privilege: acl '$1' ignored"
372                                                 shift
373                                                 continue
374                                         fi
375                                 fi
376
377                                 QUERY="insert into $ACL_TABLE ($ACL_USER_COLUMN,\
378 $ACL_GROUP_COLUMN,$ACL_MODIFIED_COLUMN,$ACL_DOMAIN_COLUMN ) values \
379 ('$OSERUSER','$1', $DBFNOW, '$OSERDOMAIN' );"
380                                 $DBCMD "$QUERY"
381                                 if [ $? -ne 0 ] ; then
382                                         merr "acl - SQL Error"
383                                         exit 1
384                                 fi
385                                 acl_inserted=1
386                                 shift
387                         done
388
389                         if [ $acl_inserted -eq 1 ] ; then
390                                 $0 acl show "$OSERUSER@$OSERDOMAIN"
391                         fi
392
393                         ;;
394
395                 revoke)
396                         if [ $# -eq 3 ] ; then
397                                 CLAUSE=" and $ACL_GROUP_COLUMN='$3' "
398                         elif [ $# -ne 2 ] ; then
399                                 merr "acl - wrong number of parameters"
400                                 usage_acl
401                                 exit 1
402                         fi
403
404                         set_user $2
405
406                         QUERY="delete from $ACL_TABLE where \
407 $ACL_TABLE.$ACL_USER_COLUMN='$OSERUSER' AND $ACL_DOMAIN_COLUMN='$OSERDOMAIN' \
408 $CLAUSE;"
409                         $DBCMD "$QUERY"
410
411                         $0 acl show "$2"
412
413                         ;;
414
415                 *)
416                         merr "acl - invalid commad '$1'"
417                         usage_acl
418                         exit 1
419                         ;;
420         esac
421 }
422
423
424 #
425 ##### ------------------------------------------------ #####
426 ### alias management
427 #
428 check_ul_alias() {
429         require_ctlengine
430         RES=`ctl_cmd_run ul.lookup "$ALS_TABLE" "$1@$2"`
431         RET="$?"
432         ALIAS_UL_EXISTS=0
433         if [ $RET -ne 0 ] ; then
434                 merr "Kamailio $CTLENGINE not accessible: $RET"
435                 exit 1
436         fi
437         echo "$RES" | $EGREP "^404" > /dev/null
438         if [ $? -ne 0 ] ; then
439                 echo "$RES" | $EGREP "^400" > /dev/null
440                 if [ $? -eq 0 ] ; then
441                         merr "400; check if you use aliases in Kamailio"
442                         exit 1
443                 fi
444                 echo "$RES" | $EGREP "^200" > /dev/null
445                 if [ $? -eq 0 ] ; then
446                         ALIAS_UL_EXISTS=1
447                 fi
448                 # other errors
449                 merr "$RES"
450                 exit 1
451         fi
452 }
453
454 check_db_alias() {
455         require_dbengine
456
457         ALIAS_DB_EXISTS=0
458
459         QUERY="select count(*) from $DA_TABLE where $DA_ALIAS_USER_COLUMN='$1' \
460 and $DA_ALIAS_DOMAIN_COLUMN='$2';"
461         CNT=`$DBROCMD "$QUERY" | $EGREP -v ERROR | $LAST_LINE`
462         mdbg "check_db_alias: alias counter=$CNT"
463         if [ "$CNT" = "0" ] ; then
464                 ALIAS_DB_EXISTS=0
465         else
466                 ALIAS_DB_EXISTS=1
467         fi
468 }
469
470 #
471 # check for alias duplicates
472 #   params: user domain
473 #   output: false if exists, true otherwise
474 check_alias() {
475         ALIAS_EXISTS=0
476
477         if [ "$ENABLE_ALIASES" = "1" ] ; then
478                 check_ul_alias "$1" "$2"
479                 if [ "$ALIAS_UL_EXISTS" = "0" ] ; then
480                         ALIAS_EXISTS=0
481                 else
482                         ALIAS_EXISTS=1
483                 fi
484         elif  [ "$ENABLE_ALIASES" = "2" ] ; then
485                 check_db_alias "$1" "$2"
486                 if [ "$ALIAS_DB_EXISTS" = "0" ] ; then
487                         ALIAS_EXISTS=0
488                 else
489                         ALIAS_EXISTS=1
490                 fi
491         fi
492 }
493
494 # db-based aliases
495 alias_db() {
496         if [ "$#" -lt 2 ] ; then
497                 merr "alias_db - too few parameters"
498                 echo
499                 usage_alias_db
500                 exit 1
501         fi
502
503         require_dbengine
504
505         shift
506
507         case $1 in
508                 list)
509                         if [ $# -eq 2 ] ; then
510                                 # print aliases for user
511                                 check_aor "$2"
512                                 if [ "$?" -ne "0" ] ; then
513                                         merr "alias_db - <$2> is not a valid AoR (user@domain)"
514                                         exit 1
515                                 fi
516
517                                 set_user $2
518
519                                 CLAUSE="WHERE $DA_USER_COLUMN='$OSERUSER' AND \
520 $DA_DOMAIN_COLUMN='$OSERDOMAIN'"
521                                 mecho "Dumping aliases for user=<$2>"
522                                 echo
523                                 QUERY="SELECT CONCAT($DA_ALIAS_USER_COLUMN,\
524 '@',$DA_ALIAS_DOMAIN_COLUMN) AS ALIAS FROM $DA_TABLE $CLAUSE;"
525                                 $DBROCMD "$QUERY"
526                                                                         # | $AWK 'BEGIN {line=0;}
527                                                                         #               /^\+/ { next }
528                                                                         #               {       if(line==0) print "ALIASES";
529                                                                         #                       else print line ")\t" $1 "@" $2;
530                                                                         #                       line++; }'
531                         elif [ $# -eq 1 ] ; then
532                                 mecho "Dumping all aliases may take long: do you want to proceed? [Y|N] "
533                                 read answer
534                                 if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
535                                         mecho "Dumping all aliases..."
536                                         echo
537                                 else
538                                         exit 1
539                                 fi
540                                 QUERY="SELECT $DA_ALIAS_USER_COLUMN, $DA_ALIAS_DOMAIN_COLUMN,\
541 $DA_USER_COLUMN, $DA_DOMAIN_COLUMN FROM $DA_TABLE;"
542                                 $DBROCMD "$QUERY"
543                                         # | $AWK 'BEGIN {line=0;}
544                                         #       /^\+/ { next }
545                                         #       {       line++;
546                                         #               if(line==1) print "SIP-ID               \tALIAS\n";
547                                         #               else print $3 "@" $4 "\t" $1 "@" $2 }'
548                         else
549                                 merr "alias_db - wrong number of params for command [list]"
550                                 echo
551                                 usage_alias_db
552                                 exit 1
553                         fi
554
555                         exit $?
556                         ;;
557                 show)
558                         if [ $# -ne 2 ] ; then
559                                 merr "alias_db - wrong number of params for command [show]"
560                                 usage_alias_db
561                                 exit 1
562                         fi
563
564                         check_aor "$2"
565                         if [ "$?" -ne "0" ] ; then
566                                 merr "alias_db - $2 is not a valid AoR (user@domain)"
567                                 exit 1
568                         fi
569
570                         set_user $2
571
572                         CLAUSE="WHERE $DA_ALIAS_USER_COLUMN='$OSERUSER' AND \
573 $DA_ALIAS_DOMAIN_COLUMN='$OSERDOMAIN'"
574                         QUERY="SELECT CONCAT($DA_USER_COLUMN,'@',$DA_DOMAIN_COLUMN) \
575 AS 'SIP-ID' FROM $DA_TABLE $CLAUSE ; "
576                         $DBROCMD "$QUERY"
577                         #TMP_UUID=`sql_ro_query "$QUERY" | $AWK 'BEGIN {line=0;}
578                         #                                                               /^\+/ { next }
579                         #                                                               { line++;
580                         #                                                                 if(line==2) print $1 "@" $2;}'`
581                         #
582                         #if [ "$TMP_UUID" = "" ] ; then
583                         #       mecho "non-existent alias <$2>"
584                         #       exit 1
585                         #fi
586                         #
587                         #echo "Details for alias <$2>"
588                         #echo
589                         #echo "SIP-ID: $TMP_UUID"
590                         #echo
591                         #exit $?
592                         ;;
593                 add)
594                         if [ $# -ne 3 ] ; then
595                                 usage_alias_db
596                                 exit 1
597                         fi
598                         shift
599                         check_aor "$1"
600                         if [ "$?" -ne "0" ] ; then
601                                 err "alias_db - $1 is not a valid AoR (user@domain)"
602                                 exit 1
603                         fi
604
605                         check_aor "$2"
606                         if [ "$?" -ne "0" ] ; then
607                                 err "alias_db - $2 is not a valid AoR (user@domain)"
608                                 exit 1
609                         fi
610
611                         set_user $1
612                         TMP_OSERUSER=$OSERUSER
613                         TMP_OSERDOMAIN=$OSERDOMAIN
614                         set_user $2
615
616                         if is_value_in_db $DA_TABLE $DA_ALIAS_USER_COLUMN $TMP_OSERUSER; then
617                                 minfo "$TMP_OSERUSER alias already in $DA_TABLE table"
618                                 exit 0
619                         fi
620
621                         QUERY="INSERT INTO $DA_TABLE ($DA_USER_COLUMN,$DA_DOMAIN_COLUMN,\
622 $DA_ALIAS_USER_COLUMN,$DA_ALIAS_DOMAIN_COLUMN) VALUES ('$OSERUSER',\
623 '$OSERDOMAIN','$TMP_OSERUSER','$TMP_OSERDOMAIN' );"
624                         $DBCMD "$QUERY"
625                         if [ $? -ne 0 ] ; then
626                                 merr "alias_db - SQL Error"
627                                 exit 1
628                         fi
629
630                         exit $?
631                         ;;
632                 rm)
633                         if [ $# -ne 2 ] ; then
634                                 merr "alias_db - wrong numbers of parameters"
635                                 usage_alias_db
636                                 exit 1
637                         fi
638
639                         shift
640
641                         check_aor "$1"
642                         if [ "$?" -ne "0" ] ; then
643                                 merr "alias_db - $1 is not a valid URI"
644                                 exit 1
645                         fi
646
647                         set_user $1
648                         CLAUSE="WHERE $DA_ALIAS_USER_COLUMN='$OSERUSER' AND \
649 $DA_ALIAS_DOMAIN_COLUMN='$OSERDOMAIN'"
650                         QUERY="DELETE FROM $DA_TABLE $CLAUSE;"
651                         $DBCMD "$QUERY"
652                         if [ $? -ne 0 ] ; then
653                                 merr "alias_db - SQL Error"
654                                 exit 1
655                         fi
656
657                         exit $?
658                         ;;
659
660                 help)
661                         usage_alias_db "alone"
662                         ;;
663
664                 *)
665                         usage_alias_db
666                         exit 1
667                         ;;
668         esac
669 } # end db-aliases
670
671
672 #
673 ##### ------------------------------------------------ #####
674 ### AVP management
675 #
676 # avp list [-T table] [-u <sip-id|uuid>]
677 #     [-a attribute] [-v value] [-t type] ... list AVPs
678 # avp add [-T table] <sip-id|uuid>
679 #     <attribute> <type> <value> ............ add AVP (*)
680 # avp rm [-T table]  [-u <sip-id|uuid>]
681 #     [-a attribute] [-v value] [-t type] ... remove AVP (*)
682
683 avpops() {
684         require_dbengine
685         if [ "$#" -lt 2 ] ; then
686                 merr "avp - too few parameters"
687                 minfo "see '$0 avp help'"
688                 exit 1
689         fi
690         if [ "$1" = "avp" ] ; then
691                 shift
692         else
693                 merr "avp - unknown command $1"
694                 minfo "see '$0 avp help'"
695                 exit 1
696         fi
697
698         case $1 in
699                 list)
700                         shift
701                         CLAUSE=""
702                         while [ "$#" != "0" ]
703                         do
704                                 TMP_ARG=$1
705                                 shift
706                                 case $TMP_ARG in
707                                         -T)
708                                                 if [ -z "$1" ] ; then
709                                                         merr "avp list - table name parameter missing"
710                                                         exit 1
711                                                 fi
712                                                 AVP_TABLE=$1
713                                         ;;
714                                         -u)
715                                                 if [ -z "$1" ] ; then
716                                                         merr "avp list - user id or uuid parameter missing"
717                                                         exit 1
718                                                 fi
719                                                 is_aor "$1"
720                                                 if [ "$?" -eq "0" ] ; then
721                                                         set_user $1
722                                                         if [ "$CLAUSE" = "" ] ; then
723                                                                 CLAUSE=" WHERE $AVP_USER_COLUMN='$OSERUSER' \
724 AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
725                                                         else
726                                                                 CLAUSE="$CLAUSE AND \
727 $AVP_USER_COLUMN='$OSERUSER' AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
728                                                         fi
729                                                 else
730                                                         if [ "$CLAUSE" = "" ] ; then
731                                                                 CLAUSE=" WHERE $AVP_UUID_COLUMN='$1'"
732                                                         else
733                                                                 CLAUSE="$CLAUSE AND $AVP_UUID_COLUMN='$1'"
734                                                         fi
735                                                 fi
736                                         ;;
737                                         -a)
738                                                 if [ -z "$1" ] ; then
739                                                         merr "avp list - attribute name parameter missing"
740                                                         exit 1
741                                                 fi
742                                                 if [ "$CLAUSE" = "" ] ; then
743                                                         CLAUSE=" WHERE $AVP_ATTRIBUTE_COLUMN='$1'"
744                                                 else
745                                                         CLAUSE="$CLAUSE AND $AVP_ATTRIBUTE_COLUMN='$1'"
746                                                 fi
747                                         ;;
748                                         -v)
749                                                 if [ -z "$1" ] ; then
750                                                         merr "avp list - value parameter missing"
751                                                         exit 1
752                                                 fi
753                                                 if [ "$CLAUSE" = "" ] ; then
754                                                         CLAUSE=" WHERE $AVP_VALUE_COLUMN='$1'"
755                                                 else
756                                                         CLAUSE="$CLAUSE AND $AVP_VALUE_COLUMN='$1'"
757                                                 fi
758                                         ;;
759                                         -t)
760                                                 if [ -z "$1" ] ; then
761                                                         merr "avp list - type parameter missing"
762                                                         exit 1
763                                                 fi
764                                                 if [ "$CLAUSE" = "" ] ; then
765                                                         CLAUSE=" WHERE $AVP_TYPE_COLUMN='$1'"
766                                                 else
767                                                         CLAUSE="$CLAUSE AND $AVP_TYPE_COLUMN='$1'"
768                                                 fi
769                                         ;;
770                                         *)
771                                                 merr "avp list - unknown parameter $1"
772                                                 exit 1
773                                         ;;
774                                 esac
775                                 shift
776                         done
777
778                         QUERY="SELECT $AVP_UUID_COLUMN,$AVP_USER_COLUMN,\
779 $AVP_DOMAIN_COLUMN,$AVP_ATTRIBUTE_COLUMN,$AVP_TYPE_COLUMN,$AVP_VALUE_COLUMN \
780 FROM $AVP_TABLE $CLAUSE;"
781                         mdbg "Query: $QUERY"
782                         mecho "Dumping AVPs"
783                         echo
784                         $DBROCMD "$QUERY"
785                         # | $AWK 'BEGIN {line=0;}
786                         #               /^\+/ { next }
787                         #               {       if(line==0) print "##   UUID   \tUserID     \tAttribute     \tType     \tValue\n";
788                         #                       else {
789                         #                               ORS_BAK=ORS;
790                         #                               ORS="";
791                         #                               print line ")  " $1  $2 "@" $3 "\t" $4 "\t\"" $5;
792                         #                               for (i=6;i<=NF;++i) print FS $i;
793                         #                               ORS=ORS_BAK;
794                         #                               print "\"";
795                         #                       }
796                         #                       line++;
797                         #               }'
798
799                         exit $?
800                         ;;
801
802                 add)
803                         shift
804                         if [ $# -ne 4 ] ; then
805                                 if [ $# -ne 6 ] ; then
806                                         merr "avp add - bad number of parameters"
807                                         exit 1
808                                 fi
809                         fi
810                         if [ $# -eq 6 ] ; then
811                                 if [ "$1" = "-T" ] ; then
812                                         AVP_TABLE=$2
813                                         shift
814                                         shift
815                                 else
816                                         mecho "avp add - unknown parameter '$1'"
817                                         exit 1
818                                 fi
819                         fi
820
821                         is_aor "$1"
822                         if [ "$?" -eq "0" ] ; then
823                                 set_user $1
824                         else
825                                 AVP_UUID=$1
826                         fi
827
828                         QUERY="INSERT INTO $AVP_TABLE \
829 ($AVP_UUID_COLUMN,$AVP_USER_COLUMN,$AVP_DOMAIN_COLUMN,$AVP_ATTRIBUTE_COLUMN,\
830 $AVP_TYPE_COLUMN,$AVP_VALUE_COLUMN,$AVP_MODIFIED_COLUMN) \
831 VALUES ('$AVP_UUID','$OSERUSER','$OSERDOMAIN','$2',$3,'$4',$DBFNOW);"
832                         # echo "Query: $QUERY"
833                         $DBCMD "$QUERY"
834                         if [ $? -ne 0 ] ; then
835                                 merr "avp add - SQL Error"
836                                 exit 1
837                         else
838                                 echo
839                                 mecho "avp add - attribute added"
840                         fi
841
842                         exit $?
843                         ;;
844
845                 rm)
846                         shift
847                         CLAUSE=""
848                         while [ "$#" != "0" ]
849                         do
850                                 TMP_ARG=$1
851                                 shift
852                                 case $TMP_ARG in
853                                         -T)
854                                                 if [ -z "$1" ] ; then
855                                                         merr "avp rm - table name parameter missing"
856                                                         exit 1
857                                                 fi
858                                                 AVP_TABLE=$1
859                                         ;;
860                                         -u)
861                                                 if [ -z "$1" ] ; then
862                                                         merr "avp rm - user id or uuid parameter missing"
863                                                         exit 1
864                                                 fi
865                                                 is_aor "$1"
866                                                 if [ "$?" -eq "0" ] ; then
867                                                         set_user $1
868                                                         if [ "$CLAUSE" = "" ] ; then
869                                                                 CLAUSE="WHERE $AVP_USER_COLUMN='$OSERUSER' \
870 AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
871                                                         else
872                                                                 CLAUSE="$CLAUSE AND \
873 $AVP_USER_COLUMN='$OSERUSER' AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
874                                                         fi
875                                                 else
876                                                         if [ "$CLAUSE" = "" ] ; then
877                                                                 CLAUSE="WHERE $AVP_UUID_COLUMN='$1'"
878                                                         else
879                                                                 CLAUSE="$CLAUSE AND $AVP_UUID_COLUMN='$1'"
880                                                         fi
881                                                 fi
882                                         ;;
883                                         -a)
884                                                 if [ -z "$1" ] ; then
885                                                         merr "avp rm - attribute name parameter missing"
886                                                         exit 1
887                                                 fi
888                                                 if [ "$CLAUSE" = "" ] ; then
889                                                         CLAUSE="WHERE $AVP_ATTRIBUTE_COLUMN='$1'"
890                                                 else
891                                                         CLAUSE="$CLAUSE AND $AVP_ATTRIBUTE_COLUMN='$1'"
892                                                 fi
893                                         ;;
894                                         -v)
895                                                 if [ -z "$1" ] ; then
896                                                         merr "avp rm - value parameter missing"
897                                                         exit 1
898                                                 fi
899                                                 if [ "$CLAUSE" = "" ] ; then
900                                                         CLAUSE="WHERE $AVP_VALUE_COLUMN='$1'"
901                                                 else
902                                                         CLAUSE="$CLAUSE AND $AVP_VALUE_COLUMN='$1'"
903                                                 fi
904                                         ;;
905                                         -t)
906                                                 if [ -z "$1" ] ; then
907                                                         merr "avp rm - type parameter missing"
908                                                         exit 1
909                                                 fi
910                                                 if [ "$CLAUSE" = "" ] ; then
911                                                         CLAUSE="WHERE $AVP_TYPE_COLUMN='$1'"
912                                                 else
913                                                         CLAUSE="$CLAUSE AND $AVP_TYPE_COLUMN='$1'"
914                                                 fi
915                                         ;;
916                                         *)
917                                                 merr "avp rm - unknown parameter $1"
918                                                 exit 1
919                                         ;;
920                                 esac
921                                 shift
922                         done
923                         QUERY="DELETE FROM $AVP_TABLE $CLAUSE;"
924                         mdbg "Query: $QUERY"
925                         $DBCMD "$QUERY"
926                         if [ $? -ne 0 ] ; then
927                                 merr "avp rm - SQL Error"
928                                 exit 1
929                         else
930                                 echo
931                                 mecho "avp rm - AVP(s) deleted"
932                         fi
933
934                         exit $?
935                         ;;
936
937                 help)
938                         usage_avp
939                         ;;
940
941                 *)
942                         merr "avp - unknown command"
943                         usage
944                         exit 1
945                         ;;
946         esac
947 } # end avpops()
948
949 #
950 ##### ------------------------------------------------ #####
951 ### cisco restart
952 #
953 cisco_restart() {
954         require_ctlengine
955         myhost=`get_my_host`
956         CMDPARAMS="= tm.t_uac_start NOTIFY \"$1\" \".\" \".\" \"From:sip:daemon@$myhost=CRLF=To:<$1>=CRLF=Event:check-sync=CRLF=Contact:sip:daemon@$myhost=CRLF=\""
957
958         ctl_cmd_run $CMDPARAMS
959 }
960
961 #
962 ##### ------------------------------------------------ #####
963 ### DB operations
964 #
965 db_ops() {
966         require_dbengine
967         case $1 in
968                 exec|query)
969                         shift
970                         if [ $# -ne 1 ] ; then
971                                 merr "missing query parameter"
972                                 exit 1
973                         fi
974                         $DBCMD "$1"
975                         ;;
976                 roexec|roquery)
977                         shift
978                         if [ $# -ne 1 ] ; then
979                                 merr "missing query parameter"
980                                 exit 1
981                         fi
982                         $DBROCMD "$1"
983                         ;;
984                 run)
985                         shift
986                         if [ $# -ne 1 ] ; then
987                                 merr "missing query parameter"
988                                 exit 1
989                         fi
990                         eval QUERY=\$$1
991                         if [ -z "$QUERY" ] ; then
992                                 merr "missing query value"
993                                 exit 1
994                         fi
995                         $DBCMD "$QUERY"
996                         ;;
997                 rorun)
998                         shift
999                         if [ $# -ne 1 ] ; then
1000                                 merr "missing query parameter"
1001                                 exit 1
1002                         fi
1003                         eval QUERY=\$$1
1004                         if [ -z "$QUERY" ] ; then
1005                                 merr "missing query value"
1006                                 exit 1
1007                         fi
1008                         $DBROCMD "$QUERY"
1009                         ;;
1010                 show)
1011                         shift
1012                         if [ $# -ne 1 ] ; then
1013                                 merr "missing table parameter"
1014                                 exit 1
1015                         fi
1016                         QUERY="select * FROM $1;"
1017                         $DBROCMD "$QUERY"
1018                         ;;
1019                 showg)
1020                         shift
1021                         if [ $# -ne 1 ] ; then
1022                                 merr "missing table parameter"
1023                                 exit 1
1024                         fi
1025                         QUERY="select * FROM $1\\G;"
1026                         $DBROCMD "$QUERY"
1027                         ;;
1028                 smatch)
1029                         shift
1030                         if [ $# -ne 3 ] ; then
1031                                 merr "missing parameters"
1032                                 exit 1
1033                         fi
1034                         QUERY="SELECT * FROM $1 WHERE $2='$3'\\G;"
1035                         $DBROCMD "$QUERY"
1036                         ;;
1037                 nmatch)
1038                         shift
1039                         if [ $# -ne 3 ] ; then
1040                                 merr "missing parameters"
1041                                 exit 1
1042                         fi
1043                         QUERY="SELECT * FROM $1 WHERE $2=$3\\G;"
1044                         $DBROCMD "$QUERY"
1045                         ;;
1046                 connect)
1047                         shift
1048                         if [ -z "$DBCMDCONNECT" ] ; then
1049                                 merr "extension not implemented"
1050                                 exit 1
1051                         fi
1052                         $DBCMDCONNECT
1053                         ;;
1054                 version)
1055                         shift
1056                         if [ $# -ne 3 ] ; then
1057                                 merr "missing parameters"
1058                                 exit 1
1059                         fi
1060                         case $1 in
1061                                 add)
1062                                         QUERY="INSERT INTO version (table_name, table_version) VALUES ('$2', $3);"
1063                                         $DBCMD "$QUERY"
1064                                 ;;
1065                                 set)
1066                                         QUERY="DELETE FROM version WHERE table_name='$2';"
1067                                         $DBCMD "$QUERY"
1068                                         QUERY="INSERT INTO version (table_name, table_version) VALUES ('$2', $3);"
1069                                         $DBCMD "$QUERY"
1070                                 ;;
1071                                 update)
1072                                         QUERY="UPDATE version SET table_version=$3 WHERE table_name='$2';"
1073                                         $DBCMD "$QUERY"
1074                                 ;;
1075                                 *)
1076                                         usage_db_ops
1077                                         exit 1
1078                         esac
1079                         ;;
1080                 *)
1081                         usage_db_ops
1082                         exit 1
1083         esac
1084 }
1085
1086 #
1087 ##### ------------------------------------------------ #####
1088 ### domain management
1089 #
1090 domain() {
1091         case $1 in
1092                 reload)
1093                         require_ctlengine
1094                         ctl_cmd_run domain.reload
1095                         ;;
1096                 show)
1097                         require_ctlengine
1098                         ctl_cmd_run domain.dump
1099                         ;;
1100                 showdb)
1101                         require_dbengine
1102                         QUERY="select * FROM $DOMAIN_TABLE ; "
1103                         $DBROCMD "$QUERY"
1104                         ;;
1105                 add)
1106                         require_dbengine
1107                         shift
1108                         if [ $# -ne 1 ] ; then
1109                                 merr "missing domain parameter"
1110                                 exit 1
1111                         fi
1112                         if is_value_in_db $DOMAIN_TABLE $DO_DOMAIN_COLUMN $1; then
1113                                 minfo "$1 already in $DOMAIN_TABLE table"
1114                                 exit 0
1115                         fi
1116                         QUERY="insert into $DOMAIN_TABLE ($DO_DOMAIN_COLUMN, \
1117                         $DO_LAST_MODIFIED_COLUMN) VALUES ('$1',$DBFNOW);"
1118                         $DBCMD "$QUERY"
1119                         if [ $? -ne 0 ] ; then
1120                                 merr "domain - SQL Error"
1121                                 exit 1
1122                         fi
1123                         minfo "execute '$0 domain reload' to synchronize cache and database"
1124                         ;;
1125                 rm)
1126                         require_dbengine
1127                         shift
1128                         if [ $# -ne 1 ] ; then
1129                                 merr "missing domain parameter"
1130                                 exit 1
1131                         fi
1132                         QUERY="delete from $DOMAIN_TABLE where domain='$1';"
1133                         $DBCMD "$QUERY"
1134                         if [ $? -ne 0 ] ; then
1135                                 merr "domain - SQL Error"
1136                                 exit 1
1137                         fi
1138                         minfo "execute '$0 domain reload' to synchronize cache and database"
1139                         ;;
1140                 *)
1141                         usage_domain
1142                         exit 1
1143         esac
1144 }
1145
1146 #
1147 ##### ------------------------------------------------ #####
1148 ### uid_domain management
1149 #
1150 uid_domain() {
1151         case $1 in
1152                 reload)
1153                         require_kamcmd
1154                         $SERCTLCMD domain.reload
1155                         ;;
1156                 show)
1157                         require_kamcmd
1158                         $SERCTLCMD domain.dump
1159                         ;;
1160                 showdb)
1161                         require_dbengine
1162                         QUERY="select * FROM $UID_DOMAIN_TABLE ; "
1163                         $DBROCMD "$QUERY"
1164                         ;;
1165                 add)
1166                         require_dbengine
1167                         shift
1168                         if [ $# -lt 1 ] ; then
1169                                 merr "too few parameters"
1170                                 exit 1
1171                         fi
1172
1173                         DOMAIN=$1
1174                         DID=$2
1175                         FLAGS=$3
1176
1177                         if [ -z "$2" ] ; then
1178                                 DID=$DOMAIN
1179                         fi
1180                         if [ -z "$3" ] ; then
1181                                 FLAGS=$(( $SRDB_LOAD_SER | $SRDB_CANON | $SRDB_FOR_SERWEB ))
1182                         fi
1183
1184                         if is_value_in_db $UID_DOMAIN_TABLE $UID_DO_DOMAIN_COLUMN $DOMAIN; then
1185                                 minfo "$1 already in $UID_DOMAIN_TABLE table"
1186                                 exit 0
1187                         fi
1188                         QUERY="insert into $UID_DOMAIN_TABLE ($UID_DO_DID_COLUMN,$UID_DO_DOMAIN_COLUMN,$UID_DO_FLAGS_COLUMN) VALUES ('$DID','$DOMAIN',$FLAGS);"
1189                         $DBCMD "$QUERY"
1190                         if [ $? -ne 0 ] ; then
1191                                 merr "uid_domain - SQL Error"
1192                                 exit 1
1193                         fi
1194                         minfo "execute '$0 uid_domain reload' to synchronize cache and database"
1195                         ;;
1196                 rm)
1197                         require_dbengine
1198                         shift
1199                         if [ $# -ne 1 ] ; then
1200                                 merr "missing domain parameter"
1201                                 exit 1
1202                         fi
1203                         QUERY="delete from $UID_DOMAIN_TABLE where domain='$1';"
1204                         $DBCMD "$QUERY"
1205                         if [ $? -ne 0 ] ; then
1206                                 merr "domain - SQL Error"
1207                                 exit 1
1208                         fi
1209                         minfo "execute '$0 uid_domain reload' to synchronize cache and database"
1210                         ;;
1211                 *)
1212                         usage_uid_domain
1213                         exit 1
1214         esac
1215 }
1216
1217 #
1218 ##### ------------------------------------------------ #####
1219 ### permissions trusted management
1220 #
1221 permissions_trusted() {
1222         case $1 in
1223                 reload)
1224                         require_ctlengine
1225                         ctl_cmd_run permissions.trustedReload
1226                         ;;
1227                 dump)
1228                         require_ctlengine
1229                         ctl_cmd_run permissions.trustedDump
1230                         ;;
1231                 show)
1232                         require_dbengine
1233                         QUERY="select * FROM $TRUSTED_TABLE ; "
1234                         $DBROCMD "$QUERY"
1235                         ;;
1236                 add)
1237                         require_dbengine
1238                         shift
1239                         if [ $# -lt 2 ] ; then
1240                                 usage_trusted
1241                                 exit 1
1242                         fi
1243                         if is_value_in_db $TRUSTED_TABLE src_ip $1; then
1244                                 minfo "$1 already in $TRUSTED_TABLE table"
1245                                 exit 0
1246                         fi
1247                         case $2 in
1248                                 any|udp|tcp|tls|sctp|none)
1249                                         ;;
1250                                 *)
1251                                         merr "unknown protocol"
1252                                         exit 1
1253                         esac
1254                         PATTERN=""
1255                         if [ ! -z "$3" ]; then
1256                                 PATTERN="$3"
1257                         fi
1258
1259                         QUERY="insert into $TRUSTED_TABLE \
1260                                 ( $TRUSTED_SRC_IP_COLUMN, $TRUSTED_PROTO_COLUMN, \
1261                                 $TRUSTED_FROM_PATTERN_COLUMN, $TRUSTED_TAG_COLUMN) \
1262                                 VALUES ('$1', '$2', '$PATTERN', '$4');"
1263                         $DBCMD "$QUERY"
1264                         if [ $? -ne 0 ] ; then
1265                                 merr "trusted - SQL Error"
1266                                 exit 1
1267                         fi
1268                         minfo "execute '$0 trusted reload' to synchronize cache and database"
1269                         ;;
1270                 rm)
1271                         require_dbengine
1272                         shift
1273                         if [ $# -ne 1 ] ; then
1274                                 usage_trusted
1275                                 exit 1
1276                         fi
1277                         QUERY="delete from $TRUSTED_TABLE where $TRUSTED_SRC_IP_COLUMN='$1';"
1278                         $DBCMD "$QUERY"
1279                         if [ $? -ne 0 ] ; then
1280                                 merr "trusted - SQL Error"
1281                                 exit 1
1282                         fi
1283                         minfo "execute '$0 trusted reload' to synchronize cache and database"
1284                         ;;
1285                 *)
1286                         usage_trusted
1287                         exit 1
1288         esac
1289 }
1290
1291
1292 #
1293 ##### ------------------------------------------------ #####
1294 ### permissions address management
1295 #
1296 permissions_address() {
1297         case $1 in
1298                 reload)
1299                         require_ctlengine
1300                         ctl_cmd_run permissions.addressReload
1301                         ;;
1302                 dump)
1303                         require_ctlengine
1304                         ctl_cmd_run permissions.addressDump
1305                         ctl_cmd_run permissions.subnetDump
1306                         ;;
1307                 show)
1308                         require_dbengine
1309                         QUERY="select * FROM $ADDRESS_TABLE ; "
1310                         $DBROCMD "$QUERY"
1311                         ;;
1312                 add)
1313                         require_dbengine
1314                         shift
1315                         if [ $# -lt 2 ] ; then
1316                                 usage_address
1317                                 exit 1
1318                         fi
1319                         AMASK=32
1320                         if [ ! -z "$3" ]; then
1321                                 AMASK="$3"
1322                         fi
1323                         APORT=0
1324                         if [ ! -z "$4" ]; then
1325                                 APORT="$4"
1326                         fi
1327                         ATAG=""
1328                         if [ ! -z "$5" ]; then
1329                                 ATAG="$5"
1330                         fi
1331
1332                         QUERY="insert into $ADDRESS_TABLE \
1333                                 (grp, ip_addr, mask, port, tag) \
1334                                 VALUES ($1, '$2', $AMASK, $APORT, '$ATAG');"
1335                         $DBCMD "$QUERY"
1336                         if [ $? -ne 0 ] ; then
1337                                 merr "permissions address - SQL Error"
1338                                 exit 1
1339                         fi
1340                         minfo "execute '$0 address reload' to synchronize cache and database"
1341                         ;;
1342                 rm)
1343                         require_dbengine
1344                         shift
1345                         if [ $# -ne 2 ] ; then
1346                                 usage_address
1347                                 exit 1
1348                         fi
1349                         QUERY="delete from $ADDRESS_TABLE where grp=$1 and ip_addr='$2';"
1350                         $DBCMD "$QUERY"
1351                         if [ $? -ne 0 ] ; then
1352                                 merr "permissions address - SQL Error"
1353                                 exit 1
1354                         fi
1355                         minfo "execute '$0 address reload' to synchronize cache and database"
1356                         ;;
1357                 *)
1358                         usage_address
1359                         exit 1
1360         esac
1361 }
1362
1363
1364 #
1365 ##### ------------------------------------------------ #####
1366 ### mtree management
1367 #
1368 mtree_management() {
1369         case $1 in
1370                 reload)
1371                         require_ctlengine
1372                         ctl_cmd_run mtree.reload $2
1373                         ;;
1374                 dump)
1375                         require_ctlengine
1376                         ctl_cmd_run mtree.list $2
1377                         ;;
1378                 showdb)
1379                         require_dbengine
1380                         QUERY="select * FROM $2 ; "
1381                         $DBROCMD "$QUERY"
1382                         ;;
1383                 add)
1384                         require_dbengine
1385                         if [ $# -lt 4 ] ; then
1386                                 usage_mtree
1387                                 exit 1
1388                         fi
1389
1390                         QUERY="INSERT INTO $2 \
1391                                 (tprefix, tvalue) \
1392                                 VALUES ($3, '$4');"
1393                         $DBCMD "$QUERY"
1394                         if [ $? -ne 0 ] ; then
1395                                 merr "mtree - SQL Error"
1396                                 exit 1
1397                         fi
1398                         minfo "execute '$0 mtree reload' to synchronize cache and database"
1399                         ;;
1400                 rm)
1401                         require_dbengine
1402                         shift
1403                         if [ $# -ne 3 ] ; then
1404                                 usage_address
1405                                 exit 1
1406                         fi
1407                         QUERY="DELETE FROM $2 where tprefix='$3';"
1408                         $DBCMD "$QUERY"
1409                         if [ $? -ne 0 ] ; then
1410                                 merr "mtree - SQL Error"
1411                                 exit 1
1412                         fi
1413                         minfo "execute '$0 mtree reload' to synchronize cache and database"
1414                         ;;
1415                 *)
1416                         usage_mtree
1417                         exit 1
1418         esac
1419 }
1420
1421
1422 #
1423 ##### ------------------------------------------------ #####
1424 ### LCR management
1425 #
1426 lcr() {
1427         case $1 in
1428                 show_gws)
1429                         merr "command disabled"
1430                         exit 1
1431                         require_dbengine
1432                         mecho "lcr gateways"
1433                         QUERY="select * FROM $GW_TABLE ORDER BY $LCR_ID_COLUMN, $LCR_GW_GRPID_COLUMN; "
1434                         $DBROCMD "$QUERY"
1435                         ;;
1436                 show_routes)
1437                         merr "command disabled"
1438                         exit 1
1439                         require_dbengine
1440                         mecho "lcr routes"
1441                         QUERY="select * FROM $LCR_TABLE ORDER BY $LCR_ID_COLUMN, $LCR_PREFIX_COLUMN; "
1442                         $DBROCMD "$QUERY"
1443                         ;;
1444                 reload)
1445                         merr "command disabled"
1446                         exit 1
1447                         ctl_cmd_run lcr.reload
1448                         ;;
1449                 dump_gws)
1450                         merr "command disabled"
1451                         exit 1
1452                         ctl_cmd_run lcr.dump_gws
1453                         ;;
1454                 dump_routes)
1455                         merr "command disabled"
1456                         exit 1
1457                         ctl_cmd_run lcr.dump_lcrs
1458                         ;;
1459                 eval_weights)
1460                         shift
1461                         $AWK 'BEGIN {
1462                                 if (ARGC < 2) {
1463                                         printf("Usage: lcr eval_weights <list of weights (integers 1-254)>\n");
1464                                         exit;
1465                                 }
1466                                 iters = 100000;
1467                                 for (i = 1; i < ARGC; i++) { counts[i] = 0; }
1468                                 for (i = 1; i <= iters; i++) {
1469                                         for (j = 1; j < ARGC; j++) {
1470                                                 elem[j] = ARGV[j] * rshift(int(2147483647 * rand()), 8);
1471                                         }
1472                                         at = 1;
1473                                         max = elem[at];
1474                                         for (j = 2; j < ARGC; j++) {
1475                                                 if (elem[j] > max) {
1476                                                         max = elem[j];
1477                                                         at = j;
1478                                                 }
1479                                         }
1480                                         counts[at] = counts[at] + 1;
1481                                 }
1482                                 for (i = 1; i < ARGC; i++) {
1483                                         printf("weight %d probability %.4f\n", ARGV[i], counts[i]/iters);
1484                                 }
1485                         }' $@
1486                         ;;
1487                 *)
1488                         usage_lcr
1489                         exit 1
1490         esac
1491 }
1492
1493 #
1494 ##### ------------------------------------------------ #####
1495 ### CARRIERROUTE management
1496 #
1497 cr() {
1498         require_dbengine
1499         require_ctlengine
1500         case $1 in
1501                 show)
1502                         mecho "cr carrier names"
1503                         QUERY="select * FROM $CARRIER_NAME_TABLE ORDER BY $CARRIERROUTE_CARRIER_NAME_ID_COLUMN; "
1504                         $DBROCMD "$QUERY"
1505                         mecho "cr domain names"
1506                         QUERY="select * FROM $DOMAIN_NAME_TABLE ORDER BY $CARRIERROUTE_DOMAIN_NAME_ID_COLUMN; "
1507                         $DBROCMD "$QUERY"
1508                         mecho "cr routes"
1509                         QUERY="select * FROM $CARRIERROUTE_TABLE ORDER BY \
1510                                 $CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN,\
1511                                 $CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN,\
1512                                 $CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN,\
1513                                 $CARRIERROUTE_CARRIERROUTE_PROB_COLUMN;"
1514                         $DBROCMD "$QUERY"
1515                         ;;
1516                 reload)
1517                         ctl_cmd_run cr.reload_routes
1518                         ;;
1519
1520                 dump)
1521                         ctl_cmd_run cr.dump_routes
1522                         ;;
1523
1524                 addcn)
1525                         shift
1526                         if [ $# -ne 2 ] ; then
1527                                 merr "cr - missing carrier id or name"
1528                                 exit 1
1529                         fi
1530                         QUERY="insert into $CARRIER_NAME_TABLE
1531                                 ( $CARRIERROUTE_CARRIER_NAME_ID_COLUMN, \
1532                                 $CARRIERROUTE_CARRIER_NAME_CARRIER_COLUMN) \
1533                                 VALUES ($1, '$2');"
1534                         $DBCMD "$QUERY"
1535                         if [ $? -ne 0 ] ; then
1536                                 merr "cr - SQL Error"
1537                                 exit 1
1538                         fi
1539                         minfo "execute '$0 cr reload' to synchronize cache and database"
1540                         ;;
1541
1542                 rmcn)
1543                         shift
1544                         if [ $# -ne 1 ] ; then
1545                                 merr "cr - missing carrier id to be removed"
1546                                 exit 1
1547                         fi
1548                         QUERY="delete from $CARRIER_NAME_TABLE where $CARRIERROUTE_CARRIER_NAME_ID_COLUMN='$1';"
1549                         $DBCMD "$QUERY"
1550                         if [ $? -ne 0 ] ; then
1551                                 merr "cr - SQL Error"
1552                                 exit 1
1553                         fi
1554                         minfo "execute '$0 cr reload' to synchronize cache and database"
1555                         ;;
1556
1557                 adddn)
1558                         shift
1559                         if [ $# -ne 2 ] ; then
1560                                 merr "cr - missing domain id or name"
1561                                 exit 1
1562                         fi
1563                         QUERY="insert into $DOMAIN_NAME_TABLE
1564                                 ( $CARRIERROUTE_DOMAIN_NAME_ID_COLUMN, \
1565                                 $CARRIERROUTE_DOMAIN_NAME_DOMAIN_COLUMN) \
1566                                 VALUES ($1, '$2');"
1567                         $DBCMD "$QUERY"
1568                         if [ $? -ne 0 ] ; then
1569                                 merr "cr - SQL Error"
1570                                 exit 1
1571                         fi
1572                         minfo "execute '$0 cr reload' to synchronize cache and database"
1573                         ;;
1574
1575                 rmdn)
1576                         shift
1577                         if [ $# -ne 1 ] ; then
1578                                 merr "cr - missing domain id to be removed"
1579                                 exit 1
1580                         fi
1581                         QUERY="delete from $DOMAIN_NAME_TABLE where $CARRIERROUTE_DOMAIN_NAME_ID_COLUMN='$1';"
1582                         $DBCMD "$QUERY"
1583                         if [ $? -ne 0 ] ; then
1584                                 merr "cr - SQL Error"
1585                                 exit 1
1586                         fi
1587                         minfo "execute '$0 cr reload' to synchronize cache and database"
1588                         ;;
1589
1590                 addcarrier)
1591                         shift
1592                         if [ $# -lt 4 ] ; then
1593                                 merr "cr - too few parameters"
1594                                 exit 1
1595                         fi
1596                         PROB=1
1597                         STRIP=0
1598                         REWRITE_PREFIX=
1599                         REWRITE_SUFFIX=
1600                         COMMENT=
1601                         FLAGS=0
1602                         MASK=0
1603                         if [ $# -gt 4 ] ; then
1604                                 PROB=$5
1605                                 if [ $# -gt 5 ] ; then
1606                                         STRIP=$6
1607                                         if [ $# -gt 6 ] ; then
1608                                                 REWRITE_PREFIX=$7
1609                                                 if [ $# -gt 7 ] ; then
1610                                                         REWRITE_SUFFIX=$8
1611                                                         if [ $# -gt 8 ] ; then
1612                                                                 MASK=$9
1613                                                                 if [ $# -gt 9 ] ; then
1614                                                                         FLAGS=${10}
1615                                                                         if [ $# -gt 10 ] ; then
1616                                                                                 COMMENT=${11}
1617                                                                         fi
1618                                                                 fi
1619                                                         fi
1620                                                 fi
1621                                         fi
1622                                 fi
1623                         fi
1624                         CARRIER=$1
1625                         SCAN_PREFIX=$2
1626                         DOMAIN=$3
1627                         REWRITE_HOST=$4
1628                         echo $FLAGS
1629                         echo $MASK
1630                         QUERY="insert into $CARRIERROUTE_TABLE \
1631                                 ( $CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN, \
1632                                 $CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN, \
1633                                 $CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN, \
1634                                 $CARRIERROUTE_CARRIERROUTE_PROB_COLUMN, \
1635                                 $CARRIERROUTE_CARRIERROUTE_STRIP_COLUMN, \
1636                                 $CARRIERROUTE_CARRIERROUTE_REWRITE_HOST_COLUMN, \
1637                                 $CARRIERROUTE_CARRIERROUTE_REWRITE_PREFIX_COLUMN, \
1638                                 $CARRIERROUTE_CARRIERROUTE_REWRITE_SUFFIX_COLUMN, \
1639                                 $CARRIERROUTE_CARRIERROUTE_COMMENT_COLUMN, \
1640                                 $CARRIERROUTE_CARRIERROUTE_FLAGS_COLUMN, \
1641                                 $CARRIERROUTE_CARRIERROUTE_MASK_COLUMN ) \
1642                                 VALUES ($CARRIER, '$SCAN_PREFIX', '$DOMAIN', $PROB, $STRIP, \
1643                                 '$REWRITE_HOST', '$REWRITE_PREFIX', '$REWRITE_SUFFIX', '$COMMENT', \
1644                                 $FLAGS, $MASK);"
1645                         $DBCMD "$QUERY"
1646                         if [ $? -ne 0 ] ; then
1647                                 merr "cr - SQL Error"
1648                                 exit 1
1649                         fi
1650                         minfo "execute '$0 cr reload' to synchronize cache and database"
1651                         ;;
1652
1653                 rmcarrier)
1654                         shift
1655                         if [ $# -ne 3 ] ; then
1656                                 merr "cr - too few parameters"
1657                                 exit 1
1658                         fi
1659                         CARRIER=$1
1660                         SCAN_PREFIX=$2
1661                         DOMAIN=$3
1662                         QUERY="delete from $CARRIERROUTE_TABLE where $CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN='$CARRIER' AND \
1663                                 $CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN='$SCAN_PREFIX' AND \
1664                                 $CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN=$DOMAIN ;"
1665                         $DBCMD "$QUERY"
1666                         if [ $? -ne 0 ] ; then
1667                                 merr "cr - SQL Error"
1668                                 exit 1
1669                         fi
1670                         minfo "execute '$0 cr reload' to synchronize cache and database"
1671                         ;;
1672
1673                 *)
1674                         usage_cr
1675                         exit 1
1676         esac
1677 }
1678
1679 #
1680 ##### ------------------------------------------------ #####
1681 ### DISPATCHER management
1682 #
1683 dispatcher() {
1684         case $1 in
1685                 show)
1686                         require_dbengine
1687                         mecho "dispatcher gateways"
1688                         QUERY="select * FROM $DISPATCHER_TABLE ORDER BY $DISPATCHER_SETID_COLUMN; "
1689                         $DBROCMD "$QUERY"
1690                         ;;
1691                 addgw|add)
1692                         require_dbengine
1693                         shift
1694                         if [ $# -lt 2 ] ; then
1695                                 merr "too few parameters"
1696                                 usage_dispatcher
1697                                 exit 1
1698                         fi
1699
1700                         DISPATCHER_SETID=$1
1701                         DISPATCHER_DESTINATION=$2
1702
1703                         if [ $# -gt 2 ] ; then
1704                                 DISPATCHER_FLAGS=$3
1705                         else
1706                                 DISPATCHER_FLAGS=0
1707                         fi
1708
1709                         if [ $# -gt 3 ] ; then
1710                                 DISPATCHER_PRIORITY=$4
1711                         else
1712                                 DISPATCHER_PRIORITY=0
1713                         fi
1714
1715                         if [ $# -gt 4 ] ; then
1716                                 DISPATCHER_ATTRS=$5
1717                         else
1718                                 DISPATCHER_ATTRS=""
1719                         fi
1720
1721                         if [ $# -gt 5 ] ; then
1722                                 DISPATCHER_DESCRIPTION=$6
1723                         else
1724                                 DISPATCHER_DESCRIPTION=""
1725                         fi
1726
1727                         QUERY="insert into $DISPATCHER_TABLE \
1728                                 ( $DISPATCHER_SETID_COLUMN, $DISPATCHER_DESTINATION_COLUMN, $DISPATCHER_FLAGS_COLUMN, $DISPATCHER_PRIORITY_COLUMN, $DISPATCHER_ATTRS_COLUMN, $DISPATCHER_DESCRIPTION_COLUMN ) \
1729                                 VALUES ($DISPATCHER_SETID,'$DISPATCHER_DESTINATION',$DISPATCHER_FLAGS,$DISPATCHER_PRIORITY,'$DISPATCHER_ATTRS','$DISPATCHER_DESCRIPTION');"
1730                         $DBCMD "$QUERY"
1731
1732                         if [ $? -ne 0 ] ; then
1733                                 merr "dispatcher - SQL Error"
1734                                 exit 1
1735                         fi
1736
1737                         ;;
1738                 rmgw|rm)
1739                         require_dbengine
1740                         shift
1741                         if [ $# -ne 1 ] ; then
1742                                 merr "missing gateway id to be removed"
1743                                 exit 1
1744                         fi
1745
1746                         QUERY="delete from $DISPATCHER_TABLE where $DISPATCHER_ID_COLUMN='$1';"
1747                         $DBCMD "$QUERY"
1748
1749                         if [ $? -ne 0 ] ; then
1750                                 merr "dispatcher - SQL Error"
1751                                 exit 1
1752                         fi
1753
1754                         ;;
1755                 reload)
1756                         require_ctlengine
1757                         ctl_cmd_run dispatcher.reload
1758                         ;;
1759
1760                 dump)
1761                         require_ctlengine
1762                         ctl_cmd_run dispatcher.list
1763                         ;;
1764
1765                 *)
1766                         usage_dispatcher
1767                         exit 1
1768
1769         esac
1770 }
1771
1772 #
1773 ##### ------------------------------------------------ #####
1774 ### Server management
1775 #
1776 ksr_srv() {
1777         case $1 in
1778                 sockets)
1779                         require_ctlengine
1780                         mecho "list listen sockets"
1781                         ctl_cmd_run corex.list_sockets
1782                         ;;
1783                 aliases)
1784                         require_ctlengine
1785                         mecho "list server aliases"
1786                         ctl_cmd_run corex.list_aliases
1787                         ;;
1788                 rpclist)
1789                         require_ctlengine
1790                         mecho "list server rpc commands"
1791                         ctl_cmd_run system.listMethods
1792                         ;;
1793                 debug)
1794                         require_ctlengine
1795                         mecho "server debug level command"
1796                         if [ $# -lt 2 ] ; then
1797                                 ctl_cmd_run corex.debug
1798                         else
1799                                 ctl_cmd_run corex.debug $2
1800                         fi
1801                         ;;
1802                 version)
1803                         require_ctlengine
1804                         mecho "list server version"
1805                         ctl_cmd_run core.version
1806                         ;;
1807                 *)
1808                         usage_ksr_srv
1809                         exit 1
1810
1811         esac
1812 }
1813
1814
1815 #
1816 ##### ------------------------------------------------ #####
1817 ### DIALOG management
1818 #
1819 dialog() {
1820         case $1 in
1821                 show|list)
1822                         require_ctlengine
1823                         mecho "dialog memory records"
1824                         ctl_cmd_run dlg.list
1825                         ;;
1826                 showdb)
1827                         require_dbengine
1828                         mecho "dialog database records"
1829                         QUERY="select * FROM $DIALOG_TABLE ORDER BY id; "
1830                         $DBROCMD "$QUERY"
1831                         ;;
1832                 *)
1833                         usage_dialog
1834                         exit 1
1835
1836         esac
1837 }
1838
1839 #
1840 ##### ------------------------------------------------ #####
1841 ### DIALPLAN management
1842 #
1843 dialplan() {
1844         require_dbengine
1845         require_ctlengine
1846         case $1 in
1847                 show)
1848                         shift
1849                         if [ $# -gt 0 ] ; then
1850                                 mecho "dialplan $1 tables"
1851                                 QUERY="select * FROM $DIALPLAN_TABLE WHERE $DIALPLAN_DPID_COLUMN=$1 ORDER BY $DIALPLAN_PR_COLUMN ; "
1852                         else
1853                                 mecho "dialplan tables"
1854                                 QUERY="select * FROM $DIALPLAN_TABLE ORDER BY $DIALPLAN_DPID_COLUMN, $DIALPLAN_PR_COLUMN; "
1855                         fi
1856                         $DBROCMD "$QUERY"
1857                         ;;
1858
1859                 addrule)
1860                         shift
1861                         if [ $# -lt 4 ] ; then
1862                                 merr "too few parameters"
1863                                 usage_dialplan
1864                                 exit 1
1865                         fi
1866
1867                         DIALPLAN_DPID=$1
1868                         DIALPLAN_PR=$2
1869                         DIALPLAN_MATCH_OP=$3
1870                         case $DIALPLAN_MATCH_OP in
1871                                 equal)
1872                                         DIALPLAN_MATCH_OP=0
1873                                         ;;
1874                                 regexp)
1875                                         DIALPLAN_MATCH_OP=1
1876                                         ;;
1877                                 fnmatch)
1878                                         DIALPLAN_MATCH_OP=2
1879                                         ;;
1880                                 *)
1881                                         merr "dialplan - unexpected $DIALPLAN_MATCH_OP for operating matching. Use 'equal' or 'regexp'!"
1882                                         exit 1
1883                         esac
1884                         DIALPLAN_MATCH_EXP=$4
1885                         DIALPLAN_MATCH_LEN=0
1886                         if [ $# -gt 4 ] ; then
1887                                 DIALPLAN_MATCH_LEN=$5
1888                         fi
1889                         if [ $# -gt 5 ] ; then
1890                                 DIALPLAN_SUBST_EXP=$6
1891                         fi
1892                         if [ $# -gt 6 ] ; then
1893                                 DIALPLAN_REPL_EXP=$7
1894                         fi
1895                         if [ $# -gt 7 ] ; then
1896                                 DIALPLAN_ATTRS=$8
1897                         fi
1898
1899                         QUERY="insert into $DIALPLAN_TABLE \
1900                                 ( $DIALPLAN_DPID_COLUMN, $DIALPLAN_PR_COLUMN, $DIALPLAN_MATCH_OP_COLUMN, \
1901                                         $DIALPLAN_MATCH_EXP_COLUMN, $DIALPLAN_MATCH_LEN_COLUMN, \
1902                                         $DIALPLAN_SUBST_EXP_COLUMN, $DIALPLAN_REPL_EXP_COLUMN, \
1903                                         $DIALPLAN_ATTRS_COLUMN ) \
1904                                 VALUES ( $DIALPLAN_DPID, $DIALPLAN_PR, $DIALPLAN_MATCH_OP, \
1905                                         '$DIALPLAN_MATCH_EXP', $DIALPLAN_MATCH_LEN, '$DIALPLAN_SUBST_EXP', \
1906                                         '$DIALPLAN_REPL_EXP', '$DIALPLAN_ATTRS')";
1907                         mdbg "$QUERY"
1908                         $DBCMD "$QUERY"
1909
1910                         if [ $? -ne 0 ] ; then
1911                                 merr "dialplan - SQL Error"
1912                                 exit 1
1913                         fi
1914
1915                         minfo "do not forget to do dialplan reload"
1916                         ;;
1917
1918                 rm)
1919                         QUERY="delete from $DIALPLAN_TABLE; "
1920                         $DBCMD "$QUERY"
1921
1922                         if [ $? -ne 0 ] ; then
1923                                 merr "dialplan - SQL Error"
1924                                 exit 1
1925                         fi
1926
1927                         minfo "do not forget to do dialplan reload"
1928                         ;;
1929
1930                 rmdpid)
1931                         shift
1932                         if [ $# -lt 1 ] ; then
1933                                 merr "too few parameters"
1934                                 usage_dialplan
1935                                 exit 1
1936                         fi
1937
1938                         DIALPLAN_DPID=$1
1939
1940                         QUERY="delete from $DIALPLAN_TABLE where $DIALPLAN_DPID_COLUMN=$DIALPLAN_DPID; "
1941                         $DBCMD "$QUERY"
1942
1943                         if [ $? -ne 0 ] ; then
1944                                 merr "dialplan - SQL Error"
1945                                 exit 1
1946                         fi
1947
1948                         minfo "do not forget to do dialplan reload"
1949                         ;;
1950
1951                 rmrule)
1952                         shift
1953                         if [ $# -lt 2 ] ; then
1954                                 merr "too few parameters"
1955                                 usage_dialplan
1956                                 exit 1
1957                         fi
1958
1959                         DIALPLAN_DPID=$1
1960                         DIALPLAN_PR=$2
1961
1962                         QUERY="delete from $DIALPLAN_TABLE where $DIALPLAN_DPID_COLUMN=$DIALPLAN_DPID AND $DIALPLAN_PR_COLUMN=$DIALPLAN_PR; "
1963                         $DBCMD "$QUERY"
1964
1965                         if [ $? -ne 0 ] ; then
1966                                 merr "dialplan - SQL Error"
1967                                 exit 1
1968                         fi
1969
1970                         minfo "do not forget to do dialplan reload"
1971                         ;;
1972
1973                 reload)
1974                         ctl_cmd_run dialplan.reload
1975                         ;;
1976
1977                 *)
1978                         usage_dialplan
1979                         exit 1
1980
1981         esac
1982 }
1983
1984 #
1985 ##### ------------------------------------------------ #####
1986 ### kamailio_start
1987 #
1988 kamailio_start() {
1989         echo
1990         minfo "Starting Kamailio : "
1991         if [ -r $PID_FILE ] ; then
1992                 ps axw | $EGREP kamailio
1993                 ls -l $PID_FILE
1994                 minfo "PID file exists ($PID_FILE)! Kamailio already running?"
1995                 exit 1
1996         fi
1997
1998         if [ ! -x "$KAMBIN" ] ; then
1999                 echo
2000                 merr "Kamailio binaries not found at $KAMBIN"
2001                 merr "set KAMBIN to the path of kamailio in $0 or ~/.kamctlrc"
2002                 exit 1
2003         fi
2004         if [ $SYSLOG = 1 ] ; then
2005                 $KAMBIN -P $PID_FILE  -f $ETCDIR/kamailio.cfg $STARTOPTIONS 1>/dev/null 2>/dev/null
2006         else
2007                 $KAMBIN -P $PID_FILE -E  -f $ETCDIR/kamailio.cfg $STARTOPTIONS
2008         fi
2009         sleep 3
2010         if [ ! -s $PID_FILE ] ; then
2011                 echo
2012                 merr "PID file $PID_FILE does not exist -- Kamailio start failed"
2013                 exit 1
2014         fi
2015         minfo "started (pid: `cat $PID_FILE`)"
2016 }
2017
2018 #
2019 ##### ------------------------------------------------ #####
2020 ### kamailio_stop
2021 #
2022 kamailio_stop() {
2023         echo
2024         minfo "Stopping Kamailio : "
2025         if [ -r $PID_FILE ] ; then
2026                 kill `cat $PID_FILE`
2027                 minfo "stopped"
2028         else
2029                 echo
2030                 merr "No PID file found ($PID_FILE)! Kamailio probably not running"
2031                 minfo "check with 'ps axw | $EGREP kamailio'"
2032                 exit 1
2033         fi
2034 }
2035
2036 #
2037 ##### ------------------------------------------------ #####
2038 ### options_ping
2039 #
2040 options_ping() {
2041         myhost=`get_my_host`
2042         require_ctlengine
2043         CMDPARAMS="= tm.t_uac_wait_block OPTIONS \"$1\" \".\" \".\" \"From:sip:daemon@$myhost=CRLF=To:<$1>=CRLF=Contact:sip:daemon@$myhost=CRLF=\""
2044         ctl_cmd_run $CMDPARAMS
2045 }
2046
2047 #
2048 ##### ------------------------------------------------ #####
2049 ### rpid management
2050 #
2051 rpid() {
2052         if [ "$#" -lt 2 ] ; then
2053                 merr "rpid - too few parameters"
2054                 exit 1
2055         fi
2056         shift;
2057         require_dbengine
2058         case $1 in
2059                 show)
2060                         if [ $# -eq 2 ] ; then
2061                                 set_user $2
2062                                 is_user $2
2063                                 if [ $? -ne 0 ] ; then
2064                                         merr "rpid - invalid user '$2'"
2065                                         exit 1;
2066                                 fi
2067                                 CLAUSE=" WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND \
2068 $REALM_COLUMN='$OSERDOMAIN' "
2069                         elif [ $# -ne 1 ] ; then
2070                                 usage_rpid
2071                                 exit 1
2072                         fi
2073                         QUERY="select $SUBSCRIBER_COLUMN, $RPID_COLUMN FROM $SUB_TABLE \
2074 $CLAUSE ; "
2075                         $DBROCMD "$QUERY"
2076                         ;;
2077
2078                 add|rm)
2079                         MODE=$1;
2080
2081                         if [ "$MODE" = "add" ] ; then
2082                                 ARG_NUM=3;
2083                         else
2084                                 ARG_NUM=2;
2085                         fi
2086
2087                         if [ $# -lt $ARG_NUM ] ; then
2088                                 usage_rpid
2089                                 exit 1
2090                         fi
2091
2092                         set_user $2
2093                         is_user $2
2094                         if [ $? -ne 0 ] ; then
2095                                 merr "rpid - invalid user '$2'"
2096                                 exit 1
2097                         fi
2098                         shift 2
2099
2100                         if [ "$MODE" = "add" ] ; then
2101                                 RPID_VAL="'$1'";
2102                         else
2103                                 RPID_VAL=NULL;
2104                         fi
2105
2106                         QUERY="UPDATE $SUB_TABLE SET $RPID_COLUMN=$RPID_VAL \
2107 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND $REALM_COLUMN='$OSERDOMAIN';"
2108                         $DBCMD "$QUERY"
2109                         if [ $? -ne 0 ] ; then
2110                                 merr "rpid - SQL Error"
2111                                 exit 1
2112                         fi
2113
2114                         $0 rpid show "$OSERUSER@$OSERDOMAIN"
2115
2116                         ;;
2117
2118                 *)
2119                         usage_rpid
2120                         exit 1
2121                         ;;
2122         esac
2123 }
2124
2125 #
2126 ##### ------------------------------------------------ #####
2127 ### SPEEDDIAL management
2128 #
2129 speeddial() {
2130         if [ "$#" -lt 2 ] ; then
2131                 merr "speeddial - too few parameters"
2132                 echo
2133                 usage_speeddial
2134                 exit 1
2135         fi
2136
2137         require_dbengine
2138         shift
2139
2140         case $1 in
2141                 list)
2142                         if [ $# -eq 2 ] ; then
2143                                 # print speed-dials for user
2144                                 check_aor "$2"
2145                                 if [ "$?" -ne "0" ] ; then
2146                                         merr "speeddial - <$2> is not a valid AoR (user@domain)"
2147                                         exit 1
2148                                 fi
2149
2150                                 set_user $2
2151
2152                                 CLAUSE="WHERE $SD_USER_COLUMN='$OSERUSER' AND \
2153 $SD_DOMAIN_COLUMN='$OSERDOMAIN'"
2154                                 mecho "Dumping speed-dials for user=<$2>"
2155                                 echo
2156                                 QUERY="SELECT CONCAT($SD_SD_USER_COLUMN,'@',\
2157 $SD_SD_DOMAIN_COLUMN) AS 'Short number', $SD_NEW_URI_COLUMN AS 'New URI',\
2158 $SD_DESC_COLUMN FROM $SD_TABLE $CLAUSE;"
2159                                 $DBROCMD "$QUERY"
2160                                         #| $AWK 'BEGIN {line=0;}
2161                                         #       /^\+/ { next }
2162                 #{ if(line==0) print "##   SpeedDial   \tNew-URI     \tDescription\n";
2163                                         #       else {
2164                                         #               ORS_BAK=ORS;
2165                                         #               ORS="";
2166                                         #               print line ")  " $1 "@" $2 "\t" $3 "\t\"" $4;
2167                                         #               for (i=5;i<=NF;++i) print FS $i;
2168                                         #               ORS=ORS_BAK;
2169                                         #               print "\"";
2170                                         #       }
2171                                         #       line++;
2172                                         #}'
2173                         elif [ $# -eq 1 ] ; then
2174                                 mecho "Dumping all speed-dials may take long: do you want to proceed? [Y|N] "
2175                                 read answer
2176                                 if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
2177                                         mecho "Dumping all speed-dials..."
2178                                         echo
2179                                 else
2180                                         exit 1
2181                                 fi
2182                                 QUERY="SELECT CONCAT($SD_SD_USER_COLUMN,'@',\
2183 $SD_SD_DOMAIN_COLUMN) AS 'Short number', CONCAT($SD_USER_COLUMN,'@',\
2184 $SD_DOMAIN_COLUMN) AS 'Owner', $SD_NEW_URI_COLUMN AS 'New URI',\
2185 $SD_DESC_COLUMN FROM $SD_TABLE;"
2186                                 $DBROCMD "$QUERY"
2187                                 #| $AWK 'BEGIN {line=0;}
2188                                 #       /^\+/ { next }
2189                                 #       {       line++;
2190         #if(line==1) print "SIP-ID     \tSpeedDial  \tNew-URI    \tDescritpion\n";
2191                                 #               else {
2192                                 #                       ORS_BAK=ORS;
2193                                 #                       ORS="";
2194                                 #                       print $3 "@" $4 "\t" $1 "@" $2 "\t" $5 "\t\"" $6;
2195                                 #                       for (i=7;i<=NF;++i) print FS $i;
2196                                 #                       ORS=ORS_BAK;
2197                                 #                       print "\"";
2198                                 #               }
2199                                 #       }'
2200                         else
2201                                 merr "speeddial - wrong number of params for command [list]"
2202                                 usage_speeddial
2203                                 exit 1
2204                         fi
2205
2206                         exit $?
2207                         ;;
2208                 show)
2209                         if [ $# -ne 2 ] ; then
2210                                 merr "speeddial - wrong number of params for command [show]"
2211                                 usage_speeddial
2212                                 exit 1
2213                         fi
2214
2215                         check_aor "$2"
2216                         if [ "$?" -ne "0" ] ; then
2217                                 merr "speeddial - $2 is not a valid AoR (user@domain)"
2218                                 exit 1
2219                         fi
2220
2221                         set_user $2
2222
2223                         CLAUSE="WHERE $SD_SD_USER_COLUMN='$OSERUSER' AND \
2224 $SD_SD_DOMAIN_COLUMN='$OSERDOMAIN'"
2225                         QUERY="SELECT CONCAT($SD_USER_COLUMN,'@',$SD_DOMAIN_COLUMN) \
2226 AS 'Owner', $SD_NEW_URI_COLUMN AS 'New URI', $SD_DESC_COLUMN FROM \
2227 $SD_TABLE $CLAUSE ; "
2228                         mecho "Details for speeddial <$2>"
2229                         $DBROCMD "$QUERY"
2230                         # | $AWK 'BEGIN {line=0;} /^\+/ { next }
2231                         # {
2232                         #         if(line==0) print "##  SIP-ID    \tNew-URI   \tDescritpion\n";
2233                         #         else {
2234                         #                 ORS_BAK=ORS;usage_kamailio_monitor() {
2235                         #                 ORS="";
2236                         #                 print line ") " $1 "@" $2 "\t" $3 "\t\"" $4;
2237                         #                 for (i=5;i<=NF;++i) print FS $i;
2238                         #                 ORS=ORS_BAK;
2239                         #                 print "\"";
2240                         #         }
2241                         #         line++;
2242                         # }'
2243
2244                         exit $?
2245                         ;;
2246                 add)
2247                         if [ $# -ne 4 ] ; then
2248                                 if [ $# -ne 5 ] ; then
2249                                         merr "speeddial - wrong number of parameters"
2250                                         usage_speeddial
2251                                         exit 1
2252                                 fi
2253                         fi
2254                         shift
2255                         check_aor "$1"
2256                         if [ "$?" -ne "0" ] ; then
2257                                 merr "speeddial - $1 is not a valid AoR (user@domain)"
2258                                 exit 1
2259                         fi
2260
2261                         check_aor "$2"
2262                         if [ "$?" -ne "0" ] ; then
2263                                 merr "speeddial - $2 is not a valid AoR (user@domain)"
2264                                 exit 1
2265                         fi
2266
2267                         check_sipaor "$3"
2268                         if [ "$?" -ne "0" ] ; then
2269                                 merr "speeddial - $3 is not a valid SIP AoR (sip:user@domain)"
2270                                 exit 1
2271                         fi
2272
2273                         set_user $1
2274                         TMP_OSERUSER=$OSERUSER
2275                         TMP_OSERDOMAIN=$OSERDOMAIN
2276                         set_user $2
2277
2278                         QUERY="INSERT INTO $SD_TABLE ($SD_USER_COLUMN,$SD_DOMAIN_COLUMN,\
2279 $SD_SD_USER_COLUMN,$SD_SD_DOMAIN_COLUMN,$SD_NEW_URI_COLUMN,$SD_DESC_COLUMN) \
2280 VALUES ('$TMP_OSERUSER','$TMP_OSERDOMAIN','$OSERUSER','$OSERDOMAIN','$3','$4');"
2281                         $DBCMD "$QUERY"
2282                         if [ $? -ne 0 ] ; then
2283                                 merr "speeddial - SQL Error"
2284                                 exit 1
2285                         fi
2286                         mecho "ok - spedd dial added"
2287                         echo
2288                         exit $?
2289                         ;;
2290                 rm)
2291                         if [ $# -ne 3 ] ; then
2292                                 merr "speeddial rm - invalid number of parameters"
2293                                 usage_speeddial
2294                                 exit 1
2295                         fi
2296
2297                         shift
2298
2299                         check_aor "$1"
2300                         if [ "$?" -ne "0" ] ; then
2301                                 merr "speeddial - $1 is not a valid AoR (user@domain)"
2302                                 exit 1
2303                         fi
2304
2305                         check_aor "$2"
2306                         if [ "$?" -ne "0" ] ; then
2307                                 merr "speeddial - $2 is not a valid AoR (user@domain)"
2308                                 exit 1
2309                         fi
2310
2311                         set_user $1
2312                         TMP_OSERUSER=$OSERUSER
2313                         TMP_OSERDOMAIN=$OSERDOMAIN
2314                         set_user $2
2315
2316                         CLAUSE="WHERE $SD_USER_COLUMN='$TMP_OSERUSER' AND \
2317 $SD_DOMAIN_COLUMN='$TMP_OSERDOMAIN' AND $SD_SD_USER_COLUMN='$OSERUSER' AND \
2318 $SD_SD_DOMAIN_COLUMN='$OSERDOMAIN'"
2319                         QUERY="DELETE FROM $SD_TABLE $CLAUSE;"
2320                         $DBCMD "$QUERY"
2321                         if [ $? -ne 0 ] ; then
2322                                 merr "speeddial - SQL Error"
2323                                 exit 1
2324                         fi
2325
2326                         mecho "ok - spedd dial deleted"
2327                         echo
2328
2329                         ;;
2330
2331                 help)
2332                         usage_speeddial
2333                         ;;
2334
2335                 *)
2336                         merr "speeddial - unknown command"
2337                         usage_speeddial
2338                         exit 1
2339                         ;;
2340         esac
2341 } # end speed_dial()
2342
2343 #
2344 ##### ------------------------------------------------ #####
2345 ### acc management
2346 #
2347 acc() {
2348         if [ "$#" -lt 2 ] ; then
2349                 merr "acc - too few parameters"
2350                 usage_acc
2351                 exit 1
2352         fi
2353         shift;
2354         require_dbengine
2355         case $1 in
2356                 initdb)
2357                         QUERY=$(cat <<-END
2358 ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
2359 ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
2360 ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
2361 ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
2362 ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
2363 ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
2364 ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
2365 ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
2366 ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
2367 ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
2368 ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
2369 ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
2370 END
2371 )
2372                         $DBCMD "$QUERY"
2373                         if [ $? -ne 0 ] ; then
2374                                 merr "error - acc db init"
2375                                 exit 1
2376                         fi
2377
2378                         mecho "ok - acc db init"
2379
2380                         ;;
2381
2382                 showdb)
2383                         QUERY="select * FROM $ACC_TABLE ;"
2384                         $DBROCMD "$QUERY"
2385                         ;;
2386
2387                 recent)
2388                         TIMENOW=`date +"%s"`
2389                         if [ $# -eq 2 ] ; then
2390                                 CLAUSE=" WHERE time>=$TIMENOW - $2 "
2391                         else
2392                                 CLAUSE=" WHERE time>=$TIMENOW - 300 "
2393                         fi
2394                         QUERY="select * FROM $ACC_TABLE $CLAUSE ; "
2395                         $DBROCMD "$QUERY"
2396                         ;;
2397
2398
2399                 *)
2400                         usage_acc
2401                         exit 1
2402                         ;;
2403         esac
2404 } # end acc()
2405
2406
2407 #
2408 ##### ================================================ #####
2409 ### subscriber management
2410 #
2411 subscriber() {
2412         if [ "$#" -lt 2 ] ; then
2413                 merr "too few parameters"
2414                 usage_subscriber
2415                 exit 1
2416         fi
2417
2418         require_dbengine
2419
2420         case $1 in
2421                 add)
2422                         if [ $# -ne 3 ] ; then
2423                                 usage_subscriber
2424                                 exit 1
2425                         fi
2426                         shift
2427                         credentials $1 $2
2428                         is_user $1
2429                         if [ $? -eq 0 ] ; then
2430                                 minfo "user '$1' already exists"
2431                                 exit 1
2432                         fi
2433                         set_user $1
2434                         check_alias $OSERUSER $OSERDOMAIN
2435                         if [ "$ALIAS_EXISTS" = "1" ] ; then
2436                                 minfo "user '$1' already exists as alias"
2437                                 exit 1
2438                         fi
2439
2440                         if [ "$STORE_PLAINTEXT_PW" = "1" ] ; then
2441                                 PASS="$2"
2442                         else
2443                                 PASS=""
2444                         fi
2445
2446                         QUERY="insert into $SUB_TABLE ($SUBSCRIBER_COLUMN,\
2447                                 $REALM_COLUMN,$HA1_COLUMN,$HA1B_COLUMN,$PASSWORD_COLUMN) \
2448                                 values ('$OSERUSER','$OSERDOMAIN','$HA1','$HA1B','$PASS');";
2449                         $DBCMD "$QUERY"
2450                         if [ $? -ne 0 ] ; then
2451                                 merr "introducing the new user '$1' to the database failed"
2452                         else
2453                                 mecho "new user '$1' added"
2454                         fi
2455                         ;;
2456
2457                 show)
2458                         if [ $# -ne 2 ] ; then
2459                                 usage_subscriber
2460                                 exit 1
2461                         fi
2462                         shift
2463
2464                         set_user $1
2465
2466                         case $DBENGINE in
2467                                 MYSQL|mysql|MySQL)
2468                                         QUERY="SELECT * FROM $SUB_TABLE \
2469 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND $REALM_COLUMN='$OSERDOMAIN'\G"
2470                                 ;;
2471                                 *)
2472                                         QUERY="SELECT * FROM $SUB_TABLE \
2473 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND $REALM_COLUMN='$OSERDOMAIN';"
2474                                 ;;
2475                         esac
2476                         $DBROCMD "$QUERY"
2477                         ;;
2478
2479                 passwd)
2480                         if [ $# -ne 3 ] ; then
2481                                 usage_subscriber
2482                                 exit 1
2483                         fi
2484                         shift
2485                         credentials $1 $2
2486
2487                         is_user $1
2488                         if [ $? -ne 0 ] ; then
2489                                 merr "non-existent user '$1'"
2490                                 exit 1
2491                         fi
2492
2493                         if [ "$STORE_PLAINTEXT_PW" = "1" ] ; then
2494                                 PASS="$2"
2495                         else
2496                                 PASS=""
2497                         fi
2498
2499                         QUERY="update $SUB_TABLE set $HA1_COLUMN='$HA1', \
2500 $HA1B_COLUMN='$HA1B', $PASSWORD_COLUMN='$PASS' \
2501 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
2502                         $DBCMD "$QUERY"
2503                         if [ $? -ne 0 ] ; then
2504                                 merr "password change failed"
2505                         else
2506                                 minfo "password change succeeded"
2507                         fi
2508                         ;;
2509
2510                 rm)
2511                         if [ $# -ne 2 ] ; then
2512                                 usage_subscriber
2513                                 exit 1
2514                         fi
2515
2516                         require_ctlengine
2517                         shift
2518
2519                         is_user $1
2520                         if [ $? -ne 0 ] ; then
2521                                 merr "non-existent user '$1'"
2522                                 exit 1
2523                         fi
2524
2525                         # begin with remove all user's privileges
2526                         acl revoke $1  > /dev/null 2>&1
2527
2528                         # destroy db-aliases
2529                         QUERY="delete from $DA_TABLE where $DA_USER_COLUMN='$OSERUSER' \
2530 and $DA_DOMAIN_COLUMN='$OSERDOMAIN';"
2531                         $DBCMD "$QUERY"
2532
2533
2534                         # destroy the user now
2535                         QUERY="delete from $SUB_TABLE where $SUBSCRIBER_COLUMN='$OSERUSER' \
2536 and $REALM_COLUMN='$OSERDOMAIN';"
2537                         $DBCMD "$QUERY"
2538
2539                         # and also all his contacts
2540                         $0 ul rm $1   > /dev/null 2>&1
2541                         ;;
2542                 sets)
2543                         if [ $# -ne 4 ] ; then
2544                                 usage_subscriber
2545                                 exit 1
2546                         fi
2547                         shift
2548
2549                         is_user $1
2550                         if [ $? -ne 0 ] ; then
2551                                 merr "non-existent user '$1'"
2552                                 exit 1
2553                         fi
2554
2555                         QUERY="update $SUB_TABLE set $2='$3' \
2556 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
2557                         $DBCMD "$QUERY"
2558                         if [ $? -ne 0 ] ; then
2559                                 merr "attribute change failed"
2560                         else
2561                                 minfo "attribute change succeeded"
2562                         fi
2563                         ;;
2564                 setn)
2565                         if [ $# -ne 4 ] ; then
2566                                 usage_subscriber
2567                                 exit 1
2568                         fi
2569                         shift
2570
2571                         is_user $1
2572                         if [ $? -ne 0 ] ; then
2573                                 merr "non-existent user '$1'"
2574                                 exit 1
2575                         fi
2576
2577                         QUERY="update $SUB_TABLE set $2=$3 \
2578 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
2579                         $DBCMD "$QUERY"
2580                         if [ $? -ne 0 ] ; then
2581                                 merr "attribute change failed"
2582                         else
2583                                 minfo "attribute change succeeded"
2584                         fi
2585                         ;;
2586
2587         esac
2588
2589 }
2590
2591 #
2592 ##### ================================================ #####
2593 ### USRLOC management
2594 #
2595 usrloc() {
2596         if [ "$#" -lt 2 ] ; then
2597                 merr "usrloc - too few parameters"
2598                 usage_usrloc
2599                 exit 1
2600         fi
2601
2602         require_ctlengine
2603
2604         if [ "$1" = "alias" ] ; then
2605                 USRLOC_TABLE="$ALS_TABLE"
2606                 if [ -z "$USRLOC_TABLE" ] ; then
2607                         USRLOC_TABLE=aliases
2608                 fi
2609                 CHECK_SUB=1
2610         elif [ "$1" = "ul" ] ; then
2611                 USRLOC_TABLE="$UL_TABLE"
2612                 if [ -z "$USRLOC_TABLE" ] ; then
2613                         USRLOC_TABLE=location
2614                 fi
2615                 CHECK_SUB=0
2616         elif [ "$1" = "usrloc" ] ; then
2617                 USRLOC_TABLE="$UL_TABLE"
2618                 if [ -z "$USRLOC_TABLE" ] ; then
2619                         USRLOC_TABLE=location
2620                 fi
2621                 CHECK_SUB=0
2622         else
2623                 merr "usrloc - unknown subcommand '$1'"
2624                 usage_usrloc
2625                 exit 1
2626         fi
2627         shift
2628
2629         case $1 in
2630                 show)
2631                         if [ $# -eq 2 ] ; then
2632                                 if [ "$2" = "--brief" ] ; then
2633                                         ctl_cmd_run ul.dump brief
2634                                 else
2635                                         set_user $2
2636                                         ctl_cmd_run ul.lookup \
2637                                                 $USRLOC_TABLE "$OSERUSER@$OSERDOMAIN"
2638                                 fi
2639                         elif [ $# -eq 1 ] ; then
2640                                 ctl_cmd_run ul.dump
2641                         else
2642                                 merr "wrong number of params"
2643                                 usage_usrloc
2644                                 exit 1
2645                         fi
2646                         exit $?
2647                         ;;
2648                 add)
2649                         if [ $# -eq 3 ] ; then
2650                                 # expires 0 means persistent contact
2651                                 UL_EXPIRES=0
2652                                 UL_FLAGS=0
2653                                 BR_FLAGS=0
2654                                 UL_PATH="."
2655                         elif [ $# -eq 4 ] ; then
2656                                 UL_EXPIRES=$4
2657                                 UL_FLAGS=0
2658                                 BR_FLAGS=0
2659                                 UL_PATH="."
2660                         elif [ $# -eq 5 ] ; then
2661                                 UL_EXPIRES=$4
2662                                 UL_FLAGS=0
2663                                 BR_FLAGS=0
2664                                 UL_PATH="$5"
2665                         else
2666                                 usage_usrloc
2667                                 exit 1
2668                         fi
2669                         shift
2670                         check_uri "$2"
2671
2672                         if [ "$?" -ne "0" ] ; then
2673                                 merr "$2 is not a valid URI"
2674                                 exit 1
2675                         fi
2676
2677                         set_user $1
2678                         if [ "$CHECK_SUB" -ne 0 ] ; then
2679                                 is_user $1
2680                                 if [ $? -eq 0 ] ; then
2681                                         merr "overlap of alias with an existing subscriber name"
2682                                         exit 1;
2683                                 fi
2684                         fi
2685
2686                         check_alias $OSERUSER $OSERDOMAIN
2687                         if [ "$ALIAS_EXISTS" = "1" ] ; then
2688                                 if [ "$CHECK_SUB" -ne 0 ] ; then
2689                                         minfo "alias already defined"
2690                                 else
2691                                         merr "AOR is an alias"
2692                                 fi
2693                                 exit 1
2694                         fi
2695
2696                         if [ -z "$DEFAULT_Q" ] ; then
2697                                 DEFAULT_Q="1.0"
2698                         fi
2699
2700                         ctl_cmd_run ul.add "$USRLOC_TABLE" "$OSERUSER@$OSERDOMAIN" "$2" \
2701 "$UL_EXPIRES" "$DEFAULT_Q" "$UL_PATH" "$UL_FLAGS" "$BR_FLAGS" "$ALL_METHODS"
2702                         exit $?
2703                         ;;
2704                 rm)
2705                         if [ $# -eq 2 ] ; then
2706                                 shift
2707                                 set_user $1
2708                                 ctl_cmd_run ul.rm $USRLOC_TABLE "$OSERUSER@$OSERDOMAIN"
2709
2710                         elif [ $# -eq 3 ] ; then
2711                                 shift
2712                                 set_user $1
2713                                 check_uri "$2"
2714                                 if [ "$?" -ne "0" ] ; then
2715                                         merr "$2 is not a valid SIP URI (sip:[user@]domain)"
2716                                         exit 1
2717                                 fi
2718
2719                                 ctl_cmd_run ul.rm_contact $USRLOC_TABLE "$OSERUSER@$OSERDOMAIN" "$2"
2720
2721                         else
2722                                 merr "wrong number of params"
2723                                 usage_usrloc
2724                                 exit 1
2725                         fi
2726                         ;;
2727
2728                 dbclean)
2729                         require_dbengine
2730                         KSR_CLEAN_VAL=3600
2731                         if [ $# -eq 2 ] ; then
2732                                 KSR_CLEAN_VAL=$2
2733                         fi
2734                         QUERY="delete from $USRLOC_TABLE where expires < SUBDATE(NOW(), INTERVAL $KSR_CLEAN_VAL SECOND);"
2735                         $DBCMD "$QUERY"
2736
2737                         exit $?
2738                         ;;
2739
2740                 *)
2741                         usage_usrloc
2742                         exit 1
2743                         ;;
2744         esac
2745 }
2746
2747 ##### ================================================ #####
2748 ### TLS CA management
2749 #
2750
2751 tls_ca() {
2752
2753         case $1 in
2754                 "rootCA")
2755                         if [ -z $2 ] ; then
2756                                 # use default
2757                                 CA_BASE=$ETCDIR/tls
2758                         else
2759                                 CA_BASE=`(cd $2;pwd)`
2760                         fi
2761
2762                         if [ ! -d $CA_BASE ] ; then
2763                                 merr "Config directory ($CA_BASE) does not exist"
2764                                 exit 1
2765                         fi
2766
2767                         CA_CONF='ca.conf'
2768                         CA_PATH=$CA_BASE/rootCA
2769                         if [ ! -f $CA_BASE/$CA_CONF  ] ; then
2770                                 merr "root CA config file ($CA_BASE/$CA_CONF) does not exist"
2771                                 exit 1
2772                         fi
2773
2774                         if [ -d $CA_PATH ] ; then
2775                                 mwarn "root CA directory ($CA_PATH) exists! Remove it (y/n)?"
2776                                 read X
2777                                 if [ "$X" != "y" -a "$X" != "Y" ] ; then
2778                                         exit 1
2779                                 fi
2780                         fi
2781
2782                         mecho "Creating directory $CA_PATH and its sub-tree"
2783                         mkdir -p $CA_PATH
2784                         if [ $? -ne 0 ] ; then
2785                                 merr "Failed to create root directory $CA_PATH"
2786                                 exit 1
2787                         fi
2788                         rm -fr "${CA_PATH:?}"/*
2789                         mkdir $CA_PATH/private
2790                         mkdir $CA_PATH/certs
2791                         touch $CA_PATH/index.txt
2792                         echo 01 >$CA_PATH/serial
2793
2794                         mecho "Creating CA self-signed certificate"
2795                         ( cd $CA_PATH; openssl req -config $CA_BASE/$CA_CONF -x509 -newkey \
2796                                 rsa:2048 -days 365 -out ./cacert.pem -outform PEM )
2797                         if [ $? -ne 0 ] ; then
2798                                 merr "Failed to create self-signed certificate"
2799                                 exit 1
2800                         fi
2801
2802                         mecho "Protecting CA private key"
2803                         chmod 600 $CA_PATH/private/cakey.pem
2804
2805                         mecho "DONE"
2806                         minfo "Private key can be found in $CA_PATH/private/cakey.pem"
2807                         minfo "Certificate can be found in $CA_PATH/cacert.pem"
2808                         ;;
2809
2810                 "userCERT")
2811                         if [ -z $2 ] ; then
2812                                 merr "Missing user name parameter"
2813                                 exit 1
2814                         fi
2815
2816                         if [ -z $3 ] ; then
2817                                 # use default
2818                                 CA_BASE=$ETCDIR/tls
2819                         else
2820                                 CA_BASE=`(cd $3;pwd)`
2821                         fi
2822
2823                         if [ ! -d $CA_BASE ] ; then
2824                                 merr "Config directory ($CA_BASE) does not exist"
2825                                 exit 1
2826                         fi
2827
2828                         USER_DIR=$CA_BASE/$2
2829                         USER_CFG=$CA_BASE/$2.conf
2830                         USER=$2
2831                         REQ_CFG=$CA_BASE/request.conf
2832
2833                         if [ ! -f $USER_CFG ] ; then
2834                                 merr "User config file $USER_CFG not found"
2835                                 exit 1
2836                         fi
2837
2838                         if [ ! -f $REQ_CFG ] ; then
2839                                 merr "Request config file $REQ_CFG not found"
2840                                 exit 1
2841                         fi
2842
2843                         mecho "Using config file $USER_CFG"
2844
2845                         if [ -d $USER_DIR ] ; then
2846                                 mwarn "User CERT directory ($USER_DIR) exists! Remove it (y/n)?"
2847                                 read X
2848                                 if [ "$X" != "y" -a "$X" != "Y" ] ; then
2849                                         exit 1
2850                                 fi
2851                         fi
2852
2853                         mecho "Creating directory $USER_DIR"
2854                         mkdir -p $USER_DIR
2855                         if [ $? -ne 0 ] ; then
2856                                 merr "Failed to create user directory $USER_DIR "
2857                                 exit 1
2858                         fi
2859                         rm -fr "${USER_DIR:?}"/*
2860
2861                         mecho "Creating user certificate request"
2862                         openssl req  -config $USER_CFG -out $USER_DIR/$USER-cert_req.pem \
2863                                 -keyout $USER_DIR/$USER-privkey.pem -new -nodes
2864                         if [ $? -ne 0 ] ; then
2865                                 merr "Failed to generate certificate request"
2866                                 exit 1
2867                         fi
2868
2869                         mecho "Signing certificate request"
2870                         ( cd $CA_BASE ; openssl ca -config $REQ_CFG -in \
2871                                 $USER_DIR/$USER-cert_req.pem -out $USER_DIR/$USER-cert.pem )
2872                         if [ $? -ne 0 ] ; then
2873                                 merr "Failed to generate certificate request"
2874                                 exit 1
2875                         fi
2876
2877                         mecho "Generating CA list"
2878                         cat $CA_BASE/rootCA/cacert.pem >> $USER_DIR/$USER-calist.pem
2879
2880                         mecho "DONE"
2881                         minfo "Private key is locate at $USER_DIR/$USER-privkey.pem "
2882                         minfo "Certificate is locate at $USER_DIR/$USER-cert.pem "
2883                         minfo "CA-List is locate at $USER_DIR/$USER-calist.pem "
2884                         ;;
2885
2886                 "gen-certs")
2887                         mecho "Generating self signed certificates"
2888                         if [ -z "$CERTDAYS" ] ; then
2889                                 CERTDAYS=365
2890                         fi
2891                         if [ -n "$2" ] ; then
2892                                 openssl req -x509 -newkey rsa:4096 -nodes -subj "/CN=${2}" -keyout kamailio-selfsigned.key -out kamailio-selfsigned.pem -days ${CERTDAYS}
2893                         else
2894                                 openssl req -x509 -newkey rsa:4096 -nodes -keyout kamailio-selfsigned.key -out kamailio-selfsigned.pem -days ${CERTDAYS}
2895                         fi
2896                         ;;
2897
2898                 *)
2899                         merr "unknown TLS command $1"
2900                         usage_tls
2901                         ;;
2902         esac
2903 }
2904
2905 extcmd() {
2906         if [ -f $ETCDIR/kamctl.${1}.ext ]; then
2907                 . $ETCDIR/kamctl.${1}.ext
2908         else
2909                 if [ -f ~/.kamctl/kamctl.${1}.ext ]; then
2910                         . ~/.kamctl/kamctl.${1}.ext
2911                 else
2912                         return
2913                 fi
2914         fi
2915
2916         XCMD=cmd_${1}
2917
2918         shift
2919         $XCMD "$@"
2920
2921         exit 1
2922 }
2923
2924 ##### ================================================ #####
2925 ### trap with gdb kamailio processes using RPC core.psx
2926 #
2927
2928 kamailio_trap() {
2929         if [ -z "$GDB" ] ; then
2930                 merr "'gdb' tool not found: set GDB variable to correct tool path"
2931                 exit
2932         fi
2933         DATE=`/bin/date +%Y%m%d_%H%M%S`
2934         LOG_FILE=/tmp/gdb_kamailio_${DATE}.txt
2935         minfo "Trap file: $LOG_FILE"
2936         ctl_cmd_run core.psx > $LOG_FILE
2937         echo -n "Trapping Kamailio with gdb: "
2938         PID_TIMESTAMP_VECTOR=`$EGREP 'PID' $LOG_FILE | $EGREP -Eo '[0-9]+'`
2939         for pid in $PID_TIMESTAMP_VECTOR
2940         do
2941                 echo -n "."
2942                 PID=`echo $pid | cut -d '-' -f 1`
2943                 echo "" >> $LOG_FILE
2944                 echo "---start $PID -----------------------------------------------------" >> $LOG_FILE
2945                 $GDB kamailio $PID -batch --eval-command="bt full" >> $LOG_FILE 2>&1
2946                 echo "---end $PID -------------------------------------------------------" >> $LOG_FILE
2947         done
2948         echo "."
2949 }
2950
2951 ##### ================================================ #####
2952 ### trap with gdb kamailio processes using ps command
2953 #
2954
2955 kamailio_pstrap() {
2956         if [ -z "$GDB" ] ; then
2957                 merr "'gdb' tool not found: set GDB variable to correct tool path"
2958                 exit
2959         fi
2960         DATE=`/bin/date +%Y%m%d_%H%M%S`
2961         LOG_FILE=/tmp/gdb_kamailio_${DATE}.txt
2962         minfo "Trap file: $LOG_FILE"
2963         ps axw | grep kamailio | grep -v grep | grep -v kamctl | sort > $LOG_FILE
2964         echo "" >> $LOG_FILE
2965         echo "" >> $LOG_FILE
2966         echo -n "Trapping Kamailio with gdb: "
2967         PID_TIMESTAMP_VECTOR=`ps axw | grep kamailio | grep -v grep | grep -v kamctl | sort | awk '{print $1}'`
2968         for pid in $PID_TIMESTAMP_VECTOR
2969         do
2970                 echo -n "."
2971                 PID=`echo $pid | cut -d '-' -f 1`
2972                 echo "" >> $LOG_FILE
2973                 echo "---start $PID -----------------------------------------------------" >> $LOG_FILE
2974                 $GDB kamailio $PID -batch --eval-command="p process_no" --eval-command="p pt[process_no]" --eval-command="bt full" >> $LOG_FILE 2>&1
2975                 echo "---end $PID -------------------------------------------------------" >> $LOG_FILE
2976         done
2977         echo "."
2978 }
2979
2980 #
2981 ##### ================================================ #####
2982 ### main command switch
2983 #
2984 case $1 in
2985         acl)
2986                 shift
2987                 acl "$@"
2988                 ;;
2989
2990         add)
2991                 subscriber "$@"
2992                 ;;
2993
2994         show)
2995                 subscriber "$@"
2996                 ;;
2997
2998         passwd)
2999                 subscriber "$@"
3000                 ;;
3001
3002         rm)
3003                 subscriber "$@"
3004                 ;;
3005
3006         sets)
3007                 subscriber "$@"
3008                 ;;
3009
3010         setn)
3011                 subscriber "$@"
3012                 ;;
3013
3014         alias|ul|usrloc)
3015                 usrloc "$@"
3016                 ;;
3017
3018         alias_db|aliasdb)
3019                 alias_db "$@"
3020                 ;;
3021
3022         avp)
3023                 avpops "$@"
3024                 ;;
3025
3026         cisco_restart)
3027                 if [ "$#" -ne 2 ] ; then
3028                         usage_cisco_restart
3029                         exit 1
3030                 fi
3031                 cisco_restart $2
3032                 ;;
3033
3034         db)
3035                 shift
3036                 db_ops "$@"
3037                 ;;
3038
3039         showdb|userdb)
3040                 usage
3041                 exit 1
3042                 ;;
3043
3044         domain)
3045                 shift
3046                 domain "$@"
3047                 ;;
3048
3049         uid_domain)
3050                 shift
3051                 uid_domain "$@"
3052                 ;;
3053
3054         trusted)
3055                 shift
3056                 permissions_trusted "$@"
3057                 ;;
3058
3059         address)
3060                 shift
3061                 permissions_address "$@"
3062                 ;;
3063
3064         mtree)
3065                 shift
3066                 mtree_management "$@"
3067                 ;;
3068
3069         rpc)
3070                 require_ctlengine
3071                 shift
3072                 $CTLCMD "$@"
3073                 ;;
3074
3075         rpcprint)
3076                 require_ctlengine
3077                 shi