utils/kamctl/kamctl
30857dfe
 #!/bin/sh
7872a0d8
 #
860c33c5
 # control tool for maintaining Kamailio
7872a0d8
 #
 #===================================================================
 
cff8540d
 ### version for this script
223380dc
 VERSION='5.4.0'
cff8540d
 
43c6ed27
 PATH=$PATH:/usr/local/sbin/
9476def1
 
 # for testing only, please don't enable this in production environments
 # as this introduce security risks
4968e899
 if [ -z "$KAMCTL_TEST" ]; then
 	TEST="false"
 else
 	TEST="true"
 fi
7872a0d8
 
5f72f337
 ### include config files
aba6ee05
 
b209f27d
 # check for rc file at same location with kamctl
ed301d16
 which greadlink >/dev/null 2>&1
aba6ee05
 ret=$?
 if [ $ret -eq 0 ] ; then
b209f27d
 	KAMCTLFULLPATH=$(greadlink -f "$0")
 else
ed301d16
 	which readlink >/dev/null 2>&1
b209f27d
 	ret=$?
 	if [ $ret -eq 0 ] ; then
 		KAMCTLFULLPATH=$(readlink -f "$0")
 	fi
 fi
 if [ -n "$KAMCTLFULLPATH" ] ; then
aba6ee05
 	KAMCTLDIRPATH=$(dirname "$KAMCTLFULLPATH")
 	if [ -f $KAMCTLDIRPATH/kamctlrc ]; then
 		. $KAMCTLDIRPATH/kamctlrc
 	fi
 fi
b209f27d
 
 # check for rc file at standard locations
860c33c5
 if [ -f /etc/kamailio/kamctlrc ]; then
 	. /etc/kamailio/kamctlrc
56d62ffa
 fi
860c33c5
 if [ -f /usr/local/etc/kamailio/kamctlrc ]; then
 	. /usr/local/etc/kamailio/kamctlrc
7872a0d8
 fi
de1a4247
 if [ -f ~/.kamctlrc ]; then
 	. ~/.kamctlrc
7872a0d8
 fi
9476def1
 
5f72f337
 if [ $TEST = "true" ]; then
de1a4247
 	if [ -f ./kamctlrc ]; then
 		. ./kamctlrc
5f72f337
 	fi
 fi
7872a0d8
 
 
5f72f337
 ### force values for variables in this section
de1a4247
 # you better set the variables in ~/.kamctlrc
2b7714a2
 if [ -z "$ETCDIR" ] ; then
860c33c5
 	ETCDIR="/usr/local/etc/kamailio"
2b7714a2
 fi
 
7872a0d8
 if [ -z "$MYDIR" ] ; then
 	MYDIR=`dirname $0`
 fi
 
 if [ -z "$MYLIBDIR" ] ; then
860c33c5
 	MYLIBDIR="/usr/local/lib/kamailio/kamctl"
625c35d5
 	if [ ! -d "$MYLIBDIR" ]; then
7872a0d8
 		MYLIBDIR=$MYDIR
 	fi
 fi
 
f89005fa
 ##### ------------------------------------------------ #####
 ### load base functions
 #
 if [ -f "$MYLIBDIR/kamctl.base" ]; then
 	. "$MYLIBDIR/kamctl.base"
 else
 	echo -e "Cannot load core functions '$MYLIBDIR/kamctl.base' - exiting ...\n"
 	exit -1
 fi
 
9bacc95d
 # locate kamcmd
cff8540d
 if [ -z "$SERCMD" ] ; then
 	# try same dir as kamctl
9bacc95d
 	SERCMD="$MYDIR/kamcmd"
cff8540d
 	if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
 		# try standard location installed from sources
9bacc95d
 		SERCMD="/usr/local/sbin/kamcmd"
cff8540d
 		if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
 			# try source tree location
9bacc95d
 			SERCMD="$MYDIR/../sercmd/kamcmd"
cff8540d
 			if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
 				# try locate it with which
9bacc95d
 				SERCMD=`which kamcmd`
cff8540d
 				if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
9bacc95d
 					mdbg "kamcmd tool not found"
cff8540d
 				fi
 			fi
 		fi
 	fi
 else
 	if [ ! -f "$SERCMD" -o ! -x "$SERCMD" ] ; then
 		merr "SERCMD does not point to an executable file"
 		exit -1;
 	fi
 fi
 
7872a0d8
 #
 ##### ------------------------------------------------ #####
 ### DBENGINE
 #
 DBENGINELOADED=0
 case $DBENGINE in
625c35d5
 	MYSQL|mysql|MySQL)
de1a4247
 		if [ -f "$MYLIBDIR/kamctl.mysql" ]; then
 			. "$MYLIBDIR/kamctl.mysql"
7872a0d8
 			DBENGINELOADED=1
 		fi
 		;;
0f2041d8
 	PGSQL|pgsql|postgres|postgresql|POSTGRESQL)
de1a4247
 		if [ -f "$MYLIBDIR/kamctl.pgsql" ]; then
 			. "$MYLIBDIR/kamctl.pgsql"
7872a0d8
 			DBENGINELOADED=1
 		fi
 		;;
38fac614
 	ORACLE|oracle|Oracle)
de1a4247
 		if [ -f "$MYLIBDIR/kamctl.oracle" ]; then
 			. "$MYLIBDIR/kamctl.oracle"
38fac614
 			DBENGINELOADED=1
 		fi
 		;;
7e4aadcc
 
0f2041d8
 	DBTEXT|dbtext|textdb)
de1a4247
 		if [ -f "$MYLIBDIR/kamctl.dbtext" ]; then
 			. "$MYLIBDIR/kamctl.dbtext"
7e4aadcc
 			DBENGINELOADED=1
 		fi
 		;;
0f2041d8
 	DB_BERKELEY|db_berkeley|BERKELEY|berkeley)
de1a4247
 		if [ -f "$MYLIBDIR/kamctl.db_berkeley" ]; then
 			. "$MYLIBDIR/kamctl.db_berkeley"
0f2041d8
 			DBENGINELOADED=1
 		fi
 		;;
9d36d853
 	SQLITE|sqlite)
 		if [ -f "$MYLIBDIR/kamctl.sqlite" ]; then
 			. "$MYLIBDIR/kamctl.sqlite"
 			DBENGINELOADED=1
 		fi
 		;;
0f2041d8
 
7872a0d8
 esac
 
56778b8f
 if [ $DBENGINELOADED -eq 1 ] ; then
7872a0d8
 	mdbg "database engine '$DBENGINE' loaded"
56778b8f
 elif [ -n "$DBENGINE" ] ; then
 	mwarn "database engine not found - tried '$DBENGINE'"
7872a0d8
 fi
 
 #
 ##### ------------------------------------------------ #####
 ### CTLENGINE
 #
8b445f46
 
9bacc95d
 require_kamcmd() {
8b445f46
 	if [ -z "$SERCMD" ] ; then
9bacc95d
 		merr "kamcmd tool is missing"
8b445f46
 		exit -1
 	fi
 }
 
7872a0d8
 CTLENGINELOADED=0
a255cfce
 CTLENGINETYPE=0
 
1b8ae1ef
 if [ -z "$CTLENGINE" ] ; then
813507b1
 	CTLENGINE="RPCFIFO"
1b8ae1ef
 fi
 case $CTLENGINE in
3b8d7950
 	RPCFIFO|rpcfifo)
 		if [ -f "$MYLIBDIR/kamctl.rpcfifo" ]; then
 			. "$MYLIBDIR/kamctl.rpcfifo"
 			CTLENGINELOADED=1
a255cfce
 			CTLENGINETYPE=1
3b8d7950
 		fi
 		;;
1b8ae1ef
 esac
 
833632a0
 #### ------------------------------------------------- #####
9bacc95d
 ### Load kamcmd interface
833632a0
 #
 if [ -f "$MYLIBDIR/kamctl.ser" ]; then
 	. "$MYLIBDIR/kamctl.ser"
 fi
 
1b8ae1ef
 if [ $CTLENGINELOADED -eq 1 ] ; then
 	mdbg "Control engine '$CTLENGINE' loaded"
 else
 	mwarn "no control engine found - tried '$CTLENGINE'"
 fi
7872a0d8
 
 #
 ##### ------------------------------------------------ #####
 ### common functions
 #
 usage() {
 	CMD=`basename $0`
 	if [ "0$VERIFY_ACL" -eq 1 ] ; then
 		EXTRA_TEXT="ACL privileges are: $ACL_GROUPS"
 	fi
 	cat <<EOF
 $0 $VERSION
5f72f337
 
7872a0d8
 Existing commands:
 EOF
 for f in $USAGE_FUNCTIONS
 do
 	$f
 done
 	echo
 
 }
 
 require_dbengine() {
a255cfce
 	if [ $DBENGINELOADED -eq 0 ] ; then
7e4aadcc
 		merr "This command requires a database engine - none was loaded"
7872a0d8
 		exit -1
 	fi
 }
 
 require_ctlengine() {
a255cfce
 	if [ $CTLENGINELOADED -eq 0 ] ; then
7e4aadcc
 		merr "This command requires a control engine - none was loaded"
7872a0d8
 		exit -1
 	fi
 }
 
 #
 ##### ------------------------------------------------ #####
 ### combined functions (require db and/or ctl)
 #
 #
 
 #
 ##### ------------------------------------------------ #####
 ### helper functions (require db and/or ctl)
 #
 
a255cfce
 ##
8a1c5b01
 # wrapper to run RPC command based on configured engine
 # - first parameter is the RPC command
 # - the rest are the parameters for the RPC command
a255cfce
 ctl_cmd_run () {
 	VCMDRPC="$1"
 	shift
 
 	if [ $CTLENGINETYPE -eq 1 ] ; then
 		$CTLCMD $VCMDRPC $@
 	else
8a1c5b01
 		merr "ctl engine not configured"
 		exit 1
a255cfce
 	fi
 }
 
67252029
 lower() {
 	echo $1 | tr [A-Z] [a-z]
 }
 
7872a0d8
 # params: user
 # output: false if exists, true otherwise
 is_user() {
 	set_user $1
a255cfce
 
7872a0d8
 	QUERY="select count(*) from $SUB_TABLE where \
 $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
 
5932855a
 	CNT=`$DBROCMD "$QUERY" "$DBRAWPARAMS" | $EGREP -v ERROR | $LAST_LINE`
7872a0d8
 	mdbg "is_user: user counter=$CNT"
 	if [ "$CNT" = "0" ] ; then
 		false
 	else
 		true
 	fi
 }
 
 
67252029
 # params: table, column, value
 # output: false if exists, true otherwise
 is_value_in_db() {
 	TABLE=$1
 	COL=$2
 	VALUE=$3
a255cfce
 
67252029
 	QUERY="select count(*) from $TABLE where $COL='$VALUE';"
16f79e84
 	CNT=`$DBROCMD "$QUERY" "$DBRAWPARAMS" | $EGREP -v ERROR | $LAST_LINE`
67252029
 	mdbg "is_value_in_db: counter=$CNT"
 	if [ "$CNT" = "0" ] ; then
 		false
 	else
 		true
 	fi
 }
 
