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