nm-dhcp-dhclient.c 24.4 KB
Newer Older
Dan Williams's avatar
Dan Williams committed
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* nm-dhcp-dhclient.c - dhclient specific hooks for NetworkManager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
18
 * Copyright (C) 2005 - 2012 Red Hat, Inc.
19 20
 */

21 22
#include <config.h>
#define __CONFIG_H__
23

24 25 26 27
#define _XOPEN_SOURCE
#include <time.h>
#undef _XOPEN_SOURCE

28 29
#include "nm-default.h"

30
#if WITH_DHCLIENT
31

32 33 34
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
35 36
#include <netinet/in.h>
#include <arpa/inet.h>
37
#include <ctype.h>
38

39
#include "nm-glib-aux/nm-dedup-multi.h"
40

41
#include "nm-utils.h"
42
#include "nm-config.h"
43
#include "nm-dhcp-dhclient-utils.h"
44
#include "nm-dhcp-manager.h"
45
#include "NetworkManagerUtils.h"
46
#include "nm-dhcp-listener.h"
47
#include "nm-dhcp-client-logging.h"
48

49
/*****************************************************************************/
50

51 52 53 54 55 56 57 58 59
static const char *
_addr_family_to_path_part (int addr_family)
{
	nm_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6));
	return (addr_family == AF_INET6) ? "6" : "";
}

/*****************************************************************************/

60 61 62 63 64 65 66 67 68 69 70 71 72 73
#define NM_TYPE_DHCP_DHCLIENT            (nm_dhcp_dhclient_get_type ())
#define NM_DHCP_DHCLIENT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_DHCLIENT, NMDhcpDhclient))
#define NM_DHCP_DHCLIENT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_DHCLIENT, NMDhcpDhclientClass))
#define NM_IS_DHCP_DHCLIENT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_DHCLIENT))
#define NM_IS_DHCP_DHCLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DHCP_DHCLIENT))
#define NM_DHCP_DHCLIENT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_DHCLIENT, NMDhcpDhclientClass))

typedef struct _NMDhcpDhclient NMDhcpDhclient;
typedef struct _NMDhcpDhclientClass NMDhcpDhclientClass;

static GType nm_dhcp_dhclient_get_type (void);

/*****************************************************************************/

74 75
typedef struct {
	char *conf_file;
76
	const char *def_leasefile;
77 78
	char *lease_file;
	char *pid_file;
79
	NMDhcpListener *dhcp_listener;
80
} NMDhcpDhclientPrivate;
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
struct _NMDhcpDhclient {
	NMDhcpClient parent;
	NMDhcpDhclientPrivate _priv;
};

struct _NMDhcpDhclientClass {
	NMDhcpClientClass parent;
};

G_DEFINE_TYPE (NMDhcpDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT)

#define NM_DHCP_DHCLIENT_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDhcpDhclient, NM_IS_DHCP_DHCLIENT)

/*****************************************************************************/

97
static const char *
98
nm_dhcp_dhclient_get_path (void)
99
{
100
	return nm_utils_find_helper ("dhclient", DHCLIENT_PATH, NULL);
101
}
102

103 104
/**
 * get_dhclient_leasefile():
105
 * @addr_family: AF_INET or AF_INET6
106 107 108 109 110 111 112 113
 * @iface: the interface name of the device on which DHCP will be done
 * @uuid: the connection UUID to which the returned lease should belong
 * @out_preferred_path: on return, the "most preferred" leasefile path
 *
 * Returns the path of an existing leasefile (if any) for this interface and
 * connection UUID.  Also returns the "most preferred" leasefile path, which
 * may be different than any found leasefile.
 *
Martin Pitt's avatar
Martin Pitt committed
114
 * Returns: an existing leasefile, or %NULL if no matching leasefile could be found
115
 */
116
static char *
117 118
get_dhclient_leasefile (int addr_family,
                        const char *iface,
119 120
                        const char *uuid,
                        char **out_preferred_path)
