From 3417a0f5b40e36d764b8e8b2a47d877eea80488b Mon Sep 17 00:00:00 2001 From: Alvin Schiller <103769832+AlvinSchiller@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:44:19 +0000 Subject: [PATCH] fix: add escaping for network management --- scripts/helperscripts/inc.networkHelper.sh | 23 ++++++++--- scripts/helperscripts/setup_autohotspot.sh | 44 +++++++++++----------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/scripts/helperscripts/inc.networkHelper.sh b/scripts/helperscripts/inc.networkHelper.sh index ff0df181e..b031fd810 100644 --- a/scripts/helperscripts/inc.networkHelper.sh +++ b/scripts/helperscripts/inc.networkHelper.sh @@ -1,3 +1,16 @@ +_escape_for_shell() { + local escaped="${1//\"/\\\"}" + escaped="${escaped//\`/\\\`}" + escaped="${escaped//\$/\\\$}" + echo "$escaped" +} + +# 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" +} + _get_service_enablement() { local service="$1" local option="${2:+$2 }" # optional, dont't quote in 'systemctl' call! @@ -56,7 +69,7 @@ _get_passphrase_for_config() { local ssid="$1" local pass="$2" if [[ "${#pass}" -lt 64 ]]; then - pass=$(wpa_passphrase "$ssid" "$pass" | grep -vF '#psk' | grep -F "psk=" | cut -d = -f 2) + pass=$(wpa_passphrase "$(_escape_for_shell "$ssid")" "$(_escape_for_shell "$pass")" | grep -vF '#psk' | grep -F "psk=" | cut -d = -f 2) fi echo $pass } @@ -70,17 +83,17 @@ add_wireless_network() { pass=$(_get_passphrase_for_config "$ssid" "$pass") if [[ $(is_dhcpcd_enabled) == true ]]; then - if ! sudo cat "$WPA_CONF" | grep -qF "ssid=\"${ssid}\"" ; then - local wpa_network_with_dummy_psk=$(wpa_passphrase "$ssid" "dummypsk") + if ! sudo cat "$WPA_CONF" | grep -qF "ssid=\"$(_escape_for_shell "$ssid")\"" ; then + local wpa_network_with_dummy_psk=$(wpa_passphrase "$(_escape_for_shell "$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}" ) + local wpa_network=$(echo "$wpa_network_with_dummy_psk" | sed -e '/#psk/d' -e "s|psk=.*$|psk="$(_escape_for_sed "$pass")"|" -e "/^}/i\\\tpriority=${prio}" ) sudo bash -c "echo '${wpa_network}' >> $WPA_CONF" fi fi fi if [[ $(is_NetworkManager_enabled) == true ]]; then - if ! nmcli -g NAME,TYPE connection show | grep -F "wireless" | grep -qwF "$ssid"; then + if ! nmcli -g NAME,TYPE connection show | grep -F "wireless" | grep -qwF "$(_escape_for_shell "$ssid")"; then sudo nmcli connection add type wifi con-name "$ssid" ifname "$interface" autoconnect yes mode infrastructure ssid "$ssid" sudo nmcli connection modify "$ssid" wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$pass" conn.autoconnect-p "$prio" fi diff --git a/scripts/helperscripts/setup_autohotspot.sh b/scripts/helperscripts/setup_autohotspot.sh index 881a8e601..f74ed88d9 100644 --- a/scripts/helperscripts/setup_autohotspot.sh +++ b/scripts/helperscripts/setup_autohotspot.sh @@ -92,8 +92,8 @@ _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}" @@ -101,10 +101,10 @@ _install_autohotspot_dhcpcd() { 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}" @@ -112,7 +112,7 @@ _install_autohotspot_dhcpcd() { 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}" @@ -130,23 +130,23 @@ _install_autohotspot_dhcpcd() { # 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}" @@ -201,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}"