This file is indexed.

/usr/lib/x86_64-linux-gnu/kamailio/kamctl/kamdbctl.db_berkeley is in kamailio-berkeley-modules 4.2.0-2+deb8u3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#
# Script for maintaining Kamailio Berkeley DB tables
# Copyright (C) 2007 Cisco Systems
#
# This file is part of Kamailio, a free SIP server.
#
# Kamailio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version
#
# Kamailio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# 
# History:
# --------
# 2007-09-19  genesis (wiquan)
#

#constants
PATH=$PATH:/usr/local/BerkeleyDB.4.6/bin
DELIM="|"
BACKUP_CMD="tar czvf "
RESTORE_CMD="tar xzvf "

#berkeley db utility program that writes out db to plain text
#small hack to autodetect the db dump command, debian prefix the version..

which db_dump > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	DUMP_CMD="db_dump"
fi ;

which db4.4_dump > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	DUMP_CMD="db4.4_dump"
fi ;

which db4.5_dump > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	DUMP_CMD="db4.5_dump"
fi ;

which db4.6_dump > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	DUMP_CMD="db4.6_dump"
fi ;


which db5.0_dump > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	DUMP_CMD="db5.0_dump"
fi ;

which db5.1_dump > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	DUMP_CMD="db5.1_dump"
fi ;


#berkeley db utility program that imports data from plain text file
#small hack to autodetect the db load command, debian prefix the version..

which db_load > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	LOAD_CMD="db_load"
fi ;

which db4.4_load > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	LOAD_CMD="db4.4_load"
fi ;

which db4.5_load > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	LOAD_CMD="db4.5_load"
fi ;

which db4.6_load > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	LOAD_CMD="db4.6_load"
fi ;


which db5.0_load > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	LOAD_CMD="db5.0_load"
fi ;

which db5.1_load > /dev/null
ret=$?
if [ $ret -eq 0 ] ; then
	LOAD_CMD="db5.1_load"
fi ;

# path to the database schemas
DATA_DIR="/usr/share/kamailio/"
if [ -d "$DATA_DIR/db_berkeley/kamailio" ] ; then
	DB_SCHEMA="$DATA_DIR/db_berkeley/kamailio"
else
	DB_SCHEMA="./db_berkeley/kamailio"
fi

# path to the db_berkeley database
if [ -z "$DB_PATH" ]; then
	DB_PATH="/usr/local/etc/kamailio/db_berkeley"
fi

berkeley_usage() 
{
COMMAND=`basename $0`
cat <<EOF
Script for maintaining Kamailio Berkeley DB tables
       $COMMAND list      (lists the underlying db files in DB_PATH)
       $COMMAND cat       <db>  (db_dump the underlying db file to STDOUT)
       $COMMAND swap      <db>  (installs db.new by db -> db.old; db.new -> db)
       $COMMAND append    <db> <datafile> (appends data to an existing db;output DB_PATH/db.new)
       $COMMAND newappend <db> <datafile> (appends data to a new instance of db; output DB_PATH/db.new)
       $COMMAND export  <dump_dir> (exports table data to plain-txt files in dump_dir)
       $COMMAND import  <dump_dir> (imports plain-txt table data and creates new db tables in db_path)
EOF
} #usage


#
# 
#
kamailio_berkeley()  # parms: <op> <arg1> <arg2>
{
	case $1 in
		list|ls)
			ls -l $DB_PATH
			exit $?
			;;
		cat)
			shift
			kamailio_cat $1 $DB_PATH
			exit $?
			;;

		swap)
			shift
			kamailio_swap $1 $DB_PATH
			exit $?
			;;

		append)
			shift
			kamailio_append  $1 $2 $DB_PATH
			exit $?
			;;

		newappend)
			shift
			kamailio_newappend  $1 $2 $DB_PATH
			exit $?
			;;

		export)
			shift
			kamailio_export  $1 $DB_PATH
			exit $?
			;;

		migrate)
			shift
			kamailio_migrate $1 $DB_PATH
			exit $?
			;;

		import)
			shift
			kamailio_import  $1 $DB_PATH
			exit $?
			;;

		*)
			berkeley_usage
			exit 1;
			;;
esac
}



