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