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