Ensure critical config lines have trailing newlines before appending new content. This is guaranteed by our scripts, however, it is not guaranteed any longer when uders manually edited config files themselves. Ensure the files are okay to work with before adding anything to them.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2020-12-30 10:50:16 +01:00
parent ae1a59285d
commit 69e55aff83
2 changed files with 22 additions and 0 deletions

View File

@@ -49,6 +49,7 @@ Options:
}
add_setting() {
ensure_newline "${setupVars}"
echo "${1}=${2}" >> "${setupVars}"
}
@@ -62,6 +63,7 @@ change_setting() {
}
addFTLsetting() {
ensure_newline "${FTLconf}"
echo "${1}=${2}" >> "${FTLconf}"
}
@@ -75,6 +77,7 @@ changeFTLsetting() {
}
add_dnsmasq_setting() {
ensure_newline "${dnsmasqconfig}"
if [[ "${2}" != "" ]]; then
echo "${1}=${2}" >> "${dnsmasqconfig}"
else
@@ -146,6 +149,7 @@ SetWebPassword() {
ProcessDNSSettings() {
source "${setupVars}"
ensure_newline "${dnsmasqconfig}"
delete_dnsmasq_setting "server"
COUNTER=1
@@ -392,6 +396,7 @@ ProcessDHCPSettings() {
fi
# Write settings to file
# We do not need to ensure a newline here as the entire file is re-written
echo "###############################################################################
# DHCP SERVER CONFIG FILE AUTOMATICALLY POPULATED BY PI-HOLE WEB INTERFACE. #
# ANY CHANGES MADE TO THIS FILE WILL BE LOST ON CHANGE #
@@ -545,6 +550,7 @@ AddDHCPStaticAddress() {
ip="${args[3]}"
host="${args[4]}"
ensure_newline "${dhcpstaticconfig}"
if [[ "${ip}" == "noip" ]]; then
# Static host name
echo "dhcp-host=${mac},${host}" >> "${dhcpstaticconfig}"
@@ -689,6 +695,7 @@ AddCustomDNSAddress() {
ip="${args[2]}"
host="${args[3]}"
ensure_newline "${dnscustomfile}"
echo "${ip} ${host}" >> "${dnscustomfile}"
# Restart dnsmasq to load new custom DNS entries
@@ -711,6 +718,7 @@ AddCustomCNAMERecord() {
domain="${args[2]}"
target="${args[3]}"
ensure_newline "${dnscustomcnamefile}"
echo "cname=${domain},${target}" >> "${dnscustomcnamefile}"
# Restart dnsmasq to load new custom CNAME records

14
pihole
View File

@@ -20,6 +20,18 @@ PI_HOLE_BIN_DIR="/usr/local/bin"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source "${colfile}"
# Ensure there is a newline at the end of the file passed as argument
ensure_newline() {
# Check if the last line of the passed file is empty, if not, append a newline
# to the file to ensure we can append new content safely using echo "" >>
# later on
[ -n "$(tail -c1 "${1}")" ] && printf '\n' >> "${1}"
# There was also the suggestion of using a sed-magic call here, however, this
# had the drawback to updating all the file timestamps whenever the sed was
# run. This solution only updates the timestamp when actually appending a
# newline
}
webpageFunc() {
source "${PI_HOLE_SCRIPT_DIR}/webpage.sh"
main "$@"
@@ -191,6 +203,7 @@ Time:
local str="Pi-hole Disabled"
sed -i "/BLOCKING_ENABLED=/d" "${setupVars}"
ensure_newline "${setupVars}"
echo "BLOCKING_ENABLED=false" >> "${setupVars}"
fi
else
@@ -204,6 +217,7 @@ Time:
local str="Pi-hole Enabled"
sed -i "/BLOCKING_ENABLED=/d" "${setupVars}"
ensure_newline "${setupVars}"
echo "BLOCKING_ENABLED=true" >> "${setupVars}"
fi