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