openbsd-compat.h revision 294332
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)
73124208Sdeschar *realpath(const char *path, char *resolved);
74124208Sdes#endif
75124208Sdes
76124208Sdes#ifndef HAVE_RRESVPORT_AF
77124208Sdesint rresvport_af(int *alport, sa_family_t af);
78124208Sdes#endif
79124208Sdes
80124208Sdes#ifndef HAVE_STRLCPY
81124208Sdes/* #include <sys/types.h> XXX Still needed? */
82124208Sdessize_t strlcpy(char *dst, const char *src, size_t siz);
83124208Sdes#endif
84124208Sdes
85124208Sdes#ifndef HAVE_STRLCAT
86124208Sdes/* #include <sys/types.h> XXX Still needed? */
87124208Sdessize_t strlcat(char *dst, const char *src, size_t siz);
88124208Sdes#endif
89124208Sdes
90124208Sdes#ifndef HAVE_SETENV
91124208Sdesint setenv(register const char *name, register const char *value, int rewrite);
92124208Sdes#endif
93124208Sdes
94124208Sdes#ifndef HAVE_STRMODE
95124208Sdesvoid strmode(int mode, char *p);
96124208Sdes#endif
97124208Sdes
98215116Sdes#ifndef HAVE_STRPTIME
99215116Sdes#include  <time.h>
100215116Sdeschar *strptime(const char *buf, const char *fmt, struct tm *tm);
101215116Sdes#endif
102215116Sdes
103124208Sdes#if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
104124208Sdesint mkstemps(char *path, int slen);
105124208Sdesint mkstemp(char *path);
106124208Sdeschar *mkdtemp(char *path);
107124208Sdes#endif
108124208Sdes
109124208Sdes#ifndef HAVE_DAEMON
110124208Sdesint daemon(int nochdir, int noclose);
111124208Sdes#endif
112124208Sdes
113124208Sdes#ifndef HAVE_DIRNAME
114124208Sdeschar *dirname(const char *path);
115124208Sdes#endif
116124208Sdes
117181111Sdes#ifndef HAVE_FMT_SCALED
118181111Sdes#define	FMT_SCALED_STRSIZE	7
119181111Sdesint	fmt_scaled(long long number, char *result);
120181111Sdes#endif
121181111Sdes
122255767Sdes#ifndef HAVE_SCAN_SCALED
123255767Sdesint	scan_scaled(char *, long long *);
124255767Sdes#endif
125255767Sdes
126124208Sdes#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
127124208Sdeschar *inet_ntoa(struct in_addr in);
128124208Sdes#endif
129124208Sdes
130124208Sdes#ifndef HAVE_INET_NTOP
131240075Sdesconst char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
132124208Sdes#endif
133124208Sdes
134124208Sdes#ifndef HAVE_INET_ATON
135124208Sdesint inet_aton(const char *cp, struct in_addr *addr);
136124208Sdes#endif
137124208Sdes
138124208Sdes#ifndef HAVE_STRSEP
139124208Sdeschar *strsep(char **stringp, const char *delim);
140124208Sdes#endif
141124208Sdes
142124208Sdes#ifndef HAVE_SETPROCTITLE
143124208Sdesvoid setproctitle(const char *fmt, ...);
144124208Sdesvoid compat_init_setproctitle(int argc, char *argv[]);
145124208Sdes#endif
146124208Sdes
147124208Sdes#ifndef HAVE_GETGROUPLIST
148124208Sdes/* #include <grp.h> XXXX Still needed ? */
149124208Sdesint getgrouplist(const char *, gid_t, gid_t *, int *);
150124208Sdes#endif
151124208Sdes
152124208Sdes#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
153124208Sdesint BSDgetopt(int argc, char * const *argv, const char *opts);
154255767Sdes#include "openbsd-compat/getopt.h"
155124208Sdes#endif
156124208Sdes
157162852Sdes#if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
158162852Sdes# include <sys/types.h>
159162852Sdes# include <sys/uio.h>
160162852Sdesint writev(int, struct iovec *, int);
161162852Sdes#endif
162124208Sdes
16398937Sdes/* Home grown routines */
16498937Sdes#include "bsd-misc.h"
165248619Sdes#include "bsd-setres_id.h"
166181111Sdes#include "bsd-statvfs.h"
16798937Sdes#include "bsd-waitpid.h"
168181111Sdes#include "bsd-poll.h"
16998937Sdes
170124208Sdes#ifndef HAVE_GETPEEREID
171124208Sdesint getpeereid(int , uid_t *, gid_t *);
172261320Sdes#endif
173124208Sdes
174261320Sdes#ifdef HAVE_ARC4RANDOM
175261320Sdes# ifndef HAVE_ARC4RANDOM_STIR
176261320Sdes#  define arc4random_stir()
177261320Sdes# endif
178261320Sdes#else
179124208Sdesunsigned int arc4random(void);
180124208Sdesvoid arc4random_stir(void);
181124208Sdes#endif /* !HAVE_ARC4RANDOM */
182124208Sdes
183181111Sdes#ifndef HAVE_ARC4RANDOM_BUF
184181111Sdesvoid arc4random_buf(void *, size_t);
185181111Sdes#endif
186181111Sdes
187181111Sdes#ifndef HAVE_ARC4RANDOM_UNIFORM
188181111Sdesu_int32_t arc4random_uniform(u_int32_t);
189181111Sdes#endif
190181111Sdes
191157016Sdes#ifndef HAVE_ASPRINTF
192157016Sdesint asprintf(char **, const char *, ...);
193157016Sdes#endif
194157016Sdes
195126274Sdes#ifndef HAVE_OPENPTY
196162852Sdes# include <sys/ioctl.h>	/* for struct winsize */
197126274Sdesint openpty(int *, int *, char *, struct termios *, struct winsize *);
198126274Sdes#endif /* HAVE_OPENPTY */
199124208Sdes
200124208Sdes/* #include <sys/types.h> XXX needed? For size_t */
201124208Sdes
202124208Sdes#ifndef HAVE_SNPRINTF
203162852Sdesint snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
204124208Sdes#endif
205124208Sdes
206157016Sdes#ifndef HAVE_STRTOLL
207157016Sdeslong long strtoll(const char *, char **, int);
208157016Sdes#endif
209157016Sdes
210248619Sdes#ifndef HAVE_STRTOUL
211248619Sdesunsigned long strtoul(const char *, char **, int);
212248619Sdes#endif
213248619Sdes
214248619Sdes#ifndef HAVE_STRTOULL
215248619Sdesunsigned long long strtoull(const char *, char **, int);
216248619Sdes#endif
217248619Sdes
218149749Sdes#ifndef HAVE_STRTONUM
219149749Sdeslong long strtonum(const char *, long long, long long, const char **);
220149749Sdes#endif
221149749Sdes
222255767Sdes/* multibyte character support */
223255767Sdes#ifndef HAVE_MBLEN
224255767Sdes# define mblen(x, y)	1
225255767Sdes#endif
226255767Sdes
227162852Sdes#if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
228162852Sdes# include <stdarg.h>
229162852Sdes#endif
230162852Sdes
231157016Sdes#ifndef HAVE_VASPRINTF
232157016Sdesint vasprintf(char **, const char *, va_list);
233157016Sdes#endif
234157016Sdes
235124208Sdes#ifndef HAVE_VSNPRINTF
236124208Sdesint vsnprintf(char *, size_t, const char *, va_list);
237124208Sdes#endif
238124208Sdes
239204917Sdes#ifndef HAVE_USER_FROM_UID
240204917Sdeschar *user_from_uid(uid_t, int);
241204917Sdes#endif
242204917Sdes
243204917Sdes#ifndef HAVE_GROUP_FROM_GID
244204917Sdeschar *group_from_gid(gid_t, int);
245204917Sdes#endif
246204917Sdes
247221420Sdes#ifndef HAVE_TIMINGSAFE_BCMP
248221420Sdesint timingsafe_bcmp(const void *, const void *, size_t);
249221420Sdes#endif
250221420Sdes
251261320Sdes#ifndef HAVE_BCRYPT_PBKDF
252261320Sdesint	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
253261320Sdes    u_int8_t *, size_t, unsigned int);
254261320Sdes#endif
255261320Sdes
256263712Sdes#ifndef HAVE_EXPLICIT_BZERO
257263712Sdesvoid explicit_bzero(void *p, size_t n);
258263712Sdes#endif
259263712Sdes
260124208Sdesvoid *xmmap(size_t size);
261124208Sdeschar *xcrypt(const char *password, const char *salt);
262124208Sdeschar *shadow_pw(struct passwd *pw);
263124208Sdes
26498937Sdes/* rfc2553 socket API replacements */
265124208Sdes#include "fake-rfc2553.h"
26698937Sdes
26798937Sdes/* Routines for a single OS platform */
26898937Sdes#include "bsd-cray.h"
269124208Sdes#include "bsd-cygwin_util.h"
270162852Sdes
271162852Sdes#include "port-aix.h"
27298937Sdes#include "port-irix.h"
273162852Sdes#include "port-linux.h"
274162852Sdes#include "port-solaris.h"
275162852Sdes#include "port-tun.h"
276149749Sdes#include "port-uw.h"
27798937Sdes
278294328Sdes/* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
279294328Sdes#if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
280294328Sdes# include <features.h>
281294328Sdes# if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
282294328Sdes#  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
283294328Sdes#   include <sys/socket.h>  /* Ensure include guard is defined */
284294328Sdes#   undef FD_SET
285294328Sdes#   undef FD_ISSET
286294328Sdes#   define FD_SET(n, set)	kludge_FD_SET(n, set)
287294328Sdes#   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
288294328Sdesvoid kludge_FD_SET(int, fd_set *);
289294328Sdesint kludge_FD_ISSET(int, fd_set *);
290294328Sdes#  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
291294328Sdes# endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
292294328Sdes#endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
293294328Sdes
294124208Sdes#endif /* _OPENBSD_COMPAT_H */
295