Blob


1 /*
2 * Copyright (c) 2023 Thomas Adam <thomas@xteddy.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #ifndef _GOT_COMPAT_H
18 #define _GOT_COMPAT_H
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/uio.h>
23 #if defined(__FreeBSD__)
24 #include <sys/endian.h>
25 #include <sys/capsicum.h>
26 #elif defined(__APPLE__)
27 #include <machine/endian.h>
28 #include <libkern/OSByteOrder.h>
29 #include "compat/bsd-poll.h"
31 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
33 #define htobe16(x) OSSwapHostToBigInt16(x)
34 #define htole16(x) OSSwapHostToLittleInt16(x)
35 #define be16toh(x) OSSwapBigToHostInt16(x)
36 #define le16toh(x) OSSwapLittleToHostInt16(x)
38 #define htobe32(x) OSSwapHostToBigInt32(x)
39 #define htole32(x) OSSwapHostToLittleInt32(x)
40 #define be32toh(x) OSSwapBigToHostInt32(x)
41 #define le32toh(x) OSSwapLittleToHostInt32(x)
43 #define htobe64(x) OSSwapHostToBigInt64(x)
44 #define htole64(x) OSSwapHostToLittleInt64(x)
45 #define be64toh(x) OSSwapBigToHostInt64(x)
46 #define le64toh(x) OSSwapLittleToHostInt64(x)
48 #define st_atim st_atimespec
49 #define st_ctim st_ctimespec
50 #define st_mtim st_mtimespec
52 #else /* Linux, etc... */
53 #include <endian.h>
54 #include <grp.h>
55 #endif
57 #include <fnmatch.h>
58 #include <limits.h>
59 #include <stdio.h>
60 #include <stdint.h>
62 /* For flock. */
63 #ifndef O_EXLOCK
64 #define O_EXLOCK 0
65 #endif
67 #ifndef HAVE_FLOCK
68 #define LOCK_SH 0
69 #define LOCK_EX 0
70 #define LOCK_NB 0
71 #define flock(fd, op) (0)
72 #else
73 #include <sys/file.h>
74 #endif
76 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
77 #ifndef WAIT_ANY
78 #define WAIT_ANY (-1)
79 #endif
81 /* SOCK_NONBLOCK isn't available across BSDs... */
82 #ifndef SOCK_NONBLOCK
83 #define SOCK_NONBLOCK 00004000
84 #endif
86 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
87 * using EAI_NONAME.
88 */
89 #ifndef EAI_NODATA
90 #define EAI_NODATA EAI_NONAME
91 #endif
93 #ifndef __dead
94 #define __dead __attribute__ ((__noreturn__))
95 #endif
97 #ifndef __unused
98 #define __unused __attribute__ ((__unused__))
99 #endif
101 #ifndef __OpenBSD__
102 #define pledge(s, p) (0)
103 #define unveil(s, p) (0)
104 #endif
106 #ifndef __FreeBSD__
107 #define cap_enter() (0)
108 #endif
110 #ifndef HAVE_SETRESGID
111 #define setresgid(a, b, c) (0)
112 #endif
114 #ifndef HAVE_SETRESUID
115 #define setresuid(a, b, c) (0)
116 #endif
118 #ifndef HAVE_LINUX_LANDLOCK_H
119 #define landlock_no_fs() (0)
120 #else
121 int landlock_no_fs(void);
122 #endif
124 #ifndef INFTIM
125 #define INFTIM -1
126 #endif
128 #ifndef HAVE_BSD_UUID
129 #include <uuid/uuid.h>
130 #define uuid_s_ok 0
131 #define uuid_s_bad_version 1
132 #define uuid_s_invalid_string_uuid 2
133 #define uuid_s_no_memory 3
135 /* Length of a node address (an IEEE 802 address). */
136 #define _UUID_NODE_LEN 6
138 struct uuid {
139 uint32_t time_low;
140 uint16_t time_mid;
141 uint16_t time_hi_and_version;
142 uint8_t clock_seq_hi_and_reserved;
143 uint8_t clock_seq_low;
144 uint8_t node[_UUID_NODE_LEN];
145 };
147 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
148 int32_t uuid_is_nil(struct uuid *, uint32_t *);
149 void uuid_create(uuid_t *, uint32_t *);
150 void uuid_create_nil(struct uuid *, uint32_t *);
151 void uuid_from_string(const char *, uuid_t *, uint32_t *);
152 void uuid_to_string(uuid_t *, char **, uint32_t *);
153 #else
154 #include <uuid.h>
155 #endif
157 #ifdef HAVE_STDINT_H
158 #include <stdint.h>
159 #else
160 #include <inttypes.h>
161 #endif
163 #ifdef HAVE_QUEUE_H
164 #include <sys/queue.h>
165 #endif
167 #ifdef HAVE_TREE_H
168 #include <sys/tree.h>
169 #else
170 #include "compat/tree.h"
171 #endif
173 #ifdef HAVE_UTIL_H
174 #include <util.h>
175 #endif
177 #ifdef HAVE_LIBUTIL_H
178 #include <libutil.h>
179 #endif
181 #ifdef HAVE_IMSG
182 #else
183 #include "compat/imsg.h"
184 #endif
186 #ifdef HAVE_SIPHASH
187 #include <siphash.h>
188 #else
189 #include "compat/siphash.h"
190 #endif
192 /* Include Apple-specific headers. Mostly for crypto.*/
193 #if defined(__APPLE__)
194 #define COMMON_DIGEST_FOR_OPENSSL
195 #include <CommonCrypto/CommonDigest.h>
197 #define SHA512_BLOCK_LENGTH 128
198 typedef struct _SHA2_CTX {
199 union {
200 u_int32_t st32[8];
201 u_int64_t st64[8];
202 } state;
203 u_int64_t bitcount[2];
204 u_int8_t buffer[SHA512_BLOCK_LENGTH];
205 } SHA2_CTX;
206 #define SHA256Init SHA256_Init
207 #define SHA256Update SHA256_Update
208 #define SHA256Final SHA256_Final
209 #endif
211 #ifndef __APPLE__
212 #ifdef HAVE_SHA_AS_SHA1
213 # include <sha.h>
214 #endif
215 #ifdef HAVE_SHA1_AS_SHA1
216 # include <sha1.h>
217 #endif
218 #ifdef HAVE_SHA2
219 # include <sha2.h>
220 #else
221 # include "sha2.h"
222 #endif
223 #ifdef HAVE_SHA256
224 # include <sha256.h>
225 #endif
226 #endif
228 /* Catch-all for systems where the header files don't exist and/or the below
229 * still are not defined.
230 */
231 #ifndef SHA256_DIGEST_LENGTH
232 #define SHA256_DIGEST_LENGTH 32
233 #endif
235 #ifndef SHA256_DIGEST_STRING_LENGTH
236 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
237 #endif
239 #if defined(__DragonFly__)
240 #include <openssl/sha.h>
241 #endif
243 #ifndef SHA1_DIGEST_LENGTH
244 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
245 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
247 #define SHA1_CTX SHA_CTX
248 #define SHA1Init SHA1_Init
249 #define SHA1Update SHA1_Update
250 #define SHA1Final SHA1_Final
251 #endif
253 /*
254 * The following SA_LEN/SS_LEN dance comes from various source, notably
255 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
256 * has tweaked a lot of the following macros to suit the needs of
257 * got-portable.
258 */
260 /* From OpenNTPD portable */
261 #if !defined(SA_LEN)
262 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
263 # define SA_LEN(x) ((x)->sa_len)
264 # else
265 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
266 sizeof(struct sockaddr_in6) : \
267 sizeof(struct sockaddr_in))
268 # endif
270 #endif
272 /* From OpenBGPD portable */
273 #if !defined(SS_LEN)
274 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
275 # define SS_LEN(x) ((x)->ss_len)
276 # else
277 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
278 # endif
279 #endif
281 #ifndef HAVE_ASPRINTF
282 /* asprintf.c */
283 int asprintf(char **, const char *, ...);
284 int vasprintf(char **, const char *, va_list);
285 #endif
287 #ifndef HAVE_EXPLICIT_BZERO
288 /* explicit_bzero.c */
289 void explicit_bzero(void *, size_t);
290 #endif
292 #ifndef HAVE_GETDTABLECOUNT
293 /* getdtablecount.c */
294 int getdtablecount(void);
295 #endif
297 #ifndef HAVE_CLOSEFROM
298 /* closefrom.c */
299 void closefrom(int);
300 #endif
302 #if defined (__FreeBSD__)
303 #define closefrom(fd) (closefrom(fd), 0)
304 #endif
306 #ifndef HAVE_STRSEP
307 /* strsep.c */
308 char *strsep(char **, const char *);
309 #endif
311 #ifndef HAVE_STRTONUM
312 /* strtonum.c */
313 long long strtonum(const char *, long long, long long, const char **);
314 #endif
316 #ifndef HAVE_STRLCPY
317 /* strlcpy.c */
318 size_t strlcpy(char *, const char *, size_t);
319 #endif
321 #ifndef HAVE_STRLCAT
322 /* strlcat.c */
323 size_t strlcat(char *, const char *, size_t);
324 #endif
326 #ifndef HAVE_STRNLEN
327 /* strnlen.c */
328 size_t strnlen(const char *, size_t);
329 #endif
331 #ifndef HAVE_STRNDUP
332 /* strndup.c */
333 char *strndup(const char *, size_t);
334 #endif
336 #ifndef HAVE_GETPROGNAME
337 /* getprogname.c */
338 const char *getprogname(void);
339 #endif
341 #ifndef HAVE_GETLINE
342 /* getline.c */
343 ssize_t getline(char **, size_t *, FILE *);
344 #endif
346 #ifndef HAVE_FREEZERO
347 /* freezero.c */
348 void freezero(void *, size_t);
349 #endif
351 #ifndef HAVE_GETDTABLECOUNT
352 /* getdtablecount.c */
353 int getdtablecount(void);
354 #endif
356 #ifndef HAVE_REALLOCARRAY
357 /* reallocarray.c */
358 void *reallocarray(void *, size_t, size_t);
359 #endif
361 #ifndef HAVE_RECALLOCARRAY
362 /* recallocarray.c */
363 void *recallocarray(void *, size_t, size_t, size_t);
364 #endif
366 #ifndef HAVE_SETPROCTITLE
367 /* setproctitle.c */
368 void setproctitle(const char *, ...);
369 #endif
371 #ifndef HAVE_FMT_SCALED
372 /* fmt_scaled.c */
373 int fmt_scaled(long long, char *);
374 int scan_scaled(char *, long long *);
375 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
376 #endif
378 #ifndef HAVE_LIBBSD
379 /* getopt.c */
380 extern int BSDopterr;
381 extern int BSDoptind;
382 extern int BSDoptopt;
383 extern int BSDoptreset;
384 extern char *BSDoptarg;
385 int BSDgetopt(int, char *const *, const char *);
386 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
387 #define opterr BSDopterr
388 #define optind BSDoptind
389 #define optopt BSDoptopt
390 #define optreset BSDoptreset
391 #define optarg BSDoptarg
392 #endif
393 #endif
395 /* Check for some of the non-portable timespec*() functions.
396 * This should largely come from libbsd for systems which
397 * aren't BSD, but this will depend on how old the library
398 * is.
399 */
400 #ifndef timespecisset
401 #define timespecisset(tsp) \
402 ((tsp)->tv_sec || (tsp)->tv_nsec)
403 #endif
405 #ifndef timespecsub
406 #define timespecsub(tsp, usp, vsp) \
407 do { \
408 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
409 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
410 if ((vsp)->tv_nsec < 0) { \
411 (vsp)->tv_sec--; \
412 (vsp)->tv_nsec += 1000000000L; \
413 } \
414 } while (0)
415 #endif
417 #ifndef timespeccmp
418 #define timespeccmp(tvp, uvp, cmp) \
419 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
420 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
421 ((tvp)->tv_sec cmp (uvp)->tv_sec))
422 #endif
424 #ifndef HAVE_BSD_MERGESORT
425 /* mergesort.c */
426 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
427 #endif