aboutsummaryrefslogtreecommitdiff
path: root/bin/auditdistd/receiver.c
blob: fe08a3c332a313879a0561fdba6d48f7ce9002e3 (plain) (blame)
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*-
 * Copyright (c) 2012 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Pawel Jakub Dawidek under sponsorship from
 * the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/receiver.c#1 $
 */

#include "config.h"

#include <sys/param.h>
#if defined(HAVE_SYS_ENDIAN_H) && defined(HAVE_BSWAP)
#include <sys/endian.h>
#else /* !HAVE_SYS_ENDIAN_H || !HAVE_BSWAP */
#ifdef HAVE_MACHINE_ENDIAN_H
#include <machine/endian.h>
#else /* !HAVE_MACHINE_ENDIAN_H */
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#else /* !HAVE_ENDIAN_H */
#error "No supported endian.h"
#endif /* !HAVE_ENDIAN_H */
#endif /* !HAVE_MACHINE_ENDIAN_H */
#include <compat/endian.h>
#endif /* !HAVE_SYS_ENDIAN_H || !HAVE_BSWAP */
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_LIBUTIL_H
#include <libutil.h>
#endif
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#ifndef HAVE_STRLCPY
#include <compat/strlcpy.h>
#endif
#ifndef HAVE_FSTATAT
#include "fstatat.h"
#endif
#ifndef HAVE_OPENAT
#include "openat.h"
#endif
#ifndef HAVE_RENAMEAT
#include "renameat.h"
#endif

#include <pjdlog.h>

#include "auditdistd.h"
#include "proto.h"
#include "sandbox.h"
#include "subr.h"
#include "synch.h"
#include "trail.h"

static struct adist_config *adcfg;
static struct adist_host *adhost;

static TAILQ_HEAD(, adreq) adist_free_list;
static pthread_mutex_t adist_free_list_lock;
static pthread_cond_t adist_free_list_cond;
static TAILQ_HEAD(, adreq) adist_disk_list;
static pthread_mutex_t adist_disk_list_lock;
static pthread_cond_t adist_disk_list_cond;
static TAILQ_HEAD(, adreq) adist_send_list;
static pthread_mutex_t adist_send_list_lock;
static pthread_cond_t adist_send_list_cond;

static void
adreq_clear(struct adreq *adreq)
{

	adreq->adr_error = -1;
	adreq->adr_byteorder = ADIST_BYTEORDER_UNDEFINED;
	adreq->adr_cmd = ADIST_CMD_UNDEFINED;
	adreq->adr_seq = 0;
	adreq->adr_datasize = 0;
}

static void
init_environment(void)
{
	struct adreq *adreq;
	unsigned int ii;

	TAILQ_INIT(&adist_free_list);
	mtx_init(&adist_free_list_lock);
	cv_init(&adist_free_list_cond);
	TAILQ_INIT(&adist_disk_list);
	mtx_init(&adist_disk_list_lock);
	cv_init(&adist_disk_list_cond);
	TAILQ_INIT(&adist_send_list);
	mtx_init(&adist_send_list_lock);
	cv_init(&adist_send_list_cond);

	for (ii = 0; ii < ADIST_QUEUE_SIZE; ii++) {
		adreq = malloc(sizeof(*adreq) + ADIST_BUF_SIZE);
		if (adreq == NULL) {
			pjdlog_exitx(EX_TEMPFAIL,
			    "Unable to allocate %zu bytes of memory for adreq object.",
			    sizeof(*adreq) + ADIST_BUF_SIZE);
		}
		adreq_clear(adreq);
		TAILQ_INSERT_TAIL(&adist_free_list, adreq, adr_next);
	}
}

