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 */
54 connman_bool_t hidden;
57 struct connman_config {
61 connman_bool_t protected;
62 GHashTable *service_table;
65 static GHashTable *config_table = NULL;
66 static GSList *protected_services = NULL;
68 static int inotify_wd = -1;
70 static GIOChannel *inotify_channel = NULL;
71 static uint inotify_watch = 0;
72 static connman_bool_t cleanup = FALSE;
74 #define INTERNAL_CONFIG_PREFIX "__internal"
76 /* Definition of possible strings in the .config files */
77 #define CONFIG_KEY_NAME "Name"
78 #define CONFIG_KEY_DESC "Description"
79 #define CONFIG_KEY_PROT "Protected"
81 #define SERVICE_KEY_TYPE "Type"
82 #define SERVICE_KEY_NAME "Name"
83 #define SERVICE_KEY_SSID "SSID"
84 #define SERVICE_KEY_EAP "EAP"
85 #define SERVICE_KEY_CA_CERT "CACertFile"
86 #define SERVICE_KEY_CL_CERT "ClientCertFile"
87 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
88 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
89 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
90 #define SERVICE_KEY_IDENTITY "Identity"
91 #define SERVICE_KEY_PHASE2 "Phase2"
92 #define SERVICE_KEY_PASSPHRASE "Passphrase"
93 #define SERVICE_KEY_HIDDEN "Hidden"
95 static const char *config_possible_keys[] = {
102 static const char *service_possible_keys[] = {
110 SERVICE_KEY_PRV_KEY_PASS,
111 SERVICE_KEY_PRV_KEY_PASS_TYPE,
112 SERVICE_KEY_IDENTITY,
114 SERVICE_KEY_PASSPHRASE,
119 static void unregister_config(gpointer data)
121 struct connman_config *config = data;
123 connman_info("Removing configuration %s", config->ident);
125 g_hash_table_destroy(config->service_table);
127 g_free(config->description);
128 g_free(config->name);
129 g_free(config->ident);
133 static void unregister_service(gpointer data)
135 struct connman_config_service *config_service = data;
136 struct connman_service *service;
143 connman_info("Removing service configuration %s",
144 config_service->ident);
146 protected_services = g_slist_remove(protected_services,
149 for (list = config_service->service_identifiers; list != NULL;
151 service_id = list->data;
153 service = __connman_service_lookup_from_ident(service_id);
154 if (service != NULL) {
155 __connman_service_set_immutable(service, FALSE);
156 __connman_service_remove(service);
159 if (__connman_storage_remove_service(service_id) == FALSE)
160 DBG("Could not remove all files for service %s",
165 g_free(config_service->ident);
166 g_free(config_service->type);
167 g_free(config_service->name);
168 g_free(config_service->ssid);
169 g_free(config_service->eap);
170 g_free(config_service->identity);
171 g_free(config_service->ca_cert_file);
172 g_free(config_service->client_cert_file);
173 g_free(config_service->private_key_file);
174 g_free(config_service->private_key_passphrase);
175 g_free(config_service->private_key_passphrase_type);
176 g_free(config_service->phase2);
177 g_free(config_service->passphrase);
178 g_slist_free_full(config_service->service_identifiers, g_free);
179 g_free(config_service->config_ident);
180 g_free(config_service->config_entry);
181 g_free(config_service);
184 static void check_keys(GKeyFile *keyfile, const char *group,
185 const char **possible_keys)
188 gsize nb_avail_keys, i, j;
190 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
191 if (avail_keys == NULL)
195 * For each key in the configuration file,
196 * verify it is understood by connman
198 for (i = 0 ; i < nb_avail_keys; i++) {
199 for (j = 0; possible_keys[j] ; j++)
200 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
203 if (possible_keys[j] == NULL)
204 connman_warn("Unknown configuration key %s in [%s]",
205 avail_keys[i], group);
208 g_strfreev(avail_keys);
211 static connman_bool_t
212 is_protected_service(struct connman_config_service *service)
216 DBG("ident %s", service->ident);
218 for (list = protected_services; list; list = list->next) {
219 struct connman_config_service *s = list->data;
221 if (g_strcmp0(s->type, service->type) != 0)
224 if (s->ssid == NULL || service->ssid == NULL)
227 if (s->ssid_len != service->ssid_len)
230 if (g_strcmp0(service->type, "wifi") == 0 &&
231 strncmp(s->ssid, service->ssid, s->ssid_len) == 0) {
239 static int load_service(GKeyFile *keyfile, const char *group,
240 struct connman_config *config)
242 struct connman_config_service *service;
244 char *str, *hex_ssid;
245 gboolean service_created = FALSE;
248 /* Strip off "service_" prefix */
251 if (strlen(ident) < 1)
254 /* Verify that provided keys are good */
255 check_keys(keyfile, group, service_possible_keys);
257 service = g_hash_table_lookup(config->service_table, ident);
258 if (service == NULL) {
259 service = g_try_new0(struct connman_config_service, 1);
263 service->ident = g_strdup(ident);
265 service_created = TRUE;
268 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
270 g_free(service->type);
274 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
276 g_free(service->name);
280 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
282 if (hex_ssid != NULL) {
284 unsigned int i, j = 0, hex;
285 size_t hex_ssid_len = strlen(hex_ssid);
287 ssid = g_try_malloc0(hex_ssid_len / 2);
294 for (i = 0; i < hex_ssid_len; i += 2) {
295 if (sscanf(hex_ssid + i, "%02x", &hex) <= 0) {
296 connman_warn("Invalid SSID %s", hex_ssid);
307 g_free(service->ssid);
308 service->ssid = ssid;
309 service->ssid_len = hex_ssid_len / 2;
310 } else if (service->name != NULL) {
312 unsigned int ssid_len;
314 ssid_len = strlen(service->name);
315 ssid = g_try_malloc0(ssid_len);
321 memcpy(ssid, service->name, ssid_len);
322 g_free(service->ssid);
323 service->ssid = ssid;
324 service->ssid_len = ssid_len;
327 if (is_protected_service(service) == TRUE) {
328 connman_error("Trying to provision a protected service");
333 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
335 g_free(service->eap);
339 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
341 g_free(service->ca_cert_file);
342 service->ca_cert_file = str;
345 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
347 g_free(service->client_cert_file);
348 service->client_cert_file = str;
351 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
353 g_free(service->private_key_file);
354 service->private_key_file = str;
357 str = g_key_file_get_string(keyfile, group,
358 SERVICE_KEY_PRV_KEY_PASS, NULL);
360 g_free(service->private_key_passphrase);
361 service->private_key_passphrase = str;
364 str = g_key_file_get_string(keyfile, group,
365 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
367 g_free(service->private_key_passphrase_type);
368 service->private_key_passphrase_type = str;
371 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
373 g_free(service->identity);
374 service->identity = str;
377 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
379 g_free(service->phase2);
380 service->phase2 = str;
383 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
386 g_free(service->passphrase);
387 service->passphrase = str;
390 service->config_ident = g_strdup(config->ident);
391 service->config_entry = g_strdup_printf("service_%s", service->ident);
393 service->hidden = g_key_file_get_boolean(keyfile, group,
394 SERVICE_KEY_HIDDEN, NULL);
397 g_hash_table_insert(config->service_table, service->ident,
400 if (config->protected == TRUE)
402 g_slist_append(protected_services, service);
404 connman_info("Adding service configuration %s", service->ident);
409 if (service_created == TRUE) {
410 g_free(service->ident);
411 g_free(service->type);
412 g_free(service->name);
413 g_free(service->ssid);
420 static int load_config(struct connman_config *config)
423 GError *error = NULL;
427 gboolean protected, found = FALSE;
430 DBG("config %p", config);
432 keyfile = __connman_storage_load_config(config->ident);
436 /* Verify keys validity of the global section */
437 check_keys(keyfile, "global", config_possible_keys);
439 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
441 g_free(config->name);
445 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
447 g_free(config->description);
448 config->description = str;
451 protected = g_key_file_get_boolean(keyfile, "global",
452 CONFIG_KEY_PROT, &error);
454 config->protected = protected;
456 config->protected = TRUE;
458 groups = g_key_file_get_groups(keyfile, &length);
460 for (i = 0; groups[i] != NULL; i++) {
461 if (g_str_has_prefix(groups[i], "service_") == TRUE) {
462 if (load_service(keyfile, groups[i], config) == 0)
468 connman_warn("Config file %s/%s.config does not contain any "
469 "configuration that can be provisioned!",
470 STORAGEDIR, config->ident);
474 g_key_file_free(keyfile);
479 static struct connman_config *create_config(const char *ident)
481 struct connman_config *config;
483 DBG("ident %s", ident);
485 if (g_hash_table_lookup(config_table, ident) != NULL)
488 config = g_try_new0(struct connman_config, 1);
492 config->ident = g_strdup(ident);
494 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
495 NULL, unregister_service);
497 g_hash_table_insert(config_table, config->ident, config);
499 connman_info("Adding configuration %s", config->ident);
504 static connman_bool_t validate_ident(const char *ident)
511 for (i = 0; i < strlen(ident); i++)
512 if (g_ascii_isprint(ident[i]) == FALSE)
518 static int read_configs(void)
524 dir = g_dir_open(STORAGEDIR, 0, NULL);
528 while ((file = g_dir_read_name(dir)) != NULL) {
532 if (g_str_has_suffix(file, ".config") == FALSE)
535 ident = g_strrstr(file, ".config");
539 str = g_string_new_len(file, ident - file);
543 ident = g_string_free(str, FALSE);
545 if (validate_ident(ident) == TRUE) {
546 struct connman_config *config;
548 config = create_config(ident);
552 connman_error("Invalid config ident %s", ident);
563 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
571 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
576 status = g_io_channel_read_chars(channel, buffer,
577 sizeof(buffer) -1, &bytes_read, NULL);
580 case G_IO_STATUS_NORMAL:
582 case G_IO_STATUS_AGAIN:
585 connman_error("Reading from inotify channel failed");
592 while (bytes_read > 0) {
593 struct inotify_event *event;
598 event = (struct inotify_event *) next_event;
600 ident = next_event + sizeof(struct inotify_event);
604 len = sizeof(struct inotify_event) + event->len;
606 /* check if inotify_event block fit */
607 if (len > bytes_read)
616 if (g_str_has_suffix(ident, ".config") == FALSE)
619 ext = g_strrstr(ident, ".config");
625 if (validate_ident(ident) == FALSE) {
626 connman_error("Invalid config ident %s", ident);
630 if (event->mask & IN_CREATE)
631 create_config(ident);
633 if (event->mask & IN_MODIFY) {
634 struct connman_config *config;
636 config = g_hash_table_lookup(config_table, ident);
637 if (config != NULL) {
640 g_hash_table_remove_all(config->service_table);
642 ret = __connman_service_provision_changed(ident);
645 * Re-scan the config file for any
648 g_hash_table_remove_all(config->service_table);
650 __connman_service_provision_changed(ident);
655 if (event->mask & IN_DELETE)
656 g_hash_table_remove(config_table, ident);
662 static int create_watch(void)
670 inotify_wd = inotify_add_watch(fd, STORAGEDIR,
671 IN_MODIFY | IN_CREATE | IN_DELETE);
672 if (inotify_wd < 0) {
673 connman_error("Creation of STORAGEDIR watch failed");
678 inotify_channel = g_io_channel_unix_new(fd);
679 if (inotify_channel == NULL) {
680 connman_error("Creation of inotify channel failed");
681 inotify_rm_watch(fd, inotify_wd);
688 g_io_channel_set_close_on_unref(inotify_channel, TRUE);
689 g_io_channel_set_encoding(inotify_channel, NULL, NULL);
690 g_io_channel_set_buffered(inotify_channel, FALSE);
692 inotify_watch = g_io_add_watch(inotify_channel,
693 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
699 static void remove_watch(void)
703 if (inotify_channel == NULL)
706 if (inotify_watch > 0) {
707 g_source_remove(inotify_watch);
711 fd = g_io_channel_unix_get_fd(inotify_channel);
713 if (inotify_wd >= 0) {
714 inotify_rm_watch(fd, inotify_wd);
718 g_io_channel_unref(inotify_channel);
721 int __connman_config_init(void)
725 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
726 NULL, unregister_config);
730 return read_configs();
733 void __connman_config_cleanup(void)
741 g_hash_table_destroy(config_table);
747 static char *config_pem_fsid(const char *pem_file)
750 unsigned *fsid = (unsigned *) &buf.f_fsid;
751 unsigned long long fsid64;
753 if (pem_file == NULL)
756 if (statfs(pem_file, &buf) < 0) {
757 connman_error("statfs error %s for %s",
758 strerror(errno), pem_file);
762 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
764 return g_strdup_printf("%llx", fsid64);
767 static void provision_service(gpointer key, gpointer value, gpointer user_data)
769 struct connman_service *service = user_data;
770 struct connman_config_service *config = value;
771 struct connman_network *network;
772 const void *ssid, *service_id;
773 unsigned int ssid_len;
775 /* For now only WiFi service entries are supported */
776 if (g_strcmp0(config->type, "wifi") != 0)
779 network = __connman_service_get_network(service);
780 if (network == NULL) {
781 connman_error("Service has no network set");
785 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
787 connman_error("Network SSID not set");
791 if (config->ssid == NULL || ssid_len != config->ssid_len)
794 if (memcmp(config->ssid, ssid, ssid_len) != 0)
797 service_id = __connman_service_get_ident(service);
798 config->service_identifiers =
799 g_slist_prepend(config->service_identifiers,
800 g_strdup(service_id));
802 __connman_service_set_immutable(service, TRUE);
804 __connman_service_set_favorite_delayed(service, TRUE, TRUE);
806 __connman_service_set_config(service, config->config_ident,
807 config->config_entry);
809 if (config->eap != NULL)
810 __connman_service_set_string(service, "EAP", config->eap);
812 if (config->identity != NULL)
813 __connman_service_set_string(service, "Identity",
816 if (config->ca_cert_file != NULL)
817 __connman_service_set_string(service, "CACertFile",
818 config->ca_cert_file);
820 if (config->client_cert_file != NULL)
821 __connman_service_set_string(service, "ClientCertFile",
822 config->client_cert_file);
824 if (config->private_key_file != NULL)
825 __connman_service_set_string(service, "PrivateKeyFile",
826 config->private_key_file);
828 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
829 config->private_key_file != NULL) {
832 fsid = config_pem_fsid(config->private_key_file);
836 g_free(config->private_key_passphrase);
837 config->private_key_passphrase = fsid;
840 if (config->private_key_passphrase != NULL) {
841 __connman_service_set_string(service, "PrivateKeyPassphrase",
842 config->private_key_passphrase);
844 * TODO: Support for PEAP with both identity and key passwd.
845 * In that case, we should check if both of them are found
846 * from the config file. If not, we should not set the
847 * service passphrase in order for the UI to request for an
848 * additional passphrase.
852 if (config->phase2 != NULL)
853 __connman_service_set_string(service, "Phase2", config->phase2);
855 if (config->passphrase != NULL)
856 __connman_service_set_string(service, "Passphrase", config->passphrase);
858 if (config->hidden == TRUE)
859 __connman_service_set_hidden(service);
861 __connman_service_mark_dirty();
863 __connman_service_save(service);
866 int __connman_config_provision_service(struct connman_service *service)
868 enum connman_service_type type;
872 DBG("service %p", service);
874 /* For now only WiFi services are supported */
875 type = connman_service_get_type(service);
876 if (type != CONNMAN_SERVICE_TYPE_WIFI)
879 g_hash_table_iter_init(&iter, config_table);
881 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
882 struct connman_config *config = value;
884 g_hash_table_foreach(config->service_table,
885 provision_service, service);
891 int __connman_config_provision_service_ident(struct connman_service *service,
892 const char *ident, const char *file, const char *entry)
894 enum connman_service_type type;
895 struct connman_config *config;
898 DBG("service %p", service);
900 /* For now only WiFi services are supported */
901 type = connman_service_get_type(service);
902 if (type != CONNMAN_SERVICE_TYPE_WIFI)
905 config = g_hash_table_lookup(config_table, ident);
909 gboolean found = FALSE;
911 g_hash_table_iter_init(&iter, config->service_table);
914 * Check if we need to remove individual service if it
915 * is missing from config file.
917 if (file != NULL && entry != NULL) {
918 while (g_hash_table_iter_next(&iter, &key,
920 struct connman_config_service *config = value;
922 if (g_strcmp0(config->config_ident,
924 g_strcmp0(config->config_entry,
931 DBG("found %d ident %s file %s entry %s", found, ident,
934 if (found == FALSE) {
936 * The entry+8 will skip "service_" prefix
938 g_hash_table_remove(config->service_table,
944 g_hash_table_foreach(config->service_table,
945 provision_service, service);