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