7872a0d8
 #
 ##### ------------------------------------------------ #####
 ### ACL Management
 #
 acl() {
 	require_dbengine
 	case $1 in
 		show)
 			if [ $# -eq 2 ] ; then
28f107d0
 				if [ $VERIFY_USER -eq 1 ] ; then
 					is_user $2
 					if [ $? -ne 0 ] ; then
 						mecho "Non-existent user '$2'. Still proceeding? [Y|N] "
 						read answer
 						if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
 							minfo "Proceeding with non-local user"
 						else
 							exit 1
 						fi
7872a0d8
 					fi
 				fi
 				set_user $2
 				CLAUSE=" WHERE $ACL_USER_COLUMN='$OSERUSER' AND \
 					$ACL_DOMAIN_COLUMN='$OSERDOMAIN' "
 			elif [ $# -ne 1 ] ; then
 				usage_acl
 				exit 1
 			fi
 			QUERY="select * FROM $ACL_TABLE $CLAUSE ; "
 			$DBROCMD "$QUERY"
 
 			;;
 
 		grant)
 			if [ $# -lt 3 ] ; then
 				usage
 				exit 1
 			fi
28f107d0
 			if [ $VERIFY_USER -eq 1 ] ; then
 				is_user $2
 				if [ $? -ne 0 ] ; then
 					mecho "Non-existent user '$2'. Still proceeding? [Y|N] "
 					read answer
 					if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
 						minfo "Proceeding with non-local user"
 					else
 						exit 1
 					fi
7872a0d8
 				fi
 			fi
 			set_user $2
 			shift 2
 			acl_inserted=0
 			while [ $# -gt 0 ] ; do
 
 				if [ $VERIFY_ACL -eq 1 ] ; then
 					found=0
 					for i in $ACL_GROUPS ; do
 						if [ "$1" = "$i" ] ; then
 							found=1
 							break
 						fi
 					done
 					if [ $found -eq 0 ] ; then
 						mwarn "Invalid privilege: acl '$1' ignored"
 						shift
 						continue
 					fi
 				fi
 
 				QUERY="insert into $ACL_TABLE ($ACL_USER_COLUMN,\
 $ACL_GROUP_COLUMN,$ACL_MODIFIED_COLUMN,$ACL_DOMAIN_COLUMN ) values \
3d716a94
 ('$OSERUSER','$1', $DBFNOW, '$OSERDOMAIN' );"
7872a0d8
 				$DBCMD "$QUERY"
 				if [ $? -ne 0 ] ; then
 					merr "acl - SQL Error"
 					exit 1
 				fi
 				acl_inserted=1
 				shift
 			done
 
 			if [ $acl_inserted -eq 1 ] ; then
 				$0 acl show "$OSERUSER@$OSERDOMAIN"
 			fi
 
 			;;
 
 		revoke)
 			if [ $# -eq 3 ] ; then
 				CLAUSE=" and $ACL_GROUP_COLUMN='$3' "
 			elif [ $# -ne 2 ] ; then
 				merr "acl - wrong number of parameters"
 				usage_acl
 				exit 1
a255cfce
 			fi
7872a0d8
 
 			set_user $2
 
 			QUERY="delete from $ACL_TABLE where \
 $ACL_TABLE.$ACL_USER_COLUMN='$OSERUSER' AND $ACL_DOMAIN_COLUMN='$OSERDOMAIN' \
0a11e4d3
 $CLAUSE;"
7872a0d8
 			$DBCMD "$QUERY"
 
 			$0 acl show "$2"
 
 			;;
 
 		*)
bacc3e91
 			merr "acl - invalid commad '$1'"
7872a0d8
 			usage_acl
 			exit 1
 			;;
 	esac
 }
 
 
 #
 ##### ------------------------------------------------ #####
 ### alias management
 #
 check_ul_alias() {
 	require_ctlengine
8a1c5b01
 	RES=`ctl_cmd_run ul.lookup "$ALS_TABLE" "$1@$2"`
7872a0d8
 	RET="$?"
09a0b14f
 	ALIAS_UL_EXISTS=0
7872a0d8
 	if [ $RET -ne 0 ] ; then
860c33c5
 		merr "Kamailio $CTLENGINE not accessible: $RET"
7872a0d8
 		exit 1
 	fi
5932855a
 	echo "$RES" | $EGREP "^404" > /dev/null
7872a0d8
 	if [ $? -ne 0 ] ; then
5932855a
 		echo "$RES" | $EGREP "^400" > /dev/null
7872a0d8
 		if [ $? -eq 0 ] ; then
860c33c5
 			merr "400; check if you use aliases in Kamailio"
7872a0d8
 			exit 1
 		fi
5932855a
 		echo "$RES" | $EGREP "^200" > /dev/null
7872a0d8
 		if [ $? -eq 0 ] ; then
09a0b14f
 			ALIAS_UL_EXISTS=1
7872a0d8
 		fi
 		# other errors
7e4aadcc
 		merr "$RES"
7872a0d8
 		exit 1
 	fi
 }
 
 check_db_alias() {
 	require_dbengine
 
09a0b14f
 	ALIAS_DB_EXISTS=0
 
7872a0d8
 	QUERY="select count(*) from $DA_TABLE where $DA_ALIAS_USER_COLUMN='$1' \
 and $DA_ALIAS_DOMAIN_COLUMN='$2';"
5932855a
 	CNT=`$DBROCMD "$QUERY" | $EGREP -v ERROR | $LAST_LINE`
7872a0d8
 	mdbg "check_db_alias: alias counter=$CNT"
 	if [ "$CNT" = "0" ] ; then
09a0b14f
 		ALIAS_DB_EXISTS=0
7872a0d8
 	else
09a0b14f
 		ALIAS_DB_EXISTS=1
7872a0d8
 	fi
 }
 
 #
 # check for alias duplicates
 #   params: user domain
 #   output: false if exists, true otherwise
 check_alias() {
09a0b14f
 	ALIAS_EXISTS=0
7872a0d8
 
 	if [ "$ENABLE_ALIASES" = "1" ] ; then
 		check_ul_alias "$1" "$2"
09a0b14f
 		if [ "$ALIAS_UL_EXISTS" = "0" ] ; then
 			ALIAS_EXISTS=0
7872a0d8
 		else
09a0b14f
 			ALIAS_EXISTS=1
7872a0d8
 		fi
09a0b14f
 	elif  [ "$ENABLE_ALIASES" = "2" ] ; then
7872a0d8
 		check_db_alias "$1" "$2"
09a0b14f
 		if [ "$ALIAS_DB_EXISTS" = "0" ] ; then
 			ALIAS_EXISTS=0
7872a0d8
 		else
09a0b14f
 			ALIAS_EXISTS=1
7872a0d8
 		fi
 	fi
 }
 
 # db-based aliases
 alias_db() {
 	if [ "$#" -lt 2 ] ; then
 		merr "alias_db - too few parameters"
 		echo
 		usage_alias_db
 		exit 1
 	fi
 
 	require_dbengine
 
 	shift
 
a255cfce
 	case $1 in
7872a0d8
 		list)
 			if [ $# -eq 2 ] ; then
 				# print aliases for user
 				check_aor "$2"
 				if [ "$?" -ne "0" ] ; then
 					merr "alias_db - <$2> is not a valid AoR (user@domain)"
 					exit 1
 				fi
a255cfce
 
7872a0d8
 				set_user $2
a255cfce
 
7872a0d8
 				CLAUSE="WHERE $DA_USER_COLUMN='$OSERUSER' AND \
 $DA_DOMAIN_COLUMN='$OSERDOMAIN'"
 				mecho "Dumping aliases for user=<$2>"
 				echo
 				QUERY="SELECT CONCAT($DA_ALIAS_USER_COLUMN,\
 '@',$DA_ALIAS_DOMAIN_COLUMN) AS ALIAS FROM $DA_TABLE $CLAUSE;"
 				$DBROCMD "$QUERY"
 									# | $AWK 'BEGIN {line=0;}
 									#		/^\+/ { next }
 									#		{	if(line==0) print "ALIASES";
 									#			else print line ")\t" $1 "@" $2;
 									#			line++; }'
 			elif [ $# -eq 1 ] ; then
7e4aadcc
 				mecho "Dumping all aliases may take long: do you want to proceed? [Y|N] "
7872a0d8
 				read answer
 				if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
 					mecho "Dumping all aliases..."
 					echo
 				else
 					exit 1
 				fi
 				QUERY="SELECT $DA_ALIAS_USER_COLUMN, $DA_ALIAS_DOMAIN_COLUMN,\
 $DA_USER_COLUMN, $DA_DOMAIN_COLUMN FROM $DA_TABLE;"
 				$DBROCMD "$QUERY"
 					# | $AWK 'BEGIN {line=0;}
 					#	/^\+/ { next }
 					#	{	line++;
 					#		if(line==1) print "SIP-ID               \tALIAS\n";
 					#		else print $3 "@" $4 "\t" $1 "@" $2 }'
 			else
 				merr "alias_db - wrong number of params for command [list]"
 				echo
 				usage_alias_db
 				exit 1
 			fi
 
 			exit $?
 			;;
 		show)
 			if [ $# -ne 2 ] ; then
 				merr "alias_db - wrong number of params for command [show]"
 				usage_alias_db
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			check_aor "$2"
 			if [ "$?" -ne "0" ] ; then
 				merr "alias_db - $2 is not a valid AoR (user@domain)"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			set_user $2
a255cfce
 
7872a0d8
 			CLAUSE="WHERE $DA_ALIAS_USER_COLUMN='$OSERUSER' AND \
 $DA_ALIAS_DOMAIN_COLUMN='$OSERDOMAIN'"
 			QUERY="SELECT CONCAT($DA_USER_COLUMN,'@',$DA_DOMAIN_COLUMN) \
 AS 'SIP-ID' FROM $DA_TABLE $CLAUSE ; "
 			$DBROCMD "$QUERY"
 			#TMP_UUID=`sql_ro_query "$QUERY" | $AWK 'BEGIN {line=0;}
a255cfce
 			#								/^\+/ { next }
7872a0d8
 			#								{ line++;
 			#								  if(line==2) print $1 "@" $2;}'`
 			#
 			#if [ "$TMP_UUID" = "" ] ; then
 			#	mecho "non-existent alias <$2>"
 			#	exit 1
 			#fi
 			#
 			#echo "Details for alias <$2>"
 			#echo
 			#echo "SIP-ID: $TMP_UUID"
 			#echo
 			#exit $?
 			;;
 		add)
 			if [ $# -ne 3 ] ; then
 				usage_alias_db
 				exit 1
 			fi
 			shift
 			check_aor "$1"
 			if [ "$?" -ne "0" ] ; then
 				err "alias_db - $1 is not a valid AoR (user@domain)"
 				exit 1
 			fi
 
 			check_aor "$2"
 			if [ "$?" -ne "0" ] ; then
 				err "alias_db - $2 is not a valid AoR (user@domain)"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			set_user $1
 			TMP_OSERUSER=$OSERUSER
 			TMP_OSERDOMAIN=$OSERDOMAIN
 			set_user $2
a255cfce
 
52536182
 			if is_value_in_db $DA_TABLE $DA_ALIAS_USER_COLUMN $TMP_OSERUSER; then
67252029
 				minfo "$TMP_OSERUSER alias already in $DA_TABLE table"
 				exit 0
 			fi
 
7872a0d8
 			QUERY="INSERT INTO $DA_TABLE ($DA_USER_COLUMN,$DA_DOMAIN_COLUMN,\
 $DA_ALIAS_USER_COLUMN,$DA_ALIAS_DOMAIN_COLUMN) VALUES ('$OSERUSER',\
 '$OSERDOMAIN','$TMP_OSERUSER','$TMP_OSERDOMAIN' );"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "alias_db - SQL Error"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			exit $?
 			;;
 		rm)
 			if [ $# -ne 2 ] ; then
 				merr "alias_db - wrong numbers of parameters"
 				usage_alias_db
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			shift
a255cfce
 
7872a0d8
 			check_aor "$1"
 			if [ "$?" -ne "0" ] ; then
 				merr "alias_db - $1 is not a valid URI"
 				exit 1
 			fi
 
 			set_user $1
 			CLAUSE="WHERE $DA_ALIAS_USER_COLUMN='$OSERUSER' AND \
 $DA_ALIAS_DOMAIN_COLUMN='$OSERDOMAIN'"
 			QUERY="DELETE FROM $DA_TABLE $CLAUSE;"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "alias_db - SQL Error"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			exit $?
 			;;
a255cfce
 
7872a0d8
 		help)
 			usage_alias_db "alone"
 			;;
a255cfce
 
7872a0d8
 		*)
 			usage_alias_db
 			exit 1
 			;;
 	esac
 } # end db-aliases
 
 
 #
 ##### ------------------------------------------------ #####
 ### AVP management
 #
 # avp list [-T table] [-u <sip-id|uuid>]
 #     [-a attribute] [-v value] [-t type] ... list AVPs
 # avp add [-T table] <sip-id|uuid>
 #     <attribute> <type> <value> ............ add AVP (*)
 # avp rm [-T table]  [-u <sip-id|uuid>]
 #     [-a attribute] [-v value] [-t type] ... remove AVP (*)
 
 avpops() {
 	require_dbengine
 	if [ "$#" -lt 2 ] ; then
 		merr "avp - too few parameters"
 		minfo "see '$0 avp help'"
 		exit 1
 	fi
 	if [ "$1" = "avp" ] ; then
 		shift
 	else
 		merr "avp - unknown command $1"
 		minfo "see '$0 avp help'"
 		exit 1
 	fi
 
a255cfce
 	case $1 in
7872a0d8
 		list)
 			shift
 			CLAUSE=""
a255cfce
 			while [ "$#" != "0" ]
7872a0d8
 			do
 				TMP_ARG=$1
 				shift
a255cfce
 				case $TMP_ARG in
7872a0d8
 					-T)
 						if [ -z "$1" ] ; then
 							merr "avp list - table name parameter missing"
 							exit 1
 						fi
 						AVP_TABLE=$1
 					;;
 					-u)
 						if [ -z "$1" ] ; then
 							merr "avp list - user id or uuid parameter missing"
 							exit 1
 						fi
 						is_aor "$1"
 						if [ "$?" -eq "0" ] ; then
 							set_user $1
 							if [ "$CLAUSE" = "" ] ; then
 								CLAUSE=" WHERE $AVP_USER_COLUMN='$OSERUSER' \
 AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
 							else
 								CLAUSE="$CLAUSE AND \
 $AVP_USER_COLUMN='$OSERUSER' AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
 							fi
 						else
 							if [ "$CLAUSE" = "" ] ; then
 								CLAUSE=" WHERE $AVP_UUID_COLUMN='$1'"
 							else
 								CLAUSE="$CLAUSE AND $AVP_UUID_COLUMN='$1'"
 							fi
a255cfce
 						fi
