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;
52 struct connman_config {
56 GHashTable *service_table;
59 static GHashTable *config_table = NULL;
61 static int inotify_wd = -1;
63 static GIOChannel *inotify_channel = NULL;
64 static uint inotify_watch = 0;
66 /* Definition of possible strings in the .config files */
67 #define CONFIG_KEY_NAME "Name"
68 #define CONFIG_KEY_DESC "Description"
70 #define SERVICE_KEY_TYPE "Type"
71 #define SERVICE_KEY_NAME "Name"
72 #define SERVICE_KEY_SSID "SSID"
73 #define SERVICE_KEY_EAP "EAP"
74 #define SERVICE_KEY_CA_CERT "CACertFile"
75 #define SERVICE_KEY_CL_CERT "ClientCertFile"
76 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
77 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
78 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
79 #define SERVICE_KEY_IDENTITY "Identity"
80 #define SERVICE_KEY_PHASE2 "Phase2"
81 #define SERVICE_KEY_PASSPHRASE "Passphrase"
83 static const char *config_possible_keys[] = {
89 static const char *service_possible_keys[] = {
97 SERVICE_KEY_PRV_KEY_PASS,
98 SERVICE_KEY_PRV_KEY_PASS_TYPE,
101 SERVICE_KEY_PASSPHRASE,
105 static void unregister_config(gpointer data)
107 struct connman_config *config = data;
109 connman_info("Removing configuration %s", config->ident);
111 g_hash_table_destroy(config->service_table);
113 g_free(config->description);
114 g_free(config->name);
115 g_free(config->ident);
119 static void unregister_service(gpointer data)
121 struct connman_config_service *service = data;
123 connman_info("Removing service configuration %s", service->ident);
125 g_free(service->ident);
126 g_free(service->type);
127 g_free(service->name);
128 g_free(service->ssid);
129 g_free(service->eap);
130 g_free(service->identity);
131 g_free(service->ca_cert_file);
132 g_free(service->client_cert_file);
133 g_free(service->private_key_file);
134 g_free(service->private_key_passphrase);
135 g_free(service->private_key_passphrase_type);
136 g_free(service->phase2);
137 g_free(service->passphrase);
141 static void check_keys(GKeyFile *keyfile, const char *group,
142 const char **possible_keys)
145 gsize nb_avail_keys, i, j;
147 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
148 if (avail_keys == NULL)
152 * For each key in the configuration file,
153 * verify it is understood by connman
155 for (i = 0 ; i < nb_avail_keys; i++) {
156 for (j = 0; possible_keys[j] ; j++)
157 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
160 if (possible_keys[j] == NULL)
161 connman_warn("Unknown configuration key %s in [%s]",
162 avail_keys[i], group);
165 g_strfreev(avail_keys);
168 static int load_service(GKeyFile *keyfile, const char *group,
169 struct connman_config *config)
171 struct connman_config_service *service;
173 char *str, *hex_ssid;
175 /* Strip off "service_" prefix */
178 if (strlen(ident) < 1)
181 /* Verify that provided keys are good */
182 check_keys(keyfile, group, service_possible_keys);
184 service = g_hash_table_lookup(config->service_table, ident);
185 if (service == NULL) {
186 service = g_try_new0(struct connman_config_service, 1);
190 service->ident = g_strdup(ident);
193 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
195 g_free(service->type);
199 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
201 g_free(service->name);
205 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
207 if (hex_ssid != NULL) {
209 unsigned int i, j = 0, hex;
210 size_t hex_ssid_len = strlen(hex_ssid);
212 ssid = g_try_malloc0(hex_ssid_len / 2);
218 for (i = 0; i < hex_ssid_len; i += 2) {
219 sscanf(hex_ssid + i, "%02x", &hex);
225 g_free(service->ssid);
226 service->ssid = ssid;
227 service->ssid_len = hex_ssid_len / 2;
228 } else if (service->name != NULL) {
230 unsigned int ssid_len;
232 ssid_len = strlen(service->name);
233 ssid = g_try_malloc0(ssid_len);
237 memcpy(ssid, service->name, ssid_len);
238 g_free(service->ssid);
239 service->ssid = ssid;
240 service->ssid_len = ssid_len;
243 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
245 g_free(service->eap);
249 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
251 g_free(service->ca_cert_file);
252 service->ca_cert_file = str;
255 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
257 g_free(service->client_cert_file);
258 service->client_cert_file = str;
261 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
263 g_free(service->private_key_file);
264 service->private_key_file = str;
267 str = g_key_file_get_string(keyfile, group,
268 SERVICE_KEY_PRV_KEY_PASS, NULL);
270 g_free(service->private_key_passphrase);
271 service->private_key_passphrase = str;
274 str = g_key_file_get_string(keyfile, group,
275 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
277 g_free(service->private_key_passphrase_type);
278 service->private_key_passphrase_type = str;
281 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
283 g_free(service->identity);
284 service->identity = str;
287 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
289 g_free(service->phase2);
290 service->phase2 = str;
293 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
296 g_free(service->passphrase);
297 service->passphrase = str;
300 g_hash_table_replace(config->service_table, service->ident, service);
302 connman_info("Adding service configuration %s", service->ident);
307 static int load_config(struct connman_config *config)
315 DBG("config %p", config);
317 keyfile = __connman_storage_open_config(config->ident);
321 /* Verify keys validity of the global section */
322 check_keys(keyfile, "global", config_possible_keys);
324 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
326 g_free(config->name);
330 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
332 g_free(config->description);
333 config->description = str;
336 groups = g_key_file_get_groups(keyfile, &length);
338 for (i = 0; groups[i] != NULL; i++) {
339 if (g_str_has_prefix(groups[i], "service_") == TRUE)
340 load_service(keyfile, groups[i], config);
345 __connman_storage_close_config(config->ident, keyfile, FALSE);
350 static struct connman_config *create_config(const char *ident)
352 struct connman_config *config;
354 DBG("ident %s", ident);
356 if (g_hash_table_lookup(config_table, ident) != NULL)
359 config = g_try_new0(struct connman_config, 1);
363 config->ident = g_strdup(ident);
365 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
366 NULL, unregister_service);
368 g_hash_table_insert(config_table, config->ident, config);
370 connman_info("Adding configuration %s", config->ident);
375 static int read_configs(void)
381 dir = g_dir_open(STORAGEDIR, 0, NULL);
385 while ((file = g_dir_read_name(dir)) != NULL) {
389 if (g_str_has_suffix(file, ".config") == FALSE)
392 ident = g_strrstr(file, ".config");
396 str = g_string_new_len(file, ident - file);
400 ident = g_string_free(str, FALSE);
402 if (connman_dbus_validate_ident(ident) == TRUE) {
403 struct connman_config *config;
405 config = create_config(ident);
418 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
426 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
431 err = g_io_channel_read(channel, buffer,
432 sizeof(buffer) - 1, &bytes_read);
434 if (err != G_IO_ERROR_NONE) {
435 if (err == G_IO_ERROR_AGAIN)
438 connman_error("Reading from inotify channel failed");
445 while (bytes_read > 0) {
446 struct inotify_event *event;
451 event = (struct inotify_event *) next_event;
453 ident = next_event + sizeof(struct inotify_event);
457 len = sizeof(struct inotify_event) + event->len;
459 /* check if inotify_event block fit */
460 if (len > bytes_read)
469 if (g_str_has_suffix(ident, ".config") == FALSE)
472 ext = g_strrstr(ident, ".config");
478 if (connman_dbus_validate_ident(ident) == FALSE)
481 if (event->mask & IN_CREATE)
482 create_config(ident);
484 if (event->mask & IN_MODIFY) {
485 struct connman_config *config;
487 config = g_hash_table_lookup(config_table, ident);
488 if (config != NULL) {
489 g_hash_table_remove_all(config->service_table);
494 if (event->mask & IN_DELETE)
495 g_hash_table_remove(config_table, ident);
501 static int create_watch(void)
509 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
510 IN_MODIFY | IN_CREATE | IN_DELETE);
511 if (inotify_wd < 0) {
512 connman_error("Creation of STORAGEDIR watch failed");
517 inotify_channel = g_io_channel_unix_new(fd);
518 if (inotify_channel == NULL) {
519 connman_error("Creation of inotify channel failed");
520 inotify_rm_watch(fd, inotify_wd);
527 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
528 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
529 g_io_channel_set_buffered(inotify_channel, FALSE);
531 inotify_watch = g_io_add_watch(inotify_channel,
532 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
538 static void remove_watch(void)
542 if (inotify_channel == NULL)
545 if (inotify_watch > 0) {
546 g_source_remove(inotify_watch);
550 fd = g_io_channel_unix_get_fd(inotify_channel);
552 if (inotify_wd >= 0) {
553 inotify_rm_watch(fd, inotify_wd);
557 g_io_channel_unref(inotify_channel);
560 int __connman_config_init(void)
564 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
565 NULL, unregister_config);
569 return read_configs();
572 void __connman_config_cleanup(void)
578 g_hash_table_destroy(config_table);
582 static char *config_pem_fsid(const char *pem_file)
585 unsigned *fsid = (unsigned *) &buf.f_fsid;
586 unsigned long long fsid64;
588 if (pem_file == NULL)
591 if (statfs(pem_file, &buf) < 0) {
592 connman_error("statfs error %s for %s",
593 strerror(errno), pem_file);
597 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
599 return g_strdup_printf("%llx", fsid64);
602 static void provision_service(gpointer key, gpointer value, gpointer user_data)
604 struct connman_service *service = user_data;
605 struct connman_config_service *config = value;
606 struct connman_network *network;
608 unsigned int ssid_len;
610 /* For now only WiFi service entries are supported */
611 if (g_strcmp0(config->type, "wifi") != 0)
614 network = __connman_service_get_network(service);
615 if (network == NULL) {
616 connman_error("Service has no network set");
620 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
622 connman_error("Network SSID not set");
626 if (config->ssid == NULL || ssid_len != config->ssid_len)
629 if (memcmp(config->ssid, ssid, ssid_len) != 0)
632 __connman_service_set_immutable(service, TRUE);
633 __connman_service_set_favorite(service, TRUE);
635 if (config->eap != NULL)
636 __connman_service_set_string(service, "EAP", config->eap);
638 if (config->identity != NULL)
639 __connman_service_set_string(service, "Identity",
642 if (config->ca_cert_file != NULL)
643 __connman_service_set_string(service, "CACertFile",
644 config->ca_cert_file);
646 if (config->client_cert_file != NULL)
647 __connman_service_set_string(service, "ClientCertFile",
648 config->client_cert_file);
650 if (config->private_key_file != NULL)
651 __connman_service_set_string(service, "PrivateKeyFile",
652 config->private_key_file);
654 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
655 config->private_key_file != NULL) {
658 fsid = config_pem_fsid(config->private_key_file);
662 g_free(config->private_key_passphrase);
663 config->private_key_passphrase = fsid;
666 if (config->private_key_passphrase != NULL) {
667 __connman_service_set_string(service, "PrivateKeyPassphrase",
668 config->private_key_passphrase);
670 * TODO: Support for PEAP with both identity and key passwd.
671 * In that case, we should check if both of them are found
672 * from the config file. If not, we should not set the
673 * service passphrase in order for the UI to request for an
674 * additional passphrase.
678 if (config->phase2 != NULL)
679 __connman_service_set_string(service, "Phase2", config->phase2);
681 if (config->passphrase != NULL)
682 __connman_service_set_string(service, "Passphrase", config->passphrase);
685 int __connman_config_provision_service(struct connman_service *service)
687 enum connman_service_type type;
691 DBG("service %p", service);
693 /* For now only WiFi services are supported */
694 type = connman_service_get_type(service);
695 if (type != CONNMAN_SERVICE_TYPE_WIFI)
698 g_hash_table_iter_init(&iter, config_table);
700 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
701 struct connman_config *config = value;
703 g_hash_table_foreach(config->service_table,
704 provision_service, service);