about summary refs log tree commit diff
path: root/src/core/settings/plugins/ifcfg-rh/shvar.c
blob: 565e20f7ee4432c9864df7450a900b2bd8e427a4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 1999, 2000 Red Hat, Inc.
 */

#include "src/core/nm-default-daemon.h"

#include "shvar.h"

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "libnm-glib-aux/nm-str-buf.h"
#include "libnm-core-intern/nm-core-internal.h"
#include "nm-core-utils.h"
#include "libnm-glib-aux/nm-enum-utils.h"
#include "libnm-glib-aux/nm-io-utils.h"
#include "c-list/src/c-list.h"
#include "nms-ifcfg-rh-utils.h"

/*****************************************************************************/

struct _shvarLine {
    const char *key;

    CList lst;

    /* We index variables by their key in shvarFile.lst_idx. One shell variable might
     * occur multiple times in a file (in which case the last occurrence wins).
     * Hence, we need to keep a list of all the same keys.
     *
     * This is a pointer to the next shadowed line. */
    struct _shvarLine *prev_shadowed;

    /* There are three cases:
     *
     * 1) the line is not a valid variable assignment (that is, it doesn't
     *   start with a "FOO=" with possible whitespace prefix).
     *   In that case, @key and @key_with_prefix are %NULL, and the entire
     *   original line is in @line. Such entries are ignored for the most part.
     *
     * 2) if the line can be parsed with a "FOO=" assignment, then @line contains
     *   the part after '=', @key_with_prefix contains the key "FOO" with possible
     *   whitespace prefix, and @key points into @key_with_prefix skipping over the
     *   whitespace.
     *
     * 3) like 2, but if the value was deleted via svSetValue(), the entry is not removed,
     *   but only marked for deletion. That is done by clearing @line but preserving
     *   @key/@key_with_prefix.
     * */
    char *line;
    char *key_with_prefix;

    /* svSetValue() will clear the dirty flag. */
    bool dirty : 1;
};

typedef struct _shvarLine shvarLine;

struct _shvarFile {
    char       *fileName;
    CList       lst_head;
    GHashTable *lst_idx;
    int         fd;
    bool        modified : 1;
};

/*****************************************************************************/

static void _line_link_parse(shvarFile *s, const char *value, gsize len);

/*****************************************************************************/

#define ASSERT_key_is_well_known(key)                                  \
    nm_assert(({                                                       \
        const char *_key          = (key);                             \
        gboolean    _is_wellknown = TRUE;                              \
                                                                       \
        if (!nms_ifcfg_rh_utils_is_well_known_key(_key)) {             \
            _is_wellknown = FALSE;                                     \
            g_critical("ifcfg-rh key \"%s\" is not well-known", _key); \
        }                                                              \
                                                                       \
        _is_wellknown;                                                 \
    }))

/**
 * svParseBoolean:
 * @value: the input string
 * @fallback: the fallback value
 *
 * Parses a string and returns the boolean value it contains or,
 * in case no valid value is found, the fallback value. Valid values
 * are: "yes", "true", "t", "y", "1" and "no", "false", "f", "n", "0".
 *
 * Always sets errno. Either to zero on success, to ENOKEY for NULL
 * or to EINVAL otherwise.
 *
 * Returns: the parsed boolean value or @fallback.
 */
int
svParseBoolean(const char *value, int fallback)
{
    if (!value) {
        errno = ENOKEY;
        return fallback;
    }

    if (!g_ascii_strcasecmp("yes", value) || !g_ascii_strcasecmp("true", value)
        || !g_ascii_strcasecmp("t", value) || !g_ascii_strcasecmp("y", value)
        || !g_ascii_strcasecmp("1", value)) {
        errno = 0;
        return TRUE;
    } else if (!g_ascii_strcasecmp("no", value) || !g_ascii_strcasecmp("false", value)
               || !g_ascii_strcasecmp("f", value) || !g_ascii_strcasecmp("n", value)
               || !g_ascii_strcasecmp("0", value)) {
        errno = 0;
        return FALSE;
    }

    errno = EINVAL;
    return fallback;
}

/*****************************************************************************/

static gboolean
_shell_is_name(const char *key, gssize len)
{
    gssize i;

    /* whether @key is a valid identifier (name). */
    if (!key || len == 0)
        return FALSE;
    if (!g_ascii_isalpha(key[0]) && key[0] != '_')
        return FALSE;
    for (i = 1; TRUE; i++) {
        if (len < 0) {
            if (!key[i])
                return TRUE;
        } else {
            if (i >= len)
                return TRUE;
        }
        if (!g_ascii_isalnum(key[i]) && key[i] != '_')
            return FALSE;
    }
}

/*****************************************************************************/

/* like g_strescape(), except that it also escapes '\''' *sigh*.
 *
 * While at it, add $''. */
static char *
_escape_ansic(const char *source)
{
    const char *p;
    char       *dest;
    char       *q;
    gsize       n_alloc;

    nm_assert(source);

    n_alloc = 4;
    for (p = source; p[0]; p++) {
        switch (*p) {
        case '\b':
        case '\f':
        case '\n':
        case '\r':
        case '\t':
        case '\v':
        case '\\':
        case '"':
        case '\'':
            n_alloc += 2;
            break;
        default:
            if (!nm_ascii_is_regular(*p))
                n_alloc += 4;
            else
                n_alloc += 1;
            break;
        }
    }

    dest = g_malloc(n_alloc);

    q = dest;

    *q++ = '$';
    *q++ = '\'';

    for (p = source; p[0]; p++) {
        nm_assert(q < &dest[n_alloc]);
        switch (*p) {
        case '\b':
            *q++ = '\\';
            *q++ = 'b';
            break;
        case '\f':
            *q++ = '\\';
            *q++ = 'f';
            break;
        case '\n':
            *q++ = '\\';
            *q++ = 'n';
            break;
        case '\r':
            *q++ = '\\';
            *q++ = 'r';
            break;
        case '\t':
            *q++ = '\\';
            *q++ = 't';
            break;
        case '\v':
            *q++ = '\\';
            *q++ = 'v';
            break;
        case '\\':
        case '"':
        case '\'':
            *q++ = '\\';
            *q++ = *p;
            break;
        default:
            if (!nm_ascii_is_regular(*p)) {
                *q++ = '\\';
                *q++ = '0' + (((*p) >> 6) & 07);
                *q++ = '0' + (((*p) >> 3) & 07);
                *q++ = '0' + ((*p) & 07);
            } else
                *q++ = *p;
            break;
        }
    }
    *q++ = '\'';
    *q++ = '\0';

    nm_assert(q - dest == n_alloc);

    return dest;
}