121
{
122 123
	gs_free char *rundir_path = NULL;
	gs_free char *path = NULL;
124

125 126 127 128 129 130 131 132
	/* First, see if the lease file is in /run */
	rundir_path = g_strdup_printf (NMRUNDIR "/dhclient%s-%s-%s.lease",
	                               _addr_family_to_path_part (addr_family),
	                               uuid,
	                               iface);

	if (g_file_test (rundir_path, G_FILE_TEST_EXISTS)) {
		NM_SET_OUT (out_preferred_path, g_strdup (rundir_path));
133
		return g_steal_pointer (&rundir_path);
134 135
	}

136 137
	/* /var/lib/NetworkManager is the preferred leasefile path */
	path = g_strdup_printf (NMSTATEDIR "/dhclient%s-%s-%s.lease",
138
	                        _addr_family_to_path_part (addr_family),
139
	                        uuid,
140
	                        iface);
141

142 143
	if (g_file_test (path, G_FILE_TEST_EXISTS)) {
		NM_SET_OUT (out_preferred_path, g_strdup (path));
144
		return g_steal_pointer (&path);
145 146
	}

147 148 149 150
	if (nm_config_get_configure_and_quit (nm_config_get ()) == NM_CONFIG_CONFIGURE_AND_QUIT_INITRD)
		NM_SET_OUT (out_preferred_path, g_steal_pointer (&rundir_path));
	else
		NM_SET_OUT (out_preferred_path, g_steal_pointer (&path));
151 152 153 154 155 156 157 158 159 160

	/* If the leasefile we're looking for doesn't exist yet in the new location
	 * (eg, /var/lib/NetworkManager) then look in old locations to maintain
	 * backwards compatibility with external tools (like dracut) that put
	 * leasefiles there.
	 */

	/* Old Debian, SUSE, and Mandriva location */
	g_free (path);
	path = g_strdup_printf (LOCALSTATEDIR "/lib/dhcp/dhclient%s-%s-%s.lease",
161
	                        _addr_family_to_path_part (addr_family), uuid, iface);
162
	if (g_file_test (path, G_FILE_TEST_EXISTS))
163
		return g_steal_pointer (&path);
164 165 166 167

	/* Old Red Hat and Fedora location */
	g_free (path);
	path = g_strdup_printf (LOCALSTATEDIR "/lib/dhclient/dhclient%s-%s-%s.lease",
168
	                        _addr_family_to_path_part (addr_family), uuid, iface);
169
	if (g_file_test (path, G_FILE_TEST_EXISTS))
170
		return g_steal_pointer (&path);
171 172 173

	/* Fail */
	return NULL;
174 175 176
}

static gboolean
177
merge_dhclient_config (NMDhcpDhclient *self,
178
                       int addr_family,
179
                       const char *iface,
180
                       const char *conf_file,
181
                       GBytes *client_id,
182
                       const char *anycast_addr,
183
                       const char *hostname,
184
                       guint32 timeout,
185
                       gboolean use_fqdn,
186
                       const char *orig_path,
187
                       GBytes **out_new_client_id,
188 189
                       GError **error)
{
190 191
	gs_free char *orig = NULL;
	gs_free char *new = NULL;
192

193 194
	g_return_val_if_fail (iface, FALSE);
	g_return_val_if_fail (conf_file, FALSE);
195

196 197
	if (   orig_path
	    && g_file_test (orig_path, G_FILE_TEST_EXISTS)) {
198 199
		GError *read_error = NULL;

200
		if (!g_file_get_contents (orig_path, &orig, NULL, &read_error)) {
201 202
			_LOGW ("error reading dhclient configuration %s: %s",
			       orig_path, read_error->message);
203 204 205 206
			g_error_free (read_error);
		}
	}

207 208 209 210 211 212 213 214 215 216
	new = nm_dhcp_dhclient_create_config (iface,
	                                      addr_family,
	                                      client_id,
	                                      anycast_addr,
	                                      hostname,
	                                      timeout,
	                                      use_fqdn,
	                                      orig_path,
	                                      orig,
	                                      out_new_client_id);
217
	g_assert (new);
218

219 220 221 222
	return g_file_set_contents (conf_file,
	                            new,
	                            -1,
	                            error);
223 224
}

