File: schemas.c

package info (click to toggle)
pacemaker 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 68,576 kB
  • sloc: xml: 160,564; ansic: 143,744; python: 5,670; sh: 2,969; makefile: 2,426
file content (1604 lines) | stat: -rw-r--r-- 50,022 bytes parent folder | download
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
/*
 * Copyright 2004-2025 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU Lesser General Public License
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>             // UCHAR_MAX
#include <sys/stat.h>
#include <stdarg.h>

#include <libxml/relaxng.h>
#include <libxml/tree.h>                // xmlNode
#include <libxml/xmlstring.h>           // xmlChar
#include <libxslt/xslt.h>
#include <libxslt/transform.h>
#include <libxslt/security.h>
#include <libxslt/xsltutils.h>

#include <crm/common/schemas.h>
#include <crm/common/xml.h>
#include <crm/common/xml_internal.h>  /* PCMK__XML_LOG_BASE */

#include "crmcommon_private.h"

#define SCHEMA_ZERO { .v = { 0, 0 } }

#define schema_strdup_printf(prefix, version, suffix) \
    crm_strdup_printf(prefix "%u.%u" suffix, (version).v[0], (version).v[1])

typedef struct {
    xmlRelaxNGPtr rng;
    xmlRelaxNGValidCtxtPtr valid;
    xmlRelaxNGParserCtxtPtr parser;
} relaxng_ctx_cache_t;

static GList *known_schemas = NULL;
static bool initialized = false;
static bool silent_logging = FALSE;

static void G_GNUC_PRINTF(2, 3)
xml_log(int priority, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    if (silent_logging == FALSE) {
        /* XXX should not this enable dechunking as well? */
        PCMK__XML_LOG_BASE(priority, FALSE, 0, NULL, fmt, ap);
    }
    va_end(ap);
}

static int
xml_latest_schema_index(void)
{
    /* This function assumes that pcmk__schema_init() has been called
     * beforehand, so we have at least two schemas (one real schema and the
     * "none" schema).
     *
     * @COMPAT: The "none" schema is deprecated since 2.1.8.
     * Update this when we drop that schema.
     */
    return g_list_length(known_schemas) - 2;
}

/*!
 * \internal
 * \brief Return the schema entry of the highest-versioned schema
 *
 * \return Schema entry of highest-versioned schema
 */
static GList *
get_highest_schema(void)
{
    /* The highest numerically versioned schema is the one before none
     *
     * @COMPAT none is deprecated since 2.1.8
     */
    GList *entry = pcmk__get_schema("none");

    pcmk__assert((entry != NULL) && (entry->prev != NULL));
    return entry->prev;
}

/*!
 * \internal
 * \brief Return the name of the highest-versioned schema
 *
 * \return Name of highest-versioned schema (or NULL on error)
 */
const char *
pcmk__highest_schema_name(void)
{
    GList *entry = get_highest_schema();

    return ((pcmk__schema_t *)(entry->data))->name;
}

/*!
 * \internal
 * \brief Find first entry of highest major schema version series
 *
 * \return Schema entry of first schema with highest major version
 */
GList *
pcmk__find_x_0_schema(void)
{
#if defined(PCMK__UNIT_TESTING)
    /* If we're unit testing, this can't be static because it'll stick
     * around from one test run to the next. It needs to be cleared out
     * every time.
     */
    GList *x_0_entry = NULL;
#else
    static GList *x_0_entry = NULL;
#endif

    pcmk__schema_t *highest_schema = NULL;

    if (x_0_entry != NULL) {
        return x_0_entry;
    }
    x_0_entry = get_highest_schema();
    highest_schema = x_0_entry->data;

    for (GList *iter = x_0_entry->prev; iter != NULL; iter = iter->prev) {
        pcmk__schema_t *schema = iter->data;

        /* We've found a schema in an older major version series.  Return
         * the index of the first one in the same major version series as
         * the highest schema.
         */
        if (schema->version.v[0] < highest_schema->version.v[0]) {
            x_0_entry = iter->next;
            break;
        }

        /* We're out of list to examine.  This probably means there was only
         * one major version series, so return the first schema entry.
         */
        if (iter->prev == NULL) {
            x_0_entry = known_schemas->data;
            break;
        }
    }
    return x_0_entry;
}

static inline bool
version_from_filename(const char *filename, pcmk__schema_version_t *version)
{
    if (pcmk__ends_with(filename, ".rng")) {
        return sscanf(filename, "pacemaker-%hhu.%hhu.rng", &(version->v[0]), &(version->v[1])) == 2;
    } else {
        return sscanf(filename, "pacemaker-%hhu.%hhu", &(version->v[0]), &(version->v[1])) == 2;
    }
}

static int
schema_filter(const struct dirent *a)
{
    int rc = 0;
    pcmk__schema_version_t version = SCHEMA_ZERO;

    if (strstr(a->d_name, "pacemaker-") != a->d_name) {
        /* crm_trace("%s - wrong prefix", a->d_name); */

    } else if (!pcmk__ends_with_ext(a->d_name, ".rng")) {
        /* crm_trace("%s - wrong suffix", a->d_name); */

    } else if (!version_from_filename(a->d_name, &version)) {
        /* crm_trace("%s - wrong format", a->d_name); */

    } else {
        /* crm_debug("%s - candidate", a->d_name); */
        rc = 1;
    }

    return rc;
}

static int
schema_cmp(pcmk__schema_version_t a_version, pcmk__schema_version_t b_version)
{
    for (int i = 0; i < 2; ++i) {
        if (a_version.v[i] < b_version.v[i]) {
            return -1;
        } else if (a_version.v[i] > b_version.v[i]) {
            return 1;
        }
    }
    return 0;
}

static int
schema_cmp_directory(const struct dirent **a, const struct dirent **b)
{
    pcmk__schema_version_t a_version = SCHEMA_ZERO;
    pcmk__schema_version_t b_version = SCHEMA_ZERO;

    if (!version_from_filename(a[0]->d_name, &a_version)
        || !version_from_filename(b[0]->d_name, &b_version)) {
        // Shouldn't be possible, but makes static analysis happy
        return 0;
    }

    return schema_cmp(a_version, b_version);
}

/*!
 * \internal
 * \brief Add given schema + auxiliary data to internal bookkeeping.
 */
