Skip to content

Commit 1af76e7

Browse files
author
Alex J Lennon
committed
improv: Directly edit connection file to add psk-flags=0
NetworkManager 1.46.0 does not write psk-flags=0 to the keyfile even when set via nmcli.connection.add() or nmcli connection modify. The NetworkManager patch REQUIRES psk-flags=0 to be explicitly in the file to detect that secrets are stored. Since nmcli modify doesn't work, directly edit the connection file to add psk-flags=0 to the [wifi-security] section. This ensures the setting is persisted and the patch will work correctly after reboots. Falls back to nmcli modify if file editing fails due to permissions. Applied to both: - Generic onboarding-server.py - imx93-jaguar-eink/onboarding-server-eink.py
1 parent 8ef23c0 commit 1af76e7

File tree

2 files changed

+91
-30
lines changed

2 files changed

+91
-30
lines changed

recipes-devtools/python/python3-improv/imx93-jaguar-eink/onboarding-server-eink.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,54 @@ def wifi_connect(ssid: str, passwd: str) -> Optional[list[str]]:
172172
print(f'Could not add new connection {CON_NAME}: {e}')
173173
return None
174174

175-
# CRITICAL: Explicitly modify connection to ensure psk-flags=0 is written to file
176-
# NetworkManager 1.46.0 may not write psk-flags=0 to the keyfile if it's the default,
175+
# CRITICAL: Explicitly add psk-flags=0 to connection file
176+
# NetworkManager 1.46.0 may not write psk-flags=0 to the keyfile even when set,
177177
# but the NetworkManager patch REQUIRES it to be explicitly in the file to detect
178-
# that secrets are stored. Use nmcli connection modify to force it to be written.
178+
# that secrets are stored. Directly edit the file to ensure it's present.
179+
connection_file = f"/etc/NetworkManager/system-connections/{CON_NAME}.nmconnection"
179180
try:
180-
subprocess.run(['nmcli', 'connection', 'modify', f"{CON_NAME}",
181-
'802-11-wireless-security.psk-flags', '0'],
182-
check=True, capture_output=True, timeout=5)
183-
logger.debug(f"Explicitly set psk-flags=0 for connection {CON_NAME}")
184-
except subprocess.CalledProcessError as e:
185-
logger.warning(f"Could not explicitly set psk-flags=0 (exit code {e.returncode}): {e.stderr.decode() if e.stderr else 'unknown error'}")
186-
# Continue - connection was created, but psk-flags may not be in file
187-
except subprocess.TimeoutExpired as e:
188-
logger.warning(f"Timeout setting psk-flags=0: {e}")
189-
except FileNotFoundError:
190-
logger.error(f"nmcli command not found - cannot set psk-flags=0")
181+
if os.path.exists(connection_file):
182+
# Read the file
183+
with open(connection_file, 'r') as f:
184+
content = f.read()
185+
186+
# Check if psk-flags=0 is already in the file
187+
if 'psk-flags=0' not in content and 'psk-flags=0\n' not in content:
188+
# Add psk-flags=0 to [wifi-security] section
189+
# Find [wifi-security] section and add psk-flags=0 after psk line
190+
pattern = r'(\[wifi-security\]\n(?:[^\[]*\n)*?psk=[^\n]+\n)'
191+
replacement = r'\1psk-flags=0\n'
192+
new_content = re.sub(pattern, replacement, content)
193+
194+
# If pattern didn't match, try adding it after [wifi-security]
195+
if new_content == content:
196+
pattern = r'(\[wifi-security\]\n)'
197+
replacement = r'\1psk-flags=0\n'
198+
new_content = re.sub(pattern, replacement, content)
199+
200+
# Write the file back (requires root, but we're running as root via sudo)
201+
if new_content != content:
202+
with open(connection_file, 'w') as f:
203+
f.write(new_content)
204+
logger.info(f"Added psk-flags=0 to connection file {connection_file}")
205+
else:
206+
logger.warning(f"Could not find [wifi-security] section to add psk-flags=0")
207+
else:
208+
logger.debug(f"psk-flags=0 already in connection file")
209+
else:
210+
logger.warning(f"Connection file not found at {connection_file} - cannot add psk-flags=0")
211+
except PermissionError as e:
212+
logger.warning(f"Permission denied editing connection file {connection_file}: {e}")
213+
# Try nmcli modify as fallback
214+
try:
215+
subprocess.run(['nmcli', 'connection', 'modify', f"{CON_NAME}",
216+
'802-11-wireless-security.psk-flags', '0'],
217+
check=True, capture_output=True, timeout=5)
218+
logger.debug(f"Used nmcli to set psk-flags=0 (file edit failed)")
219+
except Exception as e2:
220+
logger.warning(f"nmcli modify also failed: {e2}")
191221
except Exception as e:
192-
logger.warning(f"Unexpected error setting psk-flags=0: {e}")
222+
logger.warning(f"Unexpected error adding psk-flags=0 to file: {e}", exc_info=True)
193223

