storage: migrate 0.76-era device enabled / powered state
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 int storage_save(GKeyFile *keyfile, char *pathname)
62 {
63         gchar *data = NULL;
64         gsize length = 0;
65         GError *error = NULL;
66         int ret = 0;
67
68         data = g_key_file_to_data(keyfile, &length, NULL);
69
70         if (!g_file_set_contents(pathname, data, length, &error)) {
71                 DBG("Failed to store information: %s", error->message);
72                 g_error_free(error);
73                 ret = -EIO;
74         }
75
76         g_free(data);
77
78         return ret;
79 }
80
81 static void storage_delete(const char *pathname)
82 {
83         DBG("file path %s", pathname);
84
85         if (unlink(pathname) < 0)
86                 connman_error("Failed to remove %s", pathname);
87 }
88
89 GKeyFile *__connman_storage_load_global()
90 {
91         gchar *pathname;
92         GKeyFile *keyfile = NULL;
93
94         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
95         if(pathname == NULL)
96                 return NULL;
97
98         keyfile = storage_load(pathname);
99
100         g_free(pathname);
101
102         return keyfile;
103 }
104
105 int __connman_storage_save_global(GKeyFile *keyfile)
106 {
107         gchar *pathname;
108         int ret;
109
110         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
111         if(pathname == NULL)
112                 return -ENOMEM;
113
114         ret = storage_save(keyfile, pathname);
115
116         g_free(pathname);
117
118         return ret;
119 }
120
121 void __connman_storage_delete_global()
122 {
123         gchar *pathname;
124
125         pathname = g_strdup_printf("%s/%s", STORAGEDIR, SETTINGS);
126         if(pathname == NULL)
127                 return;
128
129         storage_delete(pathname);
130
131         g_free(pathname);
132 }
133
134 GKeyFile *__connman_storage_load_config(const char *ident)
135 {
136         gchar *pathname;
137         GKeyFile *keyfile = NULL;
138
139         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
140         if(pathname == NULL)
141                 return NULL;
142
143         keyfile = storage_load(pathname);
144
145         g_free(pathname);
146
147         return keyfile;
148 }
149
150 void __connman_storage_save_config(GKeyFile *keyfile, const char *ident)
151 {
152         gchar *pathname;
153
154         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
155         if(pathname == NULL)
156                 return;
157
158         storage_save(keyfile, pathname);
159 }
160
161 void __connman_storage_delete_config(const char *ident)
162 {
163         gchar *pathname;
164
165         pathname = g_strdup_printf("%s/%s.config", STORAGEDIR, ident);
166         if(pathname == NULL)
167                 return;
168
169         storage_delete(pathname);
170
171         g_free(pathname);
172 }
173
174 GKeyFile *__connman_storage_open_service(const char *service_id)
175 {
176         gchar *pathname;
177         GKeyFile *keyfile = NULL;
178
179         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
180         if(pathname == NULL)
181                 return NULL;
182
183         keyfile =  storage_load(pathname);
184         if (keyfile) {
185                 g_free(pathname);
186                 return keyfile;
187         }
188
189         g_free(pathname);
190
191         keyfile = g_key_file_new();
192
193         return keyfile;
194 }
195
196 gchar **connman_storage_get_services()
197 {
198         struct dirent *d;
199         gchar *str;
200         DIR *dir;
201         GString *result;
202         gchar **services = NULL;
203         struct stat buf;
204         int ret;
205
206         dir = opendir(STORAGEDIR);
207         if (dir == NULL)
208                 return NULL;
209
210         result = g_string_new(NULL);
211
212         while ((d = readdir(dir))) {
213                 if (strcmp(d->d_name, ".") == 0 ||
214                                 strcmp(d->d_name, "..") == 0 ||
215                                 strncmp(d->d_name, "provider_", 9) == 0)
216                         continue;
217
218                 switch (d->d_type) {
219                 case DT_DIR:
220                         /*
221                          * If the settings file is not found, then
222                          * assume this directory is not a services dir.
223                          */
224                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
225                                                                 d->d_name);
226                         ret = stat(str, &buf);
227                         g_free(str);
228                         if (ret < 0)
229                                 continue;
230
231                         g_string_append_printf(result, "%s/", d->d_name);
232                         break;
233                 }
234         }
235
236         closedir(dir);
237
238         str = g_string_free(result, FALSE);
239         if (str) {
240                 str[strlen(str) - 1] = '\0';
241                 services = g_strsplit(str, "/", -1);
242         }
243         g_free(str);
244
245         return services;
246 }
247
248 GKeyFile *connman_storage_load_service(const char *service_id)
249 {
250         gchar *pathname;
251         GKeyFile *keyfile = NULL;
252
253         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
254         if(pathname == NULL)
255                 return NULL;
256
257         keyfile =  storage_load(pathname);
258         g_free(pathname);
259
260         return keyfile;
261 }
262
263 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
264 {
265         int ret = 0;
266         gchar *pathname, *dirname;
267
268         dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
269         if(dirname == NULL)
270                 return -ENOMEM;
271
272         /* If the dir doesn't exist, create it */
273         if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
274                 if(mkdir(dirname, MODE) < 0) {
275                         if (errno != EEXIST) {
276                                 g_free(dirname);
277                                 return -errno;
278                         }
279                 }
280         }
281
282         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
283
284         g_free(dirname);
285
286         ret = storage_save(keyfile, pathname);
287
288         g_free(pathname);
289
290         return ret;
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 gchar **__connman_storage_get_providers(void)
332 {
333         GSList *list = NULL;
334         int num = 0, i = 0;
335         struct dirent *d;
336         gchar *str;
337         DIR *dir;
338         struct stat buf;
339         int ret;
340         char **providers;
341         GSList *iter;
342
343         dir = opendir(STORAGEDIR);
344         if (dir == NULL)
345                 return NULL;
346
347         while ((d = readdir(dir))) {
348                 if (strcmp(d->d_name, ".") == 0 ||
349                                 strcmp(d->d_name, "..") == 0 ||
350                                 strncmp(d->d_name, "provider_", 9) != 0)
351                         continue;
352
353                 if (d->d_type == DT_DIR) {
354                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
355                                         d->d_name);
356                         ret = stat(str, &buf);
357                         g_free(str);
358                         if (ret < 0)
359                                 continue;
360                         list = g_slist_prepend(list, g_strdup(d->d_name));
361                         num += 1;
362                 }
363         }
364
365         closedir(dir);
366
367         providers = g_try_new0(char *, num + 1);
368         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
369                 if (providers != NULL)
370                         providers[i] = iter->data;
371                 else
372                         g_free(iter->data);
373                 i += 1;
374         }
375         g_slist_free(list);
376
377         return providers;
378 }
379
380 /*
381  * This function migrates keys from default.profile to settings file.
382  * This can be removed once the migration is over.
383 */
384 void __connman_storage_migrate()
385 {
386         gchar *pathname;
387         GKeyFile *keyfile_def = NULL;
388         GKeyFile *keyfile = NULL;
389         GError *error = NULL;
390         connman_bool_t delete_old_config = TRUE;
391         char **services, **keys, *value;
392         int i, k, err;
393         connman_bool_t val;
394
395         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
396         if (pathname == NULL)
397                 return;
398
399         /* If setting file exists, migration has been done. */
400         keyfile = __connman_storage_load_global();
401         if (keyfile) {
402                 g_key_file_free(keyfile);
403                 unlink(pathname);
404                 g_free(pathname);
405                 return;
406         }
407
408         /* If default.profile exists, create new settings file */
409         keyfile_def = storage_load(pathname);
410         if (keyfile_def == NULL)
411                 goto done;
412
413         services = g_key_file_get_groups(keyfile_def, NULL);
414         for (i = 0; services != NULL && services[i] != NULL; i++) {
415                 if (strncmp(services[i], "wifi_", 5) != 0 &&
416                                 strncmp(services[i], "ethernet_", 9) != 0 &&
417                                 strncmp(services[i], "cellular_", 9) != 0 &&
418                                 strncmp(services[i], "bluetooth_", 10) != 0 &&
419                                 strncmp(services[i], "wimax_", 6) != 0 &&
420                                 strncmp(services[i], "vpn_", 4) != 0)
421                         continue;
422
423                 keyfile = connman_storage_load_service(services[i]);
424                 if (keyfile != NULL) {
425                         g_key_file_free(keyfile);
426                         DBG("already exists %s", services[i]);
427                         continue;
428                 }
429
430                 keyfile = g_key_file_new();
431                 if (keyfile == NULL) {
432                         connman_warn("Migrating %s failed", services[i]);
433                         delete_old_config = FALSE;
434                         continue;
435                 }
436
437                 keys = g_key_file_get_keys(keyfile_def, services[i],
438                                 NULL, NULL);
439
440                 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
441                         value = g_key_file_get_value(keyfile_def, services[i],
442                                         keys[k], NULL);
443                         g_key_file_set_value(keyfile, services[i],
444                                         keys[k], value);
445                         g_free(value);
446                 }
447
448                 if (keys != NULL && keys[0] != NULL) {
449                         err = __connman_storage_save_service(keyfile,
450                                         services[i]);
451                         if (err >= 0)
452                                 DBG("migrated %s", services[i]);
453                         else {
454                                 connman_warn("Migrating %s failed %s",
455                                                 services[i], strerror(-err));
456                                 delete_old_config = FALSE;
457                         }
458                 } else
459                         DBG("no keys in %s", services[i]);
460
461                 g_strfreev(keys);
462                 g_key_file_free(keyfile);
463         }
464         g_strfreev(services);
465
466         /* Copy global settings from default.profile to settings. */
467         keyfile = g_key_file_new();
468
469         val = g_key_file_get_boolean(keyfile_def, "global",
470                                         "OfflineMode", &error);
471         if (error != NULL) {
472                 g_clear_error(&error);
473                 val = FALSE;
474         }
475
476         g_key_file_set_boolean(keyfile, "global",
477                                         "OfflineMode", val);
478
479         /* Migrate Powered/Enable state key/value pairs from legacy
480          * settings
481          */
482
483         val = g_key_file_get_boolean(keyfile_def, "WiFi",
484                                         "Enable", &error);
485         if (error != NULL) {
486                 g_clear_error(&error);
487                 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
488                 if (error != NULL) {
489                         g_clear_error(&error);
490                         val = FALSE;
491                 }
492         }
493
494         g_key_file_set_boolean(keyfile, "WiFi",
495                                         "Enable", val);
496
497         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
498                                         "Enable", &error);
499         if (error != NULL) {
500                 g_clear_error(&error);
501                 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
502                 if (error != NULL) {
503                         g_clear_error(&error);
504                         val = FALSE;
505                 }
506         }
507
508         g_key_file_set_boolean(keyfile, "Bluetooth",
509                                         "Enable", val);
510
511         val = g_key_file_get_boolean(keyfile_def, "Wired",
512                                         "Enable", &error);
513         if (error != NULL) {
514                 g_clear_error(&error);
515                 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
516                 if (error != NULL) {
517                         g_clear_error(&error);
518                         val = FALSE;
519                 }
520         }
521
522         g_key_file_set_boolean(keyfile, "Wired",
523                                         "Enable", val);
524
525         val = g_key_file_get_boolean(keyfile_def, "Cellular",
526                                         "Enable", &error);
527         if (error != NULL) {
528                 g_clear_error(&error);
529                 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
530                 if (error != NULL) {
531                         g_clear_error(&error);
532                         val = FALSE;
533                 }
534         }
535
536         g_key_file_set_boolean(keyfile, "Cellular",
537                                         "Enable", val);
538
539         val = g_key_file_get_boolean(keyfile_def, "WiMAX",
540                                         "Enable", &error);
541         if (error != NULL) {
542                 g_clear_error(&error);
543                 val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
544                 if (error != NULL) {
545                         g_clear_error(&error);
546                         val = FALSE;
547                 }
548         }
549
550         g_key_file_set_boolean(keyfile, "WiMAX",
551                                         "Enable", val);
552
553         if (__connman_storage_save_global(keyfile) < 0) {
554                 connman_warn("Migrating global config failed");
555                 delete_old_config = FALSE;
556         }
557
558         g_key_file_free(keyfile);
559
560         g_key_file_free(keyfile_def);
561
562         if (delete_old_config == TRUE) {
563                 DBG("migration done for %s", pathname);
564                 unlink(pathname);
565         }
566 done:
567         g_free(pathname);
568 }