7872a0d8
 					;;
 					-a)
 						if [ -z "$1" ] ; then
 							merr "avp list - attribute name parameter missing"
 							exit 1
 						fi
 						if [ "$CLAUSE" = "" ] ; then
 							CLAUSE=" WHERE $AVP_ATTRIBUTE_COLUMN='$1'"
 						else
 							CLAUSE="$CLAUSE AND $AVP_ATTRIBUTE_COLUMN='$1'"
 						fi
 					;;
 					-v)
 						if [ -z "$1" ] ; then
 							merr "avp list - value parameter missing"
 							exit 1
 						fi
 						if [ "$CLAUSE" = "" ] ; then
 							CLAUSE=" WHERE $AVP_VALUE_COLUMN='$1'"
 						else
 							CLAUSE="$CLAUSE AND $AVP_VALUE_COLUMN='$1'"
 						fi
 					;;
 					-t)
 						if [ -z "$1" ] ; then
 							merr "avp list - type parameter missing"
 							exit 1
 						fi
 						if [ "$CLAUSE" = "" ] ; then
 							CLAUSE=" WHERE $AVP_TYPE_COLUMN='$1'"
 						else
 							CLAUSE="$CLAUSE AND $AVP_TYPE_COLUMN='$1'"
 						fi
 					;;
 					*)
 						merr "avp list - unknown parameter $1"
 						exit 1
 					;;
 				esac
 				shift
 			done
a255cfce
 
7872a0d8
 			QUERY="SELECT $AVP_UUID_COLUMN,$AVP_USER_COLUMN,\
 $AVP_DOMAIN_COLUMN,$AVP_ATTRIBUTE_COLUMN,$AVP_TYPE_COLUMN,$AVP_VALUE_COLUMN \
 FROM $AVP_TABLE $CLAUSE;"
 			mdbg "Query: $QUERY"
 			mecho "Dumping AVPs"
 			echo
 			$DBROCMD "$QUERY"
 			# | $AWK 'BEGIN {line=0;}
 			#		/^\+/ { next }
 			#		{	if(line==0) print "##   UUID   \tUserID     \tAttribute     \tType     \tValue\n";
 			#			else {
 			#				ORS_BAK=ORS;
 			#				ORS="";
 			#				print line ")  " $1  $2 "@" $3 "\t" $4 "\t\"" $5;
 			#				for (i=6;i<=NF;++i) print FS $i;
 			#				ORS=ORS_BAK;
 			#				print "\"";
 			#			}
 			#			line++;
 			#		}'
a255cfce
 
7872a0d8
 			exit $?
 			;;
a255cfce
 
7872a0d8
 		add)
 			shift
 			if [ $# -ne 4 ] ; then
 				if [ $# -ne 6 ] ; then
 					merr "avp add - bad number of parameters"
 					exit 1
 				fi
 			fi
 			if [ $# -eq 6 ] ; then
 				if [ "$1" = "-T" ] ; then
 					AVP_TABLE=$2
 					shift
 					shift
 				else
7e4aadcc
 					mecho "avp add - unknown parameter '$1'"
a255cfce
 					exit 1
7872a0d8
 				fi
 			fi
a255cfce
 
7872a0d8
 			is_aor "$1"
 			if [ "$?" -eq "0" ] ; then
 				set_user $1
 			else
 				AVP_UUID=$1
 			fi
 
 			QUERY="INSERT INTO $AVP_TABLE \
 ($AVP_UUID_COLUMN,$AVP_USER_COLUMN,$AVP_DOMAIN_COLUMN,$AVP_ATTRIBUTE_COLUMN,\
 $AVP_TYPE_COLUMN,$AVP_VALUE_COLUMN,$AVP_MODIFIED_COLUMN) \
3d716a94
 VALUES ('$AVP_UUID','$OSERUSER','$OSERDOMAIN','$2',$3,'$4',$DBFNOW);"
7872a0d8
 			# echo "Query: $QUERY"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "avp add - SQL Error"
 				exit 1
 			else
 				echo
 				mecho "avp add - attribute added"
 			fi
a255cfce
 
7872a0d8
 			exit $?
 			;;
 
 		rm)
 			shift
 			CLAUSE=""
a255cfce
 			while [ "$#" != "0" ]
7872a0d8
 			do
 				TMP_ARG=$1
 				shift
a255cfce
 				case $TMP_ARG in
7872a0d8
 					-T)
 						if [ -z "$1" ] ; then
 							merr "avp rm - table name parameter missing"
 							exit 1
 						fi
 						AVP_TABLE=$1
 					;;
 					-u)
 						if [ -z "$1" ] ; then
 							merr "avp rm - user id or uuid parameter missing"
 							exit 1
 						fi
 						is_aor "$1"
 						if [ "$?" -eq "0" ] ; then
 							set_user $1
 							if [ "$CLAUSE" = "" ] ; then
 								CLAUSE="WHERE $AVP_USER_COLUMN='$OSERUSER' \
 AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
 							else
 								CLAUSE="$CLAUSE AND \
 $AVP_USER_COLUMN='$OSERUSER' AND $AVP_DOMAIN_COLUMN='$OSERDOMAIN'"
 							fi
 						else
 							if [ "$CLAUSE" = "" ] ; then
 								CLAUSE="WHERE $AVP_UUID_COLUMN='$1'"
 							else
 								CLAUSE="$CLAUSE AND $AVP_UUID_COLUMN='$1'"
 							fi
a255cfce
 						fi
7872a0d8
 					;;
 					-a)
 						if [ -z "$1" ] ; then
 							merr "avp rm - attribute name parameter missing"
 							exit 1
 						fi
 						if [ "$CLAUSE" = "" ] ; then
 							CLAUSE="WHERE $AVP_ATTRIBUTE_COLUMN='$1'"
 						else
 							CLAUSE="$CLAUSE AND $AVP_ATTRIBUTE_COLUMN='$1'"
 						fi
 					;;
 					-v)
 						if [ -z "$1" ] ; then
 							merr "avp rm - value parameter missing"
 							exit 1
 						fi
 						if [ "$CLAUSE" = "" ] ; then
 							CLAUSE="WHERE $AVP_VALUE_COLUMN='$1'"
 						else
 							CLAUSE="$CLAUSE AND $AVP_VALUE_COLUMN='$1'"
 						fi
 					;;
 					-t)
 						if [ -z "$1" ] ; then
 							merr "avp rm - type parameter missing"
 							exit 1
 						fi
 						if [ "$CLAUSE" = "" ] ; then
 							CLAUSE="WHERE $AVP_TYPE_COLUMN='$1'"
 						else
 							CLAUSE="$CLAUSE AND $AVP_TYPE_COLUMN='$1'"
 						fi
 					;;
 					*)
 						merr "avp rm - unknown parameter $1"
 						exit 1
 					;;
 				esac
 				shift
 			done
 			QUERY="DELETE FROM $AVP_TABLE $CLAUSE;"
 			mdbg "Query: $QUERY"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "avp rm - SQL Error"
 				exit 1
 			else
 				echo
 				mecho "avp rm - AVP(s) deleted"
 			fi
a255cfce
 
7872a0d8
 			exit $?
 			;;
a255cfce
 
7872a0d8
 		help)
 			usage_avp
 			;;
a255cfce
 
7872a0d8
 		*)
 			merr "avp - unknown command"
 			usage
 			exit 1
 			;;
 	esac
 } # end avpops()
 
 #
 ##### ------------------------------------------------ #####
 ### cisco restart
 #
 cisco_restart() {
 	require_ctlengine
 	myhost=`get_my_host`
359050f9
 	CMDPARAMS="= tm.t_uac_start NOTIFY \"$1\" \".\" \".\" \"From:sip:daemon@$myhost=CRLF=To:<$1>=CRLF=Event:check-sync=CRLF=Contact:sip:daemon@$myhost=CRLF=\""
ca036d85
 
4a4f0475
 	ctl_cmd_run $CMDPARAMS
7872a0d8
 }
 
 #
 ##### ------------------------------------------------ #####
 ### DB operations
 #
 db_ops() {
 	require_dbengine
 	case $1 in
f62dc1f2
 		exec|query)
7872a0d8
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing query parameter"
 				exit 1
 			fi
 			$DBCMD "$1"
 			;;
f62dc1f2
 		roexec|roquery)
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing query parameter"
 				exit 1
 			fi
 			$DBROCMD "$1"
 			;;
 		run)
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing query parameter"
 				exit 1
 			fi
 			eval QUERY=\$$1
 			if [ -z "$QUERY" ] ; then
 				merr "missing query value"
 				exit 1
 			fi
 			$DBCMD "$QUERY"
 			;;
 		rorun)
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing query parameter"
 				exit 1
 			fi
 			eval QUERY=\$$1
 			if [ -z "$QUERY" ] ; then
 				merr "missing query value"
 				exit 1
 			fi
 			$DBROCMD "$QUERY"
 			;;
7872a0d8
 		show)
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing table parameter"
 				exit 1
 			fi
 			QUERY="select * FROM $1;"
 			$DBROCMD "$QUERY"
 			;;
c5fac480
 		showg)
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing table parameter"
 				exit 1
 			fi
 			QUERY="select * FROM $1\\G;"
 			$DBROCMD "$QUERY"
 			;;
4bc07b15
 		smatch)
 			shift
 			if [ $# -ne 3 ] ; then
 				merr "missing parameters"
 				exit 1
 			fi
 			QUERY="SELECT * FROM $1 WHERE $2='$3'\\G;"
 			$DBROCMD "$QUERY"
 			;;
 		nmatch)
 			shift
 			if [ $# -ne 3 ] ; then
 				merr "missing parameters"
 				exit 1
 			fi
 			QUERY="SELECT * FROM $1 WHERE $2=$3\\G;"
 			$DBROCMD "$QUERY"
 			;;
e395c9bb
 		connect)
 			shift
 			if [ -z "$DBCMDCONNECT" ] ; then
 				merr "extension not implemented"
 				exit 1
 			fi
 			$DBCMDCONNECT
80536540
 			;;
d3b7f0d3
 		version)
 			shift
 			if [ $# -ne 3 ] ; then
 				merr "missing parameters"
 				exit 1
 			fi
 			case $1 in
 				add)
 					QUERY="INSERT INTO version (table_name, table_version) VALUES ('$2', $3);"
 					$DBCMD "$QUERY"
 				;;
 				set)
 					QUERY="DELETE FROM version WHERE table_name='$2';"
 					$DBCMD "$QUERY"
 					QUERY="INSERT INTO version (table_name, table_version) VALUES ('$2', $3);"
 					$DBCMD "$QUERY"
 				;;
 				update)
 					QUERY="UPDATE version SET table_version=$3 WHERE table_name='$2';"
 					$DBCMD "$QUERY"
 				;;
 				*)
 					usage_db_ops
 					exit 1
 			esac
 			;;
7872a0d8
 		*)
 			usage_db_ops
 			exit 1
 	esac
 }
 
 #
 ##### ------------------------------------------------ #####
 ### domain management
 #
 domain() {
 	case $1 in
 		reload)
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run domain.reload
7872a0d8
 			;;
 		show)
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run domain.dump
7872a0d8
 			;;
 		showdb)
 			require_dbengine
 			QUERY="select * FROM $DOMAIN_TABLE ; "
 			$DBROCMD "$QUERY"
 			;;
 		add)
 			require_dbengine
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing domain parameter"
 				exit 1
 			fi
52536182
 			if is_value_in_db $DOMAIN_TABLE $DO_DOMAIN_COLUMN $1; then
67252029
 				minfo "$1 already in $DOMAIN_TABLE table"
 				exit 0
 			fi
 			QUERY="insert into $DOMAIN_TABLE ($DO_DOMAIN_COLUMN, \
3d716a94
 			$DO_LAST_MODIFIED_COLUMN) VALUES ('$1',$DBFNOW);"
7872a0d8
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "domain - SQL Error"
 				exit 1
 			fi
7e4aadcc
 			minfo "execute '$0 domain reload' to synchronize cache and database"
7872a0d8
 			;;
 		rm)
 			require_dbengine
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing domain parameter"
 				exit 1
 			fi
bacc3e91
 			QUERY="delete from $DOMAIN_TABLE where domain='$1';"
7872a0d8
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
7e4aadcc
 				merr "domain - SQL Error"
7872a0d8
 				exit 1
 			fi
7e4aadcc
 			minfo "execute '$0 domain reload' to synchronize cache and database"
7872a0d8
 			;;
 		*)
 			usage_domain
 			exit 1
 	esac
67252029
 }
 
bc507d28
 #
 ##### ------------------------------------------------ #####
 ### uid_domain management
 #
 uid_domain() {
 	case $1 in
 		reload)
 			require_kamcmd
 			$SERCTLCMD domain.reload
 			;;
 		show)
 			require_kamcmd
 			$SERCTLCMD domain.dump
 			;;
 		showdb)
 			require_dbengine
 			QUERY="select * FROM $UID_DOMAIN_TABLE ; "
 			$DBROCMD "$QUERY"
 			;;
 		add)
 			require_dbengine
 			shift
 			if [ $# -lt 1 ] ; then
 				merr "too few parameters"
 				exit 1
 			fi
a255cfce
 
bc507d28
 			DOMAIN=$1
 			DID=$2
 			FLAGS=$3
a255cfce
 
bc507d28
 			if [ -z "$2" ] ; then
 				DID=$DOMAIN
a255cfce
 			fi
bc507d28
 			if [ -z "$3" ] ; then
 				FLAGS=$(( $SRDB_LOAD_SER | $SRDB_CANON | $SRDB_FOR_SERWEB ))
a255cfce
 			fi
bc507d28
 
 			if is_value_in_db $UID_DOMAIN_TABLE $UID_DO_DOMAIN_COLUMN $DOMAIN; then
 				minfo "$1 already in $UID_DOMAIN_TABLE table"
 				exit 0
 			fi
 			QUERY="insert into $UID_DOMAIN_TABLE ($UID_DO_DID_COLUMN,$UID_DO_DOMAIN_COLUMN,$UID_DO_FLAGS_COLUMN) VALUES ('$DID','$DOMAIN',$FLAGS);"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "uid_domain - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 uid_domain reload' to synchronize cache and database"
 			;;
 		rm)
 			require_dbengine
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing domain parameter"
 				exit 1
 			fi
 			QUERY="delete from $UID_DOMAIN_TABLE where domain='$1';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "domain - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 uid_domain reload' to synchronize cache and database"
 			;;
 		*)
 			usage_uid_domain
 			exit 1
 	esac
 }
 
