5 * Copyright (C) 2007-2009 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 {
36 unsigned int ssid_len;
40 char *client_cert_file;
41 char *private_key_file;
42 char *private_key_passphrase;
44 connman_bool_t passphrase_from_fsid;
47 struct connman_config {
51 struct connman_config_service *service;
54 static GHashTable *config_hash = NULL;
56 static int load_service(GKeyFile *keyfile, struct connman_config *config)
59 struct connman_config_service *service;
60 gboolean pass_from_fsid;
63 service = g_try_new0(struct connman_config_service, 1);
67 config->service = service;
69 str = g_key_file_get_string(keyfile, "service", "Type", NULL);
71 g_free(service->type);
75 hex_ssid = g_key_file_get_string(keyfile, "service", "SSID", NULL);
76 if (hex_ssid != NULL) {
78 unsigned int i, j = 0, hex;
79 size_t hex_ssid_len = strlen(hex_ssid);
81 ssid = g_try_malloc0(hex_ssid_len / 2);
87 for (i = 0; i < hex_ssid_len; i += 2) {
88 sscanf(hex_ssid + i, "%02x", &hex);
94 g_free(service->ssid);
96 service->ssid_len = hex_ssid_len / 2;
99 str = g_key_file_get_string(keyfile, "service", "EAP", NULL);
101 g_free(service->eap);
105 str = g_key_file_get_string(keyfile, "service", "CACertFile", NULL);
107 g_free(service->ca_cert_file);
108 service->ca_cert_file = str;
111 str = g_key_file_get_string(keyfile, "service",
112 "ClientCertFile", NULL);
114 g_free(service->client_cert_file);
115 service->client_cert_file = str;
118 str = g_key_file_get_string(keyfile, "service",
119 "PrivateKeyFile", NULL);
121 g_free(service->private_key_file);
122 service->private_key_file = str;
125 str = g_key_file_get_string(keyfile, "service",
126 "PrivateKeyPassphrase", NULL);
128 g_free(service->private_key_passphrase);
129 service->private_key_passphrase = str;
132 str = g_key_file_get_string(keyfile, "service", "Identity", NULL);
134 g_free(service->identity);
135 service->identity = str;
138 str = g_key_file_get_string(keyfile, "service", "Phase2", NULL);
140 g_free(service->phase2);
141 service->phase2 = str;
144 pass_from_fsid = g_key_file_get_boolean(keyfile, "service",
145 "PassphraseFromFsid", &error);
147 service->passphrase_from_fsid = pass_from_fsid;
152 static void free_service(struct connman_config_service *service)
157 g_free(service->type);
158 g_free(service->ssid);
159 g_free(service->eap);
160 g_free(service->identity);
161 g_free(service->ca_cert_file);
162 g_free(service->client_cert_file);
163 g_free(service->private_key_file);
164 g_free(service->private_key_passphrase);
165 g_free(service->phase2);
169 static int load_config(struct connman_config *config)
175 DBG("config %p", config);
177 keyfile = __connman_storage_open_config(config->ident);
181 str = g_key_file_get_string(keyfile, "global", "Name", NULL);
183 g_free(config->name);
187 str = g_key_file_get_string(keyfile, "global", "Description", NULL);
189 g_free(config->description);
190 config->description = str;
193 if (g_key_file_has_group(keyfile, "service") == TRUE) {
194 err = load_service(keyfile, config);
202 __connman_storage_close_config(config->ident, keyfile, FALSE);
207 static void free_config(struct connman_config *config)
209 free_service(config->service);
210 g_free(config->description);
211 g_free(config->name);
212 g_free(config->ident);
216 static void unregister_config(gpointer data)
218 struct connman_config *config = data;
220 connman_info("Removing configuration %s", config->ident);
225 static int create_config(const char *ident)
227 struct connman_config *config;
229 DBG("ident %s", ident);
231 config = g_try_new0(struct connman_config, 1);
235 config->ident = g_strdup(ident);
237 if (config->ident == NULL) {
242 if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
247 g_hash_table_insert(config_hash, g_strdup(config->ident), config);
249 connman_info("Adding configuration %s", config->ident);
256 static int config_init(void)
263 dir = g_dir_open(STORAGEDIR, 0, NULL);
265 while ((file = g_dir_read_name(dir)) != NULL) {
269 if (g_str_has_suffix(file, ".config") == FALSE)
272 ident = g_strrstr(file, ".config");
276 str = g_string_new_len(file, ident - file);
280 ident = g_string_free(str, FALSE);
282 if (connman_dbus_validate_ident(ident) == TRUE)
283 create_config(ident);
294 static char *config_pem_fsid(char *pem_file)
297 unsigned *fsid = (unsigned *) &buf.f_fsid;
298 unsigned long long fsid64;
300 if (pem_file == NULL)
303 if (statfs(pem_file, &buf) < 0) {
304 connman_error("statfs error %s for %s",
305 strerror(errno), pem_file);
309 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
311 return g_strdup_printf("%llx", fsid64);
314 static void config_service_setup(struct connman_service *service,
315 struct connman_config_service *config)
321 __connman_service_set_string(service, "EAP", config->eap);
323 if (config->identity)
324 __connman_service_set_string(service, "Identity",
327 if (config->ca_cert_file)
328 __connman_service_set_string(service, "CACertFile",
329 config->ca_cert_file);
331 if (config->client_cert_file)
332 __connman_service_set_string(service, "ClientCertFile",
333 config->client_cert_file);
335 if (config->private_key_file)
336 __connman_service_set_string(service, "PrivateKeyFile",
337 config->private_key_file);
339 if (config->passphrase_from_fsid == TRUE &&
340 config->private_key_file) {
343 fsid = config_pem_fsid(config->private_key_file);
347 g_free(config->private_key_passphrase);
348 config->private_key_passphrase = fsid;
351 if (config->private_key_passphrase) {
352 __connman_service_set_string(service, "PrivateKeyPassphrase",
353 config->private_key_passphrase);
355 * TODO: Support for PEAP with both identity and key passwd.
356 * In that case, we should check if both of them are found
357 * from the config file. If not, we should not set the
358 * service passphrase in order for the UI to request for an
359 * additional passphrase.
361 __connman_service_set_string(service, "Passphrase",
362 config->private_key_passphrase);
366 __connman_service_set_string(service, "Phase2", config->phase2);
369 int __connman_config_init(void)
373 config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
374 g_free, unregister_config);
376 return config_init();
379 void __connman_config_cleanup(void)
383 g_hash_table_destroy(config_hash);
387 int __connman_config_provision_service(struct connman_service *service)
391 struct connman_network *network;
393 unsigned int ssid_len;
395 DBG("service %p", service);
397 network = __connman_service_get_network(service);
398 if (network == NULL) {
399 connman_error("Network not set");
403 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
405 connman_error("Network SSID not set");
409 g_hash_table_iter_init(&iter, config_hash);
411 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
412 struct connman_config *config = value;
414 if (config->service == NULL)
417 /* For now only WiFi service entries are supported */
418 if (g_strcmp0(config->service->type, "wifi") != 0)
421 if (config->service->ssid == NULL ||
422 ssid_len != config->service->ssid_len)
425 if (memcmp(config->service->ssid, ssid, ssid_len) == 0) {
426 config_service_setup(service, config->service);