Fixed memory leak
[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                 return FALSE;
436         }
437
438         if (g_strcmp0(key, WIFI_CONFIG_PROXY_METHOD) == 0) {
439                 g_key_file_set_string(keyfile, group_name, key, value);
440         } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
441                 g_key_file_set_string(keyfile, group_name, key, value);
442         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
443                 gboolean hidden = FALSE;
444                 if (g_strcmp0(value, "TRUE") == 0)
445                         hidden = TRUE;
446                 g_key_file_set_boolean(keyfile, group_name, key, hidden);
447         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
448                 g_key_file_set_string(keyfile, group_name, key, value);
449         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
450                 g_key_file_set_string(keyfile, group_name, key, value);
451         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
452                 g_key_file_set_string(keyfile, group_name, key, value);
453         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
454                 g_key_file_set_string(keyfile, group_name, key, value);
455         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
456                 g_key_file_set_string(keyfile, group_name, key, value);
457         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
458                 g_key_file_set_string(keyfile, group_name, key, value);
459         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
460                 g_key_file_set_string(keyfile, group_name, key, value);
461         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
462                 g_key_file_set_string(keyfile, group_name, key, value);
463         } else {
464                 ERR("key[%s] is not supported", key);
465                 ret = FALSE;
466         }
467
468         _save_configuration(config_id, keyfile);
469
470         g_key_file_free(keyfile);
471         g_free(group_name);
472
473         return ret;
474 }
475
476 static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **value)
477 {
478         GKeyFile *keyfile;
479         gchar *group_name;
480         gchar *val = NULL;
481         gboolean hidden = FALSE;
482         gboolean ret = FALSE;
483
484         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
485         if (ret != TRUE) {
486                 ERR("Fail to get_wifi_config_group_name");
487                 return FALSE;
488         }
489         DBG("group_name %s", group_name);
490
491         keyfile = __get_configuration_keyfile(group_name);
492         if (keyfile == NULL) {
493                 ERR("Fail to __get_configuration_keyfile");
494                 return FALSE;
495         }
496
497         if (g_strcmp0(key, WIFI_CONFIG_NAME) == 0) {
498                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
499         } else if (g_strcmp0(key, WIFI_CONFIG_PASSPHRASE) == 0) {
500                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, NULL);
501         } else if (g_strcmp0(key, WIFI_CONFIG_PROXY_SERVER) == 0) {
502                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, NULL);
503         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
504                 hidden = g_key_file_get_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, NULL);
505                 if (hidden)
506                         val = g_strdup("TRUE");
507                 else
508                         val = g_strdup("FALSE");
509         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
510                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, NULL);
511         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
512                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CACERT, NULL);
513         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
514                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_CLIENTCERT, NULL);
515         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
516                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_PRIVATEKEY, NULL);
517         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
518                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_IDENTITY, NULL);
519         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
520                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_TYPE, NULL);
521         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
522                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_AUTH_TYPE, NULL);
523         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
524                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_EAP_SUBJECT_MATCH, NULL);
525         } else if (g_strcmp0(key, WIFI_CONFIG_FAILURE) == 0) {
526                 val = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_FAILURE, NULL);
527         } else {
528                 ERR("Invalid key[%s]", key);
529                 val = g_strdup("NOTSUPPORTED");
530         }
531
532         *value = g_strdup(val);
533         g_free(val);
534
535         g_key_file_free(keyfile);
536         g_free(group_name);
537
538         return TRUE;
539 }
540
541 static GSList *_get_list(void)
542 {
543         GSList *list = NULL;
544         struct dirent *dp = NULL;
545         DIR *dir;
546
547         dir = opendir(CONNMAN_STORAGE);
548         if (dir == NULL) {
549                 ERR("Cannot open dir %s", CONNMAN_STORAGE);
550                 return NULL;
551         }
552
553         while ((dp = readdir(dir)) != NULL) {
554                 if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
555                                 strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
556                         continue;
557                 }
558                 gchar *config_id = g_strdup(dp->d_name + WIFI_PREFIX_LENGTH);
559                 list = g_slist_append(list, g_strdup(config_id));
560                 g_free(config_id);
561         }
562         closedir(dir);
563
564         return list;
565 }
566
567 gboolean wifi_config_get_config_id(const gchar *service_profile, gchar **config_id)
568 {
569         gboolean ret = FALSE;
570         gchar *val = NULL;
571
572         if ((service_profile == NULL) || (config_id == NULL)) {
573                 ERR("Invalid parameter");
574                 return FALSE;
575         }
576
577         ret = __get_config_id(service_profile, &val);
578         *config_id = g_strdup(val);
579         g_free(val);
580
581         return ret;
582 }
583
584 gboolean wifi_config_remove_configuration(const gchar *config_id)
585 {
586         gboolean ret = FALSE;
587
588         ret = _remove_configuration(config_id);
589
590         return ret;
591 }
592
593 /* dbus method */
594 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
595 {
596         guint i = 0;
597         GSList *config_ids = NULL;
598         guint length;
599         gchar **result = NULL;
600
601         g_return_val_if_fail(wifi != NULL, FALSE);
602
603         config_ids = _get_list();
604         if (config_ids == NULL) {
605                 netconfig_error_no_profile(context);
606                 ERR("Fail to get config list");
607                 return FALSE;
608         }
609
610         length = g_slist_length(config_ids);
611         result = g_new0(gchar *, length + 1);
612         for (i = 0; i < length; i++) {
613                 gchar *config_id = g_slist_nth_data(config_ids, i);
614                 result[i] = g_strdup(config_id);
615         }
616
617         config_ids = g_slist_nth(config_ids, 0);
618         g_slist_free_full(config_ids, g_free);
619
620         wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
621
622         for (i = 0; i < length; i++)
623                 if (result[i])
624                         g_free(result[i]);
625
626         if (result)
627                 g_free(result);
628
629         return TRUE;
630 }
631
632 gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
633                 const gchar *config_id)
634 {
635         gboolean ret = FALSE;
636         GVariantBuilder *b = NULL;
637         struct wifi_config *conf = NULL;
638
639         g_return_val_if_fail(wifi != NULL, FALSE);
640
641         conf = g_new0(struct wifi_config, 1);
642
643         ret = _load_configuration(config_id, conf);
644         if (ret != TRUE) {
645                 g_free(conf);
646                 ERR("Fail to _load_configuration");
647                 netconfig_error_no_profile(context);
648                 return FALSE;
649         }
650
651         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
652         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
653         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
654         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
655
656         if (conf->proxy_address != NULL)
657                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
658         else
659                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
660
661         if (conf->last_error != NULL)
662                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
663         else
664                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
665
666         g_free(conf->proxy_address);
667         g_free(conf->last_error);
668         g_free(conf->name);
669         g_free(conf->security_type);
670         g_free(conf->is_hidden);
671         g_free(conf);
672
673         wifi_complete_load_configuration(wifi, context, g_variant_builder_end(b));
674         g_variant_builder_unref(b);
675         return TRUE;
676 }
677
678 gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
679                 const gchar *config_id, GVariant *configuration)
680 {
681         gboolean ret = FALSE;
682         struct wifi_config *conf = NULL;
683         GKeyFile *keyfile = NULL;
684         GVariantIter *iter;
685         GVariant *value;
686         gchar *field;
687         gchar *group_name = NULL;
688
689         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
690                 ERR("Invalid parameter");
691                 netconfig_error_invalid_parameter(context);
692                 return FALSE;
693         }
694
695         conf = g_new0(struct wifi_config, 1);
696
697         g_variant_get(configuration, "a{sv}", &iter);
698         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
699                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
700                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
701                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
702                                 DBG("name [%s]", conf->name);
703                         } else {
704                                 conf->name = NULL;
705                         }
706                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
707                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
708                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
709                                 DBG("ssid [%s]", conf->ssid);
710                         } else {
711                                 conf->ssid = NULL;
712                         }
713                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
714                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
715                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
716                                 DBG("passphrase []");
717                         } else {
718                                 conf->passphrase = NULL;
719                         }
720                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
721                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
722                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
723                                 DBG("is_hidden [%s]", conf->is_hidden);
724                         } else {
725                                 conf->is_hidden = NULL;
726                         }
727                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
728                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
729                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
730                                 DBG("proxy_address [%s]", conf->proxy_address);
731                         } else {
732                                 conf->proxy_address = NULL;
733                         }
734                 }
735         }
736         conf->favorite = TRUE;
737         conf->autoconnect = TRUE;
738
739         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
740         if (ret != TRUE) {
741                 g_free(conf->name);
742                 g_free(conf->ssid);
743                 g_free(conf->passphrase);
744                 g_free(conf->is_hidden);
745                 g_free(conf->proxy_address);
746                 g_free(conf);
747                 ERR("Fail to get_wifi_config_group_name");
748                 return FALSE;
749         }
750
751         keyfile = g_key_file_new();
752         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
753         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
754
755         if (conf->passphrase != NULL)
756                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
757
758         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
759         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
760
761         /* Optional field */
762         if (conf->proxy_address != NULL) {
763                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
764                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
765         }
766
767         if (conf->is_hidden != NULL) {
768                 gboolean hidden = FALSE;
769                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
770                         hidden = TRUE;
771                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
772         }
773
774         ret = _save_configuration(config_id, keyfile);
775         if (ret == TRUE) {
776                 INFO("Success to save configuration [%s]", config_id);
777                 wifi_complete_save_configuration(wifi, context);
778         } else {
779                 INFO("Fail to save configuration [%s]", config_id);
780                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveConfiguration");
781         }
782
783         g_key_file_free(keyfile);
784         g_free(conf->name);
785         g_free(conf->ssid);
786         g_free(conf->passphrase);
787         g_free(conf->is_hidden);
788         g_free(conf->proxy_address);
789         g_free(conf);
790
791         g_variant_iter_free(iter);
792
793         return ret;
794 }
795
796 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
797                 const gchar *config_id)
798 {
799         gboolean ret = FALSE;
800         GVariantBuilder *b = NULL;
801         struct wifi_config *conf = NULL;
802
803         g_return_val_if_fail(wifi != NULL, FALSE);
804
805         conf = g_new0(struct wifi_config, 1);
806         conf->eap_config = g_new0(struct wifi_eap_config, 1);
807
808         ret = _load_configuration(config_id, conf);
809         if (ret != TRUE) {
810                 g_free(conf->eap_config);
811                 g_free(conf);
812                 ERR("Fail to _load_configuration");
813                 netconfig_error_no_profile(context);
814                 return FALSE;
815         }
816
817         b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
818         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
819         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
820         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
821         if (conf->proxy_address != NULL)
822                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
823         else
824                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
825
826         if (conf->last_error != NULL)
827                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
828         else
829                 g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
830
831         if (conf->eap_config != NULL) {
832                 if (conf->eap_config->anonymous_identity != NULL)
833                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string(conf->eap_config->anonymous_identity));
834                 else
835                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, g_variant_new_string("NONE"));
836
837                 if (conf->eap_config->ca_cert != NULL)
838                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string(conf->eap_config->ca_cert));
839                 else
840                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CACERT, g_variant_new_string("NONE"));
841
842                 if (conf->eap_config->client_cert != NULL)
843                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string(conf->eap_config->client_cert));
844                 else
845                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_CLIENTCERT, g_variant_new_string("NONE"));
846
847                 if (conf->eap_config->private_key != NULL)
848                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string(conf->eap_config->private_key));
849                 else
850                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_PRIVATEKEY, g_variant_new_string("NONE"));
851
852                 if (conf->eap_config->identity != NULL)
853                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string(conf->eap_config->identity));
854                 else
855                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_IDENTITY, g_variant_new_string("NONE"));
856
857                 if (conf->eap_config->eap_type != NULL)
858                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string(conf->eap_config->eap_type));
859                 else
860                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_TYPE, g_variant_new_string("NONE"));
861
862                 if (conf->eap_config->eap_auth_type != NULL)
863                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string(conf->eap_config->eap_auth_type));
864                 else
865                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_AUTH_TYPE, g_variant_new_string("NONE"));
866
867                 if (conf->eap_config->subject_match != NULL)
868                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string(conf->eap_config->subject_match));
869                 else
870                         g_variant_builder_add(b, "{sv}", WIFI_CONFIG_EAP_SUBJECT_MATCH, g_variant_new_string("NONE"));
871         }
872
873         __free_wifi_configuration(conf);
874
875         wifi_complete_load_eap_configuration(wifi, context, g_variant_builder_end(b));
876         g_variant_builder_unref(b);
877         return TRUE;
878 }
879
880 gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
881                 const gchar *config_id, GVariant *configuration)
882 {
883         gboolean ret = FALSE;
884         struct wifi_config *conf = NULL;
885         GKeyFile *keyfile = NULL;
886         GVariantIter *iter;
887         GVariant *value;
888         gchar *field;
889         gchar *group_name = NULL;
890
891         if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
892                 ERR("Invalid parameter");
893                 netconfig_error_invalid_parameter(context);
894                 return FALSE;
895         }
896
897         conf = g_new0(struct wifi_config, 1);
898         conf->eap_config = g_new0(struct wifi_eap_config, 1);
899
900         g_variant_get(configuration, "a{sv}", &iter);
901         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
902                 if (g_strcmp0(field, WIFI_CONFIG_NAME) == 0) {
903                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
904                                 conf->name = g_strdup(g_variant_get_string(value, NULL));
905                                 DBG("name [%s]", conf->name);
906                         } else {
907                                 conf->name = NULL;
908                         }
909                 } else if (g_strcmp0(field, WIFI_CONFIG_SSID) == 0) {
910                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
911                                 conf->ssid = g_strdup(g_variant_get_string(value, NULL));
912                                 DBG("ssid [%s]", conf->ssid);
913                         } else {
914                                 conf->ssid = NULL;
915                         }
916                 } else if (g_strcmp0(field, WIFI_CONFIG_PASSPHRASE) == 0) {
917                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
918                                 conf->passphrase = g_strdup(g_variant_get_string(value, NULL));
919                                 DBG("passphrase [%s]", conf->passphrase);
920                         } else {
921                                 conf->passphrase = NULL;
922                         }
923                 } else if (g_strcmp0(field, WIFI_CONFIG_HIDDEN) == 0) {
924                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
925                                 conf->is_hidden = g_strdup(g_variant_get_string(value, NULL));
926                                 DBG("is_hidden [%s]", conf->is_hidden);
927                         } else {
928                                 conf->is_hidden = NULL;
929                         }
930                 } else if (g_strcmp0(field, WIFI_CONFIG_PROXYADDRESS) == 0) {
931                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
932                                 conf->proxy_address = g_strdup(g_variant_get_string(value, NULL));
933                                 DBG("proxy_address [%s]", conf->proxy_address);
934                         } else {
935                                 conf->proxy_address = NULL;
936                         }
937                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
938                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
939                                 conf->eap_config->anonymous_identity = g_strdup(g_variant_get_string(value, NULL));
940                                 DBG("anonymous_identity [%s]", conf->eap_config->anonymous_identity);
941                         } else {
942                                 conf->eap_config->anonymous_identity = NULL;
943                         }
944                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CACERT) == 0) {
945                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
946                                 conf->eap_config->ca_cert = g_strdup(g_variant_get_string(value, NULL));
947                                 DBG("ca_cert [%s]", conf->eap_config->ca_cert);
948                         } else {
949                                 conf->eap_config->ca_cert = NULL;
950                         }
951                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
952                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
953                                 conf->eap_config->client_cert = g_strdup(g_variant_get_string(value, NULL));
954                                 DBG("client_cert [%s]", conf->eap_config->client_cert);
955                         } else {
956                                 conf->eap_config->client_cert = NULL;
957                         }
958                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
959                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
960                                 conf->eap_config->private_key = g_strdup(g_variant_get_string(value, NULL));
961                                 DBG("private_key [%s]", conf->eap_config->private_key);
962                         } else {
963                                 conf->eap_config->private_key = NULL;
964                         }
965                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_IDENTITY) == 0) {
966                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
967                                 conf->eap_config->identity = g_strdup(g_variant_get_string(value, NULL));
968                                 DBG("identity [%s]", conf->eap_config->identity);
969                         } else {
970                                 conf->eap_config->identity = NULL;
971                         }
972                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_TYPE) == 0) {
973                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
974                                 conf->eap_config->eap_type = g_strdup(g_variant_get_string(value, NULL));
975                                 DBG("eap_type [%s]", conf->eap_config->eap_type);
976                         } else {
977                                 conf->eap_config->eap_type = NULL;
978                         }
979                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
980                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
981                                 conf->eap_config->eap_auth_type = g_strdup(g_variant_get_string(value, NULL));
982                                 DBG("eap_auth_type [%s]", conf->eap_config->eap_auth_type);
983                         } else {
984                                 conf->eap_config->eap_auth_type = NULL;
985                         }
986                 } else if (g_strcmp0(field, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
987                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
988                                 conf->eap_config->subject_match = g_strdup(g_variant_get_string(value, NULL));
989                                 DBG("subject_match [%s]", conf->eap_config->subject_match);
990                         } else {
991                                 conf->eap_config->subject_match = NULL;
992                         }
993                 }
994         }
995         conf->favorite = TRUE;
996         conf->autoconnect = TRUE;
997
998         ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
999         if (ret != TRUE) {
1000                 __free_wifi_configuration(conf);
1001                 ERR("Fail to get_wifi_config_group_name");
1002                 return FALSE;
1003         }
1004
1005         keyfile = g_key_file_new();
1006         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
1007         g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
1008
1009         if (conf->passphrase != NULL)
1010                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
1011
1012         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
1013         g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
1014
1015         /* Optional field */
1016         if (conf->proxy_address != NULL) {
1017                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_METHOD, "manual");
1018                 g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PROXY_SERVER, conf->proxy_address);
1019         }
1020
1021         if (conf->is_hidden != NULL) {
1022                 gboolean hidden = FALSE;
1023                 if (g_strcmp0(conf->is_hidden, "TRUE") == 0)
1024                         hidden = TRUE;
1025                 g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_HIDDEN, hidden);
1026         }
1027
1028         if (conf->eap_config->anonymous_identity != NULL)
1029                 g_key_file_set_string(keyfile, group_name,
1030                         WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY, conf->eap_config->anonymous_identity);
1031
1032         if (conf->eap_config->ca_cert != NULL)
1033                 g_key_file_set_string(keyfile, group_name,
1034                         WIFI_CONFIG_EAP_CACERT, conf->eap_config->ca_cert);
1035
1036         if (conf->eap_config->client_cert != NULL)
1037                 g_key_file_set_string(keyfile, group_name,
1038                         WIFI_CONFIG_EAP_CLIENTCERT, conf->eap_config->client_cert);
1039
1040         if (conf->eap_config->private_key != NULL)
1041                 g_key_file_set_string(keyfile, group_name,
1042                         WIFI_CONFIG_EAP_PRIVATEKEY, conf->eap_config->private_key);
1043
1044         if (conf->eap_config->identity != NULL)
1045                 g_key_file_set_string(keyfile, group_name,
1046                         WIFI_CONFIG_EAP_IDENTITY, conf->eap_config->identity);
1047
1048         if (conf->eap_config->eap_type != NULL)
1049                 g_key_file_set_string(keyfile, group_name,
1050                         WIFI_CONFIG_EAP_TYPE, conf->eap_config->eap_type);
1051
1052         if (conf->eap_config->eap_auth_type != NULL)
1053                 g_key_file_set_string(keyfile, group_name,
1054                         WIFI_CONFIG_EAP_AUTH_TYPE, conf->eap_config->eap_auth_type);
1055
1056         if (conf->eap_config->subject_match != NULL)
1057                 g_key_file_set_string(keyfile, group_name,
1058                         WIFI_CONFIG_EAP_SUBJECT_MATCH, conf->eap_config->subject_match);
1059
1060         ret = _save_configuration(config_id, keyfile);
1061         if (ret == TRUE) {
1062                 INFO("Success to save eap configuration [%s]", config_id);
1063                 wifi_complete_save_eap_configuration(wifi, context);
1064         } else {
1065                 INFO("Fail to save eap configuration [%s]", config_id);
1066                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSaveEapConfiguration");
1067         }
1068
1069         g_key_file_free(keyfile);
1070         __free_wifi_configuration(conf);
1071
1072         g_variant_iter_free(iter);
1073
1074         return ret;
1075 }
1076
1077 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1078 {
1079         gboolean ret = FALSE;
1080
1081         if ((wifi == NULL) || (config_id == NULL)) {
1082                 ERR("Invalid parameter");
1083                 netconfig_error_invalid_parameter(context);
1084                 return FALSE;
1085         }
1086
1087         ret = _remove_configuration(config_id);
1088         if (ret != TRUE) {
1089                 /* no configuration or error */
1090                 ERR("No [%s] configuration", config_id);
1091                 netconfig_error_no_profile(context);
1092                 return FALSE;
1093         }
1094
1095         wifi_complete_remove_configuration(wifi, context);
1096         return ret;
1097 }
1098
1099 /* config field key / value */
1100 /*
1101  * [wifi_macaddress_config_id]
1102  * Name=name (mandatory)
1103  * SSID=SSID (mandatory)
1104  * Frequency=2462 (X)
1105  * Favorite=true (X)
1106  * AutoConnect=true (Default true)
1107  * Modified=2015-03-20 (X)
1108  * IPv4.method=manual (O)
1109  * IPv4.DHCP.LastAddress=192.0.0.1 (X)
1110  * IPv6.method=auto (X)
1111  * IPv6.privacy=disabled (X)
1112  * IPv4.netmask_prefixlen=24 (X)
1113  * IPv4.local_address=192.0.0.1 (O)
1114  * IPv4.gateway=192.0.0.1 (O ? X ?)
1115  * Nameservers=192.168.43.22; (O)
1116  * Proxy.Method=manual (O)
1117  * Proxy.Servers=trst.com:8888; (O)
1118  */
1119 gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
1120                 const gchar *config_id, const gchar *key, const gchar *value)
1121 {
1122         gboolean ret = FALSE;
1123         gchar *keyfile_key = NULL;
1124
1125         g_return_val_if_fail(wifi != NULL, FALSE);
1126         g_return_val_if_fail(config_id != NULL, FALSE);
1127         g_return_val_if_fail(key != NULL, FALSE);
1128
1129         DBG("Key[%s] Value[%d]", key, value);
1130
1131         if (g_strcmp0(key, WIFI_CONFIG_PROXYADDRESS) == 0) {
1132                 ret = _set_field(config_id, WIFI_CONFIG_PROXY_METHOD, "manual");
1133                 if (!ret) {
1134                         ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
1135                         netconfig_error_invalid_parameter(context);
1136                         return FALSE;
1137                 }
1138                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
1139         } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
1140                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_HIDDEN);
1141         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY) == 0) {
1142                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_ANONYMOUS_IDENTITY);
1143         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CACERT) == 0) {
1144                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CACERT);
1145         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_CLIENTCERT) == 0) {
1146                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_CLIENTCERT);
1147         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_PRIVATEKEY) == 0) {
1148                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_PRIVATEKEY);
1149         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_IDENTITY) == 0) {
1150                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_IDENTITY);
1151         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_TYPE) == 0) {
1152                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_TYPE);
1153         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_AUTH_TYPE) == 0) {
1154                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_AUTH_TYPE);
1155         } else if (g_strcmp0(key, WIFI_CONFIG_EAP_SUBJECT_MATCH) == 0) {
1156                 keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_EAP_SUBJECT_MATCH);
1157         } else {
1158                 ERR("Not supported key[%s]", key);
1159                 netconfig_error_invalid_parameter(context);
1160                 return FALSE;
1161         }
1162
1163         ret = _set_field(config_id, keyfile_key, (const gchar *)value);
1164         if (!ret) {
1165                 ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
1166                 ret = FALSE;
1167         }
1168
1169         if (keyfile_key != NULL)
1170                 g_free(keyfile_key);
1171
1172         wifi_complete_set_config_field(wifi, context);
1173         return ret;
1174 }
1175
1176 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
1177 {
1178         gboolean ret = FALSE;
1179         gchar *passphrase = NULL;
1180
1181         if ((wifi == NULL) || (config_id == NULL)) {
1182                 ERR("Invalid parameter");
1183                 netconfig_error_invalid_parameter(context);
1184                 return FALSE;
1185         }
1186
1187         ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
1188         if (!ret) {
1189                 ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
1190                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
1191                 return FALSE;
1192         }
1193
1194         wifi_complete_get_config_passphrase(wifi, context, passphrase);
1195         g_free(passphrase);
1196
1197         return ret;
1198 }