-
Notifications
You must be signed in to change notification settings - Fork 173
/
nms-ifcfg-rh-reader.c
6732 lines (5818 loc) · 231 KB
/
nms-ifcfg-rh-reader.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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2008 - 2017 Red Hat, Inc.
*/
#include "src/core/nm-default-daemon.h"
#include "nms-ifcfg-rh-reader.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/inotify.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/rtnetlink.h>
#include <linux/if_ether.h>
#include "libnm-glib-aux/nm-uuid.h"
#include "libnm-glib-aux/nm-secret-utils.h"
#include "nm-connection.h"
#include "nm-dbus-interface.h"
#include "nm-setting-connection.h"
#include "nm-setting-ip4-config.h"
#include "nm-setting-vlan.h"
#include "nm-setting-ip6-config.h"
#include "nm-setting-wired.h"
#include "nm-setting-wireless.h"
#include "nm-setting-ethtool.h"
#include "nm-setting-8021x.h"
#include "nm-setting-bond.h"
#include "nm-setting-bond-port.h"
#include "nm-setting-team.h"
#include "nm-setting-team-port.h"
#include "nm-setting-bridge.h"
#include "nm-setting-bridge-port.h"
#include "nm-setting-dcb.h"
#include "nm-setting-user.h"
#include "nm-setting-proxy.h"
#include "nm-setting-generic.h"
#include "libnm-core-intern/nm-core-internal.h"
#include "nm-utils.h"
#include "libnm-base/nm-ethtool-base.h"
#include "libnm-platform/nm-platform.h"
#include "NetworkManagerUtils.h"
#include "nms-ifcfg-rh-common.h"
#include "nms-ifcfg-rh-utils.h"
#include "shvar.h"
/*****************************************************************************/
#define _NMLOG_DOMAIN LOGD_SETTINGS
#define _NMLOG_PREFIX_NAME "ifcfg-rh"
#define _NMLOG(level, ...) \
G_STMT_START \
{ \
nm_log((level), \
(_NMLOG_DOMAIN), \
NULL, \
NULL, \
"%s" _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \
_NMLOG_PREFIX_NAME ": " _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
} \
G_STMT_END
#define PARSE_WARNING(...) \
_LOGW("%s" _NM_UTILS_MACRO_FIRST(__VA_ARGS__), " " _NM_UTILS_MACRO_REST(__VA_ARGS__))
/*****************************************************************************/
static char *
get_full_file_path(const char *ifcfg_path, const char *file_path)
{
const char *base = file_path;
gs_free char *dirname = NULL;
char *p;
g_return_val_if_fail(ifcfg_path != NULL, NULL);
g_return_val_if_fail(file_path != NULL, NULL);
if (file_path[0] == '/')
return g_strdup(file_path);
p = strrchr(file_path, '/');
if (p)
base = p + 1;
dirname = g_path_get_dirname(ifcfg_path);
return g_build_path("/", dirname, base, NULL);
}
/*****************************************************************************/
static NMSettingSecretFlags
_secret_read_ifcfg_flags(shvarFile *ifcfg, const char *flags_key)
{
NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
gs_free char *val_free = NULL;
const char *val;
nm_assert(flags_key);
nm_assert(g_str_has_suffix(flags_key, "_FLAGS"));
val = svGetValueStr(ifcfg, flags_key, &val_free);
if (val) {
if (strstr(val, SECRET_FLAG_AGENT))
flags |= NM_SETTING_SECRET_FLAG_AGENT_OWNED;
if (strstr(val, SECRET_FLAG_NOT_SAVED))
flags |= NM_SETTING_SECRET_FLAG_NOT_SAVED;
if (strstr(val, SECRET_FLAG_NOT_REQUIRED))
flags |= NM_SETTING_SECRET_FLAG_NOT_REQUIRED;
}
return flags;
}
static void
_secret_read_ifcfg(shvarFile *ifcfg,
shvarFile *keys_ifcfg,
const char *name,
char **value,
NMSettingSecretFlags *flags)
{
char flags_key[250];
nm_sprintf_buf(flags_key, "%s_FLAGS", name);
*flags = _secret_read_ifcfg_flags(ifcfg, flags_key);
if (*flags != NM_SETTING_SECRET_FLAG_NONE)
*value = NULL;
else {
*value = svGetValue_cp(ifcfg, name);
if (!*value && keys_ifcfg)
*value = svGetValue_cp(keys_ifcfg, name);
}
}
static void
_secret_set_from_ifcfg(gpointer setting,
shvarFile *ifcfg,
shvarFile *keys_ifcfg,
const char *ifcfg_key,
const char *property_name)
{
nm_auto_free_secret char *secret = NULL;
NMSettingSecretFlags flags;
char flags_key[250];
nm_assert(NM_IS_SETTING(setting));
_secret_read_ifcfg(ifcfg, keys_ifcfg, ifcfg_key, &secret, &flags);
g_object_set(setting,
property_name,
secret,
nm_sprintf_buf(flags_key, "%s-flags", property_name),
flags,
NULL);
}
static gboolean
_secret_password_raw_to_bytes(const char *ifcfg_key,
const char *password_raw,
GBytes **out_bytes,
GError **error)
{
nm_auto_free_secret_buf NMSecretBuf *secret = NULL;
gsize len;
if (!password_raw) {
NM_SET_OUT(out_bytes, NULL);
return TRUE;
}
if (password_raw[0] == '0' && password_raw[1] == 'x')
password_raw += 2;
secret = nm_secret_buf_new(strlen(password_raw) / 2 + 3);
if (!nm_utils_hexstr2bin_full(password_raw,
FALSE,
FALSE,
FALSE,
":",
0,
secret->bin,
secret->len,
&len)) {
g_set_error(error,
NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid hex password in %s",
ifcfg_key);
return FALSE;
}
NM_SET_OUT(out_bytes, nm_secret_buf_to_gbytes_take(g_steal_pointer(&secret), len));
return TRUE;
}
/*****************************************************************************/
static GBytes *
_cert_get_cert_bytes(const char *ifcfg_path, const char *value, GError **error)
{
gs_free char *path = NULL;
if (NM_STR_HAS_PREFIX(value, "pkcs11:"))
return _nm_setting_802_1x_cert_value_to_bytes(NM_SETTING_802_1X_CK_SCHEME_PKCS11,
(guint8 *) value,
-1,
error);
path = get_full_file_path(ifcfg_path, value);
return _nm_setting_802_1x_cert_value_to_bytes(NM_SETTING_802_1X_CK_SCHEME_PATH,
(guint8 *) path,
-1,
error);
}
static gboolean
_cert_get_cert(shvarFile *ifcfg,
const char *ifcfg_key,
GBytes **out_cert,
NMSetting8021xCKScheme *out_scheme,
GError **error)
{
nm_auto_free_secret char *val_free = NULL;
const char *val;
gs_unref_bytes GBytes *cert = NULL;
GError *local = NULL;
NMSetting8021xCKScheme scheme;
val = svGetValueStr(ifcfg, ifcfg_key, &val_free);
if (!val) {
NM_SET_OUT(out_cert, NULL);
NM_SET_OUT(out_scheme, NM_SETTING_802_1X_CK_SCHEME_UNKNOWN);
return TRUE;
}
cert = _cert_get_cert_bytes(svFileGetName(ifcfg), val, &local);
if (!cert)
goto err;
scheme = _nm_setting_802_1x_cert_get_scheme(cert, &local);
if (scheme == NM_SETTING_802_1X_CK_SCHEME_UNKNOWN)
goto err;
NM_SET_OUT(out_cert, g_steal_pointer(&cert));
NM_SET_OUT(out_scheme, scheme);
return TRUE;
err:
g_set_error(error,
NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_INVALID_CONNECTION,
"invalid certificate %s: %s",
ifcfg_key,
local->message);
g_error_free(local);
return FALSE;
}
static gboolean
_cert_set_from_ifcfg(gpointer setting,
shvarFile *ifcfg,
const char *ifcfg_key,
const char *property_name,
GBytes **out_cert,
GError **error)
{
gs_unref_bytes GBytes *cert = NULL;
if (!_cert_get_cert(ifcfg, ifcfg_key, &cert, NULL, error))
return FALSE;
g_object_set(setting, property_name, cert, NULL);
NM_SET_OUT(out_cert, g_steal_pointer(&cert));
return TRUE;
}
/*****************************************************************************/
static void
check_if_bond_slave(shvarFile *ifcfg, NMSettingConnection *s_con)
{
gs_free char *value = NULL;
const char *v;
const char *master;
v = svGetValueStr(ifcfg, "MASTER_UUID", &value);
if (!v)
v = svGetValueStr(ifcfg, "MASTER", &value);
if (v) {
master = nm_setting_connection_get_master(s_con);
if (master) {
PARSE_WARNING("Already configured as slave of %s. Ignoring MASTER{_UUID}=\"%s\"",
master,
v);
return;
}
g_object_set(s_con,
NM_SETTING_CONNECTION_MASTER,
v,
NM_SETTING_CONNECTION_SLAVE_TYPE,
NM_SETTING_BOND_SETTING_NAME,
NULL);
}
/* We should be checking for SLAVE=yes as well, but NM used to not set that,
* so for backward-compatibility, we don't check.
*/
}
static void
check_if_team_slave(shvarFile *ifcfg, NMSettingConnection *s_con)
{
gs_free char *value = NULL;
const char *v;
const char *master;
v = svGetValueStr(ifcfg, "TEAM_MASTER_UUID", &value);
if (!v)
v = svGetValueStr(ifcfg, "TEAM_MASTER", &value);
if (!v)
return;
master = nm_setting_connection_get_master(s_con);
if (master) {
PARSE_WARNING("Already configured as slave of %s. Ignoring TEAM_MASTER{_UUID}=\"%s\"",
master,
v);
return;
}
g_object_set(s_con,
NM_SETTING_CONNECTION_MASTER,
v,
NM_SETTING_CONNECTION_SLAVE_TYPE,
NM_SETTING_TEAM_SETTING_NAME,
NULL);
}
static char *
make_connection_name(shvarFile *ifcfg,
const char *ifcfg_name,
const char *suggested,
const char *prefix)
{
char *full_name = NULL, *name;
/* If the ifcfg file already has a NAME, always use that */
name = svGetValueStr_cp(ifcfg, "NAME");
if (name)
return name;
/* Otherwise, construct a new NAME */
if (!prefix)
prefix = "System";
/* For cosmetic reasons, if the suggested name is the same as
* the ifcfg files name, don't use it. Mainly for wifi so that
* the SSID is shown in the connection ID instead of just "wlan0".
*/
if (suggested && strcmp(ifcfg_name, suggested))
full_name = g_strdup_printf("%s %s (%s)", prefix, suggested, ifcfg_name);
else
full_name = g_strdup_printf("%s %s", prefix, ifcfg_name);
return full_name;
}
static NMSetting *
make_connection_setting(const char *file,
shvarFile *ifcfg,
const char *type,
const char *suggested,
const char *prefix)
{
NMSettingConnection *s_con;
NMSettingConnectionLldp lldp;
const char *ifcfg_name = NULL;
gs_free char *new_id = NULL;
const char *uuid;
gs_free char *uuid_free = NULL;
gs_free char *value = NULL;
const char *v;
gs_free char *stable_id = NULL;
const char *const *iter;
int vint64, i_val;
ifcfg_name = utils_get_ifcfg_name(file, TRUE);
if (!ifcfg_name)
return NULL;
s_con = NM_SETTING_CONNECTION(nm_setting_connection_new());
new_id = make_connection_name(ifcfg, ifcfg_name, suggested, prefix);
g_object_set(s_con, NM_SETTING_CONNECTION_ID, new_id, NULL);
/* Try for a UUID key before falling back to hashing the file name */
uuid = svGetValueStr(ifcfg, "UUID", &uuid_free);
if (!uuid) {
uuid_free =
nm_uuid_generate_from_string_str(svFileGetName(ifcfg), -1, NM_UUID_TYPE_LEGACY, NULL);
uuid = uuid_free;
}
g_object_set(s_con,
NM_SETTING_CONNECTION_TYPE,
type,
NM_SETTING_CONNECTION_UUID,
uuid,
NM_SETTING_CONNECTION_STABLE_ID,
svGetValue(ifcfg, "STABLE_ID", &stable_id),
NULL);
v = svGetValueStr(ifcfg, "DEVICE", &value);
if (v) {
GError *error = NULL;
/* Only validate for NMU_IFACE_KERNEL, because ifcfg plugin anyway
* doesn't support OVS types. */
if (nm_utils_ifname_valid(v, NMU_IFACE_KERNEL, &error)) {
g_object_set(s_con, NM_SETTING_CONNECTION_INTERFACE_NAME, v, NULL);
} else {
PARSE_WARNING("invalid DEVICE name '%s': %s", v, error->message);
g_error_free(error);
}
}
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "LLDP", &value);
if (nm_streq0(v, "rx"))
lldp = NM_SETTING_CONNECTION_LLDP_ENABLE_RX;
else
lldp = svParseBoolean(v, NM_SETTING_CONNECTION_LLDP_DEFAULT);
/* Missing ONBOOT is treated as "ONBOOT=true" by the old network service */
g_object_set(s_con,
NM_SETTING_CONNECTION_AUTOCONNECT,
svGetValueBoolean(ifcfg, "ONBOOT", TRUE),
NM_SETTING_CONNECTION_AUTOCONNECT_PRIORITY,
(int) svGetValueInt64(ifcfg,
"AUTOCONNECT_PRIORITY",
10,
NM_SETTING_CONNECTION_AUTOCONNECT_PRIORITY_MIN,
NM_SETTING_CONNECTION_AUTOCONNECT_PRIORITY_MAX,
NM_SETTING_CONNECTION_AUTOCONNECT_PRIORITY_DEFAULT),
NM_SETTING_CONNECTION_AUTOCONNECT_RETRIES,
(int) svGetValueInt64(ifcfg, "AUTOCONNECT_RETRIES", 10, -1, G_MAXINT32, -1),
NM_SETTING_CONNECTION_MULTI_CONNECT,
(int) svGetValueInt64(ifcfg,
"MULTI_CONNECT",
10,
G_MININT32,
G_MAXINT32,
NM_CONNECTION_MULTI_CONNECT_DEFAULT),
NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES,
svGetValueBoolean(ifcfg,
"AUTOCONNECT_SLAVES",
NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_DEFAULT),
NM_SETTING_CONNECTION_LLDP,
lldp,
NULL);
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "USERS", &value);
if (v) {
gs_free const char **items = NULL;
items = nm_strsplit_set(v, " ");
for (iter = items; iter && *iter; iter++) {
if (!nm_setting_connection_add_permission(s_con, "user", *iter, NULL))
PARSE_WARNING("invalid USERS item '%s'", *iter);
}
}
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "ZONE", &value);
g_object_set(s_con, NM_SETTING_CONNECTION_ZONE, v, NULL);
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "SECONDARY_UUIDS", &value);
if (v) {
gs_free const char **items = NULL;
items = nm_strsplit_set(v, " \t");
for (iter = items; iter && *iter; iter++) {
if (!nm_setting_connection_add_secondary(s_con, *iter))
PARSE_WARNING("secondary connection UUID '%s' already added", *iter);
}
}
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "BRIDGE_UUID", &value);
if (!v)
v = svGetValueStr(ifcfg, "BRIDGE", &value);
if (v) {
const char *old_value;
if ((old_value = nm_setting_connection_get_master(s_con))) {
PARSE_WARNING("Already configured as slave of %s. Ignoring BRIDGE=\"%s\"",
old_value,
v);
} else {
g_object_set(s_con, NM_SETTING_CONNECTION_MASTER, v, NULL);
g_object_set(s_con,
NM_SETTING_CONNECTION_SLAVE_TYPE,
NM_SETTING_BRIDGE_SETTING_NAME,
NULL);
}
}
check_if_bond_slave(ifcfg, s_con);
check_if_team_slave(ifcfg, s_con);
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "OVS_PORT_UUID", &value);
if (!v)
v = svGetValueStr(ifcfg, "OVS_PORT", &value);
if (v) {
const char *old_value;
if ((old_value = nm_setting_connection_get_master(s_con))) {
PARSE_WARNING("Already configured as slave of %s. Ignoring OVS_PORT=\"%s\"",
old_value,
v);
} else {
g_object_set(s_con, NM_SETTING_CONNECTION_MASTER, v, NULL);
g_object_set(s_con,
NM_SETTING_CONNECTION_SLAVE_TYPE,
NM_SETTING_OVS_PORT_SETTING_NAME,
NULL);
}
}
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "VRF_UUID", &value);
if (!v)
v = svGetValueStr(ifcfg, "VRF", &value);
if (v) {
const char *old_value;
if ((old_value = nm_setting_connection_get_master(s_con))) {
PARSE_WARNING("Already configured as slave of %s. Ignoring VRF{_UUID}=\"%s\"",
old_value,
v);
} else {
g_object_set(s_con, NM_SETTING_CONNECTION_MASTER, v, NULL);
g_object_set(s_con,
NM_SETTING_CONNECTION_SLAVE_TYPE,
NM_SETTING_VRF_SETTING_NAME,
NULL);
}
}
nm_clear_g_free(&value);
v = svGetValueStr(ifcfg, "GATEWAY_PING_TIMEOUT", &value);
if (v) {
gint64 tmp;
tmp = _nm_utils_ascii_str_to_int64(v, 10, 0, G_MAXINT32 - 1, -1);
if (tmp >= 0) {
if (tmp > 600) {
tmp = 600;
PARSE_WARNING("invalid GATEWAY_PING_TIMEOUT time");
}
g_object_set(s_con, NM_SETTING_CONNECTION_GATEWAY_PING_TIMEOUT, (guint) tmp, NULL);
} else
PARSE_WARNING("invalid GATEWAY_PING_TIMEOUT time");
}
switch (svGetValueBoolean(ifcfg, "CONNECTION_METERED", -1)) {
case TRUE:
g_object_set(s_con, NM_SETTING_CONNECTION_METERED, NM_METERED_YES, NULL);
break;
case FALSE:
g_object_set(s_con, NM_SETTING_CONNECTION_METERED, NM_METERED_NO, NULL);
break;
}
vint64 = svGetValueInt64(ifcfg, "AUTH_RETRIES", 10, -1, G_MAXINT32, -1);
g_object_set(s_con, NM_SETTING_CONNECTION_AUTH_RETRIES, (int) vint64, NULL);
nm_clear_g_free(&value);
v = svGetValue(ifcfg, "DEVTIMEOUT", &value);
if (v) {
v = nm_str_skip_leading_spaces(v);
vint64 = _nm_utils_ascii_str_to_int64(v, 10, 0, ((gint64) G_MAXINT32) / 1000, -1);
if (vint64 != -1)
vint64 *= 1000;
else if (v[0] != '\0') {
char *endptr;
double d;
d = nm_g_ascii_strtod(v, &endptr);
endptr = nm_str_skip_leading_spaces(endptr);
if (errno == 0 && endptr[0] == '\0' && d >= 0.0) {
d *= 1000.0;
/* We round. Yes, this is not correct to round IEEE 754 floats in general,
* but sufficient for our case where we know that NetworkManager wrote the
* setting with up to 3 digits for the milliseconds. */
d += 0.5;
if (d >= 0.0 && d <= (double) G_MAXINT32)
vint64 = (gint64) d;
}
}
if (vint64 == -1)
PARSE_WARNING("invalid DEVTIMEOUT setting");
else
g_object_set(s_con, NM_SETTING_CONNECTION_WAIT_DEVICE_TIMEOUT, (int) vint64, NULL);
}
nm_clear_g_free(&value);
v = svGetValue(ifcfg, "MUD_URL", &value);
if (v)
g_object_set(s_con, NM_SETTING_CONNECTION_MUD_URL, v, NULL);
i_val = NM_SETTING_CONNECTION_MDNS_DEFAULT;
if (!svGetValueEnum(ifcfg, "MDNS", nm_setting_connection_mdns_get_type(), &i_val, NULL))
PARSE_WARNING("invalid MDNS setting");
g_object_set(s_con, NM_SETTING_CONNECTION_MDNS, i_val, NULL);
i_val = NM_SETTING_CONNECTION_LLMNR_DEFAULT;
if (!svGetValueEnum(ifcfg, "LLMNR", nm_setting_connection_llmnr_get_type(), &i_val, NULL))
PARSE_WARNING("invalid LLMNR setting");
g_object_set(s_con, NM_SETTING_CONNECTION_LLMNR, i_val, NULL);
i_val = NM_SETTING_CONNECTION_DNS_OVER_TLS_DEFAULT;
if (!svGetValueEnum(ifcfg,
"DNS_OVER_TLS",
nm_setting_connection_dns_over_tls_get_type(),
&i_val,
NULL))
PARSE_WARNING("invalid DNS_OVER_TLS setting");
g_object_set(s_con, NM_SETTING_CONNECTION_DNS_OVER_TLS, i_val, NULL);
return NM_SETTING(s_con);
}
static gboolean
read_ip4_address(shvarFile *ifcfg,
const char *tag,
gboolean *out_has_key,
guint32 *out_addr,
GError **error)
{
gs_free char *value_to_free = NULL;
const char *value;
in_addr_t a;
nm_assert(ifcfg);
nm_assert(tag);
nm_assert(!error || !*error);
value = svGetValueStr(ifcfg, tag, &value_to_free);
if (!value) {
NM_SET_OUT(out_has_key, FALSE);
NM_SET_OUT(out_addr, 0);
return TRUE;
}
if (inet_pton(AF_INET, value, &a) != 1) {
g_set_error(error,
NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid %s IP4 address '%s'",
tag,
value);
return FALSE;
}
NM_SET_OUT(out_has_key, TRUE);
NM_SET_OUT(out_addr, a);
return TRUE;
}
static gboolean
is_any_ip4_address_defined(shvarFile *ifcfg, int *idx)
{
int i, ignore, *ret_idx;
ret_idx = idx ?: &ignore;
for (i = -1; i <= 2; i++) {
gs_free char *value = NULL;
char tag[256];
if (svGetValueStr(ifcfg, numbered_tag(tag, "IPADDR", i), &value)) {
*ret_idx = i;
return TRUE;
}
if (svGetValueStr(ifcfg, numbered_tag(tag, "PREFIX", i), &value)) {
*ret_idx = i;
return TRUE;
}
if (svGetValueStr(ifcfg, numbered_tag(tag, "NETMASK", i), &value)) {
*ret_idx = i;
return TRUE;
}
}
return FALSE;
}
/* Returns TRUE on missing address or valid address */
static gboolean
read_full_ip4_address(shvarFile *ifcfg,
gint32 which,
NMIPAddress *base_addr,
NMIPAddress **out_address,
char **out_gateway,
GError **error)
{
char tag[256];
char prefix_tag[256];
guint32 ipaddr;
gs_free char *value = NULL;
const char *v;
int prefix = 0;
gboolean has_key;
guint32 a;
char inet_buf[NM_UTILS_INET_ADDRSTRLEN];
g_return_val_if_fail(which >= -1, FALSE);
g_return_val_if_fail(ifcfg != NULL, FALSE);
g_return_val_if_fail(out_address != NULL, FALSE);
g_return_val_if_fail(*out_address == NULL, FALSE);
g_return_val_if_fail(!error || !*error, FALSE);
/* IP address */
if (!read_ip4_address(ifcfg, numbered_tag(tag, "IPADDR", which), &has_key, &ipaddr, error))
return FALSE;
if (!has_key) {
if (!base_addr)
return TRUE;
nm_ip_address_get_address_binary(base_addr, &ipaddr);
}
/* Gateway */
if (out_gateway && !*out_gateway) {
if (!read_ip4_address(ifcfg, numbered_tag(tag, "GATEWAY", which), &has_key, &a, error))
return FALSE;
if (has_key)
*out_gateway = nm_utils_inet4_ntop_dup(a);
}
/* Prefix */
numbered_tag(prefix_tag, "PREFIX", which);
v = svGetValueStr(ifcfg, prefix_tag, &value);
if (v) {
prefix = _nm_utils_ascii_str_to_int64(v, 10, 0, 32, -1);
if (prefix < 0) {
g_set_error(error,
NM_SETTINGS_ERROR,
NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 prefix '%s'",
v);
return FALSE;
}
} else {
/* Fall back to NETMASK if no PREFIX was specified */
if (!read_ip4_address(ifcfg, numbered_tag(tag, "NETMASK", which), &has_key, &a, error))
return FALSE;
if (has_key)
prefix = nm_utils_ip4_netmask_to_prefix(a);
else {
if (base_addr)
prefix = nm_ip_address_get_prefix(base_addr);
else {
/* Try to autodetermine the prefix for the address' class */
prefix = _nm_utils_ip4_get_default_prefix(ipaddr);
PARSE_WARNING("missing %s, assuming %s/%d",
prefix_tag,
_nm_utils_inet4_ntop(ipaddr, inet_buf),
prefix);
}
}
}
*out_address = nm_ip_address_new_binary(AF_INET, &ipaddr, prefix, error);
if (*out_address)
return TRUE;
return FALSE;
}
/*****************************************************************************/
static gboolean
parse_route_line_is_comment(const char *line)
{
/* we obtained the line from a legacy route file. Here we skip
* empty lines and comments.
*
* initscripts compares: "$line" =~ '^[[:space:]]*(\#.*)?$'
*/
while (nm_utils_is_separator(line[0]))
line++;
if (NM_IN_SET(line[0], '\0', '#'))
return TRUE;
return FALSE;
}
/*****************************************************************************/
typedef enum {
PARSE_LINE_AF_FLAG_FOR_IPV4 = 0x01,
PARSE_LINE_AF_FLAG_FOR_IPV6 = 0x02,
} ParseLineAFFlag;
typedef struct {
const char *key;
/* the element is not available in this case. */
ParseLineAFFlag disabled : 3;
bool disabled_with_options_route : 1;
/* whether the element is to be ignored. Ignord is different from
* "disabled", because we still parse the option, but don't use it. */
ParseLineAFFlag ignore : 3;
bool int_base_16 : 1;
/* the type, one of PARSE_LINE_TYPE_* */
char type;
} ParseLineInfo;
typedef struct {
/* whether the command line option was found, and @v is
* initialized. */
bool has : 1;
union {
guint8 uint8;
guint32 uint32;
const char *str;
struct {
guint32 uint32;
bool lock : 1;
} uint32_with_lock;
struct {
NMIPAddr addr;
guint8 plen;
bool has_plen : 1;
} addr;
} v;
} ParseLineData;
enum {
/* route attributes */
PARSE_LINE_ATTR_ROUTE_TYPE,
PARSE_LINE_ATTR_ROUTE_TABLE,
PARSE_LINE_ATTR_ROUTE_SRC,
PARSE_LINE_ATTR_ROUTE_FROM,
PARSE_LINE_ATTR_ROUTE_TOS,
PARSE_LINE_ATTR_ROUTE_SCOPE,
PARSE_LINE_ATTR_ROUTE_ONLINK,
PARSE_LINE_ATTR_ROUTE_WINDOW,
PARSE_LINE_ATTR_ROUTE_CWND,
PARSE_LINE_ATTR_ROUTE_INITCWND,
PARSE_LINE_ATTR_ROUTE_INITRWND,
PARSE_LINE_ATTR_ROUTE_MTU,
/* iproute2 arguments that only matter when parsing the file. */
PARSE_LINE_ATTR_ROUTE_TO,
PARSE_LINE_ATTR_ROUTE_VIA,
PARSE_LINE_ATTR_ROUTE_METRIC,
/* iproute2 parameters that are well known and that we silently ignore. */
PARSE_LINE_ATTR_ROUTE_DEV,
};
#define PARSE_LINE_TYPE_UINT8 '8'
#define PARSE_LINE_TYPE_UINT32 'u'
#define PARSE_LINE_TYPE_UINT32_WITH_LOCK 'l'
#define PARSE_LINE_TYPE_ADDR 'a'
#define PARSE_LINE_TYPE_ADDR_WITH_PREFIX 'p'
#define PARSE_LINE_TYPE_IFNAME 'i'
#define PARSE_LINE_TYPE_FLAG 'f'
#define PARSE_LINE_TYPE_ROUTE_SCOPE 'S'
#define PARSE_LINE_TYPE_STRING 's'
/**
* parse_route_line:
* @line: the line to parse. This is either a line from the route-* or route6-* file,
* or the numbered OPTIONS setting.
* @addr_family: the address family.
* @options_route: (in-out): when line is from the OPTIONS setting, this is a pre-created
* route object that is completed with the settings from options. Otherwise,
* it shall point to %NULL and a new route is created and returned.
* @out_route: (out) (transfer-full) (allow-none): the parsed %NMIPRoute instance.
* In case a @options_route is passed in, it returns the input route that was modified
* in-place. But the caller must unref the returned route in either case.
* @error: the failure description.
*
* Parsing the route options line has two modes: one for the numbered OPTIONS
* setting, and one for initscript's handle_ip_file(), which takes the lines
* and passes them to `ip route add`. The modes are similar, but certain properties
* are not allowed for OPTIONS.
* The mode is differentiated by having an @options_route argument.
*
* Returns: returns a negative errno on failure. On success, it returns 0
* and @out_route.
*/
static int
parse_route_line(const char *line,
int addr_family,
NMIPRoute *options_route,
NMIPRoute **out_route,
GError **error)
{
static const ParseLineInfo parse_infos[] = {
[PARSE_LINE_ATTR_ROUTE_TYPE] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_TYPE,
.type = PARSE_LINE_TYPE_STRING,
},
[PARSE_LINE_ATTR_ROUTE_TABLE] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_TABLE,
.type = PARSE_LINE_TYPE_UINT32,
},
[PARSE_LINE_ATTR_ROUTE_SRC] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_SRC,
.type = PARSE_LINE_TYPE_ADDR,
},
[PARSE_LINE_ATTR_ROUTE_FROM] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_FROM,
.type = PARSE_LINE_TYPE_ADDR_WITH_PREFIX,
.disabled = PARSE_LINE_AF_FLAG_FOR_IPV4,
},
[PARSE_LINE_ATTR_ROUTE_TOS] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_TOS,
.type = PARSE_LINE_TYPE_UINT8,
.int_base_16 = TRUE,
.ignore = PARSE_LINE_AF_FLAG_FOR_IPV6,
},
[PARSE_LINE_ATTR_ROUTE_SCOPE] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_SCOPE,
.type = PARSE_LINE_TYPE_ROUTE_SCOPE,
.ignore = PARSE_LINE_AF_FLAG_FOR_IPV6,
},
[PARSE_LINE_ATTR_ROUTE_ONLINK] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_ONLINK,
.type = PARSE_LINE_TYPE_FLAG,
},
[PARSE_LINE_ATTR_ROUTE_WINDOW] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_WINDOW,
.type = PARSE_LINE_TYPE_UINT32_WITH_LOCK,
},
[PARSE_LINE_ATTR_ROUTE_CWND] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_CWND,
.type = PARSE_LINE_TYPE_UINT32_WITH_LOCK,
},
[PARSE_LINE_ATTR_ROUTE_INITCWND] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_INITCWND,
.type = PARSE_LINE_TYPE_UINT32_WITH_LOCK,
},
[PARSE_LINE_ATTR_ROUTE_INITRWND] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_INITRWND,
.type = PARSE_LINE_TYPE_UINT32_WITH_LOCK,
},
[PARSE_LINE_ATTR_ROUTE_MTU] =
{
.key = NM_IP_ROUTE_ATTRIBUTE_MTU,
.type = PARSE_LINE_TYPE_UINT32_WITH_LOCK,
},
[PARSE_LINE_ATTR_ROUTE_TO] =
{
.key = "to",
.type = PARSE_LINE_TYPE_ADDR_WITH_PREFIX,
.disabled_with_options_route = TRUE,
},
[PARSE_LINE_ATTR_ROUTE_VIA] =
{
.key = "via",
.type = PARSE_LINE_TYPE_ADDR,