1 /*        $NetBSD: ldap-int.h,v 1.3 2021/08/14 16:14:56 christos Exp $          */
2 
3 /*  ldap-int.h - defines & prototypes internal to the LDAP library */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2021 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /*  Portions Copyright (c) 1995 Regents of the University of Michigan.
19  *  All rights reserved.
20  */
21 
22 #ifndef   _LDAP_INT_H
23 #define   _LDAP_INT_H 1
24 
25 #ifndef NO_THREADS
26 #define LDAP_R_COMPILE 1
27 #endif
28 
29 #include "../liblber/lber-int.h"
30 #include "lutil.h"
31 #include "ldap_avl.h"
32 
33 #ifdef LDAP_R_COMPILE
34 #include <ldap_pvt_thread.h>
35 #endif
36 
37 #ifdef HAVE_CYRUS_SASL
38           /* the need for this should be removed */
39 #ifdef HAVE_SASL_SASL_H
40 #include <sasl/sasl.h>
41 #else
42 #include <sasl.h>
43 #endif
44 
45 #define SASL_MAX_BUFF_SIZE    (0xffffff)
46 #define SASL_MIN_BUFF_SIZE    4096
47 #endif
48 
49 /* for struct timeval */
50 #include <ac/time.h>
51 #include <ac/socket.h>
52 
53 #undef TV2MILLISEC
54 #define TV2MILLISEC(tv) (((tv)->tv_sec * 1000) + ((tv)->tv_usec/1000))
55 
56 /*
57  * Support needed if the library is running in the kernel
58  */
59 #if LDAP_INT_IN_KERNEL
60           /*
61            * Platform specific function to return a pointer to the
62            * process-specific global options.
63            *
64            * This function should perform the following functions:
65            *  Allocate and initialize a global options struct on a per process basis
66            *  Use callers process identifier to return its global options struct
67            *  Note: Deallocate structure when the process exits
68            */
69 #         define LDAP_INT_GLOBAL_OPT() ldap_int_global_opt()
70           struct ldapoptions *ldap_int_global_opt(void);
71 #else
72 #         define LDAP_INT_GLOBAL_OPT() (&ldap_int_global_options)
73 #endif
74 
75 /* if used from server code, ldap_debug already points elsewhere */
76 #ifndef ldap_debug
77 #define ldap_debug  ((LDAP_INT_GLOBAL_OPT())->ldo_debug)
78 #endif /* !ldap_debug */
79 
80 #define LDAP_INT_DEBUG
81 #include "ldap_log.h"
82 
83 #ifdef LDAP_DEBUG
84 
85 #define DebugTest( level ) \
86           ( ldap_debug & level )
87 
88 #define Debug0( level, fmt ) \
89           do { if ( DebugTest( (level) ) ) \
90           ldap_log_printf( NULL, (level), fmt ); \
91           } while ( 0 )
92 
93 #define Debug1( level, fmt, arg1 ) \
94           do { if ( DebugTest( (level) ) ) \
95           ldap_log_printf( NULL, (level), fmt, arg1 ); \
96           } while ( 0 )
97 
98 #define Debug2( level, fmt, arg1, arg2 ) \
99           do { if ( DebugTest( (level) ) ) \
100           ldap_log_printf( NULL, (level), fmt, arg1, arg2 ); \
101           } while ( 0 )
102 
103 #define Debug3( level, fmt, arg1, arg2, arg3 ) \
104           do { if ( DebugTest( (level) ) ) \
105           ldap_log_printf( NULL, (level), fmt, arg1, arg2, arg3 ); \
106           } while ( 0 )
107 
108 #else
109 
110 #define DebugTest( level )                                    (0 == 1)
111 #define Debug0( level, fmt )                                  ((void)0)
112 #define Debug1( level, fmt, arg1 )                            ((void)0)
113 #define Debug2( level, fmt, arg1, arg2 )                      ((void)0)
114 #define Debug3( level, fmt, arg1, arg2, arg3 )                ((void)0)
115 
116 #endif /* LDAP_DEBUG */
117 
118 #define LDAP_DEPRECATED 1
119 #include "ldap.h"
120 
121 #include "ldap_pvt.h"
122 
123 LDAP_BEGIN_DECL
124 
125 #define LDAP_URL_PREFIX         "ldap://"
126 #define LDAP_URL_PREFIX_LEN     STRLENOF(LDAP_URL_PREFIX)
127 #define PLDAP_URL_PREFIX      "pldap://"
128 #define PLDAP_URL_PREFIX_LEN  STRLENOF(PLDAP_URL_PREFIX)
129 #define LDAPS_URL_PREFIX      "ldaps://"
130 #define LDAPS_URL_PREFIX_LEN  STRLENOF(LDAPS_URL_PREFIX)
131 #define PLDAPS_URL_PREFIX     "pldaps://"
132 #define PLDAPS_URL_PREFIX_LEN STRLENOF(PLDAPS_URL_PREFIX)
133 #define LDAPI_URL_PREFIX      "ldapi://"
134 #define LDAPI_URL_PREFIX_LEN  STRLENOF(LDAPI_URL_PREFIX)
135 #ifdef LDAP_CONNECTIONLESS
136 #define LDAPC_URL_PREFIX      "cldap://"
137 #define LDAPC_URL_PREFIX_LEN  STRLENOF(LDAPC_URL_PREFIX)
138 #endif
139 #define LDAP_URL_URLCOLON     "URL:"
140 #define LDAP_URL_URLCOLON_LEN STRLENOF(LDAP_URL_URLCOLON)
141 
142 #define LDAP_REF_STR                    "Referral:\n"
143 #define LDAP_REF_STR_LEN      STRLENOF(LDAP_REF_STR)
144 #define LDAP_LDAP_REF_STR     LDAP_URL_PREFIX
145 #define LDAP_LDAP_REF_STR_LEN LDAP_URL_PREFIX_LEN
146 
147 #define LDAP_DEFAULT_REFHOPLIMIT 5
148 
149 #define LDAP_BOOL_REFERRALS             0
150 #define LDAP_BOOL_RESTART               1
151 #define LDAP_BOOL_TLS                             3
152 #define   LDAP_BOOL_CONNECT_ASYNC                 4
153 #define   LDAP_BOOL_SASL_NOCANON                  5
154 #define   LDAP_BOOL_KEEPCONN            6
155 
156 #define LDAP_BOOLEANS         unsigned long
157 #define LDAP_BOOL(n)          ((LDAP_BOOLEANS)1 << (n))
158 #define LDAP_BOOL_GET(lo, bool)         \
159           ((lo)->ldo_booleans & LDAP_BOOL(bool) ? -1 : 0)
160 #define LDAP_BOOL_SET(lo, bool) ((lo)->ldo_booleans |= LDAP_BOOL(bool))
161 #define LDAP_BOOL_CLR(lo, bool) ((lo)->ldo_booleans &= ~LDAP_BOOL(bool))
162 #define LDAP_BOOL_ZERO(lo) ((lo)->ldo_booleans = 0)
163 
164 /*
165  * This structure represents both ldap messages and ldap responses.
166  * These are really the same, except in the case of search responses,
167  * where a response has multiple messages.
168  */
169 
170 struct ldapmsg {
171           ber_int_t           lm_msgid; /* the message id */
172           ber_tag_t           lm_msgtype;         /* the message type */
173           BerElement          *lm_ber;  /* the ber encoded message contents */
174           struct ldapmsg      *lm_chain;          /* for search - next msg in the resp */
175           struct ldapmsg      *lm_chain_tail;
176           struct ldapmsg      *lm_next; /* next response */
177           time_t    lm_time;  /* used to maintain cache */
178 };
179 
180 #ifdef HAVE_TLS
181 struct ldaptls {
182           char                *lt_certfile;
183           char                *lt_keyfile;
184           char                *lt_dhfile;
185           char                *lt_cacertfile;
186           char                *lt_cacertdir;
187           char                *lt_ciphersuite;
188           char                *lt_crlfile;
189           char                *lt_randfile;       /* OpenSSL only */
190           char                *lt_ecname;                   /* OpenSSL only */
191           int                 lt_protocol_min;
192           int                 lt_protocol_max;
193           struct berval       lt_cacert;
194           struct berval       lt_cert;
195           struct berval       lt_key;
196 };
197 #endif
198 
199 typedef struct ldaplist {
200           struct ldaplist *ll_next;
201           void *ll_data;
202 } ldaplist;
203 
204 /*
205  * LDAP Client Source IP structure
206  */
207 typedef struct ldapsourceip {
208           char      *local_ip_addrs;
209           struct in_addr      ip4_addr;
210           unsigned short      has_ipv4;
211 #ifdef LDAP_PF_INET6
212           struct in6_addr     ip6_addr;
213           unsigned short      has_ipv6;
214 #endif
215 } ldapsourceip;
216 
217 /*
218  * structure representing get/set'able options
219  * which have global defaults.
220  * Protect access to this struct with ldo_mutex
221  * ldap_log.h:ldapoptions_prefix must match the head of this struct.
222  */
223 struct ldapoptions {
224           short ldo_valid;
225 #define LDAP_UNINITIALIZED    0x0
226 #define LDAP_INITIALIZED      0x1
227 #define LDAP_VALID_SESSION    0x2
228 #define LDAP_TRASHED_SESSION  0xFF
229           int   ldo_debug;
230 
231           ber_int_t           ldo_version;
232           ber_int_t           ldo_deref;
233           ber_int_t           ldo_timelimit;
234           ber_int_t           ldo_sizelimit;
235 
236           /* per API call timeout */
237           struct timeval                ldo_tm_api;
238           struct timeval                ldo_tm_net;
239 
240           LDAPURLDesc *ldo_defludp;
241           int                 ldo_defport;
242           char*     ldo_defbase;
243           char*     ldo_defbinddn;      /* bind dn */
244 
245           /*
246            * Per connection tcp-keepalive settings (Linux only,
247            * ignored where unsupported)
248            */
249           ber_int_t ldo_keepalive_idle;
250           ber_int_t ldo_keepalive_probes;
251           ber_int_t ldo_keepalive_interval;
252 
253           /*
254            * Per connection tcp user timeout (Linux >= 2.6.37 only,
255            * ignored where unsupported)
256            */
257           ber_uint_t ldo_tcp_user_timeout;
258 
259           int                 ldo_refhoplimit;    /* limit on referral nesting */
260 
261           /* LDAPv3 server and client controls */
262           LDAPControl         **ldo_sctrls;
263           LDAPControl **ldo_cctrls;
264 
265           /* LDAP rebind callback function */
266           LDAP_REBIND_PROC *ldo_rebind_proc;
267           void *ldo_rebind_params;
268           LDAP_NEXTREF_PROC *ldo_nextref_proc;
269           void *ldo_nextref_params;
270           LDAP_URLLIST_PROC *ldo_urllist_proc;
271           void *ldo_urllist_params;
272 
273           /* LDAP connection callback stack */
274           ldaplist *ldo_conn_cbs;
275 
276           LDAP_BOOLEANS ldo_booleans;   /* boolean options */
277 
278 #define LDAP_LDO_NULLARG      ,0,0,0,0 ,{0},{0} ,0,0,0,0, 0,0,0,0,0, 0,0, 0,0,0,0,0,0, 0, 0
279 
280           /* LDAP user configured bind IPs */
281           struct ldapsourceip ldo_local_ip_addrs;
282 
283 #ifdef LDAP_PF_INET6
284 #define LDAP_LDO_SOURCEIP_NULLARG       ,{0,0,0,0,0}
285 #else
286 #define LDAP_LDO_SOURCEIP_NULLARG       ,{0,0,0}
287 #endif
288 
289 #ifdef LDAP_CONNECTIONLESS
290 #define   LDAP_IS_UDP(ld)               ((ld)->ld_options.ldo_is_udp)
291           void*                         ldo_peer; /* struct sockaddr* */
292           char*                         ldo_cldapdn;
293           int                           ldo_is_udp;
294 #define   LDAP_LDO_CONNECTIONLESS_NULLARG         ,0,0,0
295 #else
296 #define   LDAP_LDO_CONNECTIONLESS_NULLARG
297 #endif
298 
299 #ifdef HAVE_TLS
300           /* tls context */
301           void                *ldo_tls_ctx;
302           LDAP_TLS_CONNECT_CB *ldo_tls_connect_cb;
303           void*                         ldo_tls_connect_arg;
304           struct ldaptls ldo_tls_info;
305 #define ldo_tls_certfile      ldo_tls_info.lt_certfile
306 #define ldo_tls_keyfile       ldo_tls_info.lt_keyfile
307 #define ldo_tls_dhfile        ldo_tls_info.lt_dhfile
308 #define ldo_tls_ecname        ldo_tls_info.lt_ecname
309 #define ldo_tls_cacertfile    ldo_tls_info.lt_cacertfile
310 #define ldo_tls_cacertdir     ldo_tls_info.lt_cacertdir
311 #define ldo_tls_ciphersuite   ldo_tls_info.lt_ciphersuite
312 #define ldo_tls_protocol_min  ldo_tls_info.lt_protocol_min
313 #define ldo_tls_protocol_max  ldo_tls_info.lt_protocol_max
314 #define ldo_tls_crlfile       ldo_tls_info.lt_crlfile
315 #define ldo_tls_randfile      ldo_tls_info.lt_randfile
316 #define ldo_tls_cacert        ldo_tls_info.lt_cacert
317 #define ldo_tls_cert          ldo_tls_info.lt_cert
318 #define ldo_tls_key ldo_tls_info.lt_key
319           int                           ldo_tls_mode;
320           int                           ldo_tls_require_cert;
321           int                           ldo_tls_impl;
322           int                           ldo_tls_crlcheck;
323           int                           ldo_tls_require_san;
324           char                *ldo_tls_pin_hashalg;
325           struct berval       ldo_tls_pin;
326 #define LDAP_LDO_TLS_NULLARG ,0,0,0,{0,0,0,0,0,0,0,0,0},0,0,0,0,0,0,{0,0}
327 #else
328 #define LDAP_LDO_TLS_NULLARG
329 #endif
330 
331 #ifdef HAVE_CYRUS_SASL
332           char*     ldo_def_sasl_mech;            /* SASL Mechanism(s) */
333           char*     ldo_def_sasl_realm;           /* SASL realm */
334           char*     ldo_def_sasl_authcid;         /* SASL authentication identity */
335           char*     ldo_def_sasl_authzid;         /* SASL authorization identity */
336 
337           /* SASL Security Properties */
338           struct sasl_security_properties         ldo_sasl_secprops;
339           int ldo_sasl_cbinding;
340 #define LDAP_LDO_SASL_NULLARG ,0,0,0,0,{0},0
341 #else
342 #define LDAP_LDO_SASL_NULLARG
343 #endif
344 
345 #ifdef HAVE_GSSAPI
346           unsigned ldo_gssapi_flags;
347 #define LDAP_GSSAPI_OPT_DO_NOT_FREE_GSS_CONTEXT   0x0001
348 #define LDAP_GSSAPI_OPT_ALLOW_REMOTE_PRINCIPAL    0x0002
349           unsigned ldo_gssapi_options;
350 #define LDAP_LDO_GSSAPI_NULLARG ,0,0
351 #else
352 #define LDAP_LDO_GSSAPI_NULLARG
353 #endif
354 
355 #ifdef LDAP_R_COMPILE
356           ldap_pvt_thread_mutex_t       ldo_mutex;
357 #define LDAP_LDO_MUTEX_NULLARG          , LDAP_PVT_MUTEX_NULL
358 #else
359 #define LDAP_LDO_MUTEX_NULLARG
360 #endif
361 };
362 
363 
364 /*
365  * structure for representing an LDAP server connection
366  */
367 typedef struct ldap_conn {
368           Sockbuf             *lconn_sb;
369 #ifdef HAVE_CYRUS_SASL
370           void                *lconn_sasl_authctx;          /* context for bind */
371           void                *lconn_sasl_sockctx;          /* for security layer */
372           void                *lconn_sasl_cbind;            /* for channel binding */
373 #endif
374 #ifdef HAVE_GSSAPI
375           void                *lconn_gss_ctx;               /* gss_ctx_id_t */
376 #endif
377           int                           lconn_refcnt;
378           time_t              lconn_created;      /* time */
379           time_t              lconn_lastused;     /* time */
380           int                           lconn_rebind_inprogress;      /* set if rebind in progress */
381           char                ***lconn_rebind_queue;                  /* used if rebind in progress */
382           int                           lconn_status;
383 #define LDAP_CONNST_NEEDSOCKET                    1
384 #define LDAP_CONNST_CONNECTING                    2
385 #define LDAP_CONNST_CONNECTED           3
386           LDAPURLDesc                   *lconn_server;
387           BerElement                    *lconn_ber;         /* ber receiving on this conn. */
388 
389           struct ldap_conn *lconn_next;
390 } LDAPConn;
391 
392 
393 /*
394  * structure used to track outstanding requests
395  */
396 typedef struct ldapreq {
397           ber_int_t lr_msgid; /* the message id */
398           int                 lr_status;          /* status of request */
399 #define LDAP_REQST_COMPLETED  0
400 #define LDAP_REQST_INPROGRESS 1
401 #define LDAP_REQST_CHASINGREFS          2
402 #define LDAP_REQST_NOTCONNECTED         3
403 #define LDAP_REQST_WRITING    4
404           int                 lr_refcnt;          /* count of references */
405           int                 lr_outrefcnt;       /* count of outstanding referrals */
406           int                 lr_abandoned;       /* the request has been abandoned */
407           ber_int_t lr_origid;          /* original request's message id */
408           int                 lr_parentcnt;       /* count of parent requests */
409           ber_tag_t lr_res_msgtype;     /* result message type */
410           ber_int_t lr_res_errno;       /* result LDAP errno */
411           char                *lr_res_error;      /* result error string */
412           char                *lr_res_matched;/* result matched DN string */
413           BerElement          *lr_ber;  /* ber encoded request contents */
414           LDAPConn  *lr_conn; /* connection used to send request */
415           struct berval       lr_dn;              /* DN of request, in lr_ber */
416           struct ldapreq      *lr_parent;         /* request that spawned this referral */
417           struct ldapreq      *lr_child;          /* first child request */
418           struct ldapreq      *lr_refnext;        /* next referral spawned */
419           struct ldapreq      *lr_prev; /* previous request */
420           struct ldapreq      *lr_next; /* next request */
421 } LDAPRequest;
422 
423 /*
424  * structure for client cache
425  */
426 #define LDAP_CACHE_BUCKETS    31        /* cache hash table size */
427 typedef struct ldapcache {
428           LDAPMessage         *lc_buckets[LDAP_CACHE_BUCKETS];/* hash table */
429           LDAPMessage         *lc_requests;                           /* unfulfilled reqs */
430           long                lc_timeout;                             /* request timeout */
431           ber_len_t           lc_maxmem;                              /* memory to use */
432           ber_len_t           lc_memused;                             /* memory in use */
433           int                 lc_enabled;                             /* enabled? */
434           unsigned long       lc_options;                             /* options */
435 #define LDAP_CACHE_OPT_CACHENOERRS      0x00000001
436 #define LDAP_CACHE_OPT_CACHEALLERRS     0x00000002
437 }  LDAPCache;
438 
439 /*
440  * structure containing referral request info for rebind procedure
441  */
442 typedef struct ldapreqinfo {
443           ber_len_t ri_msgid;
444           int                           ri_request;
445           char                *ri_url;
446 } LDAPreqinfo;
447 
448 /*
449  * structure representing an ldap connection
450  */
451 
452 struct ldap_common {
453           Sockbuf             *ldc_sb;  /* socket descriptor & buffer */
454 #define ld_sb                           ldc->ldc_sb
455 
456           unsigned short      ldc_lberoptions;
457 #define   ld_lberoptions                ldc->ldc_lberoptions
458 
459           /* protected by msgid_mutex */
460           ber_len_t           ldc_msgid;
461 #define   ld_msgid            ldc->ldc_msgid
462 
463           /* do not mess with these */
464           /* protected by req_mutex */
465           TAvlnode  *ldc_requests;      /* list of outstanding requests */
466           /* protected by res_mutex */
467           LDAPMessage         *ldc_responses;     /* list of outstanding responses */
468 #define   ld_requests                   ldc->ldc_requests
469 #define   ld_responses                  ldc->ldc_responses
470 
471           /* protected by abandon_mutex */
472           ber_len_t ldc_nabandoned;
473           ber_int_t *ldc_abandoned;     /* array of abandoned requests */
474 #define   ld_nabandoned                 ldc->ldc_nabandoned
475 #define   ld_abandoned                  ldc->ldc_abandoned
476 
477           /* unused by libldap */
478           LDAPCache *ldc_cache;         /* non-null if cache is initialized */
479 #define   ld_cache            ldc->ldc_cache
480 
481           /* do not mess with the rest though */
482 
483           /* protected by conn_mutex */
484           LDAPConn  *ldc_defconn;       /* default connection */
485 #define   ld_defconn                    ldc->ldc_defconn
486           LDAPConn  *ldc_conns;         /* list of server connections */
487 #define   ld_conns            ldc->ldc_conns
488           void                *ldc_selectinfo;/* platform specifics for select */
489 #define   ld_selectinfo                 ldc->ldc_selectinfo
490 
491           /* ldap_common refcnt - free only if 0 */
492           /* protected by ldc_mutex */
493           unsigned int                  ldc_refcnt;
494 #define   ld_ldcrefcnt                  ldc->ldc_refcnt
495 
496           /* protected by ldo_mutex */
497           struct ldapoptions ldc_options;
498 #define ld_options            ldc->ldc_options
499 
500 #define ld_valid              ld_options.ldo_valid
501 #define ld_debug              ld_options.ldo_debug
502 
503 #define ld_deref              ld_options.ldo_deref
504 #define ld_timelimit                    ld_options.ldo_timelimit
505 #define ld_sizelimit                    ld_options.ldo_sizelimit
506 
507 #define ld_defbinddn                    ld_options.ldo_defbinddn
508 #define ld_defbase            ld_options.ldo_defbase
509 #define ld_defhost            ld_options.ldo_defhost
510 #define ld_defport            ld_options.ldo_defport
511 
512 #define ld_refhoplimit                  ld_options.ldo_refhoplimit
513 
514 #define ld_sctrls             ld_options.ldo_sctrls
515 #define ld_cctrls             ld_options.ldo_cctrls
516 #define ld_rebind_proc                  ld_options.ldo_rebind_proc
517 #define ld_rebind_params      ld_options.ldo_rebind_params
518 #define ld_nextref_proc                 ld_options.ldo_nextref_proc
519 #define ld_nextref_params     ld_options.ldo_nextref_params
520 #define ld_urllist_proc                 ld_options.ldo_urllist_proc
521 #define ld_urllist_params     ld_options.ldo_urllist_params
522 
523 #define ld_version            ld_options.ldo_version
524 
525 #ifdef LDAP_R_COMPILE
526           ldap_pvt_thread_mutex_t       ldc_mutex;
527           ldap_pvt_thread_mutex_t       ldc_msgid_mutex;
528           ldap_pvt_thread_mutex_t       ldc_conn_mutex;
529           ldap_pvt_thread_mutex_t       ldc_req_mutex;
530           ldap_pvt_thread_mutex_t       ldc_res_mutex;
531           ldap_pvt_thread_mutex_t       ldc_abandon_mutex;
532 #define   ld_ldopts_mutex               ld_options.ldo_mutex
533 #define   ld_ldcmutex                   ldc->ldc_mutex
534 #define   ld_msgid_mutex                ldc->ldc_msgid_mutex
535 #define   ld_conn_mutex                 ldc->ldc_conn_mutex
536 #define   ld_req_mutex                  ldc->ldc_req_mutex
537 #define   ld_res_mutex                  ldc->ldc_res_mutex
538 #define   ld_abandon_mutex    ldc->ldc_abandon_mutex
539 #endif
540 };
541 
542 struct ldap {
543           /* thread shared */
544           struct ldap_common  *ldc;
545 
546           /* thread specific */
547           ber_int_t           ld_errno;
548           char                          *ld_error;
549           char                          *ld_matched;
550           char                          **ld_referrals;
551 };
552 
553 #define LDAP_VALID(ld)                  ( (ld)->ld_valid == LDAP_VALID_SESSION )
554 #define LDAP_TRASHED(ld)      ( (ld)->ld_valid == LDAP_TRASHED_SESSION )
555 #define LDAP_TRASH(ld)                  ( (ld)->ld_valid = LDAP_TRASHED_SESSION )
556 
557 #ifdef LDAP_R_COMPILE
558 LDAP_V ( ldap_pvt_thread_mutex_t ) ldap_int_resolv_mutex;
559 LDAP_V ( ldap_pvt_thread_mutex_t ) ldap_int_hostname_mutex;
560 LDAP_V ( int ) ldap_int_stackguard;
561 
562 #ifdef HAVE_GSSAPI
563 LDAP_V( ldap_pvt_thread_mutex_t ) ldap_int_gssapi_mutex;
564 #endif
565 #endif
566 
567 #ifdef LDAP_R_COMPILE
568 #define LDAP_MUTEX_LOCK(mutex)    ldap_pvt_thread_mutex_lock( mutex )
569 #define LDAP_MUTEX_UNLOCK(mutex)  ldap_pvt_thread_mutex_unlock( mutex )
570 #define LDAP_ASSERT_MUTEX_OWNER(mutex) \
571           LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER(mutex)
572 #else
573 #define LDAP_MUTEX_LOCK(mutex)    ((void) 0)
574 #define LDAP_MUTEX_UNLOCK(mutex)  ((void) 0)
575 #define LDAP_ASSERT_MUTEX_OWNER(mutex) ((void) 0)
576 #endif
577 
578 #define   LDAP_NEXT_MSGID(ld, id) do { \
579           LDAP_MUTEX_LOCK( &(ld)->ld_msgid_mutex ); \
580           (id) = ++(ld)->ld_msgid; \
581           LDAP_MUTEX_UNLOCK( &(ld)->ld_msgid_mutex ); \
582 } while (0)
583 
584 /*
585  * in abandon.c
586  */
587 
588 LDAP_F (int)
589 ldap_int_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp );
590 LDAP_F (int)
591 ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx );
592 LDAP_F (int)
593 ldap_int_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx );
594 
595 /*
596  * in add.c
597  */
598 
599 LDAP_F (BerElement *) ldap_build_add_req LDAP_P((
600           LDAP *ld,
601           const char *dn,
602           LDAPMod **attrs,
603           LDAPControl **sctrls,
604           LDAPControl **cctrls,
605           ber_int_t *msgidp ));
606 
607 /*
608  * in lbase64.c
609  */
610 
611 LDAP_F (int) ldap_int_decode_b64_inplace LDAP_P((
612           struct berval *value ));
613 
614 /*
615  * in compare.c
616  */
617 
618 LDAP_F (BerElement *) ldap_build_compare_req LDAP_P((
619           LDAP *ld,
620           const char *dn,
621           const char *attr,
622           struct berval *bvalue,
623           LDAPControl **sctrls,
624           LDAPControl **cctrls,
625           ber_int_t *msgidp ));
626 
627 /*
628  * in delete.c
629  */
630 
631 LDAP_F (BerElement *) ldap_build_delete_req LDAP_P((
632           LDAP *ld,
633           const char *dn,
634           LDAPControl **sctrls,
635           LDAPControl **cctrls,
636           ber_int_t *msgidp ));
637 
638 /*
639  * in extended.c
640  */
641 
642 LDAP_F (BerElement *) ldap_build_extended_req LDAP_P((
643           LDAP *ld,
644           const char *reqoid,
645           struct berval *reqdata,
646           LDAPControl **sctrls,
647           LDAPControl **cctrls,
648           ber_int_t *msgidp ));
649 
650 /*
651  * in init.c
652  */
653 
654 LDAP_V ( struct ldapoptions ) ldap_int_global_options;
655 
656 LDAP_F ( void ) ldap_int_initialize LDAP_P((struct ldapoptions *, int *));
657 LDAP_F ( void ) ldap_int_initialize_global_options LDAP_P((
658           struct ldapoptions *, int *));
659 
660 /* memory.c */
661           /* simple macros to realloc for now */
662 #define LDAP_MALLOC(s)                  (ber_memalloc_x((s),NULL))
663 #define LDAP_CALLOC(n,s)      (ber_memcalloc_x((n),(s),NULL))
664 #define LDAP_REALLOC(p,s)     (ber_memrealloc_x((p),(s),NULL))
665 #define LDAP_FREE(p)                    (ber_memfree_x((p),NULL))
666 #define LDAP_VFREE(v)                   (ber_memvfree_x((void **)(v),NULL))
667 #define LDAP_STRDUP(s)                  (ber_strdup_x((s),NULL))
668 #define LDAP_STRNDUP(s,l)     (ber_strndup_x((s),(l),NULL))
669 
670 #define LDAP_MALLOCX(s,x)     (ber_memalloc_x((s),(x)))
671 #define LDAP_CALLOCX(n,s,x)   (ber_memcalloc_x((n),(s),(x)))
672 #define LDAP_REALLOCX(p,s,x)  (ber_memrealloc_x((p),(s),(x)))
673 #define LDAP_FREEX(p,x)                 (ber_memfree_x((p),(x)))
674 #define LDAP_VFREEX(v,x)      (ber_memvfree_x((void **)(v),(x)))
675 #define LDAP_STRDUPX(s,x)     (ber_strdup_x((s),(x)))
676 #define LDAP_STRNDUPX(s,l,x)  (ber_strndup_x((s),(l),(x)))
677 
678 /*
679  * in error.c
680  */
681 LDAP_F (void) ldap_int_error_init( void );
682 
683 /*
684  * in modify.c
685  */
686 
687 LDAP_F (BerElement *) ldap_build_modify_req LDAP_P((
688           LDAP *ld,
689           const char *dn,
690           LDAPMod **mods,
691           LDAPControl **sctrls,
692           LDAPControl **cctrls,
693           ber_int_t *msgidp ));
694 
695 /*
696  * in modrdn.c
697  */
698 
699 LDAP_F (BerElement *) ldap_build_moddn_req LDAP_P((
700           LDAP *ld,
701           const char *dn,
702           const char *newrdn,
703           const char *newSuperior,
704           int deleteoldrdn,
705           LDAPControl **sctrls,
706           LDAPControl **cctrls,
707           ber_int_t *msgidp ));
708 
709 /*
710  * in unit-int.c
711  */
712 LDAP_F (void) ldap_int_utils_init LDAP_P(( void ));
713 
714 
715 /*
716  * in print.c
717  */
718 LDAP_F (int) ldap_log_printf LDAP_P((LDAP *ld, int level, const char *fmt, ...)) LDAP_GCCATTR((format(printf, 3, 4)));
719 
720 /*
721  * in controls.c
722  */
723 LDAP_F (int) ldap_int_put_controls LDAP_P((
724           LDAP *ld,
725           LDAPControl *const *ctrls,
726           BerElement *ber ));
727 
728 LDAP_F (int) ldap_int_client_controls LDAP_P((
729           LDAP *ld,
730           LDAPControl **ctrlp ));
731 
732 /*
733  * in dsparse.c
734  */
735 LDAP_F (int) ldap_int_next_line_tokens LDAP_P(( char **bufp, ber_len_t *blenp, char ***toksp ));
736 
737 
738 /*
739  * in open.c
740  */
741 LDAP_F (int) ldap_open_defconn( LDAP *ld );
742 LDAP_F (int) ldap_int_open_connection( LDAP *ld,
743           LDAPConn *conn, LDAPURLDesc *srvlist, int async );
744 LDAP_F (int) ldap_int_check_async_open( LDAP *ld, ber_socket_t sd );
745 
746 /*
747  * in os-ip.c
748  */
749 #ifndef HAVE_POLL
750 LDAP_V (int) ldap_int_tblsize;
751 LDAP_F (void) ldap_int_ip_init( void );
752 #endif
753 
754 LDAP_F (int) ldap_int_timeval_dup( struct timeval **dest,
755           const struct timeval *tm );
756 LDAP_F (int) ldap_connect_to_host( LDAP *ld, Sockbuf *sb,
757           int proto, LDAPURLDesc *srv, int async );
758 LDAP_F (int) ldap_int_poll( LDAP *ld, ber_socket_t s,
759           struct timeval *tvp, int wr );
760 
761 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
762 LDAP_V (char *) ldap_int_hostname;
763 LDAP_F (char *) ldap_host_connected_to( Sockbuf *sb,
764           const char *host );
765 #endif
766 
767 LDAP_F (int) ldap_int_select( LDAP *ld, struct timeval *timeout );
768 LDAP_F (void *) ldap_new_select_info( void );
769 LDAP_F (void) ldap_free_select_info( void *sip );
770 LDAP_F (void) ldap_mark_select_write( LDAP *ld, Sockbuf *sb );
771 LDAP_F (void) ldap_mark_select_read( LDAP *ld, Sockbuf *sb );
772 LDAP_F (void) ldap_mark_select_clear( LDAP *ld, Sockbuf *sb );
773 LDAP_F (void) ldap_clear_select_write( LDAP *ld, Sockbuf *sb );
774 LDAP_F (int) ldap_is_read_ready( LDAP *ld, Sockbuf *sb );
775 LDAP_F (int) ldap_is_write_ready( LDAP *ld, Sockbuf *sb );
776 
777 LDAP_F (int) ldap_validate_and_fill_sourceip  ( char** source_ip_lst,
778           ldapsourceip* temp_source_ip );
779 
780 LDAP_F (int) ldap_int_connect_cbs( LDAP *ld, Sockbuf *sb,
781           ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr );
782 
783 /*
784  * in os-local.c
785  */
786 #ifdef LDAP_PF_LOCAL
787 LDAP_F (int) ldap_connect_to_path( LDAP *ld, Sockbuf *sb,
788           LDAPURLDesc *srv, int async );
789 #endif /* LDAP_PF_LOCAL */
790 
791 /*
792  * in request.c
793  */
794 LDAP_F (ber_int_t) ldap_send_initial_request( LDAP *ld, ber_tag_t msgtype,
795           const char *dn, BerElement *ber, ber_int_t msgid );
796 LDAP_F (BerElement *) ldap_alloc_ber_with_options( LDAP *ld );
797 LDAP_F (void) ldap_set_ber_options( LDAP *ld, BerElement *ber );
798 
799 LDAP_F (int) ldap_send_server_request( LDAP *ld, BerElement *ber,
800           ber_int_t msgid, LDAPRequest *parentreq, LDAPURLDesc **srvlist,
801           LDAPConn *lc, LDAPreqinfo *bind, int noconn, int m_res );
802 LDAP_F (LDAPConn *) ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist,
803           int use_ldsb, int connect, LDAPreqinfo *bind, int m_req, int m_res );
804 LDAP_F (LDAPRequest *) ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid );
805 LDAP_F (void) ldap_return_request( LDAP *ld, LDAPRequest *lr, int freeit );
806 LDAP_F (int) ldap_req_cmp( const void *l, const void *r );
807 LDAP_F (void) ldap_do_free_request( void *arg );
808 LDAP_F (void) ldap_free_request( LDAP *ld, LDAPRequest *lr );
809 LDAP_F (void) ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind );
810 LDAP_F (void) ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all );
811 LDAP_F (void) ldap_dump_requests_and_responses( LDAP *ld );
812 LDAP_F (int) ldap_chase_referrals( LDAP *ld, LDAPRequest *lr,
813           char **errstrp, int sref, int *hadrefp );
814 LDAP_F (int) ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr,
815           char **refs, int sref, char **referralsp, int *hadrefp );
816 LDAP_F (int) ldap_append_referral( LDAP *ld, char **referralsp, char *s );
817 LDAP_F (int) ldap_int_flush_request( LDAP *ld, LDAPRequest *lr );
818 
819 /*
820  * in result.c:
821  */
822 LDAP_F (const char *) ldap_int_msgtype2str( ber_tag_t tag );
823 
824 /*
825  * in search.c
826  */
827 LDAP_F (BerElement *) ldap_build_search_req LDAP_P((
828           LDAP *ld,
829           const char *base,
830           ber_int_t scope,
831           const char *filter,
832           char **attrs,
833           ber_int_t attrsonly,
834           LDAPControl **sctrls,
835           LDAPControl **cctrls,
836           ber_int_t timelimit,
837           ber_int_t sizelimit,
838           ber_int_t deref,
839           ber_int_t *msgidp));
840 
841 
842 /*
843  * in unbind.c
844  */
845 LDAP_F (int) ldap_ld_free LDAP_P((
846           LDAP *ld,
847           int close,
848           LDAPControl **sctrls,
849           LDAPControl **cctrls ));
850 
851 LDAP_F (int) ldap_send_unbind LDAP_P((
852           LDAP *ld,
853           Sockbuf *sb,
854           LDAPControl **sctrls,
855           LDAPControl **cctrls ));
856 
857 /*
858  * in url.c
859  */
860 LDAP_F (LDAPURLDesc *) ldap_url_dup LDAP_P((
861           LDAPURLDesc *ludp ));
862 
863 LDAP_F (LDAPURLDesc *) ldap_url_duplist LDAP_P((
864           LDAPURLDesc *ludlist ));
865 
866 LDAP_F (int) ldap_url_parsehosts LDAP_P((
867           LDAPURLDesc **ludlist,
868           const char *hosts,
869           int port ));
870 
871 LDAP_F (char *) ldap_url_list2hosts LDAP_P((
872           LDAPURLDesc *ludlist ));
873 
874 /*
875  * in cyrus.c
876  */
877 
878 LDAP_F (int) ldap_int_sasl_init LDAP_P(( void ));
879 
880 LDAP_F (int) ldap_int_sasl_open LDAP_P((
881           LDAP *ld, LDAPConn *conn,
882           const char* host ));
883 LDAP_F (int) ldap_int_sasl_close LDAP_P(( LDAP *ld, LDAPConn *conn ));
884 
885 LDAP_F (int) ldap_int_sasl_external LDAP_P((
886           LDAP *ld, LDAPConn *conn,
887           const char* authid, ber_len_t ssf ));
888 
889 LDAP_F (int) ldap_int_sasl_get_option LDAP_P(( LDAP *ld,
890           int option, void *arg ));
891 LDAP_F (int) ldap_int_sasl_set_option LDAP_P(( LDAP *ld,
892           int option, void *arg ));
893 LDAP_F (int) ldap_int_sasl_config LDAP_P(( struct ldapoptions *lo,
894           int option, const char *arg ));
895 
896 LDAP_F (int) ldap_int_sasl_bind LDAP_P((
897           LDAP *ld,
898           const char *,
899           const char *,
900           LDAPControl **, LDAPControl **,
901 
902           /* should be passed in client controls */
903           unsigned flags,
904           LDAP_SASL_INTERACT_PROC *interact,
905           void *defaults,
906           LDAPMessage *result,
907           const char **rmech,
908           int *msgid ));
909 
910 /* in sasl.c */
911 
912 LDAP_F (BerElement *) ldap_build_bind_req LDAP_P((
913           LDAP *ld,
914           const char *dn,
915           const char *mech,
916           struct berval *cred,
917           LDAPControl **sctrls,
918           LDAPControl **cctrls,
919           ber_int_t *msgidp ));
920 
921 /* in schema.c */
922 LDAP_F (char *) ldap_int_parse_numericoid LDAP_P((
923           const char **sp,
924           int *code,
925           const int flags ));
926 
927 /*
928  * in tls.c
929  */
930 LDAP_F (int) ldap_int_tls_start LDAP_P(( LDAP *ld,
931           LDAPConn *conn, LDAPURLDesc *srv ));
932 
933 LDAP_F (void) ldap_int_tls_destroy LDAP_P(( struct ldapoptions *lo ));
934 
935 /*
936  *        in getvalues.c
937  */
938 LDAP_F (char **) ldap_value_dup LDAP_P((
939           char *const *vals ));
940 
941 /*
942  *        in gssapi.c
943  */
944 #ifdef HAVE_GSSAPI
945 LDAP_F(int) ldap_int_gssapi_get_option LDAP_P(( LDAP *ld, int option, void *arg ));
946 LDAP_F(int) ldap_int_gssapi_set_option LDAP_P(( LDAP *ld, int option, void *arg ));
947 LDAP_F(int) ldap_int_gssapi_config LDAP_P(( struct ldapoptions *lo, int option, const char *arg ));
948 LDAP_F(void) ldap_int_gssapi_close LDAP_P(( LDAP *ld, LDAPConn *lc ));
949 #endif
950 
951 LDAP_END_DECL
952 
953 #endif /* _LDAP_INT_H */
954