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