2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* ldap-pvt.h - Header for ldap_pvt_ functions.
17 * These are meant to be internal to OpenLDAP Software.
23 #include <lber.h> /* get ber_slen_t */
24 #include <lber_pvt.h> /* get Sockbuf_Buf */
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 */
34 ldap_pvt_url_scheme2proto LDAP_P((
37 ldap_pvt_url_scheme2tls LDAP_P((
41 ldap_pvt_url_scheme_port LDAP_P((
44 struct ldap_url_desc; /* avoid pulling in <ldap.h> */
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)
56 ldap_url_parse_ext LDAP_P((
58 struct ldap_url_desc **ludpp,
61 LDAP_F (int) ldap_url_parselist LDAP_P(( /* deprecated, use ldap_url_parselist_ext() */
62 struct ldap_url_desc **ludlist,
65 LDAP_F (int) ldap_url_parselist_ext LDAP_P((
66 struct ldap_url_desc **ludlist,
71 LDAP_F (char *) ldap_url_list2urls LDAP_P((
72 struct ldap_url_desc *ludlist ));
74 LDAP_F (void) ldap_free_urllist LDAP_P((
75 struct ldap_url_desc *ludlist ));
77 LDAP_F (int) ldap_pvt_scope2bv LDAP_P ((
78 int scope, struct berval *bv ));
80 LDAP_F (LDAP_CONST char *) ldap_pvt_scope2str LDAP_P ((
83 LDAP_F (int) ldap_pvt_bv2scope LDAP_P ((
86 LDAP_F (int) ldap_pvt_str2scope LDAP_P ((
90 ldap_pvt_ctime LDAP_P((
94 LDAP_F( char *) ldap_pvt_get_fqdn LDAP_P(( char * ));
96 struct hostent; /* avoid pulling in <netdb.h> */
99 ldap_pvt_gethostbyname_a LDAP_P((
101 struct hostent *resbuf,
103 struct hostent **result,
107 ldap_pvt_gethostbyaddr_a LDAP_P((
111 struct hostent *resbuf,
113 struct hostent **result,
119 ldap_pvt_get_hname LDAP_P((
120 const struct sockaddr * sa,
130 ldap_charray_add LDAP_P((
135 ldap_charray_merge LDAP_P((
140 ldap_charray_free LDAP_P(( char **a ));
143 ldap_charray_inlist LDAP_P((
148 ldap_charray_dup LDAP_P(( char **a ));
151 ldap_str2charray LDAP_P((
153 const char *brkstr ));
156 ldap_charray2str LDAP_P((
157 char **array, const char* sep ));
161 #ifdef LDAP_AVA_NULL /* in ldap.h */
162 LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx ));
163 LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx ));
165 LDAP_F( int ) ldap_bv2dn_x LDAP_P((
166 struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx ));
167 LDAP_F( int ) ldap_dn2bv_x LDAP_P((
168 LDAPDN dn, struct berval *bv, unsigned flags, void *ctx ));
169 LDAP_F( int ) ldap_bv2rdn_x LDAP_P((
170 struct berval *, LDAPRDN *, char **, unsigned flags, void *ctx ));
171 LDAP_F( int ) ldap_rdn2bv_x LDAP_P((
172 LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx ));
173 #endif /* LDAP_AVA_NULL */
176 LDAP_F (void) ldap_pvt_hex_unescape LDAP_P(( char *s ));
179 * these macros assume 'x' is an ASCII x
180 * and assume the "C" locale
182 #define LDAP_ASCII(c) (!((c) & 0x80))
183 #define LDAP_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
184 #define LDAP_DIGIT(c) ((c) >= '0' && (c) <= '9')
185 #define LDAP_LOWER(c) ((c) >= 'a' && (c) <= 'z')
186 #define LDAP_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
187 #define LDAP_ALPHA(c) (LDAP_LOWER(c) || LDAP_UPPER(c))
188 #define LDAP_ALNUM(c) (LDAP_ALPHA(c) || LDAP_DIGIT(c))
190 #define LDAP_LDH(c) (LDAP_ALNUM(c) || (c) == '-')
192 #define LDAP_HEXLOWER(c) ((c) >= 'a' && (c) <= 'f')
193 #define LDAP_HEXUPPER(c) ((c) >= 'A' && (c) <= 'F')
194 #define LDAP_HEX(c) (LDAP_DIGIT(c) || \
195 LDAP_HEXLOWER(c) || LDAP_HEXUPPER(c))
200 ldap_pvt_put_control LDAP_P((
201 const struct ldapcontrol *c,
203 LDAP_F (int) ldap_pvt_get_controls LDAP_P((
205 struct ldapcontrol ***ctrlsp));
207 #ifdef HAVE_CYRUS_SASL
209 struct sasl_security_properties; /* avoid pulling in <sasl.h> */
210 LDAP_F (int) ldap_pvt_sasl_secprops LDAP_P((
212 struct sasl_security_properties *secprops ));
213 LDAP_F (void) ldap_pvt_sasl_secprops_unparse LDAP_P((
214 struct sasl_security_properties *secprops,
215 struct berval *out ));
217 LDAP_F (void *) ldap_pvt_sasl_mutex_new LDAP_P((void));
218 LDAP_F (int) ldap_pvt_sasl_mutex_lock LDAP_P((void *mutex));
219 LDAP_F (int) ldap_pvt_sasl_mutex_unlock LDAP_P((void *mutex));
220 LDAP_F (void) ldap_pvt_sasl_mutex_dispose LDAP_P((void *mutex));
221 #endif /* HAVE_CYRUS_SASL */
223 struct sockbuf; /* avoid pulling in <lber.h> */
224 LDAP_F (int) ldap_pvt_sasl_install LDAP_P(( struct sockbuf *, void * ));
225 LDAP_F (void) ldap_pvt_sasl_remove LDAP_P(( struct sockbuf * ));
228 * SASL encryption support for LBER Sockbufs
231 struct sb_sasl_generic_data;
233 struct sb_sasl_generic_ops {
234 void (*init)(struct sb_sasl_generic_data *p,
237 ber_len_t *max_recv);
238 ber_int_t (*encode)(struct sb_sasl_generic_data *p,
242 ber_int_t (*decode)(struct sb_sasl_generic_data *p,
243 const Sockbuf_Buf *src,
245 void (*reset_buf)(struct sb_sasl_generic_data *p,
247 void (*fini)(struct sb_sasl_generic_data *p);
250 struct sb_sasl_generic_install {
251 const struct sb_sasl_generic_ops *ops;
255 struct sb_sasl_generic_data {
256 const struct sb_sasl_generic_ops *ops;
258 Sockbuf_IO_Desc *sbiod;
262 Sockbuf_Buf sec_buf_in;
267 #ifndef LDAP_PVT_SASL_LOCAL_SSF
268 #define LDAP_PVT_SASL_LOCAL_SSF 71 /* SSF for Unix Domain Sockets */
269 #endif /* ! LDAP_PVT_SASL_LOCAL_SSF */
275 LDAP_F ( int ) ldap_pvt_discard LDAP_P((
276 struct ldap *ld, ber_int_t msgid ));
279 LDAP_F( BerElement * )
280 ldap_get_message_ber LDAP_P((
284 LDAP_F (int) ldap_open_internal_connection LDAP_P((
285 struct ldap **ldp, ber_socket_t *fdp ));
286 LDAP_F (int) ldap_init_fd LDAP_P((
287 ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap **ldp ));
290 LDAP_F( int ) ldap_pvt_put_filter LDAP_P((
295 ldap_pvt_find_wildcard LDAP_P(( const char *s ));
298 ldap_pvt_filter_value_unescape LDAP_P(( char *filter ));
301 ldap_bv2escaped_filter_value_len LDAP_P(( struct berval *in ));
304 ldap_bv2escaped_filter_value_x LDAP_P(( struct berval *in, struct berval *out,
305 int inplace, void *ctx ));
309 ldap_pvt_str2upper LDAP_P(( char *str ));
312 ldap_pvt_str2lower LDAP_P(( char *str ));
314 LDAP_F( struct berval * )
315 ldap_pvt_str2upperbv LDAP_P(( char *str, struct berval *bv ));
317 LDAP_F( struct berval * )
318 ldap_pvt_str2lowerbv LDAP_P(( char *str, struct berval *bv ));
321 LDAP_F (int) ldap_int_tls_config LDAP_P(( struct ldap *ld,
322 int option, const char *arg ));
323 LDAP_F (int) ldap_pvt_tls_get_option LDAP_P(( struct ldap *ld,
324 int option, void *arg ));
325 LDAP_F (int) ldap_pvt_tls_set_option LDAP_P(( struct ldap *ld,
326 int option, void *arg ));
328 LDAP_F (void) ldap_pvt_tls_destroy LDAP_P(( void ));
329 LDAP_F (int) ldap_pvt_tls_init LDAP_P(( void ));
330 LDAP_F (int) ldap_pvt_tls_init_def_ctx LDAP_P(( int is_server ));
331 LDAP_F (int) ldap_pvt_tls_accept LDAP_P(( Sockbuf *sb, void *ctx_arg ));
332 LDAP_F (int) ldap_pvt_tls_inplace LDAP_P(( Sockbuf *sb ));
333 LDAP_F (void *) ldap_pvt_tls_sb_ctx LDAP_P(( Sockbuf *sb ));
334 LDAP_F (void) ldap_pvt_tls_ctx_free LDAP_P(( void * ));
336 typedef int LDAPDN_rewrite_dummy LDAP_P (( void *dn, unsigned flags ));
338 typedef int (LDAP_TLS_CONNECT_CB) LDAP_P (( struct ldap *ld, void *ssl,
339 void *ctx, void *arg ));
341 LDAP_F (int) ldap_pvt_tls_get_my_dn LDAP_P(( void *ctx, struct berval *dn,
342 LDAPDN_rewrite_dummy *func, unsigned flags ));
343 LDAP_F (int) ldap_pvt_tls_get_peer_dn LDAP_P(( void *ctx, struct berval *dn,
344 LDAPDN_rewrite_dummy *func, unsigned flags ));
345 LDAP_F (int) ldap_pvt_tls_get_strength LDAP_P(( void *ctx ));
350 * Multiple precision stuff
352 * May use OpenSSL's BIGNUM if built with TLS,
353 * or GNU's multiple precision library. But if
354 * long long is available, that's big enough
355 * and much more efficient.
357 * If none is available, unsigned long data is used.
364 * Use OpenSSL's BIGNUM
366 #include <openssl/crypto.h>
367 #include <openssl/bn.h>
369 typedef BIGNUM* ldap_pvt_mp_t;
370 #define LDAP_PVT_MP_INIT (NULL)
372 #define ldap_pvt_mp_init(mp) \
373 do { (mp) = BN_new(); } while (0)
375 /* FIXME: we rely on mpr being initialized */
376 #define ldap_pvt_mp_init_set(mpr,mpv) \
377 do { ldap_pvt_mp_init((mpr)); BN_add((mpr), (mpr), (mpv)); } while (0)
379 #define ldap_pvt_mp_add(mpr,mpv) \
380 BN_add((mpr), (mpr), (mpv))
382 #define ldap_pvt_mp_add_ulong(mp,v) \
383 BN_add_word((mp), (v))
385 #define ldap_pvt_mp_clear(mp) \
386 do { BN_free((mp)); (mp) = 0; } while (0)
388 #elif defined(USE_MP_GMP)
390 * Use GNU's multiple precision library
394 typedef mpz_t ldap_pvt_mp_t;
395 #define LDAP_PVT_MP_INIT { 0 }
397 #define ldap_pvt_mp_init(mp) \
400 #define ldap_pvt_mp_init_set(mpr,mpv) \
401 mpz_init_set((mpr), (mpv))
403 #define ldap_pvt_mp_add(mpr,mpv) \
404 mpz_add((mpr), (mpr), (mpv))
406 #define ldap_pvt_mp_add_ulong(mp,v) \
407 mpz_add_ui((mp), (mp), (v))
409 #define ldap_pvt_mp_clear(mp) \
414 * Use unsigned long long
417 #ifdef USE_MP_LONG_LONG
418 typedef unsigned long long ldap_pvt_mp_t;
419 #define LDAP_PVT_MP_INIT (0LL)
420 #elif defined(USE_MP_LONG)
421 typedef unsigned long ldap_pvt_mp_t;
422 #define LDAP_PVT_MP_INIT (0L)
423 #elif defined(HAVE_LONG_LONG)
424 typedef unsigned long long ldap_pvt_mp_t;
425 #define LDAP_PVT_MP_INIT (0LL)
427 typedef unsigned long ldap_pvt_mp_t;
428 #define LDAP_PVT_MP_INIT (0L)
431 #define ldap_pvt_mp_init(mp) \
432 do { (mp) = 0; } while (0)
434 #define ldap_pvt_mp_init_set(mpr,mpv) \
435 do { (mpr) = (mpv); } while (0)
437 #define ldap_pvt_mp_add(mpr,mpv) \
438 do { (mpr) += (mpv); } while (0)
440 #define ldap_pvt_mp_add_ulong(mp,v) \
441 do { (mp) += (v); } while (0)
443 #define ldap_pvt_mp_clear(mp) \
444 do { (mp) = 0; } while (0)
448 #include "ldap_pvt_uc.h"
454 #include <limits.h> /* get CHAR_BIT */
456 /* Buffer space for sign, decimal digits and \0. Note: log10(2) < 146/485. */
457 #define LDAP_PVT_INTTYPE_CHARS(type) (((sizeof(type)*CHAR_BIT-1)*146)/485 + 3)
461 #endif /* _LDAP_PVT_H */