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