-
Notifications
You must be signed in to change notification settings - Fork 49
/
dbus-xml.c
1948 lines (1606 loc) · 52.2 KB
/
dbus-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
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
/*
* Serialize and deserialize XML definitions, according to a given schema.
*
* Copyright (C) 2012, Olaf Kirch <okir@suse.de>
*
* FIXME: we ought to validate the schema, to make sure people don't do stupid things
* like attaching bitmap constraints to a string type etc.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <wicked/logging.h>
#include <wicked/xml.h>
#include "dbus-common.h"
#include "xml-schema.h"
#include "util_priv.h"
#include "limits.h"
#include "debug.h"
#include <wicked/netinfo.h>
#include <wicked/xpath.h>
#include "dbus-objects/model.h"
static void ni_dbus_define_scalar_types(ni_xs_scope_t *);
static void ni_dbus_define_xml_notations(void);
static int ni_dbus_xml_register_classes(ni_xs_scope_t *);
static ni_dbus_method_t *ni_dbus_xml_register_methods(ni_xs_service_t *, ni_xs_method_t *, const ni_dbus_method_t *);
static dbus_bool_t ni_dbus_validate_xml(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_validate_xml_void(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_validate_xml_scalar(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_validate_xml_struct(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_validate_xml_union(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_validate_xml_array(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_validate_xml_dict(xml_node_t *, const ni_xs_type_t *, const ni_dbus_xml_validate_context_t *);
static dbus_bool_t ni_dbus_serialize_xml(xml_node_t *, const ni_xs_type_t *, ni_dbus_variant_t *);
static dbus_bool_t ni_dbus_serialize_xml_scalar(xml_node_t *, const ni_xs_type_t *, ni_dbus_variant_t *);
static dbus_bool_t ni_dbus_serialize_xml_struct(xml_node_t *, const ni_xs_type_t *, ni_dbus_variant_t *);
static dbus_bool_t ni_dbus_serialize_xml_union(xml_node_t *, const ni_xs_type_t *, ni_dbus_variant_t *);
static dbus_bool_t ni_dbus_serialize_xml_array(xml_node_t *, const ni_xs_type_t *, ni_dbus_variant_t *);
static dbus_bool_t ni_dbus_serialize_xml_dict(xml_node_t *, const ni_xs_type_t *, ni_dbus_variant_t *);
static dbus_bool_t ni_dbus_serialize_xml_bitmask(const xml_node_t *, const ni_xs_scalar_info_t *, unsigned long *);
static dbus_bool_t ni_dbus_serialize_xml_bitmap(const xml_node_t *, const ni_xs_scalar_info_t *, unsigned long *);
static dbus_bool_t ni_dbus_deserialize_xml(const ni_dbus_variant_t *, const ni_xs_type_t *, xml_node_t *);
static dbus_bool_t ni_dbus_deserialize_xml_scalar(const ni_dbus_variant_t *, const ni_xs_type_t *, xml_node_t *);
static dbus_bool_t ni_dbus_deserialize_xml_struct(const ni_dbus_variant_t *, const ni_xs_type_t *, xml_node_t *);
static dbus_bool_t ni_dbus_deserialize_xml_union(const ni_dbus_variant_t *, const ni_xs_type_t *, xml_node_t *);
static dbus_bool_t ni_dbus_deserialize_xml_array(const ni_dbus_variant_t *, const ni_xs_type_t *, xml_node_t *);
static dbus_bool_t ni_dbus_deserialize_xml_dict(const ni_dbus_variant_t *, const ni_xs_type_t *, xml_node_t *);
static char * __ni_xs_type_to_dbus_signature(const ni_xs_type_t *, char *, size_t);
static char * ni_xs_type_to_dbus_signature(const ni_xs_type_t *);
static ni_xs_service_t *ni_dbus_xml_get_service_schema(const ni_xs_scope_t *, const char *);
static ni_xs_type_t * ni_dbus_xml_get_properties_schema(const ni_xs_scope_t *, const ni_xs_service_t *);
static ni_tempstate_t * __ni_dbus_xml_global_temp_state;
ni_xs_scope_t *
ni_dbus_xml_init(void)
{
ni_xs_scope_t *schema;
schema = ni_xs_scope_new(NULL, "dbus");
ni_dbus_define_scalar_types(schema);
ni_dbus_define_xml_notations();
return schema;
}
/*
* Register all services defined by the schema
*/
int
ni_dbus_xml_register_services(ni_xs_scope_t *scope)
{
ni_xs_service_t *xs_service;
int rv;
NI_TRACE_ENTER_ARGS("scope=%s", scope->name);
/* First, register any classes defined by the schema */
if ((rv = ni_dbus_xml_register_classes(scope)) < 0)
return rv;
for (xs_service = scope->services; xs_service; xs_service = xs_service->next) {
ni_dbus_service_t *service;
const ni_dbus_class_t *class = NULL;
const ni_var_t *attr;
/* An interface needs to be attached to an object. The object-class
* attribute specifies which object class this can attach to. */
if ((attr = ni_var_array_get(&xs_service->attributes, "object-class")) != NULL) {
const char *class_name = attr->value;
if ((class = ni_objectmodel_get_class(class_name)) == NULL) {
ni_error("xml service definition for %s: unknown object-class \"%s\"",
xs_service->interface, class_name);
}
}
service = (ni_dbus_service_t *) ni_objectmodel_service_by_name(xs_service->interface);
if (service != NULL) {
if (service->compatible == NULL) {
service->compatible = class;
} else if (class && service->compatible != class) {
ni_error("schema definition of interface %s changes class from %s to %s",
xs_service->interface,
service->compatible->name,
class->name);
}
} else {
service = xcalloc(1, sizeof(*service));
ni_string_dup(&service->name, xs_service->interface);
service->compatible = class;
ni_debug_dbus("register dbus service description %s", service->name);
ni_objectmodel_register_service(service);
}
service->schema = xs_service;
if (xs_service->methods)
service->methods = ni_dbus_xml_register_methods(xs_service, xs_service->methods, service->methods);
if (xs_service->signals)
service->signals = ni_dbus_xml_register_methods(xs_service, xs_service->signals, service->signals);
}
return 0;
}
/*
* Register all classes defined by a schema
*/
int
ni_dbus_xml_register_classes(ni_xs_scope_t *scope)
{
ni_xs_class_t *xs_class;
for (xs_class = scope->classes; xs_class; xs_class = xs_class->next) {
const ni_dbus_class_t *base_class;
ni_dbus_class_t *new_class;
base_class = ni_objectmodel_get_class(xs_class->base_name);
if (base_class == NULL) {
ni_error("unknown object base class \"%s\" referenced by schema", xs_class->base_name);
return -1;
}
new_class = ni_objectmodel_class_new(xs_class->name, base_class);
ni_objectmodel_register_class(new_class);
}
return 0;
}
ni_dbus_method_t *
ni_dbus_xml_register_methods(ni_xs_service_t *xs_service, ni_xs_method_t *xs_method_list, const ni_dbus_method_t *old_array)
{
ni_dbus_method_t *method_array, *method;
unsigned int num_new_methods = 0, num_old_methods = 0, num_methods = 0;
ni_xs_method_t *xs_method;
if (xs_method_list == NULL)
return NULL;
if (old_array) {
for (num_old_methods = 0; old_array[num_old_methods].name; ++num_old_methods)
;
}
for (xs_method = xs_method_list; xs_method; xs_method = xs_method->next)
num_new_methods++;
method_array = xcalloc(num_old_methods + num_new_methods + 1, sizeof(ni_dbus_method_t));
if (old_array) {
memcpy(method_array, old_array, num_old_methods * sizeof(ni_dbus_method_t));
num_methods = num_old_methods;
}
for (xs_method = xs_method_list; xs_method; xs_method = xs_method->next) {
char sigbuf[64];
unsigned int i;
/* Skip private methods */
if (xs_method->name == 0 || xs_method->name[0] == '_')
continue;
for (i = 0, method = NULL; i < num_methods; ++i) {
if (!strcmp(method_array[i].name, xs_method->name)) {
method = &method_array[i];
break;
}
}
/* First, build the method signature */
sigbuf[0] = '\0';
for (i = 0; i < xs_method->arguments.count; ++i) {
ni_xs_type_t *type = xs_method->arguments.data[i].type;
unsigned int k = strlen(sigbuf);
if (!__ni_xs_type_to_dbus_signature(type, sigbuf + k, sizeof(sigbuf) - k)) {
ni_error("bad definition of service %s method %s: "
"cannot build dbus signature of argument[%u] (%s)",
xs_service->interface, xs_method->name, i,
xs_method->arguments.data[i].name);
goto next_method;
}
}
if (method != NULL) {
if (method->call_signature && !ni_string_eq(method->call_signature, sigbuf)) {
ni_warn("%s method %s signature mismatch. Built-in \"%s\" vs schema \"%s\"",
xs_service->interface, xs_method->name,
method->call_signature, sigbuf);
}
method->schema = xs_method;
continue;
}
method = &method_array[num_methods++];
ni_string_dup((char **) &method->name, xs_method->name);
ni_string_dup((char **) &method->call_signature, sigbuf);
method->handler = NULL; /* will be bound later in ni_objectmodel_bind_extensions() */
method->schema = xs_method;
next_method: ;
}
return method_array;
}
/*
* Check whether a given method takes any arguments/returns anything
*/
unsigned int
ni_dbus_xml_method_num_args(const ni_dbus_method_t *method)
{
const ni_xs_method_t *xs_method = method->schema;
if (xs_method == NULL)
return 0;
return xs_method->arguments.count;
}
dbus_bool_t
ni_dbus_xml_method_has_return(const ni_dbus_method_t *method)
{
const ni_xs_method_t *xs_method = method->schema;
if (xs_method == NULL)
return FALSE;
return xs_method->retval != NULL;
}
/*
* Serialize XML rep of an argument to a dbus call
*/
dbus_bool_t
ni_dbus_xml_validate_argument(const ni_dbus_method_t *method, unsigned int narg,
xml_node_t *node, const ni_dbus_xml_validate_context_t *ctx)
{
const ni_xs_method_t *xs_method = method->schema;
if (!xs_method || narg >= xs_method->arguments.count)
return FALSE;
return ni_dbus_validate_xml(node, xs_method->arguments.data[narg].type, ctx);
}
ni_xs_type_t *
ni_dbus_xml_get_argument_type(const ni_dbus_method_t *method, unsigned int narg)
{
const ni_xs_method_t *xs_method = method->schema;
if (!xs_method || narg >= xs_method->arguments.count)
return NULL;
return xs_method->arguments.data[narg].type;
}
const xml_node_t *
ni_dbus_xml_get_argument_metadata(const ni_dbus_method_t *method, unsigned int narg)
{
ni_xs_type_t *xs_type;
if (!(xs_type = ni_dbus_xml_get_argument_type(method, narg)))
return NULL;
return xs_type->meta;
}
dbus_bool_t
ni_dbus_xml_serialize_arg(const ni_dbus_method_t *method, unsigned int narg,
ni_dbus_variant_t *var, xml_node_t *node)
{
ni_xs_type_t *xs_type;
if (!(xs_type = ni_dbus_xml_get_argument_type(method, narg)))
return FALSE;
return ni_dbus_serialize_xml(node, xs_type, var);
}
xml_node_t *
ni_dbus_xml_deserialize_arguments(const ni_dbus_method_t *method,
unsigned int num_vars, const ni_dbus_variant_t *vars,
xml_node_t *parent, ni_tempstate_t *temp_state)
{
xml_node_t *node = xml_node_new("arguments", parent);
const ni_xs_method_t *xs_method = method->schema;
unsigned int i;
/* This is a lousy hack, but it sure beats passing down the temp_state to
* all functions. */
__ni_dbus_xml_global_temp_state = temp_state;
for (i = 0; i < num_vars; ++i) {
xml_node_t *arg = xml_node_new(xs_method->arguments.data[i].name, node);
if (!ni_dbus_deserialize_xml(&vars[i], xs_method->arguments.data[i].type, arg)) {
xml_node_free(node);
node = NULL;
break;
}
}
__ni_dbus_xml_global_temp_state = NULL;
return node;
}
xml_node_t *
ni_dbus_xml_deserialize_properties(ni_xs_scope_t *schema, const char *interface_name, ni_dbus_variant_t *var, xml_node_t *parent)
{
ni_xs_service_t *service;
xml_node_t *node;
ni_xs_type_t *type;
if (ni_dbus_variant_is_dict(var) && var->array.len == 0)
return NULL;
if (!(service = ni_dbus_xml_get_service_schema(schema, interface_name))) {
ni_error("cannot represent %s properties - no schema definition", interface_name);
return NULL;
}
if (!(type = ni_dbus_xml_get_properties_schema(schema, service))) {
ni_error("no type named <properties> for interface %s", interface_name);
return NULL;
}
node = xml_node_new(service->name, parent);
if (!ni_dbus_deserialize_xml(var, type, node)) {
ni_error("failed to build xml for %s properties", interface_name);
return NULL;
}
return node;
}
int
ni_dbus_xml_serialize_properties(ni_xs_scope_t *schema, ni_dbus_variant_t *result, xml_node_t *node)
{
const char *interface_name = node->name;
ni_xs_service_t *service;
ni_xs_type_t *type;
ni_dbus_variant_init_dict(result);
if (!(service = ni_dbus_xml_get_service_schema(schema, interface_name))) {
ni_error("cannot represent %s properties - no schema definition", interface_name);
return -NI_ERROR_CANNOT_MARSHAL;
}
if (!(type = ni_dbus_xml_get_properties_schema(schema, service))) {
ni_error("no type named <properties> for interface %s", interface_name);
return -NI_ERROR_CANNOT_MARSHAL;
}
if (!ni_dbus_serialize_xml(node, type, result)) {
ni_error("failed to parse xml for %s properties", interface_name);
return -NI_ERROR_CANNOT_MARSHAL;
}
return 0;
}
/*
* Given an XML tree representing the data returned by an extension script,
* build the dbus response from it
*/
int
ni_dbus_serialize_return(const ni_dbus_method_t *method, ni_dbus_variant_t *result, xml_node_t *node)
{
const ni_xs_method_t *xs_method = method->schema;
ni_xs_type_t *xs_type;
ni_assert(xs_method);
if ((xs_type = xs_method->retval) == NULL)
return 0;
ni_debug_dbus("%s: serializing response (%s)", method->name, xs_type->name);
if (!ni_dbus_serialize_xml(node, xs_type, result))
return -NI_ERROR_CANNOT_MARSHAL;
return 1;
}
/*
* Extract a dbus error from an XML node
*/
void
ni_dbus_serialize_error(DBusError *error, xml_node_t *node)
{
const char *error_name;
if ((error_name = xml_node_get_attr(node, "name")) == NULL)
error_name = DBUS_ERROR_FAILED;
dbus_set_error(error, error_name, "%s",
node->cdata? node->cdata : "extension call failed (no error message returned by script)");
}
/*
* Validate an XML tree
*/
dbus_bool_t
ni_dbus_validate_xml(xml_node_t *node, const ni_xs_type_t *type, const ni_dbus_xml_validate_context_t *ctx)
{
if (ctx && ctx->metadata_callback && type->meta) {
if (!ctx->metadata_callback(node, type, type->meta, ctx->user_data))
return FALSE;
}
switch (type->class) {
case NI_XS_TYPE_VOID:
return ni_dbus_validate_xml_void(node, type, ctx);
case NI_XS_TYPE_SCALAR:
return ni_dbus_validate_xml_scalar(node, type, ctx);
case NI_XS_TYPE_STRUCT:
return ni_dbus_validate_xml_struct(node, type, ctx);
case NI_XS_TYPE_UNION:
return ni_dbus_validate_xml_union(node, type, ctx);
case NI_XS_TYPE_ARRAY:
return ni_dbus_validate_xml_array(node, type, ctx);
case NI_XS_TYPE_DICT:
return ni_dbus_validate_xml_dict(node, type, ctx);
default:
ni_error("unsupported xml type class %u", type->class);
return FALSE;
}
return TRUE;
}
/*
* Convert an XML tree to a dbus data object for serialization
*/
static dbus_bool_t
ni_dbus_serialize_xml(xml_node_t *node, const ni_xs_type_t *type, ni_dbus_variant_t *var)
{
switch (type->class) {
case NI_XS_TYPE_VOID:
return TRUE;
case NI_XS_TYPE_SCALAR:
return ni_dbus_serialize_xml_scalar(node, type, var);
case NI_XS_TYPE_STRUCT:
return ni_dbus_serialize_xml_struct(node, type, var);
case NI_XS_TYPE_UNION:
return ni_dbus_serialize_xml_union(node, type, var);
case NI_XS_TYPE_ARRAY:
return ni_dbus_serialize_xml_array(node, type, var);
case NI_XS_TYPE_DICT:
return ni_dbus_serialize_xml_dict(node, type, var);
default:
ni_error("unsupported xml type class %u", type->class);
return FALSE;
}
return TRUE;
}
/*
* Create XML from a dbus data object
*/
dbus_bool_t
ni_dbus_deserialize_xml(const ni_dbus_variant_t *var, const ni_xs_type_t *type, xml_node_t *node)
{
#if 0
int depth = 0;
{
xml_node_t *p;
for (p = node->parent; p; p = p->parent)
depth++;
}
ni_trace("%*.*sdeserialize <%s>", depth, depth, "", node->name);
#endif
switch (type->class) {
case NI_XS_TYPE_VOID:
return TRUE;
case NI_XS_TYPE_SCALAR:
return ni_dbus_deserialize_xml_scalar(var, type, node);
case NI_XS_TYPE_STRUCT:
return ni_dbus_deserialize_xml_struct(var, type, node);
case NI_XS_TYPE_UNION:
return ni_dbus_deserialize_xml_union(var, type, node);
case NI_XS_TYPE_ARRAY:
return ni_dbus_deserialize_xml_array(var, type, node);
case NI_XS_TYPE_DICT:
return ni_dbus_deserialize_xml_dict(var, type, node);
default:
ni_error("unsupported xml type class %u", type->class);
return FALSE;
}
return TRUE;
}
/*
* XML -> dbus_variant conversion for scalars
*/
static dbus_bool_t
ni_dbus_serialize_xml_bitmask(const xml_node_t *node, const ni_xs_scalar_info_t *scalar_info, unsigned long *result)
{
ni_string_array_t bit_name_arr = NI_STRING_ARRAY_INIT;
const ni_intmap_t *bits;
unsigned long value = 0, v;
unsigned int i, bv;
if (!node || !result || !scalar_info || !scalar_info->constraint.bitmask->bits)
return FALSE;
bits = scalar_info->constraint.bitmask->bits;
ni_string_split(&bit_name_arr, node->cdata, " ,|\t\n", 0);
for (i = 0; i < bit_name_arr.count; ++i) {
if (ni_parse_ulong(bit_name_arr.data[i], &v, 16) == 0) {
value |= v;
} else
if (ni_parse_uint_mapped(bit_name_arr.data[i], bits, &bv) == 0) {
value |= bv;
} else {
ni_error("%s: unknown bitmask value name <%s>",
xml_node_location(node),
bit_name_arr.data[i]);
return FALSE;
}
}
*result = value;
return TRUE;
}
static dbus_bool_t
ni_dbus_serialize_xml_bitmap(const xml_node_t *node, const ni_xs_scalar_info_t *scalar_info, unsigned long *result)
{
const ni_intmap_t *bits = scalar_info->constraint.bitmap->bits;
ni_string_array_t bit_name_arr = NI_STRING_ARRAY_INIT;
unsigned long value = 0;
unsigned int i;
unsigned int bb;
xml_node_t *child;
dbus_bool_t ret = TRUE;
if (!node)
return FALSE;
if (!node->children) {
/* Data is of the form:
* <node>flag1,...,flagN</node>
*/
ni_string_split(&bit_name_arr, node->cdata, " ,|\t\n", 0);
} else {
/* Data is of the form:
* <node>
* <flag1/>
* ...
* <flagN/>
* </node>
*/
for (child = node->children; child; child = child->next)
ni_string_array_append(&bit_name_arr, child->name);
}
for (i = 0; i < bit_name_arr.count && ret; ++i) {
if (ni_parse_uint_mapped(bit_name_arr.data[i], bits, &bb) < 0 ||
bb >= 32) {
ni_error("%s: unknown or bad bit value <%s>",
xml_node_location(node),
bit_name_arr.data[i]);
ret = FALSE;
}
/* May left shift past width of value if bb >= 32, but as ret
* will be FALSE assignment to result will not happen. */
value |= NI_BIT(bb);
}
ni_string_array_destroy(&bit_name_arr);
*result = ret ? value : *result;
return ret;
}
static dbus_bool_t
ni_dbus_serialize_xml_enum(const xml_node_t *node, const ni_xs_scalar_info_t *scalar_info, unsigned long *result)
{
const ni_intmap_t *names = scalar_info->constraint.enums->bits;
unsigned int value;
if (ni_parse_uint_maybe_mapped(node->cdata, names, &value, 0) < 0) {
ni_error("%s: element <%s>: unknown enum value \"%s\"", xml_node_location(node), node->name, node->cdata);
return FALSE;
}
*result = value;
return TRUE;
}
/*
* Validate a void node.
*/
dbus_bool_t
ni_dbus_validate_xml_void(xml_node_t *node, const ni_xs_type_t *type, const ni_dbus_xml_validate_context_t *ctx)
{
if (node->cdata) {
ni_error("%s: invalid void element <%s>: element has data", xml_node_location(node), node->name);
return FALSE;
}
if (node->children) {
ni_error("%s: invalid void element <%s>: element has children", xml_node_location(node), node->name);
return FALSE;
}
return TRUE;
}
/*
* Validate a scalar node.
* This is where we use the validation callback for
* - resolving references to e.g. device names
* - prompting the user for authentication data too valuable to store in a file
* - recording dependency information.
* and some more.
*/
dbus_bool_t
ni_dbus_validate_xml_scalar(xml_node_t *node, const ni_xs_type_t *type, const ni_dbus_xml_validate_context_t *ctx)
{
ni_xs_scalar_info_t *scalar_info = ni_xs_scalar_info(type);
unsigned long value;
if (scalar_info->constraint.bitmap)
return ni_dbus_serialize_xml_bitmap(node, scalar_info, &value);
if (scalar_info->constraint.bitmask)
return ni_dbus_serialize_xml_bitmask(node, scalar_info, &value);
/* This signals a "flag" type element, ie we simply test for its presence or
* absence. */
if (scalar_info->type == DBUS_TYPE_INVALID) {
if (node->cdata != NULL) {
ni_error("%s: invalid flag scalar <%s> - should be empty", xml_node_location(node), node->name);
return FALSE;
}
return TRUE;
}
if (node->cdata == NULL) {
ni_error("%s: unable to serialize scalar <%s> - no data", xml_node_location(node), node->name);
return FALSE;
}
if (scalar_info->constraint.enums)
return ni_dbus_serialize_xml_enum(node, scalar_info, &value);
/* FIXME: validate whether scalar value can be parsed! */
return TRUE;
}
dbus_bool_t
ni_dbus_serialize_xml_scalar(xml_node_t *node, const ni_xs_type_t *type, ni_dbus_variant_t *var)
{
ni_xs_scalar_info_t *scalar_info = ni_xs_scalar_info(type);
/* This signals a "flag" type element, ie we simply test for its presence or
* absence. We encode it as a BYTE value. */
if (scalar_info->type == DBUS_TYPE_INVALID) {
ni_dbus_variant_set_byte(var, 0);
return TRUE;
}
if (scalar_info->constraint.bitmap) {
unsigned long value;
if (!ni_dbus_serialize_xml_bitmap(node, scalar_info, &value)
|| !ni_dbus_variant_init_signature(var, ni_xs_type_to_dbus_signature(type)))
return FALSE;
return ni_dbus_variant_set_ulong(var, value);
}
if (scalar_info->constraint.bitmask) {
unsigned long value;
if (!ni_dbus_serialize_xml_bitmask(node, scalar_info, &value)
|| !ni_dbus_variant_init_signature(var, ni_xs_type_to_dbus_signature(type)))
return FALSE;
return ni_dbus_variant_set_ulong(var, value);
}
if (node->cdata == NULL) {
ni_error("unable to serialize node %s - no data", node->name);
return FALSE;
}
if (scalar_info->constraint.enums) {
unsigned long value;
if (!ni_dbus_serialize_xml_enum(node, scalar_info, &value)
|| !ni_dbus_variant_init_signature(var, ni_xs_type_to_dbus_signature(type)))
return FALSE;
return ni_dbus_variant_set_uint(var, value);
}
/* TBD: handle constants defined in the schema? */
if (!ni_dbus_variant_parse(var, node->cdata, ni_xs_type_to_dbus_signature(type))) {
ni_error("unable to serialize node %s - cannot parse value", node->name);
return FALSE;
}
return TRUE;
}
/*
* XML from dbus variant for scalars
*/
dbus_bool_t
ni_dbus_deserialize_xml_scalar(const ni_dbus_variant_t *var, const ni_xs_type_t *type, xml_node_t *node)
{
ni_xs_scalar_info_t *scalar_info = ni_xs_scalar_info(type);
const char *value;
if (var->type == DBUS_TYPE_ARRAY) {
ni_error("%s: expected a scalar, but got an array or dict", __func__);
return FALSE;
}
/* This signals a "flag" type element, ie we simply test for its presence or
* absence. We encode it as a BYTE value. */
if (scalar_info->type == DBUS_TYPE_INVALID) {
if (var->type != DBUS_TYPE_BYTE) {
ni_error("%s: <%s> flag element encoded incorrectly",
__func__, node->name);
return FALSE;
}
return TRUE;
}
if (scalar_info->constraint.bitmask) {
const ni_intmap_t *bits = scalar_info->constraint.bitmask->bits;
ni_string_array_t bit_name_arr = NI_STRING_ARRAY_INIT;
unsigned long value = 0;
if (!ni_dbus_variant_get_ulong(var, &value))
return FALSE;
for (; bits->name; ++bits) {
if ((value & bits->value) != bits->value)
continue;
ni_string_array_append(&bit_name_arr, bits->name);
value &= ~(bits->value);
}
if (value) {
char num[64] = { '\0' };
snprintf(num, sizeof(num), "0x%lx", value);
ni_string_array_append(&bit_name_arr, num);
}
ni_string_join(&node->cdata, &bit_name_arr, " | ");
ni_string_array_destroy(&bit_name_arr);
return TRUE;
}
if (scalar_info->constraint.bitmap) {
const ni_intmap_t *bits = scalar_info->constraint.bitmap->bits;
ni_string_array_t bit_name_arr = NI_STRING_ARRAY_INIT;
unsigned long value = 0;
unsigned int bb;
if (!ni_dbus_variant_get_ulong(var, &value))
return FALSE;
for (bb = 0; bb < 32; ++bb) {
const char *bit_name;
if ((value & (1 << bb)) == 0)
continue;
if ((bit_name = ni_format_uint_mapped(bb, bits)) != NULL)
ni_string_array_append(&bit_name_arr, bit_name);
else
ni_warn("unable to represent bit%u in <%s>", bb, node->name);
}
if (!ni_string_join(&node->cdata, &bit_name_arr, ", "))
ni_debug_dbus("Empty bit names string obtained.");
ni_string_array_destroy(&bit_name_arr);
return TRUE;
}
if (scalar_info->constraint.enums) {
const char *enum_name;
unsigned int value;
if (!ni_dbus_variant_get_uint(var, &value)) {
ni_error("%s: cannot get value for <%s>", __func__, node->name);
return FALSE;
}
enum_name = ni_format_uint_mapped(value, scalar_info->constraint.enums->bits);
if (enum_name != NULL) {
xml_node_set_cdata(node, enum_name);
} else {
char buffer[32];
snprintf(buffer, sizeof(buffer), "%u", value);
xml_node_set_cdata(node, buffer);
}
return TRUE;
}
if (!(value = ni_dbus_variant_sprint(var))) {
ni_error("%s: unable to represent variable value as string", __func__);
return FALSE;
}
/* FIXME: make sure we properly quote any xml meta characters */
xml_node_set_cdata(node, value);
return TRUE;
}
/*
* Serialize arrays with special notation
*/
static dbus_bool_t
ni_dbus_serialize_byte_array_notation(xml_node_t *node, const ni_xs_array_info_t *array_info,
unsigned char **data_ptr, unsigned int *data_len)
{
const ni_xs_notation_t *notation = array_info->notation;
/* For now, we handle only byte arrays */
if (notation->array_element_type != DBUS_TYPE_BYTE) {
ni_error("%s: cannot handle array notation \"%s\"", xml_node_location(node), notation->name);
return FALSE;
}
if (node->cdata == NULL) {
ni_error("%s: array not compatible with notation \"%s\"", xml_node_location(node), notation->name);
return FALSE;
}
if (!notation->parse(node->cdata, data_ptr, data_len)) {
ni_error("%s: cannot parse array with notation \"%s\", value=\"%s\"",
xml_node_location(node), notation->name, node->cdata);
return FALSE;
}
return TRUE;
}
/*
* Validate an array
*/
dbus_bool_t
ni_dbus_validate_xml_array(xml_node_t *node, const ni_xs_type_t *type, const ni_dbus_xml_validate_context_t *ctx)
{
ni_xs_array_info_t *array_info = ni_xs_array_info(type);
ni_xs_type_t *element_type = array_info->element_type;
xml_node_t *child;
if (array_info->notation) {
unsigned char *data = NULL;
unsigned int len = 0;
if (!ni_dbus_serialize_byte_array_notation(node, array_info, &data, &len))
return FALSE;
free(data);
return TRUE;
}
for (child = node->children; child; child = child->next) {
if (!ni_dbus_validate_xml(child, element_type, ctx))
return FALSE;
}
return TRUE;
}
/*
* Serialize an array
*/
dbus_bool_t
ni_dbus_serialize_xml_array(xml_node_t *node, const ni_xs_type_t *type, ni_dbus_variant_t *var)
{
ni_xs_array_info_t *array_info = ni_xs_array_info(type);
ni_xs_type_t *element_type = array_info->element_type;
xml_node_t *child;
if (array_info->notation) {
ni_dbus_variant_init_byte_array(var);
return ni_dbus_serialize_byte_array_notation(node, array_info, &var->byte_array_value, &var->array.len);
}
if (!ni_dbus_variant_init_signature(var, ni_xs_type_to_dbus_signature(type)))
return FALSE;
for (child = node->children; child; child = child->next) {
if (element_type->class == NI_XS_TYPE_SCALAR) {
if (child->cdata == NULL) {
ni_error("%s: NULL array element",
xml_node_location(child));
return FALSE;
}
/* TBD: handle constants defined in the schema? */
if (!ni_dbus_variant_array_parse_and_append_string(var, child->cdata)) {
ni_error("%s: syntax error in array element",__func__);
return FALSE;
}
} else if (element_type->class == NI_XS_TYPE_DICT) {
ni_dbus_variant_t *element;
element = ni_dbus_dict_array_add(var);
if (!element) {
/* should not happen */
ni_error("%s: could not append element to array",
xml_node_location(child));
return FALSE;
}
if (!ni_dbus_serialize_xml(child, element_type, element)) {
ni_error("%s: failed to serialize array element", xml_node_location(child));
return FALSE;
}
} else {
ni_error("%s: arrays of type %s not implemented yet",
xml_node_location(child), ni_xs_type_to_dbus_signature(element_type));
return FALSE;
}
}
return TRUE;
}
/*
* XML from dbus variant for arrays
*/
dbus_bool_t
ni_dbus_deserialize_xml_array(const ni_dbus_variant_t *var, const ni_xs_type_t *type, xml_node_t *node)
{
ni_xs_array_info_t *array_info = ni_xs_array_info(type);
ni_xs_type_t *element_type = array_info->element_type;
unsigned int i, array_len;
array_len = var->array.len;
if (array_info->notation) {
const ni_xs_notation_t *notation = array_info->notation;
char buffer[256];
/* For now, we handle only byte arrays */
if (notation->array_element_type != DBUS_TYPE_BYTE) {
ni_error("%s: cannot handle array notation \"%s\"", __func__, notation->name);
return FALSE;
}
if (!ni_dbus_variant_is_byte_array(var)) {
ni_error("%s: expected byte array, but got something else", __func__);
return FALSE;
}
if (!notation->print(var->byte_array_value, array_len, buffer, sizeof(buffer))) {
ni_error("%s: cannot represent array with notation \"%s\"", __func__, notation->name);
return FALSE;
}