static void
adreq_decode_and_validate_header(struct adreq *adreq)
{

	/* Byte-swap only is the sender is using different byte order. */
	if (adreq->adr_byteorder != ADIST_BYTEORDER) {
		adreq->adr_byteorder = ADIST_BYTEORDER;
		adreq->adr_seq = bswap64(adreq->adr_seq);
		adreq->adr_datasize = bswap32(adreq->adr_datasize);
	}

	/* Validate packet header. */

	if (adreq->adr_datasize > ADIST_BUF_SIZE) {
		pjdlog_exitx(EX_PROTOCOL, "Invalid datasize received (%ju).",
		    (uintmax_t)adreq->adr_datasize);
	}

	switch (adreq->adr_cmd) {
	case ADIST_CMD_OPEN:
	case ADIST_CMD_APPEND:
	case ADIST_CMD_CLOSE:
		if (adreq->adr_datasize == 0) {
			pjdlog_exitx(EX_PROTOCOL,
			    "Invalid datasize received (%ju).",
			    (uintmax_t)adreq->adr_datasize);
		}
		break;
	case ADIST_CMD_KEEPALIVE:
	case ADIST_CMD_ERROR:
		if (adreq->adr_datasize > 0) {
			pjdlog_exitx(EX_PROTOCOL,
			    "Invalid datasize received (%ju).",
			    (uintmax_t)adreq->adr_datasize);
		}
		break;
	default:
		pjdlog_exitx(EX_PROTOCOL, "Invalid command received (%hhu).",
		    adreq->adr_cmd);
	}
}

static void
adreq_validate_data(const struct adreq *adreq)
{

	/* Validate packet data. */

	switch (adreq->adr_cmd) {
	case ADIST_CMD_OPEN:
	case ADIST_CMD_CLOSE:
		/*
		 * File name must end up with '\0' and there must be no '\0'
		 * in the middle.
		 */
		if (adreq->adr_data[adreq->adr_datasize - 1] != '\0' ||
		    strchr(adreq->adr_data, '\0') !=
		    (const char *)adreq->adr_data + adreq->adr_datasize - 1) {
			pjdlog_exitx(EX_PROTOCOL,
			    "Invalid file name received.");
		}
		break;
	}
}

/*
 * Thread receives requests from the sender.
 */
static void *
recv_thread(void *arg __unused)
{
	struct adreq *adreq;

	for (;;) {
		pjdlog_debug(3, "recv: Taking free request.");
		QUEUE_TAKE(adreq, &adist_free_list, 0);
		pjdlog_debug(3, "recv: (%p) Got request.", adreq);

		if (proto_recv(adhost->adh_remote, &adreq->adr_packet,
		    sizeof(adreq->adr_packet)) == -1) {
			pjdlog_exit(EX_TEMPFAIL,
			    "Unable to receive request header");
		}
		adreq_decode_and_validate_header(adreq);

		switch (adreq->adr_cmd) {
		case ADIST_CMD_KEEPALIVE:
			adreq->adr_error = 0;
			adreq_log(LOG_DEBUG, 2, -1, adreq,
			    "recv: (%p) Got request header: ", adreq);
			pjdlog_debug(3,
			    "recv: (%p) Moving request to the send queue.",
			    adreq);
			QUEUE_INSERT(adreq, &adist_send_list);
			continue;
		case ADIST_CMD_ERROR:
			pjdlog_error("An error occured on the sender while reading \"%s/%s\".",
			    adhost->adh_directory, adhost->adh_trail_name);
			adreq_log(LOG_DEBUG, 2, ADIST_ERROR_READ, adreq,
			    "recv: (%p) Got request header: ", adreq);
			pjdlog_debug(3,
			    "recv: (%p) Moving request to the send queue.",
			    adreq);
			QUEUE_INSERT(adreq, &adist_disk_list);
			continue;
		case ADIST_CMD_OPEN:
		case ADIST_CMD_APPEND:
		case ADIST_CMD_CLOSE:
			if (proto_recv(adhost->adh_remote, adreq->adr_data,
			    adreq->adr_datasize) == -1) {
				pjdlog_exit(EX_TEMPFAIL,
				    "Unable to receive request data");
			}
			adreq_validate_data(adreq);
			adreq_log(LOG_DEBUG, 2, -1, adreq,
			    "recv: (%p) Got request header: ", adreq);
			pjdlog_debug(3,
			    "recv: (%p) Moving request to the disk queue.",
			    adreq);
			QUEUE_INSERT(adreq, &adist_disk_list);
			break;
		default:
			PJDLOG_ABORT("Invalid condition.");
		}
	}
	/* NOTREACHED */
	return (NULL);
}

