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_2
18 #define _GOT_COMPAT_H_2
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 #ifndef __GNUC__
63 #define __attribute__(a)
64 #endif
66 #ifndef __bounded__
67 #define __bounded__(a, b, c)
68 #endif
70 /* For flock. */
71 #ifndef O_EXLOCK
72 #define O_EXLOCK 0
73 #endif
75 #ifndef HAVE_FLOCK
76 #define LOCK_SH 0
77 #define LOCK_EX 0
78 #define LOCK_NB 0
79 #define flock(fd, op) (0)
80 #else
81 #include <sys/file.h>
82 #endif
84 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
85 #ifndef WAIT_ANY
86 #define WAIT_ANY (-1)
87 #endif
89 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
90 * using EAI_NONAME.
91 */
92 #ifndef EAI_NODATA
93 #define EAI_NODATA EAI_NONAME
94 #endif
96 #ifndef __dead
97 #define __dead __attribute__ ((__noreturn__))
98 #endif
100 #ifndef __unused
101 #define __unused __attribute__ ((__unused__))
102 #endif
104 #ifndef __OpenBSD__
105 #define pledge(s, p) (0)
106 #define unveil(s, p) (0)
107 #endif
109 #ifndef __FreeBSD__
110 #define cap_enter() (0)
111 #endif
113 #ifndef HAVE_SETRESGID
114 #define setresgid(a, b, c) (0)
115 #endif
117 #ifndef HAVE_SETRESUID
118 #define setresuid(a, b, c) (0)
119 #endif
121 #ifndef HAVE_LINUX_LANDLOCK_H
122 #define landlock_no_fs() (0)
123 #else
124 int landlock_no_fs(void);
125 #endif
127 #ifndef INFTIM
128 #define INFTIM -1
129 #endif
131 #ifndef HAVE_BSD_UUID
132 #include <uuid/uuid.h>
133 #define uuid_s_ok 0
134 #define uuid_s_bad_version 1
135 #define uuid_s_invalid_string_uuid 2
136 #define uuid_s_no_memory 3
138 /* Length of a node address (an IEEE 802 address). */
139 #define _UUID_NODE_LEN 6
141 struct uuid {
142 uint32_t time_low;
143 uint16_t time_mid;
144 uint16_t time_hi_and_version;
145 uint8_t clock_seq_hi_and_reserved;
146 uint8_t clock_seq_low;
147 uint8_t node[_UUID_NODE_LEN];
148 };
150 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
151 int32_t uuid_is_nil(struct uuid *, uint32_t *);
152 void uuid_create(uuid_t *, uint32_t *);
153 void uuid_create_nil(struct uuid *, uint32_t *);
154 void uuid_from_string(const char *, uuid_t *, uint32_t *);
155 void uuid_to_string(uuid_t *, char **, uint32_t *);
156 #else
157 #include <uuid.h>
158 #endif
160 #ifdef HAVE_STDINT_H
161 #include <stdint.h>
162 #else
163 #include <inttypes.h>
164 #endif
166 #ifdef HAVE_QUEUE_H
167 #include <sys/queue.h>
168 #endif
170 #ifndef HAVE_TREE_H
171 #include "compat/tree.h"
172 #else
173 #include <sys/tree.h>
174 #endif
176 #ifdef HAVE_UTIL_H
177 #include <util.h>
178 #endif
180 #ifdef HAVE_LIBUTIL_H
181 #include <libutil.h>
182 #endif
184 #ifndef IOV_MAX
185 # define IOV_MAX 1024
186 #endif
188 #ifndef HAVE_IMSG
189 #include "compat/imsg.h"
190 #endif
192 #ifndef HAVE_SIPHASH
193 #include "compat/siphash.h"
194 #else
195 #include <siphash.h>
196 #endif
198 /* Include Apple-specific headers. Mostly for crypto.*/
199 #if defined(__APPLE__)
200 #define COMMON_DIGEST_FOR_OPENSSL
201 #include <CommonCrypto/CommonDigest.h>
203 #define SHA512_BLOCK_LENGTH 128
204 typedef struct _SHA2_CTX {
205 union {
206 u_int32_t st32[8];
207 u_int64_t st64[8];
208 } state;
209 u_int64_t bitcount[2];
210 u_int8_t buffer[SHA512_BLOCK_LENGTH];
211 } SHA2_CTX;
212 #define SHA256Init SHA256_Init
213 #define SHA256Update SHA256_Update
214 #define SHA256Final SHA256_Final
215 #endif
217 #ifndef __APPLE__
218 #ifdef HAVE_SHA_AS_SHA1
219 # include <sha.h>
220 #endif
221 #ifdef HAVE_SHA1_AS_SHA1
222 # include <sha1.h>
223 #endif
224 #ifdef HAVE_SHA2
225 # include <sha2.h>
226 #else
227 # include "sha2.h"
228 #endif
229 #ifdef HAVE_SHA256
230 # include <sha256.h>
231 #endif
232 #endif
234 /* Catch-all for systems where the header files don't exist and/or the below
235 * still are not defined.
236 */
237 #ifndef SHA256_DIGEST_LENGTH
238 #define SHA256_DIGEST_LENGTH 32
239 #endif
241 #ifndef SHA256_DIGEST_STRING_LENGTH
242 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
243 #endif
245 #if defined(__DragonFly__)
246 #include <openssl/sha.h>
247 #endif
249 #ifndef SHA1_DIGEST_LENGTH
250 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
251 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
253 #define SHA1_CTX SHA_CTX
254 #define SHA1Init SHA1_Init
255 #define SHA1Update SHA1_Update
256 #define SHA1Final SHA1_Final
257 #endif
259 /*
260 * The following SA_LEN/SS_LEN dance comes from various source, notably
261 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
262 * has tweaked a lot of the following macros to suit the needs of
263 * got-portable.
264 */
266 /* From OpenNTPD portable */
267 #if !defined(SA_LEN)
268 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
269 # define SA_LEN(x) ((x)->sa_len)
270 # else
271 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
272 sizeof(struct sockaddr_in6) : \
273 sizeof(struct sockaddr_in))
274 # endif
276 #endif
278 /* From OpenBGPD portable */
279 #if !defined(SS_LEN)
280 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
281 # define SS_LEN(x) ((x)->ss_len)
282 # else
283 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
284 # endif
285 #endif
287 /* SOCK_NONBLOCK isn't available across BSDs... */
288 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
289 #define SOCK_NONBLOCK 00004000
290 #endif
292 #ifndef HAVE_ASPRINTF
293 /* asprintf.c */
294 int asprintf(char **, const char *, ...);
295 int vasprintf(char **, const char *, va_list);
296 #endif
298 #ifndef HAVE_EXPLICIT_BZERO
299 /* explicit_bzero.c */
300 void explicit_bzero(void *, size_t);
301 #endif
303 #ifndef HAVE_GETDTABLECOUNT
304 /* getdtablecount.c */
305 int getdtablecount(void);
306 #endif
308 #ifndef HAVE_CLOSEFROM
309 /* closefrom.c */
310 void closefrom(int);
311 #endif
313 #ifndef HAVE_STRSEP
314 /* strsep.c */
315 char *strsep(char **, const char *);
316 #endif
318 #ifndef HAVE_STRTONUM
319 /* strtonum.c */
320 long long strtonum(const char *, long long, long long, const char **);
321 #endif
323 #ifndef HAVE_STRLCPY
324 /* strlcpy.c */
325 size_t strlcpy(char *, const char *, size_t);
326 #endif
328 #ifndef HAVE_STRLCAT
329 /* strlcat.c */
330 size_t strlcat(char *, const char *, size_t);
331 #endif
333 #ifndef HAVE_STRNLEN
334 /* strnlen.c */
335 size_t strnlen(const char *, size_t);
336 #endif
338 #ifndef HAVE_STRNDUP
339 /* strndup.c */
340 char *strndup(const char *, size_t);
341 #endif
343 #ifndef HAVE_GETPROGNAME
344 /* getprogname.c */
345 const char *getprogname(void);
346 #endif
348 #ifndef HAVE_GETLINE
349 /* getline.c */
350 ssize_t getline(char **, size_t *, FILE *);
351 #endif
353 #ifndef HAVE_FREEZERO
354 /* freezero.c */
355 void freezero(void *, size_t);
356 #endif
358 #ifndef HAVE_GETDTABLECOUNT
359 /* getdtablecount.c */
360 int getdtablecount(void);
361 #endif
363 #ifndef HAVE_REALLOCARRAY
364 /* reallocarray.c */
365 void *reallocarray(void *, size_t, size_t);
366 #endif
368 #ifndef HAVE_RECALLOCARRAY
369 /* recallocarray.c */
370 void *recallocarray(void *, size_t, size_t, size_t);
371 #endif
373 #ifndef HAVE_SETPROCTITLE
374 /* setproctitle.c */
375 void setproctitle(const char *, ...);
376 #endif
378 #ifndef HAVE_FMT_SCALED
379 /* fmt_scaled.c */
380 int fmt_scaled(long long, char *);
381 int scan_scaled(char *, long long *);
382 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
383 #endif
385 #ifndef HAVE_LIBBSD
386 /* getopt.c */
387 extern int BSDopterr;
388 extern int BSDoptind;
389 extern int BSDoptopt;
390 extern int BSDoptreset;
391 extern char *BSDoptarg;
392 int BSDgetopt(int, char *const *, const char *);
393 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
394 #define opterr BSDopterr
395 #define optind BSDoptind
396 #define optopt BSDoptopt
397 #define optreset BSDoptreset
398 #define optarg BSDoptarg
399 #endif
400 #endif
402 /* Check for some of the non-portable timespec*() functions.
403 * This should largely come from libbsd for systems which
404 * aren't BSD, but this will depend on how old the library
405 * is.
406 */
407 #ifndef timespecisset
408 #define timespecisset(tsp) \
409 ((tsp)->tv_sec || (tsp)->tv_nsec)
410 #endif
412 #ifndef timespecsub
413 #define timespecsub(tsp, usp, vsp) \
414 do { \
415 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
416 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
417 if ((vsp)->tv_nsec < 0) { \
418 (vsp)->tv_sec--; \
419 (vsp)->tv_nsec += 1000000000L; \
420 } \
421 } while (0)
422 #endif
424 #ifndef timespeccmp
425 #define timespeccmp(tvp, uvp, cmp) \
426 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
427 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
428 ((tvp)->tv_sec cmp (uvp)->tv_sec))
429 #endif
431 #ifndef HAVE_MERGESORT
432 /* mergesort.c */
433 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
434 #endif