1
/* nm-pptp-service - pptp (and other pppd) integration with NetworkManager
3
* Antony J Mee <eemynotna at gmail dot com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include "pppd/pppd.h"
24
#include "pppd/ipcp.h"
27
#define PPPD_VERSION VERSION
31
#include <sys/types.h>
38
#include <sys/socket.h>
39
#include <netinet/in.h>
40
#include <arpa/inet.h>
41
#include <dbus/dbus.h>
42
#include <dbus/dbus-glib-lowlevel.h>
43
#include <dbus/dbus-glib.h>
44
#include <NetworkManager/NetworkManager.h>
46
#include "nm-ppp-starter.h"
47
#include "nm-pppd-plugin.h"
50
typedef struct NmPPPData
53
gboolean got_auth_info;
60
char pppd_version[] = PPPD_VERSION;
62
NmPPPData plugin_data;
66
void nm_ip_up(void *opaque, int arg);
67
void nm_ip_down(void *opaque, int arg);
68
void nm_exit_notify(void *opaque, int arg);
70
int nm_chap_passwd_hook(char *user, char *passwd);
71
int nm_chap_check_hook(void);
73
void nm_notify_pid (NmPPPData *data);
74
void send_config_error (DBusConnection *con, const char *item);
75
gboolean nm_get_auth_items (NmPPPData *data);
76
gboolean nm_store_auth_info (NmPPPData *data, char **auth_items, int num_auth_items);
77
gboolean nm_dbus_prepare_connection(NmPPPData *data);
78
static DBusHandlerResult nm_dbus_message_handler (DBusConnection *con, DBusMessage *message, void *user_data);
79
void nm_dbus_kill_connection(NmPPPData *data);
81
gboolean nm_dbus_prepare_connection(NmPPPData *data)
83
// DBusMessage * message = NULL;
85
// DBusObjectPathVTable vtable = { NULL,
86
// &nm_dbus_message_handler,
87
// NULL, NULL, NULL, NULL };
89
g_return_val_if_fail (data != NULL, FALSE);
90
if (data->con != NULL) return TRUE;
93
dbus_error_init (&error);
94
data->con = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
95
if ((data->con == NULL) || dbus_error_is_set (&error))
97
info("Could not get the system bus. Make sure the message bus daemon is running?");
100
dbus_connection_set_exit_on_disconnect (data->con, FALSE);
102
// dbus_error_init (&error);
103
// dbus_bus_request_name (data->con, NM_DBUS_SERVICE_PPP, 0, &error);
104
// if (dbus_error_is_set (&error))
106
// nm_warning ("Could not acquire the dbus service. dbus_bus_request_name() says: '%s'", error.message);
110
// if (!dbus_connection_register_object_path (data->con, NM_DBUS_PATH_PPP, &vtable, data))
112
// nm_warning ("Could not register a dbus handler for nm-ppp-service. Not enough memory?");
113
// dbus_connection_unref(data->con);
117
if (dbus_error_is_set (&error))
119
dbus_error_free (&error);
122
if (data->con == NULL) return FALSE;
126
void nm_dbus_kill_connection(NmPPPData *data)
128
g_return_if_fail (data != NULL);
130
if (data->con != NULL)
131
dbus_connection_unref(data->con);
132
if (data->username!=NULL) g_free(data->username);
133
if (data->password!=NULL) g_free(data->password);
137
* nm_dbus_message_handler
139
* Handle requests for our services.
142
static DBusHandlerResult nm_dbus_message_handler (DBusConnection *con, DBusMessage *message, void *user_data)
144
NmPPPData *data = (NmPPPData *)user_data;
147
DBusMessage *reply = NULL;
148
gboolean handled = TRUE;
150
g_return_val_if_fail (data != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
151
g_return_val_if_fail (con != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
152
g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
154
method = dbus_message_get_member (message);
155
path = dbus_message_get_path (message);
157
nm_info ("nm_dbus_message_handler() got method '%s' for path '%s'.", method, path);
164
dbus_connection_send (con, reply, NULL);
165
dbus_message_unref (reply);
168
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
172
int nm_chap_check_hook(void)
174
plugin_data.pppd_pid=getpid();
175
nm_notify_pid (&plugin_data);
177
if (! nm_get_auth_items (&plugin_data))
182
if (strcmp("CHAP",plugin_data.auth_type)!=0)
184
info("nm-pppd-plugin: No CHAP authentication available!");
189
info("nm-pppd-plugin: CHAP check hook.");
193
int nm_chap_passwd_hook(char *user, char *passwd)
195
info("nm-pppd-plugin: CHAP credentials requested.");
199
info("nm-pppd-plugin: pppd didn't provide username buffer");
205
info("nm-pppd-plugin: pppd didn't provide password buffer");
209
if (plugin_data.username == NULL)
211
info("nm-pppd-plugin: CHAP username not set");
215
if (plugin_data.password == NULL)
217
info("nm-pppd-plugin: CHAP password not set");
221
if (strlen(plugin_data.username) >= MAXNAMELEN)
223
info("nm-pppd-plugin: CHAP username too long!");
227
if (strlen(plugin_data.password) >= MAXSECRETLEN)
229
info("nm-pppd-plugin: CHAP password too long!");
233
strcpy(user, plugin_data.username);
234
user[MAXNAMELEN-1]='\0';
235
strcpy(passwd, plugin_data.password);
236
passwd[MAXSECRETLEN-1]='\0';
237
// info("nm-pppd-plugin: CHAP authenticating as '%s' with '%s'",user,passwd);
239
// Forget the username and password?
241
// if (plugin_data.username!=NULL) g_free(plugin_data.username);
242
// if (plugin_data.password!=NULL) g_free(plugin_data.password);
247
void nm_exit_notify(void *opaque, int arg)
249
NmPPPData *data = (NmPPPData *)opaque;
251
nm_dbus_kill_connection(data);
254
void nm_ip_down(void *opaque, int arg)
256
// DBusConnection *con = (DBusConnection *)opaque;
261
void nm_ip_up(void *opaque, int arg)
263
NmPPPData *data = (NmPPPData *)opaque;
264
DBusConnection *con = data->con;
265
DBusMessage *message;
267
char * str_ifname = NULL;
268
// guint32 * uint_ip4_dns = NULL;
269
guint32 uint_ip4_dns1 = 0;
270
guint32 uint_ip4_dns2 = 0;
271
guint32 uint_ip4_dns_len = 0;
272
// guint32 * uint_ip4_wins = NULL;
273
guint32 uint_ip4_wins1 = 0;
274
guint32 uint_ip4_wins2 = 0;
275
guint32 uint_ip4_wins_len = 0;
276
guint32 uint_ip4_address = 0;
277
guint32 uint_ip4_ptp_address = 0;
278
guint32 uint_ip4_netmask = 0xFFFFFFFF; /* Default mask of 255.255.255.255 */
282
g_return_if_fail (con != NULL);
283
if (ipcp_gotoptions[ifunit].ouraddr==0) {
284
info ("nm-pppd-plugin: didn't receive an Internal IP4 Address from ppp.");
285
send_config_error (con, "IP4 Address");
288
uint_ip4_address=ipcp_gotoptions[ifunit].ouraddr;
290
if (!(message = dbus_message_new_method_call (NM_DBUS_SERVICE_PPP_STARTER, NM_DBUS_PATH_PPP_STARTER, NM_DBUS_INTERFACE_PPP_STARTER, "signalIP4Config")))
292
nm_warning ("send_config_error(): Couldn't allocate the dbus message");
296
if (ipcp_gotoptions[ifunit].dnsaddr) {
297
if (ipcp_gotoptions[ifunit].dnsaddr[0]!=0) {
299
uint_ip4_dns1=ipcp_gotoptions[ifunit].dnsaddr[0];
300
if (ipcp_gotoptions[ifunit].dnsaddr[1]!=0) {
302
uint_ip4_dns2=ipcp_gotoptions[ifunit].dnsaddr[1];
307
if (ipcp_gotoptions[ifunit].winsaddr) {
308
if (ipcp_gotoptions[ifunit].winsaddr[0]!=0) {
310
uint_ip4_wins1=ipcp_gotoptions[ifunit].winsaddr[0];
311
if (ipcp_gotoptions[ifunit].winsaddr[1]!=0) {
313
uint_ip4_wins2=ipcp_gotoptions[ifunit].winsaddr[1];
319
info ("nm-pppd-plugin: didn't receive a tunnel device name.");
320
send_config_error (con, "IP4 Address");
322
str_ifname = g_strdup(ifname);
324
dbus_message_append_args (message,
325
DBUS_TYPE_STRING, &str_ifname,
326
DBUS_TYPE_UINT32, &uint_ip4_address,
327
DBUS_TYPE_UINT32, &uint_ip4_ptp_address,
328
DBUS_TYPE_UINT32, &uint_ip4_netmask,
330
DBUS_TYPE_UINT32, &uint_ip4_dns1,
331
DBUS_TYPE_UINT32, &uint_ip4_dns2,
332
DBUS_TYPE_UINT32, &uint_ip4_dns_len,
333
DBUS_TYPE_UINT32, &uint_ip4_wins1,
334
DBUS_TYPE_UINT32, &uint_ip4_wins2,
335
DBUS_TYPE_UINT32, &uint_ip4_wins_len,
338
if (!dbus_connection_send (con, message, NULL)) {
339
info ("nm_ip_up(): could not send dbus message");
340
dbus_message_unref (message);
346
dbus_message_unref (message);
354
* Notify nm-ppp-starter of a config error from pppd.
357
void send_config_error (DBusConnection *con, const char *item)
359
DBusMessage *message;
361
g_return_if_fail (con != NULL);
362
g_return_if_fail (item != NULL);
364
if (!(message = dbus_message_new_method_call (NM_DBUS_SERVICE_PPP_STARTER, NM_DBUS_PATH_PPP_STARTER, NM_DBUS_INTERFACE_PPP_STARTER, "signalConfigError")))
366
nm_warning ("send_config_error(): Couldn't allocate the dbus message");
370
dbus_message_append_args (message, DBUS_TYPE_STRING, &item, DBUS_TYPE_INVALID);
371
if (!dbus_connection_send (con, message, NULL))
372
nm_warning ("send_config_error(): could not send dbus message");
374
dbus_message_unref (message);
381
* Let the pppd starter service know our PID
382
* so that pppd may be killed later.
385
void nm_notify_pid (NmPPPData *data)
388
DBusMessage *message = NULL;
390
if (!nm_dbus_prepare_connection(data))
394
if (!(message = dbus_message_new_method_call (NM_DBUS_SERVICE_PPP_STARTER, NM_DBUS_PATH_PPP_STARTER, NM_DBUS_INTERFACE_PPP_STARTER, "notifyPID")))
396
nm_warning ("nm-pppd-plugin: Couldn't allocate the notifyPID dbus message");
400
dbus_message_append_args (message,
401
DBUS_TYPE_UINT32, &(data->pppd_pid),
404
if (!dbus_connection_send (con, message, NULL)) {
405
info ("nm_ip_up(): could not send dbus message");
406
dbus_message_unref (message);
410
// nm_warning("Sent notify message: %d",data->pppd_pid);
411
dbus_message_unref (message);
416
* Request credentials from PPP_STARTER service.
419
gboolean nm_get_auth_items (NmPPPData *data)
422
int num_auth_items = -1;
423
char *auth_items[3] = { NULL, NULL, NULL };
424
// char **auth_items = NULL;
425
DBusMessage *message = NULL;
426
DBusMessage *reply = NULL;
428
if (!nm_dbus_prepare_connection(data))
433
g_return_val_if_fail (con != NULL,FALSE);
434
if (!(message = dbus_message_new_method_call (NM_DBUS_SERVICE_PPP_STARTER, NM_DBUS_PATH_PPP_STARTER, NM_DBUS_INTERFACE_PPP_STARTER, "getAuthInfo")))
436
nm_warning("nm-pppd-plugin: failed to create getAuthInfo message.");
440
reply = dbus_connection_send_with_reply_and_block (con, message, -1, NULL);
441
dbus_message_unref (message);
444
info("nm-pppd-plugin: no reply to getAuthInfo message.");
448
if (!(dbus_message_get_args (reply, NULL,
449
DBUS_TYPE_STRING, &(auth_items[0]),
450
DBUS_TYPE_STRING, &(auth_items[1]),
451
DBUS_TYPE_STRING, &(auth_items[2]),
452
// DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &auth_items, &num_auth_items,
455
dbus_message_unref (reply);
460
if (!nm_store_auth_info (data, auth_items, num_auth_items))
462
//dbus_free_string_array (auth_items);
463
dbus_message_unref (reply);
467
//dbus_free_string_array (auth_items);
468
dbus_message_unref (reply);
475
* Decode and temporarily store the authentication info provided.
478
gboolean nm_store_auth_info (NmPPPData *data, char **auth_items, int num_auth_items)
481
g_return_val_if_fail (auth_items != NULL, FALSE);
482
g_return_val_if_fail (num_auth_items >= 1, FALSE);
484
nm_warning ("PPPD will authenticate using '%s'.", auth_items[0]);
486
if (strcmp ("CHAP", auth_items[0]) == 0) {
487
g_return_val_if_fail (num_auth_items >= 3, FALSE);
488
if (data->auth_type!=NULL) g_free(data->auth_type);
489
if (data->username!=NULL) g_free(data->username);
490
if (data->password!=NULL) g_free(data->password);
491
data->auth_type=g_strdup(auth_items[0]);
492
data->username=g_strdup(auth_items[1]);
493
data->password=g_strdup(auth_items[2]);
494
} else if (strcmp ("NONE", auth_items[0]) == 0) {
495
if (data->auth_type!=NULL) g_free(data->auth_type);
496
if (data->username!=NULL) g_free(data->username);
497
if (data->password!=NULL) g_free(data->password);
498
data->auth_type=g_strdup(auth_items[0]);
500
nm_warning ("PPPD authentication type '%s' is not allowed.", auth_items[0]);
504
data->got_auth_info=TRUE;
510
// DBusConnection * con = NULL;
511
// DBusMessage * message = NULL;
515
// if (!g_thread_supported ())
516
// g_thread_init (NULL);
518
// dbus_error_init (&error);
519
// con = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
520
// if ((con == NULL) || dbus_error_is_set (&error))
522
// dbus_error_free (&error);
523
// info("Could not get the system bus. Make sure the message bus daemon is running?");
526
// dbus_connection_set_exit_on_disconnect (con, FALSE);
529
// dbus_error_free (&error);
531
// add_options(ppp_options);
533
chap_check_hook = nm_chap_check_hook;
534
chap_passwd_hook = nm_chap_passwd_hook;
536
add_notifier(&ip_down_notifier, nm_ip_down, (void *) &plugin_data);
537
add_notifier(&ip_up_notifier, nm_ip_up, (void *) &plugin_data);
538
add_notifier(&exitnotify, nm_exit_notify, (void *) &plugin_data);
540
info("nm-pppd-plugin: plugin initialized.");