static void
add_schema(enum pcmk__schema_validator validator,
           const pcmk__schema_version_t *version, const char *name,
           GList *transforms)
{
    pcmk__schema_t *schema = NULL;

    schema = pcmk__assert_alloc(1, sizeof(pcmk__schema_t));

    schema->validator = validator;
    schema->version.v[0] = version->v[0];
    schema->version.v[1] = version->v[1];
    schema->transforms = transforms;
    // schema->schema_index is set after all schemas are loaded and sorted

    if (version->v[0] || version->v[1]) {
        schema->name = schema_strdup_printf("pacemaker-", *version, "");
    } else {
        schema->name = pcmk__str_copy(name);
    }

    known_schemas = g_list_prepend(known_schemas, schema);
}

static void
wrap_libxslt(bool finalize)
{
    static xsltSecurityPrefsPtr secprefs;
    int ret = 0;

    /* security framework preferences */
    if (!finalize) {
        pcmk__assert(secprefs == NULL);
        secprefs = xsltNewSecurityPrefs();
        ret = xsltSetSecurityPrefs(secprefs, XSLT_SECPREF_WRITE_FILE,
                                   xsltSecurityForbid)
              | xsltSetSecurityPrefs(secprefs, XSLT_SECPREF_CREATE_DIRECTORY,
                                     xsltSecurityForbid)
              | xsltSetSecurityPrefs(secprefs, XSLT_SECPREF_READ_NETWORK,
                                     xsltSecurityForbid)
              | xsltSetSecurityPrefs(secprefs, XSLT_SECPREF_WRITE_NETWORK,
                                     xsltSecurityForbid);
        if (ret != 0) {
            return;
        }
    } else {
        xsltFreeSecurityPrefs(secprefs);
        secprefs = NULL;
    }

    /* cleanup only */
    if (finalize) {
        xsltCleanupGlobals();
    }
}

/*!
 * \internal
 * \brief Check whether a directory entry matches the upgrade XSLT pattern
 *
 * \param[in] entry  Directory entry whose filename to check
 *
 * \return 1 if the entry's filename is of the form
 *         <tt>upgrade-X.Y-ORDER.xsl</tt> with each number in the range 0 to
 *         255, or 0 otherwise
 */
static int
transform_filter(const struct dirent *entry)
{
    const char *re = NULL;
    unsigned int major = 0;
    unsigned int minor = 0;
    unsigned int order = 0;

    /* Each number is an unsigned char, which is 1 to 3 digits long. (Pacemaker
     * requires an 8-bit char via a configure test.)
     */
    re = "upgrade-[[:digit:]]{1,3}\\.[[:digit:]]{1,3}-[[:digit:]]{1,3}\\.xsl";

    if (!pcmk__str_eq(entry->d_name, re, pcmk__str_regex)) {
        return 0;
    }

    /* Performance isn't critical here and this is simpler than range-checking
     * within the regex
     */
    if (sscanf(entry->d_name, "upgrade-%u.%u-%u.xsl",
               &major, &minor, &order) != 3) {
        return 0;
    }

    if ((major > UCHAR_MAX) || (minor > UCHAR_MAX) || (order > UCHAR_MAX)) {
        return 0;
    }
    return 1;
}

/*!
 * \internal
 * \brief Compare transform files based on the version strings in their names
 *
 * This is a crude version comparison that relies on the specific structure of
 * these filenames.
 *
 * \retval -1 if \p entry1 sorts before \p entry2
 * \retval  0 if \p entry1 sorts equal to \p entry2
 * \retval  1 if \p entry1 sorts after \p entry2
 *
 * \note The GNU \c versionsort() function would be perfect here, but it's not
 *       portable.
 */
static int
compare_transforms(const struct dirent **entry1, const struct dirent **entry2)
{
    unsigned char major1 = 0;
    unsigned char major2 = 0;
    unsigned char minor1 = 0;
    unsigned char minor2 = 0;
    unsigned char order1 = 0;
    unsigned char order2 = 0;

    // If these made it through the filter, they should be of the right format
    CRM_LOG_ASSERT(sscanf((*entry1)->d_name, "upgrade-%hhu.%hhu-%hhu.xsl",
                          &major1, &minor1, &order1) == 3);
    CRM_LOG_ASSERT(sscanf((*entry2)->d_name, "upgrade-%hhu.%hhu-%hhu.xsl",
                          &major2, &minor2, &order2) == 3);

    if (major1 < major2) {
        return -1;
    } else if (major1 > major2) {
        return 1;
    }

    if (minor1 < minor2) {
        return -1;
    } else if (minor1 > minor2) {
        return 1;
    }

    if (order1 < order2) {
        return -1;
    } else if (order1 > order2) {
        return 1;
    }

    return 0;
}

/*!
 * \internal
 * \brief Free a list of XSLT transform <tt>struct dirent</tt> objects
 *
 * \param[in,out] data  List to free
 */
static void
free_transform_list(void *data)
{
    g_list_free_full((GList *) data, free);
}

/*!
 * \internal
 * \brief Load names of upgrade XSLT stylesheets from a directory into a table
 *
 * Stylesheets must have names of the form "upgrade-X.Y-order.xsl", where:
 * * X is the schema major version
 * * Y is the schema minor version
 * * ORDER is the order in which the stylesheet occurs in the transform pipeline
 *
 * \param[in] dir  Directory containing XSLT stylesheets
 *
 * \return Table with schema version as key and \c GList of associated transform
 *         files (as <tt>struct dirent</tt>) as value
 */
