Преглед изворни кода

serctl merger (primarily related to multidomain as on stable and some
changes to var names, which Bogdan committed recently)

Jiri Kuthan пре 22 година
родитељ
комит
28b83c1a15
1 измењених фајлова са 130 додато и 36 уклоњено
  1. 130 36
      scripts/sc

+ 130 - 36
scripts/sc

@@ -1,5 +1,6 @@
-#!/bin/sh
+#!/bin/sh   
 #
+<<<<<<< sc
 # $Id$
 #
 # sc: ser control; tool for maintaining ser's databases
@@ -7,6 +8,9 @@
 # History:
 # --------
 # 2003-02-23 Thomas's start|stop commands checked in
+# 2003-09-08 multidomain features: usernames can now include domain 
+#              names too (jiri)
+# 2003-09-15 multidomain support completed (jiri)
 # 2003-10-30 more debugging output, less copy and paste in
 #             the previous item
 #
@@ -95,6 +99,7 @@ if [ -z "$ACL_TABLE" ] ; then
 	ACL_TABLE=grp
 fi
 ACL_USER_COLUMN=username
+ACL_DOMAIN_COLUMN=domain
 ACL_GROUP_COLUMN=grp
 ACL_MODIFIED_COLUMN=last_modified
 ACL_DOMAIN_COLUMN=domain
@@ -111,6 +116,9 @@ A_CALLID_COLUMN=callid
 A_CSEQ_COLUMN=cseq
 A_LAST_MODIFIED_COLUMN=last_modified
 
+# domain table
+DOMAIN_TABLE=domain
+
 FIFO_DBG=0
 
 EGREP="egrep"
@@ -145,6 +153,11 @@ parameter usage:
  ul add <username> <uri> ............ introduce a permanent UrLoc entry
  showdb [<username>] ................ show online users flushed in DB
 
+           * domains *
+ domain show ........................ show list of served domains
+ domain add <domainname> ............ add a new served domain
+ domain rm <domainname> ............. remove a served domain
+
            * control and diagnostics *
  moni ... show internal status      start .... start ser
  ps ..... show runnig processes     stop ..... stop ser
@@ -157,10 +170,38 @@ parameter usage:
    If the variable PW is set, the password will not be prompted.
 
      $EXTRA_TEXT
-
 EOF
 }
 
+
+# determine host name, typically for use in printing UAC
+# messages; we use today a simplistic but portable uname -n way --
+# no domain name is displayed ; fifo_uac expands !! to host
+# address only for optional header fields; uname output without
+# domain is sufficient for informational header fields such as
+# From
+#
+get_my_host() {
+	uname -n
+}
+
+# calculate name and domain of current user
+set_user() {
+
+	SERUSER=`echo $1|awk -F @ '{print $1}'`
+	SERDOMAIN=`echo $1|awk -F @ '{print $2}'`
+
+	if [ -z "$SERDOMAIN" ] ; then
+		SERDOMAIN="$SIP_DOMAIN"
+	fi
+
+	if [ -z "$SERDOMAIN" ] ; then
+		echo "domain unknown: use usernames with domain or set default domain in SIP_DOMAIN"
+		exit 1
+	fi
+}
+
+
 # check the parameter if it is a valid SIP URI
 # quite simplified now -- it captures just very basic
 # errors
