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