static GHashTable *
load_transforms_from_dir(const char *dir)
{
    struct dirent **namelist = NULL;
    GHashTable *transforms = NULL;
    int num_matches = scandir(dir, &namelist, transform_filter,
                              compare_transforms);

    if (num_matches < 0) {
        int rc = errno;

        crm_warn("Could not load transforms from %s: %s", dir, pcmk_rc_str(rc));
        goto done;
    }

    transforms = pcmk__strkey_table(free, free_transform_list);

    for (int i = 0; i < num_matches; i++) {
        pcmk__schema_version_t version = SCHEMA_ZERO;
        unsigned char order = 0;  // Placeholder only

        if (sscanf(namelist[i]->d_name, "upgrade-%hhu.%hhu-%hhu.xsl",
                   &(version.v[0]), &(version.v[1]), &order) == 3) {

            char *version_s = crm_strdup_printf("%hhu.%hhu",
                                                version.v[0], version.v[1]);
            GList *list = g_hash_table_lookup(transforms, version_s);

            if (list == NULL) {
                /* Prepend is more efficient. However, there won't be many of
                 * these, and we want them to remain sorted by version. It's not
                 * worth reversing all the lists at the end.
                 *
                 * Avoid calling g_hash_table_insert() if the list already
                 * exists. Otherwise free_transform_list() gets called on it.
                 */
                list = g_list_append(list, namelist[i]);
                g_hash_table_insert(transforms, version_s, list);

            } else {
                list = g_list_append(list, namelist[i]);
                free(version_s);
            }

        } else {
            // Sanity only, should never happen thanks to transform_filter()
            free(namelist[i]);
        }
    }

done:
    free(namelist);
    return transforms;
}

void
pcmk__load_schemas_from_dir(const char *dir)
{
    int lpc, max;
    struct dirent **namelist = NULL;
    GHashTable *transforms = NULL;

    max = scandir(dir, &namelist, schema_filter, schema_cmp_directory);
    if (max < 0) {
        int rc = errno;

        crm_warn("Could not load schemas from %s: %s", dir, pcmk_rc_str(rc));
        goto done;
    }

    // Look for any upgrade transforms in the same directory
    transforms = load_transforms_from_dir(dir);

    for (lpc = 0; lpc < max; lpc++) {
        pcmk__schema_version_t version = SCHEMA_ZERO;

        if (version_from_filename(namelist[lpc]->d_name, &version)) {
            char *version_s = crm_strdup_printf("%hhu.%hhu",
                                                version.v[0], version.v[1]);
            char *orig_key = NULL;
            GList *transform_list = NULL;

            if (transforms != NULL) {
                // The schema becomes the owner of transform_list
                g_hash_table_lookup_extended(transforms, version_s,
                                             (gpointer *) &orig_key,
                                             (gpointer *) &transform_list);
                g_hash_table_steal(transforms, version_s);
            }

            add_schema(pcmk__schema_validator_rng, &version, NULL,
                       transform_list);

            free(version_s);
            free(orig_key);

        } else {
            // Shouldn't be possible, but makes static analysis happy
            crm_warn("Skipping schema '%s': could not parse version",
                     namelist[lpc]->d_name);
        }
    }

    for (lpc = 0; lpc < max; lpc++) {
        free(namelist[lpc]);
    }

done:
    free(namelist);
    if (transforms != NULL) {
        g_hash_table_destroy(transforms);
    }
}

static gint
schema_sort_GCompareFunc(gconstpointer a, gconstpointer b)
{
    const pcmk__schema_t *schema_a = a;
    const pcmk__schema_t *schema_b = b;

    // @COMPAT The "none" schema is deprecated since 2.1.8
    if (pcmk__str_eq(schema_a->name, PCMK_VALUE_NONE, pcmk__str_none)) {
        return 1;
    } else if (pcmk__str_eq(schema_b->name, PCMK_VALUE_NONE, pcmk__str_none)) {
        return -1;
    } else {
        return schema_cmp(schema_a->version, schema_b->version);
    }
}

/*!
 * \internal
 * \brief Sort the list of known schemas such that all pacemaker-X.Y are in
 *        version order, then "none"
 *
 * This function should be called whenever additional schemas are loaded using
 * \c pcmk__load_schemas_from_dir(), after the initial sets in
 * \c pcmk__schema_init().
 */
void
pcmk__sort_schemas(void)
{
    known_schemas = g_list_sort(known_schemas, schema_sort_GCompareFunc);
}

/*!
 * \internal
 * \brief Load pacemaker schemas into cache
 *
 * \note This currently also serves as an entry point for the
 *       generic initialization of the libxslt library.
 */
void
pcmk__schema_init(void)
{
    if (!initialized) {
        const char *remote_schema_dir = pcmk__remote_schema_dir();
        char *base = pcmk__xml_artefact_root(pcmk__xml_artefact_ns_legacy_rng);
        const pcmk__schema_version_t zero = SCHEMA_ZERO;
        int schema_index = 0;

        initialized = true;

        wrap_libxslt(false);

        pcmk__load_schemas_from_dir(base);
        pcmk__load_schemas_from_dir(remote_schema_dir);
        free(base);

        // @COMPAT Deprecated since 2.1.8
        add_schema(pcmk__schema_validator_none, &zero, PCMK_VALUE_NONE, NULL);

        /* add_schema() prepends items to the list, so in the simple case, this
         * just reverses the list. However if there were any remote schemas,
         * sorting is necessary.
         */
        pcmk__sort_schemas();

        // Now set the schema indexes and log the final result
        for (GList *iter = known_schemas; iter != NULL; iter = iter->next) {
            pcmk__schema_t *schema = iter->data;

            crm_debug("Loaded schema %d: %s", schema_index, schema->name);
            schema->schema_index = schema_index++;
        }
    }
}

