File: sc.dbtext

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (981 lines) | stat: -rw-r--r-- 24,147 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
#!/bin/sh 
#
# $Id: sc.dbtext,v 1.17 2006/07/03 09:46:40 bogdan_iancu Exp $
#
# sc: openser control; tool for maintaining openser's databases
#
# History:
# --------
# 2005-03-25 - SC with DBText support (cesc santasusana)
# 2005-07-08 - Removed all non-DBText functions (daniel)
# 2006-05-16 - added ability to specify MD5 from a configuration file
#              FreeBSD does not have the md5sum function (norm)
#


#===================================================================

### include resource files, if any
if [ -f /etc/openser/.openscdbtextrc ]; then
	. /etc/openser/.openscdbtextrc
fi
if [ -f /usr/local/etc/openser/.openscdbtextrc ]; then
	. /usr/local/etc/openser/.openscdbtextrc
fi
if [ -f ~/.openscdbtextrc ]; then
	. ~/.openscdbtextrc
fi

### version fo this script
VERSION='1.1 - $Revision: 1.17 $'

##### ----------------------------------------------- #####
### SQL config
if [ -z $DBTEXT_PATH ]; then
	DBTEXT_PATH="/usr/local/etc/openser/dbtext"
	export DBTEXT_PATH
fi

##### ----------------------------------------------- #####
### binaries
if [ -z "$LAST_LINE" ] ; then
	LAST_LINE='tail -1'
fi
if [ -z "$MD5" ]; then
	MD5="md5sum"
fi

##### ----------------------------------------------- #####
### path to useful tools
if [ -z "$EGREP" ] ; then
	EGREP="egrep"
fi
if [ -z "$AWK" ] ; then
	AWK="awk"
fi

##### ----------------------------------------------- #####
# ACL name verification
if [ -z "$VERIFY_ACL" ] ; then
	VERIFY_ACL=1
fi
if [ -z "$ACL_GROUPS" ] ; then
	ACL_GROUPS="local ld int voicemail free-pstn"
fi

##### ----------------------------------------------- #####
#### table names

# UsrLoc Table
if [ -z "$UL_TABLE" ] ; then
	UL_TABLE=location
fi
USER_COLUMN=username
CALLID_COLUMN=callid

# subscriber table
if [ -z "$SUB_TABLE" ] ; then
	SUB_TABLE=subscriber
fi
REALM_COLUMN=domain
HA1_COLUMN=HA1
HA1B_COLUMN=HA1B
PASSWORD_COLUMN=password
RPID_COLUMN=rpid
SUBSCRIBER_COLUMN='username'
EMAIL_COLUMN=email_address
SUB_CREATED_COLUMN=datetime_created
SUB_MODIFIED_COLUMN=datetime_modified
PHP_LIB_COLUMN=phplib_id

# acl table
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

# aliases table
if [ -z "$ALS_TABLE" ] ; then
	ALS_TABLE=aliases
fi
A_USER_COLUMN=username
A_CONTACT_COLUMN=contact
A_EXPIRES_COLUMN=expires
A_Q_COLUMN=q
A_CALLID_COLUMN=callid
A_CSEQ_COLUMN=cseq
A_LAST_MODIFIED_COLUMN=last_modified

# domain table
if [ -z "$DOMAIN_TABLE" ] ; then
	DOMAIN_TABLE=domain
fi

# URI table
if [ -z "$URI_TABLE" ] ; then
	URI_TABLE=uri
fi
URIUSER_COLUMN=uri_user
MODIFIED_COLUMN=last_modified

# dbaliases table
if [ -z "$DA_TABLE" ] ; then
	DA_TABLE=dbaliases
fi
DA_USER_COLUMN=username
DA_DOMAIN_COLUMN=domain
DA_ALIAS_USER_COLUMN=alias_username
DA_ALIAS_DOMAIN_COLUMN=alias_domain

# speeddial table
if [ -z "$SD_TABLE" ] ; then
	SD_TABLE=speed_dial
