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 27 #ifndef _OPENBSD_COMPAT_H 28 #define _OPENBSD_COMPAT_H 29 30 #include "includes.h" 31 32 #include <sys/types.h> 33 #include <pwd.h> 34 35 #include <sys/socket.h> 36 37 #include <stddef.h> /* for wchar_t */ 38 39 /* OpenBSD function replacements */ 40 #include "base64.h" 41 #include "sigact.h" 42 #include "readpassphrase.h" 43 #include "vis.h" 44 #include "getrrsetbyname.h" 45 #include "sha1.h" 46 #include "sha2.h" 47 #include "md5.h" 48 #include "blf.h" 49 #include "fnmatch.h" 50 51 #if defined(HAVE_LOGIN_CAP) && !defined(HAVE_LOGIN_GETPWCLASS) 52 # include <login_cap.h> 53 # define login_getpwclass(pw) login_getclass(pw->pw_class) 54 #endif 55 56 #ifndef HAVE_BASENAME 57 char *basename(const char *path); 58 #endif 59 60 #ifndef HAVE_BINDRESVPORT_SA 61 int bindresvport_sa(int sd, struct sockaddr *sa); 62 #endif 63 64 #ifndef HAVE_CLOSEFROM 65 void closefrom(int); 66 #endif 67 68 #if defined(HAVE_DECL_FTRUNCATE) && HAVE_DECL_FTRUNCATE == 0 69 int ftruncate(int filedes, off_t length); 70 #endif 71 72 #ifndef HAVE_GETLINE 73 #include <stdio.h> 74 ssize_t getline(char **, size_t *, FILE *); 75 #endif 76 77 #ifndef HAVE_GETPAGESIZE 78 int getpagesize(void); 79 #endif 80 81 #ifndef HAVE_GETCWD 82 char *getcwd(char *pt, size_t size); 83 #endif 84 85 #ifndef HAVE_KILLPG 86 int killpg(pid_t, int); 87 #endif 88 89 #if defined(HAVE_DECL_MEMMEM) && HAVE_DECL_MEMMEM == 0 90 void *memmem(const void *, size_t, const void *, size_t); 91 #endif 92 93 #ifndef HAVE_REALLOCARRAY 94 void *reallocarray(void *, size_t, size_t); 95 #endif 96 97 #ifndef HAVE_RECALLOCARRAY 98 void *recallocarray(void *, size_t, size_t, size_t); 99 #endif 100 101 #ifndef HAVE_RRESVPORT_AF 102 int rresvport_af(int *alport, sa_family_t af); 103 #endif 104 105 #ifndef HAVE_STRLCPY 106 size_t strlcpy(char *dst, const char *src, size_t siz); 107 #endif 108 109 #ifndef HAVE_STRLCAT 110 size_t strlcat(char *dst, const char *src, size_t siz); 111 #endif 112 113 #ifndef HAVE_STRCASESTR 114 char *strcasestr(const char *, const char *); 115 #endif 116 117 #ifndef HAVE_STRNLEN 118 size_t strnlen(const char *, size_t); 119 #endif 120 121 #ifndef HAVE_STRNDUP 122 char *strndup(const char *s, size_t n); 123 #endif 124 125 #ifndef HAVE_SETENV 126 int setenv(register const char *name, register const char *value, int rewrite); 127 #endif 128 129 #ifndef HAVE_STRMODE 130 void strmode(int mode, char *p); 131 #endif 132 133 #ifndef HAVE_STRPTIME 134 #include <time.h> 135 char *strptime(const char *buf, const char *fmt, struct tm *tm); 136 #endif 137 138 #if !defined(HAVE_MKDTEMP) 139 int mkstemps(char *path, int slen); 140 int mkstemp(char *path); 141 char *mkdtemp(char *path); 142 #endif 143 144 #ifndef HAVE_DAEMON 145 int daemon(int nochdir, int noclose); 146 #endif 147 148 #ifndef HAVE_DIRNAME 149 char *dirname(const char *path); 150 #endif 151 152 #ifndef HAVE_FMT_SCALED 153 #define FMT_SCALED_STRSIZE 7 154 int fmt_scaled(long long number, char *result); 155 #endif 156 157 #ifndef HAVE_SCAN_SCALED 158 int scan_scaled(char *, long long *); 159 #endif 160 161 #if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA) 162 char *inet_ntoa(struct in_addr in); 163 #endif 164 165 #ifndef HAVE_INET_NTOP 166 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); 167 #endif 168 169 #ifndef HAVE_INET_ATON 170 int inet_aton(const char *cp, struct in_addr *addr); 171 #endif 172 173 #ifndef HAVE_STRSEP 174 char *strsep(char **stringp, const char *delim); 175 #endif 176 177 #ifndef HAVE_SETPROCTITLE 178 void setproctitle(const char *fmt, ...); 179 void compat_init_setproctitle(int argc, char *argv[]); 180 #endif 181 182 #ifndef HAVE_GETGROUPLIST 183 int getgrouplist(const char *, gid_t, gid_t *, int *); 184 #endif 185 186 #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET) 187 int BSDgetopt(int argc, char * const *argv, const char *opts); 188 #include "openbsd-compat/getopt.h" 189 #endif 190 191 #if ((defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0) || \ 192 (defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0)) 193 # include <sys/types.h> 194 # include <sys/uio.h> 195 196 # if defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0 197 int readv(int, struct iovec *, int); 198 # endif 199 200 # if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0 201 int writev(int, struct iovec *, int); 202 # endif 203 #endif 204 205 /* Home grown routines */ 206 #include "bsd-signal.h" 207 #include "bsd-misc.h" 208 #include "bsd-setres_id.h" 209 #include "bsd-statvfs.h" 210 #include "bsd-waitpid.h" 211 #include "bsd-poll.h" 212 213 #if defined(HAVE_DECL_GETPEEREID) && HAVE_DECL_GETPEEREID == 0 214 int getpeereid(int , uid_t *, gid_t *); 215 #endif 216 217 #ifndef HAVE_ARC4RANDOM 218 uint32_t arc4random(void); 219 #endif /* !HAVE_ARC4RANDOM */ 220 221 #ifndef HAVE_ARC4RANDOM_BUF 222 void arc4random_buf(void *, size_t); 223 #endif 224 225 #ifndef HAVE_ARC4RANDOM_STIR 226 # define arc4random_stir() 227 #endif 228 229 #ifndef HAVE_ARC4RANDOM_UNIFORM 230 uint32_t arc4random_uniform(uint32_t); 231 #endif 232 233 #ifndef HAVE_ASPRINTF 234 int asprintf(char **, const char *, ...); 235 #endif 236 237 #ifndef HAVE_OPENPTY 238 # include <sys/ioctl.h> /* for struct winsize */ 239 int openpty(int *, int *, char *, struct termios *, struct winsize *); 240 #endif /* HAVE_OPENPTY */ 241 242 #ifndef HAVE_SNPRINTF 243 int snprintf(char *, size_t, SNPRINTF_CONST char *, ...); 244 #endif 245 246 #ifndef HAVE_STRTOLL 247 long long strtoll(const char *, char **, int); 248 #endif 249 250 #ifndef HAVE_STRTOUL 251 unsigned long strtoul(const char *, char **, int); 252 #endif 253 254 #ifndef HAVE_STRTOULL 255 unsigned long long strtoull(const char *, char **, int); 256 #endif 257 258 #ifndef HAVE_STRTONUM 259 long long strtonum(const char *, long long, long long, const char **); 260 #endif 261 262 /* multibyte character support */ 263 #ifndef HAVE_MBLEN 264 # define mblen(x, y) (1) 265 #endif 266 267 #ifndef HAVE_WCWIDTH 268 # define wcwidth(x) (((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1) 269 /* force our no-op nl_langinfo and mbtowc */ 270 # undef HAVE_NL_LANGINFO 271 # undef HAVE_MBTOWC 272 # undef HAVE_LANGINFO_H 273 #endif 274 275 #ifndef HAVE_NL_LANGINFO 276 # define nl_langinfo(x) "" 277 #endif 278 279 #ifndef HAVE_MBTOWC 280 int mbtowc(wchar_t *, const char*, size_t); 281 #endif 282 283 #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF) 284 # include <stdarg.h> 285 #endif 286 287 /* 288 * Some platforms unconditionally undefine va_copy() so we define VA_COPY() 289 * instead. This is known to be the case on at least some configurations of 290 * AIX with the xlc compiler. 291 */ 292 #ifndef VA_COPY 293 # ifdef HAVE_VA_COPY 294 # define VA_COPY(dest, src) va_copy(dest, src) 295 # else 296 # ifdef HAVE___VA_COPY 297 # define VA_COPY(dest, src) __va_copy(dest, src) 298 # else 299 # define VA_COPY(dest, src) (dest) = (src) 300 # endif 301 # endif 302 #endif 303 304 #ifndef HAVE_VASPRINTF 305 int vasprintf(char **, const char *, va_list); 306 #endif 307 308 #ifndef HAVE_VSNPRINTF 309 int vsnprintf(char *, size_t, const char *, va_list); 310 #endif 311 312 #ifndef HAVE_USER_FROM_UID 313 char *user_from_uid(uid_t, int); 314 #endif 315 316 #ifndef HAVE_GROUP_FROM_GID 317 char *group_from_gid(gid_t, int); 318 #endif 319 320 #ifndef HAVE_TIMINGSAFE_BCMP 321 int timingsafe_bcmp(const void *, const void *, size_t); 322 #endif 323 324 #ifndef HAVE_BCRYPT_PBKDF 325 int bcrypt_pbkdf(const char *, size_t, const uint8_t *, size_t, 326 uint8_t *, size_t, unsigned int); 327 #endif 328 329 #ifndef HAVE_EXPLICIT_BZERO 330 void explicit_bzero(void *p, size_t n); 331 #endif 332 333 #ifndef HAVE_FREEZERO 334 void freezero(void *, size_t); 335 #endif 336 337 #ifndef HAVE_LOCALTIME_R 338 struct tm *localtime_r(const time_t *, struct tm *); 339 #endif 340 341 #ifndef HAVE_TIMEGM 342 #include <time.h> 343 time_t timegm(struct tm *); 344 #endif 345 346 char *xcrypt(const char *password, const char *salt); 347 char *shadow_pw(struct passwd *pw); 348 349 /* rfc2553 socket API replacements */ 350 #include "fake-rfc2553.h" 351 352 /* Routines for a single OS platform */ 353 #include "bsd-cygwin_util.h" 354 355 #include "port-aix.h" 356 #include "port-irix.h" 357 #include "port-linux.h" 358 #include "port-solaris.h" 359 #include "port-net.h" 360 #include "port-uw.h" 361 362 /* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */ 363 #if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE) 364 # include <features.h> 365 # if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ) 366 # if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) 367 # include <sys/socket.h> /* Ensure include guard is defined */ 368 # undef FD_SET 369 # undef FD_ISSET 370 # define FD_SET(n, set) kludge_FD_SET(n, set) 371 # define FD_ISSET(n, set) kludge_FD_ISSET(n, set) 372 void kludge_FD_SET(int, fd_set *); 373 int kludge_FD_ISSET(int, fd_set *); 374 # endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */ 375 # endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */ 376 #endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */ 377 378 #endif /* _OPENBSD_COMPAT_H */ 379