static bool
validate_with_relaxng(xmlDocPtr doc, xmlRelaxNGValidityErrorFunc error_handler,
                      void *error_handler_context, const char *relaxng_file,
                      relaxng_ctx_cache_t **cached_ctx)
{
    int rc = 0;
    bool valid = true;
    relaxng_ctx_cache_t *ctx = NULL;

    CRM_CHECK(doc != NULL, return false);
    CRM_CHECK(relaxng_file != NULL, return false);

    if (cached_ctx && *cached_ctx) {
        ctx = *cached_ctx;

    } else {
        crm_debug("Creating RNG parser context");
        ctx = pcmk__assert_alloc(1, sizeof(relaxng_ctx_cache_t));

        ctx->parser = xmlRelaxNGNewParserCtxt(relaxng_file);
        CRM_CHECK(ctx->parser != NULL, goto cleanup);

        if (error_handler) {
            xmlRelaxNGSetParserErrors(ctx->parser,
                                      (xmlRelaxNGValidityErrorFunc) error_handler,
                                      (xmlRelaxNGValidityWarningFunc) error_handler,
                                      error_handler_context);
        } else {
            xmlRelaxNGSetParserErrors(ctx->parser,
                                      (xmlRelaxNGValidityErrorFunc) fprintf,
                                      (xmlRelaxNGValidityWarningFunc) fprintf,
                                      stderr);
        }

        ctx->rng = xmlRelaxNGParse(ctx->parser);
        CRM_CHECK(ctx->rng != NULL,
                  crm_err("Could not find/parse %s", relaxng_file);
                  goto cleanup);

        ctx->valid = xmlRelaxNGNewValidCtxt(ctx->rng);
        CRM_CHECK(ctx->valid != NULL, goto cleanup);

        if (error_handler) {
            xmlRelaxNGSetValidErrors(ctx->valid,
                                     (xmlRelaxNGValidityErrorFunc) error_handler,
                                     (xmlRelaxNGValidityWarningFunc) error_handler,
                                     error_handler_context);
        } else {
            xmlRelaxNGSetValidErrors(ctx->valid,
                                     (xmlRelaxNGValidityErrorFunc) fprintf,
                                     (xmlRelaxNGValidityWarningFunc) fprintf,
                                     stderr);
        }
    }

    rc = xmlRelaxNGValidateDoc(ctx->valid, doc);
    if (rc > 0) {
        valid = false;

    } else if (rc < 0) {
        crm_err("Internal libxml error during validation");
    }

  cleanup:

    if (cached_ctx) {
        *cached_ctx = ctx;

    } else {
        if (ctx->parser != NULL) {
            xmlRelaxNGFreeParserCtxt(ctx->parser);
        }
        if (ctx->valid != NULL) {
            xmlRelaxNGFreeValidCtxt(ctx->valid);
        }
        if (ctx->rng != NULL) {
            xmlRelaxNGFree(ctx->rng);
        }
        free(ctx);
    }

    return valid;
}

static void
free_schema(gpointer data)
{
    pcmk__schema_t *schema = data;
    relaxng_ctx_cache_t *ctx = NULL;

    switch (schema->validator) {
        case pcmk__schema_validator_none: // not cached
            break;

        case pcmk__schema_validator_rng: // cached
            ctx = (relaxng_ctx_cache_t *) schema->cache;
            if (ctx == NULL) {
                break;
            }

            if (ctx->parser != NULL) {
                xmlRelaxNGFreeParserCtxt(ctx->parser);
            }

            if (ctx->valid != NULL) {
                xmlRelaxNGFreeValidCtxt(ctx->valid);
            }

            if (ctx->rng != NULL) {
                xmlRelaxNGFree(ctx->rng);
            }

            free(ctx);
            schema->cache = NULL;
            break;
    }

    free(schema->name);
    g_list_free_full(schema->transforms, free);
    free(schema);
}

/*!
 * \internal
 * \brief Clean up global memory associated with XML schemas
 */
void
pcmk__schema_cleanup(void)
{
    if (known_schemas != NULL) {
        g_list_free_full(known_schemas, free_schema);
        known_schemas = NULL;
    }
    initialized = false;

    wrap_libxslt(true);
}

/*!
 * \internal
 * \brief Get schema list entry corresponding to a schema name
 *
 * \param[in] name  Name of schema to get
 *
 * \return Schema list entry corresponding to \p name, or NULL if unknown
 */
GList *
pcmk__get_schema(const char *name)
{
    if (name == NULL) {
        return NULL;
    }
    for (GList *iter = known_schemas; iter != NULL; iter = iter->next) {
        pcmk__schema_t *schema = iter->data;

        if (pcmk__str_eq(name, schema->name, pcmk__str_none)) {
            return iter;
        }
    }
    return NULL;
}

/*!
 * \internal
 * \brief Compare two schema version numbers given the schema names
 *
 * \param[in] schema1  Name of first schema to compare
 * \param[in] schema2  Name of second schema to compare
 *
 * \return Standard comparison result (negative integer if \p schema1 has the
 *         lower version number, positive integer if \p schema1 has the higher
 *         version number, of 0 if the version numbers are equal)
 */
int
pcmk__cmp_schemas_by_name(const char *schema1_name, const char *schema2_name)
{
    GList *entry1 = pcmk__get_schema(schema1_name);
    GList *entry2 = pcmk__get_schema(schema2_name);

    if (entry1 == NULL) {
        return (entry2 == NULL)? 0 : -1;

    } else if (entry2 == NULL) {
        return 1;

    } else {
        pcmk__schema_t *schema1 = entry1->data;
        pcmk__schema_t *schema2 = entry2->data;

        return schema1->schema_index - schema2->schema_index;
    }
}

static bool
validate_with(xmlNode *xml, pcmk__schema_t *schema,
              xmlRelaxNGValidityErrorFunc error_handler,
              void *error_handler_context)
{
    bool valid = false;
    char *file = NULL;
    relaxng_ctx_cache_t **cache = NULL;

    if (schema == NULL) {
        return false;
    }

    if (schema->validator == pcmk__schema_validator_none) {
        return true;
    }

    file = pcmk__xml_artefact_path(pcmk__xml_artefact_ns_legacy_rng,
                                   schema->name);

    crm_trace("Validating with %s (type=%d)",
              pcmk__s(file, "missing schema"), schema->validator);
    switch (schema->validator) {
        case pcmk__schema_validator_rng:
            cache = (relaxng_ctx_cache_t **) &(schema->cache);
            valid = validate_with_relaxng(xml->doc, error_handler, error_handler_context, file, cache);
            break;
        default:
            crm_err("Unknown validator type: %d", schema->validator);
            break;
    }

    free(file);
    return valid;
}

static bool
validate_with_silent(xmlNode *xml, pcmk__schema_t *schema)
{
    bool rc, sl_backup = silent_logging;
    silent_logging = TRUE;
    rc = validate_with(xml, schema, (xmlRelaxNGValidityErrorFunc) xml_log, GUINT_TO_POINTER(LOG_ERR));
    silent_logging = sl_backup;
    return rc;
}

