Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp

98 lines
2.1 KiB
C++
Raw Normal View History

2016-10-15 21:37:59 +05:00
#include <array>
#include <fstream>
2017-10-17 19:29:56 +05:00
#include <iostream>
#include <nntpchan/base64.hpp>
#include <nntpchan/crypto.hpp>
#include <nntpchan/nntp_auth.hpp>
2016-10-15 21:37:59 +05:00
namespace nntpchan
{
2018-05-06 18:53:03 +05:00
HashedCredDB::HashedCredDB() : LineReader() {}
2017-05-03 17:09:23 +05:00
2017-10-17 19:29:56 +05:00
bool HashedCredDB::CheckLogin(const std::string &user, const std::string &passwd)
{
std::unique_lock<std::mutex> lock(m_access);
m_found = false;
m_user = user;
m_passwd = passwd;
m_instream->seekg(0, std::ios::end);
const auto l = m_instream->tellg();
m_instream->seekg(0, std::ios::beg);
char *buff = new char[l];
// read file
m_instream->read(buff, l);
Data(buff, l);
delete[] buff;
return m_found;
}
2016-10-15 21:37:59 +05:00
2017-10-17 19:29:56 +05:00
bool HashedCredDB::ProcessLine(const std::string &line)
{
// strip comments
auto comment = line.find("#");
std::string part = line;
for (; comment != std::string::npos; comment = part.find("#"))
2016-10-15 21:37:59 +05:00
{
2017-10-17 19:29:56 +05:00
if (comment)
part = part.substr(0, comment);
else
break;
2016-10-15 21:37:59 +05:00
}
2017-10-17 19:29:56 +05:00
if (!part.size())
return false; // empty line after comments
auto idx = part.find(":");
if (idx == std::string::npos)
return false; // bad format
if (m_user != part.substr(0, idx))
return false; // username mismatch
part = part.substr(idx + 1);
2016-10-15 21:37:59 +05:00
2017-10-17 19:29:56 +05:00
idx = part.find(":");
if (idx == std::string::npos)
return false; // bad format
std::string cred = part.substr(0, idx);
std::string salt = part.substr(idx + 1);
return Hash(m_passwd, salt) == cred;
}
2016-10-15 22:53:35 +05:00
2018-05-03 22:38:35 +05:00
void HashedCredDB::HandleLine(const std::string line)
2017-10-17 19:29:56 +05:00
{
if (m_found)
return;
if (ProcessLine(line))
m_found = true;
}
2017-05-03 17:09:23 +05:00
2017-10-17 19:29:56 +05:00
void HashedCredDB::SetStream(std::istream *s) { m_instream = s; }
2016-10-15 22:53:35 +05:00
2017-10-17 19:29:56 +05:00
std::string HashedCredDB::Hash(const std::string &data, const std::string &salt)
{
SHA512Digest h;
std::string d = data + salt;
SHA512((const uint8_t *)d.c_str(), d.size(), h);
return B64Encode(h.data(), h.size());
}
2017-05-03 17:09:23 +05:00
2017-10-17 19:29:56 +05:00
HashedFileDB::HashedFileDB(const std::string &fname) : m_fname(fname), f(nullptr) {}
2016-10-15 22:53:35 +05:00
2017-10-17 19:29:56 +05:00
HashedFileDB::~HashedFileDB() {}
2016-10-15 22:53:35 +05:00
2017-10-17 19:29:56 +05:00
void HashedFileDB::Close()
{
if (f.is_open())
f.close();
}
2016-10-15 22:53:35 +05:00
2017-10-17 19:29:56 +05:00
bool HashedFileDB::Open()
{
if (!f.is_open())
f.open(m_fname);
if (f.is_open())
2016-10-15 22:53:35 +05:00
{
2017-10-17 19:29:56 +05:00
SetStream(&f);
return true;
2016-10-15 22:53:35 +05:00
}
2017-10-17 19:29:56 +05:00
return false;
}
2016-10-15 21:37:59 +05:00
}