Skip to content

Latest commit

 

History

History
934 lines (806 loc) · 27.9 KB

usb_moded-config.c

File metadata and controls

934 lines (806 loc) · 27.9 KB
 
Mar 22, 2011
Mar 22, 2011
1
/**
Aug 24, 2018
Aug 24, 2018
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
*/
Mar 22, 2011
Mar 22, 2011
33
Sep 11, 2011
Sep 11, 2011
34
#include "usb_moded-config-private.h"
Sep 5, 2018
Sep 5, 2018
35
36
37
38
#include "usb_moded-control.h"
#include "usb_moded-dbus-private.h"
#include "usb_moded-dyn-config.h"
Mar 22, 2011
Mar 22, 2011
39
#include "usb_moded-log.h"
Nov 13, 2012
Nov 13, 2012
40
#include "usb_moded-modes.h"
Sep 5, 2018
Sep 5, 2018
41
#include "usb_moded-worker.h"
Mar 22, 2011
Mar 22, 2011
42
May 11, 2016
May 11, 2016
43
44
45
46
#ifdef USE_MER_SSU
# include "usb_moded-ssu.h"
#endif
Sep 5, 2018
Sep 5, 2018
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>
Aug 24, 2018
Aug 24, 2018
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)
Sep 5, 2018
Sep 5, 2018
116
return -1;
Apr 20, 2014
Apr 20, 2014
117
Aug 24, 2018
Aug 24, 2018
118
if ((b1 | b2 | b3 | b4) > 255)
Sep 5, 2018
Sep 5, 2018
119
return -1;
Aug 24, 2018
Aug 24, 2018
120
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
Sep 5, 2018
Sep 5, 2018
121
return -1;
Aug 24, 2018
Aug 24, 2018
122
123
/* all ok */
return 0;
Apr 20, 2014
Apr 20, 2014
124
125
}
Aug 24, 2018
Aug 24, 2018
126
char *config_find_mounts(void)
Mar 22, 2011
Mar 22, 2011
127
128
{
Aug 24, 2018
Aug 24, 2018
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);
}
Sep 5, 2018
Sep 5, 2018
137
return ret;
Mar 22, 2011
Mar 22, 2011
138
139
}
Aug 24, 2018
Aug 24, 2018
140
int config_find_sync(void)
Mar 22, 2011
Mar 22, 2011
141
142
{
Sep 5, 2018
Sep 5, 2018
143
return config_get_conf_int(FS_SYNC_ENTRY, FS_SYNC_KEY);
Mar 22, 2011
Mar 22, 2011
144
145
}
Aug 24, 2018
Aug 24, 2018
146
char * config_find_alt_mount(void)
Mar 22, 2011
Mar 22, 2011
147
{
Sep 5, 2018
Sep 5, 2018
148
return config_get_conf_string(ALT_MOUNT_ENTRY, ALT_MOUNT_KEY);
Apr 6, 2011
Apr 6, 2011
149
150
}
Aug 24, 2018
Aug 24, 2018
151
char * config_find_udev_path(void)
Apr 7, 2011
Apr 7, 2011
152
{
Sep 5, 2018
Sep 5, 2018
153
return config_get_conf_string(UDEV_PATH_ENTRY, UDEV_PATH_KEY);
Apr 7, 2011
Apr 7, 2011
154
}
Jan 13, 2012
Jan 13, 2012
155
Aug 24, 2018
Aug 24, 2018
156
char * config_find_udev_subsystem(void)
Jan 13, 2012
Jan 13, 2012
157
{
Sep 5, 2018
Sep 5, 2018
158
return config_get_conf_string(UDEV_PATH_ENTRY, UDEV_SUBSYSTEM_KEY);
Jan 13, 2012
Jan 13, 2012
159
}
Apr 7, 2011
Apr 7, 2011
160
Aug 24, 2018
Aug 24, 2018
161
char * config_check_trigger(void)
May 10, 2011
May 10, 2011
162
{
Sep 5, 2018
Sep 5, 2018
163
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PATH_KEY);
May 10, 2011
May 10, 2011
164
165
}
Aug 24, 2018
Aug 24, 2018
166
char * config_get_trigger_subsystem(void)
May 16, 2011
May 16, 2011
167
{
Sep 5, 2018
Sep 5, 2018
168
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_UDEV_SUBSYSTEM);
May 16, 2011
May 16, 2011
169
170
}
Aug 24, 2018
Aug 24, 2018
171
char * config_get_trigger_mode(void)
May 10, 2011
May 10, 2011
172
{
Sep 5, 2018
Sep 5, 2018
173
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_MODE_KEY);
May 10, 2011
May 10, 2011
174
}
May 16, 2011
May 16, 2011
175
Aug 24, 2018
Aug 24, 2018
176
char * config_get_trigger_property(void)
May 16, 2011
May 16, 2011
177
{
Sep 5, 2018
Sep 5, 2018
178
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_KEY);
May 16, 2011
May 16, 2011
179
180
}
Aug 24, 2018
Aug 24, 2018
181
char * config_get_trigger_value(void)
May 16, 2011
May 16, 2011
182
{
Sep 5, 2018
Sep 5, 2018
183
return config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_VALUE_KEY);
May 16, 2011
May 16, 2011
184
}
May 10, 2011
May 10, 2011
185
Aug 24, 2018
Aug 24, 2018
186
static char * config_get_network_ip(void)
Aug 7, 2012
Aug 7, 2012
187
{
Aug 24, 2018
Aug 24, 2018
188
189
190
char * ip = config_get_kcmdline_string(NETWORK_IP_KEY);
if (ip != NULL)
if(!config_validate_ip(ip))
Sep 5, 2018
Sep 5, 2018
191
return ip;
May 17, 2013
May 17, 2013
192
Sep 5, 2018
Sep 5, 2018
193
return config_get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY);
Aug 7, 2012
Aug 7, 2012
194
195
}
Aug 24, 2018
Aug 24, 2018
196
static char * config_get_network_interface(void)
Aug 7, 2012
Aug 7, 2012
197
{
Sep 5, 2018
Sep 5, 2018
198
return config_get_conf_string(NETWORK_ENTRY, NETWORK_INTERFACE_KEY);
Aug 7, 2012
Aug 7, 2012
199
200
}
Aug 24, 2018
Aug 24, 2018
201
static char * config_get_network_gateway(void)
Aug 12, 2012
Aug 12, 2012
202
{
Aug 24, 2018
Aug 24, 2018
203
204
char * gw = config_get_kcmdline_string(NETWORK_GATEWAY_KEY);
if (gw != NULL)
Sep 5, 2018
Sep 5, 2018
205
return gw;
May 17, 2013
May 17, 2013
206
Sep 5, 2018
Sep 5, 2018
207
return config_get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY);
Aug 12, 2012
Aug 12, 2012
208
209
}
Aug 24, 2018
Aug 24, 2018
210
static char * config_get_network_netmask(void)
Aug 3, 2015
Aug 3, 2015
211
{
Aug 24, 2018
Aug 24, 2018
212
213
char * netmask = config_get_kcmdline_string(NETWORK_NETMASK_KEY);
if (netmask != NULL)
Sep 5, 2018
Sep 5, 2018
214
return netmask;
Aug 3, 2015
Aug 3, 2015
215
Sep 5, 2018
Sep 5, 2018
216
return config_get_conf_string(NETWORK_ENTRY, NETWORK_NETMASK_KEY);
Aug 3, 2015
Aug 3, 2015
217
218
}
Aug 24, 2018
Aug 24, 2018
219
static char * config_get_network_nat_interface(void)
Nov 27, 2013
Nov 27, 2013
220
{
Sep 5, 2018
Sep 5, 2018
221
return config_get_conf_string(NETWORK_ENTRY, NETWORK_NAT_INTERFACE_KEY);
Nov 27, 2013
Nov 27, 2013
222
223
}
Nov 13, 2012
Nov 13, 2012
224
/* create basic conffile with sensible defaults */
Aug 24, 2018
Aug 24, 2018
225
static void config_create_conf_file(void)
Nov 13, 2012
Nov 13, 2012
226
{
Aug 24, 2018
Aug 24, 2018
227
228
229
230
GKeyFile *settingsfile;
gchar *keyfile;
int dir = 1;
struct stat dir_stat;
Nov 13, 2012
Nov 13, 2012
231
Aug 24, 2018
Aug 24, 2018
232
/* since this function can also be called when the dir exists we only create
Aug 24, 2018
Aug 24, 2018
233
* it if it is missing */
Aug 24, 2018
Aug 24, 2018
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)
Apr 20, 2011
Apr 20, 2011
271
return ret;
Aug 24, 2018
Aug 24, 2018
272
273
274
origkeys = keys;
while (*keys != NULL)
{
Apr 20, 2011
Apr 20, 2011
275
276
if(!strcmp(*keys, key))
{
Aug 24, 2018
Aug 24, 2018
277
278
ret = g_key_file_get_integer(settingsfile, entry, *keys, NULL);
log_debug("%s key value = %d\n", key, ret);
Apr 20, 2011
Apr 20, 2011
279
280
}
keys++;
Aug 24, 2018
Aug 24, 2018
281
282
283
}
g_strfreev(origkeys);
g_key_file_free(settingsfile);
Sep 5, 2018
Sep 5, 2018
284
return ret;
Aug 24, 2018
Aug 24, 2018
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)
Oct 17, 2013
Oct 17, 2013
304
goto end;
Aug 24, 2018
Aug 24, 2018
305
306
307
origkeys = keys;
while (*keys != NULL)
{
Apr 20, 2011
Apr 20, 2011
308
309
if(!strcmp(*keys, key))
{
Aug 24, 2018
Aug 24, 2018
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);
}
Apr 20, 2011
Apr 20, 2011
315
316
}
keys++;
Aug 24, 2018
Aug 24, 2018
317
318
}
g_strfreev(origkeys);
Oct 17, 2013
Oct 17, 2013
319
end:
Aug 24, 2018
Aug 24, 2018
320
g_key_file_free(settingsfile);
Sep 5, 2018
Sep 5, 2018
321
return tmp_char;
Apr 20, 2011
Apr 20, 2011
322
Apr 15, 2011
Apr 15, 2011
323
}
Apr 20, 2011
Apr 20, 2011
324
Aug 24, 2018
Aug 24, 2018
325
static char * config_get_kcmdline_string(const char *entry)
May 17, 2013
May 17, 2013
326
{
Aug 24, 2018
Aug 24, 2018
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;
May 17, 2013
May 17, 2013
336
Aug 24, 2018
Aug 24, 2018
337
338
339
if ((fd = open("/proc/cmdline", O_RDONLY)) < 0)
{
log_debug("could not read /proc/cmdline");
Sep 5, 2018
Sep 5, 2018
340
return ret;
Aug 24, 2018
Aug 24, 2018
341
}
May 17, 2013
May 17, 2013
342
Sep 5, 2018
Sep 5, 2018
343
len = read(fd, cmdLine, sizeof cmdLine - 1);
Aug 24, 2018
Aug 24, 2018
344
close(fd);
May 17, 2013
May 17, 2013
345
Aug 24, 2018
Aug 24, 2018
346
347
348
if (len <= 0)
{
log_debug("kernel command line was empty");
Sep 5, 2018
Sep 5, 2018
349
return ret;
Aug 24, 2018
Aug 24, 2018
350
}
May 17, 2013
May 17, 2013
351
Aug 24, 2018
Aug 24, 2018
352
cmdLine[len] = '\0';
May 17, 2013
May 17, 2013
353
Aug 24, 2018
Aug 24, 2018
354
/* we're looking for a piece of the kernel command line matching this:
Aug 24, 2018
Aug 24, 2018
355
* ip=192.168.3.100::192.168.3.1:255.255.255.0::usb0:on */
Aug 24, 2018
Aug 24, 2018
356
357
358
if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr))
{
g_error_free(optErr);
Sep 5, 2018
Sep 5, 2018
359
return ret;
Aug 24, 2018
Aug 24, 2018
360
}
May 17, 2013
May 17, 2013
361
Aug 24, 2018
Aug 24, 2018
362
363
/* find the ip token */
for (i=0; i < argc; i++)
May 17, 2013
May 17, 2013
364
{
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
398
return ret;
Aug 24, 2018
Aug 24, 2018
399
400
401
402
}
char * config_get_mode_setting(void)
{
Sep 5, 2018
Sep 5, 2018
403
char *mode = 0;
Aug 24, 2018
Aug 24, 2018
404
Sep 5, 2018
Sep 5, 2018
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;
Sep 11, 2011
Sep 11, 2011
417
}
May 27, 2015
May 27, 2015
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
*
Jul 7, 2016
Jul 7, 2016
424
* @return: 0 when the old value is the same as the new one, 1 otherwise
May 27, 2015
May 27, 2015
425
426
427
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
Aug 24, 2018
Aug 24, 2018
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
Aug 24, 2018
Aug 24, 2018
460
461
* the contents will be correctly written to file afterwards.
* Just a precaution. */
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
467
return ret;
Sep 11, 2011
Sep 11, 2011
468
}
Nov 13, 2012
Nov 13, 2012
469
Aug 24, 2018
Aug 24, 2018
470
set_config_result_t config_set_mode_setting(const char *mode)
May 24, 2013
May 24, 2013
471
{
Sep 5, 2018
Sep 5, 2018
472
if (strcmp(mode, MODE_ASK) && common_valid_mode(mode))
Aug 24, 2018
Aug 24, 2018
473
return SET_CONFIG_ERROR;
Sep 5, 2018
Sep 5, 2018
474
return config_set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode);
May 24, 2013
May 24, 2013
475
476
}
Dec 26, 2015
Dec 26, 2015
477
/* Builds the string used for hidden modes, when hide set to one builds the
Aug 24, 2018
Aug 24, 2018
478
* new string of hidden modes when adding one, otherwise it will remove one */
Aug 24, 2018
Aug 24, 2018
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("");
}
Dec 26, 2015
Dec 26, 2015
493
Aug 24, 2018
Aug 24, 2018
494
modes_arr = g_strsplit(modes_old, ",", 0);
Dec 26, 2015
Dec 26, 2015
495
Aug 24, 2018
Aug 24, 2018
496
modes_tmp = g_string_new(NULL);
Jul 7, 2016
Jul 7, 2016
497
Aug 24, 2018
Aug 24, 2018
498
for(i = 0; modes_arr[i] != NULL; i++)
Jul 7, 2016
Jul 7, 2016
499
{
Aug 24, 2018
Aug 24, 2018
500
501
502
503
504
if(strlen(modes_arr[i]) == 0)
{
/* Skip any empty strings */
continue;
}
Jul 7, 2016
Jul 7, 2016
505
Aug 24, 2018
Aug 24, 2018
506
507
508
509
510
if(!strcmp(modes_arr[i], mode_name))
{
/* When unhiding, just skip all matching entries */
if(!include)
continue;
Jul 7, 2016
Jul 7, 2016
511
Aug 24, 2018
Aug 24, 2018
512
513
514
/* When hiding, keep the 1st match and ignore the rest */
include = 0;
}
Jul 7, 2016
Jul 7, 2016
515
Aug 24, 2018
Aug 24, 2018
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]);
}
Jul 7, 2016
Jul 7, 2016
520
Aug 24, 2018
Aug 24, 2018
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);
}
Jul 7, 2016
Jul 7, 2016
528
Aug 24, 2018
Aug 24, 2018
529
modes_new = g_string_free(modes_tmp, FALSE), modes_tmp = 0;
Jul 7, 2016
Jul 7, 2016
530
Aug 24, 2018
Aug 24, 2018
531
g_strfreev(modes_arr), modes_arr = 0;
Jul 7, 2016
Jul 7, 2016
532
Aug 24, 2018
Aug 24, 2018
533
g_free(modes_old), modes_old = 0;
Jul 7, 2016
Jul 7, 2016
534
Aug 24, 2018
Aug 24, 2018
535
return modes_new;
Dec 26, 2015
Dec 26, 2015
536
537
}
Aug 24, 2018
Aug 24, 2018
538
set_config_result_t config_set_hide_mode_setting(const char *mode)
Dec 15, 2015
Dec 15, 2015
539
{
Aug 24, 2018
Aug 24, 2018
540
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Jul 7, 2016
Jul 7, 2016
541
Aug 24, 2018
Aug 24, 2018
542
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 1);
Jul 7, 2016
Jul 7, 2016
543
Aug 24, 2018
Aug 24, 2018
544
545
546
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
Dec 15, 2015
Dec 15, 2015
547
Aug 24, 2018
Aug 24, 2018
548
if(ret == SET_CONFIG_UPDATED) {
Sep 5, 2018
Sep 5, 2018
549
550
551
common_send_hidden_modes_signal();
common_send_supported_modes_signal();
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
552
}
Jul 7, 2016
Jul 7, 2016
553
Aug 24, 2018
Aug 24, 2018
554
g_free(hidden_modes);
Jul 7, 2016
Jul 7, 2016
555
Sep 5, 2018
Sep 5, 2018
556
return ret;
Dec 15, 2015
Dec 15, 2015
557
558
}
Aug 24, 2018
Aug 24, 2018
559
set_config_result_t config_set_unhide_mode_setting(const char *mode)
Dec 15, 2015
Dec 15, 2015
560
{
Aug 24, 2018
Aug 24, 2018
561
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Jul 7, 2016
Jul 7, 2016
562
Aug 24, 2018
Aug 24, 2018
563
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 0);
Jul 7, 2016
Jul 7, 2016
564
Aug 24, 2018
Aug 24, 2018
565
566
567
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
Dec 15, 2015
Dec 15, 2015
568
Aug 24, 2018
Aug 24, 2018
569
if(ret == SET_CONFIG_UPDATED) {
Sep 5, 2018
Sep 5, 2018
570
571
572
common_send_hidden_modes_signal();
common_send_supported_modes_signal();
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
573
}
Jul 7, 2016
Jul 7, 2016
574
Aug 24, 2018
Aug 24, 2018
575
g_free(hidden_modes);
Jul 7, 2016
Jul 7, 2016
576
Sep 5, 2018
Sep 5, 2018
577
return ret;
Dec 15, 2015
Dec 15, 2015
578
579
}
Aug 24, 2018
Aug 24, 2018
580
set_config_result_t config_set_mode_whitelist(const char *whitelist)
Aug 11, 2017
Aug 11, 2017
581
{
Aug 24, 2018
Aug 24, 2018
582
set_config_result_t ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist);
Aug 11, 2017
Aug 11, 2017
583
Aug 24, 2018
Aug 24, 2018
584
585
586
if(ret == SET_CONFIG_UPDATED) {
char *mode_setting;
const char *current_mode;
Aug 11, 2017
Aug 11, 2017
587
Aug 24, 2018
Aug 24, 2018
588
mode_setting = config_get_mode_setting();
Sep 5, 2018
Sep 5, 2018
589
if (strcmp(mode_setting, MODE_ASK) && common_valid_mode(mode_setting))
Aug 24, 2018
Aug 24, 2018
590
591
config_set_mode_setting(MODE_ASK);
g_free(mode_setting);
Aug 11, 2017
Aug 11, 2017
592
Sep 5, 2018
Sep 5, 2018
593
current_mode = control_get_usb_mode();
Aug 24, 2018
Aug 24, 2018
594
595
596
if (!strcmp(current_mode, MODE_UNDEFINED)) {
/* Disconnected -> do nothing */
}
Sep 5, 2018
Sep 5, 2018
597
else if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && common_valid_mode(current_mode)) {
Aug 24, 2018
Aug 24, 2018
598
599
/* Invalid mode that is not MODE_ASK or MODE_CHARGING_FALLBACK
* -> switch to MODE_CHARGING_FALLBACK */
Sep 5, 2018
Sep 5, 2018
600
control_set_usb_mode(MODE_CHARGING_FALLBACK);
Aug 24, 2018
Aug 24, 2018
601
}
Aug 11, 2017
Aug 11, 2017
602
Aug 24, 2018
Aug 24, 2018
603
umdbus_send_whitelisted_modes_signal(whitelist);
Sep 5, 2018
Sep 5, 2018
604
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
605
}
Aug 11, 2017
Aug 11, 2017
606
Aug 24, 2018
Aug 24, 2018
607
return ret;
Aug 11, 2017
Aug 11, 2017
608
609
}
Aug 24, 2018
Aug 24, 2018
610
set_config_result_t config_set_mode_in_whitelist(const char *mode, int allowed)
Aug 11, 2017
Aug 11, 2017
611
{
Aug 24, 2018
Aug 24, 2018
612
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Aug 11, 2017
Aug 11, 2017
613
Aug 24, 2018
Aug 24, 2018
614
char *whitelist = config_make_modes_string(MODE_WHITELIST_KEY, mode, allowed);
Aug 11, 2017
Aug 11, 2017
615
Aug 24, 2018
Aug 24, 2018
616
617
618
if (whitelist) {
ret = config_set_mode_whitelist(whitelist);
}
Aug 11, 2017
Aug 11, 2017
619
Aug 24, 2018
Aug 24, 2018
620
g_free(whitelist);
Aug 11, 2017
Aug 11, 2017
621
Sep 5, 2018
Sep 5, 2018
622
return ret;
Aug 11, 2017
Aug 11, 2017
623
624
}
Apr 20, 2014
Apr 20, 2014
625
626
627
628
/*
* @param config : the key to be set
* @param setting : The value to be set
*/
Aug 24, 2018
Aug 24, 2018
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
Aug 24, 2018
Aug 24, 2018
662
663
* the contents will be correctly written to file afterwards.
* Just a precaution. */
Aug 24, 2018
Aug 24, 2018
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;
Sep 5, 2018
Sep 5, 2018
680
mode_list_elem_t *data;
Aug 24, 2018
Aug 24, 2018
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
Aug 24, 2018
Aug 24, 2018
692
* the information from the specific mode */
Aug 24, 2018
Aug 24, 2018
693
694
695
696
697
ret = config_get_network_interface();
if(ret)
goto end;
/* no interface override specified, let's use the one
Aug 24, 2018
Aug 24, 2018
698
* from the mode config */
Sep 5, 2018
Sep 5, 2018
699
data = worker_get_usb_mode_data();
Aug 24, 2018
Aug 24, 2018
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))
Sep 5, 2018
Sep 5, 2018
711
return config_get_network_gateway();
Aug 24, 2018
Aug 24, 2018
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))
Sep 5, 2018
Sep 5, 2018
719
return config_get_network_nat_interface();
Aug 24, 2018
Aug 24, 2018
720
721
else
/* no matching keys, return error */
Sep 5, 2018
Sep 5, 2018
722
return NULL;
Sep 9, 2013
Sep 9, 2013
723
end:
Sep 5, 2018
Sep 5, 2018
724
return ret;
Aug 31, 2013
Aug 31, 2013
725
726
}
Apr 6, 2016
Apr 6, 2016
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
*/
Aug 24, 2018
Aug 24, 2018
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);
}
Apr 6, 2016
Apr 6, 2016
746
}
Mar 30, 2013
Mar 30, 2013
747
Apr 6, 2016
Apr 6, 2016
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
*/
Aug 24, 2018
Aug 24, 2018
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);
}
Apr 6, 2016
Apr 6, 2016
764
}
Mar 30, 2013
Mar 30, 2013
765
Apr 6, 2016
Apr 6, 2016
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
*/
Aug 24, 2018
Aug 24, 2018
772
static void config_merge_file(GKeyFile *dest, GKeyFile *srce)
Apr 6, 2016
Apr 6, 2016
773
{
Aug 24, 2018
Aug 24, 2018
774
gchar **grp = g_key_file_get_groups(srce, 0);
Mar 30, 2013
Mar 30, 2013
775
Aug 24, 2018
Aug 24, 2018
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);
}
Apr 6, 2016
Apr 6, 2016
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)
*/
Aug 24, 2018
Aug 24, 2018
791
static int config_glob_error_cb(const char *path, int err)
Apr 6, 2016
Apr 6, 2016
792
{
Aug 24, 2018
Aug 24, 2018
793
794
log_debug("%s: glob: %s", path, g_strerror(err));
return 0;
Apr 6, 2016
Apr 6, 2016
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.
*/
Aug 24, 2018
Aug 24, 2018
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);
}
Apr 6, 2016
Apr 6, 2016
832
exit:
Aug 24, 2018
Aug 24, 2018
833
834
globfree(&gb);
return ini;
Apr 6, 2016
Apr 6, 2016
835
}
Jun 19, 2013
Jun 19, 2013
836
Apr 6, 2016
Apr 6, 2016
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
*/
Aug 24, 2018
Aug 24, 2018
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);
}
Apr 6, 2016
Apr 6, 2016
876
out:
Aug 24, 2018
Aug 24, 2018
877
878
879
g_key_file_free(tempfile);
g_key_file_free(settingsfile);
return ret;
Mar 30, 2013
Mar 30, 2013
880
}
Aug 2, 2013
Aug 2, 2013
881
Aug 24, 2018
Aug 24, 2018
882
char * config_get_android_manufacturer(void)
Aug 2, 2013
Aug 2, 2013
883
{
May 11, 2016
May 11, 2016
884
#ifdef USE_MER_SSU
Aug 24, 2018
Aug 24, 2018
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;
}
May 11, 2016
May 11, 2016
892
893
#endif
Aug 24, 2018
Aug 24, 2018
894
return config_get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY);
Aug 2, 2013
Aug 2, 2013
895
}
Aug 7, 2013
Aug 7, 2013
896
Aug 24, 2018
Aug 24, 2018
897
char * config_get_android_vendor_id(void)
Aug 2, 2013
Aug 2, 2013
898
{
Sep 5, 2018
Sep 5, 2018
899
return config_get_conf_string(ANDROID_ENTRY, ANDROID_VENDOR_ID_KEY);
Aug 2, 2013
Aug 2, 2013
900
901
}
Aug 24, 2018
Aug 24, 2018
902
char * config_get_android_product(void)
Aug 2, 2013
Aug 2, 2013
903
{
May 11, 2016
May 11, 2016
904
#ifdef USE_MER_SSU
Aug 24, 2018
Aug 24, 2018
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;
}
May 11, 2016
May 11, 2016
912
913
#endif
Aug 24, 2018
Aug 24, 2018
914
return config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY);
Aug 2, 2013
Aug 2, 2013
915
916
}
Aug 24, 2018
Aug 24, 2018
917
char * config_get_android_product_id(void)
Aug 7, 2013
Aug 7, 2013
918
{
Sep 5, 2018
Sep 5, 2018
919
return config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY);
Aug 7, 2013
Aug 7, 2013
920
921
}
Aug 24, 2018
Aug 24, 2018
922
char * config_get_hidden_modes(void)
Dec 15, 2015
Dec 15, 2015
923
{
Sep 5, 2018
Sep 5, 2018
924
return config_get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY);
Dec 15, 2015
Dec 15, 2015
925
}
Aug 24, 2018
Aug 24, 2018
926
char * config_get_mode_whitelist(void)
Aug 11, 2017
Aug 11, 2017
927
{
Sep 5, 2018
Sep 5, 2018
928
return config_get_conf_string(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY);
Aug 11, 2017
Aug 11, 2017
929
}
Dec 15, 2015
Dec 15, 2015
930
Aug 24, 2018
Aug 24, 2018
931
int config_is_roaming_not_allowed(void)
Feb 13, 2014
Feb 13, 2014
932
{
Sep 5, 2018
Sep 5, 2018
933
return config_get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY);
Feb 13, 2014
Feb 13, 2014
934
}