2016-02-14 06:50:12 +00:00
|
|
|
/*
|
|
|
|
* virlease.c: Leases file handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2014 Nehal J Wani
|
|
|
|
*
|
|
|
|
* 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 "virlease.h"
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "viralloc.h"
|
2016-02-14 10:38:37 +00:00
|
|
|
#include "virutil.h"
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NETWORK
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VIR_NETWORK_DHCP_LEASE_FILE_SIZE_MAX:
|
|
|
|
*
|
|
|
|
* Macro providing the upper limit on the size of leases file
|
|
|
|
*/
|
|
|
|
#define VIR_NETWORK_DHCP_LEASE_FILE_SIZE_MAX (32 * 1024 * 1024)
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virLeaseReadCustomLeaseFile(virJSONValuePtr leases_array_new,
|
|
|
|
const char *custom_lease_file,
|
|
|
|
const char *ip_to_delete,
|
|
|
|
char **server_duid)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *lease_entries = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) leases_array = NULL;
|
2016-02-14 06:50:12 +00:00
|
|
|
int custom_lease_file_len = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Read entire contents */
|
|
|
|
if ((custom_lease_file_len = virFileReadAll(custom_lease_file,
|
|
|
|
VIR_NETWORK_DHCP_LEASE_FILE_SIZE_MAX,
|
|
|
|
&lease_entries)) < 0) {
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for previous leases */
|
2018-07-24 15:52:42 +00:00
|
|
|
if (custom_lease_file_len == 0)
|
|
|
|
return 0;
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
if (!(leases_array = virJSONValueFromString(lease_entries))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("invalid json in file: %s, rewriting it"),
|
|
|
|
custom_lease_file);
|
2018-07-24 15:52:42 +00:00
|
|
|
return 0;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!virJSONValueIsArray(leases_array)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("couldn't fetch array of leases"));
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < virJSONValueArraySize(leases_array)) {
|
2020-12-18 15:09:09 +00:00
|
|
|
virJSONValuePtr lease_tmp = virJSONValueArrayGet(leases_array, i);
|
|
|
|
long long expirytime;
|
|
|
|
const char *ip_tmp = NULL;
|
|
|
|
|
|
|
|
if (!lease_tmp) {
|
2016-02-14 06:50:12 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to parse json"));
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address")) ||
|
|
|
|
(virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", &expirytime) < 0)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to parse json"));
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether lease has to be included or not */
|
|
|
|
if (ip_to_delete && STREQ(ip_tmp, ip_to_delete)) {
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:49:37 +00:00
|
|
|
if (server_duid && strchr(ip_tmp, ':')) {
|
2020-12-18 15:09:09 +00:00
|
|
|
const char *server_duid_tmp = NULL;
|
|
|
|
|
2016-02-14 06:50:12 +00:00
|
|
|
/* This is an ipv6 lease */
|
2020-12-18 15:09:09 +00:00
|
|
|
if ((server_duid_tmp = virJSONValueObjectGetString(lease_tmp, "server-duid"))) {
|
2019-10-18 13:08:21 +00:00
|
|
|
if (!*server_duid)
|
|
|
|
*server_duid = g_strdup(server_duid_tmp);
|
2016-02-14 06:50:12 +00:00
|
|
|
} else {
|
|
|
|
/* Inject server-duid into those ipv6 leases which
|
|
|
|
* didn't have it previously, for example, those
|
|
|
|
* created by leaseshelper from libvirt 1.2.6 */
|
|
|
|
if (virJSONValueObjectAppendString(lease_tmp, "server-duid", *server_duid) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move old lease to new array */
|
|
|
|
if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to create json"));
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ignore_value(virJSONValueArraySteal(leases_array, i));
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:42 +00:00
|
|
|
return 0;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virLeasePrintLeases(virJSONValuePtr leases_array_new,
|
|
|
|
const char *server_duid)
|
|
|
|
{
|
|
|
|
virJSONValuePtr lease_tmp = NULL;
|
|
|
|
const char *ip_tmp = NULL;
|
|
|
|
long long expirytime = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Man page of dnsmasq says: the script (helper program, in our case)
|
|
|
|
* should write the saved state of the lease database, in dnsmasq
|
|
|
|
* leasefile format, to stdout and exit with zero exit code, when
|
|
|
|
* called with argument init. Format:
|
|
|
|
* $expirytime $mac $ip $hostname $clientid # For all ipv4 leases
|
|
|
|
* duid $server-duid # If DHCPv6 is present
|
|
|
|
* $expirytime $iaid $ip $hostname $clientduid # For all ipv6 leases */
|
|
|
|
|
|
|
|
/* Traversing the ipv4 leases */
|
|
|
|
for (i = 0; i < virJSONValueArraySize(leases_array_new); i++) {
|
|
|
|
lease_tmp = virJSONValueArrayGet(leases_array_new, i);
|
|
|
|
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to parse json"));
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
if (!strchr(ip_tmp, ':')) {
|
|
|
|
if (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time",
|
|
|
|
&expirytime) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
printf("%lld %s %s %s %s\n",
|
|
|
|
expirytime,
|
|
|
|
virJSONValueObjectGetString(lease_tmp, "mac-address"),
|
|
|
|
virJSONValueObjectGetString(lease_tmp, "ip-address"),
|
2019-02-12 16:15:17 +00:00
|
|
|
NULLSTR_STAR(virJSONValueObjectGetString(lease_tmp, "hostname")),
|
|
|
|
NULLSTR_STAR(virJSONValueObjectGetString(lease_tmp, "client-id")));
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Traversing the ipv6 leases */
|
|
|
|
if (server_duid) {
|
|
|
|
printf("duid %s\n", server_duid);
|
|
|
|
for (i = 0; i < virJSONValueArraySize(leases_array_new); i++) {
|
|
|
|
lease_tmp = virJSONValueArrayGet(leases_array_new, i);
|
|
|
|
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to parse json"));
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
if (strchr(ip_tmp, ':')) {
|
|
|
|
if (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time",
|
|
|
|
&expirytime) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
printf("%lld %s %s %s %s\n",
|
|
|
|
expirytime,
|
|
|
|
virJSONValueObjectGetString(lease_tmp, "iaid"),
|
|
|
|
virJSONValueObjectGetString(lease_tmp, "ip-address"),
|
2019-02-12 16:15:17 +00:00
|
|
|
NULLSTR_STAR(virJSONValueObjectGetString(lease_tmp, "hostname")),
|
|
|
|
NULLSTR_STAR(virJSONValueObjectGetString(lease_tmp, "client-id")));
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:42 +00:00
|
|
|
return 0;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virLeaseNew(virJSONValuePtr *lease_ret,
|
|
|
|
const char *mac,
|
|
|
|
const char *clientid,
|
|
|
|
const char *ip,
|
|
|
|
const char *hostname,
|
|
|
|
const char *iaid,
|
|
|
|
const char *server_duid)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virJSONValue) lease_new = NULL;
|
2019-08-01 12:35:56 +00:00
|
|
|
const char *exptime_tmp = getenv("DNSMASQ_LEASE_EXPIRES");
|
2016-02-14 06:50:12 +00:00
|
|
|
long long expirytime = 0;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *exptime = NULL;
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
/* In case hostname is still unknown, use the last known one */
|
|
|
|
if (!hostname)
|
2019-08-01 12:35:56 +00:00
|
|
|
hostname = getenv("DNSMASQ_OLD_HOSTNAME");
|
2016-02-14 06:50:12 +00:00
|
|
|
|
2018-07-24 15:52:42 +00:00
|
|
|
if (!mac)
|
|
|
|
return 0;
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
if (exptime_tmp) {
|
2019-10-20 11:49:46 +00:00
|
|
|
exptime = g_strdup(exptime_tmp);
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
/* Removed extraneous trailing space in DNSMASQ_LEASE_EXPIRES
|
|
|
|
* (dnsmasq < 2.52) */
|
2020-12-18 15:09:10 +00:00
|
|
|
virTrimSpaces(exptime, NULL);
|
2016-02-14 06:50:12 +00:00
|
|
|
|
2020-12-18 15:09:11 +00:00
|
|
|
if (virStrToLong_ll(exptime, NULL, 10, &expirytime) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to convert lease expiry time to long long: %s"),
|
|
|
|
NULLSTR(exptime));
|
|
|
|
return -1;
|
|
|
|
}
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new lease */
|
2020-03-04 09:04:33 +00:00
|
|
|
lease_new = virJSONValueNewObject();
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
if (iaid && virJSONValueObjectAppendString(lease_new, "iaid", iaid) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
if (ip && virJSONValueObjectAppendString(lease_new, "ip-address", ip) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2020-08-02 20:57:29 +00:00
|
|
|
if (virJSONValueObjectAppendString(lease_new, "mac-address", mac) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
if (hostname && virJSONValueObjectAppendString(lease_new, "hostname", hostname) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
if (clientid && virJSONValueObjectAppendString(lease_new, "client-id", clientid) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
if (server_duid && virJSONValueObjectAppendString(lease_new, "server-duid", server_duid) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2020-12-18 15:09:11 +00:00
|
|
|
if (virJSONValueObjectAppendNumberLong(lease_new, "expiry-time", expirytime) < 0)
|
2018-07-24 15:52:42 +00:00
|
|
|
return -1;
|
2016-02-14 06:50:12 +00:00
|
|
|
|
|
|
|
*lease_ret = lease_new;
|
|
|
|
lease_new = NULL;
|
2018-07-24 15:52:42 +00:00
|
|
|
return 0;
|
2016-02-14 06:50:12 +00:00
|
|
|
}
|