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