summaryrefslogtreecommitdiff
path: root/src/ppp-manager/nm-pppd-plugin.c
blob: 0cea4e1d10d239604aa34efab759e4ef2e0fbbab (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * 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 of the License, 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) 2008 Novell, Inc.
 * Copyright (C) 2008 Red Hat, Inc.
 */

#include <config.h>
#include <string.h>
#include <pppd/pppd.h>
#include <pppd/fsm.h>
#include <pppd/ipcp.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dlfcn.h>
#include <glib.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>

#define INET6
#include <pppd/eui64.h>
#include <pppd/ipv6cp.h>

#include "NetworkManager.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 void
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));

	switch (arg) {
	case PHASE_DEAD:
		ppp_status = NM_PPP_STATUS_DEAD;
		ppp_phase = "dead";
		break;
	case PHASE_INITIALIZE:
		ppp_status = NM_PPP_STATUS_INITIALIZE;
		ppp_phase = "initialize";
		break;
	case PHASE_SERIALCONN:
		ppp_status = NM_PPP_STATUS_SERIALCONN;
		ppp_phase = "serial connection";
		break;
	case PHASE_DORMANT:
		ppp_status = NM_PPP_STATUS_DORMANT;
		ppp_phase = "dormant";
		break;
	case PHASE_ESTABLISH:
		ppp_status = NM_PPP_STATUS_ESTABLISH;
		ppp_phase = "establish";
		break;
	case PHASE_AUTHENTICATE:
		ppp_status = NM_PPP_STATUS_AUTHENTICATE;
		ppp_phase = "authenticate";
		break;
	case PHASE_CALLBACK:
		ppp_status = NM_PPP_STATUS_CALLBACK;
		ppp_phase = "callback";
		break;
	case PHASE_NETWORK:
		ppp_status = NM_PPP_STATUS_NETWORK;
		ppp_phase = "network";
		break;
	case PHASE_RUNNING:
		ppp_status = NM_PPP_STATUS_RUNNING;
		ppp_phase = "running";
		break;
	case PHASE_TERMINATE:
		ppp_status = NM_PPP_STATUS_TERMINATE;
		ppp_phase = "terminate";
		break;
	case PHASE_DISCONNECT:
		ppp_status = NM_PPP_STATUS_DISCONNECT;
		ppp_phase = "disconnect";
		break;
	case PHASE_HOLDOFF:
		ppp_status = NM_PPP_STATUS_HOLDOFF;
		ppp_phase = "holdoff";
		break;
	case PHASE_MASTER:
		ppp_status = NM_PPP_STATUS_MASTER;
		ppp_phase = "master";
		break;

	default:
		ppp_phase = "unknown";
		break;
	}

	g_message ("nm-ppp-plugin: (%s): status %d / phase '%s'",
	           __func__,
	           ppp_status,
	           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);
	}
}

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;
	guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit);

	g_return_if_fail (DBUS_IS_G_PROXY (proxy));

	g_message ("nm-ppp-plugin: (%s): ip-up event", __func__);

	if (!opts.ouraddr) {
		g_warning ("nm-ppp-plugin: (%s): didn't receive an internal IP from pppd!", __func__);
		nm_phasechange (NULL, PHASE_DEAD);
		return;
	}

	hash = g_hash_table_new_full (g_str_hash, g_str_equal,
	                              NULL, value_destroy);

	g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_INTERFACE, 
	                     str_to_gvalue (ifname));

	g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_ADDRESS, 
	                     uint_to_gvalue (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));
	} else if (opts.hisaddr) {
		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, 
		                     uint_to_gvalue (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_hash_table_insert (hash, NM_PPP_IP4_CONFIG_PREFIX, uint_to_gvalue (32));

	if (opts.dnsaddr[0] || opts.dnsaddr[1]) {
		array = g_array_new (FALSE, FALSE, sizeof (guint32));

		if (opts.dnsaddr[0])
			g_array_append_val (array, opts.dnsaddr[0]);
		if (opts.dnsaddr[1])
			g_array_append_val (array, 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);
	}

	if (opts.winsaddr[0] || opts.winsaddr[1]) {
		array = g_array_new (FALSE, FALSE, sizeof (guint32));

		if (opts.winsaddr[0])
			g_array_append_val (array, 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);

		g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_WINS, val);
	}

	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);
}

static GValue *
eui64_to_gvalue (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;
}

static void
nm_ip6_up (void *data, int arg)
{
	ipv6cp_options *ho = &ipv6cp_hisoptions[0];
	ipv6cp_options *go = &ipv6cp_gotoptions[0];
	GHashTable *hash;

	g_return_if_fail (DBUS_IS_G_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));

	/* 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);
}

static int
get_chap_check (void)
{
	return 1;
}

static int
get_pap_check (void)
{
	return 1;
}

static int
get_credentials (char *username, char *password)
{
	char *my_username = NULL;
	char *my_password = NULL;
	size_t len;
	GError *err = NULL;

	if (!password) {
		/* pppd is checking pap support; return 1 for supported */
		g_return_val_if_fail (username, -1);
		return 1;
	}

	g_return_val_if_fail (username, -1);
	g_return_val_if_fail (DBUS_IS_G_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)) {
		g_warning ("nm-ppp-plugin: (%s): could not get secrets: (%d) %s",
		           __func__,
		           err ? err->code : -1,
		           err->message ? err->message : "(unknown)");
		g_error_free (err);
		return -1;
	}

	g_message ("nm-ppp-plugin: (%s): got credentials from NetworkManager", __func__);

	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) {
		len = strlen (my_password) + 1;
		len = len < MAXSECRETLEN ? len : MAXSECRETLEN;

		strncpy (password, my_password, len);
		password[len - 1] = '\0';

		g_free (my_password);
	}

	return 1;
}

static void
nm_exit_notify (void *data, int arg)
{
	g_return_if_fail (DBUS_IS_G_PROXY (proxy));

	g_message ("nm-ppp-plugin: (%s): cleaning up", __func__);

	g_object_unref (proxy);
	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;
	GError *err = NULL;

#if !GLIB_CHECK_VERSION (2, 35, 0)
	g_type_init ();
#endif

	g_message ("nm-ppp-plugin: (%s): initializing", __func__);

	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &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_error_free (err);
		return -1;
	}

	/* 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);

	chap_passwd_hook = get_credentials;
	chap_check_hook = get_chap_check;
	pap_passwd_hook = get_credentials;
	pap_check_hook = get_pap_check;

	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;
}