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;
52 char *config_ident; /* file prefix */
53 char *config_entry; /* entry name */
56 struct connman_config {
60 connman_bool_t protected;
61 GHashTable *service_table;
64 static GHashTable *config_table = NULL;
65 static GSList *protected_services = NULL;
67 static int inotify_wd = -1;
69 static GIOChannel *inotify_channel = NULL;
70 static uint inotify_watch = 0;
72 #define INTERNAL_CONFIG_PREFIX "__internal"
74 /* Definition of possible strings in the .config files */
75 #define CONFIG_KEY_NAME "Name"
76 #define CONFIG_KEY_DESC "Description"
77 #define CONFIG_KEY_PROT "Protected"
79 #define SERVICE_KEY_TYPE "Type"
80 #define SERVICE_KEY_NAME "Name"
81 #define SERVICE_KEY_SSID "SSID"
82 #define SERVICE_KEY_EAP "EAP"
83 #define SERVICE_KEY_CA_CERT "CACertFile"
84 #define SERVICE_KEY_CL_CERT "ClientCertFile"
85 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
86 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
87 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
88 #define SERVICE_KEY_IDENTITY "Identity"
89 #define SERVICE_KEY_PHASE2 "Phase2"
90 #define SERVICE_KEY_PASSPHRASE "Passphrase"
92 static const char *config_possible_keys[] = {
99 static const char *service_possible_keys[] = {
107 SERVICE_KEY_PRV_KEY_PASS,
108 SERVICE_KEY_PRV_KEY_PASS_TYPE,
109 SERVICE_KEY_IDENTITY,
111 SERVICE_KEY_PASSPHRASE,
115 static void unregister_config(gpointer data)
117 struct connman_config *config = data;
119 connman_info("Removing configuration %s", config->ident);
121 g_hash_table_destroy(config->service_table);
123 g_free(config->description);
124 g_free(config->name);
125 g_free(config->ident);
129 static void unregister_service(gpointer data)
131 struct connman_config_service *config_service = data;
132 struct connman_service *service;
136 connman_info("Removing service configuration %s",
137 config_service->ident);
139 protected_services = g_slist_remove(protected_services,
142 for (list = config_service->service_identifiers; list != NULL;
144 service_id = list->data;
146 service = __connman_service_lookup_from_ident(service_id);
147 if (service != NULL) {
148 __connman_service_set_immutable(service, FALSE);
149 __connman_service_remove(service);
152 if (__connman_storage_remove_service(service_id) == FALSE)
153 DBG("Could not remove all files for service %s",
157 g_free(config_service->ident);
158 g_free(config_service->type);
159 g_free(config_service->name);
160 g_free(config_service->ssid);
161 g_free(config_service->eap);
162 g_free(config_service->identity);
163 g_free(config_service->ca_cert_file);
164 g_free(config_service->client_cert_file);
165 g_free(config_service->private_key_file);
166 g_free(config_service->private_key_passphrase);
167 g_free(config_service->private_key_passphrase_type);
168 g_free(config_service->phase2);
169 g_free(config_service->passphrase);
170 g_slist_free_full(config_service->service_identifiers, g_free);
171 g_free(config_service->config_ident);
172 g_free(config_service->config_entry);
173 g_free(config_service);
176 static void check_keys(GKeyFile *keyfile, const char *group,
177 const char **possible_keys)
180 gsize nb_avail_keys, i, j;
182 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
183 if (avail_keys == NULL)
187 * For each key in the configuration file,
188 * verify it is understood by connman
190 for (i = 0 ; i < nb_avail_keys; i++) {
191 for (j = 0; possible_keys[j] ; j++)
192 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
195 if (possible_keys[j] == NULL)
196 connman_warn("Unknown configuration key %s in [%s]",
197 avail_keys[i], group);
200 g_strfreev(avail_keys);
203 static connman_bool_t
204 is_protected_service(struct connman_config_service *service)
208 DBG("ident %s", service->ident);
210 for (list = protected_services; list; list = list->next) {
211 struct connman_config_service *s = list->data;
213 if (g_strcmp0(s->type, service->type) != 0)
216 if (s->ssid == NULL || service->ssid == NULL)
219 if (s->ssid_len != service->ssid_len)
222 if (g_strcmp0(service->type, "wifi") == 0 &&
223 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
231 static int load_service(GKeyFile *keyfile, const char *group,
232 struct connman_config *config)
234 struct connman_config_service *service;
236 char *str, *hex_ssid;
237 gboolean service_created = FALSE;
240 /* Strip off "service_" prefix */
243 if (strlen(ident) < 1)
246 /* Verify that provided keys are good */
247 check_keys(keyfile, group, service_possible_keys);
249 service = g_hash_table_lookup(config->service_table, ident);
250 if (service == NULL) {
251 service = g_try_new0(struct connman_config_service, 1);
255 service->ident = g_strdup(ident);
257 service_created = TRUE;
260 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
262 g_free(service->type);
266 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
268 g_free(service->name);
272 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
274 if (hex_ssid != NULL) {
276 unsigned int i, j = 0, hex;
277 size_t hex_ssid_len = strlen(hex_ssid);
279 ssid = g_try_malloc0(hex_ssid_len / 2);
286 for (i = 0; i < hex_ssid_len; i += 2) {
287 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
288 connman_warn("Invalid SSID %s", hex_ssid);
299 g_free(service->ssid);
300 service->ssid = ssid;
301 service->ssid_len = hex_ssid_len / 2;
302 } else if (service->name != NULL) {
304 unsigned int ssid_len;
306 ssid_len = strlen(service->name);
307 ssid = g_try_malloc0(ssid_len);
313 memcpy(ssid, service->name, ssid_len);
314 g_free(service->ssid);
315 service->ssid = ssid;
316 service->ssid_len = ssid_len;
319 if (is_protected_service(service) == TRUE) {
320 connman_error("Trying to provision a protected service");
325 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
327 g_free(service->eap);
331 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
333 g_free(service->ca_cert_file);
334 service->ca_cert_file = str;
337 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
339 g_free(service->client_cert_file);
340 service->client_cert_file = str;
343 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
345 g_free(service->private_key_file);
346 service->private_key_file = str;
349 str = g_key_file_get_string(keyfile, group,
350 SERVICE_KEY_PRV_KEY_PASS, NULL);
352 g_free(service->private_key_passphrase);
353 service->private_key_passphrase = str;
356 str = g_key_file_get_string(keyfile, group,
357 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
359 g_free(service->private_key_passphrase_type);
360 service->private_key_passphrase_type = str;
363 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
365 g_free(service->identity);
366 service->identity = str;
369 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
371 g_free(service->phase2);
372 service->phase2 = str;
375 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
378 g_free(service->passphrase);
379 service->passphrase = str;
382 service->config_ident = g_strdup(config->ident);
383 service->config_entry = g_strdup_printf("service_%s", service->ident);
386 g_hash_table_insert(config->service_table, service->ident,
389 if (config->protected == TRUE)
391 g_slist_append(protected_services, service);
393 connman_info("Adding service configuration %s", service->ident);
398 if (service_created == TRUE) {
399 g_free(service->ident);
400 g_free(service->type);
401 g_free(service->name);
402 g_free(service->ssid);
409 static int load_config(struct connman_config *config)
412 GError *error = NULL;
416 gboolean protected, found = FALSE;
419 DBG("config %p", config);
421 keyfile = __connman_storage_load_config(config->ident);
425 /* Verify keys validity of the global section */
426 check_keys(keyfile, "global", config_possible_keys);
428 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
430 g_free(config->name);
434 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
436 g_free(config->description);
437 config->description = str;
440 protected = g_key_file_get_boolean(keyfile, "global",
441 CONFIG_KEY_PROT, &error);
443 config->protected = protected;
445 config->protected = TRUE;
447 groups = g_key_file_get_groups(keyfile, &length);
449 for (i = 0; groups[i] != NULL; i++) {
450 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
451 if (load_service(keyfile, groups[i], config) == 0)
457 connman_warn("Config file %s/%s.config does not contain any "
458 "configuration that can be provisioned!",
459 STORAGEDIR, config->ident);
463 g_key_file_free(keyfile);
468 static struct connman_config *create_config(const char *ident)
470 struct connman_config *config;
472 DBG("ident %s", ident);
474 if (g_hash_table_lookup(config_table, ident) != NULL)
477 config = g_try_new0(struct connman_config, 1);
481 config->ident = g_strdup(ident);
483 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
484 NULL, unregister_service);
486 g_hash_table_insert(config_table, config->ident, config);
488 connman_info("Adding configuration %s", config->ident);
493 static connman_bool_t validate_ident(const char *ident)
500 for (i = 0; i < strlen(ident); i++)
501 if (g_ascii_isprint(ident[i]) == FALSE)
507 static int read_configs(void)
513 dir = g_dir_open(STORAGEDIR, 0, NULL);
517 while ((file = g_dir_read_name(dir)) != NULL) {
521 if (g_str_has_suffix(file, ".config") == FALSE)
524 ident = g_strrstr(file, ".config");
528 str = g_string_new_len(file, ident - file);
532 ident = g_string_free(str, FALSE);
534 if (validate_ident(ident) == TRUE) {
535 struct connman_config *config;
537 config = create_config(ident);
541 connman_error("Invalid config ident %s", ident);
552 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
560 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
565 status = g_io_channel_read_chars(channel, buffer,
566 sizeof(buffer) -1, &bytes_read, NULL);
569 case G_IO_STATUS_NORMAL:
571 case G_IO_STATUS_AGAIN:
574 connman_error("Reading from inotify channel failed");
581 while (bytes_read > 0) {
582 struct inotify_event *event;
587 event = (struct inotify_event *) next_event;
589 ident = next_event + sizeof(struct inotify_event);
593 len = sizeof(struct inotify_event) + event->len;
595 /* check if inotify_event block fit */
596 if (len > bytes_read)
605 if (g_str_has_suffix(ident, ".config") == FALSE)
608 ext = g_strrstr(ident, ".config");
614 if (validate_ident(ident) == FALSE) {
615 connman_error("Invalid config ident %s", ident);
619 if (event->mask & IN_CREATE)
620 create_config(ident);
622 if (event->mask & IN_MODIFY) {
623 struct connman_config *config;
625 config = g_hash_table_lookup(config_table, ident);
626 if (config != NULL) {
627 g_hash_table_remove_all(config->service_table);
629 __connman_service_provision_changed(ident);
633 if (event->mask & IN_DELETE)
634 g_hash_table_remove(config_table, ident);
640 static int create_watch(void)
648 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
649 IN_MODIFY | IN_CREATE | IN_DELETE);
650 if (inotify_wd < 0) {
651 connman_error("Creation of STORAGEDIR watch failed");
656 inotify_channel = g_io_channel_unix_new(fd);
657 if (inotify_channel == NULL) {
658 connman_error("Creation of inotify channel failed");
659 inotify_rm_watch(fd, inotify_wd);
666 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
667 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
668 g_io_channel_set_buffered(inotify_channel, FALSE);
670 inotify_watch = g_io_add_watch(inotify_channel,
671 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
677 static void remove_watch(void)
681 if (inotify_channel == NULL)
684 if (inotify_watch > 0) {
685 g_source_remove(inotify_watch);
689 fd = g_io_channel_unix_get_fd(inotify_channel);
691 if (inotify_wd >= 0) {
692 inotify_rm_watch(fd, inotify_wd);
696 g_io_channel_unref(inotify_channel);
699 int __connman_config_init(void)
703 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
704 NULL, unregister_config);
708 return read_configs();
711 void __connman_config_cleanup(void)
717 g_hash_table_destroy(config_table);
721 static char *config_pem_fsid(const char *pem_file)
724 unsigned *fsid = (unsigned *) &buf.f_fsid;
725 unsigned long long fsid64;
727 if (pem_file == NULL)
730 if (statfs(pem_file, &buf) < 0) {
731 connman_error("statfs error %s for %s",
732 strerror(errno), pem_file);
736 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
738 return g_strdup_printf("%llx", fsid64);
741 static void provision_service(gpointer key, gpointer value, gpointer user_data)
743 struct connman_service *service = user_data;
744 struct connman_config_service *config = value;
745 struct connman_network *network;
746 const void *ssid, *service_id;
747 unsigned int ssid_len;
749 /* For now only WiFi service entries are supported */
750 if (g_strcmp0(config->type, "wifi") != 0)
753 network = __connman_service_get_network(service);
754 if (network == NULL) {
755 connman_error("Service has no network set");
759 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
761 connman_error("Network SSID not set");
765 if (config->ssid == NULL || ssid_len != config->ssid_len)
768 if (memcmp(config->ssid, ssid, ssid_len) != 0)
771 service_id = __connman_service_get_ident(service);
772 config->service_identifiers =
773 g_slist_prepend(config->service_identifiers,
774 g_strdup(service_id));
776 __connman_service_set_immutable(service, TRUE);
778 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
780 __connman_service_set_config(service, config->config_ident,
781 config->config_entry);
783 if (config->eap != NULL)
784 __connman_service_set_string(service, "EAP", config->eap);
786 if (config->identity != NULL)
787 __connman_service_set_string(service, "Identity",
790 if (config->ca_cert_file != NULL)
791 __connman_service_set_string(service, "CACertFile",
792 config->ca_cert_file);
794 if (config->client_cert_file != NULL)
795 __connman_service_set_string(service, "ClientCertFile",
796 config->client_cert_file);
798 if (config->private_key_file != NULL)
799 __connman_service_set_string(service, "PrivateKeyFile",
800 config->private_key_file);
802 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
803 config->private_key_file != NULL) {
806 fsid = config_pem_fsid(config->private_key_file);
810 g_free(config->private_key_passphrase);
811 config->private_key_passphrase = fsid;
814 if (config->private_key_passphrase != NULL) {
815 __connman_service_set_string(service, "PrivateKeyPassphrase",
816 config->private_key_passphrase);
818 * TODO: Support for PEAP with both identity and key passwd.
819 * In that case, we should check if both of them are found
820 * from the config file. If not, we should not set the
821 * service passphrase in order for the UI to request for an
822 * additional passphrase.
826 if (config->phase2 != NULL)
827 __connman_service_set_string(service, "Phase2", config->phase2);
829 if (config->passphrase != NULL)
830 __connman_service_set_string(service, "Passphrase", config->passphrase);
833 int __connman_config_provision_service(struct connman_service *service)
835 enum connman_service_type type;
839 DBG("service %p", service);
841 /* For now only WiFi services are supported */
842 type = connman_service_get_type(service);
843 if (type != CONNMAN_SERVICE_TYPE_WIFI)
846 g_hash_table_iter_init(&iter, config_table);
848 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
849 struct connman_config *config = value;
851 g_hash_table_foreach(config->service_table,
852 provision_service, service);
858 int __connman_config_provision_service_ident(struct connman_service *service,
861 enum connman_service_type type;
862 struct connman_config *config;
864 DBG("service %p", service);
866 /* For now only WiFi services are supported */
867 type = connman_service_get_type(service);
868 if (type != CONNMAN_SERVICE_TYPE_WIFI)
871 config = g_hash_table_lookup(config_table, ident);
873 g_hash_table_foreach(config->service_table,
874 provision_service, service);