2 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/subr.c#3 $
29 #include <config/config.h>
32 #include <sys/types.h>
33 #include <sys/event.h>
43 #ifndef HAVE_ARC4RANDOM
44 #include <openssl/rand.h>
48 #include <compat/strlcat.h>
51 #include "auditdistd.h"
56 vsnprlcat(char *str, size_t size, const char *fmt, va_list ap)
61 return (vsnprintf(str + len, size - len, fmt, ap));
65 snprlcat(char *str, size_t size, const char *fmt, ...)
71 result = vsnprlcat(str, size, fmt, ap);
81 case ADIST_ROLE_SENDER:
83 case ADIST_ROLE_RECEIVER:
90 adist_errstr(int error)
94 case ADIST_ERROR_WRONG_ORDER:
95 return ("wrong operations order");
96 case ADIST_ERROR_INVALID_NAME:
97 return ("invalid trail file name");
98 case ADIST_ERROR_OPEN_OLD:
99 return ("attempt to open an old trail file");
100 case ADIST_ERROR_CREATE:
101 return ("creation of new trail file failed");
102 case ADIST_ERROR_OPEN:
103 return ("open of existing trail file failed");
104 case ADIST_ERROR_READ:
105 return ("read failed");
106 case ADIST_ERROR_WRITE:
107 return ("write failed");
108 case ADIST_ERROR_RENAME:
109 return ("rename of a trail file failed");
111 return ("unknown error");
116 adreq_log(int loglevel, int debuglevel, int error, struct adreq *adreq,
117 const char *fmt, ...)
123 (void)vsnprintf(msg, sizeof(msg), fmt, ap);
125 (void)snprlcat(msg, sizeof(msg), "(seq=%ju) ",
126 (uintmax_t)adreq->adr_seq);
127 switch (adreq->adr_cmd) {
129 (void)snprlcat(msg, sizeof(msg), "OPEN(%s)",
132 case ADIST_CMD_APPEND:
133 (void)snprlcat(msg, sizeof(msg), "APPEND(%ju)",
134 (uintmax_t)adreq->adr_datasize);
136 case ADIST_CMD_CLOSE:
137 (void)snprlcat(msg, sizeof(msg), "CLOSE(%s)",
140 case ADIST_CMD_KEEPALIVE:
141 (void)snprlcat(msg, sizeof(msg), "KEEPALIVE");
143 case ADIST_CMD_ERROR:
144 (void)snprlcat(msg, sizeof(msg), "ERROR");
147 (void)snprlcat(msg, sizeof(msg), "UNKNOWN(%hhu)",
152 (void)snprlcat(msg, sizeof(msg), ": %s", adist_errstr(error));
153 (void)strlcat(msg, ".", sizeof(msg));
154 pjdlog_common(loglevel, debuglevel, -1, "%s", msg);
158 adist_random(unsigned char *buf, size_t size)
160 #ifdef HAVE_ARC4RANDOM_BUF
161 arc4random_buf(buf, size);
163 #elif defined(HAVE_ARC4RANDOM)
166 PJDLOG_ASSERT(size > 0);
167 PJDLOG_ASSERT((size % sizeof(val)) == 0);
171 bcopy(&val, buf, sizeof(val));
178 if (RAND_bytes(buf, (int)size) == 0)
184 static int wait_for_dir_kq = -1;
185 static int wait_for_file_kq = -1;
188 wait_for_dir_init(int fd)
194 PJDLOG_ASSERT(wait_for_dir_kq == -1);
197 PJDLOG_ASSERT(fd != -1);
202 pjdlog_errno(LOG_WARNING, "kqueue() failed");
205 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
207 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
209 pjdlog_errno(LOG_WARNING, "kevent() failed");
214 wait_for_dir_kq = kq;
221 wait_for_file_init(int fd)
228 PJDLOG_ASSERT(fd != -1);
233 pjdlog_errno(LOG_WARNING, "kqueue() failed");
236 EV_SET(&ev[0], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
238 EV_SET(&ev[1], fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
240 if (kevent(kq, ev, 2, NULL, 0, NULL) == -1) {
242 pjdlog_errno(LOG_WARNING, "kevent() failed");
247 wait_for_file_kq = kq;
254 * Wait for new file to appear in directory.
263 if (wait_for_dir_kq == -1) {
269 PJDLOG_ASSERT(wait_for_dir_kq != -1);
271 if (kevent(wait_for_dir_kq, NULL, 0, &ev, 1, NULL) == -1) {
272 pjdlog_errno(LOG_WARNING, "kevent() failed");
279 * Wait for file growth or rename.
288 if (wait_for_file_kq == -1) {
294 PJDLOG_ASSERT(wait_for_file_kq != -1);
296 if (kevent(wait_for_file_kq, NULL, 0, ev, 2, NULL) == -1) {
297 pjdlog_errno(LOG_WARNING, "kevent() failed");