/*****************************************************************************/

#define _char_req_escape(ch)     NM_IN_SET(ch, '"', '\\', '$', '`')
#define _char_req_escape_old(ch) NM_IN_SET(ch, '"', '\\', '$', '`', '\'', '~')
#define _char_req_quotes(ch)     NM_IN_SET(ch, ' ', '\'', '~', '\t', '|', '&', ';', '(', ')', '<', '>')

const char *
svEscape(const char *s, char **to_free)
{
    char *new;
    gsize    mangle          = 0;
    gboolean requires_quotes = FALSE;
    gsize    n_alloc;
    gsize    slen;
    gsize    i;
    gsize    j;
    gboolean all_ascii = TRUE;

    for (slen = 0; s[slen]; slen++) {
        if (_char_req_escape(s[slen]))
            mangle++;
        else if (_char_req_quotes(s[slen]))
            requires_quotes = TRUE;
        else if (!nm_ascii_is_regular(s[slen])) {
            if (nm_ascii_is_ctrl_or_del(s[slen])) {
                /* if the string contains newline we can only express it using ANSI C quotation
                 * (as we don't support line continuation).
                 * Additionally, ANSI control characters look odd with regular quotation, so handle
                 * them too. */
                return (*to_free = _escape_ansic(s));
            }
            all_ascii       = FALSE;
            requires_quotes = TRUE;
        }
    }

    if (!all_ascii && !g_utf8_validate(s, -1, NULL)) {
        /* The string is not valid ASCII/UTF-8. We can escape that via
         * _escape_ansic(), however the reader might have a problem to
         * do something sensible with the blob later.
         *
         * This is really a bug of the caller, which should not present us with
         * non-text in the first place. But at this place, we cannot handle the
         * error better, so just escape it. */
        return (*to_free = _escape_ansic(s));
    }

    if (!mangle && !requires_quotes) {
        *to_free = NULL;
        return s;
    }

    n_alloc = slen + mangle + 3; /* 3 is extra ""\0 */
    new     = g_malloc(n_alloc);

    j        = 0;
    new[j++] = '"';
    for (i = 0; i < slen; i++) {
        if (_char_req_escape(s[i]))
            new[j++] = '\\';
        new[j++] = s[i];
    }
    new[j++] = '"';
    new[j++] = '\0';

    nm_assert(j == n_alloc);

    *to_free = new;
    return new;
}

static gboolean
_looks_like_old_svescaped(const char *value)
{
    gsize k;

    if (value[0] != '"')
        return FALSE;

    for (k = 1;; k++) {
        if (value[k] == '\0')
            return FALSE;
        if (!_char_req_escape_old(value[k]))
            continue;

        if (value[k] == '"')
            return (value[k + 1] == '\0');
        else if (value[k] == '\\') {
            k++;
            if (!_char_req_escape_old(value[k]))
                return FALSE;
        } else
            return FALSE;
    }
}

static gboolean
_ch_octal_is(char ch)
{
    return ch >= '0' && ch < '8';
}

static guint8
_ch_octal_get(char ch)
{
    nm_assert(_ch_octal_is(ch));
    return (ch - '0');
}

static gboolean
_ch_hex_is(char ch)
{
    return g_ascii_isxdigit(ch);
}

static guint8
_ch_hex_get(char ch)
{
    nm_assert(_ch_hex_is(ch));
    return ch <= '9' ? ch - '0' : (ch & 0x4F) - 'A' + 10;
}

static void
_strbuf_init(NMStrBuf *str, const char *value, gsize i)
{
    nm_assert(str);
    nm_assert(value);

    if (str->allocated == 0) {
        /* if @str is not yet initialized, it initializes
         * a new NMStrBuf and copies @i characters from
         * @value over.
         *
         * Unescaping usually does not extend the length of a string,
         * so we might be tempted to allocate a fixed buffer of length
         * (strlen(value)+CONST).
         * However, due to $'\Ux' escapes, the maximum length is some
         * (FACTOR*strlen(value) + CONST), which is non trivial to get
         * right in all cases. Also, we would have to provision for the
         * very unlikely extreme case.
         * Instead, use a NMStrBuf buffer which can grow as needed. But for an
         * initial guess, strlen(value) is a good start */
        nm_str_buf_maybe_expand(str, strlen(value) + 3u, FALSE);
        nm_str_buf_append_len(str, value, i);
    }
}

const char *
svUnescape(const char *value, char **to_free)
{
    return svUnescape_full(value, to_free, TRUE);
}

