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