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;
452 groups = g_key_file_get_groups(keyfile, &length);
454 for (i = 0; groups[i] != NULL; i++) {
455 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
456 if (load_service(keyfile, groups[i], config) == 0)
462 connman_warn("Config file %s/%s.config does not contain any "
463 "configuration that can be provisioned!",
464 STORAGEDIR, config->ident);
468 g_key_file_free(keyfile);
473 static struct connman_config *create_config(const char *ident)
475 struct connman_config *config;
477 DBG("ident %s", ident);
479 if (g_hash_table_lookup(config_table, ident) != NULL)
482 config = g_try_new0(struct connman_config, 1);
486 config->ident = g_strdup(ident);
488 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
489 NULL, unregister_service);
491 g_hash_table_insert(config_table, config->ident, config);
493 connman_info("Adding configuration %s", config->ident);
498 static connman_bool_t validate_ident(const char *ident)
505 for (i = 0; i < strlen(ident); i++)
506 if (g_ascii_isprint(ident[i]) == FALSE)
512 static int read_configs(void)
518 dir = g_dir_open(STORAGEDIR, 0, NULL);
522 while ((file = g_dir_read_name(dir)) != NULL) {
526 if (g_str_has_suffix(file, ".config") == FALSE)
529 ident = g_strrstr(file, ".config");
533 str = g_string_new_len(file, ident - file);
537 ident = g_string_free(str, FALSE);
539 if (validate_ident(ident) == TRUE) {
540 struct connman_config *config;
542 config = create_config(ident);
546 connman_error("Invalid config ident %s", ident);
557 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
565 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
570 status = g_io_channel_read_chars(channel, buffer,
571 sizeof(buffer) -1, &bytes_read, NULL);
574 case G_IO_STATUS_NORMAL:
576 case G_IO_STATUS_AGAIN:
579 connman_error("Reading from inotify channel failed");
586 while (bytes_read > 0) {
587 struct inotify_event *event;
592 event = (struct inotify_event *) next_event;
594 ident = next_event + sizeof(struct inotify_event);
598 len = sizeof(struct inotify_event) + event->len;
600 /* check if inotify_event block fit */
601 if (len > bytes_read)
610 if (g_str_has_suffix(ident, ".config") == FALSE)
613 ext = g_strrstr(ident, ".config");
619 if (validate_ident(ident) == FALSE) {
620 connman_error("Invalid config ident %s", ident);
624 if (event->mask & IN_CREATE)
625 create_config(ident);
627 if (event->mask & IN_MODIFY) {
628 struct connman_config *config;
630 config = g_hash_table_lookup(config_table, ident);
631 if (config != NULL) {
634 g_hash_table_remove_all(config->service_table);
636 ret = __connman_service_provision_changed(ident);
639 * Re-scan the config file for any
642 g_hash_table_remove_all(config->service_table);
644 __connman_service_provision_changed(ident);
649 if (event->mask & IN_DELETE)
650 g_hash_table_remove(config_table, ident);
656 static int create_watch(void)
664 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
665 IN_MODIFY | IN_CREATE | IN_DELETE);
666 if (inotify_wd < 0) {
667 connman_error("Creation of STORAGEDIR watch failed");
672 inotify_channel = g_io_channel_unix_new(fd);
673 if (inotify_channel == NULL) {
674 connman_error("Creation of inotify channel failed");
675 inotify_rm_watch(fd, inotify_wd);
682 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
683 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
684 g_io_channel_set_buffered(inotify_channel, FALSE);
686 inotify_watch = g_io_add_watch(inotify_channel,
687 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
693 static void remove_watch(void)
697 if (inotify_channel == NULL)
700 if (inotify_watch > 0) {
701 g_source_remove(inotify_watch);
705 fd = g_io_channel_unix_get_fd(inotify_channel);
707 if (inotify_wd >= 0) {
708 inotify_rm_watch(fd, inotify_wd);
712 g_io_channel_unref(inotify_channel);
715 int __connman_config_init(void)
719 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
720 NULL, unregister_config);
724 return read_configs();
727 void __connman_config_cleanup(void)
735 g_hash_table_destroy(config_table);
741 static char *config_pem_fsid(const char *pem_file)
744 unsigned *fsid = (unsigned *) &buf.f_fsid;
745 unsigned long long fsid64;
747 if (pem_file == NULL)
750 if (statfs(pem_file, &buf) < 0) {
751 connman_error("statfs error %s for %s",
752 strerror(errno), pem_file);
756 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
758 return g_strdup_printf("%llx", fsid64);
761 static void provision_service(gpointer key, gpointer value, gpointer user_data)
763 struct connman_service *service = user_data;
764 struct connman_config_service *config = value;
765 struct connman_network *network;
766 const void *ssid, *service_id;
767 unsigned int ssid_len;
769 /* For now only WiFi service entries are supported */
770 if (g_strcmp0(config->type, "wifi") != 0)
773 network = __connman_service_get_network(service);
774 if (network == NULL) {
775 connman_error("Service has no network set");
779 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
781 connman_error("Network SSID not set");
785 if (config->ssid == NULL || ssid_len != config->ssid_len)
788 if (memcmp(config->ssid, ssid, ssid_len) != 0)
791 service_id = __connman_service_get_ident(service);
792 config->service_identifiers =
793 g_slist_prepend(config->service_identifiers,
794 g_strdup(service_id));
796 __connman_service_set_immutable(service, TRUE);
798 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
800 __connman_service_set_config(service, config->config_ident,
801 config->config_entry);
803 if (config->eap != NULL)
804 __connman_service_set_string(service, "EAP", config->eap);
806 if (config->identity != NULL)
807 __connman_service_set_string(service, "Identity",
810 if (config->ca_cert_file != NULL)
811 __connman_service_set_string(service, "CACertFile",
812 config->ca_cert_file);
814 if (config->client_cert_file != NULL)
815 __connman_service_set_string(service, "ClientCertFile",
816 config->client_cert_file);
818 if (config->private_key_file != NULL)
819 __connman_service_set_string(service, "PrivateKeyFile",
820 config->private_key_file);
822 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
823 config->private_key_file != NULL) {
826 fsid = config_pem_fsid(config->private_key_file);
830 g_free(config->private_key_passphrase);
831 config->private_key_passphrase = fsid;
834 if (config->private_key_passphrase != NULL) {
835 __connman_service_set_string(service, "PrivateKeyPassphrase",
836 config->private_key_passphrase);
838 * TODO: Support for PEAP with both identity and key passwd.
839 * In that case, we should check if both of them are found
840 * from the config file. If not, we should not set the
841 * service passphrase in order for the UI to request for an
842 * additional passphrase.
846 if (config->phase2 != NULL)
847 __connman_service_set_string(service, "Phase2", config->phase2);
849 if (config->passphrase != NULL)
850 __connman_service_set_string(service, "Passphrase", config->passphrase);
852 __connman_service_mark_dirty();
854 __connman_service_save(service);
857 int __connman_config_provision_service(struct connman_service *service)
859 enum connman_service_type type;
863 DBG("service %p", service);
865 /* For now only WiFi services are supported */
866 type = connman_service_get_type(service);
867 if (type != CONNMAN_SERVICE_TYPE_WIFI)
870 g_hash_table_iter_init(&iter, config_table);
872 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
873 struct connman_config *config = value;
875 g_hash_table_foreach(config->service_table,
876 provision_service, service);
882 int __connman_config_provision_service_ident(struct connman_service *service,
883 const char *ident, const char *file, const char *entry)
885 enum connman_service_type type;
886 struct connman_config *config;
889 DBG("service %p", service);
891 /* For now only WiFi services are supported */
892 type = connman_service_get_type(service);
893 if (type != CONNMAN_SERVICE_TYPE_WIFI)
896 config = g_hash_table_lookup(config_table, ident);
900 gboolean found = FALSE;
902 g_hash_table_iter_init(&iter, config->service_table);
905 * Check if we need to remove individual service if it
906 * is missing from config file.
908 if (file != NULL && entry != NULL) {
909 while (g_hash_table_iter_next(&iter, &key,
911 struct connman_config_service *config = value;
913 if (g_strcmp0(config->config_ident,
915 g_strcmp0(config->config_entry,
922 DBG("found %d ident %s file %s entry %s", found, ident,
925 if (found == FALSE) {
927 * The entry+8 will skip "service_" prefix
929 g_hash_table_remove(config->service_table,
935 g_hash_table_foreach(config->service_table,
936 provision_service, service);