summary refs log tree commit diff
path: root/src/ppp-manager/nm-pppd-plugin.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ppp-manager/nm-pppd-plugin.c')
-rw-r--r--src/ppp-manager/nm-pppd-plugin.c264
1 files changed, 161 insertions, 103 deletions
diff --git a/src/ppp-manager/nm-pppd-plugin.c b/src/ppp-manager/nm-pppd-plugin.c
index e6ee86f7..d5e8d707 100644
--- a/src/ppp-manager/nm-pppd-plugin.c
+++ b/src/ppp-manager/nm-pppd-plugin.c
@@ -19,6 +19,8 @@
  * Copyright (C) 2008 Red Hat, Inc.
  */
 
+#include "config.h"
+
 #include <string.h>
 #include <pppd/pppd.h>
 #include <pppd/fsm.h>
@@ -26,20 +28,22 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include <glib.h>
-#include <glib-object.h>
-#include <dbus/dbus-glib.h>
+#include <dlfcn.h>
+#include <gio/gio.h>
+
+#define INET6
+#include <pppd/eui64.h>
+#include <pppd/ipv6cp.h>
 
-#include "NetworkManager.h"
+#include "nm-dbus-interface.h"
 #include "nm-pppd-plugin.h"
 #include "nm-ppp-status.h"
-#include "nm-dbus-glib-types.h"
 
 int plugin_init (void);
 
 char pppd_version[] = VERSION;
 
-static DBusGProxy *proxy = NULL;
+static GDBusProxy *proxy = NULL;
 
 static void
 nm_phasechange (void *data, int arg)
@@ -47,7 +51,7 @@ nm_phasechange (void *data, int arg)
 	NMPPPStatus ppp_status = NM_PPP_STATUS_UNKNOWN;
 	char *ppp_phase;
 
-	g_return_if_fail (DBUS_IS_G_PROXY (proxy));
+	g_return_if_fail (G_IS_DBUS_PROXY (proxy));
 
 	switch (arg) {
 	case PHASE_DEAD:
@@ -114,56 +118,24 @@ nm_phasechange (void *data, int arg)
 	           ppp_phase);
 
 	if (ppp_status != NM_PPP_STATUS_UNKNOWN) {
-		dbus_g_proxy_call_no_reply (proxy, "SetState",
-		                            G_TYPE_UINT, ppp_status, G_TYPE_INVALID,
-		                            G_TYPE_INVALID);
+		g_dbus_proxy_call (proxy,
+		                   "SetState",
+		                   g_variant_new ("(u)", ppp_status),
+		                   G_DBUS_CALL_FLAGS_NONE, -1,
+		                   NULL,
+		                   NULL, NULL);
 	}
 }
 
-static GValue *
-str_to_gvalue (const char *str)
-{
-	GValue *val;
-
-	val = g_slice_new0 (GValue);
-	g_value_init (val, G_TYPE_STRING);
-	g_value_set_string (val, str);
-
-	return val;
-}
-
-static GValue *
-uint_to_gvalue (guint32 i)
-{
-	GValue *val;
-
-	val = g_slice_new0 (GValue);
-	g_value_init (val, G_TYPE_UINT);
-	g_value_set_uint (val, i);
-
-	return val;
-}
-
-static void
-value_destroy (gpointer data)
-{
-	GValue *val = (GValue *) data;
-
-	g_value_unset (val);
-	g_slice_free (GValue, val);
-}
-
 static void
 nm_ip_up (void *data, int arg)
 {
 	ipcp_options opts = ipcp_gotoptions[0];
 	ipcp_options peer_opts = ipcp_hisoptions[0];
-	GHashTable *hash;
-	GArray *array;
-	GValue *val;
+	GVariantBuilder builder;
 	guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit);
 
-	g_return_if_fail (DBUS_IS_G_PROXY (proxy));
+	g_return_if_fail (G_IS_DBUS_PROXY (proxy));
 
 	g_message ("nm-ppp-plugin: (%s): ip-up event", __func__);
 
@@ -173,70 +145,122 @@ nm_ip_up (void *data, int arg)
 		return;
 	}
 
