Create ensureWpaSupplicantConf(), call from setKnownStationsWPA

This commit is contained in:
billz
2025-09-25 01:52:09 -07:00
parent 87d317db52
commit e514178b33

View File

@@ -313,6 +313,8 @@ class WiFiManager
*/
public function setKnownStationsWPA($networks)
{
$this->ensureWpaSupplicantConf();
$iface = escapeshellarg($_SESSION['wifi_client_interface']);
$output = shell_exec("sudo wpa_cli -i $iface list_networks 2>&1");
@@ -475,5 +477,42 @@ class WiFiManager
return false;
}
/**
* Ensures /etc/wpa_supplicant/wpa_supplicant.conf exists with minimal safe contents
* Does not overwrite an existing file
*
* @throws \RuntimeException on permission or write failure
*/
public function ensureWpaSupplicantConf(): void
{
$confPath = '/etc/wpa_supplicant/wpa_supplicant.conf';
if (file_exists($confPath)) {
// Already exists, do nothing
return;
}
$contents = <<<CONF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
CONF;
$tmpFile = tempnam(sys_get_temp_dir(), 'wpa_conf_');
if ($tmpFile === false) {
throw new \RuntimeException("Failed to create temporary file for wpa_supplicant.conf");
}
file_put_contents($tmpFile, $contents);
chmod($tmpFile, 0600);
$cmd = escapeshellcmd("sudo cp $tmpFile $confPath");
exec($cmd, $output, $exitCode);
unlink($tmpFile);
if ($exitCode !== 0) {
throw new \RuntimeException("Failed to initialize wpa_supplicant.conf: " . implode("\n", $output));
}
}
}