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 <stdlib.h>
60 #include <stdio.h>
61 #include <stdint.h>
63 #ifndef __GNUC__
64 #define __attribute__(a)
65 #endif
67 #ifndef __bounded__
68 #define __bounded__(a, b, c)
69 #endif
71 /* For flock. */
72 #ifndef O_EXLOCK
73 #define O_EXLOCK 0
74 #endif
76 #ifndef HAVE_FLOCK
77 #define LOCK_SH 0
78 #define LOCK_EX 0
79 #define LOCK_NB 0
80 #define flock(fd, op) (0)
81 #else
82 #include <sys/file.h>
83 #endif
85 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
86 #ifndef WAIT_ANY
87 #define WAIT_ANY (-1)
88 #endif
90 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
91 * using EAI_NONAME.
92 */
93 #ifndef EAI_NODATA
94 #define EAI_NODATA EAI_NONAME
95 #endif
97 #ifndef __dead
98 #define __dead __attribute__ ((__noreturn__))
99 #endif
101 #ifndef __unused
102 #define __unused __attribute__ ((__unused__))
103 #endif
105 #ifndef __OpenBSD__
106 #define pledge(s, p) (0)
107 #define unveil(s, p) (0)
108 #endif
110 #ifndef __FreeBSD__
111 #define cap_enter() (0)
112 #endif
114 #ifndef HAVE_SETRESGID
115 #define setresgid(a, b, c) (0)
116 #endif
118 #ifndef HAVE_SETRESUID
119 #define setresuid(a, b, c) (0)
120 #endif
122 #ifndef HAVE_LINUX_LANDLOCK_H
123 #define landlock_no_fs() (0)
124 #else
125 int landlock_no_fs(void);
126 #endif
128 #ifndef INFTIM
129 #define INFTIM -1
130 #endif
132 #ifndef HAVE_BSD_UUID
133 #include <uuid/uuid.h>
134 #define uuid_s_ok 0
135 #define uuid_s_bad_version 1
136 #define uuid_s_invalid_string_uuid 2
137 #define uuid_s_no_memory 3
139 /* Length of a node address (an IEEE 802 address). */
140 #define _UUID_NODE_LEN 6
142 struct uuid {
143 uint32_t time_low;
144 uint16_t time_mid;
145 uint16_t time_hi_and_version;
146 uint8_t clock_seq_hi_and_reserved;
147 uint8_t clock_seq_low;
148 uint8_t node[_UUID_NODE_LEN];
149 };
151 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
152 int32_t uuid_is_nil(struct uuid *, uint32_t *);
153 void uuid_create(uuid_t *, uint32_t *);
154 void uuid_create_nil(struct uuid *, uint32_t *);
155 void uuid_from_string(const char *, uuid_t *, uint32_t *);
156 void uuid_to_string(uuid_t *, char **, uint32_t *);
157 #else
158 #include <uuid.h>
159 #endif
161 #ifdef HAVE_STDINT_H
162 #include <stdint.h>
163 #else
164 #include <inttypes.h>
165 #endif
167 #ifdef HAVE_QUEUE_H
168 #include <sys/queue.h>
169 #endif
171 #ifndef HAVE_TREE_H
172 #include "compat/tree.h"
173 #else
174 #include <sys/tree.h>
175 #endif
177 #ifdef HAVE_UTIL_H
178 #include <util.h>
179 #endif
181 #ifdef HAVE_LIBUTIL_H
182 #include <libutil.h>
183 #endif
185 #ifndef IOV_MAX
186 # define IOV_MAX 1024
187 #endif
189 #ifndef HAVE_IMSG
190 #include "compat/imsg.h"
191 #endif
193 #ifndef HAVE_SIPHASH
194 #include "compat/siphash.h"
195 #else
196 #include <siphash.h>
197 #endif
199 /* Include Apple-specific headers. Mostly for crypto.*/
200 #if defined(__APPLE__)
201 #define COMMON_DIGEST_FOR_OPENSSL
202 #include <CommonCrypto/CommonDigest.h>
204 #define SHA512_BLOCK_LENGTH 128
205 typedef struct _SHA2_CTX {
206 union {
207 u_int32_t st32[8];
208 u_int64_t st64[8];
209 } state;
210 u_int64_t bitcount[2];
211 u_int8_t buffer[SHA512_BLOCK_LENGTH];
212 } SHA2_CTX;
213 #define SHA256Init SHA256_Init
214 #define SHA256Update SHA256_Update
215 #define SHA256Final SHA256_Final
216 #endif
218 #ifndef __APPLE__
219 #ifdef HAVE_SHA_AS_SHA1
220 # include <sha.h>
221 #endif
222 #ifdef HAVE_SHA1_AS_SHA1
223 # include <sha1.h>
224 #endif
225 #ifdef HAVE_SHA2
226 # include <sha2.h>
227 #else
228 # include "sha2.h"
229 #endif
230 #ifdef HAVE_SHA256
231 # include <sha256.h>
232 #endif
233 #endif
235 /* Catch-all for systems where the header files don't exist and/or the below
236 * still are not defined.
237 */
238 #ifndef SHA256_DIGEST_LENGTH
239 #define SHA256_DIGEST_LENGTH 32
240 #endif
242 #ifndef SHA256_DIGEST_STRING_LENGTH
243 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
244 #endif
246 #if defined(__DragonFly__)
247 #include <openssl/sha.h>
248 #endif
250 #ifndef SHA1_DIGEST_LENGTH
251 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
252 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
254 #define SHA1_CTX SHA_CTX
255 #define SHA1Init SHA1_Init
256 #define SHA1Update SHA1_Update
257 #define SHA1Final SHA1_Final
258 #endif
260 /*
261 * The following SA_LEN/SS_LEN dance comes from various source, notably
262 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
263 * has tweaked a lot of the following macros to suit the needs of
264 * got-portable.
265 */
267 /* From OpenNTPD portable */
268 #if !defined(SA_LEN)
269 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
270 # define SA_LEN(x) ((x)->sa_len)
271 # else
272 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
273 sizeof(struct sockaddr_in6) : \
274 sizeof(struct sockaddr_in))
275 # endif
277 #endif
279 /* From OpenBGPD portable */
280 #if !defined(SS_LEN)
281 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
282 # define SS_LEN(x) ((x)->ss_len)
283 # else
284 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
285 # endif
286 #endif
288 /* SOCK_NONBLOCK isn't available across BSDs... */
289 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
290 #define SOCK_NONBLOCK 00004000
291 #endif
293 #ifndef HAVE_ASPRINTF
294 /* asprintf.c */
295 int asprintf(char **, const char *, ...);
296 int vasprintf(char **, const char *, va_list);
297 #endif
299 #ifndef HAVE_EXPLICIT_BZERO
300 /* explicit_bzero.c */
301 void explicit_bzero(void *, size_t);
302 #endif
304 #ifndef HAVE_GETDTABLECOUNT
305 /* getdtablecount.c */
306 int getdtablecount(void);
307 #endif
309 #ifndef HAVE_CLOSEFROM
310 /* closefrom.c */
311 void closefrom(int);
312 #endif
314 #ifndef HAVE_STRSEP
315 /* strsep.c */
316 char *strsep(char **, const char *);
317 #endif
319 #ifndef HAVE_STRTONUM
320 /* strtonum.c */
321 long long strtonum(const char *, long long, long long, const char **);
322 #endif
324 #ifndef HAVE_STRLCPY
325 /* strlcpy.c */
326 size_t strlcpy(char *, const char *, size_t);
327 #endif
329 #ifndef HAVE_STRLCAT
330 /* strlcat.c */
331 size_t strlcat(char *, const char *, size_t);
332 #endif
334 #ifndef HAVE_STRNLEN
335 /* strnlen.c */
336 size_t strnlen(const char *, size_t);
337 #endif
339 #ifndef HAVE_STRNDUP
340 /* strndup.c */
341 char *strndup(const char *, size_t);
342 #endif
344 #ifndef HAVE_GETPROGNAME
345 /* getprogname.c */
346 const char *getprogname(void);
347 #endif
349 #ifndef HAVE_GETLINE
350 /* getline.c */
351 ssize_t getline(char **, size_t *, FILE *);
352 #endif
354 #ifndef HAVE_FREEZERO
355 /* freezero.c */
356 void freezero(void *, size_t);
357 #endif
359 #ifndef HAVE_GETDTABLECOUNT
360 /* getdtablecount.c */
361 int getdtablecount(void);
362 #endif
364 #ifndef HAVE_REALLOCARRAY
365 /* reallocarray.c */
366 void *reallocarray(void *, size_t, size_t);
367 #endif
369 #ifndef HAVE_RECALLOCARRAY
370 /* recallocarray.c */
371 void *recallocarray(void *, size_t, size_t, size_t);
372 #endif
374 #ifndef HAVE_SETPROCTITLE
375 /* setproctitle.c */
376 void setproctitle(const char *, ...);
377 #endif
379 #ifndef HAVE_FMT_SCALED
380 /* fmt_scaled.c */
381 int fmt_scaled(long long, char *);
382 int scan_scaled(char *, long long *);
383 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
384 #endif
386 #ifndef HAVE_LIBBSD
387 /* getopt.c */
388 extern int BSDopterr;
389 extern int BSDoptind;
390 extern int BSDoptopt;
391 extern int BSDoptreset;
392 extern char *BSDoptarg;
393 int BSDgetopt(int, char *const *, const char *);
394 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
395 #define opterr BSDopterr
396 #define optind BSDoptind
397 #define optopt BSDoptopt
398 #define optreset BSDoptreset
399 #define optarg BSDoptarg
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
435 #endif