openbsd-compat.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 1999-2003 Damien Miller. All rights reserved.
  3. * Copyright (c) 2003 Ben Lindstrom. All rights reserved.
  4. * Copyright (c) 2002 Tim Rice. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef _OPENBSD_COMPAT_H
  27. #define _OPENBSD_COMPAT_H
  28. #include "includes.h"
  29. #include <sys/types.h>
  30. #include <pwd.h>
  31. #include <sys/socket.h>
  32. #include <stddef.h> /* for wchar_t */
  33. /* OpenBSD function replacements */
  34. #include "base64.h"
  35. #include "sigact.h"
  36. #include "readpassphrase.h"
  37. #include "vis.h"
  38. #include "getrrsetbyname.h"
  39. #include "sha1.h"
  40. #include "sha2.h"
  41. #include "md5.h"
  42. #include "blf.h"
  43. #include "fnmatch.h"
  44. #ifndef HAVE_BASENAME
  45. char *basename(const char *path);
  46. #endif
  47. #ifndef HAVE_BINDRESVPORT_SA
  48. int bindresvport_sa(int sd, struct sockaddr *sa);
  49. #endif
  50. #ifndef HAVE_CLOSEFROM
  51. void closefrom(int);
  52. #endif
  53. #ifndef HAVE_GETLINE
  54. #include <stdio.h>
  55. ssize_t getline(char **, size_t *, FILE *);
  56. #endif
  57. #ifndef HAVE_GETPAGESIZE
  58. int getpagesize(void);
  59. #endif
  60. #ifndef HAVE_GETCWD
  61. char *getcwd(char *pt, size_t size);
  62. #endif
  63. #if defined(HAVE_DECL_MEMMEM) && HAVE_DECL_MEMMEM == 0
  64. void *memmem(const void *, size_t, const void *, size_t);
  65. #endif
  66. #ifndef HAVE_REALLOCARRAY
  67. void *reallocarray(void *, size_t, size_t);
  68. #endif
  69. #ifndef HAVE_RECALLOCARRAY
  70. void *recallocarray(void *, size_t, size_t, size_t);
  71. #endif
  72. #ifndef HAVE_RRESVPORT_AF
  73. int rresvport_af(int *alport, sa_family_t af);
  74. #endif
  75. #ifndef HAVE_STRLCPY
  76. size_t strlcpy(char *dst, const char *src, size_t siz);
  77. #endif
  78. #ifndef HAVE_STRLCAT
  79. size_t strlcat(char *dst, const char *src, size_t siz);
  80. #endif
  81. #ifndef HAVE_STRCASESTR
  82. char *strcasestr(const char *, const char *);
  83. #endif
  84. #ifndef HAVE_STRNLEN
  85. size_t strnlen(const char *, size_t);
  86. #endif
  87. #ifndef HAVE_STRNDUP
  88. char *strndup(const char *s, size_t n);
  89. #endif
  90. #ifndef HAVE_SETENV
  91. int setenv(register const char *name, register const char *value, int rewrite);
  92. #endif
  93. #ifndef HAVE_STRMODE
  94. void strmode(int mode, char *p);
  95. #endif
  96. #ifndef HAVE_STRPTIME
  97. #include <time.h>
  98. char *strptime(const char *buf, const char *fmt, struct tm *tm);
  99. #endif
  100. #if !defined(HAVE_MKDTEMP)
  101. int mkstemps(char *path, int slen);
  102. int mkstemp(char *path);
  103. char *mkdtemp(char *path);
  104. #endif
  105. #ifndef HAVE_DAEMON
  106. int daemon(int nochdir, int noclose);
  107. #endif
  108. #ifndef HAVE_DIRNAME
  109. char *dirname(const char *path);
  110. #endif
  111. #ifndef HAVE_FMT_SCALED
  112. #define FMT_SCALED_STRSIZE 7
  113. int fmt_scaled(long long number, char *result);
  114. #endif
  115. #ifndef HAVE_SCAN_SCALED
  116. int scan_scaled(char *, long long *);
  117. #endif
  118. #if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
  119. char *inet_ntoa(struct in_addr in);
  120. #endif
  121. #ifndef HAVE_INET_NTOP
  122. const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  123. #endif
  124. #ifndef HAVE_INET_ATON
  125. int inet_aton(const char *cp, struct in_addr *addr);
  126. #endif
  127. #ifndef HAVE_STRSEP
  128. char *strsep(char **stringp, const char *delim);
  129. #endif
  130. #ifndef HAVE_SETPROCTITLE
  131. void setproctitle(const char *fmt, ...);
  132. void compat_init_setproctitle(int argc, char *argv[]);
  133. #endif
  134. #ifndef HAVE_GETGROUPLIST
  135. int getgrouplist(const char *, gid_t, gid_t *, int *);
  136. #endif
  137. #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
  138. int BSDgetopt(int argc, char * const *argv, const char *opts);
  139. #include "openbsd-compat/getopt.h"
  140. #endif
  141. #if ((defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0) || \
  142. (defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0))
  143. # include <sys/types.h>
  144. # include <sys/uio.h>
  145. # if defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0
  146. int readv(int, struct iovec *, int);
  147. # endif
  148. # if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
  149. int writev(int, struct iovec *, int);
  150. # endif
  151. #endif
  152. /* Home grown routines */
  153. #include "bsd-misc.h"
  154. #include "bsd-setres_id.h"
  155. #include "bsd-signal.h"
  156. #include "bsd-statvfs.h"
  157. #include "bsd-waitpid.h"
  158. #include "bsd-poll.h"
  159. #if defined(HAVE_DECL_GETPEEREID) && HAVE_DECL_GETPEEREID == 0
  160. int getpeereid(int , uid_t *, gid_t *);
  161. #endif
  162. #ifdef HAVE_ARC4RANDOM
  163. # ifndef HAVE_ARC4RANDOM_STIR
  164. # define arc4random_stir()
  165. # endif
  166. #else
  167. unsigned int arc4random(void);
  168. void arc4random_stir(void);
  169. #endif /* !HAVE_ARC4RANDOM */
  170. #ifndef HAVE_ARC4RANDOM_BUF
  171. void arc4random_buf(void *, size_t);
  172. #endif
  173. #ifndef HAVE_ARC4RANDOM_UNIFORM
  174. u_int32_t arc4random_uniform(u_int32_t);
  175. #endif
  176. #ifndef HAVE_ASPRINTF
  177. int asprintf(char **, const char *, ...);
  178. #endif
  179. #ifndef HAVE_OPENPTY
  180. # include <sys/ioctl.h> /* for struct winsize */
  181. int openpty(int *, int *, char *, struct termios *, struct winsize *);
  182. #endif /* HAVE_OPENPTY */
  183. #ifndef HAVE_SNPRINTF
  184. int snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
  185. #endif
  186. #ifndef HAVE_STRTOLL
  187. long long strtoll(const char *, char **, int);
  188. #endif
  189. #ifndef HAVE_STRTOUL
  190. unsigned long strtoul(const char *, char **, int);
  191. #endif
  192. #ifndef HAVE_STRTOULL
  193. unsigned long long strtoull(const char *, char **, int);
  194. #endif
  195. #ifndef HAVE_STRTONUM
  196. long long strtonum(const char *, long long, long long, const char **);
  197. #endif
  198. /* multibyte character support */
  199. #ifndef HAVE_MBLEN
  200. # define mblen(x, y) (1)
  201. #endif
  202. #ifndef HAVE_WCWIDTH
  203. # define wcwidth(x) (((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
  204. /* force our no-op nl_langinfo and mbtowc */
  205. # undef HAVE_NL_LANGINFO
  206. # undef HAVE_MBTOWC
  207. # undef HAVE_LANGINFO_H
  208. #endif
  209. #ifndef HAVE_NL_LANGINFO
  210. # define nl_langinfo(x) ""
  211. #endif
  212. #ifndef HAVE_MBTOWC
  213. int mbtowc(wchar_t *, const char*, size_t);
  214. #endif
  215. #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
  216. # include <stdarg.h>
  217. #endif
  218. /*
  219. * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
  220. * instead. This is known to be the case on at least some configurations of
  221. * AIX with the xlc compiler.
  222. */
  223. #ifndef VA_COPY
  224. # ifdef HAVE_VA_COPY
  225. # define VA_COPY(dest, src) va_copy(dest, src)
  226. # else
  227. # ifdef HAVE___VA_COPY
  228. # define VA_COPY(dest, src) __va_copy(dest, src)
  229. # else
  230. # define VA_COPY(dest, src) (dest) = (src)
  231. # endif
  232. # endif
  233. #endif
  234. #ifndef HAVE_VASPRINTF
  235. int vasprintf(char **, const char *, va_list);
  236. #endif
  237. #ifndef HAVE_VSNPRINTF
  238. int vsnprintf(char *, size_t, const char *, va_list);
  239. #endif
  240. #ifndef HAVE_USER_FROM_UID
  241. char *user_from_uid(uid_t, int);
  242. #endif
  243. #ifndef HAVE_GROUP_FROM_GID
  244. char *group_from_gid(gid_t, int);
  245. #endif
  246. #ifndef HAVE_TIMINGSAFE_BCMP
  247. int timingsafe_bcmp(const void *, const void *, size_t);
  248. #endif
  249. #ifndef HAVE_BCRYPT_PBKDF
  250. int bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
  251. u_int8_t *, size_t, unsigned int);
  252. #endif
  253. #ifndef HAVE_EXPLICIT_BZERO
  254. void explicit_bzero(void *p, size_t n);
  255. #endif
  256. #ifndef HAVE_FREEZERO
  257. void freezero(void *, size_t);
  258. #endif
  259. #ifndef HAVE_LOCALTIME_R
  260. struct tm *localtime_r(const time_t *, struct tm *);
  261. #endif
  262. char *xcrypt(const char *password, const char *salt);
  263. char *shadow_pw(struct passwd *pw);
  264. /* rfc2553 socket API replacements */
  265. #include "fake-rfc2553.h"
  266. /* Routines for a single OS platform */
  267. #include "bsd-cygwin_util.h"
  268. #include "port-aix.h"
  269. #include "port-irix.h"
  270. #include "port-linux.h"
  271. #include "port-solaris.h"
  272. #include "port-net.h"
  273. #include "port-uw.h"
  274. /* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
  275. #if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
  276. # include <features.h>
  277. # if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
  278. # if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
  279. # include <sys/socket.h> /* Ensure include guard is defined */
  280. # undef FD_SET
  281. # undef FD_ISSET
  282. # define FD_SET(n, set) kludge_FD_SET(n, set)
  283. # define FD_ISSET(n, set) kludge_FD_ISSET(n, set)
  284. void kludge_FD_SET(int, fd_set *);
  285. int kludge_FD_ISSET(int, fd_set *);
  286. # endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
  287. # endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
  288. #endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
  289. #endif /* _OPENBSD_COMPAT_H */