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