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