Cleanup and fix for udev rules written for clients

Fix device type in client table
This commit is contained in:
Christian Zeitnitz 2021-03-29 15:16:23 +02:00
parent f7cef44b06
commit 300f2c2457
4 changed files with 89 additions and 74 deletions

View File

@ -17,76 +17,78 @@ if (isset($_POST['interface'])) {
$file = $int.".ini"; $file = $int.".ini";
$cfgfile="/etc/wvdial.conf"; $cfgfile="/etc/wvdial.conf";
if ( $int == "mobiledata") { if ( $int == "mobiledata") {
$cfg['pin'] = $_POST["pin-mobile"]; $cfg['pin'] = $_POST["pin-mobile"];
$cfg['apn'] = $_POST["apn-mobile"]; $cfg['apn'] = $_POST["apn-mobile"];
$cfg['apn_user'] = $_POST["apn-user-mobile"]; $cfg['apn_user'] = $_POST["apn-user-mobile"];
$cfg['apn_pw'] = $_POST["apn-pw-mobile"]; $cfg['apn_pw'] = $_POST["apn-pw-mobile"];
if (file_exists($cfgfile)) { if (file_exists($cfgfile)) {
if($cfg["pin"] !== "") exec('sudo /bin/sed -i "s/CPIN=\".*\"/CPIN=\"'.$cfg["pin"].'\"/gi" '.$cfgfile); if($cfg["pin"] !== "") exec('sudo /bin/sed -i "s/CPIN=\".*\"/CPIN=\"'.$cfg["pin"].'\"/gi" '.$cfgfile);
if($cfg["apn"] !== "") exec('sudo /bin/sed -i "s/\"IP\"\,\".*\"/\"IP\"\,\"'.$cfg["apn"].'\"/gi" '.$cfgfile); if($cfg["apn"] !== "") exec('sudo /bin/sed -i "s/\"IP\"\,\".*\"/\"IP\"\,\"'.$cfg["apn"].'\"/gi" '.$cfgfile);
if($cfg["apn_user"] !== "") exec('sudo /bin/sed -i "s/^username = .*$/Username = '.$cfg["apn_user"].'/gi" '.$cfgfile); if($cfg["apn_user"] !== "") exec('sudo /bin/sed -i "s/^username = .*$/Username = '.$cfg["apn_user"].'/gi" '.$cfgfile);
if($cfg["apn_pw"] !== "") exec('sudo /bin/sed -i "s/^password = .*$/Password = '.$cfg["apn_pw"].'/gi" '.$cfgfile); if($cfg["apn_pw"] !== "") exec('sudo /bin/sed -i "s/^password = .*$/Password = '.$cfg["apn_pw"].'/gi" '.$cfgfile);
} }
} else if ( preg_match("/netdevices/",$int)) { } else if ( preg_match("/netdevices/",$int)) {
if(!isset($_POST['opts']) ) { if(!isset($_POST['opts']) ) {
$jsonData = ['return'=>0,'output'=>['No valid data to add/delete udev rule ']]; $jsonData = ['return'=>0,'output'=>['No valid data to add/delete udev rule ']];
echo json_encode($jsonData); echo json_encode($jsonData);
return; return;
} else { } else {
$opts=explode(" ",$_POST['opts'] ); $opts=explode(" ",$_POST['opts'] );
$dev=$opts[0]; $dev=$opts[0];
$vid=$_POST["int-vid-".$dev]; $vid=$_POST["int-vid-".$dev];
$pid=$_POST["int-pid-".$dev]; $pid=$_POST["int-pid-".$dev];
$mac=$_POST["int-mac-".$dev]; $mac=$_POST["int-mac-".$dev];
$name=trim($_POST["int-name-".$dev]); $name=trim($_POST["int-name-".$dev]);
$type=$_POST["int-type-".$dev]; $name=preg_replace("/[^a-z0-9]/", "", strtolower($name));
$newtype=$_POST["int-new-type-".$dev]; $type=$_POST["int-type-".$dev];
$udevfile=$_SESSION["udevrules"]["udev_rules_file"]; $newtype=$_POST["int-new-type-".$dev];
// $udevfile="/etc/udev/rules.d/80-net-devices.rules"; $udevfile=$_SESSION["udevrules"]["udev_rules_file"]; // default file /etc/udev/rules.d/80-net-devices.rules";
// find the rule prototype and prefix // find the rule prototype and prefix
$rule = ""; $rule = "";
foreach($_SESSION["udevrules"]["network_devices"] as $devt) { foreach($_SESSION["udevrules"]["network_devices"] as $devt) {
if($devt["type"]==$newtype) { if($devt["type"]==$newtype) {
$rule = $devt["udev_rule"]; $rulenew = $devt["udev_rule"];
$prefix = $devt["name_prefix"]; $prefix = $devt["name_prefix"];
} }
} }
if(!empty($mac)) $rule = preg_replace("/\\\$MAC\\\$/i",$mac,$rule);
if(!empty($vid)) $rule = preg_replace("/\\\$IDVENDOR\\\$/i",$vid,$rule); // check for an existing rule and delete lines with same MAC or same VID/PID
if(!empty($pid)) $rule = preg_replace("/\\\$IDPRODUCT\\\$/i",$pid,$rule); if (!empty($vid) && !empty($pid)) {
// check for existing rule $rule = '^.*ATTRS{idVendor}==\"' . $vid . '\".*ATTRS{idProduct}==\"' . $pid . '\".*$';
$pre=""; exec('sudo sed -i "/'.$rule.'/Id" '.$udevfile); // clear all entries with this VID/PID
if(preg_match("/^(\w+)[0-9]$/",$dev,$match)=== 1) $pre=$match[1]; $rule = '^.*ATTRS{idProduct}==\"' . $pid . '\".*ATTRS{idVendor}==\"' . $vid . '\".*$';
$ruleold = preg_replace("/\\\$DEVNAME\\\$/i",$pre.".",$rule); exec('sudo sed -i "/'.$rule.'/Id" '.$udevfile); // clear all entries with this VID/PID
exec("grep -oP '".$ruleold."' $udevfile",$ret); }
$newRule = empty($ret) || !empty($name); if (!empty($mac)) {
// delete current entry exec('sudo sed -i "/^.*'.$mac.'.*$/d" '.$udevfile); // clear all entries with same MAC
if(!empty($ret)) exec("sudo sed -i '/^".$ruleold."$/d' ".$udevfile); }
exec('sudo sed -i "/^.*'.$mac.'.*$/d" '.$udevfile);
if($newRule) {
// create new entry // create new entry
if(empty($name)) $name = $prefix."0"; if ( ($type != $newtype) || !empty($name) ) { // new device type or new name
if(!empty($name)) $rule = preg_replace("/\\\$DEVNAME\\\$/i",$name,$rule); if (empty($name)) $name = $prefix."*";
if (!empty($rule) ) exec('echo \''.$rule.'\' | sudo /usr/bin/tee -a '.$udevfile); if (!empty($mac)) $rule = preg_replace("/\\\$MAC\\\$/i", $mac, $rulenew);
} if (!empty($vid)) $rule = preg_replace("/\\\$IDVENDOR\\\$/i", $vid, $rule);
$ret=print_r($ret,true); if (!empty($pid)) $rule = preg_replace("/\\\$IDPRODUCT\\\$/i", $pid, $rule);
$jsonData = ['return'=>0,'output'=>['Settings changed for device '.$dev ] ]; if (!empty($name)) $rule = preg_replace("/\\\$DEVNAME\\\$/i",$name,$rule);
echo json_encode($jsonData); if (!empty($rule)) exec('echo \''.$rule.'\' | sudo /usr/bin/tee -a '.$udevfile);
return; }
$ret=print_r($ret,true);
$jsonData = ['return'=>0,'output'=>['Settings changed for device '.$dev. '<br>Changes will only be in effect after reconnecting the device' ] ];
echo json_encode($jsonData);
return;
} }
} else { } else {
$ip = $_POST[$int.'-ipaddress']; $ip = $_POST[$int.'-ipaddress'];
$netmask = mask2cidr($_POST[$int.'-netmask']); $netmask = mask2cidr($_POST[$int.'-netmask']);
$dns1 = $_POST[$int.'-dnssvr']; $dns1 = $_POST[$int.'-dnssvr'];
$dns2 = $_POST[$int.'-dnssvralt']; $dns2 = $_POST[$int.'-dnssvralt'];
$cfg['interface'] = $int; $cfg['interface'] = $int;
$cfg['routers'] = $_POST[$int.'-gateway']; $cfg['routers'] = $_POST[$int.'-gateway'];
$cfg['ip_address'] = $ip."/".$netmask; $cfg['ip_address'] = $ip."/".$netmask;
$cfg['domain_name_server'] = $dns1." ".$dns2; $cfg['domain_name_server'] = $dns1." ".$dns2;
$cfg['static'] = $_POST[$int.'-static']; $cfg['static'] = $_POST[$int.'-static'];
$cfg['failover'] = $_POST[$int.'-failover']; $cfg['failover'] = $_POST[$int.'-failover'];
} }
if (write_php_ini($cfg, RASPI_CONFIG.'/networking/'.$file)) { if (write_php_ini($cfg, RASPI_CONFIG.'/networking/'.$file)) {
$jsonData = ['return'=>0,'output'=>['Successfully Updated Network Configuration']]; $jsonData = ['return'=>0,'output'=>['Successfully Updated Network Configuration']];

View File

@ -20,16 +20,8 @@ function getClients($simple=true)
} }
foreach ($rawdevs as $i => $dev) { foreach ($rawdevs as $i => $dev) {
$cl["device"][$i]["name"]=$dev; $cl["device"][$i]["name"]=$dev;
if (preg_match("/^(\w+)[0-9]$/", $dev, $nam) === 1) { $nam = (preg_match("/^(\w+)[0-9]$/",$dev,$nam) === 1) ? $nam=$nam[1] : "";
$nam=$nam[1]; $cl["device"][$i]["type"]=$ty=getClientType($dev);
} else {
$nam="none";
}
if (($n = array_search($nam, $_SESSION["net-device-name-prefix"])) === false) {
$n = count($_SESSION["net-device-types"])-1;
}
$ty = $_SESSION["net-device-types"][$n];
$cl["device"][$i]["type"]=$ty;
unset($udevinfo); unset($udevinfo);
exec("udevadm info /sys/class/net/$dev 2> /dev/null", $udevinfo); exec("udevadm info /sys/class/net/$dev 2> /dev/null", $udevinfo);
if ($nam == "ppp" && isset($devtty)) { if ($nam == "ppp" && isset($devtty)) {
@ -181,6 +173,27 @@ function getClients($simple=true)
return $cl; return $cl;
} }
function getClientType($dev) {
loadClientConfig();
// check if device type stored in DEVTYPE or raspapType (from UDEV rule) protperty of the device
exec("udevadm info /sys/class/net/$dev 2> /dev/null", $udevadm);
$type="none";
if (!empty($udevadm)) {
$type=preg_only_match("/raspapType=(\w*)/i",$udevadm);
if (empty($type)) {
$type=preg_only_match("/DEVTYPE=(\w*)/i",$udevadm);
}
}
if (empty($type) || array_search($type, $_SESSION["net-device-name-prefix"]) === false) {
// no device type yet -> get device type from device name
if (preg_match("/^(\w+)[0-9]$/",$dev,$nam) === 1) $nam=$nam[1];
else $nam="none";
if (($n = array_search($nam, $_SESSION["net-device-name-prefix"])) === false) $n = count($_SESSION["net-device-types"])-1;
$type = $_SESSION["net-device-types"][$n];
}
return $type;
}
function loadClientConfig() function loadClientConfig()
{ {
// load network device config file for UDEV rules into $_SESSION // load network device config file for UDEV rules into $_SESSION

View File

@ -46,7 +46,7 @@ function getRouteInfo($checkAccess)
$rInfo[$i]["access-ip"] = empty($okip) ? false : true; $rInfo[$i]["access-ip"] = empty($okip) ? false : true;
unset($okdns); unset($okdns);
exec('ping -W1 -c 1 -I ' . $prop[0] . ' ' . RASPI_ACCESS_CHECK_DNS . ' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"', $okdns); exec('ping -W1 -c 1 -I ' . $prop[0] . ' ' . RASPI_ACCESS_CHECK_DNS . ' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"', $okdns);
$rInfo[$i]["access-dns"] = empty($okdns) ? false : true; $rInfo[$i]["access-dns"] = empty($okdns) ? false : true;
} }
} }
} else { } else {

View File

@ -141,7 +141,7 @@
foreach($_SESSION["net-device-types"] as $i => $type) { foreach($_SESSION["net-device-types"] as $i => $type) {
$txt=$_SESSION["net-device-types-info"][$i]; $txt=$_SESSION["net-device-types-info"][$i];
$txtdisabled = in_array($type,array("ppp","tun")) ? "disabled":""; $txtdisabled = in_array($type,array("ppp","tun")) ? "disabled":"";
if(preg_match("/^".$_SESSION["net-device-name-prefix"][$i]."[0-9]*$/",$dev["name"])===1) echo '<option '.$txtdisabled.' selected value="'.$type.'">'.$txt.'</option>'; if(preg_match("/^".$_SESSION["net-device-name-prefix"][$i].".*$/",$dev["type"])===1) echo '<option '.$txtdisabled.' selected value="'.$type.'">'.$txt.'</option>';
else echo '<option '.$txtdisabled.' value="'.$type.'">'.$txt.'</option>'; else echo '<option '.$txtdisabled.' value="'.$type.'">'.$txt.'</option>';
} }
echo "</select></td>"; echo "</select></td>";