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
|
/*
* Copyright (c) 2002-2010 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 1998-2010 Balázs Scheidler
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "afunix.h"
#include "misc.h"
#include "messages.h"
#include "gprocess.h"
#if WITH_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#else
#include "sd-daemon.h"
#endif
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
void
afunix_sd_set_uid(LogDriver *s, gchar *owner)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
if (!resolve_user(owner, &self->owner))
msg_error("Error resolving username",
evt_tag_str("owner", owner),
NULL);
}
void
afunix_sd_set_gid(LogDriver *s, gchar *group)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
if (!resolve_group(group, &self->group))
msg_error("Error resolving group",
evt_tag_str("group", group),
NULL);
}
void
afunix_sd_set_perm(LogDriver *s, gint perm)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
self->perm = perm & 0777;
}
#if ENABLE_SYSTEMD
static gboolean
afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
gint fd, fds;
*result_fd = -1;
fd = -1;
fds = sd_listen_fds(0);
if (fds == 0)
return TRUE;
msg_debug("Systemd socket activation",
evt_tag_int("systemd-sockets", fds),
evt_tag_str("systemd-listen-pid", getenv("LISTEN_PID")),
evt_tag_str("systemd-listen-fds", getenv("LISTEN_FDS")),
NULL);
if (fds < 0)
{
msg_error("Failed to acquire systemd sockets, incorrectly set LISTEN_FDS environment variable?",
NULL);
return FALSE;
}
else if (fds > 0)
{
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fds; fd++)
{
/* check if any type is available */
if (sd_is_socket_unix(fd, 0, -1, self->filename, 0))
{
int type = (self->super.flags & AFSOCKET_STREAM) ? SOCK_STREAM : SOCK_DGRAM;
/* check if it matches our idea of the socket type */
if (sd_is_socket_unix(fd, type, -1, self->filename, 0))
{
*result_fd = fd;
break;
}
else
{
msg_error("The systemd supplied UNIX domain socket is of a different type, check the configured driver and the matching systemd unit file",
evt_tag_str("filename", self->filename),
evt_tag_int("systemd-sock-fd", fd),
evt_tag_str("expecting", type == SOCK_STREAM ? "unix-stream()" : "unix-dgram()"),
NULL);
return FALSE;
}
}
else
{
/* systemd passed an fd we didn't really care about. This is
* not an error, but might be worth mentioning it at the debug
* level.
*/
msg_debug("Ignoring systemd supplied fd as it is not a UNIX domain socket",
evt_tag_str("filename", self->filename),
evt_tag_int("systemd-sock-fd", fd),
NULL);
}
}
}
else
return TRUE;
if (*result_fd != -1)
{
g_fd_set_nonblock(*result_fd, TRUE);
g_fd_set_cloexec(*result_fd, TRUE);
msg_verbose("Acquired systemd socket",
evt_tag_str("filename", self->filename),
evt_tag_int("systemd-sock-fd", *result_fd),
NULL);
}
else
{
msg_debug("Failed to acquire systemd socket, trying to open ourselves",
evt_tag_str("filename", self->filename),
NULL);
}
return TRUE;
}
#else
static gboolean
afunix_sd_acquire_socket(AFSocketSourceDriver *s, gint *result_fd)
{
return TRUE;
}
#endif
static gboolean
afunix_sd_apply_transport(AFSocketSourceDriver *s)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
if (!self->super.bind_addr)
self->super.bind_addr = g_sockaddr_unix_new(self->filename);
return TRUE;
}
static gboolean
afunix_sd_init(LogPipe *s)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
if (afsocket_sd_init(s))
{
cap_t saved_caps;
saved_caps = g_process_cap_save();
g_process_cap_modify(CAP_CHOWN, TRUE);
g_process_cap_modify(CAP_FOWNER, TRUE);
g_process_cap_modify(CAP_DAC_OVERRIDE, TRUE);
set_permissions(self->filename, self->owner, self->group, self->perm);
g_process_cap_restore(saved_caps);
return TRUE;
}
return FALSE;
}
static void
afunix_sd_free(LogPipe *s)
{
AFUnixSourceDriver *self = (AFUnixSourceDriver *) s;
g_free(self->filename);
afsocket_sd_free(s);
}
LogDriver *
afunix_sd_new(gchar *filename, guint32 flags)
{
AFUnixSourceDriver *self = g_new0(AFUnixSourceDriver, 1);
afsocket_sd_init_instance(&self->super, &self->sock_options, AF_UNIX, flags);
self->super.super.super.super.init = afunix_sd_init;
self->super.super.super.super.free_fn = afunix_sd_free;
self->super.acquire_socket = afunix_sd_acquire_socket;
self->super.apply_transport = afunix_sd_apply_transport;
self->super.max_connections = 256;
if (self->super.flags & AFSOCKET_STREAM)
self->super.reader_options.super.init_window_size = self->super.max_connections * 100;
if (self->super.flags & AFSOCKET_DGRAM)
afsocket_sd_set_transport(&self->super.super.super, "unix-dgram");
else if (self->super.flags & AFSOCKET_STREAM)
afsocket_sd_set_transport(&self->super.super.super, "unix-stream");
self->filename = g_strdup(filename);
self->owner = -1;
self->group = -1;
self->perm = 0666;
return &self->super.super.super;
}
static gboolean
afunix_dd_apply_transport(AFSocketDestDriver *s)
{
AFUnixDestDriver *self = (AFUnixDestDriver *) s;
if (!self->super.bind_addr)
self->super.bind_addr = g_sockaddr_unix_new(NULL);
if (!self->super.dest_addr)
self->super.dest_addr = g_sockaddr_unix_new(self->filename);
if (!self->super.dest_name)
self->super.dest_name = g_strdup_printf("localhost.afunix:%s", self->filename);
return TRUE;
}
static void
afunix_dd_free(LogPipe *s)
{
AFUnixDestDriver *self = (AFUnixDestDriver *) s;
g_free(self->filename);
afsocket_dd_free(s);
}
LogDriver *
afunix_dd_new(gchar *filename, guint flags)
{
AFUnixDestDriver *self = g_new0(AFUnixDestDriver, 1);
afsocket_dd_init_instance(&self->super, &self->sock_options, AF_UNIX, "localhost", flags);
self->super.super.super.super.free_fn = afunix_dd_free;
self->super.apply_transport = afunix_dd_apply_transport;
self->filename = g_strdup(filename);
if (self->super.flags & AFSOCKET_DGRAM)
afsocket_dd_set_transport(&self->super.super.super, "unix-dgram");
else if (self->super.flags & AFSOCKET_STREAM)
afsocket_dd_set_transport(&self->super.super.super, "unix-stream");
return &self->super.super.super;
}
|