1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3  *               2008, 2009, 2010, 2011, 2012, 2013, 2019, 2020
4  *      Inferno Nettverk A/S, Norway.  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. The above copyright notice, this list of conditions and the following
10  *    disclaimer must appear in all copies of the software, derivative works
11  *    or modified versions, and any portions thereof, aswell as in all
12  *    supporting documentation.
13  * 2. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by
16  *      Inferno Nettverk A/S, Norway.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Inferno Nettverk A/S requests users of this software to return to
32  *
33  *  Software Distribution Coordinator  or  sdc@inet.no
34  *  Inferno Nettverk A/S
35  *  Oslo Research Park
36  *  Gaustadall�en 21
37  *  NO-0349 Oslo
38  *  Norway
39  *
40  * any improvements or extensions that they make and grant Inferno Nettverk A/S
41  * the rights to redistribute these changes.
42  *
43  */
44 
45 /* $Id: osdep.h,v 1.98.6.3.4.3 2020/11/11 16:11:50 karls Exp $ */
46 
47 /*
48  * ensure pam/libwrap defines are only set when compiling server
49  */
50 
51 #if HAVE_COND_PAM && !SOCKS_CLIENT
52 #define HAVE_PAM (1)
53 #else
54 #define HAVE_PAM (0)
55 #endif /* HAVE_COND_PAM && !SOCKS_CLIENT */
56 
57 #if HAVE_COND_LIBWRAP && !SOCKS_CLIENT
58 #define HAVE_LIBWRAP (1)
59 #else
60 #define HAVE_LIBWRAP (0)
61 #endif /* HAVE_COND_LIBWRAP && !SOCKS_CLIENT */
62 
63 
64 #if HAVE_COND_LIVEDEBUG
65 #define HAVE_LIVEDEBUG (1)
66 #else
67 #define HAVE_LIVEDEBUG (0)
68 #endif /* HAVE_COND_LIVEDEBUG */
69 
70 
71 #include <sys/types.h>
72 #if HAVE_SYS_TIME_H
73 #include <sys/time.h>
74 #endif /* HAVE_SYS_TIME_H */
75 #if HAVE_SYS_FILE_H
76 #include <sys/file.h>
77 #endif /* HAVE_SYS_FILE_H */
78 #include <sys/resource.h>
79 #include <sys/ioctl.h>
80 #if HAVE_SYS_IPC_H
81 #include <sys/ipc.h>
82 #endif /* HAVE_SYS_IPC_H */
83 #if HAVE_SYS_SEM_H
84 #include <sys/sem.h>
85 #endif /* HAVE_SYS_SEM_H */
86 #if (defined __sun) && (defined _NO_SUN_PRAGMA)
87 /* want to avoid connect/listen etc. being remapped to _xnet_foo */
88 #undef __PRAGMA_REDEFINE_EXTNAME
89 #endif /* __sun */
90 #include <sys/socket.h>
91 #if (defined __sun) && (defined _NO_SUN_PRAGMA)
92 /* avoid collision with config_parse.y keywords */
93 #undef bind
94 #undef connect
95 #endif /* __sun */
96 #include <net/if.h>
97 #if NEED_SYS_SOCKIO_H
98 #include <sys/sockio.h>
99 #endif /* NEED_SYS_SOCKIO_H */
100 #include <sys/mman.h>
101 #include <sys/un.h>
102 #include <sys/stat.h>
103 #ifdef SOCKS_DLIB_OSF
104 #undef __DECC
105 #endif /* SOCKS_DLIB_OSF */
106 #include <sys/uio.h>
107 #ifdef SOCKS_DLIB_OSF
108 #define __DECC
109 #endif /* SOCKS_DLIB_OSF */
110 #include <sys/wait.h>
111 #ifdef HAVE_SYS_SELECT_H
112 #include <sys/select.h>
113 #endif /* HAVE_SYS_SELECT_H */
114 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #if HAVE_NETINET_IP_H
117 #include <netinet/ip.h>
118 #endif /* HAVE_NETINET_IP_H */
119 #if HAVE_NETINET_IP_VAR_H
120 #include <netinet/ip_var.h>
121 #endif /* HAVE_NETINET_IP_VAR_H */
122 #include <arpa/inet.h>
123 #if HAVE_ARPA_NAMESER_H
124 #include <arpa/nameser.h>
125 #endif /* HAVE_ARPA_NAMESER_H */
126 
127 #include <netinet/tcp.h>
128 
129 #if HAVE_RPC_RPC_H
130 #include <rpc/rpc.h>
131 #endif /* HAVE_RPC_RPC_H */
132 
133 #if !HAVE_TIMER_MACROS
134 #include "timers.h"
135 #endif /* !HAVE_TIMER_MACROS */
136 
137 #include <netinet/ip_icmp.h>
138 #include <netinet/udp.h>
139 
140 #include <inttypes.h>
141 
142 #include <assert.h>
143 #if HAVE_CRYPT_H
144 #include <crypt.h>
145 #endif /* HAVE_CRYPT_H */
146 
147 #include <ctype.h>
148 #include <errno.h>
149 #include <fcntl.h>
150 #if HAVE_LIMITS_H
151 #include <limits.h>
152 #endif /* HAVE_LIMITS_H */
153 #include <netdb.h>
154 #include <pwd.h>
155 #include <setjmp.h>
156 #include <signal.h>
157 #include <stdarg.h>
158 #include <stdio.h>
159 #include <stdlib.h>
160 
161 #if HAVE_STRINGS_H
162 #include <strings.h>
163 #endif /* HAVE_STRINGS_H */
164 #include <string.h>
165 
166 #include <syslog.h>
167 #if HAVE_LIBWRAP && HAVE_TCPD_H
168 #include <tcpd.h>
169 #endif /* HAVE_LIBWRAP && HAVE_TCPD_H */
170 #if TIME_WITH_SYS_TIME || (!TIME_WITH_SYS_TIME && !HAVE_SYS_TIME_H)
171 #include <time.h>
172 #endif /* TIME_WITH_SYS_TIME || (!TIME_WITH_SYS_TIME && !HAVE_SYS_TIME_H) */
173 #if HAVE_UNISTD_H
174 #ifdef HAVE_DEC_PROTO
175 #undef _XOPEN_SOURCE_EXTENDED
176 #endif /* HAVE_DEC_PROTO */
177 #include <unistd.h>
178 #ifdef HAVE_DEC_PROTO
179 #define _XOPEN_SOURCE_EXTENDED 1
180 #endif /* HAVE_DEC_PROTO */
181 #endif /* HAVE_UNISTD_H */
182 #if HAVE_RESOLV_H
183 #include <resolv.h>
184 #endif /* HAVE_RESOLV_H */
185 #if HAVE_PAM
186 #include <security/pam_appl.h>
187 #endif /* HAVE_PAM */
188 #include <grp.h>
189 
190 #if HAVE_GSSAPI
191 
192 #if HAVE_GSSAPI_GSSAPI_KRB5_H
193 #include <gssapi/gssapi_krb5.h>
194 #elif HAVE_GSSAPI_GSSAPI_H
195 #include <gssapi/gssapi.h>
196 #elif HAVE_GSSAPI_H
197 #include <gssapi.h>
198 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */
199 
200 #if !HAVE_HEIMDAL_KERBEROS
201 #if HAVE_GSSAPI_GSSAPI_EXT_H
202 #include <gssapi/gssapi_ext.h>
203 #endif /* HAVE_GSSAPI_GSSAPI_EXT_H */
204 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
205 #include <gssapi/gssapi_generic.h>
206 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */
207 #endif /* !HAVE_HEIMDAL_KERBEROS */
208 #endif /* HAVE_GSSAPI */
209 
210 #if HAVE_KRB5_H
211 #include <krb5.h>
212 #elif HAVE_KERBEROSV5_KRB5_H
213 #include <kerberosv5/krb5.h>
214 #endif /* HAVE_KERBEROSV5_KRB5_H */
215 
216 #if HAVE_STDDEF_H
217 #include <stddef.h>
218 #endif /* HAVE_STDDEF_H */
219 
220 #if HAVE_PTHREAD_H
221 #include <pthread.h>
222 #endif /* HAVE_PTHREAD_H */
223 
224 #if HAVE_GETIFADDRS && HAVE_IFADDRS_H
225 #include <ifaddrs.h>
226 #else
227 #include "ifaddrs.h"
228 #endif /* HAVE_IFADDRS_H */
229 
230 #if HAVE_VALGRIND_VALGRIND_H
231 #include <valgrind/valgrind.h>
232 #endif /* HAVE_VALGRIND_VALGRIND_H */
233 
234 /*
235  *  End of system headers
236  */
237 
238 /* XXX should have way to control use on client/server */
239 #if HAVE_LIBCFAIL && !SOCKS_CLIENT
240 #include "cfail.h"
241 #endif /* HAVE_LIBCFAIL && !SOCKS_CLIENT*/
242 
243 #if HAVE_PRAGMA_SUPPORT
244 #define NOWARN_ADDRESS_PRAGMA() _Pragma("GCC diagnostic ignored \"-Waddress\"")
245 #else
246 #define NOWARN_ADDRESS_PRAGMA()
247 #endif /* HAVE_PRAGMA_SUPPORT */
248 
249 #if HAVE_DECL_ATTRIBUTE && !defined lint
250 #define __ATTRIBUTE__(x) __attribute__(x)
251 #else
252 #define __ATTRIBUTE__(x)
253 #endif /* HAVE_DECL_ATTRIBUTE */
254 
255 #if HAVE_DECL_CONSTRUCTOR
256 #define ATTRIBUTE_CONSTRUCTOR constructor
257 #else
258 #define ATTRIBUTE_CONSTRUCTOR
259 #endif /* HAVE_DECL_CONSTRUCTOR */
260 
261 #if HAVE_DECL_NONNULL
262 #define __NONNULL__(x) __nonnull__(x)
263 #else
264 #define __NONNULL__(x)
265 #endif /* HAVE_DECL_NONNULL */
266 
267 #if HAVE_DECL_FORMAT
268 #define FORMAT(x, y, z) format(x, y, z)
269 #else
270 #define FORMAT(x, y, z)
271 #endif /* HAVE_DECL_FORMAT */
272 
273 #if HAVE_DECL_BOUNDED
274 #define __BOUNDED__(x, y, z) __bounded__(x, y, z)
275 #else
276 #define __BOUNDED__(x, y, z)
277 #endif /* HAVE_DECL_BOUNDED */
278 
279 #if HAVE_LINUX_BUGS
280 #if (defined __bswap_16) && (!defined __bswap_32)
281 #undef ntohl
282 #undef ntohs
283 #undef htonl
284 #undef htons
285 #endif
286 #endif /* HAVE_LINUX_BUGS */
287 
288 #ifndef RLIMIT_OFILE
289 #define RLIMIT_OFILE RLIMIT_NOFILE
290 #endif /* !RLIMIT_OFILE */
291 
292 #ifndef WAIT_ANY
293 #define WAIT_ANY -1
294 #endif /* !WAIT_ANY */
295 
296 /* libscompat replacement function prototypes */
297 
298 #if !HAVE_SETPROCTITLE
299 void setproctitle(const char *fmt, ...)
300    __ATTRIBUTE__((FORMAT(__printf__, 1, 2)));
301 void initsetproctitle(int, char **);
302 #endif /* !HAVE_SETPROCTITLE */
303 
304 #if !HAVE_SOCKATMARK
305 int sockatmark(int);
306 #endif /* !HAVE_SOCKATMARK */
307 
308 #if !HAVE_STRLCPY
309 size_t strlcpy(char *dst, const char *src, size_t size);
310 size_t strlcat(char *dst, const char *src, size_t size);
311 #endif /* !HAVE_STRLCPY */
312 
313 #if !HAVE_HSTRERROR
314 const char *hstrerror(int);
315 #endif /* !HAVE_HSTRERROR */
316 
317 #if !HAVE_MEMMOVE
318 void * memmove(void *, const void *, register size_t);
319 #endif /* !HAVE_MEMMOVE */
320 
321 #if !HAVE_INET_PTON
322 int inet_pton(int af, const char *src, void *dst);
323 #endif /* !HAVE_INET_PTON */
324 
325 #if !HAVE_ISSETUGID
326 int issetugid(void);
327 #endif /* !HAVE_ISSETUGID */
328 
329 #if !HAVE_PSELECT
330 inline int pselect(int nfds, fd_set *rset, fd_set *wset, fd_set *xset,
331     const struct timespec *ts, const sigset_t *sigmask);
332 #endif /* !HAVE_ISSETUGID */
333 
334 #if !HAVE_VSYSLOG
335 void vsyslog(int, const char *, va_list);
336 #endif /* !HAVE_VSYSLOG */
337 
338 #if NEED_GETSOCKOPT_CAST
339 #define getsockopt(a,b,c,d,e) getsockopt((a),(b),(c),(char *)(d),(e))
340 #define setsockopt(a,b,c,d,e) setsockopt((a),(b),(c),(char *)(d),(e))
341 #endif /* NEED_GETSOCKOPT_CAST */
342 
343 #if !HAVE_SIG_ATOMIC_T
344 typedef int sig_atomic_t;
345 #endif /* !HAVE_SIG_ATOMIC_T */
346 
347 #define BYTEWIDTH_8BITS 1
348 #define BYTEWIDTH_16BITS 2
349 #define BYTEWIDTH_32BITS 4
350 
351 #if SIZEOF_CHAR == BYTEWIDTH_8BITS
352  typedef unsigned char ubits_8;
353  typedef          char sbits_8;
354 #else
355 #error "no known 8 bits wide data type"
356 #endif /* SIZEOF_CHAR == BYTEWIDTH_8BITS */
357 
358 #if SIZEOF_SHORT == BYTEWIDTH_16BITS
359  typedef unsigned short ubits_16;
360  typedef          short sbits_16;
361 #else
362 # if SIZEOF_INT == BYTEWIDTH_16BITS
363   typedef unsigned int ubits_16;
364   typedef          int sbits_16;
365 # else
366 #error "no known 16 bits wide data type"
367 # endif /* SIZEOF_INT == BYTEWIDTH_16BITS */
368 #endif
369 
370 #if SIZEOF_INT == BYTEWIDTH_32BITS
371  typedef unsigned int ubits_32;
372  typedef          int sbits_32;
373 #else
374 # if SIZEOF_SHORT == BYTEWIDTH_32BITS
375    typedef unsigned short ubits_32;
376    typedef          short sbits_32;
377 # else
378 #  if SIZEOF_LONG == BYTEWIDTH_32BITS
379     typedef unsigned long ubits_32;
380     typedef          long sbits_32;
381 #  else
382 #error "no known 32 bits wide data type"
383 #  endif /* SIZEOF_LONG == BYTEWIDTH_32BITS */
384 # endif /* SIZEOF_SHORT == BYTEWIDTH_32BITS */
385 #endif /* SIZEOF_INT == BYTEWIDTH_32BITS */
386 
387 #if !HAVE_INT8_T
388 #define int8_t sbits_8
389 #endif /* HAVE_INT8_T */
390 
391 #if !HAVE_INT16_T
392 #define int16_t sbits_16
393 #endif /* HAVE_INT16_T */
394 
395 #if !HAVE_INT32_T
396 #define int32_t sbits_32
397 #endif /* HAVE_INT32_T */
398 
399 #if !HAVE_UINT8_T
400 #define uint8_t ubits_8
401 #endif /* HAVE_UINT8_T */
402 
403 #if !HAVE_UINT16_T
404 #define uint16_t ubits_16
405 #endif /* HAVE_UINT16_T */
406 
407 #if !HAVE_UINT32_T
408 #define uint32_t ubits_32
409 #endif /* HAVE_UINT32_T */
410 
411 #if !HAVE_IN_PORT_T
412 #define in_port_t ubits_16
413 #endif /* HAVE_IN_PORT_T */
414 
415 #if !HAVE_IN_ADDR_T
416 #define in_addr_t ubits_32
417 #endif /* HAVE_IN_ADDR_T */
418 
419 #ifndef INADDR_NONE
420 #define INADDR_NONE (ubits_32)0xffffffff
421 #endif /* !INADDR_NONE */
422 
423 #ifndef MAX
424 #define MAX(a,b) ((a) > (b) ? (a) : (b))
425 #endif /* !MAX */
426 
427 #ifndef MIN
428 #define MIN(a,b) ((a) < (b) ? (a) : (b))
429 #endif /* !MIN */
430 
431 #if NEED_EXIT_FAILURE
432 /* assumes EXIT_SUCCESS is undefined too */
433 #define EXIT_SUCCESS 0
434 #define EXIT_FAILURE 1
435 #endif /* NEED_EXIT_FAILURE */
436 
437 #if NEED_SA_RESTART
438 #define SA_RESTART SV_INTERRUPT
439 #endif /* NEED_SA_RESTART */
440 
441 #if NEED_AF_LOCAL
442 #define AF_LOCAL AF_UNIX
443 #endif /* NEED_AF_LOCAL */
444 
445 #if HAVE_NOMALLOC_REALLOC
446 #define realloc(p,s) (((p) == NULL) ? (malloc(s)) : (realloc((p),(s))))
447 #endif /* HAVE_NOMALLOC_REALLOC */
448 
449 #if HAVE_NONULL_FREE
450 #define free(p)                                                                \
451 do {                                                                           \
452    if ((p) != NULL)                                                            \
453       free((p));                                                               \
454 } while (/* CONSTCOND */ 0)
455 #endif /* HAVE_NONULL_FREE */
456 
457 #ifndef __CONCAT
458 #define __CONCAT(x,y) x ## y
459 #endif /* !__CONCAT */
460 
461 #ifndef __CONCAT3
462 #define __CONCAT3(x,y,z) x ## y ## z
463 #endif /* !__CONCAT3 */
464 
465 #ifndef ROUNDDOWN
466 #define ROUNDDOWN(x, size)  (((x) / (size) ) * (size))
467 #endif
468 
469 #ifndef ROUNDUP
470 #define ROUNDUP(x, size)    ((((x) + (size) - 1) / (size)) * (size))
471 #endif
472 
473 #define ROUNDFLOAT(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x) - 0.5))
474 
475 #ifndef EPROTO
476 #define EPROTO EPROTOTYPE
477 #endif /* !EPROTO */
478 
479 #ifndef ETHER_ADDR_LEN
480 #define ETHER_ADDR_LEN  (6)
481 #endif /* !ETHER_ADDR_LEN */
482 
483 #if !HAVE_STRUCT_IPOPTS
484 #define   MAX_IPOPTLEN   40
485 struct ipoption {
486    struct   in_addr ipopt_dst;
487    sbits_8   ipopt_list[MAX_IPOPTLEN];
488 };
489 #endif /* !HAVE_STRUCT_IPOPTS */
490 
491 #if !HAVE_FDMASK
492 typedef unsigned long fd_mask;
493 #endif /* !HAVE_FDMASK */
494 
495 #if !HAVE_IN6_ADDR
496 /* from OpenBSD netinet6/in6.h */
497 struct in6_addr {
498    union {
499       u_int8_t   __u6_addr8[16];
500       u_int16_t  __u6_addr16[8];
501       u_int32_t  __u6_addr32[4];
502    } __u6_addr;/* 128-bit IP6 address */
503 };
504 #define s6_addr   __u6_addr.__u6_addr8
505 #endif /* !HAVE_IN6_ADDR */
506 
507 
508 /*
509  * hostid structure (defined here as linux/tcp.h cannot be included)
510  */
511 #if HAVE_MAX_HOSTIDS
512 #if SOCKS_HOSTID_VERSION == 1
513 struct tcp_ipa {
514    u_int32_t ipa_ipaddress[HAVE_MAX_HOSTIDS];
515 };
516 #else
517 #error "error: unsupported IPA_VERSION"
518 #endif /* SOCKS_HOSTID_VERSION */
519 #endif /* HAVE_MAX_HOSTIDS */
520 
521 /* replace select() macros on Linux to avoid FORTIFY related bug */
522 #if HAVE_LINUX_BUGS
523 #undef __FD_ELT
524 #define __FD_ELT(d) ((d) / (__NFDBITS))
525 #endif /* HAVE_LINUX_BUGS */
526 
527 /*
528  * libscompat functions
529  */
530 
531 #if !HAVE_DAEMON
532 int daemon(int, int);
533 #endif /* !HAVE_DAEMON */
534 
535 #if !HAVE_DIFFTIME
536 double difftime(const time_t time1, const time_t time0);
537 #endif /* !HAVE_DIFFTIME */
538 
539 #include "in_cksum.h"
540