Skip to content

Commit fcf8a39

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. Because of changing the script to bash, also reverts some of the changes in 483200a. The changed escaping of `DoctrineMigrations\\Version` passed to mysql is purely due to changing from `/bin/sh` to `/bin/bash`. Somehow passing that to the `mysql` wrapper function, it got unescaped before, even though `/bin/sh` is just a symlink to bash...
1 parent b19d278 commit fcf8a39

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

sql/dj_setup_database.in

Lines changed: 35 additions & 14 deletions
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,30 +52,48 @@ not have to pass any of the options above.
5252
EOF
5353
}
5454

55+
urlencode()
56+
{
57+
# Redirect via stdin to prevent ugly escaping hacks.
58+
echo -n "$1" | php -r "echo rawurlencode(file_get_contents('php://stdin'));"
59+
}
60+
61+
# This is global variable to be able to return the output from
62+
# mysql_options() below as an array, which is not possible otherwise.
63+
declare -a _mysql_options
64+
5565
mysql_options()
5666
{
67+
local user pass
68+
_mysql_options=()
69+
5770
# shellcheck disable=SC2153
5871
if [ -n "$DBUSER" ]; then
59-
_user="-u $DBUSER"
60-
else
61-
_user="${DBA_USER:+-u ${DBA_USER}}"
72+
_mysql_options+=('-u' "$DBUSER")
73+
elif [ -n "$DBA_USER" ]; then
74+
_mysql_options+=('-u' "$DBA_USER")
6275
fi
6376
# shellcheck disable=SC2153
6477
if [ -n "$PASSWD" ]; then
65-
_pass="-p$PASSWD"
66-
else
67-
[ -n "$PROMPT_PASSWD" ] && _pass="-p"
68-
[ -n "$DBA_PASSWD" ] && _pass="-p$DBA_PASSWD"
78+
_mysql_options+=("-p$PASSWD")
79+
elif [ -n "$DBA_PASSWD" ]; then
80+
_mysql_options+=("-p$DBA_PASSWD")
81+
elif [ -n "$PROMPT_PASSWD" ]; then
82+
_mysql_options+=('-p')
6983
fi
7084

71-
[ -z "$USE_SOCKET" ] && port="-P$DBPORT"
72-
echo $_user ${_pass:+"$_pass"} -h "$DBHOST" ${port:+"$port"}
85+
_mysql_options+=('-h' "$DBHOST")
86+
87+
if [ -z "$USE_SOCKET" ]; then
88+
_mysql_options+=("-P$DBPORT")
89+
fi
7390
}
7491

7592
# Wrapper around mysql command to allow setting options, user, etc.
7693
mysql()
7794
{
78-
command mysql $(mysql_options) --silent --skip-column-names "$@"
95+
mysql_options
96+
command mysql "${_mysql_options[@]}" --silent --skip-column-names "$@"
7997
}
8098

8199
# Quick shell hack to get a key from an INI file.
@@ -126,10 +144,13 @@ symfony_console()
126144
fi
127145

128146
if [ -n "$DBA_USER" ]; then
147+
user=$(urlencode "${DBA_USER}")
148+
host=$(urlencode "${domjudge_DBHOST}")
149+
db=$(urlencode "${domjudge_DBNAME}")
129150
if [ -n "$DBA_PASSWD" ]; then
130-
DATABASE_URL=mysql://${DBA_USER}:${DBA_PASSWD}@${domjudge_DBHOST}:${domjudge_DBPORT}/${domjudge_DBNAME}
151+
DATABASE_URL="mysql://$user:$(urlencode "${DBA_PASSWD}")@$host:${domjudge_DBPORT}/$db"
131152
else
132-
DATABASE_URL=mysql://${DBA_USER}@${domjudge_DBHOST}:${domjudge_DBPORT}/${domjudge_DBNAME}
153+
DATABASE_URL="mysql://$user@$host:${domjudge_DBPORT}/$db"
133154
fi
134155
fi
135156
fi
@@ -356,7 +377,7 @@ upgrade)
356377
# shellcheck disable=SC2016,SC2028
357378
echo 'INSERT INTO `doctrine_migration_versions`
358379
(version, executed_at, execution_time)
359-
SELECT concat("DoctrineMigrations\\\\Version", version), executed_at, 1
380+
SELECT concat("DoctrineMigrations\\Version", version), executed_at, 1
360381
FROM migration_versions;' | mysql "$DBNAME"
361382
echo "DROP TABLE \`migration_versions\`" | mysql "$DBNAME"
362383
fi

0 commit comments

Comments
 (0)