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
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
|
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* In addition, as a special exception, Red Hat, Inc. gives You the additional
* right to link the code of this Program with code not covered under the GNU
* General Public License ("Non-GPL Code") and to distribute linked combinations
* including the two, subject to the limitations in this paragraph. Non-GPL Code
* permitted under this exception must only link to the code of this Program
* through those well defined interfaces identified in the file named EXCEPTION
* found in the source code files (the "Approved Interfaces"). The files of
* Non-GPL Code may instantiate templates or use macros or inline functions from
* the Approved Interfaces without causing the resulting work to be covered by
* the GNU General Public License. Only Red Hat, Inc. may make changes or
* additions to the list of Approved Interfaces. You must obey the GNU General
* Public License in all respects for all of the Program code and other code used
* in conjunction with the Program except the Non-GPL Code covered by this
* exception. If you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you do not wish to
* provide this exception without modification, you must delete this exception
* statement from your version and license this file solely under the GPL without
* exception.
*
*
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
/* SSL-related stuff for slapd */
#if defined(NET_SSL)
#if defined( _WINDOWS )
#include <windows.h>
#include <winsock.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "proto-ntutil.h"
#include <string.h>
#include <stdlib.h>
#include <direct.h>
#include <io.h>
#endif
#ifdef LINUX
#include <sys/param.h>
#endif
#include <ssl.h>
#include <nss.h>
#include <key.h>
#include <sslproto.h>
#include "secmod.h"
#include <string.h>
#include <errno.h>
#define NEED_TOK_DES /* defines tokDes and ptokDes - see slap.h */
#include "slap.h"
#include "svrcore.h"
#include "fe.h"
#include <ldap_ssl.h> /* ldapssl_client_init */
#include "certdb.h"
/* For IRIX... */
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
extern char* slapd_SSL3ciphers;
extern symbol_t supported_ciphers[];
/* dongle_file_name is set in slapd_nss_init when we set the path for the
key, cert, and secmod files - the dongle file must be in the same directory
and use the same naming scheme
*/
static char* dongle_file_name = NULL;
static int _security_library_initialized = 0;
static int _ssl_listener_initialized = 0;
/* Our name for the internal token, must match PKCS-11 config data below */
static char *internalTokenName = "Internal (Software) Token";
static int stimeout;
static char *ciphers = NULL;
static char * configDN = "cn=encryption,cn=config";
/* Copied from libadmin/libadmin.h public/nsapi.h */
#define SERVER_KEY_NAME "Server-Key"
#define MAGNUS_ERROR_LEN 1024
#define LOG_WARN 0
#define LOG_FAILURE 3
#define FILE_PATHSEP '/'
/* ----------------------- Multiple cipher support ------------------------ */
#ifdef NET_SSL
static char **cipher_names = NULL;
typedef struct {
char *version;
char *name;
int num;
} cipherstruct;
static cipherstruct _conf_ciphers[] = {
{"SSL3","rc4", SSL_EN_RC4_128_WITH_MD5},
{"SSL3","rc4export", SSL_EN_RC4_128_EXPORT40_WITH_MD5},
{"SSL3","rc2", SSL_EN_RC2_128_CBC_WITH_MD5},
{"SSL3","rc2export", SSL_EN_RC2_128_CBC_EXPORT40_WITH_MD5},
/*{"idea", SSL_EN_IDEA_128_CBC_WITH_MD5}, */
{"SSL3","des", SSL_EN_DES_64_CBC_WITH_MD5},
{"SSL3","desede3", SSL_EN_DES_192_EDE3_CBC_WITH_MD5},
{"SSL3","rsa_rc4_128_md5", SSL_RSA_WITH_RC4_128_MD5},
{"SSL3","rsa_3des_sha", SSL_RSA_WITH_3DES_EDE_CBC_SHA},
{"SSL3","rsa_des_sha", SSL_RSA_WITH_DES_CBC_SHA},
{"SSL3","rsa_fips_3des_sha", SSL_RSA_FIPS_WITH_3DES_EDE_CBC_SHA},
{"SSL3","rsa_fips_des_sha", SSL_RSA_FIPS_WITH_DES_CBC_SHA},
{"SSL3","rsa_rc4_40_md5", SSL_RSA_EXPORT_WITH_RC4_40_MD5},
{"SSL3","rsa_rc2_40_md5", SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5},
{"SSL3","rsa_null_md5", SSL_RSA_WITH_NULL_MD5},
{"TLS","tls_rsa_export1024_with_rc4_56_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA},
{"TLS","tls_rsa_export1024_with_des_cbc_sha", TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA},
{"SSL3","fortezza", SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA},
{"SSL3","fortezza_rc4_128_sha", SSL_FORTEZZA_DMS_WITH_RC4_128_SHA},
{"SSL3","fortezza_null", SSL_FORTEZZA_DMS_WITH_NULL_SHA},
/*{"SSL3","dhe_dss_40_sha", SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA}, */
{"SSL3","dhe_dss_des_sha", SSL_DHE_DSS_WITH_DES_CBC_SHA},
{"SSL3","dhe_dss_3des_sha", SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA},
/*{"SSL3","dhe_rsa_40_sha", SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA}, */
{"SSL3","dhe_rsa_des_sha", SSL_DHE_RSA_WITH_DES_CBC_SHA},
{"SSL3","dhe_rsa_3des_sha", SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA},
{"TLS","tls_rsa_aes_128_sha", TLS_RSA_WITH_AES_128_CBC_SHA},
{"TLS","tls_dhe_dss_aes_128_sha", TLS_DHE_DSS_WITH_AES_128_CBC_SHA},
{"TLS","tls_dhe_rsa_aes_128_sha", TLS_DHE_RSA_WITH_AES_128_CBC_SHA},
{"TLS","tls_rsa_aes_256_sha", TLS_RSA_WITH_AES_256_CBC_SHA},
{"TLS","tls_dhe_dss_aes_256_sha", TLS_DHE_DSS_WITH_AES_256_CBC_SHA},
{"TLS","tls_dhe_rsa_aes_256_sha", TLS_DHE_RSA_WITH_AES_256_CBC_SHA},
/*{"TLS","tls_dhe_dss_1024_des_sha", TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA}, */
{"TLS","tls_dhe_dss_1024_rc4_sha", TLS_RSA_EXPORT1024_WITH_RC4_56_SHA},
{"TLS","tls_dhe_dss_rc4_128_sha", TLS_DHE_DSS_WITH_RC4_128_SHA},
{NULL, NULL, 0}
};
char ** getSupportedCiphers()
{
SSLCipherSuiteInfo info;
char *sep = "::";
int number_of_ciphers = sizeof (_conf_ciphers) /sizeof(cipherstruct);
int i;
if (cipher_names == NULL ) {
cipher_names = (char **) slapi_ch_calloc ((number_of_ciphers +1 ) , sizeof(char *));
for (i = 0 ; _conf_ciphers[i].name != NULL; i++ ) {
SSL_GetCipherSuiteInfo((PRUint16)_conf_ciphers[i].num,&info,sizeof(info));
cipher_names[i] = PR_smprintf("%s%s%s%s%s%s%s%s%d\0",_conf_ciphers[i].version,sep,_conf_ciphers[i].name,sep,info.symCipherName,sep,info.macAlgorithmName,sep,info.symKeyBits);
}
cipher_names[i] = NULL;
}
return cipher_names;
}
void
_conf_setallciphers(int active)
{
int x;
/* MLM - change: Because null_md5 is NOT encrypted at all, force
* them to activate it by name. */
for(x = 0; _conf_ciphers[x].name; x++) {
if(active && !strcmp(_conf_ciphers[x].name, "rsa_null_md5")) {
continue;
}
#ifdef NET_SSL
SSL_CipherPrefSetDefault(_conf_ciphers[x].num, active ? PR_TRUE : PR_FALSE);
#endif
}
}
char *
_conf_setciphers(char *ciphers)
{
char *t, err[MAGNUS_ERROR_LEN];
int x, active;
char *raw = ciphers;
/* Default is to activate all of them */
if(!ciphers || ciphers[0] == '\0') {
_conf_setallciphers(1);
return NULL;
}
/* Enable all the ciphers by default and the following while loop would disable the user disabled ones This is needed becuase we added a new set of ciphers in the table . Right now there is no support for this from the console */
_conf_setallciphers(1);
t = ciphers;
while(t) {
while((*ciphers) && (isspace(*ciphers))) ++ciphers;
switch(*ciphers++) {
case '+':
active = 1; break;
case '-':
active = 0; break;
default:
PR_snprintf(err, sizeof(err), "invalid ciphers <%s>: format is "
"+cipher1,-cipher2...", raw);
return slapi_ch_strdup(err);
}
if( (t = strchr(ciphers, ',')) )
*t++ = '\0';
if(!strcasecmp(ciphers, "all"))
_conf_setallciphers(active);
else {
for(x = 0; _conf_ciphers[x].name; x++) {
if(!strcasecmp(ciphers, _conf_ciphers[x].name)) {
SSL_CipherPrefSetDefault(_conf_ciphers[x].num, active ? PR_TRUE : PR_FALSE);
break;
}
}
if(!_conf_ciphers[x].name) {
PR_snprintf(err, sizeof(err), "unknown cipher %s", ciphers);
return slapi_ch_strdup(err);
}
}
if(t)
ciphers = t;
}
return NULL;
}
/* SSL Policy stuff */
/*
* SSLPLCY_Install
*
* Call the SSL_CipherPolicySet function for each ciphersuite.
*/
PRStatus
SSLPLCY_Install(void)
{
SECStatus s = 0;
#ifdef NS_DOMESTIC
s = NSS_SetDomesticPolicy();
#else
s = NSS_SetExportPolicy();
#endif
return s?PR_FAILURE:PR_SUCCESS;
}
#endif /* NET_SSL */
static void
slapd_SSL_report(int degree, char *fmt, va_list args)
{
char buf[2048];
PR_vsnprintf( buf, sizeof(buf), fmt, args );
LDAPDebug( LDAP_DEBUG_ANY, "SSL %s: %s\n",
(degree == LOG_FAILURE) ? "failure" : "alert",
buf, 0 );
}
void
slapd_SSL_error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
slapd_SSL_report(LOG_FAILURE, fmt, args);
exit(1);
}
void
slapd_SSL_warn(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
slapd_SSL_report(LOG_WARN, fmt, args);
va_end(args);
}
/**
* Get a particular entry
*/
static Slapi_Entry *
getConfigEntry( const char *dn, Slapi_Entry **e2 ) {
Slapi_DN sdn;
slapi_sdn_init_dn_byref( &sdn, dn );
slapi_search_internal_get_entry( &sdn, NULL, e2,
plugin_get_default_component_id());
slapi_sdn_done( &sdn );
return *e2;
}
/**
* Free an entry
*/
static void
freeConfigEntry( Slapi_Entry ** e ) {
if ( (e != NULL) && (*e != NULL) ) {
slapi_entry_free( *e );
*e = NULL;
}
}
/**
* Get a list of child DNs
*/
static char **
getChildren( char *dn ) {
Slapi_PBlock *new_pb = NULL;
Slapi_Entry **e;
int search_result = 1;
int nEntries = 0;
char **list = NULL;
new_pb = slapi_search_internal ( dn, LDAP_SCOPE_ONELEVEL,
"(objectclass=nsEncryptionModule)",
NULL, NULL, 0);
slapi_pblock_get( new_pb, SLAPI_NENTRIES, &nEntries);
if ( nEntries > 0 ) {
slapi_pblock_get( new_pb, SLAPI_PLUGIN_INTOP_RESULT, &search_result);
slapi_pblock_get( new_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &e);
if ( e != NULL ) {
int i;
list = (char **)slapi_ch_malloc( sizeof(*list) * (nEntries + 1));
for ( i = 0; e[i] != NULL; i++ ) {
list[i] = slapi_ch_strdup(slapi_entry_get_dn(e[i]));
}
list[nEntries] = NULL;
}
}
slapi_free_search_results_internal(new_pb);
slapi_pblock_destroy(new_pb );
return list;
}
/**
* Free a list of child DNs
*/
static void
freeChildren( char **list ) {
if ( list != NULL ) {
int i;
for ( i = 0; list[i] != NULL; i++ ) {
slapi_ch_free( (void **)(&list[i]) );
}
slapi_ch_free( (void **)(&list) );
}
}
static void
warn_if_no_cert_file(const char *filename)
{
PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
if (PR_SUCCESS != status) {
/* if file ends in -cert7.db and the corresponding -cert8.db exists, just
warn */
char *cert8 = slapi_ch_strdup(filename);
char *ptr;
if ((ptr = PL_strrstr(cert8, "-cert7.db"))) {
strcpy(ptr, "-cert8.db");
status = PR_Access(cert8, PR_ACCESS_READ_OK);
if (PR_SUCCESS == status) {
slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
"Notice: certificate DB file %s does not exist but %s does - suggest updating nscertfile\n",
filename, cert8);
}
}
slapi_ch_free_string(&cert8);
if (PR_SUCCESS != status) {
slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
"Warning: certificate DB file %s does not exist - SSL initialization will likely fail\n",
filename);
}
}
}
static void
warn_if_no_key_file(const char *path, const char *name)
{
char last = path[strlen(path)-1];
char *filename = slapi_ch_smprintf("%s%s%s", path, ((last == '/' || last == '\\') ? "" : "/"), name);
PRStatus status = PR_Access(filename, PR_ACCESS_READ_OK);
if (PR_SUCCESS != status) {
/* if file ends in -key3.db and the corresponding -key4.db exists, just
warn */
char *key4 = slapi_ch_strdup(filename);
char *ptr;
if ((ptr = PL_strrstr(key4, "-key3.db"))) {
strcpy(ptr, "-key4.db");
status = PR_Access(key4, PR_ACCESS_READ_OK);
if (PR_SUCCESS == status) {
slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
"Notice: key DB file %s does not exist but %s does - suggest updating nskeyfile\n",
filename, key4);
}
}
slapi_ch_free_string(&key4);
if (PR_SUCCESS != status) {
slapi_log_error(SLAPI_LOG_FATAL, "SSL Initialization",
"Warning: key DB file %s does not exist - SSL initialization will likely fail\n",
filename);
}
}
slapi_ch_free_string(&filename);
}
/*
* slapd_nss_init() is always called from main(), even if we do not
* plan to listen on a secure port. If config_available is 0, the
* config. entries from dse.ldif are NOT available (used only when
* running in referral mode).
* As of DS6.1, the init_ssl flag passed is ignored.
*/
int
slapd_nss_init(int init_ssl, int config_available)
{
SECStatus secStatus;
PRErrorCode errorCode;
char *keyfn = NULL;
char *certfn = NULL;
char *val = NULL;
char certPref[1024];
char keyPref[1024];
char path[1024];
int rv = 0;
int len = 0;
PRUint32 nssFlags = 0;
Slapi_Entry *ec = NULL;
char *instancedir;
if (config_available) {
getConfigEntry( configDN, &ec );
}
if ( ec != NULL ) {
certfn = slapi_entry_attr_get_charptr( ec, "nscertfile" );
keyfn = slapi_entry_attr_get_charptr( ec, "nskeyfile" );
slapi_entry_free (ec);
ec = NULL;
}
instancedir = config_get_instancedir();
PL_strncpyz(path, instancedir, sizeof(path));
slapi_ch_free_string(&instancedir);
/* make sure path does not end in the path separator character */
len = strlen(path);
if (path[len-1] == '/' || path[len-1] == '\\') {
path[len-1] = '\0';
}
/* get the server root from the path */
val = strrchr(path, '/');
if (!val) {
val = strrchr(path, '\\');
}
val++;
if(keyfn && certfn) {
if (is_abspath(certfn)) {
warn_if_no_cert_file(certfn);
/* first, initialize path from the certfn */
PL_strncpyz(path, certfn, sizeof(path));
/* extract path from cert db filename */
val = strrchr(path, '/');
if (!val) {
val = strrchr(path, '\\');
}
*val = 0; /* path is initialized */
/* next, init the cert db prefix */
val++;
PL_strncpyz(certPref, val, sizeof(certPref));
} else {
PL_strncpyz(val, certfn, sizeof(path)-(val-path));
warn_if_no_cert_file(path); /* assumes certfn is relative to server root */
val = strrchr(path, '/');
if (!val) {
val = strrchr(path, '\\');
}
val++;
PL_strncpyz(certPref, val, sizeof(certPref));
*val = '\0';
}
/* path represents now the base directory where cert, key, pin, and module db live */
/* richm - use strrstr to get the last occurance of -cert in the string, in case
the instance is named slapd-cert - the certdb name will be slapd-cert-cert7.db
*/
val = PL_strrstr(certPref, "-cert");
val++;
*val = '\0';
/* certPref keeps the prefix added to the cert db, usually "slapd-myserver-" */
/* now find the key db prefix */
val = strrchr(keyfn, '/');
if (!val) {
val = strrchr(keyfn, '\\');
}
if (val != NULL) {
val++;
} else {
val = keyfn;
}
PL_strncpyz(keyPref, val, sizeof(keyPref));
warn_if_no_key_file(path, keyPref);
/* richm - use strrstr to get the last occurance of -key in the string, in case
the instance is named slapd-key - the keydb name will be slapd-key-key3.db
*/
val = PL_strrstr(keyPref, "-key");
val++;
*val = '\0';
/* keypref keeps the prefix added to the key db, usually "slapd-myserver-" */
} else {
if ( config_get_security() ) {
/* Have to have the key and cert file names to enable an SSL port */
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to retrieve SSL "
"configuration information ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
"nskeyfile: %s, nscertfile: %s ",
errorCode, slapd_pr_strerror(errorCode),
(keyfn ? "found" : "not found"),
(certfn ? "found" : "not found"));
}
PR_snprintf(certPref, sizeof(certPref), "%s-", val);
PL_strncpyz(keyPref, certPref, sizeof(keyPref));
PL_strncpyz(val, "alias/", sizeof(path)-(val-path));
}
slapi_ch_free((void **) &certfn);
slapi_ch_free((void **) &keyfn);
/******** Initialise NSS *********/
nssFlags &= (~NSS_INIT_READONLY);
slapd_pk11_configurePKCS11(NULL, NULL, tokDes, ptokDes, NULL, NULL, NULL, NULL, 0, 0 );
secStatus = NSS_Initialize(path, certPref, keyPref, "secmod.db", nssFlags);
dongle_file_name = PR_smprintf("%s/%spin.txt", path, certPref);
if (secStatus != SECSuccess) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: NSS initialization failed ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
"path: %s, certdb prefix: %s, keydb prefix: %s.",
errorCode, slapd_pr_strerror(errorCode), path, certPref, keyPref);
rv = -1;
}
/****** end of NSS Initialization ******/
return rv;
}
/*
* slapd_ssl_init() is called from main() if we plan to listen
* on a secure port.
*/
int
slapd_ssl_init() {
PRErrorCode errorCode;
char ** family_list;
char *val = NULL;
char cipher_string[1024];
int rv = 0;
PK11SlotInfo *slot;
#ifndef _WIN32
SVRCOREStdPinObj *StdPinObj;
#else
SVRCOREFilePinObj *FilePinObj;
SVRCOREAltPinObj *AltPinObj;
SVRCORENTUserPinObj *NTUserPinObj;
#endif
Slapi_Entry *entry = NULL;
/* Get general information */
getConfigEntry( configDN, &entry );
val = slapi_entry_attr_get_charptr( entry, "nssslSessionTimeout" );
ciphers = slapi_entry_attr_get_charptr( entry, "nsssl3ciphers" );
/* We are currently using the value of sslSessionTimeout
for ssl3SessionTimeout, see SSL_ConfigServerSessionIDCache() */
/* Note from Tom Weinstein on the meaning of the timeout:
Timeouts are in seconds. '0' means use the default, which is
24hrs for SSL3 and 100 seconds for SSL2.
*/
if(!val) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to retrieve SSL "
"configuration information ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s): "
"nssslSessionTimeout: %s ",
errorCode, slapd_pr_strerror(errorCode),
(val ? "found" : "not found"));
slapi_ch_free((void **) &val);
slapi_ch_free((void **) &ciphers);
return -1;
}
stimeout = atoi(val);
slapi_ch_free((void **) &val);
#ifndef _WIN32
if ( SVRCORE_CreateStdPinObj(&StdPinObj, dongle_file_name, PR_TRUE) !=
SVRCORE_Success) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to create PinObj ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
SVRCORE_RegisterPinObj((SVRCOREPinObj *)StdPinObj);
#else
if (SVRCORE_CreateFilePinObj(&FilePinObj, dongle_file_name) !=
SVRCORE_Success) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to create FilePinObj ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
if (SVRCORE_CreateNTUserPinObj(&NTUserPinObj) != SVRCORE_Success){
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to create NTUserPinObj ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
if (SVRCORE_CreateAltPinObj(&AltPinObj, (SVRCOREPinObj *)FilePinObj,
(SVRCOREPinObj *)NTUserPinObj) != SVRCORE_Success) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to create AltPinObj ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
SVRCORE_RegisterPinObj((SVRCOREPinObj *)AltPinObj);
#endif /* _WIN32 */
if((family_list = getChildren(configDN))) {
char **family;
char *token;
char *activation;
for (family = family_list; *family; family++) {
token = NULL;
activation = NULL;
freeConfigEntry( &entry );
getConfigEntry( *family, &entry );
if ( entry == NULL ) {
continue;
}
activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
if((!activation) || (!strcasecmp(activation, "off"))) {
/* this family was turned off, goto next */
slapi_ch_free((void **) &activation);
continue;
}
slapi_ch_free((void **) &activation);
token = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
if( token ) {
if( !strcasecmp(token, "internal") ||
!strcasecmp(token, "internal (software)"))
slot = slapd_pk11_getInternalKeySlot();
else
slot = slapd_pk11_findSlotByName(token);
} else {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to get token ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
slapi_ch_free((void **) &token);
if (!slot) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to find slot ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
/* authenticate */
if(slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess)
{
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to authenticate ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
}
freeChildren( family_list );
}
freeConfigEntry( &entry );
if(SSLPLCY_Install() != PR_SUCCESS) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to set SSL export policy ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
/* ugaston- Cipher preferences must be set before any sslSocket is created
* for such sockets to take preferences into account.
*/
/* Step Three.5: Set SSL cipher preferences */
*cipher_string = 0;
if(ciphers && (*ciphers) && strcmp(ciphers, "blank"))
PL_strncpyz(cipher_string, ciphers, sizeof(cipher_string));
slapi_ch_free((void **) &ciphers);
if( NULL != (val = _conf_setciphers(cipher_string)) ) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to set SSL cipher "
"preference information: %s (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
val, errorCode, slapd_pr_strerror(errorCode));
rv = 3;
slapi_ch_free((void **) &val);
}
freeConfigEntry( &entry );
/* Introduce a way of knowing whether slapd_ssl_init has
* already been executed. */
_security_library_initialized = 1;
if ( rv != 0 )
return rv;
return 0;
}
int slapd_ssl_init2(PRFileDesc **fd, int startTLS)
{
PRFileDesc *pr_sock, *sock = (*fd);
PRErrorCode errorCode;
SECStatus rv = SECFailure;
char ** family_list;
CERTCertificate *cert = NULL;
SECKEYPrivateKey *key = NULL;
char errorbuf[BUFSIZ];
char *val = NULL;
char *default_val = NULL;
int nFamilies = 0;
SECStatus sslStatus;
int slapd_SSLclientAuth;
char* tmpDir;
Slapi_Entry *e = NULL;
/* turn off the PKCS11 pin interactive mode */
#ifndef _WIN32
SVRCOREStdPinObj *StdPinObj;
StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
SVRCORE_SetStdPinInteractive(StdPinObj, PR_FALSE);
#endif
errorbuf[0] = '\0';
/* Import pr fd into SSL */
pr_sock = SSL_ImportFD( NULL, sock );
if( pr_sock == (PRFileDesc *)NULL ) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to import NSPR "
"fd into SSL (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return 1;
}
(*fd) = pr_sock;
/* Step / Three.6 /
* - If in FIPS mode, authenticate to the token before
* doing anything else
*/
{
PK11SlotInfo *slot = slapd_pk11_getInternalSlot();
if (!slot) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to get internal slot ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
if(slapd_pk11_isFIPS()) {
if(slapd_pk11_authenticate(slot, PR_TRUE, NULL) != SECSuccess) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to authenticate ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
}
slapd_pk11_setSlotPWValues(slot, 0, 0);
}
/*
* Now, get the complete list of cipher families. Each family
* has a token name and personality name which we'll use to find
* appropriate keys and certs, and call SSL_ConfigSecureServer
* with.
*/
if((family_list = getChildren(configDN))) {
char **family;
char cert_name[1024];
char *token;
char *personality;
char *activation;
for (family = family_list; *family; family++) {
token = NULL;
personality = NULL;
activation = NULL;
getConfigEntry( *family, &e );
if ( e == NULL ) {
continue;
}
activation = slapi_entry_attr_get_charptr( e, "nssslactivation" );
if((!activation) || (!strcasecmp(activation, "off"))) {
/* this family was turned off, goto next */
slapi_ch_free((void **) &activation);
freeConfigEntry( &e );
continue;
}
slapi_ch_free((void **) &activation);
token = slapi_entry_attr_get_charptr( e, "nsssltoken" );
personality = slapi_entry_attr_get_charptr( e, "nssslpersonalityssl" );
if( token && personality ) {
if( !strcasecmp(token, "internal") ||
!strcasecmp(token, "internal (software)") )
PL_strncpyz(cert_name, personality, sizeof(cert_name));
else
/* external PKCS #11 token - attach token name */
PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
}
else {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to get cipher "
"family information. Missing nsssltoken or"
"nssslpersonalityssl in %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
*family, errorCode, slapd_pr_strerror(errorCode));
slapi_ch_free((void **) &token);
slapi_ch_free((void **) &personality);
freeConfigEntry( &e );
continue;
}
slapi_ch_free((void **) &token);
/* Step Four -- Locate the server certificate */
cert = slapd_pk11_findCertFromNickname(cert_name, NULL);
if (cert == NULL) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Can't find "
"certificate (%s) for family %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
cert_name, *family,
errorCode, slapd_pr_strerror(errorCode));
}
/* Step Five -- Get the private key from cert */
if( cert != NULL )
key = slapd_pk11_findKeyByAnyCert(cert, NULL);
if (key == NULL) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Unable to retrieve "
"private key for cert %s of family %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
cert_name, *family,
errorCode, slapd_pr_strerror(errorCode));
slapi_ch_free((void **) &personality);
CERT_DestroyCertificate(cert);
cert = NULL;
freeConfigEntry( &e );
continue;
}
/* Step Six -- Configure Secure Server Mode */
if(pr_sock) {
SECCertificateUsage returnedUsages;
rv = CERT_VerifyCertificateNow(
CERT_GetDefaultCertDB(), cert, PR_TRUE,
certificateUsageSSLServer,
SSL_RevealPinArg(pr_sock),
&returnedUsages);
if (SECSuccess == rv) {
if( slapd_pk11_fortezzaHasKEA(cert) == PR_TRUE ) {
rv = SSL_ConfigSecureServer(*fd, cert, key, kt_fortezza);
}
else {
rv = SSL_ConfigSecureServer(*fd, cert, key, kt_rsa);
}
if (SECSuccess != rv) {
errorCode = PR_GetError();
slapd_SSL_warn("ConfigSecureServer: "
"Server key/certificate is "
"bad for cert %s of family %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
cert_name, *family, errorCode,
slapd_pr_strerror(errorCode));
}
} else {
/* verify certificate failed */
/* If the common name in the subject DN for the certificate
* is not identical to the domain name passed in the
* hostname parameter, SECFailure. */
errorCode = PR_GetError();
slapd_SSL_warn("CERT_VerifyCertificateNow: "
"verify certificate failed "
"for cert %s of family %s ("
SLAPI_COMPONENT_NAME_NSPR
" error %d - %s)",
cert_name, *family, errorCode,
slapd_pr_strerror(errorCode));
}
}
if (cert) {
CERT_DestroyCertificate(cert);
cert = NULL;
}
slapi_ch_free((void **) &personality);
if (SECSuccess != rv) {
freeConfigEntry( &e );
continue;
}
nFamilies++;
freeConfigEntry( &e );
}
freeChildren( family_list );
}
if ( !nFamilies ) {
slapd_SSL_error("None of the cipher are valid");
return -1;
}
/* Step Seven -- Configure Server Session ID Cache */
tmpDir = slapd_get_tmp_dir();
slapi_log_error(
SLAPI_LOG_TRACE,
"slapd_ssl_init2",
"tmp dir = %s\n", tmpDir);
rv = SSL_ConfigServerSessionIDCache(0, stimeout, stimeout, tmpDir);
if (rv) {
errorCode = PR_GetError();
if (errorCode == ENOSPC) {
slapd_SSL_error("Config of server nonce cache failed, "
"out of disk space! Make more room in /tmp "
"and try again. (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
}
else {
slapd_SSL_error("Config of server nonce cache failed (error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
}
return rv;
}
sslStatus = SSL_OptionSet(pr_sock, SSL_SECURITY, PR_TRUE);
if (sslStatus != SECSuccess) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to enable security "
"on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
return -1;
}
sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL3, PR_TRUE);
if (sslStatus != SECSuccess) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to enable SSLv3 "
"on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
}
sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_TLS, PR_TRUE);
if (sslStatus != SECSuccess) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to enable TLS "
"on the imported socket (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
}
/* Explicitly disabling SSL2 - NGK */
sslStatus = SSL_OptionSet(pr_sock, SSL_ENABLE_SSL2, PR_FALSE);
/* Retrieve the SSL Client Authentication status from cn=config */
/* Set a default value if no value found */
getConfigEntry( configDN, &e );
val = NULL;
if ( e != NULL ) {
val = slapi_entry_attr_get_charptr( e, "nssslclientauth" );
}
if( !val ) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Cannot get SSL Client "
"Authentication status. No nsslclientauth in %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
configDN, errorCode, slapd_pr_strerror(errorCode));
switch( SLAPD_SSLCLIENTAUTH_DEFAULT ) {
case SLAPD_SSLCLIENTAUTH_OFF:
default_val = "off";
break;
case SLAPD_SSLCLIENTAUTH_ALLOWED:
default_val = "allowed";
break;
case SLAPD_SSLCLIENTAUTH_REQUIRED:
default_val = "required";
break;
default:
default_val = "allowed";
break;
}
val = default_val;
}
if( config_set_SSLclientAuth( "nssslclientauth", val, errorbuf,
CONFIG_APPLY ) != LDAP_SUCCESS ) {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Cannot set SSL Client "
"Authentication status to \"%s\", error (%s). "
"Supported values are \"off\", \"allowed\" "
"and \"required\". (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
val, errorbuf, errorCode, slapd_pr_strerror(errorCode));
}
if (val != default_val) {
slapi_ch_free_string(&val);
}
freeConfigEntry( &e );
if(( slapd_SSLclientAuth = config_get_SSLclientAuth()) != SLAPD_SSLCLIENTAUTH_OFF ) {
int err;
switch (slapd_SSLclientAuth) {
case SLAPD_SSLCLIENTAUTH_ALLOWED:
#ifdef SSL_REQUIRE_CERTIFICATE /* new feature */
if ((err = SSL_OptionSet (pr_sock, SSL_REQUIRE_CERTIFICATE, PR_FALSE)) < 0) {
PRErrorCode prerr = PR_GetError();
LDAPDebug (LDAP_DEBUG_ANY,
"SSL_OptionSet(SSL_REQUIRE_CERTIFICATE,PR_FALSE) %d "
SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
err, prerr, slapd_pr_strerror(prerr));
}
#endif
/* Give the client a clear opportunity to send her certificate: */
case SLAPD_SSLCLIENTAUTH_REQUIRED:
if ((err = SSL_OptionSet (pr_sock, SSL_REQUEST_CERTIFICATE, PR_TRUE)) < 0) {
PRErrorCode prerr = PR_GetError();
LDAPDebug (LDAP_DEBUG_ANY,
"SSL_OptionSet(SSL_REQUEST_CERTIFICATE,PR_TRUE) %d "
SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
err, prerr, slapd_pr_strerror(prerr));
}
default: break;
}
}
/* Introduce a way of knowing whether slapd_ssl_init2 has
* already been executed.
* The cases in which slapd_ssl_init2 is executed during an
* Start TLS operation are not taken into account, for it is
* the fact of being executed by the server's SSL listener socket
* that matters. */
if (!startTLS)
_ssl_listener_initialized = 1; /* --ugaston */
return 0;
}
/* richm 20020227
To do LDAP client SSL init, we need to do
static void
ldapssl_basic_init( void )
{
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
PR_SetConcurrency( 4 );
}
NSS_Init(certdbpath);
SSL_OptionSetDefault(SSL_ENABLE_SSL2, PR_FALSE);
SSL_OptionSetDefault(SSL_ENABLE_SSLdirs
3, PR_TRUE);
#ifdef NS_DOMESTIC
s = NSS_SetDomesticPolicy();
#elif NS_EXPORT
s = NSS_SetExportPolicy();
We already do pr_init, we don't need pr_setconcurrency, we already do nss_init and the rest
*/
int
slapd_SSL_client_init()
{
return 0;
}
static int
slapd_SSL_client_auth (LDAP* ld)
{
int rc = 0;
PRErrorCode errorCode;
char* pw = NULL;
char ** family_list;
Slapi_Entry *entry = NULL;
char cert_name[1024];
char *token = NULL;
#ifndef _WIN32
SVRCOREStdPinObj *StdPinObj;
#else
SVRCOREAltPinObj *AltPinObj;
#endif
SVRCOREError err = SVRCORE_Success;
if((family_list = getChildren(configDN))) {
char **family;
char *personality = NULL;
char *activation = NULL;
char *cipher = NULL;
for (family = family_list; *family; family++) {
getConfigEntry( *family, &entry );
if ( entry == NULL ) {
continue;
}
activation = slapi_entry_attr_get_charptr( entry, "nssslactivation" );
if((!activation) || (!strcasecmp(activation, "off"))) {
/* this family was turned off, goto next */
slapi_ch_free((void **) &activation);
freeConfigEntry( &entry );
continue;
}
slapi_ch_free((void **) &activation);
personality = slapi_entry_attr_get_charptr( entry, "nssslpersonalityssl" );
cipher = slapi_entry_attr_get_charptr( entry, "cn" );
if ( cipher && !strcasecmp(cipher, "RSA" )) {
char *ssltoken;
/* If there already is a token name, use it */
if (token) {
slapi_ch_free((void **) &personality);
slapi_ch_free((void **) &cipher);
freeConfigEntry( &entry );
continue;
}
ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
if( ssltoken && personality ) {
if( !strcasecmp(ssltoken, "internal") ||
!strcasecmp(ssltoken, "internal (software)") ) {
/* Translate config internal name to more
* readable form. Certificate name is just
* the personality for internal tokens.
*/
token = slapi_ch_strdup(internalTokenName);
PL_strncpyz(cert_name, personality, sizeof(cert_name));
slapi_ch_free((void **) &ssltoken);
} else {
/* external PKCS #11 token - attach token name */
/*ssltoken was already dupped and we don't need it anymore*/
token = ssltoken;
PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
}
} else {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to get cipher "
"family information. Missing nsssltoken or"
"nssslpersonalityssl in %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
*family, errorCode, slapd_pr_strerror(errorCode));
slapi_ch_free((void **) &ssltoken);
slapi_ch_free((void **) &personality);
slapi_ch_free((void **) &cipher);
freeConfigEntry( &entry );
continue;
}
} else { /* external PKCS #11 cipher */
char *ssltoken;
ssltoken = slapi_entry_attr_get_charptr( entry, "nsssltoken" );
if( token && personality ) {
/* free the old token and remember the new one */
if (token) slapi_ch_free((void **)&token);
token = ssltoken; /*ssltoken was already dupped and we don't need it anymore*/
/* external PKCS #11 token - attach token name */
PR_snprintf(cert_name, sizeof(cert_name), "%s:%s", token, personality);
} else {
errorCode = PR_GetError();
slapd_SSL_warn("Security Initialization: Failed to get cipher "
"family information. Missing nsssltoken or"
"nssslpersonalityssl in %s ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
*family, errorCode, slapd_pr_strerror(errorCode));
slapi_ch_free((void **) &ssltoken);
slapi_ch_free((void **) &personality);
slapi_ch_free((void **) &cipher);
freeConfigEntry( &entry );
continue;
}
}
slapi_ch_free((void **) &personality);
slapi_ch_free((void **) &cipher);
freeConfigEntry( &entry );
} /* end of for */
freeChildren( family_list );
}
/* Free config data */
/* We cannot allow NSS to cache outgoing client auth connections -
each client auth connection must have it's own non-shared SSL
connection to the peer so that it will go through the
entire handshake protocol every time including the use of its
own unique client cert - see bug 605457
*/
ldapssl_set_option(ld, SSL_NO_CACHE, PR_TRUE);
#ifndef _WIN32
StdPinObj = (SVRCOREStdPinObj *)SVRCORE_GetRegisteredPinObj();
err = SVRCORE_StdPinGetPin( &pw, StdPinObj, token );
#else
AltPinObj = (SVRCOREAltPinObj *)SVRCORE_GetRegisteredPinObj();
pw = SVRCORE_GetPin( (SVRCOREPinObj *)AltPinObj, token, PR_FALSE);
#endif
if ( err != SVRCORE_Success || pw == NULL) {
errorCode = PR_GetError();
slapd_SSL_warn("SSL client authentication cannot be used "
"(no password). (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
errorCode, slapd_pr_strerror(errorCode));
} else {
rc = ldapssl_enable_clientauth (ld, SERVER_KEY_NAME, pw, cert_name);
if (rc != 0) {
errorCode = PR_GetError();
slapd_SSL_warn("ldapssl_enable_clientauth(%s, %s) %i ("
SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
SERVER_KEY_NAME, cert_name, rc,
errorCode, slapd_pr_strerror(errorCode));
}
}
if (token) slapi_ch_free((void**)&token);
slapi_ch_free((void**)&pw);
LDAPDebug (LDAP_DEBUG_TRACE, "slapd_SSL_client_auth() %i\n", rc, 0, 0);
return rc;
}
int
slapd_simple_client_bind_s(LDAP* ld, char* DN, char* pw, int LDAPv)
{
int rc;
PRErrorCode errorCode;
ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *) &LDAPv);
rc = ldap_simple_bind_s (ld, DN, pw);
if (rc != 0) {
errorCode = PR_GetError();
slapd_SSL_warn("ldap_simple_bind_s(%s, %s) %i (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
DN, pw, rc, errorCode, slapd_pr_strerror(errorCode));
}
LDAPDebug (LDAP_DEBUG_TRACE, "slapd_simple_client_bind_s(%s, %i) %i\n", DN, LDAPv, rc);
return rc;
}
int
slapd_SSL_client_bind_s (LDAP* ld, char* DN, char* pw, int use_SSL, int LDAPv)
{
int rc;
struct berval noCred = {0, 0};
if (!use_SSL || LDAPv == LDAP_VERSION2) {
rc = slapd_simple_client_bind_s(ld, DN, pw, LDAPv);
} else {
LDAPDebug (
LDAP_DEBUG_TRACE,
"slapd_SSL_client_bind_s: Trying SSL Client Authentication\n",
0, 0, 0);
rc = slapd_SSL_client_auth(ld);
if(rc != 0)
{
LDAPDebug (
LDAP_DEBUG_TRACE,
"slapd_SSL_client_bind_s: SSL Client Auth Failed during replication Bind\n",
0, 0, 0);
return rc;
}
rc = ldap_sasl_bind_s (ld, "", LDAP_SASL_EXTERNAL, &noCred,
NULL /* LDAPControl **serverctrls */,
NULL /* LDAPControl **clientctrls */,
NULL /* struct berval **servercredp */);
}
LDAPDebug (
LDAP_DEBUG_TRACE,
"slapd_SSL_client_bind_s(%i,%i) %i\n", use_SSL, LDAPv, rc);
return rc;
}
int
slapd_sasl_ext_client_bind (LDAP* ld, int **msgid)
{
int rc;
PRErrorCode errorCode;
struct berval noCred = {0, 0};
LDAPDebug (
LDAP_DEBUG_TRACE,
"slapd_sasl_ext_client_bind: Trying SSL Client Authentication\n",
0, 0, 0);
rc = slapd_SSL_client_auth(ld);
if(rc != 0)
{
LDAPDebug (
LDAP_DEBUG_TRACE,
"slapd_sasl_ext_client_bind: SSL Client Auth Failed during replication Bind\n",
0, 0, 0);
return rc;
}
rc = ldap_sasl_bind (ld, "", LDAP_SASL_EXTERNAL, &noCred,
NULL,
NULL,
*msgid);
if (rc != 0) {
errorCode = PR_GetError();
slapd_SSL_warn("ldap_sasl_bind(\"\",LDAP_SASL_EXTERNAL) %i (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)",
rc, errorCode, slapd_pr_strerror(errorCode));
}
LDAPDebug (
LDAP_DEBUG_TRACE,
"slapd_sasl_ext_client_bind %i\n", rc, 0, 0);
return rc;
}
int slapd_Client_auth(LDAP* ld)
{
int rc=0;
rc = slapd_SSL_client_auth (ld);
return rc;
}
/* Function for keeping track of the SSL initialization status:
* - returns 1: when slapd_ssl_init has been executed
*/
int
slapd_security_library_is_initialized()
{
return _security_library_initialized;
}
/* Function for keeping track of the SSL listener socket initialization status:
* - returns 1: when slapd_ssl_init2 has been executed
*/
int
slapd_ssl_listener_is_initialized()
{
return _ssl_listener_initialized;
}
char* slapd_get_tmp_dir()
{
static char tmpdir[] = "/tmp";
static char tmp[256];
char* instanceDir;
#if defined( XP_WIN32 )
unsigned ilen;
char pch;
#endif
struct stat ffinfo;
tmp[0] = '\0';
if((instanceDir = config_get_instancedir()) == NULL)
{
slapi_log_error(
SLAPI_LOG_FATAL,
"slapd_get_tmp_dir",
"config_get_instancedir returns NULL Setting tmp dir to default\n");
#if defined( XP_WIN32 )
ilen = sizeof(tmp);
GetTempPath( ilen, tmp );
tmp[ilen-1] = (char)0;
ilen = strlen(tmp);
/* Remove trailing slash. */
pch = tmp[ilen-1];
if( pch == '\\' || pch == '/' )
tmp[ilen-1] = '\0';
return tmp;
#else
return( tmpdir );
#endif
}
PR_snprintf(tmp,sizeof(tmp),"%s/tmp",instanceDir);
slapi_ch_free_string(&instanceDir);
#if defined( XP_WIN32 )
for(ilen=0;ilen < strlen(tmp); ilen++)
{
if(tmp[ilen]=='/')
tmp[ilen]='\\';
}
#endif
if(stat(tmp,&ffinfo) == -1)
#if defined( XP_WIN32 )
if(CreateDirectory(tmp, NULL) == 0)
{
slapi_log_error(
SLAPI_LOG_FATAL,
"slapd_get_tmp_dir",
"CreateDirectory(%s, NULL) Error: %s\n",
tmp, strerror(errno));
return ( tmpdir );
}
#else
if(mkdir(tmp, 00770) == -1)
{
slapi_log_error(
SLAPI_LOG_FATAL,
"slapd_get_tmp_dir",
"mkdir(%s, 00770) Error: %s\n",
tmp, strerror(errno));
return ( tmpdir );
}
#endif
return ( tmp );
}
#endif /* NET_SSL */
|