Browse code

- initial version, a rewrite of the mysql version for postgresql - Currently tested are only the "create" and "drop" commands, thus the other commands are disabled. - I had to modify the tables defintions and I'm not sure if this broke something. Thus please report problems.

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@139 689a6050-402a-0410-94f2-e92a70836424

Klaus Darilion authored on 27/07/2005 09:29:04
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,864 @@
0
+#!/bin/sh
1
+#
2
+# $Id$
3
+#
4
+# Script for adding and dropping ser MySql tables
5
+#
6
+# TO-DO: update_structures command for migriting to new
7
+#        table definitons
8
+# USAGE: call the command without any parameters for info
9
+#
10
+# 2003-01-21 changed SILO table definition, by dcm
11
+#
12
+# History:
13
+# 2003-03-12 added replication mark and state columns to location (nils)
14
+# 2003-03-05: Changed user to username, user is reserved word (janakj)
15
+# 2003-01-26 statistics table introduced (jiri)
16
+# 2003-01-25: Optimized keys of some core tables (janakj)
17
+# 2003-01-25: USER_ID changed to user everywhere (janakj)
18
+# 2003-01-24: Changed realm column of subscriber and pending
19
+#             tables to domain (janakj)
20
+# 2003-04-14  reinstall introduced (jiri)
21
+# 2004-07-05  new definition of table silo (dcm)
22
+# 2005-07-26  modify mysqldb.sh for postgres (darilion), known issues:
23
+#  -  int unsigned replaced by bigint
24
+#  -  postgresql creates some implicit indexes, thus some of the
25
+#     indexes are doubled
26
+#  -  msilo: blob replaced by text, is this fine?
27
+#  -  datetime types not sure
28
+
29
+
30
+# include resource files, if any
31
+if [ -f /etc/ser/.openserpostgresqlrc ]; then
32
+	. /etc/ser/.openserpostgresqlrc
33
+fi
34
+if [ -f /usr/local/etc/openser/.openserpostgresqlrc ]; then
35
+	. /usr/local/etc/openser/.openserpostgresqlrc
36
+fi
37
+if [ -f ~/.openserpostgresqlrc ]; then
38
+	. ~/.openserpostgresqlrc
39
+fi
40
+
41
+#################################################################
42
+# config vars
43
+#################################################################
44
+# name of the database to be used by SER
45
+if [ -z "$DBNAME" ]; then
46
+	DBNAME="openser"
47
+fi
48
+# address of MySQL server
49
+if [ -z "$DBHOST" ]; then
50
+	DBHOST="localhost"
51
+fi
52
+# user with full privileges over DBNAME database
53
+if [ -z "$DBRWUSER" ]; then
54
+	DBRWUSER="openser"
55
+fi
56
+# password user with full privileges over DBNAME database
57
+if [ -z "$DEFAULT_PW" ]; then
58
+	DEFAULT_PW="openserrw"
59
+fi
60
+# read-only user
61
+if [ -z "$DBROUSER" ]; then
62
+	DBROUSER="openserro"
63
+fi
64
+# password for read-only user
65
+if [ -z "$RO_PW" ]; then
66
+	RO_PW="openserro"
67
+fi
68
+# full privileges MySQL user
69
+if [ -z "$SQL_USER" ]; then
70
+	SQL_USER="postgres"
71
+	if [ ! -r ~/.pgpass ]; then
72
+		echo "~./pgpass does not exist, please create this file and support proper credentials for user postgres."
73
+		echo "Note: you need at least postgresql>= 7.3"
74
+		exit 1
75
+	fi
76
+fi
77
+
78
+CMD="psql -h $DBHOST -d template1 -U $SQL_USER "
79
+# the following commands are untested:
80
+#   DUMP_CMD="pg_dump -h $DBHOST -u$SQL_USER -c -t "
81
+#   BACKUP_CMD="mysqldump -h $DBHOST -u$SQL_USER -c "
82
+
83
+# type of sql tables
84
+if [ -z "$TABLE_TYPE" ]; then
85
+	TABLE_TYPE=""
86
+fi
87
+# user name column
88
+if [ -z "$USERCOL" ]; then
89
+	USERCOL="username"
90
+fi
91
+# path to gen_ha1 tool
92
+if [ -z "$GENHA1" ]; then
93
+	GENHA1='/usr/sbin/gen_ha1'
94
+fi
95
+
96
+DUMMY_DATE="2000-01-01 00:00:01"
97
+FOREVER="2020-05-28 21:32:15"
98
+
99
+DEFAULT_ALIASES_EXPIRES=$FOREVER
100
+DEFAULT_Q="1.0"
101
+DEFAULT_CALLID="Default-Call-ID"
102
+DEFAULT_CSEQ="42"
103
+DEFAULT_LOCATION_EXPIRES=$FOREVER
104
+
105
+#################################################################
106
+
107
+
108
+usage() {
109
+COMMAND=`basename $0`
110
+cat <<EOF
111
+usage: $COMMAND create
112
+       $COMMAND drop   (!!entirely deletes tables)
113
+       $COMMAND reinit (!!entirely deletes and than re-creates tables
114
+
115
+ NOTE: the following commands are not tested with postgresql,
116
+ thus they are disabled.
117
+       $COMMAND backup (dumps current database to stdout)
118
+       $COMMAND restore <file> (restores tables from a file)
119
+       $COMMAND copy <new_db> (creates a new db from an existing one)
120
+       $COMMAND reinstall (updates to a new SER database)
121
+
122
+       if you want to manipulate database as other postgresql user than
123
+       "postgres", want to change database name from default value "$DBNAME",
124
+       or want to use other values for users and password, edit the
125
+       "config vars" section of the command $COMMAND
126
+
127
+EOF
128
+} #usage
129
+
130
+
131
+# noot needed for postgresql
132
+## read password
133
+#prompt_pw()
134
+#{
135
+#	savetty=`stty -g`
136
+#	printf "MySql password for $SQL_USER: "
137
+#	stty -echo
138
+#	read PW
139
+#	stty $savetty
140
+#	echo
141
+#}
142
+
143
+# execute sql command
144
+sql_query()
145
+{
146
+	$CMD "$@"
147
+}
148
+
149
+# dump all rows
150
+ser_dump()  # pars: <database name>
151
+{
152
+	if [ $# -ne 1 ] ; then
153
+		echo "ser_dump function takes one param"
154
+		exit 1
155
+	fi
156
+	$DUMP_CMD "-p$PW" $1
157
+}
158
+
159
+
160
+# copy a database to database_bak
161
+ser_backup() # par: <database name>
162
+{
163
+	if [ $# -ne 1 ] ; then
164
+		echo  "ser_backup function takes one param"
165
+		exit 1
166
+	fi
167
+	BU=/tmp/mysql_bup.$$
168
+	$BACKUP_CMD "-p$PW" $1 > $BU
169
+	if [ "$?" -ne 0 ] ; then
170
+		echo "ser backup dump failed"
171
+		exit 1
172
+	fi
173
+	sql_query <<EOF
174
+	create database $1_bak;
175
+EOF
176
+
177
+	ser_restore $1_bak $BU
178
+	if [ "$?" -ne 0 ]; then
179
+		echo "ser backup/restore failed"
180
+		rm $BU
181
+		exit 1
182
+	fi
183
+}
184
+
185
+ser_restore() #pars: <database name> <filename>
186
+{
187
+if [ $# -ne 2 ] ; then
188
+	echo "ser_restore function takes two params"
189
+	exit 1
190
+fi
191
+sql_query $1 < $2
192
+}
193
+
194
+ser_drop()  # pars: <database name>
195
+{
196
+	if [ $# -ne 1 ] ; then
197
+		echo "ser_drop function takes two params"
198
+		exit 1
199
+	fi
200
+
201
+	# postgresql users are not dropped automatically
202
+	DROP_USER="DROP USER \"$DBRWUSER\";
203
+	           DROP USER \"$DBROUSER\";"
204
+
205
+	sql_query << EOF
206
+	drop database $1;
207
+	$DROP_USER
208
+EOF
209
+} #ser_drop
210
+
211
+# read realm
212
+prompt_realm()
213
+{
214
+	printf "Domain (realm) for the default user 'admin': "
215
+	read SIP_DOMAIN
216
+	echo
217
+}
218
+
219
+# calculate credentials for admin
220
+credentials()
221
+{
222
+	HA1=`$GENHA1 admin $SIP_DOMAIN $DEFAULT_PW`
223
+	if [ $? -ne 0 ] ; then
224
+		echo "HA1 calculation failed"
225
+		exit 1
226
+	fi
227
+	HA1B=`$GENHA1 "admin@$SIP_DOMAIN" $SIP_DOMAIN $DEFAULT_PW`
228
+	if [ $? -ne 0 ] ; then
229
+		echo "HA1B calculation failed"
230
+		exit 1
231
+	fi
232
+
233
+  #PHPLIB_ID of users should be difficulty to guess for security reasons
234
+  NOW=`date`;
235
+  PHPLIB_ID=`$GENHA1 "$RANDOM" "$NOW" $SIP_DOMAIN`
236
+	if [ $? -ne 0 ] ; then
237
+    echo "PHPLIB_ID calculation failed"
238
+		exit 1
239
+	fi
240
+}
241
+
242
+ser_create () # pars: <database name> [<no_init_user>]
243
+{
244
+if [ $# -eq 1 ] ; then
245
+	if [ -z "$SIP_DOMAIN" ] ; then
246
+		prompt_realm
247
+	fi
248
+	credentials
249
+	# by default we create initial user
250
+	INITIAL_USER="INSERT INTO subscriber
251
+		($USERCOL, password, first_name, last_name, phone,
252
+		email_address, datetime_created, datetime_modified, confirmation,
253
+    flag, sendnotification, greeting, ha1, domain, ha1b, phplib_id )
254
+		VALUES ( 'admin', '$DEFAULT_PW', 'Initial', 'Admin', '123',
255
+		'root@localhost', '2002-09-04 19:37:45', '$DUMMY_DATE',
256
+		'57DaSIPuCm52UNe54LF545750cfdL48OMZfroM53', 'o', '', '',
257
+		'$HA1', '$SIP_DOMAIN', '$HA1B',
258
+    '$PHPLIB_ID' );
259
+
260
+    INSERT INTO admin_privileges ($USERCOL, domain, priv_name, priv_value)
261
+    VALUES ('admin', '$SIP_DOMAIN', 'is_admin', '1');
262
+
263
+    INSERT INTO admin_privileges ($USERCOL, domain, priv_name, priv_value)
264
+    VALUES ('admin', '$SIP_DOMAIN', 'change_privileges', '1');"
265
+elif [ $# -eq 2 ] ; then
266
+	# if 3rd param set, don't create any initial user
267
+	INITIAL_USER=""
268
+else
269
+	echo "ser_create function takes one or two params"
270
+	exit 1
271
+fi
272
+
273
+# define constants for database definition
274
+USE_CMD='\connect'
275
+GRANT_CMD="CREATE USER $DBRWUSER WITH PASSWORD '$DEFAULT_PW';
276
+	CREATE USER $DBROUSER WITH PASSWORD '$RO_PW';
277
+	GRANT ALL PRIVILEGES ON TABLE version, acc, active_sessions, aliases, dbaliases, grp,
278
+		location, missed_calls, pending, phonebook, subscriber, silo, domain,
279
+		uri, server_monitoring, usr_preferences, trusted, server_monitoring_agg,
280
+		admin_privileges, speed_dial TO $DBRWUSER;
281
+	GRANT SELECT ON TABLE version, acc, active_sessions, aliases, dbaliases, grp,
282
+		location, missed_calls, pending, phonebook, subscriber, silo, domain,
283
+		uri, server_monitoring, usr_preferences, trusted, server_monitoring_agg,
284
+		admin_privileges, speed_dial TO $DBROUSER;"
285
+TIMESTAMP="timestamp NOT NULL DEFAULT NOW()"
286
+DATETIME="TIMESTAMP WITHOUT TIME ZONE NOT NULL default '$DUMMY_DATE'"
287
+DATETIMEALIAS="TIMESTAMP WITHOUT TIME ZONE NOT NULL default '$DEFAULT_ALIASES_EXPIRES'"
288
+DATETIMELOCATION="TIMESTAMP WITHOUT TIME ZONE NOT NULL default '$DEFAULT_LOCATION_EXPIRES'"
289
+FLOAT="NUMERIC(10,2)"
290
+AUTO_INCREMENT="SERIAL PRIMARY KEY"
291
+
292
+echo "creating database $1 ..."
293
+
294
+#cat <<EOF
295
+sql_query <<EOF
296
+create database $1;
297
+$USE_CMD $1;
298
+
299
+/*
300
+ * Table structure versions
301
+ */
302
+
303
+CREATE TABLE version (
304
+   table_name varchar(64) NOT NULL,
305
+   table_version smallint DEFAULT '0' NOT NULL
306
+) $TABLE_TYPE;
307
+
308
+/*
309
+ * Dumping data for table 'version'
310
+ */
311
+
312
+INSERT INTO version VALUES ( 'acc', '2');
313
+INSERT INTO version VALUES ( 'active_sessions', '1');
314
+INSERT INTO version VALUES ( 'admin_privileges', '1');
315
+INSERT INTO version VALUES ( 'aliases', '1001');
316
+INSERT INTO version VALUES ( 'calls_forwarding', '1');
317
+INSERT INTO version VALUES ( 'config', '1');
318
+INSERT INTO version VALUES ( 'dbaliases', '1');
319
+INSERT INTO version VALUES ( 'domain', '1');
320
+INSERT INTO version VALUES ( 'event', '1');
321
+INSERT INTO version VALUES ( 'grp', '2');
322
+INSERT INTO version VALUES ( 'location', '1001');
323
+INSERT INTO version VALUES ( 'missed_calls', '2');
324
+INSERT INTO version VALUES ( 'pending', '4');
325
+INSERT INTO version VALUES ( 'phonebook', '1');
326
+INSERT INTO version VALUES ( 'preferences_types', '1');
327
+INSERT INTO version VALUES ( 'realm', '1');
328
+INSERT INTO version VALUES ( 'reserved', '1');
329
+INSERT INTO version VALUES ( 'server_monitoring', '1');
330
+INSERT INTO version VALUES ( 'server_monitoring_agg', '1');
331
+INSERT INTO version VALUES ( 'silo', '3');
332
+INSERT INTO version VALUES ( 'speed_dial', '1');
333
+INSERT INTO version VALUES ( 'subscriber', '5');
334
+INSERT INTO version VALUES ( 'trusted', '1');
335
+INSERT INTO version VALUES ( 'uri', '1');
336
+INSERT INTO version VALUES ( 'usr_preferences', '1');
337
+
338
+
339
+/*
340
+ * Table structure for table 'acc' -- accounted calls
341
+ */
342
+
343
+CREATE TABLE acc (
344
+  sip_from varchar(128) NOT NULL default '',
345
+  sip_to varchar(128) NOT NULL default '',
346
+  sip_status varchar(128) NOT NULL default '',
347
+  sip_method varchar(16) NOT NULL default '',
348
+  i_uri varchar(128) NOT NULL default '',
349
+  o_uri varchar(128) NOT NULL default '',
350
+  from_uri varchar(128) NOT NULL default '',
351
+  to_uri varchar(128) NOT NULL default '',
352
+  sip_callid varchar(128) NOT NULL default '',
353
+  $USERCOL varchar(64) NOT NULL default '',
354
+  domain varchar(128) NOT NULL default '',
355
+  fromtag varchar(128) NOT NULL default '',
356
+  totag varchar(128) NOT NULL default '',
357
+  time $DATETIME,
358
+  timestamp $TIMESTAMP,
359
+  src varchar(128) default NULL,
360
+  dst varchar(128) default NULL
361
+) $TABLE_TYPE;
362
+
363
+CREATE INDEX acc_user_indx ON acc ($USERCOL, domain);
364
+CREATE INDEX sip_callid_indx ON acc (sip_callid);
365
+
366
+
367
+/*
368
+ * Table structure for table 'active_sessions' -- web stuff
369
+ */
370
+
371
+CREATE TABLE active_sessions (
372
+  sid varchar(32) NOT NULL default '',
373
+  name varchar(32) NOT NULL default '',
374
+  val text,
375
+  changed varchar(14) NOT NULL default '',
376
+  PRIMARY KEY  (name,sid)
377
+) $TABLE_TYPE;
378
+
379
+CREATE INDEX changed_indx ON active_sessions (changed);
380
+
381
+
382
+/*
383
+ * Table structure for table 'admin_privileges' -- multidomain serweb ACL control
384
+ */
385
+
386
+CREATE TABLE admin_privileges (
387
+  $USERCOL varchar(64) NOT NULL default '',
388
+  domain varchar(128) NOT NULL default '',
389
+  priv_name varchar(64) NOT NULL default '',
390
+  priv_value varchar(64) NOT NULL default '',
391
+  PRIMARY KEY  ($USERCOL,priv_name,priv_value,domain)
392
+) $TABLE_TYPE;
393
+
394
+
395
+/*
396
+ * Table structure for table 'aliases' -- location-like table
397
+ * (aliases_contact index makes lookup of missed calls much faster)
398
+ */
399
+
400
+CREATE TABLE aliases (
401
+  $USERCOL varchar(64) NOT NULL default '',
402
+  domain varchar(128) NOT NULL default '',
403
+  contact varchar(255) NOT NULL default '',
404
+  received varchar(255) default NULL,
405
+  expires $DATETIMEALIAS,
406
+  q $FLOAT NOT NULL default '$DEFAULT_Q',
407
+  callid varchar(255) NOT NULL default '$DEFAULT_CALLID',
408
+  cseq int NOT NULL default '$DEFAULT_CSEQ',
409
+  last_modified $TIMESTAMP,
410
+  flags int NOT NULL default '0',
411
+  user_agent varchar(50) NOT NULL default '',
412
+  socket varchar(128) default NULL,
413
+  PRIMARY KEY($USERCOL, domain, contact)
414
+) $TABLE_TYPE;
415
+
416
+CREATE INDEX aliases_contact_indx ON aliases (contact);
417
+
418
+
419
+/*
420
+ * DB aliases
421
+ */
422
+
423
+CREATE TABLE dbaliases (
424
+  alias_username varchar(64) NOT NULL default '',
425
+  alias_domain varchar(128) NOT NULL default '',
426
+  $USERCOL varchar(64) NOT NULL default '',
427
+  domain varchar(128) NOT NULL default '',
428
+  UNIQUE (alias_username,alias_domain)
429
+) $TABLE_TYPE;
430
+
431
+CREATE INDEX alias_user_indx ON dbaliases ($USERCOL, domain);
432
+		  
433
+
434
+/*
435
+ * Table structure for table 'domain' -- domains proxy is responsible for
436
+ */
437
+
438
+CREATE TABLE domain (
439
+  domain varchar(128) NOT NULL default '',
440
+  last_modified $DATETIME,
441
+  PRIMARY KEY  (domain)
442
+) $TABLE_TYPE;
443
+
444
+
445
+/*
446
+ * Table structure for table 'grp' -- group membership
447
+ * table; used primarily for ACLs
448
+ */
449
+
450
+CREATE TABLE grp (
451
+  $USERCOL varchar(64) NOT NULL default '',
452
+  domain varchar(128) NOT NULL default '',
453
+  grp varchar(50) NOT NULL default '',
454
+  last_modified $DATETIME,
455
+  PRIMARY KEY($USERCOL, domain, grp)
456
+) $TABLE_TYPE;
457
+
458
+
459
+/*
460
+ * Table structure for table 'location' -- that is persistent UsrLoc
461
+ */
462
+
463
+CREATE TABLE location (
464
+  $USERCOL varchar(64) NOT NULL default '',
465
+  domain varchar(128) NOT NULL default '',
466
+  contact varchar(255) NOT NULL default '',
467
+  received varchar(255) default NULL,
468
+  expires $DATETIMELOCATION,
469
+  q $FLOAT NOT NULL default '$DEFAULT_Q',
470
+  callid varchar(255) NOT NULL default '$DEFAULT_CALLID',
471
+  cseq int NOT NULL default '$DEFAULT_CSEQ',
472
+  last_modified $TIMESTAMP,
473
+  flags int NOT NULL default '0',
474
+  user_agent varchar(50) NOT NULL default '',
475
+  socket varchar(128) default NULL,
476
+  PRIMARY KEY($USERCOL, domain, contact)
477
+) $TABLE_TYPE;
478
+
479
+
480
+/* 
481
+ * Table structure for table 'missed_calls' -- acc-like table
482
+ * for keeping track of missed calls
483
+ */ 
484
+
485
+CREATE TABLE missed_calls (
486
+  sip_from varchar(128) NOT NULL default '',
487
+  sip_to varchar(128) NOT NULL default '',
488
+  sip_status varchar(128) NOT NULL default '',
489
+  sip_method varchar(16) NOT NULL default '',
490
+  i_uri varchar(128) NOT NULL default '',
491
+  o_uri varchar(128) NOT NULL default '',
492
+  from_uri varchar(128) NOT NULL default '',
493
+  to_uri varchar(128) NOT NULL default '',
494
+  sip_callid varchar(128) NOT NULL default '',
495
+  $USERCOL varchar(64) NOT NULL default '',
496
+  domain varchar(128) NOT NULL default '',
497
+  fromtag varchar(128) NOT NULL default '',
498
+  totag varchar(128) NOT NULL default '',
499
+  time $DATETIME,
500
+  timestamp $TIMESTAMP,
501
+  src varchar(128) default NULL,
502
+  dst varchar(128) default NULL
503
+) $TABLE_TYPE;
504
+
505
+CREATE INDEX mc_user_indx ON missed_calls ($USERCOL, domain);
506
+
507
+
508
+/*
509
+ * Table structure for table 'pending' -- unconfirmed subscribtion
510
+ * requests
511
+ */
512
+
513
+CREATE TABLE pending (
514
+  phplib_id varchar(32) NOT NULL default '',
515
+  $USERCOL varchar(64) NOT NULL default '',
516
+  domain varchar(128) NOT NULL default '',
517
+  password varchar(25) NOT NULL default '',
518
+  first_name varchar(25) NOT NULL default '',
519
+  last_name varchar(45) NOT NULL default '',
520
+  phone varchar(15) NOT NULL default '',
521
+  email_address varchar(50) NOT NULL default '',
522
+  datetime_created $DATETIME,
523
+  datetime_modified $DATETIME,
524
+  confirmation varchar(64) NOT NULL default '',
525
+  flag char(1) NOT NULL default 'o',
526
+  sendnotification varchar(50) NOT NULL default '',
527
+  greeting varchar(50) NOT NULL default '',
528
+  ha1 varchar(128) NOT NULL default '',
529
+  ha1b varchar(128) NOT NULL default '',
530
+  allow_find char(1) NOT NULL default '0',
531
+  timezone varchar(128) default NULL,
532
+  rpid varchar(128) default NULL,
533
+  domn int default NULL,
534
+  uuid varchar(64) default NULL,
535
+  PRIMARY KEY ($USERCOL, domain),
536
+  UNIQUE (phplib_id)
537
+) $TABLE_TYPE;
538
+
539
+CREATE INDEX user_2_pend_indx ON pending ($USERCOL);
540
+
541
+
542
+/*
543
+ * Table structure for table 'phonebook' -- user's phonebook
544
+ */
545
+
546
+CREATE TABLE phonebook (
547
+  id $AUTO_INCREMENT,
548
+  $USERCOL varchar(64) NOT NULL default '',
549
+  domain varchar(128) NOT NULL default '',
550
+  fname varchar(32) NOT NULL default '',
551
+  lname varchar(32) NOT NULL default '',
552
+  sip_uri varchar(128) NOT NULL default ''
553
+) $TABLE_TYPE;
554
+
555
+
556
+/*
557
+ * Table structure for table 'server_monitoring'
558
+ */
559
+
560
+CREATE TABLE server_monitoring (
561
+  time $DATETIME,
562
+  id bigint NOT NULL default '0',
563
+  param varchar(32) NOT NULL default '',
564
+  value int NOT NULL default '0',
565
+  increment int NOT NULL default '0',
566
+  PRIMARY KEY  (id,param)
567
+) $TABLE_TYPE;
568
+
569
+
570
+/*
571
+ * Table structure for table 'server_monitoring_agg'
572
+ */
573
+
574
+CREATE TABLE server_monitoring_agg (
575
+  param varchar(32) NOT NULL default '',
576
+  s_value int NOT NULL default '0',
577
+  s_increment int NOT NULL default '0',
578
+  last_aggregated_increment int NOT NULL default '0',
579
+  av real NOT NULL default '0',
580
+  mv int NOT NULL default '0',
581
+  ad real NOT NULL default '0',
582
+  lv int NOT NULL default '0',
583
+  min_val int NOT NULL default '0',
584
+  max_val int NOT NULL default '0',
585
+  min_inc int NOT NULL default '0',
586
+  max_inc int NOT NULL default '0',
587
+  lastupdate $DATETIME,
588
+  PRIMARY KEY  (param)
589
+) $TABLE_TYPE;
590
+
591
+
592
+/*
593
+ * "instant" message silo
594
+ */
595
+
596
+CREATE TABLE silo(
597
+    mid $AUTO_INCREMENT,
598
+    src_addr VARCHAR(255) NOT NULL DEFAULT '',
599
+    dst_addr VARCHAR(255) NOT NULL DEFAULT '',
600
+    r_uri VARCHAR(255) NOT NULL DEFAULT '',
601
+    username VARCHAR(64) NOT NULL DEFAULT '',
602
+    domain VARCHAR(128) NOT NULL DEFAULT '',
603
+    inc_time INTEGER NOT NULL DEFAULT 0,
604
+    exp_time INTEGER NOT NULL DEFAULT 0,
605
+    ctype VARCHAR(32) NOT NULL DEFAULT 'text/plain',
606
+    body TEXT NOT NULL DEFAULT ''
607
+) $TABLE_TYPE;
608
+
609
+
610
+/*
611
+ * Table structure for table 'speed_dial'
612
+ */
613
+
614
+CREATE TABLE speed_dial (
615
+  username varchar(64) NOT NULL default '',
616
+  domain varchar(128) NOT NULL default '',
617
+  sd_username varchar(64) NOT NULL default '',
618
+  sd_domain varchar(128) NOT NULL default '',
619
+  new_uri varchar(192) NOT NULL default '',
620
+  description varchar(64) NOT NULL default '',
621
+  PRIMARY KEY  (username,domain,sd_domain,sd_username)
622
+) $TABLE_TYPE;
623
+
624
+
625
+/*
626
+ * Table structure for table 'subscriber' -- user database
627
+ */
628
+
629
+CREATE TABLE subscriber (
630
+  phplib_id varchar(32) NOT NULL default '',
631
+  $USERCOL varchar(64) NOT NULL default '',
632
+  domain varchar(128) NOT NULL default '',
633
+  password varchar(25) NOT NULL default '',
634
+  first_name varchar(25) NOT NULL default '',
635
+  last_name varchar(45) NOT NULL default '',
636
+  phone varchar(15) NOT NULL default '',
637
+  email_address varchar(50) NOT NULL default '',
638
+  datetime_created $DATETIME,
639
+  datetime_modified $DATETIME,
640
+  confirmation varchar(64) NOT NULL default '',
641
+  flag char(1) NOT NULL default 'o',
642
+  sendnotification varchar(50) NOT NULL default '',
643
+  greeting varchar(50) NOT NULL default '',
644
+  ha1 varchar(128) NOT NULL default '',
645
+  ha1b varchar(128) NOT NULL default '',
646
+  allow_find char(1) NOT NULL default '0',
647
+  timezone varchar(128) default NULL,
648
+  rpid varchar(128) default NULL,
649
+  domn int default NULL,
650
+  uuid varchar(64) default NULL,
651
+  nickname varchar(64) NOT NULL default '',
652
+  UNIQUE (phplib_id),
653
+  PRIMARY KEY ($USERCOL, domain)
654
+) $TABLE_TYPE;
655
+
656
+CREATE INDEX user_2_subs_indx ON subscriber ($USERCOL);
657
+
658
+
659
+/*
660
+ * Table structure for table trusted
661
+ */
662
+
663
+CREATE TABLE trusted (
664
+  src_ip varchar(39) NOT NULL,
665
+  proto varchar(4) NOT NULL,
666
+  from_pattern varchar(64) NOT NULL,
667
+  PRIMARY KEY (src_ip, proto, from_pattern)
668
+) $TABLE_TYPE;
669
+
670
+
671
+/*
672
+ * Table structure for table 'uri' -- uri user parts users are allowed to use
673
+ */
674
+
675
+CREATE TABLE uri (
676
+  $USERCOL varchar(64) NOT NULL default '',
677
+  domain varchar(128) NOT NULL default '',
678
+  uri_user varchar(50) NOT NULL default '',
679
+  last_modified $DATETIME,
680
+  PRIMARY KEY ($USERCOL, domain, uri_user)
681
+) $TABLE_TYPE;
682
+
683
+
684
+/*
685
+ * Table structure for table 'usr_preferences'
686
+ */
687
+
688
+CREATE TABLE usr_preferences (
689
+  uuid varchar(64) NOT NULL default '',
690
+  $USERCOL varchar(100) NOT NULL default '0',
691
+  domain varchar(128) NOT NULL default '',
692
+  attribute varchar(32) NOT NULL default '',
693
+  type int NOT NULL default '0',
694
+  value varchar(128) NOT NULL default '',
695
+  modified $TIMESTAMP,
696
+  PRIMARY KEY  (attribute,$USERCOL,domain)
697
+) $TABLE_TYPE;
698
+
699
+
700
+/* add an admin user "admin" with password==$DEFAULT_PW,
701
+ * so that one can try it out on quick start
702
+ */
703
+
704
+$INITIAL_USER
705
+
706
+/*
707
+ * GRANT permissions
708
+ */
709
+
710
+$GRANT_CMD
711
+
712
+EOF
713
+
714
+} # ser_create
715
+
716
+
717
+#export PW
718
+#if [ "$#" -ne 0 ]; then
719
+#  prompt_pw
720
+#fi
721
+
722
+case $1 in
723
+#	reinstall)
724
+#
725
+#		#1 create a backup database (named *_bak)
726
+#		echo "creating backup database"
727
+#		ser_backup $DBNAME
728
+#		if [ "$?" -ne 0 ] ; then
729
+#			echo "reinstall: ser_backup failed"
730
+#			exit 1
731
+#		fi
732
+#		#2 dump original database and change names in it
733
+#		echo "dumping table content ($DBNAME)"
734
+#		tmp_file=/tmp/ser_mysql.$$
735
+#		ser_dump $DBNAME  > $tmp_file
736
+#		if [ "$?" -ne 0 ] ; then
737
+#			echo "reinstall: dumping original db failed"
738
+#			exit 1
739
+#		fi
740
+#		sed "s/[uU][sS][eE][rR]_[iI][dD]/user/g" $tmp_file |
741
+#			sed "s/[uU][sS][eE][rR]\($\|[^a-zA-Z]\)/$USERCOL\1/g" |
742
+#			sed "s/[rR][eE][aA][lL][mM]/domain/g"> ${tmp_file}.2
743
+#		#3 drop original database
744
+#		echo "dropping table ($DBNAME)"
745
+#		ser_drop $DBNAME
746
+#		if [ "$?" -ne 0 ] ; then
747
+#			echo "reinstall: dropping table failed"
748
+#			rm $tmp_file*
749
+#			exit 1
750
+#		fi
751
+#		#4 change names in table definition and restore
752
+#		echo "creating new structures"
753
+#		ser_create $DBNAME no_init_user
754
+#		if [ "$?" -ne 0 ] ; then
755
+#			echo "reinstall: creating new table failed"
756
+#			rm $tmp_file*
757
+#			exit 1
758
+#		fi
759
+#		#5 restoring table content
760
+#		echo "restoring table content"
761
+#
762
+#		# Recreate perms column here so that subsequent
763
+#		# restore succeeds
764
+#
765
+#    sql_query $DBNAME << EOF
766
+#    ALTER TABLE subscriber ADD perms VARCHAR(32)  AFTER ha1b;
767
+#    ALTER TABLE pending ADD perms VARCHAR(32)  AFTER ha1b;
768
+#EOF
769
+#
770
+#
771
+#		ser_restore $DBNAME ${tmp_file}.2
772
+#		if [ "$?" -ne 0 ] ; then
773
+#			echo "reinstall: restoring table failed"
774
+#			rm $tmp_file*
775
+#			exit 1
776
+#		fi
777
+#
778
+#
779
+#    sql_query $DBNAME << EOF
780
+#
781
+#    # Move perms from subscriber to admin_privileges
782
+#    INSERT INTO admin_privileges ($USERCOL, domain, priv_name, priv_value) SELECT $USERCOL, domain, 'is_admin', '1' FROM subscriber WHERE perms='admin';
783
+#
784
+#		# Drop perms column here
785
+#    ALTER TABLE subscriber DROP perms;
786
+#    ALTER TABLE pending DROP perms;
787
+#
788
+#EOF
789
+#
790
+##XX
791
+##		rm $tmp_file*
792
+#		exit 0
793
+#		;;
794
+#	copy)
795
+#		# copy database to some other name
796
+#		shift
797
+#		if [ $# -ne 1 ]; then
798
+#			usage
799
+#			exit 1
800
+#		fi
801
+#		tmp_file=/tmp/ser_mysql.$$
802
+#		ser_dump $DBNAME  > $tmp_file
803
+#		ret=$?
804
+#		if [ "$ret" -ne 0 ]; then
805
+#			rm $tmp_file
806
+#			exit $ret
807
+#		fi
808
+#		ser_create $1 no_init_user
809
+#		ret=$?
810
+#		if [ "$ret" -ne 0 ]; then
811
+#			rm $tmp_file
812
+#			exit $ret
813
+#		fi
814
+#		ser_restore $1 $tmp_file
815
+#		ret=$?
816
+#		rm $tmp_file
817
+#		exit $ret
818
+#		;;
819
+#	backup)
820
+#		# backup current database
821
+#		ser_dump $DBNAME
822
+#		exit $?
823
+#		;;
824
+#	restore)
825
+#		# restore database from a backup
826
+#		shift
827
+#		if [ $# -ne 1 ]; then
828
+#			usage
829
+#			exit 1
830
+#		fi
831
+#		ser_restore $DBNAME $1
832
+#		exit $?
833
+#		;;
834
+	create)
835
+		# create new database structures
836
+		shift
837
+		if [ $# -eq 1 ] ; then
838
+			DBNAME="$1"
839
+		fi
840
+		ser_create $DBNAME
841
+		exit $?
842
+		;;
843
+	drop)
844
+		# delete ser database
845
+		ser_drop $DBNAME
846
+		exit $?
847
+		;;
848
+	reinit)
849
+		# delete database and create a new one
850
+		ser_drop $DBNAME
851
+		ret=$?
852
+		if [ "$ret" -ne 0 ]; then
853
+			exit $ret
854
+		fi
855
+		ser_create $DBNAME
856
+		exit $?
857
+		;;
858
+	*)
859
+		usage
860
+		exit 1;
861
+		;;
862
+esac
863
+