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