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