E-MailRelay
gpopauth.cpp
1 //
2 // Copyright (C) 2001-2018 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 // gpopauth.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gpop.h"
23 #include "gpopauth.h"
24 #include "gsaslserver.h"
25 #include "gsaslserverfactory.h"
26 #include "gstr.h"
27 
28 /// \class GPop::AuthImp
29 /// A private pimple-pattern implementation class used by GPop::Auth.
31 {
32 public:
33  explicit AuthImp( const Secrets & ) ;
34  bool valid() const ;
35  bool init( const std::string & mechanism ) ;
36  bool authenticated( const std::string & , const std::string & ) ;
37  bool mustChallenge() const ;
38  std::string challenge() ;
39  std::string id() const ;
40  std::string mechanisms() const ;
41  bool sensitive() const ;
42 
43 private:
44  const Secrets & m_secrets ;
45  unique_ptr<GAuth::SaslServer> m_sasl ;
46  bool m_first ;
47 } ;
48 
49 // ==
50 
51 GPop::AuthImp::AuthImp( const Secrets & secrets ) :
52  m_secrets(secrets) ,
53  m_sasl(GAuth::SaslServerFactory::newSaslServer(secrets,true/*apop*/)) ,
54  m_first(true)
55 {
56  m_sasl->init( "APOP" ) ; // we need the apop challenge string early
57 }
58 
59 bool GPop::AuthImp::valid() const
60 {
61  return m_secrets.valid() && m_sasl->active() ;
62 }
63 
64 bool GPop::AuthImp::init( const std::string & mechanism )
65 {
66  G_DEBUG( "GPop::AuthImp::init: mechanism " << mechanism ) ;
67 
68  // the first APOP authentication attempt must keep the original
69  // challenge as advertised in the server-ready line -- so only the
70  // first APOP request will work, and only if before any AUTH
71  // request -- could do better
72  //
73  if( m_first )
74  {
75  m_first = false ;
76  if( mechanism == "APOP" )
77  return true ;
78  }
79 
80  return m_sasl->init( mechanism ) ;
81 }
82 
83 bool GPop::AuthImp::authenticated( const std::string & p1 , const std::string & p2 )
84 {
85  bool done_1 = false ;
86  std::string challenge_1 = m_sasl->apply( p1 , done_1 ) ;
87  if( done_1 )
88  {
89  return challenge_1.empty() && m_sasl->authenticated() ;
90  }
91  else
92  {
93  bool done_2 = false ;
94  std::string challenge_2 = m_sasl->apply( p2 , done_2 ) ;
95  return done_2 && challenge_2.empty() && m_sasl->authenticated() ;
96  }
97 }
98 
99 bool GPop::AuthImp::mustChallenge() const
100 {
101  return m_sasl->mustChallenge() ;
102 }
103 
104 std::string GPop::AuthImp::challenge()
105 {
106  return m_sasl->initialChallenge() ;
107 }
108 
109 std::string GPop::AuthImp::id() const
110 {
111  return m_sasl->id() ;
112 }
113 
114 std::string GPop::AuthImp::mechanisms() const
115 {
116  return m_sasl->mechanisms() ;
117 }
118 
119 bool GPop::AuthImp::sensitive() const
120 {
121  return m_sasl->requiresEncryption() ;
122 }
123 
124 // ==
125 
126 GPop::Auth::Auth( const Secrets & secrets ) :
127  m_imp( new AuthImp(secrets) )
128 {
129 }
130 
132 {
133  delete m_imp ;
134 }
135 
136 bool GPop::Auth::valid() const
137 {
138  return m_imp->valid() ;
139 }
140 
141 bool GPop::Auth::init( const std::string & mechanism )
142 {
143  return m_imp->init(mechanism) ;
144 }
145 
146 bool GPop::Auth::authenticated( const std::string & p1 , const std::string & p2 )
147 {
148  return m_imp->authenticated(p1,p2) ;
149 }
150 
152 {
153  return m_imp->mustChallenge() ;
154 }
155 
157 {
158  return m_imp->challenge() ;
159 }
160 
161 std::string GPop::Auth::id() const
162 {
163  return m_imp->id() ;
164 }
165 
166 std::string GPop::Auth::mechanisms() const
167 {
168  return m_imp->mechanisms() ;
169 }
170 
172 {
173  return m_imp->sensitive() ;
174 }
A simple interface to a store of secrets as used in authentication.
Definition: gpopsecrets.h:43
std::string mechanisms() const
Returns a space-separated list of standard, supported SASL mechanisms (so not including APOP)...
Definition: gpopauth.cpp:166
bool mustChallenge() const
Returns true if the init()ialised mechanism requires an initial challenge.
Definition: gpopauth.cpp:151
bool init(const std::string &mechanism)
Initialises or reinitialises with the specified mechanism.
Definition: gpopauth.cpp:141
std::string challenge()
Returns an initial challenge appropriate to the current mechanism.
Definition: gpopauth.cpp:156
std::string id() const
Returns the authenticated user id.
Definition: gpopauth.cpp:161
bool sensitive() const
Returns true if the implementation requires authentication to be restricted to encrypted transports...
Definition: gpopauth.cpp:171
bool authenticated(const std::string &rsp1, const std::string &rsp2)
Authenticates a one-step (APOP,PLAIN) or two-step (LOGIN) challenge-response sequence.
Definition: gpopauth.cpp:146
A private pimple-pattern implementation class used by GPop::Auth.
Definition: gpopauth.cpp:30
~Auth()
Destructor.
Definition: gpopauth.cpp:131
bool valid() const
Returns true if the secrets are valid.
Definition: gpopauth.cpp:136
SASL authentication classes.
Definition: gcram.h:30
Auth(const Secrets &)
Constructor.
Definition: gpopauth.cpp:126