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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/**
* @file authenticate.cc
* @brief Implementation of the common (C/R) authentication mechanism.
* @author Andreas Aardal Hanssen, Erwin Hoffmann
* @date 2002-2005, 2023
*/
#include <string>
#include <vector>
#include <errno.h>
#include <signal.h>
#include <grp.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
// #ifndef HAVE_SYS_WAIT_H
// #include <wait.h>
// #else
#include <sys/wait.h>
// #endif
#include "authenticate.h"
#include "convert.h"
#include "globals.h"
#include "iodevice.h"
#include "iofactory.h"
#include "session.h"
using namespace Binc;
using std::endl;
using std::string;
// 0 = ok
// 1 = internal error
// 2 = failed
// 3 = timeout
// -1 = abort
int Binc::authenticate(Depot &depot,
const string &username,
const string &password,
const string &challenge)
{
Session &session = Session::getInstance();
session.setUserID(username);
// check if checkpassword is present
if (access(session.unparsedArgs[0], X_OK) != 0) { // x is enough
bincError << "unable to start authenticator " << session.unparsedArgs[0] << ": " << strerror(errno)
<< endl;
return 1;
}
// The information supplied on descriptor 3 is a login name
// terminated by \0, a password terminated by \0, a timestamp
// terminated by \0, and possibly more data. There are no other
// restrictions on the form of the login name, password, and
// timestamp.
int authintercom[2];
int intercomw[2];
int intercomr[2];
bool authenticated = false;
if (pipe(authintercom) == -1) {
session.setLastError("An error occurred when creating pipes: " + string(strerror(errno)));
return -1;
}
if (pipe(intercomw) == -1) {
session.setLastError("An error occurred when creating pipes: " + string(strerror(errno)));
close(authintercom[0]);
close(authintercom[1]);
return -1;
}
if (pipe(intercomr) == -1) {
session.setLastError("An error occurred when creating pipes: " + string(strerror(errno)));
close(intercomw[0]);
close(intercomr[0]);
close(authintercom[0]);
close(authintercom[1]);
return -1;
}
string timestamp;
time_t t = time(nullptr);
char *c;
if ((c = ctime(&t)) != nullptr) {
timestamp = c;
trim(timestamp);
} else {
timestamp = "unknown timestamp";
}
string pid = std::to_string(session.getPid());
// execute authentication module
int result;
int childspid = fork();
if (childspid == -1) {
bincLog << "bincimap-up: pid " << pid << " failed to start main server: " << strerror(errno) << endl;
return 1;
}
if (childspid == 0) {
close(authintercom[1]);
close(intercomr[0]);
close(intercomw[1]);
if (dup2(intercomr[1], 1) == -1) {
bincDebug << "bincimap-up: pid " << pid
<< " authenticate(), [auth module] dup2 failed: " << strerror(errno) << endl;
bincDebug.flush();
exit(111);
}
if (dup2(intercomw[0], 0) == -1) {
bincDebug << "bincimap-up: pid " << pid
<< " authenticate(), [auth module] dup2 failed: " << strerror(errno) << endl;
bincDebug.flush();
exit(111);
}
if (dup2(authintercom[0], 3) == -1) {
bincDebug << "bincimap-up: pid " << pid
<< " authenticate(), [auth module] dup2 failed: " << strerror(errno) << endl;
bincDebug.flush();
exit(111);
}
if (session.unparsedArgs[0] != nullptr) {
execvp(session.unparsedArgs[0], &session.unparsedArgs[0]);
bincDebug << "bincimap-up: pid " << pid << " authenticate(), [auth module] invocation of "
<< session.unparsedArgs[0] << " failed: " << strerror(errno) << endl;
bincDebug.flush();
exit(111);
}
bincLog << "bincimap-up: pid " << pid
<< " missing mandatory -- in argument list,"
" after bincimap-up + arguments, before authenticator."
" Please check your run scripts and the man page bincimap(1) for"
" more on how to invoke Binc IMAP."
<< endl;
bincDebug.flush();
exit(111);
}
close(authintercom[0]);
// create the string of data to be passed to the checkpassword stub
int dataSize = username.length() + password.length() + challenge.length() + timestamp.length();
dataSize += 4;
char *checkpasswordData = new char[dataSize];
char *cpTmp = checkpasswordData;
strcpy(cpTmp, username.c_str());
cpTmp += username.length();
*cpTmp++ = '\0';
strcpy(cpTmp, password.c_str());
cpTmp += password.length();
*cpTmp++ = '\0';
// add challenge
strcpy(cpTmp, challenge.c_str());
cpTmp += challenge.length();
*cpTmp++ = '\0';
strcpy(cpTmp, timestamp.c_str());
cpTmp += timestamp.length();
*cpTmp++ = '\0';
bincDebug << "bincimap-up: pid " << pid << " authenticate(), writing username/password to "
<< session.unparsedArgs[0] << endl;
// write the userid
signal(SIGPIPE, SIG_IGN);
int res = write(authintercom[1], checkpasswordData, dataSize);
delete[] checkpasswordData;
if (res != dataSize) {
bincWarning << "bincimap-up: pid " << pid << " error writing to authenticator "
<< session.unparsedArgs[0] << ": " << strerror(errno) << endl;
return 1;
}
// close the write channel. this is necessary for the checkpassword
// module to see an EOF.
close(authintercom[1]);
close(intercomr[1]);
close(intercomw[0]);
fd_set rmask;
FD_ZERO(&rmask);
FD_SET(fileno(stdin), &rmask);
FD_SET(intercomr[0], &rmask);
int maxfd = intercomr[0];
bool disconnected = false;
bool timedout = false;
bincClient.clearFlags(IODevice::HasInputLimit);
bool eof = false;
while (!eof) {
fd_set rtmp = rmask;
struct timeval timeout;
// time out 5 minutes after the idle timeout. we expect the main
// server to time out at the right time, but will shut down at
// T+5m in case of a server lockup.
timeout.tv_sec = IDLE_TIMEOUT + AUTH_PENALTY * AUTH_TIMEOUT;
timeout.tv_usec = 0;
// select sometimes returns when we attach to the process with
// tracing tools such as ktrace and strace, setting errno to
// EINTR.
int n;
do {
n = select(maxfd + 1, &rtmp, nullptr, nullptr, &timeout);
} while (n < 0 && errno == EINTR);
if (n < 0) {
bincWarning << "bincimpa-up: pid " << pid << " error: invalid exit from select, "
<< strerror(errno) << endl;
break;
}
if (n == 0) {
bincLog << "bincimap-up: pid " << pid << " server timed out after " << IDLE_TIMEOUT << " seconds"
<< endl;
timedout = true;
break;
}
if (FD_ISSET(fileno(stdin), &rtmp)) {
authenticated = true;
do {
string data;
int ret = bincClient.readStr(&data);
if (ret == 0 || ret == -1) {
session.setLastError("client disconnected");
eof = true;
disconnected = true;
break;
}
// Fall through. Triggered when there was no data
// to read, even though no error has occurred
if (ret == -2) continue;
int w;
do {
w = write(intercomw[1], data.c_str(), data.length());
} while (w < 0 && errno == EINTR);
if (w > 0) Session::getInstance().addReadBytes(w);
if (w < 0) {
bincDebug << "bincimap-up: pid " << pid << " error writing to server: " << strerror(errno)
<< endl;
eof = true;
}
} while (bincClient.canRead());
}
if (FD_ISSET(intercomr[0], &rtmp)) {
char buf[8192];
int ret = read(intercomr[0], buf, sizeof(buf));
if (ret == 0) {
// Main server has shut down
eof = true;
break;
} else if (ret == -1) {
bincDebug << "bincimap-up: pid " << pid << " error reading from server: " << strerror(errno)
<< endl;
eof = true;
break;
} else {
// umask(0);
Session::getInstance().addWriteBytes(ret);
bincClient << string(buf, ret);
bincClient.flush();
}
}
}
close(intercomr[0]);
close(intercomw[1]);
// catch the dead baby
if (waitpid(childspid, &result, 0) != childspid) {
bincLog << "bincimap-up: pid " << pid << " <" << username
<< "> authentication failed: " << (authenticated ? "server " : session.unparsedArgs[0])
<< " waitpid returned unexpected value" << endl;
string tmp = strerror(errno);
return -1;
}
// if the server died because we closed the sockets after a timeout, exit 3.
if (timedout) return 3;
if (disconnected) return 0;
if (WIFSIGNALED(result)) {
bincLog << "bincimap-up: pid " << pid << " <" << username
<< "> authentication failed: " << (authenticated ? "server" : session.unparsedArgs[0])
<< " died by signal " << WTERMSIG(result) << endl;
sleep(AUTH_PENALTY);
session.setState(Session::LOGOUT);
return -1;
}
bincDebug << "bincimap-up: pid " << pid << " authenticate() ,"
<< (authenticated ? "authenticator" : "server") << " exited with code "
<< WEXITSTATUS(result) << endl;
switch (WEXITSTATUS(result)) {
case 0:
break;
case 1:
// authentication failed - sleep
bincLog << "bincimap-up: pid " << pid << " <" << username << "> failed to log in" << endl;
sleep(AUTH_PENALTY);
return 2;
case 2:
case 111: // wrong call or missing auth data
// abused
bincLog << "bincimap-up: pid " << pid << " <" << username
<< "> authentication failed: " << (authenticated ? "authenticator" : "server")
<< " reports wrong usage" << endl;
sleep(AUTH_PENALTY);
return 2;
default:
// internal error -- or authenticator fooled us
bincLog << "bincimap-up: pid " << pid << " <" << username
<< "> authentication failed: " << (authenticated ? "authenticator" : "server")
<< " returned " << WEXITSTATUS(result) << endl;
return -1;
}
return 0;
}
|