blob: 865aaee532bd5a97eb4f1e2c2e23904741beb4a5 [file] [log] [blame]
Damien Milleraf639512003-06-11 22:51:32 +10001/*
2 * Copyright (c) 1999-2003 Damien Miller. All rights reserved.
Ben Lindstrom515d0f92003-08-29 16:59:52 +00003 * Copyright (c) 2003 Ben Lindstrom. All rights reserved.
4 * Copyright (c) 2002 Tim Rice. All rights reserved.
Damien Milleraf639512003-06-11 22:51:32 +10005 *
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
Ben Lindstrom515d0f92003-08-29 16:59:52 +000027#ifndef _OPENBSD_COMPAT_H
28#define _OPENBSD_COMPAT_H
Damien Millere9cf3572001-02-09 12:55:35 +110029
Ben Lindstrom515d0f92003-08-29 16:59:52 +000030#include "includes.h"
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +000031
Damien Millera1738e42006-07-10 21:33:04 +100032#include <sys/types.h>
33#include <pwd.h>
34
Darren Tucker4e880e62006-07-11 00:20:51 +100035#include <sys/socket.h>
36
Darren Tuckera2333582016-07-14 10:59:09 +100037#include <stddef.h> /* for wchar_t */
38
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +000039/* OpenBSD function replacements */
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +000040#include "base64.h"
Ben Lindstrom803f16c2001-02-24 00:24:19 +000041#include "sigact.h"
Damien Millerc8a38682001-06-25 18:09:16 +100042#include "readpassphrase.h"
Damien Millerb93addb2003-01-07 17:04:18 +110043#include "vis.h"
Damien Millerd9ec3702003-05-15 12:27:08 +100044#include "getrrsetbyname.h"
Damien Miller72ef7c12015-01-15 02:21:31 +110045#include "sha1.h"
Damien Milleraf87af12006-03-15 13:02:28 +110046#include "sha2.h"
Damien Miller72ef7c12015-01-15 02:21:31 +110047#include "rmd160.h"
48#include "md5.h"
Damien Miller1ff130d2013-12-07 11:51:51 +110049#include "blf.h"
Ben Lindstrom515d0f92003-08-29 16:59:52 +000050
51#ifndef HAVE_BASENAME
52char *basename(const char *path);
53#endif
54
55#ifndef HAVE_BINDRESVPORT_SA
56int bindresvport_sa(int sd, struct sockaddr *sa);
57#endif
58
Damien Miller36f49652004-08-15 18:40:59 +100059#ifndef HAVE_CLOSEFROM
60void closefrom(int);
61#endif
62
Darren Tucker6301e6c2018-07-02 21:16:58 +100063#ifndef HAVE_GETLINE
Darren Tucker8a85f542018-11-25 21:44:05 +110064#include <stdio.h>
Darren Tucker6301e6c2018-07-02 21:16:58 +100065ssize_t getline(char **, size_t *, FILE *);
66#endif
67
Damien Miller151c6e42017-06-01 15:25:13 +100068#ifndef HAVE_GETPAGESIZE
69int getpagesize(void);
70#endif
71
Ben Lindstrom515d0f92003-08-29 16:59:52 +000072#ifndef HAVE_GETCWD
73char *getcwd(char *pt, size_t size);
Darren Tuckerc20dccb2016-08-02 09:44:25 +100074#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +000075
Darren Tucker642652d2014-12-10 01:32:23 +110076#ifndef HAVE_REALLOCARRAY
77void *reallocarray(void *, size_t, size_t);
78#endif
79
Damien Miller151c6e42017-06-01 15:25:13 +100080#ifndef HAVE_RECALLOCARRAY
81void *recallocarray(void *, size_t, size_t, size_t);
82#endif
83
Ben Lindstrom515d0f92003-08-29 16:59:52 +000084#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
Darren Tuckerd3e2aee2015-07-17 12:52:34 +100085/*
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
Ben Lindstrom515d0f92003-08-29 16:59:52 +000093char *realpath(const char *path, char *resolved);
Darren Tuckerd3e2aee2015-07-17 12:52:34 +100094#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +000095
96#ifndef HAVE_RRESVPORT_AF
97int rresvport_af(int *alport, sa_family_t af);
98#endif
99
100#ifndef HAVE_STRLCPY
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000101size_t strlcpy(char *dst, const char *src, size_t siz);
102#endif
103
104#ifndef HAVE_STRLCAT
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000105size_t strlcat(char *dst, const char *src, size_t siz);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000106#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000107
Darren Tucker25275f12016-12-13 12:54:23 +1100108#ifndef HAVE_STRCASESTR
109char *strcasestr(const char *, const char *);
110#endif
111
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000112#ifndef HAVE_SETENV
113int setenv(register const char *name, register const char *value, int rewrite);
114#endif
115
116#ifndef HAVE_STRMODE
117void strmode(int mode, char *p);
118#endif
119
Darren Tuckeraa74f672010-08-16 13:15:23 +1000120#ifndef HAVE_STRPTIME
121#include <time.h>
122char *strptime(const char *buf, const char *fmt, struct tm *tm);
123#endif
124
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000125#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
126int mkstemps(char *path, int slen);
127int mkstemp(char *path);
128char *mkdtemp(char *path);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000129#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000130
131#ifndef HAVE_DAEMON
132int daemon(int nochdir, int noclose);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000133#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000134
135#ifndef HAVE_DIRNAME
136char *dirname(const char *path);
137#endif
138
Damien Millera7058ec2008-05-20 08:57:06 +1000139#ifndef HAVE_FMT_SCALED
140#define FMT_SCALED_STRSIZE 7
141int fmt_scaled(long long number, char *result);
142#endif
143
Darren Tuckere194ba42013-05-16 20:47:31 +1000144#ifndef HAVE_SCAN_SCALED
145int scan_scaled(char *, long long *);
146#endif
147
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000148#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
149char *inet_ntoa(struct in_addr in);
150#endif
151
152#ifndef HAVE_INET_NTOP
Damien Miller3e6fe872011-09-23 11:16:09 +1000153const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000154#endif
155
156#ifndef HAVE_INET_ATON
157int inet_aton(const char *cp, struct in_addr *addr);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000158#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000159
160#ifndef HAVE_STRSEP
161char *strsep(char **stringp, const char *delim);
162#endif
163
164#ifndef HAVE_SETPROCTITLE
165void setproctitle(const char *fmt, ...);
166void compat_init_setproctitle(int argc, char *argv[]);
167#endif
168
169#ifndef HAVE_GETGROUPLIST
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000170int getgrouplist(const char *, gid_t, gid_t *, int *);
171#endif
172
173#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
174int BSDgetopt(int argc, char * const *argv, const char *opts);
Darren Tucker0abfb552013-05-10 18:08:49 +1000175#include "openbsd-compat/getopt.h"
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000176#endif
177
Darren Tucker2eb40412018-02-24 21:06:48 +1100178#if ((defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0) || \
179 (defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0))
Darren Tuckered0b5922006-09-03 22:44:49 +1000180# include <sys/types.h>
181# include <sys/uio.h>
Darren Tucker2eb40412018-02-24 21:06:48 +1100182
183# if defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0
184int readv(int, struct iovec *, int);
185# endif
186
187# if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
Darren Tuckered0b5922006-09-03 22:44:49 +1000188int writev(int, struct iovec *, int);
Darren Tucker2eb40412018-02-24 21:06:48 +1100189# endif
Darren Tuckered0b5922006-09-03 22:44:49 +1000190#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000191
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +0000192/* Home grown routines */
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +0000193#include "bsd-misc.h"
Darren Tuckerf96ff182012-11-05 17:04:37 +1100194#include "bsd-setres_id.h"
Darren Tuckerfbfa6f92018-02-11 21:25:11 +1300195#include "bsd-signal.h"
Darren Tucker598eaa62008-06-09 03:32:29 +1000196#include "bsd-statvfs.h"
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +0000197#include "bsd-waitpid.h"
Darren Tuckerfebf0f52007-06-25 22:15:12 +1000198#include "bsd-poll.h"
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +0000199
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000200#ifndef HAVE_GETPEEREID
201int getpeereid(int , uid_t *, gid_t *);
Darren Tuckerccdb9be2013-11-08 18:54:38 +1100202#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000203
Darren Tuckerccdb9be2013-11-08 18:54:38 +1100204#ifdef HAVE_ARC4RANDOM
205# ifndef HAVE_ARC4RANDOM_STIR
206# define arc4random_stir()
207# endif
208#else
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000209unsigned int arc4random(void);
210void arc4random_stir(void);
211#endif /* !HAVE_ARC4RANDOM */
212
Damien Millera4be7c22008-05-19 14:47:37 +1000213#ifndef HAVE_ARC4RANDOM_BUF
214void arc4random_buf(void *, size_t);
215#endif
216
217#ifndef HAVE_ARC4RANDOM_UNIFORM
218u_int32_t arc4random_uniform(u_int32_t);
219#endif
220
Damien Miller57f39152005-11-24 19:58:19 +1100221#ifndef HAVE_ASPRINTF
222int asprintf(char **, const char *, ...);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000223#endif
Damien Miller57f39152005-11-24 19:58:19 +1100224
Damien Millerf4da3bb2004-01-21 17:07:16 +1100225#ifndef HAVE_OPENPTY
Darren Tuckerdc6118e2006-03-15 22:25:54 +1100226# include <sys/ioctl.h> /* for struct winsize */
Damien Millerf4da3bb2004-01-21 17:07:16 +1100227int openpty(int *, int *, char *, struct termios *, struct winsize *);
228#endif /* HAVE_OPENPTY */
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000229
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000230#ifndef HAVE_SNPRINTF
Darren Tucker66c32d52006-06-30 10:51:32 +1000231int snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000232#endif
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000233
Darren Tucker6e422112005-09-30 09:55:49 +1000234#ifndef HAVE_STRTOLL
235long long strtoll(const char *, char **, int);
236#endif
237
Darren Tuckerf32db832013-02-15 12:20:41 +1100238#ifndef HAVE_STRTOUL
239unsigned long strtoul(const char *, char **, int);
240#endif
241
242#ifndef HAVE_STRTOULL
243unsigned long long strtoull(const char *, char **, int);
244#endif
245
Damien Millerde3cb0a2005-05-26 20:48:25 +1000246#ifndef HAVE_STRTONUM
247long long strtonum(const char *, long long, long long, const char **);
248#endif
249
Darren Tuckerae133d42013-06-06 08:30:20 +1000250/* multibyte character support */
251#ifndef HAVE_MBLEN
Darren Tuckera647b9b2015-05-08 11:07:27 +1000252# define mblen(x, y) (1)
Darren Tuckerae133d42013-06-06 08:30:20 +1000253#endif
254
Darren Tuckera2333582016-07-14 10:59:09 +1000255#ifndef HAVE_WCWIDTH
256# define wcwidth(x) (((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
257/* force our no-op nl_langinfo and mbtowc */
258# undef HAVE_NL_LANGINFO
259# undef HAVE_MBTOWC
260# undef HAVE_LANGINFO_H
261#endif
262
263#ifndef HAVE_NL_LANGINFO
264# define nl_langinfo(x) ""
265#endif
266
267#ifndef HAVE_MBTOWC
268int mbtowc(wchar_t *, const char*, size_t);
269#endif
270
Darren Tucker5998ed02006-07-12 23:10:33 +1000271#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
272# include <stdarg.h>
273#endif
274
Darren Tucker5abfb152016-07-15 14:48:30 +1000275/*
276 * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
277 * instead. This is known to be the case on at least some configurations of
278 * AIX with the xlc compiler.
279 */
280#ifndef VA_COPY
281# ifdef HAVE_VA_COPY
282# define VA_COPY(dest, src) va_copy(dest, src)
283# else
284# ifdef HAVE___VA_COPY
285# define VA_COPY(dest, src) __va_copy(dest, src)
286# else
287# define VA_COPY(dest, src) (dest) = (src)
288# endif
289# endif
290#endif
291
Damien Miller57f39152005-11-24 19:58:19 +1100292#ifndef HAVE_VASPRINTF
293int vasprintf(char **, const char *, va_list);
294#endif
295
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000296#ifndef HAVE_VSNPRINTF
297int vsnprintf(char *, size_t, const char *, va_list);
298#endif
299
Darren Tucker2563e3f2010-01-16 11:53:07 +1100300#ifndef HAVE_USER_FROM_UID
Darren Tucker69371b52010-01-16 13:30:30 +1100301char *user_from_uid(uid_t, int);
Darren Tucker2563e3f2010-01-16 11:53:07 +1100302#endif
303
Darren Tucker4e218552010-01-16 23:58:37 +1100304#ifndef HAVE_GROUP_FROM_GID
Darren Tucker69371b52010-01-16 13:30:30 +1100305char *group_from_gid(gid_t, int);
Darren Tucker2563e3f2010-01-16 11:53:07 +1100306#endif
307
Damien Milleraa180632010-10-07 21:25:27 +1100308#ifndef HAVE_TIMINGSAFE_BCMP
309int timingsafe_bcmp(const void *, const void *, size_t);
310#endif
311
Damien Miller1ff130d2013-12-07 11:51:51 +1100312#ifndef HAVE_BCRYPT_PBKDF
313int bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
314 u_int8_t *, size_t, unsigned int);
315#endif
316
Damien Miller1d2c4562014-02-04 11:18:20 +1100317#ifndef HAVE_EXPLICIT_BZERO
318void explicit_bzero(void *p, size_t n);
319#endif
320
Damien Millerb7956912017-09-19 12:29:23 +1000321#ifndef HAVE_FREEZERO
322void freezero(void *, size_t);
323#endif
324
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000325char *xcrypt(const char *password, const char *salt);
326char *shadow_pw(struct passwd *pw);
327
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +0000328/* rfc2553 socket API replacements */
Damien Millerc28e38d2003-06-05 18:52:47 +1000329#include "fake-rfc2553.h"
Ben Lindstrom3c06f6a2001-01-31 21:52:01 +0000330
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000331/* Routines for a single OS platform */
Darren Tucker70a08cd2003-05-04 10:41:20 +1000332#include "bsd-cygwin_util.h"
Damien Miller1b06dc32006-08-31 03:24:41 +1000333
334#include "port-aix.h"
Ben Lindstromf095a852002-02-19 20:02:48 +0000335#include "port-irix.h"
Damien Miller73b42d22006-04-22 21:26:08 +1000336#include "port-linux.h"
Damien Miller1b06dc32006-08-31 03:24:41 +1000337#include "port-solaris.h"
Damien Millerf5594f92017-10-25 13:13:57 +1100338#include "port-net.h"
Damien Miller1b06dc32006-08-31 03:24:41 +1000339#include "port-uw.h"
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000340
Damien Miller703b98a2014-10-01 09:43:07 +1000341/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
342#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
343# include <features.h>
344# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
345# if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
346# include <sys/socket.h> /* Ensure include guard is defined */
347# undef FD_SET
348# undef FD_ISSET
349# define FD_SET(n, set) kludge_FD_SET(n, set)
350# define FD_ISSET(n, set) kludge_FD_ISSET(n, set)
351void kludge_FD_SET(int, fd_set *);
352int kludge_FD_ISSET(int, fd_set *);
353# endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
354# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
355#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
356
Ben Lindstrom515d0f92003-08-29 16:59:52 +0000357#endif /* _OPENBSD_COMPAT_H */