Browse code

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

Jiri Kuthan authored on 04/11/2003 01:20:28
Showing 1 changed files
... ...
@@ -1,5 +1,6 @@
1
-#!/bin/sh
1
+#!/bin/sh   
2 2
 #
3
+<<<<<<< sc
3 4
 # $Id$
4 5
 #
5 6
 # sc: ser control; tool for maintaining ser's databases
... ...
@@ -7,6 +8,9 @@
7 8
 # History:
8 9
 # --------
9 10
 # 2003-02-23 Thomas's start|stop commands checked in
11
+# 2003-09-08 multidomain features: usernames can now include domain 
12
+#              names too (jiri)
13
+# 2003-09-15 multidomain support completed (jiri)
10 14
 # 2003-10-30 more debugging output, less copy and paste in
11 15
 #             the previous item
12 16
 #
... ...
@@ -95,6 +99,7 @@ if [ -z "$ACL_TABLE" ] ; then
95 99
 	ACL_TABLE=grp
96 100
 fi
97 101
 ACL_USER_COLUMN=username
102
+ACL_DOMAIN_COLUMN=domain
98 103
 ACL_GROUP_COLUMN=grp
99 104
 ACL_MODIFIED_COLUMN=last_modified
100 105
 ACL_DOMAIN_COLUMN=domain
... ...
@@ -111,6 +116,9 @@ A_CALLID_COLUMN=callid
111 116
 A_CSEQ_COLUMN=cseq
112 117
 A_LAST_MODIFIED_COLUMN=last_modified
113 118
 
119
+# domain table
120
+DOMAIN_TABLE=domain
121
+
114 122
 FIFO_DBG=0
115 123
 
116 124
 EGREP="egrep"
... ...
@@ -145,6 +153,11 @@ parameter usage:
145 153
  ul add <username> <uri> ............ introduce a permanent UrLoc entry
146 154
  showdb [<username>] ................ show online users flushed in DB
147 155
 
156
+           * domains *
157
+ domain show ........................ show list of served domains
158
+ domain add <domainname> ............ add a new served domain
159
+ domain rm <domainname> ............. remove a served domain
160
+
148 161
            * control and diagnostics *
149 162
  moni ... show internal status      start .... start ser
150 163
  ps ..... show runnig processes     stop ..... stop ser
... ...
@@ -157,10 +170,38 @@ parameter usage:
157 170
    If the variable PW is set, the password will not be prompted.
158 171
 
159 172
      $EXTRA_TEXT
160
-
161 173
 EOF
162 174
 }
163 175
 
176
+
177
+# determine host name, typically for use in printing UAC
178
+# messages; we use today a simplistic but portable uname -n way --
179
+# no domain name is displayed ; fifo_uac expands !! to host
180
+# address only for optional header fields; uname output without
181
+# domain is sufficient for informational header fields such as
182
+# From
183
+#
184
+get_my_host() {
185
+	uname -n
186
+}
187
+
188
+# calculate name and domain of current user
189
+set_user() {
190
+
191
+	SERUSER=`echo $1|awk -F @ '{print $1}'`
192
+	SERDOMAIN=`echo $1|awk -F @ '{print $2}'`
193
+
194
+	if [ -z "$SERDOMAIN" ] ; then
195
+		SERDOMAIN="$SIP_DOMAIN"
196
+	fi
197
+
198
+	if [ -z "$SERDOMAIN" ] ; then
199
+		echo "domain unknown: use usernames with domain or set default domain in SIP_DOMAIN"
200
+		exit 1
201
+	fi
202
+}
203
+
204
+
164 205
 # check the parameter if it is a valid SIP URI
165 206
 # quite simplified now -- it captures just very basic
166 207
 # errors
