| 422 | } |
| 423 | |
| 424 | std::string setMacAddress(const std::string &pars) |
| 425 | { |
| 426 | std::string interface, macAddress, network; |
| 427 | bool isWifi; |
| 428 | deserializePars(pars, interface, macAddress, network, isWifi); |
| 429 | |
| 430 | if (!Validation::isValidInterfaceName(interface)) { |
| 431 | spdlog::error("Invalid interface name: {}", interface); |
| 432 | return serializeResult(false); |
| 433 | } |
| 434 | |
| 435 | if (!Validation::isValidMacAddress(macAddress)) { |
| 436 | spdlog::error("Invalid MAC address: {}", macAddress); |
| 437 | return serializeResult(false); |
| 438 | } |
| 439 | |
| 440 | // network is passed to nmcli; validating here also guards the resetMacAddresses() call below. |
| 441 | if (!Validation::isValidNetworkManagerConnectionName(network)) { |
| 442 | spdlog::error("Invalid network connection name: {}", network); |
| 443 | return serializeResult(false); |
| 444 | } |
| 445 | |
| 446 | std::string mac = |
| 447 | macAddress.substr(0, 2) + ":" + |
| 448 | macAddress.substr(2, 2) + ":" + |
| 449 | macAddress.substr(4, 2) + ":" + |
| 450 | macAddress.substr(6, 2) + ":" + |
| 451 | macAddress.substr(8, 2) + ":" + |
| 452 | macAddress.substr(10, 2); |
| 453 | |
| 454 | spdlog::debug("Set MAC address on {} ({} - {}): {}", interface, network, (isWifi ? "wifi" : "ethernet"), mac); |
| 455 | |
| 456 | // reset addresses on other networks |
| 457 | Utils::resetMacAddresses(network); |
| 458 | |
| 459 | #ifdef CLI_ONLY |
| 460 | // Must bring interface down to change the MAC address |
| 461 | Utils::executeCommand("ip", {"link", "set", "dev", interface, "down"}); |
| 462 | Utils::executeCommand("ip", {"link", "set", "dev", interface, "address", mac}); |
| 463 | Utils::executeCommand("ip", {"link", "set", "dev", interface, "up"}); |
| 464 | #else |
| 465 | std::string out; |
| 466 | // Pass the connection name with an explicit "id" selector so nmcli always treats it as a name, |
| 467 | // never as one of its own selector keywords (id/uuid/path) that would otherwise consume the |
| 468 | // following token. isValidNetworkManagerConnectionName already blocks a leading '-'; this closes |
| 469 | // the same argv-semantics gap for a connection literally named "id"/"uuid"/"path". |
| 470 | if (isWifi) { |
| 471 | Utils::executeCommand("nmcli", {"connection", "modify", "id", network, "wifi.cloned-mac-address", mac}, &out); |
| 472 | } else { |
| 473 | Utils::executeCommand("nmcli", {"connection", "modify", "id", network, "ethernet.cloned-mac-address", mac}, &out); |
| 474 | } |
| 475 | // restart the connection |
| 476 | Utils::executeCommand("nmcli", {"connection", "up", "id", network}); |
| 477 | #endif |
| 478 | return serializeResult(true); |
| 479 | } |
| 480 | |
| 481 | std::string setDnsLeakProtectEnabled(const std::string &pars) |
nothing calls this directly
no test coverage detected