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