5 * Copyright (C) 2007-2012 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
31 #include <sys/inotify.h>
36 struct connman_config_service {
41 unsigned int ssid_len;
45 char *client_cert_file;
46 char *private_key_file;
47 char *private_key_passphrase;
48 char *private_key_passphrase_type;
51 GSList *service_identifiers;
54 struct connman_config {
58 connman_bool_t protected;
59 GHashTable *service_table;
62 static GHashTable *config_table = NULL;
63 static GSList *protected_services = NULL;
65 static int inotify_wd = -1;
67 static GIOChannel *inotify_channel = NULL;
68 static uint inotify_watch = 0;
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 *config_service = data;
130 struct connman_service *service;
134 connman_info("Removing service configuration %s",
135 config_service->ident);
137 protected_services = g_slist_remove(protected_services,
140 for (list = config_service->service_identifiers; list != NULL;
142 service_id = list->data;
144 service = __connman_service_lookup_from_ident(service_id);
145 if (service != NULL) {
146 __connman_service_set_immutable(service, FALSE);
147 __connman_service_remove(service);
150 if (__connman_storage_remove_service(service_id) == FALSE)
151 DBG("Could not remove all files for service %s",
155 g_free(config_service->ident);
156 g_free(config_service->type);
157 g_free(config_service->name);
158 g_free(config_service->ssid);
159 g_free(config_service->eap);
160 g_free(config_service->identity);
161 g_free(config_service->ca_cert_file);
162 g_free(config_service->client_cert_file);
163 g_free(config_service->private_key_file);
164 g_free(config_service->private_key_passphrase);
165 g_free(config_service->private_key_passphrase_type);
166 g_free(config_service->phase2);
167 g_free(config_service->passphrase);
168 g_slist_free_full(config_service->service_identifiers, g_free);
169 g_free(config_service);
172 static void check_keys(GKeyFile *keyfile, const char *group,
173 const char **possible_keys)
176 gsize nb_avail_keys, i, j;
178 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
179 if (avail_keys == NULL)
183 * For each key in the configuration file,
184 * verify it is understood by connman
186 for (i = 0 ; i < nb_avail_keys; i++) {
187 for (j = 0; possible_keys[j] ; j++)
188 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
191 if (possible_keys[j] == NULL)
192 connman_warn("Unknown configuration key %s in [%s]",
193 avail_keys[i], group);
196 g_strfreev(avail_keys);
199 static connman_bool_t
200 is_protected_service(struct connman_config_service *service)
204 DBG("ident %s", service->ident);
206 for (list = protected_services; list; list = list->next) {
207 struct connman_config_service *s = list->data;
209 if (g_strcmp0(s->type, service->type) != 0)
212 if (s->ssid == NULL || service->ssid == NULL)
215 if (s->ssid_len != service->ssid_len)
218 if (g_strcmp0(service->type, "wifi") == 0 &&
219 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
227 static int load_service(GKeyFile *keyfile, const char *group,
228 struct connman_config *config)
230 struct connman_config_service *service;
232 char *str, *hex_ssid;
233 gboolean service_created = FALSE;
236 /* Strip off "service_" prefix */
239 if (strlen(ident) < 1)
242 /* Verify that provided keys are good */
243 check_keys(keyfile, group, service_possible_keys);
245 service = g_hash_table_lookup(config->service_table, ident);
246 if (service == NULL) {
247 service = g_try_new0(struct connman_config_service, 1);
251 service->ident = g_strdup(ident);
253 service_created = TRUE;
256 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
258 g_free(service->type);
262 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
264 g_free(service->name);
268 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
270 if (hex_ssid != NULL) {
272 unsigned int i, j = 0, hex;
273 size_t hex_ssid_len = strlen(hex_ssid);
275 ssid = g_try_malloc0(hex_ssid_len / 2);
282 for (i = 0; i < hex_ssid_len; i += 2) {
283 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
284 connman_warn("Invalid SSID %s", hex_ssid);
295 g_free(service->ssid);
296 service->ssid = ssid;
297 service->ssid_len = hex_ssid_len / 2;
298 } else if (service->name != NULL) {
300 unsigned int ssid_len;
302 ssid_len = strlen(service->name);
303 ssid = g_try_malloc0(ssid_len);
309 memcpy(ssid, service->name, ssid_len);
310 g_free(service->ssid);
311 service->ssid = ssid;
312 service->ssid_len = ssid_len;
315 if (is_protected_service(service) == TRUE) {
316 connman_error("Trying to provision a protected service");
321 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
323 g_free(service->eap);
327 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
329 g_free(service->ca_cert_file);
330 service->ca_cert_file = str;
333 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
335 g_free(service->client_cert_file);
336 service->client_cert_file = str;
339 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
341 g_free(service->private_key_file);
342 service->private_key_file = str;
345 str = g_key_file_get_string(keyfile, group,
346 SERVICE_KEY_PRV_KEY_PASS, NULL);
348 g_free(service->private_key_passphrase);
349 service->private_key_passphrase = str;
352 str = g_key_file_get_string(keyfile, group,
353 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
355 g_free(service->private_key_passphrase_type);
356 service->private_key_passphrase_type = str;
359 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
361 g_free(service->identity);
362 service->identity = str;
365 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
367 g_free(service->phase2);
368 service->phase2 = str;
371 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
374 g_free(service->passphrase);
375 service->passphrase = str;
379 g_hash_table_insert(config->service_table, service->ident,
382 if (config->protected == TRUE)
384 g_slist_append(protected_services, service);
386 connman_info("Adding service configuration %s", service->ident);
391 if (service_created == TRUE) {
392 g_free(service->ident);
393 g_free(service->type);
394 g_free(service->name);
395 g_free(service->ssid);
402 static int load_config(struct connman_config *config)
405 GError *error = NULL;
409 gboolean protected, found = FALSE;
412 DBG("config %p", config);
414 keyfile = __connman_storage_load_config(config->ident);
418 /* Verify keys validity of the global section */
419 check_keys(keyfile, "global", config_possible_keys);
421 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
423 g_free(config->name);
427 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
429 g_free(config->description);
430 config->description = str;
433 protected = g_key_file_get_boolean(keyfile, "global",
434 CONFIG_KEY_PROT, &error);
436 config->protected = protected;
438 config->protected = TRUE;
440 groups = g_key_file_get_groups(keyfile, &length);
442 for (i = 0; groups[i] != NULL; i++) {
443 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
444 if (load_service(keyfile, groups[i], config) == 0)
450 connman_warn("Config file %s/%s.config does not contain any "
451 "configuration that can be provisioned!",
452 STORAGEDIR, config->ident);
456 g_key_file_free(keyfile);
461 static struct connman_config *create_config(const char *ident)
463 struct connman_config *config;
465 DBG("ident %s", ident);
467 if (g_hash_table_lookup(config_table, ident) != NULL)
470 config = g_try_new0(struct connman_config, 1);
474 config->ident = g_strdup(ident);
476 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
477 NULL, unregister_service);
479 g_hash_table_insert(config_table, config->ident, config);
481 connman_info("Adding configuration %s", config->ident);
486 static connman_bool_t validate_ident(const char *ident)
493 for (i = 0; i < strlen(ident); i++)
494 if (g_ascii_isprint(ident[i]) == FALSE)
500 static int read_configs(void)
506 dir = g_dir_open(STORAGEDIR, 0, NULL);
510 while ((file = g_dir_read_name(dir)) != NULL) {
514 if (g_str_has_suffix(file, ".config") == FALSE)
517 ident = g_strrstr(file, ".config");
521 str = g_string_new_len(file, ident - file);
525 ident = g_string_free(str, FALSE);
527 if (validate_ident(ident) == TRUE) {
528 struct connman_config *config;
530 config = create_config(ident);
534 connman_error("Invalid config ident %s", ident);
545 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
553 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
558 status = g_io_channel_read_chars(channel, buffer,
559 sizeof(buffer) -1, &bytes_read, NULL);
562 case G_IO_STATUS_NORMAL:
564 case G_IO_STATUS_AGAIN:
567 connman_error("Reading from inotify channel failed");
574 while (bytes_read > 0) {
575 struct inotify_event *event;
580 event = (struct inotify_event *) next_event;
582 ident = next_event + sizeof(struct inotify_event);
586 len = sizeof(struct inotify_event) + event->len;
588 /* check if inotify_event block fit */
589 if (len > bytes_read)
598 if (g_str_has_suffix(ident, ".config") == FALSE)
601 ext = g_strrstr(ident, ".config");
607 if (validate_ident(ident) == FALSE) {
608 connman_error("Invalid config ident %s", ident);
612 if (event->mask & IN_CREATE)
613 create_config(ident);
615 if (event->mask & IN_MODIFY) {
616 struct connman_config *config;
618 config = g_hash_table_lookup(config_table, ident);
619 if (config != NULL) {
620 g_hash_table_remove_all(config->service_table);
622 __connman_service_provision_changed(ident);
626 if (event->mask & IN_DELETE)
627 g_hash_table_remove(config_table, ident);
633 static int create_watch(void)
641 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
642 IN_MODIFY | IN_CREATE | IN_DELETE);
643 if (inotify_wd < 0) {
644 connman_error("Creation of STORAGEDIR watch failed");
649 inotify_channel = g_io_channel_unix_new(fd);
650 if (inotify_channel == NULL) {
651 connman_error("Creation of inotify channel failed");
652 inotify_rm_watch(fd, inotify_wd);
659 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
660 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
661 g_io_channel_set_buffered(inotify_channel, FALSE);
663 inotify_watch = g_io_add_watch(inotify_channel,
664 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
670 static void remove_watch(void)
674 if (inotify_channel == NULL)
677 if (inotify_watch > 0) {
678 g_source_remove(inotify_watch);
682 fd = g_io_channel_unix_get_fd(inotify_channel);
684 if (inotify_wd >= 0) {
685 inotify_rm_watch(fd, inotify_wd);
689 g_io_channel_unref(inotify_channel);
692 int __connman_config_init(void)
696 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
697 NULL, unregister_config);
701 return read_configs();
704 void __connman_config_cleanup(void)
710 g_hash_table_destroy(config_table);
714 static char *config_pem_fsid(const char *pem_file)
717 unsigned *fsid = (unsigned *) &buf.f_fsid;
718 unsigned long long fsid64;
720 if (pem_file == NULL)
723 if (statfs(pem_file, &buf) < 0) {
724 connman_error("statfs error %s for %s",
725 strerror(errno), pem_file);
729 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
731 return g_strdup_printf("%llx", fsid64);
734 static void provision_service(gpointer key, gpointer value, gpointer user_data)
736 struct connman_service *service = user_data;
737 struct connman_config_service *config = value;
738 struct connman_network *network;
739 const void *ssid, *service_id;
740 unsigned int ssid_len;
742 /* For now only WiFi service entries are supported */
743 if (g_strcmp0(config->type, "wifi") != 0)
746 network = __connman_service_get_network(service);
747 if (network == NULL) {
748 connman_error("Service has no network set");
752 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
754 connman_error("Network SSID not set");
758 if (config->ssid == NULL || ssid_len != config->ssid_len)
761 if (memcmp(config->ssid, ssid, ssid_len) != 0)
764 service_id = __connman_service_get_ident(service);
765 config->service_identifiers =
766 g_slist_prepend(config->service_identifiers,
767 g_strdup(service_id));
769 __connman_service_set_immutable(service, TRUE);
771 __connman_service_set_favorite(service, TRUE);
773 if (config->eap != NULL)
774 __connman_service_set_string(service, "EAP", config->eap);
776 if (config->identity != NULL)
777 __connman_service_set_string(service, "Identity",
780 if (config->ca_cert_file != NULL)
781 __connman_service_set_string(service, "CACertFile",
782 config->ca_cert_file);
784 if (config->client_cert_file != NULL)
785 __connman_service_set_string(service, "ClientCertFile",
786 config->client_cert_file);
788 if (config->private_key_file != NULL)
789 __connman_service_set_string(service, "PrivateKeyFile",
790 config->private_key_file);
792 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
793 config->private_key_file != NULL) {
796 fsid = config_pem_fsid(config->private_key_file);
800 g_free(config->private_key_passphrase);
801 config->private_key_passphrase = fsid;
804 if (config->private_key_passphrase != NULL) {
805 __connman_service_set_string(service, "PrivateKeyPassphrase",
806 config->private_key_passphrase);
808 * TODO: Support for PEAP with both identity and key passwd.
809 * In that case, we should check if both of them are found
810 * from the config file. If not, we should not set the
811 * service passphrase in order for the UI to request for an
812 * additional passphrase.
816 if (config->phase2 != NULL)
817 __connman_service_set_string(service, "Phase2", config->phase2);
819 if (config->passphrase != NULL)
820 __connman_service_set_string(service, "Passphrase", config->passphrase);
823 int __connman_config_provision_service(struct connman_service *service)
825 enum connman_service_type type;
829 DBG("service %p", service);
831 /* For now only WiFi services are supported */
832 type = connman_service_get_type(service);
833 if (type != CONNMAN_SERVICE_TYPE_WIFI)
836 g_hash_table_iter_init(&iter, config_table);
838 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
839 struct connman_config *config = value;
841 g_hash_table_foreach(config->service_table,
842 provision_service, service);
848 int __connman_config_provision_service_ident(struct connman_service *service,
851 enum connman_service_type type;
852 struct connman_config *config;
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 config = g_hash_table_lookup(config_table, ident);
863 g_hash_table_foreach(config->service_table,
864 provision_service, service);