xref: /freebsd/crypto/openssh/openbsd-compat/openbsd-compat.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 #define mkstemp(x) _ssh_mkstemp(x)
145 int _ssh_mkstemp(char *);
146 
147 #ifndef HAVE_DAEMON
148 int daemon(int nochdir, int noclose);
149 #endif
150 
151 #ifndef HAVE_DIRNAME
152 char *dirname(const char *path);
153 #endif
154 
155 #ifndef HAVE_FMT_SCALED
156 #define	FMT_SCALED_STRSIZE	7
157 int	fmt_scaled(long long number, char *result);
158 #endif
159 
160 #ifndef HAVE_SCAN_SCALED
161 int	scan_scaled(char *, long long *);
162 #endif
163 
164 #if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
165 char *inet_ntoa(struct in_addr in);
166 #endif
167 
168 #ifndef HAVE_INET_NTOP
169 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
170 #endif
171 
172 #ifndef HAVE_INET_ATON
173 int inet_aton(const char *cp, struct in_addr *addr);
174 #endif
175 
176 #ifndef HAVE_STRSEP
177 char *strsep(char **stringp, const char *delim);
178 #endif
179 
180 #ifndef HAVE_SETPROCTITLE
181 void setproctitle(const char *fmt, ...);
182 void compat_init_setproctitle(int argc, char *argv[]);
183 #endif
184 
185 #ifndef HAVE_GETGROUPLIST
186 int getgrouplist(const char *, gid_t, gid_t *, int *);
187 #endif
188 
189 #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
190 int BSDgetopt(int argc, char * const *argv, const char *opts);
191 #include "openbsd-compat/getopt.h"
192 #endif
193 
194 #if ((defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0) || \
195     (defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0))
196 # include <sys/types.h>
197 # include <sys/uio.h>
198 
199 # if defined(HAVE_DECL_READV) && HAVE_DECL_READV == 0
200 int readv(int, struct iovec *, int);
201 # endif
202 
203 # if defined(HAVE_DECL_WRITEV) && HAVE_DECL_WRITEV == 0
204 int writev(int, struct iovec *, int);
205 # endif
206 #endif
207 
208 /* Home grown routines */
209 #include "bsd-signal.h"
210 #include "bsd-misc.h"
211 #include "bsd-setres_id.h"
212 #include "bsd-statvfs.h"
213 #include "bsd-waitpid.h"
214 #include "bsd-poll.h"
215 
216 #if defined(HAVE_DECL_GETPEEREID) && HAVE_DECL_GETPEEREID == 0
217 int getpeereid(int , uid_t *, gid_t *);
218 #endif
219 
220 #ifndef HAVE_ARC4RANDOM
221 uint32_t arc4random(void);
222 #endif /* !HAVE_ARC4RANDOM */
223 
224 #ifndef HAVE_ARC4RANDOM_BUF
225 void arc4random_buf(void *, size_t);
226 #endif
227 
228 #ifndef HAVE_ARC4RANDOM_STIR
229 # define arc4random_stir()
230 #endif
231 
232 #ifndef HAVE_ARC4RANDOM_UNIFORM
233 uint32_t arc4random_uniform(uint32_t);
234 #endif
235 
236 #ifndef HAVE_ASPRINTF
237 int asprintf(char **, const char *, ...);
238 #endif
239 
240 #ifndef HAVE_OPENPTY
241 # include <sys/ioctl.h>	/* for struct winsize */
242 int openpty(int *, int *, char *, struct termios *, struct winsize *);
243 #endif /* HAVE_OPENPTY */
244 
245 #ifndef HAVE_SNPRINTF
246 int snprintf(char *, size_t, SNPRINTF_CONST char *, ...);
247 #endif
248 
249 #ifndef HAVE_STRTOLL
250 long long strtoll(const char *, char **, int);
251 #endif
252 
253 #ifndef HAVE_STRTOUL
254 unsigned long strtoul(const char *, char **, int);
255 #endif
256 
257 #ifndef HAVE_STRTOULL
258 unsigned long long strtoull(const char *, char **, int);
259 #endif
260 
261 #ifndef HAVE_STRTONUM
262 long long strtonum(const char *, long long, long long, const char **);
263 #endif
264 
265 /* multibyte character support */
266 #ifndef HAVE_MBLEN
267 # define mblen(x, y)	(1)
268 #endif
269 
270 #ifndef HAVE_WCWIDTH
271 # define wcwidth(x)	(((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1)
272 /* force our no-op nl_langinfo and mbtowc */
273 # undef HAVE_NL_LANGINFO
274 # undef HAVE_MBTOWC
275 # undef HAVE_LANGINFO_H
276 #endif
277 
278 #ifndef HAVE_NL_LANGINFO
279 # define nl_langinfo(x)	""
280 #endif
281 
282 #ifndef HAVE_MBTOWC
283 int mbtowc(wchar_t *, const char*, size_t);
284 #endif
285 
286 #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF)
287 # include <stdarg.h>
288 #endif
289 
290 /*
291  * Some platforms unconditionally undefine va_copy() so we define VA_COPY()
292  * instead.  This is known to be the case on at least some configurations of
293  * AIX with the xlc compiler.
294  */
295 #ifndef VA_COPY
296 # ifdef HAVE_VA_COPY
297 #  define VA_COPY(dest, src) va_copy(dest, src)
298 # else
299 #  ifdef HAVE___VA_COPY
300 #   define VA_COPY(dest, src) __va_copy(dest, src)
301 #  else
302 #   define VA_COPY(dest, src) (dest) = (src)
303 #  endif
304 # endif
305 #endif
306 
307 #ifndef HAVE_VASPRINTF
308 int vasprintf(char **, const char *, va_list);
309 #endif
310 
311 #ifndef HAVE_VSNPRINTF
312 int vsnprintf(char *, size_t, const char *, va_list);
313 #endif
314 
315 #ifndef HAVE_USER_FROM_UID
316 char *user_from_uid(uid_t, int);
317 #endif
318 
319 #ifndef HAVE_GROUP_FROM_GID
320 char *group_from_gid(gid_t, int);
321 #endif
322 
323 #ifndef HAVE_TIMINGSAFE_BCMP
324 int timingsafe_bcmp(const void *, const void *, size_t);
325 #endif
326 
327 #ifndef HAVE_BCRYPT_PBKDF
328 int	bcrypt_pbkdf(const char *, size_t, const uint8_t *, size_t,
329     uint8_t *, size_t, unsigned int);
330 #endif
331 
332 #ifndef HAVE_EXPLICIT_BZERO
333 void explicit_bzero(void *p, size_t n);
334 #endif
335 
336 #ifndef HAVE_FREEZERO
337 void freezero(void *, size_t);
338 #endif
339 
340 #ifndef HAVE_LOCALTIME_R
341 struct tm *localtime_r(const time_t *, struct tm *);
342 #endif
343 
344 #ifndef HAVE_TIMEGM
345 #include <time.h>
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