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 (g_strcmp0(service->type, "wifi") == 0 &&
195 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
203 static int load_service(GKeyFile *keyfile, const char *group,
204 struct connman_config *config)
206 struct connman_config_service *service;
208 char *str, *hex_ssid;
209 gboolean service_created = FALSE;
212 /* Strip off "service_" prefix */
215 if (strlen(ident) < 1)
218 /* Verify that provided keys are good */
219 check_keys(keyfile, group, service_possible_keys);
221 service = g_hash_table_lookup(config->service_table, ident);
222 if (service == NULL) {
223 service = g_try_new0(struct connman_config_service, 1);
227 service->ident = g_strdup(ident);
229 service_created = TRUE;
232 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
234 g_free(service->type);
238 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
240 g_free(service->name);
244 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
246 if (hex_ssid != NULL) {
248 unsigned int i, j = 0, hex;
249 size_t hex_ssid_len = strlen(hex_ssid);
251 ssid = g_try_malloc0(hex_ssid_len / 2);
258 for (i = 0; i < hex_ssid_len; i += 2) {
259 sscanf(hex_ssid + i, "%02x", &hex);
265 g_free(service->ssid);
266 service->ssid = ssid;
267 service->ssid_len = hex_ssid_len / 2;
268 } else if (service->name != NULL) {
270 unsigned int ssid_len;
272 ssid_len = strlen(service->name);
273 ssid = g_try_malloc0(ssid_len);
279 memcpy(ssid, service->name, ssid_len);
280 g_free(service->ssid);
281 service->ssid = ssid;
282 service->ssid_len = ssid_len;
285 if (is_protected_service(service) == TRUE) {
286 connman_error("Trying to provision a protected service");
291 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
293 g_free(service->eap);
297 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
299 g_free(service->ca_cert_file);
300 service->ca_cert_file = str;
303 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
305 g_free(service->client_cert_file);
306 service->client_cert_file = str;
309 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
311 g_free(service->private_key_file);
312 service->private_key_file = str;
315 str = g_key_file_get_string(keyfile, group,
316 SERVICE_KEY_PRV_KEY_PASS, NULL);
318 g_free(service->private_key_passphrase);
319 service->private_key_passphrase = str;
322 str = g_key_file_get_string(keyfile, group,
323 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
325 g_free(service->private_key_passphrase_type);
326 service->private_key_passphrase_type = str;
329 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
331 g_free(service->identity);
332 service->identity = str;
335 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
337 g_free(service->phase2);
338 service->phase2 = str;
341 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
344 g_free(service->passphrase);
345 service->passphrase = str;
348 if (g_strcmp0(config->ident, NONFS_CONFIG_NAME) != 0)
349 service->from_fs = TRUE;
351 service->from_fs = FALSE;
354 g_hash_table_insert(config->service_table, service->ident,
357 if (config->protected == TRUE)
359 g_slist_append(protected_services, service);
361 connman_info("Adding service configuration %s", service->ident);
366 if (service_created == TRUE) {
367 g_free(service->ident);
368 g_free(service->type);
369 g_free(service->name);
370 g_free(service->ssid);
377 static int load_config(struct connman_config *config)
386 DBG("config %p", config);
388 keyfile = __connman_storage_open_config(config->ident);
392 /* Verify keys validity of the global section */
393 check_keys(keyfile, "global", config_possible_keys);
395 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
397 g_free(config->name);
401 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
403 g_free(config->description);
404 config->description = str;
407 protected = g_key_file_get_boolean(keyfile, "global",
408 CONFIG_KEY_PROT, NULL);
409 config->protected = protected;
411 groups = g_key_file_get_groups(keyfile, &length);
413 for (i = 0; groups[i] != NULL; i++) {
414 if (g_str_has_prefix(groups[i], "service_") == TRUE)
415 load_service(keyfile, groups[i], config);
420 __connman_storage_close_config(config->ident, keyfile, FALSE);
425 static struct connman_config *create_config(const char *ident)
427 struct connman_config *config;
429 DBG("ident %s", ident);
431 if (g_hash_table_lookup(config_table, ident) != NULL)
434 config = g_try_new0(struct connman_config, 1);
438 config->ident = g_strdup(ident);
440 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
441 NULL, unregister_service);
443 g_hash_table_insert(config_table, config->ident, config);
445 connman_info("Adding configuration %s", config->ident);
450 int __connman_config_load_service(GKeyFile *keyfile, const char *group,
451 connman_bool_t persistent)
453 struct connman_config *config;
454 const char *service_name;
455 char *ident, *filename = NULL, *content = NULL;
456 gsize content_length;
459 service_name = group + strlen("service_");
460 ident = g_strdup_printf("%s_%s", INTERNAL_CONFIG_PREFIX, service_name);
464 DBG("ident %s", ident);
466 config = g_hash_table_lookup(config_table, ident);
467 if (config == NULL) {
468 config = create_config(ident);
469 if (config == NULL) {
474 config->protected = FALSE;
477 err = load_service(keyfile, group, config);
478 if (persistent == FALSE || err < 0)
481 g_key_file_set_string(keyfile, "global", CONFIG_KEY_NAME,
483 g_key_file_set_string(keyfile, "global", CONFIG_KEY_DESC,
484 "Internal Config File");
486 content = g_key_file_to_data(keyfile, &content_length, NULL);
487 if (content == NULL) {
492 filename = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
493 if (filename == NULL) {
498 DBG("Saving %d bytes to %s", content_length, filename);
500 if (g_file_set_contents(filename, content,
501 content_length, NULL) == FALSE) {
516 static int read_configs(void)
522 dir = g_dir_open(STORAGEDIR, 0, NULL);
526 while ((file = g_dir_read_name(dir)) != NULL) {
530 if (g_str_has_suffix(file, ".config") == FALSE)
533 ident = g_strrstr(file, ".config");
537 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
540 str = g_string_new_len(file, ident - file);
544 ident = g_string_free(str, FALSE);
546 if (connman_dbus_validate_ident(ident) == TRUE) {
547 struct connman_config *config;
549 config = create_config(ident);
562 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
570 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
575 status = g_io_channel_read_chars(channel, buffer,
576 sizeof(buffer) -1, &bytes_read, NULL);
579 case G_IO_STATUS_NORMAL:
581 case G_IO_STATUS_AGAIN:
584 connman_error("Reading from inotify channel failed");
591 while (bytes_read > 0) {
592 struct inotify_event *event;
597 event = (struct inotify_event *) next_event;
599 ident = next_event + sizeof(struct inotify_event);
603 len = sizeof(struct inotify_event) + event->len;
605 /* check if inotify_event block fit */
606 if (len > bytes_read)
615 if (g_str_has_suffix(ident, ".config") == FALSE)
618 ext = g_strrstr(ident, ".config");
624 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
627 if (connman_dbus_validate_ident(ident) == FALSE)
630 if (event->mask & IN_CREATE)
631 create_config(ident);
633 if (event->mask & IN_MODIFY) {
634 struct connman_config *config;
636 config = g_hash_table_lookup(config_table, ident);
637 if (config != NULL) {
638 g_hash_table_remove_all(config->service_table);
643 if (event->mask & IN_DELETE)
644 g_hash_table_remove(config_table, ident);
650 static int create_watch(void)
658 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
659 IN_MODIFY | IN_CREATE | IN_DELETE);
660 if (inotify_wd < 0) {
661 connman_error("Creation of STORAGEDIR watch failed");
666 inotify_channel = g_io_channel_unix_new(fd);
667 if (inotify_channel == NULL) {
668 connman_error("Creation of inotify channel failed");
669 inotify_rm_watch(fd, inotify_wd);
676 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
677 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
678 g_io_channel_set_buffered(inotify_channel, FALSE);
680 inotify_watch = g_io_add_watch(inotify_channel,
681 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
687 static void remove_watch(void)
691 if (inotify_channel == NULL)
694 if (inotify_watch > 0) {
695 g_source_remove(inotify_watch);
699 fd = g_io_channel_unix_get_fd(inotify_channel);
701 if (inotify_wd >= 0) {
702 inotify_rm_watch(fd, inotify_wd);
706 g_io_channel_unref(inotify_channel);
709 int __connman_config_init(void)
713 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
714 NULL, unregister_config);
718 return read_configs();
721 void __connman_config_cleanup(void)
727 g_hash_table_destroy(config_table);
731 static char *config_pem_fsid(const char *pem_file)
734 unsigned *fsid = (unsigned *) &buf.f_fsid;
735 unsigned long long fsid64;
737 if (pem_file == NULL)
740 if (statfs(pem_file, &buf) < 0) {
741 connman_error("statfs error %s for %s",
742 strerror(errno), pem_file);
746 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
748 return g_strdup_printf("%llx", fsid64);
751 static void provision_service(gpointer key, gpointer value, gpointer user_data)
753 struct connman_service *service = user_data;
754 struct connman_config_service *config = value;
755 struct connman_network *network;
757 unsigned int ssid_len;
759 /* For now only WiFi service entries are supported */
760 if (g_strcmp0(config->type, "wifi") != 0)
763 network = __connman_service_get_network(service);
764 if (network == NULL) {
765 connman_error("Service has no network set");
769 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
771 connman_error("Network SSID not set");
775 if (config->ssid == NULL || ssid_len != config->ssid_len)
778 if (memcmp(config->ssid, ssid, ssid_len) != 0)
781 /* do not provision immutable services with non-fs originated configs */
782 if (config->from_fs == FALSE &&
783 __connman_service_get_immutable(service) == TRUE)
786 /* only lock services with a config originated from the filesystem */
787 if (config->from_fs == TRUE)
788 __connman_service_set_immutable(service, TRUE);
790 __connman_service_set_favorite(service, TRUE);
792 if (config->eap != NULL)
793 __connman_service_set_string(service, "EAP", config->eap);
795 if (config->identity != NULL)
796 __connman_service_set_string(service, "Identity",
799 if (config->ca_cert_file != NULL)
800 __connman_service_set_string(service, "CACertFile",
801 config->ca_cert_file);
803 if (config->client_cert_file != NULL)
804 __connman_service_set_string(service, "ClientCertFile",
805 config->client_cert_file);
807 if (config->private_key_file != NULL)
808 __connman_service_set_string(service, "PrivateKeyFile",
809 config->private_key_file);
811 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
812 config->private_key_file != NULL) {
815 fsid = config_pem_fsid(config->private_key_file);
819 g_free(config->private_key_passphrase);
820 config->private_key_passphrase = fsid;
823 if (config->private_key_passphrase != NULL) {
824 __connman_service_set_string(service, "PrivateKeyPassphrase",
825 config->private_key_passphrase);
827 * TODO: Support for PEAP with both identity and key passwd.
828 * In that case, we should check if both of them are found
829 * from the config file. If not, we should not set the
830 * service passphrase in order for the UI to request for an
831 * additional passphrase.
835 if (config->phase2 != NULL)
836 __connman_service_set_string(service, "Phase2", config->phase2);
838 if (config->passphrase != NULL)
839 __connman_service_set_string(service, "Passphrase", config->passphrase);
842 int __connman_config_provision_service(struct connman_service *service)
844 enum connman_service_type type;
848 DBG("service %p", service);
850 /* For now only WiFi services are supported */
851 type = connman_service_get_type(service);
852 if (type != CONNMAN_SERVICE_TYPE_WIFI)
855 g_hash_table_iter_init(&iter, config_table);
857 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
858 struct connman_config *config = value;
860 g_hash_table_foreach(config->service_table,
861 provision_service, service);