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