]> git.sur5r.net Git - openldap/blob - include/ldap_pvt.h
066fabab46bbb7d5b0c0ca6e5ec9571e5a991962
[openldap] / include / ldap_pvt.h
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  * 
4  * Copyright 1998-2013 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 /* use this macro to allocate buffer for ldap_pvt_csnstr */
131 #define LDAP_PVT_CSNSTR_BUFSIZE 64
132 LDAP_F( size_t )
133 ldap_pvt_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod );
134
135 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
136
137 struct hostent; /* avoid pulling in <netdb.h> */
138
139 LDAP_F( int )
140 ldap_pvt_gethostbyname_a LDAP_P((
141         const char *name,
142         struct hostent *resbuf,
143         char **buf,
144         struct hostent **result,
145         int *herrno_ptr ));
146
147 LDAP_F( int )
148 ldap_pvt_gethostbyaddr_a LDAP_P((
149         const char *addr,
150         int len,
151         int type,
152         struct hostent *resbuf,
153         char **buf,
154         struct hostent **result,
155         int *herrno_ptr ));
156
157 struct sockaddr;
158
159 LDAP_F( int )
160 ldap_pvt_get_hname LDAP_P((
161         const struct sockaddr * sa,
162         int salen,
163         char *name,
164         int namelen,
165         char **herr ));
166
167
168 /* charray.c */
169
170 LDAP_F( int )
171 ldap_charray_add LDAP_P((
172     char        ***a,
173     const char *s ));
174
175 LDAP_F( int )
176 ldap_charray_merge LDAP_P((
177     char        ***a,
178     char        **s ));
179
180 LDAP_F( void )
181 ldap_charray_free LDAP_P(( char **a ));
182
183 LDAP_F( int )
184 ldap_charray_inlist LDAP_P((
185     char        **a,
186     const char *s ));
187
188 LDAP_F( char ** )
189 ldap_charray_dup LDAP_P(( char **a ));
190
191 LDAP_F( char ** )
192 ldap_str2charray LDAP_P((
193         const char *str,
194         const char *brkstr ));
195
196 LDAP_F( char * )
197 ldap_charray2str LDAP_P((
198         char **array, const char* sep ));
199
200 /* getdn.c */
201
202 #ifdef LDAP_AVA_NULL    /* in ldap.h */
203 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx ));
204 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx ));
205
206 LDAP_F( int ) ldap_bv2dn_x LDAP_P(( 
207         struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx ));
208 LDAP_F( int ) ldap_dn2bv_x LDAP_P(( 
209         LDAPDN dn, struct berval *bv, unsigned flags, void *ctx ));
210 LDAP_F( int ) ldap_bv2rdn_x LDAP_P(( 
211         struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx ));
212 LDAP_F( int ) ldap_rdn2bv_x LDAP_P(( 
213         LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx ));
214 #endif /* LDAP_AVA_NULL */
215
216 /* url.c */
217 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s ));
218
219 /*
220  * these macros assume 'x' is an ASCII x
221  * and assume the "C" locale
222  */
223 #define LDAP_ASCII(c)           (!((c) & 0x80))
224 #define LDAP_SPACE(c)           ((c) == ' ' || (c) == '\t' || (c) == '\n')
225 #define LDAP_DIGIT(c)           ((c) >= '0' && (c) <= '9')
226 #define LDAP_LOWER(c)           ((c) >= 'a' && (c) <= 'z')
227 #define LDAP_UPPER(c)           ((c) >= 'A' && (c) <= 'Z')
228 #define LDAP_ALPHA(c)           (LDAP_LOWER(c) || LDAP_UPPER(c))
229 #define LDAP_ALNUM(c)           (LDAP_ALPHA(c) || LDAP_DIGIT(c))
230
231 #define LDAP_LDH(c)                     (LDAP_ALNUM(c) || (c) == '-')
232
233 #define LDAP_HEXLOWER(c)        ((c) >= 'a' && (c) <= 'f')
234 #define LDAP_HEXUPPER(c)        ((c) >= 'A' && (c) <= 'F')
235 #define LDAP_HEX(c)                     (LDAP_DIGIT(c) || \
236                                                                 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
237
238 /* controls.c */
239 struct ldapcontrol;
240 LDAP_F (int)
241 ldap_pvt_put_control LDAP_P((
242         const struct ldapcontrol *c,
243         BerElement *ber ));
244 LDAP_F (int) ldap_pvt_get_controls LDAP_P((
245         BerElement *be,
246         struct ldapcontrol ***ctrlsp));
247
248 #ifdef HAVE_CYRUS_SASL
249 /* cyrus.c */
250 struct sasl_security_properties; /* avoid pulling in <sasl.h> */
251 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P((
252         const char *in,
253         struct sasl_security_properties *secprops ));
254 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P((
255         struct sasl_security_properties *secprops,
256         struct berval *out ));
257
258 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void));
259 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex));
260 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex));
261 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex));
262 #endif /* HAVE_CYRUS_SASL */
263
264 struct sockbuf; /* avoid pulling in <lber.h> */
265 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * ));
266 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * ));
267
268 /*
269  * SASL encryption support for LBER Sockbufs
270  */
271
272 struct sb_sasl_generic_data;
273
274 struct sb_sasl_generic_ops {
275         void (*init)(struct sb_sasl_generic_data *p,
276                      ber_len_t *min_send,
277                      ber_len_t *max_send,
278                      ber_len_t *max_recv);
279         ber_int_t (*encode)(struct sb_sasl_generic_data *p,
280                             unsigned char *buf,
281                             ber_len_t len,
282                             Sockbuf_Buf *dst);
283         ber_int_t (*decode)(struct sb_sasl_generic_data *p,
284                             const Sockbuf_Buf *src,
285                             Sockbuf_Buf *dst);
286         void (*reset_buf)(struct sb_sasl_generic_data *p,
287                           Sockbuf_Buf *buf);
288         void (*fini)(struct sb_sasl_generic_data *p);
289 };
290
291 struct sb_sasl_generic_install {
292         const struct sb_sasl_generic_ops        *ops;
293         void                                    *ops_private;
294 };
295
296 struct sb_sasl_generic_data {
297         const struct sb_sasl_generic_ops        *ops;
298         void                                    *ops_private;
299         Sockbuf_IO_Desc                         *sbiod;
300         ber_len_t                               min_send;
301         ber_len_t                               max_send;
302         ber_len_t                               max_recv;
303         Sockbuf_Buf                             sec_buf_in;
304         Sockbuf_Buf                             buf_in;
305         Sockbuf_Buf                             buf_out;
306         unsigned int                            flags;
307 #define LDAP_PVT_SASL_PARTIAL_WRITE     1
308 };
309  
310 #ifndef LDAP_PVT_SASL_LOCAL_SSF
311 #define LDAP_PVT_SASL_LOCAL_SSF 71      /* SSF for Unix Domain Sockets */
312 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
313
314 struct ldap;
315 struct ldapmsg;
316 struct ldifrecord;
317
318 /* abandon */
319 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
320         struct ldap *ld, ber_int_t msgid ));
321
322 /* ldifutil.c */
323 LDAP_F( int )
324 ldap_parse_ldif_record_x LDAP_P((
325         struct berval *rbuf,
326         unsigned long linenum,
327         struct ldifrecord *lr,
328         const char *errstr,
329         unsigned int flags,
330         void *ctx ));
331
332 /* messages.c */
333 LDAP_F( BerElement * )
334 ldap_get_message_ber LDAP_P((
335         struct ldapmsg * ));
336
337 /* open */
338 LDAP_F (int) ldap_open_internal_connection LDAP_P((
339         struct ldap **ldp, ber_socket_t *fdp ));
340 LDAP_F (int) ldap_init_fd LDAP_P((
341         ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
342
343 /* sasl.c */
344 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb,
345         struct sb_sasl_generic_install *install_arg ));
346 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb ));
347
348 /* search.c */
349 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
350         BerElement *ber,
351         const char *str ));
352
353 LDAP_F( char * )
354 ldap_pvt_find_wildcard LDAP_P(( const char *s ));
355
356 LDAP_F( ber_slen_t )
357 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
358
359 LDAP_F( ber_len_t )
360 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
361
362 LDAP_F( int )
363 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
364         int inplace, void *ctx ));
365
366 LDAP_F (int) ldap_pvt_search LDAP_P((
367         struct ldap *ld,
368         LDAP_CONST char *base,
369         int scope,
370         LDAP_CONST char *filter,
371         char **attrs,
372         int attrsonly,
373         struct ldapcontrol **sctrls,
374         struct ldapcontrol **cctrls,
375         struct timeval *timeout,
376         int sizelimit,
377         int deref,
378         int *msgidp ));
379
380 LDAP_F(int) ldap_pvt_search_s LDAP_P((
381         struct ldap *ld,
382         LDAP_CONST char *base,
383         int scope,
384         LDAP_CONST char *filter,
385         char **attrs,
386         int attrsonly,
387         struct ldapcontrol **sctrls,
388         struct ldapcontrol **cctrls,
389         struct timeval *timeout,
390         int sizelimit,
391         int deref,
392         struct ldapmsg **res ));
393
394 /* string.c */
395 LDAP_F( char * )
396 ldap_pvt_str2upper LDAP_P(( char *str ));
397
398 LDAP_F( char * )
399 ldap_pvt_str2lower LDAP_P(( char *str ));
400
401 LDAP_F( struct berval * )
402 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
403
404 LDAP_F( struct berval * )
405 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
406
407 /* tls.c */
408 LDAP_F (int) ldap_pvt_tls_config LDAP_P(( struct ldap *ld,
409         int option, const char *arg ));
410 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
411         int option, void *arg ));
412 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
413         int option, void *arg ));
414
415 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
416 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
417 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
418 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
419 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
420 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
421 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
422
423 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
424
425 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
426         void *ctx, void *arg ));
427
428 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
429         LDAPDN_rewrite_dummy *func, unsigned flags ));
430 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
431         LDAPDN_rewrite_dummy *func, unsigned flags ));
432 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
433 LDAP_F (int) ldap_pvt_tls_get_unique LDAP_P(( void *ctx, struct berval *buf, int is_server ));
434 LDAP_F (const char *) ldap_pvt_tls_get_version LDAP_P(( void *ctx ));
435 LDAP_F (const char *) ldap_pvt_tls_get_cipher LDAP_P(( void *ctx ));
436
437 LDAP_END_DECL
438
439 /*
440  * Multiple precision stuff
441  * 
442  * May use OpenSSL's BIGNUM if built with TLS,
443  * or GNU's multiple precision library. But if
444  * long long is available, that's big enough
445  * and much more efficient.
446  *
447  * If none is available, unsigned long data is used.
448  */
449
450 LDAP_BEGIN_DECL
451
452 #ifdef USE_MP_BIGNUM
453 /*
454  * Use OpenSSL's BIGNUM
455  */
456 #include <openssl/crypto.h>
457 #include <openssl/bn.h>
458
459 typedef BIGNUM* ldap_pvt_mp_t;
460 #define LDAP_PVT_MP_INIT        (NULL)
461
462 #define ldap_pvt_mp_init(mp) \
463         do { (mp) = BN_new(); } while (0)
464
465 /* FIXME: we rely on mpr being initialized */
466 #define ldap_pvt_mp_init_set(mpr,mpv) \
467         do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
468
469 #define ldap_pvt_mp_add(mpr,mpv) \
470         BN_add((mpr), (mpr), (mpv))
471
472 #define ldap_pvt_mp_add_ulong(mp,v) \
473         BN_add_word((mp), (v))
474
475 #define ldap_pvt_mp_clear(mp) \
476         do { BN_free((mp)); (mp) = 0; } while (0)
477
478 #elif defined(USE_MP_GMP)
479 /*
480  * Use GNU's multiple precision library
481  */
482 #include <gmp.h>
483
484 typedef mpz_t           ldap_pvt_mp_t;
485 #define LDAP_PVT_MP_INIT        { 0 }
486
487 #define ldap_pvt_mp_init(mp) \
488         mpz_init((mp))
489
490 #define ldap_pvt_mp_init_set(mpr,mpv) \
491         mpz_init_set((mpr), (mpv))
492
493 #define ldap_pvt_mp_add(mpr,mpv) \
494         mpz_add((mpr), (mpr), (mpv))
495
496 #define ldap_pvt_mp_add_ulong(mp,v)     \
497         mpz_add_ui((mp), (mp), (v))
498
499 #define ldap_pvt_mp_clear(mp) \
500         mpz_clear((mp))
501
502 #else
503 /*
504  * Use unsigned long long
505  */
506
507 #ifdef USE_MP_LONG_LONG
508 typedef unsigned long long      ldap_pvt_mp_t;
509 #define LDAP_PVT_MP_INIT        (0LL)
510 #elif defined(USE_MP_LONG)
511 typedef unsigned long           ldap_pvt_mp_t;
512 #define LDAP_PVT_MP_INIT        (0L)
513 #elif defined(HAVE_LONG_LONG)
514 typedef unsigned long long      ldap_pvt_mp_t;
515 #define LDAP_PVT_MP_INIT        (0LL)
516 #else
517 typedef unsigned long           ldap_pvt_mp_t;
518 #define LDAP_PVT_MP_INIT        (0L)
519 #endif
520
521 #define ldap_pvt_mp_init(mp) \
522         do { (mp) = 0; } while (0)
523
524 #define ldap_pvt_mp_init_set(mpr,mpv) \
525         do { (mpr) = (mpv); } while (0)
526
527 #define ldap_pvt_mp_add(mpr,mpv) \
528         do { (mpr) += (mpv); } while (0)
529
530 #define ldap_pvt_mp_add_ulong(mp,v) \
531         do { (mp) += (v); } while (0)
532
533 #define ldap_pvt_mp_clear(mp) \
534         do { (mp) = 0; } while (0)
535
536 #endif /* MP */
537
538 #include "ldap_pvt_uc.h"
539
540 LDAP_END_DECL
541
542 LDAP_BEGIN_DECL
543
544 #include <limits.h>                             /* get CHAR_BIT */
545
546 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
547 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
548
549 LDAP_END_DECL
550
551 #endif /* _LDAP_PVT_H */