1 /*
2  * Copyright (c) 1999-2003 Damien Miller.  All rights reserved.
3  * Copyright (c) 2003 Ben Lindstrom. All rights reserved.
4  * Copyright (c) 2002 Tim Rice.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _OPENBSD_COMPAT_H
28 #define _OPENBSD_COMPAT_H
29 
30 #include "includes.h"
31 
32 #include <sys/types.h>
33 #include <pwd.h>
34 
35 #include <sys/socket.h>
36 
37 #include <stddef.h>  /* for wchar_t */
38 
39 /* OpenBSD function replacements */
40 #include "base64.h"
41 #include "sigact.h"
42 #include "readpassphrase.h"
43 #include "vis.h"
44 #include "getrrsetbyname.h"
45 #include "sha1.h"
46 #include "sha2.h"
47 #include "rmd160.h"
48 #include "md5.h"
49 #include "blf.h"
50 
51 #ifndef HAVE_BASENAME
52 char *basename(const char *path);
53 #endif
54 
55 #ifndef HAVE_BINDRESVPORT_SA
56 int bindresvport_sa(int sd, struct sockaddr *sa);
57 #endif
58 
59 #ifndef HAVE_CLOSEFROM
60 void closefrom(int);
61 #endif
62 
63 #ifndef HAVE_GETCWD
64 char *getcwd(char *pt, size_t size);
65 #endif
66 
67 #ifndef HAVE_REALLOCARRAY
68 void *reallocarray(void *, size_t, size_t);
69 #endif
70 
71 #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
72 /*
73  * glibc's FORTIFY_SOURCE can redefine this and prevent us picking up the
74  * compat version.
75  */
76 # ifdef BROKEN_REALPATH
77 #  define realpath(x, y) _ssh_compat_realpath(x, y)
78 # endif
79 
80 char *realpath(const char *path, char *resolved);
81 #endif
82 
83 #ifndef HAVE_RRESVPORT_AF
84 int rresvport_af(int *alport, sa_family_t af);
85 #endif
86 
87 #ifndef HAVE_STRLCPY
88 size_t strlcpy(char *dst, const char *src, size_t siz);
89 #endif
90 
91 #ifndef HAVE_STRLCAT
92 size_t strlcat(char *dst, const char *src, size_t siz);
93 #endif
94 
95 #ifndef HAVE_STRCASESTR
96 char *strcasestr(const char *, const char *);
97 #endif
98 
99 #ifndef HAVE_SETENV
100 int setenv(register const char *name, register const char *value, int rewrite);
101 #endif
102 
103 #ifndef HAVE_STRMODE
104 void strmode(int mode, char *p);
105 #endif
106 
107 #ifndef HAVE_STRPTIME
108 #include  <time.h>
109 char *strptime(const char *buf, const char *fmt, struct tm *tm);
110 #endif
111 
112 #if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
113 int mkstemps(char *path, int slen);
114 int mkstemp(char *path);
115 char *mkdtemp(char *path);
116 #endif
117 
118 #ifndef HAVE_DAEMON
119 int daemon(int nochdir, int noclose);
120 #endif
121 
122 #ifndef HAVE_DIRNAME
123 char *dirname(const char *path);
124 #endif
125 
126 #ifndef HAVE_FMT_SCALED
127 #define	FMT_SCALED_STRSIZE	7
128 int	fmt_scaled(long long number, char *result);
129 #endif
130 
131 #ifndef HAVE_SCAN_SCALED
132 int	scan_scaled(char *, long long *);
133 #endif
134 
135 #if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
136 char *inet_ntoa(struct in_addr in);
137 #endif
138 
139 #ifndef HAVE_INET_NTOP
140 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
141 #endif
142 
143 #ifndef HAVE_INET_ATON
144 int inet_aton(const char *cp, struct in_addr *addr);
145 #endif
146 
147 #ifndef HAVE_STRSEP
148 char *strsep(char **stringp, const char *delim);
149 #endif
150 
151 #ifndef HAVE_SETPROCTITLE
152 void setproctitle(const char *fmt, ...);
153 void compat_init_setproctitle(int argc, char *argv[]);
154 #endif
155 
156 #ifndef HAVE_GETGROUPLIST
157 int getgrouplist(const char *, gid_t, gid_t *, int *);
158 #endif
159 
160 #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
161 int BSDgetopt(int argc, char * const *argv, const char *opts);
162 #include "openbsd-compat/getopt.h"
163 #endif
164 
165 #if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
166 # include <sys/types.h>
167 # include <sys/uio.h>
168 int writev(int, struct iovec *, int);
169 #endif
170 
171 /* Home grown routines */
172 #include "bsd-misc.h"
173 #include "bsd-setres_id.h"
174 #include "bsd-statvfs.h"
175 #include "bsd-waitpid.h"
176 #include "bsd-poll.h"
177 
178 #ifndef HAVE_GETPEEREID
179 int getpeereid(int , uid_t *, gid_t *);
180 #endif
181 
182 #ifdef HAVE_ARC4RANDOM
183 # ifndef HAVE_ARC4RANDOM_STIR
184 #  define arc4random_stir()
185 # endif
186 #else
187 unsigned int arc4random(void);
188 void arc4random_stir(void);
189 #endif /* !HAVE_ARC4RANDOM */
190 
191 #ifndef HAVE_ARC4RANDOM_BUF
192 void arc4random_buf(void *, size_t);
193 #endif
194 
195 #ifndef HAVE_ARC4RANDOM_UNIFORM
196 u_int32_t arc4random_uniform(u_int32_t);
197 #endif
198 
199 #ifndef HAVE_ASPRINTF
200 int asprintf(char **, const char *, ...);
201 #endif
202 
203 #ifndef HAVE_OPENPTY
204 # include <sys/ioctl.h>	/* for struct winsize */
205 int openpty(int *, int *, char *, struct termios *, struct winsize *);
206 #endif /* HAVE_OPENPTY */
207 
208 #ifndef HAVE_SNPRINTF
209 int snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
210 #endif
211 
212 #ifndef HAVE_STRTOLL
213 long long strtoll(const char *, char **, int);
214 #endif
215 
216 #ifndef HAVE_STRTOUL
217 unsigned long strtoul(const char *, char **, int);
218 #endif
219 
220 #ifndef HAVE_STRTOULL
221 unsigned long long strtoull(const char *, char **, int);
222 #endif
223 
224 #ifndef HAVE_STRTONUM
225 long long strtonum(const char *, long long, long long, const char **);
226 #endif
227 
228 /* multibyte character support */
229 #ifndef HAVE_MBLEN
230 # define mblen(x, y)	(1)
231 #endif
232 
233 #ifndef HAVE_WCWIDTH
234 # define wcwidth(x)	(((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
235 /* force our no-op nl_langinfo and mbtowc */
236 # undef HAVE_NL_LANGINFO
237 # undef HAVE_MBTOWC
238 # undef HAVE_LANGINFO_H
239 #endif
240 
241 #ifndef HAVE_NL_LANGINFO
242 # define nl_langinfo(x)	""
243 #endif
244 
245 #ifndef HAVE_MBTOWC
246 int mbtowc(wchar_t *, const char*, size_t);
247 #endif
248 
249 #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
250 # include <stdarg.h>
251 #endif
252 
253 /*
254  * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
255  * instead.  This is known to be the case on at least some configurations of
256  * AIX with the xlc compiler.
257  */
258 #ifndef VA_COPY
259 # ifdef HAVE_VA_COPY
260 #  define VA_COPY(dest, src) va_copy(dest, src)
261 # else
262 #  ifdef HAVE___VA_COPY
263 #   define VA_COPY(dest, src) __va_copy(dest, src)
264 #  else
265 #   define VA_COPY(dest, src) (dest) = (src)
266 #  endif
267 # endif
268 #endif
269 
270 #ifndef HAVE_VASPRINTF
271 int vasprintf(char **, const char *, va_list);
272 #endif
273 
274 #ifndef HAVE_VSNPRINTF
275 int vsnprintf(char *, size_t, const char *, va_list);
276 #endif
277 
278 #ifndef HAVE_USER_FROM_UID
279 char *user_from_uid(uid_t, int);
280 #endif
281 
282 #ifndef HAVE_GROUP_FROM_GID
283 char *group_from_gid(gid_t, int);
284 #endif
285 
286 #ifndef HAVE_TIMINGSAFE_BCMP
287 int timingsafe_bcmp(const void *, const void *, size_t);
288 #endif
289 
290 #ifndef HAVE_BCRYPT_PBKDF
291 int	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
292     u_int8_t *, size_t, unsigned int);
293 #endif
294 
295 #ifndef HAVE_EXPLICIT_BZERO
296 void explicit_bzero(void *p, size_t n);
297 #endif
298 
299 char *xcrypt(const char *password, const char *salt);
300 char *shadow_pw(struct passwd *pw);
301 
302 /* rfc2553 socket API replacements */
303 #include "fake-rfc2553.h"
304 
305 /* Routines for a single OS platform */
306 #include "bsd-cray.h"
307 #include "bsd-cygwin_util.h"
308 
309 #include "port-aix.h"
310 #include "port-irix.h"
311 #include "port-linux.h"
312 #include "port-solaris.h"
313 #include "port-tun.h"
314 #include "port-uw.h"
315 
316 /* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
317 #if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
318 # include <features.h>
319 # if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
320 #  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
321 #   include <sys/socket.h>  /* Ensure include guard is defined */
322 #   undef FD_SET
323 #   undef FD_ISSET
324 #   define FD_SET(n, set)	kludge_FD_SET(n, set)
325 #   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
326 void kludge_FD_SET(int, fd_set *);
327 int kludge_FD_ISSET(int, fd_set *);
328 #  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
329 # endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
330 #endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
331 
332 #endif /* _OPENBSD_COMPAT_H */
333