Skip to content

Commit

Permalink
Installation fixes and refactoring (MiczFlor#2350)
Browse files Browse the repository at this point in the history
* fix: added vars into logfile

* refactor: split finish_installation routine

extracted rfid_reader setup

* refactor: removed "Replace homedir" code in installation

v2 will not be refactored to be used with a different user

* fix: add escaping for all vars written to PhonieboxInstall.conf

* refactor: change and unify sed delimiter to pipe char

* fix: add escaping for sed replacements

* refactor: moved network setup into install_main

* refactor: introduce helper script, moved functions

* fix: add escaping for autohotspot

* fix: command for special char handling

* fix: update testdata for special chars
  • Loading branch information
AlvinSchiller authored Apr 23, 2024
1 parent d76a7ef commit 4626e5d
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 216 deletions.
17 changes: 17 additions & 0 deletions scripts/helperscripts/inc.helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Reads a textfile and pipes all lines as args to the given command.
# Does filter out comments.
# Arguments:
# 1 : textfile to read
# 2... : command to receive args (e.g. 'echo', 'apt-get -y install', ...)
call_with_args_from_file () {
local package_file="$1"
shift

sed 's|#.*||g' ${package_file} | xargs "$@"
}

# escape relevant chars for strings used in 'sed' commands. implies delimiter char '|'
escape_for_sed() {
local escaped=$(echo "$1" | sed -e 's/[\&'\''|]/\\&/g')
echo "$escaped"
}
2 changes: 1 addition & 1 deletion scripts/helperscripts/inc.networkHelper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ add_wireless_network() {
local wpa_network_with_dummy_psk=$(wpa_passphrase "$ssid" "dummypsk")
if echo "$wpa_network_with_dummy_psk" | grep -qF 'network='; then
local wpa_network=$(echo "$wpa_network_with_dummy_psk" | sed -e '/#psk/d' -e "s/psk=.*$/psk=${pass}/" -e "/^}/i\\\tpriority=${prio}" )
sudo bash -c "echo '${wpa_network}' >> $WPA_CONF"
echo "${wpa_network}" | sudo tee -a "$WPA_CONF" > /dev/null
fi
fi
fi
Expand Down
59 changes: 24 additions & 35 deletions scripts/helperscripts/setup_autohotspot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ if [[ "$#" -lt 2 || ( "${AUTOHOTSPOTconfig}" != "NO" && "${AUTOHOTSPOTconfig}" !
exit 1
fi

# Reads a textfile and pipes all lines as args to the given command.
# Does filter out comments.
# Arguments:
# 1 : textfile to read
# 2... : command to receive args (e.g. 'echo', 'apt-get -y install', ...)
call_with_args_from_file () {
local package_file="$1"
shift

sed 's/#.*//g' ${package_file} | xargs "$@"
}

_get_last_ip_segment() {
local ip="$1"
echo $ip | cut -d'.' -f1-3
Expand Down Expand Up @@ -80,6 +68,7 @@ wifi_interface=wlan0
ip_without_last_segment=$(_get_last_ip_segment $AUTOHOTSPOTip)
autohotspot_profile="Phoniebox_Hotspot"

source "${JUKEBOX_HOME_DIR}"/scripts/helperscripts/inc.helper.sh
source "${JUKEBOX_HOME_DIR}"/scripts/helperscripts/inc.networkHelper.sh

_install_autohotspot_dhcpcd() {
Expand All @@ -103,27 +92,27 @@ _install_autohotspot_dhcpcd() {
config_file_backup "${dnsmasq_conf}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/dnsmasq.conf "${dnsmasq_conf}"
sudo sed -i "s|%WIFI_INTERFACE%|${wifi_interface}|g" "${dnsmasq_conf}"
sudo sed -i "s|%IP_WITHOUT_LAST_SEGMENT%|${ip_without_last_segment}|g" "${dnsmasq_conf}"
sudo sed -i "s|%WIFI_INTERFACE%|$(escape_for_sed "${wifi_interface}")|g" "${dnsmasq_conf}"
sudo sed -i "s|%IP_WITHOUT_LAST_SEGMENT%|$(escape_for_sed "${ip_without_last_segment}")|g" "${dnsmasq_conf}"
sudo chown root:root "${dnsmasq_conf}"
sudo chmod 644 "${dnsmasq_conf}"

# configure hostapd conf
config_file_backup "${hostapd_conf}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/hostapd.conf "${hostapd_conf}"
sudo sed -i "s|%WIFI_INTERFACE%|${wifi_interface}|g" "${hostapd_conf}"
sudo sed -i "s|%AUTOHOTSPOTssid%|${AUTOHOTSPOTssid}|g" "${hostapd_conf}"
sudo sed -i "s|%AUTOHOTSPOTpass%|${AUTOHOTSPOTpass}|g" "${hostapd_conf}"
sudo sed -i "s|%AUTOHOTSPOTcountryCode%|${AUTOHOTSPOTcountryCode}|g" "${hostapd_conf}"
sudo sed -i "s|%WIFI_INTERFACE%|$(escape_for_sed "${wifi_interface}")|g" "${hostapd_conf}"
sudo sed -i "s|%AUTOHOTSPOTssid%|$(escape_for_sed "${AUTOHOTSPOTssid}")|g" "${hostapd_conf}"
sudo sed -i "s|%AUTOHOTSPOTpass%|$(escape_for_sed "${AUTOHOTSPOTpass}")|g" "${hostapd_conf}"
sudo sed -i "s|%AUTOHOTSPOTcountryCode%|$(escape_for_sed "${AUTOHOTSPOTcountryCode}")|g" "${hostapd_conf}"
sudo chown root:root "${hostapd_conf}"
sudo chmod 644 "${hostapd_conf}"

# configure hostapd daemon
config_file_backup "${hostapd_deamon}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/hostapd "${hostapd_deamon}"
sudo sed -i "s|%HOSTAPD_CONF%|${hostapd_conf}|g" "${hostapd_deamon}"
sudo sed -i "s|%HOSTAPD_CONF%|$(escape_for_sed "${hostapd_conf}")|g" "${hostapd_deamon}"
sudo chown root:root "${hostapd_deamon}"
sudo chmod 644 "${hostapd_deamon}"

Expand All @@ -136,28 +125,28 @@ _install_autohotspot_dhcpcd() {
fi

if [[ ! $(grep -w "${dhcpcd_conf_nohook_wpa_supplicant}" ${dhcpcd_conf}) ]]; then
sudo bash -c "echo ${dhcpcd_conf_nohook_wpa_supplicant} >> ${dhcpcd_conf}"
echo "${dhcpcd_conf_nohook_wpa_supplicant}" | sudo tee -a "${dhcpcd_conf}" > /dev/null
fi

# create service to trigger hotspot
sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/autohotspot "${autohotspot_script}"
sudo sed -i "s|%WIFI_INTERFACE%|${wifi_interface}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_IP%|${AUTOHOTSPOTip}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_SERVICE_DAEMON%|${autohotspot_service_daemon}|g" "${autohotspot_script}"
sudo sed -i "s|%WIFI_INTERFACE%|$(escape_for_sed "${wifi_interface}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_IP%|$(escape_for_sed "${AUTOHOTSPOTip}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_SERVICE_DAEMON%|$(escape_for_sed "${autohotspot_service_daemon}")|g" "${autohotspot_script}"
sudo chmod +x "${autohotspot_script}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/autohotspot-daemon.service "${autohotspot_service_daemon_path}"
sudo sed -i "s|%WIFI_INTERFACE%|${wifi_interface}|g" "${autohotspot_service_daemon_path}"
sudo sed -i "s|%WIFI_INTERFACE%|$(escape_for_sed "${wifi_interface}")|g" "${autohotspot_service_daemon_path}"
sudo chown root:root "${autohotspot_service_daemon_path}"
sudo chmod 644 "${autohotspot_service_daemon_path}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/autohotspot.service "${autohotspot_service_path}"
sudo sed -i "s|%AUTOHOTSPOT_SCRIPT%|${autohotspot_script}|g" "${autohotspot_service_path}"
sudo sed -i "s|%AUTOHOTSPOT_SCRIPT%|$(escape_for_sed "${autohotspot_script}")|g" "${autohotspot_service_path}"
sudo chown root:root "${autohotspot_service_path}"
sudo chmod 644 "${autohotspot_service_path}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/dhcpcd/autohotspot.timer "${autohotspot_timer_path}"
sudo sed -i "s|%AUTOHOTSPOT_SERVICE%|${autohotspot_service}|g" "${autohotspot_timer_path}"
sudo sed -i "s|%AUTOHOTSPOT_SERVICE%|$(escape_for_sed "${autohotspot_service}")|g" "${autohotspot_timer_path}"
sudo chown root:root "${autohotspot_timer_path}"
sudo chmod 644 "${autohotspot_timer_path}"

Expand Down Expand Up @@ -212,22 +201,22 @@ _install_autohotspot_NetworkManager() {

# create service to trigger hotspot
sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/NetworkManager/autohotspot "${autohotspot_script}"
sudo sed -i "s|%WIFI_INTERFACE%|${wifi_interface}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_PROFILE%|${autohotspot_profile}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_SSID%|${AUTOHOTSPOTssid}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_PASSWORD%|${AUTOHOTSPOTpass}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_IP%|${AUTOHOTSPOTip}|g" "${autohotspot_script}"
sudo sed -i "s|%IP_WITHOUT_LAST_SEGMENT%|${ip_without_last_segment}|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_TIMER_NAME%|${autohotspot_timer}|g" "${autohotspot_script}"
sudo sed -i "s|%WIFI_INTERFACE%|$(escape_for_sed "${wifi_interface}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_PROFILE%|$(escape_for_sed "${autohotspot_profile}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_SSID%|$(escape_for_sed "${AUTOHOTSPOTssid}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_PASSWORD%|$(escape_for_sed "${AUTOHOTSPOTpass}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_IP%|$(escape_for_sed "${AUTOHOTSPOTip}")|g" "${autohotspot_script}"
sudo sed -i "s|%IP_WITHOUT_LAST_SEGMENT%|$(escape_for_sed "${ip_without_last_segment}")|g" "${autohotspot_script}"
sudo sed -i "s|%AUTOHOTSPOT_TIMER_NAME%|$(escape_for_sed "${autohotspot_timer}")|g" "${autohotspot_script}"
sudo chmod +x "${autohotspot_script}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/NetworkManager/autohotspot.service "${autohotspot_service_path}"
sudo sed -i "s|%AUTOHOTSPOT_SCRIPT%|${autohotspot_script}|g" "${autohotspot_service_path}"
sudo sed -i "s|%AUTOHOTSPOT_SCRIPT%|$(escape_for_sed "${autohotspot_script}")|g" "${autohotspot_service_path}"
sudo chown root:root "${autohotspot_service_path}"
sudo chmod 644 "${autohotspot_service_path}"

sudo cp "${JUKEBOX_HOME_DIR}"/misc/sampleconfigs/autohotspot/NetworkManager/autohotspot.timer "${autohotspot_timer_path}"
sudo sed -i "s|%AUTOHOTSPOT_SERVICE%|${autohotspot_service}|g" "${autohotspot_timer_path}"
sudo sed -i "s|%AUTOHOTSPOT_SERVICE%|$(escape_for_sed "${autohotspot_service}")|g" "${autohotspot_timer_path}"
sudo chown root:root "${autohotspot_timer_path}"
sudo chmod 644 "${autohotspot_timer_path}"

Expand Down
Loading

0 comments on commit 4626e5d

Please sign in to comment.