From 66212b7926c1ec6e6d27a5ab2e67d5823f11940f Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 2 Feb 2021 00:38:04 -0800 Subject: [PATCH] dns.mitm: support % in hosts file as stand-in for environment identifier --- libstratosphere/include/stratosphere.hpp | 1 + libstratosphere/include/stratosphere/nsd.hpp | 18 +++++++ .../nsd/impl/device/nsd_device.hpp | 24 +++++++++ .../include/stratosphere/nsd/nsd_types.hpp | 37 ++++++++++++++ .../source/nsd/impl/device/nsd_device.cpp | 49 +++++++++++++++++++ .../include/vapours/util/util_string_util.hpp | 4 +- 6 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 libstratosphere/include/stratosphere/nsd.hpp create mode 100644 libstratosphere/include/stratosphere/nsd/impl/device/nsd_device.hpp create mode 100644 libstratosphere/include/stratosphere/nsd/nsd_types.hpp create mode 100644 libstratosphere/source/nsd/impl/device/nsd_device.cpp diff --git a/libstratosphere/include/stratosphere.hpp b/libstratosphere/include/stratosphere.hpp index aa648c20..c6bbe862 100644 --- a/libstratosphere/include/stratosphere.hpp +++ b/libstratosphere/include/stratosphere.hpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include diff --git a/libstratosphere/include/stratosphere/nsd.hpp b/libstratosphere/include/stratosphere/nsd.hpp new file mode 100644 index 00000000..d0cfe384 --- /dev/null +++ b/libstratosphere/include/stratosphere/nsd.hpp @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include diff --git a/libstratosphere/include/stratosphere/nsd/impl/device/nsd_device.hpp b/libstratosphere/include/stratosphere/nsd/impl/device/nsd_device.hpp new file mode 100644 index 00000000..38521e38 --- /dev/null +++ b/libstratosphere/include/stratosphere/nsd/impl/device/nsd_device.hpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include + +namespace ams::nsd::impl::device { + + const EnvironmentIdentifier &GetEnvironmentIdentifierFromSettings(); + +} diff --git a/libstratosphere/include/stratosphere/nsd/nsd_types.hpp b/libstratosphere/include/stratosphere/nsd/nsd_types.hpp new file mode 100644 index 00000000..9f8ace67 --- /dev/null +++ b/libstratosphere/include/stratosphere/nsd/nsd_types.hpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include + +namespace ams::nsd { + + struct EnvironmentIdentifier { + static constexpr size_t Size = 8; + char value[Size]; + + constexpr friend bool operator==(const EnvironmentIdentifier &lhs, const EnvironmentIdentifier &rhs) { + return util::Strncmp(lhs.value, rhs.value, Size) == 0; + } + + constexpr friend bool operator!=(const EnvironmentIdentifier &lhs, const EnvironmentIdentifier &rhs) { + return !(lhs == rhs); + } + }; + + constexpr inline const EnvironmentIdentifier EnvironmentIdentifierOfProductDevice = {"lp1"}; + constexpr inline const EnvironmentIdentifier EnvironmentIdentifierOfNotProductDevice = {"dd1"}; + +} diff --git a/libstratosphere/source/nsd/impl/device/nsd_device.cpp b/libstratosphere/source/nsd/impl/device/nsd_device.cpp new file mode 100644 index 00000000..8d06b32b --- /dev/null +++ b/libstratosphere/source/nsd/impl/device/nsd_device.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +namespace ams::nsd::impl::device { + + namespace { + + constexpr const char SettingsName[] = "nsd"; + constexpr const char SettingsKey[] = "environment_identifier"; + + constinit os::SdkMutex g_environment_identifier_lock; + constinit EnvironmentIdentifier g_environment_identifier = {}; + constinit bool g_determined_environment_identifier = false; + + } + + const EnvironmentIdentifier &GetEnvironmentIdentifierFromSettings() { + std::scoped_lock lk(g_environment_identifier_lock); + + if (!g_determined_environment_identifier) { + /* Check size. */ + AMS_ABORT_UNLESS(settings::fwdbg::GetSettingsItemValueSize(SettingsName, SettingsKey) != 0); + + /* Get value. */ + const size_t size = settings::fwdbg::GetSettingsItemValue(g_environment_identifier.value, EnvironmentIdentifier::Size, SettingsName, SettingsKey); + AMS_ABORT_UNLESS(size < EnvironmentIdentifier::Size); + AMS_ABORT_UNLESS(g_environment_identifier == EnvironmentIdentifierOfProductDevice || g_environment_identifier == EnvironmentIdentifierOfNotProductDevice); + + g_determined_environment_identifier = true; + } + + return g_environment_identifier; + } + +} diff --git a/libvapours/include/vapours/util/util_string_util.hpp b/libvapours/include/vapours/util/util_string_util.hpp index d06d96a7..9abe8aaa 100644 --- a/libvapours/include/vapours/util/util_string_util.hpp +++ b/libvapours/include/vapours/util/util_string_util.hpp @@ -31,7 +31,7 @@ namespace ams::util { } template - int Strncmp(const T *lhs, const T *rhs, int count) { + constexpr int Strncmp(const T *lhs, const T *rhs, int count) { AMS_ASSERT(lhs != nullptr); AMS_ASSERT(rhs != nullptr); AMS_ABORT_UNLESS(count >= 0); @@ -50,7 +50,7 @@ namespace ams::util { } template - int Strnicmp(const T *lhs, const T *rhs, int count) { + constexpr int Strnicmp(const T *lhs, const T *rhs, int count) { AMS_ASSERT(lhs != nullptr); AMS_ASSERT(rhs != nullptr); AMS_ABORT_UNLESS(count >= 0);