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