Fix dbus method return value
[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                 return 0;
696         }
697
698         length = 0;
699         while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
700                 length++;
701         g_variant_iter_free(iter_copy);
702
703         if (length == 0) {
704                 g_free(tmp_dst);
705                 tmp_dst = NULL;
706         } else {
707                 tmp_dst[length] = '\0';
708         }
709
710         *dst = tmp_dst;
711         DBG("Length [%d]", length);
712         return length;
713 }
714
715 gboolean _add_vsie(int frame_id, const char* vsie)
716 {
717         GVariant *params = NULL;
718         GVariant *message = NULL;
719         GVariantBuilder *bytearray_builder = NULL;
720         char *if_path;
721         int i = 0;
722         size_t vsie_len = 0;
723
724         unsigned char *bytearray = NULL;
725         size_t bytearray_len = 0;
726
727         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
728                 DBG("Invalid parameter, frame-id: %d", frame_id);
729                 return FALSE;
730         }
731
732         vsie_len = strlen(vsie);
733         if (vsie_len == 0) {
734                 DBG("vsie length is zero");
735                 return FALSE;
736         }
737
738         bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
739
740         bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
741         if (bytearray == NULL) {
742                 DBG("Failed to allocate memory to bytearray");
743                 return FALSE;
744         }
745
746         if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
747                 DBG("invalid vsie string");
748                 g_free(bytearray);
749                 return FALSE;
750         }
751
752         bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
753         for (i = 0; i < bytearray_len; i++)
754                 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
755
756         params = g_variant_new("(iay)", frame_id, bytearray_builder);
757         g_variant_builder_unref(bytearray_builder);
758
759         if_path = netconfig_wifi_get_supplicant_interface();
760
761         if (if_path == NULL) {
762                 ERR("Fail to get wpa_supplicant DBus path");
763                 g_free(bytearray);
764                 return FALSE;
765         }
766
767         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
768                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
769
770         g_free(if_path);
771         if (message == NULL) {
772                 ERR("Failed to send command to wpa_supplicant");
773                 g_free(bytearray);
774                 return FALSE;
775         }
776
777         DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
778
779         g_free(bytearray);
780         return TRUE;
781 }
782
783 gboolean _get_vsie(int frame_id, char **vsie)
784 {
785         GVariant *params = NULL;
786         GVariant *message = NULL;
787         char *if_path;
788
789         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
790                 DBG("Invalid parameter, frame-id: %d", frame_id);
791                 return FALSE;
792         }
793
794         if_path = netconfig_wifi_get_supplicant_interface();
795         if (if_path == NULL) {
796                 ERR("Fail to get wpa_supplicant DBus path");
797                 return FALSE;
798         }
799
800         params = g_variant_new("(i)", frame_id);
801
802         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
803                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
804
805         g_free(if_path);
806         if (message == NULL) {
807                 ERR("Failed to send command to wpa_supplicant");
808                 return FALSE;
809         } else {
810                 GVariantIter *iter = NULL;
811                 unsigned char *vsie_bytes = NULL;
812                 int vsie_len = 0;
813                 int ret = 0;
814
815                 g_variant_get(message, "(ay)", &iter);
816                 if (iter == NULL) {
817                         ERR("vsie is not present");
818                         return FALSE;
819                 }
820
821                 vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
822                 if (vsie_bytes == NULL) {
823                         ERR("vsie_bytes not allocated");
824                         return FALSE;
825                 }
826
827                 ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
828                 if (ret < 0) {
829                         g_free(vsie_bytes);
830                         ERR("vsie not allocated.");
831                         return FALSE;
832                 }
833
834                 g_free(vsie_bytes);
835         }
836
837         ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
838
839         return TRUE;
840
841 }
842
843 gboolean _remove_vsie(int frame_id, const char *vsie)
844 {
845         GVariant *params = NULL;
846         GVariant *message = NULL;
847         GVariantBuilder *bytearray_builder = NULL;
848         char *if_path;
849         int i = 0;
850         size_t vsie_len = 0;
851
852         unsigned char *bytearray = NULL;
853         size_t bytearray_len = 0;
854
855         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
856                 DBG("Invalid parameter, frame-id: %d", frame_id);
857                 return FALSE;
858         }
859
860         vsie_len = strlen(vsie);
861         if (vsie_len == 0) {
862                 DBG("vsie length is zero");
863                 return FALSE;
864         }
865
866         bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
867
868         bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
869         if (bytearray == NULL) {
870                 DBG("Failed to allocate memory to bytearray");
871                 return FALSE;
872         }
873
874         if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
875                 DBG("invalid vsie string");
876                 g_free(bytearray);
877                 return FALSE;
878         }
879
880         bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
881         for (i = 0; i < bytearray_len; i++)
882                 g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
883
884         params = g_variant_new("(iay)", frame_id, bytearray_builder);
885         g_variant_builder_unref(bytearray_builder);
886
887         if_path = netconfig_wifi_get_supplicant_interface();
888         if (if_path == NULL) {
889                 ERR("Fail to get wpa_supplicant DBus path");
890                 g_free(bytearray);
891                 return FALSE;
892         }
893
894         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
895                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
896
897         g_free(if_path);
898         if (message == NULL) {
899                 ERR("Failed to send command to wpa_supplicant");
900                 g_free(bytearray);
901                 return FALSE;
902         }
903
904         DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
905
906         g_free(bytearray);
907         return TRUE;
908 }
909
910 /* dbus method */
911 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
912 {
913         guint i = 0;
914         GSList *config_ids = NULL;
915         guint length;
916         gchar **result = NULL;
917
918         g_return_val_if_fail(wifi != NULL, TRUE);
919
920         config_ids = _get_list();
921         if (config_ids == NULL) {
922                 ERR("Fail to get config list");
923                 netconfig_error_no_profile(context);
924                 return TRUE;
925         }
926
927         length = g_slist_length(config_ids);
928         result = g_new0(gchar *, length + 1);
929         for (i = 0; i < length; i++) {
930                 gchar *config_id = g_slist_nth_data(config_ids, i);
931                 result[i] = g_strdup(config_id);
932         }
933
934         config_ids = g_slist_nth(config_ids, 0);
935         g_slist_free_full(config_ids, g_free);
936
937         wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
938
939         for (i = 0; i < length; i++)
940                 if (result[i])
941                         g_free(result[i]);
942
943         if (result)
944                 g_free(result);
945
946         return TRUE;
947 }
948
949 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
950                 const gchar *config_id)
951 {
952         gboolean ret = FALSE;
953         GVariantBuilder *b = NULL;
954         struct wifi_config *conf = NULL;
955
956         g_return_val_if_fail(wifi != NULL, TRUE);
957
958         conf = g_new0(struct wifi_config, 1);
959
960         ret = _load_configuration(config_id, conf);
961         if (ret != TRUE) {
962                 g_free(conf);
963                 ERR("Fail to _load_configuration");
964                 netconfig_error_no_profile(context);
965                 return TRUE;
966         }
967
968         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
969         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
970         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
971         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
972
973         if (conf->proxy_address != NULL)
974                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
975         else
976                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
977
978         if (conf->last_error != NULL)
979                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
980         else
981                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
982
983         g_free(conf->proxy_address);
984         g_free(conf->last_error);
985         g_free(conf->name);
986         g_free(conf->security_type);
987         g_free(conf->is_hidden);
988         g_free(conf);
989
990         wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
991         g_variant_builder_unref(b);
992         return TRUE;
993 }
994
995 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
996                 const gchar *config_id, GVariant *configuration)
997 {
998         gboolean ret = FALSE;
999         struct wifi_config *conf = NULL;
1000         GKeyFile *keyfile = NULL;
1001         GVariantIter *iter;
1002         GVariant *value;
1003         gchar *field;
1004         gchar *group_name = NULL;
1005
1006         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1007                 ERR("Invalid parameter");
1008                 netconfig_error_invalid_parameter(context);
1009                 return TRUE;
1010         }
1011
1012         conf = g_new0(struct wifi_config, 1);
1013
1014         g_variant_get(configuration, "a{sv}", &iter);
1015         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1016                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1017                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1018                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
1019                                 DBG("name [%s]", conf->name);
1020                         } else {
1021                                 conf->name = NULL;
1022                         }
1023                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1024                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1025                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1026                                 DBG("ssid [%s]", conf->ssid);
1027                         } else {
1028                                 conf->ssid = NULL;
1029                         }
1030                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1031                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1032                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1033                                 DBG("passphrase []");
1034                         } else {
1035                                 conf->passphrase = NULL;
1036                         }
1037                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1038                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1039                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1040                                 DBG("is_hidden [%s]", conf->is_hidden);
1041                         } else {
1042                                 conf->is_hidden = NULL;
1043                         }
1044                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1045                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1046                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1047                                 DBG("proxy_address [%s]", conf->proxy_address);
1048                         } else {
1049                                 conf->proxy_address = NULL;
1050                         }
1051                 }
1052         }
1053         conf->favorite = TRUE;
1054         conf->autoconnect = TRUE;
1055
1056         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1057         if (ret != TRUE) {
1058                 g_free(conf->name);
1059                 g_free(conf->ssid);
1060                 g_free(conf->passphrase);
1061                 g_free(conf->is_hidden);
1062                 g_free(conf->proxy_address);
1063                 g_free(conf);
1064                 ERR("Fail to get_wifi_config_group_name");
1065                 netconfig_error_fail_save_congifuration(context);
1066                 return TRUE;
1067         }
1068
1069         keyfile = g_key_file_new();
1070         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1071         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1072
1073         if (conf->passphrase != NULL) {
1074                 gchar *enc_data = NULL;
1075                 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1076
1077                 if (!enc_data) {
1078                         ERR("Failed to encrypt the passphrase");
1079                 } else {
1080                         g_free(conf->passphrase);
1081                         conf->passphrase = enc_data;
1082                 }
1083
1084                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1085         }
1086
1087         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1088         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1089
1090         /* Optional field */
1091         if (conf->proxy_address != NULL) {
1092                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1093                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1094         }
1095
1096         if (conf->is_hidden != NULL) {
1097                 gboolean hidden = FALSE;
1098                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1099                         hidden = TRUE;
1100                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1101         }
1102
1103         ret = _save_configuration(config_id, keyfile);
1104         if (ret == TRUE) {
1105                 INFO("Success to save configuration [%s]", config_id);
1106                 wifi_complete_save_configuration(wifi, context);
1107         } else {
1108                 INFO("Fail to save configuration [%s]", config_id);
1109                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1110         }
1111
1112         g_key_file_free(keyfile);
1113         g_free(group_name);
1114         g_free(conf->name);
1115         g_free(conf->ssid);
1116         g_free(conf->passphrase);
1117         g_free(conf->is_hidden);
1118         g_free(conf->proxy_address);
1119         g_free(conf);
1120
1121         g_variant_iter_free(iter);
1122
1123         return TRUE;
1124 }
1125
1126 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1127                 const gchar *config_id)
1128 {
1129         gboolean ret = FALSE;
1130         GVariantBuilder *b = NULL;
1131         struct wifi_config *conf = NULL;
1132
1133         g_return_val_if_fail(wifi != NULL, TRUE);
1134
1135         conf = g_new0(struct wifi_config, 1);
1136         conf->eap_config = g_new0(struct wifi_eap_config, 1);
1137
1138         ret = _load_configuration(config_id, conf);
1139         if (ret != TRUE) {
1140                 g_free(conf->eap_config);
1141                 g_free(conf);
1142                 ERR("Fail to _load_configuration");
1143                 netconfig_error_no_profile(context);
1144                 return TRUE;
1145         }
1146
1147         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1148         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1149         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1150         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1151         if (conf->proxy_address != NULL)
1152                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1153         else
1154                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1155
1156         if (conf->last_error != NULL)
1157                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1158         else
1159                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1160
1161         if (conf->eap_config != NULL) {
1162                 if (conf->eap_config->anonymous_identity != NULL)
1163                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1164                 else
1165                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1166
1167                 if (conf->eap_config->ca_cert != NULL)
1168                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1169                 else
1170                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1171
1172                 if (conf->eap_config->client_cert != NULL)
1173                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1174                 else
1175                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1176
1177                 if (conf->eap_config->private_key != NULL)
1178                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1179                 else
1180                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1181
1182                 if (conf->eap_config->identity != NULL)
1183                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1184                 else
1185                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1186
1187                 if (conf->eap_config->eap_type != NULL)
1188                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1189                 else
1190                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1191
1192                 if (conf->eap_config->eap_auth_type != NULL)
1193                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1194                 else
1195                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1196
1197                 if (conf->eap_config->subject_match != NULL)
1198                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1199                 else
1200                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1201         }
1202
1203         __free_wifi_configuration(conf);
1204
1205         wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1206         g_variant_builder_unref(b);
1207         return TRUE;
1208 }
1209
1210 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1211                 const gchar *config_id, GVariant *configuration)
1212 {
1213         gboolean ret = FALSE;
1214         struct wifi_config *conf = NULL;
1215         GKeyFile *keyfile = NULL;
1216         GVariantIter *iter;
1217         GVariant *value;
1218         gchar *field;
1219         gchar *group_name = NULL;
1220
1221         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1222                 ERR("Invalid parameter");
1223                 netconfig_error_invalid_parameter(context);
1224                 return TRUE;
1225         }
1226
1227         conf = g_new0(struct wifi_config, 1);
1228         conf->eap_config = g_new0(struct wifi_eap_config, 1);
1229
1230         g_variant_get(configuration, "a{sv}", &iter);
1231         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1232                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1233                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1234                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
1235                                 DBG("name [%s]", conf->name);
1236                         } else {
1237                                 conf->name = NULL;
1238                         }
1239                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1240                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1241                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1242                                 DBG("ssid [%s]", conf->ssid);
1243                         } else {
1244                                 conf->ssid = NULL;
1245                         }
1246                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1247                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1248                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1249                                 DBG("passphrase [%s]", conf->passphrase);
1250                         } else {
1251                                 conf->passphrase = NULL;
1252                         }
1253                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1254                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1255                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1256                                 DBG("is_hidden [%s]", conf->is_hidden);
1257                         } else {
1258                                 conf->is_hidden = NULL;
1259                         }
1260                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1261                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1262                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1263                                 DBG("proxy_address [%s]", conf->proxy_address);
1264                         } else {
1265                                 conf->proxy_address = NULL;
1266                         }
1267                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1268                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1269                                 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1270                                 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1271                         } else {
1272                                 conf->eap_config->anonymous_identity = NULL;
1273                         }
1274                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1275                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1276                                 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1277                                 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1278                         } else {
1279                                 conf->eap_config->ca_cert = NULL;
1280                         }
1281                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1282                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1283                                 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1284                                 DBG("client_cert [%s]", conf->eap_config->client_cert);
1285                         } else {
1286                                 conf->eap_config->client_cert = NULL;
1287                         }
1288                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1289                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1290                                 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1291                                 DBG("private_key [%s]", conf->eap_config->private_key);
1292                         } else {
1293                                 conf->eap_config->private_key = NULL;
1294                         }
1295                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1296                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1297                                 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1298                                 DBG("identity [%s]", conf->eap_config->identity);
1299                         } else {
1300                                 conf->eap_config->identity = NULL;
1301                         }
1302                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1303                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1304                                 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1305                                 DBG("eap_type [%s]", conf->eap_config->eap_type);
1306                         } else {
1307                                 conf->eap_config->eap_type = NULL;
1308                         }
1309                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1310                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1311                                 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1312                                 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1313                         } else {
1314                                 conf->eap_config->eap_auth_type = NULL;
1315                         }
1316                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1317                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1318                                 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1319                                 DBG("subject_match [%s]", conf->eap_config->subject_match);
1320                         } else {
1321                                 conf->eap_config->subject_match = NULL;
1322                         }
1323                 }
1324         }
1325         conf->favorite = TRUE;
1326         conf->autoconnect = TRUE;
1327
1328         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1329         if (ret != TRUE) {
1330                 __free_wifi_configuration(conf);
1331                 ERR("Fail to get_wifi_config_group_name");
1332                 return TRUE;
1333         }
1334
1335         keyfile = g_key_file_new();
1336         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1337         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1338
1339         if (conf->passphrase != NULL) {
1340                 gchar *enc_data = NULL;
1341                 enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
1342
1343                 if (!enc_data) {
1344                         ERR("Failed to encrypt the passphrase");
1345                 } else {
1346                         g_free(conf->passphrase);
1347                         conf->passphrase = enc_data;
1348                 }
1349                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1350         }
1351
1352         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1353         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1354
1355         /* Optional field */
1356         if (conf->proxy_address != NULL) {
1357                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1358                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1359         }
1360
1361         if (conf->is_hidden != NULL) {
1362                 gboolean hidden = FALSE;
1363                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1364                         hidden = TRUE;
1365                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1366         }
1367
1368         if (conf->eap_config->anonymous_identity != NULL)
1369                 g_key_file_set_string(keyfile, group_name,
1370                         WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1371
1372         if (conf->eap_config->ca_cert != NULL)
1373                 g_key_file_set_string(keyfile, group_name,
1374                         WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1375
1376         if (conf->eap_config->client_cert != NULL)
1377                 g_key_file_set_string(keyfile, group_name,
1378                         WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1379
1380         if (conf->eap_config->private_key != NULL)
1381                 g_key_file_set_string(keyfile, group_name,
1382                         WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1383
1384         if (conf->eap_config->identity != NULL)
1385                 g_key_file_set_string(keyfile, group_name,
1386                         WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1387
1388         if (conf->eap_config->eap_type != NULL)
1389                 g_key_file_set_string(keyfile, group_name,
1390                         WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1391
1392         if (conf->eap_config->eap_auth_type != NULL)
1393                 g_key_file_set_string(keyfile, group_name,
1394                         WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1395
1396         if (conf->eap_config->subject_match != NULL)
1397                 g_key_file_set_string(keyfile, group_name,
1398                         WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1399
1400         ret = _save_configuration(config_id, keyfile);
1401         if (ret == TRUE) {
1402                 INFO("Success to save eap configuration [%s]", config_id);
1403                 wifi_complete_save_eap_configuration(wifi, context);
1404         } else {
1405                 INFO("Fail to save eap configuration [%s]", config_id);
1406                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1407         }
1408
1409         g_key_file_free(keyfile);
1410         g_free(group_name);
1411         __free_wifi_configuration(conf);
1412
1413         g_variant_iter_free(iter);
1414
1415         return TRUE;
1416 }
1417
1418 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1419 {
1420         gboolean ret = FALSE;
1421
1422         if ((wifi == NULL) || (config_id == NULL)) {
1423                 ERR("Invalid parameter");
1424                 netconfig_error_invalid_parameter(context);
1425                 return TRUE;
1426         }
1427
1428         ret = _remove_configuration(config_id);
1429         if (ret != TRUE) {
1430                 /* no configuration or error */
1431                 ERR("No [%s] configuration", config_id);
1432                 netconfig_error_no_profile(context);
1433                 return TRUE;
1434         }
1435
1436         wifi_complete_remove_configuration(wifi, context);
1437         return TRUE;
1438 }
1439
1440 /* config field key / value */
1441 /*
1442  * [wifi_macaddress_config_id]
1443  * Name=name (mandatory)
1444  * SSID=SSID (mandatory)
1445  * Frequency=2462 (X)
1446  * Favorite=true (X)
1447  * AutoConnect=true (Default true)
1448  * Modified=2015-03-20 (X)
1449  * IPv4.method=manual (O)
1450  * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1451  * IPv6.method=auto (X)
1452  * IPv6.privacy=disabled (X)
1453  * IPv4.netmask_prefixlen=24 (X)
1454  * IPv4.local_address=192.0.0.1 (O)
1455  * IPv4.gateway=192.0.0.1 (O ? X ?)
1456  * Nameservers=192.168.43.22; (O)
1457  * Proxy.Method=manual (O)
1458  * Proxy.Servers=trst.com:8888; (O)
1459  */
1460 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1461                 const gchar *config_id, const gchar *key, const gchar *value)
1462 {
1463         gboolean ret = FALSE;
1464         gchar *keyfile_key = NULL;
1465
1466         g_return_val_if_fail(wifi != NULL, TRUE);
1467         g_return_val_if_fail(config_id != NULL, TRUE);
1468         g_return_val_if_fail(key != NULL, TRUE);
1469
1470         DBG("Key[%s] Value[%d]", key, value);
1471
1472         if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1473                 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1474                 if (!ret) {
1475                         ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1476                         netconfig_error_invalid_parameter(context);
1477                         return TRUE;
1478                 }
1479                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1480         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1481                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1482         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1483                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1484         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1485                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1486         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1487                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1488         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1489                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1490         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1491                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1492         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1493                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1494         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1495                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1496         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1497                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1498         } else {
1499                 ERR("Not supported key[%s]", key);
1500                 netconfig_error_invalid_parameter(context);
1501                 return TRUE;
1502         }
1503
1504         ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1505         if (!ret) {
1506                 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1507         }
1508
1509         if (keyfile_key != NULL)
1510                 g_free(keyfile_key);
1511
1512         wifi_complete_set_config_field(wifi, context);
1513         return TRUE;
1514 }
1515
1516 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1517 {
1518         gboolean ret = FALSE;
1519         gchar *passphrase = NULL;
1520
1521         if ((wifi == NULL) || (config_id == NULL)) {
1522                 ERR("Invalid parameter");
1523                 netconfig_error_invalid_parameter(context);
1524                 return TRUE;
1525         }
1526
1527         ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1528         if (!ret) {
1529                 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1530                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1531                 return TRUE;
1532         }
1533
1534         wifi_complete_get_config_passphrase(wifi, context, passphrase);
1535         g_free(passphrase);
1536
1537         return TRUE;
1538 }
1539
1540 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1541                 int frame_id, const gchar *vsie)
1542 {
1543         DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1544
1545         g_return_val_if_fail(wifi != NULL, TRUE);
1546         g_return_val_if_fail(vsie != NULL, TRUE);
1547
1548         gboolean ret = FALSE;
1549
1550         ret = _add_vsie(frame_id, vsie);
1551         if (!ret) {
1552                 DBG("Failed to add vsie: %s", vsie);
1553                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1554                 return TRUE;
1555         }
1556
1557         wifi_complete_add_vsie(wifi, context);
1558         return TRUE;
1559 }
1560
1561 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1562                 int frame_id)
1563 {
1564         DBG("Frame ID: [%d]", frame_id);
1565
1566         g_return_val_if_fail(wifi != NULL, TRUE);
1567
1568         gboolean ret = FALSE;
1569         gchar *vsie = NULL;
1570
1571         ret = _get_vsie(frame_id, &vsie);
1572         if (!ret) {
1573                 DBG("Failed to get vsie for frame:[%d]", frame_id);
1574                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1575                 return TRUE;
1576         }
1577
1578         DBG("Received vsie: %s", vsie);
1579         wifi_complete_get_vsie(wifi, context, vsie);
1580
1581         return TRUE;
1582 }
1583
1584 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1585                 int frame_id, const gchar *vsie)
1586 {
1587         DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1588
1589         g_return_val_if_fail(wifi != NULL, TRUE);
1590         g_return_val_if_fail(vsie != NULL, TRUE);
1591
1592         gboolean ret = FALSE;
1593
1594         ret = _remove_vsie(frame_id, vsie);
1595         if (!ret) {
1596                 DBG("Failed to remove vsie: %s", vsie);
1597                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1598                 return TRUE;
1599         }
1600
1601         wifi_complete_remove_vsie(wifi, context);
1602         return TRUE;
1603 }