-
Notifications
You must be signed in to change notification settings - Fork 18
/
postgres_entry.sh
128 lines (109 loc) · 4.24 KB
/
postgres_entry.sh
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
# This script runs postgres server in very stupid ways, this script is tested
# extensively on Kali
get_config_value(){
parameter=$1
file=$2
echo "$(grep -i $parameter $file | sed "s|$parameter: ||g;s|~|$HOME|g")"
}
get_postgres_server_ip() {
echo "$(sudo netstat -lptn | grep "^tcp " | grep postgres | sed 's/\s\+/ /g' | cut -d ' ' -f4 | cut -d ':' -f1)"
}
get_postgres_server_port() {
echo "$(sudo netstat -lptn | grep "^tcp " | grep postgres | sed 's/\s\+/ /g' | cut -d ' ' -f4 | cut -d ':' -f2)"
}
# Bail out if not root privileges
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
init_postgres() {
db_name=$1
db_user=$2
db_pass=$3
su postgres -c "psql -c \"CREATE USER $db_user WITH PASSWORD '$db_pass'\""
su postgres -c "psql -c \"CREATE DATABASE $db_name WITH OWNER $db_user ENCODING 'utf-8' TEMPLATE template0;\""
}
clean_postgres() {
db_name=$1
db_user=$2
su postgres -c "psql -c \"DROP DATABASE $db_name\""
su postgres -c "psql -c \"DROP USER $db_user\""
}
FILE_PATH=$(readlink -f "$0")
SCRIPTS_DIR=$(dirname "$FILE_PATH")
RootDir=${1:-$(dirname "$SCRIPTS_DIR")}
config_file="/root/.owtf/conf/framework.cfg"
db_config_file="$(get_config_value DATABASE_SETTINGS_FILE $config_file)"
# Saved postgres settings
saved_server_ip="$(get_config_value DATABASE_IP $db_config_file)"
saved_server_port="$(get_config_value DATABASE_PORT $db_config_file)"
saved_server_dbname="$(get_config_value DATABASE_NAME $db_config_file)"
saved_server_user="$(get_config_value DATABASE_USER $db_config_file)"
saved_server_pass="$(get_config_value DATABASE_PASS $db_config_file)"
postgres_version="$(psql --version 2>&1 | tail -1 | awk '{print $3}' | sed 's/\./ /g' | awk '{print $1 "." $2}')"
# Postgres setup.
PGDATA=/var/lib/postgresql/${postgres_version}/data
PGLOG=$PGDATA/serverlog
mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql
mkdir -p $PGDATA && chown -R postgres $PGDATA
postgresql_start() {
echo "Starting the postgresql service"
# get the return values of which commands to determine the service controller
which service >> /dev/null 2>&1
service_bin=$?
which systemctl >> /dev/null 2>&1
systemctl_bin=$?
if [ "$service_bin" != "1" ]; then
service postgresql start
service postgresql status | grep -q '^Running clusters: ..*$'
status_exitcode="$?"
elif [ "$systemctl_bin" != "1" ]; then
systemctl start postgresql
systemctl status postgresql | grep -q "active"
status_exitcode="$?"
elif [ "$systemctl_bin" != "0" ] && [ "$service_bin" != "0" ]; then
echo "[+] Using pg_ctlcluster to start the server."
sudo pg_ctlcluster ${postgres_version} main start
else
echo "[+] We couldn't determine how to start the postgres server, please start it and rerun this script"
exit 1
fi
}
postgresql_fix() {
# remove SSL=true from the postgresql main config
postgres_version="$(psql --version 2>&1 | tail -1 | awk '{print $3}' | sed 's/\./ /g' | awk '{print $1 "." $2}')"
postgres_conf="$(echo 'SHOW config_file;' | sudo -u postgres psql | grep 'postgres')"
# hardcode
remove_ssl="y"
case $remove_ssl in
[yY][eE][sS]|[yY])
sudo sed -i -e '/ssl =/ s/= .*/= false/' $postgres_conf
echo "Restarting the postgresql service"
# get the return values of which commands to determine the service controller
sudo which service >> /dev/null 2>&1
service_bin=$?
sudo which systemctl >> /dev/null 2>&1
systemctl_bin=$?
if [ "$service_bin" != "1" ]; then
sudo service postgresql restart
sudo service postgresql status | grep -q "Active: active"
elif [ "$systemctl_bin" != "1" ]; then
sudo systemctl restart postgresql
sudo systemctl status postgresql | grep -q "Active: active"
else
sudo pg_ctlcluster ${postgres_version} main restart
fi
;;
*)
# do nothing
;;
esac
}
postgresql_start
postgresql_fix
# Clean db before creating it.
check_owtf_db=$(su - postgres -c "psql -l | grep -w $saved_server_dbname | grep -w $saved_server_user | wc -l")
if [ "$check_owtf_db" != "0" ]; then
clean_postgres $saved_server_dbname $saved_server_user
fi
init_postgres $saved_server_dbname $saved_server_user $saved_server_pass