/*
 * Function that opens trail file requested by the sender.
 * If the file already exist, it has to be the most recent file and it can
 * only be open for append.
 * If the file doesn't already exist, it has to be "older" than all existing
 * files.
 */
static int
receiver_open(const char *filename)
{
	int fd;

	/*
	 * Previous file should be closed by now. Sending OPEN request without
	 * sending CLOSE for the previous file is a sender bug.
	 */
	if (adhost->adh_trail_fd != -1) {
		pjdlog_error("Sender requested opening file \"%s\" without first closing \"%s\".",
		    filename, adhost->adh_trail_name);
		return (ADIST_ERROR_WRONG_ORDER);
	}

	if (!trail_validate_name(filename, NULL)) {
		pjdlog_error("Sender wants to open file \"%s\", which has invalid name.",
		    filename);
		return (ADIST_ERROR_INVALID_NAME);
	}

	switch (trail_name_compare(filename, adhost->adh_trail_name)) {
	case TRAIL_RENAMED:
		if (!trail_is_not_terminated(adhost->adh_trail_name)) {
			pjdlog_error("Terminated trail \"%s/%s\" was unterminated on the sender as \"%s/%s\"?",
			    adhost->adh_directory, adhost->adh_trail_name,
			    adhost->adh_directory, filename);
			return (ADIST_ERROR_INVALID_NAME);
		}
		if (renameat(adhost->adh_trail_dirfd, adhost->adh_trail_name,
		    adhost->adh_trail_dirfd, filename) == -1) {
			pjdlog_errno(LOG_ERR,
			    "Unable to rename file \"%s/%s\" to \"%s/%s\"",
			    adhost->adh_directory, adhost->adh_trail_name,
			    adhost->adh_directory, filename);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_RENAME);
		}
		pjdlog_debug(1, "Renamed file \"%s/%s\" to \"%s/%s\".",
		    adhost->adh_directory, adhost->adh_trail_name,
		    adhost->adh_directory, filename);
		/* FALLTHROUGH */
	case TRAIL_IDENTICAL:
		/* Opening existing file. */
		fd = openat(adhost->adh_trail_dirfd, filename,
		    O_WRONLY | O_APPEND | O_NOFOLLOW);
		if (fd == -1) {
			pjdlog_errno(LOG_ERR,
			    "Unable to open file \"%s/%s\" for append",
			    adhost->adh_directory, filename);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_OPEN);
		}
		pjdlog_debug(1, "Opened file \"%s/%s\".",
		    adhost->adh_directory, filename);
		break;
	case TRAIL_NEWER:
		/* Opening new file. */
		fd = openat(adhost->adh_trail_dirfd, filename,
		    O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
		if (fd == -1) {
			pjdlog_errno(LOG_ERR,
			    "Unable to create file \"%s/%s\"",
			    adhost->adh_directory, filename);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_CREATE);
		}
		pjdlog_debug(1, "Created file \"%s/%s\".",
		    adhost->adh_directory, filename);
		break;
	case TRAIL_OLDER:
		/* Trying to open old file. */
		pjdlog_error("Sender wants to open an old file \"%s\".", filename);
		return (ADIST_ERROR_OPEN_OLD);
	default:
		PJDLOG_ABORT("Unknown return value from trail_name_compare().");
	}
	PJDLOG_VERIFY(strlcpy(adhost->adh_trail_name, filename,
	    sizeof(adhost->adh_trail_name)) < sizeof(adhost->adh_trail_name));
	adhost->adh_trail_fd = fd;
	return (0);
}

/*
 * Function appends data to the trail file that is currently open.
 */
