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
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2012 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 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; 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 "file-opener.h"
#include "messages.h"
#include "gprocess.h"
#include "fdhelpers.h"
#include "pathutils.h"
#include "cfg.h"
#include "transport/transport-file.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
static inline gboolean
file_opener_prepare_open(FileOpener *self, const gchar *name)
{
if (self->prepare_open)
return self->prepare_open(self, name);
return TRUE;
}
static inline gint
file_opener_open(FileOpener *self, const gchar *name, gint flags)
{
return self->open(self, name, flags);
}
static gint
file_opener_get_open_flags_method(FileOpener *self, FileDirection dir)
{
switch (dir)
{
case AFFILE_DIR_READ:
return O_RDONLY | O_NOCTTY | O_NONBLOCK | O_LARGEFILE;
case AFFILE_DIR_WRITE:
return O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK | O_LARGEFILE | O_APPEND;
default:
g_assert_not_reached();
}
}
static inline gint
file_opener_get_open_flags(FileOpener *self, FileDirection dir)
{
return self->get_open_flags(self, dir);
}
static const gchar *spurious_paths[] = {"../", "/..", NULL};
static inline gboolean
_string_contains_fragment(const gchar *str, const gchar *fragments[])
{
int i;
for (i = 0; fragments[i]; i++)
{
if (strstr(str, fragments[i]))
return TRUE;
}
return FALSE;
}
static inline gboolean
_is_path_spurious(const gchar *name)
{
return _string_contains_fragment(name, spurious_paths);
}
static inline gboolean
_obtain_capabilities(FileOpener *self, const gchar *name, cap_t *act_caps)
{
if (self->options->needs_privileges)
{
g_process_enable_cap("cap_dac_read_search");
g_process_enable_cap("cap_syslog");
}
else
{
g_process_enable_cap("cap_dac_override");
}
if (self->options->create_dirs &&
!file_perm_options_create_containing_directory(&self->options->file_perm_options, name))
{
return FALSE;
}
return TRUE;
}
static inline void
_set_fd_permission(FileOpener *self, int fd)
{
if (fd != -1)
{
g_fd_set_cloexec(fd, TRUE);
g_process_enable_cap("cap_chown");
g_process_enable_cap("cap_fowner");
file_perm_options_apply_fd(&self->options->file_perm_options, fd);
}
}
static int
_open(FileOpener *self, const gchar *name, gint open_flags)
{
FilePermOptions *perm_opts = &self->options->file_perm_options;
int fd;
int mode = (perm_opts && (perm_opts->file_perm >= 0))
? perm_opts->file_perm : 0600;
fd = open(name, open_flags, mode);
return fd;
}
FileOpenerResult
file_opener_open_fd(FileOpener *self, const gchar *name, FileDirection dir, gint *fd)
{
cap_t saved_caps;
if (_is_path_spurious(name))
{
msg_error("Spurious path, logfile not created",
evt_tag_str("path", name));
return FILE_OPENER_RESULT_ERROR_PERMANENT;
}
saved_caps = g_process_cap_save();
if (!_obtain_capabilities(self, name, &saved_caps))
{
g_process_cap_restore(saved_caps);
return FILE_OPENER_RESULT_ERROR_TRANSIENT;
}
if (!file_opener_prepare_open(self, name))
return FILE_OPENER_RESULT_ERROR_TRANSIENT;
*fd = file_opener_open(self, name, file_opener_get_open_flags(self, dir));
if (!is_file_device(name))
_set_fd_permission(self, *fd);
g_process_cap_restore(saved_caps);
msg_trace("affile_open_file",
evt_tag_str("path", name),
evt_tag_int("fd", *fd));
if (*fd == -1)
return FILE_OPENER_RESULT_ERROR_TRANSIENT;
return FILE_OPENER_RESULT_SUCCESS;
}
static gboolean
_is_symlink_creation_needed(const gchar *name, const gchar *target)
{
gboolean r = FALSE;
GError *e = NULL;
gchar *s = g_file_read_link(name, &e);
if (e != NULL)
{
if (g_error_matches(e, G_FILE_ERROR, G_FILE_ERROR_NOENT))
r = TRUE;
else msg_error("Error checking symlink",
evt_tag_str("filename", name),
evt_tag_str("message", e->message));
g_error_free(e);
}
else if (strcmp(s, target) != 0)
{
if (unlink(name) == 0)
r = TRUE;
else
msg_error("Error removing symlink",
evt_tag_str("filename", name),
evt_tag_errno(EVT_TAG_OSERROR, errno));
}
g_free(s);
return r;
}
void
file_opener_symlink(FileOpener *self, const gchar *name, const gchar *target)
{
cap_t saved_caps;
msg_trace("file_opener_symlink",
evt_tag_str("filename", name),
evt_tag_str("target", target));
if (!_is_symlink_creation_needed(name, target)) return;
saved_caps = g_process_cap_save();
if (!_obtain_capabilities(self, name, &saved_caps))
{
g_process_cap_restore(saved_caps);
return;
}
g_process_enable_cap("cap_chown");
msg_info("Creating symlink",
evt_tag_str("filename", name),
evt_tag_str("target", target));
if (symlink(target, name) == -1)
msg_error("Error creating symlink",
evt_tag_str("filename", name),
evt_tag_str("target", target),
evt_tag_errno(EVT_TAG_OSERROR, errno));
else if (!file_perm_options_apply_symlink(&self->options->file_perm_options, name))
msg_error("Error setting symlink ownership",
evt_tag_str("filename", name),
evt_tag_errno(EVT_TAG_OSERROR, errno));
g_process_cap_restore(saved_caps);
}
void
file_opener_set_options(FileOpener *self, FileOpenerOptions *options)
{
self->options = options;
}
void
file_opener_init_instance(FileOpener *self)
{
self->get_open_flags = file_opener_get_open_flags_method;
self->open = _open;
}
FileOpener *
file_opener_new(void)
{
FileOpener *self = g_new0(FileOpener, 1);
file_opener_init_instance(self);
return self;
}
void
file_opener_free(FileOpener *self)
{
g_free(self);
}
void
file_opener_options_defaults(FileOpenerOptions *options)
{
file_perm_options_defaults(&options->file_perm_options);
options->create_dirs = -1;
options->needs_privileges = FALSE;
}
void
file_opener_options_defaults_dont_change_permissions(FileOpenerOptions *options)
{
file_opener_options_defaults(options);
file_perm_options_inherit_dont_change(&options->file_perm_options);
}
void
file_opener_options_init(FileOpenerOptions *options, GlobalConfig *cfg)
{
file_perm_options_inherit_from(&options->file_perm_options, &cfg->file_perm_options);
if (options->create_dirs == -1)
options->create_dirs = cfg->create_dirs;
}
void
file_opener_options_deinit(FileOpenerOptions *options)
{
/* empty, this function only serves to meet the conventions of *Options */
}
|