67252029
 #
 ##### ------------------------------------------------ #####
7b976204
 ### permissions trusted management
67252029
 #
7b976204
 permissions_trusted() {
67252029
 	case $1 in
 		reload)
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run permissions.trustedReload
67252029
 			;;
 		dump)
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run permissions.trustedDump
67252029
 			;;
 		show)
 			require_dbengine
 			QUERY="select * FROM $TRUSTED_TABLE ; "
 			$DBROCMD "$QUERY"
 			;;
 		add)
 			require_dbengine
 			shift
 			if [ $# -lt 2 ] ; then
 				usage_trusted
 				exit 1
 			fi
52536182
 			if is_value_in_db $TRUSTED_TABLE src_ip $1; then
67252029
 				minfo "$1 already in $TRUSTED_TABLE table"
 				exit 0
 			fi
 			case $2 in
 				any|udp|tcp|tls|sctp|none)
 					;;
 				*)
 					merr "unknown protocol"
 					exit 1
 			esac
aeeb7944
 			PATTERN=""
67252029
 			if [ ! -z "$3" ]; then
 				PATTERN="$3"
 			fi
 
 			QUERY="insert into $TRUSTED_TABLE \
 				( $TRUSTED_SRC_IP_COLUMN, $TRUSTED_PROTO_COLUMN, \
 				$TRUSTED_FROM_PATTERN_COLUMN, $TRUSTED_TAG_COLUMN) \
aeeb7944
 				VALUES ('$1', '$2', '$PATTERN', '$4');"
67252029
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "trusted - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 trusted reload' to synchronize cache and database"
 			;;
 		rm)
 			require_dbengine
 			shift
28328b90
 			if [ $# -ne 1 ] ; then
67252029
 				usage_trusted
 				exit 1
 			fi
 			QUERY="delete from $TRUSTED_TABLE where $TRUSTED_SRC_IP_COLUMN='$1';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "trusted - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 trusted reload' to synchronize cache and database"
 			;;
 		*)
 			usage_trusted
 			exit 1
 	esac
7872a0d8
 }
 
7b976204
 
 #
 ##### ------------------------------------------------ #####
 ### permissions address management
 #
 permissions_address() {
 	case $1 in
 		reload)
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run permissions.addressReload
7b976204
 			;;
 		dump)
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run permissions.addressDump
 			ctl_cmd_run permissions.subnetDump
7b976204
 			;;
 		show)
 			require_dbengine
 			QUERY="select * FROM $ADDRESS_TABLE ; "
 			$DBROCMD "$QUERY"
 			;;
 		add)
 			require_dbengine
 			shift
 			if [ $# -lt 2 ] ; then
 				usage_address
 				exit 1
 			fi
 			AMASK=32
 			if [ ! -z "$3" ]; then
 				AMASK="$3"
 			fi
 			APORT=0
 			if [ ! -z "$4" ]; then
 				APORT="$4"
 			fi
 			ATAG=""
 			if [ ! -z "$5" ]; then
 				ATAG="$5"
 			fi
 
 			QUERY="insert into $ADDRESS_TABLE \
 				(grp, ip_addr, mask, port, tag) \
 				VALUES ($1, '$2', $AMASK, $APORT, '$ATAG');"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "permissions address - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 address reload' to synchronize cache and database"
 			;;
 		rm)
 			require_dbengine
 			shift
 			if [ $# -ne 2 ] ; then
 				usage_address
 				exit 1
 			fi
 			QUERY="delete from $ADDRESS_TABLE where grp=$1 and ip_addr='$2';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "permissions address - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 address reload' to synchronize cache and database"
 			;;
 		*)
 			usage_address
 			exit 1
 	esac
 }
 
 
0bd3c9ea
 #
 ##### ------------------------------------------------ #####
 ### mtree management
 #
 mtree_management() {
 	case $1 in
 		reload)
 			require_ctlengine
 			ctl_cmd_run mtree.reload $2
 			;;
 		dump)
 			require_ctlengine
 			ctl_cmd_run mtree.list $2
 			;;
 		showdb)
 			require_dbengine
 			QUERY="select * FROM $2 ; "
 			$DBROCMD "$QUERY"
 			;;
 		add)
 			require_dbengine
 			if [ $# -lt 4 ] ; then
 				usage_mtree
 				exit 1
 			fi
 
 			QUERY="INSERT INTO $2 \
 				(tprefix, tvalue) \
 				VALUES ($3, '$4');"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "mtree - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 mtree reload' to synchronize cache and database"
 			;;
 		rm)
 			require_dbengine
 			shift
 			if [ $# -ne 3 ] ; then
 				usage_address
 				exit 1
 			fi
 			QUERY="DELETE FROM $2 where tprefix='$3';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "mtree - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 mtree reload' to synchronize cache and database"
 			;;
 		*)
 			usage_mtree
 			exit 1
 	esac
 }
 
 
7872a0d8
 #
 ##### ------------------------------------------------ #####
 ### LCR management
 #
 lcr() {
 	case $1 in
10802946
 		show_gws)
95792d55
 			merr "command disabled"
05e8b301
 			exit 1
f6673e29
 			require_dbengine
7872a0d8
 			mecho "lcr gateways"
10802946
 			QUERY="select * FROM $GW_TABLE ORDER BY $LCR_ID_COLUMN, $LCR_GW_GRPID_COLUMN; "
7872a0d8
 			$DBROCMD "$QUERY"
 			;;
10802946
 		show_routes)
95792d55
 			merr "command disabled"
05e8b301
 			exit 1
f6673e29
 			require_dbengine
10802946
 			mecho "lcr routes"
 			QUERY="select * FROM $LCR_TABLE ORDER BY $LCR_ID_COLUMN, $LCR_PREFIX_COLUMN; "
 			$DBROCMD "$QUERY"
7872a0d8
 			;;
10802946
 		reload)
95792d55
 			merr "command disabled"
05e8b301
 			exit 1
8a1c5b01
 			ctl_cmd_run lcr.reload
7872a0d8
 			;;
10802946
 		dump_gws)
95792d55
 			merr "command disabled"
05e8b301
 			exit 1
8a1c5b01
 			ctl_cmd_run lcr.dump_gws
7872a0d8
 			;;
10802946
 		dump_routes)
95792d55
 			merr "command disabled"
05e8b301
 			exit 1
8a1c5b01
 			ctl_cmd_run lcr.dump_lcrs
7872a0d8
 			;;
f6673e29
 		eval_weights)
 			shift
 			$AWK 'BEGIN {
 				if (ARGC < 2) {
 					printf("Usage: lcr eval_weights <list of weights (integers 1-254)>\n");
 					exit;
 				}
 				iters = 100000;
 				for (i = 1; i < ARGC; i++) { counts[i] = 0; }
 				for (i = 1; i <= iters; i++) {
 					for (j = 1; j < ARGC; j++) {
 						elem[j] = ARGV[j] * rshift(int(2147483647 * rand()), 8);
 					}
 					at = 1;
 					max = elem[at];
 					for (j = 2; j < ARGC; j++) {
 						if (elem[j] > max) {
 							max = elem[j];
 							at = j;
 						}
 					}
 					counts[at] = counts[at] + 1;
 				}
 				for (i = 1; i < ARGC; i++) {
 					printf("weight %d probability %.4f\n", ARGV[i], counts[i]/iters);
 				}
 			}' $@
 			;;
7872a0d8
 		*)
f23f6846
 			usage_lcr
7872a0d8
 			exit 1
 	esac
 }
 
585f2ed1
 #
 ##### ------------------------------------------------ #####
 ### CARRIERROUTE management
 #
 cr() {
 	require_dbengine
 	require_ctlengine
67252029
 	case $1 in
 		show)
bbd176ff
 			mecho "cr carrier names"
 			QUERY="select * FROM $CARRIER_NAME_TABLE ORDER BY $CARRIERROUTE_CARRIER_NAME_ID_COLUMN; "
 			$DBROCMD "$QUERY"
 			mecho "cr domain names"
 			QUERY="select * FROM $DOMAIN_NAME_TABLE ORDER BY $CARRIERROUTE_DOMAIN_NAME_ID_COLUMN; "
67252029
 			$DBROCMD "$QUERY"
 			mecho "cr routes"
 			QUERY="select * FROM $CARRIERROUTE_TABLE ORDER BY \
 				$CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN,\
 				$CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN,\
 				$CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN,\
 				$CARRIERROUTE_CARRIERROUTE_PROB_COLUMN;"
 			$DBROCMD "$QUERY"
 			;;
 		reload)
8a1c5b01
 			ctl_cmd_run cr.reload_routes
67252029
 			;;
585f2ed1
 
67252029
 		dump)
8a1c5b01
 			ctl_cmd_run cr.dump_routes
67252029
 			;;
464a4d33
 
bbd176ff
 		addcn)
 			shift
 			if [ $# -ne 2 ] ; then
 				merr "cr - missing carrier id or name"
 				exit 1
 			fi
 			QUERY="insert into $CARRIER_NAME_TABLE
 				( $CARRIERROUTE_CARRIER_NAME_ID_COLUMN, \
 				$CARRIERROUTE_CARRIER_NAME_CARRIER_COLUMN) \
 				VALUES ($1, '$2');"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "cr - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 cr reload' to synchronize cache and database"
 			;;
 
 		rmcn)
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "cr - missing carrier id to be removed"
 				exit 1
 			fi
 			QUERY="delete from $CARRIER_NAME_TABLE where $CARRIERROUTE_CARRIER_NAME_ID_COLUMN='$1';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "cr - SQL Error"
a255cfce
 				exit 1
bbd176ff
 			fi
 			minfo "execute '$0 cr reload' to synchronize cache and database"
 			;;
 
 		adddn)
67252029
 			shift
 			if [ $# -ne 2 ] ; then
bbd176ff
 				merr "cr - missing domain id or name"
585f2ed1
 				exit 1
67252029
 			fi
bbd176ff
 			QUERY="insert into $DOMAIN_NAME_TABLE
 				( $CARRIERROUTE_DOMAIN_NAME_ID_COLUMN, \
 				$CARRIERROUTE_DOMAIN_NAME_DOMAIN_COLUMN) \
67252029
 				VALUES ($1, '$2');"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "cr - SQL Error"
 				exit 1
 			fi
 			minfo "execute '$0 cr reload' to synchronize cache and database"
 			;;
585f2ed1
 
bbd176ff
 		rmdn)
