Skip to content

Commit e371261

Browse files
committed
Fixes for supporting passwords with weird characters
Use URL encoding in DATABASE_URL and return mysql_options as an array (via ugly global variable), so each element in it can be separately added to the command line using `@` for expansion.
1 parent e5fd474 commit e371261

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

sql/dj_setup_database.in

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# @configure_input@
33

44
# This script allows one to perform DOMjudge database setup actions.
@@ -52,32 +52,47 @@ not have to pass any of the options above.
5252
EOF
5353
}
5454

55+
urlencode()
56+
{
57+
php -r "echo rawurlencode('$1');"
58+
}
59+
60+
# This is global variable to be able to return the output from
61+
# mysql_options() below as an array, which is not possible otherwise.
62+
declare -a _mysql_options
63+
5564
mysql_options()
5665
{
5766
local user pass
67+
_mysql_options=()
5868

5969
# shellcheck disable=SC2153
6070
if [ -n "$DBUSER" ]; then
61-
user="-u $DBUSER"
62-
else
63-
user="${DBA_USER:+-u ${DBA_USER}}"
71+
_mysql_options+=('-u' "$DBUSER")
72+
elif [ -n "$DBA_USER" ]; then
73+
_mysql_options+=('-u' "$DBA_USER")
6474
fi
6575
# shellcheck disable=SC2153
6676
if [ -n "$PASSWD" ]; then
67-
pass="-p$PASSWD"
68-
else
69-
[ -n "$PROMPT_PASSWD" ] && pass="-p"
70-
[ -n "$DBA_PASSWD" ] && pass="-p$DBA_PASSWD"
77+
_mysql_options+=("-p$PASSWD")
78+
elif [ -n "$DBA_PASSWD" ]; then
79+
_mysql_options+=("-p$DBA_PASSWD")
80+
elif [ -n "$PROMPT_PASSWD" ]; then
81+
_mysql_options+=('-p')
7182
fi
7283

73-
[ -z "$USE_SOCKET" ] && port="-P$DBPORT"
74-
echo $user ${pass:+"$pass"} -h "$DBHOST" ${port:+"$port"}
84+
_mysql_options+=('-h' "$DBHOST")
85+
86+
if [ -z "$USE_SOCKET" ]; then
87+
_mysql_options+=("-P$DBPORT")
88+
fi
7589
}
7690

7791
# Wrapper around mysql command to allow setting options, user, etc.
7892
mysql()
7993
{
80-
command mysql $(mysql_options) --silent --skip-column-names "$@"
94+
mysql_options
95+
command mysql "${_mysql_options[@]}" --silent --skip-column-names "$@"
8196
}
8297

8398
# Quick shell hack to get a key from an INI file.
@@ -128,10 +143,13 @@ symfony_console()
128143
fi
129144

130145
if [ -n "$DBA_USER" ]; then
146+
user=$(urlencode "${DBA_USER}")
147+
host=$(urlencode "${domjudge_DBHOST}")
148+
db=$(urlencode "${domjudge_DBNAME}")
131149
if [ -n "$DBA_PASSWD" ]; then
132-
DATABASE_URL=mysql://${DBA_USER}:${DBA_PASSWD}@${domjudge_DBHOST}:${domjudge_DBPORT}/${domjudge_DBNAME}
150+
DATABASE_URL="mysql://$user:$(urlencode "${DBA_PASSWD}")@$host:${domjudge_DBPORT}/$db"
133151
else
134-
DATABASE_URL=mysql://${DBA_USER}@${domjudge_DBHOST}:${domjudge_DBPORT}/${domjudge_DBNAME}
152+
DATABASE_URL="mysql://$user@$host:${domjudge_DBPORT}/$db"
135153
fi
136154
fi
137155
fi

0 commit comments

Comments
 (0)