forked from PADL/nss_ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap-nss.c
5144 lines (4373 loc) · 125 KB
/
ldap-nss.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
/* Copyright (C) 1997-2010 Luke Howard.
This file is part of the nss_ldap library.
Contributed by Luke Howard, <[email protected]>, 1997.
The nss_ldap library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The nss_ldap library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the nss_ldap library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
static char rcsId[] =
"$Id$";
#include "config.h"
#ifdef HAVE_PORT_BEFORE_H
#include <port_before.h>
#endif
#if defined(HAVE_THREAD_H) && !defined(_AIX)
#include <thread.h>
#elif defined(HAVE_PTHREAD_H)
#include <pthread.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <stdio.h>
#include <syslog.h>
#include <signal.h>
#include <fcntl.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <errno.h>
#ifdef HAVE_RESOLV_H
#include <resolv.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#include <netinet/in.h>
#ifdef HAVE_LBER_H
#include <lber.h>
#endif
#ifdef HAVE_LDAP_H
#include <ldap.h>
#endif
#ifdef HAVE_LDAP_SSL_H
#include <ldap_ssl.h>
#endif
#ifdef HAVE_GSSLDAP_H
#include <gssldap.h>
#endif
#ifdef HAVE_GSSSASL_H
#include <gsssasl.h>
#endif
/* Try to handle systems with both SASL libraries installed */
#if defined(HAVE_SASL_SASL_H) && defined(HAVE_SASL_AUXPROP_REQUEST)
#include <sasl/sasl.h>
#elif defined(HAVE_SASL_H)
#include <sasl.h>
#endif
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
#include "ldap-nss.h"
#include "ltf.h"
#include "util.h"
#include "dnsconfig.h"
#include "pagectrl.h"
/* Prefer the threads library over the pthreads facility unless running on AIX */
#if defined(HAVE_THREAD_H) && !defined(_AIX)
#ifdef HAVE_PTHREAD_ATFORK
#undef HAVE_PTHREAD_ATFORK
#endif
#endif
/* how many messages to retrieve results for */
#ifndef LDAP_MSG_ONE
#define LDAP_MSG_ONE 0x00
#endif
#ifndef LDAP_MSG_ALL
#define LDAP_MSG_ALL 0x01
#endif
#ifndef LDAP_MSG_RECEIVED
#define LDAP_MSG_RECEIVED 0x02
#endif
#ifdef HAVE_LDAP_LD_FREE
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
extern int ldap_ld_free (LDAP * ld, int close, LDAPControl **,
LDAPControl **);
#else
extern int ldap_ld_free (LDAP * ld, int close);
#endif /* OPENLDAP 2.x */
#endif /* HAVE_LDAP_LD_FREE */
NSS_LDAP_DEFINE_LOCK (__lock);
/*
* the configuration is read by the first call to do_open().
* Pointers to elements of the list are passed around but should not
* be freed.
*/
static char __configbuf[NSS_LDAP_CONFIG_BUFSIZ];
#ifdef HAVE_SIGACTION
static struct sigaction __stored_handler;
static int __sigaction_retval = -1;
#else
static void (*__sigpipe_handler) (int) = SIG_DFL;
#endif /* HAVE_SIGACTION */
/*
* SASL mechs setup
*/
static ldap_session_mech_setup_t sasl_setups[] = {
__nss_ldap_krb5_cache
};
/*
* Global LDAP session.
*/
static ldap_session_t __session =
{
NULL, /* LDAP session connection */
NULL, /* LDAP session configuration data */
0, /* Timestamp of last activity */
LS_UNINITIALIZED, /* LDAP session current state */
0, /* Index of URI used for this connection */
-1, /* Initial PID information */
-1, /* Initial EUID information */
NULL, /* Head of Opaque pointer list for extensions */
NULL /* SASL mechanism entry points */
/* There are 2 large data areas at the end of the structure for socket addresses */
};
/* Track initial operation of the library when threading active */
#if defined(HAVE_PTHREAD_ATFORK) || defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
static pthread_once_t __once = PTHREAD_ONCE_INIT;
#endif
#ifdef LBER_OPT_LOG_PRINT_FILE
static FILE *__debugfile;
#endif /* LBER_OPT_LOG_PRINT_FILE */
#ifdef HAVE_LDAPSSL_CLIENT_INIT
static int __ssl_initialized = 0;
#endif /* HAVE_LDAPSSL_CLIENT_INIT */
#if defined(HAVE_PTHREAD_ATFORK) || defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
# if defined(HAVE_PTHREAD_ATFORK)
# define ATFORK_DO pthread_atfork
# else
# define ATFORK_DO __libc_atfork
# endif
/*
* Prepare for fork (); lock mutex.
*/
static void do_atfork_prepare (void);
/*
* Forked in parent, unlock mutex.
*/
static void do_atfork_parent (void);
/*
* Forked in child; close LDAP socket, unlock mutex.
*/
static void do_atfork_child (void);
/*
* Install handlers for atfork, called once.
*/
static void do_atfork_setup (void);
#endif
/*
* Close the global session, sending an unbind.
*/
static void do_close (ldap_session_t *session);
/*
* Close the global session without sending an unbind.
*/
static void do_close_no_unbind (ldap_session_t *session);
/*
* Disable keepalive on a LDAP connection's socket.
*/
static void do_set_sockopts (ldap_session_t *session);
/*
* TLS routines: set global SSL session options.
*/
#if defined(HAVE_LDAP_START_TLS_S) || defined(HAVE_LDAP_START_TLS) || (defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS))
static int do_ssl_options (ldap_config_t * cfg);
static int do_start_tls (ldap_session_t * session);
#endif
/*
* Read and validate configuration file.
* Check current connection state and drop if any of
* connection has been snaffled by other software
* have forked (thread forked or process forked)
* idle time has elapsed since last used
*/
static NSS_STATUS do_check_init (ldap_session_t *session);
/*
* Read configuration file and initialize schema
*/
static NSS_STATUS do_init (ldap_session_t *session);
/*
* Open the global session
*/
static NSS_STATUS do_open (ldap_session_t *session);
/*
* Perform an asynchronous search.
*/
static int do_search (ldap_session_t * session, const char *base, int scope,
const char *filter, const char **attrs,
int sizelimit, int *);
/*
* Perform a synchronous search.
*/
static int do_search_s (ldap_session_t * session, const char *base, int scope,
const char *filter, const char **attrs,
int sizelimit, LDAPMessage **);
/*
* Fetch an LDAP result.
*/
static NSS_STATUS do_result (ldap_session_t *session, ent_context_t * ctx, int all);
/*
* Format a filter given a prototype.
*/
static NSS_STATUS do_filter (const ldap_args_t * args, const char *filterprot,
ldap_service_search_descriptor_t * sd,
char *filter, size_t filterlen,
char **dynamicFilter, const char **retFilter);
/*
* Parse a result, fetching new results until a successful parse
* or exceptional condition.
*/
static NSS_STATUS do_parse (ldap_session_t *session, ent_context_t * ctx,
void *result, char *buffer,
size_t buflen, int *errnop, parser_t parser);
/*
* Parse a result, fetching results from the result chain
* rather than the server.
*/
static NSS_STATUS do_parse_s (ldap_session_t *session, ent_context_t * ctx,
void *result, char *buffer,
size_t buflen, int *errnop, parser_t parser);
/*
* Function to be braced by reconnect harness. Used so we
* can apply the reconnect code to both asynchronous and
* synchronous searches.
*/
typedef int (*search_func_t) (ldap_session_t *, const char *, int, const char *,
const char **, int, void *);
/*
* Do a search with a reconnect harness.
*/
static NSS_STATUS
do_with_reconnect (ldap_session_t *session, const char *base, int scope,
const char *filter, const char **attrs, int sizelimit,
void *private, search_func_t func);
/*
* Map error from LDAP status code to NSS status code
*/
static NSS_STATUS do_map_error (int rc);
/*
* support the sasl interaction
*/
static int do_sasl_interact (LDAP * ld, unsigned flags, void *defaults, void *p);
/*
* Do a sasl bind with a defined timeout
*/
static int do_sasl_bind (ldap_session_t *session, int timelimit, const char *dn, const char *pw);
/*
* Do a bind with a defined timeout
*/
static int do_bind (ldap_session_t *session, int timelimit, const char *dn, const char *pw,
int with_sasl);
static int
do_get_our_socket (ldap_session_t *session, int *sd);
static int
do_dupfd (int oldfd, int newfd);
static void
do_drop_connection (ldap_session_t *session, int sd, int closeSd);
static inline int
__local_option (void *outvalue)
{
return LDAP_OPT_SUCCESS;
}
/*
* Define MACROS to handle all of the LDAP options as in-line code
* rather than conditions all over the package
* - Howard Wilkinson October 2009
*/
#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_DESC)
#define GET_SOCKET_DESCRIPTOR(__conn__, __sd__) \
(ldap_get_option ((__conn__), LDAP_OPT_DESC, (__sd__)))
#else
#define GET_SOCKET_DESCRIPTOR(__conn__, __sd__) \
(__local_option (*(__sd__) = (__conn__)->ld_sb.sb_sd))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_DESC)
#define SET_SOCKET_DESCRIPTOR(__conn__, __sd__) \
(ldap_set_option ((__conn__), LDAP_OPT_DESC, (__sd__)))
#else
#define SET_SOCKET_DESCRIPTOR(__conn__, __sd__) \
(__local_option ((__conn__)->ld_sb.sb_sd = *(__sd__)))
#endif
#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_PROTOCOL_VERSION)
#define GET_PROTOCOL_VERSION(__conn__, __version__) \
(ldap_get_option ((__conn__), LDAP_OPT_PROTOCOL_VERSION, (__version__)))
#else
#define GET_PROTOCOL_VERSION(__conn__, __version__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_PROTOCOL_VERSION)
#define SET_PROTOCOL_VERSION(__conn__, __version__) \
(ldap_set_option ((__conn__), LDAP_OPT_PROTOCOL_VERSION, (__version__)))
#else
#define SET_PROTOCOL_VERSION(__conn__, __version__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_ERROR_NUMBER)
#define GET_ERROR_NUMBER(__conn__, __errno__) \
(ldap_get_option ((__conn__), LDAP_OPT_ERROR_NUMBER, (__errno__)))
#else
#define GET_ERROR_NUMBER(__conn__, __errno__) \
(__local_option (*(__errno__) = (__conn__)->ld_errno))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_ERROR_NUMBER)
#define SET_ERROR_NUMBER(__conn__, __errno__) \
(ldap_set_option ((__conn__), LDAP_OPT_ERROR_NUMBER, (__errno__)))
#else
#define SET_ERROR_NUMBER(__conn__, __errno__) \
(__local_option ((__conn__)->ld_errno = *(__errno__))
#endif
#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_ERROR_STRING)
#define GET_ERROR_STRING(__conn__, __errstr__) \
(ldap_get_option ((__conn__), LDAP_OPT_ERROR_STRING, (__errstr__)))
#else
#define GET_ERROR_STRING(__conn__, __errstr__) \
(__local_option (*(__errstr__) = (__conn__)->ld_error))
#endif
#if defined(HAVE_LDAP_GET_OPTION) && defined(LDAP_OPT_MATCHED_DN)
#define GET_MATCHED_DN(__conn__, __dn__) \
(ldap_get_option ((__conn__), LDAP_OPT_MATCHED_DN, (__dn__)))
#else
#define GET_MATCHED_DN(__conn__, __dn__) \
(__local_option (*(__dn__) = (__conn__)->ld_matched))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_DEREF)
#define SET_DEREF(__conn__, __deref__) \
(ldap_set_option ((__conn__), LDAP_OPT_DEREF, (__deref__)))
#else
#define SET_DEREF(__conn__, __dref__) \
(__local_option ((__conn__)->ld_deref = *(__deref__)))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_TIMELIMIT)
#define SET_TIMELIMIT(__conn__, __timelimit__) \
(ldap_set_option ((__conn__), LDAP_OPT_TIMELIMIT, (__timelimit__)))
#else
#define SET_TIMELIMIT(__conn__, __timelimit__) \
(__local_option ((__conn__)->ld_timelimit = *(__timelimit__)))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined (LDAP_OPT_SIZELIMIT)
#define SET_SIZELIMIT(__conn__, __sizelimit__) \
(ldap_set_option ((__conn__), LDAP_OPT_SIZELIMIT, (__sizelimit__)))
#else
#define SET_SIZELIMIT(__conn__, __sizelimit__) \
(__local_option ((__conn__)->ld_sizelimit = *(__sizelimit__)))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_TIMEOUT)
#define SET_TIMEOUT(__conn__, __timeout__) \
(ldap_set_option ((__conn__), LDAP_OPT_TIMEOUT, (__timeout__)))
#else
#define SET_TIMEOUT(__conn__, __timeout__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_X_OPT_CONNECT_TIMEOUT)
/*
* This is a new option in the Netscape SDK which sets
* the TCP connect timeout. For want of a better value,
* we use the bind_timelimit to control this.
*/
#define SET_CONNECT_TIMEOUT(__conn__, __timeout__) \
(ldap_set_option ((__conn__), LDAP_X_OPT_CONNECT_TIMEOUT, (__timeout__)))
#else
#define SET_CONNECT_TIMEOUT(__conn__, __timeout__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_NETWORK_TIMEOUT)
#define SET_NETWORK_TIMEOUT(__conn__, __timeout__) \
(ldap_set_option ((__conn__), LDAP_OPT_NETWORK_TIMEOUT, (__timeout__)))
#else
#define SET_NETWORK_TIMEOUT(__conn__, __timeout__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_REFERRALS)
#define SET_REFERRALS(__conn__, __referrals__) \
(ldap_set_option ((__conn__), LDAP_OPT_REFERRALS, ((*(__referrals__)) ? LDAP_OPT_ON : LDAP_OPT_OFF)))
#else
#define SET_REFERRALS(__conn__, __referrals__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_RESTART)
#define SET_RESTART(__conn__, __restart__) \
(ldap_set_option ((__conn__), LDAP_OPT_RESTART, ((*(__restart__)) ? LDAP_OPT_ON : LDAP_OPT_OFF)))
#else
#define SET_RESTART(__conn__, __restart__) \
(__local_option (NULL))
#endif
/* Support this in Solaris 9 and others than do not define the OPTION macro */
/* not in Solaris 9? */
# ifndef LDAP_OPT_SSL
# define LDAP_OPT_SSL 0x0A
# endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_SSL)
#define SET_SSL(__conn__, __ssl__) \
(ldap_set_option ((__conn__), LDAP_OPT_SSL, ((*(__ssl__)) ? LDAP_OPT_ON : LDAP_OPT_OFF)))
#else
#define SET_SSL(__conn__, __ssl__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
#define SET_TLS(__conn__, __tls__) \
(ldap_set_option ((__conn__), LDAP_OPT_X_TLS, (__tls__)))
#else
#define SET_TLS(__conn__, __tls__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_RANDOM_FILE)
#define SET_TLS_RANDOM_FILE(__conn__, __randomfile__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_RANDOM_FILE, (__randomfile__)))
#else
#define SET_TLS_RANDOM_FILE(__conn__, __randfile) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_CACERTFILE)
#define SET_TLS_CACERTFILE(__conn__, __cacertfile__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTFILE, (__cacertfile__)))
#else
#define SET_TLS_CACERTFILE(__conn__, __certfile__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_CACERTDIR)
#define SET_TLS_CACERTDIR(__conn__, __cacertdir__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTDIR, (__cacertdir__)))
#else
#define SET_TLS_CACERTDIR(__conn__, __cacertdir__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_REQUIRE_CERT)
#define SET_TLS_REQUIRE_CERT(__conn__, __checkpeer__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, (__checkpeer__)))
#else
#define SET_TLS_REQUIRE_CERT(__conn__, __checkpeer__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_CIPHER_SUITE)
#define SET_TLS_CIPHER_SUITE(__conn__, __ciphers__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_CIPHER_SUITE, (__ciphers__)))
#else
#define SET_TLS_CIPHER_SUITE(__conn__, __ciphersuite__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_CERTFILE)
#define SET_TLS_CERTFILE(__conn__, __certfile__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_CERTFILE, (__certfile__)))
#else
#define SET_TLS_CERTFILE(__conn__, __certfile__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS_KEYFILE)
#define SET_TLS_KEYFILE(__conn__, __keyfile__) \
(ldap_set_option (NULL, LDAP_OPT_X_TLS_KEYFILE, (__keyfile__)))
#else
#define SET_TLS_KEYFILE(__conn_, __keyfile__) \
(__local_option (NULL))
#endif
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_SASL_SECPROPS)
#define SET_SASL_SECPROPS(__conn__, __sasl_secprops__) \
(ldap_set_option ((__conn__), LDAP_OPT_X_SASL_SECPROPS, (__sasl_secprops__)))
#else
#define SET_SASL_SECPROPS(__conn__, __sasl_secprops__) \
(__local_option (NULL))
#endif
const char *
__nss_ldap_status2string (NSS_STATUS stat)
{
switch (stat)
{
case NSS_TRYAGAIN: return "NSS_TRYAGAIN";
case NSS_UNAVAIL: return "NSS_UNAVAIL";
case NSS_NOTFOUND: return "NSS_NOTFOUND";
case NSS_SUCCESS: return "NSS_SUCCESS";
case NSS_RETURN: return "NSS_RETURN";
default: return "UNKNOWN";
}
}
static NSS_STATUS
do_map_error (int rc)
{
NSS_STATUS stat;
switch (rc)
{
case LDAP_SUCCESS:
case LDAP_SIZELIMIT_EXCEEDED:
case LDAP_TIMELIMIT_EXCEEDED:
stat = NSS_SUCCESS;
break;
case LDAP_NO_SUCH_ATTRIBUTE:
case LDAP_UNDEFINED_TYPE:
case LDAP_INAPPROPRIATE_MATCHING:
case LDAP_CONSTRAINT_VIOLATION:
case LDAP_TYPE_OR_VALUE_EXISTS:
case LDAP_INVALID_SYNTAX:
case LDAP_NO_SUCH_OBJECT:
case LDAP_ALIAS_PROBLEM:
case LDAP_INVALID_DN_SYNTAX:
case LDAP_IS_LEAF:
case LDAP_ALIAS_DEREF_PROBLEM:
case LDAP_FILTER_ERROR:
stat = NSS_NOTFOUND;
break;
case LDAP_SERVER_DOWN:
case LDAP_TIMEOUT:
case LDAP_UNAVAILABLE:
case LDAP_BUSY:
#ifdef LDAP_CONNECT_ERROR
case LDAP_CONNECT_ERROR:
#endif /* LDAP_CONNECT_ERROR */
stat = NSS_TRYAGAIN;
break;
case LDAP_LOCAL_ERROR:
case LDAP_INVALID_CREDENTIALS:
default:
stat = NSS_UNAVAIL;
break;
}
return stat;
}
/*
* Rebind functions.
*/
#if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
#if LDAP_SET_REBIND_PROC_ARGS == 3
static int
do_rebind (LDAP * ld, LDAP_CONST char *url, ber_tag_t request,
ber_int_t msgid, void *arg)
#else
static int
do_rebind (LDAP * ld, LDAP_CONST char *url, int request, ber_int_t msgid)
#endif
{
char *who = NULL, *cred = NULL;
int timelimit;
int with_sasl = 0;
uid_t euid;
ldap_config_t *cfg;
ldap_session_t *session = &__session;
int rc;
debug ("==> do_rebind");
cfg = session->ls_config;
euid = geteuid ();
if (euid == 0)
{
who = (cfg->ldc_rootbinddn != NULL) ? cfg->ldc_rootbinddn : cfg->ldc_binddn;
with_sasl = (cfg->ldc_rootsaslid != NULL) ? cfg->ldc_rootusesasl : cfg->ldc_usesasl;
if (with_sasl)
{
cred = (cfg->ldc_rootsaslid != NULL) ? cfg->ldc_rootsaslid : cfg->ldc_saslid;
}
else
{
cred = (cfg->ldc_rootbindpw != NULL) ? cfg->ldc_rootbindpw : cfg->ldc_bindpw;
}
}
else
{
who = cfg->ldc_binddn;
with_sasl = cfg->ldc_usesasl;
if (with_sasl)
{
cred = cfg->ldc_saslid;
}
else
{
cred = cfg->ldc_bindpw;
}
}
timelimit = cfg->ldc_bind_timelimit;
#ifdef HAVE_LDAP_START_TLS_S
if (cfg->ldc_ssl_on == SSL_START_TLS)
{
int version;
if (GET_PROTOCOL_VERSION (session->ls_conn, &version) == LDAP_OPT_SUCCESS)
{
if (version < LDAP_VERSION3)
{
version = LDAP_VERSION3;
SET_PROTOCOL_VERSION (session->ls_conn, &version);
}
}
if (do_start_tls (session) == LDAP_SUCCESS)
{
debug ("TLS startup succeeded");
}
else
{
debug ("TLS startup failed");
return NSS_UNAVAIL;
}
}
#endif /* HAVE_LDAP_START_TLS_S */
rc = do_bind (session, timelimit, with_sasl ? cred : who, with_sasl ? NULL : cred, with_sasl);
debug ("<== do_rebind");
return rc;
}
#else
#if LDAP_SET_REBIND_PROC_ARGS == 3
static int
do_rebind (LDAP * ld, char **whop, char **credp, int *methodp,
int freeit, void *arg)
#elif LDAP_SET_REBIND_PROC_ARGS == 2
static int
do_rebind (LDAP * ld, char **whop, char **credp, int *methodp, int freeit)
#endif
{
uid_t euid;
ldap_config_t *cfg;
ldap_session_t *session = &__session;
debug ("==> do_rebind");
cfg = session->ls_config;
euid = geteuid ();
if (freeit != 0)
{
if (*whop != NULL)
free (*whop);
if (*credp != NULL)
free (*credp);
}
/*
* If we are running as root and we have a root binddn
* then use the root binddn and root bindpw
* (use the ordinary bindpw if a root one has not been provided)
*/
if (euid == 0 && cfg->ldc_rootbinddn != NULL)
{
*whop = strdup(cfg->ldc_rootbinddn);
*credp = ((cfg->ldc_rootbindpw != NULL)
? strdup (cfg->ldc_rootbindpw)
: ((cfg->ldc_bindpw != NULL)
? strdup (cfg->ldc_bindpw)
: NULL));
}
/*
* If not running as root
* then use the base binddn and the bindpw (if supplied)
*/
else if (cfg->ldc_binddn != NULL)
{
*whop = strdup (cfg->ldc_binddn);
*credp = ((cfg->ldc_bindpw != NULL)
? strdup (cfg->ldc_bindpw)
: NULL);
}
else
{
*whop = NULL;
*credp = NULL;
}
*methodp = LDAP_AUTH_SIMPLE;
debug ("<== do_rebind");
return LDAP_SUCCESS;
}
#endif
#ifdef HAVE_NSSWITCH_H
/*
* Default destructor.
* The entry point for this function is the destructor in the dispatch
* table for the switch. Thus, it's safe to grab the mutex from this
* function.
*/
NSS_STATUS
_nss_ldap_default_destr (nss_backend_t * be, void *args)
{
debug ("==> _nss_ldap_default_destr");
if ((((nss_ldap_backend_t *) be)->state) != NULL)
{
_nss_ldap_enter ();
_nss_ldap_ent_context_release (&(((nss_ldap_backend_t *) be)->state));
_nss_ldap_leave ();
}
/* Ditch the backend. */
free (be);
debug ("<== _nss_ldap_default_destr");
return NSS_SUCCESS;
}
/*
* This is the default "constructor" which gets called from each
* constructor, in the NSS dispatch table.
*/
NSS_STATUS
_nss_ldap_default_constr (nss_ldap_backend_t * be)
{
debug ("==> _nss_ldap_default_constr");
be->state = NULL;
#ifdef HPUX
__thread_mutex_init (&__lock, NULL);
#endif
debug ("<== _nss_ldap_default_constr");
return NSS_SUCCESS;
}
#endif /* HAVE_NSSWITCH_H */
#if defined(HAVE_PTHREAD_ATFORK) || defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
static void
do_atfork_prepare (void)
{
debug ("==> do_atfork_prepare");
_nss_ldap_enter ();
debug ("<== do_atfork_prepare");
}
static void
do_atfork_parent (void)
{
debug ("==> do_atfork_parent");
_nss_ldap_leave ();
debug ("<== do_atfork_parent");
}
static void
do_atfork_child (void)
{
ldap_session_t *session = &__session;
sigset_t unblock, mask;
debug ("==> do_atfork_child");
sigemptyset(&unblock);
sigaddset(&unblock, SIGPIPE);
sigprocmask(SIG_UNBLOCK, &unblock, &mask);
do_close_no_unbind (session);
sigprocmask(SIG_SETMASK, &mask, NULL);
_nss_ldap_leave ();
debug ("<== do_atfork_child");
}
static void
do_atfork_setup (void)
{
debug ("==> do_atfork_setup");
(void) ATFORK_DO (do_atfork_prepare, do_atfork_parent, do_atfork_child);
debug ("<== do_atfork_setup");
}
static NSS_STATUS
do_thread_once()
{
debug ("==> do_thread_once");
#if defined(HAVE_PTHREAD_ONCE) && defined(HAVE_PTHREAD_ATFORK)
if (pthread_once (&__once, do_atfork_setup) != 0)
{
debug ("<== do_thread_once (pthread_once failed)");
return NSS_UNAVAIL;
}
#elif defined(HAVE_PTHREAD_ATFORK) && ( defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H) )
__libc_once (__once, do_atfork_setup);
#elif defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
/*
* Only install the pthread_atfork() handlers if
* we are linked against libpthreads. Otherwise,
* do close the session when the PID changes.
*/
if !(__pthread_once == NULL || __pthread_atfork == NULL)
__libc_once (__once, do_atfork_setup);
#endif
debug ("<== do_thread_once");
return NSS_SUCCESS;
}
static int
pthreading_active(void)
{
#if defined(HAVE_PTHREAD_ATFORK)
return 1;
#elif defined(HAVE_LIBC_LOCK_H) || defined(HAVE_BITS_LIBC_LOCK_H)
return (__pthread_once != NULL &&__pthread_atfork != NULL && __once != PTHREAD_ONCE_INIT);
#else
return 0;
#endif
}
#endif
/*
* Acquires global lock, blocks SIGPIPE.
*/
void
_nss_ldap_enter (void)
{
#ifdef HAVE_SIGACTION
struct sigaction new_handler;
memset (&new_handler, 0, sizeof (new_handler));
#if 0
/* XXX need to test for sa_sigaction, not on all platforms */
new_handler.sa_sigaction = NULL;
#endif
new_handler.sa_handler = SIG_IGN;
sigemptyset (&new_handler.sa_mask);
new_handler.sa_flags = 0;
#endif /* HAVE_SIGACTION */
debug ("==> _nss_ldap_enter");
NSS_LDAP_LOCK (__lock);
/*
* Patch for Debian Bug 130006:
* ignore SIGPIPE for all LDAP operations.
*
* The following bug was reintroduced in nss_ldap-213 and is fixed here:
* http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=84344
*
* See:
* http://www.gnu.org/software/libc/manual/html_node/Signal-and-Sigaction.html
* for more details.
*/
#ifdef HAVE_SIGACTION
__sigaction_retval = sigaction (SIGPIPE, &new_handler, &__stored_handler);
#elif defined(HAVE_SIGSET)
__sigpipe_handler = sigset (SIGPIPE, SIG_IGN);
#else
__sigpipe_handler = signal (SIGPIPE, SIG_IGN);
#endif /* HAVE_SIGSET */
debug ("<== _nss_ldap_enter");
return;
}
/*
* Releases global mutex, releases SIGPIPE.
*/
void
_nss_ldap_leave (void)
{
debug ("==> _nss_ldap_leave");
#ifdef HAVE_SIGACTION
if (__sigaction_retval == 0)
(void) sigaction (SIGPIPE, &__stored_handler, NULL);
#else
if (__sigpipe_handler != SIG_ERR && __sigpipe_handler != SIG_IGN)
{
# ifdef HAVE_SIGSET
(void) sigset (SIGPIPE, __sigpipe_handler);
# else
(void) signal (SIGPIPE, __sigpipe_handler);
# endif /* HAVE_SIGSET */
}
#endif /* HAVE_SIGACTION */
NSS_LDAP_UNLOCK (__lock);
debug ("<== _nss_ldap_leave");
return;
}
static void
do_set_sockopts (ldap_session_t *session)
{
/*
* Netscape SSL-enabled LDAP library does not
* return the real socket.
*/
#ifndef HAVE_LDAPSSL_CLIENT_INIT
int sd = -1;
debug ("==> do_set_sockopts");
if ((GET_SOCKET_DESCRIPTOR (session->ls_conn, &sd) == LDAP_OPT_SUCCESS) && (sd > 0))
{
int off = 0;
NSS_LDAP_SOCKLEN_T socknamelen = sizeof (NSS_LDAP_SOCKADDR_STORAGE);
NSS_LDAP_SOCKLEN_T peernamelen = sizeof (NSS_LDAP_SOCKADDR_STORAGE);