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