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