fi
SD_USER_COLUMN=username
SD_DOMAIN_COLUMN=domain
SD_SD_USER_COLUMN=sd_username
SD_SD_DOMAIN_COLUMN=sd_domain
SD_NEW_URI_COLUMN=new_uri
SD_DESC_COLUMN=description

# avp table
if [ -z "$AVP_TABLE" ] ; then
	AVP_TABLE=usr_preferences
fi

AVP_UUID_COLUMN=uuid
AVP_USER_COLUMN=username
AVP_DOMAIN_COLUMN=domain
AVP_ATTRIBUTE_COLUMN=attribute
AVP_VALUE_COLUMN=value
AVP_TYPE_COLUMN=type
AVP_MODIFIED_COLUMN=modified

# lcr table
if [ -z "$LCR_TABLE" ] ; then
	LCR_TABLE=lcr
fi

# gw table
if [ -z "$GW_TABLE" ] ; then
	GW_TABLE=gw
fi

# gw_grp table
if [ -z "$GW_GRP_TABLE" ] ; then
	GW_GRP_TABLE=gw_grp
fi

#===================================================================

#Set variables to dbtext's files ...
if [ -z "$UL_FILE" ] ; then
	UL_FILE=$DBTEXT_PATH/$UL_TABLE
fi
if [ -z "$DOMAIN_FILE" ] ; then
	DOMAIN_FILE=$DBTEXT_PATH/$DOMAIN_TABLE
fi
if [ -z "$ALS_FILE" ] ; then
	ALS_FILE=$DBTEXT_PATH/$ALS_TABLE
fi
if [ -z "$SUB_FILE" ] ; then
	SUB_FILE=$DBTEXT_PATH/$SUB_TABLE
fi
if [ -z "$URI_FILE" ] ; then
	URI_FILE=$DBTEXT_PATH/$URI_TABLE
fi
if [ -z "$ACL_FILE" ] ; then
	ACL_FILE=$DBTEXT_PATH/$ACL_TABLE
fi
if [ -z "$LCR_FILE" ] ; then
        LCR_FILE=$DBTEXT_PATH/$LCR_TABLE
fi
if [ -z "$GW_FILE" ] ; then
        GW_FILE=$DBTEXT_PATH/$GW_TABLE
fi
if [ -z "$GW_GRP_FILE" ] ; then
        GW_GRP_FILE=$DBTEXT_PATH/$GW_GRP_TABLE
fi

#===================================================================


usage() {
CMD=`basename $0`
if [ "0$VERIFY_ACL" -eq 1 ] ; then
	EXTRA_TEXT="ACL privileges are: $ACL_GROUPS"
fi
cat <<EOF
$0 $VERSION
parameter usage: 
           * subscribers *
 add <username> <password> <email> .. add a new subscriber (*)
 passwd <username> <passwd> ......... change user's password (*)
 rm <username> ...................... delete a user (*)
 mail <username> .................... send an email to a user
 rpid add <username> <rpid> ......... add rpid for a user (*)
 rpid rm <username> ................. remove rpid for a user (*)
 rpid show <username> ............... show rpid of a user

           * access control lists *
 acl show [<username>] .............. show user membership
 acl grant <username> <group> ....... grant user membership (*)
 acl revoke <username> [<group>] .... grant user membership(s) (*)

           * usrloc *
 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

           * lcr *
   * IP addresses must be entered in dotted quad format e.g. 1.2.3.4        *
   * <uri_scheme> and <transport> must be entered in integer or text,       *
   * e.g. transport '2' is identical to transport 'tcp'.                    *
   *   scheme: 1=sip, 2=sips;   transport: 1=udp, 2=tcp, 3=tls              *
   * Examples:  openser_dbtext_ctl lcr addgw_grp usa 1                      *
   *            openser_dbtext_ctl lcr addgw level3 1.2.3.4 5080 sip tcp 1  *
   *            openser_dbtext_ctl lcr addroute +1 % 1 1                    *
 lcr show ...................................... show routes, gateways and groups
 lcr addgw_grp <grp_name> <grp_id> ............. add gateway group with grp_id
 lcr rmgw_grp  <grp_id> ........................ delete the gw_grp
 lcr addgw <gw_name> <ip> <port> <scheme> <transport> <grp_id> ........... add a gateway
 lcr addgw <gw_name> <ip> <port> <scheme> <transport> <grp_id> <prefix> .. add a gateway with prefix
 lcr rmgw  <gw_name> ..................................................... delete a gateway
 lcr addroute <prefix> <from> <grp_id> <prio> .. add a route
 lcr rmroute  <prefix> <from> <grp_id> <prio> .. delete a route

           * control and diagnostics *
 dbtext [dbtext_path] ... create the needed dbtext files (default 
                          location is /usr/local/etc/openser/dbtext)

           * Database Selection *
 
 The exact location of the dbtext files to use must be set. Export
 DBTEXT_PATH="/path/to/dbtext" (no trailing slash). No password is needed
 for dbtext. All these parameters can also be set in the "serctl" script
 file directly. Note that dbtext tries to mimic the results of a MySQL DB,
 but it is not always possible to reproduce them 100%.
 DBText files field order is the same as in the MySQL database.
     
 $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
}

