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