/
usb_moded-config.c
934 lines (806 loc) · 27.9 KB
1
/**
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
* @file usb_moded-config.c
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
* Copyright (C) 2012-2018 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Philippe De Swert <phdeswer@lumi.maa>
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Reto Zingg <reto.zingg@jollamobile.com>
* @author: Thomas Perl <m@thp.io>
* @author: Slava Monich <slava.monich@jolla.com>
* @author: Martin Jones <martin.jones@jollamobile.com>
* @author: Jarko Poutiainen <jarko.poutiainen@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
* @author: Andrew den Exter <andrew.den.exter@jolla.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
33
34
#include "usb_moded-config-private.h"
35
36
37
38
#include "usb_moded-control.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-dyn-config.h"
39
#include "usb_moded-log.h"
40
#include "usb_moded-modes.h"
41
#include "usb_moded-worker.h"
42
43
44
45
46
#ifdef USE_MER_SSU
# include "usb_moded-ssu.h"
#endif
47
48
49
50
51
52
53
54
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <glob.h>
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- config -- */
static int config_validate_ip (const char *ipadd);
char *config_find_mounts (void);
int config_find_sync (void);
char *config_find_alt_mount (void);
char *config_find_udev_path (void);
char *config_find_udev_subsystem (void);
char *config_check_trigger (void);
char *config_get_trigger_subsystem (void);
char *config_get_trigger_mode (void);
char *config_get_trigger_property (void);
char *config_get_trigger_value (void);
static char *config_get_network_ip (void);
static char *config_get_network_interface (void);
static char *config_get_network_gateway (void);
static char *config_get_network_netmask (void);
static char *config_get_network_nat_interface(void);
static void config_create_conf_file (void);
static int config_get_conf_int (const gchar *entry, const gchar *key);
static char *config_get_conf_string (const gchar *entry, const gchar *key);
static char *config_get_kcmdline_string (const char *entry);
char *config_get_mode_setting (void);
int config_value_changed (GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value);
set_config_result_t config_set_config_setting (const char *entry, const char *key, const char *value);
set_config_result_t config_set_mode_setting (const char *mode);
static char *config_make_modes_string (const char *key, const char *mode_name, int include);
set_config_result_t config_set_hide_mode_setting (const char *mode);
set_config_result_t config_set_unhide_mode_setting (const char *mode);
set_config_result_t config_set_mode_whitelist (const char *whitelist);
set_config_result_t config_set_mode_in_whitelist (const char *mode, int allowed);
set_config_result_t config_set_network_setting (const char *config, const char *setting);
char *config_get_network_setting (const char *config);
static void config_merge_key (GKeyFile *dest, GKeyFile *srce, const char *grp, const char *key);
static void config_merge_group (GKeyFile *dest, GKeyFile *srce, const char *grp);
static void config_merge_file (GKeyFile *dest, GKeyFile *srce);
static int config_glob_error_cb (const char *path, int err);
static GKeyFile *config_read_ini_files (void);
int config_merge_conf_file (void);
char *config_get_android_manufacturer (void);
char *config_get_android_vendor_id (void);
char *config_get_android_product (void);
char *config_get_android_product_id (void);
char *config_get_hidden_modes (void);
char *config_get_mode_whitelist (void);
int config_is_roaming_not_allowed (void);
/* ========================================================================= *
* Functions
* ========================================================================= */
static int config_validate_ip(const char *ipadd)
{
unsigned int b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c) != 4)
116
return -1;
117
118
if ((b1 | b2 | b3 | b4) > 255)
119
return -1;
120
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
121
return -1;
122
123
/* all ok */
return 0;
124
125
}
126
char *config_find_mounts(void)
127
128
{
129
130
131
132
133
134
135
136
char *ret = NULL;
ret = config_get_conf_string(FS_MOUNT_ENTRY, FS_MOUNT_KEY);
if(ret == NULL)
{
ret = g_strdup(FS_MOUNT_DEFAULT);
log_debug("Default mount = %s\n", ret);
}
137
return ret;
138
139
}
140
int config_find_sync(void)
141
142
{
143
return config_get_conf_int(FS_SYNC_ENTRY, FS_SYNC_KEY);
144
145
}
146
char * config_find_alt_mount(void)
147
{
148
return config_get_conf_string(ALT_MOUNT_ENTRY, ALT_MOUNT_KEY);
149
150
}
151
char * config_find_udev_path(void)
152
{
153
return config_get_conf_string(UDEV_PATH_ENTRY, UDEV_PATH_KEY);
154
}
155
156
char * config_find_udev_subsystem(void)
157
{
158
return config_get_conf_string(UDEV_PATH_ENTRY, UDEV_SUBSYSTEM_KEY);
159
}
160
161
char * config_check_trigger(void)
162
{
163
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PATH_KEY);
164
165
}
166
char * config_get_trigger_subsystem(void)
167
{
168
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_UDEV_SUBSYSTEM);
169
170
}
171
char * config_get_trigger_mode(void)
172
{
173
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_MODE_KEY);
174
}
175
176
char * config_get_trigger_property(void)
177
{
178
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_KEY);
179
180
}
181
char * config_get_trigger_value(void)
182
{
183
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_VALUE_KEY);
184
}
185
186
static char * config_get_network_ip(void)
187
{
188
189
190
char * ip = config_get_kcmdline_string(NETWORK_IP_KEY);
if (ip != NULL)
if(!config_validate_ip(ip))
191
return ip;
192
193
return config_get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY);
194
195
}
196
static char * config_get_network_interface(void)
197
{
198
return config_get_conf_string(NETWORK_ENTRY, NETWORK_INTERFACE_KEY);
199
200
}
201
static char * config_get_network_gateway(void)
202
{
203
204
char * gw = config_get_kcmdline_string(NETWORK_GATEWAY_KEY);
if (gw != NULL)
205
return gw;
206
207
return config_get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY);
208
209
}
210
static char * config_get_network_netmask(void)
211
{
212
213
char * netmask = config_get_kcmdline_string(NETWORK_NETMASK_KEY);
if (netmask != NULL)
214
return netmask;
215
216
return config_get_conf_string(NETWORK_ENTRY, NETWORK_NETMASK_KEY);
217
218
}
219
static char * config_get_network_nat_interface(void)
220
{
221
return config_get_conf_string(NETWORK_ENTRY, NETWORK_NAT_INTERFACE_KEY);
222
223
}
224
/* create basic conffile with sensible defaults */
225
static void config_create_conf_file(void)
226
{
227
228
229
230
GKeyFile *settingsfile;
gchar *keyfile;
int dir = 1;
struct stat dir_stat;
231
232
/* since this function can also be called when the dir exists we only create
233
* it if it is missing */
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
if(stat(CONFIG_FILE_DIR, &dir_stat))
{
dir = mkdir(CONFIG_FILE_DIR, 0755);
if(dir < 0)
{
log_warning("Could not create confdir, continuing without configuration!\n");
/* no point in trying to generate the config file if the dir cannot be created */
return;
}
}
settingsfile = g_key_file_new();
g_key_file_set_string(settingsfile, MODE_SETTING_ENTRY, MODE_SETTING_KEY, MODE_DEVELOPER );
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
if(g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL) == 0)
log_debug("Conffile creation failed. Continuing without configuration!\n");
free(keyfile);
g_key_file_free(settingsfile);
}
static int config_get_conf_int(const gchar *entry, const gchar *key)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar **keys, **origkeys;
int ret = 0;
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
log_debug("no conffile, Creating\n");
config_create_conf_file();
}
keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL);
if(keys == NULL)
271
return ret;
272
273
274
origkeys = keys;
while (*keys != NULL)
{
275
276
if(!strcmp(*keys, key))
{
277
278
ret = g_key_file_get_integer(settingsfile, entry, *keys, NULL);
log_debug("%s key value = %d\n", key, ret);
279
280
}
keys++;
281
282
283
}
g_strfreev(origkeys);
g_key_file_free(settingsfile);
284
return ret;
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
}
static char * config_get_conf_string(const gchar *entry, const gchar *key)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar **keys, **origkeys, *tmp_char = NULL;
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
log_debug("No conffile. Creating\n");
config_create_conf_file();
/* should succeed now */
g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
}
keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL);
if(keys == NULL)
304
goto end;
305
306
307
origkeys = keys;
while (*keys != NULL)
{
308
309
if(!strcmp(*keys, key))
{
310
311
312
313
314
tmp_char = g_key_file_get_string(settingsfile, entry, *keys, NULL);
if(tmp_char)
{
log_debug("key %s value = %s\n", key, tmp_char);
}
315
316
}
keys++;
317
318
}
g_strfreev(origkeys);
319
end:
320
g_key_file_free(settingsfile);
321
return tmp_char;
322
323
}
324
325
static char * config_get_kcmdline_string(const char *entry)
326
{
327
328
329
330
331
332
333
334
335
int fd;
char cmdLine[1024];
char *ret = NULL;
int len;
gint argc = 0;
gchar **argv = NULL;
gchar **arg_tokens = NULL, **network_tokens = NULL;
GError *optErr = NULL;
int i;
336
337
338
339
if ((fd = open("/proc/cmdline", O_RDONLY)) < 0)
{
log_debug("could not read /proc/cmdline");
340
return ret;
341
}
342
343
len = read(fd, cmdLine, sizeof cmdLine - 1);
344
close(fd);
345
346
347
348
if (len <= 0)
{
log_debug("kernel command line was empty");
349
return ret;
350
}
351
352
cmdLine[len] = '\0';
353
354
/* we're looking for a piece of the kernel command line matching this:
355
* ip=192.168.3.100::192.168.3.1:255.255.255.0::usb0:on */
356
357
358
if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr))
{
g_error_free(optErr);
359
return ret;
360
}
361
362
363
/* find the ip token */
for (i=0; i < argc; i++)
364
{
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
arg_tokens = g_strsplit(argv[i], "=", 2);
if (!g_ascii_strcasecmp(arg_tokens[0], "usb_moded_ip"))
{
network_tokens = g_strsplit(arg_tokens[1], ":", 7);
/* check if it is for the usb or rndis interface */
if(g_strrstr(network_tokens[5], "usb")|| (g_strrstr(network_tokens[5], "rndis")))
{
if(!strcmp(entry, NETWORK_IP_KEY))
{
g_free(ret), ret = g_strdup(network_tokens[0]);
log_debug("Command line ip = %s\n", ret);
}
if(!strcmp(entry, NETWORK_GATEWAY_KEY))
{
/* gateway might be empty, so we do not want to return an empty string */
if(strlen(network_tokens[2]) > 2)
{
g_free(ret), ret = g_strdup(network_tokens[2]);
log_debug("Command line gateway = %s\n", ret);
}
}
if(!strcmp(entry, NETWORK_NETMASK_KEY))
{
g_free(ret), ret = g_strdup(network_tokens[3]);
log_debug("Command line netmask = %s\n", ret);
}
}
}
g_strfreev(arg_tokens);
}
g_strfreev(argv);
g_strfreev(network_tokens);
398
return ret;
399
400
401
402
}
char * config_get_mode_setting(void)
{
403
char *mode = 0;
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Kernel command line can be used to override settings */
if( (mode = config_get_kcmdline_string(MODE_SETTING_KEY)) )
goto EXIT;
if( (mode = config_get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY)) )
goto EXIT;
/* If no default mode is configured, treat it as charging only */
mode = g_strdup(MODE_CHARGING);
EXIT:
return mode;
417
}
418
419
420
421
422
423
/*
* @param settingsfile: already opened settingsfile we want to read an entry from
* @param entry: entry we want to read
* @param key: key value of the entry we want to read
* @new_value: potentially new value we want to compare against
*
424
* @return: 0 when the old value is the same as the new one, 1 otherwise
425
426
427
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
char *old_value = g_key_file_get_string(settingsfile, entry, key, NULL);
int changed = (g_strcmp0(old_value, new_value) != 0);
g_free(old_value);
return changed;
}
set_config_result_t config_set_config_setting(const char *entry, const char *key, const char *value)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
set_config_result_t ret = SET_CONFIG_ERROR;
gchar *keyfile;
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(test)
{
if(!config_value_changed(settingsfile, entry, key, value))
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
else
{
log_debug("No conffile. Creating.\n");
config_create_conf_file();
}
g_key_file_set_string(settingsfile, entry, key, value);
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
460
461
* the contents will be correctly written to file afterwards.
* Just a precaution. */
462
463
464
465
466
g_key_file_free(settingsfile);
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
g_free(keyfile);
467
return ret;
468
}
469
470
set_config_result_t config_set_mode_setting(const char *mode)
471
{
472
if (strcmp(mode, MODE_ASK) && common_valid_mode(mode))
473
return SET_CONFIG_ERROR;
474
return config_set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode);
475
476
}
477
/* Builds the string used for hidden modes, when hide set to one builds the
478
* new string of hidden modes when adding one, otherwise it will remove one */
479
480
481
482
483
484
485
486
487
488
489
490
491
492
static char * config_make_modes_string(const char *key, const char *mode_name, int include)
{
char *modes_new = 0;
char *modes_old = 0;
gchar **modes_arr = 0;
GString *modes_tmp = 0;
int i;
/* Get current comma separated list of hidden modes */
modes_old = config_get_conf_string(MODE_SETTING_ENTRY, key);
if(!modes_old)
{
modes_old = g_strdup("");
}
493
494
modes_arr = g_strsplit(modes_old, ",", 0);
495
496
modes_tmp = g_string_new(NULL);
497
498
for(i = 0; modes_arr[i] != NULL; i++)
499
{
500
501
502
503
504
if(strlen(modes_arr[i]) == 0)
{
/* Skip any empty strings */
continue;
}
505
506
507
508
509
510
if(!strcmp(modes_arr[i], mode_name))
{
/* When unhiding, just skip all matching entries */
if(!include)
continue;
511
512
513
514
/* When hiding, keep the 1st match and ignore the rest */
include = 0;
}
515
516
517
518
519
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, modes_arr[i]);
}
520
521
522
523
524
525
526
527
if(include)
{
/* Adding a hidden mode and no matching entry was found */
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, mode_name);
}
528
529
modes_new = g_string_free(modes_tmp, FALSE), modes_tmp = 0;
530
531
g_strfreev(modes_arr), modes_arr = 0;
532
533
g_free(modes_old), modes_old = 0;
534
535
return modes_new;
536
537
}
538
set_config_result_t config_set_hide_mode_setting(const char *mode)
539
{
540
set_config_result_t ret = SET_CONFIG_UNCHANGED;
541
542
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 1);
543
544
545
546
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
547
548
if(ret == SET_CONFIG_UPDATED) {
549
550
551
common_send_hidden_modes_signal();
common_send_supported_modes_signal();
common_send_available_modes_signal();
552
}
553
554
g_free(hidden_modes);
555
556
return ret;
557
558
}
559
set_config_result_t config_set_unhide_mode_setting(const char *mode)
560
{
561
set_config_result_t ret = SET_CONFIG_UNCHANGED;
562
563
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 0);
564
565
566
567
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
568
569
if(ret == SET_CONFIG_UPDATED) {
570
571
572
common_send_hidden_modes_signal();
common_send_supported_modes_signal();
common_send_available_modes_signal();
573
}
574
575
g_free(hidden_modes);
576
577
return ret;
578
579
}
580
set_config_result_t config_set_mode_whitelist(const char *whitelist)
581
{
582
set_config_result_t ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist);
583
584
585
586
if(ret == SET_CONFIG_UPDATED) {
char *mode_setting;
const char *current_mode;
587
588
mode_setting = config_get_mode_setting();
589
if (strcmp(mode_setting, MODE_ASK) && common_valid_mode(mode_setting))
590
591
config_set_mode_setting(MODE_ASK);
g_free(mode_setting);
592
593
current_mode = control_get_usb_mode();
594
595
596
if (!strcmp(current_mode, MODE_UNDEFINED)) {
/* Disconnected -> do nothing */
}
597
else if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && common_valid_mode(current_mode)) {
598
599
/* Invalid mode that is not MODE_ASK or MODE_CHARGING_FALLBACK
* -> switch to MODE_CHARGING_FALLBACK */
600
control_set_usb_mode(MODE_CHARGING_FALLBACK);
601
}
602
603
umdbus_send_whitelisted_modes_signal(whitelist);
604
common_send_available_modes_signal();
605
}
606
607
return ret;
608
609
}
610
set_config_result_t config_set_mode_in_whitelist(const char *mode, int allowed)
611
{
612
set_config_result_t ret = SET_CONFIG_UNCHANGED;
613
614
char *whitelist = config_make_modes_string(MODE_WHITELIST_KEY, mode, allowed);
615
616
617
618
if (whitelist) {
ret = config_set_mode_whitelist(whitelist);
}
619
620
g_free(whitelist);
621
622
return ret;
623
624
}
625
626
627
628
/*
* @param config : the key to be set
* @param setting : The value to be set
*/
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
set_config_result_t config_set_network_setting(const char *config, const char *setting)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar *keyfile;
if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
if(config_validate_ip(setting) != 0)
return SET_CONFIG_ERROR;
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
{
set_config_result_t ret = SET_CONFIG_ERROR;
if (test)
{
if(!config_value_changed(settingsfile, NETWORK_ENTRY, config, setting))
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
else
{
log_debug("No conffile. Creating.\n");
config_create_conf_file();
}
g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting);
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
662
663
* the contents will be correctly written to file afterwards.
* Just a precaution. */
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
g_key_file_free(settingsfile);
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
free(keyfile);
return ret;
}
else
{
g_key_file_free(settingsfile);
return SET_CONFIG_ERROR;
}
}
char * config_get_network_setting(const char *config)
{
char * ret = 0;
680
mode_list_elem_t *data;
681
682
683
684
685
686
687
688
689
690
691
if(!strcmp(config, NETWORK_IP_KEY))
{
ret = config_get_network_ip();
if(!ret)
ret = strdup("192.168.2.15");
}
else if(!strcmp(config, NETWORK_INTERFACE_KEY))
{
/* check main configuration before using
692
* the information from the specific mode */
693
694
695
696
697
ret = config_get_network_interface();
if(ret)
goto end;
/* no interface override specified, let's use the one
698
* from the mode config */
699
data = worker_get_usb_mode_data();
700
701
702
703
704
705
706
707
708
709
710
if(data)
{
if(data->network_interface)
{
ret = strdup(data->network_interface);
goto end;
}
}
ret = strdup("usb0");
}
else if(!strcmp(config, NETWORK_GATEWAY_KEY))
711
return config_get_network_gateway();
712
713
714
715
716
717
718
else if(!strcmp(config, NETWORK_NETMASK_KEY))
{
ret = config_get_network_netmask();
if(!ret)
ret = strdup("255.255.255.0");
}
else if(!strcmp(config, NETWORK_NAT_INTERFACE_KEY))
719
return config_get_network_nat_interface();
720
721
else
/* no matching keys, return error */
722
return NULL;
723
end:
724
return ret;
725
726
}
727
728
729
730
731
732
733
734
735
736
/**
* Merge value from one keyfile to another
*
* Existing values will be overridden
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
* @param grp value group to merge
* @param key value key to merge
*/
737
738
739
740
741
742
743
744
745
static void config_merge_key(GKeyFile *dest, GKeyFile *srce,
const char *grp, const char *key)
{
gchar *val = g_key_file_get_value(srce, grp, key, 0);
if( val ) {
log_debug("[%s] %s = %s", grp, key, val);
g_key_file_set_value(dest, grp, key, val);
g_free(val);
}
746
}
747
748
749
750
751
752
753
754
/**
* Merge group of values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
* @param grp value group to merge
*/
755
756
757
758
759
760
761
762
763
static void config_merge_group(GKeyFile *dest, GKeyFile *srce,
const char *grp)
{
gchar **key = g_key_file_get_keys(srce, grp, 0, 0);
if( key ) {
for( size_t i = 0; key[i]; ++i )
config_merge_key(dest, srce, grp, key[i]);
g_strfreev(key);
}
764
}
765
766
767
768
769
770
771
/**
* Merge all groups and values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
*/
772
static void config_merge_file(GKeyFile *dest, GKeyFile *srce)
773
{
774
gchar **grp = g_key_file_get_groups(srce, 0);
775
776
777
778
779
780
if( grp ) {
for( size_t i = 0; grp[i]; ++i )
config_merge_group(dest, srce, grp[i]);
g_strfreev(grp);
}
781
782
783
784
785
786
787
788
789
790
}
/**
* Callback function for logging errors within glob()
*
* @param path path to file/dir where error occurred
* @param err errno that occurred
*
* @return 0 (= do not stop glob)
*/
791
static int config_glob_error_cb(const char *path, int err)
792
{
793
794
log_debug("%s: glob: %s", path, g_strerror(err));
return 0;
795
796
797
798
799
800
801
}
/**
* Read *.ini files on CONFIG_FILE_DIR in the order of [0-9][A-Z][a-z]
*
* @return the in memory value-pair file.
*/
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
static GKeyFile *config_read_ini_files(void)
{
static const char pattern[] = CONFIG_FILE_DIR"/*.ini";
GKeyFile *ini = g_key_file_new();
glob_t gb;
memset(&gb, 0, sizeof gb);
if( glob(pattern, 0, config_glob_error_cb, &gb) != 0 ) {
log_debug("no configuration ini-files found");
g_key_file_free(ini);
ini = NULL;
goto exit;
}
for( size_t i = 0; i < gb.gl_pathc; ++i ) {
const char *path = gb.gl_pathv[i];
GError *err = 0;
GKeyFile *tmp = g_key_file_new();
if( !g_key_file_load_from_file(tmp, path, 0, &err) ) {
log_debug("%s: can't load: %s", path, err->message);
} else {
log_debug("processing %s ...", path);
config_merge_file(ini, tmp);
}
g_clear_error(&err);
g_key_file_free(tmp);
}
832
exit:
833
834
globfree(&gb);
return ini;
835
}
836
837
838
839
840
841
842
/**
* Read the *.ini files and create/overwrite FS_MOUNT_CONFIG_FILE with
* the merged data.
*
* @return 0 on failure
*/
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
int config_merge_conf_file(void)
{
GString *keyfile_string = NULL;
GKeyFile *settingsfile,*tempfile;
int ret = 0;
settingsfile = config_read_ini_files();
if (!settingsfile)
{
log_debug("No configuration. Creating defaults.");
config_create_conf_file();
/* There was no configuration so no info to be merged */
return ret;
}
tempfile = g_key_file_new();
if (g_key_file_load_from_file(tempfile, FS_MOUNT_CONFIG_FILE,
G_KEY_FILE_NONE,NULL)) {
if (!g_strcmp0(g_key_file_to_data(settingsfile, NULL, NULL),
g_key_file_to_data(tempfile, NULL, NULL)))
goto out;
}
log_debug("Merging configuration");
keyfile_string = g_string_new(NULL);
keyfile_string = g_string_append(keyfile_string,
g_key_file_to_data(settingsfile,
NULL, NULL));
if (keyfile_string) {
ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE,
keyfile_string->str,-1, NULL);
g_string_free(keyfile_string, TRUE);
}
876
out:
877
878
879
g_key_file_free(tempfile);
g_key_file_free(settingsfile);
return ret;
880
}
881
882
char * config_get_android_manufacturer(void)
883
{
884
#ifdef USE_MER_SSU
885
886
887
888
889
890
891
/* If SSU can provide manufacturer name, use it. Otherwise fall
* back to using the name specified in configuration files. */
char *ssu_name = ssu_get_manufacturer_name();
if( ssu_name )
{
return ssu_name;
}
892
893
#endif
894
return config_get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY);
895
}
896
897
char * config_get_android_vendor_id(void)
898
{
899
return config_get_conf_string(ANDROID_ENTRY, ANDROID_VENDOR_ID_KEY);
900
901
}
902
char * config_get_android_product(void)
903
{
904
#ifdef USE_MER_SSU
905
906
907
908
909
910
911
/* If SSU can provide device model name, use it. Otherwise fall
* back to using the name specified in configuration files. */
char *ssu_name = ssu_get_product_name();
if( ssu_name )
{
return ssu_name;
}
912
913
#endif
914
return config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY);
915
916
}
917
char * config_get_android_product_id(void)
918
{
919
return config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY);
920
921
}
922
char * config_get_hidden_modes(void)
923
{
924
return config_get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY);
925
}
926
char * config_get_mode_whitelist(void)
927
{
928
return config_get_conf_string(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY);
929
}
930
931
int config_is_roaming_not_allowed(void)
932
{
933
return config_get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY);
934
}