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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
#include "auth-common.h"
#if defined(PASSDB_CHECKPASSWORD) || defined(USERDB_CHECKPASSWORD)
#include "lib-signals.h"
#include "array.h"
#include "buffer.h"
#include "str.h"
#include "ioloop.h"
#include "hash.h"
#include "execv-const.h"
#include "env-util.h"
#include "safe-memset.h"
#include "strescape.h"
#include "child-wait.h"
#include "db-checkpassword.h"
#include <unistd.h>
#include <sys/wait.h>
#define CHECKPASSWORD_MAX_REQUEST_LEN 512
struct chkpw_auth_request {
struct db_checkpassword *db;
struct auth_request *request;
char *auth_password;
db_checkpassword_callback_t *callback;
void (*request_callback)();
pid_t pid;
int fd_out, fd_in;
struct io *io_out, *io_in;
string_t *input_buf;
size_t output_pos, output_len;
int exit_status;
bool exited:1;
};
struct db_checkpassword {
char *checkpassword_path, *checkpassword_reply_path;
HASH_TABLE(void *, struct chkpw_auth_request *) clients;
struct child_wait *child_wait;
};
static void
env_put_extra_fields(const ARRAY_TYPE(auth_field) *extra_fields)
{
const struct auth_field *field;
const char *key, *value;
array_foreach(extra_fields, field) {
key = t_str_ucase(field->key);
value = field->value != NULL ? field->value : "1";
env_put(key, value);
}
}
static void checkpassword_request_close(struct chkpw_auth_request *request)
{
io_remove(&request->io_in);
io_remove(&request->io_out);
i_close_fd(&request->fd_in);
i_close_fd(&request->fd_out);
}
static void checkpassword_request_free(struct chkpw_auth_request **_request)
{
struct chkpw_auth_request *request = *_request;
*_request = NULL;
if (!request->exited) {
hash_table_remove(request->db->clients,
POINTER_CAST(request->pid));
child_wait_remove_pid(request->db->child_wait, request->pid);
}
checkpassword_request_close(request);
if (request->auth_password != NULL) {
safe_memset(request->auth_password, 0,
strlen(request->auth_password));
i_free(request->auth_password);
}
auth_request_unref(&request->request);
str_free(&request->input_buf);
i_free(request);
}
static void checkpassword_finish(struct chkpw_auth_request **_request,
enum db_checkpassword_status status)
{
struct chkpw_auth_request *request = *_request;
const char *const *extra_fields;
*_request = NULL;
extra_fields = t_strsplit_tabescaped(str_c(request->input_buf));
request->callback(request->request, status, extra_fields,
request->request_callback);
checkpassword_request_free(&request);
}
static void checkpassword_internal_failure(struct chkpw_auth_request **request)
{
checkpassword_finish(request, DB_CHECKPASSWORD_STATUS_INTERNAL_FAILURE);
}
static void
checkpassword_request_finish_auth(struct chkpw_auth_request *request)
{
switch (request->exit_status) {
/* standard checkpassword exit codes: */
case 1:
e_info(authdb_event(request->request),
"Login failed (status=%d)",
request->exit_status);
checkpassword_finish(&request, DB_CHECKPASSWORD_STATUS_FAILURE);
break;
case 0:
if (request->input_buf->used == 0) {
e_error(authdb_event(request->request),
"Received no input");
checkpassword_internal_failure(&request);
break;
}
checkpassword_finish(&request, DB_CHECKPASSWORD_STATUS_OK);
break;
case 2:
/* checkpassword is called with wrong parameters? unlikely */
e_error(authdb_event(request->request),
"Child %s exited with status 2 (tried to use "
"userdb-only checkpassword program for passdb?)",
dec2str(request->pid));
checkpassword_internal_failure(&request);
break;
case 111:
/* temporary problem, treat as internal error */
default:
/* whatever error.. */
e_error(authdb_event(request->request),
"Child %s exited with status %d",
dec2str(request->pid), request->exit_status);
checkpassword_internal_failure(&request);
break;
}
}
static void
checkpassword_request_finish_lookup(struct chkpw_auth_request *request)
{
switch (request->exit_status) {
case 3:
/* User does not exist. */
e_info(authdb_event(request->request),
"User unknown");
checkpassword_finish(&request, DB_CHECKPASSWORD_STATUS_FAILURE);
break;
case 2:
/* This is intentionally not 0. checkpassword-reply exits with
2 on success when AUTHORIZED is set. */
if (request->input_buf->used == 0) {
e_error(authdb_event(request->request),
"Received no input");
checkpassword_internal_failure(&request);
break;
}
checkpassword_finish(&request, DB_CHECKPASSWORD_STATUS_OK);
break;
default:
/* whatever error... */
e_error(authdb_event(request->request),
"Child %s exited with status %d",
dec2str(request->pid), request->exit_status);
checkpassword_internal_failure(&request);
break;
}
}
static void
checkpassword_request_half_finish(struct chkpw_auth_request *request)
{
/* the process must have exited, and the input fd must have closed */
if (!request->exited || request->fd_in != -1)
return;
if (request->auth_password != NULL)
checkpassword_request_finish_auth(request);
else
checkpassword_request_finish_lookup(request);
}
static void env_put_auth_vars(struct auth_request *request)
{
const struct var_expand_table *tab;
unsigned int i;
tab = auth_request_get_var_expand_table(request, NULL);
for (i = 0; tab[i].key != '\0' || tab[i].long_key != NULL; i++) {
/* avoid keeping passwords in environment .. just in case
an attacker might find it from there. environment is no
longer world-readable in modern OSes, but maybe the attacker
could be running with the same UID. of course then the
attacker could usually ptrace() the process, except that is
disabled on some secured systems. so, although I find it
highly unlikely anyone could actually attack Dovecot this
way in a real system, be safe just in case. besides, lets
try to keep at least minimally compatible with the
checkpassword API. */
if (tab[i].long_key != NULL && tab[i].value != NULL &&
strcasecmp(tab[i].long_key, "password") != 0) {
env_put(t_strdup_printf("AUTH_%s",
t_str_ucase(tab[i].long_key)),
tab[i].value);
}
}
}
static void checkpassword_setup_env(struct auth_request *request)
{
const struct auth_request_fields *fields = &request->fields;
/* Besides passing the standard username and password in a
pipe, also pass some other possibly interesting information
via environment. Use UCSPI names for local/remote IPs. */
env_put("PROTO", "TCP"); /* UCSPI */
env_put("ORIG_UID", dec2str(getuid()));
env_put("SERVICE", fields->service);
if (fields->local_ip.family != 0) {
env_put("TCPLOCALIP", net_ip2addr(&fields->local_ip));
/* FIXME: for backwards compatibility only,
remove some day */
env_put("LOCAL_IP", net_ip2addr(&fields->local_ip));
}
if (fields->remote_ip.family != 0) {
env_put("TCPREMOTEIP", net_ip2addr(&fields->remote_ip));
/* FIXME: for backwards compatibility only,
remove some day */
env_put("REMOTE_IP", net_ip2addr(&fields->remote_ip));
}
if (fields->local_port != 0)
env_put("TCPLOCALPORT", dec2str(fields->local_port));
if (fields->remote_port != 0)
env_put("TCPREMOTEPORT", dec2str(fields->remote_port));
if (fields->master_user != NULL)
env_put("MASTER_USER", fields->master_user);
if (!auth_fields_is_empty(fields->extra_fields)) {
const ARRAY_TYPE(auth_field) *extra_fields =
auth_fields_export(fields->extra_fields);
/* extra fields could come from master db */
env_put_extra_fields(extra_fields);
}
env_put_auth_vars(request);
}
static const char *
checkpassword_get_cmd(struct auth_request *request, const char *args,
const char *checkpassword_reply_path)
{
string_t *str;
const char *error;
str = t_str_new(256);
if (auth_request_var_expand(str, args, request, NULL, &error) <= 0) {
e_error(authdb_event(request),
"Failed to expand checkpassword_path=%s: %s",
args, error);
}
return t_strconcat(str_c(str), " ", checkpassword_reply_path, NULL);
}
static void checkpassword_child_input(struct chkpw_auth_request *request)
{
unsigned char buf[1024];
ssize_t ret;
ret = read(request->fd_in, buf, sizeof(buf));
if (ret > 0) {
str_append_data(request->input_buf, buf, ret);
return;
}
if (ret < 0) {
e_error(authdb_event(request->request),
"read() failed: %m");
checkpassword_internal_failure(&request);
} else if (memchr(str_data(request->input_buf), '\0',
str_len(request->input_buf)) != NULL) {
e_error(authdb_event(request->request),
"NUL characters in checkpassword reply");
checkpassword_internal_failure(&request);
} else if (strchr(str_c(request->input_buf), '\n') != NULL) {
e_error(authdb_event(request->request),
"LF characters in checkpassword reply");
checkpassword_internal_failure(&request);
} else {
e_debug(authdb_event(request->request),
"Received input: %s", str_c(request->input_buf));
checkpassword_request_close(request);
checkpassword_request_half_finish(request);
}
}
static void checkpassword_child_output(struct chkpw_auth_request *request)
{
/* Send: username \0 password \0 timestamp \0.
Must be 512 bytes or less. The "timestamp" parameter is actually
useful only for APOP authentication. We don't support it, so
keep it empty */
struct auth_request *auth_request = request->request;
buffer_t *buf;
const unsigned char *data;
size_t size;
ssize_t ret;
buf = t_buffer_create(CHECKPASSWORD_MAX_REQUEST_LEN);
buffer_append(buf, auth_request->fields.user,
strlen(auth_request->fields.user)+1);
if (request->auth_password != NULL) {
buffer_append(buf, request->auth_password,
strlen(request->auth_password)+1);
} else {
buffer_append_c(buf, '\0');
}
buffer_append_c(buf, '\0');
data = buffer_get_data(buf, &size);
i_assert(size == request->output_len);
/* already checked this */
i_assert(size <= CHECKPASSWORD_MAX_REQUEST_LEN);
ret = write(request->fd_out, data + request->output_pos,
size - request->output_pos);
if (ret <= 0) {
if (ret < 0) {
e_error(authdb_event(request->request),
"write() failed: %m");
} else {
e_error(authdb_event(request->request),
"write() returned 0");
}
checkpassword_internal_failure(&request);
return;
}
request->output_pos += ret;
if (request->output_pos < size)
return;
/* finished sending the data */
io_remove(&request->io_out);
if (close(request->fd_out) < 0)
e_error(authdb_event(request->request), "close() failed: %m");
request->fd_out = -1;
}
static void ATTR_NORETURN
checkpassword_exec(struct db_checkpassword *db, struct auth_request *request,
int fd_in, int fd_out, bool authenticate)
{
const char *cmd, *const *args;
/* fd 3 is used to send the username+password for the script
fd 4 is used to communicate with checkpassword-reply */
if (dup2(fd_out, 3) < 0 || dup2(fd_in, 4) < 0) {
e_error(authdb_event(request),
"dup2() failed: %m");
lib_exit(111);
}
if (!authenticate) {
/* We want to retrieve passdb/userdb data and don't do
authorization, so we need to signalize the
checkpassword program that the password shall be
ignored by setting AUTHORIZED. This needs a
special checkpassword program which knows how to
handle this. */
env_put("AUTHORIZED", "1");
if (request->wanted_credentials_scheme != NULL) {
/* passdb credentials lookup */
env_put("CREDENTIALS_LOOKUP", "1");
env_put("SCHEME", request->wanted_credentials_scheme);
}
}
checkpassword_setup_env(request);
cmd = checkpassword_get_cmd(request, db->checkpassword_path,
db->checkpassword_reply_path);
e_debug(authdb_event(request), "execute: %s", cmd);
/* very simple argument splitting. */
args = t_strsplit(cmd, " ");
execv_const(args[0], args);
}
static void sigchld_handler(const struct child_wait_status *status,
struct db_checkpassword *db)
{
struct chkpw_auth_request *request =
hash_table_lookup(db->clients, POINTER_CAST(status->pid));
i_assert(request != NULL);
hash_table_remove(db->clients, POINTER_CAST(status->pid));
request->exited = TRUE;
if (WIFSIGNALED(status->status)) {
e_error(authdb_event(request->request),
"Child %s died with signal %d",
dec2str(status->pid), WTERMSIG(status->status));
checkpassword_internal_failure(&request);
} else if (WIFEXITED(status->status)) {
request->exit_status = WEXITSTATUS(status->status);
e_debug(authdb_event(request->request),
"exit_status=%d", request->exit_status);
checkpassword_request_half_finish(request);
} else {
/* shouldn't happen */
e_debug(authdb_event(request->request),
"Child %s exited with status=%d",
dec2str(status->pid), status->status);
checkpassword_internal_failure(&request);
}
}
void db_checkpassword_call(struct db_checkpassword *db,
struct auth_request *request,
const char *auth_password,
db_checkpassword_callback_t *callback,
void (*request_callback)())
{
struct chkpw_auth_request *chkpw_auth_request;
size_t output_len;
int fd_in[2], fd_out[2];
pid_t pid;
/* <username> \0 <password> \0 timestamp \0 */
output_len = strlen(request->fields.user) + 3;
if (auth_password != NULL)
output_len += strlen(auth_password);
if (output_len > CHECKPASSWORD_MAX_REQUEST_LEN) {
e_info(authdb_event(request),
"Username+password combination too long (%zu bytes)",
output_len);
callback(request, DB_CHECKPASSWORD_STATUS_FAILURE,
NULL, request_callback);
return;
}
fd_in[0] = -1;
if (pipe(fd_in) < 0 || pipe(fd_out) < 0) {
e_error(authdb_event(request),
"pipe() failed: %m");
if (fd_in[0] != -1) {
i_close_fd(&fd_in[0]);
i_close_fd(&fd_in[1]);
}
callback(request, DB_CHECKPASSWORD_STATUS_INTERNAL_FAILURE,
NULL, request_callback);
return;
}
pid = fork();
if (pid == -1) {
e_error(authdb_event(request),
"fork() failed: %m");
i_close_fd(&fd_in[0]);
i_close_fd(&fd_in[1]);
i_close_fd(&fd_out[0]);
i_close_fd(&fd_out[1]);
callback(request, DB_CHECKPASSWORD_STATUS_INTERNAL_FAILURE,
NULL, request_callback);
return;
}
if (pid == 0) {
/* child */
i_close_fd(&fd_in[0]);
i_close_fd(&fd_out[1]);
checkpassword_exec(db, request, fd_in[1], fd_out[0],
auth_password != NULL);
/* not reached */
}
if (close(fd_in[1]) < 0) {
e_error(authdb_event(request),
"close(fd_in[1]) failed: %m");
}
if (close(fd_out[0]) < 0) {
e_error(authdb_event(request),
"close(fd_out[0]) failed: %m");
}
auth_request_ref(request);
chkpw_auth_request = i_new(struct chkpw_auth_request, 1);
chkpw_auth_request->db = db;
chkpw_auth_request->pid = pid;
chkpw_auth_request->fd_in = fd_in[0];
chkpw_auth_request->fd_out = fd_out[1];
chkpw_auth_request->auth_password = i_strdup(auth_password);
chkpw_auth_request->request = request;
chkpw_auth_request->output_len = output_len;
chkpw_auth_request->input_buf = str_new(default_pool, 256);
chkpw_auth_request->callback = callback;
chkpw_auth_request->request_callback = request_callback;
chkpw_auth_request->io_in =
io_add(fd_in[0], IO_READ, checkpassword_child_input,
chkpw_auth_request);
chkpw_auth_request->io_out =
io_add(fd_out[1], IO_WRITE, checkpassword_child_output,
chkpw_auth_request);
hash_table_insert(db->clients, POINTER_CAST(pid), chkpw_auth_request);
child_wait_add_pid(db->child_wait, pid);
}
struct db_checkpassword *
db_checkpassword_init(const char *checkpassword_path,
const char *checkpassword_reply_path)
{
struct db_checkpassword *db;
db = i_new(struct db_checkpassword, 1);
db->checkpassword_path = i_strdup(checkpassword_path);
db->checkpassword_reply_path = i_strdup(checkpassword_reply_path);
hash_table_create_direct(&db->clients, default_pool, 0);
db->child_wait =
child_wait_new_with_pid((pid_t)-1, sigchld_handler, db);
return db;
}
void db_checkpassword_deinit(struct db_checkpassword **_db)
{
struct db_checkpassword *db = *_db;
struct hash_iterate_context *iter;
void *key;
struct chkpw_auth_request *request;
*_db = NULL;
iter = hash_table_iterate_init(db->clients);
while (hash_table_iterate(iter, db->clients, &key, &request))
checkpassword_internal_failure(&request);
hash_table_iterate_deinit(&iter);
child_wait_free(&db->child_wait);
hash_table_destroy(&db->clients);
i_free(db->checkpassword_reply_path);
i_free(db->checkpassword_path);
i_free(db);
}
#endif
|