service: Save/load service hidden status
[platform/upstream/connman.git] / src / provider.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-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
32 #include "connman.h"
33
34 static DBusConnection *connection = NULL;
35
36 static GHashTable *provider_hash = NULL;
37
38 static GSList *driver_list = NULL;
39
40 struct connman_route {
41         int family;
42         char *host;
43         char *netmask;
44         char *gateway;
45 };
46
47 struct connman_provider {
48         int refcount;
49         struct connman_service *vpn_service;
50         int index;
51         char *identifier;
52         char *name;
53         char *type;
54         char *host;
55         char *domain;
56         int family;
57         GHashTable *routes;
58         struct connman_provider_driver *driver;
59         void *driver_data;
60         GHashTable *setting_strings;
61         GHashTable *user_routes;
62         gchar **user_networks;
63         gsize num_user_networks;
64 };
65
66 void __connman_provider_append_properties(struct connman_provider *provider,
67                                                         DBusMessageIter *iter)
68 {
69         if (provider->host != NULL)
70                 connman_dbus_dict_append_basic(iter, "Host",
71                                         DBUS_TYPE_STRING, &provider->host);
72
73         if (provider->domain != NULL)
74                 connman_dbus_dict_append_basic(iter, "Domain",
75                                         DBUS_TYPE_STRING, &provider->domain);
76
77         if (provider->type != NULL)
78                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
79                                                  &provider->type);
80 }
81
82 int __connman_provider_append_user_route(struct connman_provider *provider,
83                         int family, const char *network, const char *netmask)
84 {
85         struct connman_route *route;
86         char *key = g_strdup_printf("%d/%s/%s", family, network, netmask);
87
88         DBG("family %d network %s netmask %s", family, network, netmask);
89
90         route = g_hash_table_lookup(provider->user_routes, key);
91         if (route == NULL) {
92                 route = g_try_new0(struct connman_route, 1);
93                 if (route == NULL) {
94                         connman_error("out of memory");
95                         return -ENOMEM;
96                 }
97
98                 route->family = family;
99                 route->host = g_strdup(network);
100                 route->netmask = g_strdup(netmask);
101
102                 g_hash_table_replace(provider->user_routes, key, route);
103         } else
104                 g_free(key);
105
106         return 0;
107 }
108
109 static void set_user_networks(struct connman_provider *provider,
110                                                         char **networks)
111 {
112         int i = 0;
113
114         while (networks[i] != NULL) {
115                 char **elems = g_strsplit(networks[i], "/", 0);
116                 char *network, *netmask = NULL;
117                 int family = PF_UNSPEC, ret;
118
119                 if (elems == NULL)
120                         break;
121
122                 network = elems[0];
123                 if (elems[1] != NULL)
124                         netmask = elems[1];
125
126                 if (g_strrstr(network, ":") != NULL)
127                         family = AF_INET6;
128                 else if (g_strrstr(network, ".") != NULL) {
129                         family = AF_INET;
130
131                         if (g_strrstr(netmask, ".") == NULL) {
132                                 /* We have netmask length */
133                                 in_addr_t addr;
134                                 struct in_addr netmask_in;
135                                 unsigned char prefix_len = atoi(netmask);
136
137                                 addr = 0xffffffff << (32 - prefix_len);
138                                 netmask_in.s_addr = htonl(addr);
139                                 netmask = inet_ntoa(netmask_in);
140
141                                 DBG("network %s netmask %s", network, netmask);
142                         }
143                 }
144
145                 ret = __connman_provider_append_user_route(provider,
146                                                 family, network, netmask);
147                 g_strfreev(elems);
148
149                 if (ret != 0)
150                         break;
151
152                 i++;
153         }
154 }
155
156 static int provider_load_from_keyfile(struct connman_provider *provider,
157                 GKeyFile *keyfile)
158 {
159         gsize idx = 0;
160         gchar **settings;
161         gchar *key, *value;
162         gsize length;
163
164         settings = g_key_file_get_keys(keyfile, provider->identifier, &length,
165                                 NULL);
166         if (settings == NULL) {
167                 g_key_file_free(keyfile);
168                 return -ENOENT;
169         }
170
171         while (idx < length) {
172                 key = settings[idx];
173                 if (key != NULL) {
174                         if (g_str_equal(key, "Networks") == TRUE) {
175                                 g_strfreev(provider->user_networks);
176                                 provider->user_networks =
177                                         g_key_file_get_string_list(keyfile,
178                                                 provider->identifier,
179                                                 key,
180                                                 &provider->num_user_networks,
181                                                 NULL);
182                         } else {
183                                 value = g_key_file_get_string(keyfile,
184                                                         provider->identifier,
185                                                         key, NULL);
186                                 connman_provider_set_string(provider, key,
187                                                         value);
188                                 g_free(value);
189                         }
190                 }
191                 idx += 1;
192         }
193         g_strfreev(settings);
194
195         if (provider->user_networks != NULL)
196                 set_user_networks(provider, provider->user_networks);
197
198         return 0;
199 }
200
201 static int connman_provider_load(struct connman_provider *provider)
202 {
203         GKeyFile *keyfile;
204
205         DBG("provider %p", provider);
206
207         keyfile = __connman_storage_load_provider(provider->identifier);
208         if (keyfile == NULL)
209                 return -ENOENT;
210
211         provider_load_from_keyfile(provider, keyfile);
212
213         g_key_file_free(keyfile);
214         return 0;
215 }
216
217 static int connman_provider_save(struct connman_provider *provider)
218 {
219         GKeyFile *keyfile;
220
221         DBG("provider %p", provider);
222
223         keyfile = g_key_file_new();
224         if (keyfile == NULL)
225                 return -ENOMEM;
226
227         g_key_file_set_string(keyfile, provider->identifier,
228                         "Name", provider->name);
229         g_key_file_set_string(keyfile, provider->identifier,
230                         "Type", provider->type);
231         g_key_file_set_string(keyfile, provider->identifier,
232                         "Host", provider->host);
233         g_key_file_set_string(keyfile, provider->identifier,
234                         "VPN.Domain", provider->domain);
235         if (provider->user_networks != NULL)
236                 g_key_file_set_string_list(keyfile, provider->identifier,
237                                 "Networks",
238                                 (const gchar **)provider->user_networks,
239                                 provider->num_user_networks);
240
241         if (provider->driver != NULL && provider->driver->save != NULL)
242                 provider->driver->save(provider, keyfile);
243
244         __connman_storage_save_provider(keyfile, provider->identifier);
245         g_key_file_free(keyfile);
246
247         return 0;
248 }
249
250 static struct connman_provider *connman_provider_lookup(const char *identifier)
251 {
252         struct connman_provider *provider = NULL;
253
254         provider = g_hash_table_lookup(provider_hash, identifier);
255
256         return provider;
257 }
258
259 static gboolean match_driver(struct connman_provider *provider,
260                                 struct connman_provider_driver *driver)
261 {
262         if (g_strcmp0(driver->name, provider->type) == 0)
263                 return TRUE;
264
265         return FALSE;
266 }
267
268 static int provider_probe(struct connman_provider *provider)
269 {
270         GSList *list;
271
272         DBG("provider %p name %s", provider, provider->name);
273
274         if (provider->driver != NULL)
275                 return -EALREADY;
276
277         for (list = driver_list; list; list = list->next) {
278                 struct connman_provider_driver *driver = list->data;
279
280                 if (match_driver(provider, driver) == FALSE)
281                         continue;
282
283                 DBG("driver %p name %s", driver, driver->name);
284
285                 if (driver->probe != NULL && driver->probe(provider) == 0) {
286                         provider->driver = driver;
287                         break;
288                 }
289         }
290
291         if (provider->driver == NULL)
292                 return -ENODEV;
293
294         return 0;
295 }
296
297 static void provider_remove(struct connman_provider *provider)
298 {
299         if (provider->driver != NULL) {
300                 provider->driver->remove(provider);
301                 provider->driver = NULL;
302         }
303 }
304
305 static int provider_register(struct connman_provider *provider)
306 {
307         return provider_probe(provider);
308 }
309
310 static void provider_unregister(struct connman_provider *provider)
311 {
312         provider_remove(provider);
313 }
314
315 struct connman_provider *
316 connman_provider_ref_debug(struct connman_provider *provider,
317                         const char *file, int line, const char *caller)
318 {
319         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
320                 file, line, caller);
321
322         __sync_fetch_and_add(&provider->refcount, 1);
323
324         return provider;
325 }
326
327 static void provider_destruct(struct connman_provider *provider)
328 {
329         DBG("provider %p", provider);
330
331         g_free(provider->name);
332         g_free(provider->type);
333         g_free(provider->host);
334         g_free(provider->domain);
335         g_free(provider->identifier);
336         g_strfreev(provider->user_networks);
337         g_hash_table_destroy(provider->routes);
338         g_hash_table_destroy(provider->user_routes);
339         g_hash_table_destroy(provider->setting_strings);
340         g_free(provider);
341 }
342
343 void connman_provider_unref_debug(struct connman_provider *provider,
344                                 const char *file, int line, const char *caller)
345 {
346         DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
347                 file, line, caller);
348
349         if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
350                 return;
351
352         provider_remove(provider);
353
354         provider_destruct(provider);
355 }
356
357 static int provider_indicate_state(struct connman_provider *provider,
358                                         enum connman_service_state state)
359 {
360         DBG("state %d", state);
361
362         __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
363                                         CONNMAN_IPCONFIG_TYPE_IPV4);
364
365         return __connman_service_ipconfig_indicate_state(provider->vpn_service,
366                                         state, CONNMAN_IPCONFIG_TYPE_IPV6);
367 }
368
369 int __connman_provider_disconnect(struct connman_provider *provider)
370 {
371         int err;
372
373         DBG("provider %p", provider);
374
375         if (provider->driver != NULL && provider->driver->disconnect != NULL)
376                 err = provider->driver->disconnect(provider);
377         else
378                 return -EOPNOTSUPP;
379
380         if (provider->vpn_service != NULL)
381                 provider_indicate_state(provider,
382                                         CONNMAN_SERVICE_STATE_DISCONNECT);
383
384         if (err < 0) {
385                 if (err != -EINPROGRESS)
386                         return err;
387
388                 return -EINPROGRESS;
389         }
390
391         return 0;
392 }
393
394 int __connman_provider_connect(struct connman_provider *provider)
395 {
396         int err;
397
398         DBG("provider %p", provider);
399
400         if (provider->driver != NULL && provider->driver->connect != NULL)
401                 err = provider->driver->connect(provider);
402         else
403                 return -EOPNOTSUPP;
404
405         if (err < 0) {
406                 if (err != -EINPROGRESS)
407                         return err;
408
409                 provider_indicate_state(provider,
410                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
411
412                 return -EINPROGRESS;
413         }
414
415         return 0;
416 }
417
418 int __connman_provider_remove(const char *path)
419 {
420         struct connman_provider *provider;
421         GHashTableIter iter;
422         gpointer value, key;
423
424         DBG("path %s", path);
425
426         g_hash_table_iter_init(&iter, provider_hash);
427         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
428                 const char *srv_path;
429                 provider = value;
430
431                 if (provider->vpn_service == NULL)
432                         continue;
433
434                 srv_path = __connman_service_get_path(provider->vpn_service);
435
436                 if (g_strcmp0(srv_path, path) == 0) {
437                         DBG("Removing VPN %s", provider->identifier);
438
439                         provider_unregister(provider);
440                         g_hash_table_remove(provider_hash,
441                                                 provider->identifier);
442                         return 0;
443                 }
444         }
445
446         return -ENXIO;
447 }
448
449 static void provider_append_routes(gpointer key, gpointer value,
450                                         gpointer user_data)
451 {
452         struct connman_route *route = value;
453         struct connman_provider *provider = user_data;
454         int index = provider->index;
455
456         if (route->family == AF_INET6) {
457                 unsigned char prefix_len = atoi(route->netmask);
458
459                 connman_inet_add_ipv6_network_route(index, route->host,
460                                                         route->gateway,
461                                                         prefix_len);
462         } else {
463                 connman_inet_add_network_route(index, route->host,
464                                                 route->gateway,
465                                                 route->netmask);
466         }
467 }
468
469 static int set_connected(struct connman_provider *provider,
470                                         connman_bool_t connected)
471 {
472         struct connman_service *service = provider->vpn_service;
473         struct connman_ipconfig *ipconfig;
474
475         if (service == NULL)
476                 return -ENODEV;
477
478         ipconfig = __connman_service_get_ipconfig(service, provider->family);
479
480         if (connected == TRUE) {
481                 if (ipconfig == NULL) {
482                         provider_indicate_state(provider,
483                                                 CONNMAN_SERVICE_STATE_FAILURE);
484                         return -EIO;
485                 }
486
487                 __connman_ipconfig_address_add(ipconfig);
488                 __connman_ipconfig_gateway_add(ipconfig);
489
490                 provider_indicate_state(provider,
491                                         CONNMAN_SERVICE_STATE_READY);
492
493                 g_hash_table_foreach(provider->routes, provider_append_routes,
494                                         provider);
495
496                 g_hash_table_foreach(provider->user_routes, provider_append_routes,
497                                         provider);
498
499         } else {
500                 if (ipconfig != NULL) {
501                         provider_indicate_state(provider,
502                                         CONNMAN_SERVICE_STATE_DISCONNECT);
503                         __connman_ipconfig_gateway_remove(ipconfig);
504                 }
505
506                 provider_indicate_state(provider,
507                                         CONNMAN_SERVICE_STATE_IDLE);
508         }
509
510         return 0;
511 }
512
513 int connman_provider_set_state(struct connman_provider *provider,
514                                         enum connman_provider_state state)
515 {
516         if (provider == NULL || provider->vpn_service == NULL)
517                 return -EINVAL;
518
519         switch (state) {
520         case CONNMAN_PROVIDER_STATE_UNKNOWN:
521                 return -EINVAL;
522         case CONNMAN_PROVIDER_STATE_IDLE:
523                 return set_connected(provider, FALSE);
524         case CONNMAN_PROVIDER_STATE_CONNECT:
525                 return provider_indicate_state(provider,
526                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
527         case CONNMAN_PROVIDER_STATE_READY:
528                 return set_connected(provider, TRUE);
529         case CONNMAN_PROVIDER_STATE_DISCONNECT:
530                 return provider_indicate_state(provider,
531                                         CONNMAN_SERVICE_STATE_DISCONNECT);
532         case CONNMAN_PROVIDER_STATE_FAILURE:
533                 return provider_indicate_state(provider,
534                                         CONNMAN_SERVICE_STATE_FAILURE);
535         }
536
537         return -EINVAL;
538 }
539
540 int connman_provider_indicate_error(struct connman_provider *provider,
541                                         enum connman_provider_error error)
542 {
543         enum connman_service_error service_error;
544
545         switch (error) {
546         case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
547                 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
548                 break;
549         case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
550                 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
551                 break;
552         case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
553                 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
554                 break;
555         default:
556                 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
557                 break;
558         }
559
560         return __connman_service_indicate_error(provider->vpn_service,
561                                                         service_error);
562 }
563
564 static void unregister_provider(gpointer data)
565 {
566         struct connman_provider *provider = data;
567
568         DBG("provider %p service %p", provider, provider->vpn_service);
569
570         if (provider->vpn_service != NULL) {
571                 connman_service_unref(provider->vpn_service);
572                 provider->vpn_service = NULL;
573         }
574
575         connman_provider_unref(provider);
576 }
577
578 static void destroy_route(gpointer user_data)
579 {
580         struct connman_route *route = user_data;
581
582         g_free(route->host);
583         g_free(route->netmask);
584         g_free(route->gateway);
585         g_free(route);
586 }
587
588 static void provider_initialize(struct connman_provider *provider)
589 {
590         DBG("provider %p", provider);
591
592         provider->index = 0;
593         provider->name = NULL;
594         provider->type = NULL;
595         provider->domain = NULL;
596         provider->identifier = NULL;
597         provider->user_networks = NULL;
598         provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
599                                         NULL, destroy_route);
600         provider->user_routes = g_hash_table_new_full(g_str_hash, g_str_equal,
601                                         g_free, destroy_route);
602         provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
603                                                         g_free, g_free);
604 }
605
606 static struct connman_provider *connman_provider_new(void)
607 {
608         struct connman_provider *provider;
609
610         provider = g_try_new0(struct connman_provider, 1);
611         if (provider == NULL)
612                 return NULL;
613
614         provider->refcount = 1;
615
616         DBG("provider %p", provider);
617         provider_initialize(provider);
618
619         return provider;
620 }
621
622 static struct connman_provider *connman_provider_get(const char *identifier)
623 {
624         struct connman_provider *provider;
625
626         provider = g_hash_table_lookup(provider_hash, identifier);
627         if (provider != NULL)
628                 return provider;
629
630         provider = connman_provider_new();
631         if (provider == NULL)
632                 return NULL;
633
634         DBG("provider %p", provider);
635
636         provider->identifier = g_strdup(identifier);
637
638         g_hash_table_insert(provider_hash, provider->identifier, provider);
639
640         return provider;
641 }
642
643 static void provider_dbus_ident(char *ident)
644 {
645         int i, len = strlen(ident);
646
647         for (i = 0; i < len; i++) {
648                 if (ident[i] >= '0' && ident[i] <= '9')
649                         continue;
650                 if (ident[i] >= 'a' && ident[i] <= 'z')
651                         continue;
652                 if (ident[i] >= 'A' && ident[i] <= 'Z')
653                         continue;
654                 ident[i] = '_';
655         }
656 }
657
658 static struct connman_provider *provider_create_from_keyfile(GKeyFile *keyfile,
659                 const char *ident)
660 {
661         struct connman_provider *provider;
662
663         if (keyfile == NULL || ident == NULL)
664                 return NULL;
665
666         provider = connman_provider_lookup(ident);
667         if (provider == NULL) {
668                 provider = connman_provider_get(ident);
669                 if (provider == NULL) {
670                         DBG("can not create provider");
671                         return NULL;
672                 }
673
674                 provider_load_from_keyfile(provider, keyfile);
675
676                 if (provider->name == NULL || provider->host == NULL ||
677                                 provider->domain == NULL) {
678                         DBG("cannot get name, host or domain");
679                         connman_provider_unref(provider);
680                         return NULL;
681                 }
682
683                 provider_register(provider);
684         }
685         return provider;
686 }
687
688 static int provider_create_service(struct connman_provider *provider)
689 {
690         if (provider->vpn_service != NULL)
691                 return -EALREADY;
692
693         provider->vpn_service =
694                 __connman_service_create_from_provider(provider);
695
696         if (provider->vpn_service == NULL)
697                 return -EOPNOTSUPP;
698
699         return 0;
700 }
701
702 static void provider_create_all_from_type(const char *provider_type)
703 {
704         unsigned int i;
705         char **providers;
706         char *id, *type;
707         GKeyFile *keyfile;
708         struct connman_provider *provider;
709
710         DBG("provider type %s", provider_type);
711
712         providers = __connman_storage_get_providers();
713
714         for (i = 0; providers[i] != NULL; i+=1) {
715
716                 if (strncmp(providers[i], "provider_", 9) != 0)
717                         continue;
718
719                 id = providers[i] + 9;
720                 keyfile = __connman_storage_load_provider(id);
721
722                 if (keyfile == NULL)
723                         continue;
724
725                 type = g_key_file_get_string(keyfile, id, "Type", NULL);
726
727                 DBG("keyfile %p id %s type %s", keyfile, id, type);
728
729                 if (strcmp(provider_type, type) != 0) {
730                         g_free(type);
731                         g_key_file_free(keyfile);
732                         continue;
733                 }
734
735                 provider = provider_create_from_keyfile(keyfile, id);
736                 if (provider != NULL) {
737                         if (provider_create_service(provider) == -EOPNOTSUPP) {
738                                 DBG("could not create service");
739                                 connman_provider_unref(provider);
740                         }
741                 }
742
743                 g_free(type);
744                 g_key_file_free(keyfile);
745         }
746         g_strfreev(providers);
747 }
748
749 static char **get_user_networks(DBusMessageIter *array, int *count)
750 {
751         DBusMessageIter entry;
752         char **networks = NULL;
753         GSList *list = NULL, *l;
754         int len;
755
756         dbus_message_iter_recurse(array, &entry);
757
758         while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
759                 const char *val;
760                 dbus_message_iter_get_basic(&entry, &val);
761
762                 list = g_slist_prepend(list, g_strdup(val));
763                 dbus_message_iter_next(&entry);
764         }
765
766         len = g_slist_length(list);
767         if (len == 0)
768                 goto out;
769
770         networks = g_try_new(char *, len + 1);
771         if (networks == NULL)
772                 goto out;
773
774         *count = len;
775         networks[len] = 0;
776
777         for (l = list; l != NULL; l = g_slist_next(l))
778                 networks[--len] = l->data;
779
780 out:
781         g_slist_free(list);
782
783         return networks;
784 }
785
786 int __connman_provider_create_and_connect(DBusMessage *msg)
787 {
788         struct connman_provider *provider;
789         DBusMessageIter iter, array;
790         const char *type = NULL, *name = NULL, *service_path;
791         const char *host = NULL, *domain = NULL;
792         char **networks = NULL;
793         char *ident;
794         int err, count = 0;
795
796         dbus_message_iter_init(msg, &iter);
797         dbus_message_iter_recurse(&iter, &array);
798
799         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
800                 DBusMessageIter entry, value;
801                 const char *key;
802
803                 dbus_message_iter_recurse(&array, &entry);
804                 dbus_message_iter_get_basic(&entry, &key);
805
806                 dbus_message_iter_next(&entry);
807                 dbus_message_iter_recurse(&entry, &value);
808
809                 switch (dbus_message_iter_get_arg_type(&value)) {
810                 case DBUS_TYPE_STRING:
811                         if (g_str_equal(key, "Type") == TRUE)
812                                 dbus_message_iter_get_basic(&value, &type);
813                         else if (g_str_equal(key, "Name") == TRUE)
814                                 dbus_message_iter_get_basic(&value, &name);
815                         else if (g_str_equal(key, "Host") == TRUE)
816                                 dbus_message_iter_get_basic(&value, &host);
817                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
818                                 dbus_message_iter_get_basic(&value, &domain);
819                         break;
820                 case DBUS_TYPE_ARRAY:
821                         if (g_str_equal(key, "Networks") == TRUE)
822                                 networks = get_user_networks(&value, &count);
823                         break;
824                 }
825
826                 dbus_message_iter_next(&array);
827         }
828
829         if (host == NULL || domain == NULL)
830                 return -EINVAL;
831
832         DBG("Type %s name %s networks %p", type, name, networks);
833
834         if (type == NULL || name == NULL)
835                 return -EOPNOTSUPP;
836
837         ident = g_strdup_printf("%s_%s", host, domain);
838         provider_dbus_ident(ident);
839
840         DBG("ident %s", ident);
841
842         provider = connman_provider_lookup(ident);
843         if (provider == NULL) {
844                 provider = connman_provider_get(ident);
845                 if (provider == NULL) {
846                         DBG("can not create provider");
847                         g_free(ident);
848                         return -EOPNOTSUPP;
849                 }
850
851                 provider->host = g_strdup(host);
852                 provider->domain = g_strdup(domain);
853                 provider->name = g_strdup(name);
854                 provider->type = g_strdup(type);
855
856                 if (provider_register(provider) == 0)
857                         connman_provider_load(provider);
858         }
859
860         if (networks != NULL) {
861                 g_strfreev(provider->user_networks);
862                 provider->user_networks = networks;
863                 provider->num_user_networks = count;
864                 set_user_networks(provider, provider->user_networks);
865         }
866
867         dbus_message_iter_init(msg, &iter);
868         dbus_message_iter_recurse(&iter, &array);
869
870         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
871                 DBusMessageIter entry, value;
872                 const char *key, *str;
873
874                 dbus_message_iter_recurse(&array, &entry);
875                 dbus_message_iter_get_basic(&entry, &key);
876
877                 dbus_message_iter_next(&entry);
878                 dbus_message_iter_recurse(&entry, &value);
879
880                 switch (dbus_message_iter_get_arg_type(&value)) {
881                 case DBUS_TYPE_STRING:
882                         dbus_message_iter_get_basic(&value, &str);
883                         connman_provider_set_string(provider, key, str);
884                         break;
885                 }
886
887                 dbus_message_iter_next(&array);
888         }
889
890         g_free(ident);
891
892         err = provider_create_service(provider);
893         if (err == -EALREADY) {
894                 DBG("provider already connected");
895         } else {
896                 if (err == -EOPNOTSUPP) {
897                         goto unref;
898                 } else {
899                         err = __connman_service_connect(provider->vpn_service);
900
901                         if (err < 0 && err != -EINPROGRESS)
902                                 goto failed;
903                 }
904         }
905
906         connman_provider_save(provider);
907         service_path = __connman_service_get_path(provider->vpn_service);
908         g_dbus_send_reply(connection, msg,
909                                 DBUS_TYPE_OBJECT_PATH, &service_path,
910                                                         DBUS_TYPE_INVALID);
911         return 0;
912
913 failed:
914         connman_service_unref(provider->vpn_service);
915         provider->vpn_service = NULL;
916
917 unref:
918         DBG("can not connect, delete provider");
919
920         g_hash_table_remove(provider_hash, provider->identifier);
921
922         return err;
923 }
924
925 const char * __connman_provider_get_ident(struct connman_provider *provider)
926 {
927         if (provider == NULL)
928                 return NULL;
929
930         return provider->identifier;
931 }
932
933 int connman_provider_set_string(struct connman_provider *provider,
934                                         const char *key, const char *value)
935 {
936         DBG("provider %p key %s value %s", provider, key, value);
937
938         if (g_str_equal(key, "Type") == TRUE) {
939                 g_free(provider->type);
940                 provider->type = g_strdup(value);
941         } else if (g_str_equal(key, "Name") == TRUE) {
942                 g_free(provider->name);
943                 provider->name = g_strdup(value);
944         } else if (g_str_equal(key, "Host") == TRUE) {
945                 g_free(provider->host);
946                 provider->host = g_strdup(value);
947         } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
948                 g_free(provider->domain);
949                 provider->domain = g_strdup(value);
950         } else
951                 g_hash_table_replace(provider->setting_strings,
952                                 g_strdup(key), g_strdup(value));
953         return 0;
954 }
955
956 const char *connman_provider_get_string(struct connman_provider *provider,
957                                                         const char *key)
958 {
959         DBG("provider %p key %s", provider, key);
960
961         if (g_str_equal(key, "Type") == TRUE)
962                 return provider->type;
963         else if (g_str_equal(key, "Name") == TRUE)
964                 return provider->name;
965         else if (g_str_equal(key, "Host") == TRUE)
966                 return provider->host;
967         else if (g_str_equal(key, "VPN.Domain") == TRUE)
968                 return provider->domain;
969
970         return g_hash_table_lookup(provider->setting_strings, key);
971 }
972
973 connman_bool_t
974 __connman_provider_check_routes(struct connman_provider *provider)
975 {
976         if (provider == NULL)
977                 return FALSE;
978
979         if (provider->user_routes != NULL &&
980                         g_hash_table_size(provider->user_routes) > 0)
981                 return TRUE;
982
983         if (provider->routes != NULL &&
984                         g_hash_table_size(provider->routes) > 0)
985                 return TRUE;
986
987         return FALSE;
988 }
989
990 void *connman_provider_get_data(struct connman_provider *provider)
991 {
992         return provider->driver_data;
993 }
994
995 void connman_provider_set_data(struct connman_provider *provider, void *data)
996 {
997         provider->driver_data = data;
998 }
999
1000 void connman_provider_set_index(struct connman_provider *provider, int index)
1001 {
1002         struct connman_service *service = provider->vpn_service;
1003         struct connman_ipconfig *ipconfig;
1004
1005         DBG("");
1006
1007         if (service == NULL)
1008                 return;
1009
1010         ipconfig = __connman_service_get_ip4config(service);
1011
1012         if (ipconfig == NULL) {
1013                 __connman_service_create_ip4config(service, index);
1014
1015                 ipconfig = __connman_service_get_ip4config(service);
1016                 if (ipconfig == NULL) {
1017                         DBG("Couldnt create ipconfig");
1018                         goto done;
1019                 }
1020         }
1021
1022         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
1023         __connman_ipconfig_set_index(ipconfig, index);
1024
1025
1026         ipconfig = __connman_service_get_ip6config(service);
1027
1028         if (ipconfig == NULL) {
1029                 __connman_service_create_ip6config(service, index);
1030
1031                 ipconfig = __connman_service_get_ip6config(service);
1032                 if (ipconfig == NULL) {
1033                         DBG("Couldnt create ipconfig for IPv6");
1034                         goto done;
1035                 }
1036         }
1037
1038         __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
1039         __connman_ipconfig_set_index(ipconfig, index);
1040
1041 done:
1042         provider->index = index;
1043 }
1044
1045 int connman_provider_get_index(struct connman_provider *provider)
1046 {
1047         return provider->index;
1048 }
1049
1050 int connman_provider_set_ipaddress(struct connman_provider *provider,
1051                                         struct connman_ipaddress *ipaddress)
1052 {
1053         struct connman_ipconfig *ipconfig = NULL;
1054
1055         ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
1056                                                         ipaddress->family);
1057         if (ipconfig == NULL)
1058                 return -EINVAL;
1059
1060         provider->family = ipaddress->family;
1061
1062         __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1063         __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1064         __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1065         __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1066         __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1067
1068         return 0;
1069 }
1070
1071 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
1072 {
1073         DBG("provider %p pac %s", provider, pac);
1074
1075         __connman_service_set_pac(provider->vpn_service, pac);
1076
1077         return 0;
1078 }
1079
1080
1081 int connman_provider_set_domain(struct connman_provider *provider,
1082                                         const char *domain)
1083 {
1084         DBG("provider %p domain %s", provider, domain);
1085
1086         g_free(provider->domain);
1087         provider->domain = g_strdup(domain);
1088
1089         __connman_service_set_domainname(provider->vpn_service, domain);
1090
1091         return 0;
1092 }
1093
1094 int connman_provider_set_nameservers(struct connman_provider *provider,
1095                                         const char *nameservers)
1096 {
1097         int i;
1098         char **nameservers_array = NULL;
1099
1100         DBG("provider %p nameservers %s", provider, nameservers);
1101
1102         __connman_service_nameserver_clear(provider->vpn_service);
1103
1104         if (nameservers == NULL)
1105                 return 0;
1106
1107         nameservers_array = g_strsplit(nameservers, " ", 0);
1108
1109         for (i = 0; nameservers_array[i] != NULL; i++) {
1110                 __connman_service_nameserver_append(provider->vpn_service,
1111                                                 nameservers_array[i], FALSE);
1112         }
1113
1114         g_strfreev(nameservers_array);
1115
1116         return 0;
1117 }
1118
1119 enum provider_route_type {
1120         PROVIDER_ROUTE_TYPE_NONE = 0,
1121         PROVIDER_ROUTE_TYPE_MASK = 1,
1122         PROVIDER_ROUTE_TYPE_ADDR = 2,
1123         PROVIDER_ROUTE_TYPE_GW   = 3,
1124 };
1125
1126 static int route_env_parse(struct connman_provider *provider, const char *key,
1127                                 int *family, unsigned long *idx,
1128                                 enum provider_route_type *type)
1129 {
1130         char *end;
1131         const char *start;
1132
1133         DBG("name %s", provider->name);
1134
1135         if (!strcmp(provider->type, "openvpn")) {
1136                 if (g_str_has_prefix(key, "route_network_") == TRUE) {
1137                         start = key + strlen("route_network_");
1138                         *type = PROVIDER_ROUTE_TYPE_ADDR;
1139                 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
1140                         start = key + strlen("route_netmask_");
1141                         *type = PROVIDER_ROUTE_TYPE_MASK;
1142                 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
1143                         start = key + strlen("route_gateway_");
1144                         *type = PROVIDER_ROUTE_TYPE_GW;
1145                 } else
1146                         return -EINVAL;
1147
1148                 *family = AF_INET;
1149                 *idx = g_ascii_strtoull(start, &end, 10);
1150
1151         } else if (!strcmp(provider->type, "openconnect")) {
1152                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
1153                         *family = AF_INET;
1154                         start = key + strlen("CISCO_SPLIT_INC_");
1155                 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
1156                         *family = AF_INET6;
1157                         start = key + strlen("CISCO_IPV6_SPLIT_INC_");
1158                 } else
1159                         return -EINVAL;
1160
1161                 *idx = g_ascii_strtoull(start, &end, 10);
1162
1163                 if (strncmp(end, "_ADDR", 5) == 0)
1164                         *type = PROVIDER_ROUTE_TYPE_ADDR;
1165                 else if (strncmp(end, "_MASK", 5) == 0)
1166                         *type = PROVIDER_ROUTE_TYPE_MASK;
1167                 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
1168                                 *family == AF_INET6) {
1169                         *type = PROVIDER_ROUTE_TYPE_MASK;
1170                 } else
1171                         return -EINVAL;
1172         }
1173
1174         return 0;
1175 }
1176
1177 int connman_provider_append_route(struct connman_provider *provider,
1178                                         const char *key, const char *value)
1179 {
1180         struct connman_route *route;
1181         int ret, family = 0;
1182         unsigned long idx = 0;
1183         enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
1184
1185         DBG("key %s value %s", key, value);
1186
1187         ret = route_env_parse(provider, key, &family, &idx, &type);
1188         if (ret < 0)
1189                 return ret;
1190
1191         DBG("idx %lu family %d type %d", idx, family, type);
1192
1193         route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
1194         if (route == NULL) {
1195                 route = g_try_new0(struct connman_route, 1);
1196                 if (route == NULL) {
1197                         connman_error("out of memory");
1198                         return -ENOMEM;
1199                 }
1200
1201                 route->family = family;
1202
1203                 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
1204                                                 route);
1205         }
1206
1207         switch (type) {
1208         case PROVIDER_ROUTE_TYPE_NONE:
1209                 break;
1210         case PROVIDER_ROUTE_TYPE_MASK:
1211                 route->netmask = g_strdup(value);
1212                 break;
1213         case PROVIDER_ROUTE_TYPE_ADDR:
1214                 route->host = g_strdup(value);
1215                 break;
1216         case PROVIDER_ROUTE_TYPE_GW:
1217                 route->gateway = g_strdup(value);
1218                 break;
1219         }
1220
1221         return 0;
1222 }
1223
1224 const char *connman_provider_get_driver_name(struct connman_provider *provider)
1225 {
1226         if (provider->driver == NULL)
1227                 return NULL;
1228
1229         return provider->driver->name;
1230 }
1231
1232 const char *connman_provider_get_save_group(struct connman_provider *provider)
1233 {
1234         return provider->identifier;
1235 }
1236
1237 static gint compare_priority(gconstpointer a, gconstpointer b)
1238 {
1239         return 0;
1240 }
1241
1242 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
1243 {
1244         struct connman_provider *provider = value;
1245
1246         if (provider->driver != NULL && provider->driver->remove)
1247                 provider->driver->remove(provider);
1248 }
1249
1250 int connman_provider_driver_register(struct connman_provider_driver *driver)
1251 {
1252         DBG("driver %p name %s", driver, driver->name);
1253
1254         driver_list = g_slist_insert_sorted(driver_list, driver,
1255                                                         compare_priority);
1256         provider_create_all_from_type(driver->name);
1257         return 0;
1258 }
1259
1260 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
1261 {
1262         DBG("driver %p name %s", driver, driver->name);
1263
1264         driver_list = g_slist_remove(driver_list, driver);
1265 }
1266
1267 static void provider_remove_all(gpointer key, gpointer value,
1268                                                 gpointer user_data)
1269 {
1270         struct connman_provider *provider = value;
1271
1272         __connman_provider_remove(provider->identifier);
1273 }
1274
1275 static void provider_offline_mode(connman_bool_t enabled)
1276 {
1277         DBG("enabled %d", enabled);
1278
1279         if (enabled == TRUE)
1280                 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
1281
1282 }
1283
1284 static struct connman_provider *provider_get(int index)
1285 {
1286         GHashTableIter iter;
1287         gpointer value, key;
1288
1289         g_hash_table_iter_init(&iter, provider_hash);
1290
1291         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1292                 struct connman_provider *provider = value;
1293
1294                 if (provider->index == index)
1295                         return provider;
1296         }
1297
1298         return NULL;
1299 }
1300
1301 static void provider_service_changed(struct connman_service *service,
1302                                 enum connman_service_state state)
1303 {
1304         struct connman_provider *provider;
1305         int vpn_index, service_index;
1306
1307         if (service == NULL)
1308                 return;
1309
1310         switch (state) {
1311         case CONNMAN_SERVICE_STATE_UNKNOWN:
1312         case CONNMAN_SERVICE_STATE_IDLE:
1313         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1314         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1315         case CONNMAN_SERVICE_STATE_READY:
1316         case CONNMAN_SERVICE_STATE_ONLINE:
1317                 return;
1318         case CONNMAN_SERVICE_STATE_DISCONNECT:
1319         case CONNMAN_SERVICE_STATE_FAILURE:
1320                 break;
1321         }
1322
1323         service_index = __connman_service_get_index(service);
1324
1325         vpn_index = __connman_connection_get_vpn_index(service_index);
1326
1327         DBG("service %p %s state %d index %d/%d", service,
1328                 __connman_service_get_ident(service),
1329                 state, service_index, vpn_index);
1330
1331         if (vpn_index < 0)
1332                 return;
1333
1334         provider = provider_get(vpn_index);
1335         if (provider == NULL)
1336                 return;
1337
1338         DBG("disconnect %p index %d", provider, vpn_index);
1339
1340         __connman_provider_disconnect(provider);
1341
1342         return;
1343 }
1344
1345 static struct connman_notifier provider_notifier = {
1346         .name                   = "provider",
1347         .offline_mode           = provider_offline_mode,
1348         .service_state_changed  = provider_service_changed,
1349 };
1350
1351 int __connman_provider_init(void)
1352 {
1353         int err;
1354
1355         DBG("");
1356
1357         connection = connman_dbus_get_connection();
1358
1359         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1360                                                 NULL, unregister_provider);
1361
1362         err = connman_notifier_register(&provider_notifier);
1363         if (err < 0) {
1364                 g_hash_table_destroy(provider_hash);
1365                 dbus_connection_unref(connection);
1366         }
1367
1368         return err;
1369 }
1370
1371 void __connman_provider_cleanup(void)
1372 {
1373         DBG("");
1374
1375         connman_notifier_unregister(&provider_notifier);
1376
1377         g_hash_table_foreach(provider_hash, clean_provider, NULL);
1378
1379         g_hash_table_destroy(provider_hash);
1380         provider_hash = NULL;
1381
1382         dbus_connection_unref(connection);
1383 }