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