leaseshelper.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * leaseshelper.c: Helper program to create custom leases file
 *
 * 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/>.
 *
 * For IPv6 support, use dnsmasq >= 2.67
 */

#include <config.h>


#include "virthread.h"
#include "virfile.h"
#include "virpidfile.h"
#include "virstring.h"
#include "virerror.h"
#include "viralloc.h"
#include "virjson.h"
34
#include "virlease.h"
35
#include "virenum.h"
36
#include "configmake.h"
37
#include "virgettext.h"
38 39 40 41 42 43 44 45 46 47 48 49

#define VIR_FROM_THIS VIR_FROM_NETWORK

static const char *program_name;

/* Display version information. */
static void
helperVersion(const char *argv0)
{
    printf("%s (%s) %s\n", argv0, PACKAGE_NAME, PACKAGE_VERSION);
}

50
G_GNUC_NORETURN static void
51 52 53 54 55
usage(int status)
{
    if (status) {
        fprintf(stderr, _("%s: try --help for more details\n"), program_name);
    } else {
56
        printf(_("Usage: %s add|old|del|init mac|clientid ip [hostname]\n"
57 58 59 60 61 62 63 64 65 66 67 68
                 "Designed for use with 'dnsmasq --dhcp-script'\n"
                 "Refer to man page of dnsmasq for more details'\n"),
               program_name);
    }
    exit(status);
}

/* Flags denoting actions for a lease */
enum virLeaseActionFlags {
    VIR_LEASE_ACTION_ADD,       /* Create new lease */
    VIR_LEASE_ACTION_OLD,       /* Lease already exists, renew it */
    VIR_LEASE_ACTION_DEL,       /* Delete the lease */
69
    VIR_LEASE_ACTION_INIT,      /* Tell dnsmasq of existing leases on restart */
70 71 72 73 74 75

    VIR_LEASE_ACTION_LAST
};

VIR_ENUM_DECL(virLeaseAction);

76 77
VIR_ENUM_IMPL(virLeaseAction,
              VIR_LEASE_ACTION_LAST,
78 79
              "add", "old", "del", "init",
);
80 81 82 83 84 85 86 87

