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