67252029
 			shift
 			if [ $# -ne 1 ] ; then
bbd176ff
 				merr "cr - missing domain id to be removed"
67252029
 				exit 1
 			fi
bbd176ff
 			QUERY="delete from $DOMAIN_NAME_TABLE where $CARRIERROUTE_DOMAIN_NAME_ID_COLUMN='$1';"
67252029
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "cr - SQL Error"
a255cfce
 				exit 1
67252029
 			fi
 			minfo "execute '$0 cr reload' to synchronize cache and database"
 			;;
585f2ed1
 
67252029
 		addcarrier)
 			shift
 			if [ $# -lt 4 ] ; then
 				merr "cr - too few parameters"
 				exit 1
 			fi
cfba6608
 			PROB=1
 			STRIP=0
 			REWRITE_PREFIX=
 			REWRITE_SUFFIX=
 			COMMENT=
7f76fb70
 			FLAGS=0
 			MASK=0
67252029
 			if [ $# -gt 4 ] ; then
 				PROB=$5
 				if [ $# -gt 5 ] ; then
 					STRIP=$6
 					if [ $# -gt 6 ] ; then
 						REWRITE_PREFIX=$7
 						if [ $# -gt 7 ] ; then
 							REWRITE_SUFFIX=$8
 							if [ $# -gt 8 ] ; then
7b8b6f64
 								MASK=$9
cfba6608
 								if [ $# -gt 9 ] ; then
 									FLAGS=${10}
 									if [ $# -gt 10 ] ; then
7b8b6f64
 										COMMENT=${11}
cfba6608
 									fi
 								fi
585f2ed1
 							fi
 						fi
 					fi
 				fi
67252029
 			fi
 			CARRIER=$1
 			SCAN_PREFIX=$2
 			DOMAIN=$3
 			REWRITE_HOST=$4
cfba6608
 			echo $FLAGS
 			echo $MASK
67252029
 			QUERY="insert into $CARRIERROUTE_TABLE \
 				( $CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_PROB_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_STRIP_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_REWRITE_HOST_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_REWRITE_PREFIX_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_REWRITE_SUFFIX_COLUMN, \
cfba6608
 				$CARRIERROUTE_CARRIERROUTE_COMMENT_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_FLAGS_COLUMN, \
 				$CARRIERROUTE_CARRIERROUTE_MASK_COLUMN ) \
 				VALUES ($CARRIER, '$SCAN_PREFIX', '$DOMAIN', $PROB, $STRIP, \
 				'$REWRITE_HOST', '$REWRITE_PREFIX', '$REWRITE_SUFFIX', '$COMMENT', \
 				$FLAGS, $MASK);"
67252029
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "cr - SQL Error"
585f2ed1
 				exit 1
67252029
 			fi
 			minfo "execute '$0 cr reload' to synchronize cache and database"
 			;;
387eb8a4
 
67252029
 		rmcarrier)
 			shift
 			if [ $# -ne 3 ] ; then
 				merr "cr - too few parameters"
387eb8a4
 				exit 1
67252029
 			fi
 			CARRIER=$1
 			SCAN_PREFIX=$2
 			DOMAIN=$3
 			QUERY="delete from $CARRIERROUTE_TABLE where $CARRIERROUTE_CARRIERROUTE_CARRIER_COLUMN='$CARRIER' AND \
 				$CARRIERROUTE_CARRIERROUTE_SCAN_PREFIX_COLUMN='$SCAN_PREFIX' AND \
 				$CARRIERROUTE_CARRIERROUTE_DOMAIN_COLUMN=$DOMAIN ;"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "cr - SQL Error"
387eb8a4
 				exit 1
67252029
 			fi
 			minfo "execute '$0 cr reload' to synchronize cache and database"
 			;;
387eb8a4
 
67252029
 		*)
 			usage_cr
 			exit 1
 	esac
387eb8a4
 }
 
464a4d33
 #
 ##### ------------------------------------------------ #####
 ### DISPATCHER management
 #
 dispatcher() {
67252029
 	case $1 in
 		show)
d2416957
 			require_dbengine
67252029
 			mecho "dispatcher gateways"
 			QUERY="select * FROM $DISPATCHER_TABLE ORDER BY $DISPATCHER_SETID_COLUMN; "
 			$DBROCMD "$QUERY"
 			;;
1cc0144e
 		addgw|add)
d2416957
 			require_dbengine
67252029
 			shift
1cc0144e
 			if [ $# -lt 2 ] ; then
67252029
 				merr "too few parameters"
464a4d33
 				usage_dispatcher
 				exit 1
67252029
 			fi
464a4d33
 
1cc0144e
 			DISPATCHER_SETID=$1
 			DISPATCHER_DESTINATION=$2
 
 			if [ $# -gt 2 ] ; then
 				DISPATCHER_FLAGS=$3
 			else
 				DISPATCHER_FLAGS=0
 			fi
 
67252029
 			if [ $# -gt 3 ] ; then
1cc0144e
 				DISPATCHER_PRIORITY=$4
67252029
 			else
1cc0144e
 				DISPATCHER_PRIORITY=0
 			fi
464a4d33
 
1cc0144e
 			if [ $# -gt 4 ] ; then
 				DISPATCHER_ATTRS=$5
 			else
 				DISPATCHER_ATTRS=""
 			fi
 
 			if [ $# -gt 5 ] ; then
 				DISPATCHER_DESCRIPTION=$6
 			else
 				DISPATCHER_DESCRIPTION=""
 			fi
464a4d33
 
67252029
 			QUERY="insert into $DISPATCHER_TABLE \
1cc0144e
 				( $DISPATCHER_SETID_COLUMN, $DISPATCHER_DESTINATION_COLUMN, $DISPATCHER_FLAGS_COLUMN, $DISPATCHER_PRIORITY_COLUMN, $DISPATCHER_ATTRS_COLUMN, $DISPATCHER_DESCRIPTION_COLUMN ) \
 				VALUES ($DISPATCHER_SETID,'$DISPATCHER_DESTINATION',$DISPATCHER_FLAGS,$DISPATCHER_PRIORITY,'$DISPATCHER_ATTRS','$DISPATCHER_DESCRIPTION');"
67252029
 			$DBCMD "$QUERY"
464a4d33
 
67252029
 			if [ $? -ne 0 ] ; then
 				merr "dispatcher - SQL Error"
 				exit 1
 			fi
464a4d33
 
67252029
 			;;
1cc0144e
 		rmgw|rm)
d2416957
 			require_dbengine
67252029
 			shift
 			if [ $# -ne 1 ] ; then
 				merr "missing gateway id to be removed"
 				exit 1
 			fi
464a4d33
 
67252029
 			QUERY="delete from $DISPATCHER_TABLE where $DISPATCHER_ID_COLUMN='$1';"
 			$DBCMD "$QUERY"
464a4d33
 
67252029
 			if [ $? -ne 0 ] ; then
 				merr "dispatcher - SQL Error"
 				exit 1
 			fi
464a4d33
 
67252029
 			;;
 		reload)
d2416957
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run dispatcher.reload
67252029
 			;;
464a4d33
 
67252029
 		dump)
d2416957
 			require_ctlengine
8a1c5b01
 			ctl_cmd_run dispatcher.list
67252029
 			;;
464a4d33
 
67252029
 		*)
 			usage_dispatcher
 			exit 1
464a4d33
 
67252029
 	esac
464a4d33
 }
 
e271bb60
 #
 ##### ------------------------------------------------ #####
 ### Server management
 #
 ksr_srv() {
 	case $1 in
 		sockets)
 			require_ctlengine
 			mecho "list listen sockets"
8a1c5b01
 			ctl_cmd_run corex.list_sockets
e271bb60
 			;;
 		aliases)
 			require_ctlengine
 			mecho "list server aliases"
8a1c5b01
 			ctl_cmd_run corex.list_aliases
e271bb60
 			;;
 		rpclist)
 			require_ctlengine
 			mecho "list server rpc commands"
8a1c5b01
 			ctl_cmd_run system.listMethods
e271bb60
 			;;
6b5aa70f
 		debug)
 			require_ctlengine
 			mecho "server debug level command"
 			if [ $# -lt 2 ] ; then
 				ctl_cmd_run corex.debug
 			else
 				ctl_cmd_run corex.debug $2
 			fi
 			;;
6f401a7a
 		modules)
 			require_ctlengine
 			mecho "list loaded modules"
 			ctl_cmd_run core.modules
 			;;
e9f24ff3
 		version)
fea4b388
 			require_ctlengine
 			mecho "list server version"
 			ctl_cmd_run core.version
 			;;
e271bb60
 		*)
 			usage_ksr_srv
 			exit 1
 
 	esac
 }
 
 
cb66f431
 #
 ##### ------------------------------------------------ #####
 ### DIALOG management
 #
 dialog() {
 	case $1 in
 		show|list)
 			require_ctlengine
 			mecho "dialog memory records"
8a1c5b01
 			ctl_cmd_run dlg.list
cb66f431
 			;;
 		showdb)
 			require_dbengine
 			mecho "dialog database records"
 			QUERY="select * FROM $DIALOG_TABLE ORDER BY id; "
 			$DBROCMD "$QUERY"
 			;;
 		*)
 			usage_dialog
 			exit 1
 
 	esac
 }
 
f20cde2d
 #
 ##### ------------------------------------------------ #####
 ### DIALPLAN management
 #
 dialplan() {
 	require_dbengine
 	require_ctlengine
 	case $1 in
 		show)
 			shift
 			if [ $# -gt 0 ] ; then
 				mecho "dialplan $1 tables"
 				QUERY="select * FROM $DIALPLAN_TABLE WHERE $DIALPLAN_DPID_COLUMN=$1 ORDER BY $DIALPLAN_PR_COLUMN ; "
 			else
 				mecho "dialplan tables"
 				QUERY="select * FROM $DIALPLAN_TABLE ORDER BY $DIALPLAN_DPID_COLUMN, $DIALPLAN_PR_COLUMN; "
 			fi
 			$DBROCMD "$QUERY"
 			;;
 
 		addrule)
 			shift
860ebc3e
 			if [ $# -lt 4 ] ; then
f20cde2d
 				merr "too few parameters"
 				usage_dialplan
 				exit 1
 			fi
 
 			DIALPLAN_DPID=$1
 			DIALPLAN_PR=$2
 			DIALPLAN_MATCH_OP=$3
 			case $DIALPLAN_MATCH_OP in
 				equal)
 					DIALPLAN_MATCH_OP=0
 					;;
 				regexp)
 					DIALPLAN_MATCH_OP=1
 					;;
860ebc3e
 				fnmatch)
 					DIALPLAN_MATCH_OP=2
 					;;
f20cde2d
 				*)
 					merr "dialplan - unexpected $DIALPLAN_MATCH_OP for operating matching. Use 'equal' or 'regexp'!"
 					exit 1
 			esac
 			DIALPLAN_MATCH_EXP=$4
860ebc3e
 			DIALPLAN_MATCH_LEN=0
 			if [ $# -gt 4 ] ; then
 				DIALPLAN_MATCH_LEN=$5
 			fi
 			if [ $# -gt 5 ] ; then
 				DIALPLAN_SUBST_EXP=$6
 			fi
 			if [ $# -gt 6 ] ; then
 				DIALPLAN_REPL_EXP=$7
 			fi
 			if [ $# -gt 7 ] ; then
 				DIALPLAN_ATTRS=$8
 			fi
f20cde2d
 
 			QUERY="insert into $DIALPLAN_TABLE \
 				( $DIALPLAN_DPID_COLUMN, $DIALPLAN_PR_COLUMN, $DIALPLAN_MATCH_OP_COLUMN, \
 					$DIALPLAN_MATCH_EXP_COLUMN, $DIALPLAN_MATCH_LEN_COLUMN, \
 					$DIALPLAN_SUBST_EXP_COLUMN, $DIALPLAN_REPL_EXP_COLUMN, \
 					$DIALPLAN_ATTRS_COLUMN ) \
 				VALUES ( $DIALPLAN_DPID, $DIALPLAN_PR, $DIALPLAN_MATCH_OP, \
 					'$DIALPLAN_MATCH_EXP', $DIALPLAN_MATCH_LEN, '$DIALPLAN_SUBST_EXP', \
 					'$DIALPLAN_REPL_EXP', '$DIALPLAN_ATTRS')";
860ebc3e
 			mdbg "$QUERY"
f20cde2d
 			$DBCMD "$QUERY"
 
 			if [ $? -ne 0 ] ; then
 				merr "dialplan - SQL Error"
 				exit 1
 			fi
 
860ebc3e
 			minfo "do not forget to do dialplan reload"
f20cde2d
 			;;
 
 		rm)
 			QUERY="delete from $DIALPLAN_TABLE; "
 			$DBCMD "$QUERY"
 
 			if [ $? -ne 0 ] ; then
 				merr "dialplan - SQL Error"
 				exit 1
 			fi
 
860ebc3e
 			minfo "do not forget to do dialplan reload"
f20cde2d
 			;;
 
 		rmdpid)
 			shift
 			if [ $# -lt 1 ] ; then
 				merr "too few parameters"
 				usage_dialplan
 				exit 1
 			fi
 
 			DIALPLAN_DPID=$1
 
 			QUERY="delete from $DIALPLAN_TABLE where $DIALPLAN_DPID_COLUMN=$DIALPLAN_DPID; "
 			$DBCMD "$QUERY"
 
 			if [ $? -ne 0 ] ; then
 				merr "dialplan - SQL Error"
 				exit 1
 			fi
 
860ebc3e
 			minfo "do not forget to do dialplan reload"
f20cde2d
 			;;
 
 		rmrule)
 			shift
 			if [ $# -lt 2 ] ; then
 				merr "too few parameters"
 				usage_dialplan
 				exit 1
 			fi
 
 			DIALPLAN_DPID=$1
 			DIALPLAN_PR=$2
 
 			QUERY="delete from $DIALPLAN_TABLE where $DIALPLAN_DPID_COLUMN=$DIALPLAN_DPID AND $DIALPLAN_PR_COLUMN=$DIALPLAN_PR; "
 			$DBCMD "$QUERY"
 
 			if [ $? -ne 0 ] ; then
 				merr "dialplan - SQL Error"
 				exit 1
 			fi
 
860ebc3e
 			minfo "do not forget to do dialplan reload"
f20cde2d
 			;;
 
 		reload)
8a1c5b01
 			ctl_cmd_run dialplan.reload
f20cde2d
 			;;
 
 		*)
 			usage_dialplan
 			exit 1
 
 	esac
 }
 
7872a0d8
 #
 ##### ------------------------------------------------ #####
e102ae72
 ### kamailio_start
7872a0d8
 #
