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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <locale.h>
#include <sys/types.h>
#include <zone.h>
#include <sys/stat.h>
#include "cryptoadm.h"
static int err; /* To store errno which may be overwritten by gettext() */
static int build_entrylist(entry_t *pent, entrylist_t **pplist);
static entry_t *dup_entry(entry_t *pent1);
static mechlist_t *dup_mechlist(mechlist_t *plist);
static entry_t *getent(char *provname, entrylist_t *entrylist);
static int interpret(char *buf, entry_t **ppent);
static int parse_sup_dis_list(const char *buf, entry_t *pent);
/*
* Duplicate the mechanism list. A null pointer is returned if the storage
* space available is insufficient or the input argument is NULL.
*/
static mechlist_t *
dup_mechlist(mechlist_t *plist)
{
mechlist_t *pres = NULL;
mechlist_t *pcur;
mechlist_t *ptmp;
int rc = SUCCESS;
while (plist != NULL) {
if (!(ptmp = create_mech(plist->name))) {
rc = FAILURE;
break;
}
if (pres == NULL) {
pres = pcur = ptmp;
} else {
pcur->next = ptmp;
pcur = pcur->next;
}
plist = plist->next;
}
if (rc != SUCCESS) {
free_mechlist(pres);
return (NULL);
}
return (pres);
}
/*
* Get the number of mechanisms in the mechanism list.
*/
int
get_mech_count(mechlist_t *plist)
{
int count = 0;
while (plist != NULL) {
count++;
plist = plist->next;
}
return (count);
}
/*
* Create one item of type entry_t with the provider name.
* Return NULL if there's not enough memory or provname is NULL.
*/
entry_t *
create_entry(char *provname)
{
entry_t *pent = NULL;
if (provname == NULL) {
return (NULL);
}
pent = calloc(1, sizeof (entry_t));
if (pent == NULL) {
cryptodebug("out of memory.");
return (NULL);
}
(void) strlcpy(pent->name, provname, MAXNAMELEN);
pent->suplist = NULL;
pent->sup_count = 0;
pent->dislist = NULL;
pent->dis_count = 0;
pent->load = B_TRUE;
return (pent);
}
/*
* Duplicate an entry for a provider from kcf.conf.
* Return NULL if memory is insufficient or the input argument is NULL.
* Called by getent().
*/
static entry_t *
dup_entry(entry_t *pent1)
{
entry_t *pent2 = NULL;
if (pent1 == NULL) {
return (NULL);
}
if ((pent2 = create_entry(pent1->name)) == NULL) {
cryptodebug("out of memory.");
return (NULL);
}
pent2->sup_count = pent1->sup_count;
pent2->dis_count = pent1->dis_count;
pent2->load = pent1->load;
if (pent1->suplist != NULL) {
pent2->suplist = dup_mechlist(pent1->suplist);
if (pent2->suplist == NULL) {
free_entry(pent2);
return (NULL);
}
}
if (pent1->dislist != NULL) {
pent2->dislist = dup_mechlist(pent1->dislist);
if (pent2->dislist == NULL) {
free_entry(pent2);
return (NULL);
}
}
return (pent2);
}
/*
* This routine parses the disabledlist or the supportedlist of an entry
* in the kcf.conf configuration file.
*
* Arguments:
* buf: an input argument which is a char string with the format of
* "disabledlist=m1,m2,..." or "supportedlist=m1,m2,..."
* pent: the entry for the disabledlist. This is an IN/OUT argument.
*
* Return value: SUCCESS or FAILURE.
*/
static int
parse_sup_dis_list(const char *buf, entry_t *pent)
{
mechlist_t *pmech = NULL;
mechlist_t *phead = NULL;
char *next_token;
char *value;
int count;
int supflag = B_FALSE;
int disflag = B_FALSE;
int rc = SUCCESS;
if (strncmp(buf, EF_SUPPORTED, strlen(EF_SUPPORTED)) == 0) {
supflag = B_TRUE;
} else if (strncmp(buf, EF_DISABLED, strlen(EF_DISABLED)) == 0) {
disflag = B_TRUE;
} else {
/* should not come here */
return (FAILURE);
}
if (value = strpbrk(buf, SEP_EQUAL)) {
value++; /* get rid of = */
} else {
cryptodebug("failed to parse the kcf.conf file.");
return (FAILURE);
}
if ((next_token = strtok(value, SEP_COMMA)) == NULL) {
cryptodebug("failed to parse the kcf.conf file.");
return (FAILURE);
}
if ((pmech = create_mech(next_token)) == NULL) {
return (FAILURE);
}
if (supflag) {
if (pent->suplist != NULL) {
cryptodebug("multiple supportedlist entries "
"for a mechanism in file kcf.conf.");
return (FAILURE);
} else {
pent->suplist = phead = pmech;
}
} else if (disflag) {
if (pent->dislist != NULL) {
cryptodebug("multiple disabledlist entries "
"for a mechanism in file kcf.conf.");
return (FAILURE);
} else {
pent->dislist = phead = pmech;
}
}
count = 1;
while (next_token) {
if (next_token = strtok(NULL, SEP_COMMA)) {
if ((pmech = create_mech(next_token)) == NULL) {
rc = FAILURE;
break;
}
count++;
phead->next = pmech;
phead = phead->next;
}
}
if (rc == SUCCESS) {
if (supflag) {
pent->sup_count = count;
} else if (disflag) {
pent->dis_count = count;
}
} else {
free_mechlist(phead);
}
return (rc);
}
/*
* Convert a char string containing a line about a provider
* from kcf.conf into an entry_t structure.
*
* Note: the input string, buf, may be modified by this function.
*
* See ent2str(), the reverse of this function, for the format of
* kcf.conf lines.
*/
static int
interpret(char *buf, entry_t **ppent)
{
entry_t *pent = NULL;
char *token1;
char *token2;
char *token3;
int rc;
/* Get provider name */
if ((token1 = strtok(buf, SEP_COLON)) == NULL) { /* buf is NULL */
return (FAILURE);
};
pent = create_entry(token1);
if (pent == NULL) {
cryptodebug("out of memory.");
return (FAILURE);
}
if ((token2 = strtok(NULL, SEP_SEMICOLON)) == NULL) {
/* The entry contains a provider name only */
free_entry(pent);
return (FAILURE);
}
if (strncmp(token2, EF_UNLOAD, strlen(EF_UNLOAD)) == 0) {
pent->load = B_FALSE; /* cryptoadm unload */
token2 = strtok(NULL, SEP_SEMICOLON);
/*
* If token2 is NULL, the entry contains a
* provider name:unload only
*/
}
if (token2 != NULL) {
/*
* Either supportedlist or disabledlist or both are present.
* Need to call strtok() to get token3 first, as function
* parse_sup_dis_list() makes strtok() calls on the
* token2 substring.
*/
token3 = strtok(NULL, SEP_SEMICOLON); /* optional */
/* parse supportedlist (or disabledlist if no supportedlist) */
if ((rc = parse_sup_dis_list(token2, pent)) != SUCCESS) {
free_entry(pent);
return (rc);
}
/* parse disabledlist (if there's a supportedlist) */
if ((token3 != NULL) && ((rc = parse_sup_dis_list(token3,
pent)) != SUCCESS)) {
free_entry(pent);
return (rc);
}
}
*ppent = pent;
return (SUCCESS);
}
/*
* Add an entry about a provider from kcf.conf to the end of an entry list.
* If the entry list pplist is NULL, create the linked list with pent as the
* first element.
*/
static int
build_entrylist(entry_t *pent, entrylist_t **pplist)
{
entrylist_t *pentlist;
entrylist_t *pcur = NULL;
pentlist = malloc(sizeof (entrylist_t));
if (pentlist == NULL) {
cryptodebug("out of memory.");
return (FAILURE);
}
pentlist->pent = pent;
pentlist->next = NULL;
if (*pplist) {
pcur = *pplist;
while (pcur->next != NULL)
pcur = pcur->next;
pcur->next = pentlist;
} else { /* empty list */
*pplist = pentlist;
}
return (SUCCESS);
}
/*
* Find the entry with the "provname" name from the entry list and duplicate
* it. Called by getent_kef().
*/
static entry_t *
getent(char *provname, entrylist_t *entrylist)
{
boolean_t found = B_FALSE;
entry_t *pent1 = NULL;
if ((provname == NULL) || (entrylist == NULL)) {
return (NULL);
}
while (!found && entrylist) {
if (strcmp(entrylist->pent->name, provname) == 0) {
found = B_TRUE;
pent1 = entrylist->pent;
} else {
entrylist = entrylist->next;
}
}
if (!found) {
return (NULL);
}
/* duplicate the entry to be returned */
return (dup_entry(pent1));
}
/*
* Free memory in entry_t.
* That is, the supported and disabled lists for a provider
* from kcf.conf.
*/
void
free_entry(entry_t *pent)
{
if (pent == NULL) {
return;
} else {
free_mechlist(pent->suplist);
free_mechlist(pent->dislist);
free(pent);
}
}
/*
* Free elements in a entrylist_t linked list,
* which lists providers in kcf.conf.
*/
void
free_entrylist(entrylist_t *entrylist)
{
entrylist_t *pnext;
while (entrylist != NULL) {
pnext = entrylist->next;
free_entry(entrylist->pent);
entrylist = pnext;
}
}
/*
* Convert an entry_t to a kcf.conf line string. Build a string to insert
* as a line in file kcf.conf. Based on the content of an entry_t,
* the result string is one of these 8 forms:
* - name:supportedlist=m1,m2,...,mj
* - name:disabledlist=m1,m2,...,mj
* - name:supportedlist=m1,...,mj;disabledlist=m1,m2,...,mk
*
* - name:unload
* - name:unload;supportedlist=m1,m2,...,mj
* - name:unload;disabledlist=m1,m2,...,mj
* - name:unload;supportedlist=m1,...,mj;disabledlist=m1,m2,...,mk
*
* - (NUL character or 0-length string)
*
* Return a 0-length empty string if no keyword is present (that is,
* supportedlist, disabledlist, or unload). A kcf.conf line with just the
* provider name with no keyword is invalid.
*
* Note that the caller is responsible for freeing the returned string
* (with free_entry()).
* See interpret() for the reverse of this function: converting a string
* to an entry_t.
*/
char *
ent2str(entry_t *pent)
{
char *buf;
mechlist_t *pcur = NULL;
boolean_t keyword_already_present = B_FALSE;
if (pent == NULL) {
return (NULL);
}
if ((buf = malloc(BUFSIZ)) == NULL) {
return (NULL);
}
/* convert the provider name */
if (strlcpy(buf, pent->name, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
if (!pent->load) { /* add "unload" keyword */
if (strlcat(buf, SEP_COLON, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
if (strlcat(buf, EF_UNLOAD, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
keyword_already_present = B_TRUE;
}
/* convert the supported list if any */
pcur = pent->suplist;
if (pcur != NULL) {
if (strlcat(buf,
keyword_already_present ? SEP_SEMICOLON : SEP_COLON,
BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
if (strlcat(buf, EF_SUPPORTED, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
while (pcur != NULL) {
if (strlcat(buf, pcur->name, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
pcur = pcur->next;
if (pcur != NULL) {
if (strlcat(buf, SEP_COMMA, BUFSIZ)
>= BUFSIZ) {
free(buf);
return (NULL);
}
}
}
keyword_already_present = B_TRUE;
}
/* convert the disabled list if any */
pcur = pent->dislist;
if (pcur != NULL) {
if (strlcat(buf,
keyword_already_present ? SEP_SEMICOLON : SEP_COLON,
BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
if (strlcat(buf, EF_DISABLED, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
while (pcur != NULL) {
if (strlcat(buf, pcur->name, BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
pcur = pcur->next;
if (pcur != NULL) {
if (strlcat(buf, SEP_COMMA, BUFSIZ)
>= BUFSIZ) {
free(buf);
return (NULL);
}
}
}
keyword_already_present = B_TRUE;
}
if (strlcat(buf, "\n", BUFSIZ) >= BUFSIZ) {
free(buf);
return (NULL);
}
if (!keyword_already_present) {
/* Only the provider name, without a keyword, is on the line */
buf[0] = '\0';
}
return (buf);
}
/*
* Enable the mechanisms for the provider pointed by *ppent. If allflag is
* TRUE, enable all. Otherwise, enable the mechanisms specified in the 3rd
* argument "mlist". The result will be stored in ppent also.
*/
int
enable_mechs(entry_t **ppent, boolean_t allflag, mechlist_t *mlist)
{
entry_t *pent;
mechlist_t *phead; /* the current and resulting disabled list */
mechlist_t *ptr = NULL;
mechlist_t *pcur = NULL;
boolean_t found;
pent = *ppent;
if (pent == NULL) {
return (FAILURE);
}
if (allflag) {
free_mechlist(pent->dislist);
pent->dis_count = 0;
pent->dislist = NULL;
return (SUCCESS);
}
/*
* for each mechanism in the to-be-enabled mechanism list,
* - check if it is in the current disabled list
* - if found, delete it from the disabled list
* otherwise, give a warning.
*/
ptr = mlist;
while (ptr != NULL) {
found = B_FALSE;
phead = pcur = pent->dislist;
while (!found && pcur) {
if (strcmp(pcur->name, ptr->name) == 0) {
found = B_TRUE;
} else {
phead = pcur;
pcur = pcur->next;
}
}
if (found) {
if (phead == pcur) {
pent->dislist = pent->dislist->next;
free(pcur);
} else {
phead->next = pcur->next;
free(pcur);
}
pent->dis_count--;
} else {
cryptoerror(LOG_STDERR, gettext(
"(Warning) %1$s is either enabled already or not "
"a valid mechanism for %2$s"), ptr->name,
pent->name);
}
ptr = ptr->next;
}
if (pent->dis_count == 0) {
pent->dislist = NULL;
}
return (SUCCESS);
}
/*
* Determine if the kernel provider name, path, is a device
* (that is, it contains a slash character (e.g., "mca/0").
* If so, it is a hardware provider; otherwise it is a software provider.
*/
boolean_t
is_device(char *path)
{
if (strchr(path, SEP_SLASH) != NULL) {
return (B_TRUE);
} else {
return (B_FALSE);
}
}
/*
* Split a hardware provider name with the "name/inst_num" format into
* a name and a number (e.g., split "mca/0" into "mca" instance 0).
*/
int
split_hw_provname(char *provname, char *pname, int *inst_num)
{
char name[MAXNAMELEN];
char *inst_str;
if (provname == NULL) {
return (FAILURE);
}
(void) strlcpy(name, provname, MAXNAMELEN);
if (strtok(name, "/") == NULL) {
return (FAILURE);
}
if ((inst_str = strtok(NULL, "/")) == NULL) {
return (FAILURE);
}
(void) strlcpy(pname, name, MAXNAMELEN);
*inst_num = atoi(inst_str);
return (SUCCESS);
}
/*
* Retrieve information from kcf.conf and build a hardware device entry list
* and a software entry list of kernel crypto providers.
*
* This list is usually incomplete, as kernel crypto providers only have to
* be listed in kcf.conf if a mechanism is disabled (by cryptoadm) or
* if the kernel provider module is not one of the default kernel providers.
*
* The kcf.conf file is available only in the global zone.
*/
int
get_kcfconf_info(entrylist_t **ppdevlist, entrylist_t **ppsoftlist)
{
FILE *pfile = NULL;
char buffer[BUFSIZ];
int len;
entry_t *pent = NULL;
int rc = SUCCESS;
if ((pfile = fopen(_PATH_KCF_CONF, "r")) == NULL) {
cryptodebug("failed to open the kcf.conf file for read only");
return (FAILURE);
}
*ppdevlist = NULL;
*ppsoftlist = NULL;
while (fgets(buffer, BUFSIZ, pfile) != NULL) {
if (buffer[0] == '#' || buffer[0] == ' ' ||
buffer[0] == '\n'|| buffer[0] == '\t') {
continue; /* ignore comment lines */
}
len = strlen(buffer);
if (buffer[len - 1] == '\n') { /* get rid of trailing '\n' */
len--;
}
buffer[len] = '\0';
if ((rc = interpret(buffer, &pent)) == SUCCESS) {
if (is_device(pent->name)) {
rc = build_entrylist(pent, ppdevlist);
} else {
rc = build_entrylist(pent, ppsoftlist);
}
} else {
cryptoerror(LOG_STDERR, gettext(
"failed to parse configuration."));
}
if (rc != SUCCESS) {
free_entrylist(*ppdevlist);
free_entrylist(*ppsoftlist);
free_entry(pent);
break;
}
}
(void) fclose(pfile);
return (rc);
}
/*
* Retrieve information from admin device and build a device entry list and
* a software entry list. This is used where there is no kcf.conf, e.g., the
* non-global zone.
*/
int
get_admindev_info(entrylist_t **ppdevlist, entrylist_t **ppsoftlist)
{
crypto_get_dev_list_t *pdevlist_kernel = NULL;
crypto_get_soft_list_t *psoftlist_kernel = NULL;
char *devname;
int inst_num;
int mcount;
mechlist_t *pmech = NULL;
entry_t *pent_dev = NULL, *pent_soft = NULL;
int i;
char *psoftname;
entrylist_t *tmp_pdev = NULL;
entrylist_t *tmp_psoft = NULL;
entrylist_t *phardlist = NULL, *psoftlist = NULL;
/*
* Get hardware providers
*/
if (get_dev_list(&pdevlist_kernel) != SUCCESS) {
cryptodebug("failed to get hardware provider list from kernel");
return (FAILURE);
}
for (i = 0; i < pdevlist_kernel->dl_dev_count; i++) {
devname = pdevlist_kernel->dl_devs[i].le_dev_name;
inst_num = pdevlist_kernel->dl_devs[i].le_dev_instance;
mcount = pdevlist_kernel->dl_devs[i].le_mechanism_count;
pmech = NULL;
if (get_dev_info(devname, inst_num, mcount, &pmech) !=
SUCCESS) {
cryptodebug(
"failed to retrieve the mechanism list for %s/%d.",
devname, inst_num);
goto fail_out;
}
if ((pent_dev = create_entry(devname)) == NULL) {
cryptodebug("out of memory.");
free_mechlist(pmech);
goto fail_out;
}
pent_dev->suplist = pmech;
pent_dev->sup_count = mcount;
if (build_entrylist(pent_dev, &tmp_pdev) != SUCCESS) {
goto fail_out;
}
}
free(pdevlist_kernel);
pdevlist_kernel = NULL;
/*
* Get software providers
*/
if (getzoneid() == GLOBAL_ZONEID) {
if (get_kcfconf_info(&phardlist, &psoftlist) != SUCCESS) {
goto fail_out;
}
}
if (get_soft_list(&psoftlist_kernel) != SUCCESS) {
cryptodebug("failed to get software provider list from kernel");
goto fail_out;
}
for (i = 0, psoftname = psoftlist_kernel->sl_soft_names;
i < psoftlist_kernel->sl_soft_count;
i++, psoftname = psoftname + strlen(psoftname) + 1) {
pmech = NULL;
if (get_soft_info(psoftname, &pmech, phardlist, psoftlist) !=
SUCCESS) {
cryptodebug(
"failed to retrieve the mechanism list for %s.",
psoftname);
goto fail_out;
}
if ((pent_soft = create_entry(psoftname)) == NULL) {
cryptodebug("out of memory.");
free_mechlist(pmech);
goto fail_out;
}
pent_soft->suplist = pmech;
pent_soft->sup_count = get_mech_count(pmech);
if (build_entrylist(pent_soft, &tmp_psoft) != SUCCESS) {
goto fail_out;
}
}
free(psoftlist_kernel);
psoftlist_kernel = NULL;
*ppdevlist = tmp_pdev;
*ppsoftlist = tmp_psoft;
return (SUCCESS);
fail_out:
if (pent_dev != NULL)
free_entry(pent_dev);
if (pent_soft != NULL)
free_entry(pent_soft);
free_entrylist(tmp_pdev);
free_entrylist(tmp_psoft);
if (pdevlist_kernel != NULL)
free(pdevlist_kernel);
if (psoftlist_kernel != NULL)
free(psoftlist_kernel);
return (FAILURE);
}
/*
* Return configuration information for a kernel provider from kcf.conf.
* For kernel software providers return a enabled list and disabled list.
* For kernel hardware providers return just a disabled list.
*
* Parameters phardlist and psoftlist are supplied by get_kcfconf_info().
* If NULL, this function calls get_kcfconf_info() internally.
*/
entry_t *
getent_kef(char *provname, entrylist_t *phardlist, entrylist_t *psoftlist)
{
entry_t *pent = NULL;
boolean_t memory_allocated = B_FALSE;
if ((phardlist == NULL) || (psoftlist == NULL)) {
if (get_kcfconf_info(&phardlist, &psoftlist) != SUCCESS) {
return (NULL);
}
memory_allocated = B_TRUE;
}
if (is_device(provname)) {
pent = getent(provname, phardlist);
} else {
pent = getent(provname, psoftlist);
}
if (memory_allocated) {
free_entrylist(phardlist);
free_entrylist(psoftlist);
}
return (pent);
}
/*
* Print out the provider name and the mechanism list.
*/
void
print_mechlist(char *provname, mechlist_t *pmechlist)
{
mechlist_t *ptr = NULL;
if (provname == NULL) {
return;
}
(void) printf("%s: ", provname);
if (pmechlist == NULL) {
(void) printf(gettext("No mechanisms presented.\n"));
return;
}
ptr = pmechlist;
while (ptr != NULL) {
(void) printf("%s", ptr->name);
ptr = ptr->next;
if (ptr == NULL) {
(void) printf("\n");
} else {
(void) printf(",");
}
}
}
/*
* Update the kcf.conf file based on the update mode:
* - If update_mode is MODIFY_MODE, modify the entry with the same name.
* If not found, append a new entry to the kcf.conf file.
* - If update_mode is DELETE_MODE, delete the entry with the same name.
* - If update_mode is ADD_MODE, append a new entry to the kcf.conf file.
*/
int
update_kcfconf(entry_t *pent, int update_mode)
{
boolean_t add_it = B_FALSE;
boolean_t delete_it = B_FALSE;
boolean_t this_entry_matches = B_FALSE;
boolean_t found_entry = B_FALSE;
FILE *pfile = NULL;
FILE *pfile_tmp = NULL;
char buffer[BUFSIZ];
char buffer2[BUFSIZ];
char tmpfile_name[MAXPATHLEN];
char *name;
char *new_str = NULL;
int rc = SUCCESS;
if (pent == NULL) {
cryptoerror(LOG_STDERR, gettext("internal error."));
return (FAILURE);
}
/* Check the update_mode */
switch (update_mode) {
case ADD_MODE:
add_it = B_TRUE;
/* FALLTHROUGH */
case MODIFY_MODE:
/* Convert the entry a string to add to kcf.conf */
if ((new_str = ent2str(pent)) == NULL) {
return (FAILURE);
}
if (strlen(new_str) == 0) {
free(new_str);
delete_it = B_TRUE;
}
break;
case DELETE_MODE:
delete_it = B_TRUE;
break;
default:
cryptoerror(LOG_STDERR, gettext("internal error."));
return (FAILURE);
}
/* Open the kcf.conf file */
if ((pfile = fopen(_PATH_KCF_CONF, "r+")) == NULL) {
err = errno;
cryptoerror(LOG_STDERR,
gettext("failed to update the configuration - %s"),
strerror(err));
cryptodebug("failed to open %s for write.", _PATH_KCF_CONF);
return (FAILURE);
}
/* Lock the kcf.conf file */
if (lockf(fileno(pfile), F_TLOCK, 0) == -1) {
err = errno;
cryptoerror(LOG_STDERR,
gettext("failed to update the configuration - %s"),
strerror(err));
(void) fclose(pfile);
return (FAILURE);
}
/*
* Create a temporary file in the /etc/crypto directory to save
* updated configuration file first.
*/
(void) strlcpy(tmpfile_name, TMPFILE_TEMPLATE, sizeof (tmpfile_name));
if (mkstemp(tmpfile_name) == -1) {
err = errno;
cryptoerror(LOG_STDERR,
gettext("failed to create a temporary file - %s"),
strerror(err));
(void) fclose(pfile);
return (FAILURE);
}
if ((pfile_tmp = fopen(tmpfile_name, "w")) == NULL) {
err = errno;
cryptoerror(LOG_STDERR, gettext("failed to open %s - %s"),
tmpfile_name, strerror(err));
(void) fclose(pfile);
return (FAILURE);
}
/*
* Loop thru the entire kcf.conf file, insert, modify or delete
* an entry.
*/
while (fgets(buffer, BUFSIZ, pfile) != NULL) {
if (add_it) {
if (fputs(buffer, pfile_tmp) == EOF) {
err = errno;
cryptoerror(LOG_STDERR, gettext(
"failed to write to a temp file: %s."),
strerror(err));
rc = FAILURE;
break;
}
} else { /* modify or delete */
this_entry_matches = B_FALSE;
if (!(buffer[0] == '#' || buffer[0] == ' ' ||
buffer[0] == '\n'|| buffer[0] == '\t')) {
/*
* Get the provider name from this line and
* check if this is the entry to be updated
* or deleted. Note: can not use "buffer"
* directly because strtok will change its
* value.
*/
(void) strlcpy(buffer2, buffer, BUFSIZ);
if ((name = strtok(buffer2, SEP_COLON)) ==
NULL) {
rc = FAILURE;
break;
}
if (strcmp(pent->name, name) == 0) {
this_entry_matches = B_TRUE;
found_entry = B_TRUE;
}
}
if (!this_entry_matches || !delete_it) {
/* write this entry */
if (this_entry_matches) {
/*
* Modify this entry: get the
* updated string and place into buffer.
*/
(void) strlcpy(buffer, new_str, BUFSIZ);
free(new_str);
}
/* write the (unchanged or modified) entry */
if (fputs(buffer, pfile_tmp) == EOF) {
err = errno;
cryptoerror(LOG_STDERR, gettext(
"failed to write to a temp file: "
"%s."), strerror(err));
rc = FAILURE;
break;
}
}
}
}
if ((!delete_it) && (rc != FAILURE)) {
if (add_it || !found_entry) {
/* append new entry to end of file */
if (fputs(new_str, pfile_tmp) == EOF) {
err = errno;
cryptoerror(LOG_STDERR, gettext(
"failed to write to a temp file: %s."),
strerror(err));
rc = FAILURE;
}
free(new_str);
}
}
(void) fclose(pfile);
if (fclose(pfile_tmp) != 0) {
err = errno;
cryptoerror(LOG_STDERR,
gettext("failed to close %s: %s"), tmpfile_name,
strerror(err));
return (FAILURE);
}
/* Copy the temporary file to the kcf.conf file */
if (rename(tmpfile_name, _PATH_KCF_CONF) == -1) {
err = errno;
cryptoerror(LOG_STDERR,
gettext("failed to update the configuration - %s"),
strerror(err));
cryptodebug("failed to rename %s to %s: %s", tmpfile,
_PATH_KCF_CONF, strerror(err));
rc = FAILURE;
} else if (chmod(_PATH_KCF_CONF,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
err = errno;
cryptoerror(LOG_STDERR,
gettext("failed to update the configuration - %s"),
strerror(err));
cryptodebug("failed to chmod to %s: %s", _PATH_KCF_CONF,
strerror(err));
rc = FAILURE;
} else {
rc = SUCCESS;
}
if ((rc == FAILURE) && (unlink(tmpfile_name) != 0)) {
err = errno;
cryptoerror(LOG_STDERR, gettext(
"(Warning) failed to remove %s: %s"),
tmpfile_name, strerror(err));
}
return (rc);
}
/*
* Disable the mechanisms for the provider pointed by *ppent. If allflag is
* TRUE, disable all. Otherwise, disable the mechanisms specified in the
* dislist argument. The "infolist" argument contains the mechanism list
* supported by this provider.
*/
int
disable_mechs(entry_t **ppent, mechlist_t *infolist, boolean_t allflag,
mechlist_t *dislist)
{
entry_t *pent;
mechlist_t *plist = NULL;
mechlist_t *phead = NULL;
mechlist_t *pmech = NULL;
int rc = SUCCESS;
pent = *ppent;
if (pent == NULL) {
return (FAILURE);
}
if (allflag) {
free_mechlist(pent->dislist);
pent->dis_count = get_mech_count(infolist);
if (!(pent->dislist = dup_mechlist(infolist))) {
return (FAILURE);
} else {
return (SUCCESS);
}
}
/*
* Not disable all. Now loop thru the mechanisms specified in the
* dislist. If the mechanism is not supported by the provider,
* ignore it with a warning. If the mechanism is disabled already,
* do nothing. Otherwise, prepend it to the beginning of the disabled
* list of the provider.
*/
plist = dislist;
while (plist != NULL) {
if (!is_in_list(plist->name, infolist)) {
cryptoerror(LOG_STDERR, gettext("(Warning) "
"%1$s is not a valid mechanism for %2$s."),
plist->name, pent->name);
} else if (!is_in_list(plist->name, pent->dislist)) {
/* Add this mechanism into the disabled list */
if ((pmech = create_mech(plist->name)) == NULL) {
rc = FAILURE;
break;
}
if (pent->dislist == NULL) {
pent->dislist = pmech;
} else {
phead = pent->dislist;
pent->dislist = pmech;
pmech->next = phead;
}
pent->dis_count++;
}
plist = plist->next;
}
return (rc);
}
/*
* Remove the mechanism passed, specified by mech, from the list of
* mechanisms, if present in the list. Else, do nothing.
*
* Returns B_TRUE if mechanism is present in the list.
*/
boolean_t
filter_mechlist(mechlist_t **pmechlist, const char *mech)
{
int cnt = 0;
mechlist_t *ptr, *pptr;
boolean_t mech_present = B_FALSE;
ptr = pptr = *pmechlist;
while (ptr != NULL) {
if (strncmp(ptr->name, mech, sizeof (mech_name_t)) == 0) {
mech_present = B_TRUE;
if (ptr == *pmechlist) {
pptr = *pmechlist = ptr->next;
free(ptr);
ptr = pptr;
} else {
pptr->next = ptr->next;
free(ptr);
ptr = pptr->next;
}
} else {
pptr = ptr;
ptr = ptr->next;
cnt++;
}
}
/* Only one entry is present */
if (cnt == 0)
*pmechlist = NULL;
return (mech_present);
}
/*
* Print out the mechanism policy for a kernel provider that has an entry
* in the kcf.conf file.
*
* The flag has_random is set to B_TRUE if the provider does random
* numbers. The flag has_mechs is set by the caller to B_TRUE if the provider
* has some mechanisms.
*
* If pent is NULL, the provider doesn't have a kcf.conf entry.
*/
void
print_kef_policy(char *provname, entry_t *pent, boolean_t has_random,
boolean_t has_mechs)
{
mechlist_t *ptr = NULL;
boolean_t rnd_disabled = B_FALSE;
if (pent != NULL) {
rnd_disabled = filter_mechlist(&pent->dislist, RANDOM);
ptr = pent->dislist;
}
(void) printf("%s:", provname);
if (has_mechs == B_TRUE) {
/*
* TRANSLATION_NOTE
* This code block may need to be modified a bit to avoid
* constructing the text message on the fly.
*/
(void) printf(gettext(" all mechanisms are enabled"));
if (ptr != NULL)
(void) printf(gettext(", except "));
while (ptr != NULL) {
(void) printf("%s", ptr->name);
ptr = ptr->next;
if (ptr != NULL)
(void) printf(",");
}
if (ptr == NULL)
(void) printf(".");
}
/*
* TRANSLATION_NOTE
* "random" is a keyword and not to be translated.
*/
if (rnd_disabled)
(void) printf(gettext(" %s is disabled."), "random");
else if (has_random)
(void) printf(gettext(" %s is enabled."), "random");
(void) printf("\n");
}
/*
* Check if a kernel software provider is in the kernel.
*
* Parameters:
* provname Provider name
* psoftlist_kernel Optional software provider list. If NULL, it will be
* obtained from get_soft_list().
* in_kernel Set to B_TRUE if device is in the kernel, else B_FALSE
*/
int
check_kernel_for_soft(char *provname, crypto_get_soft_list_t *psoftlist_kernel,
boolean_t *in_kernel)
{
char *ptr;
int i;
boolean_t psoftlist_allocated = B_FALSE;
if (provname == NULL) {
cryptoerror(LOG_STDERR, gettext("internal error."));
return (FAILURE);
}
if (psoftlist_kernel == NULL) {
if (get_soft_list(&psoftlist_kernel) == FAILURE) {
cryptodebug("failed to get the software provider list"
" from kernel.");
return (FAILURE);
}
psoftlist_allocated = B_TRUE;
}
*in_kernel = B_FALSE;
ptr = psoftlist_kernel->sl_soft_names;
for (i = 0; i < psoftlist_kernel->sl_soft_count; i++) {
if (strcmp(provname, ptr) == 0) {
*in_kernel = B_TRUE;
break;
}
ptr = ptr + strlen(ptr) + 1;
}
if (psoftlist_allocated)
free(psoftlist_kernel);
return (SUCCESS);
}
/*
* Check if a kernel hardware provider is in the kernel.
*
* Parameters:
* provname Provider name
* pdevlist Optional Hardware Crypto Device List. If NULL, it will be
* obtained from get_dev_list().
* in_kernel Set to B_TRUE if device is in the kernel, otherwise B_FALSE
*/
int
check_kernel_for_hard(char *provname,
crypto_get_dev_list_t *pdevlist, boolean_t *in_kernel)
{
char devname[MAXNAMELEN];
int inst_num;
int i;
boolean_t dev_list_allocated = B_FALSE;
if (provname == NULL) {
cryptoerror(LOG_STDERR, gettext("internal error."));
return (FAILURE);
}
if (split_hw_provname(provname, devname, &inst_num) == FAILURE) {
return (FAILURE);
}
if (pdevlist == NULL) {
if (get_dev_list(&pdevlist) == FAILURE) {
cryptoerror(LOG_STDERR, gettext("internal error."));
return (FAILURE);
}
dev_list_allocated = B_TRUE;
}
*in_kernel = B_FALSE;
for (i = 0; i < pdevlist->dl_dev_count; i++) {
if ((strcmp(pdevlist->dl_devs[i].le_dev_name, devname) == 0) &&
(pdevlist->dl_devs[i].le_dev_instance == inst_num)) {
*in_kernel = B_TRUE;
break;
}
}
if (dev_list_allocated)
free(pdevlist);
return (SUCCESS);
}
|