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
|
/* This file is part of the Project Athena Zephyr Notification System.
* It contains source for the internal Zephyr routines.
*
* Created by: Robert French
*
* $Id$
*
* Copyright (c) 1987,1988,1991 by the Massachusetts Institute of
* Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#include <internal.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <utmp.h>
#ifndef lint
static const char rcsid_Zinternal_c[] =
"$Id$";
static const char copyright[] =
"Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology.";
#endif
int __Zephyr_fd = -1;
int __Zephyr_open;
int __Zephyr_port = -1;
struct in_addr __My_addr;
int __Q_CompleteLength;
int __Q_Size;
struct _Z_InputQ *__Q_Head, *__Q_Tail;
struct sockaddr_in __HM_addr;
struct sockaddr_in __HM_addr_real;
int __HM_set;
int __Zephyr_server;
ZLocations_t *__locate_list;
int __locate_num;
int __locate_next;
ZSubscription_t *__subscriptions_list;
int __subscriptions_num;
int __subscriptions_next;
int Z_discarded_packets = 0;
#ifdef HAVE_KRB5
/* This context is used throughout */
krb5_context Z_krb5_ctx;
static struct cksum_map_s {
krb5_enctype e;
krb5_cksumtype c;
} cksum_map[] = {
/* per RFC1510 and draft-ietf-krb-wg-crypto-02.txt */
{ ENCTYPE_NULL, CKSUMTYPE_RSA_MD5 },
{ ENCTYPE_DES_CBC_CRC, CKSUMTYPE_RSA_MD5_DES },
{ ENCTYPE_DES_CBC_MD4, CKSUMTYPE_RSA_MD4_DES },
{ ENCTYPE_DES_CBC_MD5, CKSUMTYPE_RSA_MD5_DES },
/*
* The implementors hate us, and are inconsistent with names for
* most things defined after RFC1510. Note that des3-cbc-sha1
* and des3-cbc-sha1-kd are listed by number to avoid confusion
* caused by inconsistency between the names used in the specs
* and those used by implementations.
* -- jhutz, 30-Nov-2002
*/
/* source lost in history (an expired internet-draft) */
{ 5 /* des3-cbc-md5 */, 9 /* rsa-md5-des3 */ },
{ 7 /* des3-cbc-sha1 */, 13 /* hmac-sha1-des3 */ },
/* per draft-ietf-krb-wg-crypto-02.txt */
{ 16 /* des3-cbc-sha1-kd */, 12 /* hmac-sha1-des3-kd */ },
/* per draft-raeburn-krb-rijndael-krb-02.txt */
{ 17 /* aes128-cts-hmac-sha1-96 */, 15 /* hmac-sha1-96-aes128 */ },
{ 18 /* aes256-cts-hmac-sha1-96 */, 16 /* hmac-sha1-96-aes256 */ },
/* per draft-brezak-win2k-krb-rc4-hmac-04.txt */
{ 23 /* rc4-hmac */, -138 /* hmac-md5 */ },
{ 24 /* rc4-hmac-exp */, -138 /* hmac-md5 */ },
};
#define N_CKSUM_MAP (sizeof(cksum_map) / sizeof(struct cksum_map_s))
Code_t
Z_krb5_lookup_cksumtype(krb5_enctype e,
krb5_cksumtype *c)
{
int i;
for (i = 0; i < N_CKSUM_MAP; i++) {
if (cksum_map[i].e == e) {
*c = cksum_map[i].c;
return ZERR_NONE;
}
}
return KRB5_PROG_ETYPE_NOSUPP;
}
#endif /* HAVE_KRB5 */
char __Zephyr_realm[REALM_SZ];
#ifdef Z_DEBUG
void (*__Z_debug_print)(const char *fmt, va_list args, void *closure);
void *__Z_debug_print_closure;
#endif
#define min(a,b) ((a)<(b)?(a):(b))
static int Z_AddField(char **ptr, char *field, char *end);
static int find_or_insert_uid(ZUnique_Id_t *uid, ZNotice_Kind_t kind);
static Code_t Z_ZcodeFormatRawHeader(ZNotice_t *, char *, int, int *, char **,
int *, char **, char **, int cksumtype);
/* Find or insert uid in the old uids buffer. The buffer is a sorted
* circular queue. We make the assumption that most packets arrive in
* order, so we can usually search for a uid or insert it into the buffer
* by looking back just a few entries from the end. Since this code is
* only executed by the client, the implementation isn't microoptimized. */
static int
find_or_insert_uid(ZUnique_Id_t *uid,
ZNotice_Kind_t kind)
{
static struct _filter {
ZUnique_Id_t uid;
ZNotice_Kind_t kind;
time_t t;
} *buffer;
static long size;
static long start;
static long num;
time_t now;
struct _filter *new;
long i, j, new_size;
int result;
/* Initialize the uid buffer if it hasn't been done already. */
if (!buffer) {
size = Z_INITFILTERSIZE;
buffer = (struct _filter *) malloc(size * sizeof(*buffer));
if (!buffer)
return 0;
}
/* Age the uid buffer, discarding any uids older than the clock skew. */
time(&now);
while (num && (now - buffer[start % size].t) > CLOCK_SKEW)
start++, num--;
start %= size;
/* Make room for a new uid, since we'll probably have to insert one. */
if (num == size) {
new_size = size * 2 + 2;
new = (struct _filter *) malloc(new_size * sizeof(*new));
if (!new)
return 0;
for (i = 0; i < num; i++)
new[i] = buffer[(start + i) % size];
free(buffer);
buffer = new;
size = new_size;
start = 0;
}
/* Search for this uid in the buffer, starting from the end. */
for (i = start + num - 1; i >= start; i--) {
result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid));
if (result == 0 && buffer[i % size].kind == kind)
return 1;
if (result > 0)
break;
}
/* We didn't find it; insert the uid into the buffer after i. */
i++;
for (j = start + num; j > i; j--)
buffer[j % size] = buffer[(j - 1) % size];
buffer[i % size].uid = *uid;
buffer[i % size].kind = kind;
buffer[i % size].t = now;
num++;
return 0;
}
/* Return 1 if there is a packet waiting, 0 otherwise */
int
Z_PacketWaiting(void)
{
struct timeval tv;
fd_set read;
tv.tv_sec = tv.tv_usec = 0;
FD_ZERO(&read);
FD_SET(ZGetFD(), &read);
return (select(ZGetFD() + 1, &read, NULL, NULL, &tv));
}
/* Wait for a complete notice to become available */
Code_t
Z_WaitForComplete(void)
{
Code_t retval;
if (__Q_CompleteLength)
return (Z_ReadEnqueue());
while (!__Q_CompleteLength)
if ((retval = Z_ReadWait()) != ZERR_NONE)
return (retval);
return (ZERR_NONE);
}
/* Read any available packets and enqueue them */
Code_t
Z_ReadEnqueue(void)
{
Code_t retval;
if (ZGetFD() < 0)
return (ZERR_NOPORT);
while (Z_PacketWaiting())
if ((retval = Z_ReadWait()) != ZERR_NONE)
return (retval);
return (ZERR_NONE);
}
/*
* Search the queue for a notice with the proper multiuid - remove any
* notices that haven't been touched in a while
*/
struct _Z_InputQ *
Z_SearchQueue(ZUnique_Id_t *uid,
ZNotice_Kind_t kind)
{
register struct _Z_InputQ *qptr;
struct _Z_InputQ *next;
struct timeval tv;
(void) gettimeofday(&tv, (struct timezone *)0);
qptr = __Q_Head;
while (qptr) {
if (ZCompareUID(uid, &qptr->uid) && qptr->kind == kind)
return (qptr);
next = qptr->next;
if (qptr->timep && (qptr->timep+Z_NOTICETIMELIMIT < tv.tv_sec))
Z_RemQueue(qptr);
qptr = next;
}
return (NULL);
}
/*
* Now we delve into really convoluted queue handling and
* fragmentation reassembly algorithms and other stuff you probably
* don't want to look at...
*
* This routine does NOT guarantee a complete packet will be ready when it
* returns.
*/
Code_t
Z_ReadWait(void)
{
register struct _Z_InputQ *qptr;
ZNotice_t notice;
ZPacket_t packet;
struct sockaddr_in olddest, from;
int from_len, packet_len, zvlen, part, partof;
char *slash;
Code_t retval;
fd_set fds;
struct timeval tv;
if (ZGetFD() < 0)
return (ZERR_NOPORT);
FD_ZERO(&fds);
FD_SET(ZGetFD(), &fds);
tv.tv_sec = 60;
tv.tv_usec = 0;
if (select(ZGetFD() + 1, &fds, NULL, NULL, &tv) < 0)
return (errno);
if (!FD_ISSET(ZGetFD(), &fds))
return ETIMEDOUT;
from_len = sizeof(struct sockaddr_in);
packet_len = recvfrom(ZGetFD(), packet, sizeof(packet), 0,
(struct sockaddr *)&from, &from_len);
if (packet_len < 0)
return (errno);
if (!packet_len)
return (ZERR_EOF);
/* Ignore obviously non-Zephyr packets. */
zvlen = sizeof(ZVERSIONHDR) - 1;
if (packet_len < zvlen || memcmp(packet, ZVERSIONHDR, zvlen) != 0) {
Z_discarded_packets++;
return (ZERR_NONE);
}
/* Parse the notice */
if ((retval = ZParseNotice(packet, packet_len, ¬ice)) != ZERR_NONE)
return (retval);
/*
* If we're not a server and the notice is of an appropriate kind,
* send back a CLIENTACK to whoever sent it to say we got it.
*/
if (!__Zephyr_server) {
if (notice.z_kind != HMACK && notice.z_kind != SERVACK &&
notice.z_kind != SERVNAK && notice.z_kind != CLIENTACK) {
ZNotice_t tmpnotice;
ZPacket_t pkt;
int len;
tmpnotice = notice;
tmpnotice.z_kind = CLIENTACK;
tmpnotice.z_message_len = 0;
olddest = __HM_addr;
__HM_addr = from;
if ((retval = ZFormatSmallRawNotice(&tmpnotice, pkt, &len))
!= ZERR_NONE)
return(retval);
if ((retval = ZSendPacket(pkt, len, 0)) != ZERR_NONE)
return (retval);
__HM_addr = olddest;
}
if (find_or_insert_uid(¬ice.z_uid, notice.z_kind))
return(ZERR_NONE);
/* Check authentication on the notice. */
notice.z_checked_auth = ZCheckAuthentication(¬ice, &from);
}
/*
* Parse apart the z_multinotice field - if the field is blank for
* some reason, assume this packet stands by itself.
*/
slash = strchr(notice.z_multinotice, '/');
if (slash) {
part = atoi(notice.z_multinotice);
partof = atoi(slash+1);
if (part > partof || partof == 0) {
part = 0;
partof = notice.z_message_len;
}
}
else {
part = 0;
partof = notice.z_message_len;
}
/* Too big a packet...just ignore it! */
if (partof > Z_MAXNOTICESIZE)
return (ZERR_NONE);
/*
* If we aren't a server and we can find a notice in the queue
* with the same multiuid field, insert the current fragment as
* appropriate.
*/
switch (notice.z_kind) {
case SERVACK:
case SERVNAK:
/* The SERVACK and SERVNAK replies shouldn't be reassembled
(they have no parts). Instead, we should hold on to the reply
ONLY if it's the first part of a fragmented message, i.e.
multi_uid == uid. This allows programs to wait for the uid
of the first packet, and get a response when that notice
arrives. Acknowledgements of the other fragments are discarded
(XXX we assume here that they all carry the same information
regarding failure/success)
*/
if (!__Zephyr_server &&
!ZCompareUID(¬ice.z_multiuid, ¬ice.z_uid))
/* they're not the same... throw away this packet. */
return(ZERR_NONE);
/* fall thru & process it */
default:
/* for HMACK types, we assume no packet loss (local loopback
connections). The other types can be fragmented and MUST
run through this code. */
if (!__Zephyr_server && (qptr = Z_SearchQueue(¬ice.z_multiuid,
notice.z_kind))) {
/*
* If this is the first fragment, and we haven't already
* gotten a first fragment, grab the header from it.
*/
if (part == 0 && !qptr->header) {
qptr->header_len = packet_len-notice.z_message_len;
qptr->header = (char *) malloc((unsigned) qptr->header_len);
if (!qptr->header)
return (ENOMEM);
(void) memcpy(qptr->header, packet, qptr->header_len);
}
return (Z_AddNoticeToEntry(qptr, ¬ice, part));
}
}
/*
* We'll have to create a new entry...make sure the queue isn't
* going to get too big.
*/
if (__Q_Size+(__Zephyr_server ? notice.z_message_len : partof) > Z_MAXQUEUESIZE)
return (ZERR_NONE);
/*
* This is a notice we haven't heard of, so create a new queue
* entry for it and zero it out.
*/
qptr = (struct _Z_InputQ *)malloc(sizeof(struct _Z_InputQ));
if (!qptr)
return (ENOMEM);
(void) memset((char *)qptr, 0, sizeof(struct _Z_InputQ));
/* Insert the entry at the end of the queue */
qptr->next = NULL;
qptr->prev = __Q_Tail;
if (__Q_Tail)
__Q_Tail->next = qptr;
__Q_Tail = qptr;
if (!__Q_Head)
__Q_Head = qptr;
/* Copy the from field, multiuid, kind, and checked authentication. */
qptr->from = from;
qptr->uid = notice.z_multiuid;
qptr->kind = notice.z_kind;
qptr->auth = notice.z_checked_auth;
/*
* If this is the first part of the notice, we take the header
* from it. We only take it if this is the first fragment so that
* the Unique ID's will be predictable.
*
* If a Zephyr Server, we always take the header.
*/
if (__Zephyr_server || part == 0) {
qptr->header_len = packet_len-notice.z_message_len;
qptr->header = (char *) malloc((unsigned) qptr->header_len);
if (!qptr->header)
return ENOMEM;
(void) memcpy(qptr->header, packet, qptr->header_len);
}
/*
* If this is not a fragmented notice, then don't bother with a
* hole list.
* If we are a Zephyr server, all notices are treated as complete.
*/
if (__Zephyr_server || (part == 0 && notice.z_message_len == partof)) {
__Q_CompleteLength++;
qptr->holelist = (struct _Z_Hole *) 0;
qptr->complete = 1;
/* allocate a msg buf for this piece */
if (notice.z_message_len == 0)
qptr->msg = 0;
else if (!(qptr->msg = (char *) malloc((unsigned) notice.z_message_len)))
return(ENOMEM);
else
(void) memcpy(qptr->msg, notice.z_message, notice.z_message_len);
qptr->msg_len = notice.z_message_len;
__Q_Size += notice.z_message_len;
qptr->packet_len = qptr->header_len+qptr->msg_len;
if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
return (ENOMEM);
(void) memcpy(qptr->packet, qptr->header, qptr->header_len);
if(qptr->msg)
(void) memcpy(qptr->packet+qptr->header_len, qptr->msg,
qptr->msg_len);
return (ZERR_NONE);
}
/*
* We know how long the message is going to be (this is better
* than IP fragmentation...), so go ahead and allocate it all.
*/
if (!(qptr->msg = (char *) malloc((unsigned) partof)) && partof)
return (ENOMEM);
qptr->msg_len = partof;
__Q_Size += partof;
/*
* Well, it's a fragmented notice...allocate a hole list and
* initialize it to the full packet size. Then insert the
* current fragment.
*/
if (!(qptr->holelist = (struct _Z_Hole *)
malloc(sizeof(struct _Z_Hole))))
return (ENOMEM);
qptr->holelist->next = (struct _Z_Hole *) 0;
qptr->holelist->first = 0;
qptr->holelist->last = partof-1;
return (Z_AddNoticeToEntry(qptr, ¬ice, part));
}
/* Fragment management routines - compliments, more or less, of RFC815 */
Code_t
Z_AddNoticeToEntry(struct _Z_InputQ *qptr,
ZNotice_t *notice,
int part)
{
int last, oldfirst, oldlast;
struct _Z_Hole *hole, *lasthole;
struct timeval tv;
/* Bounds check. */
if (part < 0 || notice->z_message_len < 0 || part > qptr->msg_len
|| notice->z_message_len > qptr->msg_len - part)
return (ZERR_NONE);
/* Incorporate this notice's checked authentication. */
if (notice->z_checked_auth == ZAUTH_FAILED)
qptr->auth = ZAUTH_FAILED;
else if (notice->z_checked_auth == ZAUTH_NO && qptr->auth != ZAUTH_FAILED)
qptr->auth = ZAUTH_NO;
(void) gettimeofday(&tv, (struct timezone *)0);
qptr->timep = tv.tv_sec;
last = part+notice->z_message_len-1;
hole = qptr->holelist;
lasthole = (struct _Z_Hole *) 0;
/* copy in the message body */
(void) memcpy(qptr->msg+part, notice->z_message, notice->z_message_len);
/* Search for a hole that overlaps with the current fragment */
while (hole) {
if (part <= hole->last && last >= hole->first)
break;
lasthole = hole;
hole = hole->next;
}
/* If we found one, delete it and reconstruct a new hole */
if (hole) {
oldfirst = hole->first;
oldlast = hole->last;
if (lasthole)
lasthole->next = hole->next;
else
qptr->holelist = hole->next;
free((char *)hole);
/*
* Now create a new hole that is the original hole without the
* current fragment.
*/
if (part > oldfirst) {
/* Search for the end of the hole list */
hole = qptr->holelist;
lasthole = (struct _Z_Hole *) 0;
while (hole) {
lasthole = hole;
hole = hole->next;
}
if (lasthole) {
if (!(lasthole->next = (struct _Z_Hole *)
malloc(sizeof(struct _Z_InputQ))))
return (ENOMEM);
hole = lasthole->next;
}
else {
if (!(qptr->holelist = (struct _Z_Hole *)
malloc(sizeof(struct _Z_InputQ))))
return (ENOMEM);
hole = qptr->holelist;
}
hole->next = NULL;
hole->first = oldfirst;
hole->last = part-1;
}
if (last < oldlast) {
/* Search for the end of the hole list */
hole = qptr->holelist;
lasthole = (struct _Z_Hole *) 0;
while (hole) {
lasthole = hole;
hole = hole->next;
}
if (lasthole) {
if (!(lasthole->next = (struct _Z_Hole *)
malloc(sizeof(struct _Z_InputQ))))
return (ENOMEM);
hole = lasthole->next;
}
else {
if (!(qptr->holelist = (struct _Z_Hole *)
malloc(sizeof(struct _Z_InputQ))))
return (ENOMEM);
hole = qptr->holelist;
}
hole->next = (struct _Z_Hole *) 0;
hole->first = last+1;
hole->last = oldlast;
}
}
if (!qptr->holelist) {
if (!qptr->complete)
__Q_CompleteLength++;
qptr->complete = 1;
qptr->timep = 0; /* don't time out anymore */
qptr->packet_len = qptr->header_len+qptr->msg_len;
if (!(qptr->packet = (char *) malloc((unsigned) qptr->packet_len)))
return (ENOMEM);
(void) memcpy(qptr->packet, qptr->header, qptr->header_len);
(void) memcpy(qptr->packet+qptr->header_len, qptr->msg,
qptr->msg_len);
}
return (ZERR_NONE);
}
void
Z_gettimeofday(struct _ZTimeval *ztv,
struct timezone *tz)
{
struct timeval tv;
(void) gettimeofday(&tv, tz); /* yeah, yeah, I know */
ztv->tv_sec=tv.tv_sec;
ztv->tv_usec=tv.tv_usec;
}
Code_t
Z_FormatHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *len,
Z_AuthProc cert_routine)
{
Code_t retval;
static char version[BUFSIZ]; /* default init should be all \0 */
struct sockaddr_in name;
int namelen = sizeof(name);
if (!notice->z_sender)
notice->z_sender = ZGetSender();
if (notice->z_port == 0) {
if (ZGetFD() < 0) {
retval = ZOpenPort((u_short *)0);
if (retval != ZERR_NONE)
return (retval);
}
retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen);
if (retval != 0)
return (retval);
notice->z_port = name.sin_port;
}
notice->z_multinotice = "";
(void) Z_gettimeofday(¬ice->z_uid.tv, (struct timezone *)0);
notice->z_uid.tv.tv_sec = htonl((u_long) notice->z_uid.tv.tv_sec);
notice->z_uid.tv.tv_usec = htonl((u_long) notice->z_uid.tv.tv_usec);
(void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
notice->z_multiuid = notice->z_uid;
if (!version[0])
(void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
ZVERSIONMINOR);
notice->z_version = version;
return Z_FormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);
}
Code_t
Z_NewFormatHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *len,
Z_AuthProc cert_routine)
{
Code_t retval;
static char version[BUFSIZ]; /* default init should be all \0 */
struct sockaddr_in name;
struct timeval tv;
int namelen = sizeof(name);
if (!notice->z_sender)
notice->z_sender = ZGetSender();
if (notice->z_port == 0) {
if (ZGetFD() < 0) {
retval = ZOpenPort((u_short *)0);
if (retval != ZERR_NONE)
return (retval);
}
retval = getsockname(ZGetFD(), (struct sockaddr *) &name, &namelen);
if (retval != 0)
return (retval);
notice->z_port = name.sin_port;
}
notice->z_multinotice = "";
(void) gettimeofday(&tv, (struct timezone *)0);
notice->z_uid.tv.tv_sec = htonl((u_long) tv.tv_sec);
notice->z_uid.tv.tv_usec = htonl((u_long) tv.tv_usec);
(void) memcpy(¬ice->z_uid.zuid_addr, &__My_addr, sizeof(__My_addr));
notice->z_multiuid = notice->z_uid;
if (!version[0])
(void) sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
ZVERSIONMINOR);
notice->z_version = version;
return Z_NewFormatAuthHeader(notice, buffer, buffer_len, len, cert_routine);
}
Code_t
Z_FormatAuthHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *len,
Z_AuthProc cert_routine)
{
if (!cert_routine) {
notice->z_auth = 0;
notice->z_authent_len = 0;
notice->z_ascii_authent = "";
notice->z_checksum = 0;
return (Z_FormatRawHeader(notice, buffer, buffer_len,
len, NULL, NULL));
}
return ((*cert_routine)(notice, buffer, buffer_len, len));
}
Code_t
Z_NewFormatAuthHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *len,
Z_AuthProc cert_routine)
{
if (!cert_routine) {
notice->z_auth = 0;
notice->z_authent_len = 0;
notice->z_ascii_authent = "";
notice->z_checksum = 0;
return (Z_FormatRawHeader(notice, buffer, buffer_len,
len, NULL, NULL));
}
return ((*cert_routine)(notice, buffer, buffer_len, len));
}
Code_t
Z_NewFormatRawHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *hdr_len,
char **cksum_start,
int *cksum_len,
char **cstart,
char **cend)
{
return(Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len,
cksum_start, cksum_len, cstart, cend, 0));
}
Code_t
Z_AsciiFormatRawHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *hdr_len,
char **cksum_start,
int *cksum_len,
char **cstart,
char **cend)
{
return(Z_ZcodeFormatRawHeader(notice, buffer, buffer_len, hdr_len,
cksum_start, cksum_len, cstart, cend, 1));
}
static Code_t
Z_ZcodeFormatRawHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *hdr_len,
char **cksum_start,
int *cksum_len,
char **cstart,
char **cend,
int cksumstyle)
{
static char version_nogalaxy[BUFSIZ]; /* default init should be all \0 */
char newrecip[BUFSIZ];
char *ptr, *end;
int i;
if (!notice->z_class)
notice->z_class = "";
if (!notice->z_class_inst)
notice->z_class_inst = "";
if (!notice->z_opcode)
notice->z_opcode = "";
if (!notice->z_recipient)
notice->z_recipient = "";
if (!notice->z_default_format)
notice->z_default_format = "";
ptr = buffer;
end = buffer+buffer_len;
if (cksum_start)
*cksum_start = ptr;
(void) sprintf(version_nogalaxy, "%s%d.%d", ZVERSIONHDR,
ZVERSIONMAJOR, ZVERSIONMINOR);
notice->z_version = version_nogalaxy;
if (Z_AddField(&ptr, version_nogalaxy, end))
return (ZERR_HEADERLEN);
if (ZMakeAscii32(ptr, end-ptr,
Z_NUMFIELDS + notice->z_num_other_fields)
== ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid,
sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (Z_AddField(&ptr, notice->z_ascii_authent, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_class, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_class_inst, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_opcode, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_sender, end))
return (ZERR_HEADERLEN);
if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) {
if (Z_AddField(&ptr, notice->z_recipient, end))
return (ZERR_HEADERLEN);
}
else {
if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 >
sizeof(newrecip))
return (ZERR_HEADERLEN);
(void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm);
if (Z_AddField(&ptr, newrecip, end))
return (ZERR_HEADERLEN);
}
if (Z_AddField(&ptr, notice->z_default_format, end))
return (ZERR_HEADERLEN);
/* copy back the end pointer location for crypto checksum */
if (cstart)
*cstart = ptr;
if (cksumstyle == 1) {
if (Z_AddField(&ptr, notice->z_ascii_checksum, end))
return (ZERR_HEADERLEN);
} else {
#ifdef xZCODE_K4SUM
if (ZMakeZcode32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
return ZERR_HEADERLEN;
#else
if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
#endif
ptr += strlen(ptr)+1;
}
if (cend)
*cend = ptr;
if (Z_AddField(&ptr, notice->z_multinotice, end))
return (ZERR_HEADERLEN);
if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid,
sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
for (i=0;i<notice->z_num_other_fields;i++)
if (Z_AddField(&ptr, notice->z_other_fields[i], end))
return (ZERR_HEADERLEN);
if (cksum_len)
*cksum_len = ptr-*cksum_start;
*hdr_len = ptr-buffer;
return (ZERR_NONE);
}
Code_t
Z_FormatRawHeader(ZNotice_t *notice,
char *buffer,
int buffer_len,
int *len,
char **cstart,
char **cend)
{
char newrecip[BUFSIZ];
char *ptr, *end;
int i;
if (!notice->z_class)
notice->z_class = "";
if (!notice->z_class_inst)
notice->z_class_inst = "";
if (!notice->z_opcode)
notice->z_opcode = "";
if (!notice->z_recipient)
notice->z_recipient = "";
if (!notice->z_default_format)
notice->z_default_format = "";
ptr = buffer;
end = buffer+buffer_len;
if (buffer_len < strlen(notice->z_version)+1)
return (ZERR_HEADERLEN);
(void) strcpy(ptr, notice->z_version);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, Z_NUMFIELDS + notice->z_num_other_fields)
== ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, notice->z_kind) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_uid,
sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii16(ptr, end-ptr, ntohs(notice->z_port)) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, notice->z_auth) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (ZMakeAscii32(ptr, end-ptr, notice->z_authent_len) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (Z_AddField(&ptr, notice->z_ascii_authent, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_class, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_class_inst, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_opcode, end))
return (ZERR_HEADERLEN);
if (Z_AddField(&ptr, notice->z_sender, end))
return (ZERR_HEADERLEN);
if (strchr(notice->z_recipient, '@') || !*notice->z_recipient) {
if (Z_AddField(&ptr, notice->z_recipient, end))
return (ZERR_HEADERLEN);
}
else {
if (strlen(notice->z_recipient) + strlen(__Zephyr_realm) + 2 >
sizeof(newrecip))
return (ZERR_HEADERLEN);
(void) sprintf(newrecip, "%s@%s", notice->z_recipient, __Zephyr_realm);
if (Z_AddField(&ptr, newrecip, end))
return (ZERR_HEADERLEN);
}
if (Z_AddField(&ptr, notice->z_default_format, end))
return (ZERR_HEADERLEN);
/* copy back the end pointer location for crypto checksum */
if (cstart)
*cstart = ptr;
if (ZMakeAscii32(ptr, end-ptr, notice->z_checksum) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
if (cend)
*cend = ptr;
if (Z_AddField(&ptr, notice->z_multinotice, end))
return (ZERR_HEADERLEN);
if (ZMakeAscii(ptr, end-ptr, (unsigned char *)¬ice->z_multiuid,
sizeof(ZUnique_Id_t)) == ZERR_FIELDLEN)
return (ZERR_HEADERLEN);
ptr += strlen(ptr)+1;
for (i=0;i<notice->z_num_other_fields;i++)
if (Z_AddField(&ptr, notice->z_other_fields[i], end))
return (ZERR_HEADERLEN);
*len = ptr-buffer;
return (ZERR_NONE);
}
static int
Z_AddField(char **ptr,
char *field,
char *end)
{
register int len;
len = field ? strlen (field) + 1 : 1;
if (*ptr+len > end)
return 1;
if (field)
(void) strcpy(*ptr, field);
else
**ptr = '\0';
*ptr += len;
return 0;
}
struct _Z_InputQ *
Z_GetFirstComplete(void)
{
struct _Z_InputQ *qptr;
qptr = __Q_Head;
while (qptr) {
if (qptr->complete)
return (qptr);
qptr = qptr->next;
}
return ((struct _Z_InputQ *)0);
}
struct _Z_InputQ *
Z_GetNextComplete(struct _Z_InputQ *qptr)
{
qptr = qptr->next;
while (qptr) {
if (qptr->complete)
return (qptr);
qptr = qptr->next;
}
return ((struct _Z_InputQ *)0);
}
void
Z_RemQueue(struct _Z_InputQ *qptr)
{
struct _Z_Hole *hole, *nexthole;
if (qptr->complete)
__Q_CompleteLength--;
__Q_Size -= qptr->msg_len;
if (qptr->header)
free(qptr->header);
if (qptr->msg)
free(qptr->msg);
if (qptr->packet)
free(qptr->packet);
hole = qptr->holelist;
while (hole) {
nexthole = hole->next;
free((char *)hole);
hole = nexthole;
}
if (qptr == __Q_Head && __Q_Head == __Q_Tail) {
free ((char *)qptr);
__Q_Head = (struct _Z_InputQ *)0;
__Q_Tail = (struct _Z_InputQ *)0;
return;
}
if (qptr == __Q_Head) {
__Q_Head = qptr->next;
__Q_Head->prev = (struct _Z_InputQ *)0;
free ((char *)qptr);
return;
}
if (qptr == __Q_Tail) {
__Q_Tail = qptr->prev;
__Q_Tail->next = (struct _Z_InputQ *)0;
free ((char *)qptr);
return;
}
qptr->prev->next = qptr->next;
qptr->next->prev = qptr->prev;
free ((char *)qptr);
return;
}
Code_t
Z_SendFragmentedNotice(ZNotice_t *notice,
int len,
Z_AuthProc cert_func,
Z_SendProc send_func)
{
ZNotice_t partnotice;
ZPacket_t buffer;
char multi[64];
int offset, hdrsize, fragsize, ret_len, message_len, waitforack;
Code_t retval;
hdrsize = len-notice->z_message_len;
fragsize = Z_MAXPKTLEN-hdrsize-Z_FRAGFUDGE;
offset = 0;
waitforack = ((notice->z_kind == UNACKED || notice->z_kind == ACKED)
&& !__Zephyr_server);
partnotice = *notice;
while (offset < notice->z_message_len || !notice->z_message_len) {
(void) sprintf(multi, "%d/%d", offset, notice->z_message_len);
partnotice.z_multinotice = multi;
if (offset > 0) {
(void) Z_gettimeofday(&partnotice.z_uid.tv,
(struct timezone *)0);
partnotice.z_uid.tv.tv_sec =
htonl((u_long) partnotice.z_uid.tv.tv_sec);
partnotice.z_uid.tv.tv_usec =
htonl((u_long) partnotice.z_uid.tv.tv_usec);
(void) memcpy((char *)&partnotice.z_uid.zuid_addr, &__My_addr,
sizeof(__My_addr));
}
message_len = min(notice->z_message_len-offset, fragsize);
partnotice.z_message = notice->z_message+offset;
partnotice.z_message_len = message_len;
if ((retval = Z_FormatAuthHeader(&partnotice, buffer, Z_MAXHEADERLEN,
&ret_len, cert_func)) != ZERR_NONE) {
return (retval);
}
memcpy(buffer + ret_len, partnotice.z_message, message_len);
if ((retval = (*send_func)(&partnotice, buffer, ret_len+message_len,
waitforack)) != ZERR_NONE) {
return (retval);
}
offset += fragsize;
if (!notice->z_message_len)
break;
}
return (ZERR_NONE);
}
/*ARGSUSED*/
Code_t Z_XmitFragment(ZNotice_t *notice,
char *buf,
int len,
int wait)
{
return(ZSendPacket(buf, len, wait));
}
#ifdef Z_DEBUG
/* For debugging printing */
const char *const ZNoticeKinds[] = {
"UNSAFE", "UNACKED", "ACKED", "HMACK", "HMCTL", "SERVACK", "SERVNAK",
"CLIENTACK", "STAT"
};
#endif
#ifdef Z_DEBUG
#undef Z_debug
void
Z_debug(const char *format, ...)
{
va_list pvar;
if (!__Z_debug_print)
return;
va_start (pvar, format);
(*__Z_debug_print) (format, pvar, __Z_debug_print_closure);
va_end (pvar);
}
void
Z_debug_stderr(const char *format,
va_list args,
void *closure)
{
#ifdef HAVE_VPRINTF
vfprintf (stderr, format, args);
#else
_doprnt (format, args, stderr);
#endif
putc ('\n', stderr);
}
#undef ZGetFD
int ZGetFD (void) { return __Zephyr_fd; }
#undef ZQLength
int ZQLength (void) { return __Q_CompleteLength; }
#undef ZGetDestAddr
struct sockaddr_in ZGetDestAddr (void) { return __HM_addr; }
#undef ZGetRealm
Zconst char * ZGetRealm (void) { return __Zephyr_realm; }
#undef ZSetDebug
void
ZSetDebug(void (*proc) __P((const char *, va_list, void *)),
char *arg)
{
__Z_debug_print = proc;
__Z_debug_print_closure = arg;
}
#endif /* Z_DEBUG */
#ifdef HAVE_KRB5
Code_t
Z_Checksum(krb5_data *cksumbuf,
krb5_keyblock *keyblock,
krb5_cksumtype cksumtype,
char **asn1_data,
int *asn1_len)
{
krb5_error_code result;
char *data;
int len;
#if HAVE_KRB5_C_MAKE_CHECKSUM
krb5_checksum checksum;
#else
Checksum checksum;
krb5_crypto cryptctx;
#endif
#if HAVE_KRB5_C_MAKE_CHECKSUM
/* Create the checksum -- MIT crypto API */
result = krb5_c_make_checksum(Z_krb5_ctx, cksumtype,
keyblock, Z_KEYUSAGE_CLT_CKSUM,
cksumbuf, &checksum);
if (result)
return result;
/* HOLDING: checksum */
data = checksum.contents;
len = checksum.length;
#else
/* Create the checksum -- heimdal crypto API */
result = krb5_crypto_init(Z_krb5_ctx, keyblock, keyblock->keytype,
&cryptctx);
if (result)
return result;
/* HOLDING: cryptctx */
result = krb5_create_checksum(Z_krb5_ctx, cryptctx,
Z_KEYUSAGE_CLT_CKSUM, cksumtype,
cksumbuf->data, cksumbuf->length,
&checksum);
krb5_crypto_destroy(Z_krb5_ctx, cryptctx);
if (result)
return result;
len = checksum.checksum.length;
data = checksum.checksum.data;
/* HOLDING: checksum */
#endif
*asn1_data = malloc(len);
if (*asn1_data == NULL)
return errno;
memcpy(*asn1_data, data, len);
*asn1_len = len;
#if HAVE_KRB5_C_MAKE_CHECKSUM
krb5_free_checksum_contents(Z_krb5_ctx, &checksum);
#else
free_Checksum(&checksum);
#endif
return 0;
}
Code_t
Z_InsertZcodeChecksum(krb5_keyblock *keyblock,
ZNotice_t *notice,
char *buffer,
char *cksum_start,
int cksum_len,
char *cstart,
char *cend,
int buffer_len,
int *length_adjust)
{
int plain_len; /* length of part not to be checksummed */
int cksum0_len; /* length of part before checksum */
int cksum1_len; /* length of part after checksum */
krb5_data cksumbuf;
krb5_data cksum;
char *key_data;
int key_len;
krb5_enctype enctype;
krb5_cksumtype cksumtype;
Code_t result;
key_data = Z_keydata(keyblock);
key_len = Z_keylen(keyblock);
result = Z_ExtractEncCksum(keyblock, &enctype, &cksumtype);
if (result)
return (ZAUTH_FAILED);
/* Assemble the things to be checksummed */
plain_len = cksum_start - buffer;
cksum0_len = cstart - cksum_start;
cksum1_len = (cksum_start + cksum_len) - cend;
memset(&cksumbuf, 0, sizeof(cksumbuf));
cksumbuf.length = cksum0_len + cksum1_len + notice->z_message_len;
cksumbuf.data = malloc(cksumbuf.length);
if (!cksumbuf.data)
return ENOMEM;
memcpy(cksumbuf.data, cksum_start, cksum0_len);
memcpy(cksumbuf.data + cksum0_len, cend, cksum1_len);
memcpy(cksumbuf.data + cksum0_len + cksum1_len,
notice->z_message, notice->z_message_len);
/* compute the checksum */
result = Z_Checksum(&cksumbuf, keyblock, cksumtype,
(char **)&cksum.data, &cksum.length);
if (result) {
free(cksumbuf.data);
return result;
}
/*
* OK.... we can zcode to a space starting at 'cstart',
* with a length of buffer_len - (plain_len + cksum_len).
* Then we tack on the end part, which is located at
* cksumbuf.data + cksum0_len and has length cksum1_len
*/
result = ZMakeZcode(cstart, buffer_len - (plain_len + cksum_len),
cksum.data, cksum.length);
free(cksum.data);
if (!result) {
int zcode_len = strlen(cstart) + 1;
memcpy(cstart + zcode_len, cksumbuf.data + cksum0_len, cksum1_len);
*length_adjust = zcode_len - cksum_len + (cksum0_len + cksum1_len);
}
free(cksumbuf.data);
return result;
}
Code_t
Z_ExtractEncCksum(krb5_keyblock *keyblock,
krb5_enctype *enctype,
krb5_cksumtype *cksumtype)
{
*enctype = Z_enctype(keyblock);
return Z_krb5_lookup_cksumtype(*enctype, cksumtype);
}
#endif
#ifdef HAVE_KRB5
/* returns 0 if invalid or losing, 1 if valid, *sigh* */
int
Z_krb5_verify_cksum(krb5_keyblock *keyblock,
krb5_data *cksumbuf,
krb5_cksumtype cksumtype,
char *asn1_data,
int asn1_len)
{
krb5_error_code result;
#if HAVE_KRB5_C_MAKE_CHECKSUM
krb5_checksum checksum;
krb5_boolean valid;
#else
krb5_crypto cryptctx;
Checksum checksum;
size_t xlen;
#endif
memset(&checksum, 0, sizeof(checksum));
#if HAVE_KRB5_C_MAKE_CHECKSUM
/* Verify the checksum -- MIT crypto API */
checksum.length = asn1_len;
checksum.contents = asn1_data;
checksum.checksum_type = cksumtype;
result = krb5_c_verify_checksum(Z_krb5_ctx,
keyblock, Z_KEYUSAGE_SRV_CKSUM,
cksumbuf, &checksum, &valid);
if (!result && valid)
return 1;
else
return 0;
#else
checksum.checksum.length = asn1_len;
checksum.checksum.data = asn1_data;
checksum.cksumtype = cksumtype;
result = krb5_crypto_init(Z_krb5_ctx, keyblock, keyblock->keytype, &cryptctx);
if (result)
return result;
/* HOLDING: cryptctx */
result = krb5_verify_checksum(Z_krb5_ctx, cryptctx,
Z_KEYUSAGE_SRV_CKSUM,
cksumbuf->data, cksumbuf->length,
&checksum);
krb5_crypto_destroy(Z_krb5_ctx, cryptctx);
if (result)
return 0;
else
return 1;
#endif
}
#endif
|