bool
pcmk__validate_xml(xmlNode *xml_blob, const char *validation,
                   xmlRelaxNGValidityErrorFunc error_handler,
                   void *error_handler_context)
{
    GList *entry = NULL;
    pcmk__schema_t *schema = NULL;

    CRM_CHECK((xml_blob != NULL) && (xml_blob->doc != NULL), return false);

    if (validation == NULL) {
        validation = crm_element_value(xml_blob, PCMK_XA_VALIDATE_WITH);
    }
    pcmk__warn_if_schema_deprecated(validation);

    entry = pcmk__get_schema(validation);
    if (entry == NULL) {
        pcmk__config_err("Cannot validate CIB with %s " PCMK_XA_VALIDATE_WITH
                         " (manually edit to use a known schema)",
                         ((validation == NULL)? "missing" : "unknown"));
        return false;
    }

    schema = entry->data;
    return validate_with(xml_blob, schema, error_handler,
                         error_handler_context);
}

/*!
 * \internal
 * \brief Validate XML using its configured schema (and send errors to logs)
 *
 * \param[in] xml  XML to validate
 *
 * \return true if XML validates, otherwise false
 */
bool
pcmk__configured_schema_validates(xmlNode *xml)
{
    return pcmk__validate_xml(xml, NULL,
                              (xmlRelaxNGValidityErrorFunc) xml_log,
                              GUINT_TO_POINTER(LOG_ERR));
}

/* With this arrangement, an attempt to identify the message severity
   as explicitly signalled directly from XSLT is performed in rather
   a smart way (no reliance on formatting string + arguments being
   always specified as ["%s", purposeful_string], as it can also be
   ["%s: %s", some_prefix, purposeful_string] etc. so every argument
   pertaining %s specifier is investigated), and if such a mark found,
   the respective level is determined and, when the messages are to go
   to the native logs, the mark itself gets dropped
   (by the means of string shift).

   NOTE: whether the native logging is the right sink is decided per
         the ctx parameter -- NULL denotes this case, otherwise it
         carries a pointer to the numeric expression of the desired
         target logging level (messages with higher level will be
         suppressed)

   NOTE: on some architectures, this string shift may not have any
         effect, but that's an acceptable tradeoff

   The logging level for not explicitly designated messages
   (suspicious, likely internal errors or some runaways) is
   LOG_WARNING.
 */
static void G_GNUC_PRINTF(2, 3)
cib_upgrade_err(void *ctx, const char *fmt, ...)
{
    va_list ap, aq;
    char *arg_cur;

    bool found = false;
    const char *fmt_iter = fmt;
    uint8_t msg_log_level = LOG_WARNING;  /* default for runaway messages */
    const unsigned * log_level = (const unsigned *) ctx;
    enum {
        escan_seennothing,
        escan_seenpercent,
    } scan_state = escan_seennothing;

    va_start(ap, fmt);
    va_copy(aq, ap);

    while (!found && *fmt_iter != '\0') {
        /* while casing schema borrowed from libqb:qb_vsnprintf_serialize */
        switch (*fmt_iter++) {
        case '%':
            if (scan_state == escan_seennothing) {
                scan_state = escan_seenpercent;
            } else if (scan_state == escan_seenpercent) {
                scan_state = escan_seennothing;
            }
            break;
        case 's':
            if (scan_state == escan_seenpercent) {
                size_t prefix_len = 0;

                scan_state = escan_seennothing;
                arg_cur = va_arg(aq, char *);

                if (pcmk__starts_with(arg_cur, "WARNING: ")) {
                    prefix_len = sizeof("WARNING: ") - 1;
                    msg_log_level = LOG_WARNING;

                } else if (pcmk__starts_with(arg_cur, "INFO: ")) {
                    prefix_len = sizeof("INFO: ") - 1;
                    msg_log_level = LOG_INFO;

                } else if (pcmk__starts_with(arg_cur, "DEBUG: ")) {
                    prefix_len = sizeof("DEBUG: ") - 1;
                    msg_log_level = LOG_DEBUG;

                } else {
                    break;
                }

                found = true;
                if (ctx == NULL) {
                    memmove(arg_cur, arg_cur + prefix_len,
                            strlen(arg_cur + prefix_len) + 1);
                }
            }
            break;
        case '#': case '-': case ' ': case '+': case '\'': case 'I': case '.':
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
        case '*':
            break;
        case 'l':
        case 'z':
        case 't':
        case 'j':
        case 'd': case 'i':
        case 'o':
        case 'u':
        case 'x': case 'X':
        case 'e': case 'E':
        case 'f': case 'F':
        case 'g': case 'G':
        case 'a': case 'A':
        case 'c':
        case 'p':
            if (scan_state == escan_seenpercent) {
                (void) va_arg(aq, void *);  /* skip forward */
                scan_state = escan_seennothing;
            }
            break;
        default:
            scan_state = escan_seennothing;
            break;
        }
    }

    if (log_level != NULL) {
        /* intention of the following offset is:
           cibadmin -V -> start showing INFO labelled messages */
        if (*log_level + 4 >= msg_log_level) {
            vfprintf(stderr, fmt, ap);
        }
    } else {
        PCMK__XML_LOG_BASE(msg_log_level, TRUE, 0, "CIB upgrade: ", fmt, ap);
    }

    va_end(aq);
    va_end(ap);
}

/*!
 * \internal
 * \brief Apply a single XSL transformation to given XML
 *
 * \param[in] xml        XML to transform
 * \param[in] transform  XSL name
 * \param[in] to_logs    If false, certain validation errors will be sent to
 *                       stderr rather than logged
 *
 * \return Transformed XML on success, otherwise NULL
 */
static xmlNode *
apply_transformation(const xmlNode *xml, const char *transform,
                     gboolean to_logs)
{
    char *xform = NULL;
    xmlNode *out = NULL;
    xmlDocPtr res = NULL;
    xsltStylesheet *xslt = NULL;

    xform = pcmk__xml_artefact_path(pcmk__xml_artefact_ns_legacy_xslt,
                                    transform);

    /* for capturing, e.g., what's emitted via <xsl:message> */
    if (to_logs) {
        xsltSetGenericErrorFunc(NULL, cib_upgrade_err);
    } else {
        xsltSetGenericErrorFunc(&crm_log_level, cib_upgrade_err);
    }

    xslt = xsltParseStylesheetFile((const xmlChar *) xform);
    CRM_CHECK(xslt != NULL, goto cleanup);

    /* Caller allocates private data for final result document. Intermediate
     * result documents are temporary and don't need private data.
     */
    res = xsltApplyStylesheet(xslt, xml->doc, NULL);
    CRM_CHECK(res != NULL, goto cleanup);

    xsltSetGenericErrorFunc(NULL, NULL);  /* restore default one */

    out = xmlDocGetRootElement(res);

  cleanup:
    if (xslt) {
        xsltFreeStylesheet(xslt);
    }

    free(xform);

    return out;
}

