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