const char *
svUnescape_full(const char *value, char **to_free, gboolean check_utf8)
{
    NMStrBuf str                      = NM_STR_BUF_INIT(0, FALSE);
    int      looks_like_old_svescaped = -1;
    gsize    i;
    gsize    j;

    /* we handle bash syntax here (note that ifup has #!/bin/bash.
     * Thus, see https://www.gnu.org/software/bash/manual/html_node/Quoting.html#Quoting */

    /* @value shall start with the first character after "FOO=" */

    nm_assert(value);
    nm_assert(to_free);

    /* we don't expect any newlines. They must be filtered out before-hand.
     * We also don't support line continuation. */
    nm_assert(!NM_STRCHAR_ANY(value, ch, ch == '\n'));

    i = 0;
    while (TRUE) {
        if (value[i] == '\0')
            goto out_value;

        if (g_ascii_isspace(value[i]) || value[i] == ';') {
            gboolean has_semicolon = (value[i] == ';');

            /* starting with space is only allowed, if the entire
             * string consists of spaces (possibly terminated by a comment).
             * This disallows for example
             *   LANG=C ls -1
             *   LANG=  ls -1
             * but allows
             *   LANG= #comment
             *
             * As a special case, we also allow one trailing semicolon, as long
             * it is only followed by whitespace or a #-comment.
             *   FOO=;
             *   FOO=a;
             *   FOO=b ; #hallo
             */
            j = i + 1;
            while (g_ascii_isspace(value[j])
                   || (!has_semicolon && (has_semicolon = (value[j] == ';'))))
                j++;
            if (!NM_IN_SET(value[j], '\0', '#'))
                goto out_error;
            goto out_value;
        }

        if (value[i] == '\\') {
            /* backslash escape */
            _strbuf_init(&str, value, i);
            i++;
            if (G_UNLIKELY(value[i] == '\0')) {
                /* we don't support line continuation */
                goto out_error;
            }
            nm_str_buf_append_c(&str, value[i]);
            i++;
            goto loop1_next;
        }

        if (value[i] == '\'') {
            /* single quotes */
            _strbuf_init(&str, value, i);
            i++;
            j = i;
            while (TRUE) {
                if (value[j] == '\0') {
                    /* unterminated single quote. We don't support line continuation */
                    goto out_error;
                }
                if (value[j] == '\'')
                    break;
                j++;
            }
            nm_str_buf_append_len(&str, &value[i], j - i);
            i = j + 1;
            goto loop1_next;
        }

        if (value[i] == '"') {
            /* double quotes */
            _strbuf_init(&str, value, i);
            i++;
            while (TRUE) {
                if (value[i] == '"') {
                    i++;
                    break;
                }
                if (value[i] == '\0') {
                    /* unterminated double quote. We don't support line continuation. */
                    goto out_error;
                }
                if (NM_IN_SET(value[i], '`', '$')) {
                    /* we don't support shell expansion. */
                    goto out_error;
                }
                if (value[i] == '\\') {
                    i++;
                    if (value[i] == '\0') {
                        /* we don't support line continuation */
                        goto out_error;
                    }
                    if (NM_IN_SET(value[i], '$', '`', '"', '\\')) {
                        /* Drop the backslash. */
                    } else if (NM_IN_SET(value[i], '\'', '~')) {
                        /* '\'' and '~' in double quotes are not handled special by shell.
                         * However, old versions of svEscape() would wrongly use double-quoting
                         * with backslash escaping for these characters (expecting svUnescape()
                         * to remove the backslash).
                         *
                         * In order to preserve previous behavior, we continue to read such
                         * strings different then shell does. */

                        /* Actually, we can relax this. Old svEscape() escaped the entire value
                         * in a particular way with double quotes.
                         * If the value doesn't exactly look like something as created by svEscape(),
                         * don't do the compat hack and preserve the backslash. */
                        if (looks_like_old_svescaped < 0)
                            looks_like_old_svescaped = _looks_like_old_svescaped(value);
                        if (!looks_like_old_svescaped)
                            nm_str_buf_append_c(&str, '\\');
                    } else
                        nm_str_buf_append_c(&str, '\\');
                }
                nm_str_buf_append_c(&str, value[i]);
                i++;
            }
            goto loop1_next;
        }

        if (value[i] == '$' && value[i + 1] == '\'') {
            /* ANSI-C Quoting */
            _strbuf_init(&str, value, i);
            i += 2;
            while (TRUE) {
                char ch;

                if (value[i] == '\'') {
                    i++;
                    break;
                }
                if (value[i] == '\0') {
                    /* unterminated double quote. We don't support line continuation. */
                    goto out_error;
                }
                if (value[i] == '\\') {
                    i++;
                    if (value[i] == '\0') {
                        /* we don't support line continuation */
                        goto out_error;
                    }
                    switch (value[i]) {
                    case 'a':
                        ch = '\a';
                        break;
                    case 'b':
                        ch = '\b';
                        break;
                    case 'e':
                        ch = '\e';
                        break;
                    case 'E':
                        ch = '\E';
                        break;
                    case 'f':
                        ch = '\f';
                        break;
                    case 'n':
                        ch = '\n';
                        break;
                    case 'r':
                        ch = '\r';
                        break;
                    case 't':
                        ch = '\t';
                        break;
                    case 'v':
                        ch = '\v';
                        break;
                    case '?':
                        ch = '\?';
                        break;
                    case '"':
                        ch = '"';
                        break;
                    case '\\':
                        ch = '\\';
                        break;
                    case '\'':
                        ch = '\'';
                        break;
                    default:
                        if (_ch_octal_is(value[i])) {
                            guint v;

                            v = _ch_octal_get(value[i]);
                            i++;
                            if (_ch_octal_is(value[i])) {
                                v = (v * 8) + _ch_octal_get(value[i]);
                                i++;
                                if (_ch_octal_is(value[i])) {
                                    v = (v * 8) + _ch_octal_get(value[i]);
                                    i++;
                                }
                            }
                            /* like bash, we cut too large numbers off. E.g. A=$'\772' becomes 0xfa  */
                            nm_str_buf_append_c(&str, (guint8) v);
                        } else if (NM_IN_SET(value[i], 'x', 'u', 'U')) {
                            const char escape_type = value[i];
                            int max_digits = escape_type == 'x' ? 2 : escape_type == 'u' ? 4 : 8;
                            guint64 v;

                            i++;
                            if (!_ch_hex_is(value[i])) {
                                /* missing hex value after "\x" escape. This is treated like no escaping. */
                                nm_str_buf_append_c(&str, '\\', escape_type);
                            } else {
                                v = _ch_hex_get(value[i]);
                                i++;

                                while (--max_digits > 0) {
                                    if (!_ch_hex_is(value[i]))
                                        break;
                                    v = v * 16 + _ch_hex_get(value[i]);
                                    i++;
                                }
                                if (escape_type == 'x')
                                    nm_str_buf_append_c(&str, v);
                                else {
                                    /* we treat the unicode escapes as utf-8 encoded values. */
                                    nm_str_buf_append_unichar(&str, v);
                                }
                            }
                        } else {
                            nm_str_buf_append_c(&str, '\\', value[i]);
                            i++;
                        }
                        goto loop_ansic_next;
                    }
                } else
                    ch = value[i];
                nm_str_buf_append_c(&str, ch);
                i++;
loop_ansic_next:;
            }
            goto loop1_next;
        }

        if (NM_IN_SET(value[i], '|', '&', '(', ')', '<', '>')) {
            /* shell metacharacters are not supported without quoting.
             * Note that ';' is already handled above. */
            goto out_error;
        }

        /* an unquoted, regular character. Just consume it directly. */
        if (str.allocated > 0)
            nm_str_buf_append_c(&str, value[i]);
        i++;

loop1_next:;
    }

    nm_assert_not_reached();

out_value:
    if (i == 0) {
        nm_assert(str.allocated == 0);
        nm_assert(!str._priv_str);
        *to_free = NULL;
        return "";
    }

    if (str.allocated > 0) {
        if (check_utf8 && !nm_str_buf_utf8_validate(&str))
            goto out_error;
        if (str.len == 0 || nm_str_buf_get_str_unsafe(&str)[0] == '\0') {
            nm_str_buf_destroy(&str);
            *to_free = NULL;
            return "";
        } else {
            *to_free = nm_str_buf_finalize(&str, NULL);
            return *to_free;
        }
    }

    if (check_utf8 && !g_utf8_validate(value, i, NULL)) {
        *to_free = NULL;
        return NULL;
    }

    if (value[i] != '\0') {
        *to_free = g_strndup(value, i);
        return *to_free;
    }

    *to_free = NULL;
    return value;

out_error:
    nm_str_buf_destroy(&str);
    *to_free = NULL;
    return NULL;
}

