-
Notifications
You must be signed in to change notification settings - Fork 0
/
clixon_util_xml.c
373 lines (349 loc) · 11.3 KB
/
clixon_util_xml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
Copyright (C) 2017-2019 Olof Hagsand
Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgate)
This file is part of CLIXON.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 3 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2,
indicate your decision by deleting the provisions above and replace them with
the notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
* XML support functions.
* @see https://www.w3.org/TR/2008/REC-xml-20081126
* https://www.w3.org/TR/2009/REC-xml-names-20091208
* The function can do yang validation, process xml and json, etc.
* On success, nothing is printed and exitcode 0
* On failure, an error is printed on stderr and exitcode != 0
*/
#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <syslog.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/stat.h>
/* cligen */
#include <cligen/cligen.h>
/* clixon */
#include "clixon/clixon.h"
/* Command line options passed to getopt(3) */
#define UTIL_XML_OPTS "hD:f:JjXl:pvoy:Y:t:T:u"
static int
validate_tree(clixon_handle h,
cxobj *xt,
yang_stmt *yspec)
{
int retval = -1;
int ret;
cxobj *xerr = NULL; /* malloced must be freed */
cbuf *cbret = NULL;
/* should already be populated */
/* Add default values */
if (xml_default_recurse(xt, 0, 0) < 0)
goto done;
if (xml_apply(xt, -1, xml_sort_verify, h) < 0)
clixon_log(h, LOG_NOTICE, "%s: sort verify failed", __FUNCTION__);
if ((ret = xml_yang_validate_all_top(h, xt, &xerr)) < 0)
goto done;
if (ret > 0 && (ret = xml_yang_validate_add(h, xt, &xerr)) < 0)
goto done;
if (ret == 0){
if ((cbret = cbuf_new()) ==NULL){
clixon_err(OE_XML, errno, "cbuf_new");
goto done;
}
if (netconf_err2cb(h, xerr, cbret) < 0)
goto done;
fprintf(stderr, "xml validation error: %s\n", cbuf_get(cbret));
goto done;
}
retval = 0;
done:
if (cbret)
cbuf_free(cbret);
if (xerr)
xml_free(xerr);
return retval;
}
static int
usage(char *argv0)
{
fprintf(stderr, "usage:%s [options] with xml on stdin (unless -f)\n"
"where options are\n"
"\t-h \t\tHelp\n"
"\t-D <level> \tDebug\n"
"\t-f <file>\tXML input file (overrides stdin)\n"
"\t-J \t\tInput as JSON\n"
"\t-j \t\tOutput as JSON\n"
"\t-X \t\tOutput as TEXT \n"
"\t-l <s|e|o> \tLog on (s)yslog, std(e)rr, std(o)ut (stderr is default)\n"
"\t-o \t\tOutput the file\n"
"\t-v \t\tValidate the result in terms of Yang model (requires -y)\n"
"\t-p \t\tPretty-print output\n"
"\t-y <filename> \tYang filename or dir (load all files)\n"
"\t-Y <dir> \tYang dirs (can be several)\n"
"\t-t <file>\tXML top input file (where base tree is pasted to)\n"
"\t-T <path>\tXPath to where in top input file base should be pasted\n"
"\t-u \t\tTreat unknown XML as anydata\n"
,
argv0);
exit(0);
}
int
main(int argc,
char **argv)
{
int retval = -1;
int ret;
cxobj *xt = NULL; /* Base cxobj tree parsed from xml or json */
cbuf *cb = cbuf_new();
int c;
int logdst = CLIXON_LOG_STDERR;
int jsonin = 0;
int jsonout = 0;
int textout = 0;
char *input_filename = NULL;
char *top_input_filename = NULL;
char *yang_file_dir = NULL;
yang_stmt *yspec = NULL;
cxobj *xerr = NULL; /* malloced must be freed */
int pretty = 0;
int validate = 0;
int output = 0;
clixon_handle h;
struct stat st;
FILE *fp = stdin; /* base file, stdin */
FILE *tfp = NULL; /* top file */
cxobj *xcfg = NULL;
cbuf *cbret = NULL;
cxobj *xtop = NULL; /* Top tree if any */
char *top_path = NULL;
cxobj *xbot; /* Place in xtop where base cxobj is parsed */
cvec *nsc = NULL;
yang_bind yb;
int dbg = 0;
/* Initialize clixon handle */
if ((h = clixon_handle_init()) == NULL)
goto done;
/* In the startup, logs to stderr & debug flag set later */
clixon_log_init(h, __FILE__, LOG_INFO, CLIXON_LOG_STDERR);
if ((xcfg = xml_new("clixon-config", NULL, CX_ELMNT)) == NULL)
goto done;
if (clicon_conf_xml_set(h, xcfg) < 0)
goto done;
optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, UTIL_XML_OPTS)) != -1)
switch (c) {
case 'h':
usage(argv[0]);
break;
case 'D':
if (sscanf(optarg, "%d", &dbg) != 1)
usage(argv[0]);
break;
case 'f':
input_filename = optarg;
break;
case 'J':
jsonin++;
break;
case 'j':
jsonout++;
break;
case 'X':
textout++;
break;
case 'l': /* Log destination: s|e|o|f */
if ((logdst = clixon_log_opt(optarg[0])) < 0)
usage(argv[0]);
break;
case 'o':
output++;
break;
case 'v':
validate++;
break;
case 'p':
pretty++;
break;
case 'y':
yang_file_dir = optarg;
break;
case 'Y':
if (clicon_option_add(h, "CLICON_YANG_DIR", optarg) < 0)
goto done;
break;
case 't':
top_input_filename = optarg;
break;
case 'T': /* top file xpath */
top_path = optarg;
break;
case 'u':
if (clicon_option_bool_set(h, "CLICON_YANG_UNKNOWN_ANYDATA", 1) < 0)
goto done;
xml_bind_yang_unknown_anydata(1);
break;
default:
usage(argv[0]);
break;
}
if (validate && !yang_file_dir){
fprintf(stderr, "-v requires -y\n");
usage(argv[0]);
}
if (top_input_filename && top_path == NULL){
fprintf(stderr, "-t requires -T\n");
usage(argv[0]);
}
clixon_log_init(h, __FILE__, dbg?LOG_DEBUG:LOG_INFO, logdst);
clixon_debug_init(h, dbg);
if (yang_init(h) < 0)
goto done;
/* 1. Parse yang */
if (yang_file_dir){
if ((yspec = yspec_new(h, YANG_DATA_TOP)) == NULL)
goto done;
if (stat(yang_file_dir, &st) < 0){
clixon_err(OE_YANG, errno, "%s not found", yang_file_dir);
goto done;
}
if (S_ISDIR(st.st_mode)){
if (yang_spec_load_dir(h, yang_file_dir, yspec) < 0)
goto done;
}
else{
if (yang_spec_parse_file(h, yang_file_dir, yspec) < 0)
goto done;
}
}
/* If top file is declared, the base XML/JSON is pasted as child to the top-file.
* This is to emulate sub-tress, not just top-level parsing.
* Always validated
*/
if (top_input_filename){
if ((tfp = fopen(top_input_filename, "r")) == NULL){
clixon_err(OE_YANG, errno, "fopen(%s)", top_input_filename);
goto done;
}
if ((ret = clixon_xml_parse_file(tfp, YB_MODULE, yspec, &xtop, &xerr)) < 0){
fprintf(stderr, "xml parse error: %s\n", clixon_err_reason());
goto done;
}
if (ret == 0){
clixon_err_netconf(h, OE_XML, 0, xerr, "Parse top file");
goto done;
}
if (validate_tree(h, xtop, yspec) < 0)
goto done;
/* Compute canonical namespace context */
if (xml_nsctx_yangspec(yspec, &nsc) < 0)
goto done;
if ((xbot = xpath_first(xtop, nsc, "%s", top_path)) == NULL){
fprintf(stderr, "Path not found in top tree: %s\n", top_path);
goto done;
}
xt = xbot;
}
if (input_filename){
if ((fp = fopen(input_filename, "r")) == NULL){
clixon_err(OE_YANG, errno, "open(%s)", input_filename);
goto done;
}
}
/* 2. Parse data (xml/json) */
if (jsonin){
if ((ret = clixon_json_parse_file(fp, 1, top_input_filename?YB_PARENT:YB_MODULE, yspec, &xt, &xerr)) < 0)
goto done;
if (ret == 0){
clixon_err_netconf(h, OE_XML, 0, xerr, "util_xml");
goto done;
}
}
else{ /* XML */
if (!yang_file_dir)
yb = YB_NONE;
else if (xt == NULL)
yb = YB_MODULE;
else
yb = YB_PARENT;
if ((ret = clixon_xml_parse_file(fp, yb, yspec, &xt, &xerr)) < 0){
fprintf(stderr, "xml parse error: %s\n", clixon_err_reason());
goto done;
}
if (ret == 0){
clixon_err_netconf(h, OE_XML, 0, xerr, "util_xml");
goto done;
}
}
/* 3. Validate data (if yspec) */
if (validate){
if (validate_tree(h, xt, yspec) < 0)
goto done;
}
/* 4. Output data (xml/json/text) */
if (output){
if (textout){
if (clixon_text2file(stdout, xt, 0, fprintf, 1, 0) < 0)
goto done;
}
else if (jsonout){
if (clixon_json2cbuf(cb, xt, pretty, 1, 0, 0) < 0)
goto done;
}
else if (clixon_xml2cbuf(cb, xt, 0, pretty, NULL, -1, 1) < 0)
goto done;
fprintf(stdout, "%s", cbuf_get(cb));
fflush(stdout);
}
retval = 0;
done:
yang_exit(h);
if (tfp)
fclose(tfp);
if (fp)
fclose(fp);
if (nsc)
cvec_free(nsc);
if (cbret)
cbuf_free(cbret);
if (xcfg)
xml_free(xcfg);
if (xerr)
xml_free(xerr);
if (xtop)
xml_free(xtop);
else if (xt)
xml_free(xt);
if (cb)
cbuf_free(cb);
if (h)
clixon_handle_exit(h);
return retval;
}