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