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