Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illogical WiFi configuration in openhabian-config #1693

Closed
Nadahar opened this issue Jun 19, 2022 · 6 comments
Closed

Illogical WiFi configuration in openhabian-config #1693

Nadahar opened this issue Jun 19, 2022 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@Nadahar
Copy link
Contributor

Nadahar commented Jun 19, 2022

Issue information:

Something is "off" with the logic regarding the WiFi configuration. The Disable WiFi option will run:

elif [[ $1 == "disable" ]]; then
if (whiptail --title "WiFi is currently enabled" --defaultno --yesno "$enabledText" 10 80); then
cond_redirect enable_disable_wifi "disable"
echo -n "$(timestamp) [openHABian] Cleaning up old WiFi configuration... "
if cond_redirect sed -i -e '/allow-hotplug wlan0/d; /iface wlan0 inet manual/d; /wpa-roam \/etc\/wpa_supplicant\/wpa_supplicant.conf/d; /iface default inet dhcp/d' /etc/network/interfaces; then echo "OK (will reboot now)"; else echo "FAILED"; return 1; fi
whiptail --title "Operation successful" --msgbox "Setup was successful. Please reboot now." 7 80
else
echo "CANCELED"
return 0
fi
fi

...which will call enable_disable_wifi() here:
enable_disable_wifi() {
if ! is_pi; then return 0; fi
if [[ $1 == "enable" ]]; then
echo -n "$(timestamp) [openHABian] Enabling WiFi... "
if grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then
if sed -i -E '/^[[:space:]]*dtoverlay=(pi3-)?disable-wifi/d' /boot/config.txt; then echo "OK (reboot required)"; else echo "FAILED"; return 1; fi
else
echo "OK"
fi
elif [[ $1 == "disable" ]]; then
echo -n "$(timestamp) [openHABian] Disabling WiFi... "
if ! grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then
if echo "dtoverlay=disable-wifi" >> /boot/config.txt; then echo "OK (reboot required)"; else echo "FAILED"; return 1; fi
else
echo "OK"
fi
fi
}

This will (physically) disabled the onboard WiFi adapter in the Raspberry Pi as far as I can understand. I assume it will do nothing to WiFi dongles..? If so, it should probably be named a little differently, to make sure it is clear that this is about the onboard WiFi.

After rebooting, as instructed to, wlan0 is gone and all is well in that sense. But, the WiFi configuration in /etc/wpa_supplicant/wpa_supplicant.conf is left untouched, still containing the SSID and PSK from the previous configuration if it was previously configured. I would assume this means that WiFi dongles would continue to work even after a reboot.

Now comes the problem. If you enter openhabian-config again, the System Settings under 37 are still the same. You can choose between WiFi setup and Disable WiFi, although both these options will fail. It's not possible to disable the (onboard) WiFi adapter, since it already is disabled. The option won't complain, it will actually falsely assert that WiFi is still enabled:

                    ┌────────────────────────┤ WiFi is currently enabled ├─────────────────────────┐
                    │                                                                              │
                    │ WiFi is currently enabled on your box.                                       │
                    │                                                                              │
                    │ ATTENTION:                                                                   │
                    │ Would you like to disable WiFi and use Ethernet?                             │
                    │                                                                              │
                    │                     <Yes>                        <No>                        │
                    │                                                                              │
                    └──────────────────────────────────────────────────────────────────────────────┘

...it will then continue to pretend to do something and ask you to reboot.

The other option WiFi setup is more troubling though, as it will present you with this error:

                    ┌──────────────────────────────────────────────────────────────────────────────┐
                    │                                                                              │
                    │ There was an error or interruption during the execution of:                  │
                    │   "30 | System Settings"                                                     │
                    │                                                                              │
                    │ Please try again. If the error persists, please read                         │
                    │ /opt/openhabian/docs/openhabian-DEBUG.md or                                  │
                    │ https://github.com/openhab/openhabian/blob/main/docs/openhabian-DEBUG.md how │
                    │ to proceed.                                                                  │
                    │                                                                              │
                    │                                                                              │
                    │                                    <Ok>                                      │
                    │                                                                              │
                    └──────────────────────────────────────────────────────────────────────────────┘

...which doesn't actually tell you anything about "what is going on". A further look at the script will reveal what really happens:

