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