chown_openser() {
	if [ -n "$OPENSER_USER" -a -n "$OPENSER_GROUP" ] ; then
		chown $OPENSER_USER:$OPENSER_GROUP $1
	fi
}

# 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 -e "\nDomain unknown: use usernames with domain or set default domain in SIP_DOMAIN\n"
		exit 1
	fi
}


# check the parameter if it is a valid SIP URI
# quite simplified now -- it captures just very basic
# errors
check_uri() {
	echo "$1" | $EGREP "^sip(s)?:([a-zA-Z0-9_]+@)?.*\..*"  > /dev/null
	if [ $? -ne 0 ] ; then 
		echo -e "\nerror: invalid URI: $1\n" > /dev/stderr
		exit 1
	fi
}

rpid() {
	if [ "$#" -lt 2 ] ; then
		echo -e "\nrpid: too few parameters\n"
		usage
		exit 1
	fi
	shift;

	case $1 in
		show)
			echo
			if [ $# -eq 2 ] ; then
				set_user $2
				is_user 
				if [ $? -ne 0 ] ; then
					echo -e "\nnon-existent user\n"
					exit 1;
				fi
				CLAUSE=":$SERUSER:$SERDOMAIN:"
				
			elif [ $# -ne 1 ] ; then
				usage
				exit 1
			fi
			echo -e "RPID show ... "
			if [ -z $CLAUSE ]; then
				CLAUSE=""
			fi
			#19 is the rpid field
			cat $SUB_FILE | $EGREP "$CLAUSE" | cut -s -d : -f 2,19
			echo
			;;

		add|rm)
			MODE=$1;

			if [ "$MODE" = "add" ] ; then
			    ARG_NUM=3;
			else
			    ARG_NUM=2;
			fi
			
			if [ $# -lt $ARG_NUM ] ; then
				usage
				exit 1
			fi

			set_user $2
			is_user 
			if [ $? -ne 0 ] ; then
				echo -e "\nnon-existent user\n"
				exit 1
			fi
			shift 2

			if [ "$MODE" = "add" ] ; then
			        RPID_VAL="'$1'";
			else
			        RPID_VAL="";
			fi

			old_line=`cat $SUB_FILE | $EGREP :$SERUSER:$SERDOMAIN: | head -n 1`
			pre_rpid=`echo $old_line | cut -s -d : -f 1-18`
			post_rpid=`echo $old_line | cut -s -d : -f 20-25`
			new_line=$pre_rpid:$RPID_VAL:$post_rpid
			cat $SUB_FILE | sed s/$old_line/$new_line/g >> $SUB_FILE.tmp
			mv -f $SUB_FILE.tmp $SUB_FILE				
			
			echo -e "RPID added/removed ... showing current value\n"
			$0 rpid show $SERUSER@$SERDOMAIN

			;;

		*)
			usage
			exit 1
			;;
	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 -e "\nmissing domain to be added\n"
				exit 1
			fi
			line="$1:$date_now"
			echo "$line" >> $DOMAIN_FILE
			echo -e "\nDomain $1 saved into DBTEXT ... now trying to force OpenSER to reload it into memory\n"
			#FIXME: domain_reload command not supported yet
			#fifo_cmd domain_reload
			;;
		rm)
			shift
			if [ $# -ne 1 ] ; then
				echo -e "\nmissing domain to be removed\n"
				exit 1
			fi
			cat $DOMAIN_FILE | $EGREP -v "^$1:" > $DOMAIN_FILE.tmp
			mv -f $DOMAIN_FILE.tmp $DOMAIN_FILE
			echo -e "\nDomain $1 removed from DBTEXT ... now trying to force SER to remove it from memory\n"
			#FIXME: domain_reload command not supported yet
			#fifo_cmd domain_reload
			;;
		*)
			usage
			exit 1
	esac
}