-	hash = g_hash_table_new_full (g_str_hash, g_str_equal,
-	                              NULL, value_destroy);
+	g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
 
-	g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_INTERFACE, 
-	                     str_to_gvalue (ifname));
+	g_variant_builder_add (&builder, "{sv}",
+	                       NM_PPP_IP4_CONFIG_INTERFACE,
+	                       g_variant_new_string (ifname));
 
-	g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_ADDRESS, 
-	                     uint_to_gvalue (opts.ouraddr));
+	g_variant_builder_add (&builder, "{sv}",
+	                       NM_PPP_IP4_CONFIG_ADDRESS,
+	                       g_variant_new_uint32 (opts.ouraddr));
 
 	/* Prefer the peer options remote address first, _unless_ pppd made the
 	 * address up, at which point prefer the local options remote address,
 	 * and if that's not right, use the made-up address as a last resort.
 	 */
 	if (peer_opts.hisaddr && (peer_opts.hisaddr != pppd_made_up_address)) {
-		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, 
-		                     uint_to_gvalue (peer_opts.hisaddr));
+		g_variant_builder_add (&builder, "{sv}",
+		                       NM_PPP_IP4_CONFIG_GATEWAY,
+		                       g_variant_new_uint32 (peer_opts.hisaddr));
 	} else if (opts.hisaddr) {
-		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, 
-		                     uint_to_gvalue (opts.hisaddr));
+		g_variant_builder_add (&builder, "{sv}",
+		                       NM_PPP_IP4_CONFIG_GATEWAY,
+		                       g_variant_new_uint32 (opts.hisaddr));
 	} else if (peer_opts.hisaddr == pppd_made_up_address) {
 		/* As a last resort, use the made-up address */
-		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, 
-		                     uint_to_gvalue (peer_opts.hisaddr));
+		g_variant_builder_add (&builder, "{sv}",
+		                       NM_PPP_IP4_CONFIG_GATEWAY,
+		                       g_variant_new_uint32 (peer_opts.ouraddr));
 	}
 
-	g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_PREFIX, uint_to_gvalue (32));
+	g_variant_builder_add (&builder, "{sv}",
+	                       NM_PPP_IP4_CONFIG_PREFIX,
+	                       g_variant_new_uint32 (32));
 
 	if (opts.dnsaddr[0] || opts.dnsaddr[1]) {
-		array = g_array_new (FALSE, FALSE, sizeof (guint32));
+		guint32 dns[2];
+		int len = 0;
 
 		if (opts.dnsaddr[0])
-			g_array_append_val (array, opts.dnsaddr[0]);
+			dns[len++] = opts.dnsaddr[0];
 		if (opts.dnsaddr[1])
-			g_array_append_val (array, opts.dnsaddr[1]);
+			dns[len++] = opts.dnsaddr[1];
 
-		val = g_slice_new0 (GValue);
-		g_value_init (val, DBUS_TYPE_G_UINT_ARRAY);
-		g_value_set_boxed (val, array);
-
-		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_DNS, val);
+		g_variant_builder_add (&builder, "{sv}",
+		                       NM_PPP_IP4_CONFIG_DNS,
+		                       g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
+		                                                  dns, len, sizeof (guint32)));
 	}
 
 	if (opts.winsaddr[0] || opts.winsaddr[1]) {
-		array = g_array_new (FALSE, FALSE, sizeof (guint32));
+		guint32 wins[2];
+		int len = 0;
 
 		if (opts.winsaddr[0])
-			g_array_append_val (array, opts.winsaddr[0]);
+			wins[len++] = opts.winsaddr[0];
 		if (opts.winsaddr[1])
-			g_array_append_val (array, opts.winsaddr[1]);
-
-		val = g_slice_new0 (GValue);
-		g_value_init (val, DBUS_TYPE_G_UINT_ARRAY);
-		g_value_set_boxed (val, array);
+			wins[len++] = opts.winsaddr[1];
 
-		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_WINS, val);
+		g_variant_builder_add (&builder, "{sv}",
+		                       NM_PPP_IP4_CONFIG_WINS,
+		                       g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
+		                                                  wins, len, sizeof (guint32)));
 	}
 
