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 const char *config_possible_keys[] = {
82 static const char *service_possible_keys[] = {
90 SERVICE_KEY_PRV_KEY_PASS,
91 SERVICE_KEY_PRV_KEY_PASS_TYPE,
94 SERVICE_KEY_PASSPHRASE,
98 static void unregister_config(gpointer data)
100 struct connman_config *config = data;
102 connman_info("Removing configuration %s", config->ident);
104 g_hash_table_destroy(config->service_table);
106 g_free(config->description);
107 g_free(config->name);
108 g_free(config->ident);
112 static void unregister_service(gpointer data)
114 struct connman_config_service *service = data;
116 connman_info("Removing service configuration %s", service->ident);
118 g_free(service->ident);
119 g_free(service->type);
120 g_free(service->name);
121 g_free(service->ssid);
122 g_free(service->eap);
123 g_free(service->identity);
124 g_free(service->ca_cert_file);
125 g_free(service->client_cert_file);
126 g_free(service->private_key_file);
127 g_free(service->private_key_passphrase);
128 g_free(service->private_key_passphrase_type);
129 g_free(service->phase2);
130 g_free(service->passphrase);
134 static void check_keys(GKeyFile *keyfile, const char *group,
135 const char **possible_keys)
138 gsize nb_avail_keys, i, j;
140 avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
141 if (avail_keys == NULL)
145 * For each key in the configuration file,
146 * verify it is understood by connman
148 for (i = 0 ; i < nb_avail_keys; i++) {
149 for (j = 0; possible_keys[j] ; j++)
150 if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
153 if (possible_keys[j] == NULL)
154 connman_warn("Unknown configuration key %s in [%s]",
155 avail_keys[i], group);
158 g_strfreev(avail_keys);
161 static int load_service(GKeyFile *keyfile, const char *group,
162 struct connman_config *config)
164 struct connman_config_service *service;
166 char *str, *hex_ssid;
168 /* Strip off "service_" prefix */
171 if (strlen(ident) < 1)
174 /* Verify that provided keys are good */
175 check_keys(keyfile, group, service_possible_keys);
177 service = g_hash_table_lookup(config->service_table, ident);
178 if (service == NULL) {
179 service = g_try_new0(struct connman_config_service, 1);
183 service->ident = g_strdup(ident);
186 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
188 g_free(service->type);
192 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
194 g_free(service->name);
198 hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
200 if (hex_ssid != NULL) {
202 unsigned int i, j = 0, hex;
203 size_t hex_ssid_len = strlen(hex_ssid);
205 ssid = g_try_malloc0(hex_ssid_len / 2);
211 for (i = 0; i < hex_ssid_len; i += 2) {
212 sscanf(hex_ssid + i, "%02x", &hex);
218 g_free(service->ssid);
219 service->ssid = ssid;
220 service->ssid_len = hex_ssid_len / 2;
221 } else if (service->name != NULL) {
223 unsigned int ssid_len;
225 ssid_len = strlen(service->name);
226 ssid = g_try_malloc0(ssid_len);
230 memcpy(ssid, service->name, ssid_len);
231 g_free(service->ssid);
232 service->ssid = ssid;
233 service->ssid_len = ssid_len;
236 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
238 g_free(service->eap);
242 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
244 g_free(service->ca_cert_file);
245 service->ca_cert_file = str;
248 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
250 g_free(service->client_cert_file);
251 service->client_cert_file = str;
254 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
256 g_free(service->private_key_file);
257 service->private_key_file = str;
260 str = g_key_file_get_string(keyfile, group,
261 SERVICE_KEY_PRV_KEY_PASS, NULL);
263 g_free(service->private_key_passphrase);
264 service->private_key_passphrase = str;
267 str = g_key_file_get_string(keyfile, group,
268 SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
270 g_free(service->private_key_passphrase_type);
271 service->private_key_passphrase_type = str;
274 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
276 g_free(service->identity);
277 service->identity = str;
280 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
282 g_free(service->phase2);
283 service->phase2 = str;
286 str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
289 g_free(service->passphrase);
290 service->passphrase = str;
293 g_hash_table_replace(config->service_table, service->ident, service);
295 connman_info("Adding service configuration %s", service->ident);
300 static int load_config(struct connman_config *config)
308 DBG("config %p", config);
310 keyfile = __connman_storage_open_config(config->ident);
314 /* Verify keys validity of the global section */
315 check_keys(keyfile, "global", config_possible_keys);
317 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
319 g_free(config->name);
323 str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
325 g_free(config->description);
326 config->description = str;
329 groups = g_key_file_get_groups(keyfile, &length);
331 for (i = 0; groups[i] != NULL; i++) {
332 if (g_str_has_prefix(groups[i], "service_") == TRUE)
333 load_service(keyfile, groups[i], config);
338 __connman_storage_close_config(config->ident, keyfile, FALSE);
343 static int create_config(const char *ident)
345 struct connman_config *config;
347 DBG("ident %s", ident);
349 if (g_hash_table_lookup(config_table, ident) != NULL)
352 config = g_try_new0(struct connman_config, 1);
356 config->ident = g_strdup(ident);
358 config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
359 NULL, unregister_service);
361 g_hash_table_insert(config_table, config->ident, config);
363 connman_info("Adding configuration %s", config->ident);
370 static int read_configs(void)
376 dir = g_dir_open(STORAGEDIR, 0, NULL);
380 while ((file = g_dir_read_name(dir)) != NULL) {
384 if (g_str_has_suffix(file, ".config") == FALSE)
387 ident = g_strrstr(file, ".config");
391 str = g_string_new_len(file, ident - file);
395 ident = g_string_free(str, FALSE);
397 if (connman_dbus_validate_ident(ident) == TRUE)
398 create_config(ident);
409 int __connman_config_init(void)
413 config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
414 NULL, unregister_config);
416 return read_configs();
419 void __connman_config_cleanup(void)
423 g_hash_table_destroy(config_table);
427 static char *config_pem_fsid(const char *pem_file)
430 unsigned *fsid = (unsigned *) &buf.f_fsid;
431 unsigned long long fsid64;
433 if (pem_file == NULL)
436 if (statfs(pem_file, &buf) < 0) {
437 connman_error("statfs error %s for %s",
438 strerror(errno), pem_file);
442 fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
444 return g_strdup_printf("%llx", fsid64);
447 static void provision_service(gpointer key, gpointer value, gpointer user_data)
449 struct connman_service *service = user_data;
450 struct connman_config_service *config = value;
451 struct connman_network *network;
453 unsigned int ssid_len;
455 /* For now only WiFi service entries are supported */
456 if (g_strcmp0(config->type, "wifi") != 0)
459 network = __connman_service_get_network(service);
460 if (network == NULL) {
461 connman_error("Service has no network set");
465 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
467 connman_error("Network SSID not set");
471 if (config->ssid == NULL || ssid_len != config->ssid_len)
474 if (memcmp(config->ssid, ssid, ssid_len) != 0)
477 __connman_service_set_immutable(service, TRUE);
478 __connman_service_set_favorite(service, TRUE);
480 if (config->eap != NULL)
481 __connman_service_set_string(service, "EAP", config->eap);
483 if (config->identity != NULL)
484 __connman_service_set_string(service, "Identity",
487 if (config->ca_cert_file != NULL)
488 __connman_service_set_string(service, "CACertFile",
489 config->ca_cert_file);
491 if (config->client_cert_file != NULL)
492 __connman_service_set_string(service, "ClientCertFile",
493 config->client_cert_file);
495 if (config->private_key_file != NULL)
496 __connman_service_set_string(service, "PrivateKeyFile",
497 config->private_key_file);
499 if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
500 config->private_key_file != NULL) {
503 fsid = config_pem_fsid(config->private_key_file);
507 g_free(config->private_key_passphrase);
508 config->private_key_passphrase = fsid;
511 if (config->private_key_passphrase != NULL) {
512 __connman_service_set_string(service, "PrivateKeyPassphrase",
513 config->private_key_passphrase);
515 * TODO: Support for PEAP with both identity and key passwd.
516 * In that case, we should check if both of them are found
517 * from the config file. If not, we should not set the
518 * service passphrase in order for the UI to request for an
519 * additional passphrase.
523 if (config->phase2 != NULL)
524 __connman_service_set_string(service, "Phase2", config->phase2);
526 if (config->passphrase != NULL)
527 __connman_service_set_string(service, "Passphrase", config->passphrase);
530 int __connman_config_provision_service(struct connman_service *service)
532 enum connman_service_type type;
536 DBG("service %p", service);
538 /* For now only WiFi services are supported */
539 type = connman_service_get_type(service);
540 if (type != CONNMAN_SERVICE_TYPE_WIFI)
543 g_hash_table_iter_init(&iter, config_table);
545 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
546 struct connman_config *config = value;
548 g_hash_table_foreach(config->service_table,
549 provision_service, service);