1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2021 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 #ifndef _LUTIL_H
17 #define _LUTIL_H 1
18 
19 #include <ldap_cdefs.h>
20 #include <lber_types.h>
21 
22 /*
23  * Include file for LDAP utility routine
24  */
25 
26 LDAP_BEGIN_DECL
27 
28 /* n octets encode into ceiling(n/3) * 4 bytes */
29 /* Avoid floating point math through extra padding */
30 
31 #define LUTIL_BASE64_ENCODE_LEN(n)	(((n)+2)/3 * 4)
32 #define LUTIL_BASE64_DECODE_LEN(n)	((n)/4*3)
33 
34 /* ISC Base64 Routines */
35 /* base64.c */
36 
37 LDAP_LUTIL_F( int )
38 lutil_b64_ntop LDAP_P((
39 	unsigned char const *,
40 	size_t,
41 	char *,
42 	size_t));
43 
44 LDAP_LUTIL_F( int )
45 lutil_b64_pton LDAP_P((
46 	char const *,
47 	unsigned char *,
48 	size_t));
49 
50 /* detach.c */
51 LDAP_LUTIL_F( int )
52 lutil_detach LDAP_P((
53 	int debug,
54 	int do_close));
55 
56 /* entropy.c */
57 LDAP_LUTIL_F( int )
58 lutil_entropy LDAP_P((
59 	unsigned char *buf,
60 	ber_len_t nbytes ));
61 
62 /* passfile.c */
63 struct berval;	/* avoid pulling in lber.h */
64 
65 LDAP_LUTIL_F( int )
66 lutil_get_filed_password LDAP_P((
67 	const char *filename,
68 	struct berval * ));
69 
70 /* passwd.c */
71 struct lutil_pw_scheme;
72 
73 #define LUTIL_PASSWD_OK		(0)
74 #define LUTIL_PASSWD_ERR	(-1)
75 
76 typedef int (LUTIL_PASSWD_CHK_FUNC)(
77 	const struct berval *scheme,
78 	const struct berval *passwd,
79 	const struct berval *cred,
80 	const char **text );
81 
82 typedef int (LUTIL_PASSWD_HASH_FUNC) (
83 	const struct berval *scheme,
84 	const struct berval *passwd,
85 	struct berval *hash,
86 	const char **text );
87 
88 LDAP_LUTIL_F( int )
89 lutil_passwd_add LDAP_P((
90 	struct berval *scheme,
91 	LUTIL_PASSWD_CHK_FUNC *chk_fn,
92 	LUTIL_PASSWD_HASH_FUNC *hash_fn ));
93 
94 LDAP_LUTIL_F( void )
95 lutil_passwd_init LDAP_P(( void ));
96 
97 LDAP_LUTIL_F( void )
98 lutil_passwd_destroy LDAP_P(( void ));
99 
100 LDAP_LUTIL_F( int )
101 lutil_authpasswd LDAP_P((
102 	const struct berval *passwd,	/* stored password */
103 	const struct berval *cred,	/* user supplied value */
104 	const char **methods ));
105 
106 LDAP_LUTIL_F( int )
107 lutil_authpasswd_hash LDAP_P((
108 	const struct berval *cred,
109 	struct berval **passwd,	/* password to store */
110 	struct berval **salt,	/* salt to store */
111 	const char *method ));
112 
113 #ifdef SLAPD_CRYPT
114 typedef int (lutil_cryptfunc) LDAP_P((
115 	const char *key,
116 	const char *salt,
117 	char **hash ));
118 LDAP_LUTIL_V (lutil_cryptfunc *) lutil_cryptptr;
119 #endif
120 
121 LDAP_LUTIL_F( int )
122 lutil_passwd LDAP_P((
123 	const struct berval *passwd,	/* stored password */
124 	const struct berval *cred,	/* user supplied value */
125 	const char **methods,
126 	const char **text ));			/* error message */
127 
128 LDAP_LUTIL_F( int )
129 lutil_passwd_generate LDAP_P(( struct berval *pw, ber_len_t ));
130 
131 LDAP_LUTIL_F( int )
132 lutil_passwd_hash LDAP_P((
133 	const struct berval *passwd,
134 	const char *method,
135 	struct berval *hash,
136 	const char **text ));
137 
138 LDAP_LUTIL_F( int )
139 lutil_passwd_scheme LDAP_P((
140 	const char *scheme ));
141 
142 LDAP_LUTIL_F( int )
143 lutil_salt_format LDAP_P((
144 	const char *format ));
145 
146 LDAP_LUTIL_F( int )
147 lutil_passwd_string64 LDAP_P((
148 	const struct berval *sc,
149 	const struct berval *hash,
150 	struct berval *b64,
151 	const struct berval *salt ));
152 
153 /* utils.c */
154 LDAP_LUTIL_F( char* )
155 lutil_progname LDAP_P((
156 	const char* name,
157 	int argc,
158 	char *argv[] ));
159 
160 typedef struct lutil_tm {
161 	int tm_sec;	/* seconds 0-60 (1 leap second) */
162 	int tm_min;	/* minutes 0-59 */
163 	int tm_hour;	/* hours 0-23 */
164 	int tm_mday;	/* day 1-31 */
165 	int tm_mon;	/* month 0-11 */
166 	int tm_year;	/* year - 1900 */
167 	int tm_usec;	/* microseconds */
168 	int tm_usub;	/* submicro */
169 } lutil_tm;
170 
171 typedef struct lutil_timet {
172 	unsigned int tt_sec;	/* seconds since 1900 */
173 	int tt_gsec;		/* seconds since 1900, high 7 bits */
174 	unsigned int tt_usec;	/* microseconds */
175 } lutil_timet;
176 
177 /* Parse a timestamp string into a structure */
178 LDAP_LUTIL_F( int )
179 lutil_parsetime LDAP_P((
180 	char *atm, struct lutil_tm * ));
181 
182 /* Convert structured time to time in seconds since 1900 */
183 LDAP_LUTIL_F( int )
184 lutil_tm2time LDAP_P((
185 	struct lutil_tm *, struct lutil_timet * ));
186 
187 #ifdef _WIN32
188 LDAP_LUTIL_F( void )
189 lutil_slashpath LDAP_P(( char* path ));
190 #define	LUTIL_SLASHPATH(p)	lutil_slashpath(p)
191 #else
192 #define	LUTIL_SLASHPATH(p)
193 #endif
194 
195 LDAP_LUTIL_F( char* )
196 lutil_strcopy LDAP_P(( char *dst, const char *src ));
197 
198 LDAP_LUTIL_F( char* )
199 lutil_strncopy LDAP_P(( char *dst, const char *src, size_t n ));
200 
201 LDAP_LUTIL_F( char* )
202 lutil_memcopy LDAP_P(( char *dst, const char *src, size_t n ));
203 
204 #define lutil_strbvcopy(a, bv) lutil_memcopy((a),(bv)->bv_val,(bv)->bv_len)
205 
206 struct tm;
207 
208 /* use this macro to statically allocate buffer for lutil_gentime */
209 #define LDAP_LUTIL_GENTIME_BUFSIZE	22
210 #define lutil_gentime(s,m,t)	lutil_localtime((s),(m),(t),0)
211 LDAP_LUTIL_F( size_t )
212 lutil_localtime LDAP_P(( char *s, size_t smax, const struct tm *tm,
213 			long delta ));
214 
215 #ifndef HAVE_MKSTEMP
216 LDAP_LUTIL_F( int )
217 mkstemp LDAP_P (( char * template ));
218 #endif
219 
220 /* sockpair.c */
221 LDAP_LUTIL_F( int )
222 lutil_pair( ber_socket_t sd[2] );
223 
224 /* uuid.c */
225 /* use this macro to allocate buffer for lutil_uuidstr */
226 #define LDAP_LUTIL_UUIDSTR_BUFSIZE	40
227 LDAP_LUTIL_F( size_t )
228 lutil_uuidstr( char *buf, size_t len );
229 
230 LDAP_LUTIL_F( int )
231 lutil_uuidstr_from_normalized(
232 	char		*uuid,
233 	size_t		uuidlen,
234 	char		*buf,
235 	size_t		buflen );
236 
237 /*
238  * Sometimes not all declarations in a header file are needed.
239  * An indicator to this is whether or not the symbol's type has
240  * been defined. Thus, we don't need to include a symbol if
241  * its type has not been defined through another header file.
242  */
243 
244 #ifdef HAVE_NT_SERVICE_MANAGER
245 LDAP_LUTIL_V (int) is_NT_Service;
246 
247 #ifdef _LDAP_PVT_THREAD_H
248 LDAP_LUTIL_V (ldap_pvt_thread_cond_t) started_event;
249 #endif /* _LDAP_PVT_THREAD_H */
250 
251 /* macros are different between Windows and Mingw */
252 #if defined(_WINSVC_H) || defined(_WINSVC_)
253 LDAP_LUTIL_V (SERVICE_STATUS) lutil_ServiceStatus;
254 LDAP_LUTIL_V (SERVICE_STATUS_HANDLE) hlutil_ServiceStatus;
255 #endif /* _WINSVC_H */
256 
257 LDAP_LUTIL_F (void)
258 lutil_CommenceStartupProcessing( char *serverName, void (*stopper)(int)) ;
259 
260 LDAP_LUTIL_F (void)
261 lutil_ReportShutdownComplete( void );
262 
263 LDAP_LUTIL_F (void *)
264 lutil_getRegParam( char *svc, char *value );
265 
266 LDAP_LUTIL_F (int)
267 lutil_srv_install( char* service, char * displayName, char* filename,
268 		 int auto_start );
269 LDAP_LUTIL_F (int)
270 lutil_srv_remove ( char* service, char* filename );
271 
272 #endif /* HAVE_NT_SERVICE_MANAGER */
273 
274 #ifdef HAVE_NT_EVENT_LOG
275 LDAP_LUTIL_F (void)
276 lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
277 
278 LDAP_LUTIL_F (void)
279 lutil_LogStoppedEvent( char *svc );
280 #endif
281 
282 #ifdef HAVE_EBCDIC
283 /* Generally this has only been used to put '\n' to stdout. We need to
284  * make sure it is output in EBCDIC.
285  */
286 #undef putchar
287 #undef putc
288 #define putchar(c)     putc((c), stdout)
289 #define putc(c,fp)     do { char x=(c); __atoe_l(&x,1); putc(x,fp); } while(0)
290 #endif
291 
292 LDAP_LUTIL_F (int)
293 lutil_atoix( int *v, const char *s, int x );
294 
295 LDAP_LUTIL_F (int)
296 lutil_atoux( unsigned *v, const char *s, int x );
297 
298 LDAP_LUTIL_F (int)
299 lutil_atolx( long *v, const char *s, int x );
300 
301 LDAP_LUTIL_F (int)
302 lutil_atoulx( unsigned long *v, const char *s, int x );
303 
304 #define lutil_atoi(v, s)	lutil_atoix((v), (s), 10)
305 #define lutil_atou(v, s)	lutil_atoux((v), (s), 10)
306 #define lutil_atol(v, s)	lutil_atolx((v), (s), 10)
307 #define lutil_atoul(v, s)	lutil_atoulx((v), (s), 10)
308 
309 #ifdef HAVE_LONG_LONG
310 #if defined(HAVE_STRTOLL) || defined(HAVE_STRTOQ)
311 LDAP_LUTIL_F (int)
312 lutil_atollx( long long *v, const char *s, int x );
313 #define lutil_atoll(v, s)	lutil_atollx((v), (s), 10)
314 #endif /* HAVE_STRTOLL || HAVE_STRTOQ */
315 
316 #if defined(HAVE_STRTOULL) || defined(HAVE_STRTOUQ)
317 LDAP_LUTIL_F (int)
318 lutil_atoullx( unsigned long long *v, const char *s, int x );
319 #define lutil_atoull(v, s)	lutil_atoullx((v), (s), 10)
320 #endif /* HAVE_STRTOULL || HAVE_STRTOUQ */
321 #endif /* HAVE_LONG_LONG */
322 
323 LDAP_LUTIL_F (int)
324 lutil_str2bin( struct berval *in, struct berval *out, void *ctx );
325 
326 /* Parse and unparse time intervals */
327 LDAP_LUTIL_F (int)
328 lutil_parse_time( const char *in, unsigned long *tp );
329 
330 LDAP_LUTIL_F (int)
331 lutil_unparse_time( char *buf, size_t buflen, unsigned long t );
332 
333 #ifdef timerdiv
334 #define lutil_timerdiv timerdiv
335 #else /* ! timerdiv */
336 /* works inplace (x == t) */
337 #define lutil_timerdiv(t,d,x) \
338 	do { \
339 		time_t s = (t)->tv_sec; \
340 		assert( d > 0 ); \
341 		(x)->tv_sec = s / d; \
342 		(x)->tv_usec = ( (t)->tv_usec + 1000000 * ( s % d ) ) / d; \
343 	} while ( 0 )
344 #endif /* ! timerdiv */
345 
346 #ifdef timermul
347 #define lutil_timermul timermul
348 #else /* ! timermul */
349 /* works inplace (x == t) */
350 #define lutil_timermul(t,m,x) \
351 	do { \
352 		time_t u = (t)->tv_usec * m; \
353 		assert( m > 0 ); \
354 		(x)->tv_sec = (t)->tv_sec * m + u / 1000000; \
355 		(x)->tv_usec = u % 1000000; \
356 	} while ( 0 );
357 #endif /* ! timermul */
358 
359 LDAP_END_DECL
360 
361 #endif /* _LUTIL_H */
362