/*****************************************************************************/

shvarFile *
svFile_new(const char *name, int fd, const char *content)
{
    shvarFile  *s;
    const char *p;
    const char *q;

    nm_assert(name);
    nm_assert(fd >= -1);

    s  = g_slice_new(shvarFile);
    *s = (shvarFile) {
        .fileName = g_strdup(name),
        .fd       = fd,
        .lst_head = C_LIST_INIT(s->lst_head),
        .lst_idx  = g_hash_table_new(nm_pstr_hash, nm_pstr_equal),
    };

    if (content) {
        for (p = content; (q = strchr(p, '\n')) != NULL; p = q + 1)
            _line_link_parse(s, p, q - p);
        if (p[0])
            _line_link_parse(s, p, strlen(p));
    }

    return s;
}

const char *
svFileGetName(const shvarFile *s)
{
    nm_assert(s);

    return s->fileName;
}

void
_nmtst_svFileSetName(shvarFile *s, const char *fileName)
{
    /* changing the file name is not supported for regular
     * operation. Only allowed to use in tests, otherwise,
     * the filename is immutable. */
    g_free(s->fileName);
    s->fileName = g_strdup(fileName);
}

void
_nmtst_svFileSetModified(shvarFile *s)
{
    /* marking a file as modified is only for testing. */
    s->modified = TRUE;
}

/*****************************************************************************/

static void
ASSERT_shvarLine(const shvarLine *line)
{
#if NM_MORE_ASSERTS > 5
    const char *s, *s2;

    nm_assert(line);
    if (!line->key) {
        nm_assert(line->line);
        nm_assert(!line->key_with_prefix);
        s  = nm_str_skip_leading_spaces(line->line);
        s2 = strchr(s, '=');
        nm_assert(!s2 || !_shell_is_name(s, s2 - s));
    } else {
        nm_assert(line->key_with_prefix);
        nm_assert(line->key == nm_str_skip_leading_spaces(line->key_with_prefix));
        nm_assert(_shell_is_name(line->key, -1));
    }
#endif
}

static shvarLine *
line_new_parse(const char *value, gsize len)
{
    shvarLine *line;
    gsize      k, e;

    nm_assert(value);

    line  = g_slice_new(shvarLine);
    *line = (shvarLine) {
        .lst   = C_LIST_INIT(line->lst),
        .dirty = TRUE,
    };

    for (k = 0; k < len; k++) {
        if (g_ascii_isspace(value[k]))
            continue;

        if (g_ascii_isalpha(value[k]) || value[k] == '_') {
            for (e = k + 1; e < len; e++) {
                if (value[e] == '=') {
                    nm_assert(_shell_is_name(&value[k], e - k));
                    line->line            = g_strndup(&value[e + 1], len - e - 1);
                    line->key_with_prefix = g_strndup(value, e);
                    line->key             = &line->key_with_prefix[k];
                    ASSERT_shvarLine(line);
                    return line;
                }
                if (!g_ascii_isalnum(value[e]) && value[e] != '_')
                    break;
            }
        }
        break;
    }
    line->line = g_strndup(value, len);
    ASSERT_shvarLine(line);
    return line;
}

static shvarLine *
line_new_build(const char *key, const char *value)
{
    char      *value_escaped = NULL;
    shvarLine *line;
    char      *new_key;

    value = svEscape(value, &value_escaped);

    line    = g_slice_new(shvarLine);
    new_key = g_strdup(key), *line = (shvarLine) {
                                 .lst             = C_LIST_INIT(line->lst),
                                 .line            = value_escaped ?: g_strdup(value),
                                 .key_with_prefix = new_key,
                                 .key             = new_key,
                                 .dirty           = FALSE,
                             };
    ASSERT_shvarLine(line);
    return line;
}

static gboolean
line_set(shvarLine *line, const char *value)
{
    char    *value_escaped = NULL;
    gboolean changed       = FALSE;

    ASSERT_shvarLine(line);
    nm_assert(line->key);

    line->dirty = FALSE;

    if (line->key != line->key_with_prefix) {
        memmove(line->key_with_prefix, line->key, strlen(line->key) + 1);
        line->key = line->key_with_prefix;
        changed   = TRUE;
        ASSERT_shvarLine(line);
    }

    value = svEscape(value, &value_escaped);

    if (line->line) {
        if (nm_streq(value, line->line)) {
            g_free(value_escaped);
            return changed;
        }
        g_free(line->line);
    }

    line->line = value_escaped ?: g_strdup(value);
    ASSERT_shvarLine(line);
    return TRUE;
}

static void
line_free(shvarLine *line)
{
    ASSERT_shvarLine(line);
    c_list_unlink_stale(&line->lst);
    g_free(line->line);
    g_free(line->key_with_prefix);
    g_slice_free(shvarLine, line);
}

/*****************************************************************************/

