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)
382 DBG("config %p", config);
384 keyfile = __connman_storage_open_config(config->ident);
388 /* Verify keys validity of the global section */
389 check_keys(keyfile, "global", config_possible_keys);
391 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
393 g_free(config->name);
397 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
399 g_free(config->description);
400 config->description = str;
403 protected = g_key_file_get_boolean(keyfile, "global",
404 CONFIG_KEY_PROT, NULL);
405 config->protected = protected;
407 groups = g_key_file_get_groups(keyfile, &length);
409 for (i = 0; groups[i] != NULL; i++) {
410 if (g_str_has_prefix(groups[i], "service_") == TRUE)
411 load_service(keyfile, groups[i], config);
416 __connman_storage_close_config(config->ident, keyfile, FALSE);
421 static struct connman_config *create_config(const char *ident)
423 struct connman_config *config;
425 DBG("ident %s", ident);
427 if (g_hash_table_lookup(config_table, ident) != NULL)
430 config = g_try_new0(struct connman_config, 1);
434 config->ident = g_strdup(ident);
436 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
437 NULL, unregister_service);
439 g_hash_table_insert(config_table, config->ident, config);
441 connman_info("Adding configuration %s", config->ident);
446 int __connman_config_load_service(GKeyFile *keyfile, const char *group,
447 connman_bool_t persistent)
449 struct connman_config *config;
450 const char *service_name;
451 char *ident, *filename = NULL, *content = NULL;
452 gsize content_length;
455 service_name = group + strlen("service_");
456 ident = g_strdup_printf("%s_%s", INTERNAL_CONFIG_PREFIX, service_name);
460 DBG("ident %s", ident);
462 config = g_hash_table_lookup(config_table, ident);
463 if (config == NULL) {
464 config = create_config(ident);
465 if (config == NULL) {
470 config->protected = FALSE;
473 err = load_service(keyfile, group, config);
474 if (persistent == FALSE || err < 0)
477 g_key_file_set_string(keyfile, "global", CONFIG_KEY_NAME,
479 g_key_file_set_string(keyfile, "global", CONFIG_KEY_DESC,
480 "Internal Config File");
482 content = g_key_file_to_data(keyfile, &content_length, NULL);
483 if (content == NULL) {
488 filename = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
489 if (filename == NULL) {
494 DBG("Saving %d bytes to %s", content_length, filename);
496 if (g_file_set_contents(filename, content,
497 content_length, NULL) == FALSE) {
512 static int read_configs(void)
518 dir = g_dir_open(STORAGEDIR, 0, NULL);
522 while ((file = g_dir_read_name(dir)) != NULL) {
526 if (g_str_has_suffix(file, ".config") == FALSE)
529 ident = g_strrstr(file, ".config");
533 str = g_string_new_len(file, ident - file);
537 ident = g_string_free(str, FALSE);
539 if (connman_dbus_validate_ident(ident) == TRUE) {
540 struct connman_config *config;
542 config = create_config(ident);
555 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
563 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
568 status = g_io_channel_read_chars(channel, buffer,
569 sizeof(buffer) -1, &bytes_read, NULL);
572 case G_IO_STATUS_NORMAL:
574 case G_IO_STATUS_AGAIN:
577 connman_error("Reading from inotify channel failed");
584 while (bytes_read > 0) {
585 struct inotify_event *event;
590 event = (struct inotify_event *) next_event;
592 ident = next_event + sizeof(struct inotify_event);
596 len = sizeof(struct inotify_event) + event->len;
598 /* check if inotify_event block fit */
599 if (len > bytes_read)
608 if (g_str_has_suffix(ident, ".config") == FALSE)
611 ext = g_strrstr(ident, ".config");
617 if (connman_dbus_validate_ident(ident) == FALSE)
620 if (event->mask & IN_CREATE)
621 create_config(ident);
623 if (event->mask & IN_MODIFY) {
624 struct connman_config *config;
626 config = g_hash_table_lookup(config_table, ident);
627 if (config != NULL) {
628 g_hash_table_remove_all(config->service_table);
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;
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 __connman_service_set_immutable(service, TRUE);
773 __connman_service_set_favorite(service, TRUE);
775 if (config->eap != NULL)
776 __connman_service_set_string(service, "EAP", config->eap);
778 if (config->identity != NULL)
779 __connman_service_set_string(service, "Identity",
782 if (config->ca_cert_file != NULL)
783 __connman_service_set_string(service, "CACertFile",
784 config->ca_cert_file);
786 if (config->client_cert_file != NULL)
787 __connman_service_set_string(service, "ClientCertFile",
788 config->client_cert_file);
790 if (config->private_key_file != NULL)
791 __connman_service_set_string(service, "PrivateKeyFile",
792 config->private_key_file);
794 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
795 config->private_key_file != NULL) {
798 fsid = config_pem_fsid(config->private_key_file);
802 g_free(config->private_key_passphrase);
803 config->private_key_passphrase = fsid;
806 if (config->private_key_passphrase != NULL) {
807 __connman_service_set_string(service, "PrivateKeyPassphrase",
808 config->private_key_passphrase);
810 * TODO: Support for PEAP with both identity and key passwd.
811 * In that case, we should check if both of them are found
812 * from the config file. If not, we should not set the
813 * service passphrase in order for the UI to request for an
814 * additional passphrase.
818 if (config->phase2 != NULL)
819 __connman_service_set_string(service, "Phase2", config->phase2);
821 if (config->passphrase != NULL)
822 __connman_service_set_string(service, "Passphrase", config->passphrase);
825 int __connman_config_provision_service(struct connman_service *service)
827 enum connman_service_type type;
831 DBG("service %p", service);
833 /* For now only WiFi services are supported */
834 type = connman_service_get_type(service);
835 if (type != CONNMAN_SERVICE_TYPE_WIFI)
838 g_hash_table_iter_init(&iter, config_table);
840 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
841 struct connman_config *config = value;
843 g_hash_table_foreach(config->service_table,
844 provision_service, service);