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