diff --git a/Meta/build-root-filesystem.sh b/Meta/build-root-filesystem.sh index f8ee86ee397..a01de58fa98 100755 --- a/Meta/build-root-filesystem.sh +++ b/Meta/build-root-filesystem.sh @@ -129,6 +129,11 @@ if [ -f mnt/boot/Kernel.debug ]; then chmod 0400 mnt/boot/Kernel.debug fi +if [ -f mnt/bin/network-settings ]; then + chown 0:0 mnt/bin/network-settings + chmod 500 mnt/bin/network-settings +fi + chmod 600 mnt/etc/shadow chmod 755 mnt/res/devel/templates/*.postcreate echo "done" diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 9735de7824a..3420c0d3ab1 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -2,7 +2,7 @@ file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp") list(APPEND SPECIAL_TARGETS test install) list(APPEND REQUIRED_TARGETS arp base64 basename cat chmod chown clear comm cp cut date dd df diff dirname dmesg du echo env expr false - file find grep groups head host hostname id ifconfig kill killall ln logout ls mkdir mount mv nproc + file find grep groups head host hostname id ifconfig kill killall ln logout ls mkdir mount mv network-settings nproc pgrep pidof ping pkill pmap ps readlink realpath reboot rm rmdir sed route seq shutdown sleep sort stat stty su tail test touch tr true umount uname uniq uptime w wc which whoami xargs yes ) @@ -115,6 +115,7 @@ target_link_libraries(markdown-check PRIVATE LibFileSystem LibMarkdown) target_link_libraries(matroska PRIVATE LibVideo) target_link_libraries(md PRIVATE LibMarkdown) target_link_libraries(mv PRIVATE LibFileSystem) +target_link_libraries(network-settings PRIVATE LibCore LibMain) target_link_libraries(notify PRIVATE LibGfx LibGUI) target_link_libraries(open PRIVATE LibDesktop) target_link_libraries(passwd PRIVATE LibCrypt) diff --git a/Userland/Utilities/network-settings.cpp b/Userland/Utilities/network-settings.cpp new file mode 100644 index 00000000000..916423e4984 --- /dev/null +++ b/Userland/Utilities/network-settings.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023, Fabian Dellwing + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +ErrorOr serenity_main(Main::Arguments) +{ + TRY(Core::System::pledge("stdio rpath wpath cpath recvfd sendfd proc exec")); + + TRY(Core::System::unveil("/bin/NetworkServer", "x")); + TRY(Core::System::unveil("/etc/Network.ini", "rwc")); + TRY(Core::System::unveil(nullptr, nullptr)); + + auto infile = TRY(Core::File::standard_input()); + + auto input_bytes = TRY(infile->read_until_eof()); + StringView json_data = input_bytes; + + if (json_data.is_empty() || json_data.is_whitespace()) + return Error::from_errno(EINVAL); + + auto json = TRY(JsonParser(json_data).parse()); + + if (!json.is_object()) + return Error::from_errno(EINVAL); + + auto json_object = json.as_object(); + + auto config_file = TRY(Core::ConfigFile::open_for_system("Network", Core::ConfigFile::AllowWriting::Yes)); + json_object.for_each_member([&](DeprecatedString const& adapter_name, JsonValue const& adapter_data) { + adapter_data.as_object().for_each_member([&](const DeprecatedString& key, const JsonValue& value) { + switch (value.type()) { + case JsonValue::Type::String: + config_file->write_entry(adapter_name, key, value.as_string()); + break; + case JsonValue::Type::Bool: + config_file->write_bool_entry(adapter_name, key, value.as_bool()); + break; + case JsonValue::Type::Null: + break; + default: + dbgln("Extend switch/case key={}, value={}", key, value); + VERIFY_NOT_REACHED(); + } + }); + }); + TRY(config_file->sync()); + + // FIXME: This should be done in a nicer way, but for that out NetworkServer implementation needs to actually be a server that we can talk to and not just a oneshot binary. + auto command = Vector(); + TRY(command.try_append("/bin/NetworkServer"sv)); + TRY(Core::System::exec_command(command, true)); + + return 0; +}