2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-10-02 23:40:09 +02:00
|
|
|
|
2012-10-02 23:56:54 +02:00
|
|
|
#include <errno.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
|
2012-10-02 23:40:09 +02:00
|
|
|
#include "audit-fd.h"
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2012-10-02 23:40:09 +02:00
|
|
|
|
|
|
|
#include <libaudit.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <stdbool.h>
|
2012-10-02 23:40:09 +02:00
|
|
|
|
2017-05-02 22:05:32 +02:00
|
|
|
#include "capability-util.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "fd-util.h"
|
2012-10-02 23:56:54 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2012-10-02 23:40:09 +02:00
|
|
|
static bool initialized = false;
|
|
|
|
static int audit_fd;
|
|
|
|
|
|
|
|
int get_audit_fd(void) {
|
|
|
|
|
|
|
|
if (!initialized) {
|
2017-05-02 22:05:32 +02:00
|
|
|
if (have_effective_cap(CAP_AUDIT_WRITE) == 0) {
|
|
|
|
audit_fd = -EPERM;
|
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
return audit_fd;
|
|
|
|
}
|
|
|
|
|
2012-10-02 23:40:09 +02:00
|
|
|
audit_fd = audit_open();
|
|
|
|
|
|
|
|
if (audit_fd < 0) {
|
2017-09-29 09:58:22 +02:00
|
|
|
if (!IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to connect to audit log: %m");
|
2012-10-02 23:40:09 +02:00
|
|
|
|
|
|
|
audit_fd = errno ? -errno : -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return audit_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_audit_fd(void) {
|
|
|
|
|
|
|
|
if (initialized && audit_fd >= 0)
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(audit_fd);
|
2012-10-02 23:40:09 +02:00
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
audit_fd = -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int get_audit_fd(void) {
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_audit_fd(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|