194224
# NetworkManager automatically saves connections when created/modified
195225
# In NetworkManager 1.46.0+, connections are saved automatically to

recipes-devtools/python/python3-improv/onboarding-server.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import nmcli
1919
import subprocess
2020
import os
21+
import re
2122

2223
logging.basicConfig(level=logging.DEBUG)
2324
logger = logging.getLogger(name=__name__)
@@ -128,24 +129,54 @@ def wifi_connect(ssid: str, passwd: str) -> Optional[list[str]]:
128129
print(f'Could not add new connection {CON_NAME}: {e}')
129130
return None
130131

131-
# CRITICAL: Explicitly modify connection to ensure psk-flags=0 is written to file
132-
# NetworkManager 1.46.0 may not write psk-flags=0 to the keyfile if it's the default,
132+
# CRITICAL: Explicitly add psk-flags=0 to connection file
133+
# NetworkManager 1.46.0 may not write psk-flags=0 to the keyfile even when set,
133134
# but the NetworkManager patch REQUIRES it to be explicitly in the file to detect
134-
# that secrets are stored. Use nmcli connection modify to force it to be written.
135+
# that secrets are stored. Directly edit the file to ensure it's present.
136+
connection_file = f"/etc/NetworkManager/system-connections/{CON_NAME}.nmconnection"
135137
try:
136-
subprocess.run(['nmcli', 'connection', 'modify', f"{CON_NAME}",
137-
'802-11-wireless-security.psk-flags', '0'],
138-
check=True, capture_output=True, timeout=5)
139-
logger.debug(f"Explicitly set psk-flags=0 for connection {CON_NAME}")
140-
except subprocess.CalledProcessError as e:
141-
logger.warning(f"Could not explicitly set psk-flags=0 (exit code {e.returncode}): {e.stderr.decode() if e.stderr else 'unknown error'}")
142-
# Continue - connection was created, but psk-flags may not be in file
143-
except subprocess.TimeoutExpired as e:
144-
logger.warning(f"Timeout setting psk-flags=0: {e}")
145-
except FileNotFoundError:
146-
logger.error(f"nmcli command not found - cannot set psk-flags=0")
138+
if os.path.exists(connection_file):
139+
# Read the file
140+
with open(connection_file, 'r') as f:
141+
content = f.read()
142+
143+
# Check if psk-flags=0 is already in the file
144+
if 'psk-flags=0' not in content and 'psk-flags=0\n' not in content:
145+
# Add psk-flags=0 to [wifi-security] section
146+
# Find [wifi-security] section and add psk-flags=0 after psk line
147+
pattern = r'(\[wifi-security\]\n(?:[^\[]*\n)*?psk=[^\n]+\n)'
148+
replacement = r'\1psk-flags=0\n'
149+
new_content = re.sub(pattern, replacement, content)
150+
151+
# If pattern didn't match, try adding it after [wifi-security]
152+
if new_content == content:
153+
pattern = r'(\[wifi-security\]\n)'
154+
replacement = r'\1psk-flags=0\n'
155+
new_content = re.sub(pattern, replacement, content)
156+
157+
# Write the file back (requires root, but we're running as root via sudo)
158+
if new_content != content:
159+
with open(connection_file, 'w') as f:
160+
f.write(new_content)
161+
logger.info(f"Added psk-flags=0 to connection file {connection_file}")
162+
else:
163+
logger.warning(f"Could not find [wifi-security] section to add psk-flags=0")
164+
else:
165+
logger.debug(f"psk-flags=0 already in connection file")
166+
else:
167+
logger.warning(f"Connection file not found at {connection_file} - cannot add psk-flags=0")
168+
except PermissionError as e:
169+
logger.warning(f"Permission denied editing connection file {connection_file}: {e}")
170+
# Try nmcli modify as fallback
171+
try:
172+
subprocess.run(['nmcli', 'connection', 'modify', f"{CON_NAME}",
173+
'802-11-wireless-security.psk-flags', '0'],
174+
check=True, capture_output=True, timeout=5)
175+
logger.debug(f"Used nmcli to set psk-flags=0 (file edit failed)")
176+
except Exception as e2:
177+
logger.warning(f"nmcli modify also failed: {e2}")
147178
except Exception as e:
148-
logger.warning(f"Unexpected error setting psk-flags=0: {e}")
179+
logger.warning(f"Unexpected error adding psk-flags=0 to file: {e}", exc_info=True)
149180

150181
# NetworkManager automatically saves connections when created/modified
151182
# In NetworkManager 1.46.0+, connections are saved automatically to

0 commit comments

Comments
 (0)