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 #ifdef HAVE_STDINT_H
21 #include <stdint.h>
22 #else
23 #include <inttypes.h>
24 #endif
26 #include <limits.h>
27 #include <sys/uio.h>
29 #if defined(__FreeBSD__)
30 #include <sys/endian.h>
31 #include <sys/capsicum.h>
32 #elif defined(__APPLE__)
33 #include <machine/endian.h>
34 #include <libkern/OSByteOrder.h>
35 #include "compat/bsd-poll.h"
37 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
39 #define htobe16(x) OSSwapHostToBigInt16(x)
40 #define htole16(x) OSSwapHostToLittleInt16(x)
41 #define be16toh(x) OSSwapBigToHostInt16(x)
42 #define le16toh(x) OSSwapLittleToHostInt16(x)
44 #define htobe32(x) OSSwapHostToBigInt32(x)
45 #define htole32(x) OSSwapHostToLittleInt32(x)
46 #define be32toh(x) OSSwapBigToHostInt32(x)
47 #define le32toh(x) OSSwapLittleToHostInt32(x)
49 #define htobe64(x) OSSwapHostToBigInt64(x)
50 #define htole64(x) OSSwapHostToLittleInt64(x)
51 #define be64toh(x) OSSwapBigToHostInt64(x)
52 #define le64toh(x) OSSwapLittleToHostInt64(x)
54 #define st_atim st_atimespec
55 #define st_ctim st_ctimespec
56 #define st_mtim st_mtimespec
58 #else /* Linux, etc... */
59 #include <endian.h>
60 #include <grp.h>
61 #include <stdlib.h>
62 #endif
64 #ifndef __GNUC__
65 #define __attribute__(a)
66 #ifndef __bounded__
67 #define __bounded__(a, b, c)
68 #endif
69 #endif
72 #ifndef UID_MAX
73 # define UID_MAX UINT_MAX /* max value for a uid_t */
74 #endif
75 #ifndef GID_MAX
76 # define GID_MAX UINT_MAX /* max value for a gid_t */
77 #endif
79 /* For flock. */
80 #ifndef O_EXLOCK
81 #define O_EXLOCK 0
82 #endif
84 #ifndef HAVE_FLOCK
85 #define LOCK_SH 0
86 #define LOCK_EX 0
87 #define LOCK_NB 0
88 #define flock(fd, op) (0)
89 #else
90 #include <sys/file.h>
91 #endif
93 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
94 #ifndef WAIT_ANY
95 #define WAIT_ANY (-1)
96 #endif
98 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
99 * using EAI_NONAME.
100 */
101 #ifndef EAI_NODATA
102 #define EAI_NODATA EAI_NONAME
103 #endif
105 #ifndef __dead
106 #define __dead __attribute__ ((__noreturn__))
107 #endif
109 #ifndef __unused
110 #define __unused __attribute__ ((__unused__))
111 #endif
113 #ifndef __OpenBSD__
114 #define pledge(s, p) (0)
115 #define unveil(s, p) (0)
116 #endif
118 #ifndef __FreeBSD__
119 #define cap_enter() (0)
120 #endif
122 #ifndef HAVE_B64_NTOP
123 #undef b64_ntop
124 #undef b64_pton
125 int b64_ntop(u_char const *, size_t, char *, size_t);
126 int b64_pton(char const *, u_char *, size_t);
127 #endif
129 #ifndef HAVE_SETRESGID
130 #define setresgid(a, b, c) (0)
131 #endif
133 #ifndef HAVE_SETRESUID
134 #define setresuid(a, b, c) (0)
135 #endif
137 #ifndef HAVE_LINUX_LANDLOCK_H
138 #define landlock_no_fs() (0)
139 #else
140 int landlock_no_fs(void);
141 #endif
143 #ifndef INFTIM
144 #define INFTIM -1
145 #endif
147 #ifndef HAVE_BSD_UUID
148 #include <uuid/uuid.h>
149 #define uuid_s_ok 0
150 #define uuid_s_bad_version 1
151 #define uuid_s_invalid_string_uuid 2
152 #define uuid_s_no_memory 3
154 /* Length of a node address (an IEEE 802 address). */
155 #define _UUID_NODE_LEN 6
157 struct uuid {
158 uint32_t time_low;
159 uint16_t time_mid;
160 uint16_t time_hi_and_version;
161 uint8_t clock_seq_hi_and_reserved;
162 uint8_t clock_seq_low;
163 uint8_t node[_UUID_NODE_LEN];
164 };
166 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
167 int32_t uuid_is_nil(struct uuid *, uint32_t *);
168 void uuid_create(uuid_t *, uint32_t *);
169 void uuid_create_nil(struct uuid *, uint32_t *);
170 void uuid_from_string(const char *, uuid_t *, uint32_t *);
171 void uuid_to_string(uuid_t *, char **, uint32_t *);
172 #else
173 #include <uuid.h>
174 #endif
176 #ifdef HAVE_QUEUE_H
177 #include <sys/queue.h>
178 #endif
180 #ifndef HAVE_TREE_H
181 #include "compat/tree.h"
182 #else
183 #include <sys/tree.h>
184 #endif
186 #ifdef HAVE_UTIL_H
187 #include <util.h>
188 #endif
190 #ifdef HAVE_LIBUTIL_H
191 #include <libutil.h>
192 #endif
194 #ifndef IOV_MAX
195 # define IOV_MAX 1024
196 #endif
198 #ifndef HAVE_IMSG
199 #include "compat/imsg.h"
200 #else
201 #include <imsg.h>
202 #endif
204 #ifndef HAVE_SIPHASH
205 #include "compat/siphash.h"
206 #else
207 #include <siphash.h>
208 #endif
210 /* Include Apple-specific headers. Mostly for crypto.*/
211 #if defined(__APPLE__)
212 #define COMMON_DIGEST_FOR_OPENSSL
213 #include <CommonCrypto/CommonDigest.h>
215 #define SHA512_BLOCK_LENGTH 128
216 typedef struct _SHA2_CTX {
217 union {
218 u_int32_t st32[8];
219 u_int64_t st64[8];
220 } state;
221 u_int64_t bitcount[2];
222 u_int8_t buffer[SHA512_BLOCK_LENGTH];
223 } SHA2_CTX;
224 #endif
226 #if defined(__APPLE__) || defined(__NetBSD__)
227 #define SHA256Init SHA256_Init
228 #define SHA256Update SHA256_Update
229 #define SHA256Final SHA256_Final
230 #endif
232 #ifndef __APPLE__
233 #ifdef HAVE_SHA_H
234 # include <sha.h>
235 #endif
236 #ifdef HAVE_SHA1_H
237 # include <sha1.h>
238 #endif
239 #ifdef HAVE_SHA2_H
240 # include <sha2.h>
241 #else
242 # include "sha2.h"
243 #endif
244 #ifdef HAVE_SHA256_H
245 # include <sha256.h>
246 #endif
247 #endif
249 /* Catch-all for systems where the header files don't exist and/or the below
250 * still are not defined.
251 */
252 #ifndef SHA256_DIGEST_LENGTH
253 #define SHA256_DIGEST_LENGTH 32
254 #endif
256 #ifndef SHA256_DIGEST_STRING_LENGTH
257 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
258 #endif
260 #if defined(__DragonFly__)
261 #include <openssl/sha.h>
262 #endif
264 #ifndef SHA1_DIGEST_LENGTH
265 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
266 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
268 #define SHA1_CTX SHA_CTX
269 #define SHA1Init SHA1_Init
270 #define SHA1Update SHA1_Update
271 #define SHA1Final SHA1_Final
272 #endif
274 /*
275 * The following SA_LEN/SS_LEN dance comes from various source, notably
276 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
277 * has tweaked a lot of the following macros to suit the needs of
278 * got-portable.
279 */
281 /* From OpenNTPD portable */
282 #if !defined(SA_LEN)
283 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
284 # define SA_LEN(x) ((x)->sa_len)
285 # else
286 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
287 sizeof(struct sockaddr_in6) : \
288 sizeof(struct sockaddr_in))
289 # endif
291 #endif
293 /* From OpenBGPD portable */
294 #if !defined(SS_LEN)
295 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
296 # define SS_LEN(x) ((x)->ss_len)
297 # else
298 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
299 # endif
300 #endif
302 /* SOCK_NONBLOCK isn't available across BSDs... */
303 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
304 #define SOCK_NONBLOCK 00004000
305 #endif
307 #ifndef HAVE_ASPRINTF
308 /* asprintf.c */
309 int asprintf(char **, const char *, ...);
310 int vasprintf(char **, const char *, va_list);
311 #endif
313 #ifndef HAVE_EXPLICIT_BZERO
314 /* explicit_bzero.c */
315 void explicit_bzero(void *, size_t);
316 #endif
318 #ifndef HAVE_GETDTABLECOUNT
319 /* getdtablecount.c */
320 int getdtablecount(void);
321 #endif
323 #ifndef HAVE_CLOSEFROM
324 /* closefrom.c */
325 void closefrom(int);
326 #endif
328 #ifndef HAVE_STRSEP
329 /* strsep.c */
330 char *strsep(char **, const char *);
331 #endif
333 #ifndef HAVE_STRTONUM
334 /* strtonum.c */
335 long long strtonum(const char *, long long, long long, const char **);
336 #endif
338 #ifndef HAVE_STRLCPY
339 /* strlcpy.c */
340 size_t strlcpy(char *, const char *, size_t);
341 #endif
343 #ifndef HAVE_STRLCAT
344 /* strlcat.c */
345 size_t strlcat(char *, const char *, size_t);
346 #endif
348 #ifndef HAVE_STRNLEN
349 /* strnlen.c */
350 size_t strnlen(const char *, size_t);
351 #endif
353 #ifndef HAVE_STRNDUP
354 /* strndup.c */
355 char *strndup(const char *, size_t);
356 #endif
358 #ifndef HAVE_GETPROGNAME
359 /* getprogname.c */
360 const char *getprogname(void);
361 #endif
363 #ifndef HAVE_GETLINE
364 /* getline.c */
365 ssize_t getline(char **, size_t *, FILE *);
366 #endif
368 #ifndef HAVE_FREEZERO
369 /* freezero.c */
370 void freezero(void *, size_t);
371 #endif
373 #ifndef HAVE_GETDTABLECOUNT
374 /* getdtablecount.c */
375 int getdtablecount(void);
376 #endif
378 #ifndef HAVE_REALLOCARRAY
379 /* reallocarray.c */
380 void *reallocarray(void *, size_t, size_t);
381 #endif
383 #ifndef HAVE_RECALLOCARRAY
384 /* recallocarray.c */
385 void *recallocarray(void *, size_t, size_t, size_t);
386 #endif
388 #ifndef HAVE_SETPROCTITLE
389 /* setproctitle.c */
390 void setproctitle(const char *, ...);
391 #endif
393 #ifndef HAVE_FMT_SCALED
394 /* fmt_scaled.c */
395 int fmt_scaled(long long, char *);
396 int scan_scaled(char *, long long *);
397 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
398 #endif
400 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
401 /* getopt.c */
402 extern int BSDopterr;
403 extern int BSDoptind;
404 extern int BSDoptopt;
405 extern int BSDoptreset;
406 extern char *BSDoptarg;
407 int BSDgetopt(int, char *const *, const char *);
408 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
409 #define opterr BSDopterr
410 #define optind BSDoptind
411 #define optopt BSDoptopt
412 #define optreset BSDoptreset
413 #define optarg BSDoptarg
414 #endif
416 /* Check for some of the non-portable timespec*() functions.
417 * This should largely come from libbsd for systems which
418 * aren't BSD, but this will depend on how old the library
419 * is.
420 */
421 #ifndef timespecisset
422 #define timespecisset(tsp) \
423 ((tsp)->tv_sec || (tsp)->tv_nsec)
424 #endif
426 #ifndef timespecsub
427 #define timespecsub(tsp, usp, vsp) \
428 do { \
429 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
430 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
431 if ((vsp)->tv_nsec < 0) { \
432 (vsp)->tv_sec--; \
433 (vsp)->tv_nsec += 1000000000L; \
434 } \
435 } while (0)
436 #endif
438 #ifndef timespeccmp
439 #define timespeccmp(tvp, uvp, cmp) \
440 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
441 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
442 ((tvp)->tv_sec cmp (uvp)->tv_sec))
443 #endif
445 #ifndef HAVE_MERGESORT
446 /* mergesort.c */
447 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
448 #endif
449 #endif