5 * Copyright (C) 2007 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <arpa/inet.h>
33 #define GROUP_CONFIG "Config"
35 char *__connman_iface_find_passphrase(struct connman_iface *iface,
39 gchar *pathname, *result = NULL;
44 DBG("iface %p", iface);
46 if (iface->identifier == NULL)
49 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
54 keyfile = g_key_file_new();
56 g_key_file_set_list_separator(keyfile, ',');
58 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
61 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
64 list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
65 "KnownNetworks", &list_len, NULL);
66 for (i = 0; i < list_len; i++) {
67 DBG("known network %s", list[i]);
69 if (g_str_equal(list[i], network) == TRUE) {
70 DBG("found network %s", network);
72 result = g_key_file_get_string(keyfile, network,
75 result = g_strdup("");
83 g_key_file_free(keyfile);
90 int __connman_iface_load(struct connman_iface *iface)
93 gchar *pathname, *str;
97 DBG("iface %p", iface);
99 if (iface->identifier == NULL)
102 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
104 if (pathname == NULL)
107 keyfile = g_key_file_new();
109 g_key_file_set_list_separator(keyfile, ',');
111 if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
114 if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
117 str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
119 iface->policy = __connman_iface_string2policy(str);
123 list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
124 "KnownNetworks", &list_len, NULL);
128 str = g_key_file_get_string(keyfile, GROUP_CONFIG,
129 "LastNetwork", NULL);
131 g_free(iface->network.identifier);
132 iface->network.identifier = str;
134 str = g_key_file_get_string(keyfile,
135 iface->network.identifier, "PSK", NULL);
137 g_free(iface->network.passphrase);
138 iface->network.passphrase = str;
143 g_key_file_free(keyfile);
150 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
154 DBG("iface %p", iface);
156 str = __connman_iface_policy2string(iface->policy);
157 g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
159 if (iface->network.identifier != NULL) {
160 g_key_file_set_string(keyfile, GROUP_CONFIG,
161 "LastNetwork", iface->network.identifier);
163 g_key_file_remove_key(keyfile, GROUP_CONFIG,
164 "LastNetwork", NULL);
166 if (iface->network.identifier != NULL)
167 g_key_file_set_string(keyfile, iface->network.identifier,
168 "PSK", iface->network.passphrase);
171 int __connman_iface_store(struct connman_iface *iface)
174 gchar *pathname, *data = NULL;
177 DBG("iface %p", iface);
179 if (iface->identifier == NULL)
182 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
184 if (pathname == NULL)
187 keyfile = g_key_file_new();
189 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
193 if (g_key_file_load_from_data(keyfile, data, length,
194 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
201 do_update(keyfile, iface);
203 data = g_key_file_to_data(keyfile, &length, NULL);
205 g_file_set_contents(pathname, data, length, NULL);
210 g_key_file_free(keyfile);