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 GHashTable *service_table;
60 static GHashTable *config_table = NULL;
62 static int inotify_wd = -1;
64 static GIOChannel *inotify_channel = NULL;
65 static uint inotify_watch = 0;
67 #define NONFS_CONFIG_NAME "internal"
69 /* Definition of possible strings in the .config files */
70 #define CONFIG_KEY_NAME "Name"
71 #define CONFIG_KEY_DESC "Description"
73 #define SERVICE_KEY_TYPE "Type"
74 #define SERVICE_KEY_NAME "Name"
75 #define SERVICE_KEY_SSID "SSID"
76 #define SERVICE_KEY_EAP "EAP"
77 #define SERVICE_KEY_CA_CERT "CACertFile"
78 #define SERVICE_KEY_CL_CERT "ClientCertFile"
79 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
80 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
81 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
82 #define SERVICE_KEY_IDENTITY "Identity"
83 #define SERVICE_KEY_PHASE2 "Phase2"
84 #define SERVICE_KEY_PASSPHRASE "Passphrase"
86 static const char *config_possible_keys[] = {
92 static const char *service_possible_keys[] = {
100 SERVICE_KEY_PRV_KEY_PASS,
101 SERVICE_KEY_PRV_KEY_PASS_TYPE,
102 SERVICE_KEY_IDENTITY,
104 SERVICE_KEY_PASSPHRASE,
108 static void unregister_config(gpointer data)
110 struct connman_config *config = data;
112 connman_info("Removing configuration %s", config->ident);
114 g_hash_table_destroy(config->service_table);
116 g_free(config->description);
117 g_free(config->name);
118 g_free(config->ident);
122 static void unregister_service(gpointer data)
124 struct connman_config_service *service = data;
126 connman_info("Removing service configuration %s", service->ident);
128 g_free(service->ident);
129 g_free(service->type);
130 g_free(service->name);
131 g_free(service->ssid);
132 g_free(service->eap);
133 g_free(service->identity);
134 g_free(service->ca_cert_file);
135 g_free(service->client_cert_file);
136 g_free(service->private_key_file);
137 g_free(service->private_key_passphrase);
138 g_free(service->private_key_passphrase_type);
139 g_free(service->phase2);
140 g_free(service->passphrase);
144 static void check_keys(GKeyFile *keyfile, const char *group,
145 const char **possible_keys)
148 gsize nb_avail_keys, i, j;
150 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
151 if (avail_keys == NULL)
155 * For each key in the configuration file,
156 * verify it is understood by connman
158 for (i = 0 ; i < nb_avail_keys; i++) {
159 for (j = 0; possible_keys[j] ; j++)
160 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
163 if (possible_keys[j] == NULL)
164 connman_warn("Unknown configuration key %s in [%s]",
165 avail_keys[i], group);
168 g_strfreev(avail_keys);
171 static int load_service(GKeyFile *keyfile, const char *group,
172 struct connman_config *config)
174 struct connman_config_service *service;
176 char *str, *hex_ssid;
177 gboolean service_created = FALSE;
179 /* Strip off "service_" prefix */
182 if (strlen(ident) < 1)
185 /* Verify that provided keys are good */
186 check_keys(keyfile, group, service_possible_keys);
188 service = g_hash_table_lookup(config->service_table, ident);
189 if (service == NULL) {
190 service = g_try_new0(struct connman_config_service, 1);
194 service->ident = g_strdup(ident);
196 service_created = TRUE;
199 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
201 g_free(service->type);
205 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
207 g_free(service->name);
211 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
213 if (hex_ssid != NULL) {
215 unsigned int i, j = 0, hex;
216 size_t hex_ssid_len = strlen(hex_ssid);
218 ssid = g_try_malloc0(hex_ssid_len / 2);
224 for (i = 0; i < hex_ssid_len; i += 2) {
225 sscanf(hex_ssid + i, "%02x", &hex);
231 g_free(service->ssid);
232 service->ssid = ssid;
233 service->ssid_len = hex_ssid_len / 2;
234 } else if (service->name != NULL) {
236 unsigned int ssid_len;
238 ssid_len = strlen(service->name);
239 ssid = g_try_malloc0(ssid_len);
243 memcpy(ssid, service->name, ssid_len);
244 g_free(service->ssid);
245 service->ssid = ssid;
246 service->ssid_len = ssid_len;
249 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
251 g_free(service->eap);
255 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
257 g_free(service->ca_cert_file);
258 service->ca_cert_file = str;
261 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
263 g_free(service->client_cert_file);
264 service->client_cert_file = str;
267 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
269 g_free(service->private_key_file);
270 service->private_key_file = str;
273 str = g_key_file_get_string(keyfile, group,
274 SERVICE_KEY_PRV_KEY_PASS, NULL);
276 g_free(service->private_key_passphrase);
277 service->private_key_passphrase = str;
280 str = g_key_file_get_string(keyfile, group,
281 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
283 g_free(service->private_key_passphrase_type);
284 service->private_key_passphrase_type = str;
287 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
289 g_free(service->identity);
290 service->identity = str;
293 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
295 g_free(service->phase2);
296 service->phase2 = str;
299 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
302 g_free(service->passphrase);
303 service->passphrase = str;
306 if (g_strcmp0(config->ident, NONFS_CONFIG_NAME) != 0)
307 service->from_fs = TRUE;
309 service->from_fs = FALSE;
312 g_hash_table_insert(config->service_table, service->ident,
315 connman_info("Adding service configuration %s", service->ident);
320 static int load_config(struct connman_config *config)
328 DBG("config %p", config);
330 keyfile = __connman_storage_open_config(config->ident);
334 /* Verify keys validity of the global section */
335 check_keys(keyfile, "global", config_possible_keys);
337 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
339 g_free(config->name);
343 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
345 g_free(config->description);
346 config->description = str;
349 groups = g_key_file_get_groups(keyfile, &length);
351 for (i = 0; groups[i] != NULL; i++) {
352 if (g_str_has_prefix(groups[i], "service_") == TRUE)
353 load_service(keyfile, groups[i], config);
358 __connman_storage_close_config(config->ident, keyfile, FALSE);
363 static struct connman_config *create_config(const char *ident)
365 struct connman_config *config;
367 DBG("ident %s", ident);
369 if (g_hash_table_lookup(config_table, ident) != NULL)
372 config = g_try_new0(struct connman_config, 1);
376 config->ident = g_strdup(ident);
378 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
379 NULL, unregister_service);
381 g_hash_table_insert(config_table, config->ident, config);
383 connman_info("Adding configuration %s", config->ident);
388 int __connman_config_load_service(GKeyFile *keyfile, const char *group)
390 struct connman_config *config = g_hash_table_lookup(config_table,
393 if (config == NULL) {
394 config = create_config(NONFS_CONFIG_NAME);
399 return load_service(keyfile, group, config);
402 static int read_configs(void)
408 dir = g_dir_open(STORAGEDIR, 0, NULL);
412 while ((file = g_dir_read_name(dir)) != NULL) {
416 if (g_str_has_suffix(file, ".config") == FALSE)
419 ident = g_strrstr(file, ".config");
423 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
426 str = g_string_new_len(file, ident - file);
430 ident = g_string_free(str, FALSE);
432 if (connman_dbus_validate_ident(ident) == TRUE) {
433 struct connman_config *config;
435 config = create_config(ident);
448 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
456 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
461 status = g_io_channel_read_chars(channel, buffer,
462 sizeof(buffer) -1, &bytes_read, NULL);
465 case G_IO_STATUS_NORMAL:
467 case G_IO_STATUS_AGAIN:
470 connman_error("Reading from inotify channel failed");
477 while (bytes_read > 0) {
478 struct inotify_event *event;
483 event = (struct inotify_event *) next_event;
485 ident = next_event + sizeof(struct inotify_event);
489 len = sizeof(struct inotify_event) + event->len;
491 /* check if inotify_event block fit */
492 if (len > bytes_read)
501 if (g_str_has_suffix(ident, ".config") == FALSE)
504 ext = g_strrstr(ident, ".config");
510 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
513 if (connman_dbus_validate_ident(ident) == FALSE)
516 if (event->mask & IN_CREATE)
517 create_config(ident);
519 if (event->mask & IN_MODIFY) {
520 struct connman_config *config;
522 config = g_hash_table_lookup(config_table, ident);
523 if (config != NULL) {
524 g_hash_table_remove_all(config->service_table);
529 if (event->mask & IN_DELETE)
530 g_hash_table_remove(config_table, ident);
536 static int create_watch(void)
544 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
545 IN_MODIFY | IN_CREATE | IN_DELETE);
546 if (inotify_wd < 0) {
547 connman_error("Creation of STORAGEDIR watch failed");
552 inotify_channel = g_io_channel_unix_new(fd);
553 if (inotify_channel == NULL) {
554 connman_error("Creation of inotify channel failed");
555 inotify_rm_watch(fd, inotify_wd);
562 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
563 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
564 g_io_channel_set_buffered(inotify_channel, FALSE);
566 inotify_watch = g_io_add_watch(inotify_channel,
567 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
573 static void remove_watch(void)
577 if (inotify_channel == NULL)
580 if (inotify_watch > 0) {
581 g_source_remove(inotify_watch);
585 fd = g_io_channel_unix_get_fd(inotify_channel);
587 if (inotify_wd >= 0) {
588 inotify_rm_watch(fd, inotify_wd);
592 g_io_channel_unref(inotify_channel);
595 int __connman_config_init(void)
599 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
600 NULL, unregister_config);
604 return read_configs();
607 void __connman_config_cleanup(void)
613 g_hash_table_destroy(config_table);
617 static char *config_pem_fsid(const char *pem_file)
620 unsigned *fsid = (unsigned *) &buf.f_fsid;
621 unsigned long long fsid64;
623 if (pem_file == NULL)
626 if (statfs(pem_file, &buf) < 0) {
627 connman_error("statfs error %s for %s",
628 strerror(errno), pem_file);
632 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
634 return g_strdup_printf("%llx", fsid64);
637 static void provision_service(gpointer key, gpointer value, gpointer user_data)
639 struct connman_service *service = user_data;
640 struct connman_config_service *config = value;
641 struct connman_network *network;
643 unsigned int ssid_len;
645 /* For now only WiFi service entries are supported */
646 if (g_strcmp0(config->type, "wifi") != 0)
649 network = __connman_service_get_network(service);
650 if (network == NULL) {
651 connman_error("Service has no network set");
655 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
657 connman_error("Network SSID not set");
661 if (config->ssid == NULL || ssid_len != config->ssid_len)
664 if (memcmp(config->ssid, ssid, ssid_len) != 0)
667 /* do not provision immutable services with non-fs originated configs */
668 if (config->from_fs == FALSE &&
669 __connman_service_get_immutable(service) == TRUE)
672 /* only lock services with a config originated from the filesystem */
673 if (config->from_fs == TRUE)
674 __connman_service_set_immutable(service, TRUE);
676 __connman_service_set_favorite(service, TRUE);
678 if (config->eap != NULL)
679 __connman_service_set_string(service, "EAP", config->eap);
681 if (config->identity != NULL)
682 __connman_service_set_string(service, "Identity",
685 if (config->ca_cert_file != NULL)
686 __connman_service_set_string(service, "CACertFile",
687 config->ca_cert_file);
689 if (config->client_cert_file != NULL)
690 __connman_service_set_string(service, "ClientCertFile",
691 config->client_cert_file);
693 if (config->private_key_file != NULL)
694 __connman_service_set_string(service, "PrivateKeyFile",
695 config->private_key_file);
697 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
698 config->private_key_file != NULL) {
701 fsid = config_pem_fsid(config->private_key_file);
705 g_free(config->private_key_passphrase);
706 config->private_key_passphrase = fsid;
709 if (config->private_key_passphrase != NULL) {
710 __connman_service_set_string(service, "PrivateKeyPassphrase",
711 config->private_key_passphrase);
713 * TODO: Support for PEAP with both identity and key passwd.
714 * In that case, we should check if both of them are found
715 * from the config file. If not, we should not set the
716 * service passphrase in order for the UI to request for an
717 * additional passphrase.
721 if (config->phase2 != NULL)
722 __connman_service_set_string(service, "Phase2", config->phase2);
724 if (config->passphrase != NULL)
725 __connman_service_set_string(service, "Passphrase", config->passphrase);
728 int __connman_config_provision_service(struct connman_service *service)
730 enum connman_service_type type;
734 DBG("service %p", service);
736 /* For now only WiFi services are supported */
737 type = connman_service_get_type(service);
738 if (type != CONNMAN_SERVICE_TYPE_WIFI)
741 g_hash_table_iter_init(&iter, config_table);
743 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
744 struct connman_config *config = value;
746 g_hash_table_foreach(config->service_table,
747 provision_service, service);