device: Disconnect service when disabling device
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <unistd.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30
31 #include <connman/storage.h>
32
33 #include "connman.h"
34
35 #define SETTINGS        "settings"
36 #define DEFAULT         "default.profile"
37
38 #define MODE            (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | \
39                         S_IXGRP | S_IROTH | S_IXOTH)
40
41 static GKeyFile *storage_load(const char *pathname)
42 {
43         GKeyFile *keyfile = NULL;
44         GError *error = NULL;
45
46         DBG("Loading %s", pathname);
47
48         keyfile = g_key_file_new();
49
50         if (!g_key_file_load_from_file(keyfile, pathname, 0, &error)) {
51                 DBG("Unable to load %s: %s", pathname, error->message);
52                 g_clear_error(&error);
53
54                 g_key_file_free(keyfile);
55                 keyfile = NULL;
56         }
57
58         return keyfile;
59 }
60
61 static void storage_save(GKeyFile *keyfile, char *pathname)
62 {
63         gchar *data = NULL;
64         gsize length = 0;
65         GError *error = NULL;
66
67         data = g_key_file_to_data(keyfile, &length, NULL);
68
69         if (!g_file_set_contents(pathname, data, length, &error)) {
70                 DBG("Failed to store information: %s", error->message);
71                 g_error_free(error);
72         }
73
74         g_free(data);
75 }
76
77 static void storage_delete(const char *pathname)
78 {
79         DBG("file path %s", pathname);
80
81         if (unlink(pathname) < 0)
82                 connman_error("Failed to remove %s", pathname);
83 }
84
85 GKeyFile *__connman_storage_load_global()
86 {
87         gchar *pathname;
88         GKeyFile *keyfile = NULL;
89
90         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
91         if(pathname == NULL)
92                 return NULL;
93
94         keyfile = storage_load(pathname);
95
96         g_free(pathname);
97
98         return keyfile;
99 }
100
101 void __connman_storage_save_global(GKeyFile *keyfile)
102 {
103         gchar *pathname;
104
105         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
106         if(pathname == NULL)
107                 return;
108
109         storage_save(keyfile, pathname);
110
111         g_free(pathname);
112 }
113
114 void __connman_storage_delete_global()
115 {
116         gchar *pathname;
117
118         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
119         if(pathname == NULL)
120                 return;
121
122         storage_delete(pathname);
123
124         g_free(pathname);
125 }
126
127 GKeyFile *__connman_storage_load_config(const char *ident)
128 {
129         gchar *pathname;
130         GKeyFile *keyfile = NULL;
131
132         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
133         if(pathname == NULL)
134                 return NULL;
135
136         keyfile = storage_load(pathname);
137
138         g_free(pathname);
139
140         return keyfile;
141 }
142
143 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
144 {
145         gchar *pathname;
146
147         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
148         if(pathname == NULL)
149                 return;
150
151         storage_save(keyfile, pathname);
152 }
153
154 void __connman_storage_delete_config(const char *ident)
155 {
156         gchar *pathname;
157
158         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
159         if(pathname == NULL)
160                 return;
161
162         storage_delete(pathname);
163
164         g_free(pathname);
165 }
166
167 GKeyFile *__connman_storage_open_service(const char *service_id)
168 {
169         gchar *pathname;
170         GKeyFile *keyfile = NULL;
171
172         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
173         if(pathname == NULL)
174                 return NULL;
175
176         keyfile =  storage_load(pathname);
177         if (keyfile) {
178                 g_free(pathname);
179                 return keyfile;
180         }
181
182         g_free(pathname);
183
184         keyfile = g_key_file_new();
185
186         return keyfile;
187 }
188
189 gchar **connman_storage_get_services()
190 {
191         struct dirent *d;
192         gchar *str;
193         DIR *dir;
194         GString *result;
195         gchar **services = NULL;
196         struct stat buf;
197         int ret;
198
199         dir = opendir(STORAGEDIR);
200         if (dir == NULL)
201                 return NULL;
202
203         result = g_string_new(NULL);
204
205         while ((d = readdir(dir))) {
206                 if (strcmp(d->d_name, ".") == 0 ||
207                                 strcmp(d->d_name, "..") == 0 ||
208                                 strncmp(d->d_name, "provider_", 9) == 0)
209                         continue;
210
211                 switch (d->d_type) {
212                 case DT_DIR:
213                         /*
214                          * If the settings file is not found, then
215                          * assume this directory is not a services dir.
216                          */
217                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
218                                                                 d->d_name);
219                         ret = stat(str, &buf);
220                         g_free(str);
221                         if (ret < 0)
222                                 continue;
223
224                         g_string_append_printf(result, "%s/", d->d_name);
225                         break;
226                 }
227         }
228
229         closedir(dir);
230
231         str = g_string_free(result, FALSE);
232         if (str) {
233                 str[strlen(str) - 1] = '\0';
234                 services = g_strsplit(str, "/", -1);
235         }
236         g_free(str);
237
238         return services;
239 }
240
241 GKeyFile *connman_storage_load_service(const char *service_id)
242 {
243         gchar *pathname;
244         GKeyFile *keyfile = NULL;
245
246         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
247         if(pathname == NULL)
248                 return NULL;
249
250         keyfile =  storage_load(pathname);
251         g_free(pathname);
252         if (keyfile)
253                 return keyfile;
254
255         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
256         if(pathname == NULL)
257                 return NULL;
258
259         keyfile =  storage_load(pathname);
260
261         g_free(pathname);
262
263         return keyfile;
264 }
265
266 void __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
267 {
268         gchar *pathname, *dirname;
269
270         dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
271         if(dirname == NULL)
272                 return;
273
274         /* If the dir doesn't exist, create it */
275         if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
276                 if(mkdir(dirname, MODE) < 0) {
277                         if (errno != EEXIST) {
278                                 g_free(dirname);
279                                 return;
280                         }
281                 }
282         }
283
284         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
285
286         g_free(dirname);
287
288         storage_save(keyfile, pathname);
289
290         g_free(pathname);
291 }
292
293 GKeyFile *__connman_storage_load_provider(const char *identifier)
294 {
295         gchar *pathname;
296         GKeyFile *keyfile;
297
298         pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
299                         identifier, SETTINGS);
300         if (pathname == NULL)
301                 return NULL;
302
303         keyfile = storage_load(pathname);
304         g_free(pathname);
305
306         return keyfile;
307 }
308
309 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
310 {
311         gchar *pathname, *dirname;
312
313         dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
314                         "provider", identifier);
315         if (dirname == NULL)
316                 return;
317
318         if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
319                         mkdir(dirname, MODE) < 0) {
320                 g_free(dirname);
321                 return;
322         }
323
324         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
325         g_free(dirname);
326
327         storage_save(keyfile, pathname);
328         g_free(pathname);
329 }
330
331 /*
332  * This function migrates keys from default.profile to settings file.
333  * This can be removed once the migration is over.
334 */
335 void __connman_storage_migrate()
336 {
337         gchar *pathname;
338         GKeyFile *keyfile_def = NULL;
339         GKeyFile *keyfile = NULL;
340         GError *error = NULL;
341         connman_bool_t val;
342
343         /* If setting file exists, migration has been done. */
344         keyfile = __connman_storage_load_global();
345         if (keyfile) {
346                 g_key_file_free(keyfile);
347                 return;
348         }
349
350         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
351         if(pathname == NULL)
352                 return;
353
354         /* Copy global settings from default.profile to settings. */
355         keyfile = g_key_file_new();
356
357         /* If default.profile doesn't exists, create settings with defaults. */
358         keyfile_def = storage_load(pathname);
359         if (keyfile_def == NULL) {
360                 g_key_file_set_boolean(keyfile, "global",
361                                         "OfflineMode", FALSE);
362
363                 g_key_file_set_boolean(keyfile, "WiFi",
364                                         "Enable", FALSE);
365
366                 g_key_file_set_boolean(keyfile, "Bluetooth",
367                                         "Enable", FALSE);
368
369                 g_key_file_set_boolean(keyfile, "Wired",
370                                         "Enable", FALSE);
371
372                 g_key_file_set_boolean(keyfile, "Cellular",
373                                         "Enable", FALSE);
374
375                 g_key_file_set_boolean(keyfile, "WiMAX",
376                                         "Enable", FALSE);
377
378                 goto done;
379         }
380
381         val = g_key_file_get_boolean(keyfile_def, "global",
382                                         "OfflineMode", &error);
383         if (error != NULL) {
384                 g_clear_error(&error);
385                 val = FALSE;
386         }
387
388         g_key_file_set_boolean(keyfile, "global",
389                                         "OfflineMode", val);
390
391         val = g_key_file_get_boolean(keyfile_def, "WiFi",
392                                         "Enable", &error);
393         if (error != NULL) {
394                 g_clear_error(&error);
395                 val = FALSE;
396         }
397
398         g_key_file_set_boolean(keyfile, "WiFi",
399                                         "Enable", val);
400
401         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
402                                         "Enable", &error);
403         if (error != NULL) {
404                 g_clear_error(&error);
405                 val = FALSE;
406         }
407
408         g_key_file_set_boolean(keyfile, "Bluetooth",
409                                         "Enable", val);
410
411         val = g_key_file_get_boolean(keyfile_def, "Wired",
412                                         "Enable", &error);
413         if (error != NULL) {
414                 g_clear_error(&error);
415                 val = FALSE;
416         }
417
418         g_key_file_set_boolean(keyfile, "Wired",
419                                         "Enable", val);
420
421         val = g_key_file_get_boolean(keyfile_def, "Cellular",
422                                         "Enable", &error);
423         if (error != NULL) {
424                 g_clear_error(&error);
425                 val = FALSE;
426         }
427
428         g_key_file_set_boolean(keyfile, "Cellular",
429                                         "Enable", val);
430
431         val = g_key_file_get_boolean(keyfile_def, "WiMAX",
432                                         "Enable", &error);
433         if (error != NULL) {
434                 g_clear_error(&error);
435                 val = FALSE;
436         }
437
438         g_key_file_set_boolean(keyfile, "WiMAX",
439                                         "Enable", val);
440
441 done:
442         __connman_storage_save_global(keyfile);
443
444         g_key_file_free(keyfile);
445
446         if (keyfile_def)
447                 g_key_file_free(keyfile_def);
448
449         g_free(pathname);
450 }