if [[ $1 == "setup" ]]; then
if grep -qsE "^[[:space:]]*dtoverlay=(pi3-)?disable-wifi" /boot/config.txt; then
if (whiptail --title "WiFi is currently disabled" --yesno "$disabledText" 10 80); then
cond_redirect enable_disable_wifi "enable"
else
echo "CANCELED"
return 0
fi
fi
if is_pi_wlan; then
if ! dpkg -s 'firmware-brcm80211' &> /dev/null; then
echo -n "$(timestamp) [openHABian] Installing WiFi firmware... "
if cond_redirect apt-get install --yes firmware-brcm80211; then echo "OK"; else echo "FAILED"; return 1; fi
fi
fi
if ! dpkg -s 'wpasupplicant' 'wireless-tools' &> /dev/null; then
echo -n "$(timestamp) [openHABian] Installing WiFi prerequisites (wpasupplicant, wireless-tools)... "
if cond_redirect apt-get install --yes wpasupplicant wireless-tools; then echo "OK"; else echo "FAILED"; return 1; fi
fi
echo -n "$(timestamp) [openHABian] Checking if WiFi is working... "
if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
# WiFi might be blocked
rfkill unblock wifi
ip link set wlan0 up
if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
echo "FAILED"
echo -e "\\nI was not able to turn on the WiFi\\nHere is some more information:\\n"
rfkill list all
ip a
return 1
else
echo "OK"
fi
else
echo "OK"
fi
echo -n "$(timestamp) [openHABian] Configuring WiFi... "
wifiNetworkList="$(iwlist wlan0 scan | grep ESSID | sed -e 's/^[[:space:]]*ESSID://g; s/"//g; /^[[:space:]]*$/d')"
if [[ -z $wifiNetworkList ]]; then echo "FAILED (no networks found)"; return 1; fi
if ! wifiSSID="$(whiptail --title "Wifi setup" --inputbox "\\nWhich WiFi network would you like do you want to connect to?\\n\\nNetwork List:\\n${wifiNetworkList}" 20 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi
if [[ -z $wifiSSID ]]; then echo "FAILED (blank SSID)"; return 1; fi
if ! wifiPassword="$(whiptail --title "Wifi Setup" --passwordbox "\\nWhat's the password for ${wifiSSID}?" 9 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi
if ! wifiConfig="$(wpa_passphrase "${wifiSSID}" "${wifiPassword}")"; then echo "FAILED (${wifiConfig})"; return 1; fi
if ! wifiCountry="$(whiptail --title "Wifi setup" --inputbox "\\nPlease enter the two-letter country code matching your region (US, DE, NZ, AU)...\\n\\nSee https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" 12 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi
# Check if the country code is valid, valid country codes are followed by spaces in /usr/share/zoneinfo/zone.tab
if grep -qs "^${wifiCountry^^}[[:space:]]" /usr/share/zoneinfo/zone.tab; then
wifiCountry="${wifiCountry^^}"
else
echo "FAILED (${wifiCountry} is not a valid country code found in '/usr/share/zoneinfo/zone.tab')"
return 1
fi
if ! cond_redirect mkdir -p /etc/wpa_supplicant; then echo "FAILED (create directory)"; return 1; fi
if ! echo -e "# WiFi configuration generated by openHABian\\ncountry=$wifiCountry\\nctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\\nupdate_config=1\\n# Network configuration was created by wpa_passphrase to ensure correct handling of special characters\\n${wifiConfig//\}/\\tkey_mgmt=WPA-PSK\\n\}}" > /etc/wpa_supplicant/wpa_supplicant.conf; then echo "FAILED (create configuration)"; return 1; fi
if cond_redirect sed -i -e 's|REGDOMAIN=.*$|REGDOMAIN='"${wifiCountry}"'|g' /etc/default/crda; then echo "OK"; else echo "FAILED (set country)"; return 1; fi
echo -n "$(timestamp) [openHABian] Configuring network... "
if grep -qs "wlan0" /etc/network/interfaces; then
cond_echo "\\nNot writing to '/etc/network/interfaces', wlan0 entry already available. You might need to check, adopt or remove these lines."
else
echo -e "\\nallow-hotplug wlan0\\niface wlan0 inet manual\\nwpa-roam /etc/wpa_supplicant/wpa_supplicant.conf\\niface default inet dhcp" >> /etc/network/interfaces
fi
if ! cond_redirect wpa_cli reconfigure; then echo "FAILED (reconfigure)"; return 1; fi
if ! cond_redirect ip link set wlan0 down; then echo "FAILED (down)"; return 1; fi
if cond_redirect ip link set wlan0 up; then echo "OK (reboot now)"; else echo "FAILED (up)"; return 1; fi
whiptail --title "Operation successful" --msgbox "Setup was successful. The credentials provided were not tested. Please reboot now." 8 80

