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>
36 struct connman_config_service {
41 unsigned int ssid_len;
45 char *client_cert_file;
46 char *private_key_file;
47 char *private_key_passphrase;
48 char *private_key_passphrase_type;
51 GSList *service_identifiers;
52 char *config_ident; /* file prefix */
53 char *config_entry; /* entry name */
56 struct connman_config {
60 connman_bool_t protected;
61 GHashTable *service_table;
64 static GHashTable *config_table = NULL;
65 static GSList *protected_services = NULL;
67 static int inotify_wd = -1;
69 static GIOChannel *inotify_channel = NULL;
70 static uint inotify_watch = 0;
71 static connman_bool_t cleanup = FALSE;
73 #define INTERNAL_CONFIG_PREFIX "__internal"
75 /* Definition of possible strings in the .config files */
76 #define CONFIG_KEY_NAME "Name"
77 #define CONFIG_KEY_DESC "Description"
78 #define CONFIG_KEY_PROT "Protected"
80 #define SERVICE_KEY_TYPE "Type"
81 #define SERVICE_KEY_NAME "Name"
82 #define SERVICE_KEY_SSID "SSID"
83 #define SERVICE_KEY_EAP "EAP"
84 #define SERVICE_KEY_CA_CERT "CACertFile"
85 #define SERVICE_KEY_CL_CERT "ClientCertFile"
86 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
87 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
88 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
89 #define SERVICE_KEY_IDENTITY "Identity"
90 #define SERVICE_KEY_PHASE2 "Phase2"
91 #define SERVICE_KEY_PASSPHRASE "Passphrase"
93 static const char *config_possible_keys[] = {
100 static const char *service_possible_keys[] = {
108 SERVICE_KEY_PRV_KEY_PASS,
109 SERVICE_KEY_PRV_KEY_PASS_TYPE,
110 SERVICE_KEY_IDENTITY,
112 SERVICE_KEY_PASSPHRASE,
116 static void unregister_config(gpointer data)
118 struct connman_config *config = data;
120 connman_info("Removing configuration %s", config->ident);
122 g_hash_table_destroy(config->service_table);
124 g_free(config->description);
125 g_free(config->name);
126 g_free(config->ident);
130 static void unregister_service(gpointer data)
132 struct connman_config_service *config_service = data;
133 struct connman_service *service;
140 connman_info("Removing service configuration %s",
141 config_service->ident);
143 protected_services = g_slist_remove(protected_services,
146 for (list = config_service->service_identifiers; list != NULL;
148 service_id = list->data;
150 service = __connman_service_lookup_from_ident(service_id);
151 if (service != NULL) {
152 __connman_service_set_immutable(service, FALSE);
153 __connman_service_remove(service);
156 if (__connman_storage_remove_service(service_id) == FALSE)
157 DBG("Could not remove all files for service %s",
162 g_free(config_service->ident);
163 g_free(config_service->type);
164 g_free(config_service->name);
165 g_free(config_service->ssid);
166 g_free(config_service->eap);
167 g_free(config_service->identity);
168 g_free(config_service->ca_cert_file);
169 g_free(config_service->client_cert_file);
170 g_free(config_service->private_key_file);
171 g_free(config_service->private_key_passphrase);
172 g_free(config_service->private_key_passphrase_type);
173 g_free(config_service->phase2);
174 g_free(config_service->passphrase);
175 g_slist_free_full(config_service->service_identifiers, g_free);
176 g_free(config_service->config_ident);
177 g_free(config_service->config_entry);
178 g_free(config_service);
181 static void check_keys(GKeyFile *keyfile, const char *group,
182 const char **possible_keys)
185 gsize nb_avail_keys, i, j;
187 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
188 if (avail_keys == NULL)
192 * For each key in the configuration file,
193 * verify it is understood by connman
195 for (i = 0 ; i < nb_avail_keys; i++) {
196 for (j = 0; possible_keys[j] ; j++)
197 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
200 if (possible_keys[j] == NULL)
201 connman_warn("Unknown configuration key %s in [%s]",
202 avail_keys[i], group);
205 g_strfreev(avail_keys);
208 static connman_bool_t
209 is_protected_service(struct connman_config_service *service)
213 DBG("ident %s", service->ident);
215 for (list = protected_services; list; list = list->next) {
216 struct connman_config_service *s = list->data;
218 if (g_strcmp0(s->type, service->type) != 0)
221 if (s->ssid == NULL || service->ssid == NULL)
224 if (s->ssid_len != service->ssid_len)
227 if (g_strcmp0(service->type, "wifi") == 0 &&
228 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
236 static int load_service(GKeyFile *keyfile, const char *group,
237 struct connman_config *config)
239 struct connman_config_service *service;
241 char *str, *hex_ssid;
242 gboolean service_created = FALSE;
245 /* Strip off "service_" prefix */
248 if (strlen(ident) < 1)
251 /* Verify that provided keys are good */
252 check_keys(keyfile, group, service_possible_keys);
254 service = g_hash_table_lookup(config->service_table, ident);
255 if (service == NULL) {
256 service = g_try_new0(struct connman_config_service, 1);
260 service->ident = g_strdup(ident);
262 service_created = TRUE;
265 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
267 g_free(service->type);
271 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
273 g_free(service->name);
277 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
279 if (hex_ssid != NULL) {
281 unsigned int i, j = 0, hex;
282 size_t hex_ssid_len = strlen(hex_ssid);
284 ssid = g_try_malloc0(hex_ssid_len / 2);
291 for (i = 0; i < hex_ssid_len; i += 2) {
292 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
293 connman_warn("Invalid SSID %s", hex_ssid);
304 g_free(service->ssid);
305 service->ssid = ssid;
306 service->ssid_len = hex_ssid_len / 2;
307 } else if (service->name != NULL) {
309 unsigned int ssid_len;
311 ssid_len = strlen(service->name);
312 ssid = g_try_malloc0(ssid_len);
318 memcpy(ssid, service->name, ssid_len);
319 g_free(service->ssid);
320 service->ssid = ssid;
321 service->ssid_len = ssid_len;
324 if (is_protected_service(service) == TRUE) {
325 connman_error("Trying to provision a protected service");
330 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
332 g_free(service->eap);
336 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
338 g_free(service->ca_cert_file);
339 service->ca_cert_file = str;
342 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
344 g_free(service->client_cert_file);
345 service->client_cert_file = str;
348 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
350 g_free(service->private_key_file);
351 service->private_key_file = str;
354 str = g_key_file_get_string(keyfile, group,
355 SERVICE_KEY_PRV_KEY_PASS, NULL);
357 g_free(service->private_key_passphrase);
358 service->private_key_passphrase = str;
361 str = g_key_file_get_string(keyfile, group,
362 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
364 g_free(service->private_key_passphrase_type);
365 service->private_key_passphrase_type = str;
368 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
370 g_free(service->identity);
371 service->identity = str;
374 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
376 g_free(service->phase2);
377 service->phase2 = str;
380 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
383 g_free(service->passphrase);
384 service->passphrase = str;
387 service->config_ident = g_strdup(config->ident);
388 service->config_entry = g_strdup_printf("service_%s", service->ident);
391 g_hash_table_insert(config->service_table, service->ident,
394 if (config->protected == TRUE)
396 g_slist_append(protected_services, service);
398 connman_info("Adding service configuration %s", service->ident);
403 if (service_created == TRUE) {
404 g_free(service->ident);
405 g_free(service->type);
406 g_free(service->name);
407 g_free(service->ssid);
414 static int load_config(struct connman_config *config)
417 GError *error = NULL;
421 gboolean protected, found = FALSE;
424 DBG("config %p", config);
426 keyfile = __connman_storage_load_config(config->ident);
430 /* Verify keys validity of the global section */
431 check_keys(keyfile, "global", config_possible_keys);
433 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
435 g_free(config->name);
439 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
441 g_free(config->description);
442 config->description = str;
445 protected = g_key_file_get_boolean(keyfile, "global",
446 CONFIG_KEY_PROT, &error);
448 config->protected = protected;
450 config->protected = TRUE;
451 #if defined TIZEN_EXT
452 g_clear_error(&error);
455 groups = g_key_file_get_groups(keyfile, &length);
457 for (i = 0; groups[i] != NULL; i++) {
458 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
459 if (load_service(keyfile, groups[i], config) == 0)
465 connman_warn("Config file %s/%s.config does not contain any "
466 "configuration that can be provisioned!",
467 STORAGEDIR, config->ident);
471 g_key_file_free(keyfile);
476 static struct connman_config *create_config(const char *ident)
478 struct connman_config *config;
480 DBG("ident %s", ident);
482 if (g_hash_table_lookup(config_table, ident) != NULL)
485 config = g_try_new0(struct connman_config, 1);
489 config->ident = g_strdup(ident);
491 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
492 NULL, unregister_service);
494 g_hash_table_insert(config_table, config->ident, config);
496 connman_info("Adding configuration %s", config->ident);
501 static connman_bool_t validate_ident(const char *ident)
508 for (i = 0; i < strlen(ident); i++)
509 if (g_ascii_isprint(ident[i]) == FALSE)
515 static int read_configs(void)
521 dir = g_dir_open(STORAGEDIR, 0, NULL);
525 while ((file = g_dir_read_name(dir)) != NULL) {
529 if (g_str_has_suffix(file, ".config") == FALSE)
532 ident = g_strrstr(file, ".config");
536 str = g_string_new_len(file, ident - file);
540 ident = g_string_free(str, FALSE);
542 if (validate_ident(ident) == TRUE) {
543 struct connman_config *config;
545 config = create_config(ident);
549 connman_error("Invalid config ident %s", ident);
560 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
568 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
573 status = g_io_channel_read_chars(channel, buffer,
574 sizeof(buffer) -1, &bytes_read, NULL);
577 case G_IO_STATUS_NORMAL:
579 case G_IO_STATUS_AGAIN:
582 connman_error("Reading from inotify channel failed");
589 while (bytes_read > 0) {
590 struct inotify_event *event;
595 event = (struct inotify_event *) next_event;
597 ident = next_event + sizeof(struct inotify_event);
601 len = sizeof(struct inotify_event) + event->len;
603 /* check if inotify_event block fit */
604 if (len > bytes_read)
613 if (g_str_has_suffix(ident, ".config") == FALSE)
616 ext = g_strrstr(ident, ".config");
622 if (validate_ident(ident) == FALSE) {
623 connman_error("Invalid config ident %s", ident);
627 if (event->mask & IN_CREATE)
628 create_config(ident);
630 if (event->mask & IN_MODIFY) {
631 struct connman_config *config;
633 config = g_hash_table_lookup(config_table, ident);
634 if (config != NULL) {
637 g_hash_table_remove_all(config->service_table);
639 ret = __connman_service_provision_changed(ident);
642 * Re-scan the config file for any
645 g_hash_table_remove_all(config->service_table);
647 __connman_service_provision_changed(ident);
649 #if defined TIZEN_EXT
650 __connman_service_auto_connect();
655 if (event->mask & IN_DELETE)
656 g_hash_table_remove(config_table, ident);
662 static int create_watch(void)
670 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
671 IN_MODIFY | IN_CREATE | IN_DELETE);
672 if (inotify_wd < 0) {
673 connman_error("Creation of STORAGEDIR watch failed");
678 inotify_channel = g_io_channel_unix_new(fd);
679 if (inotify_channel == NULL) {
680 connman_error("Creation of inotify channel failed");
681 inotify_rm_watch(fd, inotify_wd);
688 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
689 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
690 g_io_channel_set_buffered(inotify_channel, FALSE);
692 inotify_watch = g_io_add_watch(inotify_channel,
693 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
699 static void remove_watch(void)
703 if (inotify_channel == NULL)
706 if (inotify_watch > 0) {
707 g_source_remove(inotify_watch);
711 fd = g_io_channel_unix_get_fd(inotify_channel);
713 if (inotify_wd >= 0) {
714 inotify_rm_watch(fd, inotify_wd);
718 g_io_channel_unref(inotify_channel);
721 int __connman_config_init(void)
725 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
726 NULL, unregister_config);
730 return read_configs();
733 void __connman_config_cleanup(void)
741 g_hash_table_destroy(config_table);
747 static char *config_pem_fsid(const char *pem_file)
750 unsigned *fsid = (unsigned *) &buf.f_fsid;
751 unsigned long long fsid64;
753 if (pem_file == NULL)
756 if (statfs(pem_file, &buf) < 0) {
757 connman_error("statfs error %s for %s",
758 strerror(errno), pem_file);
762 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
764 return g_strdup_printf("%llx", fsid64);
767 static void provision_service(gpointer key, gpointer value, gpointer user_data)
769 struct connman_service *service = user_data;
770 struct connman_config_service *config = value;
771 struct connman_network *network;
772 const void *ssid, *service_id;
773 unsigned int ssid_len;
775 /* For now only WiFi service entries are supported */
776 if (g_strcmp0(config->type, "wifi") != 0)
779 network = __connman_service_get_network(service);
780 if (network == NULL) {
781 connman_error("Service has no network set");
785 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
787 connman_error("Network SSID not set");
791 if (config->ssid == NULL || ssid_len != config->ssid_len)
794 if (memcmp(config->ssid, ssid, ssid_len) != 0)
797 service_id = __connman_service_get_ident(service);
798 config->service_identifiers =
799 g_slist_prepend(config->service_identifiers,
800 g_strdup(service_id));
802 __connman_service_set_immutable(service, TRUE);
804 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
806 __connman_service_set_config(service, config->config_ident,
807 config->config_entry);
809 if (config->eap != NULL)
810 __connman_service_set_string(service, "EAP", config->eap);
812 if (config->identity != NULL)
813 __connman_service_set_string(service, "Identity",
816 if (config->ca_cert_file != NULL)
817 __connman_service_set_string(service, "CACertFile",
818 config->ca_cert_file);
820 if (config->client_cert_file != NULL)
821 __connman_service_set_string(service, "ClientCertFile",
822 config->client_cert_file);
824 if (config->private_key_file != NULL)
825 __connman_service_set_string(service, "PrivateKeyFile",
826 config->private_key_file);
828 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
829 config->private_key_file != NULL) {
832 fsid = config_pem_fsid(config->private_key_file);
836 g_free(config->private_key_passphrase);
837 config->private_key_passphrase = fsid;
840 if (config->private_key_passphrase != NULL) {
841 __connman_service_set_string(service, "PrivateKeyPassphrase",
842 config->private_key_passphrase);
844 * TODO: Support for PEAP with both identity and key passwd.
845 * In that case, we should check if both of them are found
846 * from the config file. If not, we should not set the
847 * service passphrase in order for the UI to request for an
848 * additional passphrase.
852 if (config->phase2 != NULL)
853 __connman_service_set_string(service, "Phase2", config->phase2);
855 if (config->passphrase != NULL)
856 __connman_service_set_string(service, "Passphrase", config->passphrase);
858 __connman_service_mark_dirty();
860 __connman_service_save(service);
863 int __connman_config_provision_service(struct connman_service *service)
865 enum connman_service_type type;
869 DBG("service %p", service);
871 /* For now only WiFi services are supported */
872 type = connman_service_get_type(service);
873 if (type != CONNMAN_SERVICE_TYPE_WIFI)
876 g_hash_table_iter_init(&iter, config_table);
878 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
879 struct connman_config *config = value;
881 g_hash_table_foreach(config->service_table,
882 provision_service, service);
888 int __connman_config_provision_service_ident(struct connman_service *service,
889 const char *ident, const char *file, const char *entry)
891 enum connman_service_type type;
892 struct connman_config *config;
895 DBG("service %p", service);
897 /* For now only WiFi services are supported */
898 type = connman_service_get_type(service);
899 if (type != CONNMAN_SERVICE_TYPE_WIFI)
902 config = g_hash_table_lookup(config_table, ident);
906 gboolean found = FALSE;
908 g_hash_table_iter_init(&iter, config->service_table);
911 * Check if we need to remove individual service if it
912 * is missing from config file.
914 if (file != NULL && entry != NULL) {
915 while (g_hash_table_iter_next(&iter, &key,
917 struct connman_config_service *config = value;
919 if (g_strcmp0(config->config_ident,
921 g_strcmp0(config->config_entry,
928 DBG("found %d ident %s file %s entry %s", found, ident,
931 if (found == FALSE) {
933 * The entry+8 will skip "service_" prefix
935 g_hash_table_remove(config->service_table,
941 g_hash_table_foreach(config->service_table,
942 provision_service, service);