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