1f479a3d1a9c4a62601c5c8d28906eba84fd0bd7
[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 <string.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <sys/stat.h>
25 #include <glib.h>
26 #include <unistd.h>
27
28 #include <vconf.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "neterror.h"
33 #include "wifi-config.h"
34
35 #define CONNMAN_STORAGE         "/var/lib/connman"
36
37 #define WIFI_SECURITY_NONE              "none"
38 #define WIFI_SECURITY_WEP               "wep"
39 #define WIFI_SECURITY_WPA_PSK   "psk"
40 #define WIFI_SECURITY_EAP               "ieee8021x"
41
42 #define WIFI_CONFIG_PREFIX      "wifi_"
43 #define MAC_ADDRESS_LENGTH              12
44 #define WIFI_PREFIX_LENGTH              MAC_ADDRESS_LENGTH + 6  /* wifi_485a3f2f506a_ */
45 #define PROFILE_PREFIX_LENGTH   WIFI_PREFIX_LENGTH + 21 /* /net/connman/service/wifi_485a3f2f506a_ */
46
47 #define WIFI_MAC_ADD_LENGTH             17
48 #define WIFI_MAC_ADD_PATH               "/sys/class/net/wlan0/address"
49
50 struct wifi_eap_config {
51         gchar *anonymous_identity;
52         gchar *ca_cert;
53         gchar *client_cert;
54         gchar *private_key;
55         gchar *identity;
56         gchar *eap_type;
57         gchar *eap_auth_type;
58         gchar *subject_match;
59 };
60
61 struct wifi_config {
62         gchar *name;
63         gchar *ssid;
64         gchar *passphrase;
65         gchar *security_type;
66         gboolean favorite;
67         gboolean autoconnect;
68         gchar *is_hidden;
69         gchar *proxy_address;
70         struct wifi_eap_config *eap_config;
71         gchar *last_error;
72 };
73
74 static void __free_wifi_configuration(struct wifi_config *conf)
75 {
76         if (conf == NULL)
77                 return;
78
79         g_free(conf->name);
80         g_free(conf->ssid);
81         g_free(conf->passphrase);
82         g_free(conf->security_type);
83         g_free(conf->is_hidden);
84         g_free(conf->proxy_address);
85         g_free(conf->last_error);
86         if (conf->eap_config) {
87                 g_free(conf->eap_config->anonymous_identity);
88                 g_free(conf->eap_config->ca_cert);
89                 g_free(conf->eap_config->client_cert);
90                 g_free(conf->eap_config->private_key);
91                 g_free(conf->eap_config->identity);
92                 g_free(conf->eap_config->eap_type);
93                 g_free(conf->eap_config->eap_auth_type);
94                 g_free(conf->eap_config->subject_match);
95                 g_free(conf->eap_config);
96         }
97         g_free(conf);
98 }
99
100 static gboolean __get_mac_address(gchar **mac_address)
101 {
102         gchar *tmp_mac = NULL;
103         gchar *tmp = NULL;
104         gchar mac[13] = { 0, };
105         gint i = 0, j = 0;
106
107         if (TIZEN_TV) {
108                 FILE *fp = NULL;
109                 char buf[WIFI_MAC_ADD_LENGTH + 1];
110                 if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
111                         fp = fopen(WIFI_MAC_ADD_PATH, "r");
112
113                 if (fp == NULL) {
114                         ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
115                         *mac_address = NULL;
116                         return FALSE;
117                 }
118
119                 if (fgets(buf, sizeof(buf), fp) == NULL) {
120                         ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
121                         *mac_address = NULL;
122                         fclose(fp);
123                         return FALSE;
124                 }
125                 tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
126                 if (tmp_mac == NULL) {
127                         ERR("malloc() failed");
128                         *mac_address = NULL;
129                         fclose(fp);
130                         return FALSE;
131                 }
132                 g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
133                 fclose(fp);
134         } else {
135                 tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
136                 if (tmp_mac == NULL) {
137                         ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
138                         *mac_address = NULL;
139                         return FALSE;
140                 }
141         }
142         tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
143         g_free(tmp_mac);
144         while (tmp && tmp[i]) {
145                 if (tmp[i] != ':')
146                         mac[j++] = tmp[i];
147                 i++;
148         }
149         mac[12] = '\0';
150         *mac_address = g_strdup(mac);
151
152         return TRUE;
153 }
154
155 static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gchar **group_name)
156 {
157         gchar *mac_address = NULL;
158         gchar *g_name = NULL;
159         gboolean ret = FALSE;
160
161         ret = __get_mac_address(&mac_address);
162         if ((ret != TRUE) || (strlen(mac_address) == 0)) {
163                 ERR("Cannot get WIFI MAC address");
164                 return FALSE;
165         }
166
167         g_name = g_strdup_printf("%s%s_%s", prefix, mac_address, config_id);
168         if (g_name == NULL) {
169                 g_free(mac_address);
170                 return FALSE;
171         }
172
173         *group_name = g_strdup(g_name);
174
175         g_free(mac_address);
176         g_free(g_name);
177
178         return TRUE;
179 }
180
181 static gboolean __get_security_type(const gchar *config_id, gchar **type)
182 {
183         if (g_str_has_suffix(config_id, WIFI_SECURITY_NONE) == TRUE) {
184                 *type = g_strdup(WIFI_SECURITY_NONE);
185         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WEP) == TRUE) {
186                 *type = g_strdup(WIFI_SECURITY_WEP);
187         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_WPA_PSK) == TRUE) {
188                 *type = g_strdup(WIFI_SECURITY_WPA_PSK);
189         } else if (g_str_has_suffix(config_id, WIFI_SECURITY_EAP) == TRUE) {
190                 *type = g_strdup(WIFI_SECURITY_EAP);
191         } else {
192                 *type = NULL;
193                 return FALSE;
194         }
195
196         return TRUE;
197 }
198
199 static gboolean __get_config_id(const gchar *profile, gchar **config_id)
200 {
201         *config_id = g_strdup(profile + PROFILE_PREFIX_LENGTH);
202         if (*config_id == NULL) {
203                 ERR("OOM");
204                 return FALSE;
205         }
206
207         return TRUE;
208 }
209
210
211 static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
212 {
213         GKeyFile *keyfile = NULL;
214         gchar *path;
215
216         path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
217
218         keyfile = netconfig_keyfile_load(path);
219         if (keyfile == NULL) {
220                 ERR("keyfile[%s] is NULL", path);
221         }
222
223         g_free(path);
224
225         return keyfile;
226 }
227
228 static gboolean __remove_file(const gchar *pathname, const gchar *filename)
229 {
230         gboolean ret = FALSE;
231         gchar *path;
232
233         path = g_strdup_printf("%s/%s", pathname, filename);
234         if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) {
235                 ret = TRUE;
236         } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) {
237                 unlink(path);
238                 ret = TRUE;
239         }
240
241         g_free(path);
242         return ret;
243 }
244
245 static gboolean __remove_configuration(const gchar *pathname)
246 {
247         int ret = 0;
248
249         if (__remove_file(pathname, "settings") != TRUE) {
250                 ERR("Cannot remove [%s/settings]", pathname);
251                 return FALSE;
252         }
253         if (__remove_file(pathname, "data") != TRUE) {
254                 ERR("Cannot remove [%s/data]", pathname);
255                 return FALSE;
256         }
257
258         ret = rmdir(pathname);
259         if (ret == -1) {
260                 ERR("Cannot remove [%s]", pathname);
261                 return FALSE;
262         }
263
264         return TRUE;
265 }
266
267 static gboolean _load_configuration(const gchar *config_id, struct wifi_config *config)
268 {
269         GKeyFile *keyfile;
270         gchar *group_name;
271         gboolean hidden = FALSE;
272         gboolean ret = FALSE;
273
274         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
275         if (ret != TRUE) {
276                 ERR("Fail to get_wifi_config_group_name");
277                 return FALSE;
278         }
279
280         keyfile = __get_configuration_keyfile(group_name);
281         if (keyfile == NULL) {
282                 ERR("Fail to __get_configuration_keyfile[%s]", group_name);
283                 g_free(group_name);
284                 return FALSE;
285         }
286
287         config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
288         DBG("name [%s]", config->name);
289
290         ret = __get_security_type(config_id, &config->security_type);
291         if (ret != TRUE) {
292                 ERR("Fail to _get_security_type");
293                 g_key_file_free(keyfile);
294                 g_free(group_name);
295                 return FALSE;
296         }
297         DBG("security_type [%s]", config->security_type);
298
299         config->proxy_address = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
300         if (config->proxy_address)
301                 DBG("proxy_address [%s]", config->proxy_address);
302
303         hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
304         if (hidden)
305                 config->is_hidden = g_strdup("TRUE");
306         else
307                 config->is_hidden = g_strdup("FALSE");
308         DBG("is_hidden [%s]", config->is_hidden);
309
310         if (g_strcmp0(config->security_type, WIFI_SECURITY_EAP) == 0) {
311                 config->eap_config->anonymous_identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
312                 config->eap_config->ca_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
313                 config->eap_config->client_cert = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
314                 config->eap_config->private_key = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
315                 config->eap_config->identity = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
316                 config->eap_config->eap_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
317                 config->eap_config->eap_auth_type = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
318                 config->eap_config->subject_match = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
319
320                 if (config->eap_config->anonymous_identity)
321                         DBG("anonymous_identity [%s]", config->eap_config->anonymous_identity);
322                 if (config->eap_config->ca_cert)
323                         DBG("ca_cert [%s]", config->eap_config->ca_cert);
324                 if (config->eap_config->client_cert)
325                         DBG("client_cert [%s]", config->eap_config->client_cert);
326                 if (config->eap_config->private_key)
327                         DBG("private_key [%s]", config->eap_config->private_key);
328                 if (config->eap_config->identity)
329                         DBG("identity [%s]", config->eap_config->identity);
330                 if (config->eap_config->eap_type)
331                         DBG("eap_type [%s]", config->eap_config->eap_type);
332                 if (config->eap_config->eap_auth_type)
333                         DBG("eap_auth_type [%s]", config->eap_config->eap_auth_type);
334                 if (config->eap_config->subject_match)
335                         DBG("subject_match [%s]", config->eap_config->subject_match);
336         }
337
338         config->last_error = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
339         if (config->last_error)
340                 DBG("last_error [%s]", config->last_error);
341
342         g_key_file_free(keyfile);
343         g_free(group_name);
344
345         return TRUE;
346 }
347
348 static gboolean _save_configuration(const gchar *config_id, GKeyFile *keyfile)
349 {
350         gchar *dir;
351         gchar *path;
352         gchar *group_name;
353         gboolean ret = FALSE;
354
355         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
356         if (ret != TRUE) {
357                 ERR("Fail to get_wifi_config_group_name");
358                 return FALSE;
359         }
360
361         dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
362         if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
363                 if (__remove_configuration(dir) != TRUE) {
364                         ERR("[%s] is existed, but cannot remove", dir);
365                         g_free(group_name);
366                         g_free(dir);
367                         return FALSE;
368                 }
369         }
370
371         if (mkdir(dir, (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
372                 ERR("Cannot mkdir %s", dir);
373                 g_free(group_name);
374                 g_free(dir);
375                 return FALSE;
376         }
377
378         path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
379         netconfig_keyfile_save(keyfile, path);
380         g_free(group_name);
381         g_free(dir);
382         g_free(path);
383
384         return TRUE;
385 }
386
387 static gboolean _remove_configuration(const gchar *config_id)
388 {
389         gboolean ret = FALSE;
390         gchar *dir;
391         gchar *group_name;
392
393         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
394         if (ret != TRUE) {
395                 ERR("Fail to get_wifi_config_group_name");
396                 return FALSE;
397         }
398
399         dir = g_strdup_printf(CONNMAN_STORAGE "/%s", group_name);
400         if (g_file_test(dir, G_FILE_TEST_IS_DIR) == TRUE) {
401                 if (__remove_configuration(dir) != TRUE) {
402                         ERR("[%s] is existed, but cannot remove", dir);
403                         ret = FALSE;
404                 }
405                 INFO("Success to remove [%s]", dir);
406                 ret = TRUE;
407         } else {
408                 ERR("[%s] is not existed", dir);
409                 ret = FALSE;
410         }
411
412         g_free(group_name);
413         g_free(dir);
414
415         return ret;
416 }
417
418
419 static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar *value)
420 {
421         gboolean ret = TRUE;
422         GKeyFile *keyfile;
423         gchar *group_name;
424
425         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
426         if (ret != TRUE) {
427                 ERR("Fail to get_wifi_config_group_name");
428                 return FALSE;
429         }
430         DBG("group_name %s", group_name);
431
432         keyfile = __get_configuration_keyfile(group_name);
433         if (keyfile == NULL) {
434                 ERR("Fail to __get_configuration_keyfile");
435                 g_free(group_name);
436                 return FALSE;
437         }
438
439         if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
440                 g_key_file_set_string(keyfile, group_name, key, value);
441         } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
442                 g_key_file_set_string(keyfile, group_name, key, value);
443         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
444                 gboolean hidden = FALSE;
445                 if (g_strcmp0(value, "TRUE") == 0)
446                         hidden = TRUE;
447                 g_key_file_set_boolean(keyfile, group_name, key, hidden);
448         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
449                 g_key_file_set_string(keyfile, group_name, key, value);
450         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
451                 g_key_file_set_string(keyfile, group_name, key, value);
452         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
453                 g_key_file_set_string(keyfile, group_name, key, value);
454         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
455                 g_key_file_set_string(keyfile, group_name, key, value);
456         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
457                 g_key_file_set_string(keyfile, group_name, key, value);
458         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
459                 g_key_file_set_string(keyfile, group_name, key, value);
460         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
461                 g_key_file_set_string(keyfile, group_name, key, value);
462         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
463                 g_key_file_set_string(keyfile, group_name, key, value);
464         } else {
465                 ERR("key[%s] is not supported", key);
466                 ret = FALSE;
467         }
468
469         _save_configuration(config_id, keyfile);
470
471         g_key_file_free(keyfile);
472         g_free(group_name);
473
474         return ret;
475 }
476
477 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
478 {
479         GKeyFile *keyfile;
480         gchar *group_name;
481         gchar *val = NULL;
482         gboolean hidden = FALSE;
483         gboolean ret = FALSE;
484
485         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
486         if (ret != TRUE) {
487                 ERR("Fail to get_wifi_config_group_name");
488                 return FALSE;
489         }
490         DBG("group_name %s", group_name);
491
492         keyfile = __get_configuration_keyfile(group_name);
493         if (keyfile == NULL) {
494                 ERR("Fail to __get_configuration_keyfile");
495                 g_free(group_name);
496                 return FALSE;
497         }
498
499         if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
500                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
501         } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
502                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
503         } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
504                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
505         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
506                 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
507                 if (hidden)
508                         val = g_strdup("TRUE");
509                 else
510                         val = g_strdup("FALSE");
511         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
512                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
513         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
514                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
515         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
516                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
517         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
518                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
519         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
520                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
521         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
522                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
523         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
524                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
525         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
526                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
527         } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
528                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
529         } else {
530                 ERR("Invalid key[%s]", key);
531                 val = g_strdup("NOTSUPPORTED");
532         }
533
534         *value = g_strdup(val);
535         g_free(val);
536
537         g_key_file_free(keyfile);
538         g_free(group_name);
539
540         return TRUE;
541 }
542
543 static GSList *_get_list(void)
544 {
545         GSList *list = NULL;
546         struct dirent de;
547         struct dirent *dp = NULL;
548         DIR *dir;
549
550         dir = opendir(CONNMAN_STORAGE);
551         if (dir == NULL) {
552                 ERR("Cannot open dir %s", CONNMAN_STORAGE);
553                 return NULL;
554         }
555
556         while ((readdir_r(dir, &de, &dp) == 0) && (dp != NULL)) {
557                 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
558                                 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
559                         continue;
560                 }
561                 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
562                 list = g_slist_append(list, g_strdup(config_id));
563                 g_free(config_id);
564         }
565         closedir(dir);
566
567         return list;
568 }
569
570 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
571 {
572         gboolean ret = FALSE;
573         gchar *val = NULL;
574
575         if ((service_profile == NULL) || (config_id == NULL)) {
576                 ERR("Invalid parameter");
577                 return FALSE;
578         }
579
580         ret = __get_config_id(service_profile, &val);
581         *config_id = g_strdup(val);
582         g_free(val);
583
584         return ret;
585 }
586
587 gboolean wifi_config_remove_configuration(const gchar *config_id)
588 {
589         gboolean ret = FALSE;
590
591         ret = _remove_configuration(config_id);
592
593         return ret;
594 }
595
596 /* dbus method */
597 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
598 {
599         guint i = 0;
600         GSList *config_ids = NULL;
601         guint length;
602         gchar **result = NULL;
603
604         g_return_val_if_fail(wifi != NULL, FALSE);
605
606         config_ids = _get_list();
607         if (config_ids == NULL) {
608                 netconfig_error_no_profile(context);
609                 ERR("Fail to get config list");
610                 return FALSE;
611         }
612
613         length = g_slist_length(config_ids);
614         result = g_new0(gchar *, length + 1);
615         for (i = 0; i < length; i++) {
616                 gchar *config_id = g_slist_nth_data(config_ids, i);
617                 result[i] = g_strdup(config_id);
618         }
619
620         config_ids = g_slist_nth(config_ids, 0);
621         g_slist_free_full(config_ids, g_free);
622
623         wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
624
625         for (i = 0; i < length; i++)
626                 if (result[i])
627                         g_free(result[i]);
628
629         if (result)
630                 g_free(result);
631
632         return TRUE;
633 }
634
635 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
636                 const gchar *config_id)
637 {
638         gboolean ret = FALSE;
639         GVariantBuilder *b = NULL;
640         struct wifi_config *conf = NULL;
641
642         g_return_val_if_fail(wifi != NULL, FALSE);
643
644         conf = g_new0(struct wifi_config, 1);
645
646         ret = _load_configuration(config_id, conf);
647         if (ret != TRUE) {
648                 g_free(conf);
649                 ERR("Fail to _load_configuration");
650                 netconfig_error_no_profile(context);
651                 return FALSE;
652         }
653
654         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
655         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
656         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
657         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
658
659         if (conf->proxy_address != NULL)
660                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
661         else
662                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
663
664         if (conf->last_error != NULL)
665                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
666         else
667                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
668
669         g_free(conf->proxy_address);
670         g_free(conf->last_error);
671         g_free(conf->name);
672         g_free(conf->security_type);
673         g_free(conf->is_hidden);
674         g_free(conf);
675
676         wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
677         g_variant_builder_unref(b);
678         return TRUE;
679 }
680
681 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
682                 const gchar *config_id, GVariant *configuration)
683 {
684         gboolean ret = FALSE;
685         struct wifi_config *conf = NULL;
686         GKeyFile *keyfile = NULL;
687         GVariantIter *iter;
688         GVariant *value;
689         gchar *field;
690         gchar *group_name = NULL;
691
692         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
693                 ERR("Invalid parameter");
694                 netconfig_error_invalid_parameter(context);
695                 return FALSE;
696         }
697
698         conf = g_new0(struct wifi_config, 1);
699
700         g_variant_get(configuration, "a{sv}", &iter);
701         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
702                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
703                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
704                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
705                                 DBG("name [%s]", conf->name);
706                         } else {
707                                 conf->name = NULL;
708                         }
709                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
710                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
711                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
712                                 DBG("ssid [%s]", conf->ssid);
713                         } else {
714                                 conf->ssid = NULL;
715                         }
716                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
717                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
718                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
719                                 DBG("passphrase []");
720                         } else {
721                                 conf->passphrase = NULL;
722                         }
723                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
724                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
725                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
726                                 DBG("is_hidden [%s]", conf->is_hidden);
727                         } else {
728                                 conf->is_hidden = NULL;
729                         }
730                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
731                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
732                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
733                                 DBG("proxy_address [%s]", conf->proxy_address);
734                         } else {
735                                 conf->proxy_address = NULL;
736                         }
737                 }
738         }
739         conf->favorite = TRUE;
740         conf->autoconnect = TRUE;
741
742         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
743         if (ret != TRUE) {
744                 g_free(conf->name);
745                 g_free(conf->ssid);
746                 g_free(conf->passphrase);
747                 g_free(conf->is_hidden);
748                 g_free(conf->proxy_address);
749                 g_free(conf);
750                 ERR("Fail to get_wifi_config_group_name");
751                 return FALSE;
752         }
753
754         keyfile = g_key_file_new();
755         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
756         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
757
758         if (conf->passphrase != NULL)
759                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
760
761         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
762         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
763
764         /* Optional field */
765         if (conf->proxy_address != NULL) {
766                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
767                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
768         }
769
770         if (conf->is_hidden != NULL) {
771                 gboolean hidden = FALSE;
772                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
773                         hidden = TRUE;
774                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
775         }
776
777         ret = _save_configuration(config_id, keyfile);
778         if (ret == TRUE) {
779                 INFO("Success to save configuration [%s]", config_id);
780                 wifi_complete_save_configuration(wifi, context);
781         } else {
782                 INFO("Fail to save configuration [%s]", config_id);
783                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
784         }
785
786         g_key_file_free(keyfile);
787         g_free(conf->name);
788         g_free(conf->ssid);
789         g_free(conf->passphrase);
790         g_free(conf->is_hidden);
791         g_free(conf->proxy_address);
792         g_free(conf);
793
794         g_variant_iter_free(iter);
795
796         return ret;
797 }
798
799 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
800                 const gchar *config_id)
801 {
802         gboolean ret = FALSE;
803         GVariantBuilder *b = NULL;
804         struct wifi_config *conf = NULL;
805
806         g_return_val_if_fail(wifi != NULL, FALSE);
807
808         conf = g_new0(struct wifi_config, 1);
809         conf->eap_config = g_new0(struct wifi_eap_config, 1);
810
811         ret = _load_configuration(config_id, conf);
812         if (ret != TRUE) {
813                 g_free(conf->eap_config);
814                 g_free(conf);
815                 ERR("Fail to _load_configuration");
816                 netconfig_error_no_profile(context);
817                 return FALSE;
818         }
819
820         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
821         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
822         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
823         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
824         if (conf->proxy_address != NULL)
825                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
826         else
827                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
828
829         if (conf->last_error != NULL)
830                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
831         else
832                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
833
834         if (conf->eap_config != NULL) {
835                 if (conf->eap_config->anonymous_identity != NULL)
836                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
837                 else
838                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
839
840                 if (conf->eap_config->ca_cert != NULL)
841                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
842                 else
843                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
844
845                 if (conf->eap_config->client_cert != NULL)
846                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
847                 else
848                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
849
850                 if (conf->eap_config->private_key != NULL)
851                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
852                 else
853                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
854
855                 if (conf->eap_config->identity != NULL)
856                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
857                 else
858                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
859
860                 if (conf->eap_config->eap_type != NULL)
861                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
862                 else
863                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
864
865                 if (conf->eap_config->eap_auth_type != NULL)
866                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
867                 else
868                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
869
870                 if (conf->eap_config->subject_match != NULL)
871                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
872                 else
873                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
874         }
875
876         __free_wifi_configuration(conf);
877
878         wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
879         g_variant_builder_unref(b);
880         return TRUE;
881 }
882
883 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
884                 const gchar *config_id, GVariant *configuration)
885 {
886         gboolean ret = FALSE;
887         struct wifi_config *conf = NULL;
888         GKeyFile *keyfile = NULL;
889         GVariantIter *iter;
890         GVariant *value;
891         gchar *field;
892         gchar *group_name = NULL;
893
894         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
895                 ERR("Invalid parameter");
896                 netconfig_error_invalid_parameter(context);
897                 return FALSE;
898         }
899
900         conf = g_new0(struct wifi_config, 1);
901         conf->eap_config = g_new0(struct wifi_eap_config, 1);
902
903         g_variant_get(configuration, "a{sv}", &iter);
904         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
905                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
906                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
907                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
908                                 DBG("name [%s]", conf->name);
909                         } else {
910                                 conf->name = NULL;
911                         }
912                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
913                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
914                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
915                                 DBG("ssid [%s]", conf->ssid);
916                         } else {
917                                 conf->ssid = NULL;
918                         }
919                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
920                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
921                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
922                                 DBG("passphrase [%s]", conf->passphrase);
923                         } else {
924                                 conf->passphrase = NULL;
925                         }
926                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
927                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
928                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
929                                 DBG("is_hidden [%s]", conf->is_hidden);
930                         } else {
931                                 conf->is_hidden = NULL;
932                         }
933                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
934                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
935                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
936                                 DBG("proxy_address [%s]", conf->proxy_address);
937                         } else {
938                                 conf->proxy_address = NULL;
939                         }
940                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
941                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
942                                 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
943                                 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
944                         } else {
945                                 conf->eap_config->anonymous_identity = NULL;
946                         }
947                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
948                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
949                                 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
950                                 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
951                         } else {
952                                 conf->eap_config->ca_cert = NULL;
953                         }
954                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
955                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
956                                 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
957                                 DBG("client_cert [%s]", conf->eap_config->client_cert);
958                         } else {
959                                 conf->eap_config->client_cert = NULL;
960                         }
961                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
962                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
963                                 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
964                                 DBG("private_key [%s]", conf->eap_config->private_key);
965                         } else {
966                                 conf->eap_config->private_key = NULL;
967                         }
968                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
969                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
970                                 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
971                                 DBG("identity [%s]", conf->eap_config->identity);
972                         } else {
973                                 conf->eap_config->identity = NULL;
974                         }
975                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
976                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
977                                 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
978                                 DBG("eap_type [%s]", conf->eap_config->eap_type);
979                         } else {
980                                 conf->eap_config->eap_type = NULL;
981                         }
982                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
983                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
984                                 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
985                                 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
986                         } else {
987                                 conf->eap_config->eap_auth_type = NULL;
988                         }
989                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
990                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
991                                 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
992                                 DBG("subject_match [%s]", conf->eap_config->subject_match);
993                         } else {
994                                 conf->eap_config->subject_match = NULL;
995                         }
996                 }
997         }
998         conf->favorite = TRUE;
999         conf->autoconnect = TRUE;
1000
1001         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
1002         if (ret != TRUE) {
1003                 __free_wifi_configuration(conf);
1004                 ERR("Fail to get_wifi_config_group_name");
1005                 return FALSE;
1006         }
1007
1008         keyfile = g_key_file_new();
1009         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1010         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1011
1012         if (conf->passphrase != NULL)
1013                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1014
1015         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1016         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1017
1018         /* Optional field */
1019         if (conf->proxy_address != NULL) {
1020                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1021                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1022         }
1023
1024         if (conf->is_hidden != NULL) {
1025                 gboolean hidden = FALSE;
1026                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1027                         hidden = TRUE;
1028                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1029         }
1030
1031         if (conf->eap_config->anonymous_identity != NULL)
1032                 g_key_file_set_string(keyfile, group_name,
1033                         WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1034
1035         if (conf->eap_config->ca_cert != NULL)
1036                 g_key_file_set_string(keyfile, group_name,
1037                         WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1038
1039         if (conf->eap_config->client_cert != NULL)
1040                 g_key_file_set_string(keyfile, group_name,
1041                         WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1042
1043         if (conf->eap_config->private_key != NULL)
1044                 g_key_file_set_string(keyfile, group_name,
1045                         WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1046
1047         if (conf->eap_config->identity != NULL)
1048                 g_key_file_set_string(keyfile, group_name,
1049                         WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1050
1051         if (conf->eap_config->eap_type != NULL)
1052                 g_key_file_set_string(keyfile, group_name,
1053                         WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1054
1055         if (conf->eap_config->eap_auth_type != NULL)
1056                 g_key_file_set_string(keyfile, group_name,
1057                         WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1058
1059         if (conf->eap_config->subject_match != NULL)
1060                 g_key_file_set_string(keyfile, group_name,
1061                         WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1062
1063         ret = _save_configuration(config_id, keyfile);
1064         if (ret == TRUE) {
1065                 INFO("Success to save eap configuration [%s]", config_id);
1066                 wifi_complete_save_eap_configuration(wifi, context);
1067         } else {
1068                 INFO("Fail to save eap configuration [%s]", config_id);
1069                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1070         }
1071
1072         g_key_file_free(keyfile);
1073         __free_wifi_configuration(conf);
1074
1075         g_variant_iter_free(iter);
1076
1077         return ret;
1078 }
1079
1080 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1081 {
1082         gboolean ret = FALSE;
1083
1084         if ((wifi == NULL) || (config_id == NULL)) {
1085                 ERR("Invalid parameter");
1086                 netconfig_error_invalid_parameter(context);
1087                 return FALSE;
1088         }
1089
1090         ret = _remove_configuration(config_id);
1091         if (ret != TRUE) {
1092                 /* no configuration or error */
1093                 ERR("No [%s] configuration", config_id);
1094                 netconfig_error_no_profile(context);
1095                 return FALSE;
1096         }
1097
1098         wifi_complete_remove_configuration(wifi, context);
1099         return ret;
1100 }
1101
1102 /* config field key / value */
1103 /*
1104  * [wifi_macaddress_config_id]
1105  * Name=name (mandatory)
1106  * SSID=SSID (mandatory)
1107  * Frequency=2462 (X)
1108  * Favorite=true (X)
1109  * AutoConnect=true (Default true)
1110  * Modified=2015-03-20 (X)
1111  * IPv4.method=manual (O)
1112  * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1113  * IPv6.method=auto (X)
1114  * IPv6.privacy=disabled (X)
1115  * IPv4.netmask_prefixlen=24 (X)
1116  * IPv4.local_address=192.0.0.1 (O)
1117  * IPv4.gateway=192.0.0.1 (O ? X ?)
1118  * Nameservers=192.168.43.22; (O)
1119  * Proxy.Method=manual (O)
1120  * Proxy.Servers=trst.com:8888; (O)
1121  */
1122 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1123                 const gchar *config_id, const gchar *key, const gchar *value)
1124 {
1125         gboolean ret = FALSE;
1126         gchar *keyfile_key = NULL;
1127
1128         g_return_val_if_fail(wifi != NULL, FALSE);
1129         g_return_val_if_fail(config_id != NULL, FALSE);
1130         g_return_val_if_fail(key != NULL, FALSE);
1131
1132         DBG("Key[%s] Value[%d]", key, value);
1133
1134         if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1135                 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1136                 if (!ret) {
1137                         ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1138                         netconfig_error_invalid_parameter(context);
1139                         return FALSE;
1140                 }
1141                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1142         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1143                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1144         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1145                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1146         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1147                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1148         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1149                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1150         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1151                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1152         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1153                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1154         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1155                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1156         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1157                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1158         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1159                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1160         } else {
1161                 ERR("Not supported key[%s]", key);
1162                 netconfig_error_invalid_parameter(context);
1163                 return FALSE;
1164         }
1165
1166         ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1167         if (!ret) {
1168                 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1169                 ret = FALSE;
1170         }
1171
1172         if (keyfile_key != NULL)
1173                 g_free(keyfile_key);
1174
1175         wifi_complete_set_config_field(wifi, context);
1176         return ret;
1177 }
1178
1179 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1180 {
1181         gboolean ret = FALSE;
1182         gchar *passphrase = NULL;
1183
1184         if ((wifi == NULL) || (config_id == NULL)) {
1185                 ERR("Invalid parameter");
1186                 netconfig_error_invalid_parameter(context);
1187                 return FALSE;
1188         }
1189
1190         ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1191         if (!ret) {
1192                 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1193                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1194                 return FALSE;
1195         }
1196
1197         wifi_complete_get_config_passphrase(wifi, context, passphrase);
1198         g_free(passphrase);
1199
1200         return ret;
1201 }