core: Remove WiMAX definitions from code
[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(void)
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(void)
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 GKeyFile *__connman_storage_open_service(const char *service_id)
151 {
152         gchar *pathname;
153         GKeyFile *keyfile = NULL;
154
155         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
156         if(pathname == NULL)
157                 return NULL;
158
159         keyfile =  storage_load(pathname);
160         if (keyfile) {
161                 g_free(pathname);
162                 return keyfile;
163         }
164
165         g_free(pathname);
166
167         keyfile = g_key_file_new();
168
169         return keyfile;
170 }
171
172 gchar **connman_storage_get_services(void)
173 {
174         struct dirent *d;
175         gchar *str;
176         DIR *dir;
177         GString *result;
178         gchar **services = NULL;
179         struct stat buf;
180         int ret;
181
182         dir = opendir(STORAGEDIR);
183         if (dir == NULL)
184                 return NULL;
185
186         result = g_string_new(NULL);
187
188         while ((d = readdir(dir))) {
189                 if (strcmp(d->d_name, ".") == 0 ||
190                                 strcmp(d->d_name, "..") == 0 ||
191                                 strncmp(d->d_name, "provider_", 9) == 0)
192                         continue;
193
194                 switch (d->d_type) {
195                 case DT_DIR:
196                 case DT_UNKNOWN:
197                         /*
198                          * If the settings file is not found, then
199                          * assume this directory is not a services dir.
200                          */
201                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
202                                                                 d->d_name);
203                         ret = stat(str, &buf);
204                         g_free(str);
205                         if (ret < 0)
206                                 continue;
207
208                         g_string_append_printf(result, "%s/", d->d_name);
209                         break;
210                 }
211         }
212
213         closedir(dir);
214
215         str = g_string_free(result, FALSE);
216         if (str && str[0] != '\0') {
217                 /*
218                  * Remove the trailing separator so that services doesn't end up
219                  * with an empty element.
220                  */
221                 str[strlen(str) - 1] = '\0';
222                 services = g_strsplit(str, "/", -1);
223         }
224         g_free(str);
225
226         return services;
227 }
228
229 GKeyFile *connman_storage_load_service(const char *service_id)
230 {
231         gchar *pathname;
232         GKeyFile *keyfile = NULL;
233
234         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, SETTINGS);
235         if(pathname == NULL)
236                 return NULL;
237
238         keyfile =  storage_load(pathname);
239         g_free(pathname);
240
241         return keyfile;
242 }
243
244 int __connman_storage_save_service(GKeyFile *keyfile, const char *service_id)
245 {
246         int ret = 0;
247         gchar *pathname, *dirname;
248
249         dirname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
250         if(dirname == NULL)
251                 return -ENOMEM;
252
253         /* If the dir doesn't exist, create it */
254         if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
255                 if(mkdir(dirname, MODE) < 0) {
256                         if (errno != EEXIST) {
257                                 g_free(dirname);
258                                 return -errno;
259                         }
260                 }
261         }
262
263         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
264
265         g_free(dirname);
266
267         ret = storage_save(keyfile, pathname);
268
269         g_free(pathname);
270
271         return ret;
272 }
273
274 static gboolean remove_file(const char *service_id, const char *file)
275 {
276         gchar *pathname;
277         gboolean ret = FALSE;
278
279         pathname = g_strdup_printf("%s/%s/%s", STORAGEDIR, service_id, file);
280         if(pathname == NULL)
281                 return FALSE;
282
283         if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
284                 ret = TRUE;
285         } else if (g_file_test(pathname, G_FILE_TEST_IS_REGULAR) == TRUE) {
286                 unlink(pathname);
287                 ret = TRUE;
288         }
289
290         g_free(pathname);
291         return ret;
292 }
293
294 static gboolean remove_dir(const char *service_id)
295 {
296         gchar *pathname;
297         gboolean ret = FALSE;
298
299         pathname = g_strdup_printf("%s/%s", STORAGEDIR, service_id);
300         if(pathname == NULL)
301                 return FALSE;
302
303         if (g_file_test(pathname, G_FILE_TEST_EXISTS) == FALSE) {
304                 ret = TRUE;
305         } else if (g_file_test(pathname, G_FILE_TEST_IS_DIR) == TRUE) {
306                 rmdir(pathname);
307                 ret = TRUE;
308         }
309
310         g_free(pathname);
311         return ret;
312 }
313
314 gboolean __connman_storage_remove_service(const char *service_id)
315 {
316         gboolean removed;
317
318         /* Remove service configuration file */
319         removed = remove_file(service_id, SETTINGS);
320         if (removed == FALSE)
321                 return FALSE;
322
323         /* Remove the statistics file also */
324         removed = remove_file(service_id, "data");
325         if (removed == FALSE)
326                 return FALSE;
327
328         removed = remove_dir(service_id);
329         if (removed == FALSE)
330                 return FALSE;
331
332         DBG("Removed service dir %s/%s", STORAGEDIR, service_id);
333
334         return TRUE;
335 }
336
337 GKeyFile *__connman_storage_load_provider(const char *identifier)
338 {
339         gchar *pathname;
340         GKeyFile *keyfile;
341
342         pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
343                         identifier, SETTINGS);
344         if (pathname == NULL)
345                 return NULL;
346
347         keyfile = storage_load(pathname);
348         g_free(pathname);
349
350         return keyfile;
351 }
352
353 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
354 {
355         gchar *pathname, *dirname;
356
357         dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
358                         "provider", identifier);
359         if (dirname == NULL)
360                 return;
361
362         if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
363                         mkdir(dirname, MODE) < 0) {
364                 g_free(dirname);
365                 return;
366         }
367
368         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
369         g_free(dirname);
370
371         storage_save(keyfile, pathname);
372         g_free(pathname);
373 }
374
375 static gboolean remove_all(const char *id)
376 {
377         gboolean removed;
378
379         remove_file(id, SETTINGS);
380         remove_file(id, "data");
381
382         removed = remove_dir(id);
383         if (removed == FALSE)
384                 return FALSE;
385
386         return TRUE;
387 }
388
389 gboolean __connman_storage_remove_provider(const char *identifier)
390 {
391         gboolean removed;
392         gchar *id;
393
394         id = g_strdup_printf("%s_%s", "provider", identifier);
395         if (id == NULL)
396                 return FALSE;
397
398         if (remove_all(id) == TRUE)
399                 DBG("Removed provider dir %s/%s", STORAGEDIR, id);
400
401         g_free(id);
402
403         id = g_strdup_printf("%s_%s", "vpn", identifier);
404         if (id == NULL)
405                 return FALSE;
406
407         if ((removed = remove_all(id)) == TRUE)
408                 DBG("Removed vpn dir %s/%s", STORAGEDIR, id);
409
410         g_free(id);
411
412         return removed;
413 }
414
415 gchar **__connman_storage_get_providers(void)
416 {
417         GSList *list = NULL;
418         int num = 0, i = 0;
419         struct dirent *d;
420         gchar *str;
421         DIR *dir;
422         struct stat buf;
423         int ret;
424         char **providers;
425         GSList *iter;
426
427         dir = opendir(STORAGEDIR);
428         if (dir == NULL)
429                 return NULL;
430
431         while ((d = readdir(dir))) {
432                 if (strcmp(d->d_name, ".") == 0 ||
433                                 strcmp(d->d_name, "..") == 0 ||
434                                 strncmp(d->d_name, "provider_", 9) != 0)
435                         continue;
436
437                 if (d->d_type == DT_DIR) {
438                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
439                                         d->d_name);
440                         ret = stat(str, &buf);
441                         g_free(str);
442                         if (ret < 0)
443                                 continue;
444                         list = g_slist_prepend(list, g_strdup(d->d_name));
445                         num += 1;
446                 }
447         }
448
449         closedir(dir);
450
451         providers = g_try_new0(char *, num + 1);
452         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
453                 if (providers != NULL)
454                         providers[i] = iter->data;
455                 else
456                         g_free(iter->data);
457                 i += 1;
458         }
459         g_slist_free(list);
460
461         return providers;
462 }
463
464 /*
465  * This function migrates keys from default.profile to settings file.
466  * This can be removed once the migration is over.
467 */
468 void __connman_storage_migrate(void)
469 {
470         gchar *pathname;
471         GKeyFile *keyfile_def = NULL;
472         GKeyFile *keyfile = NULL;
473         GError *error = NULL;
474         connman_bool_t delete_old_config = TRUE;
475         char **services, **keys, *value;
476         int i, k, err;
477         connman_bool_t val;
478
479         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
480         if (pathname == NULL)
481                 return;
482
483         /* If setting file exists, migration has been done. */
484         keyfile = __connman_storage_load_global();
485         if (keyfile) {
486                 g_key_file_free(keyfile);
487                 unlink(pathname);
488                 g_free(pathname);
489                 return;
490         }
491
492         /* If default.profile exists, create new settings file */
493         keyfile_def = storage_load(pathname);
494         if (keyfile_def == NULL)
495                 goto done;
496
497         services = g_key_file_get_groups(keyfile_def, NULL);
498         for (i = 0; services != NULL && services[i] != NULL; i++) {
499                 if (strncmp(services[i], "wifi_", 5) != 0 &&
500                                 strncmp(services[i], "ethernet_", 9) != 0 &&
501                                 strncmp(services[i], "cellular_", 9) != 0 &&
502                                 strncmp(services[i], "bluetooth_", 10) != 0 &&
503                                 strncmp(services[i], "vpn_", 4) != 0)
504                         continue;
505
506                 keyfile = connman_storage_load_service(services[i]);
507                 if (keyfile != NULL) {
508                         g_key_file_free(keyfile);
509                         DBG("already exists %s", services[i]);
510                         continue;
511                 }
512
513                 keyfile = g_key_file_new();
514                 if (keyfile == NULL) {
515                         connman_warn("Migrating %s failed", services[i]);
516                         delete_old_config = FALSE;
517                         continue;
518                 }
519
520                 keys = g_key_file_get_keys(keyfile_def, services[i],
521                                 NULL, NULL);
522
523                 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
524                         value = g_key_file_get_value(keyfile_def, services[i],
525                                         keys[k], NULL);
526                         g_key_file_set_value(keyfile, services[i],
527                                         keys[k], value);
528                         g_free(value);
529                 }
530
531                 if (keys != NULL && keys[0] != NULL) {
532                         err = __connman_storage_save_service(keyfile,
533                                         services[i]);
534                         if (err >= 0)
535                                 DBG("migrated %s", services[i]);
536                         else {
537                                 connman_warn("Migrating %s failed %s",
538                                                 services[i], strerror(-err));
539                                 delete_old_config = FALSE;
540                         }
541                 } else
542                         DBG("no keys in %s", services[i]);
543
544                 g_strfreev(keys);
545                 g_key_file_free(keyfile);
546         }
547         g_strfreev(services);
548
549         /* Copy global settings from default.profile to settings. */
550         keyfile = g_key_file_new();
551
552         val = g_key_file_get_boolean(keyfile_def, "global",
553                                         "OfflineMode", &error);
554         if (error != NULL) {
555                 g_clear_error(&error);
556                 val = FALSE;
557         }
558
559         g_key_file_set_boolean(keyfile, "global",
560                                         "OfflineMode", val);
561
562         /* Migrate Powered/Enable state key/value pairs from legacy
563          * settings
564          */
565
566         val = g_key_file_get_boolean(keyfile_def, "WiFi",
567                                         "Enable", &error);
568         if (error != NULL) {
569                 g_clear_error(&error);
570                 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
571                 if (error != NULL) {
572                         g_clear_error(&error);
573                         val = FALSE;
574                 }
575         }
576
577         g_key_file_set_boolean(keyfile, "WiFi",
578                                         "Enable", val);
579
580         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
581                                         "Enable", &error);
582         if (error != NULL) {
583                 g_clear_error(&error);
584                 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
585                 if (error != NULL) {
586                         g_clear_error(&error);
587                         val = FALSE;
588                 }
589         }
590
591         g_key_file_set_boolean(keyfile, "Bluetooth",
592                                         "Enable", val);
593
594         val = g_key_file_get_boolean(keyfile_def, "Wired",
595                                         "Enable", &error);
596         if (error != NULL) {
597                 g_clear_error(&error);
598                 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
599                 if (error != NULL) {
600                         g_clear_error(&error);
601                         val = FALSE;
602                 }
603         }
604
605         g_key_file_set_boolean(keyfile, "Wired",
606                                         "Enable", val);
607
608         val = g_key_file_get_boolean(keyfile_def, "Cellular",
609                                         "Enable", &error);
610         if (error != NULL) {
611                 g_clear_error(&error);
612                 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
613                 if (error != NULL) {
614                         g_clear_error(&error);
615                         val = FALSE;
616                 }
617         }
618
619         g_key_file_set_boolean(keyfile, "Cellular",
620                                         "Enable", val);
621
622         if (__connman_storage_save_global(keyfile) < 0) {
623                 connman_warn("Migrating global config failed");
624                 delete_old_config = FALSE;
625         }
626
627         g_key_file_free(keyfile);
628
629         g_key_file_free(keyfile_def);
630
631         if (delete_old_config == TRUE) {
632                 DBG("migration done for %s", pathname);
633                 unlink(pathname);
634         }
635 done:
636         g_free(pathname);
637 }