storage.c: Search for settings file even if d_type is DT_UNKNOWN.
[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 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()
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 gchar **__connman_storage_get_providers(void)
376 {
377         GSList *list = NULL;
378         int num = 0, i = 0;
379         struct dirent *d;
380         gchar *str;
381         DIR *dir;
382         struct stat buf;
383         int ret;
384         char **providers;
385         GSList *iter;
386
387         dir = opendir(STORAGEDIR);
388         if (dir == NULL)
389                 return NULL;
390
391         while ((d = readdir(dir))) {
392                 if (strcmp(d->d_name, ".") == 0 ||
393                                 strcmp(d->d_name, "..") == 0 ||
394                                 strncmp(d->d_name, "provider_", 9) != 0)
395                         continue;
396
397                 if (d->d_type == DT_DIR) {
398                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
399                                         d->d_name);
400                         ret = stat(str, &buf);
401                         g_free(str);
402                         if (ret < 0)
403                                 continue;
404                         list = g_slist_prepend(list, g_strdup(d->d_name));
405                         num += 1;
406                 }
407         }
408
409         closedir(dir);
410
411         providers = g_try_new0(char *, num + 1);
412         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
413                 if (providers != NULL)
414                         providers[i] = iter->data;
415                 else
416                         g_free(iter->data);
417                 i += 1;
418         }
419         g_slist_free(list);
420
421         return providers;
422 }
423
424 /*
425  * This function migrates keys from default.profile to settings file.
426  * This can be removed once the migration is over.
427 */
428 void __connman_storage_migrate()
429 {
430         gchar *pathname;
431         GKeyFile *keyfile_def = NULL;
432         GKeyFile *keyfile = NULL;
433         GError *error = NULL;
434         connman_bool_t delete_old_config = TRUE;
435         char **services, **keys, *value;
436         int i, k, err;
437         connman_bool_t val;
438
439         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
440         if (pathname == NULL)
441                 return;
442
443         /* If setting file exists, migration has been done. */
444         keyfile = __connman_storage_load_global();
445         if (keyfile) {
446                 g_key_file_free(keyfile);
447                 unlink(pathname);
448                 g_free(pathname);
449                 return;
450         }
451
452         /* If default.profile exists, create new settings file */
453         keyfile_def = storage_load(pathname);
454         if (keyfile_def == NULL)
455                 goto done;
456
457         services = g_key_file_get_groups(keyfile_def, NULL);
458         for (i = 0; services != NULL && services[i] != NULL; i++) {
459                 if (strncmp(services[i], "wifi_", 5) != 0 &&
460                                 strncmp(services[i], "ethernet_", 9) != 0 &&
461                                 strncmp(services[i], "cellular_", 9) != 0 &&
462                                 strncmp(services[i], "bluetooth_", 10) != 0 &&
463                                 strncmp(services[i], "wimax_", 6) != 0 &&
464                                 strncmp(services[i], "vpn_", 4) != 0)
465                         continue;
466
467                 keyfile = connman_storage_load_service(services[i]);
468                 if (keyfile != NULL) {
469                         g_key_file_free(keyfile);
470                         DBG("already exists %s", services[i]);
471                         continue;
472                 }
473
474                 keyfile = g_key_file_new();
475                 if (keyfile == NULL) {
476                         connman_warn("Migrating %s failed", services[i]);
477                         delete_old_config = FALSE;
478                         continue;
479                 }
480
481                 keys = g_key_file_get_keys(keyfile_def, services[i],
482                                 NULL, NULL);
483
484                 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
485                         value = g_key_file_get_value(keyfile_def, services[i],
486                                         keys[k], NULL);
487                         g_key_file_set_value(keyfile, services[i],
488                                         keys[k], value);
489                         g_free(value);
490                 }
491
492                 if (keys != NULL && keys[0] != NULL) {
493                         err = __connman_storage_save_service(keyfile,
494                                         services[i]);
495                         if (err >= 0)
496                                 DBG("migrated %s", services[i]);
497                         else {
498                                 connman_warn("Migrating %s failed %s",
499                                                 services[i], strerror(-err));
500                                 delete_old_config = FALSE;
501                         }
502                 } else
503                         DBG("no keys in %s", services[i]);
504
505                 g_strfreev(keys);
506                 g_key_file_free(keyfile);
507         }
508         g_strfreev(services);
509
510         /* Copy global settings from default.profile to settings. */
511         keyfile = g_key_file_new();
512
513         val = g_key_file_get_boolean(keyfile_def, "global",
514                                         "OfflineMode", &error);
515         if (error != NULL) {
516                 g_clear_error(&error);
517                 val = FALSE;
518         }
519
520         g_key_file_set_boolean(keyfile, "global",
521                                         "OfflineMode", val);
522
523         /* Migrate Powered/Enable state key/value pairs from legacy
524          * settings
525          */
526
527         val = g_key_file_get_boolean(keyfile_def, "WiFi",
528                                         "Enable", &error);
529         if (error != NULL) {
530                 g_clear_error(&error);
531                 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
532                 if (error != NULL) {
533                         g_clear_error(&error);
534                         val = FALSE;
535                 }
536         }
537
538         g_key_file_set_boolean(keyfile, "WiFi",
539                                         "Enable", val);
540
541         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
542                                         "Enable", &error);
543         if (error != NULL) {
544                 g_clear_error(&error);
545                 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
546                 if (error != NULL) {
547                         g_clear_error(&error);
548                         val = FALSE;
549                 }
550         }
551
552         g_key_file_set_boolean(keyfile, "Bluetooth",
553                                         "Enable", val);
554
555         val = g_key_file_get_boolean(keyfile_def, "Wired",
556                                         "Enable", &error);
557         if (error != NULL) {
558                 g_clear_error(&error);
559                 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
560                 if (error != NULL) {
561                         g_clear_error(&error);
562                         val = FALSE;
563                 }
564         }
565
566         g_key_file_set_boolean(keyfile, "Wired",
567                                         "Enable", val);
568
569         val = g_key_file_get_boolean(keyfile_def, "Cellular",
570                                         "Enable", &error);
571         if (error != NULL) {
572                 g_clear_error(&error);
573                 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
574                 if (error != NULL) {
575                         g_clear_error(&error);
576                         val = FALSE;
577                 }
578         }
579
580         g_key_file_set_boolean(keyfile, "Cellular",
581                                         "Enable", val);
582
583         val = g_key_file_get_boolean(keyfile_def, "WiMAX",
584                                         "Enable", &error);
585         if (error != NULL) {
586                 g_clear_error(&error);
587                 val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
588                 if (error != NULL) {
589                         g_clear_error(&error);
590                         val = FALSE;
591                 }
592         }
593
594         g_key_file_set_boolean(keyfile, "WiMAX",
595                                         "Enable", val);
596
597         if (__connman_storage_save_global(keyfile) < 0) {
598                 connman_warn("Migrating global config failed");
599                 delete_old_config = FALSE;
600         }
601
602         g_key_file_free(keyfile);
603
604         g_key_file_free(keyfile_def);
605
606         if (delete_old_config == TRUE) {
607                 DBG("migration done for %s", pathname);
608                 unlink(pathname);
609         }
610 done:
611         g_free(pathname);
612 }