Blame src/dhcp/nm-dhcp-dhclient.c

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2005 - 2012 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include <config.h>
Packit 5756e2
#define __CONFIG_H__
Packit 5756e2
Packit 5756e2
#define _XOPEN_SOURCE
Packit 5756e2
#include <time.h>
Packit 5756e2
#undef _XOPEN_SOURCE
Packit 5756e2
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#if WITH_DHCLIENT
Packit 5756e2
Packit Service a1bd4f
    #include <stdlib.h>
Packit Service a1bd4f
    #include <unistd.h>
Packit Service a1bd4f
    #include <stdio.h>
Packit Service a1bd4f
    #include <netinet/in.h>
Packit Service a1bd4f
    #include <arpa/inet.h>
Packit Service a1bd4f
    #include <ctype.h>
Packit 5756e2
Packit Service a1bd4f
    #include "nm-glib-aux/nm-dedup-multi.h"
Packit 5756e2
Packit Service a1bd4f
    #include "nm-utils.h"
Packit Service a1bd4f
    #include "nm-dhcp-dhclient-utils.h"
Packit Service a1bd4f
    #include "nm-dhcp-manager.h"
Packit Service a1bd4f
    #include "NetworkManagerUtils.h"
Packit Service a1bd4f
    #include "nm-dhcp-listener.h"
Packit Service a1bd4f
    #include "nm-dhcp-client-logging.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static const char *
Packit Service a1bd4f
_addr_family_to_path_part(int addr_family)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert(NM_IN_SET(addr_family, AF_INET, AF_INET6));
Packit Service a1bd4f
    return (addr_family == AF_INET6) ? "6" : "";
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
    #define NM_TYPE_DHCP_DHCLIENT (nm_dhcp_dhclient_get_type())
Packit Service a1bd4f
    #define NM_DHCP_DHCLIENT(obj) \
Packit Service a1bd4f
        (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_DHCP_DHCLIENT, NMDhcpDhclient))
Packit Service a1bd4f
    #define NM_DHCP_DHCLIENT_CLASS(klass) \
Packit Service a1bd4f
        (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_DHCP_DHCLIENT, NMDhcpDhclientClass))
Packit Service a1bd4f
    #define NM_IS_DHCP_DHCLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_DHCP_DHCLIENT))
Packit Service a1bd4f
    #define NM_IS_DHCP_DHCLIENT_CLASS(klass) \
Packit Service a1bd4f
        (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_DHCP_DHCLIENT))
Packit Service a1bd4f
    #define NM_DHCP_DHCLIENT_GET_CLASS(obj) \
Packit Service a1bd4f
        (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_DHCP_DHCLIENT, NMDhcpDhclientClass))
Packit Service a1bd4f
Packit Service a1bd4f
typedef struct _NMDhcpDhclient      NMDhcpDhclient;
Packit 5756e2
typedef struct _NMDhcpDhclientClass NMDhcpDhclientClass;
Packit 5756e2
Packit Service a1bd4f
static GType nm_dhcp_dhclient_get_type(void);
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    char *          conf_file;
Packit Service a1bd4f
    const char *    def_leasefile;
Packit Service a1bd4f
    char *          lease_file;
Packit Service a1bd4f
    char *          pid_file;
Packit Service a1bd4f
    NMDhcpListener *dhcp_listener;
Packit 5756e2
} NMDhcpDhclientPrivate;
Packit 5756e2
Packit 5756e2
struct _NMDhcpDhclient {
Packit Service a1bd4f
    NMDhcpClient          parent;
Packit Service a1bd4f
    NMDhcpDhclientPrivate _priv;
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
struct _NMDhcpDhclientClass {
Packit Service a1bd4f
    NMDhcpClientClass parent;
Packit 5756e2
};
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NMDhcpDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT)
Packit 5756e2
Packit Service a1bd4f
    #define NM_DHCP_DHCLIENT_GET_PRIVATE(self) \
