1295367Sdes/* $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
39323124Sdes#include <stddef.h>  /* for wchar_t */
40323124Sdes
4198937Sdes/* OpenBSD function replacements */
4298937Sdes#include "base64.h"
4398937Sdes#include "sigact.h"
4498937Sdes#include "readpassphrase.h"
45113908Sdes#include "vis.h"
46124208Sdes#include "getrrsetbyname.h"
47295367Sdes#include "sha1.h"
48162852Sdes#include "sha2.h"
49295367Sdes#include "rmd160.h"
50295367Sdes#include "md5.h"
51262566Sdes#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
69295367Sdes#ifndef HAVE_REALLOCARRAY
70295367Sdesvoid *reallocarray(void *, size_t, size_t);
71295367Sdes#endif
72295367Sdes
73124208Sdes#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
74295367Sdes/*
75295367Sdes * glibc's FORTIFY_SOURCE can redefine this and prevent us picking up the
76295367Sdes * compat version.
77295367Sdes */
78295367Sdes# ifdef BROKEN_REALPATH
79295367Sdes#  define realpath(x, y) _ssh_compat_realpath(x, y)
80295367Sdes# endif
81295367Sdes
82124208Sdeschar *realpath(const char *path, char *resolved);
83295367Sdes#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 *);
181262566Sdes#endif
182124208Sdes
183262566Sdes#ifdef HAVE_ARC4RANDOM
184262566Sdes# ifndef HAVE_ARC4RANDOM_STIR
185262566Sdes#  define arc4random_stir()
186262566Sdes# endif
187262566Sdes#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
233295367Sdes# define mblen(x, y)	(1)
234255767Sdes#endif
235255767Sdes
236323124Sdes#ifndef HAVE_WCWIDTH
237323124Sdes# define wcwidth(x)	(((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
238323124Sdes/* force our no-op nl_langinfo and mbtowc */
239323124Sdes# undef HAVE_NL_LANGINFO
240323124Sdes# undef HAVE_MBTOWC
241323124Sdes# undef HAVE_LANGINFO_H
242323124Sdes#endif
243323124Sdes
244323124Sdes#ifndef HAVE_NL_LANGINFO
245323124Sdes# define nl_langinfo(x)	""
246323124Sdes#endif
247323124Sdes
248323124Sdes#ifndef HAVE_MBTOWC
249323124Sdesint mbtowc(wchar_t *, const char*, size_t);
250323124Sdes#endif
251323124Sdes
252162852Sdes#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
253162852Sdes# include <stdarg.h>
254162852Sdes#endif
255162852Sdes
256323124Sdes/*
257323124Sdes * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
258323124Sdes * instead.  This is known to be the case on at least some configurations of
259323124Sdes * AIX with the xlc compiler.
260323124Sdes */
261323124Sdes#ifndef VA_COPY
262323124Sdes# ifdef HAVE_VA_COPY
263323124Sdes#  define VA_COPY(dest, src) va_copy(dest, src)
264323124Sdes# else
265323124Sdes#  ifdef HAVE___VA_COPY
266323124Sdes#   define VA_COPY(dest, src) __va_copy(dest, src)
267323124Sdes#  else
268323124Sdes#   define VA_COPY(dest, src) (dest) = (src)
269323124Sdes#  endif
270323124Sdes# endif
271323124Sdes#endif
272323124Sdes
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
293262566Sdes#ifndef HAVE_BCRYPT_PBKDF
294262566Sdesint	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
295262566Sdes    u_int8_t *, size_t, unsigned int);
296262566Sdes#endif
297262566Sdes
298264377Sdes#ifndef HAVE_EXPLICIT_BZERO
299264377Sdesvoid explicit_bzero(void *p, size_t n);
300264377Sdes#endif
301264377Sdes
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
320295367Sdes/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
321295367Sdes#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
322295367Sdes# include <features.h>
323295367Sdes# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
324295367Sdes#  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
325295367Sdes#   include <sys/socket.h>  /* Ensure include guard is defined */
326295367Sdes#   undef FD_SET
327295367Sdes#   undef FD_ISSET
328295367Sdes#   define FD_SET(n, set)	kludge_FD_SET(n, set)
329295367Sdes#   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
330295367Sdesvoid kludge_FD_SET(int, fd_set *);
331295367Sdesint kludge_FD_ISSET(int, fd_set *);
332295367Sdes#  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
333295367Sdes# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
334295367Sdes#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
335295367Sdes
336124208Sdes#endif /* _OPENBSD_COMPAT_H */
337