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
|
/* protocol.c
Functions supporting the object management protocol... */
/*
* Copyright (c) 1999-2000 Internet Software Consortium.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of The Internet Software Consortium nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This software has been written for the Internet Software Consortium
* by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
* To learn more about the Internet Software Consortium, see
* ``http://www.isc.org/''. To learn more about Vixie Enterprises,
* see ``http://www.vix.com''. To learn more about Nominum, Inc., see
* ``http://www.nominum.com''.
*/
#include <omapip/omapip_p.h>
OMAPI_OBJECT_ALLOC (omapi_protocol, omapi_protocol_object_t,
omapi_type_protocol)
OMAPI_OBJECT_ALLOC (omapi_protocol_listener, omapi_protocol_listener_object_t,
omapi_type_protocol_listener)
isc_result_t omapi_protocol_connect (omapi_object_t *h,
const char *server_name,
unsigned port,
omapi_object_t *a)
{
isc_result_t rstatus, status;
omapi_protocol_object_t *obj;
#ifdef DEBUG_PROTOCOL
log_debug ("omapi_protocol_connect(%s port=%d)", server_name, port);
#endif
obj = (omapi_protocol_object_t *)0;
status = omapi_protocol_allocate (&obj, MDL);
if (status != ISC_R_SUCCESS)
return status;
rstatus = omapi_connect ((omapi_object_t *)obj, server_name, port);
if (rstatus != ISC_R_SUCCESS && rstatus != ISC_R_INCOMPLETE) {
omapi_protocol_dereference (&obj, MDL);
return rstatus;
}
status = omapi_object_reference (&h -> outer,
(omapi_object_t *)obj, MDL);
if (status != ISC_R_SUCCESS) {
omapi_protocol_dereference (&obj, MDL);
return status;
}
status = omapi_object_reference (&obj -> inner, h, MDL);
if (status != ISC_R_SUCCESS) {
omapi_protocol_dereference (&obj, MDL);
return status;
}
/* If we were passed a default authenticator, store it now. We'll
open it once we're connected. */
if (a) {
obj -> default_auth =
dmalloc (sizeof(omapi_remote_auth_t), MDL);
if (!obj -> default_auth) {
omapi_protocol_dereference (&obj, MDL);
return ISC_R_NOMEMORY;
}
obj -> default_auth -> next = (omapi_remote_auth_t *)0;
status = omapi_object_reference (&obj -> default_auth -> a,
a, MDL);
if (status != ISC_R_SUCCESS) {
dfree (obj -> default_auth, MDL);
omapi_protocol_dereference (&obj, MDL);
return status;
}
obj -> insecure = 0;
rstatus = ISC_R_INCOMPLETE;
} else {
obj -> insecure = 1;
#if 0
status = ISC_R_SUCCESS;
#endif
}
omapi_protocol_dereference (&obj, MDL);
return rstatus;
}
/* Send the protocol introduction message. */
isc_result_t omapi_protocol_send_intro (omapi_object_t *h,
unsigned ver,
unsigned hsize)
{
isc_result_t status;
omapi_protocol_object_t *p;
#ifdef DEBUG_PROTOCOL
log_debug ("omapi_protocol_send_intro()");
#endif
if (h -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)h;
if (!h -> outer || h -> outer -> type != omapi_type_connection)
return ISC_R_NOTCONNECTED;
status = omapi_connection_put_uint32 (h -> outer, ver);
if (status != ISC_R_SUCCESS)
return status;
status = omapi_connection_put_uint32 (h -> outer, hsize);
if (status != ISC_R_SUCCESS)
return status;
/* Require the other end to send an intro - this kicks off the
protocol input state machine. */
p -> state = omapi_protocol_intro_wait;
status = omapi_connection_require (h -> outer, 8);
if (status != ISC_R_SUCCESS && status != ISC_R_NOTYET)
return status;
/* Make up an initial transaction ID for this connection. */
p -> next_xid = random ();
return ISC_R_SUCCESS;
}
isc_result_t omapi_protocol_send_message (omapi_object_t *po,
omapi_object_t *id,
omapi_object_t *mo,
omapi_object_t *omo)
{
omapi_protocol_object_t *p;
omapi_object_t *c;
omapi_message_object_t *m, *om;
omapi_remote_auth_t *ra;
omapi_value_t *signature;
isc_result_t status;
u_int32_t foo;
unsigned auth_len;
if (po -> type != omapi_type_protocol ||
!po -> outer || po -> outer -> type != omapi_type_connection ||
mo -> type != omapi_type_message)
return ISC_R_INVALIDARG;
if (omo && omo -> type != omapi_type_message)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)po;
c = (omapi_object_t *)(po -> outer);
m = (omapi_message_object_t *)mo;
om = (omapi_message_object_t *)omo;
#ifdef DEBUG_PROTOCOL
log_debug ("omapi_protocol_send_message()"
"op=%ld handle=%#lx id=%#lx rid=%#lx",
(long)m -> op,
(long)(m -> object ? m -> object -> handle : m -> handle),
(long)p -> next_xid, (long)m -> rid);
#endif
/* Find the authid to use for this message. */
if (id) {
for (ra = p -> remote_auth_list; ra; ra = ra -> next) {
if (ra -> a == id) {
break;
}
}
if (!ra)
return ISC_R_KEY_UNKNOWN;
} else if (p -> remote_auth_list) {
ra = p -> default_auth;
} else {
ra = (omapi_remote_auth_t *)0;
}
if (ra) {
m -> authid = ra -> remote_handle;
status = omapi_object_reference (&m -> id_object,
ra -> a, MDL);
if (status != ISC_R_SUCCESS)
return status;
}
/* Write the ID of the authentication key we're using. */
status = omapi_connection_put_uint32 (c, ra ? ra -> remote_handle : 0);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Activate the authentication key on the connection. */
auth_len = 0;
if (ra) {
status = omapi_set_object_value (c, (omapi_object_t *)0,
"output-authenticator",
ra -> a);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
status = omapi_connection_output_auth_length (c, &auth_len);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
}
/* Write the authenticator length */
status = omapi_connection_put_uint32 (c, auth_len);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Write the opcode. */
status = omapi_connection_put_uint32 (c, m -> op);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Write the handle. If we've been given an explicit handle, use
that. Otherwise, use the handle of the object we're sending.
The caller is responsible for arranging for one of these handles
to be set (or not). */
status = omapi_connection_put_uint32 (c, (m -> h
? m -> h
: (m -> object
? m -> object -> handle
: 0)));
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Set and write the transaction ID. */
m -> id = p -> next_xid++;
status = omapi_connection_put_uint32 (c, m -> id);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Write the transaction ID of the message to which this is a
response, if there is such a message. */
status = omapi_connection_put_uint32 (c, om ? om -> id : m -> rid);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Stuff out the name/value pairs specific to this message. */
status = omapi_stuff_values (c, id, (omapi_object_t *)m);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Write the zero-length name that terminates the list of name/value
pairs specific to the message. */
status = omapi_connection_put_uint16 (c, 0);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Stuff out all the published name/value pairs in the object that's
being sent in the message, if there is one. */
if (m -> object) {
status = omapi_stuff_values (c, id, m -> object);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
}
/* Write the zero-length name that terminates the list of name/value
pairs for the associated object. */
status = omapi_connection_put_uint16 (c, 0);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
if (ra) {
/* Calculate the message signature. */
signature = (omapi_value_t *)0;
status = omapi_get_value_str (c, (omapi_object_t *)0,
"output-signature", &signature);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Write the authenticator... */
status = (omapi_connection_copyin
(c, signature -> value -> u.buffer.value,
signature -> value -> u.buffer.len));
omapi_value_dereference (&signature, MDL);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Dectivate the authentication key on the connection. */
status = omapi_set_value_str (c, (omapi_object_t *)0,
"output-authenticator",
(omapi_typed_data_t *)0);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
}
if (!omo) {
omapi_protocol_reference (&m -> protocol_object, p, MDL);
}
return ISC_R_SUCCESS;
}
isc_result_t omapi_protocol_signal_handler (omapi_object_t *h,
const char *name, va_list ap)
{
isc_result_t status;
omapi_protocol_object_t *p;
omapi_object_t *c;
omapi_message_object_t *m;
omapi_value_t *signature;
u_int16_t nlen;
u_int32_t vlen;
u_int32_t th;
#if defined (DEBUG_MEMORY_LEAKAGE)
unsigned long previous_outstanding = 0xBEADCAFE;
unsigned long connect_outstanding = 0xBEADCAFE;
#endif
if (h -> type != omapi_type_protocol) {
/* XXX shouldn't happen. Put an assert here? */
return ISC_R_UNEXPECTED;
}
p = (omapi_protocol_object_t *)h;
if (!strcmp (name, "connect")) {
#if defined (DEBUG_MEMORY_LEAKAGE)
connect_outstanding = dmalloc_outstanding;
#endif
/* Send the introductory message. */
status = omapi_protocol_send_intro
(h, OMAPI_PROTOCOL_VERSION,
sizeof (omapi_protocol_header_t));
if (status != ISC_R_SUCCESS) {
omapi_disconnect (p -> outer, 1);
return status;
}
return ISC_R_SUCCESS;
}
/* Should only receive these when opening the initial authenticator. */
if (!strcmp (name, "status")) {
status = va_arg (ap, isc_result_t);
if (status != ISC_R_SUCCESS) {
omapi_signal_in (h -> inner, "status", status,
(omapi_object_t *)0);
omapi_disconnect (p -> outer, 1);
return status;
} else {
return omapi_signal_in (h -> inner, "ready");
}
}
/* If we get a disconnect, dump memory usage. */
if (!strcmp (name, "disconnect")) {
#if defined (DEBUG_MEMORY_LEAKAGE)
if (connect_outstanding != 0xBEADCAFE) {
log_info ("generation %ld: %ld new, %ld outstanding, %ld%s",
dmalloc_generation,
dmalloc_outstanding - previous_outstanding,
dmalloc_outstanding, dmalloc_longterm, " long-term");
}
#endif
#if defined (DEBUG_MEMORY_LEAKAGE)
dmalloc_dump_outstanding ();
#endif
#if defined (DEBUG_RC_HISTORY_EXHAUSTIVELY)
dump_rc_history ();
#endif
for (m = omapi_registered_messages; m; m = m -> next) {
if (m -> protocol_object == p) {
if (m -> object)
omapi_signal (m -> object, "disconnect");
}
}
}
/* Not a signal we recognize? */
if (strcmp (name, "ready")) {
if (p -> inner && p -> inner -> type -> signal_handler)
return (*(p -> inner -> type -> signal_handler)) (h,
name,
ap);
return ISC_R_NOTFOUND;
}
if (!p -> outer || p -> outer -> type != omapi_type_connection)
return ISC_R_INVALIDARG;
c = p -> outer;
/* We get here because we requested that we be woken up after
some number of bytes were read, and that number of bytes
has in fact been read. */
switch (p -> state) {
case omapi_protocol_intro_wait:
/* Get protocol version and header size in network
byte order. */
omapi_connection_get_uint32 (c, &p -> protocol_version);
omapi_connection_get_uint32 (c, &p -> header_size);
/* We currently only support the current protocol version. */
if (p -> protocol_version != OMAPI_PROTOCOL_VERSION) {
omapi_disconnect (c, 1);
return ISC_R_VERSIONMISMATCH;
}
if (p -> header_size < sizeof (omapi_protocol_header_t)) {
omapi_disconnect (c, 1);
return ISC_R_PROTOCOLERROR;
}
if (p -> default_auth) {
status = omapi_protocol_send_open
(h, (omapi_object_t *)0, "authenticator",
p -> default_auth -> a,
OMAPI_NOTIFY_PROTOCOL);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
} else {
status = omapi_signal_in (h -> inner, "ready");
}
to_header_wait:
/* The next thing we're expecting is a message header. */
p -> state = omapi_protocol_header_wait;
/* Register a need for the number of bytes in a
header, and if we already have that many, process
them immediately. */
if ((omapi_connection_require (c, p -> header_size)) !=
ISC_R_SUCCESS)
break;
/* If we already have the data, fall through. */
case omapi_protocol_header_wait:
#if defined (DEBUG_MEMORY_LEAKAGE)
if (previous_outstanding != 0xBEADCAFE) {
log_info ("%s %ld: %ld new, %ld outstanding, %ld%s",
"generation", dmalloc_generation,
dmalloc_outstanding - previous_outstanding,
dmalloc_outstanding, dmalloc_longterm,
" long-term");
#endif
#if (defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL))
dmalloc_dump_outstanding ();
#endif
#if defined (DEBUG_RC_HISTORY_EXHAUSTIVELY)
dump_rc_history ();
#endif
#if defined (DEBUG_MEMORY_LEAKAGE)
}
previous_outstanding = dmalloc_outstanding;
#endif
status = omapi_message_new ((omapi_object_t **)&p -> message,
MDL);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
p -> verify_result = ISC_R_SUCCESS;
/* Swap in the header... */
omapi_connection_get_uint32 (c, &p -> message -> authid);
/* Bind the authenticator to the message object. */
if (p -> message -> authid) {
status = (omapi_protocol_lookup_auth
(&p -> message -> id_object, h,
p -> message -> authid));
if (status != ISC_R_SUCCESS)
p -> verify_result = status;
/* Activate the authentication key. */
status = omapi_set_object_value
(c, (omapi_object_t *)0, "input-authenticator",
p -> message -> id_object);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
}
omapi_connection_get_uint32 (c, &p -> message -> authlen);
omapi_connection_get_uint32 (c, &p -> message -> op);
omapi_connection_get_uint32 (c, &th);
p -> message -> h = th;
omapi_connection_get_uint32 (c, &p -> message -> id);
omapi_connection_get_uint32 (c, &p -> message -> rid);
/* If there was any extra header data, skip over it. */
if (p -> header_size > sizeof (omapi_protocol_header_t)) {
omapi_connection_copyout
(0, c, (p -> header_size -
sizeof (omapi_protocol_header_t)));
}
/* XXX must compute partial signature across the
XXX preceding bytes. Also, if authenticator
specifies encryption as well as signing, we may
have to decrypt the data on the way in. */
/* First we read in message-specific values, then object
values. */
p -> reading_message_values = 1;
need_name_length:
/* The next thing we're expecting is length of the
first name. */
p -> state = omapi_protocol_name_length_wait;
/* Wait for a 16-bit length. */
if ((omapi_connection_require (c, 2)) != ISC_R_SUCCESS)
break;
/* If it's already here, fall through. */
case omapi_protocol_name_length_wait:
omapi_connection_get_uint16 (c, &nlen);
/* A zero-length name means that we're done reading name+value
pairs. */
if (nlen == 0) {
/* If we've already read in the object, we are
done reading the message, but if we've just
finished reading in the values associated
with the message, we need to read the
object. */
if (p -> reading_message_values) {
p -> reading_message_values = 0;
goto need_name_length;
}
/* If the authenticator length is zero, there's no
signature to read in, so go straight to processing
the message. */
if (p -> message -> authlen == 0)
goto message_done;
/* The next thing we're expecting is the
message signature. */
p -> state = omapi_protocol_signature_wait;
/* Wait for the number of bytes specified for
the authenticator. If we already have it,
go read it in. */
if (omapi_connection_require
(c, p -> message -> authlen) == ISC_R_SUCCESS)
goto signature_wait;
break;
}
/* Allocate a buffer for the name. */
status = (omapi_data_string_new (&p -> name, nlen, MDL));
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return ISC_R_NOMEMORY;
}
p -> state = omapi_protocol_name_wait;
if (omapi_connection_require (c, nlen) != ISC_R_SUCCESS)
break;
/* If it's already here, fall through. */
case omapi_protocol_name_wait:
omapi_connection_copyout (p -> name -> value, c,
p -> name -> len);
/* Wait for a 32-bit length. */
p -> state = omapi_protocol_value_length_wait;
if ((omapi_connection_require (c, 4)) != ISC_R_SUCCESS)
break;
/* If it's already here, fall through. */
case omapi_protocol_value_length_wait:
omapi_connection_get_uint32 (c, &vlen);
/* Zero-length values are allowed - if we get one, we
don't have to read any data for the value - just
get the next one, if there is a next one. */
if (!vlen)
goto insert_new_value;
status = omapi_typed_data_new (MDL, &p -> value,
omapi_datatype_data,
vlen);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return ISC_R_NOMEMORY;
}
p -> state = omapi_protocol_value_wait;
if (omapi_connection_require (c, vlen) != ISC_R_SUCCESS)
break;
/* If it's already here, fall through. */
case omapi_protocol_value_wait:
omapi_connection_copyout (p -> value -> u.buffer.value, c,
p -> value -> u.buffer.len);
insert_new_value:
if (p -> reading_message_values) {
status = (omapi_set_value
((omapi_object_t *)p -> message,
p -> message -> id_object,
p -> name, p -> value));
} else {
if (!p -> message -> object) {
/* We need a generic object to hang off of the
incoming message. */
status = (omapi_generic_new
(&p -> message -> object, MDL));
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
}
status = (omapi_set_value
((omapi_object_t *)p -> message -> object,
p -> message -> id_object,
p -> name, p -> value));
}
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
omapi_data_string_dereference (&p -> name, MDL);
if (p -> value)
omapi_typed_data_dereference (&p -> value, MDL);
goto need_name_length;
signature_wait:
case omapi_protocol_signature_wait:
if (p -> message -> id_object) {
/* Compute the signature of the message. */
signature = (omapi_value_t *)0;
status = omapi_get_value_str (c, (omapi_object_t *)0,
"input-signature",
&signature);
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return status;
}
/* Disable the authentication key on the connection. */
status = omapi_set_value_str (c, (omapi_object_t *)0,
"input-authenticator",
(omapi_typed_data_t *)0);
if (status != ISC_R_SUCCESS) {
omapi_value_dereference (&signature, MDL);
omapi_disconnect (c, 1);
return status;
}
}
/* Read the authenticator. */
status = omapi_typed_data_new (MDL,
&p -> message -> authenticator,
omapi_datatype_data,
p -> message -> authlen);
if (status != ISC_R_SUCCESS) {
omapi_value_dereference (&signature, MDL);
omapi_disconnect (c, 1);
return ISC_R_NOMEMORY;
}
omapi_connection_copyout
(p -> message -> authenticator -> u.buffer.value, c,
p -> message -> authlen);
/* Verify the signature. */
if (p -> message -> id_object &&
((signature -> value -> u.buffer.len !=
p -> message -> authlen) ||
(memcmp (signature -> value -> u.buffer.value,
p -> message -> authenticator -> u.buffer.value,
p -> message -> authlen) != 0))) {
/* Invalid signature. */
p -> verify_result = ISC_R_INVALIDKEY;
}
omapi_value_dereference (&signature, MDL);
/* Process the message. */
message_done:
if (p -> verify_result != ISC_R_SUCCESS) {
status = omapi_protocol_send_status
(h, (omapi_object_t *)0, p -> verify_result,
p -> message -> id, (char *)0);
} else {
status = omapi_message_process
((omapi_object_t *)p -> message, h);
}
if (status != ISC_R_SUCCESS) {
omapi_disconnect (c, 1);
return ISC_R_NOMEMORY;
}
omapi_message_dereference (&p -> message, MDL);
#if defined (DEBUG_MEMORY_LEAKAGE)
log_info ("generation %ld: %ld new, %ld outstanding, %ld%s",
dmalloc_generation,
dmalloc_outstanding - previous_outstanding,
dmalloc_outstanding, dmalloc_longterm, " long-term");
#endif
#if (defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL))
dmalloc_dump_outstanding ();
#endif
#if defined (DEBUG_RC_HISTORY_EXHAUSTIVELY)
dump_rc_history ();
#endif
#if defined (DEBUG_MEMORY_LEAKAGE)
previous_outstanding = 0xBEADCAFE;
#endif
/* Now wait for the next message. */
goto to_header_wait;
default:
/* XXX should never get here. Assertion? */
break;
}
return ISC_R_SUCCESS;
}
isc_result_t omapi_protocol_add_auth (omapi_object_t *po,
omapi_object_t *ao,
omapi_handle_t handle)
{
omapi_protocol_object_t *p;
omapi_remote_auth_t *r;
isc_result_t status;
if (ao -> type != omapi_type_auth_key &&
(!ao -> inner || ao -> inner -> type != omapi_type_auth_key))
return ISC_R_INVALIDARG;
if (po -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)po;
#ifdef DEBUG_PROTOCOL
log_debug ("omapi_protocol_add_auth(name=%s)",
((omapi_auth_key_t *)ao) -> name);
#endif
if (p -> verify_auth) {
status = (p -> verify_auth) (po, (omapi_auth_key_t *)ao);
if (status != ISC_R_SUCCESS)
return status;
}
/* If omapi_protocol_connect() was called with a default
authenticator, p -> default_auth will already be set,
but p -> remote_auth_list will not yet be initialized. */
if (p -> default_auth && !p -> remote_auth_list) {
if (p -> default_auth -> a != ao) {
/* Something just went horribly wrong. */
omapi_disconnect (p -> outer, 1);
return ISC_R_UNEXPECTED;
}
p -> remote_auth_list = p -> default_auth;
p -> default_auth -> remote_handle = handle;
return omapi_signal_in (p -> inner, "ready");
}
r = dmalloc (sizeof(*r), MDL);
if (!r)
return ISC_R_NOMEMORY;
status = omapi_object_reference (&r -> a, ao, MDL);
if (status != ISC_R_SUCCESS) {
dfree (r, MDL);
return status;
}
r -> remote_handle = handle;
r -> next = p -> remote_auth_list;
p -> remote_auth_list = r;
return ISC_R_SUCCESS;
}
isc_result_t omapi_protocol_lookup_auth (omapi_object_t **a,
omapi_object_t *po,
omapi_handle_t handle)
{
omapi_protocol_object_t *p;
omapi_remote_auth_t *r;
if (po -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)po;
for (r = p -> remote_auth_list; r; r = r -> next)
if (r -> remote_handle == handle)
return omapi_object_reference (a, r -> a, MDL);
return ISC_R_KEY_UNKNOWN;
}
isc_result_t omapi_protocol_set_value (omapi_object_t *h,
omapi_object_t *id,
omapi_data_string_t *name,
omapi_typed_data_t *value)
{
omapi_protocol_object_t *p;
omapi_remote_auth_t *r;
if (h -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)h;
if (omapi_ds_strcmp (name, "default-authenticator") == 0) {
if (value -> type != omapi_datatype_object)
return ISC_R_INVALIDARG;
if (!value || !value -> u.object) {
p -> default_auth = (omapi_remote_auth_t *)0;
} else {
for (r = p -> remote_auth_list; r; r = r -> next)
if (r -> a == value -> u.object)
break;
if (!r)
return ISC_R_KEY_UNKNOWN;
p -> default_auth = r;
}
return ISC_R_SUCCESS;
}
if (h -> inner && h -> inner -> type -> set_value)
return (*(h -> inner -> type -> set_value))
(h -> inner, id, name, value);
return ISC_R_NOTFOUND;
}
isc_result_t omapi_protocol_get_value (omapi_object_t *h,
omapi_object_t *id,
omapi_data_string_t *name,
omapi_value_t **value)
{
omapi_protocol_object_t *p;
if (h -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)h;
if (omapi_ds_strcmp (name, "default-authenticator") == 0) {
if (!p -> default_auth)
return ISC_R_NOTFOUND;
return omapi_make_object_value (value, name,
p -> default_auth -> a, MDL);
}
if (h -> inner && h -> inner -> type -> get_value)
return (*(h -> inner -> type -> get_value))
(h -> inner, id, name, value);
return ISC_R_NOTFOUND;
}
isc_result_t omapi_protocol_destroy (omapi_object_t *h,
const char *file, int line)
{
omapi_protocol_object_t *p;
if (h -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
p = (omapi_protocol_object_t *)h;
if (p -> message)
omapi_message_dereference (&p -> message, file, line);
/* This will happen if: 1) A default authenticator is supplied to
omapi_protocol_connect(), and 2) something goes wrong before
the authenticator can be opened. */
if (p -> default_auth && !p -> remote_auth_list)
dfree (p -> default_auth, file, line);
while (p -> remote_auth_list) {
omapi_remote_auth_t *r = p -> remote_auth_list -> next;
omapi_object_dereference (&r -> a, file, line);
dfree (r, file, line);
p -> remote_auth_list = r;
}
return ISC_R_SUCCESS;
}
/* Write all the published values associated with the object through the
specified connection. */
isc_result_t omapi_protocol_stuff_values (omapi_object_t *c,
omapi_object_t *id,
omapi_object_t *p)
{
int i;
if (p -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
if (p -> inner && p -> inner -> type -> stuff_values)
return (*(p -> inner -> type -> stuff_values)) (c, id,
p -> inner);
return ISC_R_SUCCESS;
}
/* Returns a boolean indicating whether this protocol requires that
messages be authenticated or not. */
isc_boolean_t omapi_protocol_authenticated (omapi_object_t *h)
{
if (h -> type != omapi_type_protocol)
return isc_boolean_false;
if (((omapi_protocol_object_t *)h) -> insecure)
return isc_boolean_false;
else
return isc_boolean_true;
}
/* Sets the address and authenticator verification callbacks. The handle
is to a listener object, not a protocol object. */
isc_result_t omapi_protocol_configure_security (omapi_object_t *h,
isc_result_t (*verify_addr)
(omapi_object_t *,
omapi_addr_t *),
isc_result_t (*verify_auth)
(omapi_object_t *,
omapi_auth_key_t *))
{
omapi_protocol_listener_object_t *l;
if (h -> outer && h -> outer -> type == omapi_type_protocol_listener)
h = h -> outer;
if (h -> type != omapi_type_protocol_listener)
return ISC_R_INVALIDARG;
l = (omapi_protocol_listener_object_t *)h;
l -> verify_auth = verify_auth;
l -> insecure = 0;
return omapi_listener_configure_security (h -> outer, verify_addr);
}
/* Set up a listener for the omapi protocol. The handle stored points to
a listener object, not a protocol object. */
isc_result_t omapi_protocol_listen (omapi_object_t *h,
unsigned port,
int max)
{
isc_result_t status;
omapi_protocol_listener_object_t *obj;
obj = (omapi_protocol_listener_object_t *)0;
status = omapi_protocol_listener_allocate (&obj, MDL);
if (status != ISC_R_SUCCESS)
return status;
status = omapi_object_reference (&h -> outer,
(omapi_object_t *)obj, MDL);
if (status != ISC_R_SUCCESS) {
omapi_protocol_listener_dereference (&obj, MDL);
return status;
}
status = omapi_object_reference (&obj -> inner, h, MDL);
if (status != ISC_R_SUCCESS) {
omapi_protocol_listener_dereference (&obj, MDL);
return status;
}
/* What a terrible default. */
obj -> insecure = 1;
status = omapi_listen ((omapi_object_t *)obj, port, max);
omapi_protocol_listener_dereference (&obj, MDL);
return status;
}
/* Signal handler for protocol listener - if we get a connect signal,
create a new protocol connection, otherwise pass the signal down. */
isc_result_t omapi_protocol_listener_signal (omapi_object_t *o,
const char *name, va_list ap)
{
isc_result_t status;
omapi_object_t *c;
omapi_protocol_object_t *obj;
omapi_protocol_listener_object_t *p;
if (!o || o -> type != omapi_type_protocol_listener)
return ISC_R_INVALIDARG;
p = (omapi_protocol_listener_object_t *)o;
/* Not a signal we recognize? */
if (strcmp (name, "connect")) {
if (p -> inner && p -> inner -> type -> signal_handler)
return (*(p -> inner -> type -> signal_handler))
(p -> inner, name, ap);
return ISC_R_NOTFOUND;
}
c = va_arg (ap, omapi_object_t *);
if (!c || c -> type != omapi_type_connection)
return ISC_R_INVALIDARG;
obj = (omapi_protocol_object_t *)0;
status = omapi_protocol_allocate (&obj, MDL);
if (status != ISC_R_SUCCESS)
return status;
obj -> verify_auth = p -> verify_auth;
obj -> insecure = p -> insecure;
status = omapi_object_reference (&obj -> outer, c, MDL);
if (status != ISC_R_SUCCESS) {
lose:
omapi_protocol_dereference (&obj, MDL);
omapi_disconnect (c, 1);
return status;
}
status = omapi_object_reference (&c -> inner,
(omapi_object_t *)obj, MDL);
if (status != ISC_R_SUCCESS)
goto lose;
/* Send the introductory message. */
status = omapi_protocol_send_intro ((omapi_object_t *)obj,
OMAPI_PROTOCOL_VERSION,
sizeof (omapi_protocol_header_t));
if (status != ISC_R_SUCCESS)
goto lose;
omapi_protocol_dereference (&obj, MDL);
return status;
}
isc_result_t omapi_protocol_listener_set_value (omapi_object_t *h,
omapi_object_t *id,
omapi_data_string_t *name,
omapi_typed_data_t *value)
{
if (h -> type != omapi_type_protocol_listener)
return ISC_R_INVALIDARG;
if (h -> inner && h -> inner -> type -> set_value)
return (*(h -> inner -> type -> set_value))
(h -> inner, id, name, value);
return ISC_R_NOTFOUND;
}
isc_result_t omapi_protocol_listener_get_value (omapi_object_t *h,
omapi_object_t *id,
omapi_data_string_t *name,
omapi_value_t **value)
{
if (h -> type != omapi_type_protocol_listener)
return ISC_R_INVALIDARG;
if (h -> inner && h -> inner -> type -> get_value)
return (*(h -> inner -> type -> get_value))
(h -> inner, id, name, value);
return ISC_R_NOTFOUND;
}
isc_result_t omapi_protocol_listener_destroy (omapi_object_t *h,
const char *file, int line)
{
if (h -> type != omapi_type_protocol_listener)
return ISC_R_INVALIDARG;
return ISC_R_SUCCESS;
}
/* Write all the published values associated with the object through the
specified connection. */
isc_result_t omapi_protocol_listener_stuff (omapi_object_t *c,
omapi_object_t *id,
omapi_object_t *p)
{
int i;
if (p -> type != omapi_type_protocol_listener)
return ISC_R_INVALIDARG;
if (p -> inner && p -> inner -> type -> stuff_values)
return (*(p -> inner -> type -> stuff_values)) (c, id,
p -> inner);
return ISC_R_SUCCESS;
}
isc_result_t omapi_protocol_send_status (omapi_object_t *po,
omapi_object_t *id,
isc_result_t waitstatus,
unsigned rid, const char *msg)
{
isc_result_t status;
omapi_message_object_t *message = (omapi_message_object_t *)0;
omapi_object_t *mo;
if (po -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
status = omapi_message_new ((omapi_object_t **)&message, MDL);
if (status != ISC_R_SUCCESS)
return status;
mo = (omapi_object_t *)message;
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"op", OMAPI_OP_STATUS);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"rid", (int)rid);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"result", (int)waitstatus);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
/* If a message has been provided, send it. */
if (msg) {
status = omapi_set_string_value (mo, (omapi_object_t *)0,
"message", msg);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
}
status = omapi_protocol_send_message (po, id, mo, (omapi_object_t *)0);
omapi_message_dereference (&message, MDL);
return status;
}
/* The OMAPI_NOTIFY_PROTOCOL flag will cause the notify-object for the
message to be set to the protocol object. This is used when opening
the default authenticator. */
isc_result_t omapi_protocol_send_open (omapi_object_t *po,
omapi_object_t *id,
const char *type,
omapi_object_t *object,
unsigned flags)
{
isc_result_t status;
omapi_message_object_t *message = (omapi_message_object_t *)0;
omapi_object_t *mo;
if (po -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
status = omapi_message_new ((omapi_object_t **)&message, MDL);
mo = (omapi_object_t *)message;
if (status == ISC_R_SUCCESS)
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"op", OMAPI_OP_OPEN);
if (status == ISC_R_SUCCESS)
status = omapi_set_object_value (mo, (omapi_object_t *)0,
"object", object);
if ((flags & OMAPI_CREATE) && (status == ISC_R_SUCCESS))
status = omapi_set_boolean_value (mo, (omapi_object_t *)0,
"create", 1);
if ((flags & OMAPI_UPDATE) && (status == ISC_R_SUCCESS))
status = omapi_set_boolean_value (mo, (omapi_object_t *)0,
"update", 1);
if ((flags & OMAPI_EXCL) && (status == ISC_R_SUCCESS))
status = omapi_set_boolean_value (mo, (omapi_object_t *)0,
"exclusive", 1);
if ((flags & OMAPI_NOTIFY_PROTOCOL) && (status == ISC_R_SUCCESS))
status = omapi_set_object_value (mo, (omapi_object_t *)0,
"notify-object", po);
if (type && (status == ISC_R_SUCCESS))
status = omapi_set_string_value (mo, (omapi_object_t *)0,
"type", type);
if (status == ISC_R_SUCCESS)
status = omapi_message_register (mo);
if (status == ISC_R_SUCCESS) {
status = omapi_protocol_send_message (po, id, mo,
(omapi_object_t *)0);
if (status != ISC_R_SUCCESS)
omapi_message_unregister (mo);
}
if (message)
omapi_message_dereference (&message, MDL);
return status;
}
isc_result_t omapi_protocol_send_update (omapi_object_t *po,
omapi_object_t *id,
unsigned rid,
omapi_object_t *object)
{
isc_result_t status;
omapi_message_object_t *message = (omapi_message_object_t *)0;
omapi_object_t *mo;
if (po -> type != omapi_type_protocol)
return ISC_R_INVALIDARG;
status = omapi_message_new ((omapi_object_t **)&message, MDL);
if (status != ISC_R_SUCCESS)
return status;
mo = (omapi_object_t *)message;
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"op", OMAPI_OP_UPDATE);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
if (rid) {
omapi_handle_t handle;
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"rid", (int)rid);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
status = omapi_object_handle (&handle, object);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
status = omapi_set_int_value (mo, (omapi_object_t *)0,
"handle", (int)handle);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
}
status = omapi_set_object_value (mo, (omapi_object_t *)0,
"object", object);
if (status != ISC_R_SUCCESS) {
omapi_message_dereference (&message, MDL);
return status;
}
status = omapi_protocol_send_message (po, id, mo, (omapi_object_t *)0);
omapi_message_dereference (&message, MDL);
return status;
}
|