vpn-provider: Register agent driver
[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)
461                 return __connman_error_failed(msg, -err);
462         else
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 < 0) {
970                 if (err != -EINPROGRESS)
971                         return err;
972
973                 return -EINPROGRESS;
974         }
975
976         return 0;
977 }
978
979 static void connect_cb(struct vpn_provider *provider, void *user_data,
980                                                                 int error)
981 {
982         DBusMessage *pending = user_data;
983
984         DBG("provider %p user %p error %d", provider, user_data, error);
985
986         if (error != 0) {
987                 DBusMessage *reply = __connman_error_failed(pending, error);
988                 if (reply != NULL)
989                         g_dbus_send_message(connection, reply);
990
991                 vpn_provider_indicate_error(provider,
992                                         VPN_PROVIDER_ERROR_CONNECT_FAILED);
993                 vpn_provider_set_state(provider, VPN_PROVIDER_STATE_FAILURE);
994         } else
995                 g_dbus_send_reply(connection, pending, DBUS_TYPE_INVALID);
996
997         dbus_message_unref(pending);
998 }
999
1000 int __vpn_provider_connect(struct vpn_provider *provider, DBusMessage *msg)
1001 {
1002         int err;
1003
1004         DBG("provider %p", provider);
1005
1006         if (provider->driver != NULL && provider->driver->connect != NULL) {
1007                 dbus_message_ref(msg);
1008                 err = provider->driver->connect(provider, connect_cb, msg);
1009         } else
1010                 return -EOPNOTSUPP;
1011
1012         return err;
1013 }
1014
1015 static void connection_removed_signal(struct vpn_provider *provider)
1016 {
1017         DBusMessage *signal;
1018         DBusMessageIter iter;
1019
1020         signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1021                         VPN_MANAGER_INTERFACE, "ConnectionRemoved");
1022         if (signal == NULL)
1023                 return;
1024
1025         dbus_message_iter_init_append(signal, &iter);
1026         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1027                                                         &provider->path);
1028         dbus_connection_send(connection, signal, NULL);
1029         dbus_message_unref(signal);
1030 }
1031
1032 static char *get_ident(const char *path)
1033 {
1034         char *pos;
1035
1036         if (*path != '/')
1037                 return NULL;
1038
1039         pos = strrchr(path, '/');
1040         if (pos == NULL)
1041                 return NULL;
1042
1043         return pos + 1;
1044 }
1045
1046 int __vpn_provider_remove(const char *path)
1047 {
1048         struct vpn_provider *provider;
1049         char *ident;
1050
1051         DBG("path %s", path);
1052
1053         ident = get_ident(path);
1054
1055         provider = vpn_provider_lookup(ident);
1056         if (provider != NULL) {
1057                 DBG("Removing VPN %s", provider->identifier);
1058
1059                 connection_removed_signal(provider);
1060
1061                 provider_unregister(provider);
1062                 g_hash_table_remove(provider_hash, provider->identifier);
1063
1064                 __connman_storage_remove_provider(ident);
1065                 return 0;
1066         }
1067
1068         return -ENXIO;
1069 }
1070
1071 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1072 {
1073         struct vpn_provider *provider = user_data;
1074         const char *address, *gateway, *peer;
1075
1076         address = __vpn_ipconfig_get_local(provider->ipconfig_ipv4);
1077         if (address != NULL) {
1078                 in_addr_t addr;
1079                 struct in_addr netmask;
1080                 char *mask;
1081                 int prefixlen;
1082
1083                 prefixlen = __vpn_ipconfig_get_prefixlen(
1084                                                 provider->ipconfig_ipv4);
1085
1086                 addr = 0xffffffff << (32 - prefixlen);
1087                 netmask.s_addr = htonl(addr);
1088                 mask = inet_ntoa(netmask);
1089
1090                 connman_dbus_dict_append_basic(iter, "Address",
1091                                                 DBUS_TYPE_STRING, &address);
1092
1093                 connman_dbus_dict_append_basic(iter, "Netmask",
1094                                                 DBUS_TYPE_STRING, &mask);
1095         }
1096
1097         gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv4);
1098         if (gateway != NULL)
1099                 connman_dbus_dict_append_basic(iter, "Gateway",
1100                                                 DBUS_TYPE_STRING, &gateway);
1101
1102         peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv4);
1103         if (peer != NULL)
1104                 connman_dbus_dict_append_basic(iter, "Peer",
1105                                                 DBUS_TYPE_STRING, &peer);
1106 }
1107
1108 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1109 {
1110         struct vpn_provider *provider = user_data;
1111         const char *address, *gateway, *peer;
1112
1113         address = __vpn_ipconfig_get_local(provider->ipconfig_ipv6);
1114         if (address != NULL) {
1115                 unsigned char prefixlen;
1116
1117                 connman_dbus_dict_append_basic(iter, "Address",
1118                                                 DBUS_TYPE_STRING, &address);
1119
1120                 prefixlen = __vpn_ipconfig_get_prefixlen(
1121                                                 provider->ipconfig_ipv6);
1122
1123                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1124                                                 DBUS_TYPE_BYTE, &prefixlen);
1125         }
1126
1127         gateway = __vpn_ipconfig_get_gateway(provider->ipconfig_ipv6);
1128         if (gateway != NULL)
1129                 connman_dbus_dict_append_basic(iter, "Gateway",
1130                                                 DBUS_TYPE_STRING, &gateway);
1131
1132         peer = __vpn_ipconfig_get_peer(provider->ipconfig_ipv6);
1133         if (peer != NULL)
1134                 connman_dbus_dict_append_basic(iter, "Peer",
1135                                                 DBUS_TYPE_STRING, &peer);
1136 }
1137
1138 static const char *state2string(enum vpn_provider_state state)
1139 {
1140         switch (state) {
1141         case VPN_PROVIDER_STATE_UNKNOWN:
1142                 break;
1143         case VPN_PROVIDER_STATE_IDLE:
1144                 return "idle";
1145         case VPN_PROVIDER_STATE_CONNECT:
1146                 return "configuration";
1147         case VPN_PROVIDER_STATE_READY:
1148                 return "ready";
1149         case VPN_PROVIDER_STATE_DISCONNECT:
1150                 return "disconnect";
1151         case VPN_PROVIDER_STATE_FAILURE:
1152                 return "failure";
1153         }
1154
1155         return NULL;
1156 }
1157
1158 static int provider_indicate_state(struct vpn_provider *provider,
1159                                 enum vpn_provider_state state)
1160 {
1161         const char *str;
1162
1163         DBG("provider %p state %d", provider, state);
1164
1165         str = state2string(state);
1166         if (str == NULL)
1167                 return -EINVAL;
1168
1169         provider->state = state;
1170
1171         if (state == VPN_PROVIDER_STATE_READY) {
1172                 connman_dbus_property_changed_basic(provider->path,
1173                                         VPN_CONNECTION_INTERFACE, "Index",
1174                                         DBUS_TYPE_INT32, &provider->index);
1175
1176                 if (provider->family == AF_INET)
1177                         connman_dbus_property_changed_dict(provider->path,
1178                                         VPN_CONNECTION_INTERFACE, "IPv4",
1179                                         append_ipv4, provider);
1180                 else if (provider->family == AF_INET6)
1181                         connman_dbus_property_changed_dict(provider->path,
1182                                         VPN_CONNECTION_INTERFACE, "IPv6",
1183                                         append_ipv6, provider);
1184         }
1185
1186         connman_dbus_property_changed_basic(provider->path,
1187                                         VPN_CONNECTION_INTERFACE, "State",
1188                                         DBUS_TYPE_STRING, &str);
1189         return 0;
1190 }
1191
1192 static void append_nameservers(DBusMessageIter *iter, char **servers)
1193 {
1194         int i;
1195
1196         DBG("%p", servers);
1197
1198         for (i = 0; servers[i] != NULL; i++) {
1199                 DBG("servers[%d] %s", i, servers[i]);
1200                 dbus_message_iter_append_basic(iter,
1201                                         DBUS_TYPE_STRING, &servers[i]);
1202         }
1203 }
1204
1205 static void append_dns(DBusMessageIter *iter, void *user_data)
1206 {
1207         struct vpn_provider *provider = user_data;
1208
1209         if (provider->nameservers != NULL)
1210                 append_nameservers(iter, provider->nameservers);
1211 }
1212
1213 static void append_state(DBusMessageIter *iter,
1214                                         struct vpn_provider *provider)
1215 {
1216         char *str;
1217
1218         switch (provider->state) {
1219         case VPN_PROVIDER_STATE_UNKNOWN:
1220         case VPN_PROVIDER_STATE_IDLE:
1221                 str = "idle";
1222                 break;
1223         case VPN_PROVIDER_STATE_CONNECT:
1224                 str = "configuration";
1225                 break;
1226         case VPN_PROVIDER_STATE_READY:
1227                 str = "ready";
1228                 break;
1229         case VPN_PROVIDER_STATE_DISCONNECT:
1230                 str = "disconnect";
1231                 break;
1232         case VPN_PROVIDER_STATE_FAILURE:
1233                 str = "failure";
1234                 break;
1235         }
1236
1237         connman_dbus_dict_append_basic(iter, "State",
1238                                 DBUS_TYPE_STRING, &str);
1239 }
1240
1241 static void append_properties(DBusMessageIter *iter,
1242                                         struct vpn_provider *provider)
1243 {
1244         DBusMessageIter dict;
1245
1246         connman_dbus_dict_open(iter, &dict);
1247
1248         append_state(&dict, provider);
1249
1250         if (provider->type != NULL)
1251                 connman_dbus_dict_append_basic(&dict, "Type",
1252                                         DBUS_TYPE_STRING, &provider->type);
1253
1254         if (provider->name != NULL)
1255                 connman_dbus_dict_append_basic(&dict, "Name",
1256                                         DBUS_TYPE_STRING, &provider->name);
1257
1258         if (provider->host != NULL)
1259                 connman_dbus_dict_append_basic(&dict, "Host",
1260                                         DBUS_TYPE_STRING, &provider->host);
1261         if (provider->index >= 0)
1262                 connman_dbus_dict_append_basic(&dict, "Index",
1263                                         DBUS_TYPE_INT32, &provider->index);
1264         if (provider->domain != NULL)
1265                 connman_dbus_dict_append_basic(&dict, "Domain",
1266                                         DBUS_TYPE_STRING, &provider->domain);
1267
1268         if (provider->family == AF_INET)
1269                 connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
1270                                                 provider);
1271         else if (provider->family == AF_INET6)
1272                 connman_dbus_dict_append_dict(&dict, "IPv6", append_ipv6,
1273                                                 provider);
1274
1275         connman_dbus_dict_append_array(&dict, "Nameservers",
1276                                 DBUS_TYPE_STRING, append_dns, provider);
1277
1278         connman_dbus_dict_append_array(&dict, "UserRoutes",
1279                                 DBUS_TYPE_DICT_ENTRY, append_routes,
1280                                 provider->user_routes);
1281
1282         connman_dbus_dict_append_array(&dict, "ServerRoutes",
1283                                 DBUS_TYPE_DICT_ENTRY, append_routes,
1284                                 provider->routes);
1285
1286         connman_dbus_dict_close(iter, &dict);
1287 }
1288
1289 static void connection_added_signal(struct vpn_provider *provider)
1290 {
1291         DBusMessage *signal;
1292         DBusMessageIter iter;
1293
1294         signal = dbus_message_new_signal(VPN_MANAGER_PATH,
1295                         VPN_MANAGER_INTERFACE, "ConnectionAdded");
1296         if (signal == NULL)
1297                 return;
1298
1299         dbus_message_iter_init_append(signal, &iter);
1300         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1301                                                         &provider->path);
1302         append_properties(&iter, provider);
1303
1304         dbus_connection_send(connection, signal, NULL);
1305         dbus_message_unref(signal);
1306 }
1307
1308 static connman_bool_t check_host(char **hosts, char *host)
1309 {
1310         int i;
1311
1312         if (hosts == NULL)
1313                 return FALSE;
1314
1315         for (i = 0; hosts[i] != NULL; i++) {
1316                 if (g_strcmp0(hosts[i], host) == 0)
1317                         return TRUE;
1318         }
1319
1320         return FALSE;
1321 }
1322
1323 static void provider_append_routes(gpointer key, gpointer value,
1324                                         gpointer user_data)
1325 {
1326         struct vpn_route *route = value;
1327         struct vpn_provider *provider = user_data;
1328         int index = provider->index;
1329
1330         if (handle_routes == FALSE)
1331                 return;
1332
1333         /*
1334          * If the VPN administrator/user has given a route to
1335          * VPN server, then we must discard that because the
1336          * server cannot be contacted via VPN tunnel.
1337          */
1338         if (check_host(provider->host_ip, route->network) == TRUE) {
1339                 DBG("Discarding VPN route to %s via %s at index %d",
1340                         route->network, route->gateway, index);
1341                 return;
1342         }
1343
1344         if (route->family == AF_INET6) {
1345                 unsigned char prefix_len = atoi(route->netmask);
1346
1347                 connman_inet_add_ipv6_network_route(index, route->network,
1348                                                         route->gateway,
1349                                                         prefix_len);
1350         } else {
1351                 connman_inet_add_network_route(index, route->network,
1352                                                 route->gateway,
1353                                                 route->netmask);
1354         }
1355 }
1356
1357 static int set_connected(struct vpn_provider *provider,
1358                                         connman_bool_t connected)
1359 {
1360         struct vpn_ipconfig *ipconfig;
1361
1362         DBG("provider %p id %s connected %d", provider,
1363                                         provider->identifier, connected);
1364
1365         if (connected == TRUE) {
1366                 if (provider->family == AF_INET6)
1367                         ipconfig = provider->ipconfig_ipv6;
1368                 else
1369                         ipconfig = provider->ipconfig_ipv4;
1370
1371                 __vpn_ipconfig_address_add(ipconfig, provider->family);
1372
1373                 if (handle_routes == TRUE)
1374                         __vpn_ipconfig_gateway_add(ipconfig, provider->family);
1375
1376                 provider_indicate_state(provider,
1377                                         VPN_PROVIDER_STATE_READY);
1378
1379                 g_hash_table_foreach(provider->routes, provider_append_routes,
1380                                         provider);
1381
1382                 g_hash_table_foreach(provider->user_routes,
1383                                         provider_append_routes, provider);
1384
1385         } else {
1386                 provider_indicate_state(provider,
1387                                         VPN_PROVIDER_STATE_DISCONNECT);
1388
1389                 provider_indicate_state(provider,
1390                                         VPN_PROVIDER_STATE_IDLE);
1391         }
1392
1393         return 0;
1394 }
1395
1396 int vpn_provider_set_state(struct vpn_provider *provider,
1397                                         enum vpn_provider_state state)
1398 {
1399         if (provider == NULL)
1400                 return -EINVAL;
1401
1402         switch (state) {
1403         case VPN_PROVIDER_STATE_UNKNOWN:
1404                 return -EINVAL;
1405         case VPN_PROVIDER_STATE_IDLE:
1406                 return set_connected(provider, FALSE);
1407         case VPN_PROVIDER_STATE_CONNECT:
1408                 return provider_indicate_state(provider, state);
1409         case VPN_PROVIDER_STATE_READY:
1410                 return set_connected(provider, TRUE);
1411         case VPN_PROVIDER_STATE_DISCONNECT:
1412                 return provider_indicate_state(provider, state);
1413         case VPN_PROVIDER_STATE_FAILURE:
1414                 return provider_indicate_state(provider, state);
1415         }
1416         return -EINVAL;
1417 }
1418
1419 int vpn_provider_indicate_error(struct vpn_provider *provider,
1420                                         enum vpn_provider_error error)
1421 {
1422         DBG("provider %p id %s error %d", provider, provider->identifier,
1423                                                                         error);
1424
1425         switch (error) {
1426         case VPN_PROVIDER_ERROR_LOGIN_FAILED:
1427                 break;
1428         case VPN_PROVIDER_ERROR_AUTH_FAILED:
1429                 break;
1430         case VPN_PROVIDER_ERROR_CONNECT_FAILED:
1431                 break;
1432         default:
1433                 break;
1434         }
1435
1436         return 0;
1437 }
1438
1439 static void unregister_provider(gpointer data)
1440 {
1441         struct vpn_provider *provider = data;
1442
1443         configuration_count_del();
1444
1445         vpn_provider_unref(provider);
1446 }
1447
1448 static void provider_initialize(struct vpn_provider *provider)
1449 {
1450         DBG("provider %p", provider);
1451
1452         provider->index = 0;
1453         provider->fd = -1;
1454         provider->name = NULL;
1455         provider->type = NULL;
1456         provider->domain = NULL;
1457         provider->identifier = NULL;
1458         provider->user_networks = NULL;
1459         provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1460                                         NULL, free_route);
1461         provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
1462                                         g_free, free_route);
1463         provider->setting_strings = g_hash_table_new_full(g_str_hash,
1464                                                 g_str_equal, g_free, g_free);
1465 }
1466
1467 static struct vpn_provider *vpn_provider_new(void)
1468 {
1469         struct vpn_provider *provider;
1470
1471         provider = g_try_new0(struct vpn_provider, 1);
1472         if (provider == NULL)
1473                 return NULL;
1474
1475         provider->refcount = 1;
1476
1477         DBG("provider %p", provider);
1478         provider_initialize(provider);
1479
1480         return provider;
1481 }
1482
1483 static struct vpn_provider *vpn_provider_get(const char *identifier)
1484 {
1485         struct vpn_provider *provider;
1486
1487         provider = g_hash_table_lookup(provider_hash, identifier);
1488         if (provider != NULL)
1489                 return provider;
1490
1491         provider = vpn_provider_new();
1492         if (provider == NULL)
1493                 return NULL;
1494
1495         DBG("provider %p", provider);
1496
1497         provider->identifier = g_strdup(identifier);
1498
1499         g_hash_table_insert(provider_hash, provider->identifier, provider);
1500
1501         configuration_count_add();
1502
1503         return provider;
1504 }
1505
1506 static void provider_dbus_ident(char *ident)
1507 {
1508         int i, len = strlen(ident);
1509
1510         for (i = 0; i < len; i++) {
1511                 if (ident[i] >= '0' && ident[i] <= '9')
1512                         continue;
1513                 if (ident[i] >= 'a' && ident[i] <= 'z')
1514                         continue;
1515                 if (ident[i] >= 'A' && ident[i] <= 'Z')
1516                         continue;
1517                 ident[i] = '_';
1518         }
1519 }
1520
1521 static int connection_unregister(struct vpn_provider *provider)
1522 {
1523         if (provider->path == NULL)
1524                 return -EALREADY;
1525
1526         g_dbus_unregister_interface(connection, provider->path,
1527                                 VPN_CONNECTION_INTERFACE);
1528
1529         g_free(provider->path);
1530         provider->path = NULL;
1531
1532         return 0;
1533 }
1534
1535 static int connection_register(struct vpn_provider *provider)
1536 {
1537         DBG("provider %p path %s", provider, provider->path);
1538
1539         if (provider->path != NULL)
1540                 return -EALREADY;
1541
1542         provider->path = g_strdup_printf("%s/connection/%s", VPN_PATH,
1543                                                 provider->identifier);
1544
1545         g_dbus_register_interface(connection, provider->path,
1546                                 VPN_CONNECTION_INTERFACE,
1547                                 connection_methods, connection_signals,
1548                                 NULL, provider, NULL);
1549
1550         return 0;
1551 }
1552
1553 static struct vpn_provider *provider_create_from_keyfile(GKeyFile *keyfile,
1554                 const char *ident)
1555 {
1556         struct vpn_provider *provider;
1557
1558         if (keyfile == NULL || ident == NULL)
1559                 return NULL;
1560
1561         provider = vpn_provider_lookup(ident);
1562         if (provider == NULL) {
1563                 provider = vpn_provider_get(ident);
1564                 if (provider == NULL) {
1565                         DBG("can not create provider");
1566                         return NULL;
1567                 }
1568
1569                 provider_load_from_keyfile(provider, keyfile);
1570
1571                 if (provider->name == NULL || provider->host == NULL ||
1572                                 provider->domain == NULL) {
1573                         DBG("cannot get name, host or domain");
1574                         vpn_provider_unref(provider);
1575                         return NULL;
1576                 }
1577
1578                 if (provider_register(provider) == 0)
1579                         connection_register(provider);
1580         }
1581         return provider;
1582 }
1583
1584 static void provider_create_all_from_type(const char *provider_type)
1585 {
1586         unsigned int i;
1587         char **providers;
1588         char *id, *type;
1589         GKeyFile *keyfile;
1590
1591         DBG("provider type %s", provider_type);
1592
1593         providers = __connman_storage_get_providers();
1594
1595         for (i = 0; providers[i] != NULL; i+=1) {
1596
1597                 if (strncmp(providers[i], "provider_", 9) != 0)
1598                         continue;
1599
1600                 id = providers[i] + 9;
1601                 keyfile = __connman_storage_load_provider(id);
1602
1603                 if (keyfile == NULL)
1604                         continue;
1605
1606                 type = g_key_file_get_string(keyfile, id, "Type", NULL);
1607
1608                 DBG("keyfile %p id %s type %s", keyfile, id, type);
1609
1610                 if (strcmp(provider_type, type) != 0) {
1611                         g_free(type);
1612                         g_key_file_free(keyfile);
1613                         continue;
1614                 }
1615
1616                 if (provider_create_from_keyfile(keyfile, id) == NULL)
1617                         DBG("could not create provider");
1618
1619                 g_free(type);
1620                 g_key_file_free(keyfile);
1621         }
1622         g_strfreev(providers);
1623 }
1624
1625 int __vpn_provider_create(DBusMessage *msg)
1626 {
1627         struct vpn_provider *provider;
1628         DBusMessageIter iter, array;
1629         const char *type = NULL, *name = NULL;
1630         const char *host = NULL, *domain = NULL;
1631         GSList *networks = NULL;
1632         char *ident;
1633         int err;
1634
1635         dbus_message_iter_init(msg, &iter);
1636         dbus_message_iter_recurse(&iter, &array);
1637
1638         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1639                 DBusMessageIter entry, value;
1640                 const char *key;
1641
1642                 dbus_message_iter_recurse(&array, &entry);
1643                 dbus_message_iter_get_basic(&entry, &key);
1644
1645                 dbus_message_iter_next(&entry);
1646                 dbus_message_iter_recurse(&entry, &value);
1647
1648                 switch (dbus_message_iter_get_arg_type(&value)) {
1649                 case DBUS_TYPE_STRING:
1650                         if (g_str_equal(key, "Type") == TRUE)
1651                                 dbus_message_iter_get_basic(&value, &type);
1652                         else if (g_str_equal(key, "Name") == TRUE)
1653                                 dbus_message_iter_get_basic(&value, &name);
1654                         else if (g_str_equal(key, "Host") == TRUE)
1655                                 dbus_message_iter_get_basic(&value, &host);
1656                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
1657                                 dbus_message_iter_get_basic(&value, &domain);
1658                         break;
1659                 case DBUS_TYPE_ARRAY:
1660                         if (g_str_equal(key, "UserRoutes") == TRUE)
1661                                 networks = get_user_networks(&value);
1662                         break;
1663                 }
1664
1665                 dbus_message_iter_next(&array);
1666         }
1667
1668         if (host == NULL || domain == NULL)
1669                 return -EINVAL;
1670
1671         DBG("Type %s name %s networks %p", type, name, networks);
1672
1673         if (type == NULL || name == NULL)
1674                 return -EOPNOTSUPP;
1675
1676         ident = g_strdup_printf("%s_%s", host, domain);
1677         provider_dbus_ident(ident);
1678
1679         DBG("ident %s", ident);
1680
1681         provider = vpn_provider_lookup(ident);
1682         if (provider == NULL) {
1683                 provider = vpn_provider_get(ident);
1684                 if (provider == NULL) {
1685                         DBG("can not create provider");
1686                         g_free(ident);
1687                         return -EOPNOTSUPP;
1688                 }
1689
1690                 provider->host = g_strdup(host);
1691                 provider->domain = g_strdup(domain);
1692                 provider->name = g_strdup(name);
1693                 provider->type = g_strdup(type);
1694
1695                 if (provider_register(provider) == 0)
1696                         vpn_provider_load(provider);
1697
1698                 provider_resolv_host_addr(provider);
1699         }
1700
1701         if (networks != NULL) {
1702                 g_slist_free_full(provider->user_networks, free_route);
1703                 provider->user_networks = networks;
1704                 set_user_networks(provider, provider->user_networks);
1705         }
1706
1707         dbus_message_iter_init(msg, &iter);
1708         dbus_message_iter_recurse(&iter, &array);
1709
1710         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1711                 DBusMessageIter entry, value;
1712                 const char *key, *str;
1713
1714                 dbus_message_iter_recurse(&array, &entry);
1715                 dbus_message_iter_get_basic(&entry, &key);
1716
1717                 dbus_message_iter_next(&entry);
1718                 dbus_message_iter_recurse(&entry, &value);
1719
1720                 switch (dbus_message_iter_get_arg_type(&value)) {
1721                 case DBUS_TYPE_STRING:
1722                         dbus_message_iter_get_basic(&value, &str);
1723                         vpn_provider_set_string(provider, key, str);
1724                         break;
1725                 }
1726
1727                 dbus_message_iter_next(&array);
1728         }
1729
1730         g_free(ident);
1731
1732         vpn_provider_save(provider);
1733
1734         err = provider_register(provider);
1735         if (err != 0 && err != -EALREADY)
1736                 return err;
1737
1738         connection_register(provider);
1739
1740         DBG("provider %p index %d path %s", provider, provider->index,
1741                                                         provider->path);
1742
1743         g_dbus_send_reply(connection, msg,
1744                                 DBUS_TYPE_OBJECT_PATH, &provider->path,
1745                                 DBUS_TYPE_INVALID);
1746
1747         connection_added_signal(provider);
1748
1749         return 0;
1750 }
1751
1752 static void append_connection_structs(DBusMessageIter *iter, void *user_data)
1753 {
1754         DBusMessageIter entry;
1755         GHashTableIter hash;
1756         gpointer value, key;
1757
1758         g_hash_table_iter_init(&hash, provider_hash);
1759
1760         while (g_hash_table_iter_next(&hash, &key, &value) == TRUE) {
1761                 struct vpn_provider *provider = value;
1762
1763                 DBG("path %s", provider->path);
1764
1765                 if (provider->identifier == NULL)
1766                         continue;
1767
1768                 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
1769                                 NULL, &entry);
1770                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1771                                 &provider->path);
1772                 append_properties(&entry, provider);
1773                 dbus_message_iter_close_container(iter, &entry);
1774         }
1775 }
1776
1777 DBusMessage *__vpn_provider_get_connections(DBusMessage *msg)
1778 {
1779         DBusMessage *reply;
1780
1781         DBG("");
1782
1783         reply = dbus_message_new_method_return(msg);
1784         if (reply == NULL)
1785                 return NULL;
1786
1787         __connman_dbus_append_objpath_dict_array(reply,
1788                         append_connection_structs, NULL);
1789
1790         return reply;
1791 }
1792
1793 const char * __vpn_provider_get_ident(struct vpn_provider *provider)
1794 {
1795         if (provider == NULL)
1796                 return NULL;
1797
1798         return provider->identifier;
1799 }
1800
1801 int vpn_provider_set_string(struct vpn_provider *provider,
1802                                         const char *key, const char *value)
1803 {
1804         DBG("provider %p key %s value %s", provider, key, value);
1805
1806         if (g_str_equal(key, "Type") == TRUE) {
1807                 g_free(provider->type);
1808                 provider->type = g_strdup(value);
1809         } else if (g_str_equal(key, "Name") == TRUE) {
1810                 g_free(provider->name);
1811                 provider->name = g_strdup(value);
1812         } else if (g_str_equal(key, "Host") == TRUE) {
1813                 g_free(provider->host);
1814                 provider->host = g_strdup(value);
1815         } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
1816                 g_free(provider->domain);
1817                 provider->domain = g_strdup(value);
1818         } else
1819                 g_hash_table_replace(provider->setting_strings,
1820                                 g_strdup(key), g_strdup(value));
1821         return 0;
1822 }
1823
1824 const char *vpn_provider_get_string(struct vpn_provider *provider,
1825                                                         const char *key)
1826 {
1827         DBG("provider %p key %s", provider, key);
1828
1829         if (g_str_equal(key, "Type") == TRUE)
1830                 return provider->type;
1831         else if (g_str_equal(key, "Name") == TRUE)
1832                 return provider->name;
1833         else if (g_str_equal(key, "Host") == TRUE)
1834                 return provider->host;
1835         else if (g_str_equal(key, "HostIP") == TRUE) {
1836                 if (provider->host_ip == NULL ||
1837                                 provider->host_ip[0] == NULL)
1838                         return provider->host;
1839                 else
1840                         return provider->host_ip[0];
1841         } else if (g_str_equal(key, "VPN.Domain") == TRUE)
1842                 return provider->domain;
1843
1844         return g_hash_table_lookup(provider->setting_strings, key);
1845 }
1846
1847 connman_bool_t __vpn_provider_check_routes(struct vpn_provider *provider)
1848 {
1849         if (provider == NULL)
1850                 return FALSE;
1851
1852         if (provider->user_routes != NULL &&
1853                         g_hash_table_size(provider->user_routes) > 0)
1854                 return TRUE;
1855
1856         if (provider->routes != NULL &&
1857                         g_hash_table_size(provider->routes) > 0)
1858                 return TRUE;
1859
1860         return FALSE;
1861 }
1862
1863 void *vpn_provider_get_data(struct vpn_provider *provider)
1864 {
1865         return provider->driver_data;
1866 }
1867
1868 void vpn_provider_set_data(struct vpn_provider *provider, void *data)
1869 {
1870         provider->driver_data = data;
1871 }
1872
1873 void vpn_provider_set_index(struct vpn_provider *provider, int index)
1874 {
1875         DBG("index %d provider %p", index, provider);
1876
1877         if (provider->ipconfig_ipv4 == NULL) {
1878                 provider->ipconfig_ipv4 = __vpn_ipconfig_create(index,
1879                                                                 AF_INET);
1880                 if (provider->ipconfig_ipv4 == NULL) {
1881                         DBG("Couldnt create ipconfig for IPv4");
1882                         goto done;
1883                 }
1884         }
1885
1886         __vpn_ipconfig_set_index(provider->ipconfig_ipv4, index);
1887
1888         if (provider->ipconfig_ipv6 == NULL) {
1889                 provider->ipconfig_ipv6 = __vpn_ipconfig_create(index,
1890                                                                 AF_INET6);
1891                 if (provider->ipconfig_ipv6 == NULL) {
1892                         DBG("Couldnt create ipconfig for IPv6");
1893                         goto done;
1894                 }
1895         }
1896
1897         __vpn_ipconfig_set_index(provider->ipconfig_ipv6, index);
1898
1899 done:
1900         provider->index = index;
1901 }
1902
1903 int vpn_provider_get_index(struct vpn_provider *provider)
1904 {
1905         return provider->index;
1906 }
1907
1908 int vpn_provider_set_ipaddress(struct vpn_provider *provider,
1909                                         struct connman_ipaddress *ipaddress)
1910 {
1911         struct vpn_ipconfig *ipconfig = NULL;
1912
1913         switch (ipaddress->family) {
1914         case AF_INET:
1915                 ipconfig = provider->ipconfig_ipv4;
1916                 break;
1917         case AF_INET6:
1918                 ipconfig = provider->ipconfig_ipv6;
1919                 break;
1920         default:
1921                 break;
1922         }
1923
1924         DBG("provider %p ipconfig %p family %d", provider, ipconfig,
1925                                                         ipaddress->family);
1926
1927         if (ipconfig == NULL)
1928                 return -EINVAL;
1929
1930         provider->family = ipaddress->family;
1931
1932         __vpn_ipconfig_set_local(ipconfig, ipaddress->local);
1933         __vpn_ipconfig_set_peer(ipconfig, ipaddress->peer);
1934         __vpn_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1935         __vpn_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1936         __vpn_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1937
1938         return 0;
1939 }
1940
1941 int vpn_provider_set_pac(struct vpn_provider *provider,
1942                                 const char *pac)
1943 {
1944         DBG("provider %p pac %s", provider, pac);
1945
1946         return 0;
1947 }
1948
1949
1950 int vpn_provider_set_domain(struct vpn_provider *provider,
1951                                         const char *domain)
1952 {
1953         DBG("provider %p domain %s", provider, domain);
1954
1955         g_free(provider->domain);
1956         provider->domain = g_strdup(domain);
1957
1958         return 0;
1959 }
1960
1961 int vpn_provider_set_nameservers(struct vpn_provider *provider,
1962                                         const char *nameservers)
1963 {
1964         DBG("provider %p nameservers %s", provider, nameservers);
1965
1966         g_strfreev(provider->nameservers);
1967         provider->nameservers = NULL;
1968
1969         if (nameservers == NULL)
1970                 return 0;
1971
1972         provider->nameservers = g_strsplit(nameservers, " ", 0);
1973
1974         return 0;
1975 }
1976
1977 enum provider_route_type {
1978         PROVIDER_ROUTE_TYPE_NONE = 0,
1979         PROVIDER_ROUTE_TYPE_MASK = 1,
1980         PROVIDER_ROUTE_TYPE_ADDR = 2,
1981         PROVIDER_ROUTE_TYPE_GW   = 3,
1982 };
1983
1984 static int route_env_parse(struct vpn_provider *provider, const char *key,
1985                                 int *family, unsigned long *idx,
1986                                 enum provider_route_type *type)
1987 {
1988         char *end;
1989         const char *start;
1990
1991         DBG("name %s", provider->name);
1992
1993         if (!strcmp(provider->type, "openvpn")) {
1994                 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1995                         start = key + strlen("route_network_");
1996                         *type = PROVIDER_ROUTE_TYPE_ADDR;
1997                 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1998                         start = key + strlen("route_netmask_");
1999                         *type = PROVIDER_ROUTE_TYPE_MASK;
2000                 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
2001                         start = key + strlen("route_gateway_");
2002                         *type = PROVIDER_ROUTE_TYPE_GW;
2003                 } else
2004                         return -EINVAL;
2005
2006                 *family = AF_INET;
2007                 *idx = g_ascii_strtoull(start, &end, 10);
2008
2009         } else if (!strcmp(provider->type, "openconnect")) {
2010                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
2011                         *family = AF_INET;
2012                         start = key + strlen("CISCO_SPLIT_INC_");
2013                 } else if (g_str_has_prefix(key,
2014                                         "CISCO_IPV6_SPLIT_INC_") == TRUE) {
2015                         *family = AF_INET6;
2016                         start = key + strlen("CISCO_IPV6_SPLIT_INC_");
2017                 } else
2018                         return -EINVAL;
2019
2020                 *idx = g_ascii_strtoull(start, &end, 10);
2021
2022                 if (strncmp(end, "_ADDR", 5) == 0)
2023                         *type = PROVIDER_ROUTE_TYPE_ADDR;
2024                 else if (strncmp(end, "_MASK", 5) == 0)
2025                         *type = PROVIDER_ROUTE_TYPE_MASK;
2026                 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
2027                                 *family == AF_INET6) {
2028                         *type = PROVIDER_ROUTE_TYPE_MASK;
2029                 } else
2030                         return -EINVAL;
2031         }
2032
2033         return 0;
2034 }
2035
2036 int vpn_provider_append_route(struct vpn_provider *provider,
2037                                         const char *key, const char *value)
2038 {
2039         struct vpn_route *route;
2040         int ret, family = 0;
2041         unsigned long idx = 0;
2042         enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
2043
2044         DBG("key %s value %s", key, value);
2045
2046         ret = route_env_parse(provider, key, &family, &idx, &type);
2047         if (ret < 0)
2048                 return ret;
2049
2050         DBG("idx %lu family %d type %d", idx, family, type);
2051
2052         route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
2053         if (route == NULL) {
2054                 route = g_try_new0(struct vpn_route, 1);
2055                 if (route == NULL) {
2056                         connman_error("out of memory");
2057                         return -ENOMEM;
2058                 }
2059
2060                 route->family = family;
2061
2062                 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
2063                                                 route);
2064         }
2065
2066         switch (type) {
2067         case PROVIDER_ROUTE_TYPE_NONE:
2068                 break;
2069         case PROVIDER_ROUTE_TYPE_MASK:
2070                 route->netmask = g_strdup(value);
2071                 break;
2072         case PROVIDER_ROUTE_TYPE_ADDR:
2073                 route->network = g_strdup(value);
2074                 break;
2075         case PROVIDER_ROUTE_TYPE_GW:
2076                 route->gateway = g_strdup(value);
2077                 break;
2078         }
2079
2080         if (handle_routes == FALSE) {
2081                 if (route->netmask != NULL && route->gateway != NULL &&
2082                                                         route->network != NULL)
2083                         provider_schedule_changed(provider,
2084                                                 SERVER_ROUTES_CHANGED);
2085         }
2086
2087         return 0;
2088 }
2089
2090 const char *vpn_provider_get_driver_name(struct vpn_provider *provider)
2091 {
2092         if (provider->driver == NULL)
2093                 return NULL;
2094
2095         return provider->driver->name;
2096 }
2097
2098 const char *vpn_provider_get_save_group(struct vpn_provider *provider)
2099 {
2100         return provider->identifier;
2101 }
2102
2103 static gint compare_priority(gconstpointer a, gconstpointer b)
2104 {
2105         return 0;
2106 }
2107
2108 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
2109 {
2110         struct vpn_provider *provider = value;
2111
2112         if (provider->driver != NULL && provider->driver->remove)
2113                 provider->driver->remove(provider);
2114
2115         connection_unregister(provider);
2116 }
2117
2118 int vpn_provider_driver_register(struct vpn_provider_driver *driver)
2119 {
2120         DBG("driver %p name %s", driver, driver->name);
2121
2122         driver_list = g_slist_insert_sorted(driver_list, driver,
2123                                                         compare_priority);
2124         provider_create_all_from_type(driver->name);
2125         return 0;
2126 }
2127
2128 void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
2129 {
2130         GHashTableIter iter;
2131         gpointer value, key;
2132
2133         DBG("driver %p name %s", driver, driver->name);
2134
2135         driver_list = g_slist_remove(driver_list, driver);
2136
2137         g_hash_table_iter_init(&iter, provider_hash);
2138         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2139                 struct vpn_provider *provider = value;
2140
2141                 if (provider != NULL && provider->driver != NULL &&
2142                                 provider->driver->type == driver->type &&
2143                                 g_strcmp0(provider->driver->name,
2144                                                         driver->name) == 0) {
2145                         provider->driver = NULL;
2146                 }
2147         }
2148 }
2149
2150 static gboolean check_vpn_count(gpointer data)
2151 {
2152         if (configuration_count == 0) {
2153                 connman_info("No VPN configurations found, quitting.");
2154                 raise(SIGTERM);
2155         }
2156
2157         return FALSE;
2158 }
2159
2160 void __vpn_provider_check_connections(void)
2161 {
2162         /*
2163          * If we were started when there is no providers configured,
2164          * then just quit. This happens when connman starts and its
2165          * vpn plugin asks connman-vpnd if it has any connections
2166          * configured. If there are none, then we can stop the vpn
2167          * daemon.
2168          */
2169         g_timeout_add(1000, check_vpn_count, NULL);
2170 }
2171
2172 const char *vpn_provider_get_name(struct vpn_provider *provider)
2173 {
2174         return provider->name;
2175 }
2176
2177 const char *vpn_provider_get_host(struct vpn_provider *provider)
2178 {
2179         return provider->host;
2180 }
2181
2182 const char *vpn_provider_get_path(struct vpn_provider *provider)
2183 {
2184         return provider->path;
2185 }
2186
2187 static int agent_probe(struct connman_agent *agent)
2188 {
2189         DBG("agent %p", agent);
2190         return 0;
2191 }
2192
2193 static void agent_remove(struct connman_agent *agent)
2194 {
2195         DBG("agent %p", agent);
2196 }
2197
2198 static struct connman_agent_driver agent_driver = {
2199         .name           = "vpn",
2200         .interface      = VPN_AGENT_INTERFACE,
2201         .probe          = agent_probe,
2202         .remove         = agent_remove,
2203 };
2204
2205 int __vpn_provider_init(gboolean do_routes)
2206 {
2207         int err;
2208
2209         DBG("");
2210
2211         handle_routes = do_routes;
2212
2213         err = connman_agent_driver_register(&agent_driver);
2214         if (err < 0) {
2215                 connman_error("Cannot register agent driver for %s",
2216                                                 agent_driver.name);
2217                 return err;
2218         }
2219
2220         connection = connman_dbus_get_connection();
2221
2222         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2223                                                 NULL, unregister_provider);
2224
2225         return 0;
2226 }
2227
2228 void __vpn_provider_cleanup(void)
2229 {
2230         DBG("");
2231
2232         g_hash_table_foreach(provider_hash, clean_provider, NULL);
2233
2234         g_hash_table_destroy(provider_hash);
2235         provider_hash = NULL;
2236
2237         connman_agent_driver_unregister(&agent_driver);
2238
2239         dbus_connection_unref(connection);
2240 }