static void
_line_link_parse(shvarFile *s, const char *value, gsize len)
{
    shvarLine *line;

    line = line_new_parse(value, len);
    if (!line->key)
        goto do_link;

    if (G_UNLIKELY(!g_hash_table_insert(s->lst_idx, line, line))) {
        shvarLine *existing_key;
        shvarLine *existing_val;

        /* Slow-path: we have duplicate keys. Fix the mess we created.
         * Unfortunately, g_hash_table_insert() now had to allocate an extra
         * array to track the keys/values differently. I wish there was an
         * GHashTable API to add a key only if it does not exist yet. */

        if (!g_hash_table_lookup_extended(s->lst_idx,
                                          line,
                                          (gpointer *) &existing_key,
                                          (gpointer *) &existing_val))
            nm_assert_not_reached();

        nm_assert(existing_val == line);
        nm_assert(existing_key != line);
        line->prev_shadowed = existing_key;
        g_hash_table_replace(s->lst_idx, line, line);
    }

do_link:
    c_list_link_tail(&s->lst_head, &line->lst);
}

/*****************************************************************************/

/* Open the file <name>, returning a shvarFile on success and NULL on failure.
 * Add a wrinkle to let the caller specify whether or not to create the file
 * (actually, return a structure anyway) if it doesn't exist.
 */
static shvarFile *
svOpenFileInternal(const char *name, gboolean create, GError **error)
{
    gboolean              closefd = FALSE;
    int                   errsv   = 0;
    gs_free char         *content = NULL;
    gs_free_error GError *local   = NULL;
    nm_auto_close int     fd      = -1;

    if (create)
        fd = open(name, O_RDWR | O_CLOEXEC); /* NOT O_CREAT */
    if (fd < 0) {
        /* try read-only */
        fd = open(name, O_RDONLY | O_CLOEXEC); /* NOT O_CREAT */
        if (fd < 0)
            errsv = errno;
        else
            closefd = TRUE;
    }

    if (fd < 0) {
        if (create)
            return svFile_new(name, -1, NULL);

        g_set_error(error,
                    G_FILE_ERROR,
                    g_file_error_from_errno(errsv),
                    "Could not read file '%s': %s",
                    name,
                    nm_strerror_native(errsv));
        return NULL;
    }

    if (!nm_utils_fd_get_contents(closefd ? nm_steal_fd(&fd) : fd,
                                  closefd,
                                  10 * 1024 * 1024,
                                  NM_UTILS_FILE_GET_CONTENTS_FLAG_NONE,
                                  &content,
                                  NULL,
                                  NULL,
                                  &local)) {
        if (create)
            return svFile_new(name, -1, NULL);

        g_set_error(error,
                    G_FILE_ERROR,
                    local->domain == G_FILE_ERROR ? local->code : G_FILE_ERROR_FAILED,
                    "Could not read file '%s': %s",
                    name,
                    local->message);
        return NULL;
    }

    /* closefd is set if we opened the file read-only, so go ahead and
     * close it, because we can't write to it anyway */
    nm_assert(closefd || fd >= 0);
    return svFile_new(name, !closefd ? nm_steal_fd(&fd) : -1, content);
}

/* Open the file <name>, return shvarFile on success, NULL on failure */
shvarFile *
svOpenFile(const char *name, GError **error)
{
    return svOpenFileInternal(name, FALSE, error);
}

/* Create a new file structure, returning actual data if the file exists,
 * and a suitable starting point if it doesn't.
 */
shvarFile *
svCreateFile(const char *name)
{
    return svOpenFileInternal(name, TRUE, NULL);
}

/*****************************************************************************/

static gboolean
_svKeyMatchesType(const char *key, SvKeyType match_key_type)
{
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_ANY))
        return TRUE;

#define _IS_NUMBERED(key, tag)                             \
    ({                                                     \
        gint64 _idx;                                       \
                                                           \
        NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG(key, tag, &_idx) \
        &&_idx >= 0;                                       \
    })

    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_ROUTE_SVFORMAT)) {
        if (_IS_NUMBERED(key, "ADDRESS") || _IS_NUMBERED(key, "NETMASK")
            || _IS_NUMBERED(key, "GATEWAY") || _IS_NUMBERED(key, "METRIC")
            || _IS_NUMBERED(key, "OPTIONS"))
            return TRUE;
    }
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_IP4_ADDRESS)) {
        if (_IS_NUMBERED(key, "IPADDR") || _IS_NUMBERED(key, "PREFIX")
            || _IS_NUMBERED(key, "NETMASK") || _IS_NUMBERED(key, "GATEWAY"))
            return TRUE;
    }
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_USER)) {
        if (g_str_has_prefix(key, "NM_USER_"))
            return TRUE;
    }
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_TC)) {
        if (_IS_NUMBERED(key, "QDISC") || _IS_NUMBERED(key, "FILTER"))
            return TRUE;
    }
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_SRIOV_VF)) {
        if (_IS_NUMBERED(key, "SRIOV_VF"))
            return TRUE;
    }
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_ROUTING_RULE4)) {
        if (_IS_NUMBERED(key, "ROUTING_RULE_"))
            return TRUE;
    }
    if (NM_FLAGS_HAS(match_key_type, SV_KEY_TYPE_ROUTING_RULE6)) {
        if (_IS_NUMBERED(key, "ROUTING_RULE6_"))
            return TRUE;
    }

    return FALSE;
}

gint64
svNumberedParseKey(const char *key)
{
    gint64 idx;

    if (NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG(key, "ROUTING_RULE_", &idx)
        || NMS_IFCFG_RH_UTIL_IS_NUMBERED_TAG(key, "ROUTING_RULE6_", &idx))
        return idx;
    return -1;
}

GHashTable *
svGetKeys(shvarFile *s, SvKeyType match_key_type)
{
    GHashTable      *keys = NULL;
    CList           *current;
    const shvarLine *line;

    nm_assert(s);

    c_list_for_each (current, &s->lst_head) {
        line = c_list_entry(current, shvarLine, lst);
        if (line->key && line->line && _svKeyMatchesType(line->key, match_key_type)) {
            /* we don't clone the keys. The keys are only valid
             * until @s gets modified. */
            if (!keys)
                keys = g_hash_table_new_full(nm_str_hash, g_str_equal, NULL, NULL);
            g_hash_table_add(keys, (gpointer) line->key);
        }
    }
    return keys;
}

