Fix incorrect dbus usage that caused crash
[platform/upstream/connman.git] / plugins / vpn.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2013  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 <string.h>
27 #include <errno.h>
28 #include <sys/socket.h>
29 #include <stdlib.h>
30 #include <glib.h>
31
32 #include <gdbus.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/technology.h>
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/dbus.h>
39 #include <connman/provider.h>
40 #include <connman/ipaddress.h>
41 #include <connman/notifier.h>
42 #include <connman/vpn-dbus.h>
43 #include <connman/inet.h>
44 #include <gweb/gresolv.h>
45
46 #define DBUS_TIMEOUT 10000
47
48 static DBusConnection *connection;
49
50 static GHashTable *vpn_connections = NULL;
51 static guint watch;
52 static guint added_watch;
53 static guint removed_watch;
54 static guint property_watch;
55
56 struct vpn_route {
57         int family;
58         char *network;
59         char *netmask;
60         char *gateway;
61 };
62
63 struct config_create_data {
64         connection_ready_cb callback;
65         DBusMessage *message;
66         char *path;
67 };
68
69 struct connection_data {
70         char *path;
71         char *ident;
72         struct connman_provider *provider;
73         int index;
74         DBusPendingCall *call;
75         DBusPendingCall *disconnect_call;
76         bool connect_pending;
77         struct config_create_data *cb_data;
78         char *service_ident;
79
80         char *state;
81         char *type;
82         char *name;
83         char *host;
84         char **host_ip;
85         char *domain;
86         char **nameservers;
87         bool immutable;
88
89         GHashTable *server_routes;
90         GHashTable *user_routes;
91         GHashTable *setting_strings;
92
93         struct connman_ipaddress *ip;
94
95         GResolv *resolv;
96         guint resolv_id;
97 };
98
99 static int set_string(struct connman_provider *provider,
100                                         const char *key, const char *value)
101 {
102         struct connection_data *data;
103
104         data = connman_provider_get_data(provider);
105         if (!data)
106                 return -EINVAL;
107
108         DBG("data %p provider %p key %s value %s", data, provider, key, value);
109
110         if (g_str_equal(key, "Type")) {
111                 g_free(data->type);
112                 data->type = g_strdup(value);
113         } else if (g_str_equal(key, "Name")) {
114                 g_free(data->name);
115                 data->name = g_strdup(value);
116         } else if (g_str_equal(key, "Host")) {
117                 g_free(data->host);
118                 data->host = g_strdup(value);
119         } else if (g_str_equal(key, "VPN.Domain") ||
120                                 g_str_equal(key, "Domain")) {
121                 g_free(data->domain);
122                 data->domain = g_strdup(value);
123         } else
124                 g_hash_table_replace(data->setting_strings,
125                                 g_strdup(key), g_strdup(value));
126         return 0;
127 }
128
129 static const char *get_string(struct connman_provider *provider,
130                                                         const char *key)
131 {
132         struct connection_data *data;
133
134         data = connman_provider_get_data(provider);
135         if (!data)
136                 return NULL;
137
138         DBG("data %p provider %p key %s", data, provider, key);
139
140         if (g_str_equal(key, "Type"))
141                 return data->type;
142         else if (g_str_equal(key, "Name"))
143                 return data->name;
144         else if (g_str_equal(key, "Host"))
145                 return data->host;
146         else if (g_str_equal(key, "HostIP")) {
147                 if (!data->host_ip ||
148                                 !data->host_ip[0])
149                         return data->host;
150                 else
151                         return data->host_ip[0];
152         } else if (g_str_equal(key, "VPN.Domain"))
153                 return data->domain;
154
155         return g_hash_table_lookup(data->setting_strings, key);
156 }
157
158 static char *get_ident(const char *path)
159 {
160         char *pos;
161
162         if (*path != '/')
163                 return NULL;
164
165         pos = strrchr(path, '/');
166         if (!pos)
167                 return NULL;
168
169         return pos + 1;
170 }
171
172 static void cancel_host_resolv(struct connection_data *data)
173 {
174         if (data->resolv_id != 0)
175                 g_resolv_cancel_lookup(data->resolv, data->resolv_id);
176
177         data->resolv_id = 0;
178
179         g_resolv_unref(data->resolv);
180         data->resolv = NULL;
181 }
182
183 static gboolean remove_resolv(gpointer user_data)
184 {
185         struct connection_data *data = user_data;
186
187         cancel_host_resolv(data);
188
189         return FALSE;
190 }
191
192 static void resolv_result(GResolvResultStatus status,
193                                         char **results, gpointer user_data)
194 {
195         struct connection_data *data = user_data;
196
197         DBG("status %d", status);
198
199         if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results &&
200                                                 g_strv_length(results) > 0) {
201                 g_strfreev(data->host_ip);
202                 data->host_ip = g_strdupv(results);
203         }
204
205         /*
206          * We cannot unref the resolver here as resolv struct is manipulated
207          * by gresolv.c after we return from this callback.
208          */
209         g_idle_add(remove_resolv, data);
210
211         data->resolv_id = 0;
212 }
213
214 static void resolv_host_addr(struct connection_data *data)
215 {
216         if (!data->host)
217                 return;
218
219         if (connman_inet_check_ipaddress(data->host) > 0)
220                 return;
221
222         if (data->host_ip)
223                 return;
224
225         data->resolv = g_resolv_new(0);
226         if (!data->resolv) {
227                 DBG("Cannot resolv %s", data->host);
228                 return;
229         }
230
231         DBG("Trying to resolv %s", data->host);
232
233         data->resolv_id = g_resolv_lookup_hostname(data->resolv, data->host,
234                                                 resolv_result, data);
235 }
236
237 static void free_config_cb_data(struct config_create_data *cb_data)
238 {
239         if (!cb_data)
240                 return;
241
242         g_free(cb_data->path);
243         cb_data->path = NULL;
244
245         if (cb_data->message) {
246                 dbus_message_unref(cb_data->message);
247                 cb_data->message = NULL;
248         }
249
250         cb_data->callback = NULL;
251
252         g_free(cb_data);
253 }
254
255 static bool provider_is_connected(struct connection_data *data)
256 {
257         return data && (g_str_equal(data->state, "ready") ||
258                         g_str_equal(data->state, "configuration"));
259 }
260
261 static void set_provider_state(struct connection_data *data)
262 {
263         enum connman_provider_state state = CONNMAN_PROVIDER_STATE_UNKNOWN;
264         int err = 0;
265
266         DBG("provider %p new state %s", data->provider, data->state);
267
268         if (!provider_is_connected(data)) {
269                 g_free(data->service_ident);
270                 data->service_ident = NULL;
271         }
272
273         if (g_str_equal(data->state, "ready")) {
274                 state = CONNMAN_PROVIDER_STATE_READY;
275                 goto set;
276         } else if (g_str_equal(data->state, "configuration")) {
277                 state = CONNMAN_PROVIDER_STATE_CONNECT;
278         } else if (g_str_equal(data->state, "idle")) {
279                 state = CONNMAN_PROVIDER_STATE_IDLE;
280         } else if (g_str_equal(data->state, "disconnect")) {
281                 err = ECONNREFUSED;
282                 state = CONNMAN_PROVIDER_STATE_DISCONNECT;
283                 goto set;
284         } else if (g_str_equal(data->state, "failure")) {
285                 err = ECONNREFUSED;
286                 state = CONNMAN_PROVIDER_STATE_FAILURE;
287                 goto set;
288         }
289
290         connman_provider_set_state(data->provider, state);
291         return;
292
293 set:
294         if (data->cb_data)
295                 data->cb_data->callback(data->cb_data->message,
296                                         err, data->ident);
297
298         connman_provider_set_state(data->provider, state);
299
300         free_config_cb_data(data->cb_data);
301         data->cb_data = NULL;
302 }
303
304 static int create_provider(struct connection_data *data, void *user_data)
305 {
306         struct connman_provider_driver *driver = user_data;
307         int err;
308
309         DBG("%s", data->path);
310
311         data->provider = connman_provider_get(data->ident);
312         if (!data->provider)
313                 return -ENOMEM;
314
315         DBG("provider %p name %s", data->provider, data->name);
316
317         connman_provider_set_data(data->provider, data);
318         connman_provider_set_driver(data->provider, driver);
319
320         err = connman_provider_create_service(data->provider);
321         if (err == 0) {
322                 connman_provider_set_immutable(data->provider, data->immutable);
323                 if (g_str_equal(data->state, "ready")) {
324                         connman_provider_set_index(data->provider,
325                                                         data->index);
326                         if (data->ip)
327                                 connman_provider_set_ipaddress(data->provider,
328                                                                 data->ip);
329                 }
330
331                 set_provider_state(data);
332         }
333
334         return 0;
335 }
336
337 static void destroy_route(gpointer user_data)
338 {
339         struct vpn_route *route = user_data;
340
341         g_free(route->network);
342         g_free(route->netmask);
343         g_free(route->gateway);
344         g_free(route);
345 }
346
347 static struct connection_data *create_connection_data(const char *path)
348 {
349         struct connection_data *data;
350
351         data = g_try_new0(struct connection_data, 1);
352         if (!data)
353                 return NULL;
354
355         DBG("path %s", path);
356
357         data->path = g_strdup(path);
358         data->ident = g_strdup(get_ident(path));
359         data->index = -1;
360
361         data->setting_strings = g_hash_table_new_full(g_str_hash,
362                                                 g_str_equal, g_free, g_free);
363
364         data->server_routes = g_hash_table_new_full(g_direct_hash,
365                                         g_str_equal, g_free, destroy_route);
366         data->user_routes = g_hash_table_new_full(g_str_hash,
367                                         g_str_equal, g_free, destroy_route);
368
369         return data;
370 }
371
372 static int extract_ip(DBusMessageIter *array, int family,
373                                                 struct connection_data *data)
374 {
375         DBusMessageIter dict;
376         char *address = NULL, *gateway = NULL, *netmask = NULL, *peer = NULL;
377         unsigned char prefix_len;
378
379         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
380                 return -EINVAL;
381
382         dbus_message_iter_recurse(array, &dict);
383
384         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
385                 DBusMessageIter entry, value;
386                 const char *key;
387
388                 dbus_message_iter_recurse(&dict, &entry);
389                 dbus_message_iter_get_basic(&entry, &key);
390
391                 dbus_message_iter_next(&entry);
392                 dbus_message_iter_recurse(&entry, &value);
393
394                 if (g_str_equal(key, "Address")) {
395                         dbus_message_iter_get_basic(&value, &address);
396                         DBG("address %s", address);
397                 } else if (g_str_equal(key, "Netmask")) {
398                         dbus_message_iter_get_basic(&value, &netmask);
399                         DBG("netmask %s", netmask);
400                 } else if (g_str_equal(key, "PrefixLength")) {
401                         dbus_message_iter_get_basic(&value, &netmask);
402                         DBG("prefix length %s", netmask);
403                 } else if (g_str_equal(key, "Peer")) {
404                         dbus_message_iter_get_basic(&value, &peer);
405                         DBG("peer %s", peer);
406                 } else if (g_str_equal(key, "Gateway")) {
407                         dbus_message_iter_get_basic(&value, &gateway);
408                         DBG("gateway %s", gateway);
409                 }
410
411                 dbus_message_iter_next(&dict);
412         }
413
414         connman_ipaddress_free(data->ip);
415         data->ip = connman_ipaddress_alloc(family);
416         if (!data->ip)
417                 return -ENOMEM;
418
419         switch (family) {
420         case AF_INET:
421                 connman_ipaddress_set_ipv4(data->ip, address, netmask,
422                                                                 gateway);
423                 break;
424         case AF_INET6:
425                 prefix_len = atoi(netmask);
426                 connman_ipaddress_set_ipv6(data->ip, address, prefix_len,
427                                                                 gateway);
428                 break;
429         default:
430                 return -EINVAL;
431         }
432
433         connman_ipaddress_set_peer(data->ip, peer);
434
435         return 0;
436 }
437
438 static int extract_nameservers(DBusMessageIter *array,
439                                                 struct connection_data *data)
440 {
441         DBusMessageIter entry;
442         char **nameservers = NULL;
443         int i = 0;
444
445         dbus_message_iter_recurse(array, &entry);
446
447         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
448                 const char *nameserver;
449
450                 dbus_message_iter_get_basic(&entry, &nameserver);
451
452                 nameservers = g_try_renew(char *, nameservers, i + 2);
453                 if (!nameservers)
454                         return -ENOMEM;
455
456                 DBG("[%d] %s", i, nameserver);
457
458                 nameservers[i] = g_strdup(nameserver);
459                 if (!nameservers[i])
460                         return -ENOMEM;
461
462                 nameservers[++i] = NULL;
463
464                 dbus_message_iter_next(&entry);
465         }
466
467         g_strfreev(data->nameservers);
468         data->nameservers = nameservers;
469
470         return 0;
471 }
472
473 static int errorstr2val(const char *error) {
474         if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".InProgress") == 0)
475                 return -EINPROGRESS;
476
477         if (g_strcmp0(error, CONNMAN_ERROR_INTERFACE ".AlreadyConnected") == 0)
478                 return -EISCONN;
479
480         return -ECONNREFUSED;
481 }
482
483 static void connect_reply(DBusPendingCall *call, void *user_data)
484 {
485         DBusMessage *reply;
486         DBusError error;
487         struct connection_data *data = user_data;
488         struct config_create_data *cb_data = data->cb_data;
489
490         if (!dbus_pending_call_get_completed(call))
491                 return;
492
493         DBG("user_data %p path %s", user_data, cb_data ? cb_data->path : NULL);
494
495         reply = dbus_pending_call_steal_reply(call);
496
497         dbus_error_init(&error);
498
499         if (dbus_set_error_from_message(&error, reply)) {
500                 int err = errorstr2val(error.name);
501
502                 if (err != -EINPROGRESS) {
503                         connman_error("Connect reply: %s (%s)", error.message,
504                                                                 error.name);
505                         DBG("data %p cb_data %p", data, cb_data);
506
507                         if (cb_data) {
508                                 cb_data->callback(cb_data->message, err, NULL);
509                                 free_config_cb_data(cb_data);
510                                 data->cb_data = NULL;
511                         }
512                 }
513
514                 dbus_error_free(&error);
515         }
516
517         /*
518          * The vpn connection is up when we get a "ready" state
519          * property so at this point we do nothing for the provider
520          * state.
521          */
522
523         dbus_message_unref(reply);
524
525         dbus_pending_call_unref(call);
526 }
527
528 static int connect_provider(struct connection_data *data, void *user_data,
529                         const char *dbus_sender)
530 {
531         DBusPendingCall *call;
532         DBusMessage *message;
533         struct config_create_data *cb_data = user_data;
534         struct connman_service *transport = connman_service_get_default();
535
536         DBG("data %p user %p path %s sender %s", data, cb_data, data->path,
537                                                                 dbus_sender);
538
539         if (!transport) {
540                 DBG("no default service, refusing to connect");
541                 return -EINVAL;
542         }
543
544         data->connect_pending = false;
545
546         /* We need to pass original dbus sender to connman-vpnd,
547          * use a Connect2 method for that if the original dbus sender is set.
548          * Connect method requires no parameter, Connect2 requires dbus sender
549          * name to be set.
550          */
551         message = dbus_message_new_method_call(VPN_SERVICE, data->path,
552                                         VPN_CONNECTION_INTERFACE,
553                                         dbus_sender && *dbus_sender ?
554                                                 VPN_CONNECT2 : VPN_CONNECT);
555         if (!message)
556                 return -ENOMEM;
557
558         if (dbus_sender && *dbus_sender)
559                 dbus_message_append_args(message, DBUS_TYPE_STRING,
560                                         &dbus_sender, NULL);
561         else
562                 dbus_sender = "";
563
564         if (!dbus_connection_send_with_reply(connection, message,
565                                                 &call, DBUS_TIMEOUT)) {
566                 connman_error("Unable to call %s.%s()",
567                         VPN_CONNECTION_INTERFACE, dbus_sender && *dbus_sender ?
568                                                 VPN_CONNECT2 : VPN_CONNECT);
569                 dbus_message_unref(message);
570                 return -EINVAL;
571         }
572
573         if (!call) {
574                 dbus_message_unref(message);
575                 return -EINVAL;
576         }
577
578         if (cb_data) {
579                 g_free(cb_data->path);
580                 cb_data->path = g_strdup(data->path);
581         }
582
583         /*
584          * This is the service which (most likely) will be used
585          * as a transport for VPN connection.
586          */
587         g_free(data->service_ident);
588         data->service_ident =
589                 g_strdup(connman_service_get_identifier(transport));
590         DBG("transport %s", data->service_ident);
591
592         dbus_pending_call_set_notify(call, connect_reply, data, NULL);
593
594         dbus_message_unref(message);
595
596         return -EINPROGRESS;
597 }
598
599 static void add_connection(const char *path, DBusMessageIter *properties,
600                         void *user_data)
601 {
602         struct connection_data *data;
603         int err;
604         char *ident = get_ident(path);
605         bool found = false;
606
607         data = g_hash_table_lookup(vpn_connections, ident);
608         if (data) {
609                 /*
610                  * We might have a dummy connection struct here that
611                  * was created by configuration_create_reply() so in
612                  * that case just continue.
613                  */
614                 if (!data->connect_pending)
615                         return;
616
617                 found = true;
618         } else {
619                 data = create_connection_data(path);
620                 if (!data)
621                         return;
622         }
623
624         DBG("data %p path %s", data, path);
625
626         while (dbus_message_iter_get_arg_type(properties) ==
627                         DBUS_TYPE_DICT_ENTRY) {
628                 DBusMessageIter entry, value;
629                 const char *key;
630                 char *str;
631
632                 dbus_message_iter_recurse(properties, &entry);
633                 dbus_message_iter_get_basic(&entry, &key);
634
635                 dbus_message_iter_next(&entry);
636                 dbus_message_iter_recurse(&entry, &value);
637
638                 if (g_str_equal(key, "State")) {
639                         dbus_message_iter_get_basic(&value, &str);
640                         DBG("state %s -> %s", data->state, str);
641                         data->state = g_strdup(str);
642                 } else if (g_str_equal(key, "IPv4")) {
643                         extract_ip(&value, AF_INET, data);
644                 } else if (g_str_equal(key, "IPv6")) {
645                         extract_ip(&value, AF_INET6, data);
646                 } else if (g_str_equal(key, "Name")) {
647                         dbus_message_iter_get_basic(&value, &str);
648                         data->name = g_strdup(str);
649                 } else if (g_str_equal(key, "Type")) {
650                         dbus_message_iter_get_basic(&value, &str);
651                         data->type = g_strdup(str);
652                 } else if (g_str_equal(key, "Immutable")) {
653                         dbus_bool_t immutable;
654
655                         dbus_message_iter_get_basic(&value, &immutable);
656                         data->immutable = immutable;
657                 } else if (g_str_equal(key, "Host")) {
658                         dbus_message_iter_get_basic(&value, &str);
659                         data->host = g_strdup(str);
660                 } else if (g_str_equal(key, "Domain")) {
661                         dbus_message_iter_get_basic(&value, &str);
662                         g_free(data->domain);
663                         data->domain = g_strdup(str);
664                 } else if (g_str_equal(key, "Nameservers")) {
665                         extract_nameservers(&value, data);
666                 } else if (g_str_equal(key, "Index")) {
667                         dbus_message_iter_get_basic(&value, &data->index);
668                 } else if (g_str_equal(key, "ServerRoutes")) {
669                         /* Ignored */
670                 } else if (g_str_equal(key, "UserRoutes")) {
671                         /* Ignored */
672                 } else {
673                         if (dbus_message_iter_get_arg_type(&value) ==
674                                                         DBUS_TYPE_STRING) {
675                                 dbus_message_iter_get_basic(&value, &str);
676                                 g_hash_table_replace(data->setting_strings,
677                                                 g_strdup(key), g_strdup(str));
678                         } else {
679                                 DBG("unknown key %s", key);
680                         }
681                 }
682
683                 dbus_message_iter_next(properties);
684         }
685
686         if (!found)
687                 g_hash_table_insert(vpn_connections, g_strdup(data->ident),
688                                                                         data);
689
690         err = create_provider(data, user_data);
691         if (err < 0)
692                 goto out;
693
694         resolv_host_addr(data);
695
696         if (data->nameservers)
697                 connman_provider_set_nameservers(data->provider,
698                                                 data->nameservers);
699
700         if (data->domain)
701                 connman_provider_set_domain(data->provider,
702                                                 data->domain);
703
704         if (data->connect_pending) {
705                 const char *dbus_sender = NULL;
706
707                 if (data->cb_data && data->cb_data->message) {
708                         dbus_sender =
709                                 dbus_message_get_sender(data->cb_data->message);
710                 }
711                 connect_provider(data, data->cb_data, dbus_sender);
712         }
713
714         return;
715
716 out:
717         DBG("removing %s", data->ident);
718         g_hash_table_remove(vpn_connections, data->ident);
719 }
720
721 static void get_connections_reply(DBusPendingCall *call, void *user_data)
722 {
723         DBusMessage *reply;
724         DBusError error;
725         DBusMessageIter array, dict;
726         const char *signature = DBUS_TYPE_ARRAY_AS_STRING
727                 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
728                 DBUS_TYPE_OBJECT_PATH_AS_STRING
729                 DBUS_TYPE_ARRAY_AS_STRING
730                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
731                 DBUS_TYPE_STRING_AS_STRING
732                 DBUS_TYPE_VARIANT_AS_STRING
733                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
734                 DBUS_STRUCT_END_CHAR_AS_STRING;
735
736         if (!dbus_pending_call_get_completed(call))
737                 return;
738
739         DBG("");
740
741         reply = dbus_pending_call_steal_reply(call);
742
743         dbus_error_init(&error);
744
745         if (dbus_set_error_from_message(&error, reply)) {
746                 connman_error("%s", error.message);
747                 dbus_error_free(&error);
748                 goto done;
749         }
750
751         if (!dbus_message_has_signature(reply, signature)) {
752                 connman_error("vpnd signature \"%s\" does not match "
753                                                         "expected \"%s\"",
754                         dbus_message_get_signature(reply), signature);
755                 goto done;
756         }
757
758         if (!dbus_message_iter_init(reply, &array))
759                 goto done;
760
761         dbus_message_iter_recurse(&array, &dict);
762
763         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
764                 DBusMessageIter value, properties;
765                 const char *path;
766
767                 dbus_message_iter_recurse(&dict, &value);
768                 dbus_message_iter_get_basic(&value, &path);
769
770                 dbus_message_iter_next(&value);
771                 dbus_message_iter_recurse(&value, &properties);
772
773                 add_connection(path, &properties, user_data);
774
775                 dbus_message_iter_next(&dict);
776         }
777
778 done:
779         dbus_message_unref(reply);
780
781         dbus_pending_call_unref(call);
782 }
783
784 static int get_connections(void *user_data)
785 {
786         DBusPendingCall *call;
787         DBusMessage *message;
788
789         DBG("");
790
791         message = dbus_message_new_method_call(VPN_SERVICE, "/",
792                                         VPN_MANAGER_INTERFACE,
793                                         GET_CONNECTIONS);
794         if (!message)
795                 return -ENOMEM;
796
797         if (!dbus_connection_send_with_reply(connection, message,
798                                                 &call, DBUS_TIMEOUT)) {
799                 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
800                                                         GET_CONNECTIONS);
801                 dbus_message_unref(message);
802                 return -EINVAL;
803         }
804
805         if (!call) {
806                 dbus_message_unref(message);
807                 return -EINVAL;
808         }
809
810         dbus_pending_call_set_notify(call, get_connections_reply,
811                                                         user_data, NULL);
812
813         dbus_message_unref(message);
814
815         return -EINPROGRESS;
816 }
817
818 static int provider_probe(struct connman_provider *provider)
819 {
820         return 0;
821 }
822
823 static void remove_connection_reply(DBusPendingCall *call, void *user_data)
824 {
825         DBusMessage *reply;
826         DBusError error;
827
828         if (!dbus_pending_call_get_completed(call))
829                 return;
830
831         DBG("");
832
833         reply = dbus_pending_call_steal_reply(call);
834
835         dbus_error_init(&error);
836
837         if (dbus_set_error_from_message(&error, reply)) {
838                 /*
839                  * If the returned error is NotFound, it means that we
840                  * have actually removed the provider in vpnd already.
841                  */
842                 if (!dbus_error_has_name(&error,
843                                 CONNMAN_ERROR_INTERFACE".NotFound"))
844                         connman_error("%s", error.message);
845
846                 dbus_error_free(&error);
847         }
848
849         dbus_message_unref(reply);
850
851         dbus_pending_call_unref(call);
852 }
853
854 static int provider_remove(struct connman_provider *provider)
855 {
856         DBusPendingCall *call;
857         DBusMessage *message;
858         struct connection_data *data;
859
860         data = connman_provider_get_data(provider);
861
862         DBG("provider %p data %p", provider, data);
863
864         if (!data) {
865                 /*
866                  * This means the provider is already removed,
867                  * just ignore the dbus in this case.
868                  */
869                 return -EALREADY;
870         }
871
872         /*
873          * When provider.c:provider_remove() calls this function,
874          * it will remove the provider itself after the call.
875          * This means that we cannot use the provider pointer later
876          * as it is no longer valid.
877          */
878         data->provider = NULL;
879
880         message = dbus_message_new_method_call(VPN_SERVICE, "/",
881                                         VPN_MANAGER_INTERFACE,
882                                         VPN_REMOVE);
883         if (!message)
884                 return -ENOMEM;
885
886         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &data->path,
887                                 NULL);
888
889         if (!dbus_connection_send_with_reply(connection, message,
890                                                 &call, DBUS_TIMEOUT)) {
891                 connman_error("Unable to call %s.%s()", VPN_MANAGER_INTERFACE,
892                                                         VPN_REMOVE);
893                 dbus_message_unref(message);
894                 return -EINVAL;
895         }
896
897         if (!call) {
898                 dbus_message_unref(message);
899                 return -EINVAL;
900         }
901
902         dbus_pending_call_set_notify(call, remove_connection_reply,
903                                                         NULL, NULL);
904
905         dbus_message_unref(message);
906
907         return 0;
908 }
909
910 static int provider_connect(struct connman_provider *provider,
911                                         const char *dbus_sender)
912 {
913         struct connection_data *data;
914
915         data = connman_provider_get_data(provider);
916         if (!data)
917                 return -EINVAL;
918
919         return connect_provider(data, NULL, dbus_sender);
920 }
921
922 static void disconnect_reply(DBusPendingCall *call, void *user_data)
923 {
924         struct connection_data *data = user_data;
925         DBusMessage *reply;
926         DBusError error;
927
928         DBG("user %p", user_data);
929
930         reply = dbus_pending_call_steal_reply(call);
931
932         dbus_error_init(&error);
933
934         if (dbus_set_error_from_message(&error, reply)) {
935                 connman_error("%s", error.message);
936                 dbus_error_free(&error);
937                 goto done;
938         }
939
940 done:
941         dbus_message_unref(reply);
942         dbus_pending_call_unref(call);
943         data->disconnect_call = NULL;
944 }
945
946 static int disconnect_provider(struct connection_data *data)
947 {
948         bool sent;
949         DBusMessage *message;
950
951         DBG("data %p path %s", data, data->path);
952
953         if (data->disconnect_call) {
954                 DBG("already disconnecting");
955                 return -EINVAL;
956         }
957
958         message = dbus_message_new_method_call(VPN_SERVICE, data->path,
959                                         VPN_CONNECTION_INTERFACE,
960                                         VPN_DISCONNECT);
961         if (!message)
962                 return -ENOMEM;
963
964         sent = dbus_connection_send_with_reply(connection, message,
965                                         &data->disconnect_call, DBUS_TIMEOUT);
966         dbus_message_unref(message);
967
968         if (!sent || !data->disconnect_call) {
969                 connman_error("Unable to call %s.%s()",
970                         VPN_CONNECTION_INTERFACE, VPN_DISCONNECT);
971                 return -EINVAL;
972         }
973
974         dbus_pending_call_set_notify(data->disconnect_call, disconnect_reply,
975                                                                 data, NULL);
976
977         g_free(data->service_ident);
978         data->service_ident = NULL;
979
980         connman_provider_set_state(data->provider,
981                                         CONNMAN_PROVIDER_STATE_DISCONNECT);
982         return -EINPROGRESS;
983 }
984
985 static int provider_disconnect(struct connman_provider *provider)
986 {
987         struct connection_data *data;
988
989         DBG("provider %p", provider);
990
991         data = connman_provider_get_data(provider);
992         if (!data)
993                 return -EINVAL;
994
995         if (provider_is_connected(data))
996                 return disconnect_provider(data);
997
998         return 0;
999 }
1000
1001 static void configuration_create_reply(DBusPendingCall *call, void *user_data)
1002 {
1003         DBusMessage *reply;
1004         DBusError error;
1005         DBusMessageIter iter;
1006         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1007         const char *path;
1008         char *ident;
1009         struct connection_data *data;
1010         struct config_create_data *cb_data = user_data;
1011
1012         if (!dbus_pending_call_get_completed(call))
1013                 return;
1014
1015         DBG("user %p", cb_data);
1016
1017         reply = dbus_pending_call_steal_reply(call);
1018
1019         dbus_error_init(&error);
1020
1021         if (dbus_set_error_from_message(&error, reply)) {
1022                 connman_error("dbus error: %s", error.message);
1023                 dbus_error_free(&error);
1024                 goto done;
1025         }
1026
1027         if (!dbus_message_has_signature(reply, signature)) {
1028                 connman_error("vpn configuration signature \"%s\" does not "
1029                                                 "match expected \"%s\"",
1030                         dbus_message_get_signature(reply), signature);
1031                 goto done;
1032         }
1033
1034         if (!dbus_message_iter_init(reply, &iter))
1035                 goto done;
1036
1037         dbus_message_iter_get_basic(&iter, &path);
1038
1039         /*
1040          * Then try to connect the VPN as expected by ConnectProvider API
1041          */
1042         ident = get_ident(path);
1043
1044         data = g_hash_table_lookup(vpn_connections, ident);
1045         if (!data) {
1046                 /*
1047                  * Someone removed the data. We cannot really continue.
1048                  */
1049                 DBG("Pending data not found for %s, cannot continue!", ident);
1050         } else {
1051                 data->call = NULL;
1052                 data->connect_pending = true;
1053
1054                 if (!data->cb_data)
1055                         data->cb_data = cb_data;
1056                 else
1057                         DBG("Connection callback data already in use!");
1058
1059                 /*
1060                  * Connection is created in add_connections() after
1061                  * we have received the ConnectionAdded signal.
1062                  */
1063
1064                 DBG("cb %p msg %p", data->cb_data,
1065                         data->cb_data ? data->cb_data->message : NULL);
1066         }
1067
1068 done:
1069         dbus_message_unref(reply);
1070
1071         dbus_pending_call_unref(call);
1072 }
1073
1074 static void set_dbus_ident(char *ident)
1075 {
1076         int i, len = strlen(ident);
1077
1078         for (i = 0; i < len; i++) {
1079                 if (ident[i] >= '0' && ident[i] <= '9')
1080                         continue;
1081                 if (ident[i] >= 'a' && ident[i] <= 'z')
1082                         continue;
1083                 if (ident[i] >= 'A' && ident[i] <= 'Z')
1084                         continue;
1085                 ident[i] = '_';
1086         }
1087 }
1088
1089 static struct vpn_route *parse_user_route(const char *user_route)
1090 {
1091         char *network, *netmask;
1092         struct vpn_route *route = NULL;
1093         int family = PF_UNSPEC;
1094         char **elems = g_strsplit(user_route, "/", 0);
1095
1096         if (!elems)
1097                 return NULL;
1098
1099         network = elems[0];
1100         if (!network || *network == '\0') {
1101                 DBG("no network/netmask set");
1102                 goto out;
1103         }
1104
1105         netmask = elems[1];
1106         if (netmask && *netmask == '\0') {
1107                 DBG("no netmask set");
1108                 goto out;
1109         }
1110
1111         if (g_strrstr(network, ":"))
1112                 family = AF_INET6;
1113         else if (g_strrstr(network, ".")) {
1114                 family = AF_INET;
1115
1116                 if (!g_strrstr(netmask, ".")) {
1117                         /* We have netmask length */
1118                         in_addr_t addr;
1119                         struct in_addr netmask_in;
1120                         unsigned char prefix_len = 32;
1121
1122                         if (netmask) {
1123                                 char *ptr;
1124                                 long int value = strtol(netmask, &ptr, 10);
1125                                 if (ptr != netmask && *ptr == '\0' &&
1126                                                 value && value <= 32)
1127                                         prefix_len = value;
1128                         }
1129
1130                         addr = 0xffffffff << (32 - prefix_len);
1131                         netmask_in.s_addr = htonl(addr);
1132                         netmask = inet_ntoa(netmask_in);
1133
1134                         DBG("network %s netmask %s", network, netmask);
1135                 }
1136         }
1137
1138         route = g_try_new(struct vpn_route, 1);
1139         if (!route)
1140                 goto out;
1141
1142         route->network = g_strdup(network);
1143         route->netmask = g_strdup(netmask);
1144         route->gateway = NULL;
1145         route->family = family;
1146
1147 out:
1148         g_strfreev(elems);
1149         return route;
1150 }
1151
1152 static GSList *get_user_networks(DBusMessageIter *array)
1153 {
1154         DBusMessageIter entry;
1155         GSList *list = NULL;
1156
1157         dbus_message_iter_recurse(array, &entry);
1158
1159         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1160                 const char *val;
1161                 struct vpn_route *route;
1162
1163                 dbus_message_iter_get_basic(&entry, &val);
1164
1165                 route = parse_user_route(val);
1166                 if (route)
1167                         list = g_slist_prepend(list, route);
1168
1169                 dbus_message_iter_next(&entry);
1170         }
1171
1172         return list;
1173 }
1174
1175 static void append_route(DBusMessageIter *iter, void *user_data)
1176 {
1177         struct vpn_route *route = user_data;
1178         DBusMessageIter item;
1179         int family = 0;
1180
1181         connman_dbus_dict_open(iter, &item);
1182
1183         if (!route)
1184                 goto empty_dict;
1185
1186         if (route->family == AF_INET)
1187                 family = 4;
1188         else if (route->family == AF_INET6)
1189                 family = 6;
1190
1191         if (family != 0)
1192                 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
1193                                         DBUS_TYPE_INT32, &family);
1194
1195         if (route->network)
1196                 connman_dbus_dict_append_basic(&item, "Network",
1197                                         DBUS_TYPE_STRING, &route->network);
1198
1199         if (route->netmask)
1200                 connman_dbus_dict_append_basic(&item, "Netmask",
1201                                         DBUS_TYPE_STRING, &route->netmask);
1202
1203         if (route->gateway)
1204                 connman_dbus_dict_append_basic(&item, "Gateway",
1205                                         DBUS_TYPE_STRING, &route->gateway);
1206
1207 empty_dict:
1208         connman_dbus_dict_close(iter, &item);
1209 }
1210
1211 static void append_routes(DBusMessageIter *iter, void *user_data)
1212 {
1213         GSList *list, *routes = user_data;
1214
1215         DBG("routes %p", routes);
1216
1217         for (list = routes; list; list = g_slist_next(list)) {
1218                 DBusMessageIter dict;
1219                 struct vpn_route *route = list->data;
1220
1221                 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
1222                                                 &dict);
1223                 append_route(&dict, route);
1224                 dbus_message_iter_close_container(iter, &dict);
1225         }
1226 }
1227
1228 static int create_configuration(DBusMessage *msg, connection_ready_cb callback)
1229 {
1230         DBusMessage *new_msg = NULL;
1231         DBusPendingCall *call;
1232         DBusMessageIter iter, array, new_iter, new_dict;
1233         const char *type = NULL, *name = NULL;
1234         const char *host = NULL, *domain = NULL;
1235         char *ident, *me = NULL;
1236         int err = 0;
1237         dbus_bool_t result;
1238         struct connection_data *data;
1239         struct config_create_data *user_data = NULL;
1240         GSList *networks = NULL;
1241
1242         /*
1243          * We copy the old message data into new message. We cannot
1244          * just use the old message as is because the user route
1245          * information is not in the same format in vpnd.
1246          */
1247         new_msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1248         dbus_message_iter_init_append(new_msg, &new_iter);
1249         connman_dbus_dict_open(&new_iter, &new_dict);
1250
1251         dbus_message_iter_init(msg, &iter);
1252         dbus_message_iter_recurse(&iter, &array);
1253
1254         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1255                 DBusMessageIter entry, value;
1256                 void *item_value;
1257                 const char *key;
1258                 int value_type;
1259
1260                 dbus_message_iter_recurse(&array, &entry);
1261                 dbus_message_iter_get_basic(&entry, &key);
1262
1263                 dbus_message_iter_next(&entry);
1264                 dbus_message_iter_recurse(&entry, &value);
1265
1266                 value_type = dbus_message_iter_get_arg_type(&value);
1267                 item_value = NULL;
1268
1269                 switch (value_type) {
1270                 case DBUS_TYPE_STRING:
1271                         dbus_message_iter_get_basic(&value, &item_value);
1272
1273                         if (g_str_equal(key, "Type"))
1274                                 type = (const char *)item_value;
1275                         else if (g_str_equal(key, "Name"))
1276                                 name = (const char *)item_value;
1277                         else if (g_str_equal(key, "Host"))
1278                                 host = (const char *)item_value;
1279                         else if (g_str_equal(key, "VPN.Domain"))
1280                                 domain = (const char *)item_value;
1281
1282                         DBG("%s %s", key, (char *)item_value);
1283
1284                         if (item_value)
1285                                 connman_dbus_dict_append_basic(&new_dict, key,
1286                                                 value_type, &item_value);
1287                         break;
1288                 case DBUS_TYPE_ARRAY:
1289                         if (g_str_equal(key, "Networks")) {
1290                                 networks = get_user_networks(&value);
1291                                 connman_dbus_dict_append_array(&new_dict,
1292                                                         "UserRoutes",
1293                                                         DBUS_TYPE_DICT_ENTRY,
1294                                                         append_routes,
1295                                                         networks);
1296                         }
1297                         break;
1298                 }
1299
1300                 dbus_message_iter_next(&array);
1301         }
1302
1303         connman_dbus_dict_close(&new_iter, &new_dict);
1304
1305         DBG("VPN type %s name %s host %s domain %s networks %p",
1306                 type, name, host, domain, networks);
1307
1308         if (!host || !domain) {
1309                 err = -EINVAL;
1310                 goto done;
1311         }
1312
1313         if (!type || !name) {
1314                 err = -EOPNOTSUPP;
1315                 goto done;
1316         }
1317
1318         ident = g_strdup_printf("%s_%s", host, domain);
1319         set_dbus_ident(ident);
1320
1321         DBG("ident %s", ident);
1322
1323         data = g_hash_table_lookup(vpn_connections, ident);
1324         if (data) {
1325                 if (data->call || data->cb_data) {
1326                         DBG("create configuration call already pending");
1327                         err = -EINPROGRESS;
1328                         goto done;
1329                 }
1330         } else {
1331                 char *path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1332                                                                 ident);
1333                 data = create_connection_data(path);
1334                 g_free(path);
1335
1336                 if (!data) {
1337                         err = -ENOMEM;
1338                         goto done;
1339                 }
1340
1341                 g_hash_table_insert(vpn_connections, g_strdup(ident), data);
1342         }
1343
1344         /*
1345          * User called net.connman.Manager.ConnectProvider if we are here.
1346          * So use the data from original message in the new msg.
1347          */
1348         me = g_strdup(dbus_message_get_destination(msg));
1349
1350         dbus_message_set_interface(new_msg, VPN_MANAGER_INTERFACE);
1351         dbus_message_set_path(new_msg, "/");
1352         dbus_message_set_destination(new_msg, VPN_SERVICE);
1353         dbus_message_set_sender(new_msg, me);
1354         dbus_message_set_member(new_msg, "Create");
1355
1356         user_data = g_try_new0(struct config_create_data, 1);
1357         if (!user_data) {
1358                 err = -ENOMEM;
1359                 goto done;
1360         }
1361
1362         user_data->callback = callback;
1363         user_data->message = dbus_message_ref(msg);
1364         user_data->path = NULL;
1365
1366         DBG("cb %p msg %p", user_data, msg);
1367
1368         result = dbus_connection_send_with_reply(connection, new_msg,
1369                                                 &call, DBUS_TIMEOUT);
1370         if (!result || !call) {
1371                 err = -EIO;
1372                 goto done;
1373         }
1374
1375         dbus_pending_call_set_notify(call, configuration_create_reply,
1376                                                         user_data, NULL);
1377         data->call = call;
1378
1379 done:
1380         if (new_msg)
1381                 dbus_message_unref(new_msg);
1382
1383         if (networks)
1384                 g_slist_free_full(networks, destroy_route);
1385
1386         g_free(me);
1387         return err;
1388 }
1389
1390 static bool check_host(char **hosts, char *host)
1391 {
1392         int i;
1393
1394         if (!hosts)
1395                 return false;
1396
1397         for (i = 0; hosts[i]; i++) {
1398                 if (g_strcmp0(hosts[i], host) == 0)
1399                         return true;
1400         }
1401
1402         return false;
1403 }
1404
1405 static void set_route(struct connection_data *data, struct vpn_route *route)
1406 {
1407         /*
1408          * If the VPN administrator/user has given a route to
1409          * VPN server, then we must discard that because the
1410          * server cannot be contacted via VPN tunnel.
1411          */
1412         if (check_host(data->host_ip, route->network)) {
1413                 DBG("Discarding VPN route to %s via %s at index %d",
1414                         route->network, route->gateway, data->index);
1415                 return;
1416         }
1417
1418         if (route->family == AF_INET6) {
1419                 unsigned char prefix_len = atoi(route->netmask);
1420
1421                 connman_inet_add_ipv6_network_route(data->index,
1422                                                         route->network,
1423                                                         route->gateway,
1424                                                         prefix_len);
1425         } else {
1426                 connman_inet_add_network_route(data->index, route->network,
1427                                                 route->gateway,
1428                                                 route->netmask);
1429         }
1430 }
1431
1432 static int set_routes(struct connman_provider *provider,
1433                                 enum connman_provider_route_type type)
1434 {
1435         struct connection_data *data;
1436         GHashTableIter iter;
1437         gpointer value, key;
1438
1439         DBG("provider %p", provider);
1440
1441         data = connman_provider_get_data(provider);
1442         if (!data)
1443                 return -EINVAL;
1444
1445         if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1446                                         type == CONNMAN_PROVIDER_ROUTE_USER) {
1447                 g_hash_table_iter_init(&iter, data->user_routes);
1448
1449                 while (g_hash_table_iter_next(&iter, &key, &value))
1450                         set_route(data, value);
1451         }
1452
1453         if (type == CONNMAN_PROVIDER_ROUTE_ALL ||
1454                                 type == CONNMAN_PROVIDER_ROUTE_SERVER) {
1455                 g_hash_table_iter_init(&iter, data->server_routes);
1456
1457                 while (g_hash_table_iter_next(&iter, &key, &value))
1458                         set_route(data, value);
1459         }
1460
1461         return 0;
1462 }
1463
1464 static bool check_routes(struct connman_provider *provider)
1465 {
1466         struct connection_data *data;
1467
1468         DBG("provider %p", provider);
1469
1470         data = connman_provider_get_data(provider);
1471         if (!data)
1472                 return false;
1473
1474         if (data->user_routes &&
1475                         g_hash_table_size(data->user_routes) > 0)
1476                 return true;
1477
1478         if (data->server_routes &&
1479                         g_hash_table_size(data->server_routes) > 0)
1480                 return true;
1481
1482         return false;
1483 }
1484
1485 static struct connman_provider_driver provider_driver = {
1486         .name = "VPN",
1487         .type = CONNMAN_PROVIDER_TYPE_VPN,
1488         .probe = provider_probe,
1489         .remove = provider_remove,
1490         .connect = provider_connect,
1491         .disconnect = provider_disconnect,
1492         .set_property = set_string,
1493         .get_property = get_string,
1494         .create = create_configuration,
1495         .set_routes = set_routes,
1496         .check_routes = check_routes,
1497 };
1498
1499 static void destroy_provider(struct connection_data *data)
1500 {
1501         DBG("data %p", data);
1502
1503         if (provider_is_connected(data))
1504                 connman_provider_disconnect(data->provider);
1505
1506         connman_provider_set_data(data->provider, NULL);
1507         connman_provider_remove(data->provider);
1508         data->provider = NULL;
1509 }
1510
1511 static void connection_destroy(gpointer hash_data)
1512 {
1513         struct connection_data *data = hash_data;
1514
1515         DBG("data %p", data);
1516
1517         if (data->provider)
1518                 destroy_provider(data);
1519
1520         if (data->call) {
1521                 dbus_pending_call_cancel(data->call);
1522                 dbus_pending_call_unref(data->call);
1523         }
1524
1525         if (data->disconnect_call) {
1526                 dbus_pending_call_cancel(data->disconnect_call);
1527                 dbus_pending_call_unref(data->disconnect_call);
1528         }
1529
1530         g_free(data->service_ident);
1531         g_free(data->path);
1532         g_free(data->ident);
1533         g_free(data->state);
1534         g_free(data->type);
1535         g_free(data->name);
1536         g_free(data->host);
1537         g_strfreev(data->host_ip);
1538         g_free(data->domain);
1539         g_hash_table_destroy(data->server_routes);
1540         g_hash_table_destroy(data->user_routes);
1541         g_strfreev(data->nameservers);
1542         g_hash_table_destroy(data->setting_strings);
1543         connman_ipaddress_free(data->ip);
1544
1545         cancel_host_resolv(data);
1546
1547         g_free(data);
1548 }
1549
1550 static void vpnd_created(DBusConnection *conn, void *user_data)
1551 {
1552         DBG("connection %p", conn);
1553
1554         get_connections(user_data);
1555 }
1556
1557 static void vpnd_removed(DBusConnection *conn, void *user_data)
1558 {
1559         DBG("connection %p", conn);
1560
1561         g_hash_table_remove_all(vpn_connections);
1562 }
1563
1564 static void remove_connection(DBusConnection *conn, const char *path)
1565 {
1566         DBG("path %s", path);
1567
1568         g_hash_table_remove(vpn_connections, get_ident(path));
1569 }
1570
1571 static gboolean connection_removed(DBusConnection *conn, DBusMessage *message,
1572                                 void *user_data)
1573 {
1574         const char *path;
1575         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
1576         struct connection_data *data;
1577
1578         if (!dbus_message_has_signature(message, signature)) {
1579                 connman_error("vpn removed signature \"%s\" does not match "
1580                                                         "expected \"%s\"",
1581                         dbus_message_get_signature(message), signature);
1582                 return TRUE;
1583         }
1584
1585         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1586                                 DBUS_TYPE_INVALID);
1587
1588         data = g_hash_table_lookup(vpn_connections, get_ident(path));
1589         if (data)
1590                 remove_connection(conn, path);
1591
1592         return TRUE;
1593 }
1594
1595 static gboolean connection_added(DBusConnection *conn, DBusMessage *message,
1596                                 void *user_data)
1597 {
1598         DBusMessageIter iter, properties;
1599         const char *path;
1600         const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
1601                 DBUS_TYPE_ARRAY_AS_STRING
1602                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1603                 DBUS_TYPE_STRING_AS_STRING
1604                 DBUS_TYPE_VARIANT_AS_STRING
1605                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
1606
1607         if (!dbus_message_has_signature(message, signature)) {
1608                 connman_error("vpn ConnectionAdded signature \"%s\" does not "
1609                                                 "match expected \"%s\"",
1610                         dbus_message_get_signature(message), signature);
1611                 return TRUE;
1612         }
1613
1614         DBG("");
1615
1616         if (!dbus_message_iter_init(message, &iter))
1617                 return TRUE;
1618
1619         dbus_message_iter_get_basic(&iter, &path);
1620
1621         dbus_message_iter_next(&iter);
1622         dbus_message_iter_recurse(&iter, &properties);
1623
1624         add_connection(path, &properties, user_data);
1625
1626         return TRUE;
1627 }
1628
1629 static int save_route(GHashTable *routes, int family, const char *network,
1630                         const char *netmask, const char *gateway)
1631 {
1632         struct vpn_route *route;
1633         char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
1634
1635         DBG("family %d network %s netmask %s", family, network, netmask);
1636
1637         route = g_hash_table_lookup(routes, key);
1638         if (!route) {
1639                 route = g_try_new0(struct vpn_route, 1);
1640                 if (!route) {
1641                         connman_error("out of memory");
1642                         return -ENOMEM;
1643                 }
1644
1645                 route->family = family;
1646                 route->network = g_strdup(network);
1647                 route->netmask = g_strdup(netmask);
1648                 route->gateway = g_strdup(gateway);
1649
1650                 g_hash_table_replace(routes, key, route);
1651         } else
1652                 g_free(key);
1653
1654         return 0;
1655 }
1656
1657 static int read_route_dict(GHashTable *routes, DBusMessageIter *dicts)
1658 {
1659         DBusMessageIter dict;
1660         const char *network, *netmask, *gateway;
1661         int family;
1662
1663         dbus_message_iter_recurse(dicts, &dict);
1664
1665         network = netmask = gateway = NULL;
1666         family = PF_UNSPEC;
1667
1668         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1669
1670                 DBusMessageIter entry, value;
1671                 const char *key;
1672
1673                 dbus_message_iter_recurse(&dict, &entry);
1674                 dbus_message_iter_get_basic(&entry, &key);
1675
1676                 dbus_message_iter_next(&entry);
1677                 dbus_message_iter_recurse(&entry, &value);
1678
1679                 if (g_str_equal(key, "ProtocolFamily")) {
1680                         int pf;
1681                         dbus_message_iter_get_basic(&value, &pf);
1682                         switch (pf) {
1683                         case 4:
1684                                 family = AF_INET;
1685                                 break;
1686                         case 6:
1687                                 family = AF_INET6;
1688                                 break;
1689                         }
1690                         DBG("family %d", family);
1691                 } else if (g_str_equal(key, "Netmask")) {
1692                         dbus_message_iter_get_basic(&value, &netmask);
1693                         DBG("netmask %s", netmask);
1694                 } else if (g_str_equal(key, "Network")) {
1695                         dbus_message_iter_get_basic(&value, &network);
1696                         DBG("host %s", network);
1697                 } else if (g_str_equal(key, "Gateway")) {
1698                         dbus_message_iter_get_basic(&value, &gateway);
1699                         DBG("gateway %s", gateway);
1700                 }
1701
1702                 dbus_message_iter_next(&dict);
1703         }
1704
1705         if (!netmask || !network || !gateway) {
1706                 DBG("Value missing.");
1707                 return -EINVAL;
1708         }
1709
1710         return save_route(routes, family, network, netmask, gateway);
1711 }
1712
1713 static int routes_changed(DBusMessageIter *array, GHashTable *routes)
1714 {
1715         DBusMessageIter entry;
1716         int ret = -EINVAL;
1717
1718         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) {
1719                 DBG("Expecting array, ignoring routes.");
1720                 return -EINVAL;
1721         }
1722
1723         while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
1724
1725                 dbus_message_iter_recurse(array, &entry);
1726
1727                 while (dbus_message_iter_get_arg_type(&entry) ==
1728                                                         DBUS_TYPE_STRUCT) {
1729                         DBusMessageIter dicts;
1730
1731                         dbus_message_iter_recurse(&entry, &dicts);
1732
1733                         while (dbus_message_iter_get_arg_type(&dicts) ==
1734                                                         DBUS_TYPE_ARRAY) {
1735                                 int err = read_route_dict(routes, &dicts);
1736                                 if (ret != 0)
1737                                         ret = err;
1738                                 dbus_message_iter_next(&dicts);
1739                         }
1740
1741                         dbus_message_iter_next(&entry);
1742                 }
1743
1744                 dbus_message_iter_next(array);
1745         }
1746
1747         return ret;
1748 }
1749
1750 static gboolean property_changed(DBusConnection *conn,
1751                                 DBusMessage *message,
1752                                 void *user_data)
1753 {
1754         const char *path = dbus_message_get_path(message);
1755         struct connection_data *data = NULL;
1756         DBusMessageIter iter, value;
1757         bool ip_set = false;
1758         int err;
1759         char *str;
1760         const char *key;
1761         const char *signature = DBUS_TYPE_STRING_AS_STRING
1762                 DBUS_TYPE_VARIANT_AS_STRING;
1763
1764         if (!dbus_message_has_signature(message, signature)) {
1765                 connman_error("vpn property signature \"%s\" does not match "
1766                                                         "expected \"%s\"",
1767                         dbus_message_get_signature(message), signature);
1768                 return TRUE;
1769         }
1770
1771         data = g_hash_table_lookup(vpn_connections, get_ident(path));
1772         if (!data)
1773                 return TRUE;
1774
1775         if (!dbus_message_iter_init(message, &iter))
1776                 return TRUE;
1777
1778         dbus_message_iter_get_basic(&iter, &key);
1779
1780         dbus_message_iter_next(&iter);
1781         dbus_message_iter_recurse(&iter, &value);
1782
1783         DBG("key %s", key);
1784
1785         if (g_str_equal(key, "State")) {
1786                 dbus_message_iter_get_basic(&value, &str);
1787
1788                 DBG("%s %s -> %s", data->path, data->state, str);
1789
1790                 if (g_str_equal(data->state, str))
1791                         return TRUE;
1792
1793                 g_free(data->state);
1794                 data->state = g_strdup(str);
1795
1796                 set_provider_state(data);
1797         } else if (g_str_equal(key, "Index")) {
1798                 dbus_message_iter_get_basic(&value, &data->index);
1799                 connman_provider_set_index(data->provider, data->index);
1800         } else if (g_str_equal(key, "IPv4")) {
1801                 err = extract_ip(&value, AF_INET, data);
1802                 ip_set = true;
1803         } else if (g_str_equal(key, "IPv6")) {
1804                 err = extract_ip(&value, AF_INET6, data);
1805                 ip_set = true;
1806         } else if (g_str_equal(key, "ServerRoutes")) {
1807                 err = routes_changed(&value, data->server_routes);
1808                 /*
1809                  * Note that the vpnd will delay the route sending a bit
1810                  * (in order to collect the routes from VPN client),
1811                  * so we might have got the State changed property before
1812                  * we got ServerRoutes. This means that we must try to set
1813                  * the routes here because they would be left unset otherwise.
1814                  */
1815                 if (err == 0)
1816                         set_routes(data->provider,
1817                                                 CONNMAN_PROVIDER_ROUTE_SERVER);
1818         } else if (g_str_equal(key, "UserRoutes")) {
1819                 err = routes_changed(&value, data->user_routes);
1820                 if (err == 0)
1821                         set_routes(data->provider,
1822                                                 CONNMAN_PROVIDER_ROUTE_USER);
1823         } else if (g_str_equal(key, "Nameservers")) {
1824                 if (extract_nameservers(&value, data) == 0 &&
1825                                                 data->nameservers)
1826                         connman_provider_set_nameservers(data->provider,
1827                                                         data->nameservers);
1828         } else if (g_str_equal(key, "Domain")) {
1829                 dbus_message_iter_get_basic(&value, &str);
1830                 g_free(data->domain);
1831                 data->domain = g_strdup(str);
1832                 connman_provider_set_domain(data->provider, data->domain);
1833         }
1834
1835         if (ip_set && err == 0) {
1836                 err = connman_provider_set_ipaddress(data->provider, data->ip);
1837                 if (err < 0)
1838                         DBG("setting provider IP address failed (%s/%d)",
1839                                 strerror(-err), -err);
1840         }
1841
1842         return TRUE;
1843 }
1844
1845 static int vpn_find_online_transport_cb(struct connman_service *service,
1846                                                         void *user_data)
1847 {
1848         if (connman_service_get_type(service) != CONNMAN_SERVICE_TYPE_VPN) {
1849                 switch (connman_service_get_state(service)) {
1850                 case CONNMAN_SERVICE_STATE_ONLINE:
1851                         *((struct connman_service**)user_data) = service;
1852                         return 1;
1853                 default:
1854                         break;
1855                 }
1856         }
1857
1858         return 0;
1859 }
1860
1861 static struct connman_service *vpn_find_online_transport()
1862 {
1863         struct connman_service *service = NULL;
1864
1865         connman_service_iterate_services(vpn_find_online_transport_cb,
1866                                                                 &service);
1867         return service;
1868 }
1869
1870 static bool vpn_is_valid_transport(struct connman_service *transport)
1871 {
1872         if (transport) {
1873                 struct connman_service *online;
1874
1875                 switch (connman_service_get_state(transport)) {
1876                 case CONNMAN_SERVICE_STATE_READY:
1877                         online = vpn_find_online_transport();
1878
1879                         /* Stay connected if there are no online services */
1880                         if (!online)
1881                                 return true;
1882
1883                         DBG("%s is ready, %s is online, disconnecting",
1884                                 connman_service_get_identifier(transport),
1885                                 connman_service_get_identifier(online));
1886                         break;
1887
1888                 case CONNMAN_SERVICE_STATE_ONLINE:
1889                         online = vpn_find_online_transport();
1890
1891                         /* Check if our transport is still the default */
1892                         if (online == transport)
1893                                 return true;
1894
1895                         DBG("%s is replaced by %s as default, disconnecting",
1896                                 connman_service_get_identifier(transport),
1897                                 connman_service_get_identifier(online));
1898                         break;
1899
1900                 default:
1901                         break;
1902                 }
1903         } else {
1904                 DBG("transport gone");
1905         }
1906
1907         return false;
1908 }
1909
1910 static void vpn_disconnect_check_provider(struct connection_data *data)
1911 {
1912         if (data->service_ident && provider_is_connected(data)) {
1913                 struct connman_service *service =
1914                         connman_service_lookup_from_identifier
1915                                                 (data->service_ident);
1916
1917                 if (!vpn_is_valid_transport(service)) {
1918                         disconnect_provider(data);
1919                 }
1920         }
1921 }
1922
1923 static void vpn_disconnect_check()
1924 {
1925         GHashTableIter iter;
1926         gpointer value;
1927
1928         DBG("");
1929         g_hash_table_iter_init(&iter, vpn_connections);
1930         while (g_hash_table_iter_next(&iter, NULL, &value))
1931                 vpn_disconnect_check_provider(value);
1932 }
1933
1934 static void vpn_service_add(struct connman_service *service, const char *name)
1935 {
1936         vpn_disconnect_check();
1937 }
1938
1939 static void vpn_service_list_changed(struct connman_service *service)
1940 {
1941         vpn_disconnect_check();
1942 }
1943
1944 static void vpn_service_state_changed(struct connman_service *service,
1945                                         enum connman_service_state state)
1946 {
1947         vpn_disconnect_check();
1948 }
1949
1950 static const struct connman_notifier vpn_notifier = {
1951         .name                   = "vpn",
1952         .priority               = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
1953         .default_changed        = vpn_service_list_changed,
1954         .service_add            = vpn_service_add,
1955         .service_remove         = vpn_service_list_changed,
1956         .service_state_changed  = vpn_service_state_changed
1957 };
1958
1959 static int vpn_init(void)
1960 {
1961         int err;
1962
1963         connection = connman_dbus_get_connection();
1964         if (!connection)
1965                 return -EIO;
1966
1967         watch = g_dbus_add_service_watch(connection, VPN_SERVICE,
1968                         vpnd_created, vpnd_removed, &provider_driver, NULL);
1969
1970         added_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1971                                         VPN_MANAGER_INTERFACE,
1972                                         CONNECTION_ADDED, connection_added,
1973                                         &provider_driver, NULL);
1974
1975         removed_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1976                                         VPN_MANAGER_INTERFACE,
1977                                         CONNECTION_REMOVED, connection_removed,
1978                                         NULL, NULL);
1979
1980         property_watch = g_dbus_add_signal_watch(connection, VPN_SERVICE, NULL,
1981                                         VPN_CONNECTION_INTERFACE,
1982                                         PROPERTY_CHANGED, property_changed,
1983                                         NULL, NULL);
1984
1985         if (added_watch == 0 || removed_watch == 0 || property_watch == 0) {
1986                 err = -EIO;
1987                 goto remove;
1988         }
1989
1990         err = connman_provider_driver_register(&provider_driver);
1991         if (err == 0) {
1992                 vpn_connections = g_hash_table_new_full(g_str_hash,
1993                                                 g_str_equal,
1994                                                 g_free, connection_destroy);
1995
1996                 vpnd_created(connection, &provider_driver);
1997         }
1998
1999         connman_notifier_register(&vpn_notifier);
2000         return err;
2001
2002 remove:
2003         g_dbus_remove_watch(connection, watch);
2004         g_dbus_remove_watch(connection, added_watch);
2005         g_dbus_remove_watch(connection, removed_watch);
2006         g_dbus_remove_watch(connection, property_watch);
2007
2008         dbus_connection_unref(connection);
2009
2010         return err;
2011 }
2012
2013 static void vpn_exit(void)
2014 {
2015         g_dbus_remove_watch(connection, watch);
2016         g_dbus_remove_watch(connection, added_watch);
2017         g_dbus_remove_watch(connection, removed_watch);
2018         g_dbus_remove_watch(connection, property_watch);
2019
2020         connman_notifier_unregister(&vpn_notifier);
2021         connman_provider_driver_unregister(&provider_driver);
2022
2023         if (vpn_connections)
2024                 g_hash_table_destroy(vpn_connections);
2025
2026         dbus_connection_unref(connection);
2027 }
2028
2029 CONNMAN_PLUGIN_DEFINE(vpn, "VPN plugin", VERSION,
2030                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpn_init, vpn_exit)