nm-dhcp-dhcpcanon.c 8.55 KB
Newer Older
juga's avatar
juga committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-dhcp-dhcpcanon.c - dhcpcanon 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.
 *
 * Copyright (C) 2017 juga <juga at riseup dot net>
 */

#include "nm-default.h"

#if WITH_DHCPCANON

#include <stdlib.h>
#include <unistd.h>

#include "nm-utils.h"
#include "nm-dhcp-manager.h"
#include "NetworkManagerUtils.h"
#include "nm-dhcp-listener.h"
#include "nm-dhcp-client-logging.h"

#define NM_TYPE_DHCP_DHCPCANON            (nm_dhcp_dhcpcanon_get_type ())
#define NM_DHCP_DHCPCANON(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_DHCPCANON, NMDhcpDhcpcanon))
#define NM_DHCP_DHCPCANON_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_DHCPCANON, NMDhcpDhcpcanonClass))
#define NM_IS_DHCP_DHCPCANON(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_DHCPCANON))
#define NM_IS_DHCP_DHCPCANON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DHCP_DHCPCANON))
#define NM_DHCP_DHCPCANON_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_DHCPCANON, NMDhcpDhcpcanonClass))

typedef struct _NMDhcpDhcpcanon NMDhcpDhcpcanon;
typedef struct _NMDhcpDhcpcanonClass NMDhcpDhcpcanonClass;

static GType nm_dhcp_dhcpcanon_get_type (void);

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

typedef struct {
	char *conf_file;
	const char *def_leasefile;
	char *lease_file;
	char *pid_file;
	NMDhcpListener *dhcp_listener;
} NMDhcpDhcpcanonPrivate;

struct _NMDhcpDhcpcanon {
	NMDhcpClient parent;
	NMDhcpDhcpcanonPrivate _priv;
};

struct _NMDhcpDhcpcanonClass {
	NMDhcpClientClass parent;
};

G_DEFINE_TYPE (NMDhcpDhcpcanon, nm_dhcp_dhcpcanon, NM_TYPE_DHCP_CLIENT)

#define NM_DHCP_DHCPCANON_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDhcpDhcpcanon, NM_IS_DHCP_DHCPCANON)

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

static const char *
nm_dhcp_dhcpcanon_get_path (void)
{
	return nm_utils_find_helper ("dhcpcanon", DHCPCANON_PATH, NULL);
}

static gboolean
dhcpcanon_start (NMDhcpClient *client,
                const char *mode_opt,
80
                GBytes *duid,
juga's avatar
juga committed
81 82
                gboolean release,
                pid_t *out_pid,
83 84
                guint needed_prefixes,
                GError **error)
juga's avatar
juga committed
85 86 87
{
	NMDhcpDhcpcanon *self = NM_DHCP_DHCPCANON (client);
	NMDhcpDhcpcanonPrivate *priv = NM_DHCP_DHCPCANON_GET_PRIVATE (self);
88
	gs_unref_ptrarray GPtrArray *argv = NULL;
juga's avatar
juga committed
89
	pid_t pid;
90 91 92 93 94 95 96
	gs_free_error GError *local = NULL;
	const char *iface;
	const char *system_bus_address;
	const char *dhcpcanon_path;
	gs_free char *binary_name = NULL;
	gs_free char *pid_file = NULL;
	gs_free char *system_bus_address_env = NULL;
juga's avatar
juga committed
97 98
	int addr_family;

99
	g_return_val_if_fail (!priv->pid_file, FALSE);
juga's avatar
juga committed
100 101

	iface = nm_dhcp_client_get_iface (client);
102

juga's avatar
juga committed
103
	addr_family = nm_dhcp_client_get_addr_family (client);
104

juga's avatar
juga committed
105 106
	dhcpcanon_path = nm_dhcp_dhcpcanon_get_path ();
	if (!dhcpcanon_path) {
107
		nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcanon binary not found");
juga's avatar
juga committed
108 109 110
		return FALSE;
	}

111 112
	_LOGD ("dhcpcanon_path: %s", dhcpcanon_path);

juga's avatar
juga committed
113 114 115 116 117 118 119 120 121 122 123 124
	pid_file = g_strdup_printf (RUNSTATEDIR "/dhcpcanon%c-%s.pid",
	                            nm_utils_addr_family_to_char (addr_family),
	                            iface);
	_LOGD ("pid_file: %s", pid_file);

	/* Kill any existing dhcpcanon from the pidfile */
	binary_name = g_path_get_basename (dhcpcanon_path);
	nm_dhcp_client_stop_existing (pid_file, binary_name);

	argv = g_ptr_array_new ();
	g_ptr_array_add (argv, (gpointer) dhcpcanon_path);

125
	g_ptr_array_add (argv, (gpointer) "-sf"); /* Set script file */
juga's avatar
juga committed
126 127
	g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path);

128 129
	g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */
	g_ptr_array_add (argv, (gpointer) pid_file);
