diff --git a/src/wifi_connection.py b/src/wifi_connection.py index 59558c5..be9e7ab 100644 --- a/src/wifi_connection.py +++ b/src/wifi_connection.py @@ -533,6 +533,103 @@ class WiFiConnectionService: return output + def _get_active_access_point_bssid(self, interface): + + device_path = self._device_path( + interface + ) + + parts = self._get_property( + device_path, + NM_DEVICE_WIRELESS, + "ActiveAccessPoint" + ) + + if len(parts) != 2 or parts[0] != "o": + raise WiFiConnectionError( + "Unexpected D-Bus type for ActiveAccessPoint." + ) + + object_path = parts[1] + + if ( + object_path == "/" + or + not object_path.startswith( + "/org/freedesktop/NetworkManager/AccessPoint/" + ) + ): + raise WiFiConnectionError( + "NetworkManager did not return the active access point." + ) + + parts = self._get_property( + object_path, + NM_ACCESS_POINT, + "HwAddress" + ) + + if len(parts) != 2 or parts[0] != "s": + raise WiFiConnectionError( + "Unexpected D-Bus type for the active access point BSSID." + ) + + bssid = parts[1] + + self._validate_bssid( + bssid + ) + + return bssid.upper() + + def _handle_failed_profile_creation( + self, + profile_name, + creation_error + ): + + try: + + output = self._nmcli( + "--terse", + "--escape", + "yes", + "--fields", + "NAME", + "connection", + "show" + ) + + except NetworkCommandError as inspection_error: + + raise NetworkCleanupError( + "NetworkManager failed to create a temporary profile, " + "and its state could not be determined. " + f"Profile name: {profile_name}. " + f"Inspection failed: {inspection_error}" + ) from creation_error + + if profile_name in output.splitlines(): + + try: + + self._nmcli( + "connection", + "delete", + "id", + profile_name + ) + + except NetworkCommandError as cleanup_error: + + raise NetworkCleanupError( + "NetworkManager may have created a temporary profile, " + "but it could not be deleted: " + f"{cleanup_error}" + ) from creation_error + + raise creation_error + def _create_temporary_profile( self, interface, @@ -575,9 +672,18 @@ class WiFiConnectionService: ] ) - self._nmcli( - *arguments - ) + try: + + self._nmcli( + *arguments + ) + + except NetworkCommandError as error: + + self._handle_failed_profile_creation( + profile_name, + error + ) try: @@ -663,7 +769,7 @@ class WiFiConnectionService: "\n" ) - except Exception: + except Exception as error: try: os.close( @@ -676,8 +782,14 @@ class WiFiConnectionService: os.unlink( filename ) - except OSError: - pass + + except OSError as cleanup_error: + + raise NetworkCleanupError( + "Could not prepare the temporary password file. " + "The file could not be deleted: " + f"{cleanup_error}" + ) from error raise @@ -704,6 +816,8 @@ class WiFiConnectionService: password_file = None + operation_error = None + try: if access_point.key_mgmt: @@ -735,6 +849,10 @@ class WiFiConnectionService: timeout=90 ) + except Exception as error: + + operation_error = error + finally: if password_file: @@ -743,8 +861,25 @@ class WiFiConnectionService: os.unlink( password_file ) - except OSError: - pass + + except OSError as cleanup_error: + + if operation_error: + + raise NetworkCleanupError( + "Wi-Fi activation failed: " + f"{operation_error}. " + "The temporary password file could not be deleted: " + f"{cleanup_error}" + ) from operation_error + + raise NetworkCleanupError( + "The temporary password file could not be deleted: " + f"{cleanup_error}" + ) from cleanup_error + + if operation_error: + raise operation_error def _delete_profile(self, profile_uuid): @@ -846,6 +981,14 @@ class WiFiConnectionService: interface ) + previous_bssid = None + + if previous_uuid: + + previous_bssid = self._get_active_access_point_bssid( + interface + ) + profile_uuid = None success = False operation_error = None @@ -891,6 +1034,8 @@ class WiFiConnectionService: previous_uuid, "ifname", interface, + "ap", + previous_bssid, timeout=90 )