Packit Service a1bd4f
        _NM_GET_PRIVATE(self, NMDhcpDhclient, NM_IS_DHCP_DHCLIENT)
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static const char *
Packit Service a1bd4f
nm_dhcp_dhclient_get_path(void)
Packit 5756e2
{
Packit Service a1bd4f
    return nm_utils_find_helper("dhclient", DHCLIENT_PATH, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * get_dhclient_leasefile():
Packit 5756e2
 * @addr_family: AF_INET or AF_INET6
Packit 5756e2
 * @iface: the interface name of the device on which DHCP will be done
Packit 5756e2
 * @uuid: the connection UUID to which the returned lease should belong
Packit 5756e2
 * @out_preferred_path: on return, the "most preferred" leasefile path
Packit 5756e2
 *
Packit 5756e2
 * Returns the path of an existing leasefile (if any) for this interface and
Packit 5756e2
 * connection UUID.  Also returns the "most preferred" leasefile path, which
Packit 5756e2
 * may be different than any found leasefile.
Packit 5756e2
 *
Packit 5756e2
 * Returns: an existing leasefile, or %NULL if no matching leasefile could be found
Packit 5756e2
 */
Packit 5756e2
static char *
Packit Service a1bd4f
get_dhclient_leasefile(int         addr_family,
Packit Service a1bd4f
                       const char *iface,
Packit Service a1bd4f
                       const char *uuid,
Packit Service a1bd4f
                       char **     out_preferred_path)
Packit 5756e2
{
Packit Service a1bd4f
    gs_free char *path = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    if (nm_dhcp_utils_get_leasefile_path(addr_family, "dhclient", iface, uuid, &path)) {
Packit Service a1bd4f
        NM_SET_OUT(out_preferred_path, g_strdup(path));
Packit Service a1bd4f
        return g_steal_pointer(&path);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    NM_SET_OUT(out_preferred_path, g_steal_pointer(&path));
Packit Service a1bd4f
Packit Service a1bd4f
    /* If the leasefile we're looking for doesn't exist yet in the new location
Packit Service a1bd4f
     * (eg, /var/lib/NetworkManager) then look in old locations to maintain
Packit Service a1bd4f
     * backwards compatibility with external tools (like dracut) that put
Packit Service a1bd4f
     * leasefiles there.
Packit Service a1bd4f
     */
Packit Service a1bd4f
Packit Service a1bd4f
    /* Old Debian, SUSE, and Mandriva location */
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
    path = g_strdup_printf(LOCALSTATEDIR "/lib/dhcp/dhclient%s-%s-%s.lease",
Packit Service a1bd4f
                           _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                           uuid,
Packit Service a1bd4f
                           iface);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return g_steal_pointer(&path);
Packit Service a1bd4f
Packit Service a1bd4f
    /* Old Red Hat and Fedora location */
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
    path = g_strdup_printf(LOCALSTATEDIR "/lib/dhclient/dhclient%s-%s-%s.lease",
Packit Service a1bd4f
                           _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                           uuid,
Packit Service a1bd4f
                           iface);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return g_steal_pointer(&path);
Packit Service a1bd4f
Packit Service a1bd4f
    /* Fail */
Packit Service a1bd4f
    return NULL;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
merge_dhclient_config(NMDhcpDhclient *    self,
Packit Service a1bd4f
                      int                 addr_family,
Packit Service a1bd4f
                      const char *        iface,
Packit Service a1bd4f
                      const char *        conf_file,
Packit Service a1bd4f
                      GBytes *            client_id,
Packit Service a1bd4f
                      const char *        anycast_addr,
Packit Service a1bd4f
                      const char *        hostname,
Packit Service a1bd4f
                      guint32             timeout,
Packit Service a1bd4f
                      gboolean            use_fqdn,
Packit Service a1bd4f
                      NMDhcpHostnameFlags hostname_flags,
Packit Service a1bd4f
                      const char *        mud_url,
Packit Service a1bd4f
                      const char *const * reject_servers,
Packit Service a1bd4f
                      const char *        orig_path,
Packit Service a1bd4f
                      GBytes **           out_new_client_id,
Packit Service a1bd4f
                      GError **           error)
Packit 5756e2
{
Packit Service a1bd4f
    gs_free char *orig = NULL;
Packit Service a1bd4f
    gs_free char *new  = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    g_return_val_if_fail(iface, FALSE);
Packit Service a1bd4f
    g_return_val_if_fail(conf_file, FALSE);
Packit Service a1bd4f
Packit Service a1bd4f
    if (orig_path && g_file_test(orig_path, G_FILE_TEST_EXISTS)) {
Packit Service a1bd4f
        GError *read_error = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
        if (!g_file_get_contents(orig_path, &orig, NULL, &read_error)) {
Packit Service a1bd4f
            _LOGW("error reading dhclient configuration %s: %s", orig_path, read_error->message);
Packit Service a1bd4f
            g_error_free(read_error);
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    new = nm_dhcp_dhclient_create_config(iface,
Packit Service a1bd4f
                                         addr_family,
Packit Service a1bd4f
                                         client_id,
Packit Service a1bd4f
                                         anycast_addr,
Packit Service a1bd4f
                                         hostname,
Packit Service a1bd4f
                                         timeout,
Packit Service a1bd4f
                                         use_fqdn,
Packit Service a1bd4f
                                         hostname_flags,
Packit Service a1bd4f
                                         mud_url,
Packit Service a1bd4f
                                         reject_servers,
Packit Service a1bd4f
                                         orig_path,
Packit Service a1bd4f
                                         orig,
Packit Service a1bd4f
                                         out_new_client_id);
Packit Service a1bd4f
    nm_assert(new);
Packit Service a1bd4f
Packit Service a1bd4f
    return g_file_set_contents(conf_file, new, -1, error);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static char *
Packit Service a1bd4f
find_existing_config(NMDhcpDhclient *self, int addr_family, const char *iface, const char *uuid)
Packit 5756e2
{
Packit Service a1bd4f
    char *path;
Packit Service a1bd4f
Packit Service a1bd4f
    /* NetworkManager-overridden configuration can be used to ship DHCP config
Packit Service a1bd4f
     * with NetworkManager itself. It can be uuid-specific, device-specific
Packit Service a1bd4f
     * or generic.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    if (uuid) {
Packit Service a1bd4f
        path = g_strdup_printf(NMCONFDIR "/dhclient%s-%s.conf",
Packit Service a1bd4f
                               _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                               uuid);
Packit Service a1bd4f
        _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
        if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
            return path;
Packit Service a1bd4f
        g_free(path);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    path = g_strdup_printf(NMCONFDIR "/dhclient%s-%s.conf",
Packit Service a1bd4f
                           _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                           iface);
Packit Service a1bd4f
    _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return path;
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
Packit Service a1bd4f
    path = g_strdup_printf(NMCONFDIR "/dhclient%s.conf", _addr_family_to_path_part(addr_family));
Packit Service a1bd4f
    _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return path;
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
Packit Service a1bd4f
    /* Distribution's dhclient configuration is used so that we can use
Packit Service a1bd4f
     * configuration shipped with dhclient (if any).
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * This replaces conditional compilation based on distribution name. Fedora
Packit Service a1bd4f
     * and Debian store the configs in /etc/dhcp while upstream defaults to /etc
Packit Service a1bd4f
     * which is then used by many other distributions. Some distributions
Packit Service a1bd4f
     * (including Fedora) don't even provide a default configuration file.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    path = g_strdup_printf(SYSCONFDIR "/dhcp/dhclient%s-%s.conf",
Packit Service a1bd4f
                           _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                           iface);
Packit Service a1bd4f
    _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return path;
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
Packit Service a1bd4f
    path = g_strdup_printf(SYSCONFDIR "/dhclient%s-%s.conf",
Packit Service a1bd4f
                           _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                           iface);
Packit Service a1bd4f
    _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return path;
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
Packit Service a1bd4f
    path =
Packit Service a1bd4f
        g_strdup_printf(SYSCONFDIR "/dhcp/dhclient%s.conf", _addr_family_to_path_part(addr_family));
Packit Service a1bd4f
    _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return path;
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
Packit Service a1bd4f
    path = g_strdup_printf(SYSCONFDIR "/dhclient%s.conf", _addr_family_to_path_part(addr_family));
Packit Service a1bd4f
    _LOGD("looking for existing config %s", path);
Packit Service a1bd4f
    if (g_file_test(path, G_FILE_TEST_EXISTS))
Packit Service a1bd4f
        return path;
Packit Service a1bd4f
    g_free(path);
Packit Service a1bd4f
Packit Service a1bd4f
    return NULL;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/* NM provides interface-specific options; thus the same dhclient config
Packit 5756e2
 * file cannot be used since DHCP transactions can happen in parallel.
Packit 5756e2
 * Since some distros don't have default per-interface dhclient config files,
Packit 5756e2
 * read their single config file and merge that into a custom per-interface
Packit 5756e2
 * config file along with the NM options.
Packit 5756e2
 */
Packit 5756e2
static char *
Packit Service a1bd4f
create_dhclient_config(NMDhcpDhclient *    self,
Packit Service a1bd4f
                       int                 addr_family,
Packit Service a1bd4f
                       const char *        iface,
Packit Service a1bd4f
                       const char *        uuid,
Packit Service a1bd4f
                       GBytes *            client_id,
Packit Service a1bd4f
                       const char *        dhcp_anycast_addr,
Packit Service a1bd4f
                       const char *        hostname,
Packit Service a1bd4f
                       guint32             timeout,
Packit Service a1bd4f
                       gboolean            use_fqdn,
Packit Service a1bd4f
                       NMDhcpHostnameFlags hostname_flags,
Packit Service a1bd4f
                       const char *        mud_url,
Packit Service a1bd4f
                       const char *const * reject_servers,
Packit Service a1bd4f
                       GBytes **           out_new_client_id)
Packit 5756e2
{
Packit Service a1bd4f
    gs_free char *orig = NULL;
Packit Service a1bd4f
    char *new          = NULL;
Packit Service a1bd4f
    GError *error      = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    g_return_val_if_fail(iface != NULL, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    new = g_strdup_printf(NMSTATEDIR "/dhclient%s-%s.conf",
Packit Service a1bd4f
                          _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                          iface);
Packit Service a1bd4f
Packit Service a1bd4f
    _LOGD("creating composite dhclient config %s", new);
Packit Service a1bd4f
Packit Service a1bd4f
    orig = find_existing_config(self, addr_family, iface, uuid);
Packit Service a1bd4f
    if (orig)
Packit Service a1bd4f
        _LOGD("merging existing dhclient config %s", orig);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        _LOGD("no existing dhclient configuration to merge");
Packit Service a1bd4f
Packit Service a1bd4f
    if (!merge_dhclient_config(self,
Packit Service a1bd4f
                               addr_family,
Packit Service a1bd4f
                               iface,
Packit Service a1bd4f
                               new,
Packit Service a1bd4f
                               client_id,
Packit Service a1bd4f
                               dhcp_anycast_addr,
Packit Service a1bd4f
                               hostname,
Packit Service a1bd4f
                               timeout,
Packit Service a1bd4f
                               use_fqdn,
Packit Service a1bd4f
                               hostname_flags,
Packit Service a1bd4f
                               mud_url,
Packit Service a1bd4f
                               reject_servers,
Packit Service a1bd4f
                               orig,
Packit Service a1bd4f
                               out_new_client_id,
Packit Service a1bd4f
                               &error)) {
Packit Service a1bd4f
        _LOGW("error creating dhclient configuration: %s", error->message);
Packit Service a1bd4f
        g_clear_error(&error);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return new;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
dhclient_start(NMDhcpClient *client,
Packit Service a1bd4f
               const char *  mode_opt,
Packit Service a1bd4f
               gboolean      release,
Packit Service a1bd4f
               pid_t *       out_pid,
Packit Service a1bd4f
               int           prefixes,
Packit Service a1bd4f
               GError **     error)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpDhclient *       self       = NM_DHCP_DHCLIENT(client);
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv       = NM_DHCP_DHCLIENT_GET_PRIVATE(self);
Packit Service a1bd4f
    gs_unref_ptrarray GPtrArray *argv = NULL;
Packit Service a1bd4f
    pid_t                        pid;
Packit Service a1bd4f
    gs_free_error GError *local = NULL;
Packit Service a1bd4f
    const char *          iface;
Packit Service a1bd4f
    const char *          uuid;
Packit Service a1bd4f
    const char *          system_bus_address;
Packit Service a1bd4f
    const char *          dhclient_path;
Packit Service a1bd4f
    char *                binary_name;
Packit Service a1bd4f
    gs_free char *        cmd_str                  = NULL;
Packit Service a1bd4f
    gs_free char *        pid_file                 = NULL;
Packit Service a1bd4f
    gs_free char *        system_bus_address_env   = NULL;
Packit Service a1bd4f
    gs_free char *        preferred_leasefile_path = NULL;
Packit Service a1bd4f
    const int             addr_family              = nm_dhcp_client_get_addr_family(client);
Packit Service a1bd4f
Packit Service a1bd4f
    g_return_val_if_fail(!priv->pid_file, FALSE);
Packit Service a1bd4f
Packit Service a1bd4f
    NM_SET_OUT(out_pid, 0);
Packit Service a1bd4f
Packit Service a1bd4f
    dhclient_path = nm_dhcp_dhclient_get_path();
Packit Service a1bd4f
    if (!dhclient_path) {
Packit Service a1bd4f
        nm_utils_error_set_literal(error, NM_UTILS_ERROR_UNKNOWN, "dhclient binary not found");
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    iface = nm_dhcp_client_get_iface(client);
Packit Service a1bd4f
    uuid  = nm_dhcp_client_get_uuid(client);
Packit Service a1bd4f
Packit Service a1bd4f
    pid_file = g_strdup_printf(NMRUNDIR "/dhclient%s-%s.pid",
Packit Service a1bd4f
                               _addr_family_to_path_part(addr_family),
Packit Service a1bd4f
                               iface);
Packit Service a1bd4f
Packit Service a1bd4f
    /* Kill any existing dhclient from the pidfile */
Packit Service a1bd4f
    binary_name = g_path_get_basename(dhclient_path);
Packit Service a1bd4f
    nm_dhcp_client_stop_existing(pid_file, binary_name);
Packit Service a1bd4f
    g_free(binary_name);
Packit Service a1bd4f
Packit Service a1bd4f
    if (release) {
Packit Service a1bd4f
        /* release doesn't use the pidfile after killing an old client */
Packit Service a1bd4f
        nm_clear_g_free(&pid_file);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_free(priv->lease_file);
Packit Service a1bd4f
    priv->lease_file = get_dhclient_leasefile(addr_family, iface, uuid, &preferred_leasefile_path);
Packit Service a1bd4f
    nm_assert(preferred_leasefile_path);
Packit Service a1bd4f
    if (!priv->lease_file) {
Packit Service a1bd4f
        /* No existing leasefile, dhclient will create one at the preferred path */
Packit Service a1bd4f
        priv->lease_file = g_steal_pointer(&preferred_leasefile_path);
Packit Service a1bd4f
    } else if (!nm_streq0(priv->lease_file, preferred_leasefile_path)) {
Packit Service a1bd4f
        gs_unref_object GFile *src = g_file_new_for_path(priv->lease_file);
Packit Service a1bd4f
        gs_unref_object GFile *dst = g_file_new_for_path(preferred_leasefile_path);
Packit Service a1bd4f
Packit Service a1bd4f
        /* Try to copy the existing leasefile to the preferred location */
Packit Service a1bd4f
        if (!g_file_copy(src, dst, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &local)) {
Packit Service a1bd4f
            gs_free char *s_path = NULL;
Packit Service a1bd4f
            gs_free char *d_path = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
            /* Failure; just use the existing leasefile */
Packit Service a1bd4f
            _LOGW("failed to copy leasefile %s to %s: %s",
Packit Service a1bd4f
                  (s_path = g_file_get_path(src)),
Packit Service a1bd4f
                  (d_path = g_file_get_path(dst)),
Packit Service a1bd4f
                  local->message);
Packit Service a1bd4f
            g_clear_error(&local);
Packit Service a1bd4f
        } else {
Packit Service a1bd4f
            /* Success; use the preferred leasefile path */
Packit Service a1bd4f
            g_free(priv->lease_file);
Packit Service a1bd4f
            priv->lease_file = g_file_get_path(dst);
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    /* Save the DUID to the leasefile dhclient will actually use */
Packit Service a1bd4f
    if (addr_family == AF_INET6) {
Packit Service a1bd4f
        if (!nm_dhcp_dhclient_save_duid(priv->lease_file,
Packit Service a1bd4f
                                        nm_dhcp_client_get_client_id(client),
Packit Service a1bd4f
                                        &local)) {
Packit Service a1bd4f
            nm_utils_error_set(error,
Packit Service a1bd4f
                               NM_UTILS_ERROR_UNKNOWN,
Packit Service a1bd4f
                               "failed to save DUID to '%s': %s",
Packit Service a1bd4f
                               priv->lease_file,
Packit Service a1bd4f
                               local->message);
Packit Service a1bd4f
            return FALSE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    argv = g_ptr_array_new();
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) dhclient_path);
Packit Service a1bd4f
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) "-d");
Packit Service a1bd4f
Packit Service a1bd4f
    /* Be quiet. dhclient logs to syslog anyway. And we duplicate the syslog
Packit Service a1bd4f
     * to stderr in case of NM running with --debug.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) "-q");
Packit Service a1bd4f
Packit Service a1bd4f
    if (release)
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) "-r");
Packit Service a1bd4f
Packit Service a1bd4f
    if (addr_family == AF_INET6) {
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) "-6");
Packit Service a1bd4f
        if (mode_opt)
Packit Service a1bd4f
            g_ptr_array_add(argv, (gpointer) mode_opt);
Packit Service a1bd4f
        while (prefixes--)
Packit Service a1bd4f
            g_ptr_array_add(argv, (gpointer) "-P");
Packit Service a1bd4f
    }
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) "-sf"); /* Set script file */
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) nm_dhcp_helper_path);
Packit Service a1bd4f
Packit Service a1bd4f
    if (pid_file) {
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) "-pf"); /* Set pid file */
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) pid_file);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) "-lf"); /* Set lease file */
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) priv->lease_file);
Packit Service a1bd4f
Packit Service a1bd4f
    if (priv->conf_file) {
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) "-cf"); /* Set interface config file */
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) priv->conf_file);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    /* Usually the system bus address is well-known; but if it's supposed
Packit Service a1bd4f
     * to be something else, we need to push it to dhclient, since dhclient
Packit Service a1bd4f
     * sanitizes the environment it gives the action scripts.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    system_bus_address = getenv("DBUS_SYSTEM_BUS_ADDRESS");
Packit Service a1bd4f
    if (system_bus_address) {
Packit Service a1bd4f
        system_bus_address_env = g_strdup_printf("DBUS_SYSTEM_BUS_ADDRESS=%s", system_bus_address);
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) "-e");
Packit Service a1bd4f
        g_ptr_array_add(argv, (gpointer) system_bus_address_env);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_ptr_array_add(argv, (gpointer) iface);
Packit Service a1bd4f
    g_ptr_array_add(argv, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    _LOGD("running: %s", (cmd_str = g_strjoinv(" ", (char **) argv->pdata)));
Packit Service a1bd4f
Packit Service a1bd4f
    if (!g_spawn_async(NULL,
Packit Service a1bd4f
                       (char **) argv->pdata,
Packit Service a1bd4f
                       NULL,
Packit Service a1bd4f
                       G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL
Packit Service a1bd4f
                           | G_SPAWN_STDERR_TO_DEV_NULL,
Packit Service a1bd4f
                       nm_utils_setpgid,
Packit Service a1bd4f
                       NULL,
Packit Service a1bd4f
                       &pid,
Packit Service a1bd4f
                       &local)) {
Packit Service a1bd4f
        nm_utils_error_set(error,
Packit Service a1bd4f
                           NM_UTILS_ERROR_UNKNOWN,
Packit Service a1bd4f
                           "dhclient failed to start: %s",
Packit Service a1bd4f
                           local->message);
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    _LOGI("dhclient started with pid %lld", (long long int) pid);
Packit Service a1bd4f
Packit Service a1bd4f
    if (!release)
Packit Service a1bd4f
        nm_dhcp_client_watch_child(client, pid);
Packit Service a1bd4f
Packit Service a1bd4f
    priv->pid_file = g_steal_pointer(&pid_file);
Packit Service a1bd4f
Packit Service a1bd4f
    NM_SET_OUT(out_pid, pid);
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
ip4_start(NMDhcpClient *client,
Packit Service a1bd4f
          const char *  dhcp_anycast_addr,
Packit Service a1bd4f
          const char *  last_ip4_address,
Packit Service a1bd4f
          GError **     error)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpDhclient *       self = NM_DHCP_DHCLIENT(client);
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE(self);
Packit Service a1bd4f
    GBytes *               client_id;
Packit Service a1bd4f
    gs_unref_bytes GBytes *new_client_id = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    client_id = nm_dhcp_client_get_client_id(client);
Packit Service a1bd4f
Packit Service a1bd4f
    priv->conf_file = create_dhclient_config(self,
Packit Service a1bd4f
                                             AF_INET,
Packit Service a1bd4f
                                             nm_dhcp_client_get_iface(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_uuid(client),
Packit Service a1bd4f
                                             client_id,
Packit Service a1bd4f
                                             dhcp_anycast_addr,
Packit Service a1bd4f
                                             nm_dhcp_client_get_hostname(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_timeout(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_use_fqdn(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_hostname_flags(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_mud_url(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_reject_servers(client),
Packit Service a1bd4f
                                             &new_client_id);
Packit Service a1bd4f
    if (!priv->conf_file) {
Packit Service a1bd4f
        nm_utils_error_set_literal(error,
Packit Service a1bd4f
                                   NM_UTILS_ERROR_UNKNOWN,
Packit Service a1bd4f
                                   "error creating dhclient configuration file");
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (new_client_id) {
Packit Service a1bd4f
        nm_assert(!client_id);
Packit Service a1bd4f
        nm_dhcp_client_set_client_id(client, new_client_id);
Packit Service a1bd4f
    }
Packit Service a1bd4f
    return dhclient_start(client, NULL, FALSE, NULL, 0, error);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
ip6_start(NMDhcpClient *            client,
Packit Service a1bd4f
          const char *              dhcp_anycast_addr,
Packit Service a1bd4f
          const struct in6_addr *   ll_addr,
Packit Service a1bd4f
          NMSettingIP6ConfigPrivacy privacy,
Packit Service a1bd4f
          guint                     needed_prefixes,
Packit Service a1bd4f
          GError **                 error)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpDhclient *       self = NM_DHCP_DHCLIENT(client);
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE(self);
Packit Service a1bd4f
Packit Service a1bd4f
    if (nm_dhcp_client_get_iaid_explicit(client))
Packit Service a1bd4f
        _LOGW("dhclient does not support specifying an IAID for DHCPv6, it will be ignored");
Packit Service a1bd4f
Packit Service a1bd4f
    priv->conf_file = create_dhclient_config(self,
Packit Service a1bd4f
                                             AF_INET6,
Packit Service a1bd4f
                                             nm_dhcp_client_get_iface(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_uuid(client),
Packit Service a1bd4f
                                             NULL,
Packit Service a1bd4f
                                             dhcp_anycast_addr,
Packit Service a1bd4f
                                             nm_dhcp_client_get_hostname(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_timeout(client),
Packit Service a1bd4f
                                             TRUE,
Packit Service a1bd4f
                                             nm_dhcp_client_get_hostname_flags(client),
Packit Service a1bd4f
                                             nm_dhcp_client_get_mud_url(client),
Packit Service a1bd4f
                                             NULL,
Packit Service a1bd4f
                                             NULL);
Packit Service a1bd4f
    if (!priv->conf_file) {
Packit Service a1bd4f
        nm_utils_error_set_literal(error,
Packit Service a1bd4f
                                   NM_UTILS_ERROR_UNKNOWN,
Packit Service a1bd4f
                                   "error creating dhclient configuration file");
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return dhclient_start(client,
Packit Service a1bd4f
                          nm_dhcp_client_get_info_only(NM_DHCP_CLIENT(self)) ? "-S" : "-N",
Packit Service a1bd4f
                          FALSE,
Packit Service a1bd4f
                          NULL,
Packit Service a1bd4f
                          needed_prefixes,
Packit Service a1bd4f
                          error);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
stop(NMDhcpClient *client, gboolean release)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpDhclient *       self = NM_DHCP_DHCLIENT(client);
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE(self);
Packit Service a1bd4f
    int                    errsv;
Packit Service a1bd4f
Packit Service a1bd4f
    NM_DHCP_CLIENT_CLASS(nm_dhcp_dhclient_parent_class)->stop(client, release);
Packit Service a1bd4f
Packit Service a1bd4f
    if (priv->conf_file)
Packit Service a1bd4f
        if (remove(priv->conf_file) == -1) {
Packit Service a1bd4f
            errsv = errno;
Packit Service a1bd4f
            _LOGD("could not remove dhcp config file \"%s\": %d (%s)",
Packit Service a1bd4f
                  priv->conf_file,
Packit Service a1bd4f
                  errsv,
Packit Service a1bd4f
                  nm_strerror_native(errsv));
Packit Service a1bd4f
        }
Packit Service a1bd4f
    if (priv->pid_file) {
Packit Service a1bd4f
        if (remove(priv->pid_file) == -1) {
Packit Service a1bd4f
            errsv = errno;
Packit Service a1bd4f
            _LOGD("could not remove dhcp pid file \"%s\": %s (%d)",
Packit Service a1bd4f
                  priv->pid_file,
Packit Service a1bd4f
                  nm_strerror_native(errsv),
Packit Service a1bd4f
                  errsv);
Packit Service a1bd4f
        }
Packit Service a1bd4f
        nm_clear_g_free(&priv->pid_file);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (release) {
Packit Service a1bd4f
        pid_t rpid = -1;
Packit Service a1bd4f
Packit Service a1bd4f
        if (dhclient_start(client, NULL, TRUE, &rpid, 0, NULL)) {
Packit Service a1bd4f
            /* Wait a few seconds for the release to happen */
Packit Service a1bd4f
            nm_dhcp_client_stop_pid(rpid, nm_dhcp_client_get_iface(client));
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static GBytes *
Packit Service a1bd4f
get_duid(NMDhcpClient *client)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpDhclient *       self      = NM_DHCP_DHCLIENT(client);
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv      = NM_DHCP_DHCLIENT_GET_PRIVATE(self);
Packit Service a1bd4f
    GBytes *               duid      = NULL;
Packit Service a1bd4f
    gs_free char *         leasefile = NULL;
Packit Service a1bd4f
    GError *               error     = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    /* Look in interface-specific leasefile first for backwards compat */
Packit Service a1bd4f
    leasefile = get_dhclient_leasefile(AF_INET6,
Packit Service a1bd4f
                                       nm_dhcp_client_get_iface(client),
Packit Service a1bd4f
                                       nm_dhcp_client_get_uuid(client),
Packit Service a1bd4f
                                       NULL);
Packit Service a1bd4f
    if (leasefile) {
Packit Service a1bd4f
        _LOGD("looking for DUID in '%s'", leasefile);
Packit Service a1bd4f
        duid = nm_dhcp_dhclient_read_duid(leasefile, &error);
Packit Service a1bd4f
        if (error) {
Packit Service a1bd4f
            _LOGW("failed to read leasefile '%s': %s", leasefile, error->message);
Packit Service a1bd4f
            g_clear_error(&error);
Packit Service a1bd4f
        }
Packit Service a1bd4f
        if (duid)
Packit Service a1bd4f
            return duid;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    /* Otherwise, read the default machine-wide DUID */
Packit Service a1bd4f
    _LOGD("looking for default DUID in '%s'", priv->def_leasefile);
Packit Service a1bd4f
    duid = nm_dhcp_dhclient_read_duid(priv->def_leasefile, &error);
Packit Service a1bd4f
    if (error) {
Packit Service a1bd4f
        _LOGW("failed to read leasefile '%s': %s", priv->def_leasefile, error->message);
Packit Service a1bd4f
        g_clear_error(&error);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return duid;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_dhcp_dhclient_init(NMDhcpDhclient *self)
Packit 5756e2
{
Packit Service a1bd4f
    static const char *const FILES[] = {
Packit Service a1bd4f
        SYSCONFDIR "/dhclient6.leases", /* default */
Packit Service a1bd4f
        LOCALSTATEDIR "/lib/dhcp/dhclient6.leases",
Packit Service a1bd4f
        LOCALSTATEDIR "/lib/dhclient/dhclient6.leases",
Packit Service a1bd4f
    };
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE(self);
Packit Service a1bd4f
    int                    i;
Packit Service a1bd4f
Packit Service a1bd4f
    priv->def_leasefile = FILES[0];
Packit Service a1bd4f
    for (i = 0; i < G_N_ELEMENTS(FILES); i++) {
Packit Service a1bd4f
        if (g_file_test(FILES[i], G_FILE_TEST_EXISTS)) {
Packit Service a1bd4f
            priv->def_leasefile = FILES[i];
Packit Service a1bd4f
            break;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    priv->dhcp_listener = g_object_ref(nm_dhcp_listener_get());
Packit Service a1bd4f
    g_signal_connect(priv->dhcp_listener,
Packit Service a1bd4f
                     NM_DHCP_LISTENER_EVENT,
Packit Service a1bd4f
                     G_CALLBACK(nm_dhcp_client_handle_event),
Packit Service a1bd4f
                     self);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
dispose(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE(object);
Packit 5756e2
Packit Service a1bd4f
    if (priv->dhcp_listener) {
Packit Service a1bd4f
        g_signal_handlers_disconnect_by_func(priv->dhcp_listener,
Packit Service a1bd4f
                                             G_CALLBACK(nm_dhcp_client_handle_event),
Packit Service a1bd4f
                                             NM_DHCP_DHCLIENT(object));
Packit Service a1bd4f
        g_clear_object(&priv->dhcp_listener);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    nm_clear_g_free(&priv->pid_file);
Packit Service a1bd4f
    nm_clear_g_free(&priv->conf_file);
Packit Service a1bd4f
    nm_clear_g_free(&priv->lease_file);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nm_dhcp_dhclient_parent_class)->dispose(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nm_dhcp_dhclient_class_init(NMDhcpDhclientClass *dhclient_class)
Packit 5756e2
{
Packit Service a1bd4f
    NMDhcpClientClass *client_class = NM_DHCP_CLIENT_CLASS(dhclient_class);
Packit Service a1bd4f
    GObjectClass *     object_class = G_OBJECT_CLASS(dhclient_class);
Packit 5756e2
Packit Service a1bd4f
    object_class->dispose = dispose;
Packit 5756e2
Packit Service a1bd4f
    client_class->ip4_start = ip4_start;
Packit Service a1bd4f
    client_class->ip6_start = ip6_start;
Packit Service a1bd4f
    client_class->stop      = stop;
Packit Service a1bd4f
    client_class->get_duid  = get_duid;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
const NMDhcpClientFactory _nm_dhcp_client_factory_dhclient = {
Packit Service a1bd4f
    .name     = "dhclient",
Packit Service a1bd4f
    .get_type = nm_dhcp_dhclient_get_type,
Packit Service a1bd4f
    .get_path = nm_dhcp_dhclient_get_path,
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
#endif /* WITH_DHCLIENT */