vpn-provider: Report disconnect errors other than -EINPROGRESS
[platform/upstream/connman.git] / vpn / vpn-provider.c
1 /*
2  *
3  *  ConnMan VPN daemon
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 <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gdbus.h>
31 #include <connman/log.h>
32 #include <gweb/gresolv.h>
33 #include <netdb.h>
34
35 #include "../src/connman.h"
36 #include "connman/agent.h"
37 #include "connman/vpn-dbus.h"
38 #include "vpn-provider.h"
39 #include "vpn.h"
40
41 enum {
42         USER_ROUTES_CHANGED = 0x01,
43         SERVER_ROUTES_CHANGED = 0x02,
44 };
45
46 static DBusConnection *connection;
47 static GHashTable *provider_hash;
48 static GSList *driver_list;
49 static int configuration_count;
50 static gboolean handle_routes;
51
52 struct vpn_route {
53         int family;
54         char *network;
55         char *netmask;
56         char *gateway;
57 };
58
59 struct vpn_provider {
60         int refcount;
61         int index;
62         int fd;
63         enum vpn_provider_state state;
64         char *path;
65         char *identifier;
66         char *name;
67         char *type;
68         char *host;
69         char *domain;
70         int family;
71         GHashTable *routes;
72         struct vpn_provider_driver *driver;
73         void *driver_data;
74         GHashTable *setting_strings;
75         GHashTable *user_routes;
76         GSList *user_networks;
77         GResolv *resolv;
78         char **host_ip;
79         struct vpn_ipconfig *ipconfig_ipv4;
80         struct vpn_ipconfig *ipconfig_ipv6;
81         char **nameservers;
82         int what_changed;
83         guint notify_id;
84 };
85
86 static void free_route(gpointer data)
87 {
88         struct vpn_route *route = data;
89
90         g_free(route->network);
91         g_free(route->netmask);
92         g_free(route->gateway);
93
94         g_free(route);
95 }
96
97 static void append_route(DBusMessageIter *iter, void *user_data)
98 {
99         struct vpn_route *route = user_data;
100         DBusMessageIter item;
101         int family = 0;
102
103         connman_dbus_dict_open(iter, &item);
104
105         if (route == NULL)
106                 goto empty_dict;
107
108         if (route->family == AF_INET)
109                 family = 4;
110         else if (route->family == AF_INET6)
111                 family = 6;
112
113         if (family != 0)
114                 connman_dbus_dict_append_basic(&item, "ProtocolFamily",
115                                         DBUS_TYPE_INT32, &family);
116
117         if (route->network != NULL)
118                 connman_dbus_dict_append_basic(&item, "Network",
119                                         DBUS_TYPE_STRING, &route->network);
120
121         if (route->netmask != NULL)
122                 connman_dbus_dict_append_basic(&item, "Netmask",
123                                         DBUS_TYPE_STRING, &route->netmask);
124
125         if (route->gateway != NULL)
126                 connman_dbus_dict_append_basic(&item, "Gateway",
127                                         DBUS_TYPE_STRING, &route->gateway);
128
129 empty_dict:
130         connman_dbus_dict_close(iter, &item);
131 }
132
133 static void append_routes(DBusMessageIter *iter, void *user_data)
134 {
135         GHashTable *routes = user_data;
136         GHashTableIter hash;
137         gpointer value, key;
138
139         if (routes == NULL) {
140                 append_route(iter, NULL);
141                 return;
142         }
143
144         g_hash_table_iter_init(&hash, routes);
145
146         while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
147                 DBusMessageIter dict;
148
149                 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL,
150                                                 &dict);
151                 append_route(&dict, value);
152                 dbus_message_iter_close_container(iter, &dict);
153         }
154 }
155
156 static void send_routes(struct vpn_provider *provider, GHashTable *routes,
157                         const char *name)
158 {
159         connman_dbus_property_changed_array(provider->path,
160                                         VPN_CONNECTION_INTERFACE,
161                                         name,
162                                         DBUS_TYPE_DICT_ENTRY,
163                                         append_routes,
164                                         routes);
165 }
166
167 static int provider_property_changed(struct vpn_provider *provider,
168                                 const char *name)
169 {
170         DBG("provider %p name %s", provider, name);
171
172         if (g_str_equal(name, "UserRoutes") == TRUE)
173                 send_routes(provider, provider->user_routes, name);
174         else if (g_str_equal(name, "ServerRoutes") == TRUE)
175                 send_routes(provider, provider->routes, name);
176
177         return 0;
178 }
179
180 static GSList *read_route_dict(GSList *routes, DBusMessageIter *dicts)
181 {
182         DBusMessageIter dict, value, entry;
183         const char *network, *netmask, *gateway;
184         struct vpn_route *route;
185         int family, type;
186         const char *key;
187
188         dbus_message_iter_recurse(dicts, &entry);
189
190         network = netmask = gateway = NULL;
191         family = PF_UNSPEC;
192
193         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_DICT_ENTRY) {
194
195                 dbus_message_iter_recurse(&entry, &dict);
196                 dbus_message_iter_get_basic(&dict, &key);
197
198                 dbus_message_iter_next(&dict);
199                 dbus_message_iter_recurse(&dict, &value);
200
201                 type = dbus_message_iter_get_arg_type(&value);
202
203                 switch (type) {
204                 case DBUS_TYPE_STRING:
205                         if (g_str_equal(key, "ProtocolFamily") == TRUE)
206                                 dbus_message_iter_get_basic(&value, &family);
207                         else if (g_str_equal(key, "Network") == TRUE)
208                                 dbus_message_iter_get_basic(&value, &network);
209                         else if (g_str_equal(key, "Netmask") == TRUE)
210                                 dbus_message_iter_get_basic(&value, &netmask);
211                         else if (g_str_equal(key, "Gateway") == TRUE)
212                                 dbus_message_iter_get_basic(&value, &gateway);
213                         break;
214                 }
215
216                 dbus_message_iter_next(&entry);
217         }
218
219         DBG("family %d network %s netmask %s gateway %s", family,
220                 network, netmask, gateway);
221
222         if (network == NULL || netmask == NULL) {
223                 DBG("Ignoring route as network/netmask is missing");
224                 return routes;
225         }
226
227         route = g_try_new(struct vpn_route, 1);
228         if (route == NULL) {
229                 g_slist_free_full(routes, free_route);
230                 return NULL;
231         }
232
233         if (family == PF_UNSPEC) {
234                 family = connman_inet_check_ipaddress(network);
235                 if (family < 0) {
236                         DBG("Cannot get address family of %s (%d/%s)", network,
237                                 family, gai_strerror(family));
238                         if (strstr(network, ":") != NULL) {
239                                 DBG("Guessing it is IPv6");
240                                 family = AF_INET6;
241                         } else {
242                                 DBG("Guessing it is IPv4");
243                                 family = AF_INET;
244                         }
245                 }
246         } else {
247                 switch (family) {
248                 case '4':
249                         family = AF_INET;
250                         break;
251                 case '6':
252                         family = AF_INET6;
253                         break;
254                 default:
255                         family = PF_UNSPEC;
256                         break;
257                 }
258         }
259
260         route->family = family;
261         route->network = g_strdup(network);
262         route->netmask = g_strdup(netmask);
263         route->gateway = g_strdup(gateway);
264
265         routes = g_slist_prepend(routes, route);
266         return routes;
267 }
268
269 static GSList *get_user_networks(DBusMessageIter *array)
270 {
271         DBusMessageIter entry;
272         GSList *list = NULL;
273
274         while (dbus_message_iter_get_arg_type(array) == DBUS_TYPE_ARRAY) {
275
276                 dbus_message_iter_recurse(array, &entry);
277
278                 while (dbus_message_iter_get_arg_type(&entry) ==
279                                                         DBUS_TYPE_STRUCT) {
280                         DBusMessageIter dicts;
281
282                         dbus_message_iter_recurse(&entry, &dicts);
283
284                         while (dbus_message_iter_get_arg_type(&dicts) ==
285                                                         DBUS_TYPE_ARRAY) {
286
287                                 list = read_route_dict(list, &dicts);
288                                 dbus_message_iter_next(&dicts);
289                         }
290
291                         dbus_message_iter_next(&entry);
292                 }
293
294                 dbus_message_iter_next(array);
295         }
296
297         return list;
298 }
299
300 static void set_user_networks(struct vpn_provider *provider, GSList *networks)
301 {
302         GSList *list;
303
304         for (list = networks; list != NULL; list = g_slist_next(list)) {
305                 struct vpn_route *route= list->data;
306
307                 if (__vpn_provider_append_user_route(provider,
308                                         route->family, route->network,
309                                         route->netmask) != 0)
310                         break;
311         }
312 }
313
314 static void del_routes(struct vpn_provider *provider)
315 {
316         GHashTableIter hash;
317         gpointer value, key;
318
319         g_hash_table_iter_init(&hash, provider->user_routes);
320         while (handle_routes == TRUE && g_hash_table_iter_next(&hash,
321                                                 &key, &value) == TRUE) {
322                 struct vpn_route *route = value;
323                 if (route->family == AF_INET6) {
324                         unsigned char prefixlen = atoi(route->netmask);
325                         connman_inet_del_ipv6_network_route(provider->index,
326                                                         route->network,
327                                                         prefixlen);
328                 } else
329                         connman_inet_del_host_route(provider->index,
330                                                 route->network);
331         }
332
333         g_hash_table_remove_all(provider->user_routes);
334         g_slist_free_full(provider->user_networks, free_route);
335         provider->user_networks = NULL;
336 }
337
338 static gboolean provider_send_changed(gpointer data)
339 {
340         struct vpn_provider *provider = data;
341
342         if (provider->what_changed & USER_ROUTES_CHANGED)
343                 provider_property_changed(provider, "UserRoutes");
344
345         if (provider->what_changed & SERVER_ROUTES_CHANGED)
346                 provider_property_changed(provider, "ServerRoutes");
347
348         provider->what_changed = 0;
349         provider->notify_id = 0;
350
351         return FALSE;
352 }
353
354 static void provider_schedule_changed(struct vpn_provider *provider, int flag)
355 {
356         if (provider->notify_id != 0)
357                 g_source_remove(provider->notify_id);
358
359         provider->what_changed |= flag;
360
361         provider->notify_id = g_timeout_add(100, provider_send_changed,
362                                                                 provider);
363 }
364
365 static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
366                                                                 void *data)
367 {
368         struct vpn_provider *provider = data;
369         DBusMessageIter iter, value;
370         const char *name;
371         int type;
372
373         DBG("conn %p", conn);
374
375         if (dbus_message_iter_init(msg, &iter) == FALSE)
376                 return __connman_error_invalid_arguments(msg);
377
378         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
379                 return __connman_error_invalid_arguments(msg);
380
381         dbus_message_iter_get_basic(&iter, &name);
382         dbus_message_iter_next(&iter);
383
384         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
385                 return __connman_error_invalid_arguments(msg);
386
387         dbus_message_iter_recurse(&iter, &value);
388
389         type = dbus_message_iter_get_arg_type(&value);
390
391         if (g_str_equal(name, "UserRoutes") == TRUE) {
392                 GSList *networks;
393
394                 if (type != DBUS_TYPE_ARRAY)
395                         return __connman_error_invalid_arguments(msg);
396
397                 networks = get_user_networks(&value);
398                 if (networks != NULL) {
399                         del_routes(provider);
400                         provider->user_networks = networks;
401                         set_user_networks(provider, provider->user_networks);
402
403                         if (handle_routes == FALSE)
404                                 provider_schedule_changed(provider,
405                                                         USER_ROUTES_CHANGED);
406                 }
407         } else
408                 return __connman_error_invalid_property(msg);
409
410         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
411 }
412
413 static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
414                                                                 void *data)
415 {
416         struct vpn_provider *provider = data;
417         const char *name;
418
419         DBG("conn %p", conn);
420
421         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
422                                                         DBUS_TYPE_INVALID);
423
424         if (g_str_equal(name, "UserRoutes") == TRUE) {
425                 del_routes(provider);
426
427                 if (handle_routes == FALSE)
428                         provider_property_changed(provider, name);
429         } else {
430                 return __connman_error_invalid_property(msg);
431         }
432
433         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
434 }
435
436 static DBusMessage *do_connect(DBusConnection *conn, DBusMessage *msg,
437                                                                 void *data)
438 {
439         struct vpn_provider *provider = data;
440         int err;
441
442         DBG("conn %p provider %p", conn, provider);
443
444         err = __vpn_provider_connect(provider, msg);
445         if (err < 0)
446                 return __connman_error_failed(msg, -err);
447
448         return NULL;
449 }
450
451 static DBusMessage *do_disconnect(DBusConnection *conn, DBusMessage *msg,
452                                                                 void *data)
453 {
454         struct vpn_provider *provider = data;
455         int err;
456
457         DBG("conn %p provider %p", conn, provider);
458
459         err = __vpn_provider_disconnect(provider);
460         if (err < 0 && err != -EINPROGRESS)
461                 return __connman_error_failed(msg, -err);
462
463         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
464 }
465
466 static const GDBusMethodTable connection_methods[] = {
467         { GDBUS_METHOD("SetProperty",
468                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
469                         NULL, set_property) },
470         { GDBUS_METHOD("ClearProperty",
471                         GDBUS_ARGS({ "name", "s" }), NULL,
472                         clear_property) },
473         { GDBUS_ASYNC_METHOD("Connect", NULL, NULL, do_connect) },
474         { GDBUS_METHOD("Disconnect", NULL, NULL, do_disconnect) },
475         { },
476 };
477
478 static const GDBusSignalTable connection_signals[] = {
479         { GDBUS_SIGNAL("PropertyChanged",
480                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
481         { },
482 };
483
484 static void resolv_result(GResolvResultStatus status,
485                                         char **results, gpointer user_data)
486 {
487         struct vpn_provider *provider = user_data;
488
489         DBG("status %d", status);
490
491         if (status == G_RESOLV_RESULT_STATUS_SUCCESS && results != NULL &&
492                                                 g_strv_length(results) > 0)
493                 provider->host_ip = g_strdupv(results);
494
495         vpn_provider_unref(provider);
496 }
497
498 static void provider_resolv_host_addr(struct vpn_provider *provider)
499 {
500         if (provider->host == NULL)
501                 return;
502
503         if (connman_inet_check_ipaddress(provider->host) > 0)
504                 return;
505
506         if (provider->host_ip != NULL)
507                 return;
508
509         /*
510          * If the hostname is not numeric, try to resolv it. We do not wait
511          * the result as it might take some time. We will get the result
512          * before VPN will feed routes to us because VPN client will need
513          * the IP address also before VPN connection can be established.
514          */
515         provider->resolv = g_resolv_new(0);
516         if (provider->resolv == NULL) {
517                 DBG("Cannot resolv %s", provider->host);
518                 return;
519         }
520
521         DBG("Trying to resolv %s", provider->host);
522
523         vpn_provider_ref(provider);
524
525         g_resolv_lookup_hostname(provider->resolv, provider->host,
526                                 resolv_result, provider);
527 }
528
529 void __vpn_provider_append_properties(struct vpn_provider *provider,
530                                                         DBusMessageIter *iter)
531 {
532         if (provider->host != NULL)
533                 connman_dbus_dict_append_basic(iter, "Host",
534                                         DBUS_TYPE_STRING, &provider->host);
535
536         if (provider->domain != NULL)
537                 connman_dbus_dict_append_basic(iter, "Domain",
538                                         DBUS_TYPE_STRING, &provider->domain);
539
540         if (provider->type != NULL)
541                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
542                                                  &provider->type);
543 }
544
545 int __vpn_provider_append_user_route(struct vpn_provider *provider,
546                         int family, const char *network, const char *netmask)
547 {
548         struct vpn_route *route;
549         char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
550
551         DBG("family %d network %s netmask %s", family, network, netmask);
552
553         route = g_hash_table_lookup(provider->user_routes, key);
554         if (route == NULL) {
555                 route = g_try_new0(struct vpn_route, 1);
556                 if (route == NULL) {
557                         connman_error("out of memory");
558                         return -ENOMEM;
559                 }
560
561                 route->family = family;
562                 route->network = g_strdup(network);
563                 route->netmask = g_strdup(netmask);
564
565                 g_hash_table_replace(provider->user_routes, key, route);
566         } else
567                 g_free(key);
568
569         return 0;
570 }
571
572 static struct vpn_route *get_route(char *route_str)
573 {
574         char **elems = g_strsplit(route_str, "/", 0);
575         char *network, *netmask, *gateway, *family_str;
576         int family = PF_UNSPEC;
577         struct vpn_route *route = NULL;
578
579         if (elems == NULL)
580                 return NULL;
581
582         family_str = elems[0];
583
584         network = elems[1];
585         if (network == NULL || network[0] == '\0')
586                 goto out;
587
588         netmask = elems[2];
589         if (netmask == NULL || netmask[0] == '\0')
590                 goto out;
591
592         gateway = elems[3];
593
594         route = g_try_new0(struct vpn_route, 1);
595         if (route == NULL)
596                 goto out;
597
598         if (family_str[0] == '\0' || atoi(family_str) == 0) {
599                 family = PF_UNSPEC;
600         } else {
601                 switch (family_str[0]) {
602                 case '4':
603                         family = AF_INET;
604                         break;
605                 case '6':
606                         family = AF_INET6;
607                         break;
608                 }
609         }
610
611         if (g_strrstr(network, ":") != NULL) {
612                 if (family != PF_UNSPEC && family != AF_INET6)
613                         DBG("You have IPv6 address but you have non IPv6 route");
614         } else if (g_strrstr(network, ".") != NULL) {
615                 if (family != PF_UNSPEC && family != AF_INET)
616                         DBG("You have IPv4 address but you have non IPv4 route");
617
618                 if (g_strrstr(netmask, ".") == NULL) {
619                         /* We have netmask length */
620                         in_addr_t addr;
621                         struct in_addr netmask_in;
622                         unsigned char prefix_len = 32;
623
624                         if (netmask != NULL) {
625                                 char *ptr;
626                                 long int value = strtol(netmask, &ptr, 10);
627                                 if (ptr != netmask && *ptr == '\0' &&
628                                                                 value <= 32)
629                                         prefix_len = value;
630                         }
631
632                         addr = 0xffffffff << (32 - prefix_len);
633                         netmask_in.s_addr = htonl(addr);
634                         netmask = inet_ntoa(netmask_in);
635
636                         DBG("network %s netmask %s", network, netmask);
637                 }
638         }
639
640         if (family == PF_UNSPEC) {
641                 family = connman_inet_check_ipaddress(network);
642                 if (family < 0 || family == PF_UNSPEC)
643                         goto out;
644         }
645
646         route->family = family;
647         route->network = g_strdup(network);
648         route->netmask = g_strdup(netmask);
649         route->gateway = g_strdup(gateway);
650
651 out:
652         g_strfreev(elems);
653         return route;
654 }
655
656 static GSList *get_routes(gchar **networks)
657 {
658         struct vpn_route *route;
659         GSList *routes = NULL;
660         int i;
661
662         for (i = 0; networks[i] != NULL; i++) {
663                 route = get_route(networks[i]);
664                 if (route != NULL)
665                         routes = g_slist_prepend(routes, route);
666         }
667
668         return routes;
669 }
670
671 static int provider_load_from_keyfile(struct vpn_provider *provider,
672                 GKeyFile *keyfile)
673 {
674         gsize idx = 0;
675         gchar **settings;
676         gchar *key, *value;
677         gsize length, num_user_networks;
678         gchar **networks = NULL;
679
680         settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
681                                 NULL);
682         if (settings == NULL) {
683                 g_key_file_free(keyfile);
684                 return -ENOENT;
685         }
686
687         while (idx < length) {
688                 key = settings[idx];
689                 if (key != NULL) {
690                         if (g_str_equal(key, "Networks") == TRUE) {
691                                 networks = g_key_file_get_string_list(keyfile,
692                                                 provider->identifier,
693                                                 key,
694                                                 &num_user_networks,
695                                                 NULL);
696                                 provider->user_networks = get_routes(networks);
697
698                         } else {
699                                 value = g_key_file_get_string(keyfile,
700                                                         provider->identifier,
701                                                         key, NULL);
702                                 vpn_provider_set_string(provider, key,
703                                                         value);
704                                 g_free(value);
705                         }
706                 }
707                 idx += 1;
708         }
709         g_strfreev(settings);
710         g_strfreev(networks);
711
712         if (provider->user_networks != NULL)
713                 set_user_networks(provider, provider->user_networks);
714
715         return 0;
716 }
717
718
719 static int vpn_provider_load(struct vpn_provider *provider)
720 {
721         GKeyFile *keyfile;
722
723         DBG("provider %p", provider);
724
725         keyfile = __connman_storage_load_provider(provider->identifier);
726         if (keyfile == NULL)
727                 return -ENOENT;
728
729         provider_load_from_keyfile(provider, keyfile);
730
731         g_key_file_free(keyfile);
732         return 0;
733 }
734
735 static gchar **create_network_list(GSList *networks, gsize *count)
736 {
737         GSList *list;
738         gchar **result = NULL;
739         unsigned int num_elems = 0;
740
741         for (list = networks; list != NULL; list = g_slist_next(list)) {
742                 struct vpn_route *route = list->data;
743                 int family;
744
745                 result = g_try_realloc(result,
746                                 (num_elems + 1) * sizeof(gchar *));
747                 if (result == NULL)
748                         return NULL;
749
750                 switch (route->family) {
751                 case AF_INET:
752                         family = 4;
753                         break;
754                 case AF_INET6:
755                         family = 6;
756                         break;
757                 default:
758                         family = 0;
759                         break;
760                 }
761
762                 result[num_elems] = g_strdup_printf("%d/%s/%s/%s",
763                                 family, route->network, route->netmask,
764                                 route->gateway == NULL ? "" : route->gateway);
765
766                 num_elems++;
767         }
768
769         result = g_try_realloc(result, (num_elems + 1) * sizeof(gchar *));
770         if (result == NULL)
771                 return NULL;
772
773         result[num_elems] = NULL;
774         *count = num_elems;
775         return result;
776 }
777
778 static int vpn_provider_save(struct vpn_provider *provider)
779 {
780         GKeyFile *keyfile;
781
782         DBG("provider %p", provider);
783
784         keyfile = g_key_file_new();
785         if (keyfile == NULL)
786                 return -ENOMEM;
787
788         g_key_file_set_string(keyfile, provider->identifier,
789                         "Name", provider->name);
790         g_key_file_set_string(keyfile, provider->identifier,
791                         "Type", provider->type);
792         g_key_file_set_string(keyfile, provider->identifier,
793                         "Host", provider->host);
794         g_key_file_set_string(keyfile, provider->identifier,
795                         "VPN.Domain", provider->domain);
796         if (provider->user_networks != NULL) {
797                 gchar **networks;
798                 gsize network_count;
799
800                 networks = create_network_list(provider->user_networks,
801                                                         &network_count);
802                 if (networks != NULL) {
803                         g_key_file_set_string_list(keyfile,
804                                                 provider->identifier,
805                                                 "Networks",
806                                                 (const gchar ** const)networks,
807                                                 network_count);
808                         g_strfreev(networks);
809                 }
810         }
811
812         if (provider->driver != NULL && provider->driver->save != NULL)
813                 provider->driver->save(provider, keyfile);
814
815         __connman_storage_save_provider(keyfile, provider->identifier);
816         g_key_file_free(keyfile);
817
818         return 0;
819 }
820
821 static struct vpn_provider *vpn_provider_lookup(const char *identifier)
822 {
823         struct vpn_provider *provider = NULL;
824
825         provider = g_hash_table_lookup(provider_hash, identifier);
826
827         return provider;
828 }
829
830 static gboolean match_driver(struct vpn_provider *provider,
831                                 struct vpn_provider_driver *driver)
832 {
833         if (g_strcmp0(driver->name, provider->type) == 0)
834                 return TRUE;
835
836         return FALSE;
837 }
838
839 static int provider_probe(struct vpn_provider *provider)
840 {
841         GSList *list;
842
843         DBG("provider %p name %s", provider, provider->name);
844
845         if (provider->driver != NULL)
846                 return -EALREADY;
847
848         for (list = driver_list; list; list = list->next) {
849                 struct vpn_provider_driver *driver = list->data;
850
851                 if (match_driver(provider, driver) == FALSE)
852                         continue;
853
854                 DBG("driver %p name %s", driver, driver->name);
855
856                 if (driver->probe != NULL && driver->probe(provider) == 0) {
857                         provider->driver = driver;
858                         break;
859                 }
860         }
861
862         if (provider->driver == NULL)
863                 return -ENODEV;
864
865         return 0;
866 }
867
868 static void provider_remove(struct vpn_provider *provider)
869 {
870         if (provider->driver != NULL) {
871                 provider->driver->remove(provider);
872                 provider->driver = NULL;
873         }
874 }
875
876 static int provider_register(struct vpn_provider *provider)
877 {
878         return provider_probe(provider);
879 }
880
881 static void provider_unregister(struct vpn_provider *provider)
882 {
883         provider_remove(provider);
884 }
885
886 struct vpn_provider *
887 vpn_provider_ref_debug(struct vpn_provider *provider,
888                         const char *file, int line, const char *caller)
889 {
890         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
891                 file, line, caller);
892
893         __sync_fetch_and_add(&provider->refcount, 1);
894
895         return provider;
896 }
897
898 static void provider_destruct(struct vpn_provider *provider)
899 {
900         DBG("provider %p", provider);
901
902         if (provider->notify_id != 0)
903                 g_source_remove(provider->notify_id);
904
905         g_free(provider->name);
906         g_free(provider->type);
907         g_free(provider->host);
908         g_free(provider->domain);
909         g_free(provider->identifier);
910         g_free(provider->path);
911         g_slist_free_full(provider->user_networks, free_route);
912         g_strfreev(provider->nameservers);
913         g_hash_table_destroy(provider->routes);
914         g_hash_table_destroy(provider->user_routes);
915         g_hash_table_destroy(provider->setting_strings);
916         if (provider->resolv != NULL) {
917                 g_resolv_unref(provider->resolv);
918                 provider->resolv = NULL;
919         }
920         __vpn_ipconfig_unref(provider->ipconfig_ipv4);
921         __vpn_ipconfig_unref(provider->ipconfig_ipv6);
922
923         g_strfreev(provider->host_ip);
924         g_free(provider);
925 }
926
927 void vpn_provider_unref_debug(struct vpn_provider *provider,
928                                 const char *file, int line, const char *caller)
929 {
930         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
931                 file, line, caller);
932
933         if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
934                 return;
935
936         provider_remove(provider);
937
938         provider_destruct(provider);
939 }
940
941 static void configuration_count_add(void)
942 {
943         DBG("count %d", configuration_count + 1);
944
945         __sync_fetch_and_add(&configuration_count, 1);
946 }
947
948 static void configuration_count_del(void)
949 {
950         DBG("count %d", configuration_count - 1);
951
952         if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
953                 return;
954
955         raise(SIGTERM);
956 }
957
958 int __vpn_provider_disconnect(struct vpn_provider *provider)
959 {
960         int err;
961
962         DBG("provider %p", provider);
963
964         if (provider->driver != NULL && provider->driver->disconnect != NULL)
965                 err = provider->driver->disconnect(provider);
966         else
967                 return -EOPNOTSUPP;
968
969         if (err == -EINPROGRESS)
970                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
971
972         return err;
973 }
974
975 static void connect_cb(struct vpn_provider *provider, void *user_data,
976                                                                 int error)
977 {
978         DBusMessage *pending = user_data;
979
980         DBG("provider %p user %p error %d", provider, user_data, error);
981
982         if (error != 0) {
983                 DBusMessage *reply = __connman_error_failed(pending, error);
984                 if (reply != NULL)
985                         g_dbus_send_message(connection, reply);
986
987                 vpn_provider_indicate_error(provider,
988                                         VPN_PROVIDER_ERROR_CONNECT_FAILED);
989                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
990         } else
991                 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
992
993         dbus_message_unref(pending);
994 }
995
996 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
997 {
998         int err;
999
1000         DBG("provider %p", provider);
1001
1002         if (provider->driver != NULL && provider->driver->connect != NULL) {
1003                 dbus_message_ref(msg);
1004                 err = provider->driver->connect(provider, connect_cb, msg);
1005         } else
1006                 return -EOPNOTSUPP;
1007
1008         if (err == -EINPROGRESS)
1009                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_CONNECT);
1010
1011         return err;
1012 }
1013
1014 static void connection_removed_signal(struct vpn_provider *provider)
1015 {
1016         DBusMessage *signal;
1017         DBusMessageIter iter;
1018
1019         signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1020                         VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1021         if (signal == NULL)
1022                 return;
1023
1024         dbus_message_iter_init_append(signal, &iter);
1025         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1026                                                         &provider->path);
1027         dbus_connection_send(connection, signal, NULL);
1028         dbus_message_unref(signal);
1029 }
1030
1031 static char *get_ident(const char *path)
1032 {
1033         char *pos;
1034
1035         if (*path != '/')
1036                 return NULL;
1037
1038         pos = strrchr(path, '/');
1039         if (pos == NULL)
1040                 return NULL;
1041
1042         return pos + 1;
1043 }
1044
1045 int __vpn_provider_remove(const char *path)
1046 {
1047         struct vpn_provider *provider;
1048         char *ident;
1049
1050         DBG("path %s", path);
1051
1052         ident = get_ident(path);
1053
1054         provider = vpn_provider_lookup(ident);
1055         if (provider != NULL) {
1056                 DBG("Removing VPN %s", provider->identifier);
1057
1058                 connection_removed_signal(provider);
1059
1060                 provider_unregister(provider);
1061                 g_hash_table_remove(provider_hash, provider->identifier);
1062
1063                 __connman_storage_remove_provider(ident);
1064                 return 0;
1065         }
1066
1067         return -ENXIO;
1068 }
1069
1070 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1071 {
1072         struct vpn_provider *provider = user_data;
1073         const char *address, *gateway, *peer;
1074
1075         address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1076         if (address != NULL) {
1077                 in_addr_t addr;
1078                 struct in_addr netmask;
1079                 char *mask;
1080                 int prefixlen;
1081
1082                 prefixlen = __vpn_ipconfig_get_prefixlen(
1083                                                 provider->ipconfig_ipv4);
1084
1085                 addr = 0xffffffff << (32 - prefixlen);
1086                 netmask.s_addr = htonl(addr);
1087                 mask = inet_ntoa(netmask);
1088
1089                 connman_dbus_dict_append_basic(iter, "Address",
1090                                                 DBUS_TYPE_STRING, &address);
1091
1092                 connman_dbus_dict_append_basic(iter, "Netmask",
1093                                                 DBUS_TYPE_STRING, &mask);
1094         }
1095
1096         gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1097         if (gateway != NULL)
1098                 connman_dbus_dict_append_basic(iter, "Gateway",
1099                                                 DBUS_TYPE_STRING, &gateway);
1100
1101         peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1102         if (peer != NULL)
1103                 connman_dbus_dict_append_basic(iter, "Peer",
1104                                                 DBUS_TYPE_STRING, &peer);
1105 }
1106
1107 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1108 {
1109         struct vpn_provider *provider = user_data;
1110         const char *address, *gateway, *peer;
1111
1112         address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1113         if (address != NULL) {
1114                 unsigned char prefixlen;
1115
1116                 connman_dbus_dict_append_basic(iter, "Address",
1117                                                 DBUS_TYPE_STRING, &address);
1118
1119                 prefixlen = __vpn_ipconfig_get_prefixlen(
1120                                                 provider->ipconfig_ipv6);
1121
1122                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1123                                                 DBUS_TYPE_BYTE, &prefixlen);
1124         }
1125
1126         gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1127         if (gateway != NULL)
1128                 connman_dbus_dict_append_basic(iter, "Gateway",
1129                                                 DBUS_TYPE_STRING, &gateway);
1130
1131         peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1132         if (peer != NULL)
1133                 connman_dbus_dict_append_basic(iter, "Peer",
1134                                                 DBUS_TYPE_STRING, &peer);
1135 }
1136
1137 static const char *state2string(enum vpn_provider_state state)
1138 {
1139         switch (state) {
1140         case VPN_PROVIDER_STATE_UNKNOWN:
1141                 break;
1142         case VPN_PROVIDER_STATE_IDLE:
1143                 return "idle";
1144         case VPN_PROVIDER_STATE_CONNECT:
1145                 return "configuration";
1146         case VPN_PROVIDER_STATE_READY:
1147                 return "ready";
1148         case VPN_PROVIDER_STATE_DISCONNECT:
1149                 return "disconnect";
1150         case VPN_PROVIDER_STATE_FAILURE:
1151                 return "failure";
1152         }
1153
1154         return NULL;
1155 }
1156
1157 static int provider_indicate_state(struct vpn_provider *provider,
1158                                 enum vpn_provider_state state)
1159 {
1160         const char *str;
1161
1162         DBG("provider %p state %d", provider, state);
1163
1164         str = state2string(state);
1165         if (str == NULL)
1166                 return -EINVAL;
1167
1168         provider->state = state;
1169
1170         if (state == VPN_PROVIDER_STATE_READY) {
1171                 connman_dbus_property_changed_basic(provider->path,
1172                                         VPN_CONNECTION_INTERFACE, "Index",
1173                                         DBUS_TYPE_INT32, &provider->index);
1174
1175                 if (provider->family == AF_INET)
1176                         connman_dbus_property_changed_dict(provider->path,
1177                                         VPN_CONNECTION_INTERFACE, "IPv4",
1178                                         append_ipv4, provider);
1179                 else if (provider->family == AF_INET6)
1180                         connman_dbus_property_changed_dict(provider->path,
1181                                         VPN_CONNECTION_INTERFACE, "IPv6",
1182                                         append_ipv6, provider);
1183         }
1184
1185         connman_dbus_property_changed_basic(provider->path,
1186                                         VPN_CONNECTION_INTERFACE, "State",
1187                                         DBUS_TYPE_STRING, &str);
1188         return 0;
1189 }
1190
1191 static void append_nameservers(DBusMessageIter *iter, char **servers)
1192 {
1193         int i;
1194
1195         DBG("%p", servers);
1196
1197         for (i = 0; servers[i] != NULL; i++) {
1198                 DBG("servers[%d] %s", i, servers[i]);
1199                 dbus_message_iter_append_basic(iter,
1200                                         DBUS_TYPE_STRING, &servers[i]);
1201         }
1202 }
1203
1204 static void append_dns(DBusMessageIter *iter, void *user_data)
1205 {
1206         struct vpn_provider *provider = user_data;
1207
1208         if (provider->nameservers != NULL)
1209                 append_nameservers(iter, provider->nameservers);
1210 }
1211
1212 static void append_state(DBusMessageIter *iter,
1213                                         struct vpn_provider *provider)
1214 {
1215         char *str;
1216
1217         switch (provider->state) {
1218         case VPN_PROVIDER_STATE_UNKNOWN:
1219         case VPN_PROVIDER_STATE_IDLE:
1220                 str = "idle";
1221                 break;
1222         case VPN_PROVIDER_STATE_CONNECT:
1223                 str = "configuration";
1224                 break;
1225         case VPN_PROVIDER_STATE_READY:
1226                 str = "ready";
1227                 break;
1228         case VPN_PROVIDER_STATE_DISCONNECT:
1229                 str = "disconnect";
1230                 break;
1231         case VPN_PROVIDER_STATE_FAILURE:
1232                 str = "failure";
1233                 break;
1234         }
1235
1236         connman_dbus_dict_append_basic(iter, "State",
1237                                 DBUS_TYPE_STRING, &str);
1238 }
1239
1240 static void append_properties(DBusMessageIter *iter,
1241                                         struct vpn_provider *provider)
1242 {
1243         DBusMessageIter dict;
1244
1245         connman_dbus_dict_open(iter, &dict);
1246
1247         append_state(&dict, provider);
1248
1249         if (provider->type != NULL)
1250                 connman_dbus_dict_append_basic(&dict, "Type",
1251                                         DBUS_TYPE_STRING, &provider->type);
1252
1253         if (provider->name != NULL)
1254                 connman_dbus_dict_append_basic(&dict, "Name",
1255                                         DBUS_TYPE_STRING, &provider->name);
1256
1257         if (provider->host != NULL)
1258                 connman_dbus_dict_append_basic(&dict, "Host",
1259                                         DBUS_TYPE_STRING, &provider->host);
1260         if (provider->index >= 0)
1261                 connman_dbus_dict_append_basic(&dict, "Index",
1262                                         DBUS_TYPE_INT32, &provider->index);
1263         if (provider->domain != NULL)
1264                 connman_dbus_dict_append_basic(&dict, "Domain",
1265                                         DBUS_TYPE_STRING, &provider->domain);
1266
1267         if (provider->family == AF_INET)
1268                 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1269                                                 provider);
1270         else if (provider->family == AF_INET6)
1271                 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1272                                                 provider);
1273
1274         connman_dbus_dict_append_array(&dict, "Nameservers",
1275                                 DBUS_TYPE_STRING, append_dns, provider);
1276
1277         connman_dbus_dict_append_array(&dict, "UserRoutes",
1278                                 DBUS_TYPE_DICT_ENTRY, append_routes,
1279                                 provider->user_routes);
1280
1281         connman_dbus_dict_append_array(&dict, "ServerRoutes",
1282                                 DBUS_TYPE_DICT_ENTRY, append_routes,
1283                                 provider->routes);
1284
1285         connman_dbus_dict_close(iter, &dict);
1286 }
1287
1288 static void connection_added_signal(struct vpn_provider *provider)
1289 {
1290         DBusMessage *signal;
1291         DBusMessageIter iter;
1292
1293         signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1294                         VPN_MANAGER_INTERFACE, "ConnectionAdded");
1295         if (signal == NULL)
1296                 return;
1297
1298         dbus_message_iter_init_append(signal, &iter);
1299         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1300                                                         &provider->path);
1301         append_properties(&iter, provider);
1302
1303         dbus_connection_send(connection, signal, NULL);
1304         dbus_message_unref(signal);
1305 }
1306
1307 static connman_bool_t check_host(char **hosts, char *host)
1308 {
1309         int i;
1310
1311         if (hosts == NULL)
1312                 return FALSE;
1313
1314         for (i = 0; hosts[i] != NULL; i++) {
1315                 if (g_strcmp0(hosts[i], host) == 0)
1316                         return TRUE;
1317         }
1318
1319         return FALSE;
1320 }
1321
1322 static void provider_append_routes(gpointer key, gpointer value,
1323                                         gpointer user_data)
1324 {
1325         struct vpn_route *route = value;
1326         struct vpn_provider *provider = user_data;
1327         int index = provider->index;
1328
1329         if (handle_routes == FALSE)
1330                 return;
1331
1332         /*
1333          * If the VPN administrator/user has given a route to
1334          * VPN server, then we must discard that because the
1335          * server cannot be contacted via VPN tunnel.
1336          */
1337         if (check_host(provider->host_ip, route->network) == TRUE) {
1338                 DBG("Discarding VPN route to %s via %s at index %d",
1339                         route->network, route->gateway, index);
1340                 return;
1341         }
1342
1343         if (route->family == AF_INET6) {
1344                 unsigned char prefix_len = atoi(route->netmask);
1345
1346                 connman_inet_add_ipv6_network_route(index, route->network,
1347                                                         route->gateway,
1348                                                         prefix_len);
1349         } else {
1350                 connman_inet_add_network_route(index, route->network,
1351                                                 route->gateway,
1352                                                 route->netmask);
1353         }
1354 }
1355
1356 static int set_connected(struct vpn_provider *provider,
1357                                         connman_bool_t connected)
1358 {
1359         struct vpn_ipconfig *ipconfig;
1360
1361         DBG("provider %p id %s connected %d", provider,
1362                                         provider->identifier, connected);
1363
1364         if (connected == TRUE) {
1365                 if (provider->family == AF_INET6)
1366                         ipconfig = provider->ipconfig_ipv6;
1367                 else
1368                         ipconfig = provider->ipconfig_ipv4;
1369
1370                 __vpn_ipconfig_address_add(ipconfig, provider->family);
1371
1372                 if (handle_routes == TRUE)
1373                         __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1374
1375                 provider_indicate_state(provider,
1376                                         VPN_PROVIDER_STATE_READY);
1377
1378                 g_hash_table_foreach(provider->routes, provider_append_routes,
1379                                         provider);
1380
1381                 g_hash_table_foreach(provider->user_routes,
1382                                         provider_append_routes, provider);
1383
1384         } else {
1385                 provider_indicate_state(provider,
1386                                         VPN_PROVIDER_STATE_DISCONNECT);
1387
1388                 provider_indicate_state(provider,
1389                                         VPN_PROVIDER_STATE_IDLE);
1390         }
1391
1392         return 0;
1393 }
1394
1395 int vpn_provider_set_state(struct vpn_provider *provider,
1396                                         enum vpn_provider_state state)
1397 {
1398         if (provider == NULL)
1399                 return -EINVAL;
1400
1401         switch (state) {
1402         case VPN_PROVIDER_STATE_UNKNOWN:
1403                 return -EINVAL;
1404         case VPN_PROVIDER_STATE_IDLE:
1405                 return set_connected(provider, FALSE);
1406         case VPN_PROVIDER_STATE_CONNECT:
1407                 return provider_indicate_state(provider, state);
1408         case VPN_PROVIDER_STATE_READY:
1409                 return set_connected(provider, TRUE);
1410         case VPN_PROVIDER_STATE_DISCONNECT:
1411                 return provider_indicate_state(provider, state);
1412         case VPN_PROVIDER_STATE_FAILURE:
1413                 return provider_indicate_state(provider, state);
1414         }
1415         return -EINVAL;
1416 }
1417
1418 int vpn_provider_indicate_error(struct vpn_provider *provider,
1419                                         enum vpn_provider_error error)
1420 {
1421         DBG("provider %p id %s error %d", provider, provider->identifier,
1422                                                                         error);
1423
1424         switch (error) {
1425         case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1426                 break;
1427         case VPN_PROVIDER_ERROR_AUTH_FAILED:
1428                 break;
1429         case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1430                 break;
1431         default:
1432                 break;
1433         }
1434
1435         return 0;
1436 }
1437
1438 static int connection_unregister(struct vpn_provider *provider)
1439 {
1440         DBG("provider %p path %s", provider, provider->path);
1441
1442         if (provider->path == NULL)
1443                 return -EALREADY;
1444
1445         g_dbus_unregister_interface(connection, provider->path,
1446                                 VPN_CONNECTION_INTERFACE);
1447
1448         g_free(provider->path);
1449         provider->path = NULL;
1450
1451         return 0;
1452 }
1453
1454 static int connection_register(struct vpn_provider *provider)
1455 {
1456         DBG("provider %p path %s", provider, provider->path);
1457
1458         if (provider->path != NULL)
1459                 return -EALREADY;
1460
1461         provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1462                                                 provider->identifier);
1463
1464         g_dbus_register_interface(connection, provider->path,
1465                                 VPN_CONNECTION_INTERFACE,
1466                                 connection_methods, connection_signals,
1467                                 NULL, provider, NULL);
1468
1469         return 0;
1470 }
1471
1472 static void unregister_provider(gpointer data)
1473 {
1474         struct vpn_provider *provider = data;
1475
1476         configuration_count_del();
1477
1478         connection_unregister(provider);
1479
1480         vpn_provider_unref(provider);
1481 }
1482
1483 static void provider_initialize(struct vpn_provider *provider)
1484 {
1485         DBG("provider %p", provider);
1486
1487         provider->index = 0;
1488         provider->fd = -1;
1489         provider->name = NULL;
1490         provider->type = NULL;
1491         provider->domain = NULL;
1492         provider->identifier = NULL;
1493         provider->user_networks = NULL;
1494         provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1495                                         NULL, free_route);
1496         provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1497                                         g_free, free_route);
1498         provider->setting_strings = g_hash_table_new_full(g_str_hash,
1499                                                 g_str_equal, g_free, g_free);
1500 }
1501
1502 static struct vpn_provider *vpn_provider_new(void)
1503 {
1504         struct vpn_provider *provider;
1505
1506         provider = g_try_new0(struct vpn_provider, 1);
1507         if (provider == NULL)
1508                 return NULL;
1509
1510         provider->refcount = 1;
1511
1512         DBG("provider %p", provider);
1513         provider_initialize(provider);
1514
1515         return provider;
1516 }
1517
1518 static struct vpn_provider *vpn_provider_get(const char *identifier)
1519 {
1520         struct vpn_provider *provider;
1521
1522         provider = g_hash_table_lookup(provider_hash, identifier);
1523         if (provider != NULL)
1524                 return provider;
1525
1526         provider = vpn_provider_new();
1527         if (provider == NULL)
1528                 return NULL;
1529
1530         DBG("provider %p", provider);
1531
1532         provider->identifier = g_strdup(identifier);
1533
1534         g_hash_table_insert(provider_hash, provider->identifier, provider);
1535
1536         configuration_count_add();
1537
1538         return provider;
1539 }
1540
1541 static void provider_dbus_ident(char *ident)
1542 {
1543         int i, len = strlen(ident);
1544
1545         for (i = 0; i < len; i++) {
1546                 if (ident[i] >= '0' && ident[i] <= '9')
1547                         continue;
1548                 if (ident[i] >= 'a' && ident[i] <= 'z')
1549                         continue;
1550                 if (ident[i] >= 'A' && ident[i] <= 'Z')
1551                         continue;
1552                 ident[i] = '_';
1553         }
1554 }
1555
1556 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1557                 const char *ident)
1558 {
1559         struct vpn_provider *provider;
1560
1561         if (keyfile == NULL || ident == NULL)
1562                 return NULL;
1563
1564         provider = vpn_provider_lookup(ident);
1565         if (provider == NULL) {
1566                 provider = vpn_provider_get(ident);
1567                 if (provider == NULL) {
1568                         DBG("can not create provider");
1569                         return NULL;
1570                 }
1571
1572                 provider_load_from_keyfile(provider, keyfile);
1573
1574                 if (provider->name == NULL || provider->host == NULL ||
1575                                 provider->domain == NULL) {
1576                         DBG("cannot get name, host or domain");
1577                         vpn_provider_unref(provider);
1578                         return NULL;
1579                 }
1580
1581                 if (provider_register(provider) == 0)
1582                         connection_register(provider);
1583         }
1584         return provider;
1585 }
1586
1587 static void provider_create_all_from_type(const char *provider_type)
1588 {
1589         unsigned int i;
1590         char **providers;
1591         char *id, *type;
1592         GKeyFile *keyfile;
1593
1594         DBG("provider type %s", provider_type);
1595
1596         providers = __connman_storage_get_providers();
1597
1598         for (i = 0; providers[i] != NULL; i+=1) {
1599
1600                 if (strncmp(providers[i], "provider_", 9) != 0)
1601                         continue;
1602
1603                 id = providers[i] + 9;
1604                 keyfile = __connman_storage_load_provider(id);
1605
1606                 if (keyfile == NULL)
1607                         continue;
1608
1609                 type = g_key_file_get_string(keyfile, id, "Type", NULL);
1610
1611                 DBG("keyfile %p id %s type %s", keyfile, id, type);
1612
1613                 if (strcmp(provider_type, type) != 0) {
1614                         g_free(type);
1615                         g_key_file_free(keyfile);
1616                         continue;
1617                 }
1618
1619                 if (provider_create_from_keyfile(keyfile, id) == NULL)
1620                         DBG("could not create provider");
1621
1622                 g_free(type);
1623                 g_key_file_free(keyfile);
1624         }
1625         g_strfreev(providers);
1626 }
1627
1628 int __vpn_provider_create(DBusMessage *msg)
1629 {
1630         struct vpn_provider *provider;
1631         DBusMessageIter iter, array;
1632         const char *type = NULL, *name = NULL;
1633         const char *host = NULL, *domain = NULL;
1634         GSList *networks = NULL;
1635         char *ident;
1636         int err;
1637
1638         dbus_message_iter_init(msg, &iter);
1639         dbus_message_iter_recurse(&iter, &array);
1640
1641         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1642                 DBusMessageIter entry, value;
1643                 const char *key;
1644
1645                 dbus_message_iter_recurse(&array, &entry);
1646                 dbus_message_iter_get_basic(&entry, &key);
1647
1648                 dbus_message_iter_next(&entry);
1649                 dbus_message_iter_recurse(&entry, &value);
1650
1651                 switch (dbus_message_iter_get_arg_type(&value)) {
1652                 case DBUS_TYPE_STRING:
1653                         if (g_str_equal(key, "Type") == TRUE)
1654                                 dbus_message_iter_get_basic(&value, &type);
1655                         else if (g_str_equal(key, "Name") == TRUE)
1656                                 dbus_message_iter_get_basic(&value, &name);
1657                         else if (g_str_equal(key, "Host") == TRUE)
1658                                 dbus_message_iter_get_basic(&value, &host);
1659                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
1660                                 dbus_message_iter_get_basic(&value, &domain);
1661                         break;
1662                 case DBUS_TYPE_ARRAY:
1663                         if (g_str_equal(key, "UserRoutes") == TRUE)
1664                                 networks = get_user_networks(&value);
1665                         break;
1666                 }
1667
1668                 dbus_message_iter_next(&array);
1669         }
1670
1671         if (host == NULL || domain == NULL)
1672                 return -EINVAL;
1673
1674         DBG("Type %s name %s networks %p", type, name, networks);
1675
1676         if (type == NULL || name == NULL)
1677                 return -EOPNOTSUPP;
1678
1679         ident = g_strdup_printf("%s_%s", host, domain);
1680         provider_dbus_ident(ident);
1681
1682         DBG("ident %s", ident);
1683
1684         provider = vpn_provider_lookup(ident);
1685         if (provider == NULL) {
1686                 provider = vpn_provider_get(ident);
1687                 if (provider == NULL) {
1688                         DBG("can not create provider");
1689                         g_free(ident);
1690                         return -EOPNOTSUPP;
1691                 }
1692
1693                 provider->host = g_strdup(host);
1694                 provider->domain = g_strdup(domain);
1695                 provider->name = g_strdup(name);
1696                 provider->type = g_strdup(type);
1697
1698                 if (provider_register(provider) == 0)
1699                         vpn_provider_load(provider);
1700
1701                 provider_resolv_host_addr(provider);
1702         }
1703
1704         if (networks != NULL) {
1705                 g_slist_free_full(provider->user_networks, free_route);
1706                 provider->user_networks = networks;
1707                 set_user_networks(provider, provider->user_networks);
1708         }
1709
1710         dbus_message_iter_init(msg, &iter);
1711         dbus_message_iter_recurse(&iter, &array);
1712
1713         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1714                 DBusMessageIter entry, value;
1715                 const char *key, *str;
1716
1717                 dbus_message_iter_recurse(&array, &entry);
1718                 dbus_message_iter_get_basic(&entry, &key);
1719
1720                 dbus_message_iter_next(&entry);
1721                 dbus_message_iter_recurse(&entry, &value);
1722
1723                 switch (dbus_message_iter_get_arg_type(&value)) {
1724                 case DBUS_TYPE_STRING:
1725                         dbus_message_iter_get_basic(&value, &str);
1726                         vpn_provider_set_string(provider, key, str);
1727                         break;
1728                 }
1729
1730                 dbus_message_iter_next(&array);
1731         }
1732
1733         g_free(ident);
1734
1735         vpn_provider_save(provider);
1736
1737         err = provider_register(provider);
1738         if (err != 0 && err != -EALREADY)
1739                 return err;
1740
1741         connection_register(provider);
1742
1743         DBG("provider %p index %d path %s", provider, provider->index,
1744                                                         provider->path);
1745
1746         g_dbus_send_reply(connection, msg,
1747                                 DBUS_TYPE_OBJECT_PATH, &provider->path,
1748                                 DBUS_TYPE_INVALID);
1749
1750         connection_added_signal(provider);
1751
1752         return 0;
1753 }
1754
1755 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
1756 {
1757         DBusMessageIter entry;
1758         GHashTableIter hash;
1759         gpointer value, key;
1760
1761         g_hash_table_iter_init(&hash, provider_hash);
1762
1763         while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
1764                 struct vpn_provider *provider = value;
1765
1766                 DBG("path %s", provider->path);
1767
1768                 if (provider->identifier == NULL)
1769                         continue;
1770
1771                 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
1772                                 NULL, &entry);
1773                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1774                                 &provider->path);
1775                 append_properties(&entry, provider);
1776                 dbus_message_iter_close_container(iter, &entry);
1777         }
1778 }
1779
1780 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
1781 {
1782         DBusMessage *reply;
1783
1784         DBG("");
1785
1786         reply = dbus_message_new_method_return(msg);
1787         if (reply == NULL)
1788                 return NULL;
1789
1790         __connman_dbus_append_objpath_dict_array(reply,
1791                         append_connection_structs, NULL);
1792
1793         return reply;
1794 }
1795
1796 const char * __vpn_provider_get_ident(struct vpn_provider *provider)
1797 {
1798         if (provider == NULL)
1799                 return NULL;
1800
1801         return provider->identifier;
1802 }
1803
1804 int vpn_provider_set_string(struct vpn_provider *provider,
1805                                         const char *key, const char *value)
1806 {
1807         DBG("provider %p key %s value %s", provider, key, value);
1808
1809         if (g_str_equal(key, "Type") == TRUE) {
1810                 g_free(provider->type);
1811                 provider->type = g_strdup(value);
1812         } else if (g_str_equal(key, "Name") == TRUE) {
1813                 g_free(provider->name);
1814                 provider->name = g_strdup(value);
1815         } else if (g_str_equal(key, "Host") == TRUE) {
1816                 g_free(provider->host);
1817                 provider->host = g_strdup(value);
1818         } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1819                 g_free(provider->domain);
1820                 provider->domain = g_strdup(value);
1821         } else
1822                 g_hash_table_replace(provider->setting_strings,
1823                                 g_strdup(key), g_strdup(value));
1824         return 0;
1825 }
1826
1827 const char *vpn_provider_get_string(struct vpn_provider *provider,
1828                                                         const char *key)
1829 {
1830         DBG("provider %p key %s", provider, key);
1831
1832         if (g_str_equal(key, "Type") == TRUE)
1833                 return provider->type;
1834         else if (g_str_equal(key, "Name") == TRUE)
1835                 return provider->name;
1836         else if (g_str_equal(key, "Host") == TRUE)
1837                 return provider->host;
1838         else if (g_str_equal(key, "HostIP") == TRUE) {
1839                 if (provider->host_ip == NULL ||
1840                                 provider->host_ip[0] == NULL)
1841                         return provider->host;
1842                 else
1843                         return provider->host_ip[0];
1844         } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1845                 return provider->domain;
1846
1847         return g_hash_table_lookup(provider->setting_strings, key);
1848 }
1849
1850 connman_bool_t __vpn_provider_check_routes(struct vpn_provider *provider)
1851 {
1852         if (provider == NULL)
1853                 return FALSE;
1854
1855         if (provider->user_routes != NULL &&
1856                         g_hash_table_size(provider->user_routes) > 0)
1857                 return TRUE;
1858
1859         if (provider->routes != NULL &&
1860                         g_hash_table_size(provider->routes) > 0)
1861                 return TRUE;
1862
1863         return FALSE;
1864 }
1865
1866 void *vpn_provider_get_data(struct vpn_provider *provider)
1867 {
1868         return provider->driver_data;
1869 }
1870
1871 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
1872 {
1873         provider->driver_data = data;
1874 }
1875
1876 void vpn_provider_set_index(struct vpn_provider *provider, int index)
1877 {
1878         DBG("index %d provider %p", index, provider);
1879
1880         if (provider->ipconfig_ipv4 == NULL) {
1881                 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
1882                                                                 AF_INET);
1883                 if (provider->ipconfig_ipv4 == NULL) {
1884                         DBG("Couldnt create ipconfig for IPv4");
1885                         goto done;
1886                 }
1887         }
1888
1889         __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
1890
1891         if (provider->ipconfig_ipv6 == NULL) {
1892                 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
1893                                                                 AF_INET6);
1894                 if (provider->ipconfig_ipv6 == NULL) {
1895                         DBG("Couldnt create ipconfig for IPv6");
1896                         goto done;
1897                 }
1898         }
1899
1900         __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
1901
1902 done:
1903         provider->index = index;
1904 }
1905
1906 int vpn_provider_get_index(struct vpn_provider *provider)
1907 {
1908         return provider->index;
1909 }
1910
1911 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
1912                                         struct connman_ipaddress *ipaddress)
1913 {
1914         struct vpn_ipconfig *ipconfig = NULL;
1915
1916         switch (ipaddress->family) {
1917         case AF_INET:
1918                 ipconfig = provider->ipconfig_ipv4;
1919                 break;
1920         case AF_INET6:
1921                 ipconfig = provider->ipconfig_ipv6;
1922                 break;
1923         default:
1924                 break;
1925         }
1926
1927         DBG("provider %p ipconfig %p family %d", provider, ipconfig,
1928                                                         ipaddress->family);
1929
1930         if (ipconfig == NULL)
1931                 return -EINVAL;
1932
1933         provider->family = ipaddress->family;
1934
1935         __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
1936         __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
1937         __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1938         __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1939         __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1940
1941         return 0;
1942 }
1943
1944 int vpn_provider_set_pac(struct vpn_provider *provider,
1945                                 const char *pac)
1946 {
1947         DBG("provider %p pac %s", provider, pac);
1948
1949         return 0;
1950 }
1951
1952
1953 int vpn_provider_set_domain(struct vpn_provider *provider,
1954                                         const char *domain)
1955 {
1956         DBG("provider %p domain %s", provider, domain);
1957
1958         g_free(provider->domain);
1959         provider->domain = g_strdup(domain);
1960
1961         return 0;
1962 }
1963
1964 int vpn_provider_set_nameservers(struct vpn_provider *provider,
1965                                         const char *nameservers)
1966 {
1967         DBG("provider %p nameservers %s", provider, nameservers);
1968
1969         g_strfreev(provider->nameservers);
1970         provider->nameservers = NULL;
1971
1972         if (nameservers == NULL)
1973                 return 0;
1974
1975         provider->nameservers = g_strsplit(nameservers, " ", 0);
1976
1977         return 0;
1978 }
1979
1980 enum provider_route_type {
1981         PROVIDER_ROUTE_TYPE_NONE = 0,
1982         PROVIDER_ROUTE_TYPE_MASK = 1,
1983         PROVIDER_ROUTE_TYPE_ADDR = 2,
1984         PROVIDER_ROUTE_TYPE_GW   = 3,
1985 };
1986
1987 static int route_env_parse(struct vpn_provider *provider, const char *key,
1988                                 int *family, unsigned long *idx,
1989                                 enum provider_route_type *type)
1990 {
1991         char *end;
1992         const char *start;
1993
1994         DBG("name %s", provider->name);
1995
1996         if (!strcmp(provider->type, "openvpn")) {
1997                 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1998                         start = key + strlen("route_network_");
1999                         *type = PROVIDER_ROUTE_TYPE_ADDR;
2000                 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
2001                         start = key + strlen("route_netmask_");
2002                         *type = PROVIDER_ROUTE_TYPE_MASK;
2003                 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
2004                         start = key + strlen("route_gateway_");
2005                         *type = PROVIDER_ROUTE_TYPE_GW;
2006                 } else
2007                         return -EINVAL;
2008
2009                 *family = AF_INET;
2010                 *idx = g_ascii_strtoull(start, &end, 10);
2011
2012         } else if (!strcmp(provider->type, "openconnect")) {
2013                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
2014                         *family = AF_INET;
2015                         start = key + strlen("CISCO_SPLIT_INC_");
2016                 } else if (g_str_has_prefix(key,
2017                                         "CISCO_IPV6_SPLIT_INC_") == TRUE) {
2018                         *family = AF_INET6;
2019                         start = key + strlen("CISCO_IPV6_SPLIT_INC_");
2020                 } else
2021                         return -EINVAL;
2022
2023                 *idx = g_ascii_strtoull(start, &end, 10);
2024
2025                 if (strncmp(end, "_ADDR", 5) == 0)
2026                         *type = PROVIDER_ROUTE_TYPE_ADDR;
2027                 else if (strncmp(end, "_MASK", 5) == 0)
2028                         *type = PROVIDER_ROUTE_TYPE_MASK;
2029                 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2030                                 *family == AF_INET6) {
2031                         *type = PROVIDER_ROUTE_TYPE_MASK;
2032                 } else
2033                         return -EINVAL;
2034         }
2035
2036         return 0;
2037 }
2038
2039 int vpn_provider_append_route(struct vpn_provider *provider,
2040                                         const char *key, const char *value)
2041 {
2042         struct vpn_route *route;
2043         int ret, family = 0;
2044         unsigned long idx = 0;
2045         enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2046
2047         DBG("key %s value %s", key, value);
2048
2049         ret = route_env_parse(provider, key, &family, &idx, &type);
2050         if (ret < 0)
2051                 return ret;
2052
2053         DBG("idx %lu family %d type %d", idx, family, type);
2054
2055         route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2056         if (route == NULL) {
2057                 route = g_try_new0(struct vpn_route, 1);
2058                 if (route == NULL) {
2059                         connman_error("out of memory");
2060                         return -ENOMEM;
2061                 }
2062
2063                 route->family = family;
2064
2065                 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2066                                                 route);
2067         }
2068
2069         switch (type) {
2070         case PROVIDER_ROUTE_TYPE_NONE:
2071                 break;
2072         case PROVIDER_ROUTE_TYPE_MASK:
2073                 route->netmask = g_strdup(value);
2074                 break;
2075         case PROVIDER_ROUTE_TYPE_ADDR:
2076                 route->network = g_strdup(value);
2077                 break;
2078         case PROVIDER_ROUTE_TYPE_GW:
2079                 route->gateway = g_strdup(value);
2080                 break;
2081         }
2082
2083         if (handle_routes == FALSE) {
2084                 if (route->netmask != NULL && route->gateway != NULL &&
2085                                                         route->network != NULL)
2086                         provider_schedule_changed(provider,
2087                                                 SERVER_ROUTES_CHANGED);
2088         }
2089
2090         return 0;
2091 }
2092
2093 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2094 {
2095         if (provider->driver == NULL)
2096                 return NULL;
2097
2098         return provider->driver->name;
2099 }
2100
2101 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2102 {
2103         return provider->identifier;
2104 }
2105
2106 static gint compare_priority(gconstpointer a, gconstpointer b)
2107 {
2108         return 0;
2109 }
2110
2111 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2112 {
2113         struct vpn_provider *provider = value;
2114
2115         if (provider->driver != NULL && provider->driver->remove)
2116                 provider->driver->remove(provider);
2117
2118         connection_unregister(provider);
2119 }
2120
2121 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2122 {
2123         DBG("driver %p name %s", driver, driver->name);
2124
2125         driver_list = g_slist_insert_sorted(driver_list, driver,
2126                                                         compare_priority);
2127         provider_create_all_from_type(driver->name);
2128         return 0;
2129 }
2130
2131 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2132 {
2133         GHashTableIter iter;
2134         gpointer value, key;
2135
2136         DBG("driver %p name %s", driver, driver->name);
2137
2138         driver_list = g_slist_remove(driver_list, driver);
2139
2140         g_hash_table_iter_init(&iter, provider_hash);
2141         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2142                 struct vpn_provider *provider = value;
2143
2144                 if (provider != NULL && provider->driver != NULL &&
2145                                 provider->driver->type == driver->type &&
2146                                 g_strcmp0(provider->driver->name,
2147                                                         driver->name) == 0) {
2148                         provider->driver = NULL;
2149                 }
2150         }
2151 }
2152
2153 static gboolean check_vpn_count(gpointer data)
2154 {
2155         if (configuration_count == 0) {
2156                 connman_info("No VPN configurations found, quitting.");
2157                 raise(SIGTERM);
2158         }
2159
2160         return FALSE;
2161 }
2162
2163 void __vpn_provider_check_connections(void)
2164 {
2165         /*
2166          * If we were started when there is no providers configured,
2167          * then just quit. This happens when connman starts and its
2168          * vpn plugin asks connman-vpnd if it has any connections
2169          * configured. If there are none, then we can stop the vpn
2170          * daemon.
2171          */
2172         g_timeout_add(1000, check_vpn_count, NULL);
2173 }
2174
2175 const char *vpn_provider_get_name(struct vpn_provider *provider)
2176 {
2177         return provider->name;
2178 }
2179
2180 const char *vpn_provider_get_host(struct vpn_provider *provider)
2181 {
2182         return provider->host;
2183 }
2184
2185 const char *vpn_provider_get_path(struct vpn_provider *provider)
2186 {
2187         return provider->path;
2188 }
2189
2190 static int agent_probe(struct connman_agent *agent)
2191 {
2192         DBG("agent %p", agent);
2193         return 0;
2194 }
2195
2196 static void agent_remove(struct connman_agent *agent)
2197 {
2198         DBG("agent %p", agent);
2199 }
2200
2201 static struct connman_agent_driver agent_driver = {
2202         .name           = "vpn",
2203         .interface      = VPN_AGENT_INTERFACE,
2204         .probe          = agent_probe,
2205         .remove         = agent_remove,
2206 };
2207
2208 int __vpn_provider_init(gboolean do_routes)
2209 {
2210         int err;
2211
2212         DBG("");
2213
2214         handle_routes = do_routes;
2215
2216         err = connman_agent_driver_register(&agent_driver);
2217         if (err < 0) {
2218                 connman_error("Cannot register agent driver for %s",
2219                                                 agent_driver.name);
2220                 return err;
2221         }
2222
2223         connection = connman_dbus_get_connection();
2224
2225         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2226                                                 NULL, unregister_provider);
2227
2228         return 0;
2229 }
2230
2231 void __vpn_provider_cleanup(void)
2232 {
2233         DBG("");
2234
2235         g_hash_table_foreach(provider_hash, clean_provider, NULL);
2236
2237         g_hash_table_destroy(provider_hash);
2238         provider_hash = NULL;
2239
2240         connman_agent_driver_unregister(&agent_driver);
2241
2242         dbus_connection_unref(connection);
2243 }