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 status = g_io_channel_read_chars(channel, buffer,
432 sizeof(buffer) -1, &bytes_read, NULL);
435 case G_IO_STATUS_NORMAL:
437 case G_IO_STATUS_AGAIN:
440 connman_error("Reading from inotify channel failed");
447 while (bytes_read > 0) {
448 struct inotify_event *event;
453 event = (struct inotify_event *) next_event;
455 ident = next_event + sizeof(struct inotify_event);
459 len = sizeof(struct inotify_event) + event->len;
461 /* check if inotify_event block fit */
462 if (len > bytes_read)
471 if (g_str_has_suffix(ident, ".config") == FALSE)
474 ext = g_strrstr(ident, ".config");
480 if (connman_dbus_validate_ident(ident) == FALSE)
483 if (event->mask & IN_CREATE)
484 create_config(ident);
486 if (event->mask & IN_MODIFY) {
487 struct connman_config *config;
489 config = g_hash_table_lookup(config_table, ident);
490 if (config != NULL) {
491 g_hash_table_remove_all(config->service_table);
496 if (event->mask & IN_DELETE)
497 g_hash_table_remove(config_table, ident);
503 static int create_watch(void)
511 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
512 IN_MODIFY | IN_CREATE | IN_DELETE);
513 if (inotify_wd < 0) {
514 connman_error("Creation of STORAGEDIR watch failed");
519 inotify_channel = g_io_channel_unix_new(fd);
520 if (inotify_channel == NULL) {
521 connman_error("Creation of inotify channel failed");
522 inotify_rm_watch(fd, inotify_wd);
529 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
530 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
531 g_io_channel_set_buffered(inotify_channel, FALSE);
533 inotify_watch = g_io_add_watch(inotify_channel,
534 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
540 static void remove_watch(void)
544 if (inotify_channel == NULL)
547 if (inotify_watch > 0) {
548 g_source_remove(inotify_watch);
552 fd = g_io_channel_unix_get_fd(inotify_channel);
554 if (inotify_wd >= 0) {
555 inotify_rm_watch(fd, inotify_wd);
559 g_io_channel_unref(inotify_channel);
562 int __connman_config_init(void)
566 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
567 NULL, unregister_config);
571 return read_configs();
574 void __connman_config_cleanup(void)
580 g_hash_table_destroy(config_table);
584 static char *config_pem_fsid(const char *pem_file)
587 unsigned *fsid = (unsigned *) &buf.f_fsid;
588 unsigned long long fsid64;
590 if (pem_file == NULL)
593 if (statfs(pem_file, &buf) < 0) {
594 connman_error("statfs error %s for %s",
595 strerror(errno), pem_file);
599 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
601 return g_strdup_printf("%llx", fsid64);
604 static void provision_service(gpointer key, gpointer value, gpointer user_data)
606 struct connman_service *service = user_data;
607 struct connman_config_service *config = value;
608 struct connman_network *network;
610 unsigned int ssid_len;
612 /* For now only WiFi service entries are supported */
613 if (g_strcmp0(config->type, "wifi") != 0)
616 network = __connman_service_get_network(service);
617 if (network == NULL) {
618 connman_error("Service has no network set");
622 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
624 connman_error("Network SSID not set");
628 if (config->ssid == NULL || ssid_len != config->ssid_len)
631 if (memcmp(config->ssid, ssid, ssid_len) != 0)
634 __connman_service_set_immutable(service, TRUE);
635 __connman_service_set_favorite(service, TRUE);
637 if (config->eap != NULL)
638 __connman_service_set_string(service, "EAP", config->eap);
640 if (config->identity != NULL)
641 __connman_service_set_string(service, "Identity",
644 if (config->ca_cert_file != NULL)
645 __connman_service_set_string(service, "CACertFile",
646 config->ca_cert_file);
648 if (config->client_cert_file != NULL)
649 __connman_service_set_string(service, "ClientCertFile",
650 config->client_cert_file);
652 if (config->private_key_file != NULL)
653 __connman_service_set_string(service, "PrivateKeyFile",
654 config->private_key_file);
656 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
657 config->private_key_file != NULL) {
660 fsid = config_pem_fsid(config->private_key_file);
664 g_free(config->private_key_passphrase);
665 config->private_key_passphrase = fsid;
668 if (config->private_key_passphrase != NULL) {
669 __connman_service_set_string(service, "PrivateKeyPassphrase",
670 config->private_key_passphrase);
672 * TODO: Support for PEAP with both identity and key passwd.
673 * In that case, we should check if both of them are found
674 * from the config file. If not, we should not set the
675 * service passphrase in order for the UI to request for an
676 * additional passphrase.
680 if (config->phase2 != NULL)
681 __connman_service_set_string(service, "Phase2", config->phase2);
683 if (config->passphrase != NULL)
684 __connman_service_set_string(service, "Passphrase", config->passphrase);
687 int __connman_config_provision_service(struct connman_service *service)
689 enum connman_service_type type;
693 DBG("service %p", service);
695 /* For now only WiFi services are supported */
696 type = connman_service_get_type(service);
697 if (type != CONNMAN_SERVICE_TYPE_WIFI)
700 g_hash_table_iter_init(&iter, config_table);
702 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
703 struct connman_config *config = value;
705 g_hash_table_foreach(config->service_table,
706 provision_service, service);