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 /* Definition of possible strings in the .config files */
60 #define CONFIG_KEY_NAME "Name"
61 #define CONFIG_KEY_DESC "Description"
63 #define SERVICE_KEY_TYPE "Type"
64 #define SERVICE_KEY_NAME "Name"
65 #define SERVICE_KEY_SSID "SSID"
66 #define SERVICE_KEY_EAP "EAP"
67 #define SERVICE_KEY_CA_CERT "CACertFile"
68 #define SERVICE_KEY_CL_CERT "ClientCertFile"
69 #define SERVICE_KEY_PRV_KEY "PrivateKeyFile"
70 #define SERVICE_KEY_PRV_KEY_PASS "PrivateKeyPassphrase"
71 #define SERVICE_KEY_PRV_KEY_PASS_TYPE "PrivateKeyPassphraseType"
72 #define SERVICE_KEY_IDENTITY "Identity"
73 #define SERVICE_KEY_PHASE2 "Phase2"
74 #define SERVICE_KEY_PASSPHRASE "Passphrase"
76 static void unregister_config(gpointer data)
78 struct connman_config *config = data;
80 connman_info("Removing configuration %s", config->ident);
82 g_hash_table_destroy(config->service_table);
84 g_free(config->description);
86 g_free(config->ident);
90 static void unregister_service(gpointer data)
92 struct connman_config_service *service = data;
94 connman_info("Removing service configuration %s", service->ident);
96 g_free(service->ident);
97 g_free(service->type);
98 g_free(service->name);
99 g_free(service->ssid);
100 g_free(service->eap);
101 g_free(service->identity);
102 g_free(service->ca_cert_file);
103 g_free(service->client_cert_file);
104 g_free(service->private_key_file);
105 g_free(service->private_key_passphrase);
106 g_free(service->private_key_passphrase_type);
107 g_free(service->phase2);
108 g_free(service->passphrase);
112 static int load_service(GKeyFile *keyfile, const char *group,
113 struct connman_config *config)
115 struct connman_config_service *service;
117 char *str, *hex_ssid;
119 /* Strip off "service_" prefix */
122 if (strlen(ident) < 1)
125 service = g_hash_table_lookup(config->service_table, ident);
126 if (service == NULL) {
127 service = g_try_new0(struct connman_config_service, 1);
131 service->ident = g_strdup(ident);
134 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
136 g_free(service->type);
140 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
142 g_free(service->name);
146 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
148 if (hex_ssid != NULL) {
150 unsigned int i, j = 0, hex;
151 size_t hex_ssid_len = strlen(hex_ssid);
153 ssid = g_try_malloc0(hex_ssid_len / 2);
159 for (i = 0; i < hex_ssid_len; i += 2) {
160 sscanf(hex_ssid + i, "%02x", &hex);
166 g_free(service->ssid);
167 service->ssid = ssid;
168 service->ssid_len = hex_ssid_len / 2;
169 } else if (service->name != NULL) {
171 unsigned int ssid_len;
173 ssid_len = strlen(service->name);
174 ssid = g_try_malloc0(ssid_len);
178 memcpy(ssid, service->name, ssid_len);
179 g_free(service->ssid);
180 service->ssid = ssid;
181 service->ssid_len = ssid_len;
184 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
186 g_free(service->eap);
190 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
192 g_free(service->ca_cert_file);
193 service->ca_cert_file = str;
196 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
198 g_free(service->client_cert_file);
199 service->client_cert_file = str;
202 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
204 g_free(service->private_key_file);
205 service->private_key_file = str;
208 str = g_key_file_get_string(keyfile, group,
209 SERVICE_KEY_PRV_KEY_PASS, NULL);
211 g_free(service->private_key_passphrase);
212 service->private_key_passphrase = str;
215 str = g_key_file_get_string(keyfile, group,
216 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
218 g_free(service->private_key_passphrase_type);
219 service->private_key_passphrase_type = str;
222 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
224 g_free(service->identity);
225 service->identity = str;
228 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
230 g_free(service->phase2);
231 service->phase2 = str;
234 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
237 g_free(service->passphrase);
238 service->passphrase = str;
241 g_hash_table_replace(config->service_table, service->ident, service);
243 connman_info("Adding service configuration %s", service->ident);
248 static int load_config(struct connman_config *config)
256 DBG("config %p", config);
258 keyfile = __connman_storage_open_config(config->ident);
262 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
264 g_free(config->name);
268 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
270 g_free(config->description);
271 config->description = str;
274 groups = g_key_file_get_groups(keyfile, &length);
276 for (i = 0; groups[i] != NULL; i++) {
277 if (g_str_has_prefix(groups[i], "service_") == TRUE)
278 load_service(keyfile, groups[i], config);
283 __connman_storage_close_config(config->ident, keyfile, FALSE);
288 static int create_config(const char *ident)
290 struct connman_config *config;
292 DBG("ident %s", ident);
294 if (g_hash_table_lookup(config_table, ident) != NULL)
297 config = g_try_new0(struct connman_config, 1);
301 config->ident = g_strdup(ident);
303 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
304 NULL, unregister_service);
306 g_hash_table_insert(config_table, config->ident, config);
308 connman_info("Adding configuration %s", config->ident);
315 static int read_configs(void)
321 dir = g_dir_open(STORAGEDIR, 0, NULL);
325 while ((file = g_dir_read_name(dir)) != NULL) {
329 if (g_str_has_suffix(file, ".config") == FALSE)
332 ident = g_strrstr(file, ".config");
336 str = g_string_new_len(file, ident - file);
340 ident = g_string_free(str, FALSE);
342 if (connman_dbus_validate_ident(ident) == TRUE)
343 create_config(ident);
354 int __connman_config_init(void)
358 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
359 NULL, unregister_config);
361 return read_configs();
364 void __connman_config_cleanup(void)
368 g_hash_table_destroy(config_table);
372 static char *config_pem_fsid(const char *pem_file)
375 unsigned *fsid = (unsigned *) &buf.f_fsid;
376 unsigned long long fsid64;
378 if (pem_file == NULL)
381 if (statfs(pem_file, &buf) < 0) {
382 connman_error("statfs error %s for %s",
383 strerror(errno), pem_file);
387 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
389 return g_strdup_printf("%llx", fsid64);
392 static void provision_service(gpointer key, gpointer value, gpointer user_data)
394 struct connman_service *service = user_data;
395 struct connman_config_service *config = value;
396 struct connman_network *network;
398 unsigned int ssid_len;
400 /* For now only WiFi service entries are supported */
401 if (g_strcmp0(config->type, "wifi") != 0)
404 network = __connman_service_get_network(service);
405 if (network == NULL) {
406 connman_error("Service has no network set");
410 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
412 connman_error("Network SSID not set");
416 if (config->ssid == NULL || ssid_len != config->ssid_len)
419 if (memcmp(config->ssid, ssid, ssid_len) != 0)
422 __connman_service_set_immutable(service, TRUE);
423 __connman_service_set_favorite(service, TRUE);
425 if (config->eap != NULL)
426 __connman_service_set_string(service, "EAP", config->eap);
428 if (config->identity != NULL)
429 __connman_service_set_string(service, "Identity",
432 if (config->ca_cert_file != NULL)
433 __connman_service_set_string(service, "CACertFile",
434 config->ca_cert_file);
436 if (config->client_cert_file != NULL)
437 __connman_service_set_string(service, "ClientCertFile",
438 config->client_cert_file);
440 if (config->private_key_file != NULL)
441 __connman_service_set_string(service, "PrivateKeyFile",
442 config->private_key_file);
444 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
445 config->private_key_file != NULL) {
448 fsid = config_pem_fsid(config->private_key_file);
452 g_free(config->private_key_passphrase);
453 config->private_key_passphrase = fsid;
456 if (config->private_key_passphrase != NULL) {
457 __connman_service_set_string(service, "PrivateKeyPassphrase",
458 config->private_key_passphrase);
460 * TODO: Support for PEAP with both identity and key passwd.
461 * In that case, we should check if both of them are found
462 * from the config file. If not, we should not set the
463 * service passphrase in order for the UI to request for an
464 * additional passphrase.
468 if (config->phase2 != NULL)
469 __connman_service_set_string(service, "Phase2", config->phase2);
471 if (config->passphrase != NULL)
472 __connman_service_set_string(service, "Passphrase", config->passphrase);
475 int __connman_config_provision_service(struct connman_service *service)
477 enum connman_service_type type;
481 DBG("service %p", service);
483 /* For now only WiFi services are supported */
484 type = connman_service_get_type(service);
485 if (type != CONNMAN_SERVICE_TYPE_WIFI)
488 g_hash_table_iter_init(&iter, config_table);
490 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
491 struct connman_config *config = value;
493 g_hash_table_foreach(config->service_table,
494 provision_service, service);