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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 1997 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <thread.h>
#include <synch.h>
#include <stdlib.h>
#include <syslog.h>
#include <rpc/des_crypt.h>
#include "keyserv_cache.h"
struct cachekey {
struct cachekey_header *ch;
keylen_t keylen;
algtype_t algtype;
mutex_t mp;
struct cachekey *next;
};
static struct cachekey *cache = 0;
static mutex_t cache_lock = DEFAULTMUTEX;
static cond_t cache_cv = DEFAULTCV;
static u_long cache_refcnt = 0;
struct skck {
des_block common[3];
des_block verifier; /* Checksum */
struct dhkey secret;
};
struct cachekey_disklist {
uid_t uid;
struct cachekey_disklist *prev; /* LRU order */
struct cachekey_disklist *next;
struct cachekey_disklist *prevhash; /* Hash chain */
struct cachekey_disklist *nexthash;
struct dhkey public;
/*
* Storage for encrypted skck structure here. The length will be
* 8 * ( ( ( sizeof(struct skck) - 1 + secret.length ) - 1 ) / 8 + 1 )
*/
};
/* Length of skck structure for given key length (in bits) */
#define SKCK_LEN(keylen) ALIGN8(sizeof (struct skck) + KEYLEN(keylen))
/* Length of a cachekey_disklist record for given key length (in bits) */
#define CACHEKEY_RECLEN(keylen) ALIGN8(sizeof (struct cachekey_disklist) - 1 + \
KEYLEN(keylen) + SKCK_LEN(keylen))
#define NUMHASHBUCKETS 253
#define CHUNK_NUMREC 64
#define CACHEKEY_HEADER_VERSION 0
struct cachekey_header { /* First in each key cache file */
u_int version; /* version number of interface */
u_int headerlength; /* size of this header */
keylen_t keylen; /* in bits */
algtype_t algtype; /* algorithm type */
size_t reclength; /* cache file record size in bytes */
int fd; /* file descriptor */
caddr_t address; /* mmap()ed here */
size_t length; /* bytes mapped */
size_t maxsize; /* don't grow beyond this */
u_int inuse_count;
struct cachekey_disklist *inuse; /* LRU order */
struct cachekey_disklist *inuse_end;
u_int free_count;
struct cachekey_disklist *free;
struct cachekey_disklist *bucket[NUMHASHBUCKETS];
struct cachekey_disklist array[1]; /* Start of array */
};
static struct cachekey_header *create_cache_file_ch(keylen_t keylen,
algtype_t algtype,
int sizespec);
static struct cachekey_header *remap_cache_file_ch(struct cachekey_header *ch,
u_int newrecs);
static struct cachekey_header *cache_insert_ch(struct cachekey_header *ch,
uid_t uid, deskeyarray common,
des_block key,
keybuf3 *public,
keybuf3 *secret);
static struct cachekey3_list *cache_retrieve_ch(struct cachekey_header *ch,
uid_t uid,
keybuf3 *public,
des_block key);
static int cache_remove_ch(struct cachekey_header *ch,
uid_t uid,
keybuf3 *public);
static struct cachekey *get_cache_header(keylen_t keylen,
algtype_t algtype);
static void release_cache_header(struct cachekey *);
static int cache_remap_addresses_ch(
struct cachekey_header *);
static struct cachekey_disklist *find_cache_item(struct cachekey_header **,
uid_t, struct dhkey *);
static struct dhkey *keybuf3_2_dhkey(keybuf3 *);
static u_int hashval(uid_t);
static void list_remove(struct cachekey_disklist *,
struct cachekey_disklist **,
struct cachekey_disklist **,
u_int *);
static void list_remove_hash(struct cachekey_disklist *,
struct cachekey_disklist **,
struct cachekey_disklist **,
u_int *);
static void list_insert(struct cachekey_disklist *,
struct cachekey_disklist **,
struct cachekey_disklist **,
u_int *);
static void list_insert_hash(struct cachekey_disklist *,
struct cachekey_disklist **,
struct cachekey_disklist **,
u_int *);
static struct cachekey3_list * copy_cl_item(struct cachekey_header *ch,
struct cachekey_disklist *cd,
des_block key);
extern int hex2bin(u_char *, u_char *, int);
extern int bin2hex(u_char *, u_char *, int);
/*
* The folowing set of macros implement address validity checking. A valid
* address is defined to be either 0, or to fall on a record boundary. In
* the latter case, the the difference between the address and the start of
* the record array is divisible by the record length.
*/
#define FILEOFFSET(ckh) ((u_long)(ckh) - \
(u_long)((ckh)->address))
#define ADJUSTEDADDR(addr, ckh) ((u_long)(addr) + FILEOFFSET(ckh))
#define ARRAYOFFSET(addr, ckh) (ADJUSTEDADDR(addr, ckh) - \
(u_long)&((ckh)->array[0]))
#define INVALID_ADDRESS(addr, ckh) ((addr == 0) ? 0 : \
(ARRAYOFFSET(addr, ckh) % (ckh)->reclength) != 0)
/* Add offset to old address */
#define MOVE_ADDR(old, offset) ((old) == 0) ? 0 : \
(void *)((u_long)(old) + (offset))
/* Number of records in use or on free list */
#define NUMRECS(ck_header) ((ck_header)->inuse_count + \
(ck_header)->free_count)
/* Max number of records the mapped file could hold */
#define MAPRECS(ck_header) (((ck_header)->length - \
sizeof (struct cachekey_header)) / \
(ck_header)->reclength)
/* Max number of records the file will hold if extended to the maxsize */
#define MAXRECS(ck_header) (((ck_header)->maxsize - \
sizeof (struct cachekey_header)) / \
(ck_header)->reclength)
struct cachekey_header *
create_cache_file_ch(keylen_t keylen, algtype_t algtype, int sizespec)
{
char filename[MAXPATHLEN];
struct cachekey_header *ch;
int fd, newfile = 0, i, checkvalid = 1;
struct stat statbuf;
size_t reclength, length;
struct cachekey_header *oldbase = 0;
struct cachekey_disklist *cd;
size_t maxsize;
/* Construct cache file name */
if (snprintf(filename, sizeof (filename), "/var/nis/.keyserv_%d-%d",
keylen, algtype) > sizeof (filename)) {
syslog(LOG_WARNING,
"error constructing file name for mech %d-%d", keylen, algtype);
return (0);
}
/* Open/create the file */
if ((fd = open(filename, O_RDWR|O_CREAT, 0600)) < 0) {
syslog(LOG_WARNING, "cache file open error for mech %d-%d: %m",
keylen, algtype);
return (0);
}
/* We want exclusive use of the file */
if (lockf(fd, F_LOCK, 0) < 0) {
syslog(LOG_WARNING, "cache file lock error for mech %d-%d: %m",
keylen, algtype);
close(fd);
return (0);
}
/* Zero size means a new file */
if (fstat(fd, &statbuf) < 0) {
syslog(LOG_WARNING, "cache file fstat error for mech %d-%d: %m",
keylen, algtype);
close(fd);
return (0);
}
reclength = CACHEKEY_RECLEN(keylen);
if (sizespec < 0) {
/* specifies the number of records in file */
maxsize = ALIGN8(sizeof (struct cachekey_header)) +
-sizespec*reclength;
} else {
/* specifies size of file in MB */
maxsize = sizespec*1024*1024;
}
length = ALIGN8(sizeof (struct cachekey_header)) +
reclength*CHUNK_NUMREC;
if (length > maxsize) {
/*
* First record resides partly in the header, so the length
* cannot be allowed to be less than header plus one record.
*/
if (maxsize > ALIGN8(sizeof (struct cachekey_header)+reclength))
length = maxsize;
else {
length = ALIGN8(sizeof (struct cachekey_header)+
reclength);
maxsize = length;
}
}
if (statbuf.st_size == 0) {
/* Extend the file if we just created it */
if (ftruncate(fd, length) < 0) {
syslog(LOG_WARNING,
"cache file ftruncate error for mech %d-%d: %m",
keylen, algtype);
close(fd);
return (0);
}
newfile = 1;
} else {
/*
* Temporarily mmap the header, to sanity check and obtain
* the address where it was mapped the last time.
*/
if ((ch = (void *)mmap(0, sizeof (struct cachekey_header),
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) ==
MAP_FAILED) {
syslog(LOG_WARNING,
"cache file mmap1 error for mech %d-%d: %m",
keylen, algtype);
close(fd);
return (0);
}
if (ch->version != CACHEKEY_HEADER_VERSION ||
ch->headerlength != sizeof (struct cachekey_header) ||
ch->keylen != keylen ||
ch->algtype != algtype ||
ch->reclength != reclength ||
ch->length < sizeof (struct cachekey_header) ||
ch->maxsize < ch->length ||
INVALID_ADDRESS(ch->inuse, ch) ||
INVALID_ADDRESS(ch->free, ch)) {
syslog(LOG_WARNING,
"cache file consistency error for mech %d-%d",
keylen, algtype);
munmap((caddr_t)ch, sizeof (struct cachekey_header));
close(fd);
return (0);
}
oldbase = (void *)ch->address;
length = ch->length;
if (munmap((caddr_t)ch, sizeof (struct cachekey_header)) < 0) {
syslog(LOG_WARNING,
"cache file munmap error for mech %d-%d: %m",
keylen, algtype);
close(fd);
return (0);
}
}
/* Map the file */
if ((ch = (void *)mmap((caddr_t)oldbase, length,
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
syslog(LOG_WARNING,
"cache file mmap2 error for mech %d-%d: %m",
keylen, algtype);
close(fd);
return (0);
}
ch->fd = fd;
ch->maxsize = maxsize;
if (newfile) {
ch->version = CACHEKEY_HEADER_VERSION;
ch->headerlength = sizeof (struct cachekey_header);
ch->keylen = keylen;
ch->algtype = algtype;
ch->reclength = reclength;
ch->length = length;
ch->address = (caddr_t)ch;
ch->inuse_count = 0;
ch->inuse = 0;
ch->inuse_end = 0;
ch->free = 0;
ch->free_count = 0;
for (i = 0; i < NUMHASHBUCKETS; i++) {
ch->bucket[i] = 0;
}
cd = &(ch->array[0]);
for (i = 0; i < MAPRECS(ch);
i++, cd = MOVE_ADDR(cd, ch->reclength)) {
cd->uid = (uid_t)-1;
cd->prev = MOVE_ADDR(cd, -(ch->reclength));
cd->next = MOVE_ADDR(cd, +(ch->reclength));
cd->prevhash = 0;
cd->nexthash = 0;
}
/*
* Last record next pointer, and first record prev pointer,
* are both NULL.
*/
cd = MOVE_ADDR(cd, -(ch->reclength));
cd->next = 0;
cd = &(ch->array[0]);
cd->prev = 0;
ch->free_count = MAPRECS(ch);
ch->free = &(ch->array[0]);
(void) msync((caddr_t)ch, ch->length, MS_SYNC);
} else if (ch->length > maxsize) {
/* File should shrink */
if ((ch = remap_cache_file_ch(ch, MAXRECS(ch))) == 0) {
return (0);
}
checkvalid = 0;
}
/*
* cache_remap_addresses() also checks address consistency, so call
* it even if the remap is a no-op. However, if we've called
* remap_cache_file_ch(), it will have invoked cache_remap_addresses()
* already, so we don't have to do that again.
*/
if (checkvalid &&
cache_remap_addresses_ch(ch) == 0) {
syslog(LOG_WARNING, "cache file invalid for mech %d-%d",
keylen, algtype);
(void) munmap((caddr_t)ch, ch->length);
close(fd);
return (0);
}
(void) msync((caddr_t)ch, ch->length, MS_SYNC);
return (ch);
}
static int
cache_remap_addresses_ch(struct cachekey_header *ch)
{
int i;
u_long offset;
struct cachekey_disklist *cd;
offset = (u_long)ch - (u_long)ch->address;
if (INVALID_ADDRESS(ch->inuse, ch) ||
INVALID_ADDRESS(ch->inuse_end, ch) ||
INVALID_ADDRESS(ch->free, ch)) {
return (0);
}
ch->inuse = MOVE_ADDR(ch->inuse, offset);
ch->inuse_end = MOVE_ADDR(ch->inuse_end, offset);
ch->free = MOVE_ADDR(ch->free, offset);
cd = &(ch->array[0]);
for (i = 0; i < NUMRECS(ch); i++) {
if (INVALID_ADDRESS(cd->prev, ch) ||
INVALID_ADDRESS(cd->next, ch) ||
INVALID_ADDRESS(cd->prevhash, ch) ||
INVALID_ADDRESS(cd->nexthash, ch)) {
return (0);
}
cd->prev = MOVE_ADDR(cd->prev, offset);
cd->next = MOVE_ADDR(cd->next, offset);
cd->prevhash = MOVE_ADDR(cd->prevhash, offset);
cd->nexthash = MOVE_ADDR(cd->nexthash, offset);
cd = MOVE_ADDR(cd, ch->reclength);
}
for (i = 0; i < NUMHASHBUCKETS; i++) {
if (INVALID_ADDRESS(ch->bucket[i], ch)) {
return (0);
}
ch->bucket[i] = MOVE_ADDR(ch->bucket[i], offset);
}
/*
* To prevent disaster if this function is invoked again, we
* update ch->address, so that offset will be zero if we do
* get called once more, and the mapped file hasn't moved.
*/
ch->address = (caddr_t)ch;
return (1);
}
/*
* Remap cache file with space for 'newrecs' records. The mmap:ed address
* may have to move; the new address is returned.
*/
static struct cachekey_header *
remap_cache_file_ch(struct cachekey_header *ch, u_int newrecs)
{
size_t newsize, oldsize;
u_int currecs;
int i, fd;
struct cachekey_header *newch;
caddr_t oldaddr;
struct cachekey_disklist *cd = 0;
if (ch == 0)
return (0);
/*
* Since the first record partly resides in the cachekey_header,
* newrecs cannot be less than 1.
*/
if (newrecs < 1)
newrecs = 1;
newsize = ALIGN8(sizeof (struct cachekey_header)) +
(ch->reclength)*newrecs;
currecs = NUMRECS(ch);
if (newsize > ch->maxsize) {
/* Would exceed maximum allowed */
newsize = ch->maxsize;
}
/* Save stuff we need while the file is unmapped */
oldsize = ch->length;
oldaddr = (caddr_t)ch;
fd = ch->fd;
if (newsize > ch->length) {
/* Extending the file */
cd = &(ch->array[0]);
} else if (newsize == ch->length) {
/* Already OK */
return (ch);
} else {
size_t tmpsize;
struct cachekey_disklist *fcd;
/*
* Shrink the file by removing records from the end.
* First, we have to make sure the file contains valid
* addresses.
*/
if (cache_remap_addresses_ch(ch) == 0) {
syslog(LOG_WARNING, "cache file invalid for mech %d-%d",
ch->keylen, ch->algtype);
close(ch->fd);
munmap((caddr_t)ch, ch->length);
return (0);
}
fcd = MOVE_ADDR(&(ch->array[0]),
ch->reclength*(MAPRECS(ch)-1));
tmpsize = (u_long)fcd - (u_long)ch + ch->reclength;
while (tmpsize > newsize && fcd > &(ch->array[0])) {
if (fcd->uid == (uid_t)-1) {
list_remove(fcd, &(ch->free), 0,
&(ch->free_count));
} else {
list_remove_hash(fcd,
&(ch->bucket[hashval(fcd->uid)]), 0, 0);
list_remove(fcd, &(ch->inuse), &(ch->inuse_end),
&(ch->inuse_count));
}
tmpsize -= ch->reclength;
fcd = MOVE_ADDR(fcd, -(ch->reclength));
}
ch->length = newsize;
(void) msync((caddr_t)ch, ch->length, MS_SYNC);
}
/* Unmap the file */
if (munmap((caddr_t)oldaddr, oldsize) < 0) {
return (0);
}
ch = 0;
/* Truncate/extend it */
if (ftruncate(fd, newsize) < 0) {
return (0);
}
/* Map it again */
if ((newch = (void *)mmap(oldaddr, newsize,
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) ==
MAP_FAILED) {
return (0);
}
/* Update with new values */
newch->length = newsize;
if (cache_remap_addresses_ch(newch) == 0) {
syslog(LOG_WARNING, "cache file invalid for mech %d-%d",
newch->keylen, newch->algtype);
newch->length = oldsize;
close(newch->fd);
munmap((caddr_t)newch, newsize);
return (0);
}
/* If extending the file, add new records to the free list */
if (cd != 0) {
cd = MOVE_ADDR(&(newch->array[0]), currecs*newch->reclength);
for (i = currecs; i < MAPRECS(newch); i++) {
cd->uid = (uid_t)-1;
list_insert(cd, &(newch->free), 0,
&(newch->free_count));
cd = MOVE_ADDR(cd, newch->reclength);
}
}
(void) msync(newch->address, newch->length, MS_SYNC);
return (newch);
}
#ifdef DEBUG
void
print_cache_ch(struct cachekey_header *ch)
{
int i, inuse, inuse_err, free, free_err;
int pb;
struct cachekey_disklist *cd;
printf(
"\nkeylen = %d, algtype = %d, version = %d, headerlen = %d, reclen = %d\n",
ch->keylen, ch->algtype, ch->version, ch->headerlength,
ch->reclength);
printf("fd = %d, address = 0x%x, mapped length = %d, maxsize = %d\n",
ch->fd, ch->address, ch->length, ch->maxsize);
printf("inuse = %d, free = %d\n", ch->inuse_count, ch->free_count);
printf("Active hash buckets:\n");
for (i = 0, inuse = 0, inuse_err = 0; i < NUMHASHBUCKETS; i++) {
cd = ch->bucket[i];
pb = -1;
if (cd != 0) {
pb = 0;
printf("\t%d: ", i);
}
while (cd != 0) {
pb++;
printf("%d ", cd->uid);
if (cd->uid != (uid_t)-1) {
inuse++;
} else {
inuse_err++;
}
cd = cd->nexthash;
}
if (pb >= 0)
printf(" (%d)\n", pb);
}
printf("\ncounted hash inuse = %d, errors = %d\n", inuse, inuse_err);
cd = ch->inuse;
inuse = inuse_err = 0;
while (cd != 0) {
if (cd->uid != (uid_t)-1) {
inuse++;
} else {
inuse_err++;
}
cd = cd->next;
}
printf("counted LRU inuse = %d, errors = %d\n", inuse, inuse_err);
cd = ch->free;
free = free_err = 0;
while (cd != 0) {
if (cd->uid == (uid_t)-1) {
free++;
} else {
free_err++;
fprintf(stderr, "free = %d, err = %d, cd->uid = %d\n",
free, free_err, cd->uid);
}
cd = cd->next;
}
printf("counted free = %d, errors = %d\n", free, free_err);
}
void
print_cache(keylen_t keylen, algtype_t algtype)
{
struct cachekey *c;
if ((c = get_cache_header(keylen, algtype)) == 0)
return;
if (c->ch == 0) {
release_cache_header(c);
return;
}
print_cache_ch(c->ch);
release_cache_header(c);
}
#endif
static u_int
hashval(uid_t uid)
{
return (uid % NUMHASHBUCKETS);
}
static void
list_remove(
struct cachekey_disklist *item,
struct cachekey_disklist **head,
struct cachekey_disklist **tail,
u_int *count)
{
if (item == NULL) return;
/* Handle previous item, if any */
if (item->prev == 0)
*head = item->next;
else
item->prev->next = item->next;
/* Take care of the next item, if any */
if (item->next != 0)
item->next->prev = item->prev;
/* Handle tail pointer, if supplied */
if (tail != 0 && *tail == item)
*tail = item->prev;
item->prev = item->next = 0;
if (count != 0)
(*count)--;
}
static void
list_remove_hash(
struct cachekey_disklist *item,
struct cachekey_disklist **head,
struct cachekey_disklist **tail,
u_int *count)
{
if (item == NULL) return;
/* Handle previous item, if any */
if (item->prevhash == 0)
*head = item->nexthash;
else
item->prevhash->nexthash = item->nexthash;
/* Take care of the next item, if any */
if (item->nexthash != 0)
item->nexthash->prevhash = item->prevhash;
/* Handle tail pointer, if supplied */
if (tail != 0 && *tail == item)
*tail = item->prevhash;
item->prevhash = item->nexthash = 0;
if (count != 0)
(*count)--;
}
static void
list_insert(
struct cachekey_disklist *item,
struct cachekey_disklist **head,
struct cachekey_disklist **tail,
u_int *count)
{
if (item == NULL) return;
/* Insert at tail, if supplied */
if (tail != 0) {
item->prev = *tail;
if (item->prev != 0)
item->prev->next = item;
item->next = 0;
*tail = item;
if (*head == 0)
*head = item;
} else {
item->next = *head;
if (item->next != 0)
item->next->prev = item;
item->prev = 0;
*head = item;
}
if (count != 0)
(*count)++;
}
static void
list_insert_hash(
struct cachekey_disklist *item,
struct cachekey_disklist **head,
struct cachekey_disklist **tail,
u_int *count)
{
if (item == NULL) return;
/* Insert at tail, if supplied */
if (tail != 0) {
item->prevhash = *tail;
if (item->prevhash != 0)
item->prevhash->nexthash = item;
item->nexthash = 0;
*tail = item;
if (*head == 0)
*head = item;
} else {
item->nexthash = *head;
if (item->nexthash != 0)
item->nexthash->prevhash = item;
item->prevhash = 0;
*head = item;
}
if (count != 0)
(*count)++;
}
/*
* Find the cache item specified by the header, uid, and public key. If
* no such uid/public item exists, return a pointer to an empty record.
* In either case, the item returned has been removed from any and all
* lists.
*/
static struct cachekey_disklist *
find_cache_item(struct cachekey_header **ch, uid_t uid, struct dhkey *public)
{
u_int hash;
struct cachekey_disklist *cd;
hash = hashval(uid);
if ((ch == NULL) || ((*ch) == NULL)) {
return (0);
}
for (cd = (*ch)->bucket[hash]; cd != 0; cd = cd->nexthash) {
if (uid == cd->uid &&
public->length == cd->public.length &&
memcmp(public->key, cd->public.key,
cd->public.length) == 0) {
list_remove_hash(cd, &((*ch)->bucket[hash]), 0, 0);
list_remove(cd, &((*ch)->inuse), &((*ch)->inuse_end),
&((*ch)->inuse_count));
return (cd);
}
}
if ((cd = (*ch)->free) != 0) {
list_remove(cd, &((*ch)->free), 0, &((*ch)->free_count));
return (cd);
}
/* Try to extend the file by CHUNK_NUMREC records */
if (((*ch) = remap_cache_file_ch(*ch, NUMRECS(*ch)+CHUNK_NUMREC)) == 0)
return (0);
/* If the extend worked, there should now be at least one free record */
if ((cd = (*ch)->free) != 0) {
list_remove(cd, &((*ch)->free), 0, &((*ch)->free_count));
return (cd);
}
/* Sacrifice the LRU item, if there is one */
if ((cd = (*ch)->inuse) == 0)
return (0);
/* Extract from hash list */
list_remove_hash(cd, &((*ch)->bucket[hashval(cd->uid)]), 0, 0);
/* Extract from LRU list */
list_remove(cd, &((*ch)->inuse), &((*ch)->inuse_end),
&((*ch)->inuse_count));
return (cd);
}
static struct cachekey_header *
cache_insert_ch(
struct cachekey_header *ch,
uid_t uid,
deskeyarray common,
des_block key,
keybuf3 *public,
keybuf3 *secret)
{
struct cachekey_disklist *cd;
struct cachekey_header *newch;
int i, err;
struct skck *skck;
des_block ivec;
struct dhkey *pk;
struct dhkey *sk;
if (ch == 0 || uid == (uid_t)-1) {
return (0);
}
if (common.deskeyarray_len > sizeof (skck->common)/sizeof (des_block) ||
(pk = keybuf3_2_dhkey(public)) == 0 ||
(sk = keybuf3_2_dhkey(secret)) == 0) {
return (0);
}
newch = ch;
if ((cd = find_cache_item(&newch, uid, pk)) == 0) {
free(pk);
free(sk);
return (newch);
}
/*
* The item may have been free, or may have been the LRU sacrificial
* lamb, so reset all fields.
*/
cd->uid = uid;
memcpy(&(cd->public), pk, DHKEYSIZE(pk));
skck = MOVE_ADDR(&(cd->public), DHKEYSIZE(pk));
for (i = 0; i < common.deskeyarray_len; i++) {
skck->common[i] = common.deskeyarray_val[i];
}
skck->verifier = key;
memcpy(&(skck->secret), sk, DHKEYSIZE(sk));
free(pk);
free(sk);
memcpy(ivec.c, key.c, sizeof (key.c));
err = cbc_crypt(key.c, (char *)skck, SKCK_LEN(newch->keylen),
DES_ENCRYPT|DES_HW, ivec.c);
if (DES_FAILED(err)) {
/* Re-insert on free list */
list_insert(cd, &(newch->free), 0, &(newch->free_count));
return (newch);
}
/* Re-insert on hash list */
list_insert_hash(cd, &(newch->bucket[hashval(cd->uid)]), 0, 0);
/* Insert at end of LRU list */
list_insert(cd, &(newch->inuse), &(newch->inuse_end),
&(newch->inuse_count));
(void) msync((caddr_t)newch, newch->length, MS_SYNC);
return (newch);
}
static struct cachekey3_list *
copy_cl_item(struct cachekey_header *ch, struct cachekey_disklist *cd,
des_block key) {
struct cachekey3_list *cl;
struct skck *skck, *skck_cd;
int i, err;
des_block ivec;
/* Allocate the cachekey3_list structure */
if ((cl = malloc(CACHEKEY3_LIST_SIZE(ch->keylen))) == 0) {
return (0);
}
/* Allocate skck structure for decryption */
if ((skck = malloc(SKCK_LEN(ch->keylen))) == 0) {
free(cl);
return (0);
}
/* Decrypt and check verifier */
skck_cd = MOVE_ADDR(&(cd->public), DHKEYSIZE(&(cd->public)));
memcpy(skck, skck_cd, SKCK_LEN(ch->keylen));
memcpy(ivec.c, key.c, sizeof (ivec.c));
err = cbc_crypt(key.c, (char *)skck, SKCK_LEN(ch->keylen),
DES_DECRYPT|DES_HW, ivec.c);
if (DES_FAILED(err)) {
free(cl);
free(skck);
return (0);
}
if (memcmp(key.c, skck->verifier.c, sizeof (skck->verifier.c)) != 0) {
free(cl);
free(skck);
return (0);
}
/* Everything OK; copy values */
cl->public = MOVE_ADDR(cl, sizeof (struct cachekey3_list));
cl->public->keybuf3_val = MOVE_ADDR(cl->public, sizeof (keybuf3));
cl->secret = MOVE_ADDR(cl->public->keybuf3_val,
ALIGN4(2*KEYLEN(ch->keylen)+1));
cl->secret->keybuf3_val = MOVE_ADDR(cl->secret, sizeof (keybuf3));
cl->deskey.deskeyarray_val =
MOVE_ADDR(cl->secret->keybuf3_val,
ALIGN4(2*KEYLEN(ch->keylen)+1));
bin2hex(cd->public.key, (u_char *)cl->public->keybuf3_val,
cd->public.length);
cl->public->keybuf3_len = cd->public.length*2+1;
bin2hex(skck->secret.key, (u_char *)cl->secret->keybuf3_val,
skck->secret.length);
cl->secret->keybuf3_len = skck->secret.length*2+1;
cl->deskey.deskeyarray_len = sizeof (skck->common)/sizeof (des_block);
for (i = 0; i < cl->deskey.deskeyarray_len; i++) {
cl->deskey.deskeyarray_val[i] = skck->common[i];
}
cl->refcnt = 0;
cl->next = 0;
free(skck);
return (cl);
}
static struct cachekey3_list *
cache_retrieve_ch(struct cachekey_header *ch, uid_t uid, keybuf3 *public,
des_block key) {
struct cachekey_disklist *cd;
struct cachekey3_list *cl = 0, **cltmp = &cl;
u_int hash;
struct dhkey *pk = 0;
if (uid == (uid_t)-1 ||
(public != 0 && (pk = keybuf3_2_dhkey(public)) == 0)) {
return (0);
}
hash = hashval(uid);
for (cd = ch->bucket[hash]; cd != 0; cd = cd->nexthash) {
if (uid == cd->uid) {
/* Match on public key as well ? */
if (pk != 0) {
if (memcmp(cd->public.key, pk->key,
cd->public.length) != 0) {
/* Keep looking... */
continue;
}
cl = copy_cl_item(ch, cd, key);
/* Match on public key => nothing more to do */
break;
}
*cltmp = copy_cl_item(ch, cd, key);
if (*cltmp == 0) {
/* Return what we've got */
break;
}
cltmp = &((*cltmp)->next);
/* On to the next item */
}
}
if (pk != 0)
free(pk);
return (cl);
}
/*
* Remove specified item. 'public' == 0 => remove all items for uid.
* Return number of items removed.
*/
static int
cache_remove_ch(struct cachekey_header *ch, uid_t uid, keybuf3 *public) {
struct cachekey_disklist *cd, *cdtmp;
u_int hash;
int match = 0;
struct dhkey *pk = 0;
if (uid == (uid_t)-1 ||
(public != 0 && (pk = keybuf3_2_dhkey(public)) == 0)) {
return (0);
}
hash = hashval(uid);
for (cd = ch->bucket[hash]; cd != 0; ) {
if (uid == cd->uid) {
/* Match on public key as well ? */
if (pk != 0) {
if (memcmp(cd->public.key, pk->key,
cd->public.length) != 0) {
/* Keep looking... */
continue;
}
match++;
list_remove_hash(cd, &(ch->bucket[hash]), 0, 0);
list_remove(cd, &(ch->inuse), &(ch->inuse_end),
&(ch->inuse_count));
cd->uid = (uid_t)-1;
list_insert(cd, &(ch->free), 0,
&(ch->free_count));
/* Match on public key => nothing more to do */
break;
}
match++;
/*
* XXX: Assume that the order of the hash list remains
* the same after removal of an item. If this isn't
* true, we really should start over from the start
* of the hash bucket.
*/
cdtmp = cd->nexthash;
list_remove_hash(cd, &(ch->bucket[hash]), 0, 0);
list_remove(cd, &(ch->inuse), &(ch->inuse_end),
&(ch->inuse_count));
cd->uid = (uid_t)-1;
list_insert(cd, &(ch->free), 0,
&(ch->free_count));
/* On to the next item */
cd = cdtmp;
} else {
cd = cd->nexthash;
}
}
free(pk);
return (match);
}
#define INCCACHEREFCNT mutex_lock(&cache_lock); \
cache_refcnt++; \
mutex_unlock(&cache_lock)
#if !defined(lint) && !defined(__lint)
#define DECCACHEREFCNT mutex_lock(&cache_lock); \
if (cache_refcnt > 0) \
if (cache_refcnt-- == 0) (void) cond_broadcast(&cache_cv); \
mutex_unlock(&cache_lock)
#else
#define DECCACHEREFCNT mutex_lock(&cache_lock); \
if (cache_refcnt-- == 0) (void) cond_broadcast(&cache_cv); \
mutex_unlock(&cache_lock)
#endif
/*
* Return the cachekey structure for the specified keylen and algtype.
* When returned, the lock in the structure has been activated. It's the
* responsibility of the caller to unlock it by calling release_cache_header().
*/
static struct cachekey *
get_cache_header(keylen_t keylen, algtype_t algtype) {
struct cachekey *c;
INCCACHEREFCNT;
for (c = cache; c != 0; c = c->next) {
if (c->keylen == keylen && c->algtype == algtype) {
mutex_lock(&c->mp);
return (c);
}
}
/* Spin until there are no cache readers */
mutex_lock(&cache_lock);
#if !defined(lint) && !defined(__lint)
if (cache_refcnt > 0)
#endif
cache_refcnt--;
while (cache_refcnt != 0) {
(void) cond_wait(&cache_cv, &cache_lock);
}
if ((c = malloc(sizeof (struct cachekey))) != 0) {
c->ch = 0;
c->keylen = keylen;
c->algtype = algtype;
mutex_init(&c->mp, 0, 0);
c->next = cache;
cache = c;
mutex_lock(&c->mp);
cache_refcnt++;
mutex_unlock(&cache_lock);
return (c);
}
mutex_unlock(&cache_lock);
return (0);
}
static void
release_cache_header(struct cachekey *ck) {
struct cachekey *c;
if (ck == 0)
return;
for (c = cache; c != 0; c = c->next) {
if (c == ck) {
mutex_unlock(&c->mp);
DECCACHEREFCNT;
break;
}
}
}
int
create_cache_file(keylen_t keylen, algtype_t algtype, int sizespec)
{
struct cachekey *c;
int ret;
if ((c = get_cache_header(keylen, algtype)) == 0)
return (0);
if (c->ch != 0) {
/* Already created and opened */
release_cache_header(c);
return (1);
}
ret = (c->ch = create_cache_file_ch(keylen, algtype, sizespec)) != 0;
release_cache_header(c);
return (ret);
}
int
cache_insert(
keylen_t keylen,
algtype_t algtype,
uid_t uid,
deskeyarray common,
des_block key,
keybuf3 *public,
keybuf3 *secret)
{
struct cachekey *c;
int ret;
if ((c = get_cache_header(keylen, algtype)) == 0)
return (0);
if (c->ch == 0) {
release_cache_header(c);
return (0);
}
ret = (c->ch =
cache_insert_ch(c->ch, uid, common, key, public, secret)) != 0;
release_cache_header(c);
return (ret);
}
struct cachekey3_list *
cache_retrieve(
keylen_t keylen,
algtype_t algtype,
uid_t uid,
keybuf3 *public,
des_block key)
{
struct cachekey *c;
struct cachekey3_list *cl;
if ((c = get_cache_header(keylen, algtype)) == 0)
return (0);
if (c->ch == 0) {
release_cache_header(c);
return (0);
}
cl = cache_retrieve_ch(c->ch, uid, public, key);
release_cache_header(c);
return (cl);
}
int
cache_remove(keylen_t keylen, algtype_t algtype, uid_t uid, keybuf3 *public)
{
struct cachekey *c;
int ret;
if ((c = get_cache_header(keylen, algtype)) == 0)
return (0);
if (c->ch == 0) {
release_cache_header(c);
return (0);
}
ret = cache_remove_ch(c->ch, uid, public);
release_cache_header(c);
return (ret);
}
static struct dhkey *
keybuf3_2_dhkey(keybuf3 *hexkey)
{
struct dhkey *binkey;
/* hexkey->keybuf3_len*4 is the key length in bits */
if ((binkey = malloc(DHKEYALLOC(hexkey->keybuf3_len*4))) == 0)
return (0);
/* Set to zero to keep dbx and Purify access checking happy */
memset(binkey, 0, DHKEYALLOC(hexkey->keybuf3_len*4));
binkey->length = hexkey->keybuf3_len/2;
hex2bin((u_char *)hexkey->keybuf3_val, binkey->key,
(int)binkey->length);
return (binkey);
}
|