/*!
 * \internal
 * \brief Perform all transformations needed to upgrade XML to next schema
 *
 * \param[in] input_xml     XML to transform
 * \param[in] schema_index  Index of schema that successfully validates
 *                          \p original_xml
 * \param[in] to_logs       If false, certain validation errors will be sent to
 *                          stderr rather than logged
 *
 * \return XML result of schema transforms if successful, otherwise NULL
 */
static xmlNode *
apply_upgrade(const xmlNode *input_xml, int schema_index, gboolean to_logs)
{
    pcmk__schema_t *schema = g_list_nth_data(known_schemas, schema_index);
    pcmk__schema_t *upgraded_schema = g_list_nth_data(known_schemas,
                                                      schema_index + 1);

    xmlNode *old_xml = NULL;
    xmlNode *new_xml = NULL;
    xmlRelaxNGValidityErrorFunc error_handler = NULL;

    pcmk__assert((schema != NULL) && (upgraded_schema != NULL));

    if (to_logs) {
        error_handler = (xmlRelaxNGValidityErrorFunc) xml_log;
    }

    for (GList *iter = schema->transforms; iter != NULL; iter = iter->next) {
        const struct dirent *entry = iter->data;
        const char *transform = entry->d_name;

        crm_debug("Upgrading schema from %s to %s: applying XSL transform %s",
                  schema->name, upgraded_schema->name, transform);

        new_xml = apply_transformation(input_xml, transform, to_logs);
        pcmk__xml_free(old_xml);

        if (new_xml == NULL) {
            crm_err("XSL transform %s failed, aborting upgrade", transform);
            return NULL;
        }
        input_xml = new_xml;
        old_xml = new_xml;
    }

    // Final result document from upgrade pipeline needs private data
    pcmk__xml_new_private_data((xmlNode *) new_xml->doc);

    // Ensure result validates with its new schema
    if (!validate_with(new_xml, upgraded_schema, error_handler,
                       GUINT_TO_POINTER(LOG_ERR))) {
        crm_err("Schema upgrade from %s to %s failed: "
                "XSL transform pipeline produced an invalid configuration",
                schema->name, upgraded_schema->name);
        crm_log_xml_debug(new_xml, "bad-transform-result");
        pcmk__xml_free(new_xml);
        return NULL;
    }

    crm_info("Schema upgrade from %s to %s succeeded",
             schema->name, upgraded_schema->name);
    return new_xml;
}

/*!
 * \internal
 * \brief Get the schema list entry corresponding to XML configuration
 *
 * \param[in] xml  CIB XML to check
 *
 * \return List entry of schema configured in \p xml
 */
static GList *
get_configured_schema(const xmlNode *xml)
{
    const char *schema_name = crm_element_value(xml, PCMK_XA_VALIDATE_WITH);

    pcmk__warn_if_schema_deprecated(schema_name);
    return pcmk__get_schema(schema_name);
}

/*!
 * \brief Update CIB XML to latest schema that validates it
 *
 * \param[in,out] xml              XML to update (may be freed and replaced
 *                                 after being transformed)
 * \param[in]     max_schema_name  If not NULL, do not update \p xml to any
 *                                 schema later than this one
 * \param[in]     transform        If false, do not update \p xml to any schema
 *                                 that requires an XSL transform
 * \param[in]     to_logs          If false, certain validation errors will be
 *                                 sent to stderr rather than logged
 *
 * \return Standard Pacemaker return code
 */
int
pcmk__update_schema(xmlNode **xml, const char *max_schema_name, bool transform,
                    bool to_logs)
{
    int max_stable_schemas = xml_latest_schema_index();
    int max_schema_index = 0;
    int rc = pcmk_rc_ok;
    GList *entry = NULL;
    pcmk__schema_t *best_schema = NULL;
    pcmk__schema_t *original_schema = NULL;
    xmlRelaxNGValidityErrorFunc error_handler = 
        to_logs ? (xmlRelaxNGValidityErrorFunc) xml_log : NULL;

    CRM_CHECK((xml != NULL) && (*xml != NULL) && ((*xml)->doc != NULL),
              return EINVAL);

    if (max_schema_name != NULL) {
        GList *max_entry = pcmk__get_schema(max_schema_name);

        if (max_entry != NULL) {
            pcmk__schema_t *max_schema = max_entry->data;

            max_schema_index = max_schema->schema_index;
        }
    }
    if ((max_schema_index < 1) || (max_schema_index > max_stable_schemas)) {
        max_schema_index = max_stable_schemas;
    }

    entry = get_configured_schema(*xml);
    if (entry == NULL) {
        return pcmk_rc_cib_corrupt;
    }
    original_schema = entry->data;
    if (original_schema->schema_index >= max_schema_index) {
        return pcmk_rc_ok;
    }

    for (; entry != NULL; entry = entry->next) {
        pcmk__schema_t *current_schema = entry->data;
        xmlNode *upgrade = NULL;

        if (current_schema->schema_index > max_schema_index) {
            break;
        }

        if (!validate_with(*xml, current_schema, error_handler,
                           GUINT_TO_POINTER(LOG_ERR))) {
            crm_debug("Schema %s does not validate", current_schema->name);
            if (best_schema != NULL) {
                /* we've satisfied the validation, no need to check further */
                break;
            }
            rc = pcmk_rc_schema_validation;
            continue; // Try again with the next higher schema
        }

        crm_debug("Schema %s validates", current_schema->name);
        rc = pcmk_rc_ok;
        best_schema = current_schema;
        if (current_schema->schema_index == max_schema_index) {
            break; // No further transformations possible
        }

        // coverity[null_field] The index check ensures entry->next is not NULL
        if (!transform || (current_schema->transforms == NULL)
            || validate_with_silent(*xml, entry->next->data)) {
            /* The next schema either doesn't require a transform or validates
             * successfully even without the transform. Skip the transform and
             * try the next schema with the same XML.
             */
            continue;
        }

        upgrade = apply_upgrade(*xml, current_schema->schema_index, to_logs);
        if (upgrade == NULL) {
            /* The transform failed, so this schema can't be used. Later
             * schemas are unlikely to validate, but try anyway until we
             * run out of options.
             */
            rc = pcmk_rc_transform_failed;
        } else {
            best_schema = current_schema;
            pcmk__xml_free(*xml);
            *xml = upgrade;
        }
    }

    if ((best_schema != NULL)
        && (best_schema->schema_index > original_schema->schema_index)) {
        crm_info("%s the configuration schema to %s",
                 (transform? "Transformed" : "Upgraded"),
                 best_schema->name);
        crm_xml_add(*xml, PCMK_XA_VALIDATE_WITH, best_schema->name);
    }
    return rc;
}

