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",
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)
154 g_free(service->type);
155 g_free(service->ssid);
156 g_free(service->eap);
157 g_free(service->identity);
158 g_free(service->ca_cert_file);
159 g_free(service->client_cert_file);
160 g_free(service->private_key_file);
161 g_free(service->private_key_passphrase);
162 g_free(service->phase2);
166 static int load_config(struct connman_config *config)
172 DBG("config %p", config);
174 keyfile = __connman_storage_open_config(config->ident);
178 str = g_key_file_get_string(keyfile, "global", "Name", NULL);
180 g_free(config->name);
184 str = g_key_file_get_string(keyfile, "global", "Description", NULL);
186 g_free(config->description);
187 config->description = str;
190 if (g_key_file_has_group(keyfile, "service") == TRUE) {
191 err = load_service(keyfile, config);
199 __connman_storage_close_config(config->ident, keyfile, FALSE);
204 static void free_config(struct connman_config *config)
206 g_free(config->description);
207 g_free(config->name);
208 g_free(config->ident);
209 free_service(config->service);
213 static void unregister_config(gpointer data)
215 struct connman_config *config = data;
217 connman_info("Removing configuration %s", config->ident);
222 static int create_config(const char *ident)
224 struct connman_config *config;
226 DBG("ident %s", ident);
228 config = g_try_new0(struct connman_config, 1);
232 config->ident = g_strdup(ident);
234 if (config->ident == NULL) {
239 if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
244 g_hash_table_insert(config_hash, g_strdup(config->ident), config);
246 connman_info("Adding configuration %s", config->ident);
253 static int config_init(void)
260 dir = g_dir_open(STORAGEDIR, 0, NULL);
262 while ((file = g_dir_read_name(dir)) != NULL) {
266 if (g_str_has_suffix(file, ".config") == FALSE)
269 ident = g_strrstr(file, ".config");
273 str = g_string_new_len(file, ident - file);
277 ident = g_string_free(str, FALSE);
279 if (connman_dbus_validate_ident(ident) == TRUE)
280 create_config(ident);
291 static char *config_pem_fsid(char *pem_file)
294 unsigned *fsid = (unsigned *) &buf.f_fsid;
295 unsigned long long fsid64;
297 if (pem_file == NULL)
300 if (statfs(pem_file, &buf) < 0) {
301 connman_error("statfs error %s for %s",
302 strerror(errno), pem_file);
306 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
308 return g_strdup_printf("%llx", fsid64);
311 static void config_service_setup(struct connman_service *service,
312 struct connman_config_service *config)
318 __connman_service_set_string(service, "EAP", config->eap);
320 if (config->identity)
321 __connman_service_set_string(service, "Identity",
324 if (config->ca_cert_file)
325 __connman_service_set_string(service, "CACertFile",
326 config->ca_cert_file);
328 if (config->client_cert_file)
329 __connman_service_set_string(service, "ClientCertFile",
330 config->client_cert_file);
332 if (config->private_key_file)
333 __connman_service_set_string(service, "PrivateKeyFile",
334 config->private_key_file);
336 if (config->passphrase_from_fsid == TRUE &&
337 config->private_key_file) {
340 fsid = config_pem_fsid(config->private_key_file);
344 g_free(config->private_key_passphrase);
345 config->private_key_passphrase = fsid;
348 if (config->private_key_passphrase) {
349 __connman_service_set_string(service, "PrivateKeyPassphrase",
350 config->private_key_passphrase);
352 * TODO: Support for PEAP with both identity and key passwd.
353 * In that case, we should check if both of them are found
354 * from the config file. If not, we should not set the
355 * service passphrase in order for the UI to request for an
356 * additional passphrase.
358 __connman_service_set_string(service, "Passphrase",
359 config->private_key_passphrase);
363 __connman_service_set_string(service, "Phase2", config->phase2);
366 int __connman_config_init(void)
370 config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
371 g_free, unregister_config);
373 return config_init();
376 void __connman_config_cleanup(void)
380 g_hash_table_destroy(config_hash);
384 int __connman_config_provision_service(struct connman_service *service)
388 struct connman_network *network;
389 struct connman_config *config = NULL;
391 unsigned int ssid_len;
393 DBG("service %p", service);
395 network = __connman_service_get_network(service);
396 if (network == NULL) {
397 connman_error("Network not set");
401 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
403 connman_error("Network SSID not set");
407 g_hash_table_iter_init(&iter, config_hash);
409 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
412 /* For now only WiFi service entries are supported */
413 if (config->service &&
414 g_strcmp0(config->service->type, "wifi") == 0 &&
415 ssid_len == config->service->ssid_len)
416 if (config->service->ssid &&
417 memcmp(config->service->ssid, ssid,
422 config_service_setup(service, config->service);