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
33 struct connman_config_service {
38 unsigned int ssid_len;
42 char *client_cert_file;
43 char *private_key_file;
44 char *private_key_passphrase;
45 char *private_key_passphrase_type;
49 struct connman_config {
53 GHashTable *service_table;
56 static GHashTable *config_table = NULL;
58 static void unregister_config(gpointer data)
60 struct connman_config *config = data;
62 connman_info("Removing configuration %s", config->ident);
64 g_hash_table_destroy(config->service_table);
66 g_free(config->description);
68 g_free(config->ident);
72 static void unregister_service(gpointer data)
74 struct connman_config_service *service = data;
76 connman_info("Removing service configuration %s", service->ident);
78 g_free(service->ident);
79 g_free(service->type);
80 g_free(service->name);
81 g_free(service->ssid);
83 g_free(service->identity);
84 g_free(service->ca_cert_file);
85 g_free(service->client_cert_file);
86 g_free(service->private_key_file);
87 g_free(service->private_key_passphrase);
88 g_free(service->private_key_passphrase_type);
89 g_free(service->phase2);
93 static int load_service(GKeyFile *keyfile, const char *group,
94 struct connman_config *config)
96 struct connman_config_service *service;
100 /* Strip off "service_" prefix */
103 if (strlen(ident) < 1)
106 service = g_hash_table_lookup(config->service_table, ident);
107 if (service == NULL) {
108 service = g_try_new0(struct connman_config_service, 1);
112 service->ident = g_strdup(ident);
115 str = g_key_file_get_string(keyfile, group, "Type", NULL);
117 g_free(service->type);
121 str = g_key_file_get_string(keyfile, group, "Name", NULL);
123 g_free(service->name);
127 hex_ssid = g_key_file_get_string(keyfile, group, "SSID", NULL);
128 if (hex_ssid != NULL) {
130 unsigned int i, j = 0, hex;
131 size_t hex_ssid_len = strlen(hex_ssid);
133 ssid = g_try_malloc0(hex_ssid_len / 2);
139 for (i = 0; i < hex_ssid_len; i += 2) {
140 sscanf(hex_ssid + i, "%02x", &hex);
146 g_free(service->ssid);
147 service->ssid = ssid;
148 service->ssid_len = hex_ssid_len / 2;
149 } else if (service->name != NULL) {
151 unsigned int ssid_len;
153 ssid_len = strlen(service->name);
154 ssid = g_try_malloc0(ssid_len);
158 memcpy(ssid, service->name, ssid_len);
159 g_free(service->ssid);
160 service->ssid = ssid;
161 service->ssid_len = ssid_len;
164 str = g_key_file_get_string(keyfile, group, "EAP", NULL);
166 g_free(service->eap);
170 str = g_key_file_get_string(keyfile, group, "CACertFile", NULL);
172 g_free(service->ca_cert_file);
173 service->ca_cert_file = str;
176 str = g_key_file_get_string(keyfile, group, "ClientCertFile", NULL);
178 g_free(service->client_cert_file);
179 service->client_cert_file = str;
182 str = g_key_file_get_string(keyfile, group, "PrivateKeyFile", NULL);
184 g_free(service->private_key_file);
185 service->private_key_file = str;
188 str = g_key_file_get_string(keyfile, group,
189 "PrivateKeyPassphrase", NULL);
191 g_free(service->private_key_passphrase);
192 service->private_key_passphrase = str;
195 str = g_key_file_get_string(keyfile, group,
196 "PrivateKeyPassphraseType", NULL);
198 g_free(service->private_key_passphrase_type);
199 service->private_key_passphrase_type = str;
202 str = g_key_file_get_string(keyfile, group, "Identity", NULL);
204 g_free(service->identity);
205 service->identity = str;
208 str = g_key_file_get_string(keyfile, group, "Phase2", NULL);
210 g_free(service->phase2);
211 service->phase2 = str;
214 g_hash_table_replace(config->service_table, service->ident, service);
216 connman_info("Adding service configuration %s", service->ident);
221 static int load_config(struct connman_config *config)
229 DBG("config %p", config);
231 keyfile = __connman_storage_open_config(config->ident);
235 str = g_key_file_get_string(keyfile, "global", "Name", NULL);
237 g_free(config->name);
241 str = g_key_file_get_string(keyfile, "global", "Description", NULL);
243 g_free(config->description);
244 config->description = str;
247 groups = g_key_file_get_groups(keyfile, &length);
249 for (i = 0; groups[i] != NULL; i++) {
250 if (g_str_has_prefix(groups[i], "service_") == TRUE)
251 load_service(keyfile, groups[i], config);
256 __connman_storage_close_config(config->ident, keyfile, FALSE);
261 static int create_config(const char *ident)
263 struct connman_config *config;
265 DBG("ident %s", ident);
267 if (g_hash_table_lookup(config_table, ident) != NULL)
270 config = g_try_new0(struct connman_config, 1);
274 config->ident = g_strdup(ident);
276 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
277 NULL, unregister_service);
279 g_hash_table_insert(config_table, config->ident, config);
281 connman_info("Adding configuration %s", config->ident);
288 static int read_configs(void)
294 dir = g_dir_open(STORAGEDIR, 0, NULL);
298 while ((file = g_dir_read_name(dir)) != NULL) {
302 if (g_str_has_suffix(file, ".config") == FALSE)
305 ident = g_strrstr(file, ".config");
309 str = g_string_new_len(file, ident - file);
313 ident = g_string_free(str, FALSE);
315 if (connman_dbus_validate_ident(ident) == TRUE)
316 create_config(ident);
327 int __connman_config_init(void)
331 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
332 NULL, unregister_config);
334 return read_configs();
337 void __connman_config_cleanup(void)
341 g_hash_table_destroy(config_table);
345 static char *config_pem_fsid(const char *pem_file)
348 unsigned *fsid = (unsigned *) &buf.f_fsid;
349 unsigned long long fsid64;
351 if (pem_file == NULL)
354 if (statfs(pem_file, &buf) < 0) {
355 connman_error("statfs error %s for %s",
356 strerror(errno), pem_file);
360 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
362 return g_strdup_printf("%llx", fsid64);
365 static void provision_service(gpointer key, gpointer value, gpointer user_data)
367 struct connman_service *service = user_data;
368 struct connman_config_service *config = value;
369 struct connman_network *network;
371 unsigned int ssid_len;
373 /* For now only WiFi service entries are supported */
374 if (g_strcmp0(config->type, "wifi") != 0)
377 network = __connman_service_get_network(service);
378 if (network == NULL) {
379 connman_error("Service has no network set");
383 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
385 connman_error("Network SSID not set");
389 if (config->ssid == NULL || ssid_len != config->ssid_len)
392 if (memcmp(config->ssid, ssid, ssid_len) != 0)
395 __connman_service_set_immutable(service, TRUE);
396 __connman_service_set_favorite(service, TRUE);
398 if (config->eap != NULL)
399 __connman_service_set_string(service, "EAP", config->eap);
401 if (config->identity != NULL)
402 __connman_service_set_string(service, "Identity",
405 if (config->ca_cert_file != NULL)
406 __connman_service_set_string(service, "CACertFile",
407 config->ca_cert_file);
409 if (config->client_cert_file != NULL)
410 __connman_service_set_string(service, "ClientCertFile",
411 config->client_cert_file);
413 if (config->private_key_file != NULL)
414 __connman_service_set_string(service, "PrivateKeyFile",
415 config->private_key_file);
417 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
418 config->private_key_file != NULL) {
421 fsid = config_pem_fsid(config->private_key_file);
425 g_free(config->private_key_passphrase);
426 config->private_key_passphrase = fsid;
429 if (config->private_key_passphrase != NULL) {
430 __connman_service_set_string(service, "PrivateKeyPassphrase",
431 config->private_key_passphrase);
433 * TODO: Support for PEAP with both identity and key passwd.
434 * In that case, we should check if both of them are found
435 * from the config file. If not, we should not set the
436 * service passphrase in order for the UI to request for an
437 * additional passphrase.
441 if (config->phase2 != NULL)
442 __connman_service_set_string(service, "Phase2", config->phase2);
445 int __connman_config_provision_service(struct connman_service *service)
447 enum connman_service_type type;
451 DBG("service %p", service);
453 /* For now only WiFi services are supported */
454 type = connman_service_get_type(service);
455 if (type != CONNMAN_SERVICE_TYPE_WIFI)
458 g_hash_table_iter_init(&iter, config_table);
460 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
461 struct connman_config *config = value;
463 g_hash_table_foreach(config->service_table,
464 provision_service, service);