2010-09-15 13:44:11 +00:00
|
|
|
/*
|
2011-07-11 19:42:15 +00:00
|
|
|
* viraudit.c: auditing support
|
2010-09-15 13:44:11 +00:00
|
|
|
*
|
2011-05-26 15:09:42 +00:00
|
|
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
2010-09-15 13:44:11 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it 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.
|
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-09-15 13:44:11 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-09-20 12:00:09 +00:00
|
|
|
#ifdef WITH_AUDIT
|
2010-09-15 13:44:11 +00:00
|
|
|
# include <libaudit.h>
|
|
|
|
#endif
|
|
|
|
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2011-07-11 19:42:15 +00:00
|
|
|
#include "viraudit.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2010-09-15 13:44:11 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.audit");
|
|
|
|
|
2010-09-15 13:44:11 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_AUDIT
|
|
|
|
|
2012-09-20 12:00:09 +00:00
|
|
|
#if WITH_AUDIT
|
2010-09-15 13:44:11 +00:00
|
|
|
static int auditfd = -1;
|
|
|
|
#endif
|
2014-10-28 19:07:09 +00:00
|
|
|
static bool auditlog;
|
2010-09-15 13:44:11 +00:00
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virAuditOpen(unsigned int audit_level G_GNUC_UNUSED)
|
2010-09-15 13:44:11 +00:00
|
|
|
{
|
2012-09-20 12:00:09 +00:00
|
|
|
#if WITH_AUDIT
|
2010-09-15 13:44:11 +00:00
|
|
|
if ((auditfd = audit_open()) < 0) {
|
2017-12-13 10:56:13 +00:00
|
|
|
/* You get these error codes only when the kernel does not
|
|
|
|
* have audit compiled in or it's disabled (e.g. by the kernel
|
|
|
|
* cmdline) */
|
|
|
|
if (errno == EINVAL || errno == EPROTONOSUPPORT ||
|
|
|
|
errno == EAFNOSUPPORT) {
|
|
|
|
if (audit_level < 2)
|
|
|
|
VIR_INFO("Audit is not supported by the kernel");
|
|
|
|
else
|
|
|
|
virReportError(VIR_FROM_THIS, "%s", _("Audit is not supported by the kernel"));
|
|
|
|
} else {
|
|
|
|
virReportSystemError(errno, "%s", _("Unable to initialize audit layer"));
|
|
|
|
}
|
|
|
|
|
2010-09-15 13:44:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-28 19:07:09 +00:00
|
|
|
void virAuditLog(bool logging)
|
2010-09-15 13:44:11 +00:00
|
|
|
{
|
|
|
|
auditlog = logging;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 17:44:53 +00:00
|
|
|
void virAuditSend(virLogSourcePtr source,
|
|
|
|
const char *filename,
|
2010-10-20 18:21:52 +00:00
|
|
|
size_t linenr,
|
2012-09-27 13:44:22 +00:00
|
|
|
const char *funcname,
|
2019-10-14 12:45:33 +00:00
|
|
|
const char *clienttty G_GNUC_UNUSED,
|
|
|
|
const char *clientaddr G_GNUC_UNUSED,
|
|
|
|
virAuditRecordType type G_GNUC_UNUSED, bool success,
|
2010-09-15 13:44:11 +00:00
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *str = NULL;
|
2010-09-15 13:44:11 +00:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* Duplicate later checks, to short circuit & avoid printf overhead
|
|
|
|
* when nothing is enabled */
|
2012-09-20 12:00:09 +00:00
|
|
|
#if WITH_AUDIT
|
2010-09-15 13:44:11 +00:00
|
|
|
if (!auditlog && auditfd < 0)
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
if (!auditlog)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2019-10-22 12:11:15 +00:00
|
|
|
str = g_strdup_vprintf(fmt, args);
|
2010-09-15 13:44:11 +00:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (auditlog && str) {
|
|
|
|
if (success)
|
2014-02-27 17:44:53 +00:00
|
|
|
virLogMessage(source, VIR_LOG_INFO,
|
2012-09-27 13:44:22 +00:00
|
|
|
filename, linenr, funcname,
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
NULL, "success=yes %s", str);
|
2010-09-15 13:44:11 +00:00
|
|
|
else
|
2014-02-27 17:44:53 +00:00
|
|
|
virLogMessage(source, VIR_LOG_WARN,
|
2012-09-27 13:44:22 +00:00
|
|
|
filename, linenr, funcname,
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
NULL, "success=no %s", str);
|
2010-09-15 13:44:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 12:00:09 +00:00
|
|
|
#if WITH_AUDIT
|
2014-09-18 10:08:37 +00:00
|
|
|
if (str && auditfd >= 0) {
|
2010-09-15 13:44:11 +00:00
|
|
|
static const int record_types[] = {
|
|
|
|
[VIR_AUDIT_RECORD_MACHINE_CONTROL] = AUDIT_VIRT_CONTROL,
|
|
|
|
[VIR_AUDIT_RECORD_MACHINE_ID] = AUDIT_VIRT_MACHINE_ID,
|
|
|
|
[VIR_AUDIT_RECORD_RESOURCE] = AUDIT_VIRT_RESOURCE,
|
|
|
|
};
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
if (type >= G_N_ELEMENTS(record_types) || record_types[type] == 0)
|
2010-09-15 13:44:11 +00:00
|
|
|
VIR_WARN("Unknown audit record type %d", type);
|
|
|
|
else if (audit_log_user_message(auditfd, record_types[type], str, NULL,
|
|
|
|
clientaddr, clienttty, success) < 0) {
|
|
|
|
VIR_WARN("Failed to send audit message %s: %s",
|
2020-02-26 17:57:34 +00:00
|
|
|
NULLSTR(str), g_strerror(errno));
|
2010-09-15 13:44:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void virAuditClose(void)
|
|
|
|
{
|
2012-09-20 12:00:09 +00:00
|
|
|
#if WITH_AUDIT
|
2010-11-09 20:48:48 +00:00
|
|
|
VIR_FORCE_CLOSE(auditfd);
|
2010-09-15 13:44:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-10-27 10:53:48 +00:00
|
|
|
|
|
|
|
char *virAuditEncode(const char *key, const char *value)
|
|
|
|
{
|
2012-09-20 12:00:09 +00:00
|
|
|
#if WITH_AUDIT
|
2010-10-27 10:53:48 +00:00
|
|
|
return audit_encode_nv_string(key, value, 0);
|
|
|
|
#else
|
|
|
|
char *str;
|
2019-10-22 13:26:14 +00:00
|
|
|
str = g_strdup_printf("%s=%s", key, value);
|
2010-10-27 10:53:48 +00:00
|
|
|
return str;
|
|
|
|
#endif
|
|
|
|
}
|