-	g_message ("nm-ppp-plugin: (%s): sending Ip4Config to NetworkManager...", __func__);
+	g_message ("nm-ppp-plugin: (%s): sending IPv4 config to NetworkManager...", __func__);
+
+	g_dbus_proxy_call (proxy,
+	                   "SetIp4Config",
+	                   g_variant_new ("(a{sv})", &builder),
+	                   G_DBUS_CALL_FLAGS_NONE, -1,
+	                   NULL,
+	                   NULL, NULL);
+}
+
+static GVariant *
+eui64_to_variant (eui64_t eui)
+{
+	guint64 iid;
+
+	G_STATIC_ASSERT (sizeof (iid) == sizeof (eui));
 
-	dbus_g_proxy_call_no_reply (proxy, "SetIp4Config",
-	                            DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID,
-	                            G_TYPE_INVALID);
+	memcpy (&iid, &eui, sizeof (eui));
+	return g_variant_new_uint64 (iid);
+}
 
-	g_hash_table_destroy (hash);
+static void
+nm_ip6_up (void *data, int arg)
+{
+	ipv6cp_options *ho = &ipv6cp_hisoptions[0];
+	ipv6cp_options *go = &ipv6cp_gotoptions[0];
+	GVariantBuilder builder;
+
+	g_return_if_fail (G_IS_DBUS_PROXY (proxy));
+
+	g_message ("nm-ppp-plugin: (%s): ip6-up event", __func__);
+
+	g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
+	g_variant_builder_add (&builder, "{sv}",
+	                       NM_PPP_IP6_CONFIG_INTERFACE,
+	                       g_variant_new_string (ifname));
+	g_variant_builder_add (&builder, "{sv}",
+	                       NM_PPP_IP6_CONFIG_OUR_IID,
+	                       eui64_to_variant (go->ourid));
+	g_variant_builder_add (&builder, "{sv}",
+	                       NM_PPP_IP6_CONFIG_PEER_IID,
+	                       eui64_to_variant (ho->hisid));
+
+	/* DNS is done via DHCPv6 or router advertisements */
+
+	g_message ("nm-ppp-plugin: (%s): sending IPv6 config to NetworkManager...", __func__);
+
+	g_dbus_proxy_call (proxy,
+	                   "SetIp6Config",
+	                   g_variant_new ("(a{sv})", &builder),
+	                   G_DBUS_CALL_FLAGS_NONE, -1,
+	                   NULL,
+	                   NULL, NULL);
 }
 
 static int
@@ -254,9 +278,10 @@ get_pap_check (void)
 static int
 get_credentials (char *username, char *password)
 {
-	char *my_username = NULL;
-	char *my_password = NULL;
+	const char *my_username = NULL;
+	const char *my_password = NULL;
 	size_t len;
+	GVariant *ret;
 	GError *err = NULL;
 
 	if (!password) {
@@ -266,15 +291,16 @@ get_credentials (char *username, char *password)
 	}
 
 	g_return_val_if_fail (username, -1);
-	g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), -1);
+	g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
 
 	g_message ("nm-ppp-plugin: (%s): passwd-hook, requesting credentials...", __func__);
 
