]> git.sur5r.net Git - openldap/blob - include/ldap_pvt.h
ITS#8054 operation duration logging
[openldap] / include / ldap_pvt.h
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  * 
4  * Copyright 1998-2015 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 /* ldap-pvt.h - Header for ldap_pvt_ functions.
17  * These are meant to be internal to OpenLDAP Software.
18  */
19
20 #ifndef _LDAP_PVT_H
21 #define _LDAP_PVT_H 1
22
23 #include <lber.h>                               /* get ber_slen_t */
24 #include <lber_pvt.h>                           /* get Sockbuf_Buf */
25
26 LDAP_BEGIN_DECL
27
28 #define LDAP_PROTO_TCP 1 /* ldap://  */
29 #define LDAP_PROTO_UDP 2 /* reserved */
30 #define LDAP_PROTO_IPC 3 /* ldapi:// */
31 #define LDAP_PROTO_EXT 4 /* user-defined socket/sockbuf */
32
33 LDAP_F ( int )
34 ldap_pvt_url_scheme2proto LDAP_P((
35         const char * ));
36 LDAP_F ( int )
37 ldap_pvt_url_scheme2tls LDAP_P((
38         const char * ));
39
40 LDAP_F ( int )
41 ldap_pvt_url_scheme_port LDAP_P((
42         const char *, int ));
43
44 struct ldap_url_desc; /* avoid pulling in <ldap.h> */
45
46 #define LDAP_PVT_URL_PARSE_NONE                 (0x00U)
47 #define LDAP_PVT_URL_PARSE_NOEMPTY_HOST         (0x01U)
48 #define LDAP_PVT_URL_PARSE_DEF_PORT             (0x02U)
49 #define LDAP_PVT_URL_PARSE_NOEMPTY_DN           (0x04U)
50 #define LDAP_PVT_URL_PARSE_NODEF_SCOPE          (0x08U)
51 #define LDAP_PVT_URL_PARSE_HISTORIC             (LDAP_PVT_URL_PARSE_NODEF_SCOPE | \
52                                                  LDAP_PVT_URL_PARSE_NOEMPTY_HOST | \
53                                                  LDAP_PVT_URL_PARSE_DEF_PORT)
54
55 LDAP_F( int )
56 ldap_url_parse_ext LDAP_P((
57         LDAP_CONST char *url,
58         struct ldap_url_desc **ludpp,
59         unsigned flags ));
60
61 LDAP_F (int) ldap_url_parselist LDAP_P((        /* deprecated, use ldap_url_parselist_ext() */
62         struct ldap_url_desc **ludlist,
63         const char *url ));
64
65 LDAP_F (int) ldap_url_parselist_ext LDAP_P((
66         struct ldap_url_desc **ludlist,
67         const char *url,
68         const char *sep,
69         unsigned flags ));
70
71 LDAP_F (char *) ldap_url_list2urls LDAP_P((
72         struct ldap_url_desc *ludlist ));
73
74 LDAP_F (void) ldap_free_urllist LDAP_P((
75         struct ldap_url_desc *ludlist ));
76
77 LDAP_F (int) ldap_pvt_scope2bv LDAP_P ((
78         int scope, struct berval *bv ));
79
80 LDAP_F (LDAP_CONST char *) ldap_pvt_scope2str LDAP_P ((
81         int scope ));
82
83 LDAP_F (int) ldap_pvt_bv2scope LDAP_P ((
84         struct berval *bv ));
85
86 LDAP_F (int) ldap_pvt_str2scope LDAP_P ((
87         LDAP_CONST char * ));
88
89 LDAP_F( char * )
90 ldap_pvt_ctime LDAP_P((
91         const time_t *tp,
92         char *buf ));
93
94 # if defined( HAVE_GMTIME_R )
95 #   define USE_GMTIME_R
96 #   define ldap_pvt_gmtime(timep, result) gmtime_r((timep), (result))
97 # else
98 LDAP_F( struct tm * )
99 ldap_pvt_gmtime LDAP_P((
100         LDAP_CONST time_t *timep,
101         struct tm *result ));
102 #endif
103
104 # if defined( HAVE_LOCALTIME_R )
105 #   define USE_LOCALTIME_R
106 #   define ldap_pvt_localtime(timep, result) localtime_r((timep), (result))
107 # else
108 LDAP_F( struct tm * )
109 ldap_pvt_localtime LDAP_P((
110         LDAP_CONST time_t *timep,
111         struct tm *result ));
112 # endif
113
114 #if defined( USE_GMTIME_R ) && defined( USE_LOCALTIME_R )
115 #   define ldap_pvt_gmtime_lock() (0)
116 #   define ldap_pvt_gmtime_unlock() (0)
117 #else
118 LDAP_F( int )
119 ldap_pvt_gmtime_lock LDAP_P(( void ));
120
121 LDAP_F( int )
122 ldap_pvt_gmtime_unlock LDAP_P(( void ));
123 #endif /* USE_GMTIME_R && USE_LOCALTIME_R */
124
125 /* Get current time as a structured time */
126 struct lutil_tm;
127 LDAP_F( void )
128 ldap_pvt_gettime LDAP_P(( struct lutil_tm * ));
129
130 #ifdef _WIN32
131 #define gettimeofday(tv,tz)     ldap_pvt_gettimeofday(tv,tz)
132 LDAP_F( int )
133 ldap_pvt_gettimeofday LDAP_P(( struct timeval *tv, void *unused ));
134 #endif
135
136 /* use this macro to allocate buffer for ldap_pvt_csnstr */
137 #define LDAP_PVT_CSNSTR_BUFSIZE 64
138 LDAP_F( size_t )
139 ldap_pvt_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod );
140
141 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
142
143 struct hostent; /* avoid pulling in <netdb.h> */
144
145 LDAP_F( int )
146 ldap_pvt_gethostbyname_a LDAP_P((
147         const char *name,
148         struct hostent *resbuf,
149         char **buf,
150         struct hostent **result,
151         int *herrno_ptr ));
152
153 LDAP_F( int )
154 ldap_pvt_gethostbyaddr_a LDAP_P((
155         const char *addr,
156         int len,
157         int type,
158         struct hostent *resbuf,
159         char **buf,
160         struct hostent **result,
161         int *herrno_ptr ));
162
163 struct sockaddr;
164
165 LDAP_F( int )
166 ldap_pvt_get_hname LDAP_P((
167         const struct sockaddr * sa,
168         int salen,
169         char *name,
170         int namelen,
171         char **herr ));
172
173
174 /* charray.c */
175
176 LDAP_F( int )
177 ldap_charray_add LDAP_P((
178     char        ***a,
179     const char *s ));
180
181 LDAP_F( int )
182 ldap_charray_merge LDAP_P((
183     char        ***a,
184     char        **s ));
185
186 LDAP_F( void )
187 ldap_charray_free LDAP_P(( char **a ));
188
189 LDAP_F( int )
190 ldap_charray_inlist LDAP_P((
191     char        **a,
192     const char *s ));
193
194 LDAP_F( char ** )
195 ldap_charray_dup LDAP_P(( char **a ));
196
197 LDAP_F( char ** )
198 ldap_str2charray LDAP_P((
199         const char *str,
200         const char *brkstr ));
201
202 LDAP_F( char * )
203 ldap_charray2str LDAP_P((
204         char **array, const char* sep ));
205
206 /* getdn.c */
207
208 #ifdef LDAP_AVA_NULL    /* in ldap.h */
209 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx ));
210 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx ));
211
212 LDAP_F( int ) ldap_bv2dn_x LDAP_P(( 
213         struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx ));
214 LDAP_F( int ) ldap_dn2bv_x LDAP_P(( 
215         LDAPDN dn, struct berval *bv, unsigned flags, void *ctx ));
216 LDAP_F( int ) ldap_bv2rdn_x LDAP_P(( 
217         struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx ));
218 LDAP_F( int ) ldap_rdn2bv_x LDAP_P(( 
219         LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx ));
220 #endif /* LDAP_AVA_NULL */
221
222 /* url.c */
223 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s ));
224
225 /*
226  * these macros assume 'x' is an ASCII x
227  * and assume the "C" locale
228  */
229 #define LDAP_ASCII(c)           (!((c) & 0x80))
230 #define LDAP_SPACE(c)           ((c) == ' ' || (c) == '\t' || (c) == '\n')
231 #define LDAP_DIGIT(c)           ((c) >= '0' && (c) <= '9')
232 #define LDAP_LOWER(c)           ((c) >= 'a' && (c) <= 'z')
233 #define LDAP_UPPER(c)           ((c) >= 'A' && (c) <= 'Z')
234 #define LDAP_ALPHA(c)           (LDAP_LOWER(c) || LDAP_UPPER(c))
235 #define LDAP_ALNUM(c)           (LDAP_ALPHA(c) || LDAP_DIGIT(c))
236
237 #define LDAP_LDH(c)                     (LDAP_ALNUM(c) || (c) == '-')
238
239 #define LDAP_HEXLOWER(c)        ((c) >= 'a' && (c) <= 'f')
240 #define LDAP_HEXUPPER(c)        ((c) >= 'A' && (c) <= 'F')
241 #define LDAP_HEX(c)                     (LDAP_DIGIT(c) || \
242                                                                 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
243
244 /* controls.c */
245 struct ldapcontrol;
246 LDAP_F (int)
247 ldap_pvt_put_control LDAP_P((
248         const struct ldapcontrol *c,
249         BerElement *ber ));
250 LDAP_F (int) ldap_pvt_get_controls LDAP_P((
251         BerElement *be,
252         struct ldapcontrol ***ctrlsp));
253
254 #ifdef HAVE_CYRUS_SASL
255 /* cyrus.c */
256 struct sasl_security_properties; /* avoid pulling in <sasl.h> */
257 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P((
258         const char *in,
259         struct sasl_security_properties *secprops ));
260 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P((
261         struct sasl_security_properties *secprops,
262         struct berval *out ));
263
264 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void));
265 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex));
266 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex));
267 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex));
268 #endif /* HAVE_CYRUS_SASL */
269
270 struct sockbuf; /* avoid pulling in <lber.h> */
271 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * ));
272 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * ));
273
274 /*
275  * SASL encryption support for LBER Sockbufs
276  */
277
278 struct sb_sasl_generic_data;
279
280 struct sb_sasl_generic_ops {
281         void (*init)(struct sb_sasl_generic_data *p,
282                      ber_len_t *min_send,
283                      ber_len_t *max_send,
284                      ber_len_t *max_recv);
285         ber_int_t (*encode)(struct sb_sasl_generic_data *p,
286                             unsigned char *buf,
287                             ber_len_t len,
288                             Sockbuf_Buf *dst);
289         ber_int_t (*decode)(struct sb_sasl_generic_data *p,
290                             const Sockbuf_Buf *src,
291                             Sockbuf_Buf *dst);
292         void (*reset_buf)(struct sb_sasl_generic_data *p,
293                           Sockbuf_Buf *buf);
294         void (*fini)(struct sb_sasl_generic_data *p);
295 };
296
297 struct sb_sasl_generic_install {
298         const struct sb_sasl_generic_ops        *ops;
299         void                                    *ops_private;
300 };
301
302 struct sb_sasl_generic_data {
303         const struct sb_sasl_generic_ops        *ops;
304         void                                    *ops_private;
305         Sockbuf_IO_Desc                         *sbiod;
306         ber_len_t                               min_send;
307         ber_len_t                               max_send;
308         ber_len_t                               max_recv;
309         Sockbuf_Buf                             sec_buf_in;
310         Sockbuf_Buf                             buf_in;
311         Sockbuf_Buf                             buf_out;
312         unsigned int                            flags;
313 #define LDAP_PVT_SASL_PARTIAL_WRITE     1
314 };
315  
316 #ifndef LDAP_PVT_SASL_LOCAL_SSF
317 #define LDAP_PVT_SASL_LOCAL_SSF 71      /* SSF for Unix Domain Sockets */
318 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
319
320 struct ldap;
321 struct ldapmsg;
322 struct ldifrecord;
323
324 /* abandon */
325 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
326         struct ldap *ld, ber_int_t msgid ));
327
328 /* ldifutil.c */
329 LDAP_F( int )
330 ldap_parse_ldif_record_x LDAP_P((
331         struct berval *rbuf,
332         unsigned long linenum,
333         struct ldifrecord *lr,
334         const char *errstr,
335         unsigned int flags,
336         void *ctx ));
337
338 /* messages.c */
339 LDAP_F( BerElement * )
340 ldap_get_message_ber LDAP_P((
341         struct ldapmsg * ));
342
343 /* open */
344 LDAP_F (int) ldap_open_internal_connection LDAP_P((
345         struct ldap **ldp, ber_socket_t *fdp ));
346 LDAP_F (int) ldap_init_fd LDAP_P((
347         ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
348
349 /* sasl.c */
350 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb,
351         struct sb_sasl_generic_install *install_arg ));
352 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb ));
353
354 /* search.c */
355 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
356         BerElement *ber,
357         const char *str ));
358
359 LDAP_F( char * )
360 ldap_pvt_find_wildcard LDAP_P(( const char *s ));
361
362 LDAP_F( ber_slen_t )
363 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
364
365 LDAP_F( ber_len_t )
366 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
367
368 LDAP_F( int )
369 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
370         int inplace, void *ctx ));
371
372 LDAP_F (int) ldap_pvt_search LDAP_P((
373         struct ldap *ld,
374         LDAP_CONST char *base,
375         int scope,
376         LDAP_CONST char *filter,
377         char **attrs,
378         int attrsonly,
379         struct ldapcontrol **sctrls,
380         struct ldapcontrol **cctrls,
381         struct timeval *timeout,
382         int sizelimit,
383         int deref,
384         int *msgidp ));
385
386 LDAP_F(int) ldap_pvt_search_s LDAP_P((
387         struct ldap *ld,
388         LDAP_CONST char *base,
389         int scope,
390         LDAP_CONST char *filter,
391         char **attrs,
392         int attrsonly,
393         struct ldapcontrol **sctrls,
394         struct ldapcontrol **cctrls,
395         struct timeval *timeout,
396         int sizelimit,
397         int deref,
398         struct ldapmsg **res ));
399
400 /* string.c */
401 LDAP_F( char * )
402 ldap_pvt_str2upper LDAP_P(( char *str ));
403
404 LDAP_F( char * )
405 ldap_pvt_str2lower LDAP_P(( char *str ));
406
407 LDAP_F( struct berval * )
408 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
409
410 LDAP_F( struct berval * )
411 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
412
413 /* tls.c */
414 LDAP_F (int) ldap_pvt_tls_config LDAP_P(( struct ldap *ld,
415         int option, const char *arg ));
416 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
417         int option, void *arg ));
418 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
419         int option, void *arg ));
420
421 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
422 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
423 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
424 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
425 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
426 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
427 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
428
429 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
430
431 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
432         void *ctx, void *arg ));
433
434 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
435         LDAPDN_rewrite_dummy *func, unsigned flags ));
436 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
437         LDAPDN_rewrite_dummy *func, unsigned flags ));
438 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
439 LDAP_F (int) ldap_pvt_tls_get_unique LDAP_P(( void *ctx, struct berval *buf, int is_server ));
440 LDAP_F (const char *) ldap_pvt_tls_get_version LDAP_P(( void *ctx ));
441 LDAP_F (const char *) ldap_pvt_tls_get_cipher LDAP_P(( void *ctx ));
442
443 LDAP_END_DECL
444
445 /*
446  * Multiple precision stuff
447  * 
448  * May use OpenSSL's BIGNUM if built with TLS,
449  * or GNU's multiple precision library. But if
450  * long long is available, that's big enough
451  * and much more efficient.
452  *
453  * If none is available, unsigned long data is used.
454  */
455
456 LDAP_BEGIN_DECL
457
458 #ifdef USE_MP_BIGNUM
459 /*
460  * Use OpenSSL's BIGNUM
461  */
462 #include <openssl/crypto.h>
463 #include <openssl/bn.h>
464
465 typedef BIGNUM* ldap_pvt_mp_t;
466 #define LDAP_PVT_MP_INIT        (NULL)
467
468 #define ldap_pvt_mp_init(mp) \
469         do { (mp) = BN_new(); } while (0)
470
471 /* FIXME: we rely on mpr being initialized */
472 #define ldap_pvt_mp_init_set(mpr,mpv) \
473         do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
474
475 #define ldap_pvt_mp_add(mpr,mpv) \
476         BN_add((mpr), (mpr), (mpv))
477
478 #define ldap_pvt_mp_add_ulong(mp,v) \
479         BN_add_word((mp), (v))
480
481 #define ldap_pvt_mp_clear(mp) \
482         do { BN_free((mp)); (mp) = 0; } while (0)
483
484 #elif defined(USE_MP_GMP)
485 /*
486  * Use GNU's multiple precision library
487  */
488 #include <gmp.h>
489
490 typedef mpz_t           ldap_pvt_mp_t;
491 #define LDAP_PVT_MP_INIT        { 0 }
492
493 #define ldap_pvt_mp_init(mp) \
494         mpz_init((mp))
495
496 #define ldap_pvt_mp_init_set(mpr,mpv) \
497         mpz_init_set((mpr), (mpv))
498
499 #define ldap_pvt_mp_add(mpr,mpv) \
500         mpz_add((mpr), (mpr), (mpv))
501
502 #define ldap_pvt_mp_add_ulong(mp,v)     \
503         mpz_add_ui((mp), (mp), (v))
504
505 #define ldap_pvt_mp_clear(mp) \
506         mpz_clear((mp))
507
508 #else
509 /*
510  * Use unsigned long long
511  */
512
513 #ifdef USE_MP_LONG_LONG
514 typedef unsigned long long      ldap_pvt_mp_t;
515 #define LDAP_PVT_MP_INIT        (0LL)
516 #elif defined(USE_MP_LONG)
517 typedef unsigned long           ldap_pvt_mp_t;
518 #define LDAP_PVT_MP_INIT        (0L)
519 #elif defined(HAVE_LONG_LONG)
520 typedef unsigned long long      ldap_pvt_mp_t;
521 #define LDAP_PVT_MP_INIT        (0LL)
522 #else
523 typedef unsigned long           ldap_pvt_mp_t;
524 #define LDAP_PVT_MP_INIT        (0L)
525 #endif
526
527 #define ldap_pvt_mp_init(mp) \
528         do { (mp) = 0; } while (0)
529
530 #define ldap_pvt_mp_init_set(mpr,mpv) \
531         do { (mpr) = (mpv); } while (0)
532
533 #define ldap_pvt_mp_add(mpr,mpv) \
534         do { (mpr) += (mpv); } while (0)
535
536 #define ldap_pvt_mp_add_ulong(mp,v) \
537         do { (mp) += (v); } while (0)
538
539 #define ldap_pvt_mp_clear(mp) \
540         do { (mp) = 0; } while (0)
541
542 #endif /* MP */
543
544 #include "ldap_pvt_uc.h"
545
546 LDAP_END_DECL
547
548 LDAP_BEGIN_DECL
549
550 #include <limits.h>                             /* get CHAR_BIT */
551
552 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
553 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
554
555 LDAP_END_DECL
556
557 #endif /* _LDAP_PVT_H */