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;
46 connman_bool_t passphrase_from_fsid;
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->phase2);
92 static int load_service(GKeyFile *keyfile, const char *group,
93 struct connman_config *config)
95 struct connman_config_service *service;
98 gboolean pass_from_fsid;
101 /* Strip off "service_" prefix */
104 if (strlen(ident) < 1)
107 service = g_hash_table_lookup(config->service_table, ident);
108 if (service == NULL) {
109 service = g_try_new0(struct connman_config_service, 1);
113 service->ident = g_strdup(ident);
116 str = g_key_file_get_string(keyfile, group, "Type", NULL);
118 g_free(service->type);
122 str = g_key_file_get_string(keyfile, group, "Name", NULL);
124 g_free(service->type);
128 hex_ssid = g_key_file_get_string(keyfile, group, "SSID", NULL);
129 if (hex_ssid != NULL) {
131 unsigned int i, j = 0, hex;
132 size_t hex_ssid_len = strlen(hex_ssid);
134 ssid = g_try_malloc0(hex_ssid_len / 2);
140 for (i = 0; i < hex_ssid_len; i += 2) {
141 sscanf(hex_ssid + i, "%02x", &hex);
147 g_free(service->ssid);
148 service->ssid = ssid;
149 service->ssid_len = hex_ssid_len / 2;
150 } else if (service->name != NULL) {
152 unsigned int ssid_len;
154 ssid_len = strlen(service->name);
155 ssid = g_try_malloc0(ssid_len);
159 memcpy(ssid, service->name, ssid_len);
160 g_free(service->ssid);
161 service->ssid = ssid;
162 service->ssid_len = ssid_len;
165 str = g_key_file_get_string(keyfile, group, "EAP", NULL);
167 g_free(service->eap);
171 str = g_key_file_get_string(keyfile, group, "CACertFile", NULL);
173 g_free(service->ca_cert_file);
174 service->ca_cert_file = str;
177 str = g_key_file_get_string(keyfile, group, "ClientCertFile", NULL);
179 g_free(service->client_cert_file);
180 service->client_cert_file = str;
183 str = g_key_file_get_string(keyfile, group, "PrivateKeyFile", NULL);
185 g_free(service->private_key_file);
186 service->private_key_file = str;
189 str = g_key_file_get_string(keyfile, group,
190 "PrivateKeyPassphrase", NULL);
192 g_free(service->private_key_passphrase);
193 service->private_key_passphrase = str;
196 str = g_key_file_get_string(keyfile, group, "Identity", NULL);
198 g_free(service->identity);
199 service->identity = str;
202 str = g_key_file_get_string(keyfile, group, "Phase2", NULL);
204 g_free(service->phase2);
205 service->phase2 = str;
208 pass_from_fsid = g_key_file_get_boolean(keyfile, group,
209 "PassphraseFromFsid", &error);
211 service->passphrase_from_fsid = pass_from_fsid;
213 g_hash_table_replace(config->service_table, service->ident, service);
215 connman_info("Adding service configuration %s", service->ident);
220 static int load_config(struct connman_config *config)
228 DBG("config %p", config);
230 keyfile = __connman_storage_open_config(config->ident);
234 str = g_key_file_get_string(keyfile, "global", "Name", NULL);
236 g_free(config->name);
240 str = g_key_file_get_string(keyfile, "global", "Description", NULL);
242 g_free(config->description);
243 config->description = str;
246 groups = g_key_file_get_groups(keyfile, &length);
248 for (i = 0; groups[i] != NULL; i++) {
249 if (g_str_has_prefix(groups[i], "service_") == TRUE)
250 load_service(keyfile, groups[i], config);
255 __connman_storage_close_config(config->ident, keyfile, FALSE);
260 static int create_config(const char *ident)
262 struct connman_config *config;
264 DBG("ident %s", ident);
266 if (g_hash_table_lookup(config_table, ident) != NULL)
269 config = g_try_new0(struct connman_config, 1);
273 config->ident = g_strdup(ident);
275 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
276 NULL, unregister_service);
278 g_hash_table_insert(config_table, config->ident, config);
280 connman_info("Adding configuration %s", config->ident);
287 static int read_configs(void)
293 dir = g_dir_open(STORAGEDIR, 0, NULL);
297 while ((file = g_dir_read_name(dir)) != NULL) {
301 if (g_str_has_suffix(file, ".config") == FALSE)
304 ident = g_strrstr(file, ".config");
308 str = g_string_new_len(file, ident - file);
312 ident = g_string_free(str, FALSE);
314 if (connman_dbus_validate_ident(ident) == TRUE)
315 create_config(ident);
326 int __connman_config_init(void)
330 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
331 NULL, unregister_config);
333 return read_configs();
336 void __connman_config_cleanup(void)
340 g_hash_table_destroy(config_table);
344 static char *config_pem_fsid(const char *pem_file)
347 unsigned *fsid = (unsigned *) &buf.f_fsid;
348 unsigned long long fsid64;
350 if (pem_file == NULL)
353 if (statfs(pem_file, &buf) < 0) {
354 connman_error("statfs error %s for %s",
355 strerror(errno), pem_file);
359 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
361 return g_strdup_printf("%llx", fsid64);
364 static void provision_service(gpointer key, gpointer value, gpointer user_data)
366 struct connman_service *service = user_data;
367 struct connman_config_service *config = value;
368 struct connman_network *network;
370 unsigned int ssid_len;
372 /* For now only WiFi service entries are supported */
373 if (g_strcmp0(config->type, "wifi") != 0)
376 network = __connman_service_get_network(service);
377 if (network == NULL) {
378 connman_error("Service has no network set");
382 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
384 connman_error("Network SSID not set");
388 if (config->ssid == NULL || ssid_len != config->ssid_len)
391 if (memcmp(config->ssid, ssid, ssid_len) != 0)
395 __connman_service_set_string(service, "EAP", config->eap);
397 if (config->identity)
398 __connman_service_set_string(service, "Identity",
401 if (config->ca_cert_file)
402 __connman_service_set_string(service, "CACertFile",
403 config->ca_cert_file);
405 if (config->client_cert_file)
406 __connman_service_set_string(service, "ClientCertFile",
407 config->client_cert_file);
409 if (config->private_key_file)
410 __connman_service_set_string(service, "PrivateKeyFile",
411 config->private_key_file);
413 if (config->passphrase_from_fsid == TRUE &&
414 config->private_key_file) {
417 fsid = config_pem_fsid(config->private_key_file);
421 g_free(config->private_key_passphrase);
422 config->private_key_passphrase = fsid;
425 if (config->private_key_passphrase) {
426 __connman_service_set_string(service, "PrivateKeyPassphrase",
427 config->private_key_passphrase);
429 * TODO: Support for PEAP with both identity and key passwd.
430 * In that case, we should check if both of them are found
431 * from the config file. If not, we should not set the
432 * service passphrase in order for the UI to request for an
433 * additional passphrase.
435 __connman_service_set_string(service, "Passphrase",
436 config->private_key_passphrase);
440 __connman_service_set_string(service, "Phase2", config->phase2);
443 int __connman_config_provision_service(struct connman_service *service)
445 enum connman_service_type type;
449 DBG("service %p", service);
451 /* For now only WiFi services are supported */
452 type = connman_service_get_type(service);
453 if (type != CONNMAN_SERVICE_TYPE_WIFI)
456 g_hash_table_iter_init(&iter, config_table);
458 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
459 struct connman_config *config = value;
461 g_hash_table_foreach(config->service_table,
462 provision_service, service);