-	if (!dbus_g_proxy_call (proxy, "NeedSecrets", &err,
-	                        G_TYPE_INVALID,
-	                        G_TYPE_STRING, &my_username,
-	                        G_TYPE_STRING, &my_password,
-	                        G_TYPE_INVALID)) {
+	ret = g_dbus_proxy_call_sync (proxy,
+	                              "NeedSecrets",
+	                              NULL,
+	                              G_DBUS_CALL_FLAGS_NONE, -1,
+	                              NULL, &err);
+	if (!ret) {
 		g_warning ("nm-ppp-plugin: (%s): could not get secrets: (%d) %s",
 		           __func__,
 		           err ? err->code : -1,
@@ -285,14 +311,14 @@ get_credentials (char *username, char *password)
 
 	g_message ("nm-ppp-plugin: (%s): got credentials from NetworkManager", __func__);
 
+	g_variant_get (ret, "(&s&s)", &my_username, &my_password);
+
 	if (my_username) {
 		len = strlen (my_username) + 1;
 		len = len < MAXNAMELEN ? len : MAXNAMELEN;
 
 		strncpy (username, my_username, len);
 		username[len - 1] = '\0';
-
-		g_free (my_username);
 	}
 
 	if (my_password) {
@@ -301,17 +327,17 @@ get_credentials (char *username, char *password)
 
 		strncpy (password, my_password, len);
 		password[len - 1] = '\0';
-
-		g_free (my_password);
 	}
 
+	g_variant_unref (ret);
+
 	return 1;
 }
 
 static void
 nm_exit_notify (void *data, int arg)
 {
-	g_return_if_fail (DBUS_IS_G_PROXY (proxy));
+	g_return_if_fail (G_IS_DBUS_PROXY (proxy));
 
 	g_message ("nm-ppp-plugin: (%s): cleaning up", __func__);
 
@@ -319,10 +345,31 @@ nm_exit_notify (void *data, int arg)
 	proxy = NULL;
 }
 
+static void
+add_ip6_notifier (void)
+{
+	static struct notifier **notifier = NULL;
+	static gsize load_once = 0;
+
+	if (g_once_init_enter (&load_once)) {
+		void *handle = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
+
+		if (handle) {
+			notifier = dlsym (handle, "ipv6_up_notifier");
+			dlclose (handle);
+		}
+		g_once_init_leave (&load_once, 1);
+	}
+	if (notifier)
+		add_notifier (notifier, nm_ip6_up, NULL);
+	else
+		g_message ("nm-ppp-plugin: no IPV6CP notifier support; IPv6 not available");
+}
+
 int
 plugin_init (void)
 {
-	DBusGConnection *bus;
+	GDBusConnection *bus;
 	GError *err = NULL;
 
 #if !GLIB_CHECK_VERSION (2, 35, 0)
@@ -331,12 +378,10 @@ plugin_init (void)
 
 	g_message ("nm-ppp-plugin: (%s): initializing", __func__);
 
-	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
+	bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &err);
 	if (!bus) {
-		g_warning ("nm-pppd-plugin: (%s): couldn't connect to system bus: (%d) %s",
-		           __func__,
-		           err ? err->code : -1,
-		           err && err->message ? err->message : "(unknown)");
+		g_warning ("nm-pppd-plugin: (%s): couldn't connect to system bus: %s",
+		           __func__, err->message);
 		g_error_free (err);
 		return -1;
 	}
@@ -344,9 +389,21 @@ plugin_init (void)
 	/* NM passes in the object path of the corresponding PPPManager
 	 * object as the 'ipparam' argument to pppd.
 	 */
-	proxy = dbus_g_proxy_new_for_name (bus, NM_DBUS_SERVICE, ipparam, NM_DBUS_INTERFACE_PPP);
-
-	dbus_g_connection_unref (bus);
+	proxy = g_dbus_proxy_new_sync (bus,
+	                               G_DBUS_PROXY_FLAGS_NONE,
+	                               NULL,
+	                               NM_DBUS_SERVICE,
+	                               ipparam,
+	                               NM_DBUS_INTERFACE_PPP,
+	                               NULL, &err);
+	g_object_unref (bus);
+
+	if (!proxy) {
+		g_warning ("nm-pppd-plugin: (%s): couldn't create D-Bus proxy: %s",
+		           __func__, err->message);
+		g_error_free (err);
+		return -1;
+	}
 
 	chap_passwd_hook = get_credentials;
 	chap_check_hook = get_chap_check;
@@ -356,6 +413,7 @@ plugin_init (void)
 	add_notifier (&phasechange, nm_phasechange, NULL);
 	add_notifier (&ip_up_notifier, nm_ip_up, NULL);
 	add_notifier (&exitnotify, nm_exit_notify, proxy);
+	add_ip6_notifier ();
 
 	return 0;
 }