ppp-manager: port nm-pppd-plugin from dbus-glib to gdbus

This commit is contained in:
Dan Winship 2014-08-19 17:05:38 -04:00
parent 66c238e7b2
commit 89228569f8
2 changed files with 106 additions and 119 deletions

View file

@ -7,7 +7,6 @@ AM_CPPFLAGS = \
-DG_LOG_DOMAIN=\""nm-pppd-plugin"\" \ -DG_LOG_DOMAIN=\""nm-pppd-plugin"\" \
-DNETWORKMANAGER_COMPILATION \ -DNETWORKMANAGER_COMPILATION \
-DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \
$(DBUS_CFLAGS) \
$(GLIB_CFLAGS) $(GLIB_CFLAGS)
pppd_plugindir = $(PPPD_PLUGIN_DIR) pppd_plugindir = $(PPPD_PLUGIN_DIR)
@ -21,7 +20,6 @@ nm_pppd_plugin_la_SOURCES = \
nm_pppd_plugin_la_LDFLAGS = -module -avoid-version nm_pppd_plugin_la_LDFLAGS = -module -avoid-version
nm_pppd_plugin_la_LIBADD = \ nm_pppd_plugin_la_LIBADD = \
$(DBUS_LIBS) \
$(GLIB_LIBS) \ $(GLIB_LIBS) \
-ldl -ldl

View file

