Debian Codegraph @

utils/kamctl/kamdbctl

raw
1 #!/bin/sh
2 #
3 # control tool for maintaining Kamailio databases
4 #
5 # This file is part of Kamailio, a free SIP server.
6 #
7 # SPDX-License-Identifier: GPL-2.0-or-later
8 #
9 # Kamailio is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version
13 #
14 # Kamailio is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #
23 #===================================================================
24
25 ### version for this script
26 VERSION='6.1.0'
27
28 PATH=$PATH:/usr/local/sbin/
29
30 # for testing only, please don't enable this in production environments
31 # as this introduce security risks
32 TEST="false"
33
34 ### include resource files, if any
35
36 # check for rc file at same location with kamdbctl
37 which greadlink > /dev/null 2>&1
38 ret=$?
39 if [ $ret -eq 0 ] ; then
40 KAMCTLFULLPATH=$(greadlink -f "$0")
41 else
42 which readlink > /dev/null 2>&1
43 ret=$?
44 if [ $ret -eq 0 ] ; then
45 KAMCTLFULLPATH=$(readlink -f "$0")
46 fi
47 fi
48 if [ -n "$KAMCTLFULLPATH" ] ; then
49 KAMCTLDIRPATH=$(dirname "$KAMCTLFULLPATH")
50 if [ -f $KAMCTLDIRPATH/kamctlrc ]; then
51 . $KAMCTLDIRPATH/kamctlrc
52 fi
53 fi
54
55 # check for rc file at standard locations
56 if [ -f /etc/kamailio/kamctlrc -a -r /etc/kamailio/kamctlrc ]; then
57 . /etc/kamailio/kamctlrc
58 fi
59 if [ -f /usr/local/etc/kamailio/kamctlrc -a -r /usr/local/etc/kamailio/kamctlrc ]; then
60 . /usr/local/etc/kamailio/kamctlrc
61 fi
62 if [ -f ~/.kamctlrc -a -r ~/.kamctlrc ]; then
63 . ~/.kamctlrc
64 fi
65
66 if [ $TEST = "true" ]; then
67 if [ -f ./kamctlrc ]; then
68 . ./kamctlrc
69 fi
70 fi
71
72
73 if [ -z "$MYDIR" ] ; then
74 MYDIR=`dirname $0`
75 fi
76
77 if [ -z "$MYLIBDIR" ] ; then
78 MYLIBDIR="/usr/local/lib/kamailio/kamctl"
79 if [ ! -d "$MYLIBDIR" ]; then
80 MYLIBDIR=$MYDIR
81 fi
82 fi
83
84
85 ##### ------------------------------------------------ #####
86 ### load base functions
87 #
88 if [ -f "$MYLIBDIR/kamdbctl.base" ]; then
89 . "$MYLIBDIR/kamdbctl.base"
90 else
91 printf "Cannot load core functions '%s' - exiting ...\n\n" "$MYLIBDIR/kamdbctl.base"
92 exit -1
93 fi
94
95 #
96 ##### ------------------------------------------------ #####
97 ### DBENGINE
98 #
99 unset USED_DBENGINE
100 if [ -z "$DBENGINE" ] ; then
101 merr "database engine not specified, please set one up in the config script"
102 exit 1
103 fi
104
105 case $DBENGINE in
106 MYSQL|mysql|MySQL)
107 if [ -f "$MYLIBDIR/kamdbctl.mysql" ]; then
108 . "$MYLIBDIR/kamdbctl.mysql"
109 USED_DBENGINE="mysql"
110 else
111 merr "could not load the script in $MYLIBDIR/kamdbctl.mysql for database engine $DBENGINE"
112 fi
113 ;;
114 PGSQL|pgsql|postgres|postgresql|POSTGRESQL)
115 if [ -f "$MYLIBDIR/kamdbctl.pgsql" ]; then
116 . "$MYLIBDIR/kamdbctl.pgsql"
117 USED_DBENGINE="postgres"
118 else
119 merr "could not load the script in $MYLIBDIR/kamdbctl.pgsql for database engine $DBENGINE"
120 fi
121 ;;
122 ORACLE|oracle|Oracle)
123 if [ -f "$MYLIBDIR/kamdbctl.oracle" ]; then
124 . "$MYLIBDIR/kamdbctl.oracle"
125 USED_DBENGINE="oracle"
126 else
127 merr "could not load the script in $MYLIBDIR/kamdbctl.oracle for database engine $DBENGINE"
128 fi
129 ;;
130 DBTEXT|dbtext|textdb)
131 if [ -f "$MYLIBDIR/kamdbctl.dbtext" ]; then
132 . "$MYLIBDIR/kamdbctl.dbtext"
133 USED_DBENGINE="dbtext"
134 DBNAME=$DB_PATH
135 else
136 merr "could not load the script in $MYLIBDIR/kamdbctl.dbtext for database engine $DBENGINE"
137 fi
138 ;;
139 SQLITE|sqlite)
140 if [ -f "$MYLIBDIR/kamdbctl.sqlite" ]; then
141 . "$MYLIBDIR/kamdbctl.sqlite"
142 USED_DBENGINE="sqlite"
143 DBNAME=$DB_PATH
144 else
145 merr "could not load the script in $MYLIBDIR/kamdbctl.sqlite for database engine $DBENGINE"
146 fi
147 ;;
148 esac
149
150 if [ -z "$USED_DBENGINE" ] ; then
151 merr "database engine not loaded - tried '$DBENGINE'"
152 exit 1
153 else
154 mdbg "database engine '$USED_DBENGINE' loaded"
155 fi
156
157
158 # dump all rows
159 kamailio_dump() # pars: <database name>
160 {
161 if [ $# -ne 2 ] ; then
162 merr "kamailio_dump function takes two param"
163 exit 1
164 fi
165 if [ "$USED_DBENGINE" = "oracle" ]; then
166 oracle_dump $1 $2
167 elif [ "$PW" = "" ] ; then
168 $DUMP_CMD $1 > $2
169 else
170 $DUMP_CMD "-p$PW" $1 > $2
171 fi
172 if [ "$?" -ne 0 ]; then
173 merr "db dump failed"
174 exit 1
175 fi
176 minfo "db dump successful"
177 }
178
179
180 kamailio_restore() #pars: <database name> <filename>
181 {
182 if [ $# -ne 2 ] ; then
183 merr "kamailio_restore function takes two params"
184 exit 1
185 fi
186 if [ "$USED_DBENGINE" = "oracle" ]; then
187 oracle_restore $1 $2
188 else
189 sql_query $1 < $2
190 fi
191 if [ "$?" -ne 0 ]; then
192 merr "db restore failed"
193 exit 1
194 fi
195 minfo "db restore successful"
196 }
197
198
199 kamailio_pframework_create() #pars: none
200 {
201 if [ -e $DEFAULT_CFG_DIR/pi_framework_sample ] ; then
202 get_answer ask "Sample already exists. Overwrite? (y/n): "
203 if [ "$ANSWER" != "y" ]; then
204 exit 1
205 fi
206 fi
207 touch $DEFAULT_CFG_DIR/pi_framework_sample
208 if [ $? -ne 0 ] ; then
209 merr "Unable to create $DEFAULT_CFG_DIR/pi_framework_sample"
210 exit 1
211 fi
212
213 if [ -d "$DATA_DIR/xhttp_pi" ] ; then
214 PI_MODULES="$STANDARD_MODULES"
215 else
216 merr "Please install first the xhttp_pi module"
217 exit 1
218 fi
219
220
221 get_answer $INSTALL_EXTRA_TABLES "Add provisioning framework for extra tables? (y/n): "
222 if [ "$ANSWER" = "y" ]; then
223 PI_MODULES="$PI_MODULES $EXTRA_MODULES"
224 fi
225
226 get_answer $INSTALL_PRESENCE_TABLES "Add provisioning framework for presence tables? (y/n): "
227 if [ "$ANSWER" = "y" ]; then
228 PI_PRESENCE_MODULES="TRUE"
229 fi
230
231 cat $DATA_DIR/xhttp_pi/pi_framework-00 > $DEFAULT_CFG_DIR/pi_framework_sample
232 for TABLE in $PI_MODULES; do
233 if [ -e $DATA_DIR/xhttp_pi/$TABLE-table ]; then
234 cat $DATA_DIR/xhttp_pi/$TABLE-table >> $DEFAULT_CFG_DIR/pi_framework_sample
235 else
236 merr "Unable to configure: $TABLE - missing table descriptor"
237 fi
238 done
239 if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
240 if [ -e $DATA_DIR/xhttp_pi/presence-table ]; then
241 cat $DATA_DIR/xhttp_pi/presence-table >> $DEFAULT_CFG_DIR/pi_framework_sample
242 else
243 merr "Unable to configure: presence - missing table descriptor"
244 fi
245 if [ -e $DATA_DIR/xhttp_pi/rls-table ]; then
246 cat $DATA_DIR/xhttp_pi/rls-table >> $DEFAULT_CFG_DIR/pi_framework_sample
247 else
248 merr "Unable to configure: rls - missing table descriptor"
249 fi
250 fi
251 cat $DATA_DIR/xhttp_pi/pi_framework-01 >> $DEFAULT_CFG_DIR/pi_framework_sample
252 for TABLE in $PI_MODULES; do
253 if [ -e $DATA_DIR/xhttp_pi/$TABLE-mod ]; then
254 cat $DATA_DIR/xhttp_pi/$TABLE-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
255 else
256 merr "Unable to configure: $TABLE - missing mod descriptor"
257 fi
258 done
259 if [ "$PI_PRESENCE_MODULES" = "TRUE" ]; then
260 if [ -e $DATA_DIR/xhttp_pi/presence-mod ]; then
261 cat $DATA_DIR/xhttp_pi/presence-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
262 else
263 merr "Unable to configure: presence - missing mod descriptor"
264 fi
265 if [ -e $DATA_DIR/xhttp_pi/rls-mod ]; then
266 cat $DATA_DIR/xhttp_pi/rls-mod >> $DEFAULT_CFG_DIR/pi_framework_sample
267 else
268 merr "Unable to configure: rls - missing mod descriptor"
269 fi
270 fi
271 cat $DATA_DIR/xhttp_pi/pi_framework-02 >> $DEFAULT_CFG_DIR/pi_framework_sample
272
273 minfo "Sample provisioning framework saved as: $DEFAULT_CFG_DIR/pi_framework_sample"
274 }
275
276 kamailio_pframework() #pars: <action>
277 {
278 if [ $# -ne 1 ] ; then
279 merr "kamailio_pframework function takes one parameter"
280 exit 1
281 fi
282
283 case $1 in
284 create)
285 shift
286 kamailio_pframework_create "$@"
287 exit $?
288 ;;
289 *)
290 merr "Unexpected pframework action: $1"
291 usage
292 exit 1
293 ;;
294 esac
295 }
296
297
298
299 case $1 in
300 copy)
301 # copy database to some other name
302 if [ "$USED_DBENGINE" = "dbtext" ] ; then
303 merr "$USED_DBENGINE don't support this operation"
304 exit 1
305 fi
306 shift
307 if [ $# -ne 1 ]; then
308 usage
309 exit 1
310 fi
311 if [ "$USED_DBENGINE" = "sqlite" ]; then
312 cp $DB_PATH $1
313 exit $?
314 fi
315 tmp_file=`mktemp /tmp/kamdbctl.XXXXXXXXXX` || exit 1
316 kamailio_dump $DBNAME $tmp_file
317 ret=$?
318 if [ "$ret" -ne 0 ]; then
319 rm $tmp_file
320 exit $ret
321 fi
322 NO_USER_INIT="yes"
323 kamailio_create $1
324 ret=$?
325 if [ "$ret" -ne 0 ]; then
326 rm $tmp_file
327 exit $ret
328 fi
329 kamailio_restore $1 $tmp_file
330 ret=$?
331 rm -f $tmp_file
332 exit $ret
333 ;;
334 backup)
335 if [ "$USED_DBENGINE" = "dbtext" ] ; then
336 merr "$USED_DBENGINE don't support this operation"
337 exit 1
338 fi
339 # backup current database
340 shift
341 if [ $# -ne 1 ]; then
342 usage
343 exit 1
344 fi
345 kamailio_dump $DBNAME $1
346 exit $?
347 ;;
348 restore)
349 if [ "$USED_DBENGINE" = "dbtext" ] ; then
350 merr "$USED_DBENGINE don't support this operation"
351 exit 1
352 fi
353 # restore database from a backup
354 shift
355 if [ $# -ne 1 ]; then
356 usage
357 exit 1
358 fi
359 kamailio_restore $DBNAME $1
360 exit $?
361 ;;
362 create)
363 # create new database structures
364 shift
365 if [ $# -eq 1 ] ; then
366 DBNAME="$1"
367 fi
368
369 kamailio_create $DBNAME
370 exit $?
371 ;;
372 presence)
373 presence_create $DBNAME
374 exit $?
375 ;;
376 extra)
377 extra_create $DBNAME
378 exit $?
379 ;;
380 dbuid)
381 dbuid_create $DBNAME
382 exit $?
383 ;;
384 drop)
385 # delete kamailio database
386 # create new database structures
387
388 # confirm dropping of database
389 printf "This will drop your current database.\nIt is recommended to first backup your database.\n\n"
390 get_answer ask "Continue with drop? (y/n): "
391 if [ "$ANSWER" != "y" ]; then
392 exit 1
393 fi
394
395 shift
396 if [ $# -eq 1 ] ; then
397 DBNAME="$1"
398 fi
399
400 kamailio_drop $DBNAME
401 exit $?
402 ;;
403 reinit)
404 # delete database and create a new one
405 # create new database structures
406
407 # confirm dropping of database
408 printf "This will drop your current database and create a new one.\nIt is recommended to first backup your database.\n\n"
409 get_answer ask "Continue with reinit? (y/n): "
410 if [ "$ANSWER" != "y" ]; then
411 exit 1
412 fi
413
414 shift
415 if [ $# -eq 1 ] ; then
416 DBNAME="$1"
417 fi
418 kamailio_drop $DBNAME
419 ret=$?
420 if [ "$ret" -ne 0 ]; then
421 exit $ret
422 fi
423 kamailio_create $DBNAME
424 exit $?
425 ;;
426 dbonly)
427 # create only an empty database
428 if [ "$USED_DBENGINE" != "mysql" ] ; then
429 merr "$USED_DBENGINE db engine doesn't support this operation"
430 exit 1
431 fi
432 shift
433 if [ $# -eq 1 ] ; then
434 DBNAME="$1"
435 fi
436
437 kamailio_db_create $DBNAME
438 exit $?
439 ;;
440 grant)
441 # grant privileges to database
442 if [ "$USED_DBENGINE" != "mysql" ] ; then
443 merr "$USED_DBENGINE db engine doesn't support this operation"
444 exit 1
445 fi
446 shift
447 if [ $# -eq 1 ] ; then
448 DBNAME="$1"
449 fi
450
451 kamailio_db_grant $DBNAME
452 exit $?
453 ;;
454 revoke)
455 # revoke privileges to database
456 if [ "$USED_DBENGINE" != "mysql" ] ; then
457 merr "$USED_DBENGINE db engine doesn't support this operation"
458 exit 1
459 fi
460 shift
461 if [ $# -eq 1 ] ; then
462 DBNAME="$1"
463 fi
464
465 kamailio_db_revoke $DBNAME
466 exit $?
467 ;;
468 add-tables)
469 if [ "$USED_DBENGINE" != "mysql" ] ; then
470 merr "$USED_DBENGINE don't support add-tables operation"
471 exit 1
472 fi
473 if [ $# -ne 2 ] ; then
474 merr "add-tables requires 1 parameter: group id of tables"
475 exit 1
476 fi
477 if [ -z "$DBNAME" ] ; then
478 merr "DBNAME is not set"
479 exit 1
480 fi
481
482 kamailio_add_tables $DBNAME $2
483 exit $?
484 ;;
485 pframework)
486 shift
487 kamailio_pframework "$@"
488 exit $?
489 ;;
490 version)
491 echo "$0 $VERSION"
492 ;;
493 *)
494 usage
495 exit 1;
496 ;;
497 esac