From 4cc106d98185b9672376aa10aa068e19b29c3e01 Mon Sep 17 00:00:00 2001
From: Vincent Massol <vincent@massol.net>
Date: Sat, 24 Jun 2017 13:58:06 +0200
Subject: [PATCH] XDOCKER-29: Error in Docker script

---
 8/mysql-tomcat/xwiki/docker-entrypoint.sh    | 16 ++++++++++++----
 8/postgres-tomcat/xwiki/docker-entrypoint.sh | 16 ++++++++++++----
 9/mysql-tomcat/xwiki/docker-entrypoint.sh    | 16 ++++++++++++----
 9/postgres-tomcat/xwiki/docker-entrypoint.sh | 16 ++++++++++++----
 template/xwiki/docker-entrypoint.sh          | 16 ++++++++++++----
 5 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/8/mysql-tomcat/xwiki/docker-entrypoint.sh b/8/mysql-tomcat/xwiki/docker-entrypoint.sh
index b56ca7d..620b4e0 100755
--- a/8/mysql-tomcat/xwiki/docker-entrypoint.sh
+++ b/8/mysql-tomcat/xwiki/docker-entrypoint.sh
@@ -45,12 +45,20 @@ function xwiki_set_properties() {
   xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.properties "$1" "$2"
 }
 
+# Allows to use sed but with user input which can contain special sed characters such as \, / or &.
+# $1 - the text to search for
+# $2 - the replacement text
+# $3 - the file in which to do the search/replace
+function safesed {
+  sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
+}
+
 function configure() {
   echo 'Configuring XWiki...'
-  sed -i "s/replaceuser/${DB_USER:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacepassword/${DB_PASSWORD:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacecontainer/${DB_HOST:-db}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacedatabase/${DB_DATABASE:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replaceuser" ${DB_USER:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacepassword" ${DB_PASSWORD:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacecontainer" ${DB_HOST:-db} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacedatabase" ${DB_DATABASE:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
 
   echo '  Using filesystem-based attachments...'
   xwiki_set_cfg 'xwiki.store.attachment.hint' 'file'
diff --git a/8/postgres-tomcat/xwiki/docker-entrypoint.sh b/8/postgres-tomcat/xwiki/docker-entrypoint.sh
index b56ca7d..620b4e0 100755
--- a/8/postgres-tomcat/xwiki/docker-entrypoint.sh
+++ b/8/postgres-tomcat/xwiki/docker-entrypoint.sh
@@ -45,12 +45,20 @@ function xwiki_set_properties() {
   xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.properties "$1" "$2"
 }
 
+# Allows to use sed but with user input which can contain special sed characters such as \, / or &.
+# $1 - the text to search for
+# $2 - the replacement text
+# $3 - the file in which to do the search/replace
+function safesed {
+  sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
+}
+
 function configure() {
   echo 'Configuring XWiki...'
-  sed -i "s/replaceuser/${DB_USER:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacepassword/${DB_PASSWORD:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacecontainer/${DB_HOST:-db}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacedatabase/${DB_DATABASE:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replaceuser" ${DB_USER:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacepassword" ${DB_PASSWORD:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacecontainer" ${DB_HOST:-db} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacedatabase" ${DB_DATABASE:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
 
   echo '  Using filesystem-based attachments...'
   xwiki_set_cfg 'xwiki.store.attachment.hint' 'file'
diff --git a/9/mysql-tomcat/xwiki/docker-entrypoint.sh b/9/mysql-tomcat/xwiki/docker-entrypoint.sh
index b56ca7d..620b4e0 100755
--- a/9/mysql-tomcat/xwiki/docker-entrypoint.sh
+++ b/9/mysql-tomcat/xwiki/docker-entrypoint.sh
@@ -45,12 +45,20 @@ function xwiki_set_properties() {
   xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.properties "$1" "$2"
 }
 
+# Allows to use sed but with user input which can contain special sed characters such as \, / or &.
+# $1 - the text to search for
+# $2 - the replacement text
+# $3 - the file in which to do the search/replace
+function safesed {
+  sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
+}
+
 function configure() {
   echo 'Configuring XWiki...'
-  sed -i "s/replaceuser/${DB_USER:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacepassword/${DB_PASSWORD:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacecontainer/${DB_HOST:-db}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacedatabase/${DB_DATABASE:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replaceuser" ${DB_USER:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacepassword" ${DB_PASSWORD:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacecontainer" ${DB_HOST:-db} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacedatabase" ${DB_DATABASE:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
 
   echo '  Using filesystem-based attachments...'
   xwiki_set_cfg 'xwiki.store.attachment.hint' 'file'
diff --git a/9/postgres-tomcat/xwiki/docker-entrypoint.sh b/9/postgres-tomcat/xwiki/docker-entrypoint.sh
index b56ca7d..620b4e0 100755
--- a/9/postgres-tomcat/xwiki/docker-entrypoint.sh
+++ b/9/postgres-tomcat/xwiki/docker-entrypoint.sh
@@ -45,12 +45,20 @@ function xwiki_set_properties() {
   xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.properties "$1" "$2"
 }
 
+# Allows to use sed but with user input which can contain special sed characters such as \, / or &.
+# $1 - the text to search for
+# $2 - the replacement text
+# $3 - the file in which to do the search/replace
+function safesed {
+  sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
+}
+
 function configure() {
   echo 'Configuring XWiki...'
-  sed -i "s/replaceuser/${DB_USER:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacepassword/${DB_PASSWORD:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacecontainer/${DB_HOST:-db}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacedatabase/${DB_DATABASE:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replaceuser" ${DB_USER:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacepassword" ${DB_PASSWORD:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacecontainer" ${DB_HOST:-db} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacedatabase" ${DB_DATABASE:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
 
   echo '  Using filesystem-based attachments...'
   xwiki_set_cfg 'xwiki.store.attachment.hint' 'file'
diff --git a/template/xwiki/docker-entrypoint.sh b/template/xwiki/docker-entrypoint.sh
index b837135..c001486 100755
--- a/template/xwiki/docker-entrypoint.sh
+++ b/template/xwiki/docker-entrypoint.sh
@@ -45,12 +45,20 @@ function xwiki_set_properties() {
   xwiki_replace /usr/local/tomcat/webapps/ROOT/WEB-INF/xwiki.properties "\$1" "\$2"
 }
 
+# Allows to use sed but with user input which can contain special sed characters such as \\, / or &.
+# \$1 - the text to search for
+# \$2 - the replacement text
+# \$3 - the file in which to do the search/replace
+function safesed {
+  sed -i "s/\$(echo \$1 | sed -e 's/\\([[\\/.*]\\|\\]\\)/\\\\&/g')/\$(echo \$2 | sed -e 's/[\\/&]/\\\\&/g')/g" \$3
+}
+
 function configure() {
   echo 'Configuring XWiki...'
-  sed -i "s/replaceuser/\${DB_USER:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacepassword/\${DB_PASSWORD:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacecontainer/\${DB_HOST:-db}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
-  sed -i "s/replacedatabase/\${DB_DATABASE:-xwiki}/g" /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replaceuser" \${DB_USER:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacepassword" \${DB_PASSWORD:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacecontainer" \${DB_HOST:-db} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
+  safesed "replacedatabase" \${DB_DATABASE:-xwiki} /usr/local/tomcat/webapps/ROOT/WEB-INF/hibernate.cfg.xml
 
   echo '  Using filesystem-based attachments...'
   xwiki_set_cfg 'xwiki.store.attachment.hint' 'file'
-- 
GitLab