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 connman_bool_t protected;
57 GHashTable *service_table;
60 static GHashTable *config_table = NULL;
61 static GSList *protected_services = NULL;
63 static int inotify_wd = -1;
65 static GIOChannel *inotify_channel = NULL;
66 static uint inotify_watch = 0;
68 #define INTERNAL_CONFIG_PREFIX "__internal"
70 /* Definition of possible strings in the .config files */
71 #define CONFIG_KEY_NAME "Name"
72 #define CONFIG_KEY_DESC "Description"
73 #define CONFIG_KEY_PROT "Protected"
75 #define SERVICE_KEY_TYPE "Type"
76 #define SERVICE_KEY_NAME "Name"
77 #define SERVICE_KEY_SSID "SSID"
78 #define SERVICE_KEY_EAP "EAP"
79 #define SERVICE_KEY_CA_CERT "CACertFile"
80 #define SERVICE_KEY_CL_CERT "ClientCertFile"
81 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
82 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
83 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
84 #define SERVICE_KEY_IDENTITY "Identity"
85 #define SERVICE_KEY_PHASE2 "Phase2"
86 #define SERVICE_KEY_PASSPHRASE "Passphrase"
88 static const char *config_possible_keys[] = {
95 static const char *service_possible_keys[] = {
103 SERVICE_KEY_PRV_KEY_PASS,
104 SERVICE_KEY_PRV_KEY_PASS_TYPE,
105 SERVICE_KEY_IDENTITY,
107 SERVICE_KEY_PASSPHRASE,
111 static void unregister_config(gpointer data)
113 struct connman_config *config = data;
115 connman_info("Removing configuration %s", config->ident);
117 g_hash_table_destroy(config->service_table);
119 g_free(config->description);
120 g_free(config->name);
121 g_free(config->ident);
125 static void unregister_service(gpointer data)
127 struct connman_config_service *service = data;
129 connman_info("Removing service configuration %s", service->ident);
131 protected_services = g_slist_remove(protected_services, service);
133 g_free(service->ident);
134 g_free(service->type);
135 g_free(service->name);
136 g_free(service->ssid);
137 g_free(service->eap);
138 g_free(service->identity);
139 g_free(service->ca_cert_file);
140 g_free(service->client_cert_file);
141 g_free(service->private_key_file);
142 g_free(service->private_key_passphrase);
143 g_free(service->private_key_passphrase_type);
144 g_free(service->phase2);
145 g_free(service->passphrase);
149 static void check_keys(GKeyFile *keyfile, const char *group,
150 const char **possible_keys)
153 gsize nb_avail_keys, i, j;
155 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
156 if (avail_keys == NULL)
160 * For each key in the configuration file,
161 * verify it is understood by connman
163 for (i = 0 ; i < nb_avail_keys; i++) {
164 for (j = 0; possible_keys[j] ; j++)
165 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
168 if (possible_keys[j] == NULL)
169 connman_warn("Unknown configuration key %s in [%s]",
170 avail_keys[i], group);
173 g_strfreev(avail_keys);
176 static connman_bool_t
177 is_protected_service(struct connman_config_service *service)
181 DBG("ident %s", service->ident);
183 for (list = protected_services; list; list = list->next) {
184 struct connman_config_service *s = list->data;
186 if (g_strcmp0(s->type, service->type) != 0)
189 if (s->ssid == NULL || service->ssid == NULL)
192 if (s->ssid_len != service->ssid_len)
195 if (g_strcmp0(service->type, "wifi") == 0 &&
196 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
204 static int load_service(GKeyFile *keyfile, const char *group,
205 struct connman_config *config)
207 struct connman_config_service *service;
209 char *str, *hex_ssid;
210 gboolean service_created = FALSE;
213 /* Strip off "service_" prefix */
216 if (strlen(ident) < 1)
219 /* Verify that provided keys are good */
220 check_keys(keyfile, group, service_possible_keys);
222 service = g_hash_table_lookup(config->service_table, ident);
223 if (service == NULL) {
224 service = g_try_new0(struct connman_config_service, 1);
228 service->ident = g_strdup(ident);
230 service_created = TRUE;
233 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
235 g_free(service->type);
239 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
241 g_free(service->name);
245 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
247 if (hex_ssid != NULL) {
249 unsigned int i, j = 0, hex;
250 size_t hex_ssid_len = strlen(hex_ssid);
252 ssid = g_try_malloc0(hex_ssid_len / 2);
259 for (i = 0; i < hex_ssid_len; i += 2) {
260 sscanf(hex_ssid + i, "%02x", &hex);
266 g_free(service->ssid);
267 service->ssid = ssid;
268 service->ssid_len = hex_ssid_len / 2;
269 } else if (service->name != NULL) {
271 unsigned int ssid_len;
273 ssid_len = strlen(service->name);
274 ssid = g_try_malloc0(ssid_len);
280 memcpy(ssid, service->name, ssid_len);
281 g_free(service->ssid);
282 service->ssid = ssid;
283 service->ssid_len = ssid_len;
286 if (is_protected_service(service) == TRUE) {
287 connman_error("Trying to provision a protected service");
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;
350 g_hash_table_insert(config->service_table, service->ident,
353 if (config->protected == TRUE)
355 g_slist_append(protected_services, service);
357 connman_info("Adding service configuration %s", service->ident);
362 if (service_created == TRUE) {
363 g_free(service->ident);
364 g_free(service->type);
365 g_free(service->name);
366 g_free(service->ssid);
373 static int load_config(struct connman_config *config)
376 GError *error = NULL;
383 DBG("config %p", config);
385 keyfile = __connman_storage_open_config(config->ident);
389 /* Verify keys validity of the global section */
390 check_keys(keyfile, "global", config_possible_keys);
392 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
394 g_free(config->name);
398 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
400 g_free(config->description);
401 config->description = str;
404 protected = g_key_file_get_boolean(keyfile, "global",
405 CONFIG_KEY_PROT, &error);
407 config->protected = protected;
409 config->protected = TRUE;
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");
485 g_key_file_set_boolean(keyfile, "global", CONFIG_KEY_PROT, FALSE);
487 content = g_key_file_to_data(keyfile, &content_length, NULL);
488 if (content == NULL) {
493 filename = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
494 if (filename == NULL) {
499 DBG("Saving %zu bytes to %s", content_length, service_name);
501 if (g_file_set_contents(filename, content,
502 content_length, NULL) == FALSE) {
517 static connman_bool_t validate_ident(const char *ident)
524 for (i = 0; i < strlen(ident); i++)
525 if (g_ascii_isprint(ident[i]) == FALSE)
531 static int read_configs(void)
537 dir = g_dir_open(STORAGEDIR, 0, NULL);
541 while ((file = g_dir_read_name(dir)) != NULL) {
545 if (g_str_has_suffix(file, ".config") == FALSE)
548 ident = g_strrstr(file, ".config");
552 str = g_string_new_len(file, ident - file);
556 ident = g_string_free(str, FALSE);
558 if (validate_ident(ident) == TRUE) {
559 struct connman_config *config;
561 config = create_config(ident);
565 connman_error("Invalid config ident %s", ident);
576 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
584 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
589 status = g_io_channel_read_chars(channel, buffer,
590 sizeof(buffer) -1, &bytes_read, NULL);
593 case G_IO_STATUS_NORMAL:
595 case G_IO_STATUS_AGAIN:
598 connman_error("Reading from inotify channel failed");
605 while (bytes_read > 0) {
606 struct inotify_event *event;
611 event = (struct inotify_event *) next_event;
613 ident = next_event + sizeof(struct inotify_event);
617 len = sizeof(struct inotify_event) + event->len;
619 /* check if inotify_event block fit */
620 if (len > bytes_read)
629 if (g_str_has_suffix(ident, ".config") == FALSE)
632 ext = g_strrstr(ident, ".config");
638 if (validate_ident(ident) == FALSE) {
639 connman_error("Invalid config ident %s", ident);
643 if (event->mask & IN_CREATE)
644 create_config(ident);
646 if (event->mask & IN_MODIFY) {
647 struct connman_config *config;
649 config = g_hash_table_lookup(config_table, ident);
650 if (config != NULL) {
651 g_hash_table_remove_all(config->service_table);
656 if (event->mask & IN_DELETE)
657 g_hash_table_remove(config_table, ident);
663 static int create_watch(void)
671 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
672 IN_MODIFY | IN_CREATE | IN_DELETE);
673 if (inotify_wd < 0) {
674 connman_error("Creation of STORAGEDIR watch failed");
679 inotify_channel = g_io_channel_unix_new(fd);
680 if (inotify_channel == NULL) {
681 connman_error("Creation of inotify channel failed");
682 inotify_rm_watch(fd, inotify_wd);
689 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
690 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
691 g_io_channel_set_buffered(inotify_channel, FALSE);
693 inotify_watch = g_io_add_watch(inotify_channel,
694 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
700 static void remove_watch(void)
704 if (inotify_channel == NULL)
707 if (inotify_watch > 0) {
708 g_source_remove(inotify_watch);
712 fd = g_io_channel_unix_get_fd(inotify_channel);
714 if (inotify_wd >= 0) {
715 inotify_rm_watch(fd, inotify_wd);
719 g_io_channel_unref(inotify_channel);
722 int __connman_config_init(void)
726 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
727 NULL, unregister_config);
731 return read_configs();
734 void __connman_config_cleanup(void)
740 g_hash_table_destroy(config_table);
744 static char *config_pem_fsid(const char *pem_file)
747 unsigned *fsid = (unsigned *) &buf.f_fsid;
748 unsigned long long fsid64;
750 if (pem_file == NULL)
753 if (statfs(pem_file, &buf) < 0) {
754 connman_error("statfs error %s for %s",
755 strerror(errno), pem_file);
759 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
761 return g_strdup_printf("%llx", fsid64);
764 static void provision_service(gpointer key, gpointer value, gpointer user_data)
766 struct connman_service *service = user_data;
767 struct connman_config_service *config = value;
768 struct connman_network *network;
770 unsigned int ssid_len;
772 /* For now only WiFi service entries are supported */
773 if (g_strcmp0(config->type, "wifi") != 0)
776 network = __connman_service_get_network(service);
777 if (network == NULL) {
778 connman_error("Service has no network set");
782 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
784 connman_error("Network SSID not set");
788 if (config->ssid == NULL || ssid_len != config->ssid_len)
791 if (memcmp(config->ssid, ssid, ssid_len) != 0)
794 __connman_service_set_immutable(service, TRUE);
796 __connman_service_set_favorite(service, TRUE);
798 if (config->eap != NULL)
799 __connman_service_set_string(service, "EAP", config->eap);
801 if (config->identity != NULL)
802 __connman_service_set_string(service, "Identity",
805 if (config->ca_cert_file != NULL)
806 __connman_service_set_string(service, "CACertFile",
807 config->ca_cert_file);
809 if (config->client_cert_file != NULL)
810 __connman_service_set_string(service, "ClientCertFile",
811 config->client_cert_file);
813 if (config->private_key_file != NULL)
814 __connman_service_set_string(service, "PrivateKeyFile",
815 config->private_key_file);
817 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
818 config->private_key_file != NULL) {
821 fsid = config_pem_fsid(config->private_key_file);
825 g_free(config->private_key_passphrase);
826 config->private_key_passphrase = fsid;
829 if (config->private_key_passphrase != NULL) {
830 __connman_service_set_string(service, "PrivateKeyPassphrase",
831 config->private_key_passphrase);
833 * TODO: Support for PEAP with both identity and key passwd.
834 * In that case, we should check if both of them are found
835 * from the config file. If not, we should not set the
836 * service passphrase in order for the UI to request for an
837 * additional passphrase.
841 if (config->phase2 != NULL)
842 __connman_service_set_string(service, "Phase2", config->phase2);
844 if (config->passphrase != NULL)
845 __connman_service_set_string(service, "Passphrase", config->passphrase);
848 int __connman_config_provision_service(struct connman_service *service)
850 enum connman_service_type type;
854 DBG("service %p", service);
856 /* For now only WiFi services are supported */
857 type = connman_service_get_type(service);
858 if (type != CONNMAN_SERVICE_TYPE_WIFI)
861 g_hash_table_iter_init(&iter, config_table);
863 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
864 struct connman_config *config = value;
866 g_hash_table_foreach(config->service_table,
867 provision_service, service);