/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "alloc-util.h"
#include "audit-type.h"
#include "fd-util.h"
#include "hexdecoct.h"
#include "io-util.h"
#include "journald-audit.h"
#include "missing.h"
#include "string-util.h"
typedef struct MapField {
const char *audit_field;
const char *journal_field;
int (*map)(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, unsigned *n_iov);
} MapField;
static int map_simple_field(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, unsigned *n_iov) {
_cleanup_free_ char *c = NULL;
const char *e;
assert(p);
allocated = l + 1;
if (!c)
return -ENOMEM;
for (e = *p; *e != ' ' && *e != 0; e++) {
return -ENOMEM;
c[l++] = *e;
}
c[l] = 0;
return -ENOMEM;
(*n_iov) ++;
*p = e;
c = NULL;
return 1;
}
static int map_string_field_internal(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, unsigned *n_iov, bool filter_printable) {
_cleanup_free_ char *c = NULL;
const char *s, *e;
size_t l;
assert(p);
/* The kernel formats string fields in one of two formats. */
if (**p == '"') {
/* Normal quoted syntax */
s = *p + 1;
e = strchr(s, '"');
if (!e)
return 0;
c = malloc(l+1);
if (!c)
return -ENOMEM;
e += 1;
} else if (unhexchar(**p) >= 0) {
/* Hexadecimal escaping */
allocated = l + 2;
if (!c)
return -ENOMEM;
for (e = *p; *e != ' ' && *e != 0; e += 2) {
int a, b;
uint8_t x;
a = unhexchar(e[0]);
if (a < 0)
return 0;
b = unhexchar(e[1]);
if (b < 0)
return 0;
x = (uint8_t) ' ';
return -ENOMEM;
c[l++] = (char) x;
}
c[l] = 0;
} else
return 0;
return -ENOMEM;
(*n_iov) ++;
*p = e;
c = NULL;
return 1;
}
static int map_string_field(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, unsigned *n_iov) {
}
static int map_string_field_printable(const char *field, const char **p, struct iovec **iov, size_t *n_iov_allocated, unsigned *n_iov) {
}
static int map_generic_field(const char *prefix, const char **p, struct iovec **iov, size_t *n_iov_allocated, unsigned *n_iov) {
const char *e, *f;
char *c, *t;
int r;
/* Implements fallback mappings for all fields we don't know */
for (e = *p; e < *p + 16; e++) {
if (*e == 0 || *e == ' ')
return 0;
if (*e == '=')
break;
if (!((*e >= 'a' && *e <= 'z') ||
(*e >= 'A' && *e <= 'Z') ||
(*e >= '0' && *e <= '9') ||
*e == '_' || *e == '-'))
return 0;
}
if (e <= *p || e >= *p + 16)
return 0;
for (f = *p; f < e; f++) {
char x;
if (*f >= 'a' && *f <= 'z')
x = (*f - 'a') + 'A'; /* uppercase */
else if (*f == '-')
x = '_'; /* dashes → underscores */
else
x = *f;
*(t++) = x;
}
strcpy(t, "=");
e ++;
if (r < 0)
return r;
*p = e;
return r;
}
/* Kernel fields are those occurring in the audit string before
* msg='. All of these fields are trusted, hence carry the "_" prefix.
* We try to translate the fields we know into our native names. The
* other's are generically mapped to _AUDIT_FIELD_XYZ= */
/* First, we map certain well-known audit fields into native
* well-known fields */
/* Some fields don't map to native well-known fields. However,
* we know that they are string fields, hence let's undo
* string field escaping for them, though we stick to the
* generic field names. */
{}
};
/* Userspace fields are those occurring in the audit string after
* msg='. All of these fields are untrusted, hence carry no "_"
* prefix. We map the fields we don't know to AUDIT_FIELD_XYZ= */
{}
};
static int map_all_fields(
const char *p,
const MapField map_fields[],
const char *prefix,
bool handle_msg,
unsigned *n_iov) {
int r;
assert(p);
for (;;) {
bool mapped = false;
const MapField *m;
const char *v;
p += strspn(p, WHITESPACE);
if (*p == 0)
return 0;
if (handle_msg) {
v = startswith(p, "msg='");
if (v) {
const char *e;
char *c;
/* Userspace message. It's enclosed in
simple quotation marks, is not
escaped, but the last field in the
line, hence let's remove the
quotation mark, and apply the
userspace mapping instead of the
kernel mapping. */
e = endswith(v, "'");
if (!e)
return 0; /* don't continue splitting up if the final quotation mark is missing */
c = strndupa(v, e - v);
}
}
/* Try to map the kernel fields to our own names */
for (m = map_fields; m->audit_field; m++) {
v = startswith(p, m->audit_field);
if (!v)
continue;
if (r < 0)
return log_debug_errno(r, "Failed to parse audit array: %m");
if (r > 0) {
mapped = true;
p = v;
break;
}
}
if (!mapped) {
if (r < 0)
return log_debug_errno(r, "Failed to parse audit array: %m");
if (r == 0)
/* Couldn't process as generic field, let's just skip over it */
p += strcspn(p, WHITESPACE);
}
}
}
unsigned n_iov = 0, k;
const char *p, *type_name;
unsigned z;
char *m;
assert(s);
if (size <= 0)
return;
if (!data)
return;
/* Note that the input buffer is NUL terminated, but let's
* check whether there is a spurious NUL byte */
return;
if (!p)
return;
&seconds,
&msec,
&id,
&k) != 3)
return;
p += k;
p += strspn(p, WHITESPACE);
if (isempty(p))
return;
if (!iov) {
log_oom();
return;
}
z = n_iov;
log_oom();
goto finish;
}
/* free() all entries that map_all_fields() added. All others
* are allocated on the stack or are constant. */
for (; z < n_iov; z++)
}
Server *s,
const void *buffer,
const union sockaddr_union *sa,
assert(s);
return;
/* Filter out fake data */
if (!sa ||
salen != sizeof(struct sockaddr_nl) ||
log_debug("Audit netlink message from invalid sender.");
return;
}
log_debug("Audit netlink message with invalid credentials.");
return;
}
log_error("Audit netlink message truncated.");
return;
}
/* Ignore special Netlink messages */
return;
/* Below AUDIT_FIRST_USER_MSG theer are only control messages, let's ignore those */
return;
process_audit_string(s, nl->nlmsg_type, NLMSG_DATA(nl), nl->nlmsg_len - ALIGN(sizeof(struct nlmsghdr)));
}
struct {
union {
};
};
};
};
.msg_iovlen = 1,
};
ssize_t n;
if (n < 0)
return -errno;
if (n != NLMSG_LENGTH(sizeof(struct audit_status)))
return -EIO;
/* We don't wait for the result here, we can't do anything
* about it anyway */
return 0;
}
int r;
if (s->audit_fd < 0) {
};
if (s->audit_fd < 0) {
log_debug("Audit not supported in the kernel.");
else
return 0;
}
"Failed to join audit multicast group. "
"The kernel is probably too old or multicast reading is not supported. "
"Ignoring: %m");
return 0;
}
} else
if (r < 0)
r = sd_event_add_io(s->event, &s->audit_event_source, s->audit_fd, EPOLLIN, server_process_datagram, s);
if (r < 0)
return log_error_errno(r, "Failed to add audit fd to event loop: %m");
/* We are listening now, try to enable audit */
r = enable_audit(s->audit_fd, true);
if (r < 0)
log_warning_errno(r, "Failed to issue audit enable call: %m");
return 0;
}