Fixed coverity issues
[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
37 #define CONNMAN_STORAGE         "/var/lib/connman"
38
39 #define WIFI_SECURITY_NONE              "none"
40 #define WIFI_SECURITY_WEP               "wep"
41 #define WIFI_SECURITY_WPA_PSK   "psk"
42 #define WIFI_SECURITY_EAP               "ieee8021x"
43
44 #define WIFI_CONFIG_PREFIX      "wifi_"
45 #define MAC_ADDRESS_LENGTH              12
46 #define WIFI_PREFIX_LENGTH              MAC_ADDRESS_LENGTH + 6  /* wifi_485a3f2f506a_ */
47 #define PROFILE_PREFIX_LENGTH   WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
48
49 #define WIFI_MAC_ADD_LENGTH             17
50 #define WIFI_MAC_ADD_PATH               "/sys/class/net/wlan0/address"
51
52 struct wifi_eap_config {
53         gchar *anonymous_identity;
54         gchar *ca_cert;
55         gchar *client_cert;
56         gchar *private_key;
57         gchar *identity;
58         gchar *eap_type;
59         gchar *eap_auth_type;
60         gchar *subject_match;
61 };
62
63 struct wifi_config {
64         gchar *name;
65         gchar *ssid;
66         gchar *passphrase;
67         gchar *security_type;
68         gboolean favorite;
69         gboolean autoconnect;
70         gchar *is_hidden;
71         gchar *proxy_address;
72         struct wifi_eap_config *eap_config;
73         gchar *last_error;
74 };
75
76 static void __free_wifi_configuration(struct wifi_config *conf)
77 {
78         if (conf == NULL)
79                 return;
80
81         g_free(conf->name);
82         g_free(conf->ssid);
83         g_free(conf->passphrase);
84         g_free(conf->security_type);
85         g_free(conf->is_hidden);
86         g_free(conf->proxy_address);
87         g_free(conf->last_error);
88         if (conf->eap_config) {
89                 g_free(conf->eap_config->anonymous_identity);
90                 g_free(conf->eap_config->ca_cert);
91                 g_free(conf->eap_config->client_cert);
92                 g_free(conf->eap_config->private_key);
93                 g_free(conf->eap_config->identity);
94                 g_free(conf->eap_config->eap_type);
95                 g_free(conf->eap_config->eap_auth_type);
96                 g_free(conf->eap_config->subject_match);
97                 g_free(conf->eap_config);
98         }
99         g_free(conf);
100 }
101
102 static gboolean __get_mac_address(gchar **mac_address)
103 {
104         gchar *tmp_mac = NULL;
105         gchar *tmp = NULL;
106         gchar mac[13] = { 0, };
107         gint i = 0, j = 0;
108
109         if (TIZEN_TV) {
110                 FILE *fp = NULL;
111                 char buf[WIFI_MAC_ADD_LENGTH + 1];
112                 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
113                         fp = fopen(WIFI_MAC_ADD_PATH, "r");
114
115                 if (fp == NULL) {
116                         ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
117                         *mac_address = NULL;
118                         return FALSE;
119                 }
120
121                 if (fgets(buf, sizeof(buf), fp) == NULL) {
122                         ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
123                         *mac_address = NULL;
124                         fclose(fp);
125                         return FALSE;
126                 }
127                 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
128                 if (tmp_mac == NULL) {
129                         ERR("malloc() failed");
130                         *mac_address = NULL;
131                         fclose(fp);
132                         return FALSE;
133                 }
134                 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
135                 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
136                 fclose(fp);
137         } else {
138                 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
139                 if (tmp_mac == NULL) {
140                         ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
141                         *mac_address = NULL;
142                         return FALSE;
143                 }
144         }
145         tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
146         free(tmp_mac);
147         while (tmp && tmp[i]) {
148                 if (tmp[i] != ':')
149                         mac[j++] = tmp[i];
150                 i++;
151         }
152         mac[12] = '\0';
153         *mac_address = g_strdup(mac);
154         g_free(tmp);
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 static 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 static 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 static 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         const 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         if (message == NULL) {
773                 ERR("Failed to send command to wpa_supplicant");
774                 g_free(bytearray);
775                 return FALSE;
776         }
777
778         DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
779
780         g_free(bytearray);
781         return TRUE;
782 }
783
784 gboolean _get_vsie(int frame_id, char **vsie)
785 {
786         GVariant *params = NULL;
787         GVariant *message = NULL;
788         const char *if_path;
789
790         if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
791                 DBG("Invalid parameter, frame-id: %d", frame_id);
792                 return FALSE;
793         }
794
795         if_path = netconfig_wifi_get_supplicant_interface();
796         if (if_path == NULL) {
797                 ERR("Fail to get wpa_supplicant DBus path");
798                 return FALSE;
799         }
800
801         params = g_variant_new("(i)", frame_id);
802
803         message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
804                         if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
805
806         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         const 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         if (message == NULL) {
898                 ERR("Failed to send command to wpa_supplicant");
899                 g_free(bytearray);
900                 return FALSE;
901         }
902
903         DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
904
905         g_free(bytearray);
906         return TRUE;
907 }
908
909 /* dbus method */
910 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
911 {
912         guint i = 0;
913         GSList *config_ids = NULL;
914         guint length;
915         gchar **result = NULL;
916
917         g_return_val_if_fail(wifi != NULL, FALSE);
918
919         config_ids = _get_list();
920         if (config_ids == NULL) {
921                 ERR("Fail to get config list");
922                 netconfig_error_no_profile(context);
923                 return FALSE;
924         }
925
926         length = g_slist_length(config_ids);
927         result = g_new0(gchar *, length + 1);
928         for (i = 0; i < length; i++) {
929                 gchar *config_id = g_slist_nth_data(config_ids, i);
930                 result[i] = g_strdup(config_id);
931         }
932
933         config_ids = g_slist_nth(config_ids, 0);
934         g_slist_free_full(config_ids, g_free);
935
936         wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
937
938         for (i = 0; i < length; i++)
939                 if (result[i])
940                         g_free(result[i]);
941
942         if (result)
943                 g_free(result);
944
945         return TRUE;
946 }
947
948 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
949                 const gchar *config_id)
950 {
951         gboolean ret = FALSE;
952         GVariantBuilder *b = NULL;
953         struct wifi_config *conf = NULL;
954
955         g_return_val_if_fail(wifi != NULL, FALSE);
956
957         conf = g_new0(struct wifi_config, 1);
958
959         ret = _load_configuration(config_id, conf);
960         if (ret != TRUE) {
961                 g_free(conf);
962                 ERR("Fail to _load_configuration");
963                 netconfig_error_no_profile(context);
964                 return FALSE;
965         }
966
967         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
968         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
969         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
970         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
971
972         if (conf->proxy_address != NULL)
973                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
974         else
975                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
976
977         if (conf->last_error != NULL)
978                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
979         else
980                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
981
982         g_free(conf->proxy_address);
983         g_free(conf->last_error);
984         g_free(conf->name);
985         g_free(conf->security_type);
986         g_free(conf->is_hidden);
987         g_free(conf);
988
989         wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
990         g_variant_builder_unref(b);
991         return TRUE;
992 }
993
994 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
995                 const gchar *config_id, GVariant *configuration)
996 {
997         gboolean ret = FALSE;
998         struct wifi_config *conf = NULL;
999         GKeyFile *keyfile = NULL;
1000         GVariantIter *iter;
1001         GVariant *value;
1002         gchar *field;
1003         gchar *group_name = NULL;
1004
1005         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1006                 ERR("Invalid parameter");
1007                 netconfig_error_invalid_parameter(context);
1008                 return FALSE;
1009         }
1010
1011         conf = g_new0(struct wifi_config, 1);
1012
1013         g_variant_get(configuration, "a{sv}", &iter);
1014         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1015                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1016                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1017                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
1018                                 DBG("name [%s]", conf->name);
1019                         } else {
1020                                 conf->name = NULL;
1021                         }
1022                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1023                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1024                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1025                                 DBG("ssid [%s]", conf->ssid);
1026                         } else {
1027                                 conf->ssid = NULL;
1028                         }
1029                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1030                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1031                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1032                                 DBG("passphrase []");
1033                         } else {
1034                                 conf->passphrase = NULL;
1035                         }
1036                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1037                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1038                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1039                                 DBG("is_hidden [%s]", conf->is_hidden);
1040                         } else {
1041                                 conf->is_hidden = NULL;
1042                         }
1043                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1044                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1045                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1046                                 DBG("proxy_address [%s]", conf->proxy_address);
1047                         } else {
1048                                 conf->proxy_address = NULL;
1049                         }
1050                 }
1051         }
1052         conf->favorite = TRUE;
1053         conf->autoconnect = TRUE;
1054
1055         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1056         if (ret != TRUE) {
1057                 g_free(conf->name);
1058                 g_free(conf->ssid);
1059                 g_free(conf->passphrase);
1060                 g_free(conf->is_hidden);
1061                 g_free(conf->proxy_address);
1062                 g_free(conf);
1063                 ERR("Fail to get_wifi_config_group_name");
1064                 return FALSE;
1065         }
1066
1067         keyfile = g_key_file_new();
1068         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1069         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1070
1071         if (conf->passphrase != NULL)
1072                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1073
1074         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1075         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1076
1077         /* Optional field */
1078         if (conf->proxy_address != NULL) {
1079                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1080                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1081         }
1082
1083         if (conf->is_hidden != NULL) {
1084                 gboolean hidden = FALSE;
1085                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1086                         hidden = TRUE;
1087                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1088         }
1089
1090         ret = _save_configuration(config_id, keyfile);
1091         if (ret == TRUE) {
1092                 INFO("Success to save configuration [%s]", config_id);
1093                 wifi_complete_save_configuration(wifi, context);
1094         } else {
1095                 INFO("Fail to save configuration [%s]", config_id);
1096                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
1097         }
1098
1099         g_key_file_free(keyfile);
1100         g_free(group_name);
1101         g_free(conf->name);
1102         g_free(conf->ssid);
1103         g_free(conf->passphrase);
1104         g_free(conf->is_hidden);
1105         g_free(conf->proxy_address);
1106         g_free(conf);
1107
1108         g_variant_iter_free(iter);
1109
1110         return ret;
1111 }
1112
1113 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1114                 const gchar *config_id)
1115 {
1116         gboolean ret = FALSE;
1117         GVariantBuilder *b = NULL;
1118         struct wifi_config *conf = NULL;
1119
1120         g_return_val_if_fail(wifi != NULL, FALSE);
1121
1122         conf = g_new0(struct wifi_config, 1);
1123         conf->eap_config = g_new0(struct wifi_eap_config, 1);
1124
1125         ret = _load_configuration(config_id, conf);
1126         if (ret != TRUE) {
1127                 g_free(conf->eap_config);
1128                 g_free(conf);
1129                 ERR("Fail to _load_configuration");
1130                 netconfig_error_no_profile(context);
1131                 return FALSE;
1132         }
1133
1134         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1135         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
1136         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
1137         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
1138         if (conf->proxy_address != NULL)
1139                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
1140         else
1141                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
1142
1143         if (conf->last_error != NULL)
1144                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
1145         else
1146                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
1147
1148         if (conf->eap_config != NULL) {
1149                 if (conf->eap_config->anonymous_identity != NULL)
1150                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
1151                 else
1152                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
1153
1154                 if (conf->eap_config->ca_cert != NULL)
1155                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
1156                 else
1157                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
1158
1159                 if (conf->eap_config->client_cert != NULL)
1160                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
1161                 else
1162                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
1163
1164                 if (conf->eap_config->private_key != NULL)
1165                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
1166                 else
1167                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
1168
1169                 if (conf->eap_config->identity != NULL)
1170                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
1171                 else
1172                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
1173
1174                 if (conf->eap_config->eap_type != NULL)
1175                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
1176                 else
1177                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
1178
1179                 if (conf->eap_config->eap_auth_type != NULL)
1180                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
1181                 else
1182                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
1183
1184                 if (conf->eap_config->subject_match != NULL)
1185                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
1186                 else
1187                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
1188         }
1189
1190         __free_wifi_configuration(conf);
1191
1192         wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
1193         g_variant_builder_unref(b);
1194         return TRUE;
1195 }
1196
1197 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
1198                 const gchar *config_id, GVariant *configuration)
1199 {
1200         gboolean ret = FALSE;
1201         struct wifi_config *conf = NULL;
1202         GKeyFile *keyfile = NULL;
1203         GVariantIter *iter;
1204         GVariant *value;
1205         gchar *field;
1206         gchar *group_name = NULL;
1207
1208         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
1209                 ERR("Invalid parameter");
1210                 netconfig_error_invalid_parameter(context);
1211                 return FALSE;
1212         }
1213
1214         conf = g_new0(struct wifi_config, 1);
1215         conf->eap_config = g_new0(struct wifi_eap_config, 1);
1216
1217         g_variant_get(configuration, "a{sv}", &iter);
1218         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
1219                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
1220                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1221                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
1222                                 DBG("name [%s]", conf->name);
1223                         } else {
1224                                 conf->name = NULL;
1225                         }
1226                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
1227                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1228                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
1229                                 DBG("ssid [%s]", conf->ssid);
1230                         } else {
1231                                 conf->ssid = NULL;
1232                         }
1233                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
1234                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1235                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
1236                                 DBG("passphrase [%s]", conf->passphrase);
1237                         } else {
1238                                 conf->passphrase = NULL;
1239                         }
1240                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
1241                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1242                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
1243                                 DBG("is_hidden [%s]", conf->is_hidden);
1244                         } else {
1245                                 conf->is_hidden = NULL;
1246                         }
1247                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
1248                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1249                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
1250                                 DBG("proxy_address [%s]", conf->proxy_address);
1251                         } else {
1252                                 conf->proxy_address = NULL;
1253                         }
1254                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1255                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1256                                 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
1257                                 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
1258                         } else {
1259                                 conf->eap_config->anonymous_identity = NULL;
1260                         }
1261                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
1262                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1263                                 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
1264                                 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
1265                         } else {
1266                                 conf->eap_config->ca_cert = NULL;
1267                         }
1268                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1269                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1270                                 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
1271                                 DBG("client_cert [%s]", conf->eap_config->client_cert);
1272                         } else {
1273                                 conf->eap_config->client_cert = NULL;
1274                         }
1275                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1276                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1277                                 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
1278                                 DBG("private_key [%s]", conf->eap_config->private_key);
1279                         } else {
1280                                 conf->eap_config->private_key = NULL;
1281                         }
1282                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1283                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1284                                 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
1285                                 DBG("identity [%s]", conf->eap_config->identity);
1286                         } else {
1287                                 conf->eap_config->identity = NULL;
1288                         }
1289                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
1290                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1291                                 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
1292                                 DBG("eap_type [%s]", conf->eap_config->eap_type);
1293                         } else {
1294                                 conf->eap_config->eap_type = NULL;
1295                         }
1296                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1297                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1298                                 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
1299                                 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
1300                         } else {
1301                                 conf->eap_config->eap_auth_type = NULL;
1302                         }
1303                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1304                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
1305                                 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
1306                                 DBG("subject_match [%s]", conf->eap_config->subject_match);
1307                         } else {
1308                                 conf->eap_config->subject_match = NULL;
1309                         }
1310                 }
1311         }
1312         conf->favorite = TRUE;
1313         conf->autoconnect = TRUE;
1314
1315         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1316         if (ret != TRUE) {
1317                 __free_wifi_configuration(conf);
1318                 ERR("Fail to get_wifi_config_group_name");
1319                 return FALSE;
1320         }
1321
1322         keyfile = g_key_file_new();
1323         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1324         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1325
1326         if (conf->passphrase != NULL)
1327                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1328
1329         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1330         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1331
1332         /* Optional field */
1333         if (conf->proxy_address != NULL) {
1334                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1335                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1336         }
1337
1338         if (conf->is_hidden != NULL) {
1339                 gboolean hidden = FALSE;
1340                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1341                         hidden = TRUE;
1342                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1343         }
1344
1345         if (conf->eap_config->anonymous_identity != NULL)
1346                 g_key_file_set_string(keyfile, group_name,
1347                         WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1348
1349         if (conf->eap_config->ca_cert != NULL)
1350                 g_key_file_set_string(keyfile, group_name,
1351                         WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1352
1353         if (conf->eap_config->client_cert != NULL)
1354                 g_key_file_set_string(keyfile, group_name,
1355                         WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1356
1357         if (conf->eap_config->private_key != NULL)
1358                 g_key_file_set_string(keyfile, group_name,
1359                         WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1360
1361         if (conf->eap_config->identity != NULL)
1362                 g_key_file_set_string(keyfile, group_name,
1363                         WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1364
1365         if (conf->eap_config->eap_type != NULL)
1366                 g_key_file_set_string(keyfile, group_name,
1367                         WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1368
1369         if (conf->eap_config->eap_auth_type != NULL)
1370                 g_key_file_set_string(keyfile, group_name,
1371                         WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1372
1373         if (conf->eap_config->subject_match != NULL)
1374                 g_key_file_set_string(keyfile, group_name,
1375                         WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1376
1377         ret = _save_configuration(config_id, keyfile);
1378         if (ret == TRUE) {
1379                 INFO("Success to save eap configuration [%s]", config_id);
1380                 wifi_complete_save_eap_configuration(wifi, context);
1381         } else {
1382                 INFO("Fail to save eap configuration [%s]", config_id);
1383                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1384         }
1385
1386         g_key_file_free(keyfile);
1387         g_free(group_name);
1388         __free_wifi_configuration(conf);
1389
1390         g_variant_iter_free(iter);
1391
1392         return ret;
1393 }
1394
1395 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1396 {
1397         gboolean ret = FALSE;
1398
1399         if ((wifi == NULL) || (config_id == NULL)) {
1400                 ERR("Invalid parameter");
1401                 netconfig_error_invalid_parameter(context);
1402                 return FALSE;
1403         }
1404
1405         ret = _remove_configuration(config_id);
1406         if (ret != TRUE) {
1407                 /* no configuration or error */
1408                 ERR("No [%s] configuration", config_id);
1409                 netconfig_error_no_profile(context);
1410                 return FALSE;
1411         }
1412
1413         wifi_complete_remove_configuration(wifi, context);
1414         return ret;
1415 }
1416
1417 /* config field key / value */
1418 /*
1419  * [wifi_macaddress_config_id]
1420  * Name=name (mandatory)
1421  * SSID=SSID (mandatory)
1422  * Frequency=2462 (X)
1423  * Favorite=true (X)
1424  * AutoConnect=true (Default true)
1425  * Modified=2015-03-20 (X)
1426  * IPv4.method=manual (O)
1427  * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1428  * IPv6.method=auto (X)
1429  * IPv6.privacy=disabled (X)
1430  * IPv4.netmask_prefixlen=24 (X)
1431  * IPv4.local_address=192.0.0.1 (O)
1432  * IPv4.gateway=192.0.0.1 (O ? X ?)
1433  * Nameservers=192.168.43.22; (O)
1434  * Proxy.Method=manual (O)
1435  * Proxy.Servers=trst.com:8888; (O)
1436  */
1437 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1438                 const gchar *config_id, const gchar *key, const gchar *value)
1439 {
1440         gboolean ret = FALSE;
1441         gchar *keyfile_key = NULL;
1442
1443         g_return_val_if_fail(wifi != NULL, FALSE);
1444         g_return_val_if_fail(config_id != NULL, FALSE);
1445         g_return_val_if_fail(key != NULL, FALSE);
1446
1447         DBG("Key[%s] Value[%d]", key, value);
1448
1449         if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1450                 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1451                 if (!ret) {
1452                         ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1453                         netconfig_error_invalid_parameter(context);
1454                         return FALSE;
1455                 }
1456                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1457         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1458                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1459         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1460                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1461         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1462                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1463         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1464                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1465         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1466                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1467         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1468                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1469         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1470                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1471         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1472                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1473         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1474                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1475         } else {
1476                 ERR("Not supported key[%s]", key);
1477                 netconfig_error_invalid_parameter(context);
1478                 return FALSE;
1479         }
1480
1481         ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1482         if (!ret) {
1483                 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1484                 ret = FALSE;
1485         }
1486
1487         if (keyfile_key != NULL)
1488                 g_free(keyfile_key);
1489
1490         wifi_complete_set_config_field(wifi, context);
1491         return ret;
1492 }
1493
1494 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1495 {
1496         gboolean ret = FALSE;
1497         gchar *passphrase = NULL;
1498
1499         if ((wifi == NULL) || (config_id == NULL)) {
1500                 ERR("Invalid parameter");
1501                 netconfig_error_invalid_parameter(context);
1502                 return FALSE;
1503         }
1504
1505         ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1506         if (!ret) {
1507                 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1508                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1509                 return FALSE;
1510         }
1511
1512         wifi_complete_get_config_passphrase(wifi, context, passphrase);
1513         g_free(passphrase);
1514
1515         return ret;
1516 }
1517
1518 gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1519                 int frame_id, const gchar *vsie)
1520 {
1521         DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1522
1523         g_return_val_if_fail(wifi != NULL, FALSE);
1524         g_return_val_if_fail(vsie != NULL, FALSE);
1525
1526         gboolean ret = FALSE;
1527
1528         ret = _add_vsie(frame_id, vsie);
1529         if (!ret) {
1530                 DBG("Failed to add vsie: %s", vsie);
1531                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1532                 return ret;
1533         }
1534
1535         wifi_complete_add_vsie(wifi, context);
1536         return ret;
1537 }
1538
1539 gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1540                 int frame_id)
1541 {
1542         DBG("Frame ID: [%d]", frame_id);
1543
1544         g_return_val_if_fail(wifi != NULL, FALSE);
1545
1546         gboolean ret = FALSE;
1547         gchar *vsie = NULL;
1548
1549         ret = _get_vsie(frame_id, &vsie);
1550         if (!ret) {
1551                 DBG("Failed to get vsie for frame:[%d]", frame_id);
1552                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1553                 return ret;
1554         }
1555
1556         DBG("Received vsie: %s", vsie);
1557         wifi_complete_get_vsie(wifi, context, vsie);
1558
1559         return ret;
1560 }
1561
1562 gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
1563                 int frame_id, const gchar *vsie)
1564 {
1565         DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
1566
1567         g_return_val_if_fail(wifi != NULL, FALSE);
1568         g_return_val_if_fail(vsie != NULL, FALSE);
1569
1570         gboolean ret = FALSE;
1571
1572         ret = _remove_vsie(frame_id, vsie);
1573         if (!ret) {
1574                 DBG("Failed to remove vsie: %s", vsie);
1575                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1576                 return ret;
1577         }
1578
1579         wifi_complete_remove_vsie(wifi, context);
1580         return ret;
1581 }