e102ae72
 kamailio_start() {
7872a0d8
 	echo
860c33c5
 	minfo "Starting Kamailio : "
7872a0d8
 	if [ -r $PID_FILE ] ; then
860c33c5
 		ps axw | $EGREP kamailio
b504dc50
 		ls -l $PID_FILE
860c33c5
 		minfo "PID file exists ($PID_FILE)! Kamailio already running?"
7872a0d8
 		exit 1
 	fi
a255cfce
 
d4adb838
 	if [ ! -x "$KAMBIN" ] ; then
7872a0d8
 		echo
d4adb838
 		merr "Kamailio binaries not found at $KAMBIN"
 		merr "set KAMBIN to the path of kamailio in $0 or ~/.kamctlrc"
7872a0d8
 		exit 1
 	fi
 	if [ $SYSLOG = 1 ] ; then
d4adb838
 		$KAMBIN -P $PID_FILE  -f $ETCDIR/kamailio.cfg $STARTOPTIONS 1>/dev/null 2>/dev/null
7872a0d8
 	else
d4adb838
 		$KAMBIN -P $PID_FILE -E  -f $ETCDIR/kamailio.cfg $STARTOPTIONS
7872a0d8
 	fi
 	sleep 3
 	if [ ! -s $PID_FILE ] ; then
 		echo
860c33c5
 		merr "PID file $PID_FILE does not exist -- Kamailio start failed"
7872a0d8
 		exit 1
 	fi
 	minfo "started (pid: `cat $PID_FILE`)"
 }
 
 #
 ##### ------------------------------------------------ #####
e102ae72
 ### kamailio_stop
7872a0d8
 #
e102ae72
 kamailio_stop() {
7872a0d8
 	echo
860c33c5
 	minfo "Stopping Kamailio : "
7872a0d8
 	if [ -r $PID_FILE ] ; then
 		kill `cat $PID_FILE`
 		minfo "stopped"
 	else
 		echo
860c33c5
 		merr "No PID file found ($PID_FILE)! Kamailio probably not running"
 		minfo "check with 'ps axw | $EGREP kamailio'"
7872a0d8
 		exit 1
 	fi
 }
 
 #
 ##### ------------------------------------------------ #####
 ### options_ping
 #
 options_ping() {
 	myhost=`get_my_host`
b5d40c77
 	require_ctlengine
47a98532
 	CMDPARAMS="= tm.t_uac_wait_block OPTIONS \"$1\" \".\" \".\" \"From:sip:daemon@$myhost=CRLF=To:<$1>=CRLF=Contact:sip:daemon@$myhost=CRLF=\""
4a4f0475
 	ctl_cmd_run $CMDPARAMS
7872a0d8
 }
 
 #
 ##### ------------------------------------------------ #####
 ### rpid management
 #
 rpid() {
 	if [ "$#" -lt 2 ] ; then
 		merr "rpid - too few parameters"
 		exit 1
 	fi
67252029
 	shift;
7872a0d8
 	require_dbengine
 	case $1 in
 		show)
 			if [ $# -eq 2 ] ; then
 				set_user $2
a255cfce
 				is_user $2
7872a0d8
 				if [ $? -ne 0 ] ; then
 					merr "rpid - invalid user '$2'"
 					exit 1;
 				fi
8a92d1fe
 				CLAUSE=" WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND \
 $REALM_COLUMN='$OSERDOMAIN' "
7872a0d8
 			elif [ $# -ne 1 ] ; then
 				usage_rpid
 				exit 1
 			fi
 			QUERY="select $SUBSCRIBER_COLUMN, $RPID_COLUMN FROM $SUB_TABLE \
 $CLAUSE ; "
 			$DBROCMD "$QUERY"
 			;;
 
 		add|rm)
a255cfce
 			MODE=$1;
7872a0d8
 
 			if [ "$MODE" = "add" ] ; then
a255cfce
 				ARG_NUM=3;
7872a0d8
 			else
a255cfce
 				ARG_NUM=2;
7872a0d8
 			fi
a255cfce
 
7872a0d8
 			if [ $# -lt $ARG_NUM ] ; then
 				usage_rpid
 				exit 1
 			fi
 
 			set_user $2
a255cfce
 			is_user $2
7872a0d8
 			if [ $? -ne 0 ] ; then
 				merr "rpid - invalid user '$2'"
 				exit 1
 			fi
 			shift 2
 
 			if [ "$MODE" = "add" ] ; then
a255cfce
 				RPID_VAL="'$1'";
7872a0d8
 			else
a255cfce
 				RPID_VAL=NULL;
7872a0d8
 			fi
 
 			QUERY="UPDATE $SUB_TABLE SET $RPID_COLUMN=$RPID_VAL \
8a92d1fe
 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND $REALM_COLUMN='$OSERDOMAIN';"
7872a0d8
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "rpid - SQL Error"
 				exit 1
 			fi
 
8a92d1fe
 			$0 rpid show "$OSERUSER@$OSERDOMAIN"
7872a0d8
 
 			;;
 
 		*)
 			usage_rpid
 			exit 1
 			;;
 	esac
 }
 
 #
 ##### ------------------------------------------------ #####
 ### SPEEDDIAL management
 #
 speeddial() {
 	if [ "$#" -lt 2 ] ; then
 		merr "speeddial - too few parameters"
 		echo
 		usage_speeddial
 		exit 1
 	fi
 
 	require_dbengine
 	shift
 
a255cfce
 	case $1 in
7872a0d8
 		list)
 			if [ $# -eq 2 ] ; then
 				# print speed-dials for user
 				check_aor "$2"
 				if [ "$?" -ne "0" ] ; then
 					merr "speeddial - <$2> is not a valid AoR (user@domain)"
 					exit 1
 				fi
a255cfce
 
7872a0d8
 				set_user $2
a255cfce
 
7872a0d8
 				CLAUSE="WHERE $SD_USER_COLUMN='$OSERUSER' AND \
 $SD_DOMAIN_COLUMN='$OSERDOMAIN'"
 				mecho "Dumping speed-dials for user=<$2>"
 				echo
 				QUERY="SELECT CONCAT($SD_SD_USER_COLUMN,'@',\
 $SD_SD_DOMAIN_COLUMN) AS 'Short number', $SD_NEW_URI_COLUMN AS 'New URI',\
 $SD_DESC_COLUMN FROM $SD_TABLE $CLAUSE;"
 				$DBROCMD "$QUERY"
 					#| $AWK 'BEGIN {line=0;}
 					#	/^\+/ { next }
 		#{ if(line==0) print "##   SpeedDial   \tNew-URI     \tDescription\n";
 					#	else {
 					#		ORS_BAK=ORS;
 					#		ORS="";
 					#		print line ")  " $1 "@" $2 "\t" $3 "\t\"" $4;
 					#		for (i=5;i<=NF;++i) print FS $i;
 					#		ORS=ORS_BAK;
 					#		print "\"";
 					#	}
 					#	line++;
 					#}'
 			elif [ $# -eq 1 ] ; then
7e4aadcc
 				mecho "Dumping all speed-dials may take long: do you want to proceed? [Y|N] "
7872a0d8
 				read answer
 				if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
7e4aadcc
 					mecho "Dumping all speed-dials..."
7872a0d8
 					echo
 				else
 					exit 1
 				fi
 				QUERY="SELECT CONCAT($SD_SD_USER_COLUMN,'@',\
 $SD_SD_DOMAIN_COLUMN) AS 'Short number', CONCAT($SD_USER_COLUMN,'@',\
 $SD_DOMAIN_COLUMN) AS 'Owner', $SD_NEW_URI_COLUMN AS 'New URI',\
 $SD_DESC_COLUMN FROM $SD_TABLE;"
 				$DBROCMD "$QUERY"
 				#| $AWK 'BEGIN {line=0;}
 				#	/^\+/ { next }
 				#	{	line++;
 	#if(line==1) print "SIP-ID     \tSpeedDial  \tNew-URI    \tDescritpion\n";
 				#		else {
 				#			ORS_BAK=ORS;
 				#			ORS="";
 				#			print $3 "@" $4 "\t" $1 "@" $2 "\t" $5 "\t\"" $6;
 				#			for (i=7;i<=NF;++i) print FS $i;
 				#			ORS=ORS_BAK;
 				#			print "\"";
 				#		}
 				#	}'
 			else
 				merr "speeddial - wrong number of params for command [list]"
 				usage_speeddial
 				exit 1
 			fi
 
 			exit $?
 			;;
 		show)
 			if [ $# -ne 2 ] ; then
 				merr "speeddial - wrong number of params for command [show]"
 				usage_speeddial
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			check_aor "$2"
 			if [ "$?" -ne "0" ] ; then
 				merr "speeddial - $2 is not a valid AoR (user@domain)"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			set_user $2
a255cfce
 
7872a0d8
 			CLAUSE="WHERE $SD_SD_USER_COLUMN='$OSERUSER' AND \
 $SD_SD_DOMAIN_COLUMN='$OSERDOMAIN'"
 			QUERY="SELECT CONCAT($SD_USER_COLUMN,'@',$SD_DOMAIN_COLUMN) \
 AS 'Owner', $SD_NEW_URI_COLUMN AS 'New URI', $SD_DESC_COLUMN FROM \
 $SD_TABLE $CLAUSE ; "
7e4aadcc
 			mecho "Details for speeddial <$2>"
7872a0d8
 			$DBROCMD "$QUERY"
a255cfce
 			# | $AWK 'BEGIN {line=0;} /^\+/ { next }
 			# {
7872a0d8
 			#	  if(line==0) print "##  SIP-ID    \tNew-URI   \tDescritpion\n";
 			#	  else {
e102ae72
 			#		  ORS_BAK=ORS;usage_kamailio_monitor() {
7872a0d8
 			#		  ORS="";
 			#		  print line ") " $1 "@" $2 "\t" $3 "\t\"" $4;
 			#		  for (i=5;i<=NF;++i) print FS $i;
 			#		  ORS=ORS_BAK;
 			#		  print "\"";
 			#	  }
 			#	  line++;
 			# }'
 
 			exit $?
 			;;
 		add)
 			if [ $# -ne 4 ] ; then
 				if [ $# -ne 5 ] ; then
 					merr "speeddial - wrong number of parameters"
 					usage_speeddial
 					exit 1
 				fi
 			fi
 			shift
 			check_aor "$1"
 			if [ "$?" -ne "0" ] ; then
 				merr "speeddial - $1 is not a valid AoR (user@domain)"
 				exit 1
 			fi
 
 			check_aor "$2"
 			if [ "$?" -ne "0" ] ; then
 				merr "speeddial - $2 is not a valid AoR (user@domain)"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			check_sipaor "$3"
 			if [ "$?" -ne "0" ] ; then
 				merr "speeddial - $3 is not a valid SIP AoR (sip:user@domain)"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			set_user $1
 			TMP_OSERUSER=$OSERUSER
 			TMP_OSERDOMAIN=$OSERDOMAIN
 			set_user $2
a255cfce
 
7872a0d8
 			QUERY="INSERT INTO $SD_TABLE ($SD_USER_COLUMN,$SD_DOMAIN_COLUMN,\
 $SD_SD_USER_COLUMN,$SD_SD_DOMAIN_COLUMN,$SD_NEW_URI_COLUMN,$SD_DESC_COLUMN) \
 VALUES ('$TMP_OSERUSER','$TMP_OSERDOMAIN','$OSERUSER','$OSERDOMAIN','$3','$4');"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "speeddial - SQL Error"
 				exit 1
 			fi
 			mecho "ok - spedd dial added"
 			echo
 			exit $?
 			;;
 		rm)
 			if [ $# -ne 3 ] ; then
 				merr "speeddial rm - invalid number of parameters"
 				usage_speeddial
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			shift
a255cfce
 
7872a0d8
 			check_aor "$1"
 			if [ "$?" -ne "0" ] ; then
 				merr "speeddial - $1 is not a valid AoR (user@domain)"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			check_aor "$2"
 			if [ "$?" -ne "0" ] ; then
 				merr "speeddial - $2 is not a valid AoR (user@domain)"
 				exit 1
 			fi
 
 			set_user $1
 			TMP_OSERUSER=$OSERUSER
 			TMP_OSERDOMAIN=$OSERDOMAIN
 			set_user $2
 
 			CLAUSE="WHERE $SD_USER_COLUMN='$TMP_OSERUSER' AND \
 $SD_DOMAIN_COLUMN='$TMP_OSERDOMAIN' AND $SD_SD_USER_COLUMN='$OSERUSER' AND \
 $SD_SD_DOMAIN_COLUMN='$OSERDOMAIN'"
 			QUERY="DELETE FROM $SD_TABLE $CLAUSE;"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "speeddial - SQL Error"
 				exit 1
 			fi
a255cfce
 
7872a0d8
 			mecho "ok - spedd dial deleted"
 			echo
 
 			;;
a255cfce
 
7872a0d8
 		help)
 			usage_speeddial
 			;;
a255cfce
 
7872a0d8
 		*)
 			merr "speeddial - unknown command"
 			usage_speeddial
 			exit 1
 			;;
 	esac
 } # end speed_dial()
 
e2784b5e
 #
 ##### ------------------------------------------------ #####
 ### acc management
 #
 acc() {
 	if [ "$#" -lt 2 ] ; then
 		merr "acc - too few parameters"
 		usage_acc
 		exit 1
 	fi
 	shift;
 	require_dbengine
 	case $1 in
2ddc54ba
 		initdb)
 			QUERY=$(cat <<-END
 ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
 ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
 ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default '';
 ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
 ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
 ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
 ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT '';
 ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT '';
 ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default '';
 ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT '';
 ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT '';
 ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT '';
 END
 )
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "error - acc db init"
 				exit 1
 			fi
 
 			mecho "ok - acc db init"
 
 			;;
 
