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