##
# EXPORT existing data to plain-txt files in DUMP_DIR
# eg.  DB_PATH/version ---> DUMP_DIR/version.txt
#
# Export is used as part of a DB migration process to another 
# major version of berkeley db.
kamailio_export()  # parms: <DUMP_DIR> [DB_PATH]
{
	if [ $# -lt 2  ]; then
		echo  "kamailio_dump parms: <DUMP_DIR> [DB_PATH]"
		exit 1
	fi
	
	# Assert: the DB_PATH directory should already exist
	if [ ! -d $2 ] ; then
		merr "BerkeleyDB directory does not exist at: [$2]"
		exit 1
	fi
	
	# Assert: DB_PATH directory should already contain table 'version'
	if [ ! -f $2/version ] ; then
		merr "BerkeleyDB directory does not have VERSION table at: [$2]"
		exit 1
	fi
	
	# Create dir at <DUMP_DIR> to store the exported data
	if [ ! -d $1 ] ; then
		minfo "creating DUMP_DIR at: [$1]"
		mkdir -p $1
	else
		mdbg "Cleaning out DUMP_DIR to get ready for new data"
		rm -rf $1/*
	fi
	
	# DUMP_CMD will result in something like this:
	#
	#	VERSION=3
	#	format=print
	#	type=hash
	#	h_nelem=2
	#	db_pagesize=4096
	#	HEADER=END
	#	 METADATA_COLUMNS
	#	 callid(str) method(str) from_tag(str) to_tag(str) sip_code(str) sip_reason(str) time(datetime)
	#	 METADATA_KEY
	#	 0
	#	DATA=END
	#
	# However, we are only interested in the indented stuff between 
	#  'HEADER=END' and 'DATA=END',
	#  as everything else is DB instance specific. That is, we are interested in this part:
	#
	# METADATA_COLUMNS
	# callid(str) method(str) from_tag(str) to_tag(str) sip_code(str) sip_reason(str) time(datetime)
	# METADATA_KEY
	# 0
	#
	# The following PERL filter will do this processing.
	#
	# perl -pe 's/^\w.*// ; s/^\s(.*)/$1/'

	# Dump the STANDARD tables to plain-text files in DUMP_DIR
	for TABLE in $STANDARD_TABLES; do
	    if [ -f $2/$TABLE ] ; then
		    mdbg "Exporting standard table: $TABLE"
		    $DUMP_CMD -p -h $2 $TABLE  | perl -pe 's/^\w.*// ; s/^\s(.*)/$1/' > $1/$TABLE.txt
		    
		    # Check return code to make sure the export worked ok
		    if [ $? -ne 0 ] ; then
			merr "Export of standard table failed [$TABLE]"
			# there was a problem, but it is not something
			# we can handle here; We can deal with this at import
			# time.
		    fi
	    else
	    	    mwarn "Table not found: [$TABLE]"
	    fi
	done
	
	# Dump the PRESENCE tables to plain-text files in DUMP_DIR
	for TABLE in $PRESENCE_TABLES; do
	    if [ -f $2/$TABLE ] ; then
		    mdbg "Exporting presence table: $TABLE"
		    $DUMP_CMD -p -h $2 $TABLE  | perl -pe 's/^\w.*// ; s/^\s(.*)/$1/' > $1/$TABLE.txt
		    if [ $? -ne 0 ] ; then
			merr "Export of presence table failed [$TABLE]"
		    fi
	    else
	    	    mwarn "Table not found: [$TABLE]"
	    fi
	done
	
	# Dump the EXTRA tables to plain-text files in DUMP_DIR
	for TABLE in $EXTRA_TABLES; do
	    if [ -f $2/$TABLE ] ; then
		    mdbg "Exporting extra table: $TABLE"
		    $DUMP_CMD -p -h $2 $TABLE  | perl -pe 's/^\w.*// ; s/^\s(.*)/$1/' > $1/$TABLE.txt
		    if [ $? -ne 0 ] ; then
			merr "Export of extra table failed [$TABLE]"
		    fi
	    else
	    	    mwarn "Table not found: [$TABLE]"
	    fi
	done
	
	mdbg "All tables are now exported to DUMP_DIR: [$1]"
	return 0

}


##
# MIGRATE (schema)
# Examine each plain-txt file in DUMP_DIR
#  (Assumes that kamailio_export was already invoked)
#
# Migrate converts data from schema-old to schema-new in place.
#
# After this step is complete the IMPORT should be executed.
kamailio_migrate()  # parms: <DUMP_DIR> [DB_PATH]
{
	merr "db_berkeley migrate not implemented"
	exit 1
}


##
# IMPORT existing plain-txt files from DUMP_DIR to DB_PATH
# eg.  DUMP_DIR/version.txt  --> DB_PATH/version
#
# import is used as part of DB migrate to another major version of berkeley db.
# this will over-write anything in DB_PATH
kamailio_import()  # parms: <DUMP_DIR> [DB_PATH]
{
	if [ $# -lt 2  ]; then
		echo  "kamailio_dump parms: <DUMP_DIR> [DB_PATH]"
		exit 1
	fi
	
	# Assert: DUMP_DIR (source dir) already exists
	if [ ! -d $1 ] ; then
		merr "Berkeley DUMP_DIR directory does not exist: [$1]"
		exit 1;
	fi
	
	# Assert: DUMP_DIR directory should already contain table 'version.txt'
	if [ ! -f $1/version.txt ] ; then
		merr "DUMP_DIR directory does not have VERSION.txt data at: [$1]"
		exit 1
	fi
	
	# Assert: destination dir exists [DB_PATH]
	if [ ! -d $2 ] ; then
		mdbg "Berkeley DB_PATH directory is being created: [$2]"
		mkdir -p $2
	else
		# Wipe out the destination dir to make room for new data
		mwarn "Berkeley DB_PATH directory is being purged at: [$2]"
		rm -rf $2/*
	fi
	
	# Creates STANDARD tables from plain-text files in DUMP_DIR
	for TABLE in $STANDARD_TABLES; do
	    if [ -s $1/$TABLE.txt ] ; then
		    mdbg "Importing standard table: $TABLE"
		    $LOAD_CMD -T -t hash -f $1/$TABLE.txt -h $2 $TABLE
		    
		    # Check return code to make sure the export worked ok
		    if [ $? -ne 0 ] ; then
			merr "Import of standard table failed [$TABLE.txt]"
			merr "Create this missing table with kambdb_recover."
		    fi
	    else
	    	    merr "Import data not found for table: [$TABLE.txt]" 
		    merr "Create this missing table with kambdb_recover."
	    fi
	done
	

	# Creates PRESENCE tables from plain-text files in DUMP_DIR
	for TABLE in $PRESENCE_TABLES; do
	    if [ -s $1/$TABLE.txt ] ; then
		    mdbg "Importing presence table: $TABLE"
		    $LOAD_CMD -T -t hash -f $1/$TABLE.txt -h $2 $TABLE
		    
		    # Check return code to make sure the export worked ok
		    if [ $? -ne 0 ] ; then
			merr "Import of presence table failed [$TABLE.txt]"
			merr "Create this missing table with kambdb_recover."
		    fi
	    else
		    mwarn "Import data not found for table: [$TABLE.txt]"
	    fi
	done

	# Creates EXTRA tables from plain-text files in DUMP_DIR
	for TABLE in $EXTRA_TABLES; do
	    if [ -s $1/$TABLE.txt ] ; then
		    mdbg "Importing extra table: $TABLE"
		    $LOAD_CMD -T -t hash -f $1/$TABLE.txt -h $2 $TABLE
		    
		    # Check return code to make sure the export worked ok
		    if [ $? -ne 0 ] ; then
			merr "Import of extra table failed [$TABLE.txt]"
			merr "Create this missing table with kambdb_recover."
		    fi
	    else
		    mwarn "Import data not found for table: [$TABLE.txt]"
	    fi
	done
	
	mdbg "All tables are now imported to DB_PATH: [$2]"
	return 0

}


kamailio_swap()  # parms: <db> [DB_PATH]
{
	if [ $# -lt 2  ]; then
		echo  "kamailio_swap parms: <db> [DB_PATH]"
		exit 1
	fi
	
	DB=$2/$1
	DBNEW=$DB.new
	DBOLD=$DB.old
	cp $DB $DBOLD
	mv $DBNEW $DB
}

#####
# append process is:
# 1. copy DB_PATH/db to DB_PATH/db.new
# 2. appends contents of newdata to DB_PATH/db.new
#
kamailio_append()  # parms: <db> <newdata> [DB_PATH]
{
	if [ $# -lt 3  ]; then
		echo  "kamailio_append parms: <db> <newdata> [DB_PATH]"
		exit 1
	fi
	
	DB=$3/$1
	DBNEW=$DB.new
	if [ -f $DB.new ] ; then
		rm $DB.new
	fi
	
	cp $DB $DBNEW
# echo "$LOAD_CMD -T -t hash -f $2 -h $3 $1.new"
	$LOAD_CMD -T -t hash -f $2 -h $3 $1.new
	
# echo "$LOAD_CMD -r fileid -h $3 $1.new"
  	$LOAD_CMD -r fileid -h $3 $1.new
}


#####
# newappend process is:
# 1. create a new temp DBENV in /tmp/sc-<processID>
# 2. appends contents of newdata to /tmp/sc-<processID>/db
# 3. move /tmp/sc-<processID>/db over to DB_PATH/db.new
# 4. delete temp DBENV dir /tmp/sc-<processID>
#
kamailio_newappend()  # parms: <db> <newdata> [DB_PATH]
{
	if [ $# -lt 3  ]; then
		echo  "kamailio_append parms: <db> <newdata> [DB_PATH]"
		exit 1
	fi
	
	DB=$3/$1
	DBNEW=$DB.new
	if [ -f $DBNEW ] ; then
		rm $DBNEW
	fi
	TMPENV=/tmp/sc-$$
	kamailio_create $TMPENV
	cd $OLDPWD
	$LOAD_CMD -T -t hash -f $2 -h $TMPENV $1
	mv $TMPENV/$1 $DBNEW
	rm -rf $TMPENV
}


# cat all rows to STDOUT
kamailio_cat()  # pars: <database name> <DB_PATH>
{
	if [ $# -ne 2 ] ; then
		echo  "kamailio_cat params <db> [DB_PATH]"
		exit 1
	fi
	
	$DUMP_CMD -p -h $2 $1
}

kamailio_drop()  # pars:  <DB_PATH>
{
	if [ $# -ne 1 ] ; then
		echo "kamailio_drop function takes one param"
		exit 1
	fi
	
	if [ ! -d $1 ] ; then
		echo "Directory does not exist:  $1"
	fi
	
	minfo "Dropping Berkeley DB database at: $1 ..."
	
	# core
	if [ -f $1/version ] ; then
		for TABLE in $STANDARD_TABLES; do
		    mdbg "Dropping core table: $TABLE"
		    rm -f $1/$TABLE
		done
	fi
	
	# presence
	if [ -f $1/presentity ] ; then
		for TABLE in $PRESENCE_TABLES; do
		    mdbg "Dropping presence table: $TABLE"
		    rm -f $1/$TABLE
		done
	fi
	
	# extra tables
	if [ -f $1/cpl ] ; then
		for TABLE in $EXTRA_TABLES; do
		    mdbg "Dropping extra table: $TABLE"
		    rm -f $1/$TABLE
		done
	fi

	# delete db files and directory
	rm -rf $1/__db.001
	rm -rf $1/__db.002
	rm -rf $1/__db.003
	rm -rf $1/__db.004
	rmdir $1
}


kamailio_create() # pars: <DB_PATH>
{
	if [ $# -ne 1 ] ; then
		echo "kamailio_create param [DB_PATH]"
		exit 1
	fi
	
	DB_PATH=$1
	if [ ! -d $1 ] ; then
		minfo "creating Berkeley DB database at: [$1]"
		mkdir -p $DB_PATH
	fi
	
	for TABLE in $STANDARD_TABLES; do
	    mdbg "Creating standard table: $TABLE"
	    $LOAD_CMD -T -t hash -f $DB_SCHEMA/$TABLE -h $1 $TABLE
	    if [ $? -ne 0 ] ; then
		merr "Creating standard tables failed!"
		exit 1
	    fi
	done

	get_answer $INSTALL_PRESENCE_TABLES "Install presence related tables? (y/n): "
	if [ "$ANSWER" = "y" ]; then
		presence_create $1
	fi

	get_answer $INSTALL_EXTRA_TABLES "Install tables for $EXTRA_MODULES? (y/n): "
	if [ "$ANSWER" = "y" ]; then
		extra_create $1
	fi

} # kamailio_create


presence_create() # pars: <DB_PATH>
{
	if [ $# -ne 1 ] ; then
		merr "presence_create param [DB_PATH]"
		exit 1
	fi
	
	DB_PATH=$1
	if [ ! -d $1 ] ; then
		# Assert: the directory should already exist
		merr "BerkeleyDB directory does not exist at: [$1]"
		exit 1
	fi

	if [ ! -f $1/version ] ; then
		# Assert: directory should already contain table 'version'
		merr "BerkeleyDB directory does not have VERSION table at: [$1]"
		exit 1
	fi
	
	for TABLE in $PRESENCE_TABLES; do
	    mdbg "Creating presence table: $TABLE"
	    $LOAD_CMD -T -t hash -f $DB_SCHEMA/$TABLE -h $1 $TABLE
	    if [ $? -ne 0 ] ; then
		merr "Creating presence tables failed!"
		exit 1
	    fi
	done
	
}  # end presence_create


extra_create() # pars: <DB_PATH>
{

	if [ $# -ne 1 ] ; then
		merr "extra_create function takes one param (DB_PATH)"
		exit 1
	fi
	
	DB_PATH=$1
	if [ ! -d $1 ] ; then
		# Assert: the directory should already exist
		merr "BerkeleyDB directory does not exist at: [$1]"
		exit 1
	fi

	if [ ! -f $1/version ] ; then
		# Assert: directory should already contain table 'version'
		merr "BerkeleyDB directory does not have VERSION table at: [$1]"
		exit 1
	fi
	
	for TABLE in $EXTRA_TABLES; do
	    mdbg "Creating extra table: $TABLE"
	    $LOAD_CMD -T -t hash -f $DB_SCHEMA/$TABLE -h $1 $TABLE
	    if [ $? -ne 0 ] ; then
		merr "Creating extra tables failed!"
		exit 1
	    fi
	done
	
}  # end extra_create