service: Clean up append_nameservers() helper function
[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 GKeyFile *__connman_storage_load_provider(const char *identifier)
270 {
271         gchar *pathname;
272         GKeyFile *keyfile;
273
274         pathname = g_strdup_printf("%s/%s_%s/%s", STORAGEDIR, "provider",
275                         identifier, SETTINGS);
276         if (pathname == NULL)
277                 return NULL;
278
279         keyfile = storage_load(pathname);
280         g_free(pathname);
281
282         return keyfile;
283 }
284
285 void __connman_storage_save_provider(GKeyFile *keyfile, const char *identifier)
286 {
287         gchar *pathname, *dirname;
288
289         dirname = g_strdup_printf("%s/%s_%s", STORAGEDIR,
290                         "provider", identifier);
291         if (dirname == NULL)
292                 return;
293
294         if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == FALSE &&
295                         mkdir(dirname, MODE) < 0) {
296                 g_free(dirname);
297                 return;
298         }
299
300         pathname = g_strdup_printf("%s/%s", dirname, SETTINGS);
301         g_free(dirname);
302
303         storage_save(keyfile, pathname);
304         g_free(pathname);
305 }
306
307 gchar **__connman_storage_get_providers(void)
308 {
309         GSList *list = NULL;
310         int num = 0, i = 0;
311         struct dirent *d;
312         gchar *str;
313         DIR *dir;
314         struct stat buf;
315         int ret;
316         char **providers;
317         GSList *iter;
318
319         dir = opendir(STORAGEDIR);
320         if (dir == NULL)
321                 return NULL;
322
323         while ((d = readdir(dir))) {
324                 if (strcmp(d->d_name, ".") == 0 ||
325                                 strcmp(d->d_name, "..") == 0 ||
326                                 strncmp(d->d_name, "provider_", 9) != 0)
327                         continue;
328
329                 if (d->d_type == DT_DIR) {
330                         str = g_strdup_printf("%s/%s/settings", STORAGEDIR,
331                                         d->d_name);
332                         ret = stat(str, &buf);
333                         g_free(str);
334                         if (ret < 0)
335                                 continue;
336                         list = g_slist_prepend(list, g_strdup(d->d_name));
337                         num += 1;
338                 }
339         }
340
341         closedir(dir);
342
343         providers = g_try_new0(char *, num + 1);
344         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
345                 if (providers != NULL)
346                         providers[i] = iter->data;
347                 else
348                         g_free(iter->data);
349                 i += 1;
350         }
351         g_slist_free(list);
352
353         return providers;
354 }
355
356 /*
357  * This function migrates keys from default.profile to settings file.
358  * This can be removed once the migration is over.
359 */
360 void __connman_storage_migrate()
361 {
362         gchar *pathname;
363         GKeyFile *keyfile_def = NULL;
364         GKeyFile *keyfile = NULL;
365         GError *error = NULL;
366         connman_bool_t delete_old_config = TRUE;
367         char **services, **keys, *value;
368         int i, k, err;
369         connman_bool_t val;
370
371         pathname = g_strdup_printf("%s/%s", STORAGEDIR, DEFAULT);
372         if (pathname == NULL)
373                 return;
374
375         /* If setting file exists, migration has been done. */
376         keyfile = __connman_storage_load_global();
377         if (keyfile) {
378                 g_key_file_free(keyfile);
379                 unlink(pathname);
380                 g_free(pathname);
381                 return;
382         }
383
384         /* If default.profile exists, create new settings file */
385         keyfile_def = storage_load(pathname);
386         if (keyfile_def == NULL)
387                 goto done;
388
389         services = g_key_file_get_groups(keyfile_def, NULL);
390         for (i = 0; services != NULL && services[i] != NULL; i++) {
391                 if (strncmp(services[i], "wifi_", 5) != 0 &&
392                                 strncmp(services[i], "ethernet_", 9) != 0 &&
393                                 strncmp(services[i], "cellular_", 9) != 0 &&
394                                 strncmp(services[i], "bluetooth_", 10) != 0 &&
395                                 strncmp(services[i], "wimax_", 6) != 0 &&
396                                 strncmp(services[i], "vpn_", 4) != 0)
397                         continue;
398
399                 keyfile = connman_storage_load_service(services[i]);
400                 if (keyfile != NULL) {
401                         g_key_file_free(keyfile);
402                         DBG("already exists %s", services[i]);
403                         continue;
404                 }
405
406                 keyfile = g_key_file_new();
407                 if (keyfile == NULL) {
408                         connman_warn("Migrating %s failed", services[i]);
409                         delete_old_config = FALSE;
410                         continue;
411                 }
412
413                 keys = g_key_file_get_keys(keyfile_def, services[i],
414                                 NULL, NULL);
415
416                 for (k = 0; keys != NULL && keys[k] != NULL; k++) {
417                         value = g_key_file_get_value(keyfile_def, services[i],
418                                         keys[k], NULL);
419                         g_key_file_set_value(keyfile, services[i],
420                                         keys[k], value);
421                         g_free(value);
422                 }
423
424                 if (keys != NULL && keys[0] != NULL) {
425                         err = __connman_storage_save_service(keyfile,
426                                         services[i]);
427                         if (err >= 0)
428                                 DBG("migrated %s", services[i]);
429                         else {
430                                 connman_warn("Migrating %s failed %s",
431                                                 services[i], strerror(-err));
432                                 delete_old_config = FALSE;
433                         }
434                 } else
435                         DBG("no keys in %s", services[i]);
436
437                 g_strfreev(keys);
438                 g_key_file_free(keyfile);
439         }
440         g_strfreev(services);
441
442         /* Copy global settings from default.profile to settings. */
443         keyfile = g_key_file_new();
444
445         val = g_key_file_get_boolean(keyfile_def, "global",
446                                         "OfflineMode", &error);
447         if (error != NULL) {
448                 g_clear_error(&error);
449                 val = FALSE;
450         }
451
452         g_key_file_set_boolean(keyfile, "global",
453                                         "OfflineMode", val);
454
455         /* Migrate Powered/Enable state key/value pairs from legacy
456          * settings
457          */
458
459         val = g_key_file_get_boolean(keyfile_def, "WiFi",
460                                         "Enable", &error);
461         if (error != NULL) {
462                 g_clear_error(&error);
463                 val = g_key_file_get_boolean(keyfile_def, "device_Wireless", "Powered", &error);
464                 if (error != NULL) {
465                         g_clear_error(&error);
466                         val = FALSE;
467                 }
468         }
469
470         g_key_file_set_boolean(keyfile, "WiFi",
471                                         "Enable", val);
472
473         val = g_key_file_get_boolean(keyfile_def, "Bluetooth",
474                                         "Enable", &error);
475         if (error != NULL) {
476                 g_clear_error(&error);
477                 val = g_key_file_get_boolean(keyfile_def, "device_Bluetooth", "Powered", &error);
478                 if (error != NULL) {
479                         g_clear_error(&error);
480                         val = FALSE;
481                 }
482         }
483
484         g_key_file_set_boolean(keyfile, "Bluetooth",
485                                         "Enable", val);
486
487         val = g_key_file_get_boolean(keyfile_def, "Wired",
488                                         "Enable", &error);
489         if (error != NULL) {
490                 g_clear_error(&error);
491                 val = g_key_file_get_boolean(keyfile_def, "device_Ethernet", "Powered", &error);
492                 if (error != NULL) {
493                         g_clear_error(&error);
494                         val = FALSE;
495                 }
496         }
497
498         g_key_file_set_boolean(keyfile, "Wired",
499                                         "Enable", val);
500
501         val = g_key_file_get_boolean(keyfile_def, "Cellular",
502                                         "Enable", &error);
503         if (error != NULL) {
504                 g_clear_error(&error);
505                 val = g_key_file_get_boolean(keyfile_def, "device_Cellular", "Powered", &error);
506                 if (error != NULL) {
507                         g_clear_error(&error);
508                         val = FALSE;
509                 }
510         }
511
512         g_key_file_set_boolean(keyfile, "Cellular",
513                                         "Enable", val);
514
515         val = g_key_file_get_boolean(keyfile_def, "WiMAX",
516                                         "Enable", &error);
517         if (error != NULL) {
518                 g_clear_error(&error);
519                 val = g_key_file_get_boolean(keyfile_def, "device_WiMAX", "Powered", &error);
520                 if (error != NULL) {
521                         g_clear_error(&error);
522                         val = FALSE;
523                 }
524         }
525
526         g_key_file_set_boolean(keyfile, "WiMAX",
527                                         "Enable", val);
528
529         if (__connman_storage_save_global(keyfile) < 0) {
530                 connman_warn("Migrating global config failed");
531                 delete_old_config = FALSE;
532         }
533
534         g_key_file_free(keyfile);
535
536         g_key_file_free(keyfile_def);
537
538         if (delete_old_config == TRUE) {
539                 DBG("migration done for %s", pathname);
540                 unlink(pathname);
541         }
542 done:
543         g_free(pathname);
544 }