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;
174 gboolean service_created = FALSE;
176 /* Strip off "service_" prefix */
179 if (strlen(ident) < 1)
182 /* Verify that provided keys are good */
183 check_keys(keyfile, group, service_possible_keys);
185 service = g_hash_table_lookup(config->service_table, ident);
186 if (service == NULL) {
187 service = g_try_new0(struct connman_config_service, 1);
191 service->ident = g_strdup(ident);
193 service_created = TRUE;
196 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
198 g_free(service->type);
202 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
204 g_free(service->name);
208 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
210 if (hex_ssid != NULL) {
212 unsigned int i, j = 0, hex;
213 size_t hex_ssid_len = strlen(hex_ssid);
215 ssid = g_try_malloc0(hex_ssid_len / 2);
221 for (i = 0; i < hex_ssid_len; i += 2) {
222 sscanf(hex_ssid + i, "%02x", &hex);
228 g_free(service->ssid);
229 service->ssid = ssid;
230 service->ssid_len = hex_ssid_len / 2;
231 } else if (service->name != NULL) {
233 unsigned int ssid_len;
235 ssid_len = strlen(service->name);
236 ssid = g_try_malloc0(ssid_len);
240 memcpy(ssid, service->name, ssid_len);
241 g_free(service->ssid);
242 service->ssid = ssid;
243 service->ssid_len = ssid_len;
246 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
248 g_free(service->eap);
252 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
254 g_free(service->ca_cert_file);
255 service->ca_cert_file = str;
258 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
260 g_free(service->client_cert_file);
261 service->client_cert_file = str;
264 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
266 g_free(service->private_key_file);
267 service->private_key_file = str;
270 str = g_key_file_get_string(keyfile, group,
271 SERVICE_KEY_PRV_KEY_PASS, NULL);
273 g_free(service->private_key_passphrase);
274 service->private_key_passphrase = str;
277 str = g_key_file_get_string(keyfile, group,
278 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
280 g_free(service->private_key_passphrase_type);
281 service->private_key_passphrase_type = str;
284 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
286 g_free(service->identity);
287 service->identity = str;
290 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
292 g_free(service->phase2);
293 service->phase2 = str;
296 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
299 g_free(service->passphrase);
300 service->passphrase = str;
304 g_hash_table_insert(config->service_table, service->ident,
307 connman_info("Adding service configuration %s", service->ident);
312 static int load_config(struct connman_config *config)
320 DBG("config %p", config);
322 keyfile = __connman_storage_open_config(config->ident);
326 /* Verify keys validity of the global section */
327 check_keys(keyfile, "global", config_possible_keys);
329 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
331 g_free(config->name);
335 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
337 g_free(config->description);
338 config->description = str;
341 groups = g_key_file_get_groups(keyfile, &length);
343 for (i = 0; groups[i] != NULL; i++) {
344 if (g_str_has_prefix(groups[i], "service_") == TRUE)
345 load_service(keyfile, groups[i], config);
350 __connman_storage_close_config(config->ident, keyfile, FALSE);
355 static struct connman_config *create_config(const char *ident)
357 struct connman_config *config;
359 DBG("ident %s", ident);
361 if (g_hash_table_lookup(config_table, ident) != NULL)
364 config = g_try_new0(struct connman_config, 1);
368 config->ident = g_strdup(ident);
370 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
371 NULL, unregister_service);
373 g_hash_table_insert(config_table, config->ident, config);
375 connman_info("Adding configuration %s", config->ident);
380 static int read_configs(void)
386 dir = g_dir_open(STORAGEDIR, 0, NULL);
390 while ((file = g_dir_read_name(dir)) != NULL) {
394 if (g_str_has_suffix(file, ".config") == FALSE)
397 ident = g_strrstr(file, ".config");
401 str = g_string_new_len(file, ident - file);
405 ident = g_string_free(str, FALSE);
407 if (connman_dbus_validate_ident(ident) == TRUE) {
408 struct connman_config *config;
410 config = create_config(ident);
423 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
431 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
436 status = g_io_channel_read_chars(channel, buffer,
437 sizeof(buffer) -1, &bytes_read, NULL);
440 case G_IO_STATUS_NORMAL:
442 case G_IO_STATUS_AGAIN:
445 connman_error("Reading from inotify channel failed");
452 while (bytes_read > 0) {
453 struct inotify_event *event;
458 event = (struct inotify_event *) next_event;
460 ident = next_event + sizeof(struct inotify_event);
464 len = sizeof(struct inotify_event) + event->len;
466 /* check if inotify_event block fit */
467 if (len > bytes_read)
476 if (g_str_has_suffix(ident, ".config") == FALSE)
479 ext = g_strrstr(ident, ".config");
485 if (connman_dbus_validate_ident(ident) == FALSE)
488 if (event->mask & IN_CREATE)
489 create_config(ident);
491 if (event->mask & IN_MODIFY) {
492 struct connman_config *config;
494 config = g_hash_table_lookup(config_table, ident);
495 if (config != NULL) {
496 g_hash_table_remove_all(config->service_table);
501 if (event->mask & IN_DELETE)
502 g_hash_table_remove(config_table, ident);
508 static int create_watch(void)
516 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
517 IN_MODIFY | IN_CREATE | IN_DELETE);
518 if (inotify_wd < 0) {
519 connman_error("Creation of STORAGEDIR watch failed");
524 inotify_channel = g_io_channel_unix_new(fd);
525 if (inotify_channel == NULL) {
526 connman_error("Creation of inotify channel failed");
527 inotify_rm_watch(fd, inotify_wd);
534 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
535 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
536 g_io_channel_set_buffered(inotify_channel, FALSE);
538 inotify_watch = g_io_add_watch(inotify_channel,
539 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
545 static void remove_watch(void)
549 if (inotify_channel == NULL)
552 if (inotify_watch > 0) {
553 g_source_remove(inotify_watch);
557 fd = g_io_channel_unix_get_fd(inotify_channel);
559 if (inotify_wd >= 0) {
560 inotify_rm_watch(fd, inotify_wd);
564 g_io_channel_unref(inotify_channel);
567 int __connman_config_init(void)
571 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
572 NULL, unregister_config);
576 return read_configs();
579 void __connman_config_cleanup(void)
585 g_hash_table_destroy(config_table);
589 static char *config_pem_fsid(const char *pem_file)
592 unsigned *fsid = (unsigned *) &buf.f_fsid;
593 unsigned long long fsid64;
595 if (pem_file == NULL)
598 if (statfs(pem_file, &buf) < 0) {
599 connman_error("statfs error %s for %s",
600 strerror(errno), pem_file);
604 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
606 return g_strdup_printf("%llx", fsid64);
609 static void provision_service(gpointer key, gpointer value, gpointer user_data)
611 struct connman_service *service = user_data;
612 struct connman_config_service *config = value;
613 struct connman_network *network;
615 unsigned int ssid_len;
617 /* For now only WiFi service entries are supported */
618 if (g_strcmp0(config->type, "wifi") != 0)
621 network = __connman_service_get_network(service);
622 if (network == NULL) {
623 connman_error("Service has no network set");
627 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
629 connman_error("Network SSID not set");
633 if (config->ssid == NULL || ssid_len != config->ssid_len)
636 if (memcmp(config->ssid, ssid, ssid_len) != 0)
639 __connman_service_set_immutable(service, TRUE);
640 __connman_service_set_favorite(service, TRUE);
642 if (config->eap != NULL)
643 __connman_service_set_string(service, "EAP", config->eap);
645 if (config->identity != NULL)
646 __connman_service_set_string(service, "Identity",
649 if (config->ca_cert_file != NULL)
650 __connman_service_set_string(service, "CACertFile",
651 config->ca_cert_file);
653 if (config->client_cert_file != NULL)
654 __connman_service_set_string(service, "ClientCertFile",
655 config->client_cert_file);
657 if (config->private_key_file != NULL)
658 __connman_service_set_string(service, "PrivateKeyFile",
659 config->private_key_file);
661 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
662 config->private_key_file != NULL) {
665 fsid = config_pem_fsid(config->private_key_file);
669 g_free(config->private_key_passphrase);
670 config->private_key_passphrase = fsid;
673 if (config->private_key_passphrase != NULL) {
674 __connman_service_set_string(service, "PrivateKeyPassphrase",
675 config->private_key_passphrase);
677 * TODO: Support for PEAP with both identity and key passwd.
678 * In that case, we should check if both of them are found
679 * from the config file. If not, we should not set the
680 * service passphrase in order for the UI to request for an
681 * additional passphrase.
685 if (config->phase2 != NULL)
686 __connman_service_set_string(service, "Phase2", config->phase2);
688 if (config->passphrase != NULL)
689 __connman_service_set_string(service, "Passphrase", config->passphrase);
692 int __connman_config_provision_service(struct connman_service *service)
694 enum connman_service_type type;
698 DBG("service %p", service);
700 /* For now only WiFi services are supported */
701 type = connman_service_get_type(service);
702 if (type != CONNMAN_SERVICE_TYPE_WIFI)
705 g_hash_table_iter_init(&iter, config_table);
707 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
708 struct connman_config *config = value;
710 g_hash_table_foreach(config->service_table,
711 provision_service, service);