5 * Copyright (C) 2007-2010 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
30 #include <sys/inotify.h>
35 struct connman_config_service {
40 unsigned int ssid_len;
44 char *client_cert_file;
45 char *private_key_file;
46 char *private_key_passphrase;
47 char *private_key_passphrase_type;
50 connman_bool_t from_fs;
53 struct connman_config {
57 connman_bool_t protected;
58 GHashTable *service_table;
61 static GHashTable *config_table = NULL;
62 static GSList *protected_services = NULL;
64 static int inotify_wd = -1;
66 static GIOChannel *inotify_channel = NULL;
67 static uint inotify_watch = 0;
69 #define NONFS_CONFIG_NAME "internal"
70 #define INTERNAL_CONFIG_PREFIX "__internal"
72 /* Definition of possible strings in the .config files */
73 #define CONFIG_KEY_NAME "Name"
74 #define CONFIG_KEY_DESC "Description"
75 #define CONFIG_KEY_PROT "Protected"
77 #define SERVICE_KEY_TYPE "Type"
78 #define SERVICE_KEY_NAME "Name"
79 #define SERVICE_KEY_SSID "SSID"
80 #define SERVICE_KEY_EAP "EAP"
81 #define SERVICE_KEY_CA_CERT "CACertFile"
82 #define SERVICE_KEY_CL_CERT "ClientCertFile"
83 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
84 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
85 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
86 #define SERVICE_KEY_IDENTITY "Identity"
87 #define SERVICE_KEY_PHASE2 "Phase2"
88 #define SERVICE_KEY_PASSPHRASE "Passphrase"
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,
113 static void unregister_config(gpointer data)
115 struct connman_config *config = data;
117 connman_info("Removing configuration %s", config->ident);
119 g_hash_table_destroy(config->service_table);
121 g_free(config->description);
122 g_free(config->name);
123 g_free(config->ident);
127 static void unregister_service(gpointer data)
129 struct connman_config_service *service = data;
131 connman_info("Removing service configuration %s", service->ident);
133 protected_services = g_slist_remove(protected_services, service);
135 g_free(service->ident);
136 g_free(service->type);
137 g_free(service->name);
138 g_free(service->ssid);
139 g_free(service->eap);
140 g_free(service->identity);
141 g_free(service->ca_cert_file);
142 g_free(service->client_cert_file);
143 g_free(service->private_key_file);
144 g_free(service->private_key_passphrase);
145 g_free(service->private_key_passphrase_type);
146 g_free(service->phase2);
147 g_free(service->passphrase);
151 static void check_keys(GKeyFile *keyfile, const char *group,
152 const char **possible_keys)
155 gsize nb_avail_keys, i, j;
157 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
158 if (avail_keys == NULL)
162 * For each key in the configuration file,
163 * verify it is understood by connman
165 for (i = 0 ; i < nb_avail_keys; i++) {
166 for (j = 0; possible_keys[j] ; j++)
167 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
170 if (possible_keys[j] == NULL)
171 connman_warn("Unknown configuration key %s in [%s]",
172 avail_keys[i], group);
175 g_strfreev(avail_keys);
178 static connman_bool_t
179 is_protected_service(struct connman_config_service *service)
183 DBG("ident %s", service->ident);
185 for (list = protected_services; list; list = list->next) {
186 struct connman_config_service *s = list->data;
188 if (g_strcmp0(s->type, service->type) != 0)
191 if (s->ssid == NULL || service->ssid == NULL)
194 if (s->ssid_len != service->ssid_len)
197 if (g_strcmp0(service->type, "wifi") == 0 &&
198 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
206 static int load_service(GKeyFile *keyfile, const char *group,
207 struct connman_config *config)
209 struct connman_config_service *service;
211 char *str, *hex_ssid;
212 gboolean service_created = FALSE;
215 /* Strip off "service_" prefix */
218 if (strlen(ident) < 1)
221 /* Verify that provided keys are good */
222 check_keys(keyfile, group, service_possible_keys);
224 service = g_hash_table_lookup(config->service_table, ident);
225 if (service == NULL) {
226 service = g_try_new0(struct connman_config_service, 1);
230 service->ident = g_strdup(ident);
232 service_created = TRUE;
235 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
237 g_free(service->type);
241 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
243 g_free(service->name);
247 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
249 if (hex_ssid != NULL) {
251 unsigned int i, j = 0, hex;
252 size_t hex_ssid_len = strlen(hex_ssid);
254 ssid = g_try_malloc0(hex_ssid_len / 2);
261 for (i = 0; i < hex_ssid_len; i += 2) {
262 sscanf(hex_ssid + i, "%02x", &hex);
268 g_free(service->ssid);
269 service->ssid = ssid;
270 service->ssid_len = hex_ssid_len / 2;
271 } else if (service->name != NULL) {
273 unsigned int ssid_len;
275 ssid_len = strlen(service->name);
276 ssid = g_try_malloc0(ssid_len);
282 memcpy(ssid, service->name, ssid_len);
283 g_free(service->ssid);
284 service->ssid = ssid;
285 service->ssid_len = ssid_len;
288 if (is_protected_service(service) == TRUE) {
289 connman_error("Trying to provision a protected service");
294 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
296 g_free(service->eap);
300 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
302 g_free(service->ca_cert_file);
303 service->ca_cert_file = str;
306 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
308 g_free(service->client_cert_file);
309 service->client_cert_file = str;
312 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
314 g_free(service->private_key_file);
315 service->private_key_file = str;
318 str = g_key_file_get_string(keyfile, group,
319 SERVICE_KEY_PRV_KEY_PASS, NULL);
321 g_free(service->private_key_passphrase);
322 service->private_key_passphrase = str;
325 str = g_key_file_get_string(keyfile, group,
326 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
328 g_free(service->private_key_passphrase_type);
329 service->private_key_passphrase_type = str;
332 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
334 g_free(service->identity);
335 service->identity = str;
338 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
340 g_free(service->phase2);
341 service->phase2 = str;
344 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
347 g_free(service->passphrase);
348 service->passphrase = str;
351 if (g_strcmp0(config->ident, NONFS_CONFIG_NAME) != 0)
352 service->from_fs = TRUE;
354 service->from_fs = FALSE;
357 g_hash_table_insert(config->service_table, service->ident,
360 if (config->protected == TRUE)
362 g_slist_append(protected_services, service);
364 connman_info("Adding service configuration %s", service->ident);
369 if (service_created == TRUE) {
370 g_free(service->ident);
371 g_free(service->type);
372 g_free(service->name);
373 g_free(service->ssid);
380 static int load_config(struct connman_config *config)
389 DBG("config %p", config);
391 keyfile = __connman_storage_open_config(config->ident);
395 /* Verify keys validity of the global section */
396 check_keys(keyfile, "global", config_possible_keys);
398 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
400 g_free(config->name);
404 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
406 g_free(config->description);
407 config->description = str;
410 protected = g_key_file_get_boolean(keyfile, "global",
411 CONFIG_KEY_PROT, NULL);
412 config->protected = protected;
414 groups = g_key_file_get_groups(keyfile, &length);
416 for (i = 0; groups[i] != NULL; i++) {
417 if (g_str_has_prefix(groups[i], "service_") == TRUE)
418 load_service(keyfile, groups[i], config);
423 __connman_storage_close_config(config->ident, keyfile, FALSE);
428 static struct connman_config *create_config(const char *ident)
430 struct connman_config *config;
432 DBG("ident %s", ident);
434 if (g_hash_table_lookup(config_table, ident) != NULL)
437 config = g_try_new0(struct connman_config, 1);
441 config->ident = g_strdup(ident);
443 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
444 NULL, unregister_service);
446 g_hash_table_insert(config_table, config->ident, config);
448 connman_info("Adding configuration %s", config->ident);
453 int __connman_config_load_service(GKeyFile *keyfile, const char *group,
454 connman_bool_t persistent)
456 struct connman_config *config;
457 const char *service_name;
458 char *ident, *filename = NULL, *content = NULL;
459 gsize content_length;
462 service_name = group + strlen("service_");
463 ident = g_strdup_printf("%s_%s", INTERNAL_CONFIG_PREFIX, service_name);
467 DBG("ident %s", ident);
469 config = g_hash_table_lookup(config_table, ident);
470 if (config == NULL) {
471 config = create_config(ident);
472 if (config == NULL) {
477 config->protected = FALSE;
480 err = load_service(keyfile, group, config);
481 if (persistent == FALSE || err < 0)
484 g_key_file_set_string(keyfile, "global", CONFIG_KEY_NAME,
486 g_key_file_set_string(keyfile, "global", CONFIG_KEY_DESC,
487 "Internal Config File");
489 content = g_key_file_to_data(keyfile, &content_length, NULL);
490 if (content == NULL) {
495 filename = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
496 if (filename == NULL) {
501 DBG("Saving %d bytes to %s", content_length, filename);
503 if (g_file_set_contents(filename, content,
504 content_length, NULL) == FALSE) {
519 static int read_configs(void)
525 dir = g_dir_open(STORAGEDIR, 0, NULL);
529 while ((file = g_dir_read_name(dir)) != NULL) {
533 if (g_str_has_suffix(file, ".config") == FALSE)
536 ident = g_strrstr(file, ".config");
540 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
543 str = g_string_new_len(file, ident - file);
547 ident = g_string_free(str, FALSE);
549 if (connman_dbus_validate_ident(ident) == TRUE) {
550 struct connman_config *config;
552 config = create_config(ident);
565 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
573 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
578 status = g_io_channel_read_chars(channel, buffer,
579 sizeof(buffer) -1, &bytes_read, NULL);
582 case G_IO_STATUS_NORMAL:
584 case G_IO_STATUS_AGAIN:
587 connman_error("Reading from inotify channel failed");
594 while (bytes_read > 0) {
595 struct inotify_event *event;
600 event = (struct inotify_event *) next_event;
602 ident = next_event + sizeof(struct inotify_event);
606 len = sizeof(struct inotify_event) + event->len;
608 /* check if inotify_event block fit */
609 if (len > bytes_read)
618 if (g_str_has_suffix(ident, ".config") == FALSE)
621 ext = g_strrstr(ident, ".config");
627 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
630 if (connman_dbus_validate_ident(ident) == FALSE)
633 if (event->mask & IN_CREATE)
634 create_config(ident);
636 if (event->mask & IN_MODIFY) {
637 struct connman_config *config;
639 config = g_hash_table_lookup(config_table, ident);
640 if (config != NULL) {
641 g_hash_table_remove_all(config->service_table);
646 if (event->mask & IN_DELETE)
647 g_hash_table_remove(config_table, ident);
653 static int create_watch(void)
661 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
662 IN_MODIFY | IN_CREATE | IN_DELETE);
663 if (inotify_wd < 0) {
664 connman_error("Creation of STORAGEDIR watch failed");
669 inotify_channel = g_io_channel_unix_new(fd);
670 if (inotify_channel == NULL) {
671 connman_error("Creation of inotify channel failed");
672 inotify_rm_watch(fd, inotify_wd);
679 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
680 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
681 g_io_channel_set_buffered(inotify_channel, FALSE);
683 inotify_watch = g_io_add_watch(inotify_channel,
684 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
690 static void remove_watch(void)
694 if (inotify_channel == NULL)
697 if (inotify_watch > 0) {
698 g_source_remove(inotify_watch);
702 fd = g_io_channel_unix_get_fd(inotify_channel);
704 if (inotify_wd >= 0) {
705 inotify_rm_watch(fd, inotify_wd);
709 g_io_channel_unref(inotify_channel);
712 int __connman_config_init(void)
716 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
717 NULL, unregister_config);
721 return read_configs();
724 void __connman_config_cleanup(void)
730 g_hash_table_destroy(config_table);
734 static char *config_pem_fsid(const char *pem_file)
737 unsigned *fsid = (unsigned *) &buf.f_fsid;
738 unsigned long long fsid64;
740 if (pem_file == NULL)
743 if (statfs(pem_file, &buf) < 0) {
744 connman_error("statfs error %s for %s",
745 strerror(errno), pem_file);
749 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
751 return g_strdup_printf("%llx", fsid64);
754 static void provision_service(gpointer key, gpointer value, gpointer user_data)
756 struct connman_service *service = user_data;
757 struct connman_config_service *config = value;
758 struct connman_network *network;
760 unsigned int ssid_len;
762 /* For now only WiFi service entries are supported */
763 if (g_strcmp0(config->type, "wifi") != 0)
766 network = __connman_service_get_network(service);
767 if (network == NULL) {
768 connman_error("Service has no network set");
772 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
774 connman_error("Network SSID not set");
778 if (config->ssid == NULL || ssid_len != config->ssid_len)
781 if (memcmp(config->ssid, ssid, ssid_len) != 0)
784 /* do not provision immutable services with non-fs originated configs */
785 if (config->from_fs == FALSE &&
786 __connman_service_get_immutable(service) == TRUE)
789 /* only lock services with a config originated from the filesystem */
790 if (config->from_fs == TRUE)
791 __connman_service_set_immutable(service, TRUE);
793 __connman_service_set_favorite(service, TRUE);
795 if (config->eap != NULL)
796 __connman_service_set_string(service, "EAP", config->eap);
798 if (config->identity != NULL)
799 __connman_service_set_string(service, "Identity",
802 if (config->ca_cert_file != NULL)
803 __connman_service_set_string(service, "CACertFile",
804 config->ca_cert_file);
806 if (config->client_cert_file != NULL)
807 __connman_service_set_string(service, "ClientCertFile",
808 config->client_cert_file);
810 if (config->private_key_file != NULL)
811 __connman_service_set_string(service, "PrivateKeyFile",
812 config->private_key_file);
814 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
815 config->private_key_file != NULL) {
818 fsid = config_pem_fsid(config->private_key_file);
822 g_free(config->private_key_passphrase);
823 config->private_key_passphrase = fsid;
826 if (config->private_key_passphrase != NULL) {
827 __connman_service_set_string(service, "PrivateKeyPassphrase",
828 config->private_key_passphrase);
830 * TODO: Support for PEAP with both identity and key passwd.
831 * In that case, we should check if both of them are found
832 * from the config file. If not, we should not set the
833 * service passphrase in order for the UI to request for an
834 * additional passphrase.
838 if (config->phase2 != NULL)
839 __connman_service_set_string(service, "Phase2", config->phase2);
841 if (config->passphrase != NULL)
842 __connman_service_set_string(service, "Passphrase", config->passphrase);
845 int __connman_config_provision_service(struct connman_service *service)
847 enum connman_service_type type;
851 DBG("service %p", service);
853 /* For now only WiFi services are supported */
854 type = connman_service_get_type(service);
855 if (type != CONNMAN_SERVICE_TYPE_WIFI)
858 g_hash_table_iter_init(&iter, config_table);
860 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
861 struct connman_config *config = value;
863 g_hash_table_foreach(config->service_table,
864 provision_service, service);