static int
receiver_append(const unsigned char *data, size_t size)
{
	ssize_t done;
	size_t osize;

	/* We should have opened trail file. */
	if (adhost->adh_trail_fd == -1) {
		pjdlog_error("Sender requested append without first opening file.");
		return (ADIST_ERROR_WRONG_ORDER);
	}

	osize = size;
	while (size > 0) {
		done = write(adhost->adh_trail_fd, data, size);
		if (done == -1) {
			if (errno == EINTR)
				continue;
			pjdlog_errno(LOG_ERR, "Write to \"%s/%s\" failed",
			    adhost->adh_directory, adhost->adh_trail_name);
			PJDLOG_ASSERT(errno > 0);
			return (ADIST_ERROR_WRITE);
		}
		pjdlog_debug(3, "Wrote %zd bytes into \"%s/%s\".", done,
		    adhost->adh_directory, adhost->adh_trail_name);
		size -= done;
	}
	pjdlog_debug(2, "Appended %zu bytes to file \"%s/%s\".",
	    osize, adhost->adh_directory, adhost->adh_trail_name);
	return (0);
}

static int
receiver_close(const char *filename)
{

	/* We should have opened trail file. */
	if (adhost->adh_trail_fd == -1) {
		pjdlog_error("Sender requested closing file without first opening it.");
		return (ADIST_ERROR_WRONG_ORDER);
	}

	/* Validate if we can do the rename. */
	if (!trail_validate_name(adhost->adh_trail_name, filename)) {
		pjdlog_error("Sender wants to close file \"%s\" using name \"%s\".",
		    adhost->adh_trail_name, filename);
		return (ADIST_ERROR_INVALID_NAME);
	}

	PJDLOG_VERIFY(close(adhost->adh_trail_fd) == 0);
	adhost->adh_trail_fd = -1;

	pjdlog_debug(1, "Closed file \"%s/%s\".", adhost->adh_directory,
	    adhost->adh_trail_name);

	if (strcmp(adhost->adh_trail_name, filename) == 0) {
		/* File name didn't change, we are done here. */
		return (0);
	}

	if (renameat(adhost->adh_trail_dirfd, adhost->adh_trail_name,
	    adhost->adh_trail_dirfd, filename) == -1) {
		pjdlog_errno(LOG_ERR, "Unable to rename \"%s\" to \"%s\"",
		    adhost->adh_trail_name, filename);
		PJDLOG_ASSERT(errno > 0);
		return (ADIST_ERROR_RENAME);
	}
	pjdlog_debug(1, "Renamed file \"%s/%s\" to \"%s/%s\".",
	    adhost->adh_directory, adhost->adh_trail_name,
	    adhost->adh_directory, filename);
	PJDLOG_VERIFY(strlcpy(adhost->adh_trail_name, filename,
	    sizeof(adhost->adh_trail_name)) < sizeof(adhost->adh_trail_name));

	return (0);
}

static int
receiver_error(void)
{

	/* We should have opened trail file. */
	if (adhost->adh_trail_fd == -1) {
		pjdlog_error("Sender send read error, but file is not open.");
		return (ADIST_ERROR_WRONG_ORDER);
	}

	PJDLOG_VERIFY(close(adhost->adh_trail_fd) == 0);
	adhost->adh_trail_fd = -1;

	pjdlog_debug(1, "Closed file \"%s/%s\".", adhost->adh_directory,
	    adhost->adh_trail_name);

	return (0);
}

static void *
disk_thread(void *arg __unused)
{
	struct adreq *adreq;

	for (;;) {
		pjdlog_debug(3, "disk: Taking request.");
		QUEUE_TAKE(adreq, &adist_disk_list, 0);
		adreq_log(LOG_DEBUG, 3, -1, adreq, "disk: (%p) Got request: ",
		    adreq);
		/* Handle the actual request. */
		switch (adreq->adr_cmd) {
		case ADIST_CMD_OPEN:
			adreq->adr_error = receiver_open(adreq->adr_data);
			break;
		case ADIST_CMD_APPEND:
			adreq->adr_error = receiver_append(adreq->adr_data,
			    adreq->adr_datasize);
			break;
		case ADIST_CMD_CLOSE:
			adreq->adr_error = receiver_close(adreq->adr_data);
			break;
		case ADIST_CMD_ERROR:
			adreq->adr_error = receiver_error();
			break;
		default:
			PJDLOG_ABORT("Unexpected command (cmd=%hhu).",
			    adreq->adr_cmd);
		}
		if (adreq->adr_error != 0) {
			adreq_log(LOG_ERR, 0, adreq->adr_error, adreq,
			    "Request failed: ");
		}
		pjdlog_debug(3, "disk: (%p) Moving request to the send queue.",
		    adreq);
		QUEUE_INSERT(adreq, &adist_send_list);
	}
	/* NOTREACHED */
	return (NULL);
}

