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->ssid);
75 hex_ssid = g_key_file_get_string(keyfile, "service", "Type", 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->type);
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", "ClientCertFile", NULL);
113 g_free(service->client_cert_file);
114 service->client_cert_file = str;
117 str = g_key_file_get_string(keyfile, "service", "PrivateKeyFile", NULL);
119 g_free(service->private_key_file);
120 service->private_key_file = str;
123 str = g_key_file_get_string(keyfile, "service", "PrivateKeyPassphrase",
126 g_free(service->private_key_passphrase);
127 service->private_key_passphrase = str;
130 str = g_key_file_get_string(keyfile, "service", "Identity", NULL);
132 g_free(service->identity);
133 service->identity = str;
136 str = g_key_file_get_string(keyfile, "service", "Phase2", NULL);
138 g_free(service->phase2);
139 service->phase2 = str;
142 pass_from_fsid = g_key_file_get_boolean(keyfile, "service",
143 "PassphraseFromFsid", &error);
145 service->passphrase_from_fsid = pass_from_fsid;
151 static void free_service(struct connman_config_service *service)
153 g_free(service->type);
154 g_free(service->ssid);
155 g_free(service->eap);
156 g_free(service->identity);
157 g_free(service->ca_cert_file);
158 g_free(service->client_cert_file);
159 g_free(service->private_key_file);
160 g_free(service->private_key_passphrase);
161 g_free(service->phase2);
165 static int load_config(struct connman_config *config)
171 DBG("config %p", config);
173 keyfile = __connman_storage_open_config(config->ident);
177 str = g_key_file_get_string(keyfile, "global", "Name", NULL);
179 g_free(config->name);
183 str = g_key_file_get_string(keyfile, "global", "Description", NULL);
185 g_free(config->description);
186 config->description = str;
189 if (g_key_file_has_group(keyfile, "service") == TRUE) {
190 err = load_service(keyfile, config);
198 __connman_storage_close_config(config->ident, keyfile, FALSE);
203 static void free_config(struct connman_config *config)
205 g_free(config->description);
206 g_free(config->name);
207 g_free(config->ident);
208 free_service(config->service);
212 static void unregister_config(gpointer data)
214 struct connman_config *config = data;
216 connman_info("Removing configuration %s", config->ident);
221 static int create_config(const char *ident)
223 struct connman_config *config;
225 DBG("ident %s", ident);
227 config = g_try_new0(struct connman_config, 1);
231 config->ident = g_strdup(ident);
233 if (config->ident == NULL) {
238 if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
243 g_hash_table_insert(config_hash, g_strdup(config->ident), config);
245 connman_info("Adding configuration %s", config->ident);
252 static int config_init(void)
259 dir = g_dir_open(STORAGEDIR, 0, NULL);
261 while ((file = g_dir_read_name(dir)) != NULL) {
265 if (g_str_has_suffix(file, ".config") == FALSE)
268 ident = g_strrstr(file, ".config");
272 str = g_string_new_len(file, ident - file);
276 ident = g_string_free(str, FALSE);
278 if (connman_dbus_validate_ident(ident) == TRUE)
279 create_config(ident);
290 static char *config_pem_fsid(char *pem_file)
293 unsigned *fsid = (unsigned *) &buf.f_fsid;
294 unsigned long long fsid64;
296 if (pem_file == NULL)
299 if (statfs(pem_file, &buf) < 0) {
300 connman_error("statfs error %s for %s",
301 strerror(errno), pem_file);
305 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
307 return g_strdup_printf("%llx", fsid64);
310 static void config_service_setup(struct connman_service *service,
311 struct connman_config_service *config)
317 __connman_service_set_string(service, "EAP", config->eap);
319 if (config->identity)
320 __connman_service_set_string(service, "Identity",
323 if (config->ca_cert_file)
324 __connman_service_set_string(service, "CACertFile",
325 config->ca_cert_file);
327 if (config->client_cert_file)
328 __connman_service_set_string(service, "ClientCertFile",
329 config->client_cert_file);
331 if (config->private_key_file)
332 __connman_service_set_string(service, "PrivateKeyFile",
333 config->private_key_file);
335 if (config->passphrase_from_fsid == TRUE &&
336 config->private_key_file) {
339 fsid = config_pem_fsid(config->private_key_file);
343 g_free(config->private_key_passphrase);
344 config->private_key_passphrase = fsid;
347 if (config->private_key_passphrase) {
348 __connman_service_set_string(service, "PrivateKeyPassphrase",
349 config->private_key_passphrase);
351 * TODO: Support for PEAP with both identity and key passwd.
352 * In that case, we should check if both of them are found
353 * from the config file. If not, we should not set the
354 * service passphrase in order for the UI to request for an
355 * additional passphrase.
357 __connman_service_set_string(service, "Passphrase",
358 config->private_key_passphrase);
362 __connman_service_set_string(service, "Phase2", config->phase2);
365 int __connman_config_init(void)
369 config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
370 g_free, unregister_config);
372 return config_init();
375 void __connman_config_cleanup(void)
379 g_hash_table_destroy(config_hash);
383 int __connman_config_provision_service(struct connman_service *service)
387 struct connman_network *network;
388 struct connman_config *config = NULL;
390 unsigned int ssid_len;
392 DBG("service %p", service);
394 network = __connman_service_get_network(service);
395 if (network == NULL) {
396 connman_error("Network not set");
400 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
402 connman_error("Network SSID not set");
406 g_hash_table_iter_init(&iter, config_hash);
407 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
410 /* For now we only support wifi services entries */
411 if (config->service &&
412 g_strcmp0(config->service->type, "wifi") == 0 &&
413 ssid_len == config->service->ssid_len)
414 if (config->service->ssid &&
415 memcmp(config->service->ssid, ssid,
420 config_service_setup(service, config->service);