juga's avatar
juga committed
130 131

	if (priv->conf_file) {
132
		g_ptr_array_add (argv, (gpointer) "-cf"); /* Set interface config file */
juga's avatar
juga committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		g_ptr_array_add (argv, (gpointer) priv->conf_file);
	}

	/* Usually the system bus address is well-known; but if it's supposed
	 * to be something else, we need to push it to dhcpcanon, since dhcpcanon
	 * 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);
	}

	g_ptr_array_add (argv, (gpointer) iface);
	g_ptr_array_add (argv, NULL);

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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,
	                   nm_utils_setpgid,
	                   NULL,
	                   &pid,
	                   &local)) {
		nm_utils_error_set (error,
		                    NM_UTILS_ERROR_UNKNOWN,
		                    "dhcpcanon failed to start: %s",
		                    local->message);
		return FALSE;
juga's avatar
juga committed
165 166
	}

167 168 169 170 171
	nm_assert (pid > 0);
	_LOGI ("dhcpcanon started with pid %d", pid);
	nm_dhcp_client_watch_child (client, pid);
	priv->pid_file = g_steal_pointer (&pid_file);
	return TRUE;
juga's avatar
juga committed
172 173 174
}

static gboolean
175 176 177 178
ip4_start (NMDhcpClient *client,
           const char *dhcp_anycast_addr,
           const char *last_ip4_address,
           GError **error)
juga's avatar
juga committed
179
{
180 181 182 183 184 185 186
	return dhcpcanon_start (client,
	                        NULL,
	                        NULL,
	                        FALSE,
	                        NULL,
	                        0,
	                        error);
juga's avatar
juga committed
187 188 189 190 191 192 193
}

static gboolean
ip6_start (NMDhcpClient *client,
           const char *dhcp_anycast_addr,
           const struct in6_addr *ll_addr,
           NMSettingIP6ConfigPrivacy privacy,
194 195
           guint needed_prefixes,
           GError **error)
juga's avatar
juga committed
196
{
197
	nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcanon plugin does not support IPv6");
juga's avatar
juga committed
198 199
	return FALSE;
}
200

juga's avatar
juga committed
201
static void
202
stop (NMDhcpClient *client, gboolean release)
juga's avatar
juga committed
203 204 205
{
	NMDhcpDhcpcanon *self = NM_DHCP_DHCPCANON (client);
	NMDhcpDhcpcanonPrivate *priv = NM_DHCP_DHCPCANON_GET_PRIVATE (self);
206
	int errsv;
juga's avatar
juga committed
207

208
	NM_DHCP_CLIENT_CLASS (nm_dhcp_dhcpcanon_parent_class)->stop (client, release);
juga's avatar
juga committed
209 210

	if (priv->pid_file) {
211 212 213 214
		if (remove (priv->pid_file) == -1) {
			errsv = errno;
			_LOGD ("could not remove dhcp pid file \"%s\": %d (%s)", priv->pid_file, errsv, g_strerror (errsv));
		}
juga's avatar
juga committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		g_free (priv->pid_file);
		priv->pid_file = NULL;
	}
}

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

static void
nm_dhcp_dhcpcanon_init (NMDhcpDhcpcanon *self)
{
	NMDhcpDhcpcanonPrivate *priv = NM_DHCP_DHCPCANON_GET_PRIVATE (self);

	priv->dhcp_listener = g_object_ref (nm_dhcp_listener_get ());
	g_signal_connect (priv->dhcp_listener,
	                  NM_DHCP_LISTENER_EVENT,
	                  G_CALLBACK (nm_dhcp_client_handle_event),
	                  self);
}

static void
dispose (GObject *object)
{
	NMDhcpDhcpcanonPrivate *priv = NM_DHCP_DHCPCANON_GET_PRIVATE ((NMDhcpDhcpcanon *) object);

	if (priv->dhcp_listener) {
		g_signal_handlers_disconnect_by_func (priv->dhcp_listener,
		                                      G_CALLBACK (nm_dhcp_client_handle_event),
		                                      NM_DHCP_DHCPCANON (object));
		g_clear_object (&priv->dhcp_listener);
	}

	nm_clear_g_free (&priv->pid_file);

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

static void
nm_dhcp_dhcpcanon_class_init (NMDhcpDhcpcanonClass *dhcpcanon_class)
{
	NMDhcpClientClass *client_class = NM_DHCP_CLIENT_CLASS (dhcpcanon_class);
	GObjectClass *object_class = G_OBJECT_CLASS (dhcpcanon_class);

	object_class->dispose = dispose;

	client_class->ip4_start = ip4_start;
	client_class->ip6_start = ip6_start;
	client_class->stop = stop;
}

const NMDhcpClientFactory _nm_dhcp_client_factory_dhcpcanon = {
	.name = "dhcpcanon",
	.get_type = nm_dhcp_dhcpcanon_get_type,
	.get_path = nm_dhcp_dhcpcanon_get_path,
};

#endif /* WITH_DHCPCANON */