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"
71 /* Definition of possible strings in the .config files */
72 #define CONFIG_KEY_NAME "Name"
73 #define CONFIG_KEY_DESC "Description"
74 #define CONFIG_KEY_PROT "Protected"
76 #define SERVICE_KEY_TYPE "Type"
77 #define SERVICE_KEY_NAME "Name"
78 #define SERVICE_KEY_SSID "SSID"
79 #define SERVICE_KEY_EAP "EAP"
80 #define SERVICE_KEY_CA_CERT "CACertFile"
81 #define SERVICE_KEY_CL_CERT "ClientCertFile"
82 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
83 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
84 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
85 #define SERVICE_KEY_IDENTITY "Identity"
86 #define SERVICE_KEY_PHASE2 "Phase2"
87 #define SERVICE_KEY_PASSPHRASE "Passphrase"
89 static const char *config_possible_keys[] = {
96 static const char *service_possible_keys[] = {
104 SERVICE_KEY_PRV_KEY_PASS,
105 SERVICE_KEY_PRV_KEY_PASS_TYPE,
106 SERVICE_KEY_IDENTITY,
108 SERVICE_KEY_PASSPHRASE,
112 static void unregister_config(gpointer data)
114 struct connman_config *config = data;
116 connman_info("Removing configuration %s", config->ident);
118 g_hash_table_destroy(config->service_table);
120 g_free(config->description);
121 g_free(config->name);
122 g_free(config->ident);
126 static void unregister_service(gpointer data)
128 struct connman_config_service *service = data;
130 connman_info("Removing service configuration %s", service->ident);
132 protected_services = g_slist_remove(protected_services, service);
134 g_free(service->ident);
135 g_free(service->type);
136 g_free(service->name);
137 g_free(service->ssid);
138 g_free(service->eap);
139 g_free(service->identity);
140 g_free(service->ca_cert_file);
141 g_free(service->client_cert_file);
142 g_free(service->private_key_file);
143 g_free(service->private_key_passphrase);
144 g_free(service->private_key_passphrase_type);
145 g_free(service->phase2);
146 g_free(service->passphrase);
150 static void check_keys(GKeyFile *keyfile, const char *group,
151 const char **possible_keys)
154 gsize nb_avail_keys, i, j;
156 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
157 if (avail_keys == NULL)
161 * For each key in the configuration file,
162 * verify it is understood by connman
164 for (i = 0 ; i < nb_avail_keys; i++) {
165 for (j = 0; possible_keys[j] ; j++)
166 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
169 if (possible_keys[j] == NULL)
170 connman_warn("Unknown configuration key %s in [%s]",
171 avail_keys[i], group);
174 g_strfreev(avail_keys);
177 static connman_bool_t
178 is_protected_service(struct connman_config_service *service)
182 DBG("ident %s", service->ident);
184 for (list = protected_services; list; list = list->next) {
185 struct connman_config_service *s = list->data;
187 if (g_strcmp0(s->type, service->type) != 0)
190 if (s->ssid == NULL || service->ssid == NULL)
193 if (g_strcmp0(service->type, "wifi") == 0 &&
194 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
202 static int load_service(GKeyFile *keyfile, const char *group,
203 struct connman_config *config)
205 struct connman_config_service *service;
207 char *str, *hex_ssid;
208 gboolean service_created = FALSE;
210 /* Strip off "service_" prefix */
213 if (strlen(ident) < 1)
216 /* Verify that provided keys are good */
217 check_keys(keyfile, group, service_possible_keys);
219 service = g_hash_table_lookup(config->service_table, ident);
220 if (service == NULL) {
221 service = g_try_new0(struct connman_config_service, 1);
225 service->ident = g_strdup(ident);
227 service_created = TRUE;
230 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
232 g_free(service->type);
236 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
238 g_free(service->name);
242 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
244 if (hex_ssid != NULL) {
246 unsigned int i, j = 0, hex;
247 size_t hex_ssid_len = strlen(hex_ssid);
249 ssid = g_try_malloc0(hex_ssid_len / 2);
255 for (i = 0; i < hex_ssid_len; i += 2) {
256 sscanf(hex_ssid + i, "%02x", &hex);
262 g_free(service->ssid);
263 service->ssid = ssid;
264 service->ssid_len = hex_ssid_len / 2;
265 } else if (service->name != NULL) {
267 unsigned int ssid_len;
269 ssid_len = strlen(service->name);
270 ssid = g_try_malloc0(ssid_len);
274 memcpy(ssid, service->name, ssid_len);
275 g_free(service->ssid);
276 service->ssid = ssid;
277 service->ssid_len = ssid_len;
280 if (is_protected_service(service) == TRUE) {
281 connman_error("Trying to provision a protected service");
283 g_free(service->ident);
284 g_free(service->type);
285 g_free(service->name);
286 g_free(service->ssid);
292 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
294 g_free(service->eap);
298 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
300 g_free(service->ca_cert_file);
301 service->ca_cert_file = str;
304 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
306 g_free(service->client_cert_file);
307 service->client_cert_file = str;
310 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
312 g_free(service->private_key_file);
313 service->private_key_file = str;
316 str = g_key_file_get_string(keyfile, group,
317 SERVICE_KEY_PRV_KEY_PASS, NULL);
319 g_free(service->private_key_passphrase);
320 service->private_key_passphrase = str;
323 str = g_key_file_get_string(keyfile, group,
324 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
326 g_free(service->private_key_passphrase_type);
327 service->private_key_passphrase_type = str;
330 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
332 g_free(service->identity);
333 service->identity = str;
336 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
338 g_free(service->phase2);
339 service->phase2 = str;
342 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
345 g_free(service->passphrase);
346 service->passphrase = str;
349 if (g_strcmp0(config->ident, NONFS_CONFIG_NAME) != 0)
350 service->from_fs = TRUE;
352 service->from_fs = FALSE;
355 g_hash_table_insert(config->service_table, service->ident,
358 if (config->protected == TRUE)
360 g_slist_append(protected_services, service);
362 connman_info("Adding service configuration %s", service->ident);
367 static int load_config(struct connman_config *config)
376 DBG("config %p", config);
378 keyfile = __connman_storage_open_config(config->ident);
382 /* Verify keys validity of the global section */
383 check_keys(keyfile, "global", config_possible_keys);
385 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
387 g_free(config->name);
391 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
393 g_free(config->description);
394 config->description = str;
397 protected = g_key_file_get_boolean(keyfile, "global",
398 CONFIG_KEY_PROT, NULL);
399 config->protected = protected;
401 groups = g_key_file_get_groups(keyfile, &length);
403 for (i = 0; groups[i] != NULL; i++) {
404 if (g_str_has_prefix(groups[i], "service_") == TRUE)
405 load_service(keyfile, groups[i], config);
410 __connman_storage_close_config(config->ident, keyfile, FALSE);
415 static struct connman_config *create_config(const char *ident)
417 struct connman_config *config;
419 DBG("ident %s", ident);
421 if (g_hash_table_lookup(config_table, ident) != NULL)
424 config = g_try_new0(struct connman_config, 1);
428 config->ident = g_strdup(ident);
430 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
431 NULL, unregister_service);
433 g_hash_table_insert(config_table, config->ident, config);
435 connman_info("Adding configuration %s", config->ident);
440 int __connman_config_load_service(GKeyFile *keyfile, const char *group)
442 struct connman_config *config = g_hash_table_lookup(config_table,
445 if (config == NULL) {
446 config = create_config(NONFS_CONFIG_NAME);
451 return load_service(keyfile, group, config);
454 static int read_configs(void)
460 dir = g_dir_open(STORAGEDIR, 0, NULL);
464 while ((file = g_dir_read_name(dir)) != NULL) {
468 if (g_str_has_suffix(file, ".config") == FALSE)
471 ident = g_strrstr(file, ".config");
475 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
478 str = g_string_new_len(file, ident - file);
482 ident = g_string_free(str, FALSE);
484 if (connman_dbus_validate_ident(ident) == TRUE) {
485 struct connman_config *config;
487 config = create_config(ident);
500 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
508 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
513 status = g_io_channel_read_chars(channel, buffer,
514 sizeof(buffer) -1, &bytes_read, NULL);
517 case G_IO_STATUS_NORMAL:
519 case G_IO_STATUS_AGAIN:
522 connman_error("Reading from inotify channel failed");
529 while (bytes_read > 0) {
530 struct inotify_event *event;
535 event = (struct inotify_event *) next_event;
537 ident = next_event + sizeof(struct inotify_event);
541 len = sizeof(struct inotify_event) + event->len;
543 /* check if inotify_event block fit */
544 if (len > bytes_read)
553 if (g_str_has_suffix(ident, ".config") == FALSE)
556 ext = g_strrstr(ident, ".config");
562 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
565 if (connman_dbus_validate_ident(ident) == FALSE)
568 if (event->mask & IN_CREATE)
569 create_config(ident);
571 if (event->mask & IN_MODIFY) {
572 struct connman_config *config;
574 config = g_hash_table_lookup(config_table, ident);
575 if (config != NULL) {
576 g_hash_table_remove_all(config->service_table);
581 if (event->mask & IN_DELETE)
582 g_hash_table_remove(config_table, ident);
588 static int create_watch(void)
596 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
597 IN_MODIFY | IN_CREATE | IN_DELETE);
598 if (inotify_wd < 0) {
599 connman_error("Creation of STORAGEDIR watch failed");
604 inotify_channel = g_io_channel_unix_new(fd);
605 if (inotify_channel == NULL) {
606 connman_error("Creation of inotify channel failed");
607 inotify_rm_watch(fd, inotify_wd);
614 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
615 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
616 g_io_channel_set_buffered(inotify_channel, FALSE);
618 inotify_watch = g_io_add_watch(inotify_channel,
619 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
625 static void remove_watch(void)
629 if (inotify_channel == NULL)
632 if (inotify_watch > 0) {
633 g_source_remove(inotify_watch);
637 fd = g_io_channel_unix_get_fd(inotify_channel);
639 if (inotify_wd >= 0) {
640 inotify_rm_watch(fd, inotify_wd);
644 g_io_channel_unref(inotify_channel);
647 int __connman_config_init(void)
651 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
652 NULL, unregister_config);
656 return read_configs();
659 void __connman_config_cleanup(void)
665 g_hash_table_destroy(config_table);
669 static char *config_pem_fsid(const char *pem_file)
672 unsigned *fsid = (unsigned *) &buf.f_fsid;
673 unsigned long long fsid64;
675 if (pem_file == NULL)
678 if (statfs(pem_file, &buf) < 0) {
679 connman_error("statfs error %s for %s",
680 strerror(errno), pem_file);
684 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
686 return g_strdup_printf("%llx", fsid64);
689 static void provision_service(gpointer key, gpointer value, gpointer user_data)
691 struct connman_service *service = user_data;
692 struct connman_config_service *config = value;
693 struct connman_network *network;
695 unsigned int ssid_len;
697 /* For now only WiFi service entries are supported */
698 if (g_strcmp0(config->type, "wifi") != 0)
701 network = __connman_service_get_network(service);
702 if (network == NULL) {
703 connman_error("Service has no network set");
707 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
709 connman_error("Network SSID not set");
713 if (config->ssid == NULL || ssid_len != config->ssid_len)
716 if (memcmp(config->ssid, ssid, ssid_len) != 0)
719 /* do not provision immutable services with non-fs originated configs */
720 if (config->from_fs == FALSE &&
721 __connman_service_get_immutable(service) == TRUE)
724 /* only lock services with a config originated from the filesystem */
725 if (config->from_fs == TRUE)
726 __connman_service_set_immutable(service, TRUE);
728 __connman_service_set_favorite(service, TRUE);
730 if (config->eap != NULL)
731 __connman_service_set_string(service, "EAP", config->eap);
733 if (config->identity != NULL)
734 __connman_service_set_string(service, "Identity",
737 if (config->ca_cert_file != NULL)
738 __connman_service_set_string(service, "CACertFile",
739 config->ca_cert_file);
741 if (config->client_cert_file != NULL)
742 __connman_service_set_string(service, "ClientCertFile",
743 config->client_cert_file);
745 if (config->private_key_file != NULL)
746 __connman_service_set_string(service, "PrivateKeyFile",
747 config->private_key_file);
749 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
750 config->private_key_file != NULL) {
753 fsid = config_pem_fsid(config->private_key_file);
757 g_free(config->private_key_passphrase);
758 config->private_key_passphrase = fsid;
761 if (config->private_key_passphrase != NULL) {
762 __connman_service_set_string(service, "PrivateKeyPassphrase",
763 config->private_key_passphrase);
765 * TODO: Support for PEAP with both identity and key passwd.
766 * In that case, we should check if both of them are found
767 * from the config file. If not, we should not set the
768 * service passphrase in order for the UI to request for an
769 * additional passphrase.
773 if (config->phase2 != NULL)
774 __connman_service_set_string(service, "Phase2", config->phase2);
776 if (config->passphrase != NULL)
777 __connman_service_set_string(service, "Passphrase", config->passphrase);
780 int __connman_config_provision_service(struct connman_service *service)
782 enum connman_service_type type;
786 DBG("service %p", service);
788 /* For now only WiFi services are supported */
789 type = connman_service_get_type(service);
790 if (type != CONNMAN_SERVICE_TYPE_WIFI)
793 g_hash_table_iter_init(&iter, config_table);
795 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
796 struct connman_config *config = value;
798 g_hash_table_foreach(config->service_table,
799 provision_service, service);