Browse code

added aliases and granting multiple rights with 1 command

Jiri Kuthan authored on 15/06/2002 22:47:01
Showing 1 changed files
... ...
@@ -22,43 +22,134 @@ GENHA1='gen_ha1'
22 22
 MYSQL='mysql'
23 23
 SER='sr'
24 24
 
25
-# SQL names
25
+#### SQL names
26
+
27
+# Usr Loc Table
28
+USRLOC=location
26 29
 USER_COLUMN=user
30
+
31
+# subscriber table
32
+TABLE=subscriber
27 33
 REALM_COLUMN=realm
28 34
 HA1_COLUMN=HA1
29 35
 HA1B_COLUMN=HA1B
30
-# subscriber table
31
-TABLE=subscriber
32
-# Usr Loc Table
33
-USRLOC=location
34 36
 PASSWORD_COLUMN=password
35 37
 SUBSCRIBER_COLUMN='user_id'
36 38
 EMAIL_COLUMN=email_address
39
+SUB_CREATED_COLUMN=datetime_created
40
+SUB_MODIFIED_COLUMN=datetime_modified
41
+
37 42
 # acl table
38 43
 ACL_TABLE=grp
39 44
 ACL_USER_COLUMN=user
40 45
 ACL_GROUP_COLUMN=grp
46
+ACL_MODIFIED_COLUMN=last_modified
47
+
48
+# aliases table
49
+A_TABLE=aliases
50
+A_USER_COLUMN=user
51
+A_CONTACT_COLUMN=contact
52
+A_EXPIRES_COLUMN=expires
53
+A_Q_COLUMN=q
54
+A_CALLID_COLUMN=callid
55
+A_CSEQ_COLUMN=cseq
56
+A_LAST_MODIFIED_COLUMN=last_modified
41 57
 
58
+#===================================================================
42 59
 
43 60
 
44 61
 usage() {
45 62
 	echo "usage: "
46 63
 	echo -n " `basename $0` add <name> <password> <email> "
47 64
 	echo "... add a new subscriber (*)"
48
-	echo " `basename $0` dul <name> ... delete user's UsrLoc entries (*)"
49
-	echo " `basename $0` show [<name>] ... show online users"
50
-	echo " `basename $0` mail <name> ... send an email to a user"
51
-	echo " `basename $0` rm <name> ... delete a user (*)"
52
-	echo " `basename $0` passwd <user> <passwd> ... change user's password (*)"
53
-	echo " `basename $0` stat ... show runnig processes + $0 show"
54
-	echo " `basename $0` acl show [<user>] ... show user membership"
55
-	echo " `basename $0` acl grant <user> <group> ... grant user memberhsip (*)"
56
-	echo " `basename $0` acl revoke <user> [<group>] ... grant user memberhsip(s) (*)"
65
+	echo " `basename $0` dul <name> - delete user's UsrLoc entries (*)"
66
+	echo " `basename $0` show [<name>] - show online users"
67
+	echo " `basename $0` mail <name> - send an email to a user"
68
+	echo " `basename $0` rm <name> - delete a user (*)"
69
+	echo " `basename $0` passwd <user> <passwd> - change user's password (*)"
70
+	echo " `basename $0` stat - show runnig processes + $0 show"
71
+	echo " `basename $0` acl show [<user>] - show user membership"
72
+	echo " `basename $0` acl grant <user> <group>... - grant user membership (*)"
73
+	echo " `basename $0` acl revoke <user> [<group>] - grant user membership(s) (*)"
74
+	echo " `basename $0` alias show [<alias>] - show aliases"
75
+	echo " `basename $0` alias rm <alias> - remove an alias"
76
+	echo " `basename $0` alias add <alias> <uri> - show aliases"
57 77
 	echo 
58 78
 	echo "   commands labeled with (*) will prompt for a MySQL password"
79
+	echo "   if the variable PW is set, the password will not be prompted"
59 80
 	echo
60 81
 }
61 82
 