225
static char *
226
find_existing_config (NMDhcpDhclient *self, int addr_family, const char *iface, const char *uuid)
227 228 229 230 231 232 233 234
{
	char *path;

	/* NetworkManager-overridden configuration can be used to ship DHCP config
	 * with NetworkManager itself. It can be uuid-specific, device-specific
	 * or generic.
	 */
	if (uuid) {
235
		path = g_strdup_printf (NMCONFDIR "/dhclient%s-%s.conf", _addr_family_to_path_part (addr_family), uuid);
236
		_LOGD ("looking for existing config %s", path);
237 238 239 240 241
		if (g_file_test (path, G_FILE_TEST_EXISTS))
			return path;
		g_free (path);
	}

242
	path = g_strdup_printf (NMCONFDIR "/dhclient%s-%s.conf", _addr_family_to_path_part (addr_family), iface);
243
	_LOGD ("looking for existing config %s", path);
244 245 246 247
	if (g_file_test (path, G_FILE_TEST_EXISTS))
		return path;
	g_free (path);

248
	path = g_strdup_printf (NMCONFDIR "/dhclient%s.conf", _addr_family_to_path_part (addr_family));
249
	_LOGD ("looking for existing config %s", path);
250 251 252 253 254 255 256 257 258 259 260 261
	if (g_file_test (path, G_FILE_TEST_EXISTS))
		return path;
	g_free (path);

	/* Distribution's dhclient configuration is used so that we can use
	 * configuration shipped with dhclient (if any).
	 *
	 * This replaces conditional compilation based on distribution name. Fedora
	 * and Debian store the configs in /etc/dhcp while upstream defaults to /etc
	 * which is then used by many other distributions. Some distributions
	 * (including Fedora) don't even provide a default configuration file.
	 */
262
	path = g_strdup_printf (SYSCONFDIR "/dhcp/dhclient%s-%s.conf", _addr_family_to_path_part (addr_family), iface);
263
	_LOGD ("looking for existing config %s", path);
264 265 266 267
	if (g_file_test (path, G_FILE_TEST_EXISTS))
		return path;
	g_free (path);

268
	path = g_strdup_printf (SYSCONFDIR "/dhclient%s-%s.conf", _addr_family_to_path_part (addr_family), iface);
269
	_LOGD ("looking for existing config %s", path);
270 271 272 273
	if (g_file_test (path, G_FILE_TEST_EXISTS))
		return path;
	g_free (path);

274
	path = g_strdup_printf (SYSCONFDIR "/dhcp/dhclient%s.conf", _addr_family_to_path_part (addr_family));
275
	_LOGD ("looking for existing config %s", path);
276 277 278 279
	if (g_file_test (path, G_FILE_TEST_EXISTS))
		return path;
	g_free (path);

280
	path = g_strdup_printf (SYSCONFDIR "/dhclient%s.conf", _addr_family_to_path_part (addr_family));
281
	_LOGD ("looking for existing config %s", path);
282 283 284 285 286 287 288
	if (g_file_test (path, G_FILE_TEST_EXISTS))
		return path;
	g_free (path);

	return NULL;
}

289 290 291 292 293 294
/* NM provides interface-specific options; thus the same dhclient config
 * file cannot be used since DHCP transactions can happen in parallel.
 * Since some distros don't have default per-interface dhclient config files,
 * read their single config file and merge that into a custom per-interface
 * config file along with the NM options.
 */
295
static char *
296
create_dhclient_config (NMDhcpDhclient *self,
297
                        int addr_family,
298
                        const char *iface,
299
                        const char *uuid,
300
                        GBytes *client_id,
301
                        const char *dhcp_anycast_addr,
302
                        const char *hostname,
303
                        guint32 timeout,
304
                        gboolean use_fqdn,
305
                        GBytes **out_new_client_id)
