/
usb_moded-config.c
928 lines (803 loc) · 27.8 KB
/
usb_moded-config.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
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
653
654
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
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
823
824
825
826
827
828
829
830
831
832
833
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/**
* @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
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <glib.h>
#include <glob.h>
#include "usb_moded.h"
#include "usb_moded-config.h"
#include "usb_moded-config-private.h"
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-dbus-private.h"
#ifdef USE_MER_SSU
# include "usb_moded-ssu.h"
#endif
/* ========================================================================= *
* 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)
return(-1);
if ((b1 | b2 | b3 | b4) > 255)
return(-1);
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
return(-1);
/* all ok */
return 0;
}
char *config_find_mounts(void)
{
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);
}
int config_find_sync(void)
{
return(config_get_conf_int(FS_SYNC_ENTRY, FS_SYNC_KEY));
}
char * config_find_alt_mount(void)
{
return(config_get_conf_string(ALT_MOUNT_ENTRY, ALT_MOUNT_KEY));
}
char * config_find_udev_path(void)
{
return(config_get_conf_string(UDEV_PATH_ENTRY, UDEV_PATH_KEY));
}
char * config_find_udev_subsystem(void)
{
return(config_get_conf_string(UDEV_PATH_ENTRY, UDEV_SUBSYSTEM_KEY));
}
char * config_check_trigger(void)
{
return(config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PATH_KEY));
}
char * config_get_trigger_subsystem(void)
{
return(config_get_conf_string(TRIGGER_ENTRY, TRIGGER_UDEV_SUBSYSTEM));
}
char * config_get_trigger_mode(void)
{
return(config_get_conf_string(TRIGGER_ENTRY, TRIGGER_MODE_KEY));
}
char * config_get_trigger_property(void)
{
return(config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_KEY));
}
char * config_get_trigger_value(void)
{
return(config_get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_VALUE_KEY));
}
static char * config_get_network_ip(void)
{
char * ip = config_get_kcmdline_string(NETWORK_IP_KEY);
if (ip != NULL)
if(!config_validate_ip(ip))
return(ip);
return(config_get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY));
}
static char * config_get_network_interface(void)
{
return(config_get_conf_string(NETWORK_ENTRY, NETWORK_INTERFACE_KEY));
}
static char * config_get_network_gateway(void)
{
char * gw = config_get_kcmdline_string(NETWORK_GATEWAY_KEY);
if (gw != NULL)
return(gw);
return(config_get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY));
}
static char * config_get_network_netmask(void)
{
char * netmask = config_get_kcmdline_string(NETWORK_NETMASK_KEY);
if (netmask != NULL)
return(netmask);
return(config_get_conf_string(NETWORK_ENTRY, NETWORK_NETMASK_KEY));
}
static char * config_get_network_nat_interface(void)
{
return(config_get_conf_string(NETWORK_ENTRY, NETWORK_NAT_INTERFACE_KEY));
}
/* create basic conffile with sensible defaults */
static void config_create_conf_file(void)
{
GKeyFile *settingsfile;
gchar *keyfile;
int dir = 1;
struct stat dir_stat;
/* since this function can also be called when the dir exists we only create
* it if it is missing */
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)
return ret;
origkeys = keys;
while (*keys != NULL)
{
if(!strcmp(*keys, key))
{
ret = g_key_file_get_integer(settingsfile, entry, *keys, NULL);
log_debug("%s key value = %d\n", key, ret);
}
keys++;
}
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)
goto end;
origkeys = keys;
while (*keys != NULL)
{
if(!strcmp(*keys, key))
{
tmp_char = g_key_file_get_string(settingsfile, entry, *keys, NULL);
if(tmp_char)
{
log_debug("key %s value = %s\n", key, tmp_char);
}
}
keys++;
}
g_strfreev(origkeys);
end:
g_key_file_free(settingsfile);
return(tmp_char);
}
static char * config_get_kcmdline_string(const char *entry)
{
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;
if ((fd = open("/proc/cmdline", O_RDONLY)) < 0)
{
log_debug("could not read /proc/cmdline");
return(ret);
}
len = read(fd, cmdLine, sizeof(cmdLine) - 1);
close(fd);
if (len <= 0)
{
log_debug("kernel command line was empty");
return(ret);
}
cmdLine[len] = '\0';
/* we're looking for a piece of the kernel command line matching this:
* ip=192.168.3.100::192.168.3.1:255.255.255.0::usb0:on */
if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr))
{
g_error_free(optErr);
return(ret);
}
/* find the ip token */
for (i=0; i < argc; i++)
{
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));
}
/*
* @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
*
* @return: 0 when the old value is the same as the new one, 1 otherwise
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
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
* the contents will be correctly written to file afterwards.
* Just a precaution. */
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);
}
set_config_result_t config_set_mode_setting(const char *mode)
{
if (strcmp(mode, MODE_ASK) && usbmoded_valid_mode(mode))
return SET_CONFIG_ERROR;
return (config_set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}
/* Builds the string used for hidden modes, when hide set to one builds the
* new string of hidden modes when adding one, otherwise it will remove one */
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("");
}
modes_arr = g_strsplit(modes_old, ",", 0);
modes_tmp = g_string_new(NULL);
for(i = 0; modes_arr[i] != NULL; i++)
{
if(strlen(modes_arr[i]) == 0)
{
/* Skip any empty strings */
continue;
}
if(!strcmp(modes_arr[i], mode_name))
{
/* When unhiding, just skip all matching entries */
if(!include)
continue;
/* When hiding, keep the 1st match and ignore the rest */
include = 0;
}
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, modes_arr[i]);
}
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);
}
modes_new = g_string_free(modes_tmp, FALSE), modes_tmp = 0;
g_strfreev(modes_arr), modes_arr = 0;
g_free(modes_old), modes_old = 0;
return modes_new;
}
set_config_result_t config_set_hide_mode_setting(const char *mode)
{
set_config_result_t ret = SET_CONFIG_UNCHANGED;
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 1);
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
if(ret == SET_CONFIG_UPDATED) {
usbmoded_send_hidden_modes_signal();
usbmoded_send_supported_modes_signal();
usbmoded_send_available_modes_signal();
}
g_free(hidden_modes);
return(ret);
}
set_config_result_t config_set_unhide_mode_setting(const char *mode)
{
set_config_result_t ret = SET_CONFIG_UNCHANGED;
char *hidden_modes = config_make_modes_string(MODE_HIDE_KEY, mode, 0);
if( hidden_modes ) {
ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
if(ret == SET_CONFIG_UPDATED) {
usbmoded_send_hidden_modes_signal();
usbmoded_send_supported_modes_signal();
usbmoded_send_available_modes_signal();
}
g_free(hidden_modes);
return(ret);
}
set_config_result_t config_set_mode_whitelist(const char *whitelist)
{
set_config_result_t ret = config_set_config_setting(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist);
if(ret == SET_CONFIG_UPDATED) {
char *mode_setting;
const char *current_mode;
mode_setting = config_get_mode_setting();
if (strcmp(mode_setting, MODE_ASK) && usbmoded_valid_mode(mode_setting))
config_set_mode_setting(MODE_ASK);
g_free(mode_setting);
current_mode = usbmoded_get_usb_mode();
if (!strcmp(current_mode, MODE_UNDEFINED)) {
/* Disconnected -> do nothing */
}
else if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && usbmoded_valid_mode(current_mode)) {
/* Invalid mode that is not MODE_ASK or MODE_CHARGING_FALLBACK
* -> switch to MODE_CHARGING_FALLBACK */
usbmoded_set_usb_mode(MODE_CHARGING_FALLBACK);
}
umdbus_send_whitelisted_modes_signal(whitelist);
usbmoded_send_available_modes_signal();
}
return ret;
}
set_config_result_t config_set_mode_in_whitelist(const char *mode, int allowed)
{
set_config_result_t ret = SET_CONFIG_UNCHANGED;
char *whitelist = config_make_modes_string(MODE_WHITELIST_KEY, mode, allowed);
if (whitelist) {
ret = config_set_mode_whitelist(whitelist);
}
g_free(whitelist);
return(ret);
}
/*
* @param config : the key to be set
* @param setting : The value to be set
*/
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
* the contents will be correctly written to file afterwards.
* Just a precaution. */
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
* the information from the specific mode */
ret = config_get_network_interface();
if(ret)
goto end;
/* no interface override specified, let's use the one
* from the mode config */
data = usbmoded_get_usb_mode_data();
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);
end:
return(ret);
}
/**
* 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
*/
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);
}
}
/**
* 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
*/
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);
}
}
/**
* Merge all groups and values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
*/
static void config_merge_file(GKeyFile *dest, GKeyFile *srce)
{
gchar **grp = g_key_file_get_groups(srce, 0);
if( grp ) {
for( size_t i = 0; grp[i]; ++i )
config_merge_group(dest, srce, grp[i]);
g_strfreev(grp);
}
}
/**
* 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)
*/
static int config_glob_error_cb(const char *path, int err)
{
log_debug("%s: glob: %s", path, g_strerror(err));
return 0;
}
/**
* Read *.ini files on CONFIG_FILE_DIR in the order of [0-9][A-Z][a-z]
*
* @return the in memory value-pair file.
*/
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);
}
exit:
globfree(&gb);
return ini;
}
/**
* Read the *.ini files and create/overwrite FS_MOUNT_CONFIG_FILE with
* the merged data.
*
* @return 0 on failure
*/
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);
}
out:
g_key_file_free(tempfile);
g_key_file_free(settingsfile);
return ret;
}
char * config_get_android_manufacturer(void)
{
#ifdef USE_MER_SSU
/* 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;
}
#endif
return config_get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY);
}
char * config_get_android_vendor_id(void)
{
return(config_get_conf_string(ANDROID_ENTRY, ANDROID_VENDOR_ID_KEY));
}
char * config_get_android_product(void)
{
#ifdef USE_MER_SSU
/* 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;
}
#endif
return config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY);
}
char * config_get_android_product_id(void)
{
return(config_get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY));
}
char * config_get_hidden_modes(void)
{
return(config_get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY));
}
char * config_get_mode_whitelist(void)
{
return(config_get_conf_string(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY));
}
int config_is_roaming_not_allowed(void)
{
return(config_get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY));
}