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