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
|
/*
* audispd-builtins.c - some common builtin plugins
* Copyright (c) 2007,2010,2013 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This software may be freely redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*/
#include "config.h"
#include <string.h>
#include <dirent.h>
#include <libgen.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "audispd-pconfig.h"
#include "audispd-builtins.h"
// Local data
static volatile int sock = -1, conn = -1;
static int syslog_started = 0, priority;
static char *path = NULL;
// Local prototypes
static void init_af_unix(const plugin_conf_t *conf);
static void init_syslog(const plugin_conf_t *conf);
void start_builtin(plugin_conf_t *conf)
{
if (strcasecmp("builtin_af_unix", conf->path) == 0) {
conf->type = S_AF_UNIX;
init_af_unix(conf);
} else if (strcasecmp("builtin_syslog", conf->path) == 0) {
conf->type = S_SYSLOG;
init_syslog(conf);
} else
syslog(LOG_ERR, "Unknown builtin %s", conf->path);
}
void stop_builtin(plugin_conf_t *conf)
{
if (conf->type == S_AF_UNIX)
destroy_af_unix();
else if (conf->type == S_SYSLOG)
destroy_syslog();
else
syslog(LOG_ERR, "Unknown builtin %s", conf->path);
}
static void af_unix_accept(int fd)
{
int cmd;
do {
conn = accept(fd, NULL, NULL);
} while (conn < 0 && errno == EINTR);
// De-register since this is intended to be one listener
if (conn >= 0)
remove_event(fd);
cmd = fcntl(conn, F_GETFD);
fcntl(conn, F_SETFD, cmd|FD_CLOEXEC);
}
static int create_af_unix_socket(const char *path, int mode)
{
struct sockaddr_un addr;
socklen_t len;
int rc, cmd;
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
syslog(LOG_ERR, "Couldn't open af_unix socket (%s)",
strerror(errno));
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(&addr.sun_path[0], path);
len = sizeof(addr);
rc = bind(sock, (const struct sockaddr *)&addr, len);
if (rc < 0) {
syslog(LOG_ERR, "Couldn't bind af_unix socket (%s)",
strerror(errno));
destroy_af_unix();
return -1;
}
if (mode != -1) {
rc = chmod(path, mode);
if (rc < 0) {
syslog(LOG_ERR, "Couldn't chmod %s to %04o (%s)",
path, mode, strerror(errno));
destroy_af_unix();
return -1;
}
}
// Put socket in nonblock mode
cmd = fcntl(sock, F_GETFL);
fcntl(sock, F_SETFL, cmd|FNDELAY);
// don't leak the descriptor
cmd = fcntl(sock, F_GETFD);
fcntl(sock, F_SETFD, cmd|FD_CLOEXEC);
// Make socket listening...won't block
(void)listen(sock, 5);
// Register socket with poll
add_event(sock, af_unix_accept);
return 0;
}
static void init_af_unix(const plugin_conf_t *conf)
{
int i = 1, mode = -1;
char *base = NULL;
// while args
while (conf->args[i]) {
int rc, bad = 0;
// is all nums - do mode
base = conf->args[i];
while (*base) {
if (!isdigit(*base)) {
bad = 1;
break;
}
base++;
}
if (!bad) {
errno = 0;
mode = strtoul(conf->args[i], NULL, 8);
if (errno) {
syslog(LOG_ERR, "Error converting %s (%s)",
conf->args[i], strerror(errno));
mode = -1;
bad = 1;
} else if (path) {
rc = chmod(path, mode);
if (rc < 0) {
syslog(LOG_ERR,
"Couldn't chmod %s to %04o (%s)",
conf->args[i], mode,
strerror(errno));
destroy_af_unix();
return;
}
}
} else {
// else check for '/'
base = strchr(conf->args[i], '/');
if (base) {
// get dirname
DIR *d;
char *dir = strdup(conf->args[i]);
base = dirname(dir);
d = opendir(base);
if (d) {
closedir(d);
unlink(conf->args[i]);
if (create_af_unix_socket(
conf->args[i], mode)<0) {
free(dir);
return;
}
path = strdup(conf->args[i]);
bad = 0;
} else
syslog(LOG_ERR, "Couldn't open %s (%s)",
base, strerror(errno));
free(dir);
} else
syslog(LOG_ERR, "Malformed path %s",
conf->args[i]);
}
if (bad) {
destroy_af_unix();
return;
}
i++;
}
syslog(LOG_INFO, "af_unix plugin initialized");
}
void send_af_unix_string(const char *s, unsigned int len)
{
if (sock < 0)
return;
if (conn >= 0) {
int rc;
do {
rc = write(conn, s, len);
} while (rc < 0 && errno == EINTR);
if (rc < 0 && errno == EPIPE) {
close(conn);
conn = -1;
add_event(sock, af_unix_accept);
}
}
}
void send_af_unix_binary(event_t *e)
{
if (sock < 0)
return;
if (conn >= 0) {
int rc;
struct iovec vec[2];
vec[0].iov_base = &e->hdr;
vec[0].iov_len = sizeof(struct audit_dispatcher_header);
vec[1].iov_base = e->data;
vec[1].iov_len = MAX_AUDIT_MESSAGE_LENGTH;
do {
rc = writev(conn, vec, 2);
} while (rc < 0 && errno == EINTR);
if (rc < 0 && errno == EPIPE) {
close(conn);
conn = -1;
add_event(sock, af_unix_accept);
}
}
}
void destroy_af_unix(void)
{
if (conn >= 0) {
close(conn);
conn = -1;
}
if (sock >= 0) {
close(sock);
sock = -1;
}
if (path) {
unlink(path);
free(path);
path = NULL;
}
}
static void init_syslog(const plugin_conf_t *conf)
{
int i, facility = LOG_USER;
priority = LOG_INFO;
for (i = 1; i<3; i++) {
if (conf->args[i]) {
if (strcasecmp(conf->args[i], "LOG_DEBUG") == 0)
priority = LOG_DEBUG;
else if (strcasecmp(conf->args[i], "LOG_INFO") == 0)
priority = LOG_INFO;
else if (strcasecmp(conf->args[i], "LOG_NOTICE") == 0)
priority = LOG_NOTICE;
else if (strcasecmp(conf->args[i], "LOG_WARNING") == 0)
priority = LOG_WARNING;
else if (strcasecmp(conf->args[i], "LOG_ERR") == 0)
priority = LOG_ERR;
else if (strcasecmp(conf->args[i], "LOG_CRIT") == 0)
priority = LOG_CRIT;
else if (strcasecmp(conf->args[i], "LOG_ALERT") == 0)
priority = LOG_ALERT;
else if (strcasecmp(conf->args[i], "LOG_EMERG") == 0)
priority = LOG_EMERG;
else if (strcasecmp(conf->args[i], "LOG_LOCAL0") == 0)
facility = LOG_LOCAL0;
else if (strcasecmp(conf->args[i], "LOG_LOCAL1") == 0)
facility = LOG_LOCAL1;
else if (strcasecmp(conf->args[i], "LOG_LOCAL2") == 0)
facility = LOG_LOCAL2;
else if (strcasecmp(conf->args[i], "LOG_LOCAL3") == 0)
facility = LOG_LOCAL3;
else if (strcasecmp(conf->args[i], "LOG_LOCAL4") == 0)
facility = LOG_LOCAL4;
else if (strcasecmp(conf->args[i], "LOG_LOCAL5") == 0)
facility = LOG_LOCAL5;
else if (strcasecmp(conf->args[i], "LOG_LOCAL6") == 0)
facility = LOG_LOCAL6;
else if (strcasecmp(conf->args[i], "LOG_LOCAL7") == 0)
facility = LOG_LOCAL7;
else {
syslog(LOG_ERR,
"Unknown log priority/facility %s",
conf->args[i]);
syslog_started = 0;
return;
}
}
}
syslog(LOG_INFO, "syslog plugin initialized");
if (facility != LOG_USER)
openlog("audispd", 0, facility);
syslog_started = 1;
}
void send_syslog(const char *s)
{
if (syslog_started)
syslog(priority, "%s", s);
}
void destroy_syslog(void)
{
syslog_started = 0;
}
|