lutil.h 9.3 KB

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