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