Fix unnecessary background scan attempts
[platform/core/connectivity/net-config.git] / src / wifi-config.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26 #include <glib.h>
27 #include <unistd.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <sys/time.h>
32
33 #include <vconf.h>
34
35 #include "log.h"
36 #include "util.h"
37 #include "neterror.h"
38 #include "wifi-config.h"
39 #include "wifi-state.h"
40 #include "netsupplicant.h"
41 #include "wifi-key-encryption.h"
42
43 #define CONNMAN_STORAGE         "/var/lib/connman"
44
45 #define WIFI_SECURITY_NONE              "none"
46 #define WIFI_SECURITY_WEP               "wep"
47 #define WIFI_SECURITY_WPA_PSK   "psk"
48 #define WIFI_SECURITY_EAP               "ieee8021x"
49 #define WIFI_SECURITY_SAE               "sae"
50
51 #define WIFI_CONFIG_PREFIX      "wifi_"
52 #define MAC_ADDRESS_LENGTH              12
53 #define WIFI_PREFIX_LENGTH              MAC_ADDRESS_LENGTH + 6  /* wifi_485a3f2f506a_ */
54 #define PROFILE_PREFIX_LENGTH   WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
55
56 #define NET_DNS_ADDR_MAX                2
57
58 #define MAX_WIFI_PROFILES               200
59
60 struct wifi_eap_config {
61         gchar *anonymous_identity;
62         gchar *ca_cert;
63         gchar *client_cert;
64         gchar *private_key;
65         gchar *private_key_password;
66         gchar *identity;
67         gchar *eap_type;
68         gchar *eap_auth_type;
69         gchar *subject_match;
70 };
71
72 typedef struct {
73         gchar *ip_address;
74         gchar *subnet_mask;
75         gchar *gateway_address;
76         gchar *dns_address[NET_DNS_ADDR_MAX];
77         int prefix_length;
78         int dns_count;
79         gchar *ip_type;
80         gchar *dns_type;
81 } wifi_ip_info_s;
82
83 struct wifi_config {
84         gchar *name;
85         gchar *ssid;
86         gchar *passphrase;
87         gchar *security_type;
88         gboolean favorite;
89         gboolean autoconnect;
90         gchar *is_hidden;
91         gboolean is_created;
92         gchar *proxy_address;
93         struct wifi_eap_config *eap_config;
94         wifi_ip_info_s *ip_info;
95         guint frequency;
96         gchar *last_error;
97 };
98
99 static void __free_wifi_configuration(struct wifi_config *conf)
100 {
101         if (conf == NULL)
102                 return;
103
104         g_free(conf->name);
105         g_free(conf->ssid);
106         g_free(conf->passphrase);
107         g_free(conf->security_type);
108         g_free(conf->is_hidden);
109         g_free(conf->proxy_address);
110         g_free(conf->last_error);
111         if (conf->eap_config) {
112                 g_free(conf->eap_config->anonymous_identity);
113                 g_free(conf->eap_config->ca_cert);
114                 g_free(conf->eap_config->client_cert);
115                 g_free(conf->eap_config->private_key);
116                 g_free(conf->eap_config->private_key_password);
117                 g_free(conf->eap_config->identity);
118                 g_free(conf->eap_config->eap_type);
119                 g_free(conf->eap_config->eap_auth_type);
120                 g_free(conf->eap_config->subject_match);
121                 g_free(conf->eap_config);
122         }
123
124         if (conf->ip_info) {
125                 g_free(conf->ip_info->ip_type);
126                 g_free(conf->ip_info->ip_address);
127                 g_free(conf->ip_info->subnet_mask);
128                 g_free(conf->ip_info->gateway_address);
129                 g_free(conf->ip_info->dns_type);
130                 conf->ip_info->prefix_length = 0;
131
132                 int i = 0, count = conf->ip_info->dns_count;
133                 while (i < count) {
134                         g_free(conf->ip_info->dns_address[i]);
135                         i++;
136                 }
137                 g_free(conf->ip_info);
138         }
139         g_free(conf);
140 }
141
142 static gboolean __get_mac_address(const gchar *interface_name, gchar **mac_address)
143 {
144         FILE *fp = NULL;
145         char buf[WIFI_MAC_ADDR_LENGTH + 1];
146         char path[WIFI_MAC_PATH_LENGTH];
147         gchar *tmp_mac = NULL;
148         gchar *tmp = NULL;
149         gchar mac[13] = { 0, };
150         gint i = 0, j = 0;
151
152         snprintf(path, WIFI_MAC_PATH_LENGTH, WIFI_MAC_ADDR_PATH, interface_name);
153
154         if (0 == access(path, F_OK))
155                 fp = fopen(path, "r");
156
157         if (fp) {
158                 if (fgets(buf, sizeof(buf), fp) == NULL) {
159                         ERR("Failed to get MAC info from %s\n", path);
160                         *mac_address = NULL;
161                         fclose(fp);
162                         return FALSE;
163                 }
164                 tmp_mac = (gchar *)malloc(WIFI_MAC_ADDR_LENGTH + 1);
165                 if (tmp_mac == NULL) {
166                         ERR("malloc() failed");
167                         *mac_address = NULL;
168                         fclose(fp);
169                         return FALSE;
170                 }
171                 memset(tmp_mac, 0, WIFI_MAC_ADDR_LENGTH + 1);
172                 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADDR_LENGTH + 1);
173                 fclose(fp);
174         } else {
175                 ERR("Failed to open file %s\n", path);
176
177                 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
178                 if (tmp_mac == NULL) {
179                         ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
180                         *mac_address = NULL;
181                         return FALSE;
182                 }
183         }
184
185         tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
186         free(tmp_mac);
187         while (tmp && tmp[i]) {
188                 if (tmp[i] != ':')
189                         mac[j++] = tmp[i];
190                 i++;
191         }
192         mac[12] = '\0';
193         *mac_address = g_strdup(mac);
194         g_free(tmp);
195
196         return TRUE;
197 }
198
199 gboolean wifi_config_get_group_name(const gchar *prefix,
200                 const gchar *interface_name, const gchar *config_id, gchar **group_name)
201 {
202         gchar *mac_address = NULL;
203         gchar *g_name = NULL;
204         gboolean ret = FALSE;
205
206         ret = __get_mac_address(interface_name, &mac_address);
207         if ((ret != TRUE) || (strlen(mac_address) == 0)) {
208                 ERR("Cannot get WIFI MAC address");
209                 g_free(mac_address);
210                 return FALSE;
211         }
212
213         g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
214         if (g_name == NULL) {
215                 g_free(mac_address);
216                 return FALSE;
217         }
218
219         *group_name = g_strdup(g_name);
220
221         g_free(mac_address);
222         g_free(g_name);
223
224         return TRUE;
225 }
226
227 static gboolean __get_security_type(const gchar *config_id, gchar **type)
228 {
229         if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
230                 *type = g_strdup(WIFI_SECURITY_NONE);
231         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
232                 *type = g_strdup(WIFI_SECURITY_WEP);
233         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
234                 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
235         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
236                 *type = g_strdup(WIFI_SECURITY_EAP);
237         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_SAE) == TRUE) {
238                 *type = g_strdup(WIFI_SECURITY_SAE);
239         } else {
240                 *type = NULL;
241                 return FALSE;
242         }
243
244         return TRUE;
245 }
246
247 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
248 {
249         *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
250         if (*config_id == NULL) {
251                 ERR("OOM");
252                 return FALSE;
253         }
254
255         return TRUE;
256 }
257
258
259 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
260 {
261         GKeyFile *keyfile = NULL;
262         gchar *path;
263
264         path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
265
266         keyfile = netconfig_keyfile_load(path);
267         if (keyfile == NULL)
268                 ERR("keyfile[%s] is NULL", path);
269
270         g_free(path);
271
272         return keyfile;
273 }
274
275 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
276 {
277         gboolean ret = FALSE;
278         gchar *path;
279
280         path = g_strdup_printf("%s/%s", pathname, filename);
281         if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
282                 ret = TRUE;
283         } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
284                 unlink(path);
285                 ret = TRUE;
286         }
287
288         g_free(path);
289         return ret;
290 }
291
292 static gboolean __remove_all_files(const gchar *pathname)
293 {
294         DIR *dir_ptr = NULL;
295         struct dirent *file = NULL;
296
297         if ((dir_ptr = opendir(pathname)) == NULL)
298                 return TRUE;
299
300         while ((file = readdir(dir_ptr)) != NULL) {
301                 if (strncmp(file->d_name, ".", 1) == 0 || strncmp(file->d_name, "..", 2) == 0)
302                         continue;
303
304                 if (__remove_file(pathname, file->d_name) != TRUE) {
305                         ERR("Cannot remove [%s/%s]", pathname, file->d_name);
306                         closedir(dir_ptr);
307
308                         return FALSE;
309                 }
310         }
311
312         closedir(dir_ptr);
313
314         return TRUE;
315 }
316
317 static gboolean __remove_configuration(const gchar *pathname)
318 {
319         int ret = 0;
320
321         if (__remove_all_files(pathname) != TRUE) {
322                 ERR("Cannot remove [%s] directory", pathname);
323                 return FALSE;
324         }
325
326         ret = rmdir(pathname);
327         if (ret == -1) {
328                 ERR("Cannot remove [%s]", pathname);
329                 return FALSE;
330         }
331         return TRUE;
332 }
333
334 static gboolean _load_configuration(const gchar *interface_name,
335                 const gchar *config_id, struct wifi_config *config)
336 {
337         GKeyFile *keyfile;
338         gchar *group_name;
339         gboolean hidden = FALSE;
340         gboolean ret = FALSE;
341
342         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
343                         interface_name, config_id, &group_name);
344         if (ret != TRUE) {
345                 ERR("Fail to get_wifi_config_group_name");
346                 return FALSE;
347         }
348
349         keyfile = __get_configuration_keyfile(group_name);
350         if (keyfile == NULL) {
351                 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
352                 g_free(group_name);
353                 return FALSE;
354         }
355
356         config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
357         DBG("name [%s]", config->name);
358
359         if (config->name == NULL) {
360                 ERR("Fail to get Name of [%s]", group_name);
361                 g_key_file_free(keyfile);
362                 g_free(group_name);
363                 return FALSE;
364         }
365
366         __get_security_type(config_id, &config->security_type);
367         if (config->security_type == NULL) {
368                 ERR("Fail to _get_security_type");
369                 g_key_file_free(keyfile);
370                 g_free(group_name);
371                 return FALSE;
372         }
373         DBG("security_type [%s]", config->security_type);
374
375         config->passphrase = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
376         DBG("passphrase []");
377
378         config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
379         if (config->proxy_address)
380                 DBG("proxy_address [%s]", config->proxy_address);
381
382         hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
383         if (hidden)
384                 config->is_hidden = g_strdup("TRUE");
385         else
386                 config->is_hidden = g_strdup("FALSE");
387         DBG("is_hidden [%s]", config->is_hidden);
388
389         config->frequency = g_key_file_get_integer(keyfile, group_name, WIFI_CONFIG_FREQUENCY, NULL);
390         if (config->frequency)
391                 DBG("Frequency [%d]", config->frequency);
392
393         if (config->ip_info) {
394                 config->ip_info->ip_type = g_key_file_get_string(keyfile, group_name,
395                                 WIFI_CONFIG_IPV4_METHOD, NULL);
396                 if (config->ip_info->ip_type)
397                         DBG("IPv4.Method:%s", config->ip_info->ip_type);
398
399                 config->ip_info->ip_address = g_key_file_get_string(keyfile, group_name,
400                                 WIFI_CONFIG_IPV4_ADDRESS, NULL);
401                 if (config->ip_info->ip_address)
402                         DBG("IPv4.Address:%s", config->ip_info->ip_address);
403
404                 int prefix_len;
405                 in_addr_t addr;
406                 struct in_addr netmask;
407                 char *mask;
408                 GError *error = NULL;
409                 prefix_len = g_key_file_get_integer(keyfile, group_name,
410                                 WIFI_CONFIG_IPV4_SUBNET_MASK, &error);
411                 if (error != NULL) {
412                         DBG("g_key_file_get_integer failed error[%d: %s]", error->code, error->message);
413                         g_error_free(error);
414                 } else {
415                         if (prefix_len > 0 && prefix_len < 32) {
416                                 addr = 0xffffffff << (32 - prefix_len);
417                                 netmask.s_addr = htonl(addr);
418                                 mask = inet_ntoa(netmask);
419                                 config->ip_info->subnet_mask = g_strdup(mask);
420                         }
421                         if (config->ip_info->subnet_mask)
422                                 DBG("IPv4.SubnetMask:%s", config->ip_info->subnet_mask);
423                 }
424
425                 config->ip_info->gateway_address = g_key_file_get_string(keyfile,
426                                                         group_name, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, NULL);
427                 if (config->ip_info->gateway_address)
428                         DBG("IPv4.gateway:%s", config->ip_info->gateway_address);
429
430                 config->ip_info->dns_type = g_key_file_get_string(keyfile, group_name,
431                                                           WIFI_CONFIG_IPV4_DNS_METHOD, NULL);
432                 if (config->ip_info->dns_type)
433                         DBG("DNS.IPv4Method:%s", config->ip_info->dns_type);
434
435                 char **nameservers;
436                 gsize length;
437                 nameservers = g_key_file_get_string_list(keyfile, group_name,
438                                                                  WIFI_CONFIG_DNS_ADDRESS, &length, NULL);
439                 if (nameservers) {
440                         if (length > 0) {
441                                 config->ip_info->dns_count = length;
442                                 int i = 0;
443                                 while (i < NET_DNS_ADDR_MAX && nameservers[i]) {
444                                         config->ip_info->dns_address[i] = g_strdup(nameservers[i]);
445                                         DBG("DNSAddress[%d]:%s", i+1, config->ip_info->dns_address[i]);
446                                         i += 1;
447                                 }
448                         }
449                         g_strfreev(nameservers);
450                 }
451         }
452
453
454         if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
455                 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
456                 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
457                 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
458                 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
459                 config->eap_config->private_key_password = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, NULL);
460                 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
461                 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
462                 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
463                 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
464
465                 if (config->eap_config->anonymous_identity)
466                         DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
467                 if (config->eap_config->ca_cert)
468                         DBG("ca_cert [%s]", config->eap_config->ca_cert);
469                 if (config->eap_config->client_cert)
470                         DBG("client_cert [%s]", config->eap_config->client_cert);
471                 if (config->eap_config->private_key)
472                         DBG("private_key [%s]", config->eap_config->private_key);
473                 if (config->eap_config->private_key_password)
474                         DBG("private_key_password [%s]", config->eap_config->private_key_password);
475                 if (config->eap_config->identity)
476                         DBG("identity [%s]", config->eap_config->identity);
477                 if (config->eap_config->eap_type)
478                         DBG("eap_type [%s]", config->eap_config->eap_type);
479                 if (config->eap_config->eap_auth_type)
480                         DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
481                 if (config->eap_config->subject_match)
482                         DBG("subject_match [%s]", config->eap_config->subject_match);
483         }
484
485         config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
486         if (config->last_error)
487                 DBG("last_error [%s]", config->last_error);
488
489         g_key_file_free(keyfile);
490         g_free(group_name);
491
492         return TRUE;
493 }
494
495 gboolean wifi_config_save_configuration(const gchar *interface_name,
496                 const gchar *config_id, GKeyFile *keyfile)
497 {
498         gchar *dir;
499         gchar *path;
500         gchar *group_name;
501         gboolean ret = FALSE;
502
503         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
504                         interface_name, config_id, &group_name);
505         if (ret != TRUE) {
506                 ERR("Fail to get_wifi_config_group_name");
507                 return FALSE;
508         }
509
510         dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
511         if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
512                 if (__remove_configuration(dir) != TRUE) {
513                         ERR("[%s] is existed, but cannot remove", dir);
514                         g_free(group_name);
515                         g_free(dir);
516                         return FALSE;
517                 }
518         }
519
520         if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
521                 ERR("Cannot mkdir %s", dir);
522                 g_free(group_name);
523                 g_free(dir);
524                 return FALSE;
525         }
526
527         path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
528         netconfig_keyfile_save(keyfile, path);
529         g_free(group_name);
530         g_free(dir);
531         g_free(path);
532
533         return TRUE;
534 }
535
536 static gboolean _remove_configuration(const gchar *interface_name, const gchar *config_id)
537 {
538         gboolean ret = FALSE;
539         gchar *dir;
540         gchar *group_name;
541
542         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
543                         interface_name, config_id, &group_name);
544         if (ret != TRUE) {
545                 ERR("Fail to get_wifi_config_group_name");
546                 return FALSE;
547         }
548
549         dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
550         if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
551                 if (__remove_configuration(dir) != TRUE) {
552                         ERR("[%s] is existed, but cannot remove", dir);
553                         ret = FALSE;
554                 } else {
555                         INFO("Success to remove [%s]", dir);
556                         ret = TRUE;
557                 }
558         } else {
559                 ERR("[%s] is not existed", dir);
560                 ret = FALSE;
561         }
562
563         g_free(group_name);
564         g_free(dir);
565
566         return ret;
567 }
568
569
570 static gboolean _set_field(const gchar *interface_name,
571                 const gchar *config_id, const gchar *key, const gchar *value)
572 {
573         gboolean ret = TRUE;
574         GKeyFile *keyfile;
575         gchar *group_name;
576
577         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
578                         interface_name, config_id, &group_name);
579         if (ret != TRUE) {
580                 ERR("Fail to get_wifi_config_group_name");
581                 return FALSE;
582         }
583         DBG("group_name %s", group_name);
584
585         keyfile = __get_configuration_keyfile(group_name);
586         if (keyfile == NULL) {
587                 ERR("Fail to __get_configuration_keyfile");
588                 g_free(group_name);
589                 return FALSE;
590         }
591
592         if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
593                 g_key_file_set_string(keyfile, group_name, key, value);
594         } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
595                 g_key_file_set_string(keyfile, group_name, key, value);
596         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
597                 gboolean hidden = FALSE;
598                 if (g_strcmp0(value, "TRUE") == 0)
599                         hidden = TRUE;
600                 g_key_file_set_boolean(keyfile, group_name, key, hidden);
601         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
602                 g_key_file_set_string(keyfile, group_name, key, value);
603         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
604                 g_key_file_set_string(keyfile, group_name, key, value);
605         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
606                 g_key_file_set_string(keyfile, group_name, key, value);
607         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
608                 g_key_file_set_string(keyfile, group_name, key, value);
609         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
610                 g_key_file_set_string(keyfile, group_name, key, value);
611         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
612                 g_key_file_set_string(keyfile, group_name, key, value);
613         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
614                 g_key_file_set_string(keyfile, group_name, key, value);
615         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
616                 g_key_file_set_string(keyfile, group_name, key, value);
617         } else {
618                 ERR("key[%s] is not supported", key);
619                 ret = FALSE;
620         }
621
622         wifi_config_save_configuration(interface_name, config_id, keyfile);
623
624         g_key_file_free(keyfile);
625         g_free(group_name);
626
627         return ret;
628 }
629
630 static gboolean _get_field(const gchar *interface_name,
631                 const gchar *config_id, const gchar *key, gchar **value)
632 {
633         GKeyFile *keyfile;
634         gchar *group_name;
635         gchar *val = NULL;
636         gboolean hidden = FALSE;
637         gboolean ret = FALSE;
638
639         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
640                         interface_name, config_id, &group_name);
641         if (ret != TRUE) {
642                 ERR("Fail to get_wifi_config_group_name");
643                 return FALSE;
644         }
645         DBG("group_name %s", group_name);
646
647         keyfile = __get_configuration_keyfile(group_name);
648         if (keyfile == NULL) {
649                 ERR("Fail to __get_configuration_keyfile");
650                 g_free(group_name);
651                 return FALSE;
652         }
653
654         if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
655                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
656         } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
657                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
658         } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
659                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
660         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
661                 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
662                 if (hidden)
663                         val = g_strdup("TRUE");
664                 else
665                         val = g_strdup("FALSE");
666         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
667                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
668         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
669                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
670         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
671                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
672         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
673                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
674         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
675                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
676         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
677                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
678         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
679                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
680         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
681                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
682         } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
683                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
684         } else {
685                 ERR("Invalid key[%s]", key);
686                 val = g_strdup("NOTSUPPORTED");
687         }
688
689         *value = g_strdup(val);
690         g_free(val);
691
692         g_key_file_free(keyfile);
693         g_free(group_name);
694
695         return TRUE;
696 }
697
698 static GSList *_get_list(const char *mac_addr)
699 {
700         GSList *list = NULL;
701         struct dirent *dp = NULL;
702         DIR *dir;
703
704         dir = opendir(CONNMAN_STORAGE);
705         if (dir == NULL) {
706                 ERR("Cannot open dir %s", CONNMAN_STORAGE);
707                 return NULL;
708         }
709
710         while ((dp = readdir(dir)) != NULL) {
711                 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
712                                 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
713                         continue;
714                 }
715
716                 DBG("%s", dp->d_name);
717
718                 if (netconfig_check_mac_address(dp->d_name, mac_addr)) {
719                         gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
720                         DBG("%s", config_id);
721                         list = g_slist_append(list, g_strdup(config_id));
722                         g_free(config_id);
723                 }
724         }
725         closedir(dir);
726
727         return list;
728 }
729
730 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
731 {
732         gboolean ret = FALSE;
733         gchar *val = NULL;
734
735         if ((service_profile == NULL) || (config_id == NULL)) {
736                 ERR("Invalid parameter");
737                 return FALSE;
738         }
739
740         ret = __get_config_id(service_profile, &val);
741         *config_id = g_strdup(val);
742         g_free(val);
743
744         return ret;
745 }
746
747 gboolean wifi_config_remove_configuration(const gchar *interface_name,
748                 const gchar *config_id)
749 {
750         gboolean ret = FALSE;
751
752         ret = _remove_configuration(interface_name, config_id);
753
754         return ret;
755 }
756
757 int __netconfig_hex_char_to_num(char c)
758 {
759         if (c >= '0' && c <= '9')
760                 return c - '0';
761
762         if (c >= 'a' && c <= 'f')
763                 return c - 'a' + 10;
764
765         if (c >= 'A' && c <= 'F')
766                 return c - 'A' + 10;
767
768         return -1;
769 }
770
771 int __netconfig_hex_to_byte(const char *hex)
772 {
773         int a, b;
774
775         a = __netconfig_hex_char_to_num(*hex++);
776         if (a < 0)
777                 return -1;
778
779         b = __netconfig_hex_char_to_num(*hex++);
780         if (b < 0)
781                 return -1;
782
783         return (a << 4) | b;
784 }
785
786 int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
787 {
788         size_t i;
789         int a;
790         const char *ipos = hex;
791         unsigned char *opos = buf;
792
793         for (i = 0; i < len; i++) {
794                 a = __netconfig_hex_to_byte(ipos);
795                 if (a < 0)
796                         return -1;
797
798                 *opos++ = a;
799                 ipos += 2;
800         }
801
802         return 0;
803 }
804
805 static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
806 {
807         int dst_length = 0;
808         int i = 0;
809         char *buf = NULL;
810
811         if (src_len <= 0) {
812                 ERR("Invalid parameter.");
813                 return -1;
814         }
815
816         *dst = (char *) g_try_malloc0((2*src_len)+1);
817         if (!(*dst)) {
818                 ERR("failed to allocate memory to buffer.");
819                 return -1;
820         }
821
822         buf = (*dst);
823
824         for (i = 0; i < src_len; i++) {
825                 snprintf(buf, 3, "%02x", src[i]);
826                 buf += 2;
827                 dst_length += 2;
828         }
829
830         return dst_length;
831 }
832
833 static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
834 {
835         GVariantIter *iter_copy = NULL;
836         int length = 0;
837         char tmp = 0;
838         unsigned char *tmp_dst = NULL;
839
840         if (!dst || *dst || !iter) {
841                 ERR("Invalid parameter");
842                 return 0;
843         }
844
845         iter_copy = g_variant_iter_copy(iter);
846
847         while (g_variant_iter_loop(iter, "y", &tmp))
848                 length++;
849         g_variant_iter_free(iter);
850
851         tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
852         if (!tmp_dst) {
853                 ERR("failed to allocate memory");
854                 g_variant_iter_free(iter_copy);
855                 return 0;
856         }
857
858         length = 0;
859         while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
860                 length++;
861         g_variant_iter_free(iter_copy);
862
863         if (length == 0) {
864                 g_free(tmp_dst);
865                 tmp_dst = NULL;
866         } else {
867                 tmp_dst[length] = '\0';
868         }
869
870         *dst = tmp_dst;
871         DBG("Length [%d]", length);
872         return length;
873 }
874
875 gboolean _add_vsie(const char *interface_name, int frame_id, const char* vsie)
876 {
877         GVariant *params = NULL;
878         GVariant *message = NULL;
879         GVariantBuilder *bytearray_builder = NULL;
880         char *if_path;
881         int i = 0;
882         size_t vsie_len = 0;
883
884         unsigned char *bytearray = NULL;
885         size_t bytearray_len = 0;
886
887         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
888                 DBG("Invalid parameter, frame-id: %d", frame_id);
889                 return FALSE;
890         }
891
892         vsie_len = strlen(vsie);
893         if (vsie_len == 0) {
894                 DBG("vsie length is zero");
895                 return FALSE;
896         }
897
898         bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
899
900         bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
901         if (bytearray == NULL) {
902                 DBG("Failed to allocate memory to bytearray");
903                 return FALSE;
904         }
905
906         if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
907                 DBG("invalid vsie string");
908                 g_free(bytearray);
909                 return FALSE;
910         }
911
912         bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
913         for (i = 0; i < bytearray_len; i++)
914                 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
915
916         params = g_variant_new("(iay)", frame_id, bytearray_builder);
917         g_variant_builder_unref(bytearray_builder);
918
919         if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
920         if (if_path == NULL) {
921                 ERR("Fail to get wpa_supplicant DBus path");
922                 g_free(bytearray);
923                 return FALSE;
924         }
925
926         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
927                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
928
929         g_free(if_path);
930         if (message == NULL) {
931                 ERR("Failed to send command to wpa_supplicant");
932                 g_free(bytearray);
933                 return FALSE;
934         }
935
936         DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
937
938         g_free(bytearray);
939         return TRUE;
940 }
941
942 gboolean _get_vsie(const char *interface_name, int frame_id, char **vsie)
943 {
944         GVariant *params = NULL;
945         GVariant *message = NULL;
946         char *if_path;
947
948         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
949                 DBG("Invalid parameter, frame-id: %d", frame_id);
950                 return FALSE;
951         }
952
953         if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
954         if (if_path == NULL) {
955                 ERR("Fail to get wpa_supplicant DBus path");
956                 return FALSE;
957         }
958
959         params = g_variant_new("(i)", frame_id);
960
961         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
962                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
963
964         g_free(if_path);
965         if (message == NULL) {
966                 ERR("Failed to send command to wpa_supplicant");
967                 return FALSE;
968         } else {
969                 GVariantIter *iter = NULL;
970                 unsigned char *vsie_bytes = NULL;
971                 int vsie_len = 0;
972                 int ret = 0;
973
974                 g_variant_get(message, "(ay)", &iter);
975                 if (iter == NULL) {
976                         ERR("vsie is not present");
977                         return FALSE;
978                 }
979
980                 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
981                 if (vsie_bytes == NULL) {
982                         ERR("vsie_bytes not allocated");
983                         return FALSE;
984                 }
985
986                 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
987                 if (ret < 0) {
988                         g_free(vsie_bytes);
989                         ERR("vsie not allocated.");
990                         return FALSE;
991                 }
992
993                 g_free(vsie_bytes);
994         }
995
996         ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
997
998         return TRUE;
999
1000 }
1001
1002 gboolean _remove_vsie(const char *interface_name, int frame_id, const char *vsie)
1003 {
1004         GVariant *params = NULL;
1005         GVariant *message = NULL;
1006         GVariantBuilder *bytearray_builder = NULL;
1007         char *if_path;
1008         int i = 0;
1009         size_t vsie_len = 0;
1010
1011         unsigned char *bytearray = NULL;
1012         size_t bytearray_len = 0;
1013
1014         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
1015                 DBG("Invalid parameter, frame-id: %d", frame_id);
1016                 return FALSE;
1017         }
1018
1019         vsie_len = strlen(vsie);
1020         if (vsie_len == 0) {
1021                 DBG("vsie length is zero");
1022                 return FALSE;
1023         }
1024
1025         bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
1026
1027         bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
1028         if (bytearray == NULL) {
1029                 DBG("Failed to allocate memory to bytearray");
1030                 return FALSE;
1031         }
1032
1033         if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
1034                 DBG("invalid vsie string");
1035                 g_free(bytearray);
1036                 return FALSE;
1037         }
1038
1039         bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1040         for (i = 0; i < bytearray_len; i++)
1041                 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
1042
1043         params = g_variant_new("(iay)", frame_id, bytearray_builder);
1044         g_variant_builder_unref(bytearray_builder);
1045
1046         if_path = netconfig_wifi_get_supplicant_interface_path(interface_name);
1047         if (if_path == NULL) {
1048                 ERR("Fail to get wpa_supplicant DBus path");
1049                 g_free(bytearray);
1050                 return FALSE;
1051         }
1052
1053         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
1054                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
1055
1056         g_free(if_path);
1057         if (message == NULL) {
1058                 ERR("Failed to send command to wpa_supplicant");
1059                 g_free(bytearray);
1060                 return FALSE;
1061         }
1062
1063         DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
1064
1065         g_free(bytearray);
1066         return TRUE;
1067 }
1068
1069 /* dbus method */
1070 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context,
1071                 const gchar *ifname)
1072 {
1073         guint i = 0;
1074         GSList *config_ids = NULL;
1075         guint length;
1076         gchar **result = NULL;
1077         const gchar *mac_addr = NULL;
1078
1079         g_return_val_if_fail(wifi != NULL, TRUE);
1080
1081         mac_addr = wifi_state_get_mac_address(ifname);
1082         if (!mac_addr) {
1083                 ERR("Fail to get mac-address");
1084                 netconfig_error_no_profile(context);
1085                 return TRUE;
1086         }
1087
1088         DBG("%s", mac_addr);
1089         config_ids = _get_list(mac_addr);
1090         if (config_ids == NULL) {
1091                 ERR("Fail to get config list");
1092                 netconfig_error_no_profile(context);
1093                 return TRUE;
1094         }
1095
1096         length = g_slist_length(config_ids);
1097         result = g_new0(gchar *, length + 1);
1098         for (i = 0; i < length; i++) {
1099                 gchar *config_id = g_slist_nth_data(config_ids, i);
1100                 result[i] = g_strdup(config_id);
1101         }
1102
1103         config_ids = g_slist_nth(config_ids, 0);
1104         g_slist_free_full(config_ids, g_free);
1105
1106         wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
1107
1108         for (i = 0; i < length; i++)
1109                 if (result[i])
1110                         g_free(result[i]);
1111
1112         if (result)
1113                 g_free(result);
1114
1115         return TRUE;
1116 }
1117
1118 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1119                 const gchar *ifname, const gchar *config_id)
1120 {
1121         gboolean ret = FALSE;
1122         GVariantBuilder *b = NULL;
1123         struct wifi_config *conf = NULL;
1124
1125         g_return_val_if_fail(wifi != NULL, TRUE);
1126
1127         conf = g_new0(struct wifi_config, 1);
1128         conf->ip_info = g_new0(wifi_ip_info_s, 1);
1129
1130         ret = _load_configuration(ifname, config_id, conf);
1131         if (ret != TRUE) {
1132                 g_free(conf->ip_info);
1133                 g_free(conf);
1134                 ERR("Fail to _load_configuration");
1135                 netconfig_error_no_profile(context);
1136                 return TRUE;
1137         }
1138
1139         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1140         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1141         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1142         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PASSPHRASE, g_variant_new_string(conf->passphrase));
1143         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1144         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_uint32(conf->frequency));
1145
1146         if (conf->proxy_address != NULL)
1147                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1148         else
1149                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1150
1151         if (conf->ip_info->ip_type != NULL)
1152                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_METHOD, g_variant_new_string(conf->ip_info->ip_type));
1153
1154         if (conf->ip_info->ip_address != NULL)
1155                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_ADDRESS, g_variant_new_string(conf->ip_info->ip_address));
1156
1157         if (conf->ip_info->subnet_mask != NULL)
1158                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_SUBNET_MASK, g_variant_new_string(conf->ip_info->subnet_mask));
1159
1160         if (conf->ip_info->prefix_length > 0)
1161                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV6_PREFIX_LEN, g_variant_new_int32(conf->ip_info->prefix_length));
1162
1163         if (conf->ip_info->gateway_address != NULL)
1164                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, g_variant_new_string(conf->ip_info->gateway_address));
1165
1166         if (conf->ip_info->dns_type != NULL)
1167                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_IPV4_DNS_METHOD, g_variant_new_string(conf->ip_info->dns_type));
1168
1169         int i = 0, count = conf->ip_info->dns_count;
1170         while (i < count) {
1171                 if (conf->ip_info->dns_address[i] != NULL)
1172                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_DNS_ADDRESS, g_variant_new_string(conf->ip_info->dns_address[i]));
1173
1174                 i += 1;
1175         }
1176
1177         if (conf->last_error != NULL)
1178                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1179         else
1180                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1181
1182         __free_wifi_configuration(conf);
1183
1184         INFO("Success to load configuration [%s:%s]", ifname, config_id);
1185
1186         wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
1187         g_variant_builder_unref(b);
1188         return TRUE;
1189 }
1190
1191 static unsigned char __netconfig_convert_netmask_to_prefixlen(
1192                                                           const char *netmask)
1193 {
1194         unsigned char bits;
1195         in_addr_t mask;
1196         in_addr_t host;
1197
1198         if (!netmask)
1199                 return 32;
1200
1201         mask = inet_network(netmask);
1202         host = ~mask;
1203
1204         /* a valid netmask must be 2^n - 1 */
1205         if ((host & (host + 1)) != 0)
1206                 return -1;
1207
1208         bits = 0;
1209         for (; mask; mask <<= 1)
1210                 ++bits;
1211
1212         return bits;
1213 }
1214
1215 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1216                 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1217 {
1218         gboolean ret = FALSE;
1219         struct wifi_config *conf = NULL;
1220         GKeyFile *keyfile = NULL;
1221         GVariantIter *iter;
1222         GVariant *value;
1223         gchar *field;
1224         gchar *group_name = NULL;
1225         int order = 0;
1226         int rv;
1227         struct timeval modified_time;
1228
1229         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1230                 ERR("Invalid parameter");
1231                 netconfig_error_invalid_parameter(context);
1232                 return TRUE;
1233         }
1234
1235         conf = g_new0(struct wifi_config, 1);
1236         conf->ip_info = g_new0(wifi_ip_info_s, 1);
1237
1238         g_variant_get(configuration, "a{sv}", &iter);
1239         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1240                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1241                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1242                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
1243                                 DBG("name [%s]", conf->name);
1244                         } else {
1245                                 conf->name = NULL;
1246                         }
1247                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1248                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1249                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1250                                 DBG("ssid [%s]", conf->ssid);
1251                         } else {
1252                                 conf->ssid = NULL;
1253                         }
1254                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1255                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1256                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1257                                 DBG("passphrase []");
1258                         } else {
1259                                 conf->passphrase = NULL;
1260                         }
1261                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1262                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1263                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1264                                 DBG("is_hidden [%s]", conf->is_hidden);
1265                         } else {
1266                                 conf->is_hidden = NULL;
1267                         }
1268                 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1269                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
1270                                 conf->frequency = g_variant_get_uint32(value);
1271                                 DBG("frequency [%d]", conf->frequency);
1272                         } else {
1273                                 conf->frequency = 0;
1274                         }
1275                 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1276                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1277                                 conf->is_created = g_variant_get_boolean(value);
1278                                 DBG("is_created [%d]", conf->is_created);
1279                         } else {
1280                                 conf->is_created = FALSE;
1281                         }
1282                 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_METHOD) == 0) {
1283                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1284                                 conf->ip_info->ip_type = g_strdup(g_variant_get_string(value, NULL));
1285                                 DBG("IP config type [%s]", conf->ip_info->ip_type);
1286                         } else {
1287                                 conf->ip_info->ip_type = NULL;
1288                         }
1289                 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_ADDRESS) == 0) {
1290                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1291                                 conf->ip_info->ip_address = g_strdup(g_variant_get_string(value, NULL));
1292                                 DBG("IP address [%s]", conf->ip_info->ip_address);
1293                         } else {
1294                                 conf->ip_info->ip_address = NULL;
1295                         }
1296                 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_SUBNET_MASK) == 0) {
1297                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1298                                 conf->ip_info->subnet_mask = g_strdup(g_variant_get_string(value, NULL));
1299                                 DBG("Subnet Mask [%s]", conf->ip_info->subnet_mask);
1300                         } else {
1301                                 conf->ip_info->subnet_mask = NULL;
1302                         }
1303                 } else if (g_strcmp0(field, WIFI_CONFIG_IPV6_PREFIX_LEN) == 0) {
1304                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
1305                                 conf->ip_info->prefix_length = g_variant_get_int32(value);
1306                                 DBG("IPv6 Prefix Length [%d]", conf->ip_info->prefix_length);
1307                         } else {
1308                                 conf->ip_info->prefix_length = 0;
1309                         }
1310                 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_GATEWAY_ADDRESS) == 0) {
1311                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1312                                 conf->ip_info->gateway_address = g_strdup(g_variant_get_string(value, NULL));
1313                                 DBG("Gateway address [%s]", conf->ip_info->gateway_address);
1314                         } else {
1315                                 conf->ip_info->gateway_address = NULL;
1316                         }
1317                 } else if (g_strcmp0(field, WIFI_CONFIG_IPV4_DNS_METHOD) == 0) {
1318                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1319                                 conf->ip_info->dns_type = g_strdup(g_variant_get_string(value, NULL));
1320                                 DBG("DNS config type [%s]", conf->ip_info->dns_type);
1321                         } else {
1322                                 conf->ip_info->dns_type = NULL;
1323                         }
1324                 } else if (g_strcmp0(field, WIFI_CONFIG_DNS_ADDRESS) == 0) {
1325                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1326                                 conf->ip_info->dns_address[order] = g_strdup(g_variant_get_string(value, NULL));
1327                                 DBG("DNS address [%s]", conf->ip_info->dns_address[order]);
1328                                 conf->ip_info->dns_count = order + 1;
1329                                 order++;
1330                         } else {
1331                                 conf->ip_info->dns_address[order++] = NULL;
1332                         }
1333                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1334                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1335                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1336                                 DBG("proxy_address [%s]", conf->proxy_address);
1337                         } else {
1338                                 conf->proxy_address = NULL;
1339                         }
1340                 }
1341         }
1342         conf->favorite = TRUE;
1343         conf->autoconnect = TRUE;
1344
1345         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1346                         ifname, config_id, &group_name);
1347         if (ret != TRUE) {
1348                 __free_wifi_configuration(conf);
1349                 ERR("Fail to get_wifi_config_group_name");
1350                 netconfig_error_fail_save_congifuration(context);
1351                 return TRUE;
1352         }
1353
1354         keyfile = g_key_file_new();
1355         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1356         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1357
1358         if (conf->passphrase != NULL) {
1359                 gchar *enc_data = NULL;
1360
1361                 if (conf->is_created == true)
1362                         enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1363                 else
1364                         enc_data = g_strdup(conf->passphrase);
1365
1366                 if (!enc_data) {
1367                         ERR("Failed to encrypt the passphrase");
1368                 } else {
1369                         g_free(conf->passphrase);
1370                         conf->passphrase = enc_data;
1371                 }
1372
1373                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1374         }
1375
1376         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1377         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1378
1379         rv = gettimeofday(&modified_time, NULL);
1380         if (!rv) {
1381                 struct tm modified_tm;
1382                 char time_buf[255];
1383                 time_t modified_t = modified_time.tv_sec;
1384
1385                 if (localtime_r(&modified_t, &modified_tm)) {
1386                         if (strftime(time_buf, sizeof(time_buf), "%FT%TZ", &modified_tm)) {
1387                                 field = g_strdup(time_buf);
1388                                 if (field) {
1389                                         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_MODIFIED, field);
1390                                         g_free(field);
1391                                 }
1392                         }
1393                 }
1394         }
1395
1396         /* Optional field */
1397         if (conf->proxy_address != NULL) {
1398                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1399                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1400         }
1401
1402         if (conf->is_hidden != NULL) {
1403                 gboolean hidden = FALSE;
1404                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1405                         hidden = TRUE;
1406                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1407         }
1408
1409         if (conf->frequency > 0)
1410                 g_key_file_set_integer(keyfile, group_name,
1411                         WIFI_CONFIG_FREQUENCY, conf->frequency);
1412
1413         if (conf->ip_info->ip_type != NULL)
1414                 g_key_file_set_string(keyfile, group_name,
1415                         WIFI_CONFIG_IPV4_METHOD, conf->ip_info->ip_type);
1416
1417         if (conf->ip_info->ip_address != NULL)
1418                 g_key_file_set_string(keyfile, group_name,
1419                         WIFI_CONFIG_IPV4_ADDRESS, conf->ip_info->ip_address);
1420
1421         if (conf->ip_info->subnet_mask != NULL) {
1422                 unsigned char prefix_len;
1423                 prefix_len = __netconfig_convert_netmask_to_prefixlen(
1424                                 conf->ip_info->subnet_mask);
1425                 if (prefix_len > 0 && prefix_len < 32)
1426                         g_key_file_set_integer(keyfile, group_name,
1427                                         WIFI_CONFIG_IPV4_SUBNET_MASK, prefix_len);
1428         }
1429
1430         if (conf->ip_info->prefix_length > 0)
1431                 g_key_file_set_integer(keyfile, group_name,
1432                                 WIFI_CONFIG_IPV6_PREFIX_LEN, conf->ip_info->prefix_length);
1433
1434         if (conf->ip_info->gateway_address != NULL)
1435                 g_key_file_set_string(keyfile, group_name,
1436                         WIFI_CONFIG_IPV4_GATEWAY_ADDRESS, conf->ip_info->gateway_address);
1437
1438         if (conf->ip_info->dns_type != NULL)
1439                 g_key_file_set_string(keyfile, group_name,
1440                         WIFI_CONFIG_IPV4_DNS_METHOD, conf->ip_info->dns_type);
1441
1442         int i = 0, count = conf->ip_info->dns_count;
1443         while (i < count) {
1444                 if (conf->ip_info->dns_address[i] != NULL)
1445                         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_DNS_ADDRESS,
1446                                                                   conf->ip_info->dns_address[i]);
1447
1448                 i += 1;
1449         }
1450
1451         ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1452         if (ret == TRUE) {
1453                 INFO("Success to save configuration [%s]", config_id);
1454                 wifi_complete_save_configuration(wifi, context);
1455                 char *file;
1456                 if (get_files_count(CONNMAN_STORAGE) > MAX_WIFI_PROFILES) {
1457                         file = get_least_recently_profile(CONNMAN_STORAGE);
1458                         if (file) {
1459                                 gchar *profileName = g_strdup_printf(CONNMAN_STORAGE "/%s", file);
1460                                 INFO("least modified file:  %s", profileName);
1461                                 if (profileName) {
1462                                         if (__remove_configuration(profileName) != TRUE)
1463                                                 DBG("Failed to remove profile: [%s]", profileName);
1464                                 } else
1465                                         ERR("Profile: [%s] does not exist", file);
1466
1467                                 g_free(profileName);
1468                         }
1469                 }
1470         } else {
1471                 INFO("Fail to save configuration [%s]", config_id);
1472                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1473         }
1474
1475         g_key_file_free(keyfile);
1476         g_free(group_name);
1477         __free_wifi_configuration(conf);
1478
1479         g_variant_iter_free(iter);
1480
1481         return TRUE;
1482 }
1483
1484 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1485                 const gchar *ifname, const gchar *config_id)
1486 {
1487         gboolean ret = FALSE;
1488         GVariantBuilder *b = NULL;
1489         struct wifi_config *conf = NULL;
1490
1491         g_return_val_if_fail(wifi != NULL, TRUE);
1492
1493         conf = g_new0(struct wifi_config, 1);
1494         conf->eap_config = g_new0(struct wifi_eap_config, 1);
1495         conf->ip_info = g_new0(wifi_ip_info_s, 1);
1496
1497         ret = _load_configuration(ifname, config_id, conf);
1498         if (ret != TRUE) {
1499                 g_free(conf->eap_config);
1500                 g_free(conf->ip_info);
1501                 g_free(conf);
1502                 ERR("Fail to _load_configuration");
1503                 netconfig_error_no_profile(context);
1504                 return TRUE;
1505         }
1506
1507         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1508         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1509         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1510         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1511         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FREQUENCY, g_variant_new_uint32(conf->frequency));
1512
1513         if (conf->proxy_address != NULL)
1514                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1515         else
1516                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1517
1518         if (conf->last_error != NULL)
1519                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1520         else
1521                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1522
1523         if (conf->eap_config != NULL) {
1524                 if (conf->eap_config->anonymous_identity != NULL)
1525                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1526                 else
1527                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1528
1529                 if (conf->eap_config->ca_cert != NULL)
1530                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1531                 else
1532                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1533
1534                 if (conf->eap_config->client_cert != NULL)
1535                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1536                 else
1537                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1538
1539                 if (conf->eap_config->private_key != NULL)
1540                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1541                 else
1542                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1543
1544                 if (conf->eap_config->private_key_password != NULL)
1545                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string(conf->eap_config->private_key_password));
1546                 else
1547                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, g_variant_new_string("NONE"));
1548
1549                 if (conf->eap_config->identity != NULL)
1550                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1551                 else
1552                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1553
1554                 if (conf->eap_config->eap_type != NULL)
1555                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1556                 else
1557                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1558
1559                 if (conf->eap_config->eap_auth_type != NULL)
1560                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1561                 else
1562                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1563
1564                 if (conf->eap_config->subject_match != NULL)
1565                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1566                 else
1567                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1568         }
1569
1570         __free_wifi_configuration(conf);
1571
1572         wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1573         g_variant_builder_unref(b);
1574         return TRUE;
1575 }
1576
1577 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1578                 const gchar *ifname, const gchar *config_id, GVariant *configuration)
1579 {
1580         gboolean ret = FALSE;
1581         struct wifi_config *conf = NULL;
1582         GKeyFile *keyfile = NULL;
1583         GVariantIter *iter;
1584         GVariant *value;
1585         gchar *field;
1586         gchar *group_name = NULL;
1587
1588         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1589                 ERR("Invalid parameter");
1590                 netconfig_error_invalid_parameter(context);
1591                 return TRUE;
1592         }
1593
1594         conf = g_new0(struct wifi_config, 1);
1595         conf->eap_config = g_new0(struct wifi_eap_config, 1);
1596
1597         g_variant_get(configuration, "a{sv}", &iter);
1598         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1599                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1600                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1601                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
1602                                 DBG("name [%s]", conf->name);
1603                         } else {
1604                                 conf->name = NULL;
1605                         }
1606                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1607                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1608                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1609                                 DBG("ssid [%s]", conf->ssid);
1610                         } else {
1611                                 conf->ssid = NULL;
1612                         }
1613                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1614                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1615                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1616                                 DBG("passphrase [%s]", conf->passphrase);
1617                         } else {
1618                                 conf->passphrase = NULL;
1619                         }
1620                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1621                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1622                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1623                                 DBG("is_hidden [%s]", conf->is_hidden);
1624                         } else {
1625                                 conf->is_hidden = NULL;
1626                         }
1627                 } else if (g_strcmp0(field, WIFI_CONFIG_FREQUENCY) == 0) {
1628                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
1629                                 conf->frequency = g_variant_get_uint32(value);
1630                                 DBG("frequency [%d]", conf->frequency);
1631                         } else {
1632                                 conf->frequency = 0;
1633                         }
1634                 } else if (g_strcmp0(field, WIFI_CONFIG_CREATED) == 0) {
1635                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
1636                                 conf->is_created = g_variant_get_boolean(value);
1637                                 DBG("is_created [%d]", conf->is_created);
1638                         } else {
1639                                 conf->is_created = FALSE;
1640                         }
1641                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1642                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1643                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1644                                 DBG("proxy_address [%s]", conf->proxy_address);
1645                         } else {
1646                                 conf->proxy_address = NULL;
1647                         }
1648                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1649                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1650                                 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1651                                 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1652                         } else {
1653                                 conf->eap_config->anonymous_identity = NULL;
1654                         }
1655                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1656                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1657                                 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1658                                 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1659                         } else {
1660                                 conf->eap_config->ca_cert = NULL;
1661                         }
1662                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1663                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1664                                 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1665                                 DBG("client_cert [%s]", conf->eap_config->client_cert);
1666                         } else {
1667                                 conf->eap_config->client_cert = NULL;
1668                         }
1669                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1670                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1671                                 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1672                                 DBG("private_key [%s]", conf->eap_config->private_key);
1673                         } else {
1674                                 conf->eap_config->private_key = NULL;
1675                         }
1676                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD) == 0) {
1677                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1678                                 conf->eap_config->private_key_password = g_strdup(g_variant_get_string(value, NULL));
1679                                 DBG("private_key_password[%s]", conf->eap_config->private_key_password);
1680                         } else {
1681                                 conf->eap_config->private_key_password = NULL;
1682                         }
1683                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1684                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1685                                 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1686                                 DBG("identity [%s]", conf->eap_config->identity);
1687                         } else {
1688                                 conf->eap_config->identity = NULL;
1689                         }
1690                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1691                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1692                                 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1693                                 DBG("eap_type [%s]", conf->eap_config->eap_type);
1694                         } else {
1695                                 conf->eap_config->eap_type = NULL;
1696                         }
1697                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1698                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1699                                 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1700                                 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1701                         } else {
1702                                 conf->eap_config->eap_auth_type = NULL;
1703                         }
1704                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1705                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1706                                 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1707                                 DBG("subject_match [%s]", conf->eap_config->subject_match);
1708                         } else {
1709                                 conf->eap_config->subject_match = NULL;
1710                         }
1711                 }
1712         }
1713         conf->favorite = TRUE;
1714         conf->autoconnect = TRUE;
1715
1716         ret = wifi_config_get_group_name(WIFI_CONFIG_PREFIX,
1717                         ifname, config_id, &group_name);
1718         if (ret != TRUE) {
1719                 __free_wifi_configuration(conf);
1720                 ERR("Fail to get_wifi_config_group_name");
1721                 return TRUE;
1722         }
1723
1724         keyfile = g_key_file_new();
1725         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1726         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1727
1728         if (conf->passphrase != NULL) {
1729                 gchar *enc_data = NULL;
1730
1731                 if (conf->is_created == true)
1732                         enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1733                 else
1734                         enc_data = g_strdup(conf->passphrase);
1735
1736                 if (!enc_data) {
1737                         ERR("Failed to encrypt the passphrase");
1738                 } else {
1739                         g_free(conf->passphrase);
1740                         conf->passphrase = enc_data;
1741                 }
1742                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1743         }
1744
1745         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1746         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1747
1748         /* Optional field */
1749         if (conf->proxy_address != NULL) {
1750                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1751                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1752         }
1753
1754         if (conf->is_hidden != NULL) {
1755                 gboolean hidden = FALSE;
1756                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1757                         hidden = TRUE;
1758                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1759         }
1760
1761         if (conf->frequency > 0)
1762                 g_key_file_set_integer(keyfile, group_name,
1763                         WIFI_CONFIG_FREQUENCY, conf->frequency);
1764
1765         if (conf->eap_config->anonymous_identity != NULL)
1766                 g_key_file_set_string(keyfile, group_name,
1767                         WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1768
1769         if (conf->eap_config->ca_cert != NULL)
1770                 g_key_file_set_string(keyfile, group_name,
1771                         WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1772
1773         if (conf->eap_config->client_cert != NULL)
1774                 g_key_file_set_string(keyfile, group_name,
1775                         WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1776
1777         if (conf->eap_config->private_key != NULL)
1778                 g_key_file_set_string(keyfile, group_name,
1779                         WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1780
1781         if (conf->eap_config->private_key_password != NULL)
1782                 g_key_file_set_string(keyfile, group_name,
1783                         WIFI_CONFIG_EAP_PRIVATEKEY_PASSWORD, conf->eap_config->private_key_password);
1784
1785         if (conf->eap_config->identity != NULL)
1786                 g_key_file_set_string(keyfile, group_name,
1787                         WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1788
1789         if (conf->eap_config->eap_type != NULL)
1790                 g_key_file_set_string(keyfile, group_name,
1791                         WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1792
1793         if (conf->eap_config->eap_auth_type != NULL)
1794                 g_key_file_set_string(keyfile, group_name,
1795                         WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1796
1797         if (conf->eap_config->subject_match != NULL)
1798                 g_key_file_set_string(keyfile, group_name,
1799                         WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1800
1801         ret = wifi_config_save_configuration(ifname, config_id, keyfile);
1802         if (ret == TRUE) {
1803                 INFO("Success to save eap configuration [%s]", config_id);
1804                 wifi_complete_save_eap_configuration(wifi, context);
1805         } else {
1806                 INFO("Fail to save eap configuration [%s]", config_id);
1807                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1808         }
1809
1810         g_key_file_free(keyfile);
1811         g_free(group_name);
1812         __free_wifi_configuration(conf);
1813
1814         g_variant_iter_free(iter);
1815
1816         return TRUE;
1817 }
1818
1819 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1820                         const gchar *ifname, const gchar *config_id)
1821 {
1822         gboolean ret = FALSE;
1823
1824         if ((wifi == NULL) || (config_id == NULL)) {
1825                 ERR("Invalid parameter");
1826                 netconfig_error_invalid_parameter(context);
1827                 return TRUE;
1828         }
1829
1830         ret = _remove_configuration(ifname, config_id);
1831         if (ret != TRUE) {
1832                 /* no configuration or error */
1833                 ERR("No [%s] configuration", config_id);
1834                 netconfig_error_no_profile(context);
1835                 return TRUE;
1836         }
1837
1838         wifi_complete_remove_configuration(wifi, context);
1839         return TRUE;
1840 }
1841
1842 gboolean handle_reset_wifi_config(Wifi *wifi, GDBusMethodInvocation *context)
1843 {
1844         DIR *dir_ptr = NULL;
1845         struct dirent *file = NULL;
1846         struct stat buf;
1847         char dir_name[512] = { 0, };
1848         char file_name[1024] = { 0, };
1849
1850         g_return_val_if_fail(wifi != NULL, TRUE);
1851
1852         DBG("Try to remove connman Wi-Fi config files...");
1853
1854         if ((dir_ptr = opendir(CONNMAN_STORAGE)) != NULL) {
1855                 while ((file = readdir(dir_ptr)) != NULL) {
1856                         if (strncmp(file->d_name, ".", 1) == 0 || strncmp(file->d_name, "..", 2) == 0 ||
1857                                         strncmp(file->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
1858                                 continue;
1859                         }
1860
1861                         snprintf(dir_name, 512, CONNMAN_STORAGE"/%s", file->d_name);
1862
1863                         if (lstat(dir_name, &buf) == -1)
1864                                 continue;
1865
1866                         DBG("Remove wifi config: %s", file->d_name);
1867
1868                         if (S_ISDIR(buf.st_mode)) {
1869                                 memset(file_name, 0, 1024);
1870                                 snprintf(file_name, 1024, "%s/data", dir_name);
1871                                 unlink(file_name);
1872                                 memset(file_name, 0, 1024);
1873                                 snprintf(file_name, 1024, "%s/settings", dir_name);
1874                                 unlink(file_name);
1875                         }
1876                         rmdir(dir_name);
1877                 }
1878
1879                 closedir(dir_ptr);
1880                 sync();
1881         }
1882
1883         wifi_complete_reset_wifi_config(wifi, context);
1884
1885         return TRUE;
1886 }
1887
1888 /* config field key / value */
1889 /*
1890  * [wifi_macaddress_config_id]
1891  * Name=name (mandatory)
1892  * SSID=SSID (mandatory)
1893  * Frequency=2462 (X)
1894  * Favorite=true (X)
1895  * AutoConnect=true (Default true)
1896  * Modified=2015-03-20 (X)
1897  * IPv4.method=manual (O)
1898  * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1899  * IPv6.method=auto (X)
1900  * IPv6.privacy=disabled (X)
1901  * IPv4.netmask_prefixlen=24 (X)
1902  * IPv4.local_address=192.0.0.1 (O)
1903  * IPv4.gateway=192.0.0.1 (O ? X ?)
1904  * Nameservers=192.168.43.22; (O)
1905  * Proxy.Method=manual (O)
1906  * Proxy.Servers=trst.com:8888; (O)
1907  */
1908 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1909                 const gchar *ifname, const gchar *config_id, const gchar *key, const gchar *value)
1910 {
1911         gboolean ret = FALSE;
1912         gchar *keyfile_key = NULL;
1913
1914         g_return_val_if_fail(wifi != NULL, TRUE);
1915         g_return_val_if_fail(config_id != NULL, TRUE);
1916         g_return_val_if_fail(key != NULL, TRUE);
1917
1918         DBG("Key[%s] Value[%s]", key, value);
1919
1920         if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1921                 ret = _set_field(ifname, config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1922                 if (!ret) {
1923                         ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1924                         netconfig_error_invalid_parameter(context);
1925                         return TRUE;
1926                 }
1927                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1928         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1929                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1930         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1931                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1932         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1933                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1934         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1935                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1936         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1937                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1938         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1939                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1940         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1941                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1942         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1943                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1944         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1945                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1946         } else {
1947                 ERR("Not supported key[%s]", key);
1948                 netconfig_error_invalid_parameter(context);
1949                 return TRUE;
1950         }
1951
1952         ret = _set_field(ifname, config_id, keyfile_key, (const gchar *)value);
1953         if (!ret) {
1954                 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1955         }
1956
1957         if (keyfile_key != NULL)
1958                 g_free(keyfile_key);
1959
1960         wifi_complete_set_config_field(wifi, context);
1961         return TRUE;
1962 }
1963
1964 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context,
1965                         const gchar *ifname, const gchar *config_id)
1966 {
1967         gboolean ret = FALSE;
1968         gchar *passphrase = NULL;
1969
1970         if ((wifi == NULL) || (config_id == NULL)) {
1971                 ERR("Invalid parameter");
1972                 netconfig_error_invalid_parameter(context);
1973                 return TRUE;
1974         }
1975
1976         ret = _get_field(ifname, config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1977         if (!ret) {
1978                 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1979                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1980                 return TRUE;
1981         }
1982
1983         wifi_complete_get_config_passphrase(wifi, context, passphrase);
1984         g_free(passphrase);
1985
1986         return TRUE;
1987 }
1988
1989 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1990                 const gchar *ifname, int frame_id, const gchar *vsie)
1991 {
1992         DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1993
1994         g_return_val_if_fail(wifi != NULL, TRUE);
1995         g_return_val_if_fail(vsie != NULL, TRUE);
1996
1997         gboolean ret = FALSE;
1998
1999         ret = _add_vsie(ifname, frame_id, vsie);
2000         if (!ret) {
2001                 DBG("Failed to add vsie: %s", vsie);
2002                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
2003                 return TRUE;
2004         }
2005
2006         wifi_complete_add_vsie(wifi, context);
2007         return TRUE;
2008 }
2009
2010 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
2011                 const gchar *ifname, int frame_id)
2012 {
2013         DBG("Frame ID: [%d]", frame_id);
2014
2015         g_return_val_if_fail(wifi != NULL, TRUE);
2016
2017         gboolean ret = FALSE;
2018         gchar *vsie = NULL;
2019
2020         ret = _get_vsie(ifname, frame_id, &vsie);
2021         if (!ret) {
2022                 DBG("Failed to get vsie for frame:[%d]", frame_id);
2023                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
2024                 return TRUE;
2025         }
2026
2027         DBG("Received vsie: %s", vsie);
2028         wifi_complete_get_vsie(wifi, context, vsie);
2029
2030         return TRUE;
2031 }
2032
2033 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
2034                 const gchar *ifname, int frame_id, const gchar *vsie)
2035 {
2036         DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
2037
2038         g_return_val_if_fail(wifi != NULL, TRUE);
2039         g_return_val_if_fail(vsie != NULL, TRUE);
2040
2041         gboolean ret = FALSE;
2042
2043         ret = _remove_vsie(ifname, frame_id, vsie);
2044         if (!ret) {
2045                 DBG("Failed to remove vsie: %s", vsie);
2046                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
2047                 return TRUE;
2048         }
2049
2050         wifi_complete_remove_vsie(wifi, context);
2051         return TRUE;
2052 }