technology: return already enabled when tethering is enabled
[framework/connectivity/connman.git] / scripts / libppp-plugin.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  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 static int prev_phase;
49
50 char pppd_version[] = VERSION;
51
52 int plugin_init(void);
53
54 static void append(DBusMessageIter *dict, const char *key, const char *value)
55 {
56         DBusMessageIter entry;
57
58         /* We clean the environment before invoking pppd, but
59          * might as well still filter out the few things that get
60          * added that we're not interested in
61          */
62         if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
63                         !strcmp(key, "SHLVL") ||
64                         !strcmp(key, "connman_busname") ||
65                         !strcmp(key, "connman_network"))
66                 return;
67
68         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
69                                                         NULL, &entry);
70
71         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
72
73         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
74
75         dbus_message_iter_close_container(dict, &entry);
76 }
77
78
79 static int ppp_have_secret()
80 {
81         return 1;
82 }
83
84 static int ppp_get_secret(char *username, char *password)
85 {
86         DBusMessage *msg, *reply;
87         const char *user, *pass;
88         DBusError err;
89
90         if (username == NULL && password == NULL)
91                 return -1;
92
93         if (password == NULL)
94                 return 1;
95
96         if (connection == NULL)
97                 return -1;
98
99         dbus_error_init(&err);
100
101         msg = dbus_message_new_method_call(busname, path, interface, "getsec");
102         if (msg == NULL)
103                 return -1;
104
105         dbus_message_append_args(msg, DBUS_TYPE_INVALID, DBUS_TYPE_INVALID);
106
107         reply = dbus_connection_send_with_reply_and_block(connection,
108                                                                 msg, -1, &err);
109         if (reply == NULL) {
110                 if (dbus_error_is_set(&err) == TRUE)
111                         dbus_error_free(&err);
112
113                 dbus_message_unref(msg);
114                 return -1;
115         }
116
117         dbus_message_unref(msg);
118
119         dbus_error_init(&err);
120
121         if (dbus_message_get_args(reply, &err, DBUS_TYPE_STRING, &user,
122                                                 DBUS_TYPE_STRING, &pass,
123                                                 DBUS_TYPE_INVALID) == FALSE) {
124                 if (dbus_error_is_set(&err) == TRUE)
125                         dbus_error_free(&err);
126
127                 dbus_message_unref(reply);
128                 return -1;
129         }
130
131         if (username != NULL)
132                 strcpy(username, user);
133
134         strcpy(password, pass);
135
136         dbus_message_unref(reply);
137
138         return 1;
139 }
140
141 static void ppp_up(void *data, int arg)
142 {
143         char buf[INET_ADDRES_LEN];
144         char dns[INET_DNS_LEN];
145         const char *reason = "connect";
146         bool add_blank = FALSE;
147         DBusMessageIter iter, dict;
148         DBusMessage *msg;
149
150         if (connection == NULL)
151                 return;
152
153         if (ipcp_gotoptions[0].ouraddr == 0)
154                 return;
155
156         msg = dbus_message_new_method_call(busname, path,
157                                                 interface, "notify");
158         if (msg == NULL)
159                 return;
160
161         dbus_message_set_no_reply(msg, TRUE);
162
163         dbus_message_append_args(msg,
164                         DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
165
166         dbus_message_iter_init_append(msg, &iter);
167
168         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
169                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
170                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
171                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
172
173         append(&dict, "INTERNAL_IFNAME", ifname);
174
175         inet_ntop(AF_INET, &ipcp_gotoptions[0].ouraddr, buf, INET_ADDRSTRLEN);
176         append(&dict, "INTERNAL_IP4_ADDRESS", buf);
177
178         strcpy(buf, "255.255.255.255");
179         append(&dict, "INTERNAL_IP4_NETMASK", buf);
180
181         if (ipcp_gotoptions[0].dnsaddr[0] || ipcp_gotoptions[0].dnsaddr[1]) {
182                 memset(dns, 0, sizeof(dns));
183                 dns[0] = '\0';
184
185                 if (ipcp_gotoptions[0].dnsaddr[0]) {
186                         inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[0],
187                                                         buf, INET_ADDRSTRLEN);
188                         strcat(dns, buf);
189
190                         add_blank = TRUE;
191                 }
192
193                 if (ipcp_gotoptions[0].dnsaddr[1]) {
194                         inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[1],
195                                                         buf, INET_ADDRSTRLEN);
196                         if (add_blank == TRUE)
197                                 strcat(dns, " ");
198
199                         strcat(dns, buf);
200                 }
201                 append(&dict, "INTERNAL_IP4_DNS", dns);
202         }
203
204         append(&dict, "MTU", "1400");
205
206         dbus_message_iter_close_container(&iter, &dict);
207
208         dbus_connection_send(connection, msg, NULL);
209
210         dbus_connection_flush(connection);
211
212         dbus_message_unref(msg);
213 }
214
215 static void ppp_exit(void *data, int arg)
216 {
217         if (connection != NULL) {
218                 dbus_connection_unref(connection);
219                 connection = NULL;
220         }
221
222         if (busname != NULL) {
223                 free(busname);
224                 busname = NULL;
225         }
226
227         if (interface != NULL) {
228                 free(interface);
229                 interface = NULL;
230         }
231
232         if (path != NULL) {
233                 free(path);
234                 path = NULL;
235         }
236 }
237
238 static void ppp_phase_change(void *data, int arg)
239 {
240         const char *reason = "disconnect";
241         DBusMessage *msg;
242         int send_msg = 0;
243
244         if (connection == NULL)
245                 return;
246
247         if (prev_phase == PHASE_AUTHENTICATE &&
248                                 arg == PHASE_TERMINATE) {
249                 reason = "auth failed";
250                 send_msg = 1;
251         }
252
253         if (send_msg > 0 || arg == PHASE_DEAD || arg == PHASE_DISCONNECT) {
254                 msg = dbus_message_new_method_call(busname, path,
255                                                 interface, "notify");
256                 if (msg == NULL)
257                         return;
258
259                 dbus_message_set_no_reply(msg, TRUE);
260
261                 dbus_message_append_args(msg,
262                         DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
263
264                 dbus_connection_send(connection, msg, NULL);
265
266                 dbus_connection_flush(connection);
267
268                 dbus_message_unref(msg);
269         }
270
271         prev_phase = arg;
272 }
273
274 int plugin_init(void)
275 {
276         DBusError error;
277         static const char *bus, *inter, *p;
278
279         dbus_error_init(&error);
280
281         bus = getenv("CONNMAN_BUSNAME");
282         inter = getenv("CONNMAN_INTERFACE");
283         p = getenv("CONNMAN_PATH");
284
285         if (bus == NULL || inter == NULL || p == NULL)
286                 return -1;
287
288         busname = strdup(bus);
289         interface = strdup(inter);
290         path = strdup(p);
291
292         if (busname == NULL || interface == NULL || path == NULL) {
293                 ppp_exit(NULL, 0);
294                 return -1;
295         }
296
297         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
298         if (connection == NULL) {
299                 if (dbus_error_is_set(&error) == TRUE)
300                         dbus_error_free(&error);
301
302                 ppp_exit(NULL, 0);
303                 return -1;
304         }
305
306         pap_passwd_hook = ppp_get_secret;
307         chap_passwd_hook = ppp_get_secret;
308
309         chap_check_hook = ppp_have_secret;
310         pap_check_hook = ppp_have_secret;
311
312         add_notifier(&ip_up_notifier, ppp_up, NULL);
313         add_notifier(&phasechange, ppp_phase_change, NULL);
314         add_notifier(&exitnotify, ppp_exit, connection);
315
316         return 0;
317 }