1 /* $NetBSD: lutil.h,v 1.1.1.3 2010/12/12 15:21:24 adam Exp $ */ 2 3 /* OpenLDAP: pkg/ldap/include/lutil.h,v 1.63.2.10 2010/04/19 16:53:01 quanah Exp */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2010 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 #ifdef _WIN32 183 LDAP_LUTIL_F( void ) 184 lutil_slashpath LDAP_P(( char* path )); 185 #define LUTIL_SLASHPATH(p) lutil_slashpath(p) 186 #else 187 #define LUTIL_SLASHPATH(p) 188 #endif 189 190 LDAP_LUTIL_F( char* ) 191 lutil_strcopy LDAP_P(( char *dst, const char *src )); 192 193 LDAP_LUTIL_F( char* ) 194 lutil_strncopy LDAP_P(( char *dst, const char *src, size_t n )); 195 196 LDAP_LUTIL_F( char* ) 197 lutil_memcopy LDAP_P(( char *dst, const char *src, size_t n )); 198 199 #define lutil_strbvcopy(a, bv) lutil_memcopy((a),(bv)->bv_val,(bv)->bv_len) 200 201 struct tm; 202 203 /* use this macro to statically allocate buffer for lutil_gentime */ 204 #define LDAP_LUTIL_GENTIME_BUFSIZE 22 205 #define lutil_gentime(s,m,t) lutil_localtime((s),(m),(t),0) 206 LDAP_LUTIL_F( size_t ) 207 lutil_localtime LDAP_P(( char *s, size_t smax, const struct tm *tm, 208 long delta )); 209 210 #ifndef HAVE_MKSTEMP 211 LDAP_LUTIL_F( int ) 212 mkstemp LDAP_P (( char * template )); 213 #endif 214 215 /* sockpair.c */ 216 LDAP_LUTIL_F( int ) 217 lutil_pair( ber_socket_t sd[2] ); 218 219 /* uuid.c */ 220 /* use this macro to allocate buffer for lutil_uuidstr */ 221 #define LDAP_LUTIL_UUIDSTR_BUFSIZE 40 222 LDAP_LUTIL_F( size_t ) 223 lutil_uuidstr( char *buf, size_t len ); 224 225 LDAP_LUTIL_F( int ) 226 lutil_uuidstr_from_normalized( 227 char *uuid, 228 size_t uuidlen, 229 char *buf, 230 size_t buflen ); 231 232 /* 233 * Sometimes not all declarations in a header file are needed. 234 * An indicator to this is whether or not the symbol's type has 235 * been defined. Thus, we don't need to include a symbol if 236 * its type has not been defined through another header file. 237 */ 238 239 #ifdef HAVE_NT_SERVICE_MANAGER 240 LDAP_LUTIL_V (int) is_NT_Service; 241 242 #ifdef _LDAP_PVT_THREAD_H 243 LDAP_LUTIL_V (ldap_pvt_thread_cond_t) started_event; 244 #endif /* _LDAP_PVT_THREAD_H */ 245 246 /* macros are different between Windows and Mingw */ 247 #if defined(_WINSVC_H) || defined(_WINSVC_) 248 LDAP_LUTIL_V (SERVICE_STATUS) lutil_ServiceStatus; 249 LDAP_LUTIL_V (SERVICE_STATUS_HANDLE) hlutil_ServiceStatus; 250 #endif /* _WINSVC_H */ 251 252 LDAP_LUTIL_F (void) 253 lutil_CommenceStartupProcessing( char *serverName, void (*stopper)(int)) ; 254 255 LDAP_LUTIL_F (void) 256 lutil_ReportShutdownComplete( void ); 257 258 LDAP_LUTIL_F (void *) 259 lutil_getRegParam( char *svc, char *value ); 260 261 LDAP_LUTIL_F (int) 262 lutil_srv_install( char* service, char * displayName, char* filename, 263 int auto_start ); 264 LDAP_LUTIL_F (int) 265 lutil_srv_remove ( char* service, char* filename ); 266 267 #endif /* HAVE_NT_SERVICE_MANAGER */ 268 269 #ifdef HAVE_NT_EVENT_LOG 270 LDAP_LUTIL_F (void) 271 lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls ); 272 273 LDAP_LUTIL_F (void) 274 lutil_LogStoppedEvent( char *svc ); 275 #endif 276 277 #ifdef HAVE_EBCDIC 278 /* Generally this has only been used to put '\n' to stdout. We need to 279 * make sure it is output in EBCDIC. 280 */ 281 #undef putchar 282 #undef putc 283 #define putchar(c) putc((c), stdout) 284 #define putc(c,fp) do { char x=(c); __atoe_l(&x,1); putc(x,fp); } while(0) 285 #endif 286 287 LDAP_LUTIL_F (int) 288 lutil_atoix( int *v, const char *s, int x ); 289 290 LDAP_LUTIL_F (int) 291 lutil_atoux( unsigned *v, const char *s, int x ); 292 293 LDAP_LUTIL_F (int) 294 lutil_atolx( long *v, const char *s, int x ); 295 296 LDAP_LUTIL_F (int) 297 lutil_atoulx( unsigned long *v, const char *s, int x ); 298 299 #define lutil_atoi(v, s) lutil_atoix((v), (s), 10) 300 #define lutil_atou(v, s) lutil_atoux((v), (s), 10) 301 #define lutil_atol(v, s) lutil_atolx((v), (s), 10) 302 #define lutil_atoul(v, s) lutil_atoulx((v), (s), 10) 303 304 LDAP_LUTIL_F (int) 305 lutil_str2bin( struct berval *in, struct berval *out, void *ctx ); 306 307 /* Parse and unparse time intervals */ 308 LDAP_LUTIL_F (int) 309 lutil_parse_time( const char *in, unsigned long *tp ); 310 311 LDAP_LUTIL_F (int) 312 lutil_unparse_time( char *buf, size_t buflen, unsigned long t ); 313 314 #ifdef timerdiv 315 #define lutil_timerdiv timerdiv 316 #else /* ! timerdiv */ 317 /* works inplace (x == t) */ 318 #define lutil_timerdiv(t,d,x) \ 319 do { \ 320 time_t s = (t)->tv_sec; \ 321 assert( d > 0 ); \ 322 (x)->tv_sec = s / d; \ 323 (x)->tv_usec = ( (t)->tv_usec + 1000000 * ( s % d ) ) / d; \ 324 } while ( 0 ) 325 #endif /* ! timerdiv */ 326 327 #ifdef timermul 328 #define lutil_timermul timermul 329 #else /* ! timermul */ 330 /* works inplace (x == t) */ 331 #define lutil_timermul(t,m,x) \ 332 do { \ 333 time_t u = (t)->tv_usec * m; \ 334 assert( m > 0 ); \ 335 (x)->tv_sec = (t)->tv_sec * m + u / 1000000; \ 336 (x)->tv_usec = u % 1000000; \ 337 } while ( 0 ); 338 #endif /* ! timermul */ 339 340 LDAP_END_DECL 341 342 #endif /* _LUTIL_H */ 343