ftpiiu_plugin/source/ftpConfig.cpp

343 lines
6.9 KiB
C++
Raw Normal View History

2020-04-24 22:51:43 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2024 Michael Theall
2020-04-24 22:51:43 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that 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 <https://www.gnu.org/licenses/>.
#include "ftpConfig.h"
#include "fs.h"
#include "log.h"
#include "platform.h"
#include <gsl/pointers>
2020-04-24 22:51:43 +02:00
#include <sys/stat.h>
using stat_t = struct stat;
2020-04-24 22:51:43 +02:00
#include <cctype>
#include <cerrno>
#include <charconv>
#include <cstdint>
#include <cstdio>
2020-12-12 02:15:33 +01:00
#include <mutex>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
2020-04-24 22:51:43 +02:00
namespace
{
#if defined(__WIIU__) && defined(__WUPS__)
constexpr std::uint16_t DEFAULT_PORT = 21;
#else
2020-04-24 22:51:43 +02:00
constexpr std::uint16_t DEFAULT_PORT = 5000;
#endif
2020-04-24 22:51:43 +02:00
bool mkdirParent (std::string_view const path_)
2020-04-24 22:51:43 +02:00
{
auto pos = path_.find_first_of ('/');
while (pos != std::string::npos)
{
auto const dir = std::string (path_.substr (0, pos));
2020-04-24 22:51:43 +02:00
stat_t st{};
2020-04-24 22:51:43 +02:00
auto const rc = ::stat (dir.c_str (), &st);
if (rc < 0 && errno != ENOENT)
return false;
if (rc < 0 && errno == ENOENT)
{
auto const rc = ::mkdir (dir.c_str (), 0755);
if (rc < 0)
return false;
}
pos = path_.find_first_of ('/', pos + 1);
}
return true;
}
std::string_view strip (std::string_view const str_)
2020-04-24 22:51:43 +02:00
{
auto const start = str_.find_first_not_of (" \t");
if (start == std::string::npos)
return {};
auto const end = str_.find_last_not_of (" \t");
if (end == std::string::npos)
return str_.substr (start);
return str_.substr (start, end + 1 - start);
}
template <typename T>
bool parseInt (T &out_, std::string_view const val_)
2020-04-24 22:51:43 +02:00
{
auto const rc = std::from_chars (val_.data (), val_.data () + val_.size (), out_);
if (rc.ec != std::errc{})
2020-04-24 22:51:43 +02:00
{
errno = static_cast<int> (rc.ec);
return false;
}
2020-04-24 22:51:43 +02:00
if (rc.ptr != val_.data () + val_.size ())
{
errno = EINVAL;
return false;
2020-04-24 22:51:43 +02:00
}
return true;
}
}
///////////////////////////////////////////////////////////////////////////
FtpConfig::~FtpConfig () = default;
FtpConfig::FtpConfig () : m_port (DEFAULT_PORT)
{
}
UniqueFtpConfig FtpConfig::create ()
{
return UniqueFtpConfig (new FtpConfig ());
}
UniqueFtpConfig FtpConfig::load (gsl::not_null<gsl::czstring> const path_)
2020-04-24 22:51:43 +02:00
{
auto config = create ();
auto fp = fs::File ();
if (!fp.open (path_))
return config;
std::uint16_t port = DEFAULT_PORT;
std::string line;
while (!(line = fp.readLine ()).empty ())
{
auto const pos = line.find_first_of ('=');
if (pos == std::string::npos)
{
error ("Ignoring '%s'\n", line.c_str ());
continue;
}
auto const key = strip (std::string_view (line).substr (0, pos));
auto const val = strip (std::string_view (line).substr (pos + 1));
2020-04-24 22:51:43 +02:00
if (key.empty () || val.empty ())
{
error ("Ignoring '%s'\n", line.c_str ());
continue;
}
if (key == "user")
config->m_user = val;
else if (key == "pass")
config->m_pass = val;
else if (key == "port")
parseInt (port, val);
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-24 22:51:43 +02:00
else if (key == "mtime")
{
if (val == "0")
config->m_getMTime = false;
else if (val == "1")
config->m_getMTime = true;
else
error ("Invalid value for mtime: %.*s\n",
gsl::narrow_cast<int> (val.size ()),
val.data ());
2020-04-24 22:51:43 +02:00
}
2020-12-12 02:15:33 +01:00
#endif
#ifdef __SWITCH__
else if (key == "ap")
{
if (val == "0")
config->m_enableAP = false;
else if (val == "1")
config->m_enableAP = true;
else
error ("Invalid value for ap: %.*s\n",
gsl::narrow_cast<int> (val.size ()),
val.data ());
2020-12-12 02:15:33 +01:00
}
else if (key == "ssid")
config->m_ssid = val;
else if (key == "passphrase")
config->m_passphrase = val;
2020-04-24 22:51:43 +02:00
#endif
}
config->setPort (port);
return config;
}
#ifndef __NDS__
2022-10-04 04:52:19 +02:00
std::scoped_lock<platform::Mutex> FtpConfig::lockGuard ()
2020-12-12 02:15:33 +01:00
{
2022-10-04 04:52:19 +02:00
return std::scoped_lock<platform::Mutex> (m_lock);
2020-12-12 02:15:33 +01:00
}
#endif
bool FtpConfig::save (gsl::not_null<gsl::czstring> const path_)
2020-04-24 22:51:43 +02:00
{
if (!mkdirParent (path_.get ()))
2020-04-24 22:51:43 +02:00
return false;
auto fp = fs::File ();
if (!fp.open (path_, "wb"))
return false;
if (!m_user.empty ())
(void)std::fprintf (fp, "user=%s\n", m_user.c_str ());
2020-04-24 22:51:43 +02:00
if (!m_pass.empty ())
(void)std::fprintf (fp, "pass=%s\n", m_pass.c_str ());
(void)std::fprintf (fp, "port=%u\n", m_port);
2020-04-24 22:51:43 +02:00
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
(void)std::fprintf (fp, "mtime=%u\n", m_getMTime);
2020-04-24 22:51:43 +02:00
#endif
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
(void)std::fprintf (fp, "ap=%u\n", m_enableAP);
2020-12-12 02:15:33 +01:00
if (!m_ssid.empty ())
(void)std::fprintf (fp, "ssid=%s\n", m_ssid.c_str ());
2020-12-12 02:15:33 +01:00
if (!m_passphrase.empty ())
(void)std::fprintf (fp, "passphrase=%s\n", m_passphrase.c_str ());
2020-12-12 02:15:33 +01:00
#endif
2020-04-24 22:51:43 +02:00
return true;
}
std::string const &FtpConfig::user () const
{
return m_user;
}
std::string const &FtpConfig::pass () const
{
return m_pass;
}
std::string const &FtpConfig::hostname () const
{
return m_hostname;
}
2020-04-24 22:51:43 +02:00
std::uint16_t FtpConfig::port () const
{
return m_port;
}
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-24 22:51:43 +02:00
bool FtpConfig::getMTime () const
{
return m_getMTime;
}
#endif
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
bool FtpConfig::enableAP () const
{
return m_enableAP;
}
std::string const &FtpConfig::ssid () const
{
return m_ssid;
}
std::string const &FtpConfig::passphrase () const
{
return m_passphrase;
}
#endif
void FtpConfig::setUser (std::string user_)
{
m_user = std::move (user_);
}
void FtpConfig::setPass (std::string pass_)
2020-04-24 22:51:43 +02:00
{
m_pass = std::move (pass_);
2020-04-24 22:51:43 +02:00
}
void FtpConfig::setHostname (std::string hostname_)
2020-04-24 22:51:43 +02:00
{
m_hostname = std::move (hostname_);
2020-04-24 22:51:43 +02:00
}
bool FtpConfig::setPort (std::string_view const port_)
2020-04-24 22:51:43 +02:00
{
std::uint16_t parsed{};
2020-04-24 22:51:43 +02:00
if (!parseInt (parsed, port_))
return false;
return setPort (parsed);
}
bool FtpConfig::setPort (std::uint16_t const port_)
{
2021-06-13 18:36:20 +02:00
#ifdef __SWITCH__
// Switch is not allowed < 1024, except 0
if (port_ < 1024 && port_ != 0)
2020-04-24 22:51:43 +02:00
{
errno = EPERM;
return false;
}
#elif defined(__NDS__) || defined(__3DS__)
2021-06-13 18:36:20 +02:00
// 3DS is allowed < 1024, but not 0
// NDS is allowed < 1024, but 0 crashes the app
if (port_ == 0)
{
errno = EPERM;
return false;
}
#endif
2020-04-24 22:51:43 +02:00
m_port = port_;
return true;
}
2022-03-31 18:53:47 +02:00
#ifdef __3DS__
2020-04-24 22:51:43 +02:00
void FtpConfig::setGetMTime (bool const getMTime_)
{
m_getMTime = getMTime_;
}
#endif
2020-12-12 02:15:33 +01:00
#ifdef __SWITCH__
void FtpConfig::setEnableAP (bool const enable_)
{
m_enableAP = enable_;
}
void FtpConfig::setSSID (std::string_view const ssid_)
2020-12-12 02:15:33 +01:00
{
m_ssid = ssid_.substr (0, ssid_.find_first_of ('\0'));
}
void FtpConfig::setPassphrase (std::string_view const passphrase_)
2020-12-12 02:15:33 +01:00
{
m_passphrase = passphrase_.substr (0, passphrase_.find_first_of ('\0'));
}
#endif