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
|
|
|
|
#include <unistd.h>
|
|
|
|
|
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"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.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
|
|
|
/* Provide the macros in case the header file is old.
|
|
|
|
FIXME: should be removed. */
|
|
|
|
#ifndef AUDIT_VIRT_CONTROL
|
|
|
|
# define AUDIT_VIRT_CONTROL 2500 /* Start, Pause, Stop VM */
|
|
|
|
#endif
|
|
|
|
#ifndef AUDIT_VIRT_RESOURCE
|
|
|
|
# define AUDIT_VIRT_RESOURCE 2501 /* Resource assignment */
|
|
|
|
#endif
|
|
|
|
#ifndef AUDIT_VIRT_MACHINE_ID
|
|
|
|
# define AUDIT_VIRT_MACHINE_ID 2502 /* Binding of label to VM */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
2017-12-13 10:56:13 +00:00
|
|
|
int virAuditOpen(unsigned int audit_level ATTRIBUTE_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,
|
2010-09-15 13:44:11 +00:00
|
|
|
const char *clienttty ATTRIBUTE_UNUSED,
|
|
|
|
const char *clientaddr ATTRIBUTE_UNUSED,
|
2014-04-27 00:15:22 +00:00
|
|
|
virAuditRecordType type ATTRIBUTE_UNUSED, bool success,
|
2010-09-15 13:44:11 +00:00
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2018-07-13 17:55:05 +00:00
|
|
|
VIR_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);
|
2014-09-18 09:59:38 +00:00
|
|
|
if (virVasprintf(&str, fmt, args) < 0)
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_WARN("Out of memory while formatting audit message");
|
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,
|
|
|
|
};
|
|
|
|
|
2011-05-26 15:09:42 +00:00
|
|
|
if (type >= ARRAY_CARDINALITY(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) {
|
|
|
|
char ebuf[1024];
|
|
|
|
VIR_WARN("Failed to send audit message %s: %s",
|
2012-03-29 09:52:04 +00:00
|
|
|
NULLSTR(str), virStrerror(errno, ebuf, sizeof(ebuf)));
|
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;
|
|
|
|
if (virAsprintf(&str, "%s=%s", key, value) < 0)
|
|
|
|
return NULL;
|
|
|
|
return str;
|
|
|
|
#endif
|
|
|
|
}
|