83
+#params: none
84
+# output: PW
85
+prompt_pw() {
86
+    read -s -p "MySql Password: " PW
87
+    echo
88
+}
89
+
90
+
91
+# input: sql query, optional mysql command-line params
92
+sql_query() {
93
+	# if password not yet queried, query it now
94
+	if [ -z "$PW" ] ; then
95
+		read -s -p "MySql Password: " PW >&2
96
+		echo >&2
97
+	fi
98
+	$MYSQL $2 -h $SQL_HOST -u $SQL_USER "-p$PW" -e "$1 ;" $SQL_DB
99
+}
100
+
101
+# input: sql query, optional mysql command-line params
102
+sql_ro_query() {
103
+	$MYSQL $2 -h $SQL_HOST -u $RO_USER "-p$RO_PW" \
104
+		-e "$1 ;" $SQL_DB
105
+}
106
+
107
+
108
+ser_alias() {
109
+	case $1 in 
110
+		show)
111
+			if [ $# -eq 2 ] ; then
112
+				CLAUSE=" WHERE $A_USER_COLUMN='$2' "
113
+			elif [ $# -ne 1 ] ; then
114
+				usage
115
+				exit 1
116
+			fi
117
+			QUERY="select * FROM $A_TABLE $CLAUSE ; "
118
+			sql_ro_query "$QUERY"
119
+			;;
120
+		add)
121
+			if [ $# -ne 3 ] ; then
122
+				usage
123
+				exit 1
124
+			fi
125
+			QUERY="insert into $A_TABLE \
126
+				($A_USER_COLUMN, $A_CONTACT_COLUMN, $A_EXPIRES_COLUMN, \
127
+				$A_Q_COLUMN, $A_CALLID_COLUMN, $A_CSEQ_COLUMN, \
128
+				$A_LAST_MODIFIED_COLUMN) \
129
+				values ( '$2', '$3', '2099-05-28 21:32:15', \
130
+				'1.00', 'call-id-for-ever', '1',
131
+				now() );"
132
+			sql_query "$QUERY"
133
+			;;
134
+		rm)
135
+            if [ $# -ne 2 ] ; then
136
+                usage
137
+                exit 1
138
+            fi
139
+
140
+            QUERY="delete from $A_TABLE where \
141
+                $A_TABLE.$A_USER_COLUMN='$2' "
142
+			sql_query "$QUERY"
143
+
144
+            ;;
145
+
146
+		*)
147
+			usage
148
+			exit 1
149
+			;;
150
+	esac
151
+}
152
+
62 153
 acl() {
63 154
 	case $1 in
64 155
 		show)
... ...
@@ -69,21 +160,31 @@ acl() {
69 160
 				exit 1
70 161
 			fi
71 162
 			QUERY="select * FROM $ACL_TABLE $CLAUSE ; "
72
-			$MYSQL -h $SQL_HOST -u $RO_USER "-p$RO_PW" \
73
-				-e "$QUERY ;" $SQL_DB
163
+			sql_ro_query "$QUERY"
74 164
 
75 165
 			;;
76 166
 
77 167
 		grant)