306
{
307 308
	gs_free char *orig = NULL;
	char *new = NULL;
309 310
	GError *error = NULL;

311
	g_return_val_if_fail (iface != NULL, NULL);
312

313
	new = g_strdup_printf (NMSTATEDIR "/dhclient%s-%s.conf", _addr_family_to_path_part (addr_family), iface);
314

315 316
	_LOGD ("creating composite dhclient config %s", new);

317
	orig = find_existing_config (self, addr_family, iface, uuid);
318 319 320 321
	if (orig)
		_LOGD ("merging existing dhclient config %s", orig);
	else
		_LOGD ("no existing dhclient configuration to merge");
322

323 324 325 326 327 328 329 330 331 332 333 334
	if (!merge_dhclient_config (self,
	                            addr_family,
	                            iface,
	                            new,
	                            client_id,
	                            dhcp_anycast_addr,
	                            hostname,
	                            timeout,
	                            use_fqdn,
	                            orig,
	                            out_new_client_id,
	                            &error)) {
335
		_LOGW ("error creating dhclient configuration: %s", error->message);
336
		g_clear_error (&error);
337 338
	}

339
	return new;
340 341
}

342
static gboolean
343
dhclient_start (NMDhcpClient *client,
344
                const char *mode_opt,
345
                gboolean release,
346
                pid_t *out_pid,
347 348
                int prefixes,
                GError **error)
349
{
350 351
	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
352
	gs_unref_ptrarray GPtrArray *argv = NULL;
353
	pid_t pid;
354
	gs_free_error GError *local = NULL;
355 356 357 358 359 360 361 362 363 364
	const char *iface;
	const char *uuid;
	const char *system_bus_address;
	const char *dhclient_path;
	char *binary_name;
	gs_free char *cmd_str = NULL;
	gs_free char *pid_file = NULL;
	gs_free char *system_bus_address_env = NULL;
	gs_free char *preferred_leasefile_path = NULL;
	const int addr_family = nm_dhcp_client_get_addr_family (client);
365

366
	g_return_val_if_fail (!priv->pid_file, FALSE);
367

368
	NM_SET_OUT (out_pid, 0);
369

370 371
	dhclient_path = nm_dhcp_dhclient_get_path ();
	if (!dhclient_path) {
372
		nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhclient binary not found");
373
		return FALSE;
374 375
	}

376 377 378
	iface = nm_dhcp_client_get_iface (client);
	uuid = nm_dhcp_client_get_uuid (client);

379
	pid_file = g_strdup_printf (RUNSTATEDIR "/dhclient%s-%s.pid",
380 381
	                            _addr_family_to_path_part (addr_family),
	                            iface);
382

383
	/* Kill any existing dhclient from the pidfile */
384
	binary_name = g_path_get_basename (dhclient_path);
385
	nm_dhcp_client_stop_existing (pid_file, binary_name);
386
	g_free (binary_name);
387

388 389
	if (release) {
		/* release doesn't use the pidfile after killing an old client */
390
		nm_clear_g_free (&pid_file);
391 392 393
	}

	g_free (priv->lease_file);
394
	priv->lease_file = get_dhclient_leasefile (addr_family, iface, uuid, &preferred_leasefile_path);
395
	nm_assert (preferred_leasefile_path);
396 397
	if (!priv->lease_file) {
		/* No existing leasefile, dhclient will create one at the preferred path */
398 399 400 401
		priv->lease_file = g_steal_pointer (&preferred_leasefile_path);
	} else if (!nm_streq0 (priv->lease_file, preferred_leasefile_path)) {
		gs_unref_object GFile *src = g_file_new_for_path (priv->lease_file);
		gs_unref_object GFile *dst = g_file_new_for_path (preferred_leasefile_path);
402 403

		/* Try to copy the existing leasefile to the preferred location */
404
		if (!g_file_copy (src, dst, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &local)) {
405 406 407
			gs_free char *s_path = NULL;
			gs_free char *d_path = NULL;

408
			/* Failure; just use the existing leasefile */
409
			_LOGW ("failed to copy leasefile %s to %s: %s",
410 411
			       (s_path = g_file_get_path (src)),
			       (d_path = g_file_get_path (dst)),
412 413 414 415 416 417
			       local->message);
			g_clear_error (&local);
		} else {
			/* Success; use the preferred leasefile path */
			g_free (priv->lease_file);
			priv->lease_file = g_file_get_path (dst);
418 419
		}
	}
420

421
	/* Save the DUID to the leasefile dhclient will actually use */
422
	if (addr_family == AF_INET6) {
423 424 425
		if (!nm_dhcp_dhclient_save_duid (priv->lease_file,
		                                 nm_dhcp_client_get_client_id (client),
		                                 &local)) {
426 427 428 429 430
			nm_utils_error_set (error,
			                    NM_UTILS_ERROR_UNKNOWN,
			                    "failed to save DUID to '%s': %s",
			                    priv->lease_file,
			                    local->message);
431
			return FALSE;
432 433 434
		}
	}

435
	argv = g_ptr_array_new ();
436
	g_ptr_array_add (argv, (gpointer) dhclient_path);
437

438
	g_ptr_array_add (argv, (gpointer) "-d");
439

440 441 442 443 444
	/* Be quiet. dhclient logs to syslog anyway. And we duplicate the syslog
	 * to stderr in case of NM running with --debug.
	 */
	g_ptr_array_add (argv, (gpointer) "-q");

445 446 447
	if (release)
		g_ptr_array_add (argv, (gpointer) "-r");

448
	if (addr_family == AF_INET6) {
449 450 451
		g_ptr_array_add (argv, (gpointer) "-6");
		if (mode_opt)
			g_ptr_array_add (argv, (gpointer) mode_opt);
452 453
		while (prefixes--)
			g_ptr_array_add (argv, (gpointer) "-P");
454
	}
455
	g_ptr_array_add (argv, (gpointer) "-sf"); /* Set script file */
456
	g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path);
