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