78
-			if [ $# -ne 3 ] ; then
168
+			if [ $# -lt 3 ] ; then
79 169
 				usage
80 170
 				exit 1
81 171
 			fi
82
-
83
-        	$MYSQL -h $SQL_HOST -u $SQL_USER -p$PW \
84
-            	-e "use $SQL_DB; insert into $ACL_TABLE \
85
-                ($ACL_USER_COLUMN,$ACL_GROUP_COLUMN) \
86
-                values ('$2','$3');";
172
+			SIP_USER="$2"
173
+			shift 2
174
+			while [ $# -gt 0 ] ; do
175
+
176
+        		QUERY="insert into $ACL_TABLE \
177
+                	($ACL_USER_COLUMN,$ACL_GROUP_COLUMN,$ACL_MODIFIED_COLUMN) \
178
+                	values ('$SIP_USER','$1', now());"
179
+				sql_query "$QUERY"
180
+				if [ $? -ne 0 ] ; then
181
+					echo "SQL Error"
182
+					exit 1
183
+				fi
184
+				shift
185
+			done
186
+
187
+			$0 acl show $SIP_USER
87 188
 
88 189
 			;;
89 190
 
... ...
@@ -97,7 +198,9 @@ acl() {
97 198
 
98 199
 			QUERY="delete from $ACL_TABLE where \
99 200
 				$ACL_TABLE.$ACL_USER_COLUMN='$2' $CLAUSE"
100
-			$MYSQL -h $SQL_HOST -u $SQL_USER "-p$PW" -e "$QUERY ;" $SQL_DB
201
+			sql_query "$QUERY"
202
+
203
+			$0 acl show $2
101 204
 
102 205
 			;;
103 206
 
... ...
@@ -111,10 +214,9 @@ acl() {
111 214
 # params: user
112 215
 # output: false if exists, true otherwise
113 216
 is_user() {
114
-	CNT=`$MYSQL -B -h $SQL_HOST -u $SQL_USER -p$PW \
115
-		-e "use $SQL_DB; select count(*) from $TABLE \
116
-		where $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$DOMAIN';" \
117
-		| grep -v ERROR | tail -n1`
217
+	QUERY="select count(*) from $TABLE \
218
+		where $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$DOMAIN';"
219
+	CNT=`sql_query "$QUERY" | grep -v ERROR | tail -n1`
118 220
 	if [ "0$CNT" -eq 0 ] ; then
119 221
 		false
120 222
 	else
... ...
@@ -123,12 +225,6 @@ is_user() {
123 225
 
124 226
 }
125 227
 
126
-#params: none
127
-# output: PW
128
-prompt_pw() {
129
-	read -s -p "MySql Password: " PW
130
-	echo 
131
-}
132 228
 
133 229
 # params: user, password
134 230
 # output: HA1, HA1B
... ...
@@ -156,18 +252,19 @@ case $1 in
156 252
 			exit 1
157 253
 		fi
158 254
 		shift
159
-		credentials
160
-
255
+		credentials $1 $2
161 256
 		prompt_pw
257
+
162 258
 		is_user $1
163 259
 		if [ $? -ne 0 ] ; then
164 260
 			echo non-existent user
165 261
 			exit 1
166 262
 		fi
167
-		$MYSQL -h $SQL_HOST -u $SQL_USER -p$PW \
168
-			-e "use $SQL_DB; update $TABLE \
263
+		QUERY="update $TABLE \
169 264
 			set $HA1_COLUMN='$HA1', $HA1B_COLUMN='$HA1B', $PASSWORD_COLUMN='$2' \
265
+			, $SUB_MODIFIED_COLUMN=now() \
170 266
 			WHERE $SUBSCRIBER_COLUMN='$1' and $REALM_COLUMN='$DOMAIN';"
267
+		sql_query "$QUERY"
171 268
 		if [ $? -ne 0 ] ; then
172 269
 			echo "password change failed"
173 270
 		else
... ...
@@ -184,7 +281,7 @@ case $1 in
184 281
 			exit 1
185 282
 		fi
186 283
 		shift
187
-		credentials
284
+		credentials $1 $2
188 285
 		prompt_pw
189 286
         is_user $1
190 287
         if [ $? -eq 0 ] ; then
... ...
@@ -192,11 +289,11 @@ case $1 in
192 289
             exit 1
193 290
         fi
194 291
 
195
-		$MYSQL -h $SQL_HOST -u $SQL_USER -p$PW \
196
-			-e "use $SQL_DB; insert into $TABLE \
292
+		QUERY="insert into $TABLE \
197 293
 				($SUBSCRIBER_COLUMN,$REALM_COLUMN,$HA1_COLUMN,\
198
-				$HA1B_COLUMN,$PASSWORD_COLUMN,$EMAIL_COLUMN) \
199
-				values ('$1','$DOMAIN','$HA1','$HA1B','$2', '$3');";
294
+				$HA1B_COLUMN,$PASSWORD_COLUMN,$EMAIL_COLUMN, $SUB_CREATED_COLUMN) \
295
+				values ('$1','$DOMAIN','$HA1','$HA1B','$2', '$3', now() );";
296
+		sql_query "$QUERY"
200 297
 		if [ $? -ne 0 ] ; then
201 298
 			echo "introducing a new user to the database failed"
202 299
 		else
... ...
@@ -211,15 +308,15 @@ case $1 in
211 308
 			exit 1
212 309
 		fi
213 310
 		prompt_pw
214
-		CNT=`$MYSQL -B -h $SQL_HOST -u $SQL_USER -p$PW \
215
-			 -e "use $SQL_DB; select count(*) from $USRLOC \
216
-			where $USRLOC.$USER_COLUMN='$2';" | grep -v ERROR | tail -n1`
311
+		QUERY="select count(*) from $USRLOC \
312
+			where $USRLOC.$USER_COLUMN='$2';"
313
+		CNT=`sql_query "$QUERY" "-B" | grep -v ERROR | tail -n1`
217 314
 		if [ "0$CNT" -eq 0 ] ; then
218 315
 			echo Nothing to delete
219 316
 			exit 1
220 317
 		fi
221 318
 		QUERY="delete from $USRLOC where $USRLOC.$USER_COLUMN='$2'"
222
-		$MYSQL -h $SQL_HOST -u $SQL_USER "-p$PW" -e "$QUERY ;" $SQL_DB
319
+		sql_query "$QUERY"
223 320
 		if [ $? -ne 0 ] ; then
224 321
 			echo "database operation failed"
225 322
 		else
... ...
@@ -236,8 +333,7 @@ case $1 in
236 333
 		shift
237 334
 		QUERY="select $TABLE.$EMAIL_COLUMN from $TABLE where  \
238 335
 			$TABLE.$SUBSCRIBER_COLUMN='$1'"
239
-		EA=`$MYSQL -B -h $SQL_HOST -u $RO_USER "-p$RO_PW" \
240
-			-e "$QUERY ;" $SQL_DB| grep -v ERROR | tail -n1`
336
+		EA=`sql_ro_query "$QUERY" "-B" | grep -v ERROR | tail -n1`
241 337
 		if [ $? -ne 0 ] ; then
242 338
 			echo "MySql query failed"
243 339
 			exit 1
... ...
@@ -270,10 +366,10 @@ case $1 in
270 366
 			$TABLE.$SUBSCRIBER_COLUMN=$USRLOC.$USER_COLUMN" 
271 367
 
272 368
 		if [ $# -eq 1 ] ; then
273
-			$MYSQL -h $SQL_HOST -u $RO_USER "-p$RO_PW" -e "$QUERY1 ;" $SQL_DB
274
-			$MYSQL -h $SQL_HOST -u $RO_USER "-p$RO_PW" -e "$QUERY2 ;" $SQL_DB
369
+			sql_ro_query "$QUERY1"
370
+			sql_ro_query "$QUERY2"
275 371
 		else
276
-			$MYSQL -h $SQL_HOST -u $RO_USER "-p$RO_PW" -e "$QUERY3 ;" $SQL_DB
372
+			sql_ro_query "$QUERY3"
277 373
 		fi
278 374
 		echo -n "Note: Due to usage of cache, server's list "
279 375
 		echo "may differ from DB list."
... ...
@@ -286,7 +382,8 @@ case $1 in
286 382
             exit 1
287 383
         fi
288 384
 		shift 
289
-		prompt_pw
385
+		prompt_pw 
386
+
290 387
         is_user $1
291 388
         if [ $? -ne 0 ] ; then
292 389
             echo non-existent user
... ...
@@ -294,7 +391,7 @@ case $1 in
294 391
         fi
295 392
 
296 393
         QUERY="delete from $TABLE where $TABLE.$SUBSCRIBER_COLUMN='$1'"
297
-        $MYSQL -h $SQL_HOST -u $SQL_USER "-p$PW" -e "$QUERY ;" $SQL_DB
394
+		sql_query "$QUERY"
298 395
         ;;
299 396
 			
300 397
 	stat)
... ...
@@ -311,6 +408,11 @@ case $1 in
311 408
 		shift
312 409
 		acl "$@"
313 410
 		;;
411
+
412
+	alias)
413
+		shift
414
+		ser_alias "$@"
415
+		;;
314 416
 		
315 417
 	*)
316 418
 		usage