]> git.sur5r.net Git - openldap/blob - libraries/libldap/ldap-int.h
LDAPv3 referrals changes by Steve Sonntag @ Novell
[openldap] / libraries / libldap / ldap-int.h
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  ldap-int.h - defines & prototypes internal to the LDAP library
11  */
12
13 #ifndef _LDAP_INT_H
14 #define _LDAP_INT_H 1
15
16 #ifdef LDAP_R_COMPILE
17 #define LDAP_THREAD_SAFE 1
18 #endif
19
20 #include "../liblber/lber-int.h"
21
22 #define ldap_debug      (ldap_int_global_options.ldo_debug)
23 #undef Debug
24 #define Debug( level, fmt, arg1, arg2, arg3 ) \
25         ldap_log_printf( NULL, (level), (fmt), (arg1), (arg2), (arg3) )
26
27 #include "ldap_log.h"
28
29 #include "ldap.h"
30
31 #include "ldap_pvt.h"
32
33 LDAP_BEGIN_DECL
34
35 #define LDAP_URL_PREFIX         "ldap://"
36 #define LDAP_URL_PREFIX_LEN     (sizeof(LDAP_URL_PREFIX)-1)
37 #define LDAPS_URL_PREFIX                "ldaps://"
38 #define LDAPS_URL_PREFIX_LEN    (sizeof(LDAPS_URL_PREFIX)-1)
39 #define LDAPI_URL_PREFIX        "ldapi://"
40 #define LDAPI_URL_PREFIX_LEN    (sizeof(LDAPI_URL_PREFIX)-1)
41 #define LDAPIS_URL_PREFIX       "ldapis://"
42 #define LDAPIS_URL_PREFIX_LEN   (sizeof(LDAPIS_URL_PREFIX)-1)
43 #define LDAP_URL_URLCOLON               "URL:"
44 #define LDAP_URL_URLCOLON_LEN   (sizeof(LDAP_URL_URLCOLON)-1)
45 #define NULLLDAPURLDESC ((LDAPURLDesc *)NULL)
46
47 #define LDAP_REF_STR            "Referral:\n"
48 #define LDAP_REF_STR_LEN        (sizeof(LDAP_REF_STR)-1)
49 #define LDAP_LDAP_REF_STR       LDAP_URL_PREFIX
50 #define LDAP_LDAP_REF_STR_LEN   (sizeof(LDAP_LDAP_REF_STR)-1)
51
52 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
53 #define LDAP_DX_REF_STR         "dx://"
54 #define LDAP_DX_REF_STR_LEN     (sizeof(LDAP_DX_REF_STR)-1)
55 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
56
57 #define LDAP_DEFAULT_REFHOPLIMIT 5
58
59 #define LDAP_BOOL_REFERRALS             0
60 #define LDAP_BOOL_RESTART               1
61 #define LDAP_BOOL_DNS                   2
62 #define LDAP_BOOL_TLS                   3
63
64 #define LDAP_BOOLEANS   unsigned long
65 #define LDAP_BOOL(n)    (1 << (n))
66 #define LDAP_BOOL_GET(lo, bool) ((lo)->ldo_booleans & LDAP_BOOL(bool) \
67                                                                         ?  -1 : 0)
68 #define LDAP_BOOL_SET(lo, bool) ((lo)->ldo_booleans |= LDAP_BOOL(bool))
69 #define LDAP_BOOL_CLR(lo, bool) ((lo)->ldo_booleans &= ~LDAP_BOOL(bool))
70 #define LDAP_BOOL_ZERO(lo) ((lo)->ldo_booleans = 0)
71
72 /*
73  * This structure represents both ldap messages and ldap responses.
74  * These are really the same, except in the case of search responses,
75  * where a response has multiple messages.
76  */
77
78 struct ldapmsg {
79         ber_int_t               lm_msgid;       /* the message id */
80         ber_tag_t               lm_msgtype;     /* the message type */
81         BerElement      *lm_ber;        /* the ber encoded message contents */
82         struct ldapmsg  *lm_chain;      /* for search - next msg in the resp */
83         struct ldapmsg  *lm_next;       /* next response */
84         time_t  lm_time;        /* used to maintain cache */
85 };
86
87 /*
88  * structure representing get/set'able options
89  * which have global defaults.
90  */
91 struct ldapoptions {
92         short ldo_valid;
93 #define LDAP_UNINITIALIZED      0x0
94 #define LDAP_INITIALIZED        0x1
95 #define LDAP_VALID_SESSION      0x2
96
97         int             ldo_debug;
98         /* per API call timeout */
99         struct timeval          *ldo_tm_api;
100         struct timeval          *ldo_tm_net;
101
102         ber_int_t               ldo_version;
103         ber_int_t               ldo_deref;
104         ber_int_t               ldo_timelimit;
105         ber_int_t               ldo_sizelimit;
106
107         LDAPURLDesc *ldo_defludp;
108         int             ldo_defport;
109         char*   ldo_defbase;
110         char*   ldo_defbinddn;  /* simple bind dn */
111
112 #ifdef LDAP_CONNECTIONLESS
113         int             ldo_cldaptries; /* connectionless search retry count */
114         int             ldo_cldaptimeout;/* time between retries */
115 #endif
116
117         int             ldo_refhoplimit;        /* limit on referral nesting */
118
119         /* LDAPv3 server and client controls */
120         LDAPControl     **ldo_sctrls;
121         LDAPControl **ldo_cctrls;
122         /* LDAPV3 rebind callback function
123         */
124         LDAP_REBIND_PROC                *ldo_rebindproc;
125
126 #ifdef HAVE_TLS
127         /* tls context */
128         void            *ldo_tls_ctx;
129         int             ldo_tls_mode;
130 #endif
131 #ifdef HAVE_CYRUS_SASL
132         sasl_ssf_t      ldo_sasl_minssf;
133         sasl_ssf_t      ldo_sasl_maxssf;
134 #endif
135         LDAP_BOOLEANS ldo_booleans;     /* boolean options */
136 };
137
138
139 /*
140  * structure for tracking LDAP server host, ports, DNs, etc.
141  */
142 typedef struct ldap_server {
143         char                    *lsrv_host;
144         char                    *lsrv_dn;       /* if NULL, use default */
145         int                     lsrv_port;
146 /*      int                     lsrv_protocol; */
147         struct ldap_server      *lsrv_next;
148 } LDAPServer;
149
150
151 /*
152  * structure for representing an LDAP server connection
153  */
154 typedef struct ldap_conn {
155         Sockbuf                 *lconn_sb;
156         int                     lconn_refcnt;
157         time_t          lconn_lastused; /* time */
158         int                     lconn_rebind_inprogress;        /* set if rebind in progress */
159         char            ***lconn_rebind_queue;          /* used if rebind in progress */
160         int                     lconn_status;
161 #define LDAP_CONNST_NEEDSOCKET          1
162 #define LDAP_CONNST_CONNECTING          2
163 #define LDAP_CONNST_CONNECTED           3
164         LDAPURLDesc             *lconn_server;
165         char                    *lconn_krbinstance;
166         struct ldap_conn        *lconn_next;
167         BerElement              *lconn_ber;/* ber receiving on this conn. */
168 } LDAPConn;
169
170
171 /*
172  * structure used to track outstanding requests
173  */
174 typedef struct ldapreq {
175         ber_int_t               lr_msgid;       /* the message id */
176         int             lr_status;      /* status of request */
177 #define LDAP_REQST_COMPLETED    0
178 #define LDAP_REQST_INPROGRESS   1
179 #define LDAP_REQST_CHASINGREFS  2
180 #define LDAP_REQST_NOTCONNECTED 3
181 #define LDAP_REQST_WRITING      4
182         int             lr_outrefcnt;   /* count of outstanding referrals */
183         ber_int_t               lr_origid;      /* original request's message id */
184         int             lr_parentcnt;   /* count of parent requests */
185         ber_tag_t               lr_res_msgtype; /* result message type */
186         ber_int_t               lr_res_errno;   /* result LDAP errno */
187         char            *lr_res_error;  /* result error string */
188         char            *lr_res_matched;/* result matched DN string */
189         BerElement      *lr_ber;        /* ber encoded request contents */
190         LDAPConn        *lr_conn;       /* connection used to send request */
191         struct ldapreq  *lr_parent;     /* request that spawned this referral */
192         struct ldapreq  *lr_refnext;    /* next referral spawned */
193         struct ldapreq  *lr_prev;       /* previous request */
194         struct ldapreq  *lr_next;       /* next request */
195 } LDAPRequest;
196
197 /*
198  * structure for client cache
199  */
200 #define LDAP_CACHE_BUCKETS      31      /* cache hash table size */
201 typedef struct ldapcache {
202         LDAPMessage     *lc_buckets[LDAP_CACHE_BUCKETS];/* hash table */
203         LDAPMessage     *lc_requests;                   /* unfulfilled reqs */
204         long            lc_timeout;                     /* request timeout */
205         ber_len_t               lc_maxmem;                      /* memory to use */
206         ber_len_t               lc_memused;                     /* memory in use */
207         int             lc_enabled;                     /* enabled? */
208         unsigned long   lc_options;                     /* options */
209 #define LDAP_CACHE_OPT_CACHENOERRS      0x00000001
210 #define LDAP_CACHE_OPT_CACHEALLERRS     0x00000002
211 }  LDAPCache;
212
213 /*
214  * structure containing referral request info for rebind procedure
215  */
216 typedef struct ldapreqinfo {
217         ber_len_t       ri_msgid;
218         int                     ri_request;
219         char            *ri_url;
220 } LDAPreqinfo;
221
222 /*
223  * handy macro for checking if handle is connectionless
224  */
225
226 #define LDAP_IS_CLDAP(ld) ((ld)->ld_cldapnaddr>0)
227
228 /*
229  * structure representing an ldap connection
230  */
231
232 struct ldap {
233         Sockbuf         ld_sb;          /* socket descriptor & buffer */
234
235         struct ldapoptions ld_options;
236
237 #define ld_valid                ld_options.ldo_valid
238 #define ld_debug                ld_options.ldo_debug
239
240 #define ld_deref                ld_options.ldo_deref
241 #define ld_timelimit    ld_options.ldo_timelimit
242 #define ld_sizelimit    ld_options.ldo_sizelimit
243
244 #define ld_defbinddn    ld_options.ldo_defbinddn
245 #define ld_defbase              ld_options.ldo_defbase
246 #define ld_defhost              ld_options.ldo_defhost
247 #define ld_defport              ld_options.ldo_defport
248
249 #define ld_cldaptries   ld_options.ldo_cldaptries
250 #define ld_cldaptimeout ld_options.ldo_cldaptimeout
251 #define ld_refhoplimit  ld_options.ldo_refhoplimit
252
253 #define ld_sctrls               ld_options.ldo_sctrls
254 #define ld_cctrls               ld_options.ldo_cctrls
255 #define ld_rebindproc   ld_options.ldo_rebindproc
256
257 #define ld_version              ld_options.ldo_version  
258         char    *ld_host;
259         int             ld_port;
260
261         unsigned short  ld_lberoptions;
262
263         LDAPFiltDesc    *ld_filtd;      /* from getfilter for ufn searches */
264         char            *ld_ufnprefix;  /* for incomplete ufn's */
265
266         ber_int_t       ld_errno;
267         char    *ld_error;
268         char    *ld_matched;
269         ber_len_t               ld_msgid;
270
271         /* do not mess with these */
272         LDAPRequest     *ld_requests;   /* list of outstanding requests */
273         LDAPMessage     *ld_responses;  /* list of outstanding responses */
274
275         ber_int_t               *ld_abandoned;  /* array of abandoned requests */
276
277         LDAPCache       *ld_cache;      /* non-null if cache is initialized */
278         /* stuff used by connectionless searches. */
279
280         char            *ld_cldapdn;    /* DN used in connectionless search */
281         int             ld_cldapnaddr; /* number of addresses */
282         void            **ld_cldapaddrs;/* addresses to send request to */
283
284         /* do not mess with the rest though */
285         BERTranslateProc ld_lber_encode_translate_proc;
286         BERTranslateProc ld_lber_decode_translate_proc;
287
288         LDAPConn        *ld_defconn;    /* default connection */
289         LDAPConn        *ld_conns;      /* list of server connections */
290         void            *ld_selectinfo; /* platform specifics for select */
291 #ifdef HAVE_CYRUS_SASL
292         sasl_conn_t             *ld_sasl_context;
293 #endif /* HAVE_CYRUS_SASL */
294 };
295 #define LDAP_VALID(ld)  ( (ld)->ld_valid == LDAP_VALID_SESSION )
296
297 #if defined(HAVE_RES_SEARCH) && defined(LDAP_R_COMPILE)
298 #include <ldap_pvt_thread.h>
299 extern ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
300 #endif /* HAVE_RES_SEARCH && LDAP_R_COMPILE */
301
302 /*
303  * in init.c
304  */
305
306 LIBLDAP_F ( struct ldapoptions ) ldap_int_global_options;
307 LIBLDAP_F ( void ) ldap_int_initialize LDAP_P((int *));
308
309 /* memory.c */
310         /* simple macros to realloc for now */
311 #define LDAP_INT_MALLOC(s)              (LBER_MALLOC((s)))
312 #define LDAP_INT_CALLOC(n,s)    (LBER_CALLOC((n),(s)))
313 #define LDAP_INT_REALLOC(p,s)   (LBER_REALLOC((p),(s)))
314 #define LDAP_INT_FREE(p)                (LBER_FREE((p)))
315 #define LDAP_INT_VFREE(v)               (LBER_VFREE((void **)(v)))
316 #define LDAP_INT_STRDUP(s)              (LBER_STRDUP((s)))
317
318 #define LDAP_MALLOC(s)          (LBER_MALLOC((s)))
319 #define LDAP_CALLOC(n,s)        (LBER_CALLOC((n),(s)))
320 #define LDAP_REALLOC(p,s)       (LBER_REALLOC((p),(s)))
321 #define LDAP_FREE(p)            (LBER_FREE((p)))
322 #define LDAP_VFREE(v)           (LBER_VFREE((void **)(v)))
323 #define LDAP_STRDUP(s)          (LBER_STRDUP((s)))
324
325 /*
326  * in unit-int.c
327  */
328 LIBLDAP_F (void) ldap_int_utils_init LDAP_P(( void ));
329
330
331 /*
332  * in print.c
333  */
334 LIBLDAP_F (int) ldap_log_printf LDAP_P((LDAP *ld, int level, const char *fmt, ...)) LDAP_GCCATTR((format(printf, 3, 4)));
335
336 /*
337  * in cache.c
338  */
339 LIBLDAP_F (void) ldap_add_request_to_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype,
340         BerElement *request ));
341 LIBLDAP_F (void) ldap_add_result_to_cache LDAP_P(( LDAP *ld, LDAPMessage *result ));
342 LIBLDAP_F (int) ldap_check_cache LDAP_P(( LDAP *ld, ber_tag_t msgtype, BerElement *request ));
343
344 /*
345  * in controls.c
346  */
347 LIBLDAP_F (LDAPControl *) ldap_control_dup LDAP_P((
348         const LDAPControl *ctrl ));
349
350 LIBLDAP_F (LDAPControl **) ldap_controls_dup LDAP_P((
351         LDAPControl *const *ctrls ));
352
353 LIBLDAP_F (int) ldap_int_get_controls LDAP_P((
354         BerElement *be,
355         LDAPControl ***ctrlsp));
356
357 LIBLDAP_F (int) ldap_int_put_controls LDAP_P((
358         LDAP *ld,
359         LDAPControl *const *ctrls,
360         BerElement *ber ));
361
362 /*
363  * in dsparse.c
364  */
365 LIBLDAP_F (int) next_line_tokens LDAP_P(( char **bufp, ber_len_t *blenp, char ***toksp ));
366 LIBLDAP_F (void) free_strarray LDAP_P(( char **sap ));
367
368 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
369 /*
370  * in kerberos.c
371  */
372 LIBLDAP_F (char *) ldap_get_kerberosv4_credentials LDAP_P((
373         LDAP *ld,
374         LDAP_CONST char *who,
375         LDAP_CONST char *service,
376         ber_len_t *len ));
377
378 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
379
380
381 /*
382  * in open.c
383  */
384 LIBLDAP_F (int) ldap_open_defconn( LDAP *ld );
385 LIBLDAP_F (int) open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srvlist, char **krbinstancep, int async );
386
387 /*
388  * in os-ip.c
389  */
390 LIBLDAP_F (int) ldap_int_tblsize;
391 LIBLDAP_F (int) ldap_int_timeval_dup( struct timeval **dest, const struct timeval *tm );
392 LIBLDAP_F (int) ldap_connect_to_host( LDAP *ld, Sockbuf *sb, const char *host, unsigned long address, int port, int async );
393
394 LIBLDAP_F (void) ldap_close_connection( Sockbuf *sb );
395
396 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
397 LIBLDAP_F (char *) ldap_host_connected_to( Sockbuf *sb );
398 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
399
400 LIBLDAP_F (void) ldap_int_ip_init( void );
401 LIBLDAP_F (int) do_ldap_select( LDAP *ld, struct timeval *timeout );
402 LIBLDAP_F (void *) ldap_new_select_info( void );
403 LIBLDAP_F (void) ldap_free_select_info( void *sip );
404 LIBLDAP_F (void) ldap_mark_select_write( LDAP *ld, Sockbuf *sb );
405 LIBLDAP_F (void) ldap_mark_select_read( LDAP *ld, Sockbuf *sb );
406 LIBLDAP_F (void) ldap_mark_select_clear( LDAP *ld, Sockbuf *sb );
407 LIBLDAP_F (int) ldap_is_read_ready( LDAP *ld, Sockbuf *sb );
408 LIBLDAP_F (int) ldap_is_write_ready( LDAP *ld, Sockbuf *sb );
409
410 #ifdef LDAP_PF_LOCAL 
411 /*
412  * in os-local.c
413  */
414 LIBLDAP_F (int) ldap_connect_to_path( LDAP *ld, Sockbuf *sb, const char *path, int async );
415 #endif /* LDAP_PF_LOCAL */
416
417 /*
418  * in request.c
419  */
420 LIBLDAP_F (ber_int_t) ldap_send_initial_request( LDAP *ld, ber_tag_t msgtype,
421         const char *dn, BerElement *ber );
422 LIBLDAP_F (BerElement *) ldap_alloc_ber_with_options( LDAP *ld );
423 LIBLDAP_F (void) ldap_set_ber_options( LDAP *ld, BerElement *ber );
424
425 LIBLDAP_F (int) ldap_send_server_request( LDAP *ld, BerElement *ber, ber_int_t msgid, LDAPRequest *parentreq, LDAPURLDesc *srvlist, LDAPConn *lc, LDAPreqinfo *bind );
426 LIBLDAP_F (LDAPConn *) ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb, int connect, LDAPreqinfo *bind );
427 LIBLDAP_F (LDAPRequest *) ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid );
428 LIBLDAP_F (void) ldap_free_request( LDAP *ld, LDAPRequest *lr );
429 LIBLDAP_F (void) ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind );
430 LIBLDAP_F (void) ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all );
431 LIBLDAP_F (void) ldap_dump_requests_and_responses( LDAP *ld );
432 LIBLDAP_F (int) ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp );
433 LIBLDAP_F (int) ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **referralsp, int *hadrefp );
434 LIBLDAP_F (int) ldap_append_referral( LDAP *ld, char **referralsp, char *s );
435
436 /*
437  * in result.c:
438  */
439 #ifdef LDAP_CONNECTIONLESS
440 LIBLDAP_F (int) cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber );
441 #endif
442
443 /*
444  * in search.c
445  */
446 LIBLDAP_F (BerElement *) ldap_build_search_req LDAP_P((
447         LDAP *ld,
448         const char *base,
449         ber_int_t scope,
450         const char *filter,
451         char **attrs,
452         ber_int_t attrsonly,
453         LDAPControl **sctrls,
454         LDAPControl **cctrls,
455         ber_int_t timelimit,
456         ber_int_t sizelimit ));
457
458 /*
459  * in string.c
460  */
461         /* see <ac/string.h> */
462
463 /*
464  * in unbind.c
465  */
466 LIBLDAP_F (int) ldap_ld_free LDAP_P((
467         LDAP *ld,
468         int close,
469         LDAPControl **sctrls,
470         LDAPControl **cctrls ));
471
472 LIBLDAP_F (int) ldap_send_unbind LDAP_P((
473         LDAP *ld,
474         Sockbuf *sb,
475         LDAPControl **sctrls,
476         LDAPControl **cctrls ));
477
478 /*
479  * in url.c
480  */
481 LIBLDAP_F (LDAPURLDesc *) ldap_url_dup LDAP_P((
482         LDAPURLDesc *ludp ));
483
484 LIBLDAP_F (LDAPURLDesc *) ldap_url_duplist LDAP_P((
485         LDAPURLDesc *ludlist ));
486
487 LIBLDAP_F (int) ldap_url_parselist LDAP_P((
488         LDAPURLDesc **ludlist,
489         const char *url ));
490
491 LIBLDAP_F (int) ldap_url_parsehosts LDAP_P((
492         LDAPURLDesc **ludlist,
493         const char *hosts ));
494
495 LIBLDAP_F (char *) ldap_url_list2hosts LDAP_P((
496         LDAPURLDesc *ludlist ));
497
498 LIBLDAP_F (char *) ldap_url_list2urls LDAP_P((
499         LDAPURLDesc *ludlist ));
500
501 LIBLDAP_F (void) ldap_free_urllist LDAP_P((
502         LDAPURLDesc *ludlist ));
503
504
505 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
506 /*
507  * in getdxbyname.c
508  */
509 LIBLDAP_F (char **) ldap_getdxbyname( const char *domain );
510 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
511
512 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
513 /*
514  * in charset.c
515  *
516  * added-in this stuff so that libldap.a would build, i.e. refs to 
517  * these routines from open.c would resolve. 
518  * hodges@stanford.edu 5-Feb-96
519  */
520 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
521 LIBLDAP_F (int) ldap_t61_to_8859( char **bufp, ber_len_t *buflenp, int free_input );
522 LIBLDAP_F (int) ldap_8859_to_t61( char **bufp, ber_len_t *buflenp, int free_input );
523 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
524 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
525
526 LDAP_END_DECL
527
528 #endif /* _LDAP_INT_H */