summaryrefslogtreecommitdiff
path: root/src/operator-authenticate.cc
blob: 63228cdd5e9a9787057d1329c7d8efc3f8cc67d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
 *  @file operator-authenticate.cc
 *  @brief Implementation of the AUTHENTICATE command, incl. CRAM-MD5.
 *  @author Andreas Aardal Hanssen, Erwin Hoffmann
 *  @date 2002-2005, 2023
 */

#include "authenticate.h"
#include "base64.h"
#include "convert.h"
#include "depot.h"
#include "globals.h"
#include "iodevice.h"
#include "iofactory.h"
#include "operators.h"
#include "recursivedescent.h"
#include "session.h"

#include <cstring>
#include <string>

using namespace Binc;
using std::endl;
using std::string;
constexpr auto BAD = Operator::ProcessResult::BAD;
constexpr auto NO = Operator::ProcessResult::NO;
constexpr auto OK = Operator::ProcessResult::OK;
constexpr auto NOTHING = Operator::ProcessResult::NOTHING;

AuthenticateOperator::AuthenticateOperator() {}

AuthenticateOperator::~AuthenticateOperator() {}

const string AuthenticateOperator::getName() const
{
  return "AUTHENTICATE";
}

Session::State AuthenticateOperator::getState() const
{
  return Session::NONAUTHENTICATED;
}

Operator::ProcessResult AuthenticateOperator::Login(string &username, string &password)
{
  Session &session = Session::getInstance();

  bincClient << "+ " << base64encode("User Name") << endl;
  bincClient.flush();

  // Read user name
  string b64usr;
  for (;;) {
    char c;
    if (!bincClient.readChar(&c)) {
      session.setLastError("unexpected EOF");
      return BAD;
    }
    if (c == '\n') break;
    b64usr += c;
  }

  if (b64usr != "" && b64usr[0] == '*') {
    session.setLastError("Authentication cancelled by user");
    return NO;
  }

  bincClient << "+ " << base64encode("Password") << endl;
  bincClient.flush();

  // Read password
  string b64pwd;
  for (;;) {
    char c;
    if (!bincClient.readChar(&c)) {
      session.setLastError("unexpected EOF");
      return BAD;
    }
    if (c == '\n') break;
    b64pwd += c;
  }

  if (b64pwd != "" && b64pwd[0] == '*') {
    session.setLastError("Authentication cancelled by user");
    return NO;
  }

  username = base64decode(b64usr);
  password = base64decode(b64pwd);

  return OK;
}

Operator::ProcessResult AuthenticateOperator::Plain(string &username, string &password)
{
  Session &session = Session::getInstance();

  bincClient << "+ " << endl;
  bincClient.flush();

  string b64;
  for (;;) {
    char c;
    if (!bincClient.readChar(&c)) {
      session.setLastError("unexpected EOF");
      return BAD;
    }
    if (c == '\n') break;

    b64 += c;
  }

  if (b64.size() >= 1 && b64[0] == '*') {
    session.setLastError("Authentication cancelled by user");
    return NO;
  }

  string plain = base64decode(b64);
  string::size_type pos = 0;

  if ((pos = plain.find('\0')) == string::npos) {
    session.setLastError("Authentication failed. In PLAIN mode, "
                         "there must be at least two null characters "
                         "in the input string, but none were found");
    return NO;
  }

  plain = plain.substr(pos + 1);
  if ((pos = plain.find('\0')) == string::npos) {
    session.setLastError("Authentication failed. In PLAIN mode, "
                         "there must be at least two null characters "
                         "in the input string, but only one was found");
    return NO;
  }

  username = plain.substr(0, pos);
  password = plain.substr(pos + 1);

  return OK;
}

Operator::ProcessResult AuthenticateOperator::Cram(string &username,
                                                   string &password,
                                                   string &challenge)
{
  Session &session = Session::getInstance();

  // generate challenge first: <pid.time@fqdn> and deploy it to authenticator
  time_t timer;
  struct tm y2k = {};
  int timestamp;
  y2k.tm_hour = 0;
  y2k.tm_min = 0;
  y2k.tm_sec = 0;
  y2k.tm_year = 100;
  y2k.tm_mon = 0;
  y2k.tm_mday = 1;

  time(&timer); /* get current time; same as: timer = time(NULL)  */
  timestamp = difftime(timer, mktime(&y2k));

  challenge += "<";
  challenge += std::to_string(session.getPid());
  challenge += ".";
  challenge += std::to_string(timestamp);
  challenge += "@";
  challenge += session.getEnv("TCPLOCALHOST");
  challenge += ">";

  bincClient << "+ " << base64encode(challenge) << endl;
  bincClient.flush();

  // Read response
  string b64;
  for (;;) {
    char c;
    if (!bincClient.readChar(&c)) return BAD;
    if (c == '\n') break;
    b64 += c;
  }

  // Disentangle response
  string response = base64decode(b64);
  string::size_type pos = 0;

  if ((pos = response.find(' ')) == string::npos) {
    session.setLastError("Authentication failed. In CRAM-MD5 mode, "
                         "there must be a white space in the "
                         "input string between username and digest");
    return NO;
  }

  username = response.substr(0, pos);
  password = response.substr(pos + 1);

  return OK;
}