@@ -174,7 +215,7 @@ check_uri() {
 prompt_pw() {
 	if [ -z "$PW" ] ; then
 		savetty=`stty -g`
-		printf "MySql password: "
+		printf "MySql password: " > /dev/stderr
 		stty -echo
     	read PW
 		stty $savetty
@@ -309,7 +350,7 @@ sql_query() {
 	# if password not yet queried, query it now
 	if [ -z "$PW" ] ; then
 		savetty=`stty -g`
-		printf "MySql password: "
+		printf "MySql password: " > /dev/stderr
 		stty -echo
     	read PW >&2
 		stty $savetty
@@ -343,9 +384,10 @@ usrloc() {
 	case $1 in 
 		show)
 			if [ $# -eq 2 ] ; then
-				fifo_cmd ul_show_contact $USRLOC_TABLE $2
+				set_user $2
+				fifo_cmd ul_show_contact $USRLOC_TABLE "$SERUSER@$SERDOMAIN"
 			elif [ $# -eq 1 ] ; then
-				printf "Dumping all contacts may take long: are you sure you want to proceed? [Y|N] "
+				printf "Dumping all contacts may take long: are you sure you want to proceed? [Y|N] " > /dev/stderr
 				read answer
 				if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
 					fifo_cmd ul_dump
@@ -364,12 +406,13 @@ usrloc() {
 			fi
 			shift
 			check_uri "$2"
+			set_user $1
 			if [ "$?" -ne "0" ] ; then
 				echo "$2 is not a valid URI"
 				exit 1
 			fi
 
-			fifo_cmd ul_add "$USRLOC_TABLE" "$1" "$2" "$FOREVER_REL" "1.00" "0"
+			fifo_cmd ul_add "$USRLOC_TABLE" "$SERUSER@$SERDOMAIN" "$2" "$FOREVER_REL" "1.00" "0"
 			exit $?
 			;;
 		rm)
@@ -378,7 +421,8 @@ usrloc() {
                 exit 1
             fi
 			shift
-			fifo_cmd ul_rm $USRLOC_TABLE $1
+			set_user $1
+			fifo_cmd ul_rm $USRLOC_TABLE "$SERUSER@$SERDOMAIN"
 
             ;;
 
@@ -389,16 +433,60 @@ usrloc() {
 	esac
 }
 
+domain() {
+	case $1 in
+		show)
+			# QUERY="select * FROM $DOMAIN_TABLE ; "
+			# sql_ro_query "$QUERY"
+			fifo_cmd domain_dump
+			;;
+		add)
+			shift
+			if [ $# -ne 1 ] ; then
+				echo missing domain to be added
+				exit 1
+			fi
+			prompt_pw
+       		QUERY="insert into $DOMAIN_TABLE (domain) VALUES ('$1');"
+			sql_query "$QUERY"
+			if [ $? -ne 0 ] ; then
+				echo "SQL Error"
+				exit 1
+			fi
+			fifo_cmd domain_reload
+			;;
+		rm)
+			shift
+			if [ $# -ne 1 ] ; then
+				echo missing domain to be removed
+				exit 1
+			fi
+			prompt_pw
+       		QUERY="delete from $DOMAIN_TABLE where domain='$1';"
+			sql_query "$QUERY"
+			if [ $? -ne 0 ] ; then
+				echo "SQL Error"
+				exit 1
+			fi
+			fifo_cmd domain_reload
+			;;
+		*)
+			usage
+			exit 1
+	esac
+}
+
 acl() {
 	case $1 in
 		show)
 			if [ $# -eq 2 ] ; then
-				is_user $2
+				set_user $2
+				is_user 
 				if [ $? -ne 0 ] ; then
 					echo non-existent user
 					exit 1;
 				fi
-				CLAUSE=" WHERE $ACL_USER_COLUMN='$2' "
+				CLAUSE=" WHERE $ACL_USER_COLUMN='$SERUSER' AND $ACL_DOMAIN_COLUMN='$SERDOMAIN' "
 			elif [ $# -ne 1 ] ; then
 				usage
 				exit 1
@@ -414,12 +502,12 @@ acl() {
 				exit 1
 			fi
 			prompt_pw
-			is_user $2
+			set_user $2
+			is_user 
 			if [ $? -ne 0 ] ; then
 				echo non-existent user
 				exit 1
 			fi
-			SIP_USER="$2"
 			shift 2
 			while [ $# -gt 0 ] ; do
 
@@ -440,7 +528,7 @@ acl() {
 
         		QUERY="insert into $ACL_TABLE \
                 	($ACL_USER_COLUMN,$ACL_GROUP_COLUMN,$ACL_MODIFIED_COLUMN, $ACL_DOMAIN_COLUMN ) \
-                	values ('$SIP_USER','$1', now(), '$SIP_DOMAIN' );"
+                	values ('$SERUSER','$1', now(), '$SERDOMAIN' );"
 				sql_query "$QUERY"
 				if [ $? -ne 0 ] ; then
 					echo "SQL Error"
@@ -449,7 +537,7 @@ acl() {
 				shift
 			done
 
-			$0 acl show $SIP_USER
+			$0 acl show $SERUSER@$SERDOMAIN
 
 			;;
 
@@ -461,8 +549,10 @@ acl() {
 				exit 1
 			fi	
 
+			set_user $2
+
 			QUERY="delete from $ACL_TABLE where \
-				$ACL_TABLE.$ACL_USER_COLUMN='$2' $CLAUSE"
+				$ACL_TABLE.$ACL_USER_COLUMN='$SERUSER' AND $ACL_DOMAIN_COLUMN='$SERDOMAIN' $CLAUSE"
 			sql_query "$QUERY"
 
 			$0 acl show $2
@@ -480,7 +570,7 @@ acl() {
 # output: false if exists, true otherwise
 is_user() {
 	QUERY="select count(*) from $SUB_TABLE \
-		where $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$SIP_DOMAIN';"
+		where $SUBSCRIBER_COLUMN='$SERUSER' and $REALM_COLUMN='$SERDOMAIN';"
 	CNT=`sql_ro_query "$QUERY" | grep -v ERROR | $LAST_LINE`
 	if [ "0$CNT" -eq 0 ] ; then
 		false
@@ -495,12 +585,14 @@ is_user() {
 # output: HA1, HA1B
 credentials()
 {
-	HA1=`$GENHA1 $1 $SIP_DOMAIN $2`
+	set_user $1
+
+	HA1=`$GENHA1 $SERUSER $SERDOMAIN $2`
 	if [ $? -ne 0 ] ; then
 		echo "HA1 calculation failed"
 		exit 1
 	fi
-	HA1B=`$GENHA1 "$1@$SIP_DOMAIN" $SIP_DOMAIN $2`
+	HA1B=`$GENHA1 "$SERUSER@$SERDOMAIN" $SERDOMAIN $2`
 	if [ $? -ne 0 ] ; then
 		echo "HA1B calculation failed"
 		exit 1
@@ -509,15 +601,6 @@ credentials()
 
 #================================================================
 
-if [ -z "$SIP_DOMAIN" ] ; then
-	echo
-	echo "You need to set environment variable SIP_DOMAIN (e.g. to 'foobar.com') first"
-	echo
-#	This confuses new users cause its easy to miss the information above
-#	usage
-	exit 1
-fi
-
 # if the script calls itself ...
 export PW
 
@@ -588,7 +671,7 @@ case $1 in
 		QUERY="update $SUB_TABLE \
 			set $HA1_COLUMN='$HA1', $HA1B_COLUMN='$HA1B', $PASSWORD_COLUMN='$2' \
 			, $SUB_MODIFIED_COLUMN=now() \
-			WHERE $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$SIP_DOMAIN';"
+			WHERE $SUBSCRIBER_COLUMN='$SERUSER' and $REALM_COLUMN='$SERDOMAIN';"
 		sql_query "$QUERY"
 		if [ $? -ne 0 ] ; then
 			echo "password change failed"
@@ -618,7 +701,7 @@ case $1 in
 				($SUBSCRIBER_COLUMN,$REALM_COLUMN,$HA1_COLUMN,\
 				$HA1B_COLUMN,$PASSWORD_COLUMN,$EMAIL_COLUMN, $SUB_CREATED_COLUMN,  \
 				$PHP_LIB_COLUMN ) \
-				values ('$1','$SIP_DOMAIN','$HA1','$HA1B','$2', '$3', now(), '$HA1' );";
+				values ('$SERUSER','$SERDOMAIN','$HA1','$HA1B','$2', '$3', now(), '$HA1' );";
 		sql_query "$QUERY"
 		if [ $? -ne 0 ] ; then
 			echo "introducing a new user to the database failed"
@@ -661,15 +744,16 @@ case $1 in
 			exit 1
 		fi
 		shift
+		set_user $1
 		QUERY="select $SUB_TABLE.$EMAIL_COLUMN from $SUB_TABLE where  \
-			$SUB_TABLE.$SUBSCRIBER_COLUMN='$1'"
+			$SUB_TABLE.$SUBSCRIBER_COLUMN='$SERUSER' and $SUB_TABLE.$REALM_COLUMN='$SERDOMAIN'"
 		EA=`sql_ro_query "$QUERY" "-B" | grep -v ERROR | $LAST_LINE`
 		if [ $? -ne 0 ] ; then
 			echo "MySql query failed"
 			exit 1
 		fi
 		echo "Write email to $1: $EA now ..."
-		mail -s "Message from $SIP_DOMAIN SIP admin" $EA
+		mail -s "Message from $SERDOMAIN SIP admin" $EA
 		if [ $? -eq 0 ] ; then
 			echo message sent
 		else
@@ -702,8 +786,10 @@ case $1 in
 
 		shift
 
+		set_user $1
+
 		QUERY1="select $SUB_TABLE.$EMAIL_COLUMN from $SUB_TABLE where  \
-			$SUB_TABLE.$SUBSCRIBER_COLUMN='$1'"
+			$TABLE.$SUBSCRIBER_COLUMN='$SERUSER' and $TABLE.$REALM_COLUMN='$SERDOMAIN' "
 		QUERY2="select $UL_TABLE.* from $UL_TABLE where \
 			$UL_TABLE.$USER_COLUMN='$1' order by expires desc"
 		QUERY3="select $UL_TABLE.$USER_COLUMN, $SUB_TABLE.$EMAIL_COLUMN, $UL_TABLE.$CALLID_COLUMN \
@@ -729,7 +815,8 @@ case $1 in
 		shift 
 		prompt_pw 
 
-        is_user $1
+		set_user $1
+        is_user 
         if [ $? -ne 0 ] ; then
             echo non-existent user
             exit 1
@@ -739,7 +826,7 @@ case $1 in
 		$0 acl revoke $1  > /dev/null 2>&1
 
 		# destroy the user now
-        QUERY="delete from $SUB_TABLE where $SUB_TABLE.$SUBSCRIBER_COLUMN='$1'"
+        QUERY="delete from $SUB_TABLE where $SUB_TABLE.$SUBSCRIBER_COLUMN='$SERUSER' and $SUB_TABLE.$REALM_COLUMN='$SERDOMAIN'"
 		sql_query "$QUERY"
 
 		# and also all his contacts
@@ -755,6 +842,11 @@ case $1 in
 		acl "$@"
 		;;
 
+	domain)
+		shift
+		domain "$@"
+		;;
+
 	fifo)
 		shift
 		fifo_cmd "$@"
@@ -768,8 +860,9 @@ case $1 in
 			usage
 			exit 1
 		fi
+		myhost=`get_my_host`
 		RET=`fifo_cmd t_uac_dlg OPTIONS "$2" "." \
-			"From: sip:daemon@$SIP_DOMAIN" \
+			"From: sip:daemon@$myhost" \
 			"To: <$2>" "Contact: <sip:daemon@!!>" "." "." \
 			| head -1 ` 
 		print_status $RET
@@ -780,8 +873,9 @@ case $1 in
 			usage
 			exit 1
 		fi
+		myhost=`get_my_host`
 		RET=`fifo_cmd t_uac_dlg NOTIFY "$2" "." \
-			"From: sip:daemon@$SIP_DOMAIN" \
+			"From: sip:daemon@$myhost" \
 			"To: <$2>" "Event: check-sync" \
 			"Contact: <sip:daemon@!!>" "." "." |
 			head -1 `