]> git.sur5r.net Git - openldap/blob - include/ldap_pvt.h
28bf2156ca0d29efe3073124ad1b22f0962a1598
[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
323 /* abandon */
324 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
325         struct ldap *ld, ber_int_t msgid ));
326
327 /* messages.c */
328 LDAP_F( BerElement * )
329 ldap_get_message_ber LDAP_P((
330         struct ldapmsg * ));
331
332 /* open */
333 LDAP_F (int) ldap_open_internal_connection LDAP_P((
334         struct ldap **ldp, ber_socket_t *fdp ));
335 LDAP_F (int) ldap_init_fd LDAP_P((
336         ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
337
338 /* sasl.c */
339 LDAP_F (int) ldap_pvt_sasl_generic_install LDAP_P(( Sockbuf *sb,
340         struct sb_sasl_generic_install *install_arg ));
341 LDAP_F (void) ldap_pvt_sasl_generic_remove LDAP_P(( Sockbuf *sb ));
342
343 /* search.c */
344 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
345         BerElement *ber,
346         const char *str ));
347
348 LDAP_F( char * )
349 ldap_pvt_find_wildcard LDAP_P(( const char *s ));
350
351 LDAP_F( ber_slen_t )
352 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
353
354 LDAP_F( ber_len_t )
355 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
356
357 LDAP_F( int )
358 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
359         int inplace, void *ctx ));
360
361 LDAP_F (int) ldap_pvt_search LDAP_P((
362         struct ldap *ld,
363         LDAP_CONST char *base,
364         int scope,
365         LDAP_CONST char *filter,
366         char **attrs,
367         int attrsonly,
368         struct ldapcontrol **sctrls,
369         struct ldapcontrol **cctrls,
370         struct timeval *timeout,
371         int sizelimit,
372         int deref,
373         int *msgidp ));
374
375 LDAP_F(int) ldap_pvt_search_s LDAP_P((
376         struct ldap *ld,
377         LDAP_CONST char *base,
378         int scope,
379         LDAP_CONST char *filter,
380         char **attrs,
381         int attrsonly,
382         struct ldapcontrol **sctrls,
383         struct ldapcontrol **cctrls,
384         struct timeval *timeout,
385         int sizelimit,
386         int deref,
387         struct ldapmsg **res ));
388
389 /* string.c */
390 LDAP_F( char * )
391 ldap_pvt_str2upper LDAP_P(( char *str ));
392
393 LDAP_F( char * )
394 ldap_pvt_str2lower LDAP_P(( char *str ));
395
396 LDAP_F( struct berval * )
397 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
398
399 LDAP_F( struct berval * )
400 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
401
402 /* tls.c */
403 LDAP_F (int) ldap_int_tls_config LDAP_P(( struct ldap *ld,
404         int option, const char *arg ));
405 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
406         int option, void *arg ));
407 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
408         int option, void *arg ));
409
410 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
411 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
412 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
413 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
414 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
415 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
416 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
417
418 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
419
420 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
421         void *ctx, void *arg ));
422
423 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
424         LDAPDN_rewrite_dummy *func, unsigned flags ));
425 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
426         LDAPDN_rewrite_dummy *func, unsigned flags ));
427 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
428
429 LDAP_END_DECL
430
431 /*
432  * Multiple precision stuff
433  * 
434  * May use OpenSSL's BIGNUM if built with TLS,
435  * or GNU's multiple precision library. But if
436  * long long is available, that's big enough
437  * and much more efficient.
438  *
439  * If none is available, unsigned long data is used.
440  */
441
442 LDAP_BEGIN_DECL
443
444 #ifdef USE_MP_BIGNUM
445 /*
446  * Use OpenSSL's BIGNUM
447  */
448 #include <openssl/crypto.h>
449 #include <openssl/bn.h>
450
451 typedef BIGNUM* ldap_pvt_mp_t;
452 #define LDAP_PVT_MP_INIT        (NULL)
453
454 #define ldap_pvt_mp_init(mp) \
455         do { (mp) = BN_new(); } while (0)
456
457 /* FIXME: we rely on mpr being initialized */
458 #define ldap_pvt_mp_init_set(mpr,mpv) \
459         do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
460
461 #define ldap_pvt_mp_add(mpr,mpv) \
462         BN_add((mpr), (mpr), (mpv))
463
464 #define ldap_pvt_mp_add_ulong(mp,v) \
465         BN_add_word((mp), (v))
466
467 #define ldap_pvt_mp_clear(mp) \
468         do { BN_free((mp)); (mp) = 0; } while (0)
469
470 #elif defined(USE_MP_GMP)
471 /*
472  * Use GNU's multiple precision library
473  */
474 #include <gmp.h>
475
476 typedef mpz_t           ldap_pvt_mp_t;
477 #define LDAP_PVT_MP_INIT        { 0 }
478
479 #define ldap_pvt_mp_init(mp) \
480         mpz_init((mp))
481
482 #define ldap_pvt_mp_init_set(mpr,mpv) \
483         mpz_init_set((mpr), (mpv))
484
485 #define ldap_pvt_mp_add(mpr,mpv) \
486         mpz_add((mpr), (mpr), (mpv))
487
488 #define ldap_pvt_mp_add_ulong(mp,v)     \
489         mpz_add_ui((mp), (mp), (v))
490
491 #define ldap_pvt_mp_clear(mp) \
492         mpz_clear((mp))
493
494 #else
495 /*
496  * Use unsigned long long
497  */
498
499 #ifdef USE_MP_LONG_LONG
500 typedef unsigned long long      ldap_pvt_mp_t;
501 #define LDAP_PVT_MP_INIT        (0LL)
502 #elif defined(USE_MP_LONG)
503 typedef unsigned long           ldap_pvt_mp_t;
504 #define LDAP_PVT_MP_INIT        (0L)
505 #elif defined(HAVE_LONG_LONG)
506 typedef unsigned long long      ldap_pvt_mp_t;
507 #define LDAP_PVT_MP_INIT        (0LL)
508 #else
509 typedef unsigned long           ldap_pvt_mp_t;
510 #define LDAP_PVT_MP_INIT        (0L)
511 #endif
512
513 #define ldap_pvt_mp_init(mp) \
514         do { (mp) = 0; } while (0)
515
516 #define ldap_pvt_mp_init_set(mpr,mpv) \
517         do { (mpr) = (mpv); } while (0)
518
519 #define ldap_pvt_mp_add(mpr,mpv) \
520         do { (mpr) += (mpv); } while (0)
521
522 #define ldap_pvt_mp_add_ulong(mp,v) \
523         do { (mp) += (v); } while (0)
524
525 #define ldap_pvt_mp_clear(mp) \
526         do { (mp) = 0; } while (0)
527
528 #endif /* MP */
529
530 #include "ldap_pvt_uc.h"
531
532 LDAP_END_DECL
533
534 LDAP_BEGIN_DECL
535
536 #include <limits.h>                             /* get CHAR_BIT */
537
538 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
539 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
540
541 LDAP_END_DECL
542
543 #endif /* _LDAP_PVT_H */