static int
_get_keys_sorted_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
{
    const char *k_a = *((const char *const *) a);
    const char *k_b = *((const char *const *) b);
    gint64      n_a;
    gint64      n_b;

    n_a = svNumberedParseKey(k_a);
    n_b = svNumberedParseKey(k_b);
    NM_CMP_DIRECT(n_a, n_b);
    NM_CMP_RETURN(strcmp(k_a, k_b));
    nm_assert_not_reached();
    return 0;
}

const char **
svGetKeysSorted(shvarFile *s, SvKeyType match_key_type, guint *out_len)
{
    gs_unref_hashtable GHashTable *keys_hash = NULL;

    keys_hash = svGetKeys(s, match_key_type);
    if (!keys_hash) {
        NM_SET_OUT(out_len, 0);
        return NULL;
    }
    return (
        const char **) nm_utils_hash_keys_to_array(keys_hash, _get_keys_sorted_cmp, NULL, out_len);
}

/*****************************************************************************/

const char *
svFindFirstNumberedKey(shvarFile *s, const char *key_prefix)
{
    const shvarLine *line;

    g_return_val_if_fail(s, NULL);
    g_return_val_if_fail(key_prefix, NULL);

    c_list_for_each_entry (line, &s->lst_head, lst) {
        if (line->key && line->line
            && nms_ifcfg_rh_utils_is_numbered_tag(line->key, key_prefix, NULL))
            return line->key;
    }

    return NULL;
}

/*****************************************************************************/

static const char *
_svGetValue(shvarFile *s, const char *key, char **to_free)
{
    const shvarLine *line;
    const char      *v;

    nm_assert(s);
    nm_assert(_shell_is_name(key, -1));
    nm_assert(to_free);

    ASSERT_key_is_well_known(key);

    line = g_hash_table_lookup(s->lst_idx, &key);

    if (line && line->line) {
        v = svUnescape(line->line, to_free);
        if (!v) {
            /* a wrongly quoted value or non-UTF-8 is treated like the empty string.
             * See also svWriteFile(), which handles unparsable values that way. */
            nm_assert(!*to_free);
            return "";
        }
        return v;
    }
    *to_free = NULL;
    return NULL;
}

/* Returns the value for key. The value is either owned by @s
 * or returned as to_free. This aims to avoid cloning the string.
 *
 * - like svGetValue_cp(), but avoids cloning the value if possible.
 * - like svGetValueStr(), but does not ignore empty string values.
 */
const char *
svGetValue(shvarFile *s, const char *key, char **to_free)
{
    g_return_val_if_fail(s, NULL);
    g_return_val_if_fail(key, NULL);
    g_return_val_if_fail(to_free, NULL);

    return _svGetValue(s, key, to_free);
}

/* Returns the value for key. The value is either owned by @s
 * or returned as to_free. This aims to avoid cloning the string.
 *
 * - like svGetValue(), but does not return an empty string.
 * - like svGetValueStr_cp(), but avoids cloning the value if possible.
 */
const char *
svGetValueStr(shvarFile *s, const char *key, char **to_free)
{
    const char *value;

    g_return_val_if_fail(s, NULL);
    g_return_val_if_fail(key, NULL);
    g_return_val_if_fail(to_free, NULL);

    value = _svGetValue(s, key, to_free);
    if (!value || !value[0]) {
        nm_assert(!*to_free);
        return NULL;
    }
    return value;
}

/* Returns the value for key. The returned value must be freed
 * by the caller.
 *
 * - like svGetValue(), but always returns a copy of the value.
 * - like svGetValueStr_cp(), but does not ignore an empty string.
 */
char *
svGetValue_cp(shvarFile *s, const char *key)
{
    char       *to_free;
    const char *value;

    g_return_val_if_fail(s, NULL);
    g_return_val_if_fail(key, NULL);

    value = _svGetValue(s, key, &to_free);
    if (!value) {
        nm_assert(!to_free);
        return NULL;
    }
    return to_free ?: g_strdup(value);
}

/* Returns the value for key. The returned value must be freed
 * by the caller.
 * If the key is unset or the value an empty string, NULL is returned.
 *
 * - like svGetValueStr(), but always returns a copy of the value.
 * - like svGetValue_cp(), but returns NULL instead of an empty string.
 */
char *
svGetValueStr_cp(shvarFile *s, const char *key)
{
    char       *to_free;
    const char *value;

    g_return_val_if_fail(s, NULL);
    g_return_val_if_fail(key, NULL);

    value = _svGetValue(s, key, &to_free);
    if (!value || !value[0]) {
        nm_assert(!to_free);
        return NULL;
    }
    return to_free ?: g_strdup(value);
}

/* svGetValueBoolean:
 * @s: fhe file
 * @key: the name of the key to read
 * @fallback: the fallback value in any error case
 *
 * Reads a value @key and converts it to a boolean using svParseBoolean().
 * This always sets errno, see svParseBoolean().
 *
 * Returns: the parsed boolean value or @fallback.
 */
int
svGetValueBoolean(shvarFile *s, const char *key, int fallback)
{
    gs_free char *to_free = NULL;
    const char   *value;

    value = _svGetValue(s, key, &to_free);
    return svParseBoolean(value, fallback);
}

/* svGetValueTernary:
 * @s: fhe file
 * @key: the name of the key to read
 *
 * Reads a value @key and converts it to a NMTernary value.
 * This always sets errno, see svParseBoolean().
 *
 * Returns: the parsed NMTernary
 */
NMTernary
svGetValueTernary(shvarFile *s, const char *key)
{
    return svGetValueBoolean(s, key, NM_TERNARY_DEFAULT);
}

/* svGetValueInt64:
 * @s: fhe file
 * @key: the name of the key to read
 * @base: the numeric base (usually 10). Setting to 0 means "auto". Usually you want 10.
 * @min: the minimum for range-check
 * @max: the maximum for range-check
 * @fallback: the fallback value in any error case
 *
 * Reads a value @key and converts it to an integer using _nm_utils_ascii_str_to_int64().
 * In case of error, @errno will be set and @fallback returned. */
