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