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