1 /* $NetBSD: lutil.h,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $ */ 2 3 /* OpenLDAP: pkg/ldap/include/lutil.h,v 1.63.2.8 2009/11/17 17:18:11 quanah Exp */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2009 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( void ) 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 /* utils.c */ 149 LDAP_LUTIL_F( char* ) 150 lutil_progname LDAP_P(( 151 const char* name, 152 int argc, 153 char *argv[] )); 154 155 typedef struct lutil_tm { 156 int tm_sec; /* seconds 0-60 (1 leap second) */ 157 int tm_min; /* minutes 0-59 */ 158 int tm_hour; /* hours 0-23 */ 159 int tm_mday; /* day 1-31 */ 160 int tm_mon; /* month 0-11 */ 161 int tm_year; /* year - 1900 */ 162 int tm_usec; /* microseconds */ 163 int tm_usub; /* submicro */ 164 } lutil_tm; 165 166 typedef struct lutil_timet { 167 unsigned int tt_sec; /* seconds since 1900 */ 168 int tt_gsec; /* seconds since 1900, high 7 bits */ 169 unsigned int tt_usec; /* microseconds */ 170 } lutil_timet; 171 172 /* Parse a timestamp string into a structure */ 173 LDAP_LUTIL_F( int ) 174 lutil_parsetime LDAP_P(( 175 char *atm, struct lutil_tm * )); 176 177 /* Convert structured time to time in seconds since 1900 */ 178 LDAP_LUTIL_F( int ) 179 lutil_tm2time LDAP_P(( 180 struct lutil_tm *, struct lutil_timet * )); 181 182 /* Get current time as a structured time */ 183 LDAP_LUTIL_F( void ) 184 lutil_gettime LDAP_P(( struct lutil_tm * )); 185 186 #ifdef _WIN32 187 LDAP_LUTIL_F( void ) 188 lutil_slashpath LDAP_P(( char* path )); 189 #define LUTIL_SLASHPATH(p) lutil_slashpath(p) 190 #else 191 #define LUTIL_SLASHPATH(p) 192 #endif 193 194 LDAP_LUTIL_F( char* ) 195 lutil_strcopy LDAP_P(( char *dst, const char *src )); 196 197 LDAP_LUTIL_F( char* ) 198 lutil_strncopy LDAP_P(( char *dst, const char *src, size_t n )); 199 200 LDAP_LUTIL_F( char* ) 201 lutil_memcopy LDAP_P(( char *dst, const char *src, size_t n )); 202 203 #define lutil_strbvcopy(a, bv) lutil_memcopy((a),(bv)->bv_val,(bv)->bv_len) 204 205 struct tm; 206 207 /* use this macro to statically allocate buffer for lutil_gentime */ 208 #define LDAP_LUTIL_GENTIME_BUFSIZE 22 209 #define lutil_gentime(s,m,t) lutil_localtime((s),(m),(t),0) 210 LDAP_LUTIL_F( size_t ) 211 lutil_localtime LDAP_P(( char *s, size_t smax, const struct tm *tm, 212 long delta )); 213 214 #ifndef HAVE_MKSTEMP 215 LDAP_LUTIL_F( int ) 216 mkstemp LDAP_P (( char * template )); 217 #endif 218 219 /* sockpair.c */ 220 LDAP_LUTIL_F( int ) 221 lutil_pair( ber_socket_t sd[2] ); 222 223 /* uuid.c */ 224 /* use this macro to allocate buffer for lutil_uuidstr */ 225 #define LDAP_LUTIL_UUIDSTR_BUFSIZE 40 226 LDAP_LUTIL_F( size_t ) 227 lutil_uuidstr( char *buf, size_t len ); 228 229 LDAP_LUTIL_F( int ) 230 lutil_uuidstr_from_normalized( 231 char *uuid, 232 size_t uuidlen, 233 char *buf, 234 size_t buflen ); 235 236 /* csn.c */ 237 /* use this macro to allocate buffer for lutil_csnstr */ 238 #define LDAP_LUTIL_CSNSTR_BUFSIZE 64 239 LDAP_LUTIL_F( size_t ) 240 lutil_csnstr( char *buf, size_t len, unsigned int replica, unsigned int mod ); 241 242 /* 243 * Sometimes not all declarations in a header file are needed. 244 * An indicator to this is whether or not the symbol's type has 245 * been defined. Thus, we don't need to include a symbol if 246 * its type has not been defined through another header file. 247 */ 248 249 #ifdef HAVE_NT_SERVICE_MANAGER 250 LDAP_LUTIL_V (int) is_NT_Service; 251 252 #ifdef _LDAP_PVT_THREAD_H 253 LDAP_LUTIL_V (ldap_pvt_thread_cond_t) started_event; 254 #endif /* _LDAP_PVT_THREAD_H */ 255 256 /* macros are different between Windows and Mingw */ 257 #if defined(_WINSVC_H) || defined(_WINSVC_) 258 LDAP_LUTIL_V (SERVICE_STATUS) lutil_ServiceStatus; 259 LDAP_LUTIL_V (SERVICE_STATUS_HANDLE) hlutil_ServiceStatus; 260 #endif /* _WINSVC_H */ 261 262 LDAP_LUTIL_F (void) 263 lutil_CommenceStartupProcessing( char *serverName, void (*stopper)(int)) ; 264 265 LDAP_LUTIL_F (void) 266 lutil_ReportShutdownComplete( void ); 267 268 LDAP_LUTIL_F (void *) 269 lutil_getRegParam( char *svc, char *value ); 270 271 LDAP_LUTIL_F (int) 272 lutil_srv_install( char* service, char * displayName, char* filename, 273 int auto_start ); 274 LDAP_LUTIL_F (int) 275 lutil_srv_remove ( char* service, char* filename ); 276 277 #endif /* HAVE_NT_SERVICE_MANAGER */ 278 279 #ifdef HAVE_NT_EVENT_LOG 280 LDAP_LUTIL_F (void) 281 lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls ); 282 283 LDAP_LUTIL_F (void) 284 lutil_LogStoppedEvent( char *svc ); 285 #endif 286 287 #ifdef HAVE_EBCDIC 288 /* Generally this has only been used to put '\n' to stdout. We need to 289 * make sure it is output in EBCDIC. 290 */ 291 #undef putchar 292 #undef putc 293 #define putchar(c) putc((c), stdout) 294 #define putc(c,fp) do { char x=(c); __atoe_l(&x,1); putc(x,fp); } while(0) 295 #endif 296 297 LDAP_LUTIL_F (int) 298 lutil_atoix( int *v, const char *s, int x ); 299 300 LDAP_LUTIL_F (int) 301 lutil_atoux( unsigned *v, const char *s, int x ); 302 303 LDAP_LUTIL_F (int) 304 lutil_atolx( long *v, const char *s, int x ); 305 306 LDAP_LUTIL_F (int) 307 lutil_atoulx( unsigned long *v, const char *s, int x ); 308 309 #define lutil_atoi(v, s) lutil_atoix((v), (s), 10) 310 #define lutil_atou(v, s) lutil_atoux((v), (s), 10) 311 #define lutil_atol(v, s) lutil_atolx((v), (s), 10) 312 #define lutil_atoul(v, s) lutil_atoulx((v), (s), 10) 313 314 LDAP_LUTIL_F (int) 315 lutil_str2bin( struct berval *in, struct berval *out, void *ctx ); 316 317 /* Parse and unparse time intervals */ 318 LDAP_LUTIL_F (int) 319 lutil_parse_time( const char *in, unsigned long *tp ); 320 321 LDAP_LUTIL_F (int) 322 lutil_unparse_time( char *buf, size_t buflen, unsigned long t ); 323 324 #ifdef timerdiv 325 #define lutil_timerdiv timerdiv 326 #else /* ! timerdiv */ 327 /* works inplace (x == t) */ 328 #define lutil_timerdiv(t,d,x) \ 329 do { \ 330 time_t s = (t)->tv_sec; \ 331 assert( d > 0 ); \ 332 (x)->tv_sec = s / d; \ 333 (x)->tv_usec = ( (t)->tv_usec + 1000000 * ( s % d ) ) / d; \ 334 } while ( 0 ) 335 #endif /* ! timerdiv */ 336 337 #ifdef timermul 338 #define lutil_timermul timermul 339 #else /* ! timermul */ 340 /* works inplace (x == t) */ 341 #define lutil_timermul(t,m,x) \ 342 do { \ 343 time_t u = (t)->tv_usec * m; \ 344 assert( m > 0 ); \ 345 (x)->tv_sec = (t)->tv_sec * m + u / 1000000; \ 346 (x)->tv_usec = u % 1000000; \ 347 } while ( 0 ); 348 #endif /* ! timermul */ 349 350 LDAP_END_DECL 351 352 #endif /* _LUTIL_H */ 353