@ -28,9 +28,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <glib.h> #include <gio/gio.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#define INET6 #define INET6
#include <pppd/eui64.h> #include <pppd/eui64.h>
@ -39,13 +37,12 @@
#include "nm-dbus-interface.h" #include "nm-dbus-interface.h"
#include "nm-pppd-plugin.h" #include "nm-pppd-plugin.h"
#include "nm-ppp-status.h" #include "nm-ppp-status.h"
#include "nm-dbus-glib-types.h"
int plugin_init (void); int plugin_init (void);
char pppd_version[] = VERSION; char pppd_version[] = VERSION;
static DBusGProxy *proxy = NULL; static GDBusProxy *proxy = NULL;
static void static void
nm_phasechange (void *data, int arg) nm_phasechange (void *data, int arg)
@ -53,7 +50,7 @@ nm_phasechange (void *data, int arg)
NMPPPStatus ppp_status = NM_PPP_STATUS_UNKNOWN; NMPPPStatus ppp_status = NM_PPP_STATUS_UNKNOWN;
char *ppp_phase; char *ppp_phase;
g_return_if_fail (DBUS_IS_G_PROXY (proxy)); g_return_if_fail (G_IS_DBUS_PROXY (proxy));
switch (arg) { switch (arg) {
case PHASE_DEAD: case PHASE_DEAD:
@ -120,54 +117,24 @@ nm_phasechange (void *data, int arg)
ppp_phase); ppp_phase);
if (ppp_status != NM_PPP_STATUS_UNKNOWN) { if (ppp_status != NM_PPP_STATUS_UNKNOWN) {
dbus_g_proxy_call_no_reply (proxy, "SetState", g_dbus_proxy_call (proxy,
G_TYPE_UINT, ppp_status, G_TYPE_INVALID, "SetState",
G_TYPE_INVALID); 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 static void
nm_ip_up (void *data, int arg) nm_ip_up (void *data, int arg)
{ {
ipcp_options opts = ipcp_gotoptions[0]; ipcp_options opts = ipcp_gotoptions[0];
ipcp_options peer_opts = ipcp_hisoptions[0]; ipcp_options peer_opts = ipcp_hisoptions[0];
GHashTable *hash; GVariantBuilder builder;
GArray *array;
GValue *val;
guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit); 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__); g_message ("nm-ppp-plugin: (%s): ip-up event", __func__);
@ -177,85 +144,88 @@ nm_ip_up (void *data, int arg)
return; return;
} }
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
NULL, value_destroy);
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_INTERFACE, g_variant_builder_add (&builder, "{sv}",
str_to_gvalue (ifname)); NM_PPP_IP4_CONFIG_INTERFACE,
g_variant_new_string (ifname));
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_ADDRESS, g_variant_builder_add (&builder, "{sv}",
uint_to_gvalue (opts.ouraddr)); NM_PPP_IP4_CONFIG_ADDRESS,
g_variant_new_uint32 (opts.ouraddr));
/* Prefer the peer options remote address first, _unless_ pppd made the /* Prefer the peer options remote address first, _unless_ pppd made the
* address up, at which point prefer the local options remote address, * 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. * 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)) { if (peer_opts.hisaddr && (peer_opts.hisaddr != pppd_made_up_address)) {
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, g_variant_builder_add (&builder, "{sv}",
uint_to_gvalue (peer_opts.hisaddr)); NM_PPP_IP4_CONFIG_GATEWAY,
g_variant_new_uint32 (peer_opts.hisaddr));
} else if (opts.hisaddr) { } else if (opts.hisaddr) {
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, g_variant_builder_add (&builder, "{sv}",
uint_to_gvalue (opts.hisaddr)); NM_PPP_IP4_CONFIG_GATEWAY,
g_variant_new_uint32 (opts.hisaddr));
} else if (peer_opts.hisaddr == pppd_made_up_address) { } else if (peer_opts.hisaddr == pppd_made_up_address) {
/* As a last resort, use the made-up address */ /* As a last resort, use the made-up address */
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, g_variant_builder_add (&builder, "{sv}",
uint_to_gvalue (peer_opts.hisaddr)); 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]) { 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]) if (opts.dnsaddr[0])
g_array_append_val (array, opts.dnsaddr[0]); dns[len++] = opts.dnsaddr[0];
if (opts.dnsaddr[1]) if (opts.dnsaddr[1])
g_array_append_val (array, opts.dnsaddr[1]); dns[len++] = opts.dnsaddr[1];
val = g_slice_new0 (GValue); g_variant_builder_add (&builder, "{sv}",
g_value_init (val, DBUS_TYPE_G_UINT_ARRAY); NM_PPP_IP4_CONFIG_DNS,
g_value_set_boxed (val, array); g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
dns, len, sizeof (guint32)));
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_DNS, val);
} }
if (opts.winsaddr[0] || opts.winsaddr[1]) { 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]) if (opts.winsaddr[0])
g_array_append_val (array, opts.winsaddr[0]); wins[len++] = opts.winsaddr[0];
if (opts.winsaddr[1]) if (opts.winsaddr[1])
g_array_append_val (array, opts.winsaddr[1]); wins[len++] = opts.winsaddr[1];
val = g_slice_new0 (GValue); g_variant_builder_add (&builder, "{sv}",
g_value_init (val, DBUS_TYPE_G_UINT_ARRAY); NM_PPP_IP4_CONFIG_WINS,
g_value_set_boxed (val, array); g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
wins, len, sizeof (guint32)));
g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_WINS, val);
} }
g_message ("nm-ppp-plugin: (%s): sending IPv4 config to NetworkManager...", __func__); g_message ("nm-ppp-plugin: (%s): sending IPv4 config to NetworkManager...", __func__);
dbus_g_proxy_call_no_reply (proxy, "SetIp4Config", g_dbus_proxy_call (proxy,
DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID, "SetIp4Config",
G_TYPE_INVALID); g_variant_new ("(a{sv})", &builder),
G_DBUS_CALL_FLAGS_NONE, -1,
g_hash_table_destroy (hash); NULL,
NULL, NULL);
} }
static GValue * static GVariant *
eui64_to_gvalue (eui64_t eui) eui64_to_variant (eui64_t eui)
{ {
GValue *val;
guint64 iid; guint64 iid;
G_STATIC_ASSERT (sizeof (iid) == sizeof (eui)); G_STATIC_ASSERT (sizeof (iid) == sizeof (eui));
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_UINT64);
memcpy (&iid, &eui, sizeof (eui)); memcpy (&iid, &eui, sizeof (eui));
g_value_set_uint64 (val, iid); return g_variant_new_uint64 (iid);
return val;
} }
static void static void
@ -263,26 +233,33 @@ nm_ip6_up (void *data, int arg)
{ {
ipv6cp_options *ho = &ipv6cp_hisoptions[0]; ipv6cp_options *ho = &ipv6cp_hisoptions[0];
ipv6cp_options *go = &ipv6cp_gotoptions[0]; ipv6cp_options *go = &ipv6cp_gotoptions[0];
GHashTable *hash; GVariantBuilder builder;
g_return_if_fail (DBUS_IS_G_PROXY (proxy)); g_return_if_fail (G_IS_DBUS_PROXY (proxy));
g_message ("nm-ppp-plugin: (%s): ip6-up event", __func__); g_message ("nm-ppp-plugin: (%s): ip6-up event", __func__);
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_IP6_CONFIG_INTERFACE, str_to_gvalue (ifname)); g_variant_builder_add (&builder, "{sv}",
g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_OUR_IID, eui64_to_gvalue (go->ourid)); NM_PPP_IP6_CONFIG_INTERFACE,
g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_PEER_IID, eui64_to_gvalue (ho->hisid)); 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 */ /* DNS is done via DHCPv6 or router advertisements */
g_message ("nm-ppp-plugin: (%s): sending IPv6 config to NetworkManager...", __func__); g_message ("nm-ppp-plugin: (%s): sending IPv6 config to NetworkManager...", __func__);
dbus_g_proxy_call_no_reply (proxy, "SetIp6Config", g_dbus_proxy_call (proxy,
DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID, "SetIp6Config",
G_TYPE_INVALID); g_variant_new ("(a{sv})", &builder),
G_DBUS_CALL_FLAGS_NONE, -1,
g_hash_table_destroy (hash); NULL,
NULL, NULL);
} }
static int static int
@ -300,9 +277,10 @@ get_pap_check (void)
static int static int
get_credentials (char *username, char *password) get_credentials (char *username, char *password)
{ {
char *my_username = NULL; const char *my_username = NULL;
char *my_password = NULL; const char *my_password = NULL;
size_t len; size_t len;
GVariant *ret;
GError *err = NULL; GError *err = NULL;
if (!password) { if (!password) {
@ -312,15 +290,16 @@ get_credentials (char *username, char *password)
} }
g_return_val_if_fail (username, -1); 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__); g_message ("nm-ppp-plugin: (%s): passwd-hook, requesting credentials...", __func__);
if (!dbus_g_proxy_call (proxy, "NeedSecrets", &err, ret = g_dbus_proxy_call_sync (proxy,
G_TYPE_INVALID, "NeedSecrets",
G_TYPE_STRING, &my_username, NULL,
G_TYPE_STRING, &my_password, G_DBUS_CALL_FLAGS_NONE, -1,
G_TYPE_INVALID)) { NULL, &err);
if (!ret) {
g_warning ("nm-ppp-plugin: (%s): could not get secrets: (%d) %s", g_warning ("nm-ppp-plugin: (%s): could not get secrets: (%d) %s",
__func__, __func__,
err ? err->code : -1, err ? err->code : -1,
@ -331,14 +310,14 @@ get_credentials (char *username, char *password)
g_message ("nm-ppp-plugin: (%s): got credentials from NetworkManager", __func__); g_message ("nm-ppp-plugin: (%s): got credentials from NetworkManager", __func__);
g_variant_get (ret, "(&s&s)", &my_username, &my_password);
if (my_username) { if (my_username) {
len = strlen (my_username) + 1; len = strlen (my_username) + 1;
len = len < MAXNAMELEN ? len : MAXNAMELEN; len = len < MAXNAMELEN ? len : MAXNAMELEN;
strncpy (username, my_username, len); strncpy (username, my_username, len);
username[len - 1] = '\0'; username[len - 1] = '\0';
g_free (my_username);
} }
if (my_password) { if (my_password) {
@ -347,17 +326,17 @@ get_credentials (char *username, char *password)
strncpy (password, my_password, len); strncpy (password, my_password, len);
password[len - 1] = '\0'; password[len - 1] = '\0';
g_free (my_password);
} }
g_variant_unref (ret);
return 1; return 1;
} }
static void static void
nm_exit_notify (void *data, int arg) 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__); g_message ("nm-ppp-plugin: (%s): cleaning up", __func__);
@ -389,7 +368,7 @@ add_ip6_notifier (void)
int int
plugin_init (void) plugin_init (void)
{ {
DBusGConnection *bus; GDBusConnection *bus;
GError *err = NULL; GError *err = NULL;
#if !GLIB_CHECK_VERSION (2, 35, 0) #if !GLIB_CHECK_VERSION (2, 35, 0)
@ -398,12 +377,10 @@ plugin_init (void)
g_message ("nm-ppp-plugin: (%s): initializing", __func__); 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) { if (!bus) {
g_warning ("nm-pppd-plugin: (%s): couldn't connect to system bus: (%d) %s", g_warning ("nm-pppd-plugin: (%s): couldn't connect to system bus: %s",
__func__, __func__, err->message);
err ? err->code : -1,
err && err->message ? err->message : "(unknown)");
g_error_free (err); g_error_free (err);
return -1; return -1;
} }
@ -411,9 +388,21 @@ plugin_init (void)
/* NM passes in the object path of the corresponding PPPManager /* NM passes in the object path of the corresponding PPPManager
* object as the 'ipparam' argument to pppd. * object as the 'ipparam' argument to pppd.
*/ */
proxy = dbus_g_proxy_new_for_name (bus, NM_DBUS_SERVICE, ipparam, NM_DBUS_INTERFACE_PPP); 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);
dbus_g_connection_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_passwd_hook = get_credentials;
chap_check_hook = get_chap_check; chap_check_hook = get_chap_check;