viraudit.c 4.1 KB
Newer Older
1
/*
2
 * viraudit.c: auditing support
3
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23
 *
 */

#include <config.h>

24
#ifdef WITH_AUDIT
25 26 27 28 29
# include <libaudit.h>
#endif
#include <stdio.h>
#include <unistd.h>

30
#include "virerror.h"
31
#include "virlog.h"
32
#include "viraudit.h"
E
Eric Blake 已提交
33
#include "virfile.h"
34
#include "viralloc.h"
35
#include "virstring.h"
36

37 38
VIR_LOG_INIT("util.audit");

39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* 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

53
#if WITH_AUDIT
54 55 56 57 58 59
static int auditfd = -1;
#endif
static int auditlog = 0;

int virAuditOpen(void)
{
60
#if WITH_AUDIT
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if ((auditfd = audit_open()) < 0) {
        virReportSystemError(errno, "%s", _("Unable to initialize audit layer"));
        return -1;
    }

    return 0;
#else
    return -1;
#endif
}


void virAuditLog(int logging)
{
    auditlog = logging;
}


79 80
void virAuditSend(virLogSourcePtr source,
                  const char *filename,
E
Eric Blake 已提交
81
                  size_t linenr,
82
                  const char *funcname,
83 84
                  const char *clienttty ATTRIBUTE_UNUSED,
                  const char *clientaddr ATTRIBUTE_UNUSED,
85
                  virAuditRecordType type ATTRIBUTE_UNUSED, bool success,
86 87 88 89 90 91 92
                  const char *fmt, ...)
{
    char *str = NULL;
    va_list args;

    /* Duplicate later checks, to short circuit & avoid printf overhead
     * when nothing is enabled */
93
#if WITH_AUDIT
94 95 96 97 98 99 100 101
    if (!auditlog && auditfd < 0)
        return;
#else
    if (!auditlog)
        return;
#endif

    va_start(args, fmt);
102
    if (virVasprintf(&str, fmt, args) < 0)
103
        VIR_WARN("Out of memory while formatting audit message");
104 105 106 107
    va_end(args);

    if (auditlog && str) {
        if (success)
108
            virLogMessage(source, VIR_LOG_INFO,
109
                          filename, linenr, funcname,
110
                          NULL, "success=yes %s", str);
111
        else
112
            virLogMessage(source, VIR_LOG_WARN,
113
                          filename, linenr, funcname,
114
                          NULL, "success=no %s", str);
115 116
    }

117
#if WITH_AUDIT
118 119
    if (auditfd < 0) {
        VIR_FREE(str);
120
        return;
121
    }
122 123 124 125 126 127 128 129

    if (str) {
        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,
        };

E
Eric Blake 已提交
130
        if (type >= ARRAY_CARDINALITY(record_types) || record_types[type] == 0)
131 132 133 134 135
            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",
136
                     NULLSTR(str), virStrerror(errno, ebuf, sizeof(ebuf)));
137
        }
138
        VIR_FREE(str);
139 140 141 142 143 144
    }
#endif
}

void virAuditClose(void)
{
145
#if WITH_AUDIT
146
    VIR_FORCE_CLOSE(auditfd);
147 148
#endif
}
149 150 151

char *virAuditEncode(const char *key, const char *value)
{
152
#if WITH_AUDIT
153 154 155 156 157 158 159 160
    return audit_encode_nv_string(key, value, 0);
#else
    char *str;
    if (virAsprintf(&str, "%s=%s", key, value) < 0)
        return NULL;
    return str;
#endif
}