2010-04-26 13:52:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2010 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2010 Satoru SATOH <satoru.satoh@gmail.com>
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Based on iptables.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_PATHS_H
|
|
|
|
# include <paths.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "dnsmasq.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "logging.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NETWORK
|
|
|
|
#define DNSMASQ_HOSTSFILE_SUFFIX "hostsfile"
|
2011-06-24 10:04:39 +00:00
|
|
|
#define DNSMASQ_ADDNHOSTSFILE_SUFFIX "addnhosts"
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
dhcphostFree(dnsmasqDhcpHost *host)
|
|
|
|
{
|
|
|
|
VIR_FREE(host->host);
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:04:39 +00:00
|
|
|
static void
|
|
|
|
addnhostFree(dnsmasqAddnHost *host)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < host->nhostnames; i++)
|
|
|
|
VIR_FREE(host->hostnames[i]);
|
|
|
|
VIR_FREE(host->hostnames);
|
|
|
|
VIR_FREE(host->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addnhostsFree(dnsmasqAddnHostsfile *addnhostsfile)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (addnhostsfile->hosts) {
|
|
|
|
for (i = 0; i < addnhostsfile->nhosts; i++)
|
|
|
|
addnhostFree(&addnhostsfile->hosts[i]);
|
|
|
|
|
|
|
|
VIR_FREE(addnhostsfile->hosts);
|
|
|
|
|
|
|
|
addnhostsfile->nhosts = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(addnhostsfile->path);
|
|
|
|
|
|
|
|
VIR_FREE(addnhostsfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
|
|
|
|
virSocketAddr *ip,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
char *ipstr = NULL;
|
|
|
|
int idx = -1;
|
|
|
|
int i;
|
|
|
|
|
Santize naming of socket address APIs
The socket address APIs in src/util/network.h either take the
form virSocketAddrXXX, virSocketXXX or virSocketXXXAddr.
Sanitize this so everything is virSocketAddrXXXX, and ensure
that the virSocketAddr parameter is always the first one.
* src/util/network.c, src/util/network.h: Santize socket
address API naming
* src/conf/domain_conf.c, src/conf/network_conf.c,
src/conf/nwfilter_conf.c, src/network/bridge_driver.c,
src/nwfilter/nwfilter_ebiptables_driver.c,
src/nwfilter/nwfilter_learnipaddr.c,
src/qemu/qemu_command.c, src/rpc/virnetsocket.c,
src/util/dnsmasq.c, src/util/iptables.c,
src/util/virnetdev.c, src/vbox/vbox_tmpl.c: Update for
API renaming
2011-11-02 14:06:59 +00:00
|
|
|
if (!(ipstr = virSocketAddrFormat(ip)))
|
2011-06-24 10:04:39 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < addnhostsfile->nhosts; i++) {
|
|
|
|
if (STREQ((const char *)addnhostsfile->hosts[i].ip, (const char *)ipstr)) {
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
if (VIR_REALLOC_N(addnhostsfile->hosts, addnhostsfile->nhosts + 1) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
|
|
|
idx = addnhostsfile->nhosts;
|
|
|
|
if (VIR_ALLOC(addnhostsfile->hosts[idx].hostnames) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
|
|
|
if (virAsprintf(&addnhostsfile->hosts[idx].ip, "%s", ipstr) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
|
|
|
addnhostsfile->hosts[idx].nhostnames = 0;
|
|
|
|
addnhostsfile->nhosts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(addnhostsfile->hosts[idx].hostnames, addnhostsfile->hosts[idx].nhostnames + 1) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
|
|
|
if (virAsprintf(&addnhostsfile->hosts[idx].hostnames[addnhostsfile->hosts[idx].nhostnames], "%s", name) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
|
|
|
VIR_FREE(ipstr);
|
|
|
|
|
|
|
|
addnhostsfile->hosts[idx].nhostnames++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
alloc_error:
|
|
|
|
virReportOOMError();
|
|
|
|
VIR_FREE(ipstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dnsmasqAddnHostsfile *
|
|
|
|
addnhostsNew(const char *name,
|
|
|
|
const char *config_dir)
|
|
|
|
{
|
|
|
|
dnsmasqAddnHostsfile *addnhostsfile;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(addnhostsfile) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
addnhostsfile->hosts = NULL;
|
|
|
|
addnhostsfile->nhosts = 0;
|
|
|
|
|
|
|
|
if (virAsprintf(&addnhostsfile->path, "%s/%s.%s", config_dir, name,
|
|
|
|
DNSMASQ_ADDNHOSTSFILE_SUFFIX) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addnhostsfile;
|
|
|
|
|
|
|
|
error:
|
|
|
|
addnhostsFree(addnhostsfile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
addnhostsWrite(const char *path,
|
|
|
|
dnsmasqAddnHost *hosts,
|
|
|
|
unsigned int nhosts)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
FILE *f;
|
|
|
|
bool istmp = true;
|
|
|
|
unsigned int i, ii;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (nhosts == 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (virAsprintf(&tmp, "%s.new", path) < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (!(f = fopen(tmp, "w"))) {
|
|
|
|
istmp = false;
|
|
|
|
if (!(f = fopen(path, "w"))) {
|
2011-06-28 23:50:06 +00:00
|
|
|
rc = -errno;
|
2011-06-24 10:04:39 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nhosts; i++) {
|
|
|
|
if (fputs(hosts[i].ip, f) == EOF || fputc('\t', f) == EOF) {
|
2011-06-28 23:50:06 +00:00
|
|
|
rc = -errno;
|
2011-06-24 10:04:39 +00:00
|
|
|
VIR_FORCE_FCLOSE(f);
|
|
|
|
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ii = 0; ii < hosts[i].nhostnames; ii++) {
|
|
|
|
if (fputs(hosts[i].hostnames[ii], f) == EOF || fputc('\t', f) == EOF) {
|
2011-06-28 23:50:06 +00:00
|
|
|
rc = -errno;
|
2011-06-24 10:04:39 +00:00
|
|
|
VIR_FORCE_FCLOSE(f);
|
|
|
|
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fputc('\n', f) == EOF) {
|
2011-06-28 23:50:06 +00:00
|
|
|
rc = -errno;
|
2011-06-24 10:04:39 +00:00
|
|
|
VIR_FORCE_FCLOSE(f);
|
|
|
|
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_FCLOSE(f) == EOF) {
|
2011-06-28 23:50:06 +00:00
|
|
|
rc = -errno;
|
2011-06-24 10:04:39 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-06-28 23:50:06 +00:00
|
|
|
if (istmp && rename(tmp, path) < 0) {
|
|
|
|
rc = -errno;
|
|
|
|
unlink(tmp);
|
|
|
|
goto cleanup;
|
2011-06-24 10:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
addnhostsSave(dnsmasqAddnHostsfile *addnhostsfile)
|
|
|
|
{
|
|
|
|
int err = addnhostsWrite(addnhostsfile->path, addnhostsfile->hosts,
|
|
|
|
addnhostsfile->nhosts);
|
|
|
|
|
|
|
|
if (err < 0) {
|
2011-06-28 23:50:06 +00:00
|
|
|
virReportSystemError(-err, _("cannot write config file '%s'"),
|
2011-06-24 10:04:39 +00:00
|
|
|
addnhostsfile->path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
genericFileDelete(char *path)
|
|
|
|
{
|
|
|
|
if (!virFileExists(path))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (unlink(path) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot remove config file '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
static void
|
|
|
|
hostsfileFree(dnsmasqHostsfile *hostsfile)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (hostsfile->hosts) {
|
|
|
|
for (i = 0; i < hostsfile->nhosts; i++)
|
|
|
|
dhcphostFree(&hostsfile->hosts[i]);
|
|
|
|
|
|
|
|
VIR_FREE(hostsfile->hosts);
|
|
|
|
|
|
|
|
hostsfile->nhosts = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(hostsfile->path);
|
|
|
|
|
|
|
|
VIR_FREE(hostsfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileAdd(dnsmasqHostsfile *hostsfile,
|
|
|
|
const char *mac,
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
virSocketAddr *ip,
|
2010-04-26 13:52:02 +00:00
|
|
|
const char *name)
|
|
|
|
{
|
2010-10-22 16:22:20 +00:00
|
|
|
char *ipstr = NULL;
|
2010-04-26 13:52:02 +00:00
|
|
|
if (VIR_REALLOC_N(hostsfile->hosts, hostsfile->nhosts + 1) < 0)
|
|
|
|
goto alloc_error;
|
|
|
|
|
Santize naming of socket address APIs
The socket address APIs in src/util/network.h either take the
form virSocketAddrXXX, virSocketXXX or virSocketXXXAddr.
Sanitize this so everything is virSocketAddrXXXX, and ensure
that the virSocketAddr parameter is always the first one.
* src/util/network.c, src/util/network.h: Santize socket
address API naming
* src/conf/domain_conf.c, src/conf/network_conf.c,
src/conf/nwfilter_conf.c, src/network/bridge_driver.c,
src/nwfilter/nwfilter_ebiptables_driver.c,
src/nwfilter/nwfilter_learnipaddr.c,
src/qemu/qemu_command.c, src/rpc/virnetsocket.c,
src/util/dnsmasq.c, src/util/iptables.c,
src/util/virnetdev.c, src/vbox/vbox_tmpl.c: Update for
API renaming
2011-11-02 14:06:59 +00:00
|
|
|
if (!(ipstr = virSocketAddrFormat(ip)))
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
if (name) {
|
|
|
|
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s,%s",
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
mac, ipstr, name) < 0) {
|
2010-04-26 13:52:02 +00:00
|
|
|
goto alloc_error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s",
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
mac, ipstr) < 0) {
|
2010-04-26 13:52:02 +00:00
|
|
|
goto alloc_error;
|
|
|
|
}
|
|
|
|
}
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
VIR_FREE(ipstr);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
hostsfile->nhosts++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
alloc_error:
|
2010-05-03 10:26:42 +00:00
|
|
|
virReportOOMError();
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
VIR_FREE(ipstr);
|
2010-04-26 13:52:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dnsmasqHostsfile *
|
|
|
|
hostsfileNew(const char *name,
|
|
|
|
const char *config_dir)
|
|
|
|
{
|
|
|
|
dnsmasqHostsfile *hostsfile;
|
|
|
|
|
2010-05-03 10:26:42 +00:00
|
|
|
if (VIR_ALLOC(hostsfile) < 0) {
|
|
|
|
virReportOOMError();
|
2010-04-26 13:52:02 +00:00
|
|
|
return NULL;
|
2010-05-03 10:26:42 +00:00
|
|
|
}
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
hostsfile->hosts = NULL;
|
|
|
|
hostsfile->nhosts = 0;
|
|
|
|
|
|
|
|
if (virAsprintf(&hostsfile->path, "%s/%s.%s", config_dir, name,
|
2010-05-03 10:26:42 +00:00
|
|
|
DNSMASQ_HOSTSFILE_SUFFIX) < 0) {
|
|
|
|
virReportOOMError();
|
2010-04-26 13:52:02 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostsfile;
|
|
|
|
|
|
|
|
error:
|
|
|
|
hostsfileFree(hostsfile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileWrite(const char *path,
|
|
|
|
dnsmasqDhcpHost *hosts,
|
|
|
|
unsigned int nhosts)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
FILE *f;
|
|
|
|
bool istmp = true;
|
|
|
|
unsigned int i;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (nhosts == 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (virAsprintf(&tmp, "%s.new", path) < 0)
|
2011-04-27 18:11:14 +00:00
|
|
|
return -ENOMEM;
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (!(f = fopen(tmp, "w"))) {
|
|
|
|
istmp = false;
|
|
|
|
if (!(f = fopen(path, "w"))) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-04-26 13:52:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nhosts; i++) {
|
|
|
|
if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-11-17 02:13:29 +00:00
|
|
|
VIR_FORCE_FCLOSE(f);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (istmp)
|
|
|
|
unlink(tmp);
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-17 02:13:29 +00:00
|
|
|
if (VIR_FCLOSE(f) == EOF) {
|
2011-04-27 18:11:14 +00:00
|
|
|
rc = -errno;
|
2010-04-26 13:52:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-06-28 23:50:06 +00:00
|
|
|
if (istmp && rename(tmp, path) < 0) {
|
|
|
|
rc = -errno;
|
|
|
|
unlink(tmp);
|
|
|
|
goto cleanup;
|
2010-04-26 13:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(tmp);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hostsfileSave(dnsmasqHostsfile *hostsfile)
|
|
|
|
{
|
|
|
|
int err = hostsfileWrite(hostsfile->path, hostsfile->hosts,
|
2010-05-03 10:26:42 +00:00
|
|
|
hostsfile->nhosts);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
if (err < 0) {
|
2011-06-28 23:50:06 +00:00
|
|
|
virReportSystemError(-err, _("cannot write config file '%s'"),
|
2010-05-03 10:26:42 +00:00
|
|
|
hostsfile->path);
|
2010-04-26 13:52:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqContextNew:
|
|
|
|
*
|
|
|
|
* Create a new Dnsmasq context
|
|
|
|
*
|
|
|
|
* Returns a pointer to the new structure or NULL in case of error
|
|
|
|
*/
|
|
|
|
dnsmasqContext *
|
|
|
|
dnsmasqContextNew(const char *network_name,
|
|
|
|
const char *config_dir)
|
|
|
|
{
|
|
|
|
dnsmasqContext *ctx;
|
|
|
|
|
2010-05-03 10:26:42 +00:00
|
|
|
if (VIR_ALLOC(ctx) < 0) {
|
|
|
|
virReportOOMError();
|
2010-04-26 13:52:02 +00:00
|
|
|
return NULL;
|
2010-05-03 10:26:42 +00:00
|
|
|
}
|
2010-04-26 13:52:02 +00:00
|
|
|
|
network: Fix dnsmasq hostsfile creation logic and related tests
networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010).
It has a force flag. If the dnsmasq hostsfile already exists force
needs to be true to overwrite it. networkBuildDnsmasqArgv sets force
to false, networkDefine sets it to true. This results in the
hostsfile being written only in networkDefine in the common case.
If no error occurred networkSaveDnsmasqHostsfile returns true and
networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq
command line.
networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011)
to return a new dnsmasqContext instead of reusing one. This change broke
the logic of the force flag as now networkSaveDnsmasqHostsfile returns
NULL on error, but the early return -- if force was not set and the
hostsfile exists -- returns 0. This turned the early return in an error
case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option
anymore if the hostsfile already exists. It did because networkDefine
created the hostsfile already.
Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile
but didn't apply the force option correctly to the new addnhosts file.
Now force doesn't control an early return anymore, but influences the
handling of the hostsfile context creation and dnsmasqSave is always
called now. This commit also added test cases that reveal several
problems. First, the tests now calls functions that try to write the
dnsmasq config files to disk. If someone runs this tests as root this
might overwrite actively used dnsmasq config files, this is a no-go. Also
the tests depend on configure --localstatedir, this needs to be fixed as
well, because it makes the tests fail when localstatedir is different
from /var.
This patch does several things to fix this:
1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv
to the caller to separate the command line generation from the config
file writing. This makes the command line generation testable without the
risk of interfering with system files, because the tests just don't call
dnsmasqSave.
2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag
useless as the saving happens somewhere else now. This fixes the wrong
usage of the force flag in combination with then newly added addnhosts
file by removing the force flag.
3) Adapt the wrong test cases to the correct behavior, by adding the
missing --dhcp-hostsfile option. Both affected tests contain DHCP host
elements but missed the necessary --dhcp-hostsfile option.
4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile,
because it doesn't save the dnsmasqContext anymore.
5) Move all directory creations in dnsmasq context handling code from
the *New functions to dnsmasqSave to avoid directory creations in system
paths in the test cases.
6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext
anymore the test case can create one with the localstatedir that is
expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
|
|
|
if (!(ctx->config_dir = strdup(config_dir))) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
if (!(ctx->hostsfile = hostsfileNew(network_name, config_dir)))
|
|
|
|
goto error;
|
2011-06-24 10:04:39 +00:00
|
|
|
if (!(ctx->addnhostsfile = addnhostsNew(network_name, config_dir)))
|
|
|
|
goto error;
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
error:
|
|
|
|
dnsmasqContextFree(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqContextFree:
|
|
|
|
* @ctx: pointer to the dnsmasq context
|
|
|
|
*
|
|
|
|
* Free the resources associated with an dnsmasq context
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dnsmasqContextFree(dnsmasqContext *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
network: Fix dnsmasq hostsfile creation logic and related tests
networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010).
It has a force flag. If the dnsmasq hostsfile already exists force
needs to be true to overwrite it. networkBuildDnsmasqArgv sets force
to false, networkDefine sets it to true. This results in the
hostsfile being written only in networkDefine in the common case.
If no error occurred networkSaveDnsmasqHostsfile returns true and
networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq
command line.
networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011)
to return a new dnsmasqContext instead of reusing one. This change broke
the logic of the force flag as now networkSaveDnsmasqHostsfile returns
NULL on error, but the early return -- if force was not set and the
hostsfile exists -- returns 0. This turned the early return in an error
case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option
anymore if the hostsfile already exists. It did because networkDefine
created the hostsfile already.
Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile
but didn't apply the force option correctly to the new addnhosts file.
Now force doesn't control an early return anymore, but influences the
handling of the hostsfile context creation and dnsmasqSave is always
called now. This commit also added test cases that reveal several
problems. First, the tests now calls functions that try to write the
dnsmasq config files to disk. If someone runs this tests as root this
might overwrite actively used dnsmasq config files, this is a no-go. Also
the tests depend on configure --localstatedir, this needs to be fixed as
well, because it makes the tests fail when localstatedir is different
from /var.
This patch does several things to fix this:
1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv
to the caller to separate the command line generation from the config
file writing. This makes the command line generation testable without the
risk of interfering with system files, because the tests just don't call
dnsmasqSave.
2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag
useless as the saving happens somewhere else now. This fixes the wrong
usage of the force flag in combination with then newly added addnhosts
file by removing the force flag.
3) Adapt the wrong test cases to the correct behavior, by adding the
missing --dhcp-hostsfile option. Both affected tests contain DHCP host
elements but missed the necessary --dhcp-hostsfile option.
4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile,
because it doesn't save the dnsmasqContext anymore.
5) Move all directory creations in dnsmasq context handling code from
the *New functions to dnsmasqSave to avoid directory creations in system
paths in the test cases.
6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext
anymore the test case can create one with the localstatedir that is
expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
|
|
|
VIR_FREE(ctx->config_dir);
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
if (ctx->hostsfile)
|
|
|
|
hostsfileFree(ctx->hostsfile);
|
2011-06-24 10:04:39 +00:00
|
|
|
if (ctx->addnhostsfile)
|
|
|
|
addnhostsFree(ctx->addnhostsfile);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
VIR_FREE(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqAddDhcpHost:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
* @mac: pointer to the string contains mac address of the host
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
* @ip: pointer to the socket address contains ip of the host
|
2010-04-26 13:52:02 +00:00
|
|
|
* @name: pointer to the string contains hostname of the host or NULL
|
|
|
|
*
|
|
|
|
* Add dhcp-host entry.
|
|
|
|
*/
|
2011-06-28 12:07:46 +00:00
|
|
|
int
|
2010-04-26 13:52:02 +00:00
|
|
|
dnsmasqAddDhcpHost(dnsmasqContext *ctx,
|
|
|
|
const char *mac,
|
Convert virNetwork to use virSocketAddr everywhere
Instead of storing the IP address string in virNetwork related
structs, store the parsed virSocketAddr. This will make it
easier to add IPv6 support in the future, by letting driver
code directly check what address family is present
* src/conf/network_conf.c, src/conf/network_conf.h,
src/network/bridge_driver.c: Convert to use virSocketAddr
in virNetwork, instead of char *.
* src/util/bridge.c, src/util/bridge.h,
src/util/dnsmasq.c, src/util/dnsmasq.h,
src/util/iptables.c, src/util/iptables.h: Convert to
take a virSocketAddr instead of char * for any IP
address parameters
* src/util/network.h: Add macros to determine if an address
is set, and what address family is set.
2010-10-21 12:14:33 +00:00
|
|
|
virSocketAddr *ip,
|
2010-04-26 13:52:02 +00:00
|
|
|
const char *name)
|
|
|
|
{
|
2011-06-28 12:07:46 +00:00
|
|
|
return hostsfileAdd(ctx->hostsfile, mac, ip, name);
|
2010-04-26 13:52:02 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:04:39 +00:00
|
|
|
/*
|
|
|
|
* dnsmasqAddHost:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
* @ip: pointer to the socket address contains ip of the host
|
|
|
|
* @name: pointer to the string contains hostname of the host
|
|
|
|
*
|
|
|
|
* Add additional host entry.
|
|
|
|
*/
|
|
|
|
|
2011-06-28 12:07:46 +00:00
|
|
|
int
|
2011-06-24 10:04:39 +00:00
|
|
|
dnsmasqAddHost(dnsmasqContext *ctx,
|
|
|
|
virSocketAddr *ip,
|
|
|
|
const char *name)
|
|
|
|
{
|
2011-06-28 12:07:46 +00:00
|
|
|
return addnhostsAdd(ctx->addnhostsfile, ip, name);
|
2011-06-24 10:04:39 +00:00
|
|
|
}
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
/**
|
|
|
|
* dnsmasqSave:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
*
|
|
|
|
* Saves all the configurations associated with a context to disk.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dnsmasqSave(const dnsmasqContext *ctx)
|
|
|
|
{
|
2011-06-24 10:04:39 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2011-07-05 21:02:53 +00:00
|
|
|
if (virFileMakePath(ctx->config_dir) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot create config directory '%s'"),
|
network: Fix dnsmasq hostsfile creation logic and related tests
networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010).
It has a force flag. If the dnsmasq hostsfile already exists force
needs to be true to overwrite it. networkBuildDnsmasqArgv sets force
to false, networkDefine sets it to true. This results in the
hostsfile being written only in networkDefine in the common case.
If no error occurred networkSaveDnsmasqHostsfile returns true and
networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq
command line.
networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011)
to return a new dnsmasqContext instead of reusing one. This change broke
the logic of the force flag as now networkSaveDnsmasqHostsfile returns
NULL on error, but the early return -- if force was not set and the
hostsfile exists -- returns 0. This turned the early return in an error
case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option
anymore if the hostsfile already exists. It did because networkDefine
created the hostsfile already.
Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile
but didn't apply the force option correctly to the new addnhosts file.
Now force doesn't control an early return anymore, but influences the
handling of the hostsfile context creation and dnsmasqSave is always
called now. This commit also added test cases that reveal several
problems. First, the tests now calls functions that try to write the
dnsmasq config files to disk. If someone runs this tests as root this
might overwrite actively used dnsmasq config files, this is a no-go. Also
the tests depend on configure --localstatedir, this needs to be fixed as
well, because it makes the tests fail when localstatedir is different
from /var.
This patch does several things to fix this:
1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv
to the caller to separate the command line generation from the config
file writing. This makes the command line generation testable without the
risk of interfering with system files, because the tests just don't call
dnsmasqSave.
2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag
useless as the saving happens somewhere else now. This fixes the wrong
usage of the force flag in combination with then newly added addnhosts
file by removing the force flag.
3) Adapt the wrong test cases to the correct behavior, by adding the
missing --dhcp-hostsfile option. Both affected tests contain DHCP host
elements but missed the necessary --dhcp-hostsfile option.
4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile,
because it doesn't save the dnsmasqContext anymore.
5) Move all directory creations in dnsmasq context handling code from
the *New functions to dnsmasqSave to avoid directory creations in system
paths in the test cases.
6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext
anymore the test case can create one with the localstatedir that is
expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
|
|
|
ctx->config_dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
if (ctx->hostsfile)
|
2011-06-24 10:04:39 +00:00
|
|
|
ret = hostsfileSave(ctx->hostsfile);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (ctx->addnhostsfile)
|
|
|
|
ret = addnhostsSave(ctx->addnhostsfile);
|
|
|
|
}
|
2010-04-26 13:52:02 +00:00
|
|
|
|
2011-06-24 10:04:39 +00:00
|
|
|
return ret;
|
2010-04-26 13:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqDelete:
|
|
|
|
* @ctx: pointer to the dnsmasq context for each network
|
|
|
|
*
|
|
|
|
* Delete all the configuration files associated with a context.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dnsmasqDelete(const dnsmasqContext *ctx)
|
|
|
|
{
|
2011-06-24 10:04:39 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2010-04-26 13:52:02 +00:00
|
|
|
if (ctx->hostsfile)
|
2011-06-24 10:04:39 +00:00
|
|
|
ret = genericFileDelete(ctx->hostsfile->path);
|
|
|
|
if (ctx->addnhostsfile)
|
|
|
|
ret = genericFileDelete(ctx->addnhostsfile->path);
|
2010-04-26 13:52:02 +00:00
|
|
|
|
2011-06-24 10:04:39 +00:00
|
|
|
return ret;
|
2010-04-26 13:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dnsmasqReload:
|
|
|
|
* @pid: the pid of the target dnsmasq process
|
|
|
|
*
|
|
|
|
* Reloads all the configurations associated to a context
|
|
|
|
*/
|
|
|
|
int
|
2010-05-03 18:23:50 +00:00
|
|
|
dnsmasqReload(pid_t pid ATTRIBUTE_UNUSED)
|
2010-04-26 13:52:02 +00:00
|
|
|
{
|
2010-05-03 18:23:50 +00:00
|
|
|
#ifndef WIN32
|
2010-04-26 13:52:02 +00:00
|
|
|
if (kill(pid, SIGHUP) != 0) {
|
|
|
|
virReportSystemError(errno,
|
2010-05-03 18:23:50 +00:00
|
|
|
_("Failed to make dnsmasq (PID: %d) reload config files."),
|
2010-04-26 13:52:02 +00:00
|
|
|
pid);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-03 18:23:50 +00:00
|
|
|
#endif /* WIN32 */
|
2010-04-26 13:52:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|