... ...
@@ -174,7 +215,7 @@ check_uri() {
174 215
 prompt_pw() {
175 216
 	if [ -z "$PW" ] ; then
176 217
 		savetty=`stty -g`
177
-		printf "MySql password: "
218
+		printf "MySql password: " > /dev/stderr
178 219
 		stty -echo
179 220
     	read PW
180 221
 		stty $savetty
... ...
@@ -309,7 +350,7 @@ sql_query() {
309 350
 	# if password not yet queried, query it now
310 351
 	if [ -z "$PW" ] ; then
311 352
 		savetty=`stty -g`
312
-		printf "MySql password: "
353
+		printf "MySql password: " > /dev/stderr
313 354
 		stty -echo
314 355
     	read PW >&2
315 356
 		stty $savetty
... ...
@@ -343,9 +384,10 @@ usrloc() {
343 384
 	case $1 in 
344 385
 		show)
345 386
 			if [ $# -eq 2 ] ; then
346
-				fifo_cmd ul_show_contact $USRLOC_TABLE $2
387
+				set_user $2
388
+				fifo_cmd ul_show_contact $USRLOC_TABLE "$SERUSER@$SERDOMAIN"
347 389
 			elif [ $# -eq 1 ] ; then
348
-				printf "Dumping all contacts may take long: are you sure you want to proceed? [Y|N] "
390
+				printf "Dumping all contacts may take long: are you sure you want to proceed? [Y|N] " > /dev/stderr
349 391
 				read answer
350 392
 				if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
351 393
 					fifo_cmd ul_dump
... ...
@@ -364,12 +406,13 @@ usrloc() {
364 406
 			fi
365 407
 			shift
366 408
 			check_uri "$2"
409
+			set_user $1
367 410
 			if [ "$?" -ne "0" ] ; then
368 411
 				echo "$2 is not a valid URI"
369 412
 				exit 1
370 413
 			fi
371 414
 
372
-			fifo_cmd ul_add "$USRLOC_TABLE" "$1" "$2" "$FOREVER_REL" "1.00" "0"
415
+			fifo_cmd ul_add "$USRLOC_TABLE" "$SERUSER@$SERDOMAIN" "$2" "$FOREVER_REL" "1.00" "0"
373 416
 			exit $?
374 417
 			;;
375 418
 		rm)
... ...
@@ -378,7 +421,8 @@ usrloc() {
378 421
                 exit 1
379 422
             fi
380 423
 			shift
381
-			fifo_cmd ul_rm $USRLOC_TABLE $1
424
+			set_user $1
425
+			fifo_cmd ul_rm $USRLOC_TABLE "$SERUSER@$SERDOMAIN"
382 426
 
383 427
             ;;
384 428
 
... ...
@@ -389,16 +433,60 @@ usrloc() {
389 433
 	esac
390 434
 }
391 435
 
436
+domain() {
437
+	case $1 in
438
+		show)
439
+			# QUERY="select * FROM $DOMAIN_TABLE ; "
440
+			# sql_ro_query "$QUERY"
441
+			fifo_cmd domain_dump
442
+			;;
443
+		add)
444
+			shift
445
+			if [ $# -ne 1 ] ; then
446
+				echo missing domain to be added
447
+				exit 1
448
+			fi
449
+			prompt_pw
450
+       		QUERY="insert into $DOMAIN_TABLE (domain) VALUES ('$1');"
451
+			sql_query "$QUERY"
452
+			if [ $? -ne 0 ] ; then
453
+				echo "SQL Error"
454
+				exit 1
455
+			fi
456
+			fifo_cmd domain_reload
457
+			;;
458
+		rm)
459
+			shift
460
+			if [ $# -ne 1 ] ; then
461
+				echo missing domain to be removed
462
+				exit 1
463
+			fi
464
+			prompt_pw
465
+       		QUERY="delete from $DOMAIN_TABLE where domain='$1';"
466
+			sql_query "$QUERY"
467
+			if [ $? -ne 0 ] ; then
468
+				echo "SQL Error"
469
+				exit 1
470
+			fi
471
+			fifo_cmd domain_reload
472
+			;;
473
+		*)
474
+			usage
475
+			exit 1
476
+	esac
477
+}
478
+
392 479
 acl() {
393 480
 	case $1 in
394 481
 		show)
395 482
 			if [ $# -eq 2 ] ; then
396
-				is_user $2
483
+				set_user $2
484
+				is_user 
397 485
 				if [ $? -ne 0 ] ; then
398 486
 					echo non-existent user
399 487
 					exit 1;
400 488
 				fi
401
-				CLAUSE=" WHERE $ACL_USER_COLUMN='$2' "
489
+				CLAUSE=" WHERE $ACL_USER_COLUMN='$SERUSER' AND $ACL_DOMAIN_COLUMN='$SERDOMAIN' "
402 490
 			elif [ $# -ne 1 ] ; then
403 491
 				usage
404 492
 				exit 1
... ...
@@ -414,12 +502,12 @@ acl() {
414 502
 				exit 1
415 503
 			fi
416 504
 			prompt_pw
417
-			is_user $2
505
+			set_user $2
506
+			is_user 
418 507
 			if [ $? -ne 0 ] ; then
419 508
 				echo non-existent user
420 509
 				exit 1
421 510
 			fi
422
-			SIP_USER="$2"
423 511
 			shift 2
424 512
 			while [ $# -gt 0 ] ; do
425 513
 
... ...
@@ -440,7 +528,7 @@ acl() {
440 528
 
441 529
         		QUERY="insert into $ACL_TABLE \
442 530
                 	($ACL_USER_COLUMN,$ACL_GROUP_COLUMN,$ACL_MODIFIED_COLUMN, $ACL_DOMAIN_COLUMN ) \
443
-                	values ('$SIP_USER','$1', now(), '$SIP_DOMAIN' );"
531
+                	values ('$SERUSER','$1', now(), '$SERDOMAIN' );"
444 532
 				sql_query "$QUERY"
445 533
 				if [ $? -ne 0 ] ; then
446 534
 					echo "SQL Error"
... ...
@@ -449,7 +537,7 @@ acl() {
449 537
 				shift
450 538
 			done
451 539
 
452
-			$0 acl show $SIP_USER
540
+			$0 acl show $SERUSER@$SERDOMAIN
453 541
 
454 542
 			;;
455 543
 
... ...
@@ -461,8 +549,10 @@ acl() {
461 549
 				exit 1
462 550
 			fi	
463 551
 
552
+			set_user $2
553
+
464 554
 			QUERY="delete from $ACL_TABLE where \
465
-				$ACL_TABLE.$ACL_USER_COLUMN='$2' $CLAUSE"
555
+				$ACL_TABLE.$ACL_USER_COLUMN='$SERUSER' AND $ACL_DOMAIN_COLUMN='$SERDOMAIN' $CLAUSE"
466 556
 			sql_query "$QUERY"
467 557
 
468 558
 			$0 acl show $2
... ...
@@ -480,7 +570,7 @@ acl() {
480 570
 # output: false if exists, true otherwise
481 571
 is_user() {
482 572
 	QUERY="select count(*) from $SUB_TABLE \
483
-		where $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$SIP_DOMAIN';"
573
+		where $SUBSCRIBER_COLUMN='$SERUSER' and $REALM_COLUMN='$SERDOMAIN';"
484 574
 	CNT=`sql_ro_query "$QUERY" | grep -v ERROR | $LAST_LINE`
485 575
 	if [ "0$CNT" -eq 0 ] ; then
486 576
 		false
... ...
@@ -495,12 +585,14 @@ is_user() {
495 585
 # output: HA1, HA1B
496 586
 credentials()
497 587
 {
498
-	HA1=`$GENHA1 $1 $SIP_DOMAIN $2`
588
+	set_user $1
589
+
590
+	HA1=`$GENHA1 $SERUSER $SERDOMAIN $2`
499 591
 	if [ $? -ne 0 ] ; then
500 592
 		echo "HA1 calculation failed"
501 593
 		exit 1
502 594
 	fi
503
-	HA1B=`$GENHA1 "$1@$SIP_DOMAIN" $SIP_DOMAIN $2`
595
+	HA1B=`$GENHA1 "$SERUSER@$SERDOMAIN" $SERDOMAIN $2`
504 596
 	if [ $? -ne 0 ] ; then
505 597
 		echo "HA1B calculation failed"
506 598
 		exit 1
... ...
@@ -509,15 +601,6 @@ credentials()
509 601
 
510 602
 #================================================================
511 603
 
512
-if [ -z "$SIP_DOMAIN" ] ; then
513
-	echo
514
-	echo "You need to set environment variable SIP_DOMAIN (e.g. to 'foobar.com') first"
515
-	echo
516
-#	This confuses new users cause its easy to miss the information above
517
-#	usage
518
-	exit 1
519
-fi
520
-
521 604
 # if the script calls itself ...
522 605
 export PW
523 606
 
... ...
@@ -588,7 +671,7 @@ case $1 in
588 671
 		QUERY="update $SUB_TABLE \
589 672
 			set $HA1_COLUMN='$HA1', $HA1B_COLUMN='$HA1B', $PASSWORD_COLUMN='$2' \
590 673
 			, $SUB_MODIFIED_COLUMN=now() \
591
-			WHERE $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$SIP_DOMAIN';"
674
+			WHERE $SUBSCRIBER_COLUMN='$SERUSER' and $REALM_COLUMN='$SERDOMAIN';"
592 675
 		sql_query "$QUERY"
593 676
 		if [ $? -ne 0 ] ; then
594 677
 			echo "password change failed"
... ...
@@ -618,7 +701,7 @@ case $1 in
618 701
 				($SUBSCRIBER_COLUMN,$REALM_COLUMN,$HA1_COLUMN,\
619 702
 				$HA1B_COLUMN,$PASSWORD_COLUMN,$EMAIL_COLUMN, $SUB_CREATED_COLUMN,  \
620 703
 				$PHP_LIB_COLUMN ) \
621
-				values ('$1','$SIP_DOMAIN','$HA1','$HA1B','$2', '$3', now(), '$HA1' );";
704
+				values ('$SERUSER','$SERDOMAIN','$HA1','$HA1B','$2', '$3', now(), '$HA1' );";
622 705
 		sql_query "$QUERY"
623 706
 		if [ $? -ne 0 ] ; then
624 707
 			echo "introducing a new user to the database failed"
... ...
@@ -661,15 +744,16 @@ case $1 in
661 744
 			exit 1
662 745
 		fi
663 746
 		shift
747
+		set_user $1
664 748
 		QUERY="select $SUB_TABLE.$EMAIL_COLUMN from $SUB_TABLE where  \
665
-			$SUB_TABLE.$SUBSCRIBER_COLUMN='$1'"
749
+			$SUB_TABLE.$SUBSCRIBER_COLUMN='$SERUSER' and $SUB_TABLE.$REALM_COLUMN='$SERDOMAIN'"
666 750
 		EA=`sql_ro_query "$QUERY" "-B" | grep -v ERROR | $LAST_LINE`
667 751
 		if [ $? -ne 0 ] ; then
668 752
 			echo "MySql query failed"
669 753
 			exit 1
670 754
 		fi
671 755
 		echo "Write email to $1: $EA now ..."
672
-		mail -s "Message from $SIP_DOMAIN SIP admin" $EA
756
+		mail -s "Message from $SERDOMAIN SIP admin" $EA
673 757
 		if [ $? -eq 0 ] ; then
674 758
 			echo message sent
675 759
 		else
... ...
@@ -702,8 +786,10 @@ case $1 in
702 786
 
703 787
 		shift
704 788
 
789
+		set_user $1
790
+
705 791
 		QUERY1="select $SUB_TABLE.$EMAIL_COLUMN from $SUB_TABLE where  \
706
-			$SUB_TABLE.$SUBSCRIBER_COLUMN='$1'"
792
+			$TABLE.$SUBSCRIBER_COLUMN='$SERUSER' and $TABLE.$REALM_COLUMN='$SERDOMAIN' "
707 793
 		QUERY2="select $UL_TABLE.* from $UL_TABLE where \
708 794
 			$UL_TABLE.$USER_COLUMN='$1' order by expires desc"
709 795
 		QUERY3="select $UL_TABLE.$USER_COLUMN, $SUB_TABLE.$EMAIL_COLUMN, $UL_TABLE.$CALLID_COLUMN \
... ...
@@ -729,7 +815,8 @@ case $1 in
729 815
 		shift 
730 816
 		prompt_pw 
731 817
 
732
-        is_user $1
818
+		set_user $1
819
+        is_user 
733 820
         if [ $? -ne 0 ] ; then
734 821
             echo non-existent user
735 822
             exit 1
... ...
@@ -739,7 +826,7 @@ case $1 in
739 826
 		$0 acl revoke $1  > /dev/null 2>&1
740 827
 
741 828
 		# destroy the user now
742
-        QUERY="delete from $SUB_TABLE where $SUB_TABLE.$SUBSCRIBER_COLUMN='$1'"
829
+        QUERY="delete from $SUB_TABLE where $SUB_TABLE.$SUBSCRIBER_COLUMN='$SERUSER' and $SUB_TABLE.$REALM_COLUMN='$SERDOMAIN'"
743 830
 		sql_query "$QUERY"
744 831
 
745 832
 		# and also all his contacts
... ...
@@ -755,6 +842,11 @@ case $1 in
755 842
 		acl "$@"
756 843
 		;;
757 844
 
845
+	domain)
846
+		shift
847
+		domain "$@"
848
+		;;
849
+
758 850
 	fifo)
759 851
 		shift
760 852
 		fifo_cmd "$@"
... ...
@@ -768,8 +860,9 @@ case $1 in
768 860
 			usage
769 861
 			exit 1
770 862
 		fi
863
+		myhost=`get_my_host`
771 864
 		RET=`fifo_cmd t_uac_dlg OPTIONS "$2" "." \
772
-			"From: sip:daemon@$SIP_DOMAIN" \
865
+			"From: sip:daemon@$myhost" \
773 866
 			"To: <$2>" "Contact: <sip:daemon@!!>" "." "." \
774 867
 			| head -1 ` 
775 868
 		print_status $RET
... ...
@@ -780,8 +873,9 @@ case $1 in
780 873
 			usage
781 874
 			exit 1
782 875
 		fi
876
+		myhost=`get_my_host`
783 877
 		RET=`fifo_cmd t_uac_dlg NOTIFY "$2" "." \
784
-			"From: sip:daemon@$SIP_DOMAIN" \
878
+			"From: sip:daemon@$myhost" \
785 879
 			"To: <$2>" "Event: check-sync" \
786 880
 			"Contact: <sip:daemon@!!>" "." "." |
787 881
 			head -1 `