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