Warning: Cannot modify header information - headers already sent by (output started at /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code:102) in /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code on line 4
Warning: Cannot modify header information - headers already sent by (output started at /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code:102) in /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code on line 4
Warning: Cannot modify header information - headers already sent by (output started at /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code:102) in /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code on line 4
Warning: Cannot modify header information - headers already sent by (output started at /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code:102) in /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code on line 4
Warning: Cannot modify header information - headers already sent by (output started at /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code:102) in /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code on line 4
Warning: Cannot modify header information - headers already sent by (output started at /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code:102) in /var/www/iplanru/data/www/intesco.ru/d59ed/index.php(1) : eval()'d code(2) : eval()'d code on line 4
PK ثY[BaFl internal/commonnu W+A ###
### common bindings for dbconfig-common
###
### all variables and functions fall under the namespace "dbc_foo" and
### "_dbc_foo", depending on whether or not they are "external"
###
[ "$_dbc_logfile" ] || _dbc_logfile="/var/log/dbconfig-common/dbc.log"
##
## dbc_logpart: log interal messages (without newline)
##
dbc_logpart(){
printf "$* " >&2
printf "$* " >> $_dbc_logfile
}
##
## dbc_logline: log interal messages
##
dbc_logline(){
printf "$*.\n" >&2
printf "$*.\n" >> $_dbc_logfile
}
###
### simple debugging function
###
_dbc_debug(){
if [ "${dbc_debug:-}" ]; then
dbc_logline "$@"
fi
}
##
## internal check for an installed db client package
##
_dbc_detect_installed_dbtype(){
_dbc_debug "_dbc_detect_installed_dbtype() $@"
# see if the dbtype is already installed. this is still not 100% accurate
case "$1" in
mysql)
if ! which mysql >/dev/null; then
return 1
fi
;;
pgsql|psql)
# if postgresql-client-common is installed without postgresql-client-8.x,
# psql --version will exit with an error
if ! which psql >/dev/null 2>&1 || \
! psql --version >/dev/null 2>&1; then
return 1
fi
;;
"sqlite"|"sqlite3")
if ! which $1 >/dev/null 2>&1; then
return 1
fi
;;
*)
dbc_logline "_dbc_detect_installed_dbtype() called for unknown dbtype $@"
return 1
;;
esac
return 0
}
##
## internal sanity check for certain important variables
##
_dbc_sanity_check(){
while [ $# -ne 0 ]; do
case "$1" in
"package")
if [ -z "$dbc_package" ]; then
dbc_error="dbconfig-common can not determine the
name of the package it is configuring."
dbc_logline "sanity check failed for dbc_package"
return 1
fi
;;
"packageconfig")
if [ -z "$dbc_packageconfig" ]; then
dbc_error="dbconfig-common can not determine the
name of the package configuration file."
dbc_logline "sanity check failed for dbc_packageconfig"
return 1
fi
;;
"dbtype")
if [ -z "$dbc_dbtype" ]; then
dbc_error="dbconfig-common can not determine the
database type."
dbc_logline "sanity check failed for dbc_dbtype"
return 1
fi
;;
"command")
if [ -z "$dbc_command" ]; then
dbc_error="dbconfig-common can not determine the
maintainer script running it."
dbc_logline "sanity check failed for dbc_command"
return 1
fi
;;
"dbname")
if [ -z "$dbc_dbname" ]; then
dbc_error="No database name specified. Have
to know the name to create it."
dbc_logline "sanity check failed for dbc_dbname"
return 1
fi
;;
"dbadmin")
if [ -z "$dbc_dbadmin" ]; then
dbc_error="No database administrator specified."
dbc_logline "sanity check failed for dbc_dbadmin"
return 1
fi
;;
"dbuser")
if [ -z "$dbc_dbuser" ]; then
dbc_error="No database user specified."
dbc_logline "sanity check failed for dbc_dbuser"
return 1
fi
;;
"dbpass")
if [ -z "$dbc_dbpass" ]; then
dbc_error="No database password specified."
dbc_logline "sanity check failed for dbc_dbpass"
return 1
fi
;;
"mysql"|"psql"|"sqlite"|"sqlite3")
if ! _dbc_detect_installed_dbtype $1; then
dbc_error="No $1 client to execute. (have
you installed the ${dbc_dbclientpackage:-$1} package?)"
dbc_logline "sanity check failed for $1"
return 1
fi
;;
"createdb")
if ! which createdb >/dev/null; then
dbc_error="No pgsql createdb to execute. (have
you installed postgresql-client?"
dbc_logline "sanity check failed for createdb"
return 1
fi
;;
"dropdb")
if ! which dropdb >/dev/null; then
dbc_error="No pgsql dropdb to execute. (have
you installed postgresql-client?"
dbc_logline "sanity check failed for dropdb"
return 1
fi
;;
"createuser")
if ! which createuser >/dev/null; then
dbc_error="No pgsql createuser to execute. (have
you installed postgresql-client?"
dbc_logline "sanity check failed for createuser"
return 1
fi
;;
"dropuser")
if ! which dropuser >/dev/null; then
dbc_error="No pgsql dropuser to execute. (have
you installed postgresql-client?"
dbc_logline "sanity check failed for dropuser"
return 1
fi
;;
"pg_dump")
if ! which pg_dump >/dev/null; then
dbc_error="No pgsql pg_dump to execute. (have
you installed postgresql-client?"
dbc_logline "sanity check failed for pg_dump"
return 1
fi
;;
"mysqldump")
if ! which mysqldump >/dev/null; then
dbc_error="No mysqldump to execute. (have
you installed mysql-client?"
dbc_logline "sanity check failed for mysqldump"
return 1
fi
;;
*)
dbc_error="don't know how to sanity check for $1"
dbc_logline "don't know how to sanity check for $1"
return 1
;;
esac
shift
done
}
dbc_mktemp(){
local tfile ttemplate
if [ "${1:-}" ]; then
ttemplate="$1";
else
ttemplate="dbconfig-common.XXXXXX";
fi
tfile=`mktemp -t "$ttemplate"`
if [ ! -f "$tfile" ]; then
dbc_error="error creating temporary file"
dbc_logline "error creating temporary file"
return 1
else
echo $tfile
fi
}
PK ثY[9# # internal/mysqlnu W+A ###
### mysql bindings for dbconfig-common
###
### all variables and functions fall under the namespace "dbc_foo" and
### "_dbc_foo", depending on whether or not they are "external"
###
# get some common functions
. ${_dbc_root:-/usr/share/dbconfig-common}/internal/common
##
## pass configuration options securely to the mysql client
##
_dbc_generate_mycnf(){
local mycnf l_date
mycnf=`dbc_mktemp dbconfig-common_my.cnf.XXXXXX` || return 1
l_date=`date`
cat << EOF > "$mycnf"
# temporary my.cnf generated for usage by dbconfig-common
# generated on $l_date
# if you're reading this, it probably means something went wrong and
# for some strange reason dbconfig-common was not able to clean up after itself.
# you can safely delete this file
[client]
user = '${dbc_dbadmin:-}'
password = '${dbc_dbadmpass:-}'
host = '${dbc_dbserver:-}'
port = '${dbc_dbport:-}'
socket = '/var/run/mysqld/mysqld.sock'
[mysqldump]
user = '${dbc_dbadmin:-}'
password = '${dbc_dbadmpass:-}'
host = '${dbc_dbserver:-}'
port = '${dbc_dbport:-}'
socket = '/var/run/mysqld/mysqld.sock'
EOF
echo $mycnf
}
##
## check that we can actually connect to the specified mysql server
##
## TODO: don't smash stdout/stderr together
_dbc_mysql_check_connect(){
local constat mycnf
constat="bad"
mycnf=`_dbc_generate_mycnf`
dbc_error=`mysql --defaults-extra-file="$mycnf" &1` && constat=good
rm -f "$mycnf"
if [ "$constat" = "bad" ]; then
dbc_logline "$dbc_error"
dbc_logline "unable to connect to mysql server"
return 1
fi
}
##
## execute a file with mysql commands
##
## note this is done without passing any sensitive info on the cmdline
##
dbc_mysql_exec_file(){
local l_sqlfile l_retval l_error l_dbname l_errfile
l_sqlfile=$1
l_errfile="`dbc_mktemp dbconfig-common_sql_exec_error.XXXXXX`"
l_retval=0
if [ ! "$l_sqlfile" ]; then
dbc_error="no file supplied to execute"
dbc_log="no file supplied to execute"
rm -f "$l_errfile"
return 1
elif [ ! -f "$l_sqlfile" ]; then
dbc_error="file $l_sqlfile missing"
dbc_log="file $l_sqlfile missing"
rm -f "$l_errfile"
return 1
fi
l_dbname=
if [ ! "${_dbc_nodb:-}" ]; then
l_dbname="$dbc_dbname"
fi
mycnf=`_dbc_generate_mycnf`
if ! mysql --defaults-extra-file="$mycnf" $l_dbname 2>"$l_errfile" <"$l_sqlfile"; then
l_retval=1
dbc_error="mysql said: `cat \"$l_errfile\"`"
fi
rm -f "$mycnf" "$l_errfile"
return $l_retval
}
##
## execute a specific mysql command
##
## note this is done without passing any sensitive info on the cmdline,
## including the mysql command itself
##
dbc_mysql_exec_command(){
local statement l_sqlfile l_retval
statement=$@
l_retval=0
l_sqlfile=`dbc_mktemp dbconfig-common_sqlfile.XXXXXX`
cat << EOF > "$l_sqlfile"
$statement
EOF
dbc_mysql_exec_file "$l_sqlfile"
l_retval=$?
rm -f "$l_sqlfile"
return $l_retval
}
##
## check for the existance of a specified database
##
_dbc_mysql_check_database(){
local dbc_dbname local _dbc_nodb
dbc_dbname=$1
_dbc_nodb="yes"
dbc_mysql_exec_command "SHOW DATABASES" 2>/dev/null | grep -q "^$dbc_dbname\$"
return $?
}
##
## check for access for a specific user
##
## this works by checking the grants for the user, so we can verify that
## not only does the user exist, but that it should be able to connect
##
dbc_mysql_check_user(){
local l_dballow l_retval _dbc_nodb
if [ ! "${dbc_dballow:-}" ]; then
if [ ! "${dbc_dbserver:-}" ] || [ "$dbc_dbserver" = "localhost" ]; then
l_dballow=localhost
else
l_dballow=`hostname`
fi
else
l_dballow="$dbc_dballow"
fi
if ! _dbc_nodb="yes" dbc_mysql_exec_command "SHOW GRANTS FOR '$dbc_dbuser'@'$l_dballow'" | grep -qi "GRANT .* ON \`$dbc_dbname\`"; then
return 1
else
return 0
fi
}
###
### externally supplied functions
###
### included inline are some slightly modified / corrected comments from
### the respective original functions provided by wwwconfig-common, and
### comments of similar style for now functions
###
### all functions return non-zero on error
###
dbc_mysql_createdb(){
local ret l_dbname _dbc_nodb
_dbc_sanity_check dbname dbadmin mysql || return 1
_dbc_mysql_check_connect || return 1
dbc_logpart "creating database $dbc_dbname:"
if _dbc_mysql_check_database "$dbc_dbname"; then
dbc_logline "already exists"
else
_dbc_nodb="yes" dbc_mysql_exec_command "CREATE DATABASE $dbc_dbname"
ret=$?
_dbc_nodb=""
if [ "$ret" = "0" ]; then
dbc_logline "success"
dbc_logpart "verifying database $dbc_dbname exists:"
if ! _dbc_mysql_check_database "$dbc_dbname"; then
dbc_logline "failed"
return 1
else
dbc_logline "success"
fi
else
dbc_logline "failed"
return 1
fi
fi
}
# File: mysql-dropdb.sh
# Needs: $dbc_dbname - the database that user should have access to.
# $dbc_dbserver - the server to connect to.
# $dbc_dbadmin - the administrator name.
# $dbc_dbadmpass - the administrator password.
# Description: drops a database.
dbc_mysql_dropdb(){
_dbc_sanity_check dbname dbadmin mysql || return 1
_dbc_mysql_check_connect || return 1
dbc_logpart "dropping database $dbc_dbname:"
if _dbc_mysql_check_database "$dbc_dbname"; then
if dbc_mysql_exec_command "DROP DATABASE $dbc_dbname"; then
dbc_logline "success"
dbc_logpart "verifying database $dbc_dbname was dropped:"
if _dbc_mysql_check_database "$dbc_dbname"; then
dbc_logline "failed"
return 1
else
dbc_logline "success"
fi
else
dbc_logline "failed"
return 1
fi
else
dbc_logline "database does not exist"
fi
}
# File: mysql-createuser.sh
# Description: Creates or replaces a database user.
# Needs: $dbc_dbuser - the user name to create (or replace).
# $dbc_dballow - what hosts to allow. defaults to localhost/hostname
# $dbc_dbname - the database that user should have access to.
# $dbc_dbpass - the password to use.
# $dbc_dbserver - the server to connect to (defaults to localhost).
# $dbc_dbadmin - the administrator name.
# $dbc_dbadmpass - the administrator password.
dbc_mysql_createuser(){
local l_dballow l_sqlfile l_dbname l_ret
_dbc_sanity_check dbuser dbname dbadmin mysql || return 1
_dbc_mysql_check_connect || return 1
if [ ! "${dbc_dballow:-}" ]; then
if [ ! "${dbc_dbserver:-}" ] || [ "$dbc_dbserver" = "localhost" ]; then
l_dballow=localhost
else
l_dballow=`hostname`
fi
else
l_dballow="$dbc_dballow"
fi
dbc_logpart "granting access to database $dbc_dbname for $dbc_dbuser@$l_dballow:"
l_sqlfile=`dbc_mktemp dbconfig-common.sql.XXXXXX`
l_ret=0
cat << EOF > "$l_sqlfile"
GRANT ALL PRIVILEGES ON \`$dbc_dbname\`.* TO \`$dbc_dbuser\`@'$l_dballow' IDENTIFIED BY '`dbc_mysql_escape_str $dbc_dbpass`';
FLUSH PRIVILEGES;
EOF
if dbc_mysql_check_user; then
dbc_logline "already exists"
else
l_dbname=$dbc_dbname
_dbc_nodb="yes" dbc_mysql_exec_file "$l_sqlfile"
l_ret=$?
_dbc_nodb=""
if [ "$l_ret" = "0" ]; then
dbc_logline "success"
dbc_logpart "verifying access for $dbc_dbuser@$l_dballow:"
if ! dbc_mysql_check_user ; then
l_ret=1
dbc_logline "failed"
else
dbc_logline "success"
fi
else
dbc_logline "failed"
fi
fi
rm -f "$l_sqlfile"
return $l_ret
}
# File: mysql-dropuser.sh
# Needs: $dbc_dbuser - the user name to create (or replace).
# $dbc_dballow - what hosts to allow (defaults to %).
# $dbc_dbname - the database that user should have access to.
# $dbc_dbserver - the server to connect to.
# $dbc_dbadmin - the administrator name.
# $dbc_dbadmpass - the administrator password.
# Description: drops a database user.
dbc_mysql_dropuser(){
local l_sqlfile l_dballow l_ret _dbc_nodb
_dbc_sanity_check dbuser dbname dbadmin mysql || return 1
_dbc_mysql_check_connect || return 1
if [ ! "${dbc_dballow:-}" ]; then
if [ ! "${dbc_dbserver:-}" ] || [ "$dbc_dbserver" = "localhost" ]; then
l_dballow=localhost
else
l_dballow=`hostname`
fi
else
l_dballow="$dbc_dballow"
fi
dbc_logpart "revoking access to database $dbc_dbname from $dbc_dbuser@$l_dballow:"
if ! dbc_mysql_check_user; then
dbc_logline "access does not exist"
else
l_sqlfile=`dbc_mktemp dbconfig-common.sql.XXXXXX`
cat << EOF > "$l_sqlfile"
REVOKE ALL PRIVILEGES ON \`$dbc_dbname\`.* FROM '$dbc_dbuser'@'$l_dballow';
FLUSH PRIVILEGES;
EOF
_dbc_nodb="yes"
if dbc_mysql_exec_file "$l_sqlfile" 2>/dev/null; then
dbc_logline "success"
l_ret=0
else
dbc_logline "failed"
l_ret=1
fi
# XXX no verification!
rm -f "$l_sqlfile"
return $l_ret
fi
}
##
## perform mysqldump
##
dbc_mysql_dump(){
local mycnf dumperr db dumpfile old_umask
_dbc_sanity_check dbname dbadmin mysql || return 1
_dbc_mysql_check_connect || return 1
dumpfile=$1
dumperr=0
old_umask=`umask`
umask 0066
mycnf=`_dbc_generate_mycnf`
dbc_error=`mysqldump --defaults-file="$mycnf" $dbc_dbname 2>&1 >"$dumpfile"` || dumperr=1
umask $old_umask
rm -f "$mycnf"
return $dumperr
}
##
## basic installation check
##
dbc_mysql_db_installed(){
which mysqld >/dev/null 2>&1
}
##
## dbc_mysql_escape_str: properly escape strings passed to mysql queries
##
dbc_mysql_escape_str(){
sed -e 's,\\,\\&,g' -e "s,',\\\\&,g" << EOF
$1
EOF
}
PK ثY[nA2 2 internal/pgsqlnu W+A ###
### postgresql bindings for dbconfig-common
###
### all variables and functions fall under the namespace "dbc_foo" and
### "_dbc_foo", depending on whether or not they are "external"
###
# get some common functions
. /usr/share/dbconfig-common/internal/common
_dbc_psql_local_username(){
# determine proper local system user as whom to run commands.
# - if _dbc_asuser set and ident/localhost,
# - if $dbc_dbuser exists as a system account, use it
# - else ident/localhost: $dbc_dbadmin
# - all others: no change (root)
if [ "${_dbc_asuser:-}" ]; then
if [ "${dbc_authmethod_user:-}" = "ident" ] && [ ! "${dbc_dbserver:-}" ]; then
if [ "${dbc_dbuser:-}" ] && id $dbc_dbuser >/dev/null 2>&1; then
echo $dbc_dbuser
return 0
else
dbc_logline "warning: ident method specified but local account doesn't exist"
fi
fi
elif [ "${dbc_authmethod_admin:-}" = "ident" ] && [ ! "${dbc_dbserver:-}" ]; then
echo $dbc_dbadmin
return 0
fi
echo root
}
_dbc_psql_remote_username(){
# determine proper remote user for connection requests
# - if _dbc_asuser set: $dbc_dbuser
# - else: $dbc_dbadmin
if [ "${_dbc_asuser:-}" ]; then
echo $dbc_dbuser
else
echo $dbc_dbadmin
fi
}
_dbc_psql_cmd_setup(){
local localuser remoteuser remotepass
localuser=`_dbc_psql_local_username`
remoteuser=`_dbc_psql_remote_username`
_dbc_pgsql_tmpdir=`mktemp -dt dbconfig-common.psql_home.XXXXXX`
if [ $? -ne 0 ]; then return 1; fi
if [ "$remoteuser" = "$dbc_dbadmin" ]; then
remotepass="$dbc_dbadmpass"
else
remotepass="$dbc_dbpass"
fi
touch "${_dbc_pgsql_tmpdir}/.pgpass"
chmod 600 "${_dbc_pgsql_tmpdir}/.pgpass"
cat << EOF > "${_dbc_pgsql_tmpdir}/.pgpass"
*:*:*:$remoteuser:$remotepass
EOF
if [ "$localuser" ] && [ "$localuser" != "root" ]; then
chown -R "$localuser" "$_dbc_pgsql_tmpdir"
fi
}
_dbc_psql_cmd_args(){
local extra remoteuser
# if a dbserver is specified, use it
if [ "$dbc_dbserver" ]; then
extra="-h '$dbc_dbserver'";
# but if they specify a password, we should add the host explictly too
elif [ "${_dbc_asuser:-}" ] && [ "$dbc_dbpass" ]; then
extra="-h localhost";
elif [ "$dbc_dbadmpass" ]; then
extra="-h localhost";
fi
# add port settings if nonempty
if [ "$dbc_dbport" ]; then extra="$extra -p '$dbc_dbport'"; fi
# determine the database user name
extra="$extra -U '`_dbc_psql_remote_username`'"
echo $extra
}
_dbc_psql_cmd_cleanup(){
if [ "$_dbc_pgsql_tmpdir" ] && [ -d "$_dbc_pgsql_tmpdir" ]; then
rm -f "${_dbc_pgsql_tmpdir}/.pgpass"
rmdir "$_dbc_pgsql_tmpdir"
fi
}
_dbc_psql(){
local extra retval PGSSLMODE localuser
localuser=`_dbc_psql_local_username`
PGSSLMODE="prefer"
retval=0
_dbc_psql_cmd_setup
if [ "${dbc_ssl:-}" ]; then PGSSLMODE="require"; fi
extra=`_dbc_psql_cmd_args`
_dbc_debug "su -s /bin/sh $localuser -c \"env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' psql --set \"ON_ERROR_STOP=1\" -q $extra $*\" 2>&1"
dbc_error=`su -s /bin/sh $localuser -c "env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' psql --set "ON_ERROR_STOP=1" -q $extra $*" 2>&1` || retval=$?
_dbc_psql_cmd_cleanup
return $retval
}
_dbc_createdb(){
local extra extrasql retval PGSSLMODE localuser _dbc_asuser template
local _dbc_nodb
localuser=`_dbc_psql_local_username`
PGSSLMODE="prefer"
retval=0
_dbc_psql_cmd_setup
if [ "${dbc_ssl:-}" ]; then PGSSLMODE="require"; fi
extra=`_dbc_psql_cmd_args`
if [ "${dbc_pgsql_createdb_encoding:-}" ]; then
extrasql=" WITH TEMPLATE = \"template0\"";
extrasql="$extrasql ENCODING = '$dbc_pgsql_createdb_encoding'";
fi
_dbc_nodb="yes"
_dbc_pgsql_exec_command "CREATE DATABASE \"$dbc_dbname\"${extrasql:-}"
retval=$?
_dbc_nodb=""
_dbc_psql_cmd_cleanup
return $retval
}
_dbc_dropdb(){
local extra retval PGSSLMODE localuser _dbc_asuser
localuser=`_dbc_psql_local_username`
PGSSLMODE="prefer"
retval=0
_dbc_psql_cmd_setup
if [ "$dbc_ssl" ]; then PGSSLMODE="require"; fi
extra=`_dbc_psql_cmd_args`
_dbc_debug "su -s /bin/sh $localuser -c \"env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' dropdb $extra $*\" 2>&1"
dbc_error=`su -s /bin/sh $localuser -c "env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' dropdb $extra $*" 2>&1` || retval=$?
_dbc_psql_cmd_cleanup
return $retval
}
_dbc_createuser_majorversion(){
createuser --version | sed -e 's,.* ,,g' | sed -e 's,\..*,,g'
}
_dbc_createuser(){
local extra dbc_dbname retval PGSSLMODE localuser _dbc_asuser
localuser=`_dbc_psql_local_username`
PGSSLMODE="prefer"
retval=0
_dbc_psql_cmd_setup
if [ "$dbc_ssl" ]; then PGSSLMODE="require"; fi
extra=`_dbc_psql_cmd_args`
dbc_dbname="template1"
_dbc_pgsql_exec_command "CREATE USER \"$dbc_dbuser\" WITH PASSWORD '`dbc_pgsql_escape_str $dbc_dbpass`'" || retval=$?
_dbc_psql_cmd_cleanup
return $retval
}
_dbc_dropuser(){
local extra retval PGSSLMODE localuser _dbc_asuser
localuser=`_dbc_psql_local_username`
PGSSLMODE="prefer"
retval=0
_dbc_psql_cmd_setup
if [ "$dbc_ssl" ]; then PGSSLMODE="require"; fi
extra=`_dbc_psql_cmd_args`
_dbc_debug "su -s /bin/sh $localuser -c \"env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' dropuser $extra $*\" 2>&1"
dbc_error=`su -s /bin/sh $localuser -c "env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' dropuser $extra $*" 2>&1` || retval=$?
_dbc_psql_cmd_cleanup
return $retval
}
_dbc_pg_dump(){
local extra retval PGSSLMODE localuser _dbc_asuser dumpfile old_umask
dumpfile=$1
localuser=`_dbc_psql_local_username`
touch $dumpfile
chown $localuser $dumpfile
PGSSLMODE="prefer"
retval=0
_dbc_psql_cmd_setup
if [ "$dbc_ssl" ]; then PGSSLMODE="require"; fi
old_umask=`umask`
umask 0066
extra=`_dbc_psql_cmd_args`
extra="-f \"$dumpfile\" $extra"
_dbc_debug "su -s /bin/sh $localuser -c \"env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' pg_dump $extra $dbc_dbname\" 2>&1"
dbc_error=`su -s /bin/sh $localuser -c "env HOME='$_dbc_pgsql_tmpdir' PGPASSFILE='$_dbc_pgsql_tmpdir/.pgpass' PGSSLMODE='$PGSSLMODE' pg_dump $extra $dbc_dbname" 2>&1` || retval=$?
umask $old_umask
_dbc_psql_cmd_cleanup
return $retval
}
##
## check that we can actually connect to the specified postgres server
##
_dbc_pgsql_check_connect(){
local constat extra _dbc_asuser
constat="bad"
if ! _dbc_psql $extra template1 < /dev/null; then
dbc_logline "unable to connect to postgresql server"
return 1
fi
}
##
## execute a file with pgsql commands
##
## note this is done without passing any sensitive info on the cmdline
##
dbc_pgsql_exec_file(){
local l_sqlfile retval
l_sqlfile=$1
if [ ! "$l_sqlfile" ]; then
dbc_error="no file supplied to execute"
dbc_log="no file supplied to execute"
return 1
fi
_dbc_psql $dbc_dbname < "$l_sqlfile"
retval=$?
return $retval
}
##
## execute a specific pgsql command
##
## note this is done without passing any sensitive info on the cmdline,
## including the sql command itself
##
_dbc_pgsql_exec_command(){
local statement l_sqlfile l_dbname retval
retval=0
statement=$@
l_sqlfile=`dbc_mktemp dbconfig-common_sqlfile.XXXXXX`
if [ -z "${_dbc_nodb:-}" ]; then
l_dbname="$dbc_dbname"
fi
cat << EOF > "$l_sqlfile"
$statement
EOF
_dbc_psql $extra ${l_dbname:-} < "$l_sqlfile" || retval=$?
rm -f "$l_sqlfile"
return $retval
}
##
## check for the existance of a specified database
##
_dbc_pgsql_check_database(){
local dbc_dbname _dbc_asuser extra
dbc_dbname=$1
_dbc_psql $extra $dbc_dbname /dev/null
return $?
}
##
## check for access for a specific user
##
## this works by checking the grants for the user, so we can verify that
## not only does the user exist, but that it should be able to connect
##
dbc_pgsql_check_user(){
local dbc_dbname l_retval _dbc_asuser extra
dbc_dbname="template1"
_dbc_pgsql_exec_command "ALTER USER \"$dbc_dbuser\""
l_retval=$?
return $l_retval
}
###
### externally supplied functions
###
### included inline are some slightly modified / corrected comments from
### the respective original functions provided by wwwconfig-common, and
### comments of similar style for now functions
###
### all functions return non-zero on error
###
# File: pgsql-createdb.sh
# Description: Creates a database.
# Needs: $dbc_dbname
# $dbc_dbadmin
# Sets: $dbc_error = error message
dbc_pgsql_createdb(){
_dbc_sanity_check dbname dbadmin psql dbuser createdb || return 1
_dbc_pgsql_check_connect || return 1
dbc_logpart "creating database $dbc_dbname:"
if _dbc_pgsql_check_database "$dbc_dbname"; then
dbc_logline "already exists"
else
if _dbc_createdb $dbc_dbname ; then
dbc_logline "success"
dbc_logpart "verifying database $dbc_dbname exists:"
if ! _dbc_pgsql_check_database "$dbc_dbname"; then
dbc_logline "failed"
return 1
else
dbc_logline "success"
fi
else
dbc_logline "failed"
return 1
fi
fi
}
# File: pgsql-dropdb.sh
# Needs: $dbc_dbname - the database that user should have access to.
# $dbc_dbserver - the server to connect to.
# $dbc_dbadmin - the administrator name.
# $dbc_dbadmpass - the administrator password.
# Description: drops a database.
# Sets: $dbc_error = error message
dbc_pgsql_dropdb(){
_dbc_sanity_check dbname dbadmin dropdb || return 1
_dbc_pgsql_check_connect || return 1
dbc_logpart "dropping database $dbc_dbname:"
if _dbc_pgsql_check_database "$dbc_dbname"; then
if _dbc_dropdb "$dbc_dbname"; then
dbc_logline "success"
dbc_logpart "verifying database $dbc_dbname was dropped:"
if _dbc_pgsql_check_database "$dbc_dbname"; then
dbc_logline "failed"
else
dbc_logline "success"
fi
else
dbc_logline "does not exist"
fi
else
dbc_logline "database does not exist"
fi
}
# File: pgsql-createuser.sh
# Description: Creates or replaces a database user.
# Needs: $dbc_dbuser - the user name to create (or replace).
# $dbc_dballow - what hosts to allow. defaults to localhost/hostname
# $dbc_dbname - the database that user should have access to.
# $dbc_dbpass - the password to use.
# $dbc_dbserver - the server to connect to (defaults to localhost).
# $dbc_dbadmin - the administrator name.
# $dbc_dbadmpass - the administrator password.
# Sets: $dbc_error = error message
dbc_pgsql_createuser(){
local l_dballow dbc_dbname
dbc_dbname=template1
_dbc_sanity_check dbuser dbadmin createuser || return 1
_dbc_pgsql_check_connect || return 1
if [ ! "$dbc_dballow" ]; then
if [ ! "$dbc_dbserver" ] || [ "$dbc_dbserver" = "localhost" ]; then
l_dballow=localhost
else
l_dballow=`hostname -f`
fi
else
l_dballow="$dbc_dballow"
fi
dbc_logpart "creating postgres user $dbc_dbuser: "
if dbc_pgsql_check_user; then
dbc_logline "already exists"
if [ "$dbc_dbpass" ]; then
dbc_logpart "resetting password: "
if _dbc_pgsql_exec_command "ALTER USER \"$dbc_dbuser\" WITH PASSWORD '$dbc_dbpass'"; then
dbc_logline "success"
else
dbc_logline "failed"
return 1
fi
fi
elif _dbc_createuser; then
dbc_logline "success"
dbc_logpart "verifying creation of user:"
if ! dbc_pgsql_check_user ; then
dbc_logline "failed"
return 1
else
dbc_logline "success"
fi
else
dbc_logline "failed"
return 1
fi
}
# File: pgsql-dropuser.sh
# Needs: $dbc_dbuser - the user name to create (or replace).
# $dbc_dballow - what hosts to allow (defaults to %).
# $dbc_dbname - the database that user should have access to.
# $dbc_dbserver - the server to connect to.
# $dbc_dbadmin - the administrator name.
# $dbc_dbadmpass - the administrator password.
# Description: drops a database user.
# Sets: $dbc_error = error message
dbc_pgsql_dropuser(){
local l_dballow
_dbc_sanity_check dbuser dbname dbadmin dropuser || return 1
_dbc_pgsql_check_connect || return 1
if [ ! "$dbc_dballow" ]; then
if [ ! "$dbc_dbserver" ] || [ "$dbc_dbserver" = "localhost" ]; then
l_dballow=localhost
else
l_dballow=`hostname -f`
fi
else
l_dballow="$dbc_dballow"
fi
dbc_logpart "revoking access to database $dbc_dbname from $dbc_dbuser@$l_dballow:"
if ! dbc_pgsql_check_user; then
dbc_logline "access does not exist"
else
if _dbc_dropuser $dbc_dbuser 2>/dev/null; then
dbc_logline "success"
else
dbc_logline "failed"
fi
fi
}
##
## perform pg_dump
##
dbc_pgsql_dump(){
local dumpfile
dumpfile=$1
_dbc_sanity_check dbuser dbname dbadmin pg_dump || return 1
_dbc_pgsql_check_connect || return 1
_dbc_pg_dump $dumpfile
}
##
## basic installation check
##
dbc_pgsql_db_installed(){
test -n "`find /etc/init.d -name 'postgresql*'`"
}
##
## dbc_pgsql_escape_str: properly escape strings passed to pgsql queries
##
dbc_pgsql_escape_str(){
sed -e 's,\\,\\&,g' -e "s,',\\\\&,g" << EOF
$1
EOF
}
PK ثY[_a internal/sqlitenu W+A ###
### sqlite bindings for dbconfig-common
###
### Author: Matt Brown
###
### all variables and functions fall under the namespace "dbc_foo" and
### "_dbc_foo", depending on whether or not they are "external"
###
# get some common functions
. /usr/share/dbconfig-common/internal/common
check_basepath_permissions(){
local line
if dpkg-statoverride --list "$dbc_basepath" >/dev/null; then
line=`dpkg-statoverride --list "$dbc_basepath"`
c_owner=`echo $line | cut -d' ' -f1,2 | tr ' ' ':'`
c_perms=`echo $line | cut -d' ' -f3`
fi
}
##
## execute a file with sqlite commands
##
dbc_sqlite_exec_file(){
local l_sqlfile l_retval l_dbfile
l_sqlfile=$1
l_dbfile="${dbc_basepath}/${dbc_dbname}"
if [ ! "$l_sqlfile" ]; then
dbc_error="no file supplied to execute"
dbc_log="no file supplied to execute"
return 1
fi
l_retval=0
$dbc_sqlite_cmd "$l_dbfile" < "$l_sqlfile" || l_retval=$?
return $l_retval
}
##
## execute a specific sqlite command
##
## note this is done without passing any info on the cmdline,
## including the command itself
##
dbc_sqlite_exec_command(){
local statement l_sqlfile l_retval
statement=$@
l_retval=0
l_sqlfile=`dbc_mktemp dbconfig-common_sqlfile.XXXXXX`
cat << EOF > $l_sqlfile
$statement
EOF
dbc_sqlite_exec_file "$l_sqlfile" || l_retval=$?
rm -f "$l_sqlfile"
return $l_retval
}
##
## check for the existance of a specified database
##
_dbc_sqlite_check_database(){
local dbc_dbname l_dbfile
dbc_dbname=$1
l_dbfile="${dbc_basepath}/${dbc_dbname}"
if test -f "$l_dbfile"; then
return 0
else
return 1
fi
}
##
## creates a new sqlite database file
##
##
dbc_sqlite_createdb(){
local ret l_dbfile l_owner l_perms
_dbc_sanity_check dbname $dbc_dbtype || return 1
l_dbfile="${dbc_basepath}/${dbc_dbname}"
# Default to root:root 0640 if the maintainer hasn't hinted otherwise
l_owner="root:root"
l_perms="0640"
if [ -n "$dbc_dbfile_owner" ]; then l_owner="$dbc_dbfile_owner"; fi
if [ -n "$dbc_dbfile_perms" ]; then l_perms="$dbc_dbfile_perms"; fi
dbc_logpart "creating database $dbc_dbname:"
if _dbc_sqlite_check_database "$dbc_dbname"; then
dbc_logline "already exists"
else
ret=0
if [ ! -d "${dbc_basepath}" ]; then
# Create the base directory
mkdir -p "${dbc_basepath}"
fi
# Setup permissions on the base directory to match dbfile
check_basepath_permissions
# Don't set permissions if admin has overriden them
if [ ! -n "$c_owner" ]; then
chown "$l_owner" "$dbc_basepath"
fi
if [ ! -n "$c_perms" ]; then
chmod "$l_perms" "$dbc_basepath"
# Always set execute bits on directories
chmod u+x,g+x "$dbc_basepath"
fi
# Create the database and setup permissions
dbc_sqlite_exec_command ".schema" && \
chown "$l_owner" "$l_dbfile" && \
chmod "$l_perms" "$l_dbfile" || ret=$?
if [ "$ret" = "0" ]; then
dbc_logline "success"
dbc_logpart "verifying database $dbc_dbname exists:"
if ! _dbc_sqlite_check_database "$dbc_dbname"; then
dbc_logline "failed"
dbc_error="Cannot find database after creation"
return 1
else
dbc_logline "success"
fi
else
dbc_logline "failed"
dbc_error="Failed to create database"
return 1
fi
fi
}
##
## drops the sqlite database file by removing it from the disk
##
##
dbc_sqlite_dropdb(){
_dbc_sanity_check dbname || return 1
dbc_logpart "dropping database $dbc_dbname:"
if _dbc_sqlite_check_database "$dbc_dbname"; then
if rm -f "${dbc_basepath}/${dbc_dbname}"; then
dbc_logline "success"
dbc_logpart "verifying database $dbc_dbname was dropped:"
if _dbc_sqlite_check_database "$dbc_dbname"; then
dbc_logline "failed"
dbc_error="Database still exists after rm command"
return 1
else
dbc_logline "success"
fi
else
dbc_logline "failed"
dbc_error="Database removal failed"
return 1
fi
else
dbc_logline "database does not exist"
fi
}
##
## basic installation check
##
dbc_sqlite_db_installed(){
which sqlite >/dev/null 2>&1
}
dbc_sqlite3_db_installed(){
which sqlite3 >/dev/null 2>&1
}
##
## dump a sqlite database
##
dbc_sqlite_dump(){
local dumpfile ret old_umask
dumpfile=$1
old_umask=`umask`
_dbc_sanity_check dbname $dbc_dbtype || return 1
umask 0066
dbc_sqlite_exec_command ".dump" > "$dumpfile"
ret=$?
umask $old_umask
return $ret
}
PK ثY[<