457

458
	if (pid_file) {
459
		g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */
460 461
		g_ptr_array_add (argv, (gpointer) pid_file);
	}
462

463
	g_ptr_array_add (argv, (gpointer) "-lf"); /* Set lease file */
464
	g_ptr_array_add (argv, (gpointer) priv->lease_file);
465

466
	if (priv->conf_file) {
467
		g_ptr_array_add (argv, (gpointer) "-cf"); /* Set interface config file */
468 469
		g_ptr_array_add (argv, (gpointer) priv->conf_file);
	}
470

471 472 473 474 475 476 477 478 479 480 481
	/* Usually the system bus address is well-known; but if it's supposed
	 * to be something else, we need to push it to dhclient, since dhclient
	 * sanitizes the environment it gives the action scripts.
	 */
	system_bus_address = getenv ("DBUS_SYSTEM_BUS_ADDRESS");
	if (system_bus_address) {
		system_bus_address_env = g_strdup_printf ("DBUS_SYSTEM_BUS_ADDRESS=%s", system_bus_address);
		g_ptr_array_add (argv, (gpointer) "-e");
		g_ptr_array_add (argv, (gpointer) system_bus_address_env);
	}

482
	g_ptr_array_add (argv, (gpointer) iface);
483
	g_ptr_array_add (argv, NULL);
484

485
	_LOGD ("running: %s",
486
	       (cmd_str = g_strjoinv (" ", (char **) argv->pdata)));
487 488 489

	if (!g_spawn_async (NULL, (char **) argv->pdata, NULL,
	                    G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
490 491 492 493 494
	                    nm_utils_setpgid, NULL, &pid, &local)) {
		nm_utils_error_set (error,
		                    NM_UTILS_ERROR_UNKNOWN,
		                    "dhclient failed to start: %s",
		                    local->message);
495
		return FALSE;
496
	}
497

498 499 500 501 502 503
	_LOGI ("dhclient started with pid %lld", (long long int) pid);

	if (!release)
		nm_dhcp_client_watch_child (client, pid);

	priv->pid_file = g_steal_pointer (&pid_file);
504

505 506
	NM_SET_OUT (out_pid, pid);
	return TRUE;
507
}
508

509
static gboolean
510 511 512 513
ip4_start (NMDhcpClient *client,
           const char *dhcp_anycast_addr,
           const char *last_ip4_address,
           GError **error)
514
{
515 516
	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
517 518
	GBytes *client_id;
	gs_unref_bytes GBytes *new_client_id = NULL;
519

520
	client_id = nm_dhcp_client_get_client_id (client);
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	priv->conf_file = create_dhclient_config (self,
	                                          AF_INET,
	                                          nm_dhcp_client_get_iface (client),
	                                          nm_dhcp_client_get_uuid (client),
	                                          client_id,
	                                          dhcp_anycast_addr,
	                                          nm_dhcp_client_get_hostname (client),
	                                          nm_dhcp_client_get_timeout (client),
	                                          nm_dhcp_client_get_use_fqdn (client),
	                                          &new_client_id);
	if (!priv->conf_file) {
		nm_utils_error_set_literal (error,
		                            NM_UTILS_ERROR_UNKNOWN,
		                            "error creating dhclient configuration file");
		return FALSE;
	}
538

539 540 541 542 543 544 545 546 547 548
	if (new_client_id) {
		nm_assert (!client_id);
		nm_dhcp_client_set_client_id (client, new_client_id);
	}
	return dhclient_start (client,
	                       NULL,
	                       FALSE,
	                       NULL,
	                       0,
	                       error);
549 550
}

551
static gboolean
552
ip6_start (NMDhcpClient *client,
553
           const char *dhcp_anycast_addr,
554
           const struct in6_addr *ll_addr,
555
           NMSettingIP6ConfigPrivacy privacy,
556 557
           guint needed_prefixes,
           GError **error)
558
{
559 560
	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
561

562 563 564 565 566 567 568 569 570 571
	priv->conf_file = create_dhclient_config (self,
	                                          AF_INET6,
	                                          nm_dhcp_client_get_iface (client),
	                                          nm_dhcp_client_get_uuid (client),
	                                          NULL,
	                                          dhcp_anycast_addr,
	                                          nm_dhcp_client_get_hostname (client),
	                                          nm_dhcp_client_get_timeout (client),
	                                          TRUE,
	                                          NULL);
572
	if (!priv->conf_file) {
573 574 575
		nm_utils_error_set_literal (error,
		                            NM_UTILS_ERROR_UNKNOWN,
		                            "error creating dhclient configuration file");
576
		return FALSE;
577 578
	}

579 580 581 582
	return dhclient_start (client,
	                       nm_dhcp_client_get_info_only (NM_DHCP_CLIENT (self))
	                         ? "-S"
	                         : "-N",
583 584 585 586
	                       FALSE,
	                       NULL,
	                       needed_prefixes,
	                       error);
587 588
}

589
static void
590
stop (NMDhcpClient *client, gboolean release)
591
{
592 593
	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
594
	int errsv;
595

596
	NM_DHCP_CLIENT_CLASS (nm_dhcp_dhclient_parent_class)->stop (client, release);
597 598

	if (priv->conf_file)
599 600
		if (remove (priv->conf_file) == -1) {
			errsv = errno;
601
			_LOGD ("could not remove dhcp config file \"%s\": %d (%s)", priv->conf_file, errsv, nm_strerror_native (errsv));
602
		}
603
	if (priv->pid_file) {
604
		if (remove (priv->pid_file) == -1) {
605
			errsv = errno;
606
			_LOGD ("could not remove dhcp pid file \"%s\": %s (%d)", priv->pid_file, nm_strerror_native (errsv), errsv);
607 608
		}
		nm_clear_g_free (&priv->pid_file);
609 610 611
	}

	if (release) {
612
		pid_t rpid = -1;
613

614 615 616 617 618 619
		if (dhclient_start (client,
		                    NULL,
		                    TRUE,
		                    &rpid,
		                    0,
		                    NULL)) {
620
			/* Wait a few seconds for the release to happen */
621
			nm_dhcp_client_stop_pid (rpid, nm_dhcp_client_get_iface (client));
622 623
		}
	}
624 625
}

626
static GBytes *
627
get_duid (NMDhcpClient *client)
628
{
629 630
	NMDhcpDhclient *self = NM_DHCP_DHCLIENT (client);
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
631
	GBytes *duid = NULL;
632
	gs_free char *leasefile = NULL;
633 634 635
	GError *error = NULL;

	/* Look in interface-specific leasefile first for backwards compat */
636 637
	leasefile = get_dhclient_leasefile (AF_INET6,
	                                    nm_dhcp_client_get_iface (client),
638
	                                    nm_dhcp_client_get_uuid (client),
639 640
	                                    NULL);
	if (leasefile) {
641
		_LOGD ("looking for DUID in '%s'", leasefile);
642 643
		duid = nm_dhcp_dhclient_read_duid (leasefile, &error);
		if (error) {
644 645
			_LOGW ("failed to read leasefile '%s': %s",
			       leasefile, error->message);
646 647
			g_clear_error (&error);
		}
648 649
		if (duid)
			return duid;
650 651
	}

652 653 654 655 656 657 658 659
	/* Otherwise read the default machine-wide DUID */
	_LOGD ("looking for default DUID in '%s'", priv->def_leasefile);
	duid = nm_dhcp_dhclient_read_duid (priv->def_leasefile, &error);
	if (error) {
		_LOGW ("failed to read leasefile '%s': %s",
		        priv->def_leasefile,
		        error->message);
		g_clear_error (&error);
660 661
	}

662
	return duid;
663 664
}

665
/*****************************************************************************/
666 667

static void
668
nm_dhcp_dhclient_init (NMDhcpDhclient *self)
669
{
670 671 672 673 674
	static const char *const FILES[] = {
		SYSCONFDIR "/dhclient6.leases", /* default */
		LOCALSTATEDIR "/lib/dhcp/dhclient6.leases",
		LOCALSTATEDIR "/lib/dhclient/dhclient6.leases",
	};
675
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
676
	int i;
677

678 679 680 681
	priv->def_leasefile = FILES[0];
	for (i = 0; i < G_N_ELEMENTS (FILES); i++) {
		if (g_file_test (FILES[i], G_FILE_TEST_EXISTS)) {
			priv->def_leasefile = FILES[i];
682 683 684 685
			break;
		}
	}

686 687
	priv->dhcp_listener = g_object_ref (nm_dhcp_listener_get ());
	g_signal_connect (priv->dhcp_listener,
688 689 690
	                  NM_DHCP_LISTENER_EVENT,
	                  G_CALLBACK (nm_dhcp_client_handle_event),
	                  self);
691 692 693 694 695
}

static void
dispose (GObject *object)
{
696
	NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE ((NMDhcpDhclient *) object);
697

698 699 700 701 702 703
	if (priv->dhcp_listener) {
		g_signal_handlers_disconnect_by_func (priv->dhcp_listener,
		                                      G_CALLBACK (nm_dhcp_client_handle_event),
		                                      NM_DHCP_DHCLIENT (object));
		g_clear_object (&priv->dhcp_listener);
	}
704

705 706 707
	nm_clear_g_free (&priv->pid_file);
	nm_clear_g_free (&priv->conf_file);
	nm_clear_g_free (&priv->lease_file);
708 709 710 711 712

	G_OBJECT_CLASS (nm_dhcp_dhclient_parent_class)->dispose (object);
}

static void
713
nm_dhcp_dhclient_class_init (NMDhcpDhclientClass *dhclient_class)
714
{
715
	NMDhcpClientClass *client_class = NM_DHCP_CLIENT_CLASS (dhclient_class);
716 717 718 719
	GObjectClass *object_class = G_OBJECT_CLASS (dhclient_class);

	object_class->dispose = dispose;

720 721 722
	client_class->ip4_start = ip4_start;
	client_class->ip6_start = ip6_start;
	client_class->stop = stop;
723
	client_class->get_duid = get_duid;
724 725
}

726 727 728 729 730
const NMDhcpClientFactory _nm_dhcp_client_factory_dhclient = {
	.name = "dhclient",
	.get_type = nm_dhcp_dhclient_get_type,
	.get_path = nm_dhcp_dhclient_get_path,
};
731

732
#endif /* WITH_DHCLIENT */