/*
 * Thread sends requests back to primary node.
 */
static void *
send_thread(void *arg __unused)
{
	struct adreq *adreq;
	struct adrep adrep;

	for (;;) {
		pjdlog_debug(3, "send: Taking request.");
		QUEUE_TAKE(adreq, &adist_send_list, 0);
		adreq_log(LOG_DEBUG, 3, -1, adreq, "send: (%p) Got request: ",
		    adreq);
		adrep.adrp_byteorder = ADIST_BYTEORDER;
		adrep.adrp_seq = adreq->adr_seq;
		adrep.adrp_error = adreq->adr_error;
		if (proto_send(adhost->adh_remote, &adrep,
		    sizeof(adrep)) == -1) {
			pjdlog_exit(EX_TEMPFAIL, "Unable to send reply");
		}
		pjdlog_debug(3, "send: (%p) Moving request to the free queue.",
		    adreq);
		adreq_clear(adreq);
		QUEUE_INSERT(adreq, &adist_free_list);
	}
	/* NOTREACHED */
	return (NULL);
}

static void
receiver_directory_create(void)
{
	struct passwd *pw;

	/*
	 * According to getpwnam(3) we have to clear errno before calling the
	 * function to be able to distinguish between an error and missing
	 * entry (with is not treated as error by getpwnam(3)).
	 */
	errno = 0;
	pw = getpwnam(ADIST_USER);
	if (pw == NULL) {
		if (errno != 0) {
			pjdlog_exit(EX_NOUSER,
			    "Unable to find info about '%s' user", ADIST_USER);
		} else {
			pjdlog_exitx(EX_NOUSER, "User '%s' doesn't exist.",
			    ADIST_USER);
		}
	}

	if (mkdir(adhost->adh_directory, 0700) == -1) {
		pjdlog_exit(EX_OSFILE, "Unable to create directory \"%s\"",
		    adhost->adh_directory);
	}
	if (chown(adhost->adh_directory, pw->pw_uid, pw->pw_gid) == -1) {
		pjdlog_errno(LOG_ERR,
		    "Unable to change owner of the directory \"%s\"",
		    adhost->adh_directory);
		(void)rmdir(adhost->adh_directory);
		exit(EX_OSFILE);
	}
}

static void
receiver_directory_open(void)
{

#ifdef HAVE_FDOPENDIR
	adhost->adh_trail_dirfd = open(adhost->adh_directory,
	    O_RDONLY | O_DIRECTORY);
	if (adhost->adh_trail_dirfd == -1) {
		if (errno == ENOENT) {
			receiver_directory_create();
			adhost->adh_trail_dirfd = open(adhost->adh_directory,
			    O_RDONLY | O_DIRECTORY);
		}
		if (adhost->adh_trail_dirfd == -1) {
			pjdlog_exit(EX_CONFIG,
			    "Unable to open directory \"%s\"",
			    adhost->adh_directory);
		}
	}
	adhost->adh_trail_dirfp = fdopendir(adhost->adh_trail_dirfd);
	if (adhost->adh_trail_dirfp == NULL) {
		pjdlog_exit(EX_CONFIG, "Unable to fdopen directory \"%s\"",
		    adhost->adh_directory);
	}
#else
	struct stat sb;

	if (stat(adhost->adh_directory, &sb) == -1) {
		if (errno == ENOENT) {
			receiver_directory_create();
		} else {
			pjdlog_exit(EX_CONFIG,
			    "Unable to stat directory \"%s\"",
			    adhost->adh_directory);
		}
	}
	adhost->adh_trail_dirfp = opendir(adhost->adh_directory);
	if (adhost->adh_trail_dirfp == NULL) {
		pjdlog_exit(EX_CONFIG, "Unable to open directory \"%s\"",
		    adhost->adh_directory);
	}
	adhost->adh_trail_dirfd = dirfd(adhost->adh_trail_dirfp);
#endif
}

