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