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