Skip to content

Commit

Permalink
fix: add escaping for network management
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvinSchiller committed Apr 14, 2024
1 parent 341db96 commit 3417a0f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
23 changes: 18 additions & 5 deletions scripts/helperscripts/inc.networkHelper.sh
Original file line number Diff line number Diff line change
@@ -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!
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
44 changes: 22 additions & 22 deletions scripts/helperscripts/setup_autohotspot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,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 @@ -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}"

Expand Down Expand Up @@ -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}"

Expand Down

0 comments on commit 3417a0f

Please sign in to comment.