-
Notifications
You must be signed in to change notification settings - Fork 80
/
ldaputil.c
2523 lines (2307 loc) · 87.3 KB
/
ldaputil.c
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
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* mozldap_ldap_explode, mozldap_ldap_explode_dn, mozldap_ldap_explode_rdn
* are from the file ldap/libraries/libldap/getdn.c in the Mozilla LDAP C SDK
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* Copyright (c) 1994 Regents of the University of Michigan.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* ldaputil.c -- LDAP utility functions and wrappers */
#include <sys/socket.h>
#include <sys/param.h>
#include <unistd.h>
#include <pwd.h>
#include <libgen.h>
#include <pk11func.h>
#include "slap.h"
#include "prtime.h"
#include "prinrval.h"
#include "snmp_collator.h"
#if !defined(USE_OPENLDAP)
#include <ldap_ssl.h>
#include <ldappr.h>
#else
#ifdef HAVE_HEIMDAL_KERBEROS
#include <com_err.h>
#endif
#ifndef MAX_KEYTAB_NAME_LEN
#define MAX_KEYTAB_NAME_LEN 1100
#endif
/* need mutex around ldap_initialize - see https://fedorahosted.org/389/ticket/348 */
static PRCallOnceType ol_init_callOnce = {0, 0, 0};
static PRLock *ol_init_lock = NULL;
#if defined(USE_OPENLDAP)
extern void getSSLVersionRangeOL(int *min, int *max);
extern int getSSLVersionRange(char **min, char **max);
#endif
static PRStatus
internal_ol_init_init(void)
{
PR_ASSERT(NULL == ol_init_lock);
if ((ol_init_lock = PR_NewLock()) == NULL) {
PRErrorCode errorCode = PR_GetError();
slapi_log_err(SLAPI_LOG_ERR, "internal_ol_init_init", "PR_NewLock failed %d:%s\n",
errorCode, slapd_pr_strerror(errorCode));
return PR_FAILURE;
}
return PR_SUCCESS;
}
#endif
/* the server depends on the old, deprecated ldap_explode behavior which openldap
does not support - the use of the mozldap code should be discouraged as
there are issues that mozldap does not handle correctly. */
static char **mozldap_ldap_explode(const char *dn, const int notypes, const int nametype);
static char **mozldap_ldap_explode_dn(const char *dn, const int notypes);
static char **mozldap_ldap_explode_rdn(const char *rdn, const int notypes);
#ifdef HAVE_KRB5
static void clear_krb5_ccache(void);
#endif
/*
* Function: slapi_ldap_unbind()
* Purpose: release an LDAP session obtained from a call to slapi_ldap_init().
*/
void
slapi_ldap_unbind(LDAP *ld)
{
if (ld != NULL) {
ldap_unbind_ext(ld, NULL, NULL);
}
}
#if defined(USE_OPENLDAP)
/* mozldap ldap_init and ldap_url_parse accept a hostname in the form
host1[:port1]SPACEhost2[:port2]SPACEhostN[:portN]
where SPACE is a single space (0x20) character
for openldap, we have to convert this to a string like this:
PROTO://host1[:port1]/SPACEPROTO://host2[:port2]/SPACEPROTO://hostN[:portN]/
where PROTO is ldap or ldaps or ldapi
if proto is NULL, assume hostname_or_uri is really a valid ldap uri
*/
static char *
convert_to_openldap_uri(const char *hostname_or_uri, int port, const char *proto)
{
char *retstr = NULL;
char *my_copy = NULL;
char *start = NULL;
char *iter = NULL;
char *ptr = NULL;
char *s = NULL;
const char *brkstr = " ";
int done = 0;
if (!hostname_or_uri) {
return NULL;
}
if (slapi_is_ipv6_addr(hostname_or_uri)) {
/* We need to encapsulate the ipv6 addr with brackets */
my_copy = slapi_ch_smprintf("[%s]", hostname_or_uri);
} else {
my_copy = slapi_ch_strdup(hostname_or_uri);
}
/* see if hostname_or_uri is an ldap uri */
if (!proto && !PL_strncasecmp(my_copy, "ldap", 4)) {
start = my_copy + 4;
if ((*start == 's') || (*start == 'i')) {
start++;
}
if (!PL_strncmp(start, "://", 3)) {
*start = '\0';
proto = my_copy;
start += 3;
} else {
slapi_log_err(SLAPI_LOG_ERR, "convert_to_openldap_uri",
"The given LDAP URI [%s] is not valid\n", hostname_or_uri);
goto end;
}
} else if (!proto) {
slapi_log_err(SLAPI_LOG_ERR, "convert_to_openldap_uri",
"The given LDAP URI [%s] is not valid\n", hostname_or_uri);
goto end;
} else {
start = my_copy; /* just assume it's not a uri */
}
for (s = ldap_utf8strtok_r(my_copy, brkstr, &iter); s != NULL; s = ldap_utf8strtok_r(NULL, brkstr, &iter)) {
/* strtok will grab the '/' at the end of the uri, if any, so terminate parsing there */
if ((ptr = strchr(s, '/'))) {
*ptr = '\0';
done = 1;
}
if (retstr) {
retstr = PR_sprintf_append(retstr, "/ %s://%s", proto, s);
} else {
retstr = PR_smprintf("%s://%s", proto, s);
}
if (done) {
break;
}
}
/* add the port on the last one */
retstr = PR_sprintf_append(retstr, ":%d/", port);
end:
slapi_ch_free_string(&my_copy);
return retstr;
}
#endif /* USE_OPENLDAP */
const char *
slapi_urlparse_err2string(int err)
{
const char *s = "internal error";
switch (err) {
case 0:
s = "no error";
break;
case LDAP_URL_ERR_BADSCOPE:
s = "invalid search scope";
break;
case LDAP_URL_ERR_MEM:
s = "unable to allocate memory";
break;
case LDAP_URL_ERR_PARAM:
s = "bad parameter to an LDAP URL function";
break;
#if defined(USE_OPENLDAP)
case LDAP_URL_ERR_BADSCHEME:
s = "does not begin with ldap://, ldaps://, or ldapi://";
break;
case LDAP_URL_ERR_BADENCLOSURE:
s = "missing trailing '>' in enclosure";
break;
case LDAP_URL_ERR_BADURL:
s = "not a valid LDAP URL";
break;
case LDAP_URL_ERR_BADHOST:
s = "hostname part of url is not valid or not given";
break;
case LDAP_URL_ERR_BADATTRS:
s = "attribute list not formatted correctly or missing";
break;
case LDAP_URL_ERR_BADFILTER:
s = "search filter not correct";
break;
case LDAP_URL_ERR_BADEXTS:
s = "extensions not specified correctly";
break;
#else /* !USE_OPENLDAP */
case LDAP_URL_ERR_NOTLDAP:
s = "missing ldap:// or ldaps:// or ldapi://";
break;
case LDAP_URL_ERR_NODN:
s = "missing suffix";
break;
#endif
}
return (s);
}
/* there are various differences among url parsers - directory server
needs the ability to parse partial URLs - those with no dn - and
needs to be able to tell if it is a secure url (ldaps) or not */
int
slapi_ldap_url_parse(const char *url, LDAPURLDesc **ludpp, int require_dn, int *secure)
{
PR_ASSERT(url);
PR_ASSERT(ludpp);
int rc;
/* This blocks NULL getting to strlen via url_to_use later in the function. */
if (url == NULL) {
return LDAP_PARAM_ERROR;
}
const char *url_to_use = url;
#if defined(USE_OPENLDAP)
char *urlescaped = NULL;
#endif
if (secure) {
*secure = 0;
}
#if defined(USE_OPENLDAP)
/* openldap does not support the non-standard multi host:port URLs supported
by mozldap - so we have to fake out openldap - replace all spaces with %20 -
replace all but the last colon with %3A
Go to the 3rd '/' or to the end of the string (convert only the host:port part) */
if (url) {
char *p = strstr(url, "://");
if (p) {
int foundspace = 0;
int coloncount = 0;
char *lastcolon = NULL;
p += 3;
for (; *p && (*p != '/'); p++) {
if (*p == ' ') {
foundspace = 1;
}
if (*p == ':') {
coloncount++;
lastcolon = p;
}
}
if (foundspace) {
char *src = NULL, *dest = NULL;
/* have to convert url, len * 3 is way too much, but acceptable */
urlescaped = slapi_ch_calloc(strlen(url) * 3, sizeof(char));
dest = urlescaped;
/* copy the scheme */
src = strstr(url, "://");
src += 3;
memcpy(dest, url, src - url);
dest += (src - url);
/*
* we have to convert all spaces to %20 - we have to convert
* all colons except the last one to %3A
*/
for (; *src; ++src) {
if (src < p) {
if (*src == ' ') {
memcpy(dest, "%20", 3);
dest += 3;
} else if ((coloncount > 1) && (*src == ':') && (src != lastcolon)) {
memcpy(dest, "%3A", 3);
dest += 3;
} else {
*dest++ = *src;
}
} else {
*dest++ = *src;
}
}
*dest = '\0';
url_to_use = urlescaped;
}
}
}
#endif
#if defined(HAVE_LDAP_URL_PARSE_NO_DEFAULTS)
rc = ldap_url_parse_no_defaults(url_to_use, ludpp, require_dn);
if (!rc && *ludpp && secure) {
*secure = (*ludpp)->lud_options & LDAP_URL_OPT_SECURE;
}
#else /* openldap */
#if defined(HAVE_LDAP_URL_PARSE_EXT) && defined(LDAP_PVT_URL_PARSE_NONE) && defined(LDAP_PVT_URL_PARSE_NOEMPTY_DN)
rc = ldap_url_parse_ext(url_to_use, ludpp, require_dn ? LDAP_PVT_URL_PARSE_NONE : LDAP_PVT_URL_PARSE_NOEMPTY_DN);
#else
rc = ldap_url_parse(url_to_use, ludpp);
if ((rc || !*ludpp) && !require_dn) { /* failed - see if failure was due to missing dn */
size_t len = strlen(url_to_use);
/* assume the url is just scheme://host:port[/] - add the empty string
as the DN (adding a trailing / first if needed) and try to parse
again
*/
char *urlcopy;
if (len > 0) {
urlcopy = slapi_ch_smprintf("%s%s%s", url_to_use, (url_to_use[len - 1] == '/' ? "" : "/"), "");
} else {
/* When len == 0, this is effectively what we create ... */
urlcopy = slapi_ch_smprintf("/");
}
if (*ludpp) {
ldap_free_urldesc(*ludpp); /* free the old one, if any */
}
rc = ldap_url_parse(urlcopy, ludpp);
slapi_ch_free_string(&urlcopy);
if (0 == rc) { /* only problem was the DN - free it */
slapi_ch_free_string(&((*ludpp)->lud_dn));
}
}
#endif
if (!rc && *ludpp && secure) {
*secure = (*ludpp)->lud_scheme && !strcmp((*ludpp)->lud_scheme, "ldaps");
}
#endif /* openldap */
#if defined(USE_OPENLDAP)
if (urlescaped && (*ludpp) && (*ludpp)->lud_host) {
/* have to unescape lud_host - can unescape in place */
char *p = strstr((*ludpp)->lud_host, "://");
if (p) {
char *dest = NULL;
p += 3;
dest = p;
/* up to the first '/', unescape the host */
for (; *p && (*p != '/'); p++) {
if (!strncmp(p, "%20", 3)) {
*dest++ = ' ';
p += 2;
} else if (!strncmp(p, "%3A", 3)) {
*dest++ = ':';
p += 2;
} else {
*dest++ = *p;
}
}
/* just copy the remainder of the host, if any */
while (*p) {
*dest++ = *p++;
}
*dest = '\0';
}
}
slapi_ch_free_string(&urlescaped);
#endif
return rc;
}
#include <sasl.h>
int
slapi_ldap_get_lderrno(LDAP *ld, char **m, char **s)
{
int rc = LDAP_SUCCESS;
#if defined(USE_OPENLDAP)
ldap_get_option(ld, LDAP_OPT_RESULT_CODE, &rc);
if (m) {
ldap_get_option(ld, LDAP_OPT_MATCHED_DN, m);
}
if (s) {
#ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, s);
#else
ldap_get_option(ld, LDAP_OPT_ERROR_STRING, s);
#endif
}
#else /* !USE_OPENLDAP */
rc = ldap_get_lderrno(ld, m, s);
#endif
return rc;
}
void
slapi_ldif_put_type_and_value_with_options(char **out, const char *t, const char *val, int vlen, unsigned long options)
{
#if defined(USE_OPENLDAP)
/* openldap always wraps and always does conservative base64 encoding
we unwrap here, but clients will have to do their own base64 decode */
int type = LDIF_PUT_VALUE;
char *save = *out;
if (options & LDIF_OPT_VALUE_IS_URL) {
type = LDIF_PUT_URL;
}
ldif_sput(out, type, t, val, vlen);
if (options & LDIF_OPT_NOWRAP) {
/* modify out in place, stripping out continuation lines */
char *src = save;
char *dest = save;
for (; src < *out; ++src) {
if ((src < (*out - 2)) && !strncmp(src, "\n ", 2)) {
src += 2; /* skip continuation */
}
*dest++ = *src;
}
*out = dest; /* move 'out' back if we removed some continuation lines */
}
#else
ldif_put_type_and_value_with_options(out, (char *)t, (char *)val, vlen, options);
#endif
}
void
slapi_ldap_value_free(char **vals)
{
#if defined(USE_OPENLDAP)
slapi_ch_array_free(vals);
#else
ldap_value_free(vals);
#endif
}
int
slapi_ldap_count_values(char **vals)
{
#if defined(USE_OPENLDAP)
return ldap_count_values_len((struct berval **)vals);
#else
return ldap_count_values(vals);
#endif
}
int
slapi_ldap_create_proxyauth_control(
LDAP *ld __attribute__((unused)), /* only used to get current ber options */
const char *dn, /* proxy dn */
const char ctl_iscritical,
int usev2, /* use the v2 (.18) control instead */
LDAPControl **ctrlp /* value to return */
)
{
int rc = 0;
#if defined(USE_OPENLDAP)
BerElement *ber = NULL;
int beropts = 0;
char *berfmtstr = NULL;
char *ctrloid = NULL;
struct berval *bv = NULL;
/* note - there is currently no way to get the beroptions from the ld*,
so we just hard code it here */
beropts = LBER_USE_DER; /* openldap seems to use DER by default */
if (ctrlp == NULL) {
return LDAP_PARAM_ERROR;
}
if (NULL == dn) {
dn = "";
}
if (NULL == (ber = ber_alloc_t(beropts))) {
return LDAP_NO_MEMORY;
}
if (usev2) {
berfmtstr = "s";
ctrloid = LDAP_CONTROL_PROXIEDAUTH;
} else {
berfmtstr = "{s}";
ctrloid = LDAP_CONTROL_PROXYAUTH;
}
/* Curiously, LBER_ERROR is uint, but set to -1 (0xffffffffU), even though ber_printf is int ... */
if (LBER_ERROR == ber_printf(ber, berfmtstr, dn)) {
ber_free(ber, 1);
return LDAP_ENCODING_ERROR;
}
if (LBER_ERROR == ber_flatten(ber, &bv)) {
ber_bvfree(bv);
ber_free(ber, 1);
return LDAP_ENCODING_ERROR;
}
if (NULL == bv) {
ber_free(ber, 1);
return LDAP_NO_MEMORY;
}
rc = ldap_control_create(ctrloid, ctl_iscritical, bv, 1, ctrlp);
ber_bvfree(bv);
ber_free(ber, 1);
#else
if (usev2) {
rc = ldap_create_proxiedauth_control(ld, dn, ctrlp);
} else {
rc = ldap_create_proxyauth_control(ld, dn, ctl_iscritical, ctrlp);
}
#endif
return rc;
}
int
slapi_ldif_parse_line(
char *line,
struct berval *type,
struct berval *value,
int *freeval)
{
int rc;
#if defined(USE_OPENLDAP)
rc = ldif_parse_line2(line, type, value, freeval);
/* check that type and value are null terminated */
#else
int vlen;
rc = ldif_parse_line(line, &type->bv_val, &value->bv_val, &vlen);
type->bv_len = type->bv_val ? strlen(type->bv_val) : 0;
value->bv_len = vlen;
*freeval = 0; /* always returns in place */
#endif
return rc;
}
#if defined(USE_OPENLDAP)
static int
setup_ol_tls_conn(LDAP *ld, int clientauth)
{
char *certdir = config_get_certdir();
int optval = 0;
int ssl_strength = 0;
int rc = 0;
const char *cacert = NULL;
if (config_get_ssl_check_hostname()) {
ssl_strength = LDAP_OPT_X_TLS_HARD;
} else {
/* verify certificate only */
ssl_strength = LDAP_OPT_X_TLS_NEVER;
}
if ((rc = ldap_set_option(ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &ssl_strength))) {
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"failed: unable to set REQUIRE_CERT option to %d\n", ssl_strength);
}
if (slapi_client_uses_non_nss(ld)) {
cacert = slapi_get_cacertfile();
if (cacert) {
/* CA Cert PEM file exists. Set the path to openldap option. */
rc = ldap_set_option(ld, LDAP_OPT_X_TLS_CACERTFILE, cacert);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"Could not set CA cert path [%s]: %d:%s\n",
cacert, rc, ldap_err2string(rc));
}
}
if (slapi_client_uses_openssl(ld)) {
int32_t crlcheck = LDAP_OPT_X_TLS_CRL_NONE;
tls_check_crl_t tls_check_state = config_get_tls_check_crl();
if (tls_check_state == TLS_CHECK_PEER) {
crlcheck = LDAP_OPT_X_TLS_CRL_PEER;
} else if (tls_check_state == TLS_CHECK_ALL) {
crlcheck = LDAP_OPT_X_TLS_CRL_ALL;
}
/* Sets the CRL evaluation strategy. */
rc = ldap_set_option(ld, LDAP_OPT_X_TLS_CRLCHECK, &crlcheck);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"Could not set CRLCHECK [%d]: %d:%s\n",
crlcheck, rc, ldap_err2string(rc));
}
}
}
/* tell it where our cert db/file is */
if ((rc = ldap_set_option(ld, LDAP_OPT_X_TLS_CACERTDIR, certdir))) {
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"failed: unable to set CACERTDIR option to %s\n", certdir);
}
slapi_ch_free_string(&certdir);
#if defined(LDAP_OPT_X_TLS_PROTOCOL_MIN)
getSSLVersionRangeOL(&optval, NULL);
if ((rc = ldap_set_option(ld, LDAP_OPT_X_TLS_PROTOCOL_MIN, &optval))) {
char *minstr = NULL;
(void)getSSLVersionRange(&minstr, NULL);
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"failed: unable to set minimum TLS protocol level to %s\n", minstr);
slapi_ch_free_string(&minstr);
}
#endif /* LDAP_OPT_X_TLS_PROTOCOL_MIN */
if (clientauth) {
rc = slapd_SSL_client_auth(ld);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"failed: unable to setup connection for TLS/SSL EXTERNAL client cert authentication - %d\n", rc);
}
}
/* have to do this last - this creates the new TLS handle and sets/copies
all of the parameters set above into that TLS handle context - note
that optval is zero, meaning create a context for a client */
optval = 0;
if ((rc = ldap_set_option(ld, LDAP_OPT_X_TLS_NEWCTX, &optval))) {
slapi_log_err(SLAPI_LOG_ERR, "setup_ol_tls_conn",
"failed: unable to create new TLS context - %d\n", rc);
}
return rc;
}
#endif /* defined(USE_OPENLDAP) */
/*
Perform LDAP init and return an LDAP* handle. If ldapurl is given,
that is used as the basis for the protocol, host, port, and whether
to use starttls (given on the end as ldap://..../?????starttlsOID
If hostname is given, LDAP or LDAPS is assumed, and this will override
the hostname from the ldapurl, if any. If port is > 0, this is the
port number to use. It will override the port in the ldapurl, if any.
If no port is given in port or ldapurl, the default will be used based
on the secure setting (389 for ldap, 636 for ldaps, 389 for starttls)
secure takes 1 of 3 values - 0 means regular ldap, 1 means ldaps, 2
means regular ldap with starttls.
ldapi_socket is the ldapi file name
if this is given, and no other options are given, ldapi is assumed.
*/
/* util_sasl_path: the string argument for putenv.
It must be a global or a static */
char util_sasl_path[MAXPATHLEN];
LDAP *
slapi_ldap_init_ext(
const char *ldapurl, /* full ldap url */
const char *hostname, /* can also use this to override
host in url */
int port, /* can also use this to override port in url */
int secure, /* 0 for ldap, 1 for ldaps, 2 for starttls -
override proto in url */
int shared __attribute__((unused)), /* if true, LDAP* will be shared among multiple threads */
const char *ldapi_socket /* for ldapi */
)
{
LDAPURLDesc *ludp = NULL;
LDAP *ld = NULL;
int rc = 0;
int secureurl = 0;
int ldap_version3 = LDAP_VERSION3;
/* We need to provide a sasl path used for client connections, especially
if the server is not set up to be a sasl server - since mozldap provides
no way to override the default path programatically, we set the sasl
path to the environment variable SASL_PATH. */
char *configpluginpath = config_get_saslpath();
char *pluginpath = configpluginpath;
char *pp = NULL;
if (NULL == pluginpath || (*pluginpath == '\0')) {
slapi_log_err(SLAPI_LOG_SHELL, "slapi_ldap_init_ext",
"config_get_saslpath returns NULL\n");
pluginpath = ldaputil_get_saslpath();
}
if ('\0' == util_sasl_path[0] || /* first time */
NULL == (pp = strchr(util_sasl_path, '=')) || /* invalid arg for putenv */
(0 != strcmp(++pp, pluginpath)) /* sasl_path has been updated */) {
PR_snprintf(util_sasl_path, sizeof(util_sasl_path), "SASL_PATH=%s", pluginpath);
slapi_log_err(SLAPI_LOG_SHELL, "slapi_ldap_init_ext", "putenv(%s)\n", util_sasl_path);
putenv(util_sasl_path);
}
slapi_ch_free_string(&configpluginpath);
/* if ldapurl is given, parse it */
if (ldapurl && ((rc = slapi_ldap_url_parse(ldapurl, &ludp, 0, &secureurl)) || !ludp)) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"Could not parse given LDAP URL [%s] : error [%s]\n",
ldapurl, /* ldapurl cannot be NULL here */
slapi_urlparse_err2string(rc));
goto done;
}
/* use url host if no host given */
if (!hostname && ludp && ludp->lud_host) {
hostname = ludp->lud_host;
}
/* use url port if no port given */
if (!port && ludp && ludp->lud_port) {
port = ludp->lud_port;
}
/* use secure setting from url if none given */
if (!secure && ludp) {
if (secureurl) {
secure = SLAPI_LDAP_INIT_FLAG_SSL;
} else if (0 /* starttls option - not supported yet in LDAP URLs */) {
secure = SLAPI_LDAP_INIT_FLAG_startTLS;
}
}
/* ldap_url_parse doesn't yet handle ldapi */
/*
if (!ldapi_socket && ludp && ludp->lud_file) {
ldapi_socket = ludp->lud_file;
}
*/
#if defined(USE_OPENLDAP)
if (ldapurl) {
if (PR_SUCCESS != PR_CallOnce(&ol_init_callOnce, internal_ol_init_init)) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"Could not perform internal ol_init init\n");
rc = -1;
goto done;
}
PR_Lock(ol_init_lock);
rc = ldap_initialize(&ld, ldapurl);
PR_Unlock(ol_init_lock);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"Could not initialize LDAP connection to [%s]: %d:%s\n",
ldapurl, rc, ldap_err2string(rc));
goto done;
}
} else {
char *makeurl = NULL;
if (ldapi_socket) {
makeurl = slapi_ch_smprintf("ldapi://%s/", ldapi_socket);
} else { /* host port */
makeurl = convert_to_openldap_uri(hostname, port,
(secure == SLAPI_LDAP_INIT_FLAG_SSL ? "ldaps" : "ldap"));
}
if (PR_SUCCESS != PR_CallOnce(&ol_init_callOnce, internal_ol_init_init)) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"Could not perform internal ol_init init\n");
rc = -1;
goto done;
}
PR_Lock(ol_init_lock);
rc = ldap_initialize(&ld, makeurl);
PR_Unlock(ol_init_lock);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"Could not initialize LDAP connection to [%s]: %d:%s\n",
makeurl, rc, ldap_err2string(rc));
slapi_ch_free_string(&makeurl);
goto done;
}
slapi_ch_free_string(&makeurl);
}
if (config_get_connection_nocanon()) {
/*
* The NONCANON flag tells openldap to use the hostname specified in
* the ldap_initialize command, rather than looking up the
* hostname using gethostname or similar - this allows running
* sasl/gssapi tests on machines that don't have a canonical
* hostname (such as localhost.localdomain).
*/
if ((rc = ldap_set_option(ld, LDAP_OPT_X_SASL_NOCANON, LDAP_OPT_ON))) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"Could not set ldap option LDAP_OPT_X_SASL_NOCANON for (%s), error %d (%s)\n",
ldapurl, rc, ldap_err2string(rc));
}
}
#else /* !USE_OPENLDAP */
if (ldapi_socket) {
/* ldapi in mozldap client is not yet supported */
} else if (secure == SLAPI_LDAP_INIT_FLAG_SSL) {
ld = ldapssl_init(hostname, port, secure);
} else { /* regular ldap and/or starttls */
/*
* Leverage the libprldap layer to take care of all the NSPR
* integration.
* Note that ldapssl_init() uses libprldap implicitly.
*/
ld = prldap_init(hostname, port, shared);
}
#endif /* !USE_OPENLDAP */
/* must explicitly set version to 3 */
ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version3);
/* Update snmp interaction table */
if (hostname) {
if (ld == NULL) {
set_snmp_interaction_row((char *)hostname, port, -1);
} else {
set_snmp_interaction_row((char *)hostname, port, 0);
}
}
if (ld && !ldapi_socket) {
/*
* Set the outbound LDAP I/O timeout based on the server config.
*/
int io_timeout_ms = config_get_outbound_ldap_io_timeout();
if (io_timeout_ms > 0) {
#if defined(USE_OPENLDAP)
struct timeval tv;
tv.tv_sec = io_timeout_ms / 1000;
tv.tv_usec = (io_timeout_ms % 1000) * 1000;
if (LDAP_OPT_SUCCESS != ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv)) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"failed: unable to set outbound I/O timeout to %dms\n", io_timeout_ms);
slapi_ldap_unbind(ld);
ld = NULL;
goto done;
}
#else /* !USE_OPENLDAP */
if (prldap_set_session_option(ld, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT, io_timeout_ms) != LDAP_SUCCESS) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"failed: unable to set outbound I/O timeout to %dms\n", io_timeout_ms);
slapi_ldap_unbind(ld);
ld = NULL;
goto done;
}
#endif /* !USE_OPENLDAP */
}
/*
* Set SSL strength (server certificate validity checking).
*/
if (secure > 0) {
#if defined(USE_OPENLDAP)
if (setup_ol_tls_conn(ld, 0)) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"failed: unable to set SSL/TLS options\n");
}
#else
int ssl_strength = 0;
LDAP *myld = NULL;
/*
* We can only use the set functions below with a real
* LDAP* if it has already gone through ldapssl_init -
* so, use NULL if using starttls
*/
if (secure == SLAPI_LDAP_INIT_FLAG_SSL) {
myld = ld;
}
if (config_get_ssl_check_hostname()) {
/* check hostname against name in certificate */
ssl_strength = LDAPSSL_AUTH_CNCHECK;
} else {
/* verify certificate only */
ssl_strength = LDAPSSL_AUTH_CERT;
}
if ((rc = ldapssl_set_strength(myld, ssl_strength)) ||
(rc = ldapssl_set_option(myld, SSL_ENABLE_SSL2, PR_FALSE)) ||
(rc = ldapssl_set_option(myld, SSL_ENABLE_SSL3, PR_TRUE)) ||
(rc = ldapssl_set_option(myld, SSL_ENABLE_TLS, PR_TRUE))) {
int prerr = PR_GetError();
slapi_log_err(SLAPI_LOG_ERR, "slapi_ldap_init_ext",
"failed: unable to set SSL options (" SLAPI_COMPONENT_NAME_NSPR " error %d - %s)\n",
prerr, slapd_pr_strerror(prerr));
}
if (secure == SLAPI_LDAP_INIT_FLAG_SSL) {
/* tell bind code we are using SSL */
ldap_set_option(ld, LDAP_OPT_SSL, LDAP_OPT_ON);
}
#endif /* !USE_OPENLDAP */
}
}
if (ld && (secure == SLAPI_LDAP_INIT_FLAG_startTLS)) {
/*
* We don't have a way to stash context data with the LDAP*, so we
* stash the information in the client controls (currently unused).
* We don't want to open the connection in ldap_init, since that's
* not the semantic - the connection is not usually opened until
* the first operation is sent, which is usually the bind - or
* in this case, the start_tls - so we stash the start_tls so
* we can do it in slapi_ldap_bind - note that this will get
* cleaned up when the LDAP* is disposed of
*/
LDAPControl start_tls_dummy_ctrl;
LDAPControl **clientctrls = NULL;
/* returns copy of controls */
ldap_get_option(ld, LDAP_OPT_CLIENT_CONTROLS, &clientctrls);
start_tls_dummy_ctrl.ldctl_oid = START_TLS_OID;
start_tls_dummy_ctrl.ldctl_value.bv_val = NULL;
start_tls_dummy_ctrl.ldctl_value.bv_len = 0;
start_tls_dummy_ctrl.ldctl_iscritical = 0;
slapi_add_control_ext(&clientctrls, &start_tls_dummy_ctrl, 1);
/* set option frees old list and copies the new list */
ldap_set_option(ld, LDAP_OPT_CLIENT_CONTROLS, clientctrls);
ldap_controls_free(clientctrls); /* free the copy */
}
slapi_log_err(SLAPI_LOG_SHELL, "slapi_ldap_init_ext",
"Success: set up conn to [%s:%d]%s\n",
hostname, port,
(secure == SLAPI_LDAP_INIT_FLAG_startTLS) ? " using startTLS" : ((secure == SLAPI_LDAP_INIT_FLAG_SSL) ? " using SSL" : ""));
done:
ldap_free_urldesc(ludp);
return (ld);
}
char *
ldaputil_get_saslpath()
{
char *saslpath = getenv("SASL_PATH");
if (NULL == saslpath) {
#if defined(LINUX) && defined(__LP64__)
saslpath = "/usr/lib64/sasl2";
if (PR_SUCCESS != PR_Access(saslpath, PR_ACCESS_EXISTS)) {
#ifdef CPU_arm
/* the 64-bit ARMv8 architecture. */
saslpath = "/usr/lib/aarch64-linux-gnu";
#else
/* Try x86_64 gnu triplet */
saslpath = "/usr/lib/x86_64-linux-gnu";
#endif
}
#else
saslpath = "/usr/lib/sasl2";
if (PR_SUCCESS != PR_Access(saslpath, PR_ACCESS_EXISTS)) {
#ifdef CPU_arm
/* the latest 32 bit ARM architecture using the hard-float version of EABI. */
saslpath = "/usr/lib/arm-linux-gnueabihf";
if (PR_SUCCESS != PR_Access(saslpath, PR_ACCESS_EXISTS)) {
/* the 32 bit ARM architecture of EABI. */
saslpath = "/usr/lib/arm-linux-gnueabi";
}
#else
/* Try i386 gnu triplet */
saslpath = "/usr/lib/i386-linux-gnu";
#endif
}
#endif
}
return saslpath;
}
/*
* Function: slapi_ldap_init()
* Description: just like ldap_ssl_init() but also arranges for the LDAP
* session handle returned to be safely shareable by multiple threads
* if "shared" is non-zero.
* Returns:
* an LDAP session handle (NULL if some local error occurs).
*/
LDAP *
slapi_ldap_init(char *ldaphost, int ldapport, int secure, int shared)