E-MailRelay

gsecretsfile.h
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 gsecretsfile.h
19///
20
21#ifndef G_AUTH_SECRETS_FILE_H
22#define G_AUTH_SECRETS_FILE_H
23
24#include "gdef.h"
25#include "gpath.h"
26#include "gdatetime.h"
27#include "gsecret.h"
28#include "gexception.h"
29#include <string>
30#include <vector>
31#include <map>
32#include <set>
33#include <iostream>
34#include <utility>
35
36 namespace GAuth
37{
38 class SecretsFile ;
39}
40
41//| \class GAuth::SecretsFile
42/// A class to read authentication secrets from file, used by GAuth::Secrets.
43/// Updates to the file are detected automatically.
44///
45 class GAuth::SecretsFile
46{
47 public:
48 G_EXCEPTION( Error , "invalid secrets file" ) ;
49
50 static void check( const std::string & path ) ;
51 ///< Checks the given file. Logs warnings and throws an exception
52 ///< if there are any fatal errors.
53
54 SecretsFile( const G::Path & path , bool auto_reread , const std::string & debug_name ) ;
55 ///< Constructor to read "client" and "server" records from
56 ///< the named file. The path is optional; see valid().
57
58 bool valid() const ;
59 ///< Returns true if the file path was supplied in the ctor.
60
61 Secret clientSecret( const std::string & type ) const ;
62 ///< Returns the client id and secret for the given type.
63 ///< Returns the empty string if none.
64
65 Secret serverSecret( const std::string & type , const std::string & id ) const ;
66 ///< Returns the server secret for the given id and type.
67 ///< Returns the empty string if none.
68
69 std::pair<std::string,std::string> serverTrust( const std::string & address_range ) const ;
70 ///< Returns a non-empty trustee name if the server trusts clients
71 ///< in the given address range, together with context information.
72
73 std::string path() const ;
74 ///< Returns the file path, as supplied to the ctor.
75
76 bool contains( const std::string & type , const std::string & id = {} ) const ;
77 ///< Returns true if a server secret of the given type
78 ///< is available for the particular user or any user.
79
80 private:
81 struct Value
82 {
83 Value(const std::string &s_,unsigned int n_):s(s_),n(n_) {}
84 std::string s ;
85 unsigned int n ;
86 } ;
87 using Map = std::map<std::string,Value> ;
88 using Set = std::set<std::string> ;
89 using Warning = std::pair<unsigned long,std::string> ;
90 using Warnings = std::vector<Warning> ;
91 struct Contents
92 {
93 Map m_map ;
94 Set m_types ;
95 Warnings m_warnings ;
96 } ;
97
98 private:
99 void read( const G::Path & ) ;
100 void reread() const ;
101 void reread( int ) ;
102 static Contents readContents( const G::Path & ) ;
103 static Contents readContents( std::istream & ) ;
104 static void processLine( Contents & ,
105 unsigned int , const std::string & side , const std::string & , const std::string & , const std::string & ) ;
106 static void processLineImp( Contents & ,
107 unsigned int , const std::string & side , const std::string & , const std::string & , const std::string & ) ;
108 static void showWarnings( const Warnings & warnings , const G::Path & path , const std::string & debug_name = {} ) ;
109 static void addWarning( Contents & , unsigned int , const std::string & , const std::string & = {} ) ;
110 static std::string canonical( const std::string & encoding_type ) ;
111 static std::string serverKey( const std::string & , const std::string & ) ;
112 static std::string clientKey( const std::string & ) ;
113 static G::SystemTime readFileTime( const G::Path & ) ;
114 static std::string line( unsigned int ) ;
115
116 private:
117 G::Path m_path ;
118 bool m_auto ;
119 std::string m_debug_name ;
120 bool m_valid ;
121 Contents m_contents ;
122 G::SystemTime m_file_time ;
123 G::SystemTime m_check_time ;
124} ;
125
126#endif
GAuth::Secret
Encapsulates a shared secret from the secrets file plus the associated userid.
Definition: gsecret.h:42
GAuth::SecretsFile
A class to read authentication secrets from file, used by GAuth::Secrets.
Definition: gsecretsfile.h:46
GAuth::SecretsFile::serverSecret
Secret serverSecret(const std::string &type, const std::string &id) const
Returns the server secret for the given id and type.
Definition: gsecretsfile.cpp:279
GAuth::SecretsFile::check
static void check(const std::string &path)
Checks the given file.
Definition: gsecretsfile.cpp:51
GAuth::SecretsFile::valid
bool valid() const
Returns true if the file path was supplied in the ctor.
Definition: gsecretsfile.cpp:62
GAuth::SecretsFile::path
std::string path() const
Returns the file path, as supplied to the ctor.
Definition: gsecretsfile.cpp:313
GAuth::SecretsFile::contains
bool contains(const std::string &type, const std::string &id={}) const
Returns true if a server secret of the given type is available for the particular user or any user.
Definition: gsecretsfile.cpp:318
GAuth::SecretsFile::SecretsFile
SecretsFile(const G::Path &path, bool auto_reread, const std::string &debug_name)
Constructor to read "client" and "server" records from the named file.
Definition: gsecretsfile.cpp:39
GAuth::SecretsFile::serverTrust
std::pair< std::string, std::string > serverTrust(const std::string &address_range) const
Returns a non-empty trustee name if the server trusts clients in the given address range,...
Definition: gsecretsfile.cpp:297
GAuth::SecretsFile::clientSecret
Secret clientSecret(const std::string &type) const
Returns the client id and secret for the given type.
Definition: gsecretsfile.cpp:262
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::SystemTime
Represents a unix-epoch time with microsecond resolution.
Definition: gdatetime.h:125
gdatetime.h
gexception.h
gpath.h
gsecret.h
GAuth
SASL authentication classes.
Definition: gcram.cpp:36