Operator::ProcessResult AuthenticateOperator::process(Depot &depot, Request &command)
{
  Session &session = Session::getInstance();

  string authtype = command.getAuthType();
  uppercase(authtype);

  string username;
  string password;
  string challenge;
  ProcessResult r = NOTHING;

  if (authtype == "LOGIN") {
    // we only allow this type of authentication over an unencryted connection
    // if it is explicitely commanded
    if (!session.command.ssl && !session.hasEnv("ALLOW_NONSSL_PLAINTEXT_LOGINS")) {
      session.setLastError("Plain text password authentication is disallowd. "
                           "Please enable StartTLS or TLS in your mail client.");
      return NO;
    }
    if ((r = Login(username, password)) != OK) return r;

  } else if (authtype == "PLAIN") {
    // we only allow this type of authentication over an TLS encrypted connection.
    if (!session.command.ssl && !session.hasEnv("ALLOW_NONSSL_PLAINTEXT_LOGINS")) {
      session.setLastError("Plain text password authentication is disallowd. "
                           "Please enable StartTLS or TLS in your mail client.");
      return NO;
    }
    if ((r = Plain(username, password)) != OK) return r;

  } else if (authtype == "CRAM-MD5") {
    // this type can be used even over unencrypted connections
    if ((r = Cram(username, password, challenge)) != OK) return r;

  } else {  // Any other disallowed
    session.setLastError("The authentication method " + toImapString(authtype)
                         + " is not supported. "
                           "Please try again with a different method. "
                           "There is built in support for \"PLAIN\" "
                           "and \"LOGIN\".");
    return NO;
  }

  putenv(strdup(("BINCIMAP_LOGIN=AUTHENTICATE+" + command.getTag()).c_str()));

  // put the username in the environment for logging purpose

  session.setEnv("USER", username.c_str());

  // the authenticate function calls a stub which does the actual
  // authentication. the function returns 0 (success), 1 (internal
  // error) or 2 (failed)

  switch (authenticate(depot, username, password, challenge)) {
  case 1:
    session.setLastError("An internal error occurred when you attempted "
                         "to log in to the IMAP server. Please contact "
                         "your system administrator.");
    return NO;
  case 2:
    session.setLastError("Login failed. Either your user name "
                         "or your password was wrong. Please try again, "
                         "and if the problem persists, please contact "
                         "your system administrator.");
    return NO;
  case 3:
    bincClient << "* BYE Timeout after " << IDLE_TIMEOUT << " seconds of inactivity." << endl;
    break;
  case -1:
    bincClient << "* BYE The server died unexpectedly. Please contact "
                  "your system administrator for more information."
               << endl;
    break;
  default:
    //       bincLog << "<" << username.c_str() << "> authenticated" << endl;
    break;
  }

  // auth was ok. go to logout state
  session.setState(Session::LOGOUT);
  return NOTHING;
}

Parser::ParseResult AuthenticateOperator::parse(Request &c_in, Parser &p)
{
  constexpr auto ACCEPT = Parser::ParseResult::ACCEPT;

  Session &session = Session::getInstance();

  if (c_in.getUidMode()) return Parser::ParseResult::REJECT;

  Parser::ParseResult res;

  if ((res = p.expectSPACE()) != ACCEPT) {
    session.setLastError("Expected single SPACE after AUTHENTICATE");
    return res;
  }

  string authtype;
  if ((res = p.expectAtom(authtype)) != ACCEPT) {
    session.setLastError("Expected auth_type after AUTHENTICATE SPACE");
    return Parser::ParseResult::ERROR;
  }

  if ((res = p.expectCRLF()) != ACCEPT) {
    session.setLastError("Expected CRLF after AUTHENTICATE SPACE auth_type");
    return res;
  }

  c_in.setAuthType(authtype);

  c_in.setName("AUTHENTICATE");
  return ACCEPT;
}