E-MailRelay

gsecrets.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gsecrets.cpp
19///
20
21#include "gdef.h"
22#include "gsecrets.h"
23#include "gsecretsfile.h"
24#include "glog.h"
25#include <algorithm>
26
27 void GAuth::Secrets::check( const std::string & p1 , const std::string & p2 , const std::string & p3 )
28{
29 G::StringArray list ;
30 list.push_back( p1 ) ;
31 list.push_back( p2 ) ;
32 list.push_back( p3 ) ;
33 list.erase( std::remove(list.begin(),list.end(),std::string()) , list.end() ) ;
34 list.erase( std::remove(list.begin(),list.end(),"/pam") , list.end() ) ;
35 std::sort( list.begin() , list.end() ) ;
36 list.erase( std::unique(list.begin(),list.end()) , list.end() ) ;
37 std::for_each( list.begin() , list.end() , &SecretsFile::check ) ;
38}
39
40 GAuth::Secrets::Secrets( const std::string & path , const std::string & name ) :
41 m_source(path)
42{
43 G_DEBUG( "GAuth::Secrets:ctor: [" << path << "]" ) ;
44 if( m_source != "/pam" )
45 m_imp = std::make_unique<SecretsFile>( path , true , name ) ;
46}
47
48 GAuth::Secrets::Secrets()
49{
50 if( m_source != "/pam" )
51 m_imp = std::make_unique<SecretsFile>( std::string() , false , std::string() ) ;
52}
53
54GAuth::Secrets::~Secrets()
55= default ;
56
57std::string GAuth::Secrets::source() const
58{
59 return m_source ;
60}
61
62 bool GAuth::Secrets::valid() const
63{
64 return m_source == "/pam" || m_imp->valid() ;
65}
66
67 GAuth::Secret GAuth::Secrets::clientSecret( const std::string & type ) const
68{
69 return valid() ? m_imp->clientSecret(type) : Secret::none() ;
70}
71
72 GAuth::Secret GAuth::Secrets::serverSecret( const std::string & type , const std::string & id ) const
73{
74 return valid() ? m_imp->serverSecret( type , id ) : Secret::none() ;
75}
76
77std::pair<std::string,std::string> GAuth::Secrets::serverTrust( const std::string & address_range ) const
78{
79 return valid() ? m_imp->serverTrust( address_range ) : std::make_pair(std::string(),std::string()) ;
80}
81
82 bool GAuth::Secrets::contains( const std::string & type ) const
83{
84 return valid() ? m_imp->contains( type ) : false ;
85}
86
GAuth::Secret
Encapsulates a shared secret from the secrets file plus the associated userid.
Definition: gsecret.h:42
GAuth::Secret::none
static Secret none()
Factory function that returns a secret that is not valid() and has an empty id().
Definition: gsecret.cpp:94
GAuth::SecretsFile::check
static void check(const std::string &path)
Checks the given file.
Definition: gsecretsfile.cpp:51
GAuth::Secrets::serverSecret
Secret serverSecret(const std::string &type, const std::string &id) const override
Override from GAuth::SaslServerSecrets.
Definition: gsecrets.cpp:72
GAuth::Secrets::contains
bool contains(const std::string &type) const override
Override from GAuth::SaslServerSecrets.
Definition: gsecrets.cpp:82
GAuth::Secrets::check
static void check(const std::string &, const std::string &, const std::string &)
Checks the given secret sources.
Definition: gsecrets.cpp:27
GAuth::Secrets::Secrets
Secrets()
Default constructor for an in-valid(), empty-path object.
Definition: gsecrets.cpp:48
GAuth::Secrets::valid
bool valid() const override
Override from GAuth::Valid virtual base.
Definition: gsecrets.cpp:62
glog.h
gsecrets.h
gsecretsfile.h
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31