2013-03-04 16:30:40 +00:00
|
|
|
/*
|
|
|
|
* virfirewall.c: integration with firewalls
|
|
|
|
*
|
2015-05-21 17:36:18 +00:00
|
|
|
* Copyright (C) 2013-2015 Red Hat, Inc.
|
2013-03-04 16:30:40 +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
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2018-12-13 14:53:50 +00:00
|
|
|
#define LIBVIRT_VIRFIREWALLPRIV_H_ALLOW
|
2013-03-04 16:30:40 +00:00
|
|
|
#include "virfirewallpriv.h"
|
2019-01-09 19:11:32 +00:00
|
|
|
#include "virfirewalld.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2013-03-04 16:30:40 +00:00
|
|
|
#include "virerror.h"
|
|
|
|
#include "virutil.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
#include "vircommand.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virthread.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_FIREWALL
|
|
|
|
|
|
|
|
VIR_LOG_INIT("util.firewall");
|
|
|
|
|
|
|
|
typedef struct _virFirewallGroup virFirewallGroup;
|
|
|
|
typedef virFirewallGroup *virFirewallGroupPtr;
|
|
|
|
|
2019-01-20 16:04:56 +00:00
|
|
|
VIR_ENUM_DECL(virFirewallLayerCommand);
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virFirewallLayerCommand,
|
|
|
|
VIR_FIREWALL_LAYER_LAST,
|
2013-03-04 16:30:40 +00:00
|
|
|
EBTABLES_PATH,
|
|
|
|
IPTABLES_PATH,
|
2019-01-20 16:30:15 +00:00
|
|
|
IP6TABLES_PATH,
|
|
|
|
);
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
struct _virFirewallRule {
|
|
|
|
virFirewallLayer layer;
|
|
|
|
|
|
|
|
virFirewallQueryCallback queryCB;
|
|
|
|
void *queryOpaque;
|
|
|
|
bool ignoreErrors;
|
|
|
|
|
|
|
|
size_t argsAlloc;
|
|
|
|
size_t argsLen;
|
|
|
|
char **args;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virFirewallGroup {
|
|
|
|
unsigned int actionFlags;
|
|
|
|
unsigned int rollbackFlags;
|
|
|
|
|
|
|
|
size_t naction;
|
|
|
|
virFirewallRulePtr *action;
|
|
|
|
|
|
|
|
size_t nrollback;
|
|
|
|
virFirewallRulePtr *rollback;
|
|
|
|
|
|
|
|
bool addingRollback;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct _virFirewall {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
size_t ngroups;
|
|
|
|
virFirewallGroupPtr *groups;
|
|
|
|
size_t currentGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
static virFirewallBackend currentBackend = VIR_FIREWALL_BACKEND_AUTOMATIC;
|
|
|
|
static virMutex ruleLock = VIR_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFirewallValidateBackend(virFirewallBackend backend);
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFirewallOnceInit(void)
|
|
|
|
{
|
|
|
|
return virFirewallValidateBackend(currentBackend);
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virFirewall);
|
2013-03-04 16:30:40 +00:00
|
|
|
|
2014-11-11 12:34:57 +00:00
|
|
|
static bool iptablesUseLock;
|
|
|
|
static bool ip6tablesUseLock;
|
|
|
|
static bool ebtablesUseLock;
|
2014-12-22 21:57:21 +00:00
|
|
|
static bool lockOverride; /* true to avoid lock probes */
|
|
|
|
|
|
|
|
void
|
|
|
|
virFirewallSetLockOverride(bool avoid)
|
|
|
|
{
|
|
|
|
lockOverride = avoid;
|
|
|
|
}
|
2014-11-11 12:34:57 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
virFirewallCheckUpdateLock(bool *lockflag,
|
|
|
|
const char *const*args)
|
|
|
|
{
|
2014-11-19 23:18:55 +00:00
|
|
|
int status; /* Ignore failed commands without logging them */
|
2018-07-24 15:52:17 +00:00
|
|
|
VIR_AUTOPTR(virCommand) cmd = virCommandNewArgs(args);
|
2014-11-19 23:18:55 +00:00
|
|
|
if (virCommandRun(cmd, &status) < 0 || status) {
|
2014-11-11 12:34:57 +00:00
|
|
|
VIR_INFO("locking not supported by %s", args[0]);
|
|
|
|
} else {
|
|
|
|
VIR_INFO("using locking for %s", args[0]);
|
|
|
|
*lockflag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virFirewallCheckUpdateLocking(void)
|
|
|
|
{
|
|
|
|
const char *iptablesArgs[] = {
|
|
|
|
IPTABLES_PATH, "-w", "-L", "-n", NULL,
|
|
|
|
};
|
|
|
|
const char *ip6tablesArgs[] = {
|
|
|
|
IP6TABLES_PATH, "-w", "-L", "-n", NULL,
|
|
|
|
};
|
|
|
|
const char *ebtablesArgs[] = {
|
|
|
|
EBTABLES_PATH, "--concurrent", "-L", NULL,
|
|
|
|
};
|
2014-12-22 21:57:21 +00:00
|
|
|
if (lockOverride)
|
|
|
|
return;
|
2014-11-11 12:34:57 +00:00
|
|
|
virFirewallCheckUpdateLock(&iptablesUseLock,
|
|
|
|
iptablesArgs);
|
|
|
|
virFirewallCheckUpdateLock(&ip6tablesUseLock,
|
|
|
|
ip6tablesArgs);
|
|
|
|
virFirewallCheckUpdateLock(&ebtablesUseLock,
|
|
|
|
ebtablesArgs);
|
|
|
|
}
|
|
|
|
|
2013-03-04 16:30:40 +00:00
|
|
|
static int
|
|
|
|
virFirewallValidateBackend(virFirewallBackend backend)
|
|
|
|
{
|
|
|
|
VIR_DEBUG("Validating backend %d", backend);
|
|
|
|
if (backend == VIR_FIREWALL_BACKEND_AUTOMATIC ||
|
|
|
|
backend == VIR_FIREWALL_BACKEND_FIREWALLD) {
|
2019-01-09 19:11:32 +00:00
|
|
|
int rv = virFirewallDIsRegistered();
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Firewalld is registered ? %d", rv);
|
|
|
|
if (rv < 0) {
|
|
|
|
if (rv == -2) {
|
|
|
|
if (backend == VIR_FIREWALL_BACKEND_FIREWALLD) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("firewalld firewall backend requested, but service is not running"));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("firewalld service not running, trying direct backend");
|
|
|
|
backend = VIR_FIREWALL_BACKEND_DIRECT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("firewalld service running, using firewalld backend");
|
|
|
|
backend = VIR_FIREWALL_BACKEND_FIREWALLD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend == VIR_FIREWALL_BACKEND_DIRECT) {
|
|
|
|
const char *commands[] = {
|
|
|
|
IPTABLES_PATH, IP6TABLES_PATH, EBTABLES_PATH
|
|
|
|
};
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_CARDINALITY(commands); i++) {
|
|
|
|
if (!virFileIsExecutable(commands[i])) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("direct firewall backend requested, but %s is not available"),
|
|
|
|
commands[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VIR_DEBUG("found iptables/ip6tables/ebtables, using direct backend");
|
|
|
|
}
|
|
|
|
|
|
|
|
currentBackend = backend;
|
2014-11-11 12:34:57 +00:00
|
|
|
|
|
|
|
virFirewallCheckUpdateLocking();
|
|
|
|
|
2013-03-04 16:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virFirewallSetBackend(virFirewallBackend backend)
|
|
|
|
{
|
|
|
|
currentBackend = backend;
|
|
|
|
|
|
|
|
if (virFirewallInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return virFirewallValidateBackend(backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
static virFirewallGroupPtr
|
|
|
|
virFirewallGroupNew(void)
|
|
|
|
{
|
|
|
|
virFirewallGroupPtr group;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(group) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFirewallNew:
|
|
|
|
*
|
|
|
|
* Creates a new firewall ruleset for changing rules
|
|
|
|
* of @layer. This should be followed by a call to
|
|
|
|
* virFirewallStartTransaction before adding
|
|
|
|
* any rules
|
|
|
|
*
|
|
|
|
* Returns the new firewall ruleset
|
|
|
|
*/
|
|
|
|
virFirewallPtr virFirewallNew(void)
|
|
|
|
{
|
|
|
|
virFirewallPtr firewall;
|
|
|
|
|
2014-11-11 12:34:57 +00:00
|
|
|
if (virFirewallInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-04 16:30:40 +00:00
|
|
|
if (VIR_ALLOC(firewall) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return firewall;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virFirewallRuleFree(virFirewallRulePtr rule)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!rule)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < rule->argsLen; i++)
|
|
|
|
VIR_FREE(rule->args[i]);
|
|
|
|
VIR_FREE(rule->args);
|
|
|
|
VIR_FREE(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virFirewallGroupFree(virFirewallGroupPtr group)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!group)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < group->naction; i++)
|
|
|
|
virFirewallRuleFree(group->action[i]);
|
|
|
|
VIR_FREE(group->action);
|
|
|
|
|
|
|
|
for (i = 0; i < group->nrollback; i++)
|
|
|
|
virFirewallRuleFree(group->rollback[i]);
|
|
|
|
VIR_FREE(group->rollback);
|
|
|
|
|
|
|
|
VIR_FREE(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFirewallFree:
|
|
|
|
*
|
|
|
|
* Release all memory associated with the firewall
|
|
|
|
* ruleset
|
|
|
|
*/
|
|
|
|
void virFirewallFree(virFirewallPtr firewall)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!firewall)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < firewall->ngroups; i++)
|
|
|
|
virFirewallGroupFree(firewall->groups[i]);
|
|
|
|
VIR_FREE(firewall->groups);
|
|
|
|
|
|
|
|
VIR_FREE(firewall);
|
|
|
|
}
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define VIR_FIREWALL_RETURN_IF_ERROR(firewall) \
|
|
|
|
do { \
|
|
|
|
if (!firewall || firewall->err) \
|
|
|
|
return; \
|
2013-03-04 16:30:40 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule)\
|
2017-11-03 12:09:47 +00:00
|
|
|
do { \
|
|
|
|
if (!firewall || firewall->err || !rule) \
|
|
|
|
return; \
|
2013-03-04 16:30:40 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define VIR_FIREWALL_RETURN_NULL_IF_ERROR(firewall) \
|
|
|
|
do { \
|
|
|
|
if (!firewall || firewall->err) \
|
|
|
|
return NULL; \
|
2013-03-04 16:30:40 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define ADD_ARG(rule, str) \
|
|
|
|
do { \
|
|
|
|
if (VIR_RESIZE_N(rule->args, \
|
|
|
|
rule->argsAlloc, \
|
|
|
|
rule->argsLen, 1) < 0) \
|
|
|
|
goto no_memory; \
|
|
|
|
\
|
2013-03-04 16:30:40 +00:00
|
|
|
if (VIR_STRDUP(rule->args[rule->argsLen++], str) < 0) \
|
2017-11-03 12:09:47 +00:00
|
|
|
goto no_memory; \
|
2013-03-04 16:30:40 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static virFirewallRulePtr
|
|
|
|
virFirewallAddRuleFullV(virFirewallPtr firewall,
|
|
|
|
virFirewallLayer layer,
|
|
|
|
bool ignoreErrors,
|
|
|
|
virFirewallQueryCallback cb,
|
|
|
|
void *opaque,
|
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
virFirewallGroupPtr group;
|
|
|
|
virFirewallRulePtr rule;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
VIR_FIREWALL_RETURN_NULL_IF_ERROR(firewall);
|
|
|
|
|
|
|
|
if (firewall->ngroups == 0) {
|
2014-04-30 06:51:29 +00:00
|
|
|
firewall->err = EINVAL;
|
2013-03-04 16:30:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
group = firewall->groups[firewall->currentGroup];
|
|
|
|
|
|
|
|
|
|
|
|
if (VIR_ALLOC(rule) < 0)
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
rule->layer = layer;
|
|
|
|
rule->queryCB = cb;
|
|
|
|
rule->queryOpaque = opaque;
|
|
|
|
rule->ignoreErrors = ignoreErrors;
|
|
|
|
|
2014-11-11 12:34:57 +00:00
|
|
|
switch (rule->layer) {
|
|
|
|
case VIR_FIREWALL_LAYER_ETHERNET:
|
|
|
|
if (ebtablesUseLock)
|
|
|
|
ADD_ARG(rule, "--concurrent");
|
|
|
|
break;
|
|
|
|
case VIR_FIREWALL_LAYER_IPV4:
|
|
|
|
if (iptablesUseLock)
|
|
|
|
ADD_ARG(rule, "-w");
|
|
|
|
break;
|
|
|
|
case VIR_FIREWALL_LAYER_IPV6:
|
|
|
|
if (ip6tablesUseLock)
|
|
|
|
ADD_ARG(rule, "-w");
|
|
|
|
break;
|
|
|
|
case VIR_FIREWALL_LAYER_LAST:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:28:18 +00:00
|
|
|
while ((str = va_arg(args, char *)) != NULL)
|
2013-03-04 16:30:40 +00:00
|
|
|
ADD_ARG(rule, str);
|
|
|
|
|
|
|
|
if (group->addingRollback) {
|
|
|
|
if (VIR_APPEND_ELEMENT_COPY(group->rollback,
|
|
|
|
group->nrollback,
|
|
|
|
rule) < 0)
|
|
|
|
goto no_memory;
|
|
|
|
} else {
|
|
|
|
if (VIR_APPEND_ELEMENT_COPY(group->action,
|
|
|
|
group->naction,
|
|
|
|
rule) < 0)
|
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
virFirewallRuleFree(rule);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFirewallAddRuleFull:
|
|
|
|
* @firewall: firewall ruleset to add to
|
|
|
|
* @layer: the firewall layer to change
|
|
|
|
* @ignoreErrors: true to ignore failure of the command
|
|
|
|
* @cb: callback to invoke with result of query
|
|
|
|
* @opaque: data passed into @cb
|
|
|
|
* @...: NULL terminated list of strings for the rule
|
|
|
|
*
|
|
|
|
* Add any type of rule to the firewall ruleset. Any output
|
|
|
|
* generated by the addition will be fed into the query
|
|
|
|
* callback @cb. This callback is permitted to create new
|
|
|
|
* rules by invoking the virFirewallAddRule method, but
|
|
|
|
* is not permitted to start new transactions.
|
|
|
|
*
|
|
|
|
* If @ignoreErrors is set to TRUE, then any failure of
|
|
|
|
* the command is ignored. If it is set to FALSE, then
|
|
|
|
* the behaviour upon failure is determined by the flags
|
|
|
|
* set when the transaction was started.
|
|
|
|
*
|
|
|
|
* Returns the new rule
|
|
|
|
*/
|
|
|
|
virFirewallRulePtr virFirewallAddRuleFull(virFirewallPtr firewall,
|
|
|
|
virFirewallLayer layer,
|
|
|
|
bool ignoreErrors,
|
|
|
|
virFirewallQueryCallback cb,
|
|
|
|
void *opaque,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
virFirewallRulePtr rule;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, opaque);
|
|
|
|
rule = virFirewallAddRuleFullV(firewall, layer, ignoreErrors, cb, opaque, args);
|
|
|
|
va_end(args);
|
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFirewallRemoveRule:
|
|
|
|
* @firewall: firewall ruleset to remove from
|
|
|
|
* @rule: the rule to remove
|
|
|
|
*
|
|
|
|
* Remove a rule from the current transaction
|
|
|
|
*/
|
|
|
|
void virFirewallRemoveRule(virFirewallPtr firewall,
|
|
|
|
virFirewallRulePtr rule)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
virFirewallGroupPtr group;
|
|
|
|
|
|
|
|
/* Explicitly not checking firewall->err too,
|
|
|
|
* because if rule was partially created
|
|
|
|
* before hitting error we must still remove
|
|
|
|
* it to avoid leaking 'rule'
|
|
|
|
*/
|
|
|
|
if (!firewall)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (firewall->ngroups == 0)
|
|
|
|
return;
|
|
|
|
group = firewall->groups[firewall->currentGroup];
|
|
|
|
|
|
|
|
if (group->addingRollback) {
|
|
|
|
for (i = 0; i < group->nrollback; i++) {
|
|
|
|
if (group->rollback[i] == rule) {
|
|
|
|
VIR_DELETE_ELEMENT(group->rollback,
|
|
|
|
i,
|
|
|
|
group->nrollback);
|
|
|
|
virFirewallRuleFree(rule);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < group->naction; i++) {
|
|
|
|
if (group->action[i] == rule) {
|
|
|
|
VIR_DELETE_ELEMENT(group->action,
|
|
|
|
i,
|
|
|
|
group->naction);
|
|
|
|
virFirewallRuleFree(rule);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virFirewallRuleAddArg(virFirewallPtr firewall,
|
|
|
|
virFirewallRulePtr rule,
|
|
|
|
const char *arg)
|
|
|
|
{
|
|
|
|
VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule);
|
|
|
|
|
|
|
|
ADD_ARG(rule, arg);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virFirewallRuleAddArgFormat(virFirewallPtr firewall,
|
|
|
|
virFirewallRulePtr rule,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2018-07-24 15:52:16 +00:00
|
|
|
VIR_AUTOFREE(char *) arg = NULL;
|
2013-03-04 16:30:40 +00:00
|
|
|
va_list list;
|
|
|
|
|
|
|
|
VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule);
|
|
|
|
|
|
|
|
va_start(list, fmt);
|
|
|
|
|
|
|
|
if (virVasprintf(&arg, fmt, list) < 0)
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
ADD_ARG(rule, arg);
|
|
|
|
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
va_end(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virFirewallRuleAddArgSet(virFirewallPtr firewall,
|
|
|
|
virFirewallRulePtr rule,
|
|
|
|
const char *const *args)
|
|
|
|
{
|
|
|
|
VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule);
|
|
|
|
|
|
|
|
while (*args) {
|
|
|
|
ADD_ARG(rule, *args);
|
|
|
|
args++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virFirewallRuleAddArgList(virFirewallPtr firewall,
|
|
|
|
virFirewallRulePtr rule,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule);
|
|
|
|
|
|
|
|
va_start(list, rule);
|
|
|
|
|
2014-11-13 14:28:18 +00:00
|
|
|
while ((str = va_arg(list, char *)) != NULL)
|
2013-03-04 16:30:40 +00:00
|
|
|
ADD_ARG(rule, str);
|
|
|
|
|
|
|
|
va_end(list);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
va_end(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t virFirewallRuleGetArgCount(virFirewallRulePtr rule)
|
|
|
|
{
|
|
|
|
if (!rule)
|
|
|
|
return 0;
|
|
|
|
return rule->argsLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFirewallStartTransaction:
|
|
|
|
* @firewall: the firewall ruleset
|
|
|
|
* @flags: bitset of virFirewallTransactionFlags
|
|
|
|
*
|
|
|
|
* Start a new transaction with associated rollback
|
|
|
|
* block.
|
|
|
|
*
|
|
|
|
* Should be followed by calls to add various rules to
|
|
|
|
* the transaction. Then virFirwallStartRollback should
|
|
|
|
* be used to provide rules to rollback upon transaction
|
|
|
|
* failure
|
|
|
|
*/
|
|
|
|
void virFirewallStartTransaction(virFirewallPtr firewall,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virFirewallGroupPtr group;
|
|
|
|
|
|
|
|
VIR_FIREWALL_RETURN_IF_ERROR(firewall);
|
|
|
|
|
|
|
|
if (!(group = virFirewallGroupNew())) {
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
group->actionFlags = flags;
|
|
|
|
|
|
|
|
if (VIR_EXPAND_N(firewall->groups,
|
|
|
|
firewall->ngroups, 1) < 0) {
|
|
|
|
firewall->err = ENOMEM;
|
|
|
|
virFirewallGroupFree(group);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
firewall->groups[firewall->ngroups - 1] = group;
|
|
|
|
firewall->currentGroup = firewall->ngroups - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virFirewallBeginRollback:
|
|
|
|
* @firewall: the firewall ruleset
|
|
|
|
* @flags: bitset of virFirewallRollbackFlags
|
|
|
|
*
|
|
|
|
* Mark the beginning of a set of rules able to rollback
|
|
|
|
* changes in this and all earlier transactions.
|
|
|
|
*
|
|
|
|
* Should be followed by calls to add various rules needed
|
|
|
|
* to rollback state. Then virFirewallStartTransaction
|
|
|
|
* should be used to indicate the beginning of the next
|
|
|
|
* transactional ruleset.
|
|
|
|
*/
|
|
|
|
void virFirewallStartRollback(virFirewallPtr firewall,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virFirewallGroupPtr group;
|
|
|
|
|
|
|
|
VIR_FIREWALL_RETURN_IF_ERROR(firewall);
|
|
|
|
|
|
|
|
if (firewall->ngroups == 0) {
|
2014-04-30 06:51:29 +00:00
|
|
|
firewall->err = EINVAL;
|
2013-03-04 16:30:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
group = firewall->groups[firewall->ngroups-1];
|
|
|
|
group->rollbackFlags = flags;
|
|
|
|
group->addingRollback = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
virFirewallRuleToString(virFirewallRulePtr rule)
|
|
|
|
{
|
|
|
|
const char *bin = virFirewallLayerCommandTypeToString(rule->layer);
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
virBufferAdd(&buf, bin, -1);
|
|
|
|
for (i = 0; i < rule->argsLen; i++) {
|
|
|
|
virBufferAddLit(&buf, " ");
|
|
|
|
virBufferAdd(&buf, rule->args[i], -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFirewallApplyRuleDirect(virFirewallRulePtr rule,
|
|
|
|
bool ignoreErrors,
|
|
|
|
char **output)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char *bin = virFirewallLayerCommandTypeToString(rule->layer);
|
2018-07-24 15:52:17 +00:00
|
|
|
VIR_AUTOPTR(virCommand) cmd = NULL;
|
2013-03-04 16:30:40 +00:00
|
|
|
int status;
|
2018-07-24 15:52:16 +00:00
|
|
|
VIR_AUTOFREE(char *) error = NULL;
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
if (!bin) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown firewall layer %d"),
|
|
|
|
rule->layer);
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(bin, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < rule->argsLen; i++)
|
|
|
|
virCommandAddArg(cmd, rule->args[i]);
|
|
|
|
|
|
|
|
virCommandSetOutputBuffer(cmd, output);
|
|
|
|
virCommandSetErrorBuffer(cmd, &error);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
if (ignoreErrors) {
|
|
|
|
VIR_DEBUG("Ignoring error running command");
|
|
|
|
} else {
|
qemu: use line breaks in command line args written to log
The QEMU command line arguments are very long and currently all written
on a single line to /var/log/libvirt/qemu/$GUEST.log. This introduces
logic to add line breaks after every env variable and "-" optional
argument, and every positional argument. This will create a clearer log
file, which will in turn present better in bug reports when people cut +
paste from the log into a bug comment.
An example log file entry now looks like this:
2018-12-14 12:57:03.677+0000: starting up libvirt version: 5.0.0, qemu version: 3.0.0qemu-3.0.0-1.fc29, kernel: 4.19.5-300.fc29.x86_64, hostname: localhost.localdomain
LC_ALL=C \
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin \
HOME=/home/berrange \
USER=berrange \
LOGNAME=berrange \
QEMU_AUDIO_DRV=none \
/usr/bin/qemu-system-ppc64 \
-name guest=guest,debug-threads=on \
-S \
-object secret,id=masterKey0,format=raw,file=/home/berrange/.config/libvirt/qemu/lib/domain-33-guest/master-key.aes \
-machine pseries-2.10,accel=tcg,usb=off,dump-guest-core=off \
-m 1024 \
-realtime mlock=off \
-smp 1,sockets=1,cores=1,threads=1 \
-uuid c8a74977-ab18-41d0-ae3b-4041c7fffbcd \
-display none \
-no-user-config \
-nodefaults \
-chardev socket,id=charmonitor,fd=23,server,nowait \
-mon chardev=charmonitor,id=monitor,mode=control \
-rtc base=utc \
-no-shutdown \
-boot strict=on \
-device qemu-xhci,id=usb,bus=pci.0,addr=0x1 \
-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x2 \
-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny \
-msg timestamp=on
2018-12-14 12:57:03.730+0000: shutting down, reason=failed
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2018-12-14 12:07:08 +00:00
|
|
|
VIR_AUTOFREE(char *) args = virCommandToString(cmd, false);
|
2013-03-04 16:30:40 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to apply firewall rules %s: %s"),
|
|
|
|
NULLSTR(args), NULLSTR(error));
|
|
|
|
VIR_FREE(*output);
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:17 +00:00
|
|
|
return 0;
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFirewallApplyRuleFirewallD(virFirewallRulePtr rule,
|
|
|
|
bool ignoreErrors,
|
|
|
|
char **output)
|
|
|
|
{
|
2019-01-09 19:11:32 +00:00
|
|
|
/* wrapper necessary because virFirewallRule is a private struct */
|
|
|
|
return virFirewallDApplyRule(rule->layer, rule->args, rule->argsLen, ignoreErrors, output);
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFirewallApplyRule(virFirewallPtr firewall,
|
|
|
|
virFirewallRulePtr rule,
|
|
|
|
bool ignoreErrors)
|
|
|
|
{
|
2018-07-24 15:52:16 +00:00
|
|
|
VIR_AUTOFREE(char *) output = NULL;
|
|
|
|
VIR_AUTOFREE(char *) str = virFirewallRuleToString(rule);
|
2019-02-22 14:01:02 +00:00
|
|
|
VIR_AUTOSTRINGLIST lines = NULL;
|
2013-03-04 16:30:40 +00:00
|
|
|
VIR_INFO("Applying rule '%s'", NULLSTR(str));
|
|
|
|
|
|
|
|
if (rule->ignoreErrors)
|
|
|
|
ignoreErrors = rule->ignoreErrors;
|
|
|
|
|
|
|
|
switch (currentBackend) {
|
|
|
|
case VIR_FIREWALL_BACKEND_DIRECT:
|
|
|
|
if (virFirewallApplyRuleDirect(rule, ignoreErrors, &output) < 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case VIR_FIREWALL_BACKEND_FIREWALLD:
|
|
|
|
if (virFirewallApplyRuleFirewallD(rule, ignoreErrors, &output) < 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
2018-02-14 09:43:59 +00:00
|
|
|
|
|
|
|
case VIR_FIREWALL_BACKEND_AUTOMATIC:
|
|
|
|
case VIR_FIREWALL_BACKEND_LAST:
|
2013-03-04 16:30:40 +00:00
|
|
|
default:
|
2018-02-14 09:43:59 +00:00
|
|
|
virReportEnumRangeError(virFirewallBackend, currentBackend);
|
2013-03-04 16:30:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rule->queryCB && output) {
|
|
|
|
if (!(lines = virStringSplit(output, "\n", -1)))
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Invoking query %p with '%s'", rule->queryCB, output);
|
2018-12-04 16:33:28 +00:00
|
|
|
if (rule->queryCB(firewall, rule->layer, (const char *const *)lines, rule->queryOpaque) < 0)
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
if (firewall->err == ENOMEM) {
|
|
|
|
virReportOOMError();
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
if (firewall->err) {
|
|
|
|
virReportSystemError(firewall->err, "%s",
|
|
|
|
_("Unable to create rule"));
|
2018-07-24 15:52:17 +00:00
|
|
|
return -1;
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:17 +00:00
|
|
|
return 0;
|
2013-03-04 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFirewallApplyGroup(virFirewallPtr firewall,
|
|
|
|
size_t idx)
|
|
|
|
{
|
|
|
|
virFirewallGroupPtr group = firewall->groups[idx];
|
|
|
|
bool ignoreErrors = (group->actionFlags & VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
|
|
|
|
size_t i;
|
|
|
|
|
2017-09-25 10:43:33 +00:00
|
|
|
VIR_INFO("Starting transaction for firewall=%p group=%p flags=0x%x",
|
2014-11-11 12:34:57 +00:00
|
|
|
firewall, group, group->actionFlags);
|
2013-03-04 16:30:40 +00:00
|
|
|
firewall->currentGroup = idx;
|
|
|
|
group->addingRollback = false;
|
|
|
|
for (i = 0; i < group->naction; i++) {
|
|
|
|
if (virFirewallApplyRule(firewall,
|
|
|
|
group->action[i],
|
|
|
|
ignoreErrors) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virFirewallRollbackGroup(virFirewallPtr firewall,
|
|
|
|
size_t idx)
|
|
|
|
{
|
|
|
|
virFirewallGroupPtr group = firewall->groups[idx];
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
VIR_INFO("Starting rollback for group %p", group);
|
|
|
|
firewall->currentGroup = idx;
|
|
|
|
group->addingRollback = true;
|
|
|
|
for (i = 0; i < group->nrollback; i++) {
|
|
|
|
ignore_value(virFirewallApplyRule(firewall,
|
|
|
|
group->rollback[i],
|
|
|
|
true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virFirewallApply(virFirewallPtr firewall)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virMutexLock(&ruleLock);
|
|
|
|
|
2015-05-21 17:36:18 +00:00
|
|
|
if (currentBackend == VIR_FIREWALL_BACKEND_AUTOMATIC) {
|
|
|
|
/* a specific backend should have been set when the firewall
|
|
|
|
* object was created. If not, it means none was found.
|
|
|
|
*/
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Failed to initialize a valid firewall backend"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-03-04 16:30:40 +00:00
|
|
|
if (!firewall || firewall->err == ENOMEM) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (firewall->err) {
|
|
|
|
virReportSystemError(firewall->err, "%s",
|
|
|
|
_("Unable to create rule"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Applying groups for %p", firewall);
|
|
|
|
for (i = 0; i < firewall->ngroups; i++) {
|
|
|
|
if (virFirewallApplyGroup(firewall, i) < 0) {
|
2015-03-19 15:53:00 +00:00
|
|
|
VIR_DEBUG("Rolling back groups up to %zu for %p", i, firewall);
|
2013-03-04 16:30:40 +00:00
|
|
|
size_t first = i;
|
2018-07-24 15:52:17 +00:00
|
|
|
VIR_AUTOPTR(virError) saved_error = virSaveLastError();
|
2013-03-04 16:30:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look at any inheritance markers to figure out
|
|
|
|
* what the first rollback group we need to apply is
|
|
|
|
*/
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
VIR_DEBUG("Checking inheritance of group %zu", i - j);
|
|
|
|
if (firewall->groups[i - j]->rollbackFlags &
|
|
|
|
VIR_FIREWALL_ROLLBACK_INHERIT_PREVIOUS)
|
|
|
|
first = (i - j) - 1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now apply all rollback groups in order
|
|
|
|
*/
|
|
|
|
for (j = first; j <= i; j++) {
|
|
|
|
VIR_DEBUG("Rolling back group %zu", j);
|
|
|
|
virFirewallRollbackGroup(firewall, j);
|
|
|
|
}
|
|
|
|
|
|
|
|
virSetError(saved_error);
|
|
|
|
VIR_DEBUG("Done rolling back groups for %p", firewall);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Done applying groups for %p", firewall);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virMutexUnlock(&ruleLock);
|
|
|
|
return ret;
|
|
|
|
}
|