e2784b5e
 		showdb)
 			QUERY="select * FROM $ACC_TABLE ;"
 			$DBROCMD "$QUERY"
 			;;
 
 		recent)
 			TIMENOW=`date +"%s"`
 			if [ $# -eq 2 ] ; then
 				CLAUSE=" WHERE time>=$TIMENOW - $2 "
 			else
 				CLAUSE=" WHERE time>=$TIMENOW - 300 "
 			fi
 			QUERY="select * FROM $ACC_TABLE $CLAUSE ; "
 			$DBROCMD "$QUERY"
 			;;
 
 
 		*)
 			usage_acc
 			exit 1
 			;;
 	esac
 } # end acc()
 
 
7872a0d8
 #
 ##### ================================================ #####
 ### subscriber management
 #
 subscriber() {
 	if [ "$#" -lt 2 ] ; then
 		merr "too few parameters"
 		usage_subscriber
 		exit 1
 	fi
 
 	require_dbengine
a255cfce
 
7872a0d8
 	case $1 in
 		add)
b91e34bf
 			if [ $# -ne 3 ] ; then
7872a0d8
 				usage_subscriber
 				exit 1
 			fi
 			shift
 			credentials $1 $2
 			is_user $1
 			if [ $? -eq 0 ] ; then
67252029
 				minfo "user '$1' already exists"
7872a0d8
 				exit 1
 			fi
 			set_user $1
 			check_alias $OSERUSER $OSERDOMAIN
09a0b14f
 			if [ "$ALIAS_EXISTS" = "1" ] ; then
67252029
 				minfo "user '$1' already exists as alias"
7872a0d8
 				exit 1
 			fi
 
3ab6cf78
 			if [ "$STORE_PLAINTEXT_PW" = "1" ] ; then
 				PASS="$2"
 			else
 				PASS=""
 			fi
 
c5b32b32
 			QUERY="insert into $SUB_TABLE ($SUBSCRIBER_COLUMN,\
b91e34bf
 				$REALM_COLUMN,$HA1_COLUMN,$HA1B_COLUMN,$PASSWORD_COLUMN) \
 				values ('$OSERUSER','$OSERDOMAIN','$HA1','$HA1B','$PASS');";
7872a0d8
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "introducing the new user '$1' to the database failed"
 			else
 				mecho "new user '$1' added"
 			fi
 			;;
 
da016636
 		show)
 			if [ $# -ne 2 ] ; then
 				usage_subscriber
 				exit 1
 			fi
 			shift
 
 			set_user $1
 
 			case $DBENGINE in
 				MYSQL|mysql|MySQL)
 					QUERY="SELECT * FROM $SUB_TABLE \
 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND $REALM_COLUMN='$OSERDOMAIN'\G"
 				;;
 				*)
 					QUERY="SELECT * FROM $SUB_TABLE \
 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' AND $REALM_COLUMN='$OSERDOMAIN';"
 				;;
 			esac
 			$DBROCMD "$QUERY"
 			;;
 