gint64
svGetValueInt64(shvarFile *s, const char *key, guint base, gint64 min, gint64 max, gint64 fallback)
{
    char       *to_free;
    const char *value;
    gint64      result;
    int         errsv;

    value = _svGetValue(s, key, &to_free);
    if (!value) {
        nm_assert(!to_free);
        /* indicate that the key does not exist (or has a syntax error
         * and svUnescape() failed). */
        errno = ENOKEY;
        return fallback;
    }

    result = _nm_utils_ascii_str_to_int64(value, base, min, max, fallback);
    if (to_free) {
        errsv = errno;
        g_free(to_free);
        errno = errsv;
    }
    return result;
}

gboolean
svGetValueEnum(shvarFile *s, const char *key, GType gtype, int *out_value, GError **error)
{
    gs_free char *to_free = NULL;
    const char   *svalue;
    gs_free char *err_token = NULL;
    int           value;

    svalue = _svGetValue(s, key, &to_free);
    if (!svalue) {
        /* don't touch out_value. The caller is supposed
         * to initialize it with the default value. */
        errno = ENOKEY;
        return TRUE;
    }

    if (!nm_utils_enum_from_str(gtype, svalue, &value, &err_token)) {
        g_set_error(error,
                    NM_UTILS_ERROR,
                    NM_UTILS_ERROR_UNKNOWN,
                    "Invalid token \"%s\" in \"%s\" for %s",
                    err_token,
                    svalue,
                    key);
        errno = EINVAL;
        return FALSE;
    }

    NM_SET_OUT(out_value, value);
    errno = 0;
    return TRUE;
}

/*****************************************************************************/

gboolean
svUnsetAll(shvarFile *s, SvKeyType match_key_type)
{
    shvarLine *line;
    gboolean   changed = FALSE;

    g_return_val_if_fail(s, FALSE);

    c_list_for_each_entry (line, &s->lst_head, lst) {
        ASSERT_shvarLine(line);
        if (line->key && _svKeyMatchesType(line->key, match_key_type)) {
            if (nm_clear_g_free(&line->line)) {
                ASSERT_shvarLine(line);
                changed = TRUE;
            }
        }
    }

    if (changed)
        s->modified = TRUE;
    return changed;
}

gboolean
svUnsetDirtyWellknown(shvarFile *s, NMTernary new_dirty_value)
{
    shvarLine *line;
    gboolean   changed = FALSE;

    g_return_val_if_fail(s, FALSE);

    c_list_for_each_entry (line, &s->lst_head, lst) {
        const NMSIfcfgKeyTypeInfo *ti;

        ASSERT_shvarLine(line);

        if (line->dirty && line->key && line->line
            && (ti = nms_ifcfg_rh_utils_is_well_known_key(line->key))
            && !NM_FLAGS_HAS(ti->key_flags, NMS_IFCFG_KEY_TYPE_KEEP_WHEN_DIRTY)) {
            if (nm_clear_g_free(&line->line)) {
                ASSERT_shvarLine(line);
                changed = TRUE;
            }
        }

        if (new_dirty_value != NM_TERNARY_DEFAULT)
            line->dirty = (new_dirty_value != NM_TERNARY_FALSE);
    }

    if (changed)
        s->modified = TRUE;
    return changed;
}

/* Same as svSetValueStr() but it preserves empty @value -- contrary to
 * svSetValueStr() for which "" effectively means to remove the value. */
gboolean
svSetValue(shvarFile *s, const char *key, const char *value)
{
    shvarLine *line;
    shvarLine *l_shadowed;
    gboolean   changed = FALSE;

    g_return_val_if_fail(s, FALSE);
    g_return_val_if_fail(key, FALSE);

    nm_assert(_shell_is_name(key, -1));

    ASSERT_key_is_well_known(key);

    line = g_hash_table_lookup(s->lst_idx, &key);
    if (line && (l_shadowed = line->prev_shadowed)) {
        /* if we find multiple entries for the same key, we can
         * delete the shadowed ones. */
        line->prev_shadowed = NULL;
        changed             = TRUE;
        do {
            shvarLine *l = l_shadowed;

            l_shadowed = l_shadowed->prev_shadowed;
            line_free(l);
        } while (l_shadowed);
    }

    if (!value) {
        if (line) {
            /* We only clear the value, but leave the line entry. This way, if we
             * happen to re-add the value, we write it to the same line again. */
            if (nm_clear_g_free(&line->line)) {
                changed = TRUE;
            }
        }
    } else {
        if (!line) {
            line = line_new_build(key, value);
            if (!g_hash_table_add(s->lst_idx, line))
                nm_assert_not_reached();
            c_list_link_tail(&s->lst_head, &line->lst);
            changed = TRUE;
        } else {
            if (line_set(line, value))
                changed = TRUE;
        }
    }

    if (changed)
        s->modified = TRUE;
    return changed;
}

/* Set the variable <key> equal to the value <value>.
 * If <key> does not exist, and the <current> pointer is set, append
 * the key=value pair after that line.  Otherwise, append the pair
 * to the bottom of the file.
 */
gboolean
svSetValueStr(shvarFile *s, const char *key, const char *value)
{
    return svSetValue(s, key, value && value[0] ? value : NULL);
}

gboolean
svSetValueInt64(shvarFile *s, const char *key, gint64 value)
{
    char buf[NM_DECIMAL_STR_MAX(value)];

    return svSetValue(s, key, nm_sprintf_buf(buf, "%" G_GINT64_FORMAT, value));
}

gboolean
svSetValueInt64_cond(shvarFile *s, const char *key, gboolean do_set, gint64 value)
{
    if (do_set)
        return svSetValueInt64(s, key, value);
    else
        return svUnsetValue(s, key);
}

gboolean
svSetValueBoolean(shvarFile *s, const char *key, gboolean value)
{
    return svSetValue(s, key, value ? "yes" : "no");
}

gboolean
svSetValueTernary(shvarFile *s, const char *key, NMTernary value)
{
    if (NM_IN_SET(value, NM_TERNARY_TRUE, NM_TERNARY_FALSE))
        return svSetValueBoolean(s, key, (gboolean) value);
    else
        return svUnsetValue(s, key);
}

gboolean
svSetValueBoolean_cond_true(shvarFile *s, const char *key, gboolean value)
{
    return svSetValue(s, key, value ? "yes" : NULL);
}

