xref: /freebsd/crypto/openssh/openbsd-compat/openbsd-compat.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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_GETLINE
64 ssize_t getline(char **, size_t *, FILE *);
65 #endif
66 
67 #ifndef HAVE_GETPAGESIZE
68 int getpagesize(void);
69 #endif
70 
71 #ifndef HAVE_GETCWD
72 char *getcwd(char *pt, size_t size);
73 #endif
74 
75 #ifndef HAVE_REALLOCARRAY
76 void *reallocarray(void *, size_t, size_t);
77 #endif
78 
79 #ifndef HAVE_RECALLOCARRAY
80 void *recallocarray(void *, size_t, size_t, size_t);
81 #endif
82 
83 #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH)
84 /*
85  * glibc's FORTIFY_SOURCE can redefine this and prevent us picking up the
86  * compat version.
87  */
88 # ifdef BROKEN_REALPATH
89 #  define realpath(x, y) _ssh_compat_realpath(x, y)
90 # endif
91 
92 char *realpath(const char *path, char *resolved);
93 #endif
94 
95 #ifndef HAVE_RRESVPORT_AF
96 int rresvport_af(int *alport, sa_family_t af);
97 #endif
98 
99 #ifndef HAVE_STRLCPY
100 size_t strlcpy(char *dst, const char *src, size_t siz);
101 #endif
102 
103 #ifndef HAVE_STRLCAT
104 size_t strlcat(char *dst, const char *src, size_t siz);
105 #endif
106 
107 #ifndef HAVE_STRCASESTR
108 char *strcasestr(const char *, const char *);
109 #endif
110 
111 #ifndef HAVE_SETENV
112 int setenv(register const char *name, register const char *value, int rewrite);
113 #endif
114 
115 #ifndef HAVE_STRMODE
116 void strmode(int mode, char *p);
117 #endif
118 
119 #ifndef HAVE_STRPTIME
120 #include  <time.h>
121 char *strptime(const char *buf, const char *fmt, struct tm *tm);
122 #endif
123 
124 #if !defined(HAVE_MKDTEMP) || defined(HAVE_STRICT_MKSTEMP)
125 int mkstemps(char *path, int slen);
126 int mkstemp(char *path);
127 char *mkdtemp(char *path);
128 #endif
129 
130 #ifndef HAVE_DAEMON
131 int daemon(int nochdir, int noclose);
132 #endif
133 
134 #ifndef HAVE_DIRNAME
135 char *dirname(const char *path);
136 #endif
137 
138 #ifndef HAVE_FMT_SCALED
139 #define	FMT_SCALED_STRSIZE	7
140 int	fmt_scaled(long long number, char *result);
141 #endif
142 
143 #ifndef HAVE_SCAN_SCALED
144 int	scan_scaled(char *, long long *);
145 #endif
146 
147 #if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
148 char *inet_ntoa(struct in_addr in);
149 #endif
150 
151 #ifndef HAVE_INET_NTOP
152 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
153 #endif
154 
155 #ifndef HAVE_INET_ATON
156 int inet_aton(const char *cp, struct in_addr *addr);
157 #endif
158 
159 #ifndef HAVE_STRSEP
160 char *strsep(char **stringp, const char *delim);
161 #endif
162 
163 #ifndef HAVE_SETPROCTITLE
164 void setproctitle(const char *fmt, ...);
165 void compat_init_setproctitle(int argc, char *argv[]);
166 #endif
167 
168 #ifndef HAVE_GETGROUPLIST
169 int getgrouplist(const char *, gid_t, gid_t *, int *);
170 #endif
171 
172 #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
173 int BSDgetopt(int argc, char * const *argv, const char *opts);
174 #include "openbsd-compat/getopt.h"
175 #endif
176 
177 #if ((defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0) || \
178     (defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0))
179 # include <sys/types.h>
180 # include <sys/uio.h>
181 
182 # if defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0
183 int readv(int, struct iovec *, int);
184 # endif
185 
186 # if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
187 int writev(int, struct iovec *, int);
188 # endif
189 #endif
190 
191 /* Home grown routines */
192 #include "bsd-misc.h"
193 #include "bsd-setres_id.h"
194 #include "bsd-signal.h"
195 #include "bsd-statvfs.h"
196 #include "bsd-waitpid.h"
197 #include "bsd-poll.h"
198 
199 #ifndef HAVE_GETPEEREID
200 int getpeereid(int , uid_t *, gid_t *);
201 #endif
202 
203 #ifdef HAVE_ARC4RANDOM
204 # ifndef HAVE_ARC4RANDOM_STIR
205 #  define arc4random_stir()
206 # endif
207 #else
208 unsigned int arc4random(void);
209 void arc4random_stir(void);
210 #endif /* !HAVE_ARC4RANDOM */
211 
212 #ifndef HAVE_ARC4RANDOM_BUF
213 void arc4random_buf(void *, size_t);
214 #endif
215 
216 #ifndef HAVE_ARC4RANDOM_UNIFORM
217 u_int32_t arc4random_uniform(u_int32_t);
218 #endif
219 
220 #ifndef HAVE_ASPRINTF
221 int asprintf(char **, const char *, ...);
222 #endif
223 
224 #ifndef HAVE_OPENPTY
225 # include <sys/ioctl.h>	/* for struct winsize */
226 int openpty(int *, int *, char *, struct termios *, struct winsize *);
227 #endif /* HAVE_OPENPTY */
228 
229 #ifndef HAVE_SNPRINTF
230 int snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
231 #endif
232 
233 #ifndef HAVE_STRTOLL
234 long long strtoll(const char *, char **, int);
235 #endif
236 
237 #ifndef HAVE_STRTOUL
238 unsigned long strtoul(const char *, char **, int);
239 #endif
240 
241 #ifndef HAVE_STRTOULL
242 unsigned long long strtoull(const char *, char **, int);
243 #endif
244 
245 #ifndef HAVE_STRTONUM
246 long long strtonum(const char *, long long, long long, const char **);
247 #endif
248 
249 /* multibyte character support */
250 #ifndef HAVE_MBLEN
251 # define mblen(x, y)	(1)
252 #endif
253 
254 #ifndef HAVE_WCWIDTH
255 # define wcwidth(x)	(((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
256 /* force our no-op nl_langinfo and mbtowc */
257 # undef HAVE_NL_LANGINFO
258 # undef HAVE_MBTOWC
259 # undef HAVE_LANGINFO_H
260 #endif
261 
262 #ifndef HAVE_NL_LANGINFO
263 # define nl_langinfo(x)	""
264 #endif
265 
266 #ifndef HAVE_MBTOWC
267 int mbtowc(wchar_t *, const char*, size_t);
268 #endif
269 
270 #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
271 # include <stdarg.h>
272 #endif
273 
274 /*
275  * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
276  * instead.  This is known to be the case on at least some configurations of
277  * AIX with the xlc compiler.
278  */
279 #ifndef VA_COPY
280 # ifdef HAVE_VA_COPY
281 #  define VA_COPY(dest, src) va_copy(dest, src)
282 # else
283 #  ifdef HAVE___VA_COPY
284 #   define VA_COPY(dest, src) __va_copy(dest, src)
285 #  else
286 #   define VA_COPY(dest, src) (dest) = (src)
287 #  endif
288 # endif
289 #endif
290 
291 #ifndef HAVE_VASPRINTF
292 int vasprintf(char **, const char *, va_list);
293 #endif
294 
295 #ifndef HAVE_VSNPRINTF
296 int vsnprintf(char *, size_t, const char *, va_list);
297 #endif
298 
299 #ifndef HAVE_USER_FROM_UID
300 char *user_from_uid(uid_t, int);
301 #endif
302 
303 #ifndef HAVE_GROUP_FROM_GID
304 char *group_from_gid(gid_t, int);
305 #endif
306 
307 #ifndef HAVE_TIMINGSAFE_BCMP
308 int timingsafe_bcmp(const void *, const void *, size_t);
309 #endif
310 
311 #ifndef HAVE_BCRYPT_PBKDF
312 int	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
313     u_int8_t *, size_t, unsigned int);
314 #endif
315 
316 #ifndef HAVE_EXPLICIT_BZERO
317 void explicit_bzero(void *p, size_t n);
318 #endif
319 
320 #ifndef HAVE_FREEZERO
321 void freezero(void *, size_t);
322 #endif
323 
324 char *xcrypt(const char *password, const char *salt);
325 char *shadow_pw(struct passwd *pw);
326 
327 /* rfc2553 socket API replacements */
328 #include "fake-rfc2553.h"
329 
330 /* Routines for a single OS platform */
331 #include "bsd-cygwin_util.h"
332 
333 #include "port-aix.h"
334 #include "port-irix.h"
335 #include "port-linux.h"
336 #include "port-solaris.h"
337 #include "port-net.h"
338 #include "port-uw.h"
339 
340 /* _FORTIFY_SOURCE breaks FD_ISSET(n)/FD_SET(n) for n > FD_SETSIZE. Avoid. */
341 #if defined(HAVE_FEATURES_H) && defined(_FORTIFY_SOURCE)
342 # include <features.h>
343 # if defined(__GNU_LIBRARY__) && defined(__GLIBC_PREREQ)
344 #  if __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0)
345 #   include <sys/socket.h>  /* Ensure include guard is defined */
346 #   undef FD_SET
347 #   undef FD_ISSET
348 #   define FD_SET(n, set)	kludge_FD_SET(n, set)
349 #   define FD_ISSET(n, set)	kludge_FD_ISSET(n, set)
350 void kludge_FD_SET(int, fd_set *);
351 int kludge_FD_ISSET(int, fd_set *);
352 #  endif /* __GLIBC_PREREQ(2, 15) && (_FORTIFY_SOURCE > 0) */
353 # endif /* __GNU_LIBRARY__ && __GLIBC_PREREQ */
354 #endif /* HAVE_FEATURES_H && _FORTIFY_SOURCE */
355 
356 #endif /* _OPENBSD_COMPAT_H */
357