-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsfdb
executable file
·152 lines (135 loc) · 3.07 KB
/
msfdb
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
#!/bin/sh
METASPLOIT_BASEDIR=/usr/share/metasploit-framework
DB_CONF=$METASPLOIT_BASEDIR/config/database.yml
DB_NAME=msf
DB_USER=msf
DB_PORT=5432
PG_SERVICE=postgresql
pw_gen() {
openssl rand -base64 32
}
pg_cmd() {
su - postgres -c "$*"
}
start_db() {
if ! service $PG_SERVICE status >/dev/null; then
service $PG_SERVICE start
fi
}
stop_db() {
if service $PG_SERVICE status >/dev/null; then
service $PG_SERVICE stop
fi
}
db_exists() {
if pg_cmd "psql -lqt" | cut -d \| -f 1 | grep -qw $1; then
return 0
fi
return 1
}
user_exists() {
if echo "SELECT usename FROM pg_user;" | pg_cmd "psql -qt postgres" | grep -qw $1; then
return 0
fi
return 1
}
init_db() {
start_db
if [ -e $DB_CONF ]; then
echo "A database appears to be already configured, skipping initialization"
return
fi
DB_PASS=$(pw_gen)
if user_exists $DB_USER; then
echo "Resetting password of database user '$DB_USER'"
printf "ALTER ROLE $DB_USER WITH PASSWORD '$DB_PASS';\n" | pg_cmd psql postgres >/dev/null
else
echo "Creating database user '$DB_USER'"
printf "%s\n%s\n" "$DB_PASS" "$DB_PASS" | pg_cmd createuser -P $DB_USER >/dev/null
fi
echo "Creating databases '$DB_NAME' and '${DB_NAME}_test'"
if ! db_exists $DB_NAME; then
pg_cmd createdb $DB_NAME -O $DB_USER -T template0 -E UTF-8
fi
if ! db_exists ${DB_NAME}_test; then
pg_cmd createdb ${DB_NAME}_test -O $DB_USER -T template0 -E UTF-8
fi
echo "Creating configuration file in $DB_CONF"
cat > $DB_CONF <<-EOF
development:
adapter: "postgresql"
database: "$DB_NAME"
username: "$DB_USER"
password: "$DB_PASS"
host: "localhost"
port: $DB_PORT
pool: 5
timeout: 5
production:
adapter: "postgresql"
database: "$DB_NAME"
username: "$DB_USER"
password: "$DB_PASS"
host: "localhost"
port: $DB_PORT
pool: 5
timeout: 5
test:
adapter: "postgresql"
database: "$DB_NAME_test"
username: "$DB_USER"
password: "$DB_PASS"
host: "localhost"
port: $DB_PORT
pool: 5
timeout: 5
EOF
echo "Creating initial database schema"
cd $METASPLOIT_BASEDIR
bundle exec rake db:migrate >/dev/null
}
delete_db() {
start_db
if db_exists $DB_NAME; then
pg_cmd dropdb $DB_NAME
fi
if db_exists ${DB_NAME}_test; then
pg_cmd dropdb ${DB_NAME}_test
fi
if user_exists $DB_USER; then
pg_cmd dropuser $DB_USER
fi
rm -f $DB_CONF
}
reinit_db() {
delete_db
init_db
}
usage() {
PROG=`basename $0`
echo
echo "Manage a metasploit framework database"
echo
echo " $PROG init # initialize the database"
echo " $PROG reinit # delete and reinitialize the database"
echo " $PROG delete # delete database and stop using it"
echo " $PROG start # start the database"
echo " $PROG stop # stop the database"
echo
exit
}
if [ "$#" -ne 1 ]; then
usage
fi
if [ $(id -u) -ne 0 ]; then
echo "ERROR: $0: must be run as root"
exit 1
fi
case $1 in
init) init_db ;;
reinit) reinit_db ;;
delete) delete_db ;;
start) start_db ;;
stop) stop_db ;;
*) echo "Error: unrecognized action '${1}'"; usage ;;
esac