gboolean
svSetValueEnum(shvarFile *s, const char *key, GType gtype, int value)
{
    gs_free char *v = NULL;

    v = _nm_utils_enum_to_str_full(gtype, value, " ", NULL);
    return svSetValueStr(s, key, v);
}

gboolean
svUnsetValue(shvarFile *s, const char *key)
{
    return svSetValue(s, key, NULL);
}

/*****************************************************************************/

void
svWarnInvalid(shvarFile *s, const char *file_type, NMLogDomain log_domain)
{
    shvarLine *line;
    gsize      n;

    if (!nm_logging_enabled(LOGL_WARN, log_domain))
        return;

    n = 0;
    c_list_for_each_entry (line, &s->lst_head, lst) {
        gs_free char *s_tmp = NULL;

        n++;

        if (!line->key) {
            const char *str;

            nm_assert(line->line);
            str = nm_str_skip_leading_spaces(line->line);
            if (!NM_IN_SET(str[0], '\0', '#')) {
                nm_log_warn(log_domain,
                            "ifcfg-rh: %s,%s:%zu: invalid line ignored",
                            file_type,
                            s->fileName,
                            n);
            }
            continue;
        }

        if (g_hash_table_lookup(s->lst_idx, line) != line) {
            nm_log_warn(
                log_domain,
                "ifcfg-rh: %s,%s:%zu: key %s is duplicated and the early occurrence ignored",
                file_type,
                s->fileName,
                n,
                line->key);
            continue;
        }

        if (!line->line) {
            /* the line is deleted via svUnsetValue(). Ignore. */
            continue;
        }

        if (!svUnescape(line->line, &s_tmp)) {
            if (!svUnescape_full(line->line, &s_tmp, FALSE)) {
                nm_log_warn(log_domain,
                            "ifcfg-rh: %s,%s:%zu: key %s is badly quoted and is treated as \"\"",
                            file_type,
                            s->fileName,
                            n,
                            line->key);
            } else {
                nm_log_warn(log_domain,
                            "ifcfg-rh: %s,%s:%zu: key %s does not contain valid UTF-8 and is "
                            "treated as \"\"",
                            file_type,
                            s->fileName,
                            n,
                            line->key);
            }
            continue;
        }

        /* TODO: we read different shell scripts, and whether a key is recognized
         * depends on the type. For example, alias files only accept a subset of
         * known keys.
         *
         * Basically, depending on the @file_type, different keys are valid. */
        if (!nms_ifcfg_rh_utils_is_well_known_key(line->key)) {
            nm_log_dbg(log_domain,
                       "ifcfg-rh: %s,%s:%zu: key %s is unknown and ignored",
                       file_type,
                       s->fileName,
                       n,
                       line->key);
            continue;
        }
    }
}

/*****************************************************************************/

/* Write the current contents iff modified.  Returns FALSE on error
 * and TRUE on success.  Do not write if no values have been modified.
 * The mode argument is only used if creating the file, not if
 * re-writing an existing file, and is passed unchanged to the
 * open() syscall.
 */
gboolean
svWriteFile(shvarFile *s, int mode, GError **error)
{
    FILE  *f;
    int    tmpfd;
    CList *current;
    int    errsv;

    if (s->modified) {
        if (s->fd == -1)
            s->fd = open(s->fileName, O_WRONLY | O_CREAT | O_CLOEXEC, mode);
        if (s->fd == -1) {
            errsv = errno;
            g_set_error(error,
                        G_FILE_ERROR,
                        g_file_error_from_errno(errsv),
                        "Could not open file '%s' for writing: %s",
                        s->fileName,
                        nm_strerror_native(errsv));
            return FALSE;
        }
        if (ftruncate(s->fd, 0) < 0) {
            errsv = errno;
            g_set_error(error,
                        G_FILE_ERROR,
                        g_file_error_from_errno(errsv),
                        "Could not overwrite file '%s': %s",
                        s->fileName,
                        nm_strerror_native(errsv));
            return FALSE;
        }

        tmpfd = fcntl(s->fd, F_DUPFD_CLOEXEC, 0);
        if (tmpfd == -1) {
            errsv = errno;
            g_set_error(error,
                        G_FILE_ERROR,
                        g_file_error_from_errno(errsv),
                        "Internal error writing file '%s': %s",
                        s->fileName,
                        nm_strerror_native(errsv));
            return FALSE;
        }
        f = fdopen(tmpfd, "w");
        if (!f) {
            errsv = errno;
            g_set_error(error,
                        G_FILE_ERROR,
                        g_file_error_from_errno(errsv),
                        "Internal error writing file '%s': %s",
                        s->fileName,
                        nm_strerror_native(errsv));
            return FALSE;
        }
        fseek(f, 0, SEEK_SET);
        c_list_for_each (current, &s->lst_head) {
            const shvarLine *line = c_list_entry(current, shvarLine, lst);
            const char      *str;
            char            *s_tmp;
            gboolean         valid_value;

            ASSERT_shvarLine(line);

            if (!line->key) {
                str = nm_str_skip_leading_spaces(line->line);
                if (NM_IN_SET(str[0], '\0', '#'))
                    fprintf(f, "%s\n", line->line);
                else
                    fprintf(f, "#NM: %s\n", line->line);
                continue;
            }

            if (!line->line)
                continue;

            /* we check that the assignment can be properly unescaped. */
            valid_value = !!svUnescape(line->line, &s_tmp);
            g_free(s_tmp);

            if (valid_value)
                fprintf(f, "%s=%s\n", line->key_with_prefix, line->line);
            else {
                fprintf(f, "%s=\n", line->key);
                fprintf(f, "#NM: %s=%s\n", line->key_with_prefix, line->line);
            }
        }
        fclose(f);
    }

    return TRUE;
}

void
svCloseFile(shvarFile *s)
{
    shvarLine *line;

    g_return_if_fail(s != NULL);

    if (s->fd >= 0)
        nm_close(s->fd);
    g_free(s->fileName);
    g_hash_table_destroy(s->lst_idx);
    while ((line = c_list_first_entry(&s->lst_head, shvarLine, lst)))
        line_free(line);
    g_slice_free(shvarFile, s);
}