int
pcmk_update_configured_schema(xmlNode **xml)
{
    return pcmk__update_configured_schema(xml, true);
}

/*!
 * \brief Update XML from its configured schema to the latest major series
 *
 * \param[in,out] xml      XML to update
 * \param[in]     to_logs  If false, certain validation errors will be
 *                         sent to stderr rather than logged
 *
 * \return Standard Pacemaker return code
 */
int
pcmk__update_configured_schema(xmlNode **xml, bool to_logs)
{
    pcmk__schema_t *x_0_schema = pcmk__find_x_0_schema()->data;
    pcmk__schema_t *original_schema = NULL;
    GList *entry = NULL;

    if (xml == NULL) {
        return EINVAL;
    }

    entry = get_configured_schema(*xml);
    if (entry == NULL) {
        return pcmk_rc_cib_corrupt;
    }

    original_schema = entry->data;
    if (original_schema->schema_index < x_0_schema->schema_index) {
        // Current configuration schema is not acceptable, try to update
        xmlNode *converted = NULL;
        const char *new_schema_name = NULL;
        pcmk__schema_t *schema = NULL;

        entry = NULL;
        converted = pcmk__xml_copy(NULL, *xml);
        if (pcmk__update_schema(&converted, NULL, true, to_logs) == pcmk_rc_ok) {
            new_schema_name = crm_element_value(converted,
                                                PCMK_XA_VALIDATE_WITH);
            entry = pcmk__get_schema(new_schema_name);
        }
        schema = (entry == NULL)? NULL : entry->data;

        if ((schema == NULL)
            || (schema->schema_index < x_0_schema->schema_index)) {
            // Updated configuration schema is still not acceptable

            if ((schema == NULL)
                || (schema->schema_index < original_schema->schema_index)) {
                // We couldn't validate any schema at all
                if (to_logs) {
                    pcmk__config_err("Cannot upgrade configuration (claiming "
                                     "%s schema) to at least %s because it "
                                     "does not validate with any schema from "
                                     "%s to the latest",
                                     original_schema->name,
                                     x_0_schema->name, original_schema->name);
                } else {
                    fprintf(stderr, "Cannot upgrade configuration (claiming "
                                    "%s schema) to at least %s because it "
                                    "does not validate with any schema from "
                                    "%s to the latest\n",
                                    original_schema->name,
                                    x_0_schema->name, original_schema->name);
                }
            } else {
                // We updated configuration successfully, but still too low
                if (to_logs) {
                    pcmk__config_err("Cannot upgrade configuration (claiming "
                                     "%s schema) to at least %s because it "
                                     "would not upgrade past %s",
                                     original_schema->name, x_0_schema->name,
                                     pcmk__s(new_schema_name, "unspecified version"));
                } else {
                    fprintf(stderr, "Cannot upgrade configuration (claiming "
                                    "%s schema) to at least %s because it "
                                    "would not upgrade past %s\n",
                                    original_schema->name, x_0_schema->name,
                                    pcmk__s(new_schema_name, "unspecified version"));
                }
            }

            pcmk__xml_free(converted);
            converted = NULL;
            return pcmk_rc_transform_failed;

        } else {
            // Updated configuration schema is acceptable
            pcmk__xml_free(*xml);
            *xml = converted;

            if (schema->schema_index < xml_latest_schema_index()) {
                if (to_logs) {
                    pcmk__config_warn("Configuration with %s schema was "
                                      "internally upgraded to acceptable (but "
                                      "not most recent) %s",
                                      original_schema->name, schema->name);
                }
            } else if (to_logs) {
                crm_info("Configuration with %s schema was internally "
                         "upgraded to latest version %s",
                         original_schema->name, schema->name);
            }
        }

    } else if (!to_logs) {
        pcmk__schema_t *none_schema = NULL;

        entry = pcmk__get_schema(PCMK_VALUE_NONE);
        pcmk__assert((entry != NULL) && (entry->data != NULL));

        none_schema = entry->data;
        if (original_schema->schema_index >= none_schema->schema_index) {
            // @COMPAT the none schema is deprecated since 2.1.8
            fprintf(stderr, "Schema validation of configuration is "
                            "disabled (support for " PCMK_XA_VALIDATE_WITH
                            " set to \"" PCMK_VALUE_NONE "\" is deprecated"
                            " and will be removed in a future release)\n");
        }
    }

    return pcmk_rc_ok;
}

/*!
 * \internal
 * \brief Return a list of all schema files and any associated XSLT files
 *        later than the given one
 * \brief Return a list of all schema versions later than the given one
 *
 * \param[in] schema The schema to compare against (for example,
 *                   "pacemaker-3.1.rng" or "pacemaker-3.1")
 *
 * \note The caller is responsible for freeing both the returned list and
 *       the elements of the list
 */
GList *
pcmk__schema_files_later_than(const char *name)
{
    GList *lst = NULL;
    pcmk__schema_version_t ver;

    if (!version_from_filename(name, &ver)) {
        return lst;
    }

    for (GList *iter = g_list_nth(known_schemas, xml_latest_schema_index());
         iter != NULL; iter = iter->prev) {
        pcmk__schema_t *schema = iter->data;

        if (schema_cmp(ver, schema->version) != -1) {
            continue;
        }

        for (GList *iter2 = g_list_last(schema->transforms); iter2 != NULL;
             iter2 = iter2->prev) {

            const struct dirent *entry = iter2->data;

            lst = g_list_prepend(lst, pcmk__str_copy(entry->d_name));
        }

        lst = g_list_prepend(lst, crm_strdup_printf("%s.rng", schema->name));
    }

    return lst;
}

