openbsd-compat.h revision 294464
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"
46294332Sdes#include "sha1.h"
47162852Sdes#include "sha2.h"
48294332Sdes#include "rmd160.h"
49294332Sdes#include "md5.h"
50261320Sdes#include "blf.h"
5198937Sdes
52124208Sdes#ifndef HAVE_BASENAME
53124208Sdeschar *basename(const char *path);
54124208Sdes#endif
55124208Sdes
56124208Sdes#ifndef HAVE_BINDRESVPORT_SA
57124208Sdesint bindresvport_sa(int sd, struct sockaddr *sa);
58124208Sdes#endif
59124208Sdes
60137015Sdes#ifndef HAVE_CLOSEFROM
61137015Sdesvoid closefrom(int);
62137015Sdes#endif
63137015Sdes
64124208Sdes#ifndef HAVE_GETCWD
65124208Sdeschar *getcwd(char *pt, size_t size);
66124208Sdes#endif
67124208Sdes
68294332Sdes#ifndef HAVE_REALLOCARRAY
69294332Sdesvoid *reallocarray(void *, size_t, size_t);
70294332Sdes#endif
71294332Sdes
72124208Sdes#if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
73294464Sdes/*
74294464Sdes * glibc's FORTIFY_SOURCE can redefine this and prevent us picking up the
75294464Sdes * compat version.
76294464Sdes */
77294464Sdes# ifdef BROKEN_REALPATH
78294464Sdes#  define realpath(x, y) _ssh_compat_realpath(x, y)
79294464Sdes# endif
80294464Sdes
81124208Sdeschar *realpath(const char *path, char *resolved);
82294464Sdes#endif
83124208Sdes
84124208Sdes#ifndef HAVE_RRESVPORT_AF
85124208Sdesint rresvport_af(int *alport, sa_family_t af);
86124208Sdes#endif
87124208Sdes
88124208Sdes#ifndef HAVE_STRLCPY
89124208Sdes/* #include <sys/types.h> XXX Still needed? */
90124208Sdessize_t strlcpy(char *dst, const char *src, size_t siz);
91124208Sdes#endif
92124208Sdes
93124208Sdes#ifndef HAVE_STRLCAT
94124208Sdes/* #include <sys/types.h> XXX Still needed? */
95124208Sdessize_t strlcat(char *dst, const char *src, size_t siz);
96124208Sdes#endif
97124208Sdes
98124208Sdes#ifndef HAVE_SETENV
99124208Sdesint setenv(register const char *name, register const char *value, int rewrite);
100124208Sdes#endif
101124208Sdes
102124208Sdes#ifndef HAVE_STRMODE
103124208Sdesvoid strmode(int mode, char *p);
104124208Sdes#endif
105124208Sdes
106215116Sdes#ifndef HAVE_STRPTIME
107215116Sdes#include  <time.h>
108215116Sdeschar *strptime(const char *buf, const char *fmt, struct tm *tm);
109215116Sdes#endif
110215116Sdes
111124208Sdes#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
112124208Sdesint mkstemps(char *path, int slen);
113124208Sdesint mkstemp(char *path);
114124208Sdeschar *mkdtemp(char *path);
115124208Sdes#endif
116124208Sdes
117124208Sdes#ifndef HAVE_DAEMON
118124208Sdesint daemon(int nochdir, int noclose);
119124208Sdes#endif
120124208Sdes
121124208Sdes#ifndef HAVE_DIRNAME
122124208Sdeschar *dirname(const char *path);
123124208Sdes#endif
124124208Sdes
125181111Sdes#ifndef HAVE_FMT_SCALED
126181111Sdes#define	FMT_SCALED_STRSIZE	7
127181111Sdesint	fmt_scaled(long long number, char *result);
128181111Sdes#endif
129181111Sdes
130255767Sdes#ifndef HAVE_SCAN_SCALED
131255767Sdesint	scan_scaled(char *, long long *);
132255767Sdes#endif
133255767Sdes
134124208Sdes#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
135124208Sdeschar *inet_ntoa(struct in_addr in);
136124208Sdes#endif
137124208Sdes
138124208Sdes#ifndef HAVE_INET_NTOP
139240075Sdesconst char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
140124208Sdes#endif
141124208Sdes
142124208Sdes#ifndef HAVE_INET_ATON
143124208Sdesint inet_aton(const char *cp, struct in_addr *addr);
144124208Sdes#endif
145124208Sdes
146124208Sdes#ifndef HAVE_STRSEP
147124208Sdeschar *strsep(char **stringp, const char *delim);
148124208Sdes#endif
149124208Sdes
150124208Sdes#ifndef HAVE_SETPROCTITLE
151124208Sdesvoid setproctitle(const char *fmt, ...);
152124208Sdesvoid compat_init_setproctitle(int argc, char *argv[]);
153124208Sdes#endif
154124208Sdes
155124208Sdes#ifndef HAVE_GETGROUPLIST
156124208Sdes/* #include <grp.h> XXXX Still needed ? */
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 *, ...);
201157016Sdes#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/* #include <sys/types.h> XXX needed? For size_t */
209124208Sdes
210124208Sdes#ifndef HAVE_SNPRINTF
211162852Sdesint snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
212124208Sdes#endif
213124208Sdes
214157016Sdes#ifndef HAVE_STRTOLL
215157016Sdeslong long strtoll(const char *, char **, int);
216157016Sdes#endif
217157016Sdes
218248619Sdes#ifndef HAVE_STRTOUL
219248619Sdesunsigned long strtoul(const char *, char **, int);
220248619Sdes#endif
221248619Sdes
222248619Sdes#ifndef HAVE_STRTOULL
223248619Sdesunsigned long long strtoull(const char *, char **, int);
224248619Sdes#endif
225248619Sdes
226149749Sdes#ifndef HAVE_STRTONUM
227149749Sdeslong long strtonum(const char *, long long, long long, const char **);
228149749Sdes#endif
229149749Sdes
230255767Sdes/* multibyte character support */
231255767Sdes#ifndef HAVE_MBLEN
232294336Sdes# define mblen(x, y)	(1)
233255767Sdes#endif
234255767Sdes
235162852Sdes#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
236162852Sdes# include <stdarg.h>
237162852Sdes#endif
238162852Sdes
239157016Sdes#ifndef HAVE_VASPRINTF
240157016Sdesint vasprintf(char **, const char *, va_list);
241157016Sdes#endif
242157016Sdes
243124208Sdes#ifndef HAVE_VSNPRINTF
244124208Sdesint vsnprintf(char *, size_t, const char *, va_list);
245124208Sdes#endif
246124208Sdes
247204917Sdes#ifndef HAVE_USER_FROM_UID
248204917Sdeschar *user_from_uid(uid_t, int);
249204917Sdes#endif
250204917Sdes
251204917Sdes#ifndef HAVE_GROUP_FROM_GID
252204917Sdeschar *group_from_gid(gid_t, int);
253204917Sdes#endif
254204917Sdes
255221420Sdes#ifndef HAVE_TIMINGSAFE_BCMP
256221420Sdesint timingsafe_bcmp(const void *, const void *, size_t);
257221420Sdes#endif
258221420Sdes
259261320Sdes#ifndef HAVE_BCRYPT_PBKDF
260261320Sdesint	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
261261320Sdes    u_int8_t *, size_t, unsigned int);
262261320Sdes#endif
263261320Sdes
264263712Sdes#ifndef HAVE_EXPLICIT_BZERO
265263712Sdesvoid explicit_bzero(void *p, size_t n);
266263712Sdes#endif
267263712Sdes
268124208Sdesvoid *xmmap(size_t size);
269124208Sdeschar *xcrypt(const char *password, const char *salt);
270124208Sdeschar *shadow_pw(struct passwd *pw);
271124208Sdes
27298937Sdes/* rfc2553 socket API replacements */
273124208Sdes#include "fake-rfc2553.h"
27498937Sdes
27598937Sdes/* Routines for a single OS platform */
27698937Sdes#include "bsd-cray.h"
277124208Sdes#include "bsd-cygwin_util.h"
278162852Sdes
279162852Sdes#include "port-aix.h"
28098937Sdes#include "port-irix.h"
281162852Sdes#include "port-linux.h"
282162852Sdes#include "port-solaris.h"
283162852Sdes#include "port-tun.h"
284149749Sdes#include "port-uw.h"
28598937Sdes
286294328Sdes/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
287294328Sdes#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
288294328Sdes# include <features.h>
289294328Sdes# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
290294328Sdes#  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
291294328Sdes#   include <sys/socket.h>  /* Ensure include guard is defined */
292294328Sdes#   undef FD_SET
293294328Sdes#   undef FD_ISSET
294294328Sdes#   define FD_SET(n, set)	kludge_FD_SET(n, set)
295294328Sdes#   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
296294328Sdesvoid kludge_FD_SET(int, fd_set *);
297294328Sdesint kludge_FD_ISSET(int, fd_set *);
298294328Sdes#  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
299294328Sdes# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
300294328Sdes#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
301294328Sdes
302124208Sdes#endif /* _OPENBSD_COMPAT_H */
303