2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * ldap-int.h - defines & prototypes internal to the LDAP library
16 #define LDAP_THREAD_SAFE 1
19 #include "../liblber/lber-int.h"
21 #define ldap_debug (ldap_int_global_options.ldo_debug)
23 #define Debug( level, fmt, arg1, arg2, arg3 ) \
24 ldap_log_printf( NULL, (level), (fmt), (arg1), (arg2), (arg3) )
34 #define LDAP_URL_PREFIX "ldap://"
35 #define LDAP_URL_PREFIX_LEN (sizeof(LDAP_URL_PREFIX)-1)
36 #define LDAPS_URL_PREFIX "ldaps://"
37 #define LDAPS_URL_PREFIX_LEN (sizeof(LDAPS_URL_PREFIX)-1)
38 #define LDAP_URL_URLCOLON "URL:"
39 #define LDAP_URL_URLCOLON_LEN (sizeof(LDAP_URL_URLCOLON)-1)
40 #define NULLLDAPURLDESC ((LDAPURLDesc *)NULL)
42 #define LDAP_REF_STR "Referral:\n"
43 #define LDAP_REF_STR_LEN (sizeof(LDAP_REF_STR)-1)
44 #define LDAP_LDAP_REF_STR LDAP_URL_PREFIX
45 #define LDAP_LDAP_REF_STR_LEN (sizeof(LDAP_LDAP_REF_STR)-1)
47 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
48 #define LDAP_DX_REF_STR "dx://"
49 #define LDAP_DX_REF_STR_LEN (sizeof(LDAP_DX_REF_STR)-1)
50 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
52 #define LDAP_DEFAULT_REFHOPLIMIT 5
54 #define LDAP_BOOL_REFERRALS 0
55 #define LDAP_BOOL_RESTART 1
56 #define LDAP_BOOL_DNS 2
57 #define LDAP_BOOL_TLS 3
59 #define LDAP_BOOLEANS unsigned long
60 #define LDAP_BOOL(n) (1 << (n))
61 #define LDAP_BOOL_GET(lo, bool) ((lo)->ldo_booleans & LDAP_BOOL(bool) \
63 #define LDAP_BOOL_SET(lo, bool) ((lo)->ldo_booleans |= LDAP_BOOL(bool))
64 #define LDAP_BOOL_CLR(lo, bool) ((lo)->ldo_booleans &= ~LDAP_BOOL(bool))
65 #define LDAP_BOOL_ZERO(lo) ((lo)->ldo_booleans = 0)
68 * This structure represents both ldap messages and ldap responses.
69 * These are really the same, except in the case of search responses,
70 * where a response has multiple messages.
74 ber_int_t lm_msgid; /* the message id */
75 ber_tag_t lm_msgtype; /* the message type */
76 BerElement *lm_ber; /* the ber encoded message contents */
77 struct ldapmsg *lm_chain; /* for search - next msg in the resp */
78 struct ldapmsg *lm_next; /* next response */
79 time_t lm_time; /* used to maintain cache */
83 * structure representing get/set'able options
84 * which have global defaults.
88 #define LDAP_UNINITIALIZED 0x0
89 #define LDAP_INITIALIZED 0x1
90 #define LDAP_VALID_SESSION 0x2
94 ber_int_t ldo_version; /* version to connect at */
96 ber_int_t ldo_timelimit;
97 ber_int_t ldo_sizelimit;
103 #ifdef LDAP_CONNECTIONLESS
104 int ldo_cldaptries; /* connectionless search retry count */
105 int ldo_cldaptimeout;/* time between retries */
108 int ldo_refhoplimit; /* limit on referral nesting */
110 /* LDAPv3 server and client controls */
111 LDAPControl **ldo_sctrls;
112 LDAPControl **ldo_cctrls;
119 LDAP_BOOLEANS ldo_booleans; /* boolean options */
123 * structure for tracking LDAP server host, ports, DNs, etc.
125 typedef struct ldap_server {
127 char *lsrv_dn; /* if NULL, use default */
129 struct ldap_server *lsrv_next;
134 * structure for representing an LDAP server connection
136 typedef struct ldap_conn {
139 time_t lconn_lastused; /* time */
141 #define LDAP_CONNST_NEEDSOCKET 1
142 #define LDAP_CONNST_CONNECTING 2
143 #define LDAP_CONNST_CONNECTED 3
144 LDAPServer *lconn_server;
145 char *lconn_krbinstance;
146 struct ldap_conn *lconn_next;
147 BerElement *lconn_ber;/* ber receiving on this conn. */
152 * structure used to track outstanding requests
154 typedef struct ldapreq {
155 ber_int_t lr_msgid; /* the message id */
156 int lr_status; /* status of request */
157 #define LDAP_REQST_INPROGRESS 1
158 #define LDAP_REQST_CHASINGREFS 2
159 #define LDAP_REQST_NOTCONNECTED 3
160 #define LDAP_REQST_WRITING 4
161 int lr_outrefcnt; /* count of outstanding referrals */
162 ber_int_t lr_origid; /* original request's message id */
163 int lr_parentcnt; /* count of parent requests */
164 ber_tag_t lr_res_msgtype; /* result message type */
165 ber_int_t lr_res_errno; /* result LDAP errno */
166 char *lr_res_error; /* result error string */
167 char *lr_res_matched;/* result matched DN string */
168 BerElement *lr_ber; /* ber encoded request contents */
169 LDAPConn *lr_conn; /* connection used to send request */
170 struct ldapreq *lr_parent; /* request that spawned this referral */
171 struct ldapreq *lr_refnext; /* next referral spawned */
172 struct ldapreq *lr_prev; /* previous request */
173 struct ldapreq *lr_next; /* next request */
177 * structure for client cache
179 #define LDAP_CACHE_BUCKETS 31 /* cache hash table size */
180 typedef struct ldapcache {
181 LDAPMessage *lc_buckets[LDAP_CACHE_BUCKETS];/* hash table */
182 LDAPMessage *lc_requests; /* unfulfilled reqs */
183 long lc_timeout; /* request timeout */
184 ber_len_t lc_maxmem; /* memory to use */
185 ber_len_t lc_memused; /* memory in use */
186 int lc_enabled; /* enabled? */
187 unsigned long lc_options; /* options */
188 #define LDAP_CACHE_OPT_CACHENOERRS 0x00000001
189 #define LDAP_CACHE_OPT_CACHEALLERRS 0x00000002
193 * handy macro for checking if handle is connectionless
196 #define LDAP_IS_CLDAP(ld) ((ld)->ld_cldapnaddr>0)
199 * structure representing an ldap connection
203 Sockbuf ld_sb; /* socket descriptor & buffer */
205 struct ldapoptions ld_options;
207 #define ld_valid ld_options.ldo_valid
208 #define ld_debug ld_options.ldo_debug
210 #define ld_deref ld_options.ldo_deref
211 #define ld_timelimit ld_options.ldo_timelimit
212 #define ld_sizelimit ld_options.ldo_sizelimit
214 #define ld_defbase ld_options.ldo_defbase
215 #define ld_defhost ld_options.ldo_defhost
216 #define ld_defport ld_options.ldo_defport
218 #define ld_cldaptries ld_options.ldo_cldaptries
219 #define ld_cldaptimeout ld_options.ldo_cldaptimeout
220 #define ld_refhoplimit ld_options.ldo_refhoplimit
222 #define ld_sctrls ld_options.ldo_sctrls
223 #define ld_cctrls ld_options.ldo_cctrls
225 ber_int_t ld_version; /* version connected at */
229 unsigned short ld_lberoptions;
231 LDAPFiltDesc *ld_filtd; /* from getfilter for ufn searches */
232 char *ld_ufnprefix; /* for incomplete ufn's */
239 /* do not mess with these */
240 LDAPRequest *ld_requests; /* list of outstanding requests */
241 LDAPMessage *ld_responses; /* list of outstanding responses */
243 ber_int_t *ld_abandoned; /* array of abandoned requests */
245 LDAPCache *ld_cache; /* non-null if cache is initialized */
246 /* stuff used by connectionless searches. */
248 char *ld_cldapdn; /* DN used in connectionless search */
249 int ld_cldapnaddr; /* number of addresses */
250 void **ld_cldapaddrs;/* addresses to send request to */
252 /* do not mess with the rest though */
253 BERTranslateProc ld_lber_encode_translate_proc;
254 BERTranslateProc ld_lber_decode_translate_proc;
256 LDAPConn *ld_defconn; /* default connection */
257 LDAPConn *ld_conns; /* list of server connections */
258 void *ld_selectinfo; /* platform specifics for select */
259 int (*ld_rebindproc)( struct ldap *ld, char **dnp,
260 char **passwdp, int *authmethodp, int freeit );
261 /* routine to get info needed for re-bind */
263 #define LDAP_VALID(ld) ( (ld)->ld_valid == LDAP_VALID_SESSION )
269 extern struct ldapoptions ldap_int_global_options;
270 void ldap_int_initialize LDAP_P((void));
274 /* simple macros to realloc for now */
275 #define LDAP_INT_MALLOC(s) (LBER_MALLOC((s)))
276 #define LDAP_INT_CALLOC(n,s) (LBER_CALLOC((n),(s)))
277 #define LDAP_INT_REALLOC(p,s) (LBER_REALLOC((p),(s)))
278 #define LDAP_INT_FREE(p) (LBER_FREE((p)))
279 #define LDAP_INT_VFREE(v) (LBER_VFREE((void **)(v)))
280 #define LDAP_INT_STRDUP(s) (LBER_STRDUP((s)))
282 #define LDAP_MALLOC(s) (LBER_MALLOC((s)))
283 #define LDAP_CALLOC(n,s) (LBER_CALLOC((n),(s)))
284 #define LDAP_REALLOC(p,s) (LBER_REALLOC((p),(s)))
285 #define LDAP_FREE(p) (LBER_FREE((p)))
286 #define LDAP_VFREE(v) (LBER_VFREE((void **)(v)))
287 #define LDAP_STRDUP(s) (LBER_STRDUP((s)))
292 void ldap_int_utils_init LDAP_P(( void ));
298 int ldap_log_printf LDAP_P((LDAP *ld, int level, const char *fmt, ...));
303 void ldap_add_request_to_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype,
304 BerElement *request ));
305 void ldap_add_result_to_cache LDAP_P(( LDAP *ld, LDAPMessage *result ));
306 int ldap_check_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype, BerElement *request ));
311 LDAPControl *ldap_control_dup LDAP_P((
312 const LDAPControl *ctrl ));
314 LDAPControl **ldap_controls_dup LDAP_P((
315 LDAPControl *const *ctrls ));
317 int ldap_int_get_controls LDAP_P((
319 LDAPControl ***ctrlsp));
321 int ldap_int_put_controls LDAP_P((
323 LDAPControl *const *ctrls,
329 int next_line_tokens LDAP_P(( char **bufp, ber_len_t *blenp, char ***toksp ));
330 void free_strarray LDAP_P(( char **sap ));
336 char *ldap_get_kerberosv4_credentials LDAP_P((
338 LDAP_CONST char *who,
339 LDAP_CONST char *service,
342 #endif /* HAVE_KERBEROS */
348 int open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
349 char **krbinstancep, int async );
355 extern int ldap_int_tblsize;
357 int ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address, int port,
359 void ldap_close_connection( Sockbuf *sb );
362 char *ldap_host_connected_to( Sockbuf *sb );
363 #endif /* HAVE_KERBEROS */
365 void ldap_int_ip_init( void );
366 int do_ldap_select( LDAP *ld, struct timeval *timeout );
367 void *ldap_new_select_info( void );
368 void ldap_free_select_info( void *sip );
369 void ldap_mark_select_write( LDAP *ld, Sockbuf *sb );
370 void ldap_mark_select_read( LDAP *ld, Sockbuf *sb );
371 void ldap_mark_select_clear( LDAP *ld, Sockbuf *sb );
372 int ldap_is_read_ready( LDAP *ld, Sockbuf *sb );
373 int ldap_is_write_ready( LDAP *ld, Sockbuf *sb );
379 ber_int_t ldap_send_initial_request( LDAP *ld, ber_tag_t msgtype,
380 const char *dn, BerElement *ber );
381 BerElement *ldap_alloc_ber_with_options( LDAP *ld );
382 void ldap_set_ber_options( LDAP *ld, BerElement *ber );
384 int ldap_send_server_request( LDAP *ld, BerElement *ber, ber_int_t msgid,
385 LDAPRequest *parentreq, LDAPServer *srvlist, LDAPConn *lc,
387 LDAPConn *ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
388 int connect, int bind );
389 LDAPRequest *ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid );
390 void ldap_free_request( LDAP *ld, LDAPRequest *lr );
391 void ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind );
392 void ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all );
393 void ldap_dump_requests_and_responses( LDAP *ld );
395 int ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp );
396 int ldap_append_referral( LDAP *ld, char **referralsp, char *s );
401 #ifdef LDAP_CONNECTIONLESS
402 int cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber );
408 BerElement *ldap_build_search_req LDAP_P((
415 LDAPControl **sctrls,
416 LDAPControl **cctrls,
418 ber_int_t sizelimit ));
423 /* see <ac/string.h> */
428 int ldap_ld_free LDAP_P((
431 LDAPControl **sctrls,
432 LDAPControl **cctrls ));
434 int ldap_send_unbind LDAP_P((
437 LDAPControl **sctrls,
438 LDAPControl **cctrls ));
440 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
444 char **ldap_getdxbyname( const char *domain );
445 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
447 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
451 * added-in this stuff so that libldap.a would build, i.e. refs to
452 * these routines from open.c would resolve.
453 * hodges@stanford.edu 5-Feb-96
455 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
457 int ldap_t61_to_8859( char **bufp, ber_len_t *buflenp, int free_input );
459 int ldap_8859_to_t61( char **bufp, ber_len_t *buflenp, int free_input );
460 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
461 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
465 #endif /* _LDAP_INT_H */