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"\" \
-DNETWORKMANAGER_COMPILATION \
-DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \
$(DBUS_CFLAGS) \
$(GLIB_CFLAGS)
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_LIBADD = \
$(DBUS_LIBS) \
$(GLIB_LIBS) \
-ldl

View file

@ -28,9 +28,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include <glib.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <gio/gio.h>
#define INET6
#include <pppd/eui64.h>
@ -39,13 +37,12 @@
#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)
@ -53,7 +50,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:
@ -120,54 +117,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__);
@ -177,85 +144,88 @@ 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]);
wins[len++] = opts.winsaddr[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_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 IPv4 config to NetworkManager...", __func__);
dbus_g_proxy_call_no_reply (proxy, "SetIp4Config",
DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID,
G_TYPE_INVALID);
g_hash_table_destroy (hash);
g_dbus_proxy_call (proxy,
"SetIp4Config",
g_variant_new ("(a{sv})", &builder),
G_DBUS_CALL_FLAGS_NONE, -1,
NULL,
NULL, NULL);
}
static GValue *
eui64_to_gvalue (eui64_t eui)
static GVariant *
eui64_to_variant (eui64_t eui)
{
GValue *val;
guint64 iid;
G_STATIC_ASSERT (sizeof (iid) == sizeof (eui));
val = g_slice_new0 (GValue);
g_value_init (val, G_TYPE_UINT64);
memcpy (&iid, &eui, sizeof (eui));
g_value_set_uint64 (val, iid);
return val;
return g_variant_new_uint64 (iid);
}
static void
@ -263,26 +233,33 @@ nm_ip6_up (void *data, int arg)
{
ipv6cp_options *ho = &ipv6cp_hisoptions[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__);
hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);
g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_INTERFACE, str_to_gvalue (ifname));
g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_OUR_IID, eui64_to_gvalue (go->ourid));
g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_PEER_IID, eui64_to_gvalue (ho->hisid));
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__);
dbus_g_proxy_call_no_reply (proxy, "SetIp6Config",
DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID,
G_TYPE_INVALID);
g_hash_table_destroy (hash);
g_dbus_proxy_call (proxy,
"SetIp6Config",
g_variant_new ("(a{sv})", &builder),
G_DBUS_CALL_FLAGS_NONE, -1,
NULL,
NULL, NULL);
}
static int
@ -300,9 +277,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) {
@ -312,15 +290,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,
@ -331,14 +310,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) {
@ -347,17 +326,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__);
@ -389,7 +368,7 @@ add_ip6_notifier (void)
int
plugin_init (void)
{
DBusGConnection *bus;
GDBusConnection *bus;
GError *err = NULL;
#if !GLIB_CHECK_VERSION (2, 35, 0)
@ -398,12 +377,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;
}
@ -411,9 +388,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);
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_check_hook = get_chap_check;