2010-09-15 14:44:11 +01:00
/*
2011-07-11 13:42:15 -06:00
* viraudit . c : auditing support
2010-09-15 14:44:11 +01:00
*
2011-05-26 09:09:42 -06:00
* Copyright ( C ) 2010 - 2011 Red Hat , Inc .
2010-09-15 14:44:11 +01: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 16:30:55 -06:00
* License along with this library . If not , see
2012-07-21 18:06:23 +08:00
* < http : //www.gnu.org/licenses/>.
2010-09-15 14:44:11 +01:00
*
*/
# include <config.h>
2012-09-20 13:00:09 +01:00
# ifdef WITH_AUDIT
2010-09-15 14:44:11 +01: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 13:42:15 -06:00
# include "viraudit.h"
2011-07-19 12:32:58 -06:00
# include "virfile.h"
2010-09-15 14:44:11 +01:00
2014-02-28 12:16:17 +00:00
VIR_LOG_INIT ( " util.audit " ) ;
2010-09-15 14:44:11 +01:00
# define VIR_FROM_THIS VIR_FROM_AUDIT
2012-09-20 13:00:09 +01:00
# if WITH_AUDIT
2010-09-15 14:44:11 +01:00
static int auditfd = - 1 ;
# endif
2014-10-28 13:07:09 -06:00
static bool auditlog ;
2010-09-15 14:44:11 +01:00
2019-10-14 14:45:33 +02:00
int virAuditOpen ( unsigned int audit_level G_GNUC_UNUSED )
2010-09-15 14:44:11 +01:00
{
2012-09-20 13:00:09 +01:00
# if WITH_AUDIT
2010-09-15 14:44:11 +01:00
if ( ( auditfd = audit_open ( ) ) < 0 ) {
2017-12-13 11:56:13 +01: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 14:44:11 +01:00
return - 1 ;
}
return 0 ;
# else
return - 1 ;
# endif
}
2014-10-28 13:07:09 -06:00
void virAuditLog ( bool logging )
2010-09-15 14:44:11 +01:00
{
auditlog = logging ;
}
2021-03-11 08:16:13 +01:00
void virAuditSend ( virLogSource * source ,
2014-02-27 17:44:53 +00:00
const char * filename ,
2010-10-20 12:21:52 -06:00
size_t linenr ,
2012-09-27 14:44:22 +01:00
const char * funcname ,
2019-10-14 14:45:33 +02:00
const char * clienttty G_GNUC_UNUSED ,
const char * clientaddr G_GNUC_UNUSED ,
virAuditRecordType type G_GNUC_UNUSED , bool success ,
2010-09-15 14:44:11 +01:00
const char * fmt , . . . )
{
2019-10-15 15:16:31 +02:00
g_autofree char * str = NULL ;
2010-09-15 14:44:11 +01:00
va_list args ;
/* Duplicate later checks, to short circuit & avoid printf overhead
* when nothing is enabled */
2012-09-20 13:00:09 +01:00
# if WITH_AUDIT
2010-09-15 14:44:11 +01:00
if ( ! auditlog & & auditfd < 0 )
return ;
# else
if ( ! auditlog )
return ;
# endif
va_start ( args , fmt ) ;
2019-10-22 14:11:15 +02:00
str = g_strdup_vprintf ( fmt , args ) ;
2010-09-15 14:44:11 +01: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 14:44:22 +01: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 20:17:14 +02:00
NULL , " success=yes %s " , str ) ;
2010-09-15 14:44:11 +01:00
else
2014-02-27 17:44:53 +00:00
virLogMessage ( source , VIR_LOG_WARN ,
2012-09-27 14:44:22 +01: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 20:17:14 +02:00
NULL , " success=no %s " , str ) ;
2010-09-15 14:44:11 +01:00
}
2012-09-20 13:00:09 +01:00
# if WITH_AUDIT
2014-09-18 12:08:37 +02:00
if ( str & & auditfd > = 0 ) {
2010-09-15 14:44:11 +01: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 13:55:26 +02:00
if ( type > = G_N_ELEMENTS ( record_types ) | | record_types [ type ] = = 0 )
2010-09-15 14:44:11 +01: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 18:57:34 +01:00
NULLSTR ( str ) , g_strerror ( errno ) ) ;
2010-09-15 14:44:11 +01:00
}
}
# endif
}
void virAuditClose ( void )
{
2012-09-20 13:00:09 +01:00
# if WITH_AUDIT
2010-11-09 15:48:48 -05:00
VIR_FORCE_CLOSE ( auditfd ) ;
2010-09-15 14:44:11 +01:00
# endif
}
2010-10-27 11:53:48 +01:00
char * virAuditEncode ( const char * key , const char * value )
{
2012-09-20 13:00:09 +01:00
# if WITH_AUDIT
2010-10-27 11:53:48 +01:00
return audit_encode_nv_string ( key , value , 0 ) ;
# else
2021-10-22 10:56:01 +02:00
return g_strdup_printf ( " %s=%s " , key , value ) ;
2010-10-27 11:53:48 +01:00
# endif
}