ba79db07683a7801b01989a4d2cb13f1d90d4d4e
[framework/connectivity/connman.git] / scripts / libppp-plugin.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <pppd/pppd.h>
33 #include <pppd/fsm.h>
34 #include <pppd/ipcp.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include <dbus/dbus.h>
39
40 #define INET_ADDRES_LEN (INET_ADDRSTRLEN + 5)
41 #define INET_DNS_LEN    (2*INET_ADDRSTRLEN + 9)
42
43 static char *busname;
44 static char *interface;
45 static char *path;
46
47 static DBusConnection *connection;
48
49 char pppd_version[] = VERSION;
50
51 int plugin_init(void);
52
53 static void append(DBusMessageIter *dict, const char *key, const char *value)
54 {
55         DBusMessageIter entry;
56
57         /* We clean the environment before invoking pppd, but
58          * might as well still filter out the few things that get
59          * added that we're not interested in
60          */
61         if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
62                         !strcmp(key, "SHLVL") ||
63                         !strcmp(key, "connman_busname") ||
64                         !strcmp(key, "connman_network"))
65                 return;
66
67         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
68                                                         NULL, &entry);
69
70         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
71
72         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
73
74         dbus_message_iter_close_container(dict, &entry);
75 }
76
77
78 static int ppp_have_secret()
79 {
80         return 1;
81 }
82
83 static int ppp_get_secret(char *username, char *password)
84 {
85         DBusMessage *msg, *reply;
86         const char *user, *pass;
87         DBusError err;
88
89         if (username == NULL && password == NULL)
90                 return -1;
91
92         if (password == NULL)
93                 return 1;
94
95         if (connection == NULL)
96                 return -1;
97
98         dbus_error_init(&err);
99
100         msg = dbus_message_new_method_call(busname, path, interface, "getsec");
101         if (msg == NULL)
102                 return -1;
103
104         dbus_message_append_args(msg, DBUS_TYPE_INVALID, DBUS_TYPE_INVALID);
105
106         reply = dbus_connection_send_with_reply_and_block(connection,
107                                                                 msg, -1, &err);
108         if (reply == NULL) {
109                 if (dbus_error_is_set(&err) == TRUE)
110                         dbus_error_free(&err);
111
112                 dbus_message_unref(msg);
113                 return -1;
114         }
115
116         dbus_message_unref(msg);
117
118         dbus_error_init(&err);
119
120         if (dbus_message_get_args(reply, &err, DBUS_TYPE_STRING, &user,
121                                                 DBUS_TYPE_STRING, &pass,
122                                                 DBUS_TYPE_INVALID) == FALSE) {
123                 if (dbus_error_is_set(&err) == TRUE)
124                         dbus_error_free(&err);
125
126                 dbus_message_unref(reply);
127                 return -1;
128         }
129
130         if (username != NULL)
131                 strcpy(username, user);
132
133         strcpy(password, pass);
134
135         dbus_message_unref(reply);
136
137         return 1;
138 }
139
140 static void ppp_up(void *data, int arg)
141 {
142         char buf[INET_ADDRES_LEN];
143         char dns[INET_DNS_LEN];
144         const char *reason = "connect";
145         bool add_blank = FALSE;
146         DBusMessageIter iter, dict;
147         DBusMessage *msg;
148
149         if (connection == NULL)
150                 return;
151
152         if (ipcp_gotoptions[0].ouraddr == 0)
153                 return;
154
155         msg = dbus_message_new_method_call(busname, path,
156                                                 interface, "notify");
157         if (msg == NULL)
158                 return;
159
160         dbus_message_set_no_reply(msg, TRUE);
161
162         dbus_message_append_args(msg,
163                         DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
164
165         dbus_message_iter_init_append(msg, &iter);
166
167         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
168                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
169                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
170                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
171
172         append(&dict, "INTERNAL_IFNAME", ifname);
173
174         inet_ntop(AF_INET, &ipcp_gotoptions[0].ouraddr, buf, INET_ADDRSTRLEN);
175         append(&dict, "INTERNAL_IP4_ADDRESS", buf);
176
177         strcpy(buf, "255.255.255.255");
178         append(&dict, "INTERNAL_IP4_NETMASK", buf);
179
180         if (ipcp_gotoptions[0].dnsaddr[0] || ipcp_gotoptions[0].dnsaddr[1]) {
181                 memset(dns, 0, sizeof(dns));
182                 dns[0] = '\0';
183
184                 if (ipcp_gotoptions[0].dnsaddr[0]) {
185                         inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[0],
186                                                         buf, INET_ADDRSTRLEN);
187                         strcat(dns, buf);
188
189                         add_blank = TRUE;
190                 }
191
192                 if (ipcp_gotoptions[0].dnsaddr[1]) {
193                         inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[1],
194                                                         buf, INET_ADDRSTRLEN);
195                         if (add_blank == TRUE)
196                                 strcat(dns, " ");
197
198                         strcat(dns, buf);
199                 }
200                 append(&dict, "INTERNAL_IP4_DNS", dns);
201         }
202
203         append(&dict, "MTU", "1400");
204
205         dbus_message_iter_close_container(&iter, &dict);
206
207         dbus_connection_send(connection, msg, NULL);
208
209         dbus_connection_flush(connection);
210
211         dbus_message_unref(msg);
212 }
213
214 static void ppp_exit(void *data, int arg)
215 {
216         if (connection != NULL) {
217                 dbus_connection_unref(connection);
218                 connection = NULL;
219         }
220
221         if (busname != NULL) {
222                 free(busname);
223                 busname = NULL;
224         }
225
226         if (interface != NULL) {
227                 free(interface);
228                 interface = NULL;
229         }
230
231         if (path != NULL) {
232                 free(path);
233                 path = NULL;
234         }
235 }
236
237 static void ppp_phase_change(void *data, int arg)
238 {
239         const char *reason = "disconnect";
240         DBusMessage *msg;
241
242         if (connection == NULL)
243                 return;
244
245         if (arg == PHASE_DEAD || arg == PHASE_DISCONNECT) {
246                 msg = dbus_message_new_method_call(busname, path,
247                                                 interface, "notify");
248                 if (msg == NULL)
249                         return;
250
251                 dbus_message_set_no_reply(msg, TRUE);
252
253                 dbus_message_append_args(msg,
254                         DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
255
256                 dbus_connection_send(connection, msg, NULL);
257
258                 dbus_connection_flush(connection);
259
260                 dbus_message_unref(msg);
261         }
262 }
263
264 int plugin_init(void)
265 {
266         DBusError error;
267         static const char *bus, *inter, *p;
268
269         dbus_error_init(&error);
270
271         bus = getenv("CONNMAN_BUSNAME");
272         inter = getenv("CONNMAN_INTERFACE");
273         p = getenv("CONNMAN_PATH");
274
275         if (bus == NULL || inter == NULL || p == NULL)
276                 return -1;
277
278         busname = strdup(bus);
279         interface = strdup(inter);
280         path = strdup(p);
281
282         if (busname == NULL || interface == NULL || path == NULL) {
283                 ppp_exit(NULL, 0);
284                 return -1;
285         }
286
287         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
288         if (connection == NULL) {
289                 if (dbus_error_is_set(&error) == TRUE)
290                         dbus_error_free(&error);
291
292                 ppp_exit(NULL, 0);
293                 return -1;
294         }
295
296         pap_passwd_hook = ppp_get_secret;
297         chap_passwd_hook = ppp_get_secret;
298
299         chap_check_hook = ppp_have_secret;
300         pap_check_hook = ppp_have_secret;
301
302         add_notifier(&ip_up_notifier, ppp_up, NULL);
303         add_notifier(&phasechange, ppp_phase_change, NULL);
304         add_notifier(&exitnotify, ppp_exit, connection);
305
306         return 0;
307 }