1124208Sdes/*
2124208Sdes * Copyright (c) 1999-2003 Damien Miller.  All rights reserved.
3124208Sdes * Copyright (c) 2003 Ben Lindstrom. All rights reserved.
4124208Sdes * Copyright (c) 2002 Tim Rice.  All rights reserved.
5124208Sdes *
6124208Sdes * Redistribution and use in source and binary forms, with or without
7124208Sdes * modification, are permitted provided that the following conditions
8124208Sdes * are met:
9124208Sdes * 1. Redistributions of source code must retain the above copyright
10124208Sdes *    notice, this list of conditions and the following disclaimer.
11124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
12124208Sdes *    notice, this list of conditions and the following disclaimer in the
13124208Sdes *    documentation and/or other materials provided with the distribution.
14124208Sdes *
15124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25124208Sdes */
2698937Sdes
27124208Sdes#ifndef _OPENBSD_COMPAT_H
28124208Sdes#define _OPENBSD_COMPAT_H
2998937Sdes
30124208Sdes#include "includes.h"
31124208Sdes
32162852Sdes#include <sys/types.h>
33162852Sdes#include <pwd.h>
34162852Sdes
35162852Sdes#include <sys/socket.h>
36162852Sdes
37323129Sdes#include <stddef.h>  /* for wchar_t */
38323129Sdes
3998937Sdes/* OpenBSD function replacements */
4098937Sdes#include "base64.h"
4198937Sdes#include "sigact.h"
4298937Sdes#include "readpassphrase.h"
43113908Sdes#include "vis.h"
44124208Sdes#include "getrrsetbyname.h"
45294332Sdes#include "sha1.h"
46162852Sdes#include "sha2.h"
47294332Sdes#include "rmd160.h"
48294332Sdes#include "md5.h"
49261320Sdes#include "blf.h"
5098937Sdes
51124208Sdes#ifndef HAVE_BASENAME
52124208Sdeschar *basename(const char *path);
53124208Sdes#endif
54124208Sdes
55124208Sdes#ifndef HAVE_BINDRESVPORT_SA
56124208Sdesint bindresvport_sa(int sd, struct sockaddr *sa);
57124208Sdes#endif
58124208Sdes
59137015Sdes#ifndef HAVE_CLOSEFROM
60137015Sdesvoid closefrom(int);
61137015Sdes#endif
62137015Sdes
63124208Sdes#ifndef HAVE_GETCWD
64124208Sdeschar *getcwd(char *pt, size_t size);
65323134Sdes#endif
66124208Sdes
67294332Sdes#ifndef HAVE_REALLOCARRAY
68294332Sdesvoid *reallocarray(void *, size_t, size_t);
69294332Sdes#endif
70294332Sdes
71124208Sdes#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
72294464Sdes/*
73294464Sdes * glibc's FORTIFY_SOURCE can redefine this and prevent us picking up the
74294464Sdes * compat version.
75294464Sdes */
76294464Sdes# ifdef BROKEN_REALPATH
77294464Sdes#  define realpath(x, y) _ssh_compat_realpath(x, y)
78294464Sdes# endif
79294464Sdes
80124208Sdeschar *realpath(const char *path, char *resolved);
81294464Sdes#endif
82124208Sdes
83124208Sdes#ifndef HAVE_RRESVPORT_AF
84124208Sdesint rresvport_af(int *alport, sa_family_t af);
85124208Sdes#endif
86124208Sdes
87124208Sdes#ifndef HAVE_STRLCPY
88124208Sdessize_t strlcpy(char *dst, const char *src, size_t siz);
89124208Sdes#endif
90124208Sdes
91124208Sdes#ifndef HAVE_STRLCAT
92124208Sdessize_t strlcat(char *dst, const char *src, size_t siz);
93323134Sdes#endif
94124208Sdes
95323134Sdes#ifndef HAVE_STRCASESTR
96323134Sdeschar *strcasestr(const char *, const char *);
97323134Sdes#endif
98323134Sdes
99124208Sdes#ifndef HAVE_SETENV
100124208Sdesint setenv(register const char *name, register const char *value, int rewrite);
101124208Sdes#endif
102124208Sdes
103124208Sdes#ifndef HAVE_STRMODE
104124208Sdesvoid strmode(int mode, char *p);
105124208Sdes#endif
106124208Sdes
107215116Sdes#ifndef HAVE_STRPTIME
108215116Sdes#include  <time.h>
109215116Sdeschar *strptime(const char *buf, const char *fmt, struct tm *tm);
110215116Sdes#endif
111215116Sdes
112124208Sdes#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
113124208Sdesint mkstemps(char *path, int slen);
114124208Sdesint mkstemp(char *path);
115124208Sdeschar *mkdtemp(char *path);
116323134Sdes#endif
117124208Sdes
118124208Sdes#ifndef HAVE_DAEMON
119124208Sdesint daemon(int nochdir, int noclose);
120323134Sdes#endif
121124208Sdes
122124208Sdes#ifndef HAVE_DIRNAME
123124208Sdeschar *dirname(const char *path);
124124208Sdes#endif
125124208Sdes
126181111Sdes#ifndef HAVE_FMT_SCALED
127181111Sdes#define	FMT_SCALED_STRSIZE	7
128181111Sdesint	fmt_scaled(long long number, char *result);
129181111Sdes#endif
130181111Sdes
131255767Sdes#ifndef HAVE_SCAN_SCALED
132255767Sdesint	scan_scaled(char *, long long *);
133255767Sdes#endif
134255767Sdes
135124208Sdes#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
136124208Sdeschar *inet_ntoa(struct in_addr in);
137124208Sdes#endif
138124208Sdes
139124208Sdes#ifndef HAVE_INET_NTOP
140240075Sdesconst char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
141124208Sdes#endif
142124208Sdes
143124208Sdes#ifndef HAVE_INET_ATON
144124208Sdesint inet_aton(const char *cp, struct in_addr *addr);
145323134Sdes#endif
146124208Sdes
147124208Sdes#ifndef HAVE_STRSEP
148124208Sdeschar *strsep(char **stringp, const char *delim);
149124208Sdes#endif
150124208Sdes
151124208Sdes#ifndef HAVE_SETPROCTITLE
152124208Sdesvoid setproctitle(const char *fmt, ...);
153124208Sdesvoid compat_init_setproctitle(int argc, char *argv[]);
154124208Sdes#endif
155124208Sdes
156124208Sdes#ifndef HAVE_GETGROUPLIST
157124208Sdesint getgrouplist(const char *, gid_t, gid_t *, int *);
158124208Sdes#endif
159124208Sdes
160124208Sdes#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
161124208Sdesint BSDgetopt(int argc, char * const *argv, const char *opts);
162255767Sdes#include "openbsd-compat/getopt.h"
163124208Sdes#endif
164124208Sdes
165162852Sdes#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
166162852Sdes# include <sys/types.h>
167162852Sdes# include <sys/uio.h>
168162852Sdesint writev(int, struct iovec *, int);
169162852Sdes#endif
170124208Sdes
17198937Sdes/* Home grown routines */
17298937Sdes#include "bsd-misc.h"
173248619Sdes#include "bsd-setres_id.h"
174181111Sdes#include "bsd-statvfs.h"
17598937Sdes#include "bsd-waitpid.h"
176181111Sdes#include "bsd-poll.h"
17798937Sdes
178124208Sdes#ifndef HAVE_GETPEEREID
179124208Sdesint getpeereid(int , uid_t *, gid_t *);
180261320Sdes#endif
181124208Sdes
182261320Sdes#ifdef HAVE_ARC4RANDOM
183261320Sdes# ifndef HAVE_ARC4RANDOM_STIR
184261320Sdes#  define arc4random_stir()
185261320Sdes# endif
186261320Sdes#else
187124208Sdesunsigned int arc4random(void);
188124208Sdesvoid arc4random_stir(void);
189124208Sdes#endif /* !HAVE_ARC4RANDOM */
190124208Sdes
191181111Sdes#ifndef HAVE_ARC4RANDOM_BUF
192181111Sdesvoid arc4random_buf(void *, size_t);
193181111Sdes#endif
194181111Sdes
195181111Sdes#ifndef HAVE_ARC4RANDOM_UNIFORM
196181111Sdesu_int32_t arc4random_uniform(u_int32_t);
197181111Sdes#endif
198181111Sdes
199157016Sdes#ifndef HAVE_ASPRINTF
200157016Sdesint asprintf(char **, const char *, ...);
201323134Sdes#endif
202157016Sdes
203126274Sdes#ifndef HAVE_OPENPTY
204162852Sdes# include <sys/ioctl.h>	/* for struct winsize */
205126274Sdesint openpty(int *, int *, char *, struct termios *, struct winsize *);
206126274Sdes#endif /* HAVE_OPENPTY */
207124208Sdes
208124208Sdes#ifndef HAVE_SNPRINTF
209162852Sdesint snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
210323134Sdes#endif
211124208Sdes
212157016Sdes#ifndef HAVE_STRTOLL
213157016Sdeslong long strtoll(const char *, char **, int);
214157016Sdes#endif
215157016Sdes
216248619Sdes#ifndef HAVE_STRTOUL
217248619Sdesunsigned long strtoul(const char *, char **, int);
218248619Sdes#endif
219248619Sdes
220248619Sdes#ifndef HAVE_STRTOULL
221248619Sdesunsigned long long strtoull(const char *, char **, int);
222248619Sdes#endif
223248619Sdes
224149749Sdes#ifndef HAVE_STRTONUM
225149749Sdeslong long strtonum(const char *, long long, long long, const char **);
226149749Sdes#endif
227149749Sdes
228255767Sdes/* multibyte character support */
229255767Sdes#ifndef HAVE_MBLEN
230294336Sdes# define mblen(x, y)	(1)
231255767Sdes#endif
232255767Sdes
233323129Sdes#ifndef HAVE_WCWIDTH
234323129Sdes# define wcwidth(x)	(((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
235323129Sdes/* force our no-op nl_langinfo and mbtowc */
236323129Sdes# undef HAVE_NL_LANGINFO
237323129Sdes# undef HAVE_MBTOWC
238323129Sdes# undef HAVE_LANGINFO_H
239323129Sdes#endif
240323129Sdes
241323129Sdes#ifndef HAVE_NL_LANGINFO
242323129Sdes# define nl_langinfo(x)	""
243323129Sdes#endif
244323129Sdes
245323129Sdes#ifndef HAVE_MBTOWC
246323129Sdesint mbtowc(wchar_t *, const char*, size_t);
247323129Sdes#endif
248323129Sdes
249162852Sdes#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
250162852Sdes# include <stdarg.h>
251162852Sdes#endif
252162852Sdes
253323129Sdes/*
254323129Sdes * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
255323129Sdes * instead.  This is known to be the case on at least some configurations of
256323129Sdes * AIX with the xlc compiler.
257323129Sdes */
258323129Sdes#ifndef VA_COPY
259323129Sdes# ifdef HAVE_VA_COPY
260323129Sdes#  define VA_COPY(dest, src) va_copy(dest, src)
261323129Sdes# else
262323129Sdes#  ifdef HAVE___VA_COPY
263323129Sdes#   define VA_COPY(dest, src) __va_copy(dest, src)
264323129Sdes#  else
265323129Sdes#   define VA_COPY(dest, src) (dest) = (src)
266323129Sdes#  endif
267323129Sdes# endif
268323129Sdes#endif
269323129Sdes
270157016Sdes#ifndef HAVE_VASPRINTF
271157016Sdesint vasprintf(char **, const char *, va_list);
272157016Sdes#endif
273157016Sdes
274124208Sdes#ifndef HAVE_VSNPRINTF
275124208Sdesint vsnprintf(char *, size_t, const char *, va_list);
276124208Sdes#endif
277124208Sdes
278204917Sdes#ifndef HAVE_USER_FROM_UID
279204917Sdeschar *user_from_uid(uid_t, int);
280204917Sdes#endif
281204917Sdes
282204917Sdes#ifndef HAVE_GROUP_FROM_GID
283204917Sdeschar *group_from_gid(gid_t, int);
284204917Sdes#endif
285204917Sdes
286221420Sdes#ifndef HAVE_TIMINGSAFE_BCMP
287221420Sdesint timingsafe_bcmp(const void *, const void *, size_t);
288221420Sdes#endif
289221420Sdes
290261320Sdes#ifndef HAVE_BCRYPT_PBKDF
291261320Sdesint	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
292261320Sdes    u_int8_t *, size_t, unsigned int);
293261320Sdes#endif
294261320Sdes
295263712Sdes#ifndef HAVE_EXPLICIT_BZERO
296263712Sdesvoid explicit_bzero(void *p, size_t n);
297263712Sdes#endif
298263712Sdes
299124208Sdeschar *xcrypt(const char *password, const char *salt);
300124208Sdeschar *shadow_pw(struct passwd *pw);
301124208Sdes
30298937Sdes/* rfc2553 socket API replacements */
303124208Sdes#include "fake-rfc2553.h"
30498937Sdes
30598937Sdes/* Routines for a single OS platform */
30698937Sdes#include "bsd-cray.h"
307124208Sdes#include "bsd-cygwin_util.h"
308162852Sdes
309162852Sdes#include "port-aix.h"
31098937Sdes#include "port-irix.h"
311162852Sdes#include "port-linux.h"
312162852Sdes#include "port-solaris.h"
313162852Sdes#include "port-tun.h"
314149749Sdes#include "port-uw.h"
31598937Sdes
316294328Sdes/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
317294328Sdes#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
318294328Sdes# include <features.h>
319294328Sdes# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
320294328Sdes#  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
321294328Sdes#   include <sys/socket.h>  /* Ensure include guard is defined */
322294328Sdes#   undef FD_SET
323294328Sdes#   undef FD_ISSET
324294328Sdes#   define FD_SET(n, set)	kludge_FD_SET(n, set)
325294328Sdes#   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
326294328Sdesvoid kludge_FD_SET(int, fd_set *);
327294328Sdesint kludge_FD_ISSET(int, fd_set *);
328294328Sdes#  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
329294328Sdes# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
330294328Sdes#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
331294328Sdes
332124208Sdes#endif /* _OPENBSD_COMPAT_H */
333