This is where the "lacking" logic all comes together to make everything very confusing. The "WiFi setup" actually starts by enabling the (onboard) WiFi adapter. But it doesn't stop and ask for a reboot if the adapter really is disabled in /boot/config.txt. Thus, the next steps will fail, specifically this part:

if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
# WiFi might be blocked
rfkill unblock wifi
ip link set wlan0 up
if iwlist wlan0 scan |& grep -qs "Interface doesn't support scanning"; then
echo "FAILED"
echo -e "\\nI was not able to turn on the WiFi\\nHere is some more information:\\n"
rfkill list all
ip a
return 1
else
echo "OK"
fi

What has actually happened is that the (onboard) WiFi adapter has been enabled, and if you reboot and then enter openhabian-config and run WiFi setup again it will actually work. But, the way to get there is highly illogical and confusing.

Suggested fix

I really don't know bash scripting, so I would probably spend a lot of time trying to get something working put together. But, since your EULA/DCO is impossible for me to comply with, this would be futile to attempt anyway. I'm therefore going to try to explain what I think should be done instead.

I think making this logical would require:

  • Isolating enabling/disabling of the onboard WiFi adapter from the WiFi configuration. I would imagine that making the "second option" under 37 "dynamic" would be the most elegant and logical option - so that it would check the state before populating the menu, and then display either Enable onboard WiFi or Disable onboard WiFi accordingly. Devices without onboard WiFi shouldn't have this option at all.
  • Establish a way to "deconfigure" the SSID and PSK. I imagine that an extra option under WiFi setup would suffice - that instead of entering a SSID there would be a way to chose "clear configuration" or similar. This option should make /etc/wpa_supplicant/wpa_supplicant.conf be in a "default" state with no SSID/PSK configured.

System information:

OpenHABian 1.7.3 on a Raspberry Pi 4.

@Nadahar
Copy link
Contributor Author

Nadahar commented Jun 19, 2022

I forgot to mention that the reason this isn't a problem the first time you run Setup WiFi is that the onboard WiFi adapter is enabled "out of the box". The reason it isn't "active" if you do an installation without configuring SSID/PSK is simply the lack of configuration in /etc/wpa_supplicant/wpa_supplicant.conf. So, the WiFi adapter will be there, but it won't connect to any AP.

@mstormi mstormi added the help wanted Extra attention is needed label Sep 12, 2022
@mstormi
Copy link
Contributor

mstormi commented Feb 14, 2023

@Nadahar if you could come up with a PR I would merge it but I don't have the time to cope with it

Just provide the code don't bother with signing etc I'll do.

@mstormi
Copy link
Contributor

mstormi commented May 12, 2024

Given networking setup changed a lot in bookworm, I've removed the WiFi setup options from the menu for now.

Anyone feel free to contribute a PR to re-add them.

@mstormi mstormi closed this as completed May 12, 2024
@Nadahar
Copy link
Contributor Author

Nadahar commented May 12, 2024

Does that mean that we must prevent openhabian from updating or things will break, or does it only concern new installations?

@mstormi
Copy link
Contributor

mstormi commented May 12, 2024

I don't understand your question. I've only removed the options why would that break anything.

You will only get bookworm on fresh installs.

@Nadahar
Copy link
Contributor Author

Nadahar commented May 12, 2024

You will only get bookworm on fresh installs.

Ok, that's basically what I needed to know.

I don't understand your question. I've only removed the options why would that break anything.

I'm dealing with so many different systems, and it's been a long time since I did the openhabian setup now, so I simply don't remember how it works. What I do remember is that some things openhabian "leave alone", while other things get overwritten by scripts or something (like my SMB shares that keep breaking/getting their permissions removed). Since I don't have any overview of what is what, I just wanted to know if I needed to take special care to prevent Wi-Fi from breaking or not, without having to dive into all the details right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants