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