7872a0d8
 		passwd)
 			if [ $# -ne 3 ] ; then
 				usage_subscriber
 				exit 1
 			fi
 			shift
 			credentials $1 $2
 
 			is_user $1
 			if [ $? -ne 0 ] ; then
 				merr "non-existent user '$1'"
 				exit 1
 			fi
3ab6cf78
 
 			if [ "$STORE_PLAINTEXT_PW" = "1" ] ; then
 				PASS="$2"
 			else
 				PASS=""
 			fi
 
7872a0d8
 			QUERY="update $SUB_TABLE set $HA1_COLUMN='$HA1', \
0be920b0
 $HA1B_COLUMN='$HA1B', $PASSWORD_COLUMN='$PASS' \
7872a0d8
 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "password change failed"
 			else
7e4aadcc
 				minfo "password change succeeded"
7872a0d8
 			fi
 			;;
 
 		rm)
 			if [ $# -ne 2 ] ; then
 				usage_subscriber
 				exit 1
 			fi
 
 			require_ctlengine
a255cfce
 			shift
7872a0d8
 
a255cfce
 			is_user $1
7872a0d8
 			if [ $? -ne 0 ] ; then
 				merr "non-existent user '$1'"
 				exit 1
 			fi
 
 			# begin with remove all user's privileges
 			acl revoke $1  > /dev/null 2>&1
 
 			# destroy db-aliases
 			QUERY="delete from $DA_TABLE where $DA_USER_COLUMN='$OSERUSER' \
0a11e4d3
 and $DA_DOMAIN_COLUMN='$OSERDOMAIN';"
7872a0d8
 			$DBCMD "$QUERY"
 
 
 			# destroy the user now
 			QUERY="delete from $SUB_TABLE where $SUBSCRIBER_COLUMN='$OSERUSER' \
0a11e4d3
 and $REALM_COLUMN='$OSERDOMAIN';"
7872a0d8
 			$DBCMD "$QUERY"
 
 			# and also all his contacts
 			$0 ul rm $1   > /dev/null 2>&1
 			;;
1cec15e0
 		sets)
 			if [ $# -ne 4 ] ; then
 				usage_subscriber
 				exit 1
 			fi
 			shift
 
 			is_user $1
 			if [ $? -ne 0 ] ; then
 				merr "non-existent user '$1'"
 				exit 1
 			fi
 
 			QUERY="update $SUB_TABLE set $2='$3' \
 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "attribute change failed"
 			else
 				minfo "attribute change succeeded"
 			fi
 			;;
 		setn)
 			if [ $# -ne 4 ] ; then
 				usage_subscriber
 				exit 1
 			fi
 			shift
 
 			is_user $1
 			if [ $? -ne 0 ] ; then
 				merr "non-existent user '$1'"
 				exit 1
 			fi
 
 			QUERY="update $SUB_TABLE set $2=$3 \
 WHERE $SUBSCRIBER_COLUMN='$OSERUSER' and $REALM_COLUMN='$OSERDOMAIN';"
 			$DBCMD "$QUERY"
 			if [ $? -ne 0 ] ; then
 				merr "attribute change failed"
 			else
 				minfo "attribute change succeeded"
 			fi
 			;;
 
7872a0d8
 	esac
 
 }
 
 #
 ##### ================================================ #####
 ### USRLOC management
 #
 usrloc() {
 	if [ "$#" -lt 2 ] ; then
 		merr "usrloc - too few parameters"
 		usage_usrloc
 		exit 1
 	fi
 
 	require_ctlengine
 
 	if [ "$1" = "alias" ] ; then
 		USRLOC_TABLE="$ALS_TABLE"
56778b8f
 		if [ -z "$USRLOC_TABLE" ] ; then
 			USRLOC_TABLE=aliases
 		fi
7872a0d8
 		CHECK_SUB=1
 	elif [ "$1" = "ul" ] ; then
 		USRLOC_TABLE="$UL_TABLE"
56778b8f
 		if [ -z "$USRLOC_TABLE" ] ; then
 			USRLOC_TABLE=location
 		fi
7872a0d8
 		CHECK_SUB=0
 	elif [ "$1" = "usrloc" ] ; then
 		USRLOC_TABLE="$UL_TABLE"
56778b8f
 		if [ -z "$USRLOC_TABLE" ] ; then
 			USRLOC_TABLE=location
 		fi
7872a0d8
 		CHECK_SUB=0
 	else
 		merr "usrloc - unknown subcommand '$1'"
 		usage_usrloc
 		exit 1
 	fi
 	shift
 
a255cfce
 	case $1 in
7872a0d8
 		show)
 			if [ $# -eq 2 ] ; then
32b0e90a
 				if [ "$2" = "--brief" ] ; then
8a1c5b01
 					ctl_cmd_run ul.dump brief
32b0e90a
 				else
 					set_user $2
8a1c5b01
 					ctl_cmd_run ul.lookup \
32b0e90a
 						$USRLOC_TABLE "$OSERUSER@$OSERDOMAIN"
7872a0d8
 				fi
32b0e90a
 			elif [ $# -eq 1 ] ; then
8a1c5b01
 				ctl_cmd_run ul.dump
7872a0d8
 			else
 				merr "wrong number of params"
 				usage_usrloc
 				exit 1
 			fi
 			exit $?
 			;;
 		add)
 			if [ $# -eq 3 ] ; then
74a9582c
 				# expires 0 means persistent contact
7872a0d8
 				UL_EXPIRES=0
74a9582c
 				UL_FLAGS=0
e073d46a
 				BR_FLAGS=0
301525df
 				UL_PATH="."
7872a0d8
 			elif [ $# -eq 4 ] ; then
 				UL_EXPIRES=$4
 				UL_FLAGS=0
e073d46a
 				BR_FLAGS=0
301525df
 				UL_PATH="."
a4fb559b
 			elif [ $# -eq 5 ] ; then
 				UL_EXPIRES=$4
 				UL_FLAGS=0
 				BR_FLAGS=0
 				UL_PATH="$5"
7872a0d8
 			else
 				usage_usrloc
 				exit 1
 			fi
 			shift
 			check_uri "$2"
a255cfce
 
7872a0d8
 			if [ "$?" -ne "0" ] ; then
 				merr "$2 is not a valid URI"
 				exit 1
 			fi
 
 			set_user $1
 			if [ "$CHECK_SUB" -ne 0 ] ; then
a255cfce
 				is_user $1
7872a0d8
 				if [ $? -eq 0 ] ; then
 					merr "overlap of alias with an existing subscriber name"
 					exit 1;
 				fi
 			fi
 
 			check_alias $OSERUSER $OSERDOMAIN
09a0b14f
 			if [ "$ALIAS_EXISTS" = "1" ] ; then
7872a0d8
 				if [ "$CHECK_SUB" -ne 0 ] ; then
67252029
 					minfo "alias already defined"
7872a0d8
 				else
 					merr "AOR is an alias"
 				fi
 				exit 1
 			fi
 
1735feed
 			if [ -z "$DEFAULT_Q" ] ; then
513a6d8b
 				DEFAULT_Q="1.0"
1735feed
 			fi
 
8a1c5b01
 			ctl_cmd_run ul.add "$USRLOC_TABLE" "$OSERUSER@$OSERDOMAIN" "$2" \
8fac19ba
 "$UL_EXPIRES" "$DEFAULT_Q" "$UL_PATH" "$UL_FLAGS" "$BR_FLAGS" "$ALL_METHODS"
7872a0d8
 			exit $?
 			;;
 		rm)
 			if [ $# -eq 2 ] ; then
 				shift
 				set_user $1
8a1c5b01
 				ctl_cmd_run ul.rm $USRLOC_TABLE "$OSERUSER@$OSERDOMAIN"
7872a0d8
 
 			elif [ $# -eq 3 ] ; then
 				shift
 				set_user $1
 				check_uri "$2"
 				if [ "$?" -ne "0" ] ; then
 					merr "$2 is not a valid SIP URI (sip:[user@]domain)"
 					exit 1
 				fi
 
8a1c5b01
 				ctl_cmd_run ul.rm_contact $USRLOC_TABLE "$OSERUSER@$OSERDOMAIN" "$2"
7872a0d8
 
 			else
 				merr "wrong number of params"
 				usage_usrloc
 				exit 1
 			fi
 			;;
 
955e8a54
 		dbclean)
 			require_dbengine
 			KSR_CLEAN_VAL=3600
 			if [ $# -eq 2 ] ; then
 				KSR_CLEAN_VAL=$2
 			fi
 			QUERY="delete from $USRLOC_TABLE where expires < SUBDATE(NOW(), INTERVAL $KSR_CLEAN_VAL SECOND);"
 			$DBCMD "$QUERY"
 
 			exit $?
 			;;
 
7872a0d8
 		*)
 			usage_usrloc
 			exit 1
 			;;
 	esac
 }
 
2b7714a2
 ##### ================================================ #####
 ### TLS CA management
 #
 
 tls_ca() {
 
102d5dab
 	case $1 in
 		"rootCA")
 			if [ -z $2 ] ; then
 				# use default
 				CA_BASE=$ETCDIR/tls
 			else
 				CA_BASE=`(cd $2;pwd)`
 			fi
2b7714a2
 
102d5dab
 			if [ ! -d $CA_BASE ] ; then
 				merr "Config directory ($CA_BASE) does not exist"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			CA_CONF='ca.conf'
 			CA_PATH=$CA_BASE/rootCA
 			if [ ! -f $CA_BASE/$CA_CONF  ] ; then
 				merr "root CA config file ($CA_BASE/$CA_CONF) does not exist"
 				exit 1
 			fi
 
 			if [ -d $CA_PATH ] ; then
 				mwarn "root CA directory ($CA_PATH) exists! Remove it (y/n)?"
 				read X
 				if [ "$X" != "y" -a "$X" != "Y" ] ; then
 					exit 1
 				fi
 			fi
2b7714a2
 
102d5dab
 			mecho "Creating directory $CA_PATH and its sub-tree"
 			mkdir -p $CA_PATH
 			if [ $? -ne 0 ] ; then
 				merr "Failed to create root directory $CA_PATH"
2b7714a2
 				exit 1
 			fi
102d5dab
 			rm -fr "${CA_PATH:?}"/*
 			mkdir $CA_PATH/private
 			mkdir $CA_PATH/certs
 			touch $CA_PATH/index.txt
 			echo 01 >$CA_PATH/serial
2b7714a2
 
102d5dab
 			mecho "Creating CA self-signed certificate"
 			( cd $CA_PATH; openssl req -config $CA_BASE/$CA_CONF -x509 -newkey \
 				rsa:2048 -days 365 -out ./cacert.pem -outform PEM )
 			if [ $? -ne 0 ] ; then
 				merr "Failed to create self-signed certificate"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			mecho "Protecting CA private key"
 			chmod 600 $CA_PATH/private/cakey.pem
2b7714a2
 
102d5dab
 			mecho "DONE"
 			minfo "Private key can be found in $CA_PATH/private/cakey.pem"
 			minfo "Certificate can be found in $CA_PATH/cacert.pem"
 			;;
2b7714a2
 
102d5dab
 		"userCERT")
 			if [ -z $2 ] ; then
 				merr "Missing user name parameter"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			if [ -z $3 ] ; then
 				# use default
 				CA_BASE=$ETCDIR/tls
 			else
 				CA_BASE=`(cd $3;pwd)`
 			fi
2b7714a2
 
102d5dab
 			if [ ! -d $CA_BASE ] ; then
 				merr "Config directory ($CA_BASE) does not exist"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			USER_DIR=$CA_BASE/$2
 			USER_CFG=$CA_BASE/$2.conf
 			USER=$2
 			REQ_CFG=$CA_BASE/request.conf
2b7714a2
 
102d5dab
 			if [ ! -f $USER_CFG ] ; then
 				merr "User config file $USER_CFG not found"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			if [ ! -f $REQ_CFG ] ; then
 				merr "Request config file $REQ_CFG not found"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			mecho "Using config file $USER_CFG"
2b7714a2
 
102d5dab
 			if [ -d $USER_DIR ] ; then
 				mwarn "User CERT directory ($USER_DIR) exists! Remove it (y/n)?"
 				read X
 				if [ "$X" != "y" -a "$X" != "Y" ] ; then
 					exit 1
 				fi
 			fi
2b7714a2
 
102d5dab
 			mecho "Creating directory $USER_DIR"
 			mkdir -p $USER_DIR
 			if [ $? -ne 0 ] ; then
 				merr "Failed to create user directory $USER_DIR "
2b7714a2
 				exit 1
 			fi
102d5dab
 			rm -fr "${USER_DIR:?}"/*
2b7714a2
 
102d5dab
 			mecho "Creating user certificate request"
 			openssl req  -config $USER_CFG -out $USER_DIR/$USER-cert_req.pem \
 				-keyout $USER_DIR/$USER-privkey.pem -new -nodes
 			if [ $? -ne 0 ] ; then
 				merr "Failed to generate certificate request"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			mecho "Signing certificate request"
 			( cd $CA_BASE ; openssl ca -config $REQ_CFG -in \
 				$USER_DIR/$USER-cert_req.pem -out $USER_DIR/$USER-cert.pem )
 			if [ $? -ne 0 ] ; then
 				merr "Failed to generate certificate request"
 				exit 1
 			fi
2b7714a2
 
102d5dab
 			mecho "Generating CA list"
 			cat $CA_BASE/rootCA/cacert.pem >> $USER_DIR/$USER-calist.pem
2b7714a2
 
102d5dab
 			mecho "DONE"
 			minfo "Private key is locate at $USER_DIR/$USER-privkey.pem "
 			minfo "Certificate is locate at $USER_DIR/$USER-cert.pem "
 			minfo "CA-List is locate at $USER_DIR/$USER-calist.pem "
 			;;
2b7714a2
 
102d5dab
 		"gen-certs")
 			mecho "Generating self signed certificates"
 			if [ -z "$CERTDAYS" ] ; then
 				CERTDAYS=365
 			fi
 			if [ -n "$2" ] ; then
 				openssl req -x509 -newkey rsa:4096 -nodes -subj "/CN=${2}" -keyout kamailio-selfsigned.key -out kamailio-selfsigned.pem -days ${CERTDAYS}
 			else
 				openssl req -x509 -newkey rsa:4096 -nodes -keyout kamailio-selfsigned.key -out kamailio-selfsigned.pem -days ${CERTDAYS}
 			fi
 			;;
2b7714a2
 
102d5dab
 		*)
 			merr "unknown TLS command $1"
 			usage_tls
 			;;
 	esac
2b7714a2
 }
 
ea772b35
 extcmd() {
 	if [ -f $ETCDIR/kamctl.${1}.ext ]; then
 		. $ETCDIR/kamctl.${1}.ext
 	else
 		if [ -f ~/.kamctl/kamctl.${1}.ext ]; then
 			. ~/.kamctl/kamctl.${1}.ext
 		else
 			return
 		fi
 	fi
 
 	XCMD=cmd_${1}
 
 	shift
 	$XCMD "$@"
 
 	exit 1
 }
 
71380228
 ##### ================================================ #####
97ec8555
 ### trap with gdb kamailio processes using RPC core.psx
71380228
 #
 
 kamailio_trap() {
8b023bba
 	if [ -z "$GDB" ] ; then
 		merr "'gdb' tool not found: set GDB variable to correct tool path"
 		exit
 	fi
71380228
 	DATE=`/bin/date +%Y%m%d_%H%M%S`
41841dfc
 	LOG_FILE=/tmp/gdb_kamailio_${DATE}.txt
71380228
 	minfo "Trap file: $LOG_FILE"
4349c4f9
 	ctl_cmd_run core.psx > $LOG_FILE
71380228
 	echo -n "Trapping Kamailio with gdb: "
4349c4f9
 	PID_TIMESTAMP_VECTOR=`$EGREP 'PID' $LOG_FILE | $EGREP -Eo '[0-9]+'`
71380228
 	for pid in $PID_TIMESTAMP_VECTOR
 	do
 		echo -n "."
 		PID=`echo $pid | cut -d '-' -f 1`
 		echo "" >> $LOG_FILE
 		echo "---start $PID -----------------------------------------------------" >> $LOG_FILE
4349c4f9
 		$GDB kamailio $PID -batch --eval-command="bt full" >> $LOG_FILE 2>&1
71380228
 		echo "---end $PID -------------------------------------------------------" >> $LOG_FILE
 	done
 	echo "."
 }
 
97ec8555
 ##### ================================================ #####
 ### trap with gdb kamailio processes using ps command
 #
 
 kamailio_pstrap() {
 	if [ -z "$GDB" ] ; then
 		merr "'gdb' tool not found: set GDB variable to correct tool path"
 		exit
 	fi
 	DATE=`/bin/date +%Y%m%d_%H%M%S`
 	LOG_FILE=/tmp/gdb_kamailio_${DATE}.txt
 	minfo "Trap file: $LOG_FILE"
626c0dee
 	ps axw | grep kamailio | grep -v grep | grep -v kamctl | sort > $LOG_FILE
97ec8555
 	echo "" >> $LOG_FILE
 	echo "" >> $LOG_FILE
 	echo -n "Trapping Kamailio with gdb: "
626c0dee
 	PID_TIMESTAMP_VECTOR=`ps axw | grep kamailio | grep -v grep | grep -v kamctl | sort | awk '{print $1}'`
97ec8555
 	for pid in $PID_TIMESTAMP_VECTOR
 	do
 		echo -n "."
 		PID=`echo $pid | cut -d '-' -f 1`
 		echo "" >> $LOG_FILE
 		echo "---start $PID -----------------------------------------------------" >> $LOG_FILE
904a1590
 		$GDB kamailio $PID -batch --eval-command="p process_no" --eval-command="p pt[process_no]" --eval-command="bt full" >> $LOG_FILE 2>&1
97ec8555
 		echo "---end $PID -------------------------------------------------------" >> $LOG_FILE
 	done
 	echo "."
 }
 
7872a0d8
 #
 ##### ================================================ #####
 ### main command switch
 #
 case $1 in
 	acl)
 		shift
 		acl "$@"
 		;;
 
7e4aadcc
 	add)
 		subscriber "$@"
 		;;
 
da016636
 	show)
 		subscriber "$@"
 		;;
 
7e4aadcc
 	passwd)
 		subscriber "$@"
 		;;
 
 	rm)
7872a0d8
 		subscriber "$@"
 		;;
 
1cec15e0
 	sets)
 		subscriber "$@"
 		;;
 
 	setn)
 		subscriber "$@"
 		;;
 
7872a0d8
 	alias|ul|usrloc)
 		usrloc "$@"
 		;;
 
 	alias_db|aliasdb)
 		alias_db "$@"
 		;;
a255cfce
 
7872a0d8
 	avp)
 		avpops "$@"
 		;;
 
 	cisco_restart)
a255cfce
 		if [ "$#" -ne 2 ] ; then
7872a0d8
 			usage_cisco_restart
 			exit 1
 		fi
 		cisco_restart $2
 		;;
 
 	db)
 		shift
 		db_ops "$@"
7e4aadcc
 		;;
 
 	showdb|userdb)
 		usage
 		exit 1
7872a0d8
 		;;
 
 	domain)
 		shift
 		domain "$@"
 		;;
 
bc507d28
 	uid_domain)
 		shift
 		uid_domain "$@"
 		;;
 
67252029
 	trusted)
 		shift
7b976204
 		permissions_trusted "$@"
 		;;
 
 	address)
 		shift
 		permissions_address "$@"
67252029
 		;;
 
0bd3c9ea
 	mtree)
 		shift
 		mtree_management "$@"
 		;;
 
85030842
 	rpc)
7872a0d8
 		require_ctlengine
 		shift
 		$CTLCMD "$@"
 		;;
 
85030842
 	rpcprint)
33fa1f11
 		require_ctlengine
 		shift
 		$CTLCMDPRINT "$@"
 		;;
 
3eeaa5af
 	ser|sercmd|kamcmd)
9bacc95d
 		require_kamcmd
833632a0
 		shift
 		$SERCTLCMD "$@"
 		;;
 
7872a0d8
 	lcr)
 		shift
 		lcr "$@"
 		;;
 
585f2ed1
 	cr)
 		shift
 		cr "$@"
 		;;
 
464a4d33
 	dispatcher)
 		shift
 		dispatcher "$@"
 		;;
 
cb66f431
 	dialog)
 		shift
 		dialog "$@"
 		;;
 
f20cde2d
 	dialplan)
 		shift
 		dialplan "$@"
 		;;
 
7872a0d8
 	monitor|console|moni|con)
 		require_ctlengine
e102ae72
 		$KAMAILIO_MONITOR "$@"
7872a0d8
 		;;
 
 	online)
 		require_ctlengine
8a1c5b01
 		ctl_cmd_run ul.dump brief | $EGREP -i aor | $EGREP -v AoRs \
ca036d85
 			| awk '{print $2}' | sed 's/"//g' | sort | sort -mu
7872a0d8
 		exit $?
 		;;
 
 	ping)
 		# error handling is hacked -- filter_fl should not
 		# consume positive status -- that should be done by
 		# calling app
43dc4543
 		if [ "$#" -ne 2 ] ; then
7872a0d8
 			usage_ping
 			exit 1
 		fi
 		options_ping $2
 		;;
 
 	ps)
 		require_ctlengine
8a1c5b01
 		ctl_cmd_run core.psx
7872a0d8
 		;;
 
0a5cba42
 	psa)
 		require_ctlengine
 		ctl_cmd_run core.psa
 		;;
 
65247ede
 	uptime)
 		require_ctlengine
8a1c5b01
 		ctl_cmd_run core.uptime
65247ede
 		;;
 
 	stats)
 		require_ctlengine
3bcff044
 		if [ "$#" -eq 1 ] ; then
8a1c5b01
 			ctl_cmd_run stats.get_statistics all
3bcff044
 		else
8a1c5b01
 			ctl_cmd_run stats.get_statistics "${2}:"
3bcff044
 		fi
65247ede
 		;;
 
e271bb60
 	srv)
 		shift
 		ksr_srv "$@"
 		;;
 
7872a0d8
 	restart)
e102ae72
 		kamailio_stop
7872a0d8
 		sleep 2
e102ae72
 		kamailio_start
7872a0d8
 		;;
a255cfce
 
7872a0d8
 	rpid)
 		rpid "$@"
 		;;
 
 	speeddial|speed_dial)
 		speeddial "$@"
 		;;
 
e2784b5e
 	acc)
 		acc "$@"
 		;;
 
2b7714a2
 	tls)
 		shift
 		tls_ca "$@"
 		;;
 
71380228
 	trap)
 		require_ctlengine
 		kamailio_trap
 		;;
 
97ec8555
 	pstrap)
 		kamailio_pstrap
 		;;
 
7872a0d8
 	start)
e102ae72
 		kamailio_start
7872a0d8
 		;;
 
 	stop)
e102ae72
 		kamailio_stop
7872a0d8
 		;;
 
 	version)
 		echo  "$0 $VERSION"
 		;;
a255cfce
 
7872a0d8
 	*)
ea772b35
 		extcmd "$@"
 
7872a0d8
 		usage
 		exit 1
 		;;
 esac