int
main(int argc, char **argv)
{
    char *pid_file = NULL;
    char *custom_lease_file = NULL;
    const char *ip = NULL;
    const char *mac = NULL;
88
    const char *leases_str = NULL;
89 90 91 92
    const char *iaid = getenv("DNSMASQ_IAID");
    const char *clientid = getenv("DNSMASQ_CLIENT_ID");
    const char *interface = getenv("DNSMASQ_INTERFACE");
    const char *hostname = getenv("DNSMASQ_SUPPLIED_HOSTNAME");
93
    char *server_duid = NULL;
94 95 96 97 98 99 100 101 102 103 104 105
    int action = -1;
    int pid_file_fd = -1;
    int rv = EXIT_FAILURE;
    bool delete = false;
    virJSONValuePtr lease_new = NULL;
    virJSONValuePtr leases_array_new = NULL;

    virSetErrorFunc(NULL, NULL);
    virSetErrorLogPriorityFunc(NULL);

    program_name = argv[0];

106
    if (virGettextInitialize() < 0 ||
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        virErrorInitialize() < 0) {
        fprintf(stderr, _("%s: initialization failed\n"), program_name);
        exit(EXIT_FAILURE);
    }

    /* Doesn't hurt to check */
    if (argc > 1) {
        if (STREQ(argv[1], "--help"))
            usage(EXIT_SUCCESS);

        if (STREQ(argv[1], "--version")) {
            helperVersion(argv[0]);
            exit(EXIT_SUCCESS);
        }
    }

123
    if (argc != 4 && argc != 5 && argc != 2) {
124 125 126 127 128
        /* Refer man page of dnsmasq --dhcp-script for more details */
        usage(EXIT_FAILURE);
    }

    /* Make sure dnsmasq knows the interface. The interface name is not known
129 130 131 132
     * via env variable set by dnsmasq when dnsmasq (re)starts and throws 'del'
     * events for expired leases. So, libvirtd sets another env var for this
     * purpose */
    if (!interface &&
133
        !(interface = getenv("VIR_BRIDGE_NAME")))
134 135 136 137
        goto cleanup;

    ip = argv[3];
    mac = argv[2];
P
Peter Krempa 已提交
138 139 140 141 142

    if ((action = virLeaseActionTypeFromString(argv[1])) < 0) {
        fprintf(stderr, _("Unsupported action: %s\n"), argv[1]);
        exit(EXIT_FAILURE);
    }
143 144 145 146 147 148

    /* In case hostname is known, it is the 5th argument */
    if (argc == 5)
        hostname = argv[4];

    /* Check if it is an IPv6 lease */
149
    if (iaid) {
150
        mac = getenv("DNSMASQ_MAC");
151 152 153
        clientid = argv[2];
    }

154
    server_duid = g_strdup(getenv("DNSMASQ_SERVER_DUID"));
155

156 157 158 159 160
    if (virAsprintf(&custom_lease_file,
                    LOCALSTATEDIR "/lib/libvirt/dnsmasq/%s.status",
                    interface) < 0)
        goto cleanup;

161
    pid_file = g_strdup(RUNSTATEDIR "/leaseshelper.pid");
162 163

    /* Try to claim the pidfile, exiting if we can't */
164
    if ((pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0)
165 166 167 168 169 170 171
        goto cleanup;

    /* Since interfaces can be hot plugged, we need to make sure that the
     * corresponding custom lease file exists. If not, 'touch' it */
    if (virFileTouch(custom_lease_file, 0644) < 0)
        goto cleanup;

P
Peter Krempa 已提交
172
    switch ((enum virLeaseActionFlags) action) {
173
    case VIR_LEASE_ACTION_ADD:
P
Peter Krempa 已提交
174
    case VIR_LEASE_ACTION_OLD:
J
Ján Tomko 已提交
175
        /* Create new lease */
176
        if (virLeaseNew(&lease_new, mac, clientid, ip, hostname, iaid, server_duid) < 0)
J
Ján Tomko 已提交
177
            goto cleanup;
178 179 180 181 182 183 184 185 186 187 188 189 190
        /* Custom ipv6 leases *will not* be created if the env-var DNSMASQ_MAC
         * is not set. In the special case, when the $(interface).status file
         * is not already present and dnsmasq is (re)started, the corresponding
         * ipv6 custom lease will be created only when the guest sends the
         * 'old' action for its existing ipv6 interfaces.
         *
         * According to rfc3315, the combination of DUID and IAID can be used
         * to uniquely identify each ipv6 guest interface. So, in future, if
         * we introduce virNetworkGetDHCPLeaseBy(IAID|DUID|IAID+DUID) for ipv6
         * interfaces, then, the following if condition won't be required, as
         * the new lease will be created irrespective of whether the MACID is
         * known or not.
         */
J
Ján Tomko 已提交
191
        if (!lease_new)
P
Peter Krempa 已提交
192 193
            break;

194
        G_GNUC_FALLTHROUGH;
P
Peter Krempa 已提交
195
    case VIR_LEASE_ACTION_DEL:
196
        /* Delete the corresponding lease, if it already exists */
P
Peter Krempa 已提交
197 198 199 200 201 202
        delete = true;
        break;

    case VIR_LEASE_ACTION_INIT:
    case VIR_LEASE_ACTION_LAST:
        break;
203
    }
204 205 206 207 208 209 210

    if (!(leases_array_new = virJSONValueNewArray())) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("failed to create json"));
        goto cleanup;
    }

211 212 213
    if (virLeaseReadCustomLeaseFile(leases_array_new, custom_lease_file,
                                    delete ? ip : NULL, &server_duid) < 0)
        goto cleanup;
214

215 216
    switch ((enum virLeaseActionFlags) action) {
    case VIR_LEASE_ACTION_INIT:
217 218
        if (virLeasePrintLeases(leases_array_new, server_duid) < 0)
            goto cleanup;
219 220 221 222 223

        break;

    case VIR_LEASE_ACTION_OLD:
    case VIR_LEASE_ACTION_ADD:
224
        if (lease_new && virJSONValueArrayAppend(leases_array_new, lease_new) < 0) {
225 226 227 228
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to create json"));
            goto cleanup;
        }
P
Pavel Hrdina 已提交
229
        lease_new = NULL;
230

231
        G_GNUC_FALLTHROUGH;
P
Peter Krempa 已提交
232
    case VIR_LEASE_ACTION_DEL:
233 234 235 236 237
        if (!(leases_str = virJSONValueToString(leases_array_new, true))) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("empty json array"));
            goto cleanup;
        }
238

239
        /* Write to file */
240
        if (virFileRewriteStr(custom_lease_file, 0644, leases_str) < 0)
241
            goto cleanup;
P
Peter Krempa 已提交
242 243 244 245
        break;

    case VIR_LEASE_ACTION_LAST:
        break;
246
    }
247 248 249 250 251 252 253 254

    rv = EXIT_SUCCESS;

 cleanup:
    if (pid_file_fd != -1)
        virPidFileReleasePath(pid_file, pid_file_fd);

    VIR_FREE(pid_file);
255
    VIR_FREE(server_duid);
256 257 258 259 260 261
    VIR_FREE(custom_lease_file);
    virJSONValueFree(lease_new);
    virJSONValueFree(leases_array_new);

    return rv;
}