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;
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 struct connman_config *create_config(const char *ident);
322 int __connman_config_load_service(GKeyFile *keyfile, const char *group)
324 struct connman_config *config = g_hash_table_lookup(config_table,
327 if (config == NULL) {
328 config = create_config(NONFS_CONFIG_NAME);
333 return load_service(keyfile, group, config);
336 static int load_config(struct connman_config *config)
344 DBG("config %p", config);
346 keyfile = __connman_storage_open_config(config->ident);
350 /* Verify keys validity of the global section */
351 check_keys(keyfile, "global", config_possible_keys);
353 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
355 g_free(config->name);
359 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
361 g_free(config->description);
362 config->description = str;
365 groups = g_key_file_get_groups(keyfile, &length);
367 for (i = 0; groups[i] != NULL; i++) {
368 if (g_str_has_prefix(groups[i], "service_") == TRUE)
369 load_service(keyfile, groups[i], config);
374 __connman_storage_close_config(config->ident, keyfile, FALSE);
379 static struct connman_config *create_config(const char *ident)
381 struct connman_config *config;
383 DBG("ident %s", ident);
385 if (g_hash_table_lookup(config_table, ident) != NULL)
388 config = g_try_new0(struct connman_config, 1);
392 config->ident = g_strdup(ident);
394 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
395 NULL, unregister_service);
397 g_hash_table_insert(config_table, config->ident, config);
399 connman_info("Adding configuration %s", config->ident);
404 static int read_configs(void)
410 dir = g_dir_open(STORAGEDIR, 0, NULL);
414 while ((file = g_dir_read_name(dir)) != NULL) {
418 if (g_str_has_suffix(file, ".config") == FALSE)
421 ident = g_strrstr(file, ".config");
425 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
428 str = g_string_new_len(file, ident - file);
432 ident = g_string_free(str, FALSE);
434 if (connman_dbus_validate_ident(ident) == TRUE) {
435 struct connman_config *config;
437 config = create_config(ident);
450 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
458 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
463 status = g_io_channel_read_chars(channel, buffer,
464 sizeof(buffer) -1, &bytes_read, NULL);
467 case G_IO_STATUS_NORMAL:
469 case G_IO_STATUS_AGAIN:
472 connman_error("Reading from inotify channel failed");
479 while (bytes_read > 0) {
480 struct inotify_event *event;
485 event = (struct inotify_event *) next_event;
487 ident = next_event + sizeof(struct inotify_event);
491 len = sizeof(struct inotify_event) + event->len;
493 /* check if inotify_event block fit */
494 if (len > bytes_read)
503 if (g_str_has_suffix(ident, ".config") == FALSE)
506 ext = g_strrstr(ident, ".config");
512 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
515 if (connman_dbus_validate_ident(ident) == FALSE)
518 if (event->mask & IN_CREATE)
519 create_config(ident);
521 if (event->mask & IN_MODIFY) {
522 struct connman_config *config;
524 config = g_hash_table_lookup(config_table, ident);
525 if (config != NULL) {
526 g_hash_table_remove_all(config->service_table);
531 if (event->mask & IN_DELETE)
532 g_hash_table_remove(config_table, ident);
538 static int create_watch(void)
546 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
547 IN_MODIFY | IN_CREATE | IN_DELETE);
548 if (inotify_wd < 0) {
549 connman_error("Creation of STORAGEDIR watch failed");
554 inotify_channel = g_io_channel_unix_new(fd);
555 if (inotify_channel == NULL) {
556 connman_error("Creation of inotify channel failed");
557 inotify_rm_watch(fd, inotify_wd);
564 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
565 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
566 g_io_channel_set_buffered(inotify_channel, FALSE);
568 inotify_watch = g_io_add_watch(inotify_channel,
569 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
575 static void remove_watch(void)
579 if (inotify_channel == NULL)
582 if (inotify_watch > 0) {
583 g_source_remove(inotify_watch);
587 fd = g_io_channel_unix_get_fd(inotify_channel);
589 if (inotify_wd >= 0) {
590 inotify_rm_watch(fd, inotify_wd);
594 g_io_channel_unref(inotify_channel);
597 int __connman_config_init(void)
601 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
602 NULL, unregister_config);
606 return read_configs();
609 void __connman_config_cleanup(void)
615 g_hash_table_destroy(config_table);
619 static char *config_pem_fsid(const char *pem_file)
622 unsigned *fsid = (unsigned *) &buf.f_fsid;
623 unsigned long long fsid64;
625 if (pem_file == NULL)
628 if (statfs(pem_file, &buf) < 0) {
629 connman_error("statfs error %s for %s",
630 strerror(errno), pem_file);
634 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
636 return g_strdup_printf("%llx", fsid64);
639 static void provision_service(gpointer key, gpointer value, gpointer user_data)
641 struct connman_service *service = user_data;
642 struct connman_config_service *config = value;
643 struct connman_network *network;
645 unsigned int ssid_len;
647 /* For now only WiFi service entries are supported */
648 if (g_strcmp0(config->type, "wifi") != 0)
651 network = __connman_service_get_network(service);
652 if (network == NULL) {
653 connman_error("Service has no network set");
657 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
659 connman_error("Network SSID not set");
663 if (config->ssid == NULL || ssid_len != config->ssid_len)
666 if (memcmp(config->ssid, ssid, ssid_len) != 0)
669 /* do not provision immutable services with non-fs originated configs */
670 if (config->from_fs == FALSE &&
671 __connman_service_get_immutable(service) == TRUE)
674 /* only lock services with a config originated from the filesystem */
675 if (config->from_fs == TRUE)
676 __connman_service_set_immutable(service, TRUE);
678 __connman_service_set_favorite(service, TRUE);
680 if (config->eap != NULL)
681 __connman_service_set_string(service, "EAP", config->eap);
683 if (config->identity != NULL)
684 __connman_service_set_string(service, "Identity",
687 if (config->ca_cert_file != NULL)
688 __connman_service_set_string(service, "CACertFile",
689 config->ca_cert_file);
691 if (config->client_cert_file != NULL)
692 __connman_service_set_string(service, "ClientCertFile",
693 config->client_cert_file);
695 if (config->private_key_file != NULL)
696 __connman_service_set_string(service, "PrivateKeyFile",
697 config->private_key_file);
699 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
700 config->private_key_file != NULL) {
703 fsid = config_pem_fsid(config->private_key_file);
707 g_free(config->private_key_passphrase);
708 config->private_key_passphrase = fsid;
711 if (config->private_key_passphrase != NULL) {
712 __connman_service_set_string(service, "PrivateKeyPassphrase",
713 config->private_key_passphrase);
715 * TODO: Support for PEAP with both identity and key passwd.
716 * In that case, we should check if both of them are found
717 * from the config file. If not, we should not set the
718 * service passphrase in order for the UI to request for an
719 * additional passphrase.
723 if (config->phase2 != NULL)
724 __connman_service_set_string(service, "Phase2", config->phase2);
726 if (config->passphrase != NULL)
727 __connman_service_set_string(service, "Passphrase", config->passphrase);
730 int __connman_config_provision_service(struct connman_service *service)
732 enum connman_service_type type;
736 DBG("service %p", service);
738 /* For now only WiFi services are supported */
739 type = connman_service_get_type(service);
740 if (type != CONNMAN_SERVICE_TYPE_WIFI)
743 g_hash_table_iter_init(&iter, config_table);
745 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
746 struct connman_config *config = value;
748 g_hash_table_foreach(config->service_table,
749 provision_service, service);