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