lcr() {
	case $1 in
		show)
			echo lcr routes
			lines=`wc -l $LCR_FILE | cut -d " " -f 1`
			let "lines = $lines - 1"
			tail -n $lines $LCR_FILE

			echo lcr gateway groups
			lines=`wc -l $GW_GRP_FILE | cut -d " " -f 1`
			let "lines = $lines - 1"
			tail -n $lines $GW_GRP_FILE

			echo lcr gateways
			lines=`wc -l $GW_FILE | cut -d " " -f 1`
			let "lines = $lines - 1"
			#tail -n $lines $GW_FILE

			IFS=":"
			tail -n $lines $GW_FILE | ( while read -a chunks; do
				element_count=${#chunks[@]}
				let "element_count_max_limit = $element_count - 1"
				index="0"
				let "ip_4 = ${chunks[2]} / 16777216"

				let "ip_3 = $ip_4 * 16777216"
				let "ip_3_tmp = ${chunks[2]} - $ip_3"
				let "ip_3 = $ip_3_tmp / 65536"

				let "ip_2_tmp = $ip_3_tmp - $ip_3 * 65536"
				let "ip_2 = $ip_2_tmp / 256"

				let "ip_1 = $ip_2_tmp - $ip_2 * 256"

				#echo ip = $ip_1.$ip_2.$ip_3.$ip_4
				while [ $index -lt $element_count ]; do
					field=${chunks[$index]}
					if [ "$index" -eq "2" ]; then
						echo -n $ip_1.$ip_2.$ip_3.$ip_4":"
					elif [ "$index" -eq "$element_count_max_limit" ]; then
						echo -n $field
					else
						echo -n $field":"
					fi
					let "index = $index + 1"
				done
				echo 
			done
			)
			;;

		addgw_grp)
			shift
			if [ $# -lt 2 ] ; then
				echo too few paramters
				usage
				exit 1
			fi
			GRP_ID=$2
			cat $GW_GRP_FILE | $EGREP ^$GRP_ID: > /dev/null
			if [ $? -eq 0 ] ; then
				echo -e "\nerror: group already present\n" >  /dev/stderr
				exit 1
			fi
			echo $2:$1 >> $GW_GRP_FILE
			chown_openser $GW_GRP_FILE
			#FIXME: lcr_reload command not supported yet
			#fifo_cmd lcr_reload
			;;

		rmgw_grp)
			shift
			if [ $# -ne 1 ] ; then
				echo missing grp_id to be removed
				exit 1
			fi
			cat $GW_GRP_FILE | $EGREP -v "^$1:" > $GW_GRP_FILE.tmp
			mv -f $GW_GRP_FILE.tmp $GW_GRP_FILE
			chown_openser $GW_GRP_FILE
			#FIXME: lcr_reload command not supported yet
			#fifo_cmd lcr_reload
			;;

		addgw)
			shift
			if [ $# -lt 6 ] ; then
				echo too few paramters
				usage
				exit 1
			fi
			if [ $# -gt 6 ] ; then
				GW_PREFIX=$7
				if [ $# -gt 7 ] ; then
					STRIP=$8
				else
					STRIP=0
				fi
			else
				GW_PREFIX=""
				STRIP=0
			fi
			GW_NAME=$1
			GW_PORT=$3
			GW_URI_SCHEME=$4
			if   [ $GW_URI_SCHEME = 'sip' ]; then
				GW_URI_SCHEME=1
			elif [ $GW_URI_SCHEME = 'sips' ]; then
				GW_URI_SCHEME=2
			fi
			GW_TRANSPORT=$5
			if   [ $GW_TRANSPORT = 'udp' ]; then
				GW_TRANSPORT=1
			elif [ $GW_TRANSPORT = 'tcp' ]; then
				GW_TRANSPORT=2
			elif [ $GW_TRANSPORT = 'tls' ]; then
				GW_TRANSPORT=3
			fi
			GW_GRP_ID=$6
			EXPR_PATH=`which expr`
			oldIFS=$IFS
			IFS=.
			set -- $2
			if [ -z "$EXPR_PATH" ] ; then
				GW_IP=`echo "$(($1+$2*256+$3*65536+$4*16777216))"`
			else
				GW_IP=`$EXPR_PATH $1 + $2 \* 256 + $3 \* 65536 + $4 \* 16777216`
			fi

			# GW_IP=$(($1+$2*256+$3*65536+$4*16777216))
			IFS=$oldIFS
			echo $GW_NAME:$GW_GRP_ID:$GW_IP:$GW_PORT:$GW_URI_SCHEME:$GW_TRANSPORT:$STRIP:$GW_PREFIX >> $GW_FILE
			chown_openser $GW_FILE
			#FIXME: lcr_reload command not supported yet
			#fifo_cmd lcr_reload
			;;

		rmgw)
			shift
			if [ $# -ne 1 ] ; then
				echo missing gateway to be removed
				exit 1
			fi
			cat $GW_FILE | $EGREP -v "^$1:" > $GW_FILE.tmp
			mv -f $GW_FILE.tmp $GW_FILE
			chown_openser $GW_FILE
			#FIXME: lcr_reload command not supported yet
			#fifo_cmd lcr_reload
			;;

		addroute)
			shift
			if [ $# -ne 4 ] ; then
				echo too few paramters
				usage
				exit 1
			fi
			echo $1:$2:$3:$4 >> $LCR_FILE
			chown_openser $LCR_FILE
			#FIXME: lcr_reload command not supported yet
			#fifo_cmd lcr_reload
			;;

		rmroute)
			shift
			if [ $# -ne 4 ] ; then
				echo too few paramters
				usage
				exit 1
			fi
			cat $LCR_FILE | $EGREP -v "^$1:$2:$3:$4\$" > $LCR_FILE.tmp
			mv -f $LCR_FILE.tmp $LCR_FILE
			chown_openser $LCR_FILE
			#FIXME: lcr_reload command not supported yet
			#fifo_cmd lcr_reload
			;;
		*)
			usage
			exit 1
	esac
}

acl() {
	case $1 in
		show)			
			if [ $# -eq 2 ] ; then
				set_user $2
				is_user 
				if [ $? -ne 0 ] ; then
					echo -e "\nNon-existent user. Still proceeding? [Y|N] " > /dev/stderr
					read answer
					if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
						echo -e "\nProceeding with non-local user\n"
					else
						exit 1
					fi
				fi
				EGREP_COND="^$SERUSER:$SERDOMAIN:"
			elif [ $# -ne 1 ] ; then
				usage
				exit 1
			fi
			if [ -z $EGREP_COND ]; then
				EGREP_COND=""
			fi
			echo -e "\nuser*domain**group*lastmodified" | tr "*" "\t"
			cat $ACL_FILE | $EGREP -v "username" | $EGREP "$EGREP_COND" | tr ":" "\t"

			;;

		grant)
			if [ $# -lt 3 ] ; then
				usage
				exit 1
			fi
			set_user $2
			is_user 
			if [ $? -ne 0 ] ; then
				echo -e "\nNon-existent user. Still proceeding? [Y|N] " > /dev/stderr
				read answer
				if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
					echo -e "\nProceeding with non-local user\n"
				else
					exit 1
				fi
			fi
			shift 2
			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
						echo -e "\nInvalid privilege: $1 ignored\n"
						shift
						continue
					fi
				fi

				line="$SERUSER:$SERDOMAIN:$1:$date_now"
				echo $line >> $ACL_FILE
				shift
			done

			$0 acl show $SERUSER@$SERDOMAIN

			;;

		revoke)			
			if [ $# -eq 3 ] ; then
				CLAUSE="$3:"
			elif [ $# -ne 2 ] ; then
				usage
				exit 1
			fi	

			set_user $2

			cat $ACL_FILE | $EGREP -v "^$SERUSER:$SERDOMAIN:$CLAUSE" >> $ACL_FILE.tmp
			mv -f $ACL_FILE.tmp $ACL_FILE
			$0 acl show $2

			;;

		*)
			usage
			exit 1
			;;
	esac
}

# params: user
# output: false if exists, true otherwise
is_user() {
	CNT=`cat $SUB_FILE | $EGREP :$SERUSER:$SERDOMAIN: | wc -w`
	
	if [ "0$CNT" -eq 0 ] ; then
		false
	else
		true
	fi
}


# params: user, password
# output: HA1, HA1B
credentials()
{
	set_user $1

	HA1=`echo -n "$SERUSER:$SERDOMAIN:$2" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		echo -e "\nHA1 calculation failed\n"
		exit 1
	fi
	HA1B=`echo -n "$SERUSER@$SERDOMAIN:$SERDOMAIN:$2" | $MD5 | $AWK '{ print $1 }'`
	if [ $? -ne 0 ] ; then
		echo -e "\nHA1B calculation failed\n"
		exit 1
	fi
}

#================================================================

# if the script calls itself ...
export PW
echo
case $1 in

	passwd)		
		if [ $# -ne 3 ] ; then
			usage
			exit 1
		fi
		shift
		credentials $1 $2

		is_user $1
		if [ $? -ne 0 ] ; then
			echo -e "\nnon-existent user\n"
			exit 1
		fi
		
		line1=`cat $SUB_FILE | $EGREP :$SERUSER:$SERDOMAIN: | head -n 1`
		#9 is the date_created field
		line1_keep1=`echo $line1 | cut -s -d : -f 5,6,7,8,9`
		line1_keep2=`echo $line1 | cut -s -d : -f 11,12,13,14`
		line1_keep3=`echo $line1 | cut -s -d : -f 17,18,19,20,21`
		line2_update="$HA1:$SERUSER:$SERDOMAIN:$2:$line1_keep1:$date_now:$line1_keep2:$HA1:$HA1B:$line1_keep3"
		cat $SUB_FILE | sed s/"$line1"/"$line2_update"/g >> $SUB_FILE.tmp
		mv -f $SUB_FILE.tmp $SUB_FILE
		echo -e "\nPassword change succeeded (DBTEXT)\n"
		;;

	add)
		if [ $# -ne 4 ] ; then
			usage
			exit 1
		fi
		shift
		credentials $1 $2
		is_user $1
		if [ $? -eq 0 ] ; then
			echo -e "\nuser already exists\n"
			exit 1
		fi  

		#Add to SUBSCRIBER file			
		#phplib_id(str) username(str) domain(str) password(str) 
		#first_name(str,null) last_name(str,null) phone(str,null) email_address(str) 
		#datetime_created(int) datetime_modified(int) confirmation(str,null) flag(str,null) sendnotification(str,null) greeting(str,null) ha1(str) ha1b(str) allow_find(str) timezone(str,null) rpid(str,null) domn(int,null) uuid(str,null)
		line="$HA1:$SERUSER:$SERDOMAIN:$2::::$3:$date_now:$date_now::o:::$HA1:$HA1B:0::::"
		echo $line >> $SUB_FILE
			
		#Add to URI file
		#username(str) domain(str) uri_user(str) last_modified(int)
		line="$SERUSER:$SERDOMAIN:$SERUSER:$date_now"
		echo $line >> $URI_FILE
		
		echo -e "\nNew user ($SERUSER@$SERDOMAIN) added to DBTEXT files\n"
		
		;;

	mail)
		if [ $# -ne 2 ] ; then
			usage
			exit 1
		fi
		shift
		set_user $1
		EA=`cat $SUB_FILE | $EGREP :$SERUSER:$SERDOMAIN: | cut -s -d : -f 8 | $LAST_LINE`
		echo -e "\nWrite email to $1: $EA now ...\n"
		mail -s "Message from $SERDOMAIN SIP admin" $EA
		if [ $? -eq 0 ] ; then
			echo -e "\nmessage sent\n"
		else
			echo -e "\nsending message failed\n"
		fi

		;;

	rpid)
		rpid "$@"
		;;

	showdb|userdb)
		shift
		if [ $# -eq 1 ] ; then
			set_user $1
			is_user
			if [ $? -ne 0 ] ; then
				echo -e "\nnon-existent user\n"
				exit 1;
			fi
			#The email field is number 8 ...
			echo -e "\n** In Subscriber Table: ** "
			cat $SUB_FILE | $EGREP :$SERUSER:$SERDOMAIN: | cut -s -d : -f 8
			echo -e "\n** In Location Table: ** "
			cat $UL_FILE | head -n 1 | tr -d "(,)" | sed s/"str\|int\|null"/""/g | tr " " ":"
			cat $UL_FILE | $EGREP "^$SERUSER:($SERDOMAIN)?\B?:" 
		else
			usr_loc=`cat $UL_FILE | cut -s -d : -f 1 | tr '\n' '|' | sed s/'|\$'/''/g`
			if [ -n $usr_loc ]; then
				echo -e "\n** Subscribers (username and email) currently registered: ** " 
				cat $SUB_FILE | $EGREP $usr_loc | cut -s -d : -f 2,8
				echo -e "\n** Deatailed location data available on disk (username, domain, contact, callid): ** " 
				cat $UL_FILE | $EGREP $usr_loc | cut -s -d : -f 1,2,3,8
			fi
		fi
		echo -e "\nNote: Due to usage of cache, server's list " \
			"may differ from DB list.\n"

		;;

	rm)
		if [ $# -ne 2 ] ; then
			usage
			exit 1
		fi
		shift 

		set_user $1
		is_user 
		if [ $? -ne 0 ] ; then
			echo -e "\nnon-existent user\n"
			exit 1
		fi

		# begin with remove all user's privileges
		echo -e "\nACL revoke not implemented for DBTEXT ... rights not removed\n"

		cat $URI_FILE | $EGREP -v "^$SERUSER:" > $URI_FILE.tmp
		mv -f $URI_FILE.tmp $URI_FILE
				
		# destroy the user now
		cat $SUB_FILE | $EGREP -v ":$SERUSER:$SERDOMAIN:" >> $SUB_FILE.tmp
		mv -f $SUB_FILE.tmp $SUB_FILE
	
		# and also all his contacts
		$0 ul rm $1   > /dev/null 2>&1
		echo -e "\nUSER $SERUSER removed\n"
		;;
			
	acl)
		shift
		acl "$@"
		;;

	domain)
		shift
		domain "$@"
		;;

	lcr)
		shift
		lcr "$@"
		;;

	dbtext)
		
		if [ $# -eq 1 ] ; then
			DBTEXT_PATH="/usr/local/etc/openser/dbtext"
		elif [ $# -eq 2 ]; then
			DBTEXT_PATH=$2
		else 
			usage
			exit
		fi
		
		UL_FILE=$DBTEXT_PATH/$UL_TABLE
		DOMAIN_FILE=$DBTEXT_PATH/$DOMAIN_TABLE
		ALS_FILE=$DBTEXT_PATH/$ALS_TABLE
		SUB_FILE=$DBTEXT_PATH/$SUB_TABLE
		URI_FILE=$DBTEXT_PATH/$URI_TABLE
		ACL_FILE=$DBTEXT_PATH/$ACL_TABLE
		LCR_FILE=$DBTEXT_PATH/$LCR_TABLE
		GW_FILE=$DBTEXT_PATH/$GW_TABLE
		GW_GRP_FILE=$DBTEXT_PATH/$GW_GRP_TABLE
		VER_FILE=$DBTEXT_PATH/version
		
		mkdir -p $DBTEXT_PATH
		echo -e "Creating DBText files in $DBTEXT_PATH\n"
		#Subscriber file
		echo "phplib_id(str) username(str) domain(str) password(str) first_name(str,null) last_name(str,null) phone(str,null) email_address(str) datetime_created(int,null) datetime_modified(int,null) confirmation(str,null) flag(str,null) sendnotification(str,null) greeting(str,null) ha1(str) ha1b(str) allow_find(str) timezone(str,null) rpid(str,null) domn(int,null) uuid(str,null)" > $SUB_FILE
		#Location file
		echo "username(str) domain(str,null) contact(str,null) received(str,null) path(str,null) expires(int,null) q(double,null) callid(str,null) cseq(int,null) last_modified(int,null) flags(int) user_agent(str) socket(str) methods(int,null)" > $UL_FILE
		#Aliases file
		echo "username(str) domain(str,null) contact(str,null) received(str,null) path(str,null) expires(int,null) q(double,null) callid(str,null) cseq(int,null) last_modified(int,null) flags(int) user_agent(str) socket(str) methods(int,null)" > $ALS_FILE
		#Uri file
		echo "username(str) domain(str) uri_user(str) last_modified(int)" > $URI_FILE
		#Group/ACL file
		echo "username(str) domain(str) grp(str) last_modified(int)" > $ACL_FILE
		#gw file
		echo "gw_name(str) grp_id(int) ip_addr(int) port(int,null) uri_scheme(int,null) transport(int,null) strip(int) prefix(str,null)" > $GW_FILE
		#lcr file
		echo "prefix(str,null) from_uri(str,null) grp_id(int) priority(int,null)" > $LCR_FILE
		#gw_grp file
		echo "grp_id(int) grp_name(str)" > $GW_GRP_FILE
		#Version file
		echo "table_name(str) table_version(int)" > $VER_FILE
		echo "subscriber:5" >> $VER_FILE
		echo "reserved:1" >> $VER_FILE
		echo "phonebook:1" >> $VER_FILE
		echo "pending:4" >> $VER_FILE
		echo "missed_calls:2" >> $VER_FILE
		echo "location:1003" >> $VER_FILE
		echo "aliases:1003" >> $VER_FILE
		echo "grp:2" >> $VER_FILE
		echo "event:1" >> $VER_FILE
		echo "active_sessions:1" >> $VER_FILE
		echo "acc:2" >> $VER_FILE
		echo "config:1" >> $VER_FILE
		echo "silo:4" >> $VER_FILE
		echo "realm:1" >> $VER_FILE
		echo "domain:1" >> $VER_FILE
		echo "uri:1" >> $VER_FILE
		echo "server_monitoring:1" >> $VER_FILE
		echo "server_monitoring_agg:1" >> $VER_FILE
		echo "trusted:2" >> $VER_FILE
		echo "usr_preferences:2" >> $VER_FILE
		echo "usr_preferences_types:1" >> $VER_FILE
		echo "admin_privileges:1" >> $VER_FILE
		echo "calls_forwarding:1" >> $VER_FILE
		echo "speed_dial:2" >> $VER_FILE
		echo "dbaliases:1" >> $VER_FILE
		echo "gw:3" >> $VER_FILE
		echo "gw_grp:1" >> $VER_FILE
		echo "lcr:2" >> $VER_FILE
		
		echo -e "DBText files created.\n\nRemeber to set the DBTEXT_PATH variable (read usage)\n"
		;;
	
	version)
		echo  -e "\n$0 $VERSION\n"
		;;
		
	*)
		usage
		exit 1
		;;

esac