1 /* ldap-int.h - defines & prototypes internal to the LDAP library */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
24 #define LDAP_THREAD_SAFE 1
27 #include "../liblber/lber-int.h"
30 #include <ldap_pvt_thread.h>
33 #ifdef HAVE_CYRUS_SASL
34 /* the need for this should be removed */
35 #ifdef HAVE_SASL_SASL_H
36 #include <sasl/sasl.h>
41 #define SASL_MAX_BUFF_SIZE (0xffffff)
42 #define SASL_MIN_BUFF_SIZE 4096
45 /* for struct timeval */
49 #define TV2MILLISEC(tv) (((tv)->tv_sec * 1000) + ((tv)->tv_usec/1000))
52 * Support needed if the library is running in the kernel
54 #if LDAP_INT_IN_KERNEL
56 * Platform specific function to return a pointer to the
57 * process-specific global options.
59 * This function should perform the following functions:
60 * Allocate and initialize a global options struct on a per process basis
61 * Use callers process identifier to return its global options struct
62 * Note: Deallocate structure when the process exits
64 # define LDAP_INT_GLOBAL_OPT() ldap_int_global_opt()
65 struct ldapoptions *ldap_int_global_opt(void);
67 # define LDAP_INT_GLOBAL_OPT() (&ldap_int_global_options)
70 #define ldap_debug ((LDAP_INT_GLOBAL_OPT())->ldo_debug)
78 #define Debug( level, fmt, arg1, arg2, arg3 ) \
79 do { if ( ldap_debug & level ) \
80 ldap_log_printf( NULL, (level), (fmt), (arg1), (arg2), (arg3) ); \
83 #define LDAP_Debug( subsystem, level, fmt, arg1, arg2, arg3 )\
84 ldap_log_printf( NULL, (level), (fmt), (arg1), (arg2), (arg3) )
88 #define Debug( level, fmt, arg1, arg2, arg3 ) ((void)0)
89 #define LDAP_Debug( subsystem, level, fmt, arg1, arg2, arg3 ) ((void)0)
91 #endif /* LDAP_DEBUG */
93 #define LDAP_DEPRECATED 1
100 #define LDAP_URL_PREFIX "ldap://"
101 #define LDAP_URL_PREFIX_LEN STRLENOF(LDAP_URL_PREFIX)
102 #define LDAPS_URL_PREFIX "ldaps://"
103 #define LDAPS_URL_PREFIX_LEN STRLENOF(LDAPS_URL_PREFIX)
104 #define LDAPI_URL_PREFIX "ldapi://"
105 #define LDAPI_URL_PREFIX_LEN STRLENOF(LDAPI_URL_PREFIX)
106 #ifdef LDAP_CONNECTIONLESS
107 #define LDAPC_URL_PREFIX "cldap://"
108 #define LDAPC_URL_PREFIX_LEN STRLENOF(LDAPC_URL_PREFIX)
110 #define LDAP_URL_URLCOLON "URL:"
111 #define LDAP_URL_URLCOLON_LEN STRLENOF(LDAP_URL_URLCOLON)
113 #define LDAP_REF_STR "Referral:\n"
114 #define LDAP_REF_STR_LEN STRLENOF(LDAP_REF_STR)
115 #define LDAP_LDAP_REF_STR LDAP_URL_PREFIX
116 #define LDAP_LDAP_REF_STR_LEN LDAP_URL_PREFIX_LEN
118 #define LDAP_DEFAULT_REFHOPLIMIT 5
120 #define LDAP_BOOL_REFERRALS 0
121 #define LDAP_BOOL_RESTART 1
122 #define LDAP_BOOL_TLS 3
123 #define LDAP_BOOL_CONNECT_ASYNC 4
125 #define LDAP_BOOLEANS unsigned long
126 #define LDAP_BOOL(n) ((LDAP_BOOLEANS)1 << (n))
127 #define LDAP_BOOL_GET(lo, bool) \
128 ((lo)->ldo_booleans & LDAP_BOOL(bool) ? -1 : 0)
129 #define LDAP_BOOL_SET(lo, bool) ((lo)->ldo_booleans |= LDAP_BOOL(bool))
130 #define LDAP_BOOL_CLR(lo, bool) ((lo)->ldo_booleans &= ~LDAP_BOOL(bool))
131 #define LDAP_BOOL_ZERO(lo) ((lo)->ldo_booleans = 0)
134 * This structure represents both ldap messages and ldap responses.
135 * These are really the same, except in the case of search responses,
136 * where a response has multiple messages.
140 ber_int_t lm_msgid; /* the message id */
141 ber_tag_t lm_msgtype; /* the message type */
142 BerElement *lm_ber; /* the ber encoded message contents */
143 struct ldapmsg *lm_chain; /* for search - next msg in the resp */
144 struct ldapmsg *lm_chain_tail;
145 struct ldapmsg *lm_next; /* next response */
146 time_t lm_time; /* used to maintain cache */
156 char *lt_ciphersuite;
163 typedef struct ldaplist {
164 struct ldaplist *ll_next;
169 * structure representing get/set'able options
170 * which have global defaults.
174 #define LDAP_UNINITIALIZED 0x0
175 #define LDAP_INITIALIZED 0x1
176 #define LDAP_VALID_SESSION 0x2
177 #define LDAP_TRASHED_SESSION 0xFF
179 #ifdef LDAP_CONNECTIONLESS
180 #define LDAP_IS_UDP(ld) ((ld)->ld_options.ldo_is_udp)
181 void* ldo_peer; /* struct sockaddr* */
186 /* per API call timeout */
187 struct timeval ldo_tm_api;
188 struct timeval ldo_tm_net;
190 ber_int_t ldo_version;
192 ber_int_t ldo_timelimit;
193 ber_int_t ldo_sizelimit;
198 LDAP_TLS_CONNECT_CB *ldo_tls_connect_cb;
199 void* ldo_tls_connect_arg;
200 struct ldaptls ldo_tls_info;
201 #define ldo_tls_certfile ldo_tls_info.lt_certfile
202 #define ldo_tls_keyfile ldo_tls_info.lt_keyfile
203 #define ldo_tls_dhfile ldo_tls_info.lt_dhfile
204 #define ldo_tls_cacertfile ldo_tls_info.lt_cacertfile
205 #define ldo_tls_cacertdir ldo_tls_info.lt_cacertdir
206 #define ldo_tls_ciphersuite ldo_tls_info.lt_ciphersuite
207 #define ldo_tls_crlfile ldo_tls_info.lt_crlfile
209 int ldo_tls_require_cert;
210 #ifdef HAVE_OPENSSL_CRL
211 int ldo_tls_crlcheck;
215 LDAPURLDesc *ldo_defludp;
218 char* ldo_defbinddn; /* bind dn */
220 #ifdef HAVE_CYRUS_SASL
221 char* ldo_def_sasl_mech; /* SASL Mechanism(s) */
222 char* ldo_def_sasl_realm; /* SASL realm */
223 char* ldo_def_sasl_authcid; /* SASL authentication identity */
224 char* ldo_def_sasl_authzid; /* SASL authorization identity */
226 /* SASL Security Properties */
227 struct sasl_security_properties ldo_sasl_secprops;
230 int ldo_refhoplimit; /* limit on referral nesting */
232 /* LDAPv3 server and client controls */
233 LDAPControl **ldo_sctrls;
234 LDAPControl **ldo_cctrls;
236 /* LDAP rebind callback function */
237 LDAP_REBIND_PROC *ldo_rebind_proc;
238 void *ldo_rebind_params;
239 LDAP_NEXTREF_PROC *ldo_nextref_proc;
240 void *ldo_nextref_params;
241 LDAP_URLLIST_PROC *ldo_urllist_proc;
242 void *ldo_urllist_params;
244 /* LDAP connection callback stack */
245 ldaplist *ldo_conn_cbs;
247 LDAP_BOOLEANS ldo_booleans; /* boolean options */
252 * structure for representing an LDAP server connection
254 typedef struct ldap_conn {
256 #ifdef HAVE_CYRUS_SASL
257 void *lconn_sasl_authctx; /* context for bind */
258 void *lconn_sasl_sockctx; /* for security layer */
261 time_t lconn_created; /* time */
262 time_t lconn_lastused; /* time */
263 int lconn_rebind_inprogress; /* set if rebind in progress */
264 char ***lconn_rebind_queue; /* used if rebind in progress */
266 #define LDAP_CONNST_NEEDSOCKET 1
267 #define LDAP_CONNST_CONNECTING 2
268 #define LDAP_CONNST_CONNECTED 3
269 LDAPURLDesc *lconn_server;
270 BerElement *lconn_ber; /* ber receiving on this conn. */
272 struct ldap_conn *lconn_next;
277 * structure used to track outstanding requests
279 typedef struct ldapreq {
280 ber_int_t lr_msgid; /* the message id */
281 int lr_status; /* status of request */
282 #define LDAP_REQST_COMPLETED 0
283 #define LDAP_REQST_INPROGRESS 1
284 #define LDAP_REQST_CHASINGREFS 2
285 #define LDAP_REQST_NOTCONNECTED 3
286 #define LDAP_REQST_WRITING 4
287 int lr_refcnt; /* count of references */
288 int lr_outrefcnt; /* count of outstanding referrals */
289 int lr_abandoned; /* the request has been abandoned */
290 ber_int_t lr_origid; /* original request's message id */
291 int lr_parentcnt; /* count of parent requests */
292 ber_tag_t lr_res_msgtype; /* result message type */
293 ber_int_t lr_res_errno; /* result LDAP errno */
294 char *lr_res_error; /* result error string */
295 char *lr_res_matched;/* result matched DN string */
296 BerElement *lr_ber; /* ber encoded request contents */
297 LDAPConn *lr_conn; /* connection used to send request */
298 struct berval lr_dn; /* DN of request, in lr_ber */
299 struct ldapreq *lr_parent; /* request that spawned this referral */
300 struct ldapreq *lr_child; /* first child request */
301 struct ldapreq *lr_refnext; /* next referral spawned */
302 struct ldapreq *lr_prev; /* previous request */
303 struct ldapreq *lr_next; /* next request */
307 * structure for client cache
309 #define LDAP_CACHE_BUCKETS 31 /* cache hash table size */
310 typedef struct ldapcache {
311 LDAPMessage *lc_buckets[LDAP_CACHE_BUCKETS];/* hash table */
312 LDAPMessage *lc_requests; /* unfulfilled reqs */
313 long lc_timeout; /* request timeout */
314 ber_len_t lc_maxmem; /* memory to use */
315 ber_len_t lc_memused; /* memory in use */
316 int lc_enabled; /* enabled? */
317 unsigned long lc_options; /* options */
318 #define LDAP_CACHE_OPT_CACHENOERRS 0x00000001
319 #define LDAP_CACHE_OPT_CACHEALLERRS 0x00000002
323 * structure containing referral request info for rebind procedure
325 typedef struct ldapreqinfo {
332 * structure representing an ldap connection
336 Sockbuf *ld_sb; /* socket descriptor & buffer */
338 struct ldapoptions ld_options;
340 #define ld_valid ld_options.ldo_valid
341 #define ld_debug ld_options.ldo_debug
343 #define ld_deref ld_options.ldo_deref
344 #define ld_timelimit ld_options.ldo_timelimit
345 #define ld_sizelimit ld_options.ldo_sizelimit
347 #define ld_defbinddn ld_options.ldo_defbinddn
348 #define ld_defbase ld_options.ldo_defbase
349 #define ld_defhost ld_options.ldo_defhost
350 #define ld_defport ld_options.ldo_defport
352 #define ld_refhoplimit ld_options.ldo_refhoplimit
354 #define ld_sctrls ld_options.ldo_sctrls
355 #define ld_cctrls ld_options.ldo_cctrls
356 #define ld_rebind_proc ld_options.ldo_rebind_proc
357 #define ld_rebind_params ld_options.ldo_rebind_params
358 #define ld_nextref_proc ld_options.ldo_nextref_proc
359 #define ld_nextref_params ld_options.ldo_nextref_params
360 #define ld_urllist_proc ld_options.ldo_urllist_proc
361 #define ld_urllist_params ld_options.ldo_urllist_params
363 #define ld_version ld_options.ldo_version
365 unsigned short ld_lberoptions;
373 /* do not mess with these */
374 LDAPRequest *ld_requests; /* list of outstanding requests */
375 LDAPMessage *ld_responses; /* list of outstanding responses */
377 #ifdef LDAP_R_COMPILE
378 ldap_pvt_thread_mutex_t ld_conn_mutex;
379 ldap_pvt_thread_mutex_t ld_req_mutex;
380 ldap_pvt_thread_mutex_t ld_res_mutex;
383 ber_len_t ld_nabandoned;
384 ber_int_t *ld_abandoned; /* array of abandoned requests */
386 LDAPCache *ld_cache; /* non-null if cache is initialized */
388 /* do not mess with the rest though */
390 LDAPConn *ld_defconn; /* default connection */
391 LDAPConn *ld_conns; /* list of server connections */
392 void *ld_selectinfo; /* platform specifics for select */
394 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
395 #define LDAP_TRASHED(ld) ( (ld)->ld_valid == LDAP_TRASHED_SESSION )
396 #define LDAP_TRASH(ld) ( (ld)->ld_valid = LDAP_TRASHED_SESSION )
398 #ifdef LDAP_R_COMPILE
399 LDAP_V ( ldap_pvt_thread_mutex_t ) ldap_int_resolv_mutex;
401 #ifdef HAVE_CYRUS_SASL
402 LDAP_V( ldap_pvt_thread_mutex_t ) ldap_int_sasl_mutex;
406 #ifdef LDAP_R_COMPILE
407 #define LDAP_NEXT_MSGID(ld, id) \
408 ldap_pvt_thread_mutex_lock( &(ld)->ld_req_mutex ); \
409 id = ++(ld)->ld_msgid; \
410 ldap_pvt_thread_mutex_unlock( &(ld)->ld_req_mutex )
412 #define LDAP_NEXT_MSGID(ld, id) id = ++(ld)->ld_msgid
420 ldap_int_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp );
422 ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx );
424 ldap_int_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx );
430 LDAP_V ( struct ldapoptions ) ldap_int_global_options;
432 LDAP_F ( void ) ldap_int_initialize LDAP_P((struct ldapoptions *, int *));
433 LDAP_F ( void ) ldap_int_initialize_global_options LDAP_P((
434 struct ldapoptions *, int *));
437 /* simple macros to realloc for now */
438 #define LDAP_MALLOC(s) (ber_memalloc_x((s),NULL))
439 #define LDAP_CALLOC(n,s) (ber_memcalloc_x((n),(s),NULL))
440 #define LDAP_REALLOC(p,s) (ber_memrealloc_x((p),(s),NULL))
441 #define LDAP_FREE(p) (ber_memfree_x((p),NULL))
442 #define LDAP_VFREE(v) (ber_memvfree_x((void **)(v),NULL))
443 #define LDAP_STRDUP(s) (ber_strdup_x((s),NULL))
444 #define LDAP_STRNDUP(s,l) (ber_strndup_x((s),(l),NULL))
446 #define LDAP_MALLOCX(s,x) (ber_memalloc_x((s),(x)))
447 #define LDAP_CALLOCX(n,s,x) (ber_memcalloc_x((n),(s),(x)))
448 #define LDAP_REALLOCX(p,s,x) (ber_memrealloc_x((p),(s),(x)))
449 #define LDAP_FREEX(p,x) (ber_memfree_x((p),(x)))
450 #define LDAP_VFREEX(v,x) (ber_memvfree_x((void **)(v),(x)))
451 #define LDAP_STRDUPX(s,x) (ber_strdup_x((s),(x)))
452 #define LDAP_STRNDUPX(s,l,x) (ber_strndup_x((s),(l),(x)))
457 LDAP_F (void) ldap_int_error_init( void );
462 LDAP_F (void) ldap_int_utils_init LDAP_P(( void ));
468 LDAP_F (int) ldap_log_printf LDAP_P((LDAP *ld, int level, const char *fmt, ...)) LDAP_GCCATTR((format(printf, 3, 4)));
473 LDAP_F (void) ldap_add_request_to_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype,
474 BerElement *request ));
475 LDAP_F (void) ldap_add_result_to_cache LDAP_P(( LDAP *ld, LDAPMessage *result ));
476 LDAP_F (int) ldap_check_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype, BerElement *request ));
481 LDAP_F (int) ldap_int_put_controls LDAP_P((
483 LDAPControl *const *ctrls,
486 LDAP_F (int) ldap_int_client_controls LDAP_P((
488 LDAPControl **ctrlp ));
493 LDAP_F (int) ldap_int_next_line_tokens LDAP_P(( char **bufp, ber_len_t *blenp, char ***toksp ));
499 LDAP_F (int) ldap_open_defconn( LDAP *ld );
500 LDAP_F (int) ldap_int_open_connection( LDAP *ld,
501 LDAPConn *conn, LDAPURLDesc *srvlist, int async );
507 LDAP_V (int) ldap_int_tblsize;
508 LDAP_F (void) ldap_int_ip_init( void );
511 LDAP_F (int) ldap_int_timeval_dup( struct timeval **dest,
512 const struct timeval *tm );
513 LDAP_F (int) ldap_connect_to_host( LDAP *ld, Sockbuf *sb,
514 int proto, LDAPURLDesc *srv, int async );
515 LDAP_F (int) ldap_int_poll( LDAP *ld, ber_socket_t s,
516 struct timeval *tvp );
518 #if defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
519 LDAP_V (char *) ldap_int_hostname;
520 LDAP_F (char *) ldap_host_connected_to( Sockbuf *sb,
524 LDAP_F (int) ldap_int_select( LDAP *ld, struct timeval *timeout );
525 LDAP_F (void *) ldap_new_select_info( void );
526 LDAP_F (void) ldap_free_select_info( void *sip );
527 LDAP_F (void) ldap_mark_select_write( LDAP *ld, Sockbuf *sb );
528 LDAP_F (void) ldap_mark_select_read( LDAP *ld, Sockbuf *sb );
529 LDAP_F (void) ldap_mark_select_clear( LDAP *ld, Sockbuf *sb );
530 LDAP_F (int) ldap_is_read_ready( LDAP *ld, Sockbuf *sb );
531 LDAP_F (int) ldap_is_write_ready( LDAP *ld, Sockbuf *sb );
533 LDAP_F (int) ldap_int_connect_cbs( LDAP *ld, Sockbuf *sb,
534 ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr );
540 LDAP_F (int) ldap_connect_to_path( LDAP *ld, Sockbuf *sb,
541 LDAPURLDesc *srv, int async );
542 #endif /* LDAP_PF_LOCAL */
547 LDAP_F (ber_int_t) ldap_send_initial_request( LDAP *ld, ber_tag_t msgtype,
548 const char *dn, BerElement *ber, ber_int_t msgid );
549 LDAP_F (BerElement *) ldap_alloc_ber_with_options( LDAP *ld );
550 LDAP_F (void) ldap_set_ber_options( LDAP *ld, BerElement *ber );
552 LDAP_F (int) ldap_send_server_request( LDAP *ld, BerElement *ber, ber_int_t msgid, LDAPRequest *parentreq, LDAPURLDesc **srvlist, LDAPConn *lc, LDAPreqinfo *bind );
553 LDAP_F (LDAPConn *) ldap_new_connection( LDAP *ld, LDAPURLDesc **srvlist, int use_ldsb, int connect, LDAPreqinfo *bind );
554 LDAP_F (LDAPRequest *) ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid );
555 LDAP_F (void) ldap_return_request( LDAP *ld, LDAPRequest *lr, int freeit );
556 LDAP_F (void) ldap_free_request( LDAP *ld, LDAPRequest *lr );
557 LDAP_F (void) ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind );
558 LDAP_F (void) ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all );
559 LDAP_F (void) ldap_dump_requests_and_responses( LDAP *ld );
560 LDAP_F (int) ldap_chase_referrals( LDAP *ld, LDAPRequest *lr,
561 char **errstrp, int sref, int *hadrefp );
562 LDAP_F (int) ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr,
563 char **refs, int sref, char **referralsp, int *hadrefp );
564 LDAP_F (int) ldap_append_referral( LDAP *ld, char **referralsp, char *s );
565 LDAP_F (int) ldap_int_flush_request( LDAP *ld, LDAPRequest *lr );
570 LDAP_F (const char *) ldap_int_msgtype2str( ber_tag_t tag );
575 LDAP_F (BerElement *) ldap_build_search_req LDAP_P((
582 LDAPControl **sctrls,
583 LDAPControl **cctrls,
592 LDAP_F (int) ldap_ld_free LDAP_P((
595 LDAPControl **sctrls,
596 LDAPControl **cctrls ));
598 LDAP_F (int) ldap_send_unbind LDAP_P((
601 LDAPControl **sctrls,
602 LDAPControl **cctrls ));
607 LDAP_F (LDAPURLDesc *) ldap_url_dup LDAP_P((
608 LDAPURLDesc *ludp ));
610 LDAP_F (LDAPURLDesc *) ldap_url_duplist LDAP_P((
611 LDAPURLDesc *ludlist ));
613 LDAP_F (int) ldap_url_parsehosts LDAP_P((
614 LDAPURLDesc **ludlist,
618 LDAP_F (char *) ldap_url_list2hosts LDAP_P((
619 LDAPURLDesc *ludlist ));
625 LDAP_F (int) ldap_int_sasl_init LDAP_P(( void ));
627 LDAP_F (int) ldap_int_sasl_open LDAP_P((
628 LDAP *ld, LDAPConn *conn,
630 LDAP_F (int) ldap_int_sasl_close LDAP_P(( LDAP *ld, LDAPConn *conn ));
632 LDAP_F (int) ldap_int_sasl_external LDAP_P((
633 LDAP *ld, LDAPConn *conn,
634 const char* authid, ber_len_t ssf ));
636 LDAP_F (int) ldap_int_sasl_get_option LDAP_P(( LDAP *ld,
637 int option, void *arg ));
638 LDAP_F (int) ldap_int_sasl_set_option LDAP_P(( LDAP *ld,
639 int option, void *arg ));
640 LDAP_F (int) ldap_int_sasl_config LDAP_P(( struct ldapoptions *lo,
641 int option, const char *arg ));
643 LDAP_F (int) ldap_int_sasl_bind LDAP_P((
647 LDAPControl **, LDAPControl **,
649 /* should be passed in client controls */
651 LDAP_SASL_INTERACT_PROC *interact,
655 LDAP_F (char *) ldap_int_parse_numericoid LDAP_P((
663 LDAP_F (int) ldap_int_tls_config LDAP_P(( LDAP *ld,
664 int option, const char *arg ));
666 LDAP_F (int) ldap_int_tls_start LDAP_P(( LDAP *ld,
667 LDAPConn *conn, LDAPURLDesc *srv ));
669 LDAP_F (void) ldap_int_tls_destroy LDAP_P(( struct ldapoptions *lo ));
674 LDAP_F (char **) ldap_value_dup LDAP_P((
675 char *const *vals ));
679 #endif /* _LDAP_INT_H */