Fix build error
[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
36 #define CONNMAN_STORAGE         "/var/lib/connman"
37
38 #define WIFI_SECURITY_NONE              "none"
39 #define WIFI_SECURITY_WEP               "wep"
40 #define WIFI_SECURITY_WPA_PSK   "psk"
41 #define WIFI_SECURITY_EAP               "ieee8021x"
42
43 #define WIFI_CONFIG_PREFIX      "wifi_"
44 #define MAC_ADDRESS_LENGTH              12
45 #define WIFI_PREFIX_LENGTH              MAC_ADDRESS_LENGTH + 6  /* wifi_485a3f2f506a_ */
46 #define PROFILE_PREFIX_LENGTH   WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
47
48 #define WIFI_MAC_ADD_LENGTH             17
49 #define WIFI_MAC_ADD_PATH               "/sys/class/net/wlan0/address"
50
51 struct wifi_eap_config {
52         gchar *anonymous_identity;
53         gchar *ca_cert;
54         gchar *client_cert;
55         gchar *private_key;
56         gchar *identity;
57         gchar *eap_type;
58         gchar *eap_auth_type;
59         gchar *subject_match;
60 };
61
62 struct wifi_config {
63         gchar *name;
64         gchar *ssid;
65         gchar *passphrase;
66         gchar *security_type;
67         gboolean favorite;
68         gboolean autoconnect;
69         gchar *is_hidden;
70         gchar *proxy_address;
71         struct wifi_eap_config *eap_config;
72         gchar *last_error;
73 };
74
75 static void __free_wifi_configuration(struct wifi_config *conf)
76 {
77         if (conf == NULL)
78                 return;
79
80         g_free(conf->name);
81         g_free(conf->ssid);
82         g_free(conf->passphrase);
83         g_free(conf->security_type);
84         g_free(conf->is_hidden);
85         g_free(conf->proxy_address);
86         g_free(conf->last_error);
87         if (conf->eap_config) {
88                 g_free(conf->eap_config->anonymous_identity);
89                 g_free(conf->eap_config->ca_cert);
90                 g_free(conf->eap_config->client_cert);
91                 g_free(conf->eap_config->private_key);
92                 g_free(conf->eap_config->identity);
93                 g_free(conf->eap_config->eap_type);
94                 g_free(conf->eap_config->eap_auth_type);
95                 g_free(conf->eap_config->subject_match);
96                 g_free(conf->eap_config);
97         }
98         g_free(conf);
99 }
100
101 static gboolean __get_mac_address(gchar **mac_address)
102 {
103         gchar *tmp_mac = NULL;
104         gchar *tmp = NULL;
105         gchar mac[13] = { 0, };
106         gint i = 0, j = 0;
107
108         if (TIZEN_TV) {
109                 FILE *fp = NULL;
110                 char buf[WIFI_MAC_ADD_LENGTH + 1];
111                 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
112                         fp = fopen(WIFI_MAC_ADD_PATH, "r");
113
114                 if (fp == NULL) {
115                         ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
116                         *mac_address = NULL;
117                         return FALSE;
118                 }
119
120                 if (fgets(buf, sizeof(buf), fp) == NULL) {
121                         ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
122                         *mac_address = NULL;
123                         fclose(fp);
124                         return FALSE;
125                 }
126                 tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
127                 if (tmp_mac == NULL) {
128                         ERR("malloc() failed");
129                         *mac_address = NULL;
130                         fclose(fp);
131                         return FALSE;
132                 }
133                 memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
134                 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
135                 fclose(fp);
136         } else {
137                 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
138                 if (tmp_mac == NULL) {
139                         ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
140                         *mac_address = NULL;
141                         return FALSE;
142                 }
143         }
144         tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
145         free(tmp_mac);
146         while (tmp && tmp[i]) {
147                 if (tmp[i] != ':')
148                         mac[j++] = tmp[i];
149                 i++;
150         }
151         mac[12] = '\0';
152         *mac_address = g_strdup(mac);
153
154         return TRUE;
155 }
156
157 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
158 {
159         gchar *mac_address = NULL;
160         gchar *g_name = NULL;
161         gboolean ret = FALSE;
162
163         ret = __get_mac_address(&mac_address);
164         if ((ret != TRUE) || (strlen(mac_address) == 0)) {
165                 ERR("Cannot get WIFI MAC address");
166                 return FALSE;
167         }
168
169         g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
170         if (g_name == NULL) {
171                 g_free(mac_address);
172                 return FALSE;
173         }
174
175         *group_name = g_strdup(g_name);
176
177         g_free(mac_address);
178         g_free(g_name);
179
180         return TRUE;
181 }
182
183 static gboolean __get_security_type(const gchar *config_id, gchar **type)
184 {
185         if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
186                 *type = g_strdup(WIFI_SECURITY_NONE);
187         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
188                 *type = g_strdup(WIFI_SECURITY_WEP);
189         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
190                 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
191         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
192                 *type = g_strdup(WIFI_SECURITY_EAP);
193         } else {
194                 *type = NULL;
195                 return FALSE;
196         }
197
198         return TRUE;
199 }
200
201 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
202 {
203         *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
204         if (*config_id == NULL) {
205                 ERR("OOM");
206                 return FALSE;
207         }
208
209         return TRUE;
210 }
211
212
213 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
214 {
215         GKeyFile *keyfile = NULL;
216         gchar *path;
217
218         path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
219
220         keyfile = netconfig_keyfile_load(path);
221         if (keyfile == NULL) {
222                 ERR("keyfile[%s] is NULL", path);
223         }
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         ret = __get_security_type(config_id, &config->security_type);
293         if (ret != TRUE) {
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 /* dbus method */
598 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
599 {
600         guint i = 0;
601         GSList *config_ids = NULL;
602         guint length;
603         gchar **result = NULL;
604
605         g_return_val_if_fail(wifi != NULL, FALSE);
606
607         config_ids = _get_list();
608         if (config_ids == NULL) {
609                 netconfig_error_no_profile(context);
610                 ERR("Fail to get config list");
611                 return FALSE;
612         }
613
614         length = g_slist_length(config_ids);
615         result = g_new0(gchar *, length + 1);
616         for (i = 0; i < length; i++) {
617                 gchar *config_id = g_slist_nth_data(config_ids, i);
618                 result[i] = g_strdup(config_id);
619         }
620
621         config_ids = g_slist_nth(config_ids, 0);
622         g_slist_free_full(config_ids, g_free);
623
624         wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
625
626         for (i = 0; i < length; i++)
627                 if (result[i])
628                         g_free(result[i]);
629
630         if (result)
631                 g_free(result);
632
633         return TRUE;
634 }
635
636 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
637                 const gchar *config_id)
638 {
639         gboolean ret = FALSE;
640         GVariantBuilder *b = NULL;
641         struct wifi_config *conf = NULL;
642
643         g_return_val_if_fail(wifi != NULL, FALSE);
644
645         conf = g_new0(struct wifi_config, 1);
646
647         ret = _load_configuration(config_id, conf);
648         if (ret != TRUE) {
649                 g_free(conf);
650                 ERR("Fail to _load_configuration");
651                 netconfig_error_no_profile(context);
652                 return FALSE;
653         }
654
655         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
656         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
657         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
658         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
659
660         if (conf->proxy_address != NULL)
661                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
662         else
663                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
664
665         if (conf->last_error != NULL)
666                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
667         else
668                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
669
670         g_free(conf->proxy_address);
671         g_free(conf->last_error);
672         g_free(conf->name);
673         g_free(conf->security_type);
674         g_free(conf->is_hidden);
675         g_free(conf);
676
677         wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
678         g_variant_builder_unref(b);
679         return TRUE;
680 }
681
682 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
683                 const gchar *config_id, GVariant *configuration)
684 {
685         gboolean ret = FALSE;
686         struct wifi_config *conf = NULL;
687         GKeyFile *keyfile = NULL;
688         GVariantIter *iter;
689         GVariant *value;
690         gchar *field;
691         gchar *group_name = NULL;
692
693         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
694                 ERR("Invalid parameter");
695                 netconfig_error_invalid_parameter(context);
696                 return FALSE;
697         }
698
699         conf = g_new0(struct wifi_config, 1);
700
701         g_variant_get(configuration, "a{sv}", &iter);
702         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
703                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
704                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
705                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
706                                 DBG("name [%s]", conf->name);
707                         } else {
708                                 conf->name = NULL;
709                         }
710                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
711                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
712                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
713                                 DBG("ssid [%s]", conf->ssid);
714                         } else {
715                                 conf->ssid = NULL;
716                         }
717                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
718                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
719                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
720                                 DBG("passphrase []");
721                         } else {
722                                 conf->passphrase = NULL;
723                         }
724                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
725                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
726                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
727                                 DBG("is_hidden [%s]", conf->is_hidden);
728                         } else {
729                                 conf->is_hidden = NULL;
730                         }
731                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
732                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
733                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
734                                 DBG("proxy_address [%s]", conf->proxy_address);
735                         } else {
736                                 conf->proxy_address = NULL;
737                         }
738                 }
739         }
740         conf->favorite = TRUE;
741         conf->autoconnect = TRUE;
742
743         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
744         if (ret != TRUE) {
745                 g_free(conf->name);
746                 g_free(conf->ssid);
747                 g_free(conf->passphrase);
748                 g_free(conf->is_hidden);
749                 g_free(conf->proxy_address);
750                 g_free(conf);
751                 ERR("Fail to get_wifi_config_group_name");
752                 return FALSE;
753         }
754
755         keyfile = g_key_file_new();
756         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
757         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
758
759         if (conf->passphrase != NULL)
760                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
761
762         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
763         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
764
765         /* Optional field */
766         if (conf->proxy_address != NULL) {
767                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
768                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
769         }
770
771         if (conf->is_hidden != NULL) {
772                 gboolean hidden = FALSE;
773                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
774                         hidden = TRUE;
775                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
776         }
777
778         ret = _save_configuration(config_id, keyfile);
779         if (ret == TRUE) {
780                 INFO("Success to save configuration [%s]", config_id);
781                 wifi_complete_save_configuration(wifi, context);
782         } else {
783                 INFO("Fail to save configuration [%s]", config_id);
784                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
785         }
786
787         g_key_file_free(keyfile);
788         g_free(conf->name);
789         g_free(conf->ssid);
790         g_free(conf->passphrase);
791         g_free(conf->is_hidden);
792         g_free(conf->proxy_address);
793         g_free(conf);
794
795         g_variant_iter_free(iter);
796
797         return ret;
798 }
799
800 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
801                 const gchar *config_id)
802 {
803         gboolean ret = FALSE;
804         GVariantBuilder *b = NULL;
805         struct wifi_config *conf = NULL;
806
807         g_return_val_if_fail(wifi != NULL, FALSE);
808
809         conf = g_new0(struct wifi_config, 1);
810         conf->eap_config = g_new0(struct wifi_eap_config, 1);
811
812         ret = _load_configuration(config_id, conf);
813         if (ret != TRUE) {
814                 g_free(conf->eap_config);
815                 g_free(conf);
816                 ERR("Fail to _load_configuration");
817                 netconfig_error_no_profile(context);
818                 return FALSE;
819         }
820
821         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
822         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
823         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
824         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
825         if (conf->proxy_address != NULL)
826                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
827         else
828                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
829
830         if (conf->last_error != NULL)
831                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
832         else
833                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
834
835         if (conf->eap_config != NULL) {
836                 if (conf->eap_config->anonymous_identity != NULL)
837                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
838                 else
839                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
840
841                 if (conf->eap_config->ca_cert != NULL)
842                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
843                 else
844                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
845
846                 if (conf->eap_config->client_cert != NULL)
847                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
848                 else
849                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
850
851                 if (conf->eap_config->private_key != NULL)
852                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
853                 else
854                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
855
856                 if (conf->eap_config->identity != NULL)
857                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
858                 else
859                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
860
861                 if (conf->eap_config->eap_type != NULL)
862                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
863                 else
864                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
865
866                 if (conf->eap_config->eap_auth_type != NULL)
867                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
868                 else
869                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
870
871                 if (conf->eap_config->subject_match != NULL)
872                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
873                 else
874                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
875         }
876
877         __free_wifi_configuration(conf);
878
879         wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
880         g_variant_builder_unref(b);
881         return TRUE;
882 }
883
884 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
885                 const gchar *config_id, GVariant *configuration)
886 {
887         gboolean ret = FALSE;
888         struct wifi_config *conf = NULL;
889         GKeyFile *keyfile = NULL;
890         GVariantIter *iter;
891         GVariant *value;
892         gchar *field;
893         gchar *group_name = NULL;
894
895         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
896                 ERR("Invalid parameter");
897                 netconfig_error_invalid_parameter(context);
898                 return FALSE;
899         }
900
901         conf = g_new0(struct wifi_config, 1);
902         conf->eap_config = g_new0(struct wifi_eap_config, 1);
903
904         g_variant_get(configuration, "a{sv}", &iter);
905         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
906                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
907                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
908                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
909                                 DBG("name [%s]", conf->name);
910                         } else {
911                                 conf->name = NULL;
912                         }
913                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
914                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
915                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
916                                 DBG("ssid [%s]", conf->ssid);
917                         } else {
918                                 conf->ssid = NULL;
919                         }
920                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
921                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
922                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
923                                 DBG("passphrase [%s]", conf->passphrase);
924                         } else {
925                                 conf->passphrase = NULL;
926                         }
927                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
928                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
929                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
930                                 DBG("is_hidden [%s]", conf->is_hidden);
931                         } else {
932                                 conf->is_hidden = NULL;
933                         }
934                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
935                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
936                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
937                                 DBG("proxy_address [%s]", conf->proxy_address);
938                         } else {
939                                 conf->proxy_address = NULL;
940                         }
941                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
942                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
943                                 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
944                                 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
945                         } else {
946                                 conf->eap_config->anonymous_identity = NULL;
947                         }
948                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
949                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
950                                 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
951                                 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
952                         } else {
953                                 conf->eap_config->ca_cert = NULL;
954                         }
955                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
956                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
957                                 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
958                                 DBG("client_cert [%s]", conf->eap_config->client_cert);
959                         } else {
960                                 conf->eap_config->client_cert = NULL;
961                         }
962                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
963                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
964                                 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
965                                 DBG("private_key [%s]", conf->eap_config->private_key);
966                         } else {
967                                 conf->eap_config->private_key = NULL;
968                         }
969                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
970                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
971                                 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
972                                 DBG("identity [%s]", conf->eap_config->identity);
973                         } else {
974                                 conf->eap_config->identity = NULL;
975                         }
976                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
977                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
978                                 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
979                                 DBG("eap_type [%s]", conf->eap_config->eap_type);
980                         } else {
981                                 conf->eap_config->eap_type = NULL;
982                         }
983                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
984                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
985                                 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
986                                 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
987                         } else {
988                                 conf->eap_config->eap_auth_type = NULL;
989                         }
990                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
991                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
992                                 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
993                                 DBG("subject_match [%s]", conf->eap_config->subject_match);
994                         } else {
995                                 conf->eap_config->subject_match = NULL;
996                         }
997                 }
998         }
999         conf->favorite = TRUE;
1000         conf->autoconnect = TRUE;
1001
1002         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1003         if (ret != TRUE) {
1004                 __free_wifi_configuration(conf);
1005                 ERR("Fail to get_wifi_config_group_name");
1006                 return FALSE;
1007         }
1008
1009         keyfile = g_key_file_new();
1010         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1011         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1012
1013         if (conf->passphrase != NULL)
1014                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1015
1016         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1017         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1018
1019         /* Optional field */
1020         if (conf->proxy_address != NULL) {
1021                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1022                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1023         }
1024
1025         if (conf->is_hidden != NULL) {
1026                 gboolean hidden = FALSE;
1027                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1028                         hidden = TRUE;
1029                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1030         }
1031
1032         if (conf->eap_config->anonymous_identity != NULL)
1033                 g_key_file_set_string(keyfile, group_name,
1034                         WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1035
1036         if (conf->eap_config->ca_cert != NULL)
1037                 g_key_file_set_string(keyfile, group_name,
1038                         WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1039
1040         if (conf->eap_config->client_cert != NULL)
1041                 g_key_file_set_string(keyfile, group_name,
1042                         WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1043
1044         if (conf->eap_config->private_key != NULL)
1045                 g_key_file_set_string(keyfile, group_name,
1046                         WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1047
1048         if (conf->eap_config->identity != NULL)
1049                 g_key_file_set_string(keyfile, group_name,
1050                         WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1051
1052         if (conf->eap_config->eap_type != NULL)
1053                 g_key_file_set_string(keyfile, group_name,
1054                         WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1055
1056         if (conf->eap_config->eap_auth_type != NULL)
1057                 g_key_file_set_string(keyfile, group_name,
1058                         WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1059
1060         if (conf->eap_config->subject_match != NULL)
1061                 g_key_file_set_string(keyfile, group_name,
1062                         WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1063
1064         ret = _save_configuration(config_id, keyfile);
1065         if (ret == TRUE) {
1066                 INFO("Success to save eap configuration [%s]", config_id);
1067                 wifi_complete_save_eap_configuration(wifi, context);
1068         } else {
1069                 INFO("Fail to save eap configuration [%s]", config_id);
1070                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1071         }
1072
1073         g_key_file_free(keyfile);
1074         __free_wifi_configuration(conf);
1075
1076         g_variant_iter_free(iter);
1077
1078         return ret;
1079 }
1080
1081 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1082 {
1083         gboolean ret = FALSE;
1084
1085         if ((wifi == NULL) || (config_id == NULL)) {
1086                 ERR("Invalid parameter");
1087                 netconfig_error_invalid_parameter(context);
1088                 return FALSE;
1089         }
1090
1091         ret = _remove_configuration(config_id);
1092         if (ret != TRUE) {
1093                 /* no configuration or error */
1094                 ERR("No [%s] configuration", config_id);
1095                 netconfig_error_no_profile(context);
1096                 return FALSE;
1097         }
1098
1099         wifi_complete_remove_configuration(wifi, context);
1100         return ret;
1101 }
1102
1103 /* config field key / value */
1104 /*
1105  * [wifi_macaddress_config_id]
1106  * Name=name (mandatory)
1107  * SSID=SSID (mandatory)
1108  * Frequency=2462 (X)
1109  * Favorite=true (X)
1110  * AutoConnect=true (Default true)
1111  * Modified=2015-03-20 (X)
1112  * IPv4.method=manual (O)
1113  * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1114  * IPv6.method=auto (X)
1115  * IPv6.privacy=disabled (X)
1116  * IPv4.netmask_prefixlen=24 (X)
1117  * IPv4.local_address=192.0.0.1 (O)
1118  * IPv4.gateway=192.0.0.1 (O ? X ?)
1119  * Nameservers=192.168.43.22; (O)
1120  * Proxy.Method=manual (O)
1121  * Proxy.Servers=trst.com:8888; (O)
1122  */
1123 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1124                 const gchar *config_id, const gchar *key, const gchar *value)
1125 {
1126         gboolean ret = FALSE;
1127         gchar *keyfile_key = NULL;
1128
1129         g_return_val_if_fail(wifi != NULL, FALSE);
1130         g_return_val_if_fail(config_id != NULL, FALSE);
1131         g_return_val_if_fail(key != NULL, FALSE);
1132
1133         DBG("Key[%s] Value[%d]", key, value);
1134
1135         if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1136                 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1137                 if (!ret) {
1138                         ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1139                         netconfig_error_invalid_parameter(context);
1140                         return FALSE;
1141                 }
1142                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1143         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1144                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1145         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1146                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1147         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1148                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1149         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1150                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1151         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1152                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1153         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1154                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1155         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1156                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1157         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1158                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1159         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1160                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1161         } else {
1162                 ERR("Not supported key[%s]", key);
1163                 netconfig_error_invalid_parameter(context);
1164                 return FALSE;
1165         }
1166
1167         ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1168         if (!ret) {
1169                 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1170                 ret = FALSE;
1171         }
1172
1173         if (keyfile_key != NULL)
1174                 g_free(keyfile_key);
1175
1176         wifi_complete_set_config_field(wifi, context);
1177         return ret;
1178 }
1179
1180 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1181 {
1182         gboolean ret = FALSE;
1183         gchar *passphrase = NULL;
1184
1185         if ((wifi == NULL) || (config_id == NULL)) {
1186                 ERR("Invalid parameter");
1187                 netconfig_error_invalid_parameter(context);
1188                 return FALSE;
1189         }
1190
1191         ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1192         if (!ret) {
1193                 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1194                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1195                 return FALSE;
1196         }
1197
1198         wifi_complete_get_config_passphrase(wifi, context, passphrase);
1199         g_free(passphrase);
1200
1201         return ret;
1202 }