static void
append_href(xmlNode *xml, void *user_data)
{
    GList **list = user_data;
    char *href = crm_element_value_copy(xml, "href");

    if (href == NULL) {
        return;
    }
    *list = g_list_prepend(*list, href);
}

static void
external_refs_in_schema(GList **list, const char *contents)
{
    /* local-name()= is needed to ignore the xmlns= setting at the top of
     * the XML file.  Otherwise, the xpath query will always return nothing.
     */
    const char *search = "//*[local-name()='externalRef'] | //*[local-name()='include']";
    xmlNode *xml = pcmk__xml_parse(contents);

    pcmk__xpath_foreach_result(xml->doc, search, append_href, list);
    pcmk__xml_free(xml);
}

static int
read_file_contents(const char *file, char **contents)
{
    int rc = pcmk_rc_ok;
    char *path = NULL;

    if (pcmk__ends_with(file, ".rng")) {
        path = pcmk__xml_artefact_path(pcmk__xml_artefact_ns_legacy_rng, file);
    } else {
        path = pcmk__xml_artefact_path(pcmk__xml_artefact_ns_legacy_xslt, file);
    }

    rc = pcmk__file_contents(path, contents);

    free(path);
    return rc;
}

static void
add_schema_file_to_xml(xmlNode *parent, const char *file, GList **already_included)
{
    char *contents = NULL;
    char *path = NULL;
    xmlNode *file_node = NULL;
    GList *includes = NULL;
    int rc = pcmk_rc_ok;

    /* If we already included this file, don't do so again. */
    if (g_list_find_custom(*already_included, file, (GCompareFunc) strcmp) != NULL) {
        return;
    }

    /* Ensure whatever file we were given has a suffix we know about.  If not,
     * just assume it's an RNG file.
     */
    if (!pcmk__ends_with(file, ".rng") && !pcmk__ends_with(file, ".xsl")) {
        path = crm_strdup_printf("%s.rng", file);
    } else {
        path = pcmk__str_copy(file);
    }

    rc = read_file_contents(path, &contents);
    if (rc != pcmk_rc_ok || contents == NULL) {
        crm_warn("Could not read schema file %s: %s", file, pcmk_rc_str(rc));
        free(path);
        return;
    }

    /* Create a new <file path="..."> node with the contents of the file
     * as a CDATA block underneath it.
     */
    file_node = pcmk__xe_create(parent, PCMK__XE_FILE);
    crm_xml_add(file_node, PCMK_XA_PATH, path);
    *already_included = g_list_prepend(*already_included, path);

    xmlAddChild(file_node,
                xmlNewCDataBlock(parent->doc, (const xmlChar *) contents,
                                 strlen(contents)));

    /* Scan the file for any <externalRef> or <include> nodes and build up
     * a list of the files they reference.
     */
    external_refs_in_schema(&includes, contents);

    /* For each referenced file, recurse to add it (and potentially anything it
     * references, ...) to the XML.
     */
    for (GList *iter = includes; iter != NULL; iter = iter->next) {
        add_schema_file_to_xml(parent, iter->data, already_included);
    }

    free(contents);
    g_list_free_full(includes, free);
}

/*!
 * \internal
 * \brief Add an XML schema file and all the files it references as children
 *        of a given XML node
 *
 * \param[in,out] parent            The parent XML node
 * \param[in] name                  The schema version to compare against
 *                                  (for example, "pacemaker-3.1" or "pacemaker-3.1.rng")
 * \param[in,out] already_included  A list of names that have already been added
 *                                  to the parent node.
 *
 * \note The caller is responsible for freeing both the returned list and
 *       the elements of the list
 */
void
pcmk__build_schema_xml_node(xmlNode *parent, const char *name, GList **already_included)
{
    xmlNode *schema_node = pcmk__xe_create(parent, PCMK__XA_SCHEMA);

    crm_xml_add(schema_node, PCMK_XA_VERSION, name);
    add_schema_file_to_xml(schema_node, name, already_included);

    if (schema_node->children == NULL) {
        // Not needed if empty. May happen if name was invalid, for example.
        pcmk__xml_free(schema_node);
    }
}

/*!
 * \internal
 * \brief Return the directory containing any extra schema files that a
 *        Pacemaker Remote node fetched from the cluster
 */
const char *
pcmk__remote_schema_dir(void)
{
    const char *dir = pcmk__env_option(PCMK__ENV_REMOTE_SCHEMA_DIRECTORY);

    if (pcmk__str_empty(dir)) {
        return PCMK__REMOTE_SCHEMA_DIR;
    }

    return dir;
}

/*!
 * \internal
 * \brief Warn if a given validation schema is deprecated
 *
 * \param[in] Schema name to check
 */
void
pcmk__warn_if_schema_deprecated(const char *schema)
{
    /* @COMPAT Disabling validation is deprecated since 2.1.8, but
     * resource-agents' ocf-shellfuncs (at least as of 4.15.1) uses it
     */
    if (pcmk__str_eq(schema, PCMK_VALUE_NONE, pcmk__str_none)) {
        pcmk__config_warn("Support for " PCMK_XA_VALIDATE_WITH "='%s' is "
                          "deprecated and will be removed in a future release "
                          "without the possibility of upgrades (manually edit "
                          "to use a supported schema)", schema);
    }
}

// Deprecated functions kept only for backward API compatibility
// LCOV_EXCL_START

#include <crm/common/xml_compat.h>

gboolean
cli_config_update(xmlNode **xml, int *best_version, gboolean to_logs)
{
    int rc = pcmk__update_configured_schema(xml, to_logs);

    if (best_version != NULL) {
        const char *name = crm_element_value(*xml, PCMK_XA_VALIDATE_WITH);

        if (name == NULL) {
            *best_version = -1;
        } else {
            GList *entry = pcmk__get_schema(name);
            pcmk__schema_t *schema = (entry == NULL)? NULL : entry->data;

            *best_version = (schema == NULL)? -1 : schema->schema_index;
        }
    }
    return (rc == pcmk_rc_ok)? TRUE: FALSE;
}

// LCOV_EXCL_STOP
// End deprecated API