static void
receiver_connect(void)
{
	uint64_t trail_size;
	struct stat sb;

	PJDLOG_ASSERT(adhost->adh_trail_dirfp != NULL);

	trail_last(adhost->adh_trail_dirfp, adhost->adh_trail_name,
	    sizeof(adhost->adh_trail_name));

	if (adhost->adh_trail_name[0] == '\0') {
		trail_size = 0;
	} else {
		if (fstatat(adhost->adh_trail_dirfd, adhost->adh_trail_name,
		    &sb, AT_SYMLINK_NOFOLLOW) == -1) {
			pjdlog_exit(EX_CONFIG, "Unable to stat \"%s/%s\"",
			    adhost->adh_directory, adhost->adh_trail_name);
		}
		if (!S_ISREG(sb.st_mode)) {
			pjdlog_exitx(EX_CONFIG,
			    "File \"%s/%s\" is not a regular file.",
			    adhost->adh_directory, adhost->adh_trail_name);
		}
		trail_size = sb.st_size;
	}
	trail_size = htole64(trail_size);
	if (proto_send(adhost->adh_remote, &trail_size,
	    sizeof(trail_size)) == -1) {
		pjdlog_exit(EX_TEMPFAIL,
		    "Unable to send size of the most recent trail file");
	}
	if (proto_send(adhost->adh_remote, adhost->adh_trail_name,
	    sizeof(adhost->adh_trail_name)) == -1) {
		pjdlog_exit(EX_TEMPFAIL,
		    "Unable to send name of the most recent trail file");
	}
}

void
adist_receiver(struct adist_config *config, struct adist_host *adh)
{
	sigset_t mask;
	pthread_t td;
	pid_t pid;
	int error, mode, debuglevel;

	pid = fork();
	if (pid == -1) {
		pjdlog_errno(LOG_ERR, "Unable to fork");
		proto_close(adh->adh_remote);
		adh->adh_remote = NULL;
		return;
	}

	if (pid > 0) {
		/* This is parent. */
		proto_close(adh->adh_remote);
		adh->adh_remote = NULL;
		adh->adh_worker_pid = pid;
		return;
	}

	adcfg = config;
	adhost = adh;
	mode = pjdlog_mode_get();
	debuglevel = pjdlog_debug_get();

	descriptors_cleanup(adhost);

//	descriptors_assert(adhost, mode);

	pjdlog_init(mode);
	pjdlog_debug_set(debuglevel);
	pjdlog_prefix_set("[%s] (%s) ", adhost->adh_name,
	    role2str(adhost->adh_role));
#ifdef HAVE_SETPROCTITLE
	setproctitle("%s (%s)", adhost->adh_name, role2str(adhost->adh_role));
#endif

	PJDLOG_VERIFY(sigemptyset(&mask) == 0);
	PJDLOG_VERIFY(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);

	/* Error in setting timeout is not critical, but why should it fail? */
	if (proto_timeout(adhost->adh_remote, adcfg->adc_timeout) == -1)
		pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");

	init_environment();

	adhost->adh_trail_fd = -1;
	receiver_directory_open();

	if (sandbox(ADIST_USER, true, "auditdistd: %s (%s)",
	    role2str(adhost->adh_role), adhost->adh_name) != 0) {
		exit(EX_CONFIG);
	}
	pjdlog_info("Privileges successfully dropped.");

	receiver_connect();

	error = pthread_create(&td, NULL, recv_thread, adhost);
	PJDLOG_ASSERT(error == 0);
	error = pthread_create(&td, NULL, disk_thread, adhost);
	PJDLOG_ASSERT(error == 0);
	(void)send_thread(adhost);
}