#pragma once #include #include #include #include #include #include #include #include #include #include namespace rublon { inline bool fileGood(const std::filesystem::path & path) { std::ifstream file(path); return file.good(); } template < typename T > inline bool readFile(const std::filesystem::path & path, T & destination) { if(not fileGood(path)) return false; std::ifstream file(path); file.seekg(0, std::ios::end); size_t size = file.tellg(); destination.resize(size); file.seekg(0); file.read(&destination[0], size); return true; } enum class LogLevel { Debug, Info, Warning, Error }; inline StaticString< 32 > dateStr() { StaticString< 32 > date; time_t now; time(&now); strftime(date.data(), date.capacity(), "%FT%TZ", gmtime(&now)); return date; } constexpr const char * LogLevelNames[]{"Debug", "Info", "Warning", "Error"}; LogLevel g_level = LogLevel::Debug; constexpr bool syncLogFile = true; static const char * application = ""; namespace details { std::pmr::string osName(std::pmr::memory_resource * mr) { memory::MonotonicStackResource< 8 * 1024 > stackResource; std::ifstream file(std::filesystem::path{"/etc/os-release"}); if(not file.good()) return {"unknown", mr}; std::pmr::string line{&stackResource}; line.reserve(100); while(std::getline(file, line)) { std::pmr::string _key{&stackResource}; std::pmr::string _value{&stackResource}; if(!line.length()) continue; if(line[0] == '#' || line[0] == ';') continue; auto posEqual = line.find('='); _key = line.substr(0, posEqual); _value = line.substr(posEqual + 1); if(_key == "PRETTY_NAME") { _value.erase(std::remove_if(_value.begin(), _value.end(), [](auto ch) { return ch == '"'; }), _value.end()); return {_value, mr}; } } return {"unknown", mr}; } constexpr const char * logPath() { constexpr auto path = "/var/log/rublon-ssh.log"; return path; } inline void touch(const char * filename) { close(open(filename, O_CREAT | O_RDWR, 0640)); } inline void mkdir(const char * dirname) { ::mkdir(dirname, O_CREAT | O_RDWR); } inline bool exists(const char * filename) { return access(filename, F_OK) == 0; } inline bool logMissing() noexcept { return not exists(logPath()); } inline const char * initLog(const char * app = nullptr) noexcept { if(logMissing()) { touch(logPath()); } if(not app) application = app; return logPath(); } inline void doLog(LogLevel level, const char * line) noexcept { auto fp = std::unique_ptr< FILE, int (*)(FILE *) >(fopen(initLog(), "a+"), fclose); if(fp) { /// TODO add transaction ID fprintf( fp.get(), "%s %s[%s] %s\n", dateStr().c_str(), application == nullptr ? "" : application, LogLevelNames[( int ) level], line); if(syncLogFile) sync(); } // openlog ("auth", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_AUTH); // syslog (LOG_INFO, "[%s] %s", "pam_rublon", line); // closelog (); } } // namespace details inline void log(LogLevel level, const char * line) noexcept { if(level < g_level) return; details::doLog(level, line); } template < typename... Ti > void log(LogLevel level, const char * fmt, Ti &&... ti) noexcept { if(level < g_level) return; constexpr auto maxEntryLength = 1000; std::array< char, maxEntryLength > line; snprintf(line.data(), maxEntryLength, fmt, std::forward< Ti >(ti)...); details::doLog(level, line.data()); } class PrintUser { public: template < typename Printer > PrintUser(Printer & p) : _impl{std::make_unique< ModelImpl >(p)} {} private: struct model { virtual ~model(); virtual void print(std::string_view line) const = 0; }; template < typename Printer_T > struct ModelImpl : public model { ModelImpl(Printer_T & printer) : _printer{printer} {} void print(std::string_view line) const override { _printer.print(line); } Printer_T & _printer; }; std::unique_ptr< model > _impl; }; namespace conv { enum class Error { OutOfRange, NotANumber }; inline bool to_bool(std::string_view userinput) { if(userinput.size() > 16) { // todo return optional return false; } std::array< char, 16 > buf{}; auto asciitolower = [](char in) { return in - ((in <= 'Z' && in >= 'A') ? ('Z' - 'z') : 0); }; std::transform(userinput.cbegin(), userinput.cend(), buf.data(), asciitolower); return strcmp(buf.data(), "true") == 0; } inline tl::expected< std::uint32_t, Error > to_uint32(std::string_view userinput) noexcept { try { return std::stoi(userinput.data()); } catch(const std::invalid_argument & e) { return tl::make_unexpected(Error::NotANumber); } catch(const std::out_of_range & e) { return tl::make_unexpected(Error::OutOfRange); } } } // namespace conv namespace details { static inline std::string_view ltrim(std::string_view s) { while(s.length() && std::isspace(*s.begin())) s.remove_prefix(1); return s; } static inline std::string_view rtrim(std::string_view s) { while(s.length() && std::isspace(*s.rbegin())) s.remove_suffix(1); return s; } static inline std::string_view trim(std::string_view s) { return ltrim(rtrim(s)); } template < typename Headers > inline void headers(std::string_view data, Headers & headers) { memory::StrictMonotonic_4k_HeapResource stackResource; std::pmr::string tmp{&stackResource}; std::istringstream resp{}; resp.rdbuf()->pubsetbuf(const_cast< char * >(data.data()), data.size()); while(std::getline(resp, tmp) && !(trim(tmp).empty())) { auto line = std::string_view(tmp); auto index = tmp.find(':', 0); if(index != std::string::npos) { headers.insert({// typename Headers::key_type{trim(line.substr(0, index)), headers.get_allocator()}, typename Headers::mapped_type{trim(line.substr(index + 1)), headers.get_allocator()}}); } } } } // namespace details template < class InputIterator, class OutputIterator, class UnaryOperator, class Pred > OutputIterator transform_if(InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op, Pred pred) { while(first1 != last1) { if(pred(*first1)) { *result = op(*first1); ++result; } ++first1; } return result; } struct ci_less { template < typename S > bool operator()(const S & s1, const S & s2) const { return std::lexicographical_compare( s1.cbegin(), s1.cend(), s2.cbegin(), s2.cend(), [](const auto & c1, const auto & c2) { return tolower(c1) < tolower(c2); }); } }; } // namespace rublon