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