2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 #include "wifi-agent.h"
29 #include "wifi-state.h"
30 #include "wifi-config.h"
31 #include "wifi-eap-config.h"
34 #define CONNMAN_CONFIG_FIELD_TYPE "Type"
35 #define CONNMAN_CONFIG_FIELD_NAME "Name"
36 #define CONNMAN_CONFIG_FIELD_SSID "SSID"
37 #define CONNMAN_CONFIG_FIELD_EAP_METHOD "EAP"
38 #define CONNMAN_CONFIG_FIELD_IDENTITY "Identity"
39 #define CONNMAN_CONFIG_FIELD_PASSPHRASE "Passphrase"
40 #define CONNMAN_CONFIG_FIELD_PHASE2 "Phase2"
41 #define CONNMAN_CONFIG_FIELD_CA_CERT_FILE "CACertFile"
42 #define CONNMAN_CONFIG_FIELD_CLIENT_CERT_FILE "ClientCertFile"
43 #define CONNMAN_CONFIG_FIELD_PVT_KEY_FILE "PrivateKeyFile"
44 #define CONNMAN_CONFIG_FIELD_PVT_KEY_PASSPHRASE "PrivateKeyPassphrase"
45 #define CONNMAN_CONFIG_FIELD_KEYMGMT_TYPE "KeymgmtType"
47 static char *__get_encoded_ssid(const char *name)
58 str = g_try_malloc0(len * 2 + 1);
63 for (i = 0; i < len; i++) {
64 g_snprintf(pstr, 3, "%02x", name[i]);
71 static int __config_save(const char *ssid, GKeyFile *keyfile)
74 gchar *config_file = NULL;
79 config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR, ssid);
80 if (config_file == NULL) {
85 data = g_key_file_to_data(keyfile, &length, NULL);
87 file = fopen(config_file, "w");
89 ERR("Failed to open %s", config_file);
95 /* Do POSIX file operation to create and remove config files,
96 * Do not use g_file_set_contents, it breaks inotify operations */
97 if (fputs(data, file) < 0) {
98 ERR("Failed to write %s", config_file);
114 static int __config_delete(const char *ssid)
117 gchar *group_name = NULL;
118 gchar *config_file = NULL;
119 gchar *dirname = NULL;
120 gchar *cert_path = NULL;
121 GKeyFile *keyfile = NULL;
122 GError *error = NULL;
124 config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR, ssid);
125 if (config_file == NULL)
128 keyfile = g_key_file_new();
130 if (g_key_file_load_from_file(keyfile, config_file, 0, &error) != TRUE) {
131 ERR("Unable to load %s[%s]", config_file, error->message);
132 g_clear_error(&error);
138 group_name = g_strdup_printf("service_%s", ssid);
140 cert_path = g_key_file_get_string(keyfile, group_name,
141 CONNMAN_CONFIG_FIELD_CA_CERT_FILE, NULL);
142 DBG("Temporal %s", cert_path);
143 if (cert_path != NULL && remove(cert_path) != 0)
144 ERR("Failed to remove %s", cert_path);
147 cert_path = g_key_file_get_string(keyfile, group_name,
148 CONNMAN_CONFIG_FIELD_CLIENT_CERT_FILE, NULL);
149 DBG("Temporal %s", cert_path);
150 if (cert_path != NULL && remove(cert_path) != 0)
151 ERR("Failed to remove %s", cert_path);
154 cert_path = g_key_file_get_string(keyfile, group_name,
155 CONNMAN_CONFIG_FIELD_PVT_KEY_FILE, NULL);
156 DBG("Temporal %s", cert_path);
157 if (cert_path != NULL && remove(cert_path) != 0)
158 ERR("Failed to remove %s", cert_path);
161 cert_path = g_key_file_get_string(keyfile, group_name,
162 CONNMAN_CONFIG_FIELD_PVT_KEY_PASSPHRASE, NULL);
163 DBG("Temporal %s", cert_path);
164 if (cert_path != NULL && remove(cert_path) != 0)
165 ERR("Failed to remove %s", cert_path);
168 dirname = g_strdup_printf("%s/%s", WIFI_CERT_STORAGEDIR, ssid);
169 if (dirname != NULL) {
170 if (g_file_test(dirname, G_FILE_TEST_EXISTS) == TRUE)
171 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == TRUE)
177 if (remove(config_file) != 0) {
183 g_key_file_free(keyfile);
190 static gboolean __netconfig_copy_config(const char *src, const char *dst)
194 GError *error = NULL;
197 result = g_file_get_contents(src, &buf, &length, &error);
198 if (result != TRUE) {
199 ERR("Failed to read %s[%s]", error->message);
205 result = g_file_set_contents(dst, buf, length, &error);
206 if (result != TRUE) {
207 ERR("Failed to write %s[%s]", error->message);
211 INFO("Successfully installed[%d]", length);
214 if (remove(src) != 0)
215 WARN("Failed to remove %s", src);
220 static gboolean __netconfig_create_config(GVariant *fields)
222 GKeyFile *keyfile = NULL;
224 gchar *encoded_ssid = NULL;
225 gchar *dirname = NULL;
226 gchar *group_name = NULL;
227 gchar *field, *value;
228 gboolean updated = FALSE;
229 gchar *cert_file = NULL;
230 gchar *cert_path = NULL;
233 g_variant_get(fields, "a{ss}", &iter);
234 while (g_variant_iter_loop(iter, "{ss}", &field, &value)) {
236 if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_NAME) == 0) {
237 encoded_ssid = __get_encoded_ssid(value);
242 } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_SSID) == 0) {
243 encoded_ssid = g_strdup(value);
252 if (encoded_ssid == NULL) {
253 ERR("Failed to fetch SSID");
257 /* Create unique service group name */
258 group_name = g_strdup_printf("service_%s", encoded_ssid);
259 if (group_name == NULL) {
260 ERR("Failed to create service group name");
264 keyfile = g_key_file_new();
265 if (keyfile == NULL) {
266 ERR("Failed to g_key_file_new");
270 g_variant_iter_free(iter);
272 g_variant_get(fields, "a{ss}", &iter);
273 while (g_variant_iter_loop(iter, "{ss}", &field, &value)) {
274 if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_SSID) == 0 ||
275 g_strcmp0(field, CONNMAN_CONFIG_FIELD_EAP_METHOD) == 0 ||
276 g_strcmp0(field, CONNMAN_CONFIG_FIELD_PHASE2) ||
277 g_strcmp0(field, CONNMAN_CONFIG_FIELD_KEYMGMT_TYPE) == 0) {
278 DBG("field: %s, value: %s", field, value);
281 g_key_file_set_string(keyfile, group_name, field, value);
282 } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_CA_CERT_FILE) == 0 ||
283 g_strcmp0(field, CONNMAN_CONFIG_FIELD_CLIENT_CERT_FILE) == 0 ||
284 g_strcmp0(field, CONNMAN_CONFIG_FIELD_PVT_KEY_FILE) == 0 ||
285 g_strcmp0(field, CONNMAN_CONFIG_FIELD_PVT_KEY_PASSPHRASE) == 0) {
287 cert_file = strrchr(value, '/');
288 if (cert_file == NULL) {
289 ERR("Failed to get cert file: %s", value);
294 DBG("field: %s, value: %s", field, cert_file);
296 dirname = g_strdup_printf("%s/%s",
297 WIFI_CERT_STORAGEDIR, encoded_ssid);
298 if (dirname == NULL) {
299 ERR("Failed to create dirname");
302 if (g_file_test(dirname, G_FILE_TEST_IS_DIR) != TRUE) {
303 if (mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
304 S_IXGRP | S_IROTH | S_IXOTH) < 0) {
305 if (errno != EEXIST) {
313 cert_path = g_strdup_printf("%s/%s/%s",
314 WIFI_CERT_STORAGEDIR, encoded_ssid, cert_file);
315 if (cert_path == NULL) {
316 ERR("Failed to create cert path");
319 if (__netconfig_copy_config(value, cert_path) != TRUE) {
320 ERR("Failed to read cert file %s", value);
325 g_key_file_set_string(keyfile, group_name, field, cert_path);
329 DBG("field: %s, value: %s", field, value);
332 g_key_file_set_string(keyfile, group_name, field, value);
336 err = __config_save((const char *)encoded_ssid, keyfile);
338 ERR("Failed to create configuration %s[%d]", encoded_ssid, err);
340 DBG("Successfully created %s", encoded_ssid);
346 g_key_file_free(keyfile);
348 g_variant_iter_free(iter);
357 g_free(encoded_ssid);
362 static gboolean _delete_configuration(const gchar *profile)
364 gboolean ret = FALSE;
365 gchar *config_id = NULL;
367 ret = wifi_config_get_config_id(profile, &config_id);
369 ERR("Fail to get config_id from [%s]", profile);
372 ERR("get config_id [%s] from [%s]", config_id, profile);
374 ret = wifi_config_remove_configuration(config_id);
376 ERR("Fail to wifi_config_remove_configuration [%s]", config_id);
379 if (config_id != NULL) {
386 static gboolean __netconfig_delete_config(const char *profile)
388 char *wifi_ident = NULL;
395 if (NULL == profile) {
396 ERR("Invalid profile name");
400 if (_delete_configuration(profile) != TRUE) {
401 ERR("Fail to delete configuration [%s]", profile);
404 wifi_ident = strstr(profile, "wifi_");
405 if (wifi_ident == NULL) {
406 ERR("Invalid profile name");
410 essid = strchr(wifi_ident + 5, '_');
412 ERR("Invalid profile name");
417 mode = strchr(essid, '_');
419 ssid_len = mode - essid;
421 ssid = g_try_malloc0(ssid_len + 1);
423 ERR("Memory allocation failed");
427 g_strlcpy(ssid, essid, ssid_len + 1); /* include NULL-terminated */
428 err = __config_delete((const char *)ssid);
430 ERR("Failed to delete configuration %s[%d]", ssid, err);
435 DBG("Successfully deleted %s with length %d", ssid, ssid_len);
441 static void __netconfig_eap_state(
442 wifi_service_state_e state, void *user_data);
444 static wifi_state_notifier netconfig_eap_notifier = {
445 .wifi_state_changed = __netconfig_eap_state,
449 static void __netconfig_eap_state(
450 wifi_service_state_e state, void *user_data)
452 const char *wifi_profile = (const char *)user_data;
454 if (wifi_profile == NULL) {
455 wifi_state_notifier_unregister(&netconfig_eap_notifier);
459 if (state != NETCONFIG_WIFI_CONNECTED && state != NETCONFIG_WIFI_FAILURE)
462 if (state == NETCONFIG_WIFI_FAILURE)
463 __netconfig_delete_config(wifi_profile);
465 g_free(netconfig_eap_notifier.user_data);
466 netconfig_eap_notifier.user_data = NULL;
468 wifi_state_notifier_unregister(&netconfig_eap_notifier);
471 gboolean handle_create_eap_config(Wifi *wifi, GDBusMethodInvocation *context,
472 const gchar *service, GVariant *fields)
474 gboolean updated = FALSE;
475 gboolean reply = FALSE;
476 gboolean result = FALSE;
478 g_return_val_if_fail(wifi != NULL, FALSE);
480 DBG("Set agent fields for %s", service);
482 if (netconfig_is_wifi_profile(service) != TRUE) {
483 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_WRONG_PROFILE, "InvalidService");
487 updated = __netconfig_create_config(fields);
488 if (updated == TRUE) {
489 wifi_complete_create_eap_config(wifi, context);
491 if (g_strstr_len(service, strlen(service), "_hidden_") != NULL) {
494 const char *name = NULL;
495 const char *identity = NULL;
496 const char *passphrase = NULL;
498 g_variant_get(fields, "a{ss}", &iter);
500 while (g_variant_iter_loop(iter, "{ss}", &field, &value)) {
501 if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_NAME) == 0)
502 name = (const char *)value;
503 else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_SSID) == 0)
504 name = (const char *)value;
505 else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_IDENTITY) == 0)
506 identity = (const char *)value;
507 else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_PASSPHRASE) == 0)
508 passphrase = (const char *)value;
511 netconfig_wifi_set_agent_field_for_eap_network(
512 name, identity, passphrase);
514 g_variant_iter_free(iter);
517 result = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
518 service, CONNMAN_SERVICE_INTERFACE, "Connect", NULL, NULL);
520 if (netconfig_eap_notifier.user_data != NULL) {
521 g_free(netconfig_eap_notifier.user_data);
522 netconfig_eap_notifier.user_data = NULL;
524 wifi_state_notifier_unregister(&netconfig_eap_notifier);
527 netconfig_eap_notifier.user_data = g_strdup(service);
528 wifi_state_notifier_register(&netconfig_eap_notifier);
530 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INVALID_PARAMETER, "InvalidArguments");
534 ERR("Fail to connect %s", service);
541 gboolean handle_delete_eap_config(Wifi *wifi, GDBusMethodInvocation *context,
542 const gchar *profile)
544 g_return_val_if_fail(wifi != NULL, FALSE);
546 wifi_complete_delete_eap_config(wifi, context);
548 gboolean ret = __netconfig_delete_config((const char *)profile);