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;
63 static int inotify_wd = -1;
65 static GIOChannel *inotify_channel = NULL;
66 static uint inotify_watch = 0;
68 #define NONFS_CONFIG_NAME "internal"
70 /* Definition of possible strings in the .config files */
71 #define CONFIG_KEY_NAME "Name"
72 #define CONFIG_KEY_DESC "Description"
73 #define CONFIG_KEY_PROT "Protected"
75 #define SERVICE_KEY_TYPE "Type"
76 #define SERVICE_KEY_NAME "Name"
77 #define SERVICE_KEY_SSID "SSID"
78 #define SERVICE_KEY_EAP "EAP"
79 #define SERVICE_KEY_CA_CERT "CACertFile"
80 #define SERVICE_KEY_CL_CERT "ClientCertFile"
81 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
82 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
83 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
84 #define SERVICE_KEY_IDENTITY "Identity"
85 #define SERVICE_KEY_PHASE2 "Phase2"
86 #define SERVICE_KEY_PASSPHRASE "Passphrase"
88 static const char *config_possible_keys[] = {
95 static const char *service_possible_keys[] = {
103 SERVICE_KEY_PRV_KEY_PASS,
104 SERVICE_KEY_PRV_KEY_PASS_TYPE,
105 SERVICE_KEY_IDENTITY,
107 SERVICE_KEY_PASSPHRASE,
111 static void unregister_config(gpointer data)
113 struct connman_config *config = data;
115 connman_info("Removing configuration %s", config->ident);
117 g_hash_table_destroy(config->service_table);
119 g_free(config->description);
120 g_free(config->name);
121 g_free(config->ident);
125 static void unregister_service(gpointer data)
127 struct connman_config_service *service = data;
129 connman_info("Removing service configuration %s", service->ident);
131 g_free(service->ident);
132 g_free(service->type);
133 g_free(service->name);
134 g_free(service->ssid);
135 g_free(service->eap);
136 g_free(service->identity);
137 g_free(service->ca_cert_file);
138 g_free(service->client_cert_file);
139 g_free(service->private_key_file);
140 g_free(service->private_key_passphrase);
141 g_free(service->private_key_passphrase_type);
142 g_free(service->phase2);
143 g_free(service->passphrase);
147 static void check_keys(GKeyFile *keyfile, const char *group,
148 const char **possible_keys)
151 gsize nb_avail_keys, i, j;
153 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
154 if (avail_keys == NULL)
158 * For each key in the configuration file,
159 * verify it is understood by connman
161 for (i = 0 ; i < nb_avail_keys; i++) {
162 for (j = 0; possible_keys[j] ; j++)
163 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
166 if (possible_keys[j] == NULL)
167 connman_warn("Unknown configuration key %s in [%s]",
168 avail_keys[i], group);
171 g_strfreev(avail_keys);
174 static int load_service(GKeyFile *keyfile, const char *group,
175 struct connman_config *config)
177 struct connman_config_service *service;
179 char *str, *hex_ssid;
180 gboolean service_created = FALSE;
182 /* Strip off "service_" prefix */
185 if (strlen(ident) < 1)
188 /* Verify that provided keys are good */
189 check_keys(keyfile, group, service_possible_keys);
191 service = g_hash_table_lookup(config->service_table, ident);
192 if (service == NULL) {
193 service = g_try_new0(struct connman_config_service, 1);
197 service->ident = g_strdup(ident);
199 service_created = TRUE;
202 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
204 g_free(service->type);
208 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
210 g_free(service->name);
214 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
216 if (hex_ssid != NULL) {
218 unsigned int i, j = 0, hex;
219 size_t hex_ssid_len = strlen(hex_ssid);
221 ssid = g_try_malloc0(hex_ssid_len / 2);
227 for (i = 0; i < hex_ssid_len; i += 2) {
228 sscanf(hex_ssid + i, "%02x", &hex);
234 g_free(service->ssid);
235 service->ssid = ssid;
236 service->ssid_len = hex_ssid_len / 2;
237 } else if (service->name != NULL) {
239 unsigned int ssid_len;
241 ssid_len = strlen(service->name);
242 ssid = g_try_malloc0(ssid_len);
246 memcpy(ssid, service->name, ssid_len);
247 g_free(service->ssid);
248 service->ssid = ssid;
249 service->ssid_len = ssid_len;
252 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
254 g_free(service->eap);
258 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
260 g_free(service->ca_cert_file);
261 service->ca_cert_file = str;
264 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
266 g_free(service->client_cert_file);
267 service->client_cert_file = str;
270 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
272 g_free(service->private_key_file);
273 service->private_key_file = str;
276 str = g_key_file_get_string(keyfile, group,
277 SERVICE_KEY_PRV_KEY_PASS, NULL);
279 g_free(service->private_key_passphrase);
280 service->private_key_passphrase = str;
283 str = g_key_file_get_string(keyfile, group,
284 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
286 g_free(service->private_key_passphrase_type);
287 service->private_key_passphrase_type = str;
290 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
292 g_free(service->identity);
293 service->identity = str;
296 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
298 g_free(service->phase2);
299 service->phase2 = str;
302 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
305 g_free(service->passphrase);
306 service->passphrase = str;
309 if (g_strcmp0(config->ident, NONFS_CONFIG_NAME) != 0)
310 service->from_fs = TRUE;
312 service->from_fs = FALSE;
315 g_hash_table_insert(config->service_table, service->ident,
318 connman_info("Adding service configuration %s", service->ident);
323 static int load_config(struct connman_config *config)
332 DBG("config %p", config);
334 keyfile = __connman_storage_open_config(config->ident);
338 /* Verify keys validity of the global section */
339 check_keys(keyfile, "global", config_possible_keys);
341 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
343 g_free(config->name);
347 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
349 g_free(config->description);
350 config->description = str;
353 protected = g_key_file_get_boolean(keyfile, "global",
354 CONFIG_KEY_PROT, NULL);
355 config->protected = protected;
357 groups = g_key_file_get_groups(keyfile, &length);
359 for (i = 0; groups[i] != NULL; i++) {
360 if (g_str_has_prefix(groups[i], "service_") == TRUE)
361 load_service(keyfile, groups[i], config);
366 __connman_storage_close_config(config->ident, keyfile, FALSE);
371 static struct connman_config *create_config(const char *ident)
373 struct connman_config *config;
375 DBG("ident %s", ident);
377 if (g_hash_table_lookup(config_table, ident) != NULL)
380 config = g_try_new0(struct connman_config, 1);
384 config->ident = g_strdup(ident);
386 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
387 NULL, unregister_service);
389 g_hash_table_insert(config_table, config->ident, config);
391 connman_info("Adding configuration %s", config->ident);
396 int __connman_config_load_service(GKeyFile *keyfile, const char *group)
398 struct connman_config *config = g_hash_table_lookup(config_table,
401 if (config == NULL) {
402 config = create_config(NONFS_CONFIG_NAME);
407 return load_service(keyfile, group, config);
410 static int read_configs(void)
416 dir = g_dir_open(STORAGEDIR, 0, NULL);
420 while ((file = g_dir_read_name(dir)) != NULL) {
424 if (g_str_has_suffix(file, ".config") == FALSE)
427 ident = g_strrstr(file, ".config");
431 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
434 str = g_string_new_len(file, ident - file);
438 ident = g_string_free(str, FALSE);
440 if (connman_dbus_validate_ident(ident) == TRUE) {
441 struct connman_config *config;
443 config = create_config(ident);
456 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
464 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
469 status = g_io_channel_read_chars(channel, buffer,
470 sizeof(buffer) -1, &bytes_read, NULL);
473 case G_IO_STATUS_NORMAL:
475 case G_IO_STATUS_AGAIN:
478 connman_error("Reading from inotify channel failed");
485 while (bytes_read > 0) {
486 struct inotify_event *event;
491 event = (struct inotify_event *) next_event;
493 ident = next_event + sizeof(struct inotify_event);
497 len = sizeof(struct inotify_event) + event->len;
499 /* check if inotify_event block fit */
500 if (len > bytes_read)
509 if (g_str_has_suffix(ident, ".config") == FALSE)
512 ext = g_strrstr(ident, ".config");
518 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
521 if (connman_dbus_validate_ident(ident) == FALSE)
524 if (event->mask & IN_CREATE)
525 create_config(ident);
527 if (event->mask & IN_MODIFY) {
528 struct connman_config *config;
530 config = g_hash_table_lookup(config_table, ident);
531 if (config != NULL) {
532 g_hash_table_remove_all(config->service_table);
537 if (event->mask & IN_DELETE)
538 g_hash_table_remove(config_table, ident);
544 static int create_watch(void)
552 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
553 IN_MODIFY | IN_CREATE | IN_DELETE);
554 if (inotify_wd < 0) {
555 connman_error("Creation of STORAGEDIR watch failed");
560 inotify_channel = g_io_channel_unix_new(fd);
561 if (inotify_channel == NULL) {
562 connman_error("Creation of inotify channel failed");
563 inotify_rm_watch(fd, inotify_wd);
570 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
571 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
572 g_io_channel_set_buffered(inotify_channel, FALSE);
574 inotify_watch = g_io_add_watch(inotify_channel,
575 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
581 static void remove_watch(void)
585 if (inotify_channel == NULL)
588 if (inotify_watch > 0) {
589 g_source_remove(inotify_watch);
593 fd = g_io_channel_unix_get_fd(inotify_channel);
595 if (inotify_wd >= 0) {
596 inotify_rm_watch(fd, inotify_wd);
600 g_io_channel_unref(inotify_channel);
603 int __connman_config_init(void)
607 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
608 NULL, unregister_config);
612 return read_configs();
615 void __connman_config_cleanup(void)
621 g_hash_table_destroy(config_table);
625 static char *config_pem_fsid(const char *pem_file)
628 unsigned *fsid = (unsigned *) &buf.f_fsid;
629 unsigned long long fsid64;
631 if (pem_file == NULL)
634 if (statfs(pem_file, &buf) < 0) {
635 connman_error("statfs error %s for %s",
636 strerror(errno), pem_file);
640 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
642 return g_strdup_printf("%llx", fsid64);
645 static void provision_service(gpointer key, gpointer value, gpointer user_data)
647 struct connman_service *service = user_data;
648 struct connman_config_service *config = value;
649 struct connman_network *network;
651 unsigned int ssid_len;
653 /* For now only WiFi service entries are supported */
654 if (g_strcmp0(config->type, "wifi") != 0)
657 network = __connman_service_get_network(service);
658 if (network == NULL) {
659 connman_error("Service has no network set");
663 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
665 connman_error("Network SSID not set");
669 if (config->ssid == NULL || ssid_len != config->ssid_len)
672 if (memcmp(config->ssid, ssid, ssid_len) != 0)
675 /* do not provision immutable services with non-fs originated configs */
676 if (config->from_fs == FALSE &&
677 __connman_service_get_immutable(service) == TRUE)
680 /* only lock services with a config originated from the filesystem */
681 if (config->from_fs == TRUE)
682 __connman_service_set_immutable(service, TRUE);
684 __connman_service_set_favorite(service, TRUE);
686 if (config->eap != NULL)
687 __connman_service_set_string(service, "EAP", config->eap);
689 if (config->identity != NULL)
690 __connman_service_set_string(service, "Identity",
693 if (config->ca_cert_file != NULL)
694 __connman_service_set_string(service, "CACertFile",
695 config->ca_cert_file);
697 if (config->client_cert_file != NULL)
698 __connman_service_set_string(service, "ClientCertFile",
699 config->client_cert_file);
701 if (config->private_key_file != NULL)
702 __connman_service_set_string(service, "PrivateKeyFile",
703 config->private_key_file);
705 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
706 config->private_key_file != NULL) {
709 fsid = config_pem_fsid(config->private_key_file);
713 g_free(config->private_key_passphrase);
714 config->private_key_passphrase = fsid;
717 if (config->private_key_passphrase != NULL) {
718 __connman_service_set_string(service, "PrivateKeyPassphrase",
719 config->private_key_passphrase);
721 * TODO: Support for PEAP with both identity and key passwd.
722 * In that case, we should check if both of them are found
723 * from the config file. If not, we should not set the
724 * service passphrase in order for the UI to request for an
725 * additional passphrase.
729 if (config->phase2 != NULL)
730 __connman_service_set_string(service, "Phase2", config->phase2);
732 if (config->passphrase != NULL)
733 __connman_service_set_string(service, "Passphrase", config->passphrase);
736 int __connman_config_provision_service(struct connman_service *service)
738 enum connman_service_type type;
742 DBG("service %p", service);
744 /* For now only WiFi services are supported */
745 type = connman_service_get_type(service);
746 if (type != CONNMAN_SERVICE_TYPE_WIFI)
749 g_hash_table_iter_init(&iter, config_table);
751 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
752 struct connman_config *config = value;
754 g_hash_table_foreach(config->service_table,
755 provision_service, service);