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