Skip to content

Latest commit

 

History

History
925 lines (800 loc) · 27.7 KB

usb_moded-config.c

File metadata and controls

925 lines (800 loc) · 27.7 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)
Apr 20, 2014
Apr 20, 2014
116
117
return(-1);
Aug 24, 2018
Aug 24, 2018
118
if ((b1 | b2 | b3 | b4) > 255)
Apr 20, 2014
Apr 20, 2014
119
return(-1);
Aug 24, 2018
Aug 24, 2018
120
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
Apr 20, 2014
Apr 20, 2014
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
137
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);
}
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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
191
char * ip = config_get_kcmdline_string(NETWORK_IP_KEY);
if (ip != NULL)
if(!config_validate_ip(ip))
return(ip);
May 17, 2013
May 17, 2013
192
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
205
char * gw = config_get_kcmdline_string(NETWORK_GATEWAY_KEY);
if (gw != NULL)
return(gw);
May 17, 2013
May 17, 2013
206
Aug 24, 2018
Aug 24, 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
214
char * netmask = config_get_kcmdline_string(NETWORK_NETMASK_KEY);
if (netmask != NULL)
return(netmask);
Aug 3, 2015
Aug 3, 2015
215
Aug 24, 2018
Aug 24, 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
{
Aug 24, 2018
Aug 24, 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
}
g_strfreev(origkeys);
g_key_file_free(settingsfile);
return(ret);
}
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
321
g_key_file_free(settingsfile);
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
340
341
if ((fd = open("/proc/cmdline", O_RDONLY)) < 0)
{
log_debug("could not read /proc/cmdline");
return(ret);
}
May 17, 2013
May 17, 2013
342
Aug 24, 2018
Aug 24, 2018
343
344
len = read(fd, cmdLine, sizeof(cmdLine) - 1);
close(fd);
May 17, 2013
May 17, 2013
345
Aug 24, 2018
Aug 24, 2018
346
347
348
349
350
if (len <= 0)
{
log_debug("kernel command line was empty");
return(ret);
}
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
359
360
if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr))
{
g_error_free(optErr);
return(ret);
}
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
398
399
400
401
402
403
404
405
406
407
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);
return(ret);
}
char * config_get_mode_setting(void)
{
char * mode = config_get_kcmdline_string(MODE_SETTING_KEY);
if (mode != NULL)
return(mode);
return(config_get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
Sep 11, 2011
Sep 11, 2011
408
}
May 27, 2015
May 27, 2015
409
410
411
412
413
414
/*
* @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
415
* @return: 0 when the old value is the same as the new one, 1 otherwise
May 27, 2015
May 27, 2015
416
417
418
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
Aug 24, 2018
Aug 24, 2018
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
451
452
* the contents will be correctly written to file afterwards.
* Just a precaution. */
Aug 24, 2018
Aug 24, 2018
453
454
455
456
457
458
g_key_file_free(settingsfile);
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
g_free(keyfile);
return (ret);
Sep 11, 2011
Sep 11, 2011
459
}
Nov 13, 2012
Nov 13, 2012
460
Aug 24, 2018
Aug 24, 2018
461
set_config_result_t config_set_mode_setting(const char *mode)
May 24, 2013
May 24, 2013
462
{
Sep 5, 2018
Sep 5, 2018
463
if (strcmp(mode, MODE_ASK) && common_valid_mode(mode))
Aug 24, 2018
Aug 24, 2018
464
465
return SET_CONFIG_ERROR;
return (config_set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
May 24, 2013
May 24, 2013
466
467
}
Dec 26, 2015
Dec 26, 2015
468
/* Builds the string used for hidden modes, when hide set to one builds the
Aug 24, 2018
Aug 24, 2018
469
* new string of hidden modes when adding one, otherwise it will remove one */
Aug 24, 2018
Aug 24, 2018
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
484
Aug 24, 2018
Aug 24, 2018
485
modes_arr = g_strsplit(modes_old, ",", 0);
Dec 26, 2015
Dec 26, 2015
486
Aug 24, 2018
Aug 24, 2018
487
modes_tmp = g_string_new(NULL);
Jul 7, 2016
Jul 7, 2016
488
Aug 24, 2018
Aug 24, 2018
489
for(i = 0; modes_arr[i] != NULL; i++)
Jul 7, 2016
Jul 7, 2016
490
{
Aug 24, 2018
Aug 24, 2018
491
492
493
494
495
if(strlen(modes_arr[i]) == 0)
{
/* Skip any empty strings */
continue;
}
Jul 7, 2016
Jul 7, 2016
496
Aug 24, 2018
Aug 24, 2018
497
498
499
500
501
if(!strcmp(modes_arr[i], mode_name))
{
/* When unhiding, just skip all matching entries */
if(!include)
continue;
Jul 7, 2016
Jul 7, 2016
502
Aug 24, 2018
Aug 24, 2018
503
504
505
/* When hiding, keep the 1st match and ignore the rest */
include = 0;
}
Jul 7, 2016
Jul 7, 2016
506
Aug 24, 2018
Aug 24, 2018
507
508
509
510
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
511
Aug 24, 2018
Aug 24, 2018
512
513
514
515
516
517
518
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
519
Aug 24, 2018
Aug 24, 2018
520
modes_new = g_string_free(modes_tmp, FALSE), modes_tmp = 0;
Jul 7, 2016
Jul 7, 2016
521
Aug 24, 2018
Aug 24, 2018
522
g_strfreev(modes_arr), modes_arr = 0;
Jul 7, 2016
Jul 7, 2016
523
Aug 24, 2018
Aug 24, 2018
524
g_free(modes_old), modes_old = 0;
Jul 7, 2016
Jul 7, 2016
525
Aug 24, 2018
Aug 24, 2018
526
return modes_new;
Dec 26, 2015
Dec 26, 2015
527
528
}
Aug 24, 2018
Aug 24, 2018
529
set_config_result_t config_set_hide_mode_setting(const char *mode)
Dec 15, 2015
Dec 15, 2015
530
{
Aug 24, 2018
Aug 24, 2018
531
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Jul 7, 2016
Jul 7, 2016
532
Aug 24, 2018
Aug 24, 2018
533
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 1);
Jul 7, 2016
Jul 7, 2016
534
Aug 24, 2018
Aug 24, 2018
535
536
537
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
Dec 15, 2015
Dec 15, 2015
538
Aug 24, 2018
Aug 24, 2018
539
if(ret == SET_CONFIG_UPDATED) {
Sep 5, 2018
Sep 5, 2018
540
541
542
common_send_hidden_modes_signal();
common_send_supported_modes_signal();
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
543
}
Jul 7, 2016
Jul 7, 2016
544
Aug 24, 2018
Aug 24, 2018
545
g_free(hidden_modes);
Jul 7, 2016
Jul 7, 2016
546
Aug 24, 2018
Aug 24, 2018
547
return(ret);
Dec 15, 2015
Dec 15, 2015
548
549
}
Aug 24, 2018
Aug 24, 2018
550
set_config_result_t config_set_unhide_mode_setting(const char *mode)
Dec 15, 2015
Dec 15, 2015
551
{
Aug 24, 2018
Aug 24, 2018
552
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Jul 7, 2016
Jul 7, 2016
553
Aug 24, 2018
Aug 24, 2018
554
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 0);
Jul 7, 2016
Jul 7, 2016
555
Aug 24, 2018
Aug 24, 2018
556
557
558
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
Dec 15, 2015
Dec 15, 2015
559
Aug 24, 2018
Aug 24, 2018
560
if(ret == SET_CONFIG_UPDATED) {
Sep 5, 2018
Sep 5, 2018
561
562
563
common_send_hidden_modes_signal();
common_send_supported_modes_signal();
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
564
}
Jul 7, 2016
Jul 7, 2016
565
Aug 24, 2018
Aug 24, 2018
566
g_free(hidden_modes);
Jul 7, 2016
Jul 7, 2016
567
Aug 24, 2018
Aug 24, 2018
568
return(ret);
Dec 15, 2015
Dec 15, 2015
569
570
}
Aug 24, 2018
Aug 24, 2018
571
set_config_result_t config_set_mode_whitelist(const char *whitelist)
Aug 11, 2017
Aug 11, 2017
572
{
Aug 24, 2018
Aug 24, 2018
573
set_config_result_t ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist);
Aug 11, 2017
Aug 11, 2017
574
Aug 24, 2018
Aug 24, 2018
575
576
577
if(ret == SET_CONFIG_UPDATED) {
char *mode_setting;
const char *current_mode;
Aug 11, 2017
Aug 11, 2017
578
Aug 24, 2018
Aug 24, 2018
579
mode_setting = config_get_mode_setting();
Sep 5, 2018
Sep 5, 2018
580
if (strcmp(mode_setting, MODE_ASK) && common_valid_mode(mode_setting))
Aug 24, 2018
Aug 24, 2018
581
582
config_set_mode_setting(MODE_ASK);
g_free(mode_setting);
Aug 11, 2017
Aug 11, 2017
583
Sep 5, 2018
Sep 5, 2018
584
current_mode = control_get_usb_mode();
Aug 24, 2018
Aug 24, 2018
585
586
587
if (!strcmp(current_mode, MODE_UNDEFINED)) {
/* Disconnected -> do nothing */
}
Sep 5, 2018
Sep 5, 2018
588
else if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && common_valid_mode(current_mode)) {
Aug 24, 2018
Aug 24, 2018
589
590
/* Invalid mode that is not MODE_ASK or MODE_CHARGING_FALLBACK
* -> switch to MODE_CHARGING_FALLBACK */
Sep 5, 2018
Sep 5, 2018
591
control_set_usb_mode(MODE_CHARGING_FALLBACK);
Aug 24, 2018
Aug 24, 2018
592
}
Aug 11, 2017
Aug 11, 2017
593
Aug 24, 2018
Aug 24, 2018
594
umdbus_send_whitelisted_modes_signal(whitelist);
Sep 5, 2018
Sep 5, 2018
595
common_send_available_modes_signal();
Aug 24, 2018
Aug 24, 2018
596
}
Aug 11, 2017
Aug 11, 2017
597
Aug 24, 2018
Aug 24, 2018
598
return ret;
Aug 11, 2017
Aug 11, 2017
599
600
}
Aug 24, 2018
Aug 24, 2018
601
set_config_result_t config_set_mode_in_whitelist(const char *mode, int allowed)
Aug 11, 2017
Aug 11, 2017
602
{
Aug 24, 2018
Aug 24, 2018
603
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Aug 11, 2017
Aug 11, 2017
604
Aug 24, 2018
Aug 24, 2018
605
char *whitelist = config_make_modes_string(MODE_WHITELIST_KEY, mode, allowed);
Aug 11, 2017
Aug 11, 2017
606
Aug 24, 2018
Aug 24, 2018
607
608
609
if (whitelist) {
ret = config_set_mode_whitelist(whitelist);
}
Aug 11, 2017
Aug 11, 2017
610
Aug 24, 2018
Aug 24, 2018
611
g_free(whitelist);
Aug 11, 2017
Aug 11, 2017
612
Aug 24, 2018
Aug 24, 2018
613
return(ret);
Aug 11, 2017
Aug 11, 2017
614
615
}
Apr 20, 2014
Apr 20, 2014
616
617
618
619
/*
* @param config : the key to be set
* @param setting : The value to be set
*/
Aug 24, 2018
Aug 24, 2018
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
653
654
* the contents will be correctly written to file afterwards.
* Just a precaution. */
Aug 24, 2018
Aug 24, 2018
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
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;
struct mode_list_elem *data;
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
683
* the information from the specific mode */
Aug 24, 2018
Aug 24, 2018
684
685
686
687
688
ret = config_get_network_interface();
if(ret)
goto end;
/* no interface override specified, let's use the one
Aug 24, 2018
Aug 24, 2018
689
* from the mode config */
Sep 5, 2018
Sep 5, 2018
690
data = worker_get_usb_mode_data();
Aug 24, 2018
Aug 24, 2018
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
if(data)
{
if(data->network_interface)
{
ret = strdup(data->network_interface);
goto end;
}
}
ret = strdup("usb0");
}
else if(!strcmp(config, NETWORK_GATEWAY_KEY))
return(config_get_network_gateway());
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))
return(config_get_network_nat_interface());
else
/* no matching keys, return error */
return(NULL);
Sep 9, 2013
Sep 9, 2013
714
end:
Aug 24, 2018
Aug 24, 2018
715
return(ret);
Aug 31, 2013
Aug 31, 2013
716
717
}
Apr 6, 2016
Apr 6, 2016
718
719
720
721
722
723
724
725
726
727
/**
* 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
728
729
730
731
732
733
734
735
736
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
737
}
Mar 30, 2013
Mar 30, 2013
738
Apr 6, 2016
Apr 6, 2016
739
740
741
742
743
744
745
/**
* 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
746
747
748
749
750
751
752
753
754
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
755
}
Mar 30, 2013
Mar 30, 2013
756
Apr 6, 2016
Apr 6, 2016
757
758
759
760
761
762
/**
* 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
763
static void config_merge_file(GKeyFile *dest, GKeyFile *srce)
Apr 6, 2016
Apr 6, 2016
764
{
Aug 24, 2018
Aug 24, 2018
765
gchar **grp = g_key_file_get_groups(srce, 0);
Mar 30, 2013
Mar 30, 2013
766
Aug 24, 2018
Aug 24, 2018
767
768
769
770
771
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
772
773
774
775
776
777
778
779
780
781
}
/**
* 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
782
static int config_glob_error_cb(const char *path, int err)
Apr 6, 2016
Apr 6, 2016
783
{
Aug 24, 2018
Aug 24, 2018
784
785
log_debug("%s: glob: %s", path, g_strerror(err));
return 0;
Apr 6, 2016
Apr 6, 2016
786
787
788
789
790
791
792
}
/**
* 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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
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
823
exit:
Aug 24, 2018
Aug 24, 2018
824
825
globfree(&gb);
return ini;
Apr 6, 2016
Apr 6, 2016
826
}
Jun 19, 2013
Jun 19, 2013
827
Apr 6, 2016
Apr 6, 2016
828
829
830
831
832
833
/**
* 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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
867
out:
Aug 24, 2018
Aug 24, 2018
868
869
870
g_key_file_free(tempfile);
g_key_file_free(settingsfile);
return ret;
Mar 30, 2013
Mar 30, 2013
871
}
Aug 2, 2013
Aug 2, 2013
872
Aug 24, 2018
Aug 24, 2018
873
char * config_get_android_manufacturer(void)
Aug 2, 2013
Aug 2, 2013
874
{
May 11, 2016
May 11, 2016
875
#ifdef USE_MER_SSU
Aug 24, 2018
Aug 24, 2018
876
877
878
879
880
881
882
/* 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
883
884
#endif
Aug 24, 2018
Aug 24, 2018
885
return config_get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY);
Aug 2, 2013
Aug 2, 2013
886
}
Aug 7, 2013
Aug 7, 2013
887
Aug 24, 2018
Aug 24, 2018
888
char * config_get_android_vendor_id(void)
Aug 2, 2013
Aug 2, 2013
889
{
Aug 24, 2018
Aug 24, 2018
890
return(config_get_conf_string(ANDROID_ENTRY, ANDROID_VENDOR_ID_KEY));
Aug 2, 2013
Aug 2, 2013
891
892
}
Aug 24, 2018
Aug 24, 2018
893
char * config_get_android_product(void)
Aug 2, 2013
Aug 2, 2013
894
{
May 11, 2016
May 11, 2016
895
#ifdef USE_MER_SSU
Aug 24, 2018
Aug 24, 2018
896
897
898
899
900
901
902
/* 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
903
904
#endif
Aug 24, 2018
Aug 24, 2018
905
return config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY);
Aug 2, 2013
Aug 2, 2013
906
907
}
Aug 24, 2018
Aug 24, 2018
908
char * config_get_android_product_id(void)
Aug 7, 2013
Aug 7, 2013
909
{
Aug 24, 2018
Aug 24, 2018
910
return(config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY));
Aug 7, 2013
Aug 7, 2013
911
912
}
Aug 24, 2018
Aug 24, 2018
913
char * config_get_hidden_modes(void)
Dec 15, 2015
Dec 15, 2015
914
{
Aug 24, 2018
Aug 24, 2018
915
return(config_get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY));
Dec 15, 2015
Dec 15, 2015
916
}
Aug 24, 2018
Aug 24, 2018
917
char * config_get_mode_whitelist(void)
Aug 11, 2017
Aug 11, 2017
918
{
Aug 24, 2018
Aug 24, 2018
919
return(config_get_conf_string(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY));
Aug 11, 2017
Aug 11, 2017
920
}
Dec 15, 2015
Dec 15, 2015
921
Aug 24, 2018
Aug 24, 2018
922
int config_is_roaming_not_allowed(void)
Feb 13, 2014
Feb 13, 2014
923
{
Aug 24, 2018
Aug 24, 2018
924
return(config_get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY));
Feb 13, 2014
Feb 13, 2014
925
}