6694d99214ae4a22fa0372a64a78c9d8ee08dcb4
[platform/upstream/connman.git] / src / iface-storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <arpa/inet.h>
28
29 #include <glib.h>
30
31 #include "connman.h"
32
33 #define GROUP_CONFIG  "Config"
34
35 char *__connman_iface_find_passphrase(struct connman_iface *iface,
36                                                         const char *network)
37 {
38         GKeyFile *keyfile;
39         gchar *pathname, *result = NULL;
40         gchar **list;
41         gsize list_len;
42         int i;
43
44         DBG("iface %p", iface);
45
46         if (iface->identifier == NULL)
47                 return NULL;
48
49         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
50                                                         iface->identifier);
51         if (pathname == NULL)
52                 return NULL;
53
54         keyfile = g_key_file_new();
55
56         g_key_file_set_list_separator(keyfile, ',');
57
58         if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
59                 goto done;
60
61         if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
62                 goto done;
63
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]);
68
69                 if (g_str_equal(list[i], network) == TRUE) {
70                         DBG("found network %s", network);
71
72                         result = g_key_file_get_string(keyfile, network,
73                                                                 "PSK", NULL);
74                         if (result == NULL)
75                                 result = g_strdup("");
76                         break;
77                 }
78         }
79
80         g_strfreev(list);
81
82 done:
83         g_key_file_free(keyfile);
84
85         g_free(pathname);
86
87         return result;
88 }
89
90 int __connman_iface_load(struct connman_iface *iface)
91 {
92         GKeyFile *keyfile;
93         gchar *pathname, *str;
94         gchar **list;
95         gsize list_len;
96
97         DBG("iface %p", iface);
98
99         if (iface->identifier == NULL)
100                 return -EIO;
101
102         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
103                                                         iface->identifier);
104         if (pathname == NULL)
105                 return -ENOMEM;
106
107         keyfile = g_key_file_new();
108
109         g_key_file_set_list_separator(keyfile, ',');
110
111         if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
112                 goto done;
113
114         if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
115                 goto done;
116
117         str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
118         if (str != NULL) {
119                 iface->policy = __connman_iface_string2policy(str);
120                 g_free(str);
121         }
122
123         list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
124                                         "KnownNetworks", &list_len, NULL);
125
126         g_strfreev(list);
127
128         str = g_key_file_get_string(keyfile, GROUP_CONFIG,
129                                                 "LastNetwork", NULL);
130         if (str != NULL) {
131                 g_free(iface->network.identifier);
132                 iface->network.identifier = str;
133
134                 str = g_key_file_get_string(keyfile,
135                                 iface->network.identifier, "PSK", NULL);
136                 if (str != NULL) {
137                         g_free(iface->network.passphrase);
138                         iface->network.passphrase = str;
139                 }
140         }
141
142 done:
143         g_key_file_free(keyfile);
144
145         g_free(pathname);
146
147         return 0;
148 }
149
150 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
151 {
152         const char *str;
153
154         DBG("iface %p", iface);
155
156         str = __connman_iface_policy2string(iface->policy);
157         g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
158
159         if (iface->network.identifier != NULL) {
160                 g_key_file_set_string(keyfile, GROUP_CONFIG,
161                                 "LastNetwork", iface->network.identifier);
162         } else
163                 g_key_file_remove_key(keyfile, GROUP_CONFIG,
164                                                 "LastNetwork", NULL);
165
166         if (iface->network.identifier != NULL)
167                 g_key_file_set_string(keyfile, iface->network.identifier,
168                                         "PSK", iface->network.passphrase);
169 }
170
171 int __connman_iface_store(struct connman_iface *iface)
172 {
173         GKeyFile *keyfile;
174         gchar *pathname, *data = NULL;
175         gsize length;
176
177         DBG("iface %p", iface);
178
179         if (iface->identifier == NULL)
180                 return -EIO;
181
182         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
183                                                         iface->identifier);
184         if (pathname == NULL)
185                 return -ENOMEM;
186
187         keyfile = g_key_file_new();
188
189         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
190                 goto update;
191
192         if (length > 0) {
193                 if (g_key_file_load_from_data(keyfile, data, length,
194                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
195                         goto done;
196         }
197
198         g_free(data);
199
200 update:
201         do_update(keyfile, iface);
202
203         data = g_key_file_to_data(keyfile, &length, NULL);
204
205         g_file_set_contents(pathname, data, length, NULL);
206
207 done:
208         g_free(data);
209
210         g_key_file_free(keyfile);
211
212         g_free(pathname);
213
214         return 0;
215 }