5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <sys/inotify.h>
34 #include <connman/provision.h>
37 struct connman_config_service {
42 unsigned int ssid_len;
46 char *client_cert_file;
47 char *private_key_file;
48 char *private_key_passphrase;
49 char *private_key_passphrase_type;
52 GSList *service_identifiers;
53 char *config_ident; /* file prefix */
54 char *config_entry; /* entry name */
55 connman_bool_t hidden;
58 struct connman_config {
62 connman_bool_t protected;
63 GHashTable *service_table;
66 static GHashTable *config_table = NULL;
67 static GSList *protected_services = NULL;
69 static connman_bool_t cleanup = FALSE;
71 /* Definition of possible strings in the .config files */
72 #define CONFIG_KEY_NAME "Name"
73 #define CONFIG_KEY_DESC "Description"
74 #define CONFIG_KEY_PROT "Protected"
76 #define SERVICE_KEY_TYPE "Type"
77 #define SERVICE_KEY_NAME "Name"
78 #define SERVICE_KEY_SSID "SSID"
79 #define SERVICE_KEY_EAP "EAP"
80 #define SERVICE_KEY_CA_CERT "CACertFile"
81 #define SERVICE_KEY_CL_CERT "ClientCertFile"
82 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
83 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
84 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
85 #define SERVICE_KEY_IDENTITY "Identity"
86 #define SERVICE_KEY_PHASE2 "Phase2"
87 #define SERVICE_KEY_PASSPHRASE "Passphrase"
88 #define SERVICE_KEY_HIDDEN "Hidden"
90 static const char *config_possible_keys[] = {
97 static const char *service_possible_keys[] = {
105 SERVICE_KEY_PRV_KEY_PASS,
106 SERVICE_KEY_PRV_KEY_PASS_TYPE,
107 SERVICE_KEY_IDENTITY,
109 SERVICE_KEY_PASSPHRASE,
114 static void unregister_config(gpointer data)
116 struct connman_config *config = data;
118 connman_info("Removing configuration %s", config->ident);
120 g_hash_table_destroy(config->service_table);
122 g_free(config->description);
123 g_free(config->name);
124 g_free(config->ident);
128 static void unregister_service(gpointer data)
130 struct connman_config_service *config_service = data;
131 struct connman_service *service;
138 connman_info("Removing service configuration %s",
139 config_service->ident);
141 protected_services = g_slist_remove(protected_services,
144 for (list = config_service->service_identifiers; list != NULL;
146 service_id = list->data;
148 service = __connman_service_lookup_from_ident(service_id);
149 if (service != NULL) {
150 __connman_service_set_immutable(service, FALSE);
151 __connman_service_remove(service);
154 if (__connman_storage_remove_service(service_id) == FALSE)
155 DBG("Could not remove all files for service %s",
160 g_free(config_service->ident);
161 g_free(config_service->type);
162 g_free(config_service->name);
163 g_free(config_service->ssid);
164 g_free(config_service->eap);
165 g_free(config_service->identity);
166 g_free(config_service->ca_cert_file);
167 g_free(config_service->client_cert_file);
168 g_free(config_service->private_key_file);
169 g_free(config_service->private_key_passphrase);
170 g_free(config_service->private_key_passphrase_type);
171 g_free(config_service->phase2);
172 g_free(config_service->passphrase);
173 g_slist_free_full(config_service->service_identifiers, g_free);
174 g_free(config_service->config_ident);
175 g_free(config_service->config_entry);
176 g_free(config_service);
179 static void check_keys(GKeyFile *keyfile, const char *group,
180 const char **possible_keys)
183 gsize nb_avail_keys, i, j;
185 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
186 if (avail_keys == NULL)
190 * For each key in the configuration file,
191 * verify it is understood by connman
193 for (i = 0 ; i < nb_avail_keys; i++) {
194 for (j = 0; possible_keys[j] ; j++)
195 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
198 if (possible_keys[j] == NULL)
199 connman_warn("Unknown configuration key %s in [%s]",
200 avail_keys[i], group);
203 g_strfreev(avail_keys);
206 static connman_bool_t
207 is_protected_service(struct connman_config_service *service)
211 DBG("ident %s", service->ident);
213 for (list = protected_services; list; list = list->next) {
214 struct connman_config_service *s = list->data;
216 if (g_strcmp0(s->type, service->type) != 0)
219 if (s->ssid == NULL || service->ssid == NULL)
222 if (s->ssid_len != service->ssid_len)
225 if (g_strcmp0(service->type, "wifi") == 0 &&
226 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
234 static int load_service(GKeyFile *keyfile, const char *group,
235 struct connman_config *config)
237 struct connman_config_service *service;
239 char *str, *hex_ssid;
240 gboolean service_created = FALSE;
243 /* Strip off "service_" prefix */
246 if (strlen(ident) < 1)
249 /* Verify that provided keys are good */
250 check_keys(keyfile, group, service_possible_keys);
252 service = g_hash_table_lookup(config->service_table, ident);
253 if (service == NULL) {
254 service = g_try_new0(struct connman_config_service, 1);
258 service->ident = g_strdup(ident);
260 service_created = TRUE;
263 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
265 g_free(service->type);
269 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
271 g_free(service->name);
275 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
277 if (hex_ssid != NULL) {
279 unsigned int i, j = 0, hex;
280 size_t hex_ssid_len = strlen(hex_ssid);
282 ssid = g_try_malloc0(hex_ssid_len / 2);
289 for (i = 0; i < hex_ssid_len; i += 2) {
290 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
291 connman_warn("Invalid SSID %s", hex_ssid);
302 g_free(service->ssid);
303 service->ssid = ssid;
304 service->ssid_len = hex_ssid_len / 2;
305 } else if (service->name != NULL) {
307 unsigned int ssid_len;
309 ssid_len = strlen(service->name);
310 ssid = g_try_malloc0(ssid_len);
316 memcpy(ssid, service->name, ssid_len);
317 g_free(service->ssid);
318 service->ssid = ssid;
319 service->ssid_len = ssid_len;
322 if (is_protected_service(service) == TRUE) {
323 connman_error("Trying to provision a protected service");
328 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
330 g_free(service->eap);
334 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
336 g_free(service->ca_cert_file);
337 service->ca_cert_file = str;
340 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
342 g_free(service->client_cert_file);
343 service->client_cert_file = str;
346 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
348 g_free(service->private_key_file);
349 service->private_key_file = str;
352 str = g_key_file_get_string(keyfile, group,
353 SERVICE_KEY_PRV_KEY_PASS, NULL);
355 g_free(service->private_key_passphrase);
356 service->private_key_passphrase = str;
359 str = g_key_file_get_string(keyfile, group,
360 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
362 g_free(service->private_key_passphrase_type);
363 service->private_key_passphrase_type = str;
366 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
368 g_free(service->identity);
369 service->identity = str;
372 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
374 g_free(service->phase2);
375 service->phase2 = str;
378 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
381 g_free(service->passphrase);
382 service->passphrase = str;
385 service->config_ident = g_strdup(config->ident);
386 service->config_entry = g_strdup_printf("service_%s", service->ident);
388 service->hidden = g_key_file_get_boolean(keyfile, group,
389 SERVICE_KEY_HIDDEN, NULL);
392 g_hash_table_insert(config->service_table, service->ident,
395 if (config->protected == TRUE)
397 g_slist_prepend(protected_services, service);
399 connman_info("Adding service configuration %s", service->ident);
404 if (service_created == TRUE) {
405 g_free(service->ident);
406 g_free(service->type);
407 g_free(service->name);
408 g_free(service->ssid);
415 static int load_config(struct connman_config *config)
418 GError *error = NULL;
422 gboolean protected, found = FALSE;
425 DBG("config %p", config);
427 keyfile = __connman_storage_load_config(config->ident);
431 /* Verify keys validity of the global section */
432 check_keys(keyfile, "global", config_possible_keys);
434 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
436 g_free(config->name);
440 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
442 g_free(config->description);
443 config->description = str;
446 protected = g_key_file_get_boolean(keyfile, "global",
447 CONFIG_KEY_PROT, &error);
449 config->protected = protected;
451 config->protected = TRUE;
452 g_clear_error(&error);
454 groups = g_key_file_get_groups(keyfile, &length);
456 for (i = 0; groups[i] != NULL; i++) {
457 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
458 if (load_service(keyfile, groups[i], config) == 0)
464 connman_warn("Config file %s/%s.config does not contain any "
465 "configuration that can be provisioned!",
466 STORAGEDIR, config->ident);
470 g_key_file_free(keyfile);
475 static struct connman_config *create_config(const char *ident)
477 struct connman_config *config;
479 DBG("ident %s", ident);
481 if (g_hash_table_lookup(config_table, ident) != NULL)
484 config = g_try_new0(struct connman_config, 1);
488 config->ident = g_strdup(ident);
490 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
491 NULL, unregister_service);
493 g_hash_table_insert(config_table, config->ident, config);
495 connman_info("Adding configuration %s", config->ident);
500 static connman_bool_t validate_ident(const char *ident)
507 for (i = 0; i < strlen(ident); i++)
508 if (g_ascii_isprint(ident[i]) == FALSE)
514 static int read_configs(void)
520 dir = g_dir_open(STORAGEDIR, 0, NULL);
524 while ((file = g_dir_read_name(dir)) != NULL) {
528 if (g_str_has_suffix(file, ".config") == FALSE)
531 ident = g_strrstr(file, ".config");
535 str = g_string_new_len(file, ident - file);
539 ident = g_string_free(str, FALSE);
541 if (validate_ident(ident) == TRUE) {
542 struct connman_config *config;
544 config = create_config(ident);
548 connman_error("Invalid config ident %s", ident);
559 static void config_notify_handler(struct inotify_event *event,
567 if (g_str_has_suffix(ident, ".config") == FALSE)
570 ext = g_strrstr(ident, ".config");
576 if (validate_ident(ident) == FALSE) {
577 connman_error("Invalid config ident %s", ident);
581 if (event->mask & IN_CREATE)
582 create_config(ident);
584 if (event->mask & IN_MODIFY) {
585 struct connman_config *config;
587 config = g_hash_table_lookup(config_table, ident);
588 if (config != NULL) {
591 g_hash_table_remove_all(config->service_table);
593 ret = __connman_service_provision_changed(ident);
596 * Re-scan the config file for any
599 g_hash_table_remove_all(config->service_table);
601 __connman_service_provision_changed(ident);
606 if (event->mask & IN_DELETE)
607 g_hash_table_remove(config_table, ident);
610 int __connman_config_init(void)
614 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
615 NULL, unregister_config);
617 connman_inotify_register(STORAGEDIR, config_notify_handler);
619 return read_configs();
622 void __connman_config_cleanup(void)
628 connman_inotify_unregister(STORAGEDIR, config_notify_handler);
630 g_hash_table_destroy(config_table);
636 static char *config_pem_fsid(const char *pem_file)
639 unsigned *fsid = (unsigned *) &buf.f_fsid;
640 unsigned long long fsid64;
642 if (pem_file == NULL)
645 if (statfs(pem_file, &buf) < 0) {
646 connman_error("statfs error %s for %s",
647 strerror(errno), pem_file);
651 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
653 return g_strdup_printf("%llx", fsid64);
656 static void provision_service(gpointer key, gpointer value, gpointer user_data)
658 struct connman_service *service = user_data;
659 struct connman_config_service *config = value;
660 struct connman_network *network;
661 const void *ssid, *service_id;
662 unsigned int ssid_len;
664 /* For now only WiFi service entries are supported */
665 if (g_strcmp0(config->type, "wifi") != 0)
668 network = __connman_service_get_network(service);
669 if (network == NULL) {
670 connman_error("Service has no network set");
674 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
676 connman_error("Network SSID not set");
680 if (config->ssid == NULL || ssid_len != config->ssid_len)
683 if (memcmp(config->ssid, ssid, ssid_len) != 0)
686 service_id = __connman_service_get_ident(service);
687 config->service_identifiers =
688 g_slist_prepend(config->service_identifiers,
689 g_strdup(service_id));
691 __connman_service_set_immutable(service, TRUE);
693 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
695 __connman_service_set_config(service, config->config_ident,
696 config->config_entry);
698 if (config->eap != NULL)
699 __connman_service_set_string(service, "EAP", config->eap);
701 if (config->identity != NULL)
702 __connman_service_set_string(service, "Identity",
705 if (config->ca_cert_file != NULL)
706 __connman_service_set_string(service, "CACertFile",
707 config->ca_cert_file);
709 if (config->client_cert_file != NULL)
710 __connman_service_set_string(service, "ClientCertFile",
711 config->client_cert_file);
713 if (config->private_key_file != NULL)
714 __connman_service_set_string(service, "PrivateKeyFile",
715 config->private_key_file);
717 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
718 config->private_key_file != NULL) {
721 fsid = config_pem_fsid(config->private_key_file);
725 g_free(config->private_key_passphrase);
726 config->private_key_passphrase = fsid;
729 if (config->private_key_passphrase != NULL) {
730 __connman_service_set_string(service, "PrivateKeyPassphrase",
731 config->private_key_passphrase);
733 * TODO: Support for PEAP with both identity and key passwd.
734 * In that case, we should check if both of them are found
735 * from the config file. If not, we should not set the
736 * service passphrase in order for the UI to request for an
737 * additional passphrase.
741 if (config->phase2 != NULL)
742 __connman_service_set_string(service, "Phase2", config->phase2);
744 if (config->passphrase != NULL)
745 __connman_service_set_string(service, "Passphrase", config->passphrase);
747 if (config->hidden == TRUE)
748 __connman_service_set_hidden(service);
750 __connman_service_mark_dirty();
752 __connman_service_save(service);
755 int __connman_config_provision_service(struct connman_service *service)
757 enum connman_service_type type;
761 DBG("service %p", service);
763 /* For now only WiFi services are supported */
764 type = connman_service_get_type(service);
765 if (type != CONNMAN_SERVICE_TYPE_WIFI)
768 g_hash_table_iter_init(&iter, config_table);
770 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
771 struct connman_config *config = value;
773 g_hash_table_foreach(config->service_table,
774 provision_service, service);
780 int __connman_config_provision_service_ident(struct connman_service *service,
781 const char *ident, const char *file, const char *entry)
783 enum connman_service_type type;
784 struct connman_config *config;
787 DBG("service %p", service);
789 /* For now only WiFi services are supported */
790 type = connman_service_get_type(service);
791 if (type != CONNMAN_SERVICE_TYPE_WIFI)
794 config = g_hash_table_lookup(config_table, ident);
795 if (config != NULL) {
798 gboolean found = FALSE;
800 g_hash_table_iter_init(&iter, config->service_table);
803 * Check if we need to remove individual service if it
804 * is missing from config file.
806 if (file != NULL && entry != NULL) {
807 while (g_hash_table_iter_next(&iter, &key,
809 struct connman_config_service *config_service;
811 config_service = value;
813 if (g_strcmp0(config_service->config_ident,
817 if (g_strcmp0(config_service->config_entry,
825 DBG("found %d ident %s file %s entry %s", found, ident,
828 if (found == FALSE) {
830 * The entry+8 will skip "service_" prefix
832 g_hash_table_remove(config->service_table,
838 g_hash_table_foreach(config->service_table,
839 provision_service, service);
845 struct connman_config_entry **connman_config_get_entries(const char *type)
847 GHashTableIter iter_file, iter_config;
849 struct connman_config_entry **entries = NULL;
852 g_hash_table_iter_init(&iter_file, config_table);
853 while (g_hash_table_iter_next(&iter_file, &key, &value) == TRUE) {
854 struct connman_config *config_file = value;
856 count = g_hash_table_size(config_file->service_table);
858 entries = g_try_realloc(entries, (i + count + 1) *
859 sizeof(struct connman_config_entry *));
863 g_hash_table_iter_init(&iter_config,
864 config_file->service_table);
865 while (g_hash_table_iter_next(&iter_config, &key,
867 struct connman_config_service *config = value;
870 g_strcmp0(config->type, type) != 0)
873 entries[i] = g_try_new0(struct connman_config_entry,
875 if (entries[i] == NULL)
878 entries[i]->ident = g_strdup(config->ident);
879 entries[i]->name = g_strdup(config->name);
880 entries[i]->ssid = g_try_malloc0(config->ssid_len + 1);
881 if (entries[i]->ssid == NULL)
884 memcpy(entries[i]->ssid, config->ssid,
886 entries[i]->ssid_len = config->ssid_len;
887 entries[i]->hidden = config->hidden;
893 if (entries != NULL) {
894 entries = g_try_realloc(entries, (i + 1) *
895 sizeof(struct connman_config_entry *));
901 DBG("%d provisioned AP found", i);
907 connman_config_free_entries(entries);
911 void connman_config_free_entries(struct connman_config_entry **entries)
918 for (i = 0; entries[i]; i++) {
919 g_free(entries[i]->ident);
920 g_free(entries[i]->name);
921 g_free(entries[i]->ssid);