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