Skip to content

Commit de104a5

Browse files
Make templating safer and more verbose (#1343)
* letsencrypt: fix active status check Signed-off-by: nachoparker <nacho@ownyourbits.com> * letsencrypt: take into account duplicate domains ending in -0001 Signed-off-by: nachoparker <nacho@ownyourbits.com> * letsencrypt: fix renewal with httpsonly enabled Signed-off-by: nachoparker <nacho@ownyourbits.com> * fix inverted template logic for docker Signed-off-by: nachoparker <nacho@ownyourbits.com> * library.sh: Move templating to separate function - Backup old file before templating (and restore on failure) - Use stderr in the template for debug/info output Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * library.sh: Fix syntax error Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * library.sh: Only fallback to default config if explicitly allowed Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * letsencrypt.sh: Set cert-name Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * nextcloud.conf.sh: Use certificate named ncp-nextcloud if available Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * letsencrypt.sh: Support multiple, comma separated domains in field "OTHER_DOMAIN" Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * nextcloud.conf.sh: Fix path resolution for certificates Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * letsencrypt.sh: Improve warning about max trusted domains reached Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * letsencrypt.sh: Fix max trusted domains check Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * letsencrypt.sh: Fix splitting of domain string by comma Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * letsencrypt.sh: Fix splitting of domain string to array Signed-off-by: Tobias K <6317548+theCalcaholic@users.noreply.github.com> * adjustments for docker/lxc Signed-off-by: nachoparker <nacho@ownyourbits.com> Co-authored-by: nachoparker <nacho@ownyourbits.com>
1 parent 8a6c1c0 commit de104a5

File tree

6 files changed

+60
-18
lines changed

6 files changed

+60
-18
lines changed

bin/ncp/CONFIG/nc-nextcloud.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,9 @@ EOF
181181

182182
## SET APACHE VHOST
183183
echo "Setting up Apache..."
184-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh > /etc/apache2/sites-available/nextcloud.conf || {
185-
echo "ERROR: An error occured while generating the nextcloud apache2 config. Attempting safe mode..."
186-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh --defaults > /etc/apache2/sites-available/nextcloud.conf || {
187-
echo "ERROR: Safe mode templating failed as well. Nextcloud will not work."
184+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf --allow-fallback || {
185+
echo "ERROR: Parsing template failed. Nextcloud will not work."
188186
exit 1
189-
}
190187
}
191188
a2ensite nextcloud
192189

bin/ncp/NETWORKING/letsencrypt.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,30 @@ configure()
5757
rm -f /etc/cron.weekly/letsencrypt-ncp
5858
rm -f /etc/letsencrypt/renewal-hooks/deploy/ncp
5959
[[ "$DOCKERBUILD" == 1 ]] && update-rc.d letsencrypt disable
60-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh > ${nc_vhostcfg}
60+
install_template nextcloud.conf.sh "${nc_vhostcfg}"
6161
echo "letsencrypt certificates disabled. Using self-signed certificates instead."
6262
exit 0
6363
}
6464
local DOMAIN_LOWERCASE="${DOMAIN,,}"
65+
local OTHER_DOMAINS_ARRAY
6566

6667
[[ "$DOMAIN" == "" ]] && { echo "empty domain"; return 1; }
6768

69+
local IFS_BK="$IFS"
70+
IFS=",$IFS" OTHER_DOMAINS_ARRAY=(${OTHER_DOMAIN})
71+
IFS="$IFS_BK"
72+
6873
# Do it
6974
local domain_string=""
70-
for domain in $DOMAIN $OTHER_DOMAIN; do
75+
for domain in $DOMAIN "${OTHER_DOMAINS_ARRAY[@]}"; do
7176
[[ "$domain" != "" ]] && {
7277
[[ $domain_string == "" ]] && \
7378
domain_string+="${domain}" || \
7479
domain_string+=",${domain}"
7580
}
7681
done
77-
"${letsencrypt}" certonly -n --force-renew --no-self-upgrade --webroot -w "${ncdir}" --hsts --agree-tos -m "${EMAIL}" -d "${domain_string}" && {
82+
"${letsencrypt}" certonly -n --force-renew --cert-name ncp-nextcloud --no-self-upgrade --webroot -w "${ncdir}" \
83+
--hsts --agree-tos -m "${EMAIL}" -d "${domain_string}" && {
7884

7985
# Set up auto-renewal
8086
cat > /etc/cron.weekly/letsencrypt-ncp <<EOF
@@ -106,15 +112,20 @@ EOF
106112
chmod +x /etc/letsencrypt/renewal-hooks/deploy/ncp
107113

108114
# Configure Apache
109-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh > ${nc_vhostcfg}
115+
install_template nextcloud.conf.sh "${nc_vhostcfg}"
110116
sed -i "s|SSLCertificateFile.*|SSLCertificateFile /etc/letsencrypt/live/$DOMAIN_LOWERCASE/fullchain.pem|" $vhostcfg2
111117
sed -i "s|SSLCertificateKeyFile.*|SSLCertificateKeyFile /etc/letsencrypt/live/$DOMAIN_LOWERCASE/privkey.pem|" $vhostcfg2
112118

113119
# Configure Nextcloud
114120
local domain_index="${TRUSTED_DOMAINS[letsencrypt_1]}"
115-
for dom in $DOMAIN $OTHER_DOMAIN; do
121+
for dom in $DOMAIN "${OTHER_DOMAINS_ARRAY[@]}"; do
116122
[[ "$dom" != "" ]] && {
117-
ncc config:system:set trusted_domains $domain_index --value=$dom
123+
[[ $domain_index -lt 20 ]] || {
124+
echo "WARN: $dom will not be included in trusted domains for Nextcloud (maximum reached)." \
125+
"It will still be included in the SSL certificate"
126+
continue
127+
}
128+
ncc config:system:set trusted_domains "$domain_index" --value="$dom"
118129
((domain_index++))
119130
}
120131
done

bin/ncp/SYSTEM/metrics.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ configure() {
3535

3636
if [[ "$ACTIVE" != yes ]]
3737
then
38-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh --defaults > /etc/apache2/sites-available/nextcloud.conf
38+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf
3939

4040
systemctl disable prometheus-node-exporter
4141
service prometheus-node-exporter stop
@@ -59,9 +59,8 @@ configure() {
5959
rm -f "${htpasswd_file}"
6060
echo "$PASSWORD" | htpasswd -ciB "${htpasswd_file}" "$USER"
6161

62-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh > /etc/apache2/sites-available/nextcloud.conf || {
63-
echo "An unexpected error occurred while configuring apache. Rolling back..." >&2
64-
bash /usr/local/etc/ncp-templates/nextcloud.conf.sh --defaults > /etc/apache2/sites-available/nextcloud.conf
62+
install_template nextcloud.conf.sh /etc/apache2/sites-available/nextcloud.conf || {
63+
echo "ERROR while generating nextcloud.conf! Exiting..."
6564
return 1
6665
}
6766

changelog.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

2-
[v1.40.2](https://github.com/nextcloud/nextcloudpi/commit/fc3f978) (2021-10-05) nc-update-nc: BTRFS support
2+
[v1.40.4](https://github.com/nextcloud/nextcloudpi/commit/9fa18af) (2021-10-06) Make templating safer and more verbose (#1343)
33

4-
[v1.40.1](https://github.com/nextcloud/nextcloudpi/commit/7c361c5) (2021-10-05) update: improve check for apt (#1356)
4+
[v1.40.3 ](https://github.com/nextcloud/nextcloudpi/commit/8a6c1c0) (2021-10-06) ncp-check-nc-version: dont notify the same version more than once
5+
6+
[v1.40.2 ](https://github.com/nextcloud/nextcloudpi/commit/ea1e00c) (2021-10-05) nc-update-nc: BTRFS support
7+
8+
[v1.40.1 ](https://github.com/nextcloud/nextcloudpi/commit/7c361c5) (2021-10-05) update: improve check for apt (#1356)
59

610
[v1.40.0 ](https://github.com/nextcloud/nextcloudpi/commit/a0728d7) (2021-10-04) nc-notify-updates: notify of new supported NC versions
711

etc/library.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ function find_app_param_num()
183183

184184
}
185185

186+
install_template() {
187+
local template="${1?}"
188+
local target="${2?}"
189+
local bkp="$(mktemp)"
190+
[[ -f "$target" ]] && cp -a "$target" "$bkp"
191+
{
192+
if [[ "$3" == "--defaults" ]]; then
193+
{ bash "/usr/local/etc/ncp-templates/$template" --defaults > "$target"; } 2>&1
194+
else
195+
{ bash "/usr/local/etc/ncp-templates/$template" > "$target"; } 2>&1 || \
196+
{
197+
[[ "$3" == "--allow-fallback" ]] && \
198+
{ bash "/usr/local/etc/ncp-templates/$template" --defaults > "$target"; } 2>&1
199+
}
200+
fi
201+
} || {
202+
echo "ERROR: Could not generate $target from template $template. Rolling back..."
203+
mv "$bkp" "$target"
204+
return 1
205+
}
206+
rm "$bkp"
207+
}
208+
186209
find_app_param()
187210
{
188211
local script="${1?}"

etc/ncp-templates/nextcloud.conf.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
set -e
44
source /usr/local/etc/library.sh
55

6+
[[ "$1" != "--defaults" ]] || echo "INFO: Restoring template to default settings" >&2
7+
[[ ! -f /.docker-image ]] || echo "INFO: Docker installation detected" >&2
8+
69
if [[ "$1" != "--defaults" ]]; then
710
LETSENCRYPT_DOMAIN="$(
811
# force defaults during initial build
@@ -13,7 +16,10 @@ if [[ "$1" != "--defaults" ]]; then
1316
)"
1417
fi
1518

16-
if ! [[ -f /.ncp-image ]] && [[ "$1" != "--defaults" ]]; then
19+
[[ -z "$LETSENCRYPT_DOMAIN" ]] || echo "INFO: Letsencrypt domain is ${LETSENCRYPT_DOMAIN}" >&2
20+
21+
# skip during build
22+
if ! [[ -f /.ncp-image ]] && [[ "$1" != "--defaults" ]] && [[ -f "${BINDIR}/SYSTEM/metrics.sh" ]]; then
1723
METRICS_IS_ENABLED="$(
1824
source "${BINDIR}/SYSTEM/metrics.sh"
1925
tmpl_metrics_enabled && echo yes || echo no
@@ -22,6 +28,8 @@ else
2228
METRICS_IS_ENABLED=no
2329
fi
2430

31+
echo "INFO: Metrics enabled: ${METRICS_IS_ENABLED}" >&2
32+
2533
echo "### DO NOT EDIT! THIS FILE HAS BEEN AUTOMATICALLY GENERATED. CHANGES WILL BE OVERWRITTEN ###"
2634
echo ""
2735

0 commit comments

Comments
 (0)