openbsd-compat.h revision 323129
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
39323129Sdes#include <stddef.h>  /* for wchar_t */
40323129Sdes
4198937Sdes/* OpenBSD function replacements */
4298937Sdes#include "base64.h"
4398937Sdes#include "sigact.h"
4498937Sdes#include "readpassphrase.h"
45113908Sdes#include "vis.h"
46124208Sdes#include "getrrsetbyname.h"
47294332Sdes#include "sha1.h"
48162852Sdes#include "sha2.h"
49294332Sdes#include "rmd160.h"
50294332Sdes#include "md5.h"
51261320Sdes#include "blf.h"
5298937Sdes
53124208Sdes#ifndef HAVE_BASENAME
54124208Sdeschar *basename(const char *path);
55124208Sdes#endif
56124208Sdes
57124208Sdes#ifndef HAVE_BINDRESVPORT_SA
58124208Sdesint bindresvport_sa(int sd, struct sockaddr *sa);
59124208Sdes#endif
60124208Sdes
61137015Sdes#ifndef HAVE_CLOSEFROM
62137015Sdesvoid closefrom(int);
63137015Sdes#endif
64137015Sdes
65124208Sdes#ifndef HAVE_GETCWD
66124208Sdeschar *getcwd(char *pt, size_t size);
67124208Sdes#endif
68124208Sdes
69294332Sdes#ifndef HAVE_REALLOCARRAY
70294332Sdesvoid *reallocarray(void *, size_t, size_t);
71294332Sdes#endif
72294332Sdes
73124208Sdes#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
74294464Sdes/*
75294464Sdes * glibc's FORTIFY_SOURCE can redefine this and prevent us picking up the
76294464Sdes * compat version.
77294464Sdes */
78294464Sdes# ifdef BROKEN_REALPATH
79294464Sdes#  define realpath(x, y) _ssh_compat_realpath(x, y)
80294464Sdes# endif
81294464Sdes
82124208Sdeschar *realpath(const char *path, char *resolved);
83294464Sdes#endif
84124208Sdes
85124208Sdes#ifndef HAVE_RRESVPORT_AF
86124208Sdesint rresvport_af(int *alport, sa_family_t af);
87124208Sdes#endif
88124208Sdes
89124208Sdes#ifndef HAVE_STRLCPY
90124208Sdes/* #include <sys/types.h> XXX Still needed? */
91124208Sdessize_t strlcpy(char *dst, const char *src, size_t siz);
92124208Sdes#endif
93124208Sdes
94124208Sdes#ifndef HAVE_STRLCAT
95124208Sdes/* #include <sys/types.h> XXX Still needed? */
96124208Sdessize_t strlcat(char *dst, const char *src, size_t siz);
97124208Sdes#endif
98124208Sdes
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);
116124208Sdes#endif
117124208Sdes
118124208Sdes#ifndef HAVE_DAEMON
119124208Sdesint daemon(int nochdir, int noclose);
120124208Sdes#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);
145124208Sdes#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
157124208Sdes/* #include <grp.h> XXXX Still needed ? */
158124208Sdesint getgrouplist(const char *, gid_t, gid_t *, int *);
159124208Sdes#endif
160124208Sdes
161124208Sdes#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
162124208Sdesint BSDgetopt(int argc, char * const *argv, const char *opts);
163255767Sdes#include "openbsd-compat/getopt.h"
164124208Sdes#endif
165124208Sdes
166162852Sdes#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
167162852Sdes# include <sys/types.h>
168162852Sdes# include <sys/uio.h>
169162852Sdesint writev(int, struct iovec *, int);
170162852Sdes#endif
171124208Sdes
17298937Sdes/* Home grown routines */
17398937Sdes#include "bsd-misc.h"
174248619Sdes#include "bsd-setres_id.h"
175181111Sdes#include "bsd-statvfs.h"
17698937Sdes#include "bsd-waitpid.h"
177181111Sdes#include "bsd-poll.h"
17898937Sdes
179124208Sdes#ifndef HAVE_GETPEEREID
180124208Sdesint getpeereid(int , uid_t *, gid_t *);
181261320Sdes#endif
182124208Sdes
183261320Sdes#ifdef HAVE_ARC4RANDOM
184261320Sdes# ifndef HAVE_ARC4RANDOM_STIR
185261320Sdes#  define arc4random_stir()
186261320Sdes# endif
187261320Sdes#else
188124208Sdesunsigned int arc4random(void);
189124208Sdesvoid arc4random_stir(void);
190124208Sdes#endif /* !HAVE_ARC4RANDOM */
191124208Sdes
192181111Sdes#ifndef HAVE_ARC4RANDOM_BUF
193181111Sdesvoid arc4random_buf(void *, size_t);
194181111Sdes#endif
195181111Sdes
196181111Sdes#ifndef HAVE_ARC4RANDOM_UNIFORM
197181111Sdesu_int32_t arc4random_uniform(u_int32_t);
198181111Sdes#endif
199181111Sdes
200157016Sdes#ifndef HAVE_ASPRINTF
201157016Sdesint asprintf(char **, const char *, ...);
202157016Sdes#endif
203157016Sdes
204126274Sdes#ifndef HAVE_OPENPTY
205162852Sdes# include <sys/ioctl.h>	/* for struct winsize */
206126274Sdesint openpty(int *, int *, char *, struct termios *, struct winsize *);
207126274Sdes#endif /* HAVE_OPENPTY */
208124208Sdes
209124208Sdes/* #include <sys/types.h> XXX needed? For size_t */
210124208Sdes
211124208Sdes#ifndef HAVE_SNPRINTF
212162852Sdesint snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
213124208Sdes#endif
214124208Sdes
215157016Sdes#ifndef HAVE_STRTOLL
216157016Sdeslong long strtoll(const char *, char **, int);
217157016Sdes#endif
218157016Sdes
219248619Sdes#ifndef HAVE_STRTOUL
220248619Sdesunsigned long strtoul(const char *, char **, int);
221248619Sdes#endif
222248619Sdes
223248619Sdes#ifndef HAVE_STRTOULL
224248619Sdesunsigned long long strtoull(const char *, char **, int);
225248619Sdes#endif
226248619Sdes
227149749Sdes#ifndef HAVE_STRTONUM
228149749Sdeslong long strtonum(const char *, long long, long long, const char **);
229149749Sdes#endif
230149749Sdes
231255767Sdes/* multibyte character support */
232255767Sdes#ifndef HAVE_MBLEN
233294336Sdes# define mblen(x, y)	(1)
234255767Sdes#endif
235255767Sdes
236323129Sdes#ifndef HAVE_WCWIDTH
237323129Sdes# define wcwidth(x)	(((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
238323129Sdes/* force our no-op nl_langinfo and mbtowc */
239323129Sdes# undef HAVE_NL_LANGINFO
240323129Sdes# undef HAVE_MBTOWC
241323129Sdes# undef HAVE_LANGINFO_H
242323129Sdes#endif
243323129Sdes
244323129Sdes#ifndef HAVE_NL_LANGINFO
245323129Sdes# define nl_langinfo(x)	""
246323129Sdes#endif
247323129Sdes
248323129Sdes#ifndef HAVE_MBTOWC
249323129Sdesint mbtowc(wchar_t *, const char*, size_t);
250323129Sdes#endif
251323129Sdes
252162852Sdes#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
253162852Sdes# include <stdarg.h>
254162852Sdes#endif
255162852Sdes
256323129Sdes/*
257323129Sdes * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
258323129Sdes * instead.  This is known to be the case on at least some configurations of
259323129Sdes * AIX with the xlc compiler.
260323129Sdes */
261323129Sdes#ifndef VA_COPY
262323129Sdes# ifdef HAVE_VA_COPY
263323129Sdes#  define VA_COPY(dest, src) va_copy(dest, src)
264323129Sdes# else
265323129Sdes#  ifdef HAVE___VA_COPY
266323129Sdes#   define VA_COPY(dest, src) __va_copy(dest, src)
267323129Sdes#  else
268323129Sdes#   define VA_COPY(dest, src) (dest) = (src)
269323129Sdes#  endif
270323129Sdes# endif
271323129Sdes#endif
272323129Sdes
273157016Sdes#ifndef HAVE_VASPRINTF
274157016Sdesint vasprintf(char **, const char *, va_list);
275157016Sdes#endif
276157016Sdes
277124208Sdes#ifndef HAVE_VSNPRINTF
278124208Sdesint vsnprintf(char *, size_t, const char *, va_list);
279124208Sdes#endif
280124208Sdes
281204917Sdes#ifndef HAVE_USER_FROM_UID
282204917Sdeschar *user_from_uid(uid_t, int);
283204917Sdes#endif
284204917Sdes
285204917Sdes#ifndef HAVE_GROUP_FROM_GID
286204917Sdeschar *group_from_gid(gid_t, int);
287204917Sdes#endif
288204917Sdes
289221420Sdes#ifndef HAVE_TIMINGSAFE_BCMP
290221420Sdesint timingsafe_bcmp(const void *, const void *, size_t);
291221420Sdes#endif
292221420Sdes
293261320Sdes#ifndef HAVE_BCRYPT_PBKDF
294261320Sdesint	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
295261320Sdes    u_int8_t *, size_t, unsigned int);
296261320Sdes#endif
297261320Sdes
298263712Sdes#ifndef HAVE_EXPLICIT_BZERO
299263712Sdesvoid explicit_bzero(void *p, size_t n);
300263712Sdes#endif
301263712Sdes
302124208Sdesvoid *xmmap(size_t size);
303124208Sdeschar *xcrypt(const char *password, const char *salt);
304124208Sdeschar *shadow_pw(struct passwd *pw);
305124208Sdes
30698937Sdes/* rfc2553 socket API replacements */
307124208Sdes#include "fake-rfc2553.h"
30898937Sdes
30998937Sdes/* Routines for a single OS platform */
31098937Sdes#include "bsd-cray.h"
311124208Sdes#include "bsd-cygwin_util.h"
312162852Sdes
313162852Sdes#include "port-aix.h"
31498937Sdes#include "port-irix.h"
315162852Sdes#include "port-linux.h"
316162852Sdes#include "port-solaris.h"
317162852Sdes#include "port-tun.h"
318149749Sdes#include "port-uw.h"
31998937Sdes
320294328Sdes/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
321294328Sdes#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
322294328Sdes# include <features.h>
323294328Sdes# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
324294328Sdes#  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
325294328Sdes#   include <sys/socket.h>  /* Ensure include guard is defined */
326294328Sdes#   undef FD_SET
327294328Sdes#   undef FD_ISSET
328294328Sdes#   define FD_SET(n, set)	kludge_FD_SET(n, set)
329294328Sdes#   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
330294328Sdesvoid kludge_FD_SET(int, fd_set *);
331294328Sdesint kludge_FD_ISSET(int, fd_set *);
332294328Sdes#  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
333294328Sdes# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
334294328Sdes#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
335294328Sdes
336124208Sdes#endif /* _OPENBSD_COMPAT_H */
337