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