1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License along
13
* with this program; if not, write to the Free Software Foundation, Inc.,
14
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
* Copyright (C) 2008 Novell, Inc.
17
* Copyright (C) 2008 - 2009 Red Hat, Inc.
21
#include <pppd/pppd.h>
23
#include <pppd/ipcp.h>
24
#include <sys/socket.h>
25
#include <netinet/in.h>
26
#include <arpa/inet.h>
29
int plugin_init (void);
31
char pppd_version[] = VERSION;
37
mm_phasechange (void *data, int arg)
39
const char *ppp_phase = NULL;
45
case PHASE_INITIALIZE:
46
ppp_phase = "initialize";
48
case PHASE_SERIALCONN:
49
ppp_phase = "serial connection";
52
ppp_phase = "dormant";
55
ppp_phase = "establish";
57
case PHASE_AUTHENTICATE:
58
ppp_phase = "authenticate";
61
ppp_phase = "callback";
64
ppp_phase = "network";
67
ppp_phase = "running";
70
ppp_phase = "terminate";
72
case PHASE_DISCONNECT:
73
ppp_phase = "disconnect";
76
ppp_phase = "holdoff";
82
ppp_phase = "unknown";
86
g_message ("mm-test-ppp-plugin: (%s): phase now '%s'", __func__, ppp_phase);
90
append_ip4_addr (GString *str, const char *tag, guint32 addr)
92
char buf[INET_ADDRSTRLEN + 2];
93
struct in_addr tmp_addr = { .s_addr = addr };
95
memset (buf, 0, sizeof (buf));
97
if (inet_ntop (AF_INET, &tmp_addr, buf, sizeof (buf) - 1))
98
g_string_append_printf (str, "%s %s\n", tag, buf);
102
mm_ip_up (void *data, int arg)
104
ipcp_options opts = ipcp_gotoptions[0];
105
ipcp_options peer_opts = ipcp_hisoptions[0];
106
guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit);
111
g_message ("mm-test-ppp-plugin: (%s): ip-up event", __func__);
114
g_warning ("mm-test-ppp-plugin: (%s): didn't receive an internal IP from pppd!", __func__);
115
mm_phasechange (NULL, PHASE_DEAD);
119
contents = g_string_sized_new (100);
121
g_string_append_printf (contents, "iface %s\n", ifname);
123
append_ip4_addr (contents, "addr", opts.ouraddr);
125
/* Prefer the peer options remote address first, _unless_ pppd made the
126
* address up, at which point prefer the local options remote address,
127
* and if that's not right, use the made-up address as a last resort.
129
if (peer_opts.hisaddr && (peer_opts.hisaddr != pppd_made_up_address))
130
append_ip4_addr (contents, "gateway", peer_opts.hisaddr);
131
else if (opts.hisaddr)
132
append_ip4_addr (contents, "gateway", opts.hisaddr);
133
else if (peer_opts.hisaddr == pppd_made_up_address) {
134
/* As a last resort, use the made-up address */
135
append_ip4_addr (contents, "gateway", peer_opts.hisaddr);
138
if (opts.dnsaddr[0] || opts.dnsaddr[1]) {
140
append_ip4_addr (contents, "dns1", opts.dnsaddr[0]);
142
append_ip4_addr (contents, "dns2", opts.dnsaddr[1]);
145
if (opts.winsaddr[0] || opts.winsaddr[1]) {
146
if (opts.winsaddr[0])
147
append_ip4_addr (contents, "wins1", opts.winsaddr[0]);
148
if (opts.winsaddr[1])
149
append_ip4_addr (contents, "wins2", opts.winsaddr[1]);
152
g_string_append (contents, "DONE\n");
154
success = g_file_set_contents (my_file, contents->str, -1, &err);
156
g_message ("nm-ppp-plugin: (%s): saved IP4 config to %s", __func__, my_file);
158
g_message ("nm-ppp-plugin: (%s): error saving IP4 config to %s: (%d) %s",
159
__func__, my_file, err->code, err->message);
160
g_clear_error (&err);
163
g_string_free (contents, TRUE);
179
get_credentials (char *username, char *password)
183
if (username && !password) {
184
/* pppd is checking pap support; return 1 for supported */
188
g_message ("nm-ppp-plugin: (%s): sending credentials (%s / %s)",
190
my_user ? my_user : "",
191
my_pass ? my_pass : "");
194
len = strlen (my_user) + 1;
195
len = len < MAXNAMELEN ? len : MAXNAMELEN;
197
strncpy (username, my_user, len);
198
username[len - 1] = '\0';
202
len = strlen (my_pass) + 1;
203
len = len < MAXSECRETLEN ? len : MAXSECRETLEN;
205
strncpy (password, my_pass, len);
206
password[len - 1] = '\0';
213
mm_exit_notify (void *data, int arg)
215
g_message ("mm-test-ppp-plugin: (%s): cleaning up", __func__);
230
g_message ("mm-test-ppp-plugin: (%s): initializing", __func__);
232
/* mm-test passes the file + username + password in as the 'ipparam' arg
235
args = g_strsplit (ipparam, "+", 0);
236
if (!args || g_strv_length (args) != 3) {
237
g_message ("mm-test-ppp-plugin: (%s): ipparam arguments error ('%s')",
242
my_user = (args[0] && strlen (args[0])) ? g_strdup (args[0]) : NULL;
243
my_pass = (args[1] && strlen (args[1])) ? g_strdup (args[1]) : NULL;
244
my_file = (args[2] && strlen (args[2])) ? g_strdup (args[2]) : NULL;
249
g_message ("mm-test-ppp-plugin: (%s): missing IP config file",
254
chap_passwd_hook = get_credentials;
255
chap_check_hook = get_chap_check;
256
pap_passwd_hook = get_credentials;
257
pap_check_hook = get_pap_check;
259
add_notifier (&phasechange, mm_phasechange, NULL);
260
add_notifier (&ip_up_notifier, mm_ip_up, NULL);
261
add_notifier (&exitnotify, mm_exit_notify, NULL);