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