openbsd-compat.h revision 294328
1294328Sdes/* $Id: openbsd-compat.h,v 1.62 2014/09/30 23:43:08 djm Exp $ */ 298937Sdes 3124208Sdes/* 4124208Sdes * Copyright (c) 1999-2003 Damien Miller. All rights reserved. 5124208Sdes * Copyright (c) 2003 Ben Lindstrom. All rights reserved. 6124208Sdes * Copyright (c) 2002 Tim Rice. All rights reserved. 7124208Sdes * 8124208Sdes * Redistribution and use in source and binary forms, with or without 9124208Sdes * modification, are permitted provided that the following conditions 10124208Sdes * are met: 11124208Sdes * 1. Redistributions of source code must retain the above copyright 12124208Sdes * notice, this list of conditions and the following disclaimer. 13124208Sdes * 2. Redistributions in binary form must reproduce the above copyright 14124208Sdes * notice, this list of conditions and the following disclaimer in the 15124208Sdes * documentation and/or other materials provided with the distribution. 16124208Sdes * 17124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27124208Sdes */ 2898937Sdes 29124208Sdes#ifndef _OPENBSD_COMPAT_H 30124208Sdes#define _OPENBSD_COMPAT_H 3198937Sdes 32124208Sdes#include "includes.h" 33124208Sdes 34162852Sdes#include <sys/types.h> 35162852Sdes#include <pwd.h> 36162852Sdes 37162852Sdes#include <sys/socket.h> 38162852Sdes 3998937Sdes/* OpenBSD function replacements */ 4098937Sdes#include "base64.h" 4198937Sdes#include "sigact.h" 4298937Sdes#include "glob.h" 4398937Sdes#include "readpassphrase.h" 44113908Sdes#include "vis.h" 45124208Sdes#include "getrrsetbyname.h" 46162852Sdes#include "sha2.h" 47261320Sdes#include "blf.h" 4898937Sdes 49124208Sdes#ifndef HAVE_BASENAME 50124208Sdeschar *basename(const char *path); 51124208Sdes#endif 52124208Sdes 53124208Sdes#ifndef HAVE_BINDRESVPORT_SA 54124208Sdesint bindresvport_sa(int sd, struct sockaddr *sa); 55124208Sdes#endif 56124208Sdes 57137015Sdes#ifndef HAVE_CLOSEFROM 58137015Sdesvoid closefrom(int); 59137015Sdes#endif 60137015Sdes 61124208Sdes#ifndef HAVE_GETCWD 62124208Sdeschar *getcwd(char *pt, size_t size); 63124208Sdes#endif 64124208Sdes 65124208Sdes#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) 66124208Sdeschar *realpath(const char *path, char *resolved); 67124208Sdes#endif 68124208Sdes 69124208Sdes#ifndef HAVE_RRESVPORT_AF 70124208Sdesint rresvport_af(int *alport, sa_family_t af); 71124208Sdes#endif 72124208Sdes 73124208Sdes#ifndef HAVE_STRLCPY 74124208Sdes/* #include <sys/types.h> XXX Still needed? */ 75124208Sdessize_t strlcpy(char *dst, const char *src, size_t siz); 76124208Sdes#endif 77124208Sdes 78124208Sdes#ifndef HAVE_STRLCAT 79124208Sdes/* #include <sys/types.h> XXX Still needed? */ 80124208Sdessize_t strlcat(char *dst, const char *src, size_t siz); 81124208Sdes#endif 82124208Sdes 83124208Sdes#ifndef HAVE_SETENV 84124208Sdesint setenv(register const char *name, register const char *value, int rewrite); 85124208Sdes#endif 86124208Sdes 87124208Sdes#ifndef HAVE_STRMODE 88124208Sdesvoid strmode(int mode, char *p); 89124208Sdes#endif 90124208Sdes 91215116Sdes#ifndef HAVE_STRPTIME 92215116Sdes#include <time.h> 93215116Sdeschar *strptime(const char *buf, const char *fmt, struct tm *tm); 94215116Sdes#endif 95215116Sdes 96124208Sdes#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP) 97124208Sdesint mkstemps(char *path, int slen); 98124208Sdesint mkstemp(char *path); 99124208Sdeschar *mkdtemp(char *path); 100124208Sdes#endif 101124208Sdes 102124208Sdes#ifndef HAVE_DAEMON 103124208Sdesint daemon(int nochdir, int noclose); 104124208Sdes#endif 105124208Sdes 106124208Sdes#ifndef HAVE_DIRNAME 107124208Sdeschar *dirname(const char *path); 108124208Sdes#endif 109124208Sdes 110181111Sdes#ifndef HAVE_FMT_SCALED 111181111Sdes#define FMT_SCALED_STRSIZE 7 112181111Sdesint fmt_scaled(long long number, char *result); 113181111Sdes#endif 114181111Sdes 115255767Sdes#ifndef HAVE_SCAN_SCALED 116255767Sdesint scan_scaled(char *, long long *); 117255767Sdes#endif 118255767Sdes 119124208Sdes#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA) 120124208Sdeschar *inet_ntoa(struct in_addr in); 121124208Sdes#endif 122124208Sdes 123124208Sdes#ifndef HAVE_INET_NTOP 124240075Sdesconst char *inet_ntop(int af, const void *src, char *dst, socklen_t size); 125124208Sdes#endif 126124208Sdes 127124208Sdes#ifndef HAVE_INET_ATON 128124208Sdesint inet_aton(const char *cp, struct in_addr *addr); 129124208Sdes#endif 130124208Sdes 131124208Sdes#ifndef HAVE_STRSEP 132124208Sdeschar *strsep(char **stringp, const char *delim); 133124208Sdes#endif 134124208Sdes 135124208Sdes#ifndef HAVE_SETPROCTITLE 136124208Sdesvoid setproctitle(const char *fmt, ...); 137124208Sdesvoid compat_init_setproctitle(int argc, char *argv[]); 138124208Sdes#endif 139124208Sdes 140124208Sdes#ifndef HAVE_GETGROUPLIST 141124208Sdes/* #include <grp.h> XXXX Still needed ? */ 142124208Sdesint getgrouplist(const char *, gid_t, gid_t *, int *); 143124208Sdes#endif 144124208Sdes 145124208Sdes#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET) 146124208Sdesint BSDgetopt(int argc, char * const *argv, const char *opts); 147255767Sdes#include "openbsd-compat/getopt.h" 148124208Sdes#endif 149124208Sdes 150162852Sdes#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0 151162852Sdes# include <sys/types.h> 152162852Sdes# include <sys/uio.h> 153162852Sdesint writev(int, struct iovec *, int); 154162852Sdes#endif 155124208Sdes 15698937Sdes/* Home grown routines */ 15798937Sdes#include "bsd-misc.h" 158248619Sdes#include "bsd-setres_id.h" 159181111Sdes#include "bsd-statvfs.h" 16098937Sdes#include "bsd-waitpid.h" 161181111Sdes#include "bsd-poll.h" 16298937Sdes 163124208Sdes#ifndef HAVE_GETPEEREID 164124208Sdesint getpeereid(int , uid_t *, gid_t *); 165261320Sdes#endif 166124208Sdes 167261320Sdes#ifdef HAVE_ARC4RANDOM 168261320Sdes# ifndef HAVE_ARC4RANDOM_STIR 169261320Sdes# define arc4random_stir() 170261320Sdes# endif 171261320Sdes#else 172124208Sdesunsigned int arc4random(void); 173124208Sdesvoid arc4random_stir(void); 174124208Sdes#endif /* !HAVE_ARC4RANDOM */ 175124208Sdes 176181111Sdes#ifndef HAVE_ARC4RANDOM_BUF 177181111Sdesvoid arc4random_buf(void *, size_t); 178181111Sdes#endif 179181111Sdes 180181111Sdes#ifndef HAVE_ARC4RANDOM_UNIFORM 181181111Sdesu_int32_t arc4random_uniform(u_int32_t); 182181111Sdes#endif 183181111Sdes 184157016Sdes#ifndef HAVE_ASPRINTF 185157016Sdesint asprintf(char **, const char *, ...); 186157016Sdes#endif 187157016Sdes 188126274Sdes#ifndef HAVE_OPENPTY 189162852Sdes# include <sys/ioctl.h> /* for struct winsize */ 190126274Sdesint openpty(int *, int *, char *, struct termios *, struct winsize *); 191126274Sdes#endif /* HAVE_OPENPTY */ 192124208Sdes 193124208Sdes/* #include <sys/types.h> XXX needed? For size_t */ 194124208Sdes 195124208Sdes#ifndef HAVE_SNPRINTF 196162852Sdesint snprintf(char *, size_t, SNPRINTF_CONST char *, ...); 197124208Sdes#endif 198124208Sdes 199157016Sdes#ifndef HAVE_STRTOLL 200157016Sdeslong long strtoll(const char *, char **, int); 201157016Sdes#endif 202157016Sdes 203248619Sdes#ifndef HAVE_STRTOUL 204248619Sdesunsigned long strtoul(const char *, char **, int); 205248619Sdes#endif 206248619Sdes 207248619Sdes#ifndef HAVE_STRTOULL 208248619Sdesunsigned long long strtoull(const char *, char **, int); 209248619Sdes#endif 210248619Sdes 211149749Sdes#ifndef HAVE_STRTONUM 212149749Sdeslong long strtonum(const char *, long long, long long, const char **); 213149749Sdes#endif 214149749Sdes 215255767Sdes/* multibyte character support */ 216255767Sdes#ifndef HAVE_MBLEN 217255767Sdes# define mblen(x, y) 1 218255767Sdes#endif 219255767Sdes 220162852Sdes#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF) 221162852Sdes# include <stdarg.h> 222162852Sdes#endif 223162852Sdes 224157016Sdes#ifndef HAVE_VASPRINTF 225157016Sdesint vasprintf(char **, const char *, va_list); 226157016Sdes#endif 227157016Sdes 228124208Sdes#ifndef HAVE_VSNPRINTF 229124208Sdesint vsnprintf(char *, size_t, const char *, va_list); 230124208Sdes#endif 231124208Sdes 232204917Sdes#ifndef HAVE_USER_FROM_UID 233204917Sdeschar *user_from_uid(uid_t, int); 234204917Sdes#endif 235204917Sdes 236204917Sdes#ifndef HAVE_GROUP_FROM_GID 237204917Sdeschar *group_from_gid(gid_t, int); 238204917Sdes#endif 239204917Sdes 240221420Sdes#ifndef HAVE_TIMINGSAFE_BCMP 241221420Sdesint timingsafe_bcmp(const void *, const void *, size_t); 242221420Sdes#endif 243221420Sdes 244261320Sdes#ifndef HAVE_BCRYPT_PBKDF 245261320Sdesint bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t, 246261320Sdes u_int8_t *, size_t, unsigned int); 247261320Sdes#endif 248261320Sdes 249263712Sdes#ifndef HAVE_EXPLICIT_BZERO 250263712Sdesvoid explicit_bzero(void *p, size_t n); 251263712Sdes#endif 252263712Sdes 253124208Sdesvoid *xmmap(size_t size); 254124208Sdeschar *xcrypt(const char *password, const char *salt); 255124208Sdeschar *shadow_pw(struct passwd *pw); 256124208Sdes 25798937Sdes/* rfc2553 socket API replacements */ 258124208Sdes#include "fake-rfc2553.h" 25998937Sdes 26098937Sdes/* Routines for a single OS platform */ 26198937Sdes#include "bsd-cray.h" 262124208Sdes#include "bsd-cygwin_util.h" 263162852Sdes 264162852Sdes#include "port-aix.h" 26598937Sdes#include "port-irix.h" 266162852Sdes#include "port-linux.h" 267162852Sdes#include "port-solaris.h" 268162852Sdes#include "port-tun.h" 269149749Sdes#include "port-uw.h" 27098937Sdes 271294328Sdes/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */ 272294328Sdes#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE) 273294328Sdes# include <features.h> 274294328Sdes# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ) 275294328Sdes# if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) 276294328Sdes# include <sys/socket.h> /* Ensure include guard is defined */ 277294328Sdes# undef FD_SET 278294328Sdes# undef FD_ISSET 279294328Sdes# define FD_SET(n, set) kludge_FD_SET(n, set) 280294328Sdes# define FD_ISSET(n, set) kludge_FD_ISSET(n, set) 281294328Sdesvoid kludge_FD_SET(int, fd_set *); 282294328Sdesint kludge_FD_ISSET(int, fd_set *); 283294328Sdes# endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */ 284294328Sdes# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */ 285294328Sdes#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */ 286294328Sdes 287124208Sdes#endif /* _OPENBSD_COMPAT_H */ 288