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;
50 struct connman_config {
54 GHashTable *service_table;
57 static GHashTable *config_table = NULL;
59 static void unregister_config(gpointer data)
61 struct connman_config *config = data;
63 connman_info("Removing configuration %s", config->ident);
65 g_hash_table_destroy(config->service_table);
67 g_free(config->description);
69 g_free(config->ident);
73 static void unregister_service(gpointer data)
75 struct connman_config_service *service = data;
77 connman_info("Removing service configuration %s", service->ident);
79 g_free(service->ident);
80 g_free(service->type);
81 g_free(service->name);
82 g_free(service->ssid);
84 g_free(service->identity);
85 g_free(service->ca_cert_file);
86 g_free(service->client_cert_file);
87 g_free(service->private_key_file);
88 g_free(service->private_key_passphrase);
89 g_free(service->private_key_passphrase_type);
90 g_free(service->phase2);
91 g_free(service->passphrase);
95 static int load_service(GKeyFile *keyfile, const char *group,
96 struct connman_config *config)
98 struct connman_config_service *service;
100 char *str, *hex_ssid;
102 /* Strip off "service_" prefix */
105 if (strlen(ident) < 1)
108 service = g_hash_table_lookup(config->service_table, ident);
109 if (service == NULL) {
110 service = g_try_new0(struct connman_config_service, 1);
114 service->ident = g_strdup(ident);
117 str = g_key_file_get_string(keyfile, group, "Type", NULL);
119 g_free(service->type);
123 str = g_key_file_get_string(keyfile, group, "Name", NULL);
125 g_free(service->name);
129 hex_ssid = g_key_file_get_string(keyfile, group, "SSID", NULL);
130 if (hex_ssid != NULL) {
132 unsigned int i, j = 0, hex;
133 size_t hex_ssid_len = strlen(hex_ssid);
135 ssid = g_try_malloc0(hex_ssid_len / 2);
141 for (i = 0; i < hex_ssid_len; i += 2) {
142 sscanf(hex_ssid + i, "%02x", &hex);
148 g_free(service->ssid);
149 service->ssid = ssid;
150 service->ssid_len = hex_ssid_len / 2;
151 } else if (service->name != NULL) {
153 unsigned int ssid_len;
155 ssid_len = strlen(service->name);
156 ssid = g_try_malloc0(ssid_len);
160 memcpy(ssid, service->name, ssid_len);
161 g_free(service->ssid);
162 service->ssid = ssid;
163 service->ssid_len = ssid_len;
166 str = g_key_file_get_string(keyfile, group, "EAP", NULL);
168 g_free(service->eap);
172 str = g_key_file_get_string(keyfile, group, "CACertFile", NULL);
174 g_free(service->ca_cert_file);
175 service->ca_cert_file = str;
178 str = g_key_file_get_string(keyfile, group, "ClientCertFile", NULL);
180 g_free(service->client_cert_file);
181 service->client_cert_file = str;
184 str = g_key_file_get_string(keyfile, group, "PrivateKeyFile", NULL);
186 g_free(service->private_key_file);
187 service->private_key_file = str;
190 str = g_key_file_get_string(keyfile, group,
191 "PrivateKeyPassphrase", NULL);
193 g_free(service->private_key_passphrase);
194 service->private_key_passphrase = str;
197 str = g_key_file_get_string(keyfile, group,
198 "PrivateKeyPassphraseType", NULL);
200 g_free(service->private_key_passphrase_type);
201 service->private_key_passphrase_type = str;
204 str = g_key_file_get_string(keyfile, group, "Identity", NULL);
206 g_free(service->identity);
207 service->identity = str;
210 str = g_key_file_get_string(keyfile, group, "Phase2", NULL);
212 g_free(service->phase2);
213 service->phase2 = str;
216 str = g_key_file_get_string(keyfile, group, "Passphrase", NULL);
218 g_free(service->passphrase);
219 service->passphrase = str;
222 g_hash_table_replace(config->service_table, service->ident, service);
224 connman_info("Adding service configuration %s", service->ident);
229 static int load_config(struct connman_config *config)
237 DBG("config %p", config);
239 keyfile = __connman_storage_open_config(config->ident);
243 str = g_key_file_get_string(keyfile, "global", "Name", NULL);
245 g_free(config->name);
249 str = g_key_file_get_string(keyfile, "global", "Description", NULL);
251 g_free(config->description);
252 config->description = str;
255 groups = g_key_file_get_groups(keyfile, &length);
257 for (i = 0; groups[i] != NULL; i++) {
258 if (g_str_has_prefix(groups[i], "service_") == TRUE)
259 load_service(keyfile, groups[i], config);
264 __connman_storage_close_config(config->ident, keyfile, FALSE);
269 static int create_config(const char *ident)
271 struct connman_config *config;
273 DBG("ident %s", ident);
275 if (g_hash_table_lookup(config_table, ident) != NULL)
278 config = g_try_new0(struct connman_config, 1);
282 config->ident = g_strdup(ident);
284 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
285 NULL, unregister_service);
287 g_hash_table_insert(config_table, config->ident, config);
289 connman_info("Adding configuration %s", config->ident);
296 static int read_configs(void)
302 dir = g_dir_open(STORAGEDIR, 0, NULL);
306 while ((file = g_dir_read_name(dir)) != NULL) {
310 if (g_str_has_suffix(file, ".config") == FALSE)
313 ident = g_strrstr(file, ".config");
317 str = g_string_new_len(file, ident - file);
321 ident = g_string_free(str, FALSE);
323 if (connman_dbus_validate_ident(ident) == TRUE)
324 create_config(ident);
335 int __connman_config_init(void)
339 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
340 NULL, unregister_config);
342 return read_configs();
345 void __connman_config_cleanup(void)
349 g_hash_table_destroy(config_table);
353 static char *config_pem_fsid(const char *pem_file)
356 unsigned *fsid = (unsigned *) &buf.f_fsid;
357 unsigned long long fsid64;
359 if (pem_file == NULL)
362 if (statfs(pem_file, &buf) < 0) {
363 connman_error("statfs error %s for %s",
364 strerror(errno), pem_file);
368 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
370 return g_strdup_printf("%llx", fsid64);
373 static void provision_service(gpointer key, gpointer value, gpointer user_data)
375 struct connman_service *service = user_data;
376 struct connman_config_service *config = value;
377 struct connman_network *network;
379 unsigned int ssid_len;
381 /* For now only WiFi service entries are supported */
382 if (g_strcmp0(config->type, "wifi") != 0)
385 network = __connman_service_get_network(service);
386 if (network == NULL) {
387 connman_error("Service has no network set");
391 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
393 connman_error("Network SSID not set");
397 if (config->ssid == NULL || ssid_len != config->ssid_len)
400 if (memcmp(config->ssid, ssid, ssid_len) != 0)
403 __connman_service_set_immutable(service, TRUE);
404 __connman_service_set_favorite(service, TRUE);
406 if (config->eap != NULL)
407 __connman_service_set_string(service, "EAP", config->eap);
409 if (config->identity != NULL)
410 __connman_service_set_string(service, "Identity",
413 if (config->ca_cert_file != NULL)
414 __connman_service_set_string(service, "CACertFile",
415 config->ca_cert_file);
417 if (config->client_cert_file != NULL)
418 __connman_service_set_string(service, "ClientCertFile",
419 config->client_cert_file);
421 if (config->private_key_file != NULL)
422 __connman_service_set_string(service, "PrivateKeyFile",
423 config->private_key_file);
425 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
426 config->private_key_file != NULL) {
429 fsid = config_pem_fsid(config->private_key_file);
433 g_free(config->private_key_passphrase);
434 config->private_key_passphrase = fsid;
437 if (config->private_key_passphrase != NULL) {
438 __connman_service_set_string(service, "PrivateKeyPassphrase",
439 config->private_key_passphrase);
441 * TODO: Support for PEAP with both identity and key passwd.
442 * In that case, we should check if both of them are found
443 * from the config file. If not, we should not set the
444 * service passphrase in order for the UI to request for an
445 * additional passphrase.
449 if (config->phase2 != NULL)
450 __connman_service_set_string(service, "Phase2", config->phase2);
452 if (config->passphrase != NULL)
453 __connman_service_set_string(service, "Passphrase", config->passphrase);
456 int __connman_config_provision_service(struct connman_service *service)
458 enum connman_service_type type;
462 DBG("service %p", service);
464 /* For now only WiFi services are supported */
465 type = connman_service_get_type(service);
466 if (type != CONNMAN_SERVICE_TYPE_WIFI)
469 g_hash_table_iter_init(&iter, config_table);
471 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
472 struct connman_config *config = value;
474 g_hash_table_foreach(config->service_table,
475 provision_service, service);