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
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
|
/**
* @file parser_xml.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief XML data parser for libyang
*
* Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "context.h"
#include "dict.h"
#include "in_internal.h"
#include "log.h"
#include "ly_common.h"
#include "parser_data.h"
#include "parser_internal.h"
#include "plugins_exts.h"
#include "plugins_internal.h"
#include "schema_compile_node.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_data_internal.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "validation.h"
#include "xml.h"
static LY_ERR lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
struct ly_set *parsed);
void
lyd_xml_ctx_free(struct lyd_ctx *lydctx)
{
struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
lyd_ctx_free(lydctx);
lyxml_ctx_free(ctx->xmlctx);
free(ctx);
}
/**
* @brief Log namespace error.
*
* @param[in] xmlctx XML context
* @param[in] prefix XML prefix.
* @param[in] prefix_len XML prefix length.
* @param[in] attr_name Current XML attribute name.
* @param[in] attr_len Current XML attribute name length.
*/
static void
lydxml_log_namespace_err(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len,
const char *attr_name, size_t attr_len)
{
if (prefix_len && attr_len) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\" at attribute \"%.*s\".",
(int)prefix_len, prefix, (int)attr_len, attr_name);
} else if (prefix_len) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
(int)prefix_len, prefix);
} else {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing XML namespace.");
}
}
/**
* @brief Parse and create XML metadata.
*
* @param[in] lydctx XML data parser context.
* @param[in] sparent Schema node of the parent.
* @param[out] meta List of created metadata instances.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta)
{
LY_ERR ret = LY_SUCCESS;
const struct lyxml_ns *ns;
struct lys_module *mod;
const char *name;
size_t name_len;
LY_ARRAY_COUNT_TYPE u;
ly_bool filter_attrs = 0;
struct lyxml_ctx *xmlctx = lydctx->xmlctx;
*meta = NULL;
LOG_LOCSET(sparent, NULL);
/* check for NETCONF filter unqualified attributes */
if (!strcmp(sparent->module->name, "notifications")) {
/* ancient module that does not even use the extension */
filter_attrs = 1;
} else {
LY_ARRAY_FOR(sparent->exts, u) {
if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
!strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
filter_attrs = 1;
break;
}
}
}
while (xmlctx->status == LYXML_ATTRIBUTE) {
if (!xmlctx->prefix_len) {
/* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
!ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
if (!mod) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
"Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
ret = LY_ENOTFOUND;
goto cleanup;
}
goto create_meta;
}
if (lydctx->parse_opts & LYD_PARSE_STRICT) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
(int)xmlctx->name_len, xmlctx->name);
/* If LYD_VALIDATE_MULTI_ERROR is set, then continue parsing, because otherwise the parser context
* will remain in a bad state, which will cause termination on some assert or undefined behavior.
*/
LY_DPARSER_ERR_GOTO(LY_EVALID, ret = LY_EVALID, lydctx, cleanup);
}
/* skip attr */
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
assert(xmlctx->status == LYXML_ATTR_CONTENT);
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
continue;
}
/* get namespace of the attribute to find its annotation definition */
ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
if (!ns) {
lydxml_log_namespace_err(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
ret = LY_ENOTFOUND;
goto cleanup;
}
/* get the module with metadata definition */
mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
if (!mod) {
if (lydctx->parse_opts & LYD_PARSE_STRICT) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
"Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
(int)xmlctx->name_len, xmlctx->name);
ret = LY_ENOTFOUND;
goto cleanup;
}
/* skip attr */
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
assert(xmlctx->status == LYXML_ATTR_CONTENT);
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
continue;
}
create_meta:
/* remember meta name and get its content */
name = xmlctx->name;
name_len = xmlctx->name_len;
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
assert(xmlctx->status == LYXML_ATTR_CONTENT);
/* create metadata */
ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
LY_CHECK_GOTO(ret, cleanup);
/* next attribute */
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
}
cleanup:
LOG_LOCBACK(1, 0);
if (ret) {
lyd_free_meta_siblings(*meta);
*meta = NULL;
}
return ret;
}
static LY_ERR
lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
{
LY_ERR ret = LY_SUCCESS;
const struct lyxml_ns *ns;
void *val_prefix_data;
LY_VALUE_FORMAT format;
struct lyd_attr *attr2;
const char *name, *prefix;
size_t name_len, prefix_len;
assert(attr);
*attr = NULL;
while (xmlctx->status == LYXML_ATTRIBUTE) {
if (*attr) {
attr2 = *attr;
} else {
attr2 = NULL;
}
/* remember attr prefix, name, and get its content */
prefix = xmlctx->prefix;
prefix_len = xmlctx->prefix_len;
name = xmlctx->name;
name_len = xmlctx->name_len;
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
assert(xmlctx->status == LYXML_ATTR_CONTENT);
/* handle special "xml" attribute prefix */
if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
name = prefix;
name_len += 1 + prefix_len;
prefix = NULL;
prefix_len = 0;
}
/* find namespace of the attribute, if any */
ns = NULL;
if (prefix_len) {
ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
if (!ns) {
lydxml_log_namespace_err(xmlctx, prefix, prefix_len, name, name_len);
ret = LY_EVALID;
goto cleanup;
}
}
/* get value prefixes */
val_prefix_data = NULL;
LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
&xmlctx->ns, &format, &val_prefix_data), cleanup);
/* attr2 is always changed to the created attribute */
ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
LYD_HINT_DATA);
LY_CHECK_GOTO(ret, cleanup);
if (!*attr) {
*attr = attr2;
}
/* next attribute */
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
}
cleanup:
if (ret) {
lyd_free_attr_siblings(xmlctx->ctx, *attr);
*attr = NULL;
}
return ret;
}
static LY_ERR
lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
{
LY_ERR ret = LY_SUCCESS, r;
enum LYXML_PARSER_STATUS next;
struct ly_set key_set = {0};
const struct lysc_node *snode;
uint32_t i, parents_count;
assert(list && (list->nodetype == LYS_LIST));
/* get all keys into a set (keys do not have if-features or anything) */
snode = NULL;
while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
/* remember parent count */
parents_count = xmlctx->elements.count;
while (xmlctx->status == LYXML_ELEMENT) {
/* find key definition */
for (i = 0; i < key_set.count; ++i) {
snode = (const struct lysc_node *)key_set.objs[i];
if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
break;
}
}
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
/* skip attributes */
while (xmlctx->status == LYXML_ATTRIBUTE) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
assert(xmlctx->status == LYXML_ATTR_CONTENT);
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
}
assert(xmlctx->status == LYXML_ELEM_CONTENT);
if (i < key_set.count) {
/* validate the value */
r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
if (!r) {
/* key with a valid value, remove from the set */
ly_set_rm_index(&key_set, i, NULL);
}
}
/* parser next */
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
/* skip any children, resursively */
while (xmlctx->status == LYXML_ELEMENT) {
while (parents_count < xmlctx->elements.count) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
}
assert(xmlctx->status == LYXML_ELEM_CLOSE);
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
}
/* parser next, but do not parse closing element of the list because it would remove its namespaces */
assert(xmlctx->status == LYXML_ELEM_CLOSE);
LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
if (next != LYXML_ELEM_CLOSE) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
}
}
if (key_set.count) {
/* some keys are missing/did not validate */
ret = LY_ENOT;
}
cleanup:
ly_set_erase(&key_set, NULL);
return ret;
}
/**
* @brief Skip an element with all its descendants.
*
* @param[in] xmlctx XML parser context.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_data_skip(struct lyxml_ctx *xmlctx)
{
uint32_t parents_count;
/* remember current number of parents */
parents_count = xmlctx->elements.count;
assert(parents_count);
/* skip after the content */
while (xmlctx->status != LYXML_ELEM_CONTENT) {
LY_CHECK_RET(lyxml_ctx_next(xmlctx));
}
LY_CHECK_RET(lyxml_ctx_next(xmlctx));
/* skip all children elements, recursively, if any */
while (parents_count <= xmlctx->elements.count) {
LY_CHECK_RET(lyxml_ctx_next(xmlctx));
}
/* close element */
assert(xmlctx->status == LYXML_ELEM_CLOSE);
LY_CHECK_RET(lyxml_ctx_next(xmlctx));
return LY_SUCCESS;
}
/**
* @brief Check that the current element can be parsed as a data node.
*
* @param[in] lydctx XML data parser context.
* @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
{
LY_ERR ret = LY_SUCCESS;
struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
/* only checks specific to opaque nodes */
return LY_SUCCESS;
}
if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
/* nothing to check */
return LY_SUCCESS;
}
assert(xmlctx->elements.count);
/* backup parser */
LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
/* skip attributes */
while (xmlctx->status == LYXML_ATTRIBUTE) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
}
if ((*snode)->nodetype & LYD_NODE_TERM) {
/* value may not be valid in which case we parse it as an opaque node */
if (ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA)) {
LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, (int)xmlctx->value_len,
xmlctx->value);
*snode = NULL;
}
} else if ((*snode)->nodetype == LYS_LIST) {
/* skip content */
LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
if (lydxml_check_list(xmlctx, *snode)) {
/* invalid list, parse as opaque if it missing/has invalid some keys */
LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
*snode = NULL;
}
} else {
/* if there is a non-WS value, it cannot be parsed as an inner node */
assert(xmlctx->status == LYXML_ELEM_CONTENT);
if (!xmlctx->ws_only) {
*snode = NULL;
}
}
restore:
/* restore parser */
lyxml_ctx_restore(xmlctx, &pxmlctx);
return ret;
}
/**
* @brief Get sensible data hints for an opaque node.
*
* @param[in] name Node name.
* @param[in] name_len Length of @p name.
* @param[in] value Node value.
* @param[in] value_len Length of @p value.
* @param[in] first Node first sibling.
* @param[in] ns Node module namespace, NULL for no namespace.
* @param[out] hints Data hints to use.
* @param[out] anchor Anchor to insert after in case of a list.
*/
static void
lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first,
const char *ns, uint32_t *hints, struct lyd_node **anchor)
{
struct lyd_node_opaq *opaq;
char *ptr;
/* this needs to be at least 64bit, and it "should not" be an explicit int64_t
* because the code calls strtoll later on, which "might" return a bigger type */
long long num;
*hints = 0;
*anchor = NULL;
if (!value_len) {
/* no value but it may also be zero-length string */
*hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING;
} else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
/* boolean value */
*hints |= LYD_VALHINT_BOOLEAN;
} else {
num = strtoll(value, &ptr, 10);
if ((unsigned)(ptr - value) == value_len) {
/* number value */
*hints |= LYD_VALHINT_DECNUM;
if ((num < INT32_MIN) || (num > UINT32_MAX)) {
/* large number */
*hints |= LYD_VALHINT_NUM64;
}
} else {
/* string value */
*hints |= LYD_VALHINT_STRING;
}
}
if (!first) {
return;
}
/* search backwards to find the last instance */
do {
first = first->prev;
if (first->schema) {
continue;
}
opaq = (struct lyd_node_opaq *)first;
assert(opaq->format == LY_VALUE_XML);
if (!ly_strncmp(opaq->name.name, name, name_len) &&
((ns && opaq->name.module_ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
if (opaq->value && opaq->value[0]) {
/* leaf-list nodes */
opaq->hints |= LYD_NODEHINT_LEAFLIST;
*hints |= LYD_NODEHINT_LEAFLIST;
} else {
/* list nodes */
opaq->hints |= LYD_NODEHINT_LIST;
*hints |= LYD_NODEHINT_LIST;
}
*anchor = (struct lyd_node *)first;
break;
}
} while (first->prev->next);
}
/**
* @brief Get schema node for the current element.
*
* @param[in] lydctx XML data parser context.
* @param[in] parent Parsed parent data node, if any.
* @param[in] prefix Element prefix, if any.
* @param[in] prefix_len Length of @p prefix.
* @param[in] name Element name.
* @param[in] name_len Length of @p name.
* @param[out] snode Found schema node, NULL if no suitable was found.
* @param[out] ext Extension instance that provided @p snode, if any.
* @return LY_SUCCESS on success;
* @return LY_ERR on error.
*/
static LY_ERR
lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len,
const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
{
LY_ERR r;
struct lyxml_ctx *xmlctx;
const struct ly_ctx *ctx;
const struct lyxml_ns *ns;
struct lys_module *mod;
uint32_t getnext_opts;
xmlctx = lydctx->xmlctx;
ctx = xmlctx->ctx;
getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
*snode = NULL;
*ext = NULL;
/* get current namespace */
ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
if (!ns) {
if (lydctx->int_opts & LYD_INTOPT_ANY) {
goto unknown_module;
}
lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
return LY_EVALID;
}
/* get the element module, use parent context if possible because of extensions */
mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
if (!mod) {
/* check for extension data */
r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
snode, ext);
if (r != LY_ENOT) {
/* success or error */
return r;
}
unknown_module:
if (lydctx->parse_opts & LYD_PARSE_STRICT) {
if (ns) {
LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
} else if (prefix_len) {
LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix);
} else {
LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context.");
}
return LY_EVALID;
}
return LY_SUCCESS;
}
/* get the schema node */
if (!parent && lydctx->ext) {
*snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
} else {
/* try to find parent schema node even if it is an opaque node (not connected to the parent) */
*snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts);
}
if (!*snode) {
/* check for extension data */
r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
name_len, snode, ext);
if (r != LY_ENOT) {
/* success or error */
return r;
}
/* unknown data node */
if (lydctx->parse_opts & LYD_PARSE_STRICT) {
if (parent) {
LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
(int)name_len, name, LYD_NAME(parent));
} else if (lydctx->ext) {
if (lydctx->ext->argument) {
LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
(int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
} else {
LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
(int)name_len, name, lydctx->ext->def->name);
}
} else {
LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
(int)name_len, name, mod->name);
}
return LY_EVALID;
}
return LY_SUCCESS;
} else {
/* check that schema node is valid and can be used */
LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
}
return LY_SUCCESS;
}
/**
* @brief Parse an XML opque node.
*
* @param[in] lydctx XML YANG data parser context.
* @param[in] sibling Existing sibling node, if any.
* @param[in] prefix Parsed node prefix.
* @param[in] prefix_len Length of @p prefix.
* @param[in] name Parsed node name.
* @param[in] name_len Length of @p name.
* @param[out] insert_anchor Optional anchor node for inserting this node.
* @param[out] node Created node.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
{
LY_ERR rc = LY_SUCCESS;
struct lyxml_ctx *xmlctx = lydctx->xmlctx;
struct lyd_node_opaq *opaq;
const char *ns_uri, *value = NULL;
size_t value_len;
ly_bool ws_only, dynamic = 0;
const struct lyxml_ns *ns;
uint32_t hints;
void *val_prefix_data = NULL;
LY_VALUE_FORMAT format;
assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
*node = NULL;
/* remember the value */
value = xmlctx->value;
value_len = xmlctx->value_len;
ws_only = xmlctx->ws_only;
dynamic = xmlctx->dynamic;
if (dynamic) {
xmlctx->dynamic = 0;
}
/* get value prefixes, if any */
rc = ly_store_prefix_data(xmlctx->ctx, value, value_len, LY_VALUE_XML, &xmlctx->ns, &format, &val_prefix_data);
LY_CHECK_GOTO(rc, cleanup);
/* get NS again, it may have been backed up and restored */
ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
ns_uri = ns ? ns->uri : NULL;
/* get best-effort node hints */
lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
/* create the node without value */
rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0,
NULL, format, NULL, hints, node);
LY_CHECK_GOTO(rc, cleanup);
assert(*node);
LOG_LOCSET(NULL, *node);
/* parser next */
rc = lyxml_ctx_next(xmlctx);
LY_CHECK_GOTO(rc, cleanup);
/* process children */
while (xmlctx->status == LYXML_ELEMENT) {
rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
LY_CHECK_GOTO(rc, cleanup);
}
/* update the value */
opaq = (struct lyd_node_opaq *)*node;
if (opaq->child) {
if (!ws_only) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq));
rc = LY_EVALID;
goto cleanup;
}
} else if (value_len) {
lydict_remove(xmlctx->ctx, opaq->value);
if (dynamic) {
LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
dynamic = 0;
} else {
LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
}
}
/* always store val_prefix_data because the format requires them */
assert(!opaq->val_prefix_data);
opaq->val_prefix_data = val_prefix_data;
val_prefix_data = NULL;
cleanup:
if (*node) {
LOG_LOCBACK(0, 1);
}
ly_free_prefix_data(format, val_prefix_data);
if (dynamic) {
free((char *)value);
}
if (rc) {
lyd_free_tree(*node);
*node = NULL;
}
return rc;
}
/**
* @brief Parse an XML leaf/leaf-list node.
*
* @param[in] lydctx XML YANG data parser context.
* @param[in] parent Parent node, if any.
* @param[in] snode Schema node of the new node.
* @param[out] node Created node.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
struct lyd_node **node)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyxml_ctx *xmlctx = lydctx->xmlctx;
struct lyd_node *anchor;
*node = NULL;
/* create node */
r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
if (*node) {
LOG_LOCSET(NULL, *node);
}
if (*node && parent && (snode->flags & LYS_KEY)) {
/* check the key order, the anchor must never be a key */
anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
if (lydctx->parse_opts & LYD_PARSE_STRICT) {
LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
} else {
LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
}
}
}
/* parser next */
r = lyxml_ctx_next(xmlctx);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
/* no children expected */
if (xmlctx->status == LYXML_ELEMENT) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
(int)xmlctx->name_len, xmlctx->name, snode->name);
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
cleanup:
if (*node) {
LOG_LOCBACK(0, 1);
}
if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_free_tree(*node);
*node = NULL;
}
return rc;
}
/**
* @brief Parse an XML inner node.
*
* @param[in] lydctx XML YANG data parser context.
* @param[in] snode Schema node of the new node.
* @param[in] ext Extension instance of @p snode, if any.
* @param[out] node Created node.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
struct lyd_node **node)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyxml_ctx *xmlctx = lydctx->xmlctx;
uint32_t prev_parse_opts = lydctx->parse_opts;
*node = NULL;
if (!xmlctx->ws_only) {
/* value in inner node */
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
(int)xmlctx->value_len, xmlctx->value, snode->name);
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
/* create node */
rc = lyd_create_inner(snode, node);
LY_CHECK_GOTO(rc, cleanup);
assert(*node);
LOG_LOCSET(NULL, *node);
/* parser next */
rc = lyxml_ctx_next(xmlctx);
LY_CHECK_GOTO(rc, cleanup);
if (ext) {
/* only parse these extension data and validate afterwards */
lydctx->parse_opts |= LYD_PARSE_ONLY;
}
/* process children */
while (xmlctx->status == LYXML_ELEMENT) {
r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
/* restore options */
lydctx->parse_opts = prev_parse_opts;
if (snode->nodetype == LYS_LIST) {
/* check all keys exist */
r = lyd_parse_check_keys(*node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
/* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */
r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
/* add any missing default children */
r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
&lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
/* rememeber the RPC/action/notification */
lydctx->op_node = *node;
}
cleanup:
if (*node) {
LOG_LOCBACK(0, 1);
}
lydctx->parse_opts = prev_parse_opts;
if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
/* list without keys is unusable or an error */
lyd_free_tree(*node);
*node = NULL;
}
return rc;
}
/**
* @brief Parse an XML anyxml/anydata node.
*
* @param[in] lydctx XML YANG data parser context.
* @param[in] snode Schema node of the new node.
* @param[in] ext Extension instance of @p snode, if any.
* @param[out] node Created node.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
struct lyd_node **node)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyxml_ctx *xmlctx = lydctx->xmlctx;
uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
struct lyd_node *child = NULL;
char *val = NULL;
ly_bool log_node = 0;
*node = NULL;
if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
/* value in anydata node, we expect a tree */
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name);
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
if (!xmlctx->ws_only) {
/* use an arbitrary text value for anyxml */
val = strndup(xmlctx->value, xmlctx->value_len);
LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
/* parser next */
r = lyxml_ctx_next(xmlctx);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
/* create node */
r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
val = NULL;
} else {
/* create node */
r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
assert(*node);
LOG_LOCSET(NULL, *node);
log_node = 1;
/* parser next */
r = lyxml_ctx_next(xmlctx);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
/* update options so that generic data can be parsed */
lydctx->parse_opts &= ~LYD_PARSE_STRICT;
lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
/* parse any data tree */
while (xmlctx->status == LYXML_ELEMENT) {
r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
/* assign the data tree */
((struct lyd_node_any *)*node)->value.tree = child;
child = NULL;
}
cleanup:
if (log_node) {
LOG_LOCBACK(0, 1);
}
lydctx->parse_opts = prev_parse_opts;
lydctx->int_opts = prev_int_opts;
free(val);
lyd_free_tree(child);
if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_free_tree(*node);
*node = NULL;
}
return rc;
}
/**
* @brief Parse an XML subtree, recursively.
*
* @param[in] lydctx XML YANG data parser context.
* @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
* @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
* @param[in,out] parsed Optional set to add all the parsed siblings into.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
{
LY_ERR r, rc = LY_SUCCESS;
const char *prefix, *name;
size_t prefix_len, name_len;
struct lyxml_ctx *xmlctx;
const struct ly_ctx *ctx;
struct lyd_meta *meta = NULL;
struct lyd_attr *attr = NULL;
const struct lysc_node *snode = NULL;
struct lysc_ext_instance *ext = NULL;
uint32_t orig_parse_opts;
struct lyd_node *node = NULL, *insert_anchor = NULL;
ly_bool parse_subtree;
assert(parent || first_p);
xmlctx = lydctx->xmlctx;
ctx = xmlctx->ctx;
parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
/* all descendants should be parsed */
lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
orig_parse_opts = lydctx->parse_opts;
assert(xmlctx->status == LYXML_ELEMENT);
/* remember element prefix and name */
prefix = xmlctx->prefix;
prefix_len = xmlctx->prefix_len;
name = xmlctx->name;
name_len = xmlctx->name_len;
/* parser next */
rc = lyxml_ctx_next(xmlctx);
LY_CHECK_GOTO(rc, cleanup);
if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len &&
!ly_strncmp("eventTime", name, name_len)) {
/* parse eventTime, create node */
assert(xmlctx->status == LYXML_ELEM_CONTENT);
rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len,
"urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value,
xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node);
LY_CHECK_GOTO(rc, cleanup);
/* validate the value */
r = lyd_parser_notif_eventtime_validate(node);
LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
/* parser next */
r = lyxml_ctx_next(xmlctx);
LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
if (xmlctx->status != LYXML_ELEM_CLOSE) {
LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children.");
rc = LY_EVALID;
lyd_free_tree(node);
goto cleanup;
}
goto node_parsed;
}
/* get the schema node */
r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
if (r) {
rc = r;
if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
/* skip the invalid data */
if ((r = lydxml_data_skip(xmlctx))) {
rc = r;
}
}
goto cleanup;
} else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name);
/* skip element with children */
rc = lydxml_data_skip(xmlctx);
goto cleanup;
}
/* create metadata/attributes */
if (xmlctx->status == LYXML_ATTRIBUTE) {
if (snode) {
r = lydxml_metadata(lydctx, snode, &meta);
} else {
assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
r = lydxml_attrs(xmlctx, &attr);
}
}
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
assert(xmlctx->status == LYXML_ELEM_CONTENT);
if (!snode) {
/* opaque */
r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
&insert_anchor, &node);
} else if (snode->nodetype & LYD_NODE_TERM) {
/* term */
r = lydxml_subtree_term(lydctx, parent, snode, &node);
} else if (snode->nodetype & LYD_NODE_INNER) {
/* inner */
r = lydxml_subtree_inner(lydctx, snode, ext, &node);
} else {
/* any */
assert(snode->nodetype & LYD_NODE_ANY);
r = lydxml_subtree_any(lydctx, snode, ext, &node);
}
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
node_parsed:
if (node && snode) {
/* add/correct flags */
r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
/* store for ext instance node validation, if needed */
r = lyd_validate_node_ext(node, &lydctx->ext_node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
}
/* parser next */
assert(xmlctx->status == LYXML_ELEM_CLOSE);
if (!parse_subtree) {
r = lyxml_ctx_next(xmlctx);
LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
}
LY_CHECK_GOTO(!node, cleanup);
/* add metadata/attributes */
if (snode) {
lyd_insert_meta(node, meta, 0);
meta = NULL;
} else {
lyd_insert_attr(node, attr);
attr = NULL;
}
/* insert, keep first pointer correct */
if (insert_anchor) {
lyd_insert_after(insert_anchor, node);
} else if (ext) {
r = lyplg_ext_insert(parent, node);
LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
} else {
lyd_insert_node(parent, first_p, node,
lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT);
}
while (!parent && (*first_p)->prev->next) {
*first_p = (*first_p)->prev;
}
/* rememeber a successfully parsed node */
if (parsed) {
ly_set_add(parsed, node, 1, NULL);
}
cleanup:
lydctx->parse_opts = orig_parse_opts;
lyd_free_meta_siblings(meta);
lyd_free_attr_siblings(ctx, attr);
return rc;
}
/**
* @brief Parse a specific XML element into an opaque node.
*
* @param[in] xmlctx XML parser context.
* @param[in] name Name of the element.
* @param[in] uri URI of the element.
* @param[in] value Whether a value is expected in the element.
* @param[out] evnp Parsed envelope (opaque node).
* @return LY_SUCCESS on success.
* @return LY_ENOT if the specified element did not match.
* @return LY_ERR value on error.
*/
static LY_ERR
lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp)
{
LY_ERR rc = LY_SUCCESS;
const struct lyxml_ns *ns;
struct lyd_attr *attr = NULL;
const char *prefix;
size_t prefix_len;
if (xmlctx->status != LYXML_ELEMENT) {
/* nothing to parse */
return LY_ENOT;
}
if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
/* not the expected element */
return LY_ENOT;
}
prefix = xmlctx->prefix;
prefix_len = xmlctx->prefix_len;
ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
if (!ns) {
lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
return LY_EVALID;
} else if (strcmp(ns->uri, uri)) {
/* different namespace */
return LY_ENOT;
}
LY_CHECK_RET(lyxml_ctx_next(xmlctx));
/* create attributes */
if (xmlctx->status == LYXML_ATTRIBUTE) {
LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
}
assert(xmlctx->status == LYXML_ELEM_CONTENT);
if (!value && !xmlctx->ws_only) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
(int)xmlctx->value_len, xmlctx->value, name);
rc = LY_EVALID;
goto cleanup;
}
/* create node */
rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
LY_CHECK_GOTO(rc, cleanup);
/* assign atributes */
((struct lyd_node_opaq *)(*envp))->attr = attr;
attr = NULL;
/* parser next element */
LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
cleanup:
lyd_free_attr_siblings(xmlctx->ctx, attr);
if (rc) {
lyd_free_tree(*envp);
*envp = NULL;
}
return rc;
}
LY_ERR
lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyd_xml_ctx *lydctx;
ly_bool parsed_data_nodes = 0, close_elem = 0;
struct lyd_node *act = NULL;
enum LYXML_PARSER_STATUS status;
assert(ctx && in && lydctx_p);
assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
/* init context */
lydctx = calloc(1, sizeof *lydctx);
LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
lydctx->parse_opts = parse_opts;
lydctx->val_opts = val_opts;
lydctx->int_opts = int_opts;
lydctx->free = lyd_xml_ctx_free;
lydctx->ext = ext;
/* find the operation node if it exists already */
LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
/* can be either, try to parse "action" */
if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
close_elem = 1;
int_opts &= ~LYD_INTOPT_RPC;
}
}
/* parse XML data */
while (lydctx->xmlctx->status == LYXML_ELEMENT) {
r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
parsed_data_nodes = 1;
if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
break;
}
}
/* close an opened element */
if (close_elem) {
if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
assert(lydctx->xmlctx->status == LYXML_ELEMENT);
LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
(int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
rc = LY_EVALID;
goto cleanup;
}
LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
}
/* check final state */
if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
if (!parsed_data_nodes) {
/* no data nodes were parsed */
lydctx->op_node = NULL;
}
if (parse_opts & LYD_PARSE_SUBTREE) {
/* check for a sibling element */
assert(subtree_sibling);
if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
*subtree_sibling = 1;
} else {
*subtree_sibling = 0;
}
}
cleanup:
/* there should be no unres stored if validation should be skipped */
assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
!lydctx->node_when.count));
lyd_free_tree(act);
if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
} else {
*lydctx_p = (struct lyd_ctx *)lydctx;
/* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
lyxml_ctx_free(lydctx->xmlctx);
lydctx->xmlctx = NULL;
}
return rc;
}
/**
* @brief Parse all expected non-data XML elements of a NETCONF rpc message.
*
* @param[in] xmlctx XML parser context.
* @param[out] evnp Parsed envelope(s) (opaque node).
* @param[out] int_opts Internal options for parsing the rest of YANG data.
* @param[out] close_elem Number of parsed opened elements that need to be closed.
* @return LY_SUCCESS on success.
* @return LY_ERR value on error.
*/
static LY_ERR
lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
{
LY_ERR rc = LY_SUCCESS, r;
struct lyd_node *child;
assert(envp && !*envp);
/* parse "rpc" */
r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
/* parse "action", if any */
r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
if (r == LY_SUCCESS) {
/* insert */
lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT);
/* NETCONF action */
*int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
*close_elem = 2;
} else if (r == LY_ENOT) {
/* NETCONF RPC */
*int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
*close_elem = 1;
} else {
rc = r;
goto cleanup;
}
cleanup:
if (rc) {
lyd_free_tree(*envp);
*envp = NULL;
}
return rc;
}
/**
* @brief Parse an XML element as an opaque node subtree.
*
* @param[in] xmlctx XML parser context.
* @param[in] parent Parent to append nodes to.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
{
LY_ERR rc = LY_SUCCESS;
const struct lyxml_ns *ns;
struct lyd_attr *attr = NULL;
struct lyd_node *node = NULL;
struct lyd_node_opaq *opaq;
const char *name, *prefix, *value = NULL;
size_t name_len, prefix_len, value_len;
ly_bool ws_only, dynamic = 0;
assert(xmlctx->status == LYXML_ELEMENT);
name = xmlctx->name;
name_len = xmlctx->name_len;
prefix = xmlctx->prefix;
prefix_len = xmlctx->prefix_len;
ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
if (!ns) {
lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
return LY_EVALID;
}
LY_CHECK_RET(lyxml_ctx_next(xmlctx));
/* create attributes */
if (xmlctx->status == LYXML_ATTRIBUTE) {
LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
}
/* remember the value */
assert(xmlctx->status == LYXML_ELEM_CONTENT);
value = xmlctx->value;
value_len = xmlctx->value_len;
ws_only = xmlctx->ws_only;
dynamic = xmlctx->dynamic;
if (dynamic) {
xmlctx->dynamic = 0;
}
/* create the node without value */
rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL,
LY_VALUE_XML, NULL, 0, &node);
LY_CHECK_GOTO(rc, cleanup);
/* assign atributes */
((struct lyd_node_opaq *)node)->attr = attr;
attr = NULL;
/* parser next element */
LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
/* parse all the descendants */
while (xmlctx->status == LYXML_ELEMENT) {
rc = lydxml_opaq_r(xmlctx, node);
LY_CHECK_GOTO(rc, cleanup);
}
/* insert */
lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_LAST);
/* update the value */
opaq = (struct lyd_node_opaq *)node;
if (opaq->child) {
if (!ws_only) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node));
rc = LY_EVALID;
goto cleanup;
}
} else if (value_len) {
lydict_remove(xmlctx->ctx, opaq->value);
if (dynamic) {
LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
dynamic = 0;
} else {
LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
}
}
cleanup:
lyd_free_attr_siblings(xmlctx->ctx, attr);
if (dynamic) {
free((char *)value);
}
if (rc) {
lyd_free_tree(node);
}
return rc;
}
/**
* @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
*
* @param[in] xmlctx XML parser context.
* @param[in] parent Parent to append nodes to.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
{
LY_ERR r;
struct lyd_node *child, *iter;
ly_bool no_dup;
/* there must be some child */
if (xmlctx->status == LYXML_ELEM_CLOSE) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
return LY_EVALID;
}
while (xmlctx->status == LYXML_ELEMENT) {
child = NULL;
/*
* session-id
*/
r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* bad-attribute
*/
r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* bad-element
*/
r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* bad-namespace
*/
r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
if (r == LY_ENOT) {
assert(xmlctx->status == LYXML_ELEMENT);
/* custom elements, parse all the siblings */
while (xmlctx->status == LYXML_ELEMENT) {
LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
}
continue;
}
check_child:
/* check for duplicates */
if (no_dup) {
LY_LIST_FOR(lyd_child(parent), iter) {
if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
(((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
((struct lyd_node_opaq *)child)->name.name);
r = LY_EVALID;
goto error;
}
}
}
/* finish child parsing */
if (xmlctx->status != LYXML_ELEM_CLOSE) {
assert(xmlctx->status == LYXML_ELEMENT);
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
(int)xmlctx->name_len, xmlctx->name);
r = LY_EVALID;
goto error;
}
LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
/* insert */
lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
}
return LY_SUCCESS;
error:
lyd_free_tree(child);
return r;
}
/**
* @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
*
* @param[in] xmlctx XML parser context.
* @param[in] parent Parent to append nodes to.
* @return LY_ERR value.
*/
static LY_ERR
lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
{
LY_ERR r;
struct lyd_node *child, *iter;
const char *val;
ly_bool no_dup;
/* there must be some child */
if (xmlctx->status == LYXML_ELEM_CLOSE) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
return LY_EVALID;
}
while (xmlctx->status == LYXML_ELEMENT) {
child = NULL;
/*
* error-type
*/
r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
val = ((struct lyd_node_opaq *)child)->value;
if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
((struct lyd_node_opaq *)child)->name.name);
r = LY_EVALID;
goto error;
}
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* error-tag
*/
r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
val = ((struct lyd_node_opaq *)child)->value;
if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
((struct lyd_node_opaq *)child)->name.name);
r = LY_EVALID;
goto error;
}
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* error-severity
*/
r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
val = ((struct lyd_node_opaq *)child)->value;
if (strcmp(val, "error") && strcmp(val, "warning")) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
((struct lyd_node_opaq *)child)->name.name);
r = LY_EVALID;
goto error;
}
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* error-app-tag
*/
r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* error-path
*/
r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/*
* error-message
*/
r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
if (r == LY_SUCCESS) {
no_dup = 1;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
/* error-info */
r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
if (r == LY_SUCCESS) {
/* parse all the descendants */
LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
no_dup = 0;
goto check_child;
} else if (r != LY_ENOT) {
goto error;
}
if (r == LY_ENOT) {
assert(xmlctx->status == LYXML_ELEMENT);
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
(int)xmlctx->name_len, xmlctx->name);
r = LY_EVALID;
goto error;
}
check_child:
/* check for duplicates */
if (no_dup) {
LY_LIST_FOR(lyd_child(parent), iter) {
if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
(((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
((struct lyd_node_opaq *)child)->name.name);
r = LY_EVALID;
goto error;
}
}
}
/* finish child parsing */
if (xmlctx->status != LYXML_ELEM_CLOSE) {
assert(xmlctx->status == LYXML_ELEMENT);
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
(int)xmlctx->name_len, xmlctx->name);
r = LY_EVALID;
goto error;
}
LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
/* insert */
lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
}
return LY_SUCCESS;
error:
lyd_free_tree(child);
return r;
}
/**
* @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
*
* @param[in] xmlctx XML parser context.
* @param[out] evnp Parsed envelope(s) (opaque node).
* @param[out] int_opts Internal options for parsing the rest of YANG data.
* @param[out] close_elem Number of parsed opened elements that need to be closed.
* @return LY_SUCCESS on success.
* @return LY_ERR value on error.
*/
static LY_ERR
lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
{
LY_ERR rc = LY_SUCCESS, r;
struct lyd_node *child = NULL;
const char *parsed_elem = NULL;
assert(envp && !*envp);
/* parse "rpc-reply" */
r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
/* there must be some child */
if (xmlctx->status == LYXML_ELEM_CLOSE) {
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
rc = LY_EVALID;
goto cleanup;
}
/* try to parse "ok" */
r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
if (r == LY_SUCCESS) {
/* insert */
lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
/* finish child parsing */
if (xmlctx->status != LYXML_ELEM_CLOSE) {
assert(xmlctx->status == LYXML_ELEMENT);
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
(int)xmlctx->name_len, xmlctx->name);
rc = LY_EVALID;
goto cleanup;
}
LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
/* success */
parsed_elem = "ok";
goto finish;
} else if (r != LY_ENOT) {
rc = r;
goto cleanup;
}
/* try to parse all "rpc-error" elements */
while (xmlctx->status == LYXML_ELEMENT) {
r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
if (r == LY_ENOT) {
break;
} else if (r) {
rc = r;
goto cleanup;
}
/* insert */
lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
/* parse all children of "rpc-error" */
LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
/* finish child parsing */
assert(xmlctx->status == LYXML_ELEM_CLOSE);
LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
parsed_elem = "rpc-error";
}
finish:
if (parsed_elem) {
/* NETCONF rpc-reply with no data */
if (xmlctx->status != LYXML_ELEM_CLOSE) {
assert(xmlctx->status == LYXML_ELEMENT);
LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
(int)xmlctx->name_len, xmlctx->name, parsed_elem);
rc = LY_EVALID;
goto cleanup;
}
}
/* NETCONF rpc-reply */
*int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
*close_elem = 1;
cleanup:
if (rc) {
lyd_free_tree(*envp);
*envp = NULL;
}
return rc;
}
LY_ERR
lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
{
LY_ERR rc = LY_SUCCESS;
struct lyd_xml_ctx *lydctx;
struct lyd_node *node;
uint32_t i, int_opts = 0, close_elem = 0;
ly_bool parsed_data_nodes = 0;
assert(ctx && in && lydctx_p);
assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
assert(!(parse_opts & LYD_PARSE_SUBTREE));
/* init context */
lydctx = calloc(1, sizeof *lydctx);
LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
lydctx->parse_opts = parse_opts;
lydctx->val_opts = val_opts;
lydctx->free = lyd_xml_ctx_free;
lydctx->ext = ext;
switch (data_type) {
case LYD_TYPE_RPC_NETCONF:
assert(!parent);
rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
if (rc == LY_ENOT) {
LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
}
LY_CHECK_GOTO(rc, cleanup);
break;
case LYD_TYPE_NOTIF_NETCONF:
assert(!parent);
/* parse "notification" */
rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
if (rc == LY_ENOT) {
LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
}
LY_CHECK_GOTO(rc, cleanup);
/* NETCONF notification */
int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
close_elem = 1;
break;
case LYD_TYPE_REPLY_NETCONF:
assert(parent);
rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
if (rc == LY_ENOT) {
LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
}
LY_CHECK_GOTO(rc, cleanup);
break;
case LYD_TYPE_RPC_RESTCONF:
assert(parent);
/* parse "input" */
rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp);
if (rc == LY_ENOT) {
LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
}
LY_CHECK_GOTO(rc, cleanup);
int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
close_elem = 1;
break;
case LYD_TYPE_REPLY_RESTCONF:
assert(parent);
/* parse "output" */
rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp);
if (rc == LY_ENOT) {
LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
}
LY_CHECK_GOTO(rc, cleanup);
int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
close_elem = 1;
break;
default:
LOGINT(ctx);
rc = LY_EINT;
goto cleanup;
}
lydctx->int_opts = int_opts;
/* find the operation node if it exists already */
LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
/* parse XML data */
while (lydctx->xmlctx->status == LYXML_ELEMENT) {
LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
parsed_data_nodes = 1;
if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
break;
}
}
/* close all opened elements */
for (i = 0; i < close_elem; ++i) {
if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
assert(lydctx->xmlctx->status == LYXML_ELEMENT);
LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
(int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
rc = LY_EVALID;
goto cleanup;
}
LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
}
/* check final state */
if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
rc = LY_EVALID;
goto cleanup;
}
if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
rc = LY_EVALID;
goto cleanup;
}
if (int_opts & LYD_INTOPT_EVENTTIME) {
/* parse as a child of the envelope */
node = (*first_p)->prev;
if (node->schema) {
LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node.");
rc = LY_EVALID;
goto cleanup;
} else {
/* can be the only opaque node and an operation had to be parsed */
assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
lyd_unlink(node);
assert(*envp);
lyd_insert_child(*envp, node);
}
}
if (!parsed_data_nodes) {
/* no data nodes were parsed */
lydctx->op_node = NULL;
}
cleanup:
/* there should be no unres stored if validation should be skipped */
assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
!lydctx->node_when.count));
if (rc) {
lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
} else {
*lydctx_p = (struct lyd_ctx *)lydctx;
/* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
lyxml_ctx_free(lydctx->xmlctx);
lydctx->xmlctx = NULL;
}
return rc;
}
|