1    	/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2    	/* nm-dhcp-dhclient.c - dhclient specific hooks for NetworkManager
3    	 *
4    	 * This program is free software; you can redistribute it and/or modify
5    	 * it under the terms of the GNU General Public License as published by
6    	 * the Free Software Foundation; either version 2, or (at your option)
7    	 * any later version.
8    	 *
9    	 * This program is distributed in the hope that it will be useful,
10   	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   	 * GNU General Public License for more details.
13   	 *
14   	 * You should have received a copy of the GNU General Public License along
15   	 * with this program; if not, write to the Free Software Foundation, Inc.,
16   	 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17   	 *
18   	 * Copyright (C) 2005 - 2012 Red Hat, Inc.
19   	 */
20   	
21   	#define _XOPEN_SOURCE
22   	#include <time.h>
23   	#undef _XOPEN_SOURCE
24   	
25   	#include <glib.h>
26   	#include <glib/gi18n.h>
27   	#include <gio/gio.h>
28   	#include <string.h>
29   	#include <stdlib.h>
30   	#include <errno.h>
31   	#include <unistd.h>
32   	#include <stdio.h>
33   	#include <netinet/in.h>
34   	#include <arpa/inet.h>
35   	
36   	#include <config.h>
37   	
38   	#include "nm-dhcp-dhclient.h"
39   	#include "nm-utils.h"
40   	#include "nm-logging.h"
41   	#include "nm-dhcp-dhclient-utils.h"
42   	#include "nm-dhcp-manager.h"
43   	#include "nm-posix-signals.h"
44   	
45   	G_DEFINE_TYPE (NMDHCPDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT)
46   	
47   	#define NM_DHCP_DHCLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_DHCLIENT, NMDHCPDhclientPrivate))
48   	
49   	typedef struct {
50   		const char *path;
51   		char *conf_file;
52   		const char *def_leasefile;
53   		char *lease_file;
54   		char *pid_file;
55   	} NMDHCPDhclientPrivate;
56   	
57   	const char *
58   	nm_dhcp_dhclient_get_path (const char *try_first)
59   	{
60   		static const char *dhclient_paths[] = {
61   			"/sbin/dhclient",
62   			"/usr/sbin/dhclient",
63   			"/usr/pkg/sbin/dhclient",
64   			"/usr/local/sbin/dhclient",
65   			NULL
66   		};
67   		const char **path = dhclient_paths;
68   	
69   		if (strlen (try_first) && g_file_test (try_first, G_FILE_TEST_EXISTS))
70   			return try_first;
71   	
72   		while (*path != NULL) {
73   			if (g_file_test (*path, G_FILE_TEST_EXISTS))
74   				break;
75   			path++;
76   		}
77   	
78   		return *path;
79   	}
80   	
81   	/**
82   	 * get_dhclient_leasefile():
83   	 * @iface: the interface name of the device on which DHCP will be done
84   	 * @uuid: the connection UUID to which the returned lease should belong
85   	 * @ipv6: %TRUE for IPv6, %FALSE for IPv4
86   	 * @out_preferred_path: on return, the "most preferred" leasefile path
87   	 *
88   	 * Returns the path of an existing leasefile (if any) for this interface and
89   	 * connection UUID.  Also returns the "most preferred" leasefile path, which
90   	 * may be different than any found leasefile.
91   	 *
92   	 * Returns: an existing leasefile, or %NULL if no matching leasefile could be found
93   	 */
94   	static char *
95   	get_dhclient_leasefile (const char *iface,
96   	                        const char *uuid,
97   	                        gboolean ipv6,
98   	                        char **out_preferred_path)
99   	{
100  		char *path;
101  	
102  		/* /var/lib/NetworkManager is the preferred leasefile path */
103  		path = g_strdup_printf (NMSTATEDIR "/dhclient%s-%s-%s.lease",
104  		                        ipv6 ? "6" : "",
105  		                        uuid,
106  		                        iface);
107  		if (out_preferred_path)
108  			*out_preferred_path = g_strdup (path);
109  	
110  		if (g_file_test (path, G_FILE_TEST_EXISTS))
111  			return path;
112  	
113  		/* If the leasefile we're looking for doesn't exist yet in the new location
114  		 * (eg, /var/lib/NetworkManager) then look in old locations to maintain
115  		 * backwards compatibility with external tools (like dracut) that put
116  		 * leasefiles there.
117  		 */
118  	
119  		/* Old Debian, SUSE, and Mandriva location */
120  		g_free (path);
121  		path = g_strdup_printf (LOCALSTATEDIR "/lib/dhcp/dhclient%s-%s-%s.lease",
122  		                        ipv6 ? "6" : "", uuid, iface);
123  		if (g_file_test (path, G_FILE_TEST_EXISTS))
124  			return path;
125  	
126  		/* Old Red Hat and Fedora location */
127  		g_free (path);
128  		path = g_strdup_printf (LOCALSTATEDIR "/lib/dhclient/dhclient%s-%s-%s.lease",
129  		                        ipv6 ? "6" : "", uuid, iface);
130  		if (g_file_test (path, G_FILE_TEST_EXISTS))
131  			return path;
132  	
133  		/* Fail */
134  		g_free (path);
135  		return NULL;
136  	}
137  	
138  	static void
139  	add_lease_option (GHashTable *hash, char *line)
140  	{
141  		char *spc;
142  	
143  		spc = strchr (line, ' ');
144  		if (!spc) {
145  			nm_log_warn (LOGD_DHCP, "DHCP lease file line '%s' did not contain a space", line);
146  			return;
147  		}
148  	
149  		/* If it's an 'option' line, split at second space */
150  		if (g_str_has_prefix (line, "option ")) {
151  			spc = strchr (spc + 1, ' ');
152  			if (!spc) {
153  				nm_log_warn (LOGD_DHCP, "DHCP lease file option line '%s' did not contain a second space",
154  				             line);
155  				return;
156  			}
157  		}
158  	
159  		/* Split the line at the space */
160  		*spc = '\0';
161  		spc++;
162  	
163  		/* Kill the ';' at the end of the line, if any */
164  		if (*(spc + strlen (spc) - 1) == ';')
165  			*(spc + strlen (spc) - 1) = '\0';
166  	
167  		/* Treat 'interface' specially */
168  		if (g_str_has_prefix (line, "interface")) {
169  			if (*(spc) == '"')
170  				spc++; /* Jump past the " */
171  			if (*(spc + strlen (spc) - 1) == '"')
172  				*(spc + strlen (spc) - 1) = '\0';  /* Kill trailing " */
173  		}
174  	
175  		g_hash_table_insert (hash, g_strdup (line), g_strdup (spc));
176  	}
177  	
178  	GSList *
179  	nm_dhcp_dhclient_get_lease_config (const char *iface, const char *uuid, gboolean ipv6)
180  	{
181  		GSList *parsed = NULL, *iter, *leases = NULL;
182  		char *contents = NULL;
183  		char *leasefile;
184  		char **line, **split = NULL;
185  		GHashTable *hash = NULL;
186  	
187  		/* IPv6 not supported */
188  		if (ipv6)
189  			return NULL;
190  	
191  		leasefile = get_dhclient_leasefile (iface, uuid, FALSE, NULL);
192  		if (!leasefile)
193  			return NULL;
194  	
195  		if (!g_file_test (leasefile, G_FILE_TEST_EXISTS))
196  			goto out;
197  	
198  		if (!g_file_get_contents (leasefile, &contents, NULL, NULL))
199  			goto out;
200  	
201  		split = g_strsplit_set (contents, "\n\r", -1);
202  		g_free (contents);
203  		if (!split)
204  			goto out;
205  	
206  		for (line = split; line && *line; line++) {
207  			*line = g_strstrip (*line);
208  	
209  			if (!strcmp (*line, "}")) {
210  				/* Lease ends */
211  				parsed = g_slist_append (parsed, hash);
212  				hash = NULL;
213  			} else if (!strcmp (*line, "lease {")) {
214  				/* Beginning of a new lease */
215  				if (hash) {
216  					nm_log_warn (LOGD_DHCP, "DHCP lease file %s malformed; new lease started "
217  					             "without ending previous lease",
218  					             leasefile);
219  					g_hash_table_destroy (hash);
220  				}
221  	
222  				hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
223  			} else if (strlen (*line))
224  				add_lease_option (hash, *line);
225  		}
226  		g_strfreev (split);
227  	
228  		/* Check if the last lease in the file was properly ended */
229  		if (hash) {
230  			nm_log_warn (LOGD_DHCP, "DHCP lease file %s malformed; new lease started "
231  			             "without ending previous lease",
232  			             leasefile);
233  			g_hash_table_destroy (hash);
234  			hash = NULL;
235  		}
236  	
237  		for (iter = parsed; iter; iter = g_slist_next (iter)) {
238  			NMIP4Config *ip4;
239  			NMPlatformIP4Address address;
240  			const char *data;
241  			guint32 tmp;
242  			guint32 plen;
243  			struct tm expire;
244  	
245  			hash = iter->data;
246  	
247  			/* Make sure this lease is for the interface we want */
248  			data = g_hash_table_lookup (hash, "interface");
249  			if (!data || strcmp (data, iface))
250  				continue;
251  	
252  			data = g_hash_table_lookup (hash, "expire");
253  			if (data) {
254  				time_t now_tt;
255  				struct tm *now;
256  	
257  				/* Read lease expiration (in UTC) */
258  				if (!strptime (data, "%w %Y/%m/%d %H:%M:%S", &expire)) {
259  					nm_log_warn (LOGD_DHCP, "couldn't parse DHCP lease file expire time '%s'",
260  					             data);
261  					continue;
262  				}
263  	
264  				now_tt = time (NULL);
265  				now = gmtime(&now_tt);
266  	
267  				/* Ignore this lease if it's already expired */
268  				if (expire.tm_year < now->tm_year)
269  					continue;
270  				else if (expire.tm_year == now->tm_year) {
271  					if (expire.tm_mon < now->tm_mon)
272  						continue;
273  					else if (expire.tm_mon == now->tm_mon) {
274  						if (expire.tm_mday < now->tm_mday)
275  							continue;
276  						else if (expire.tm_mday == now->tm_mday) {
277  							if (expire.tm_hour < now->tm_hour)
278  								continue;
279  							else if (expire.tm_hour == now->tm_hour) {
280  								if (expire.tm_min < now->tm_min)
281  									continue;
282  								else if (expire.tm_min == now->tm_min) {
283  									if (expire.tm_sec <= now->tm_sec)
284  										continue;
285  								}
286  							}
287  						}
288  					}
289  				}
290  				/* If we get this far, the lease hasn't expired */
291  			}
292  	
293  			data = g_hash_table_lookup (hash, "fixed-address");
294  			if (!data)
295  				continue;
296  	
297  			ip4 = nm_ip4_config_new ();
298  			memset (&address, 0, sizeof (address));
299  	
300  			/* IP4 address */
301  			if (!inet_pton (AF_INET, data, &tmp)) {
302  				nm_log_warn (LOGD_DHCP, "couldn't parse DHCP lease file IP4 address '%s'", data);
303  				goto error;
304  			}
305  			address.address = tmp;
306  	
307  			/* Netmask */
308  			data = g_hash_table_lookup (hash, "option subnet-mask");
309  			if (data) {
310  				if (!inet_pton (AF_INET, data, &tmp)) {
311  					nm_log_warn (LOGD_DHCP, "couldn't parse DHCP lease file IP4 subnet mask '%s'", data);
312  					goto error;
313  				}
314  				plen = nm_utils_ip4_netmask_to_prefix (tmp);
315  			} else {
316  				/* Get default netmask for the IP according to appropriate class. */
317  				plen = nm_utils_ip4_get_default_prefix (address.address);
318  			}
319  			address.plen = plen;
320  	
321  			/* Gateway */
322  			data = g_hash_table_lookup (hash, "option routers");
323  			if (data) {
324  				if (!inet_pton (AF_INET, data, &tmp)) {
325  					nm_log_warn (LOGD_DHCP, "couldn't parse DHCP lease file IP4 gateway '%s'", data);
326  					goto error;
327  				}
328  				nm_ip4_config_set_gateway (ip4, tmp);
329  			}
330  	
331  			nm_ip4_config_add_address (ip4, &address);
332  			leases = g_slist_append (leases, ip4);
333  			continue;
334  	
335  		error:
336  			g_object_unref (ip4);
337  		}
338  	
339  	out:
340  		g_slist_free_full (parsed, (GDestroyNotify) g_hash_table_destroy);
341  		g_free (leasefile);
342  		return leases;
343  	}
344  	
345  	
346  	
347  	static gboolean
348  	merge_dhclient_config (const char *iface,
349  	                       const char *conf_file,
350  	                       gboolean is_ip6,
351  	                       NMSettingIP4Config *s_ip4,
352  	                       NMSettingIP6Config *s_ip6,
353  	                       guint8 *anycast_addr,
354  	                       const char *hostname,
355  	                       const char *orig_path,
356  	                       GError **error)
357  	{
358  		char *orig = NULL, *new;
359  		gboolean success = FALSE;
360  	
361  		g_return_val_if_fail (iface != NULL, FALSE);
362  		g_return_val_if_fail (conf_file != NULL, FALSE);
363  	
364  		if (orig_path && g_file_test (orig_path, G_FILE_TEST_EXISTS)) {
365  			GError *read_error = NULL;
366  	
367  			if (!g_file_get_contents (orig_path, &orig, NULL, &read_error)) {
368  				nm_log_warn (LOGD_DHCP, "(%s): error reading dhclient%s configuration %s: %s",
369  				             iface, is_ip6 ? "6" : "", orig_path, read_error->message);
370  				g_error_free (read_error);
371  			}
372  		}
373  	
374  		new = nm_dhcp_dhclient_create_config (iface, is_ip6, s_ip4, s_ip6, anycast_addr, hostname, orig_path, orig);
375  		g_assert (new);
376  		success = g_file_set_contents (conf_file, new, -1, error);
377  		g_free (new);
378  		g_free (orig);
379  	
380  		return success;
381  	}
382  	
383  	static char *
384  	find_existing_config (const char *iface, const char *uuid, gboolean ipv6)
385  	{
386  		char *path;
387  	
388  		/* NetworkManager-overridden configuration can be used to ship DHCP config
389  		 * with NetworkManager itself. It can be uuid-specific, device-specific
390  		 * or generic.
391  		 */
392  		if (uuid) {
393  			path = g_strdup_printf (NMCONFDIR "/dhclient%s-%s.conf", ipv6 ? "6" : "", uuid);
394  			nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
395  			if (g_file_test (path, G_FILE_TEST_EXISTS))
396  				return path;
397  			g_free (path);
398  		}
399  	
400  		path = g_strdup_printf (NMCONFDIR "/dhclient%s-%s.conf", ipv6 ? "6" : "", iface);
401  		nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
402  		if (g_file_test (path, G_FILE_TEST_EXISTS))
403  			return path;
404  		g_free (path);
405  	
406  		path = g_strdup_printf (NMCONFDIR "/dhclient%s.conf", ipv6 ? "6" : "");
407  		nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
408  		if (g_file_test (path, G_FILE_TEST_EXISTS))
409  			return path;
410  		g_free (path);
411  	
412  		/* Distribution's dhclient configuration is used so that we can use
413  		 * configuration shipped with dhclient (if any).
414  		 *
415  		 * This replaces conditional compilation based on distribution name. Fedora
416  		 * and Debian store the configs in /etc/dhcp while upstream defaults to /etc
417  		 * which is then used by many other distributions. Some distributions
418  		 * (including Fedora) don't even provide a default configuration file.
419  		 */
420  		path = g_strdup_printf (SYSCONFDIR "/dhcp/dhclient%s-%s.conf", ipv6 ? "6" : "", iface);
421  		nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
422  		if (g_file_test (path, G_FILE_TEST_EXISTS))
423  			return path;
424  		g_free (path);
425  	
426  		path = g_strdup_printf (SYSCONFDIR "/dhclient%s-%s.conf", ipv6 ? "6" : "", iface);
427  		nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
428  		if (g_file_test (path, G_FILE_TEST_EXISTS))
429  			return path;
430  		g_free (path);
431  	
432  		path = g_strdup_printf (SYSCONFDIR "/dhcp/dhclient%s.conf", ipv6 ? "6" : "");
433  		nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
434  		if (g_file_test (path, G_FILE_TEST_EXISTS))
435  			return path;
436  		g_free (path);
437  	
438  		path = g_strdup_printf (SYSCONFDIR "/dhclient%s.conf", ipv6 ? "6" : "");
439  		nm_log_dbg (ipv6 ? LOGD_DHCP6 : LOGD_DHCP4, "(%s) looking for existing config %s", iface, path);
440  		if (g_file_test (path, G_FILE_TEST_EXISTS))
441  			return path;
442  		g_free (path);
443  	
444  		return NULL;
445  	}
446  	
447  	
448  	/* NM provides interface-specific options; thus the same dhclient config
449  	 * file cannot be used since DHCP transactions can happen in parallel.
450  	 * Since some distros don't have default per-interface dhclient config files,
451  	 * read their single config file and merge that into a custom per-interface
452  	 * config file along with the NM options.
453  	 */
454  	static char *
455  	create_dhclient_config (const char *iface,
456  	                        gboolean is_ip6,
457  	                        const char *uuid,
458  	                        NMSettingIP4Config *s_ip4,
459  	                        NMSettingIP6Config *s_ip6,
460  	                        guint8 *dhcp_anycast_addr,
461  	                        const char *hostname)
462  	{
463  		char *orig = NULL, *new = NULL;
464  		GError *error = NULL;
465  		gboolean success = FALSE;
466  	
467  		g_return_val_if_fail (iface != NULL, NULL);
468  	
469  		new = g_strdup_printf (NMSTATEDIR "/dhclient%s-%s.conf", is_ip6 ? "6" : "", iface);
470  		nm_log_dbg (is_ip6 ? LOGD_DHCP6 : LOGD_DHCP4,
471  		            "(%s): creating composite dhclient config %s",
472  		            iface, new);
473  	
474  		orig = find_existing_config (iface, uuid, is_ip6);
475  		if (orig) {
476  			nm_log_dbg (is_ip6 ? LOGD_DHCP6 : LOGD_DHCP4,
477  			            "(%s): merging existing dhclient config %s",
478  			            iface, orig);
479  		} else {
480  			nm_log_dbg (is_ip6 ? LOGD_DHCP6 : LOGD_DHCP4,
481  			            "(%s): no existing dhclient configuration to merge",
482  			            iface);
483  		}
484  	
485  		error = NULL;
486  		success = merge_dhclient_config (iface, new, is_ip6, s_ip4, s_ip6, dhcp_anycast_addr, hostname, orig, &error);
487  		if (!success) {
488  			nm_log_warn (LOGD_DHCP, "(%s): error creating dhclient%s configuration: %s",
489  			             iface, is_ip6 ? "6" : "", error->message);
490  			g_error_free (error);
491  		}
492  	
493  		g_free (orig);
494  		return new;
495  	}
496  	
497  	
498  	static void
499  	dhclient_child_setup (gpointer user_data G_GNUC_UNUSED)
500  	{
501  		/* We are in the child process at this point */
502  		pid_t pid = getpid ();
503  		setpgid (pid, pid);
504  	
505  		/*
506  		 * We blocked signals in main(). We need to restore original signal
507  		 * mask for dhclient here so that it can receive signals.
508  		 */
509  		nm_unblock_posix_signals (NULL);
510  	}
511  	
512  	static GPid
513  	dhclient_start (NMDHCPClient *client,
514  	                const char *mode_opt,
515  	                const GByteArray *duid,
516  	                gboolean release)
517  	{
518  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
519  		GPtrArray *argv = NULL;
520  		GPid pid = -1;
521  		GError *error = NULL;
522  		const char *iface, *uuid, *system_bus_address;
523  		char *binary_name, *cmd_str, *pid_file = NULL, *system_bus_address_env = NULL;
524  		gboolean ipv6, success;
525  		guint log_domain;
526  		char *escaped, *preferred_leasefile_path = NULL;
527  	
528  		g_return_val_if_fail (priv->pid_file == NULL, -1);
529  	
530  		iface = nm_dhcp_client_get_iface (client);
531  		uuid = nm_dhcp_client_get_uuid (client);
532  		ipv6 = nm_dhcp_client_get_ipv6 (client);
533  	
534  		log_domain = ipv6 ? LOGD_DHCP6 : LOGD_DHCP4;
535  	
536  		if (!g_file_test (priv->path, G_FILE_TEST_EXISTS)) {
537  			nm_log_warn (log_domain, "%s does not exist.", priv->path);
538  			return -1;
539  		}
540  	
541  		pid_file = g_strdup_printf (LOCALSTATEDIR "/run/dhclient%s-%s.pid",
542  			                        ipv6 ? "6" : "",
543  			                        iface);
544  	
545  		/* Kill any existing dhclient from the pidfile */
546  		binary_name = g_path_get_basename (priv->path);
547  		nm_dhcp_client_stop_existing (pid_file, binary_name);
548  		g_free (binary_name);
549  	
550  		if (release) {
551  			/* release doesn't use the pidfile after killing an old client */
552  			g_free (pid_file);
553  			pid_file = NULL;
554  		}
555  	
556  		g_free (priv->lease_file);
557  		priv->lease_file = get_dhclient_leasefile (iface, uuid, ipv6, &preferred_leasefile_path);
558  		if (!priv->lease_file) {
559  			/* No existing leasefile, dhclient will create one at the preferred path */
560  			priv->lease_file = g_strdup (preferred_leasefile_path);
561  		} else if (g_strcmp0 (priv->lease_file, preferred_leasefile_path) != 0) {
562  			GFile *src = g_file_new_for_path (priv->lease_file);
563  			GFile *dst = g_file_new_for_path (preferred_leasefile_path);
564  	
565  			/* Try to copy the existing leasefile to the preferred location */
566  			if (g_file_copy (src, dst, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error)) {
567  				/* Success; use the preferred leasefile path */
568  				g_free (priv->lease_file);
569  				priv->lease_file = g_strdup (g_file_get_path (dst));
570  			} else {
571  				/* Failure; just use the existing leasefile */
572  				nm_log_warn (log_domain, "Failed to copy leasefile %s to %s: (%d) %s",
573  				             g_file_get_path (src), g_file_get_path (dst),
574  				             error->code, error->message);
575  				g_clear_error (&error);
576  			}
577  			g_object_unref (src);
578  			g_object_unref (dst);
579  		}
580  		g_free (preferred_leasefile_path);
581  	
582  		/* Save the DUID to the leasefile dhclient will actually use */
583  		if (ipv6) {
584  			escaped = nm_dhcp_dhclient_escape_duid (duid);
585  			success = nm_dhcp_dhclient_save_duid (priv->lease_file, escaped, &error);
586  			g_free (escaped);
587  			if (!success) {
588  				nm_log_warn (log_domain, "(%s): failed to save DUID to %s: (%d) %s.",
589  				             iface, priv->lease_file,
590  				             error ? error->code : -1,
591  				             error && error->message ? error->message : "(unknown)");
592  				return -1;
593  			}
594  		}
595  	
596  		argv = g_ptr_array_new ();
597  		g_ptr_array_add (argv, (gpointer) priv->path);
598  	
599  		g_ptr_array_add (argv, (gpointer) "-d");
600  	
601  		if (release)
602  			g_ptr_array_add (argv, (gpointer) "-r");
603  	
604  		if (ipv6) {
605  			g_ptr_array_add (argv, (gpointer) "-6");
606  			if (mode_opt)
607  				g_ptr_array_add (argv, (gpointer) mode_opt);
608  		}
609  		g_ptr_array_add (argv, (gpointer) "-sf");	/* Set script file */
610  		g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path);
611  	
612  		if (pid_file) {
613  			g_ptr_array_add (argv, (gpointer) "-pf");	/* Set pid file */
614  			g_ptr_array_add (argv, (gpointer) pid_file);
615  		}
616  	
617  		g_ptr_array_add (argv, (gpointer) "-lf");	/* Set lease file */
618  		g_ptr_array_add (argv, (gpointer) priv->lease_file);
619  	
620  		if (priv->conf_file) {
621  			g_ptr_array_add (argv, (gpointer) "-cf");	/* Set interface config file */
622  			g_ptr_array_add (argv, (gpointer) priv->conf_file);
623  		}
624  	
625  		/* Usually the system bus address is well-known; but if it's supposed
626  		 * to be something else, we need to push it to dhclient, since dhclient
627  		 * sanitizes the environment it gives the action scripts.
628  		 */
629  		system_bus_address = getenv ("DBUS_SYSTEM_BUS_ADDRESS");
630  		if (system_bus_address) {
631  			system_bus_address_env = g_strdup_printf ("DBUS_SYSTEM_BUS_ADDRESS=%s", system_bus_address);
632  			g_ptr_array_add (argv, (gpointer) "-e");
633  			g_ptr_array_add (argv, (gpointer) system_bus_address_env);
634  		}
635  	
636  	
637  		g_ptr_array_add (argv, (gpointer) iface);
638  		g_ptr_array_add (argv, NULL);
639  	
640  		cmd_str = g_strjoinv (" ", (gchar **) argv->pdata);
641  		nm_log_dbg (log_domain, "running: %s", cmd_str);
642  		g_free (cmd_str);
643  	
644  		if (!g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
645  		                    &dhclient_child_setup, NULL, &pid, &error)) {
646  			nm_log_warn (log_domain, "dhclient failed to start: '%s'", error->message);
647  			g_error_free (error);
648  			pid = -1;
649  		} else {
650  			nm_log_info (log_domain, "dhclient started with pid %d", pid);
651  			priv->pid_file = pid_file;
652  		}
653  	
654  		g_ptr_array_free (argv, TRUE);
655  		g_free (system_bus_address_env);
656  		return pid;
657  	}
658  	
659  	static GPid
660  	ip4_start (NMDHCPClient *client,
661  	           NMSettingIP4Config *s_ip4,
662  	           guint8 *dhcp_anycast_addr,
663  	           const char *hostname)
664  	{
665  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
666  		const char *iface, *uuid;
667  	
668  		iface = nm_dhcp_client_get_iface (client);
669  		uuid = nm_dhcp_client_get_uuid (client);
670  	
671  		priv->conf_file = create_dhclient_config (iface, FALSE, uuid, s_ip4, NULL, dhcp_anycast_addr, hostname);
672  		if (!priv->conf_file) {
673  			nm_log_warn (LOGD_DHCP4, "(%s): error creating dhclient configuration file.", iface);
674  			return -1;
675  		}
676  	
677  		return dhclient_start (client, NULL, NULL, FALSE);
678  	}
679  	
680  	static GPid
681  	ip6_start (NMDHCPClient *client,
682  	           NMSettingIP6Config *s_ip6,
683  	           guint8 *dhcp_anycast_addr,
684  	           const char *hostname,
685  	           gboolean info_only,
686  	           const GByteArray *duid)
687  	{
688  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
689  		const char *iface, *uuid;
690  	
691  		iface = nm_dhcp_client_get_iface (client);
692  		uuid = nm_dhcp_client_get_uuid (client);
693  	
694  		priv->conf_file = create_dhclient_config (iface, TRUE, uuid, NULL, s_ip6, dhcp_anycast_addr, hostname);
695  		if (!priv->conf_file) {
696  			nm_log_warn (LOGD_DHCP6, "(%s): error creating dhclient6 configuration file.", iface);
697  			return -1;
698  		}
699  	
700  		return dhclient_start (client, info_only ? "-S" : "-N", duid, FALSE);
701  	}
702  	
703  	static void
704  	stop (NMDHCPClient *client, gboolean release, const GByteArray *duid)
705  	{
706  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
707  	
708  		/* Chain up to parent */
709  		NM_DHCP_CLIENT_CLASS (nm_dhcp_dhclient_parent_class)->stop (client, release, duid);
710  	
(1) Event cond_true: Condition "priv->conf_file", taking true branch
711  		if (priv->conf_file)
712  			remove (priv->conf_file);
(2) Event cond_true: Condition "priv->pid_file", taking true branch
713  		if (priv->pid_file) {
(3) Event check_return: Calling function "remove(priv->pid_file)" without checking return value. This library function may fail and return an error code.
(4) Event unchecked_value: No check of the return value of "remove(priv->pid_file)".
714  			remove (priv->pid_file);
715  			g_free (priv->pid_file);
716  			priv->pid_file = NULL;
717  		}
718  	
719  		if (release) {
720  			GPid rpid;
721  	
722  			rpid = dhclient_start (client, NULL, duid, TRUE);
723  			if (rpid > 0) {
724  				/* Wait a few seconds for the release to happen */
725  				nm_dhcp_client_stop_pid (rpid, nm_dhcp_client_get_iface (client), 5);
726  			}
727  		}
728  	}
729  	
730  	static GByteArray *
731  	get_duid (NMDHCPClient *client)
732  	{
733  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
734  		GByteArray *duid = NULL;
735  		char *leasefile;
736  		GError *error = NULL;
737  	
738  		/* Look in interface-specific leasefile first for backwards compat */
739  		leasefile = get_dhclient_leasefile (nm_dhcp_client_get_iface (client),
740  		                                    nm_dhcp_client_get_uuid (client),
741  		                                    TRUE,
742  		                                    NULL);
743  		if (leasefile) {
744  			nm_log_dbg (LOGD_DHCP, "Looking for DHCPv6 DUID in '%s'.", leasefile);
745  			duid = nm_dhcp_dhclient_read_duid (leasefile, &error);
746  			g_free (leasefile);
747  	
748  			if (error) {
749  				nm_log_warn (LOGD_DHCP, "Failed to read leasefile '%s': (%d) %s",
750  				             leasefile, error->code, error->message);
751  				g_clear_error (&error);
752  			}
753  		}
754  	
755  		if (!duid && priv->def_leasefile) {
756  			/* Otherwise read the default machine-wide DUID */
757  			nm_log_dbg (LOGD_DHCP, "Looking for default DHCPv6 DUID in '%s'.", priv->def_leasefile);
758  			duid = nm_dhcp_dhclient_read_duid (priv->def_leasefile, &error);
759  			if (error) {
760  				nm_log_warn (LOGD_DHCP, "Failed to read leasefile '%s': (%d) %s",
761  				             priv->def_leasefile,
762  				             error ? error->code : -1,
763  				             error ? error->message : "(unknown)");
764  				g_clear_error (&error);
765  			}
766  		}
767  	
768  		/* return our DUID, otherwise let the parent class make a default DUID */
769  		return duid ? duid : NM_DHCP_CLIENT_CLASS (nm_dhcp_dhclient_parent_class)->get_duid (client);
770  	}
771  	
772  	/***************************************************/
773  	
774  	static const char *def_leasefiles[] = {
775  		SYSCONFDIR "/dhclient6.leases",
776  		LOCALSTATEDIR "/lib/dhcp/dhclient6.leases",
777  		LOCALSTATEDIR "/lib/dhclient/dhclient6.leases",
778  		NULL
779  	};
780  	
781  	static void
782  	nm_dhcp_dhclient_init (NMDHCPDhclient *self)
783  	{
784  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (self);
785  		const char **iter = &def_leasefiles[0];
786  	
787  		priv->path = nm_dhcp_dhclient_get_path (DHCLIENT_PATH);
788  	
789  		while (iter && *iter) {
790  			if (g_file_test (*iter, G_FILE_TEST_EXISTS)) {
791  				priv->def_leasefile = *iter;
792  				break;
793  			}
794  			iter++;
795  		}
796  	
797  		/* Fallback option */
798  		if (!priv->def_leasefile)
799  			priv->def_leasefile = SYSCONFDIR "/dhclient6.leases";
800  	}
801  	
802  	static void
803  	dispose (GObject *object)
804  	{
805  		NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (object);
806  	
807  		g_free (priv->pid_file);
808  		g_free (priv->conf_file);
809  		g_free (priv->lease_file);
810  	
811  		G_OBJECT_CLASS (nm_dhcp_dhclient_parent_class)->dispose (object);
812  	}
813  	
814  	static void
815  	nm_dhcp_dhclient_class_init (NMDHCPDhclientClass *dhclient_class)
816  	{
817  		NMDHCPClientClass *client_class = NM_DHCP_CLIENT_CLASS (dhclient_class);
818  		GObjectClass *object_class = G_OBJECT_CLASS (dhclient_class);
819  	
820  		g_type_class_add_private (dhclient_class, sizeof (NMDHCPDhclientPrivate));
821  	
822  		/* virtual methods */
823  		object_class->dispose = dispose;
824  	
825  		client_class->ip4_start = ip4_start;
826  		client_class->ip6_start = ip6_start;
827  		client_class->stop = stop;
828  		client_class->get_duid = get_duid;
829  	}
830  	
831