1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2001 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate /* 8*0Sstevel@tonic-gate * @(#) tcpd.h 1.5 96/03/19 16:22:24 9*0Sstevel@tonic-gate * 10*0Sstevel@tonic-gate * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 11*0Sstevel@tonic-gate */ 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gate /* 14*0Sstevel@tonic-gate * HAVE_IPV6 is traditionally configured at tcp_wrappers build time but for 15*0Sstevel@tonic-gate * Solaris it must always be defined to keep the library interface binary 16*0Sstevel@tonic-gate * compatible. 17*0Sstevel@tonic-gate */ 18*0Sstevel@tonic-gate #define HAVE_IPV6 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gate /* Structure to describe one communications endpoint. */ 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate #define STRING_LENGTH 128 /* hosts, users, processes */ 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate #include <sys/socket.h> 25*0Sstevel@tonic-gate #include <netinet/in.h> 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate typedef struct sockaddr_gen { 28*0Sstevel@tonic-gate union { 29*0Sstevel@tonic-gate struct sockaddr _sg_sa; 30*0Sstevel@tonic-gate struct sockaddr_in _sg_sin; 31*0Sstevel@tonic-gate #ifdef HAVE_IPV6 32*0Sstevel@tonic-gate struct sockaddr_in6 _sg_sin6; 33*0Sstevel@tonic-gate #endif 34*0Sstevel@tonic-gate } sg_addr; 35*0Sstevel@tonic-gate } sockaddr_gen; 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate typedef union gen_addr { 38*0Sstevel@tonic-gate struct in_addr ga_in; 39*0Sstevel@tonic-gate #ifdef HAVE_IPV6 40*0Sstevel@tonic-gate struct in6_addr ga_in6; 41*0Sstevel@tonic-gate #endif 42*0Sstevel@tonic-gate } gen_addr; 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate extern void sockgen_simplify(); 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate #define sg_sa sg_addr._sg_sa 47*0Sstevel@tonic-gate #define sg_sin sg_addr._sg_sin 48*0Sstevel@tonic-gate #define sg_sin6 sg_addr._sg_sin6 49*0Sstevel@tonic-gate #define sg_family sg_sa.sa_family 50*0Sstevel@tonic-gate #ifdef HAVE_IPV6 51*0Sstevel@tonic-gate #define SGADDRSZ(sag) ((sag)->sg_family == AF_INET6 ? \ 52*0Sstevel@tonic-gate sizeof (struct in6_addr) : \ 53*0Sstevel@tonic-gate sizeof (struct in_addr)) 54*0Sstevel@tonic-gate #define SGSOCKADDRSZ(sag) ((sag)->sg_family == AF_INET6 ? \ 55*0Sstevel@tonic-gate sizeof (struct sockaddr_in6) : \ 56*0Sstevel@tonic-gate sizeof (struct sockaddr_in)) 57*0Sstevel@tonic-gate #define SGPORT(sag) (*((sag)->sg_family == AF_INET6 ? \ 58*0Sstevel@tonic-gate &(sag)->sg_sin6.sin6_port : \ 59*0Sstevel@tonic-gate &(sag)->sg_sin.sin_port)) 60*0Sstevel@tonic-gate #define SGADDRP(sag) (((sag)->sg_family == AF_INET6 ? \ 61*0Sstevel@tonic-gate (char *) &(sag)->sg_sin6.sin6_addr : \ 62*0Sstevel@tonic-gate (char *) &(sag)->sg_sin.sin_addr)) 63*0Sstevel@tonic-gate #define SGFAM(sag) ((sag)->sg_family == AF_INET6 ? \ 64*0Sstevel@tonic-gate AF_INET6 : AF_INET) 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #define SG_IS_UNSPECIFIED(sag) \ 67*0Sstevel@tonic-gate ((sag)->sg_family == AF_INET6 ? \ 68*0Sstevel@tonic-gate IN6_IS_ADDR_UNSPECIFIED(&(sag)->sg_sin6.sin6_addr) : \ 69*0Sstevel@tonic-gate (sag)->sg_sin.sin_addr.s_addr == 0) 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate #define VALID_ADDRTYPE(t) ((t) == AF_INET || (t) == AF_INET6) 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate #ifndef IPV6_ABITS 74*0Sstevel@tonic-gate #define IPV6_ABITS 128 /* Size of IPV6 address in bits */ 75*0Sstevel@tonic-gate #endif 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate #else /* HAVE_IPV6 */ 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate #define SGADDRSZ(sag) sizeof(struct in_addr) 80*0Sstevel@tonic-gate #define SGSOCKADDRSZ(sag) sizeof(struct sockaddr_in) 81*0Sstevel@tonic-gate #define SGPORT(sag) ((sag)->sg_sin.sin_port) 82*0Sstevel@tonic-gate #define SGADDRP(sag) ((char*) &(sag)->sg_sin.sin_addr) 83*0Sstevel@tonic-gate #define SGFAM(sag) AF_INET 84*0Sstevel@tonic-gate #define SG_IS_UNSPECIFIED(sag) ((sag)->sg_sin.sin_addr.s_addr == 0) 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate #define VALID_ADDRTYPE(t) ((t) == AF_INET) 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate #endif /* HAVE_IPV6 */ 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate struct host_info { 91*0Sstevel@tonic-gate char name[STRING_LENGTH]; /* access via eval_hostname(host) */ 92*0Sstevel@tonic-gate char addr[STRING_LENGTH]; /* access via eval_hostaddr(host) */ 93*0Sstevel@tonic-gate struct sockaddr_gen *sin; /* socket address or 0 */ 94*0Sstevel@tonic-gate struct t_unitdata *unit; /* TLI transport address or 0 */ 95*0Sstevel@tonic-gate struct request_info *request; /* for shared information */ 96*0Sstevel@tonic-gate }; 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /* Structure to describe what we know about a service request. */ 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate struct request_info { 101*0Sstevel@tonic-gate int fd; /* socket handle */ 102*0Sstevel@tonic-gate char user[STRING_LENGTH]; /* access via eval_user(request) */ 103*0Sstevel@tonic-gate char daemon[STRING_LENGTH]; /* access via eval_daemon(request) */ 104*0Sstevel@tonic-gate char pid[10]; /* access via eval_pid(request) */ 105*0Sstevel@tonic-gate struct host_info client[1]; /* client endpoint info */ 106*0Sstevel@tonic-gate struct host_info server[1]; /* server endpoint info */ 107*0Sstevel@tonic-gate void (*sink) (); /* datagram sink function or 0 */ 108*0Sstevel@tonic-gate void (*hostname) (); /* address to printable hostname */ 109*0Sstevel@tonic-gate void (*hostaddr) (); /* address to printable address */ 110*0Sstevel@tonic-gate void (*cleanup) (); /* cleanup function or 0 */ 111*0Sstevel@tonic-gate struct netconfig *config; /* netdir handle */ 112*0Sstevel@tonic-gate }; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate /* Common string operations. Less clutter should be more readable. */ 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate #define STRN_CPY(d,s,l) { strncpy((d),(s),(l)); (d)[(l)-1] = 0; } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate #define STRN_EQ(x,y,l) (strncasecmp((x),(y),(l)) == 0) 119*0Sstevel@tonic-gate #define STRN_NE(x,y,l) (strncasecmp((x),(y),(l)) != 0) 120*0Sstevel@tonic-gate #define STR_EQ(x,y) (strcasecmp((x),(y)) == 0) 121*0Sstevel@tonic-gate #define STR_NE(x,y) (strcasecmp((x),(y)) != 0) 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate /* 124*0Sstevel@tonic-gate * Initially, all above strings have the empty value. Information that 125*0Sstevel@tonic-gate * cannot be determined at runtime is set to "unknown", so that we can 126*0Sstevel@tonic-gate * distinguish between `unavailable' and `not yet looked up'. A hostname 127*0Sstevel@tonic-gate * that we do not believe in is set to "paranoid". 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate #define STRING_UNKNOWN "unknown" /* lookup failed */ 131*0Sstevel@tonic-gate #define STRING_PARANOID "paranoid" /* hostname conflict */ 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate extern char unknown[]; 134*0Sstevel@tonic-gate extern char paranoid[]; 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate #define HOSTNAME_KNOWN(s) (STR_NE((s),unknown) && STR_NE((s),paranoid)) 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate #ifdef HAVE_IPV6 139*0Sstevel@tonic-gate #define NOT_INADDR(s) (strchr(s,':') == 0 && s[strspn(s,"0123456789./")] != 0) 140*0Sstevel@tonic-gate #else 141*0Sstevel@tonic-gate #define NOT_INADDR(s) (s[strspn(s,"0123456789./")] != 0) 142*0Sstevel@tonic-gate #endif 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /* Global functions. */ 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT) 147*0Sstevel@tonic-gate extern void fromhost(); /* get/validate client host info */ 148*0Sstevel@tonic-gate #else 149*0Sstevel@tonic-gate #define fromhost sock_host /* no TLI support needed */ 150*0Sstevel@tonic-gate #endif 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate extern int hosts_access(); /* access control */ 153*0Sstevel@tonic-gate extern void shell_cmd(); /* execute shell command */ 154*0Sstevel@tonic-gate extern char *percent_x(); /* do %<char> expansion */ 155*0Sstevel@tonic-gate extern void rfc931(); /* client name from RFC 931 daemon */ 156*0Sstevel@tonic-gate extern void clean_exit(); /* clean up and exit */ 157*0Sstevel@tonic-gate extern void refuse(); /* clean up and exit */ 158*0Sstevel@tonic-gate extern char *xgets(); /* fgets() on steroids */ 159*0Sstevel@tonic-gate extern char *split_at(); /* strchr() and split */ 160*0Sstevel@tonic-gate extern unsigned long dot_quad_addr(); /* restricted inet_addr() */ 161*0Sstevel@tonic-gate extern int numeric_addr(); /* IP4/IP6 inet_addr (restricted) */ 162*0Sstevel@tonic-gate extern struct hostent *tcpd_gethostbyname(); 163*0Sstevel@tonic-gate /* IP4/IP6 gethostbyname */ 164*0Sstevel@tonic-gate #ifdef HAVE_IPV6 165*0Sstevel@tonic-gate extern char *skip_ipv6_addrs(); /* skip over colons in IPv6 addrs */ 166*0Sstevel@tonic-gate #else 167*0Sstevel@tonic-gate #define skip_ipv6_addrs(x) x 168*0Sstevel@tonic-gate #endif 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate /* Global variables. */ 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate extern int allow_severity; /* for connection logging */ 173*0Sstevel@tonic-gate extern int deny_severity; /* for connection logging */ 174*0Sstevel@tonic-gate extern char *hosts_allow_table; /* for verification mode redirection */ 175*0Sstevel@tonic-gate extern char *hosts_deny_table; /* for verification mode redirection */ 176*0Sstevel@tonic-gate extern int hosts_access_verbose; /* for verbose matching mode */ 177*0Sstevel@tonic-gate extern int rfc931_timeout; /* user lookup timeout */ 178*0Sstevel@tonic-gate extern int resident; /* > 0 if resident process */ 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /* 181*0Sstevel@tonic-gate * Routines for controlled initialization and update of request structure 182*0Sstevel@tonic-gate * attributes. Each attribute has its own key. 183*0Sstevel@tonic-gate */ 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate #ifdef __STDC__ 186*0Sstevel@tonic-gate extern struct request_info *request_init(struct request_info *,...); 187*0Sstevel@tonic-gate extern struct request_info *request_set(struct request_info *,...); 188*0Sstevel@tonic-gate #else 189*0Sstevel@tonic-gate extern struct request_info *request_init(); /* initialize request */ 190*0Sstevel@tonic-gate extern struct request_info *request_set(); /* update request structure */ 191*0Sstevel@tonic-gate #endif 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate #define RQ_FILE 1 /* file descriptor */ 194*0Sstevel@tonic-gate #define RQ_DAEMON 2 /* server process (argv[0]) */ 195*0Sstevel@tonic-gate #define RQ_USER 3 /* client user name */ 196*0Sstevel@tonic-gate #define RQ_CLIENT_NAME 4 /* client host name */ 197*0Sstevel@tonic-gate #define RQ_CLIENT_ADDR 5 /* client host address */ 198*0Sstevel@tonic-gate #define RQ_CLIENT_SIN 6 /* client endpoint (internal) */ 199*0Sstevel@tonic-gate #define RQ_SERVER_NAME 7 /* server host name */ 200*0Sstevel@tonic-gate #define RQ_SERVER_ADDR 8 /* server host address */ 201*0Sstevel@tonic-gate #define RQ_SERVER_SIN 9 /* server endpoint (internal) */ 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate /* 204*0Sstevel@tonic-gate * Routines for delayed evaluation of request attributes. Each attribute 205*0Sstevel@tonic-gate * type has its own access method. The trivial ones are implemented by 206*0Sstevel@tonic-gate * macros. The other ones are wrappers around the transport-specific host 207*0Sstevel@tonic-gate * name, address, and client user lookup methods. The request_info and 208*0Sstevel@tonic-gate * host_info structures serve as caches for the lookup results. 209*0Sstevel@tonic-gate */ 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate extern char *eval_user(); /* client user */ 212*0Sstevel@tonic-gate extern char *eval_hostname(); /* printable hostname */ 213*0Sstevel@tonic-gate extern char *eval_hostaddr(); /* printable host address */ 214*0Sstevel@tonic-gate extern char *eval_hostinfo(); /* host name or address */ 215*0Sstevel@tonic-gate extern char *eval_client(); /* whatever is available */ 216*0Sstevel@tonic-gate extern char *eval_server(); /* whatever is available */ 217*0Sstevel@tonic-gate #define eval_daemon(r) ((r)->daemon) /* daemon process name */ 218*0Sstevel@tonic-gate #define eval_pid(r) ((r)->pid) /* process id */ 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate /* Socket-specific methods, including DNS hostname lookups. */ 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate extern void sock_host(); /* look up endpoint addresses */ 223*0Sstevel@tonic-gate extern void sock_hostname(); /* translate address to hostname */ 224*0Sstevel@tonic-gate extern void sock_hostaddr(); /* address to printable address */ 225*0Sstevel@tonic-gate #define sock_methods(r) \ 226*0Sstevel@tonic-gate { (r)->hostname = sock_hostname; (r)->hostaddr = sock_hostaddr; } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate /* The System V Transport-Level Interface (TLI) interface. */ 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT) 231*0Sstevel@tonic-gate extern void tli_host(); /* look up endpoint addresses etc. */ 232*0Sstevel@tonic-gate #endif 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate /* 235*0Sstevel@tonic-gate * Problem reporting interface. Additional file/line context is reported 236*0Sstevel@tonic-gate * when available. The jump buffer (tcpd_buf) is not declared here, or 237*0Sstevel@tonic-gate * everyone would have to include <setjmp.h>. 238*0Sstevel@tonic-gate */ 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate #ifdef __STDC__ 241*0Sstevel@tonic-gate extern void tcpd_warn(char *, ...); /* report problem and proceed */ 242*0Sstevel@tonic-gate extern void tcpd_jump(char *, ...); /* report problem and jump */ 243*0Sstevel@tonic-gate #else 244*0Sstevel@tonic-gate extern void tcpd_warn(); 245*0Sstevel@tonic-gate extern void tcpd_jump(); 246*0Sstevel@tonic-gate #endif 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate struct tcpd_context { 249*0Sstevel@tonic-gate char *file; /* current file */ 250*0Sstevel@tonic-gate int line; /* current line */ 251*0Sstevel@tonic-gate }; 252*0Sstevel@tonic-gate extern struct tcpd_context tcpd_context; 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate /* 255*0Sstevel@tonic-gate * While processing access control rules, error conditions are handled by 256*0Sstevel@tonic-gate * jumping back into the hosts_access() routine. This is cleaner than 257*0Sstevel@tonic-gate * checking the return value of each and every silly little function. The 258*0Sstevel@tonic-gate * (-1) returns are here because zero is already taken by longjmp(). 259*0Sstevel@tonic-gate */ 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate #define AC_PERMIT 1 /* permit access */ 262*0Sstevel@tonic-gate #define AC_DENY (-1) /* deny_access */ 263*0Sstevel@tonic-gate #define AC_ERROR AC_DENY /* XXX */ 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate /* 266*0Sstevel@tonic-gate * In verification mode an option function should just say what it would do, 267*0Sstevel@tonic-gate * instead of really doing it. An option function that would not return 268*0Sstevel@tonic-gate * should clear the dry_run flag to inform the caller of this unusual 269*0Sstevel@tonic-gate * behavior. 270*0Sstevel@tonic-gate */ 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate extern void process_options(); /* execute options */ 273*0Sstevel@tonic-gate extern int dry_run; /* verification flag */ 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate /* Bug workarounds. */ 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate #ifdef INET_ADDR_BUG /* inet_addr() returns struct */ 278*0Sstevel@tonic-gate #define inet_addr fix_inet_addr 279*0Sstevel@tonic-gate extern long fix_inet_addr(); 280*0Sstevel@tonic-gate #endif 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate #ifdef BROKEN_FGETS /* partial reads from sockets */ 283*0Sstevel@tonic-gate #define fgets fix_fgets 284*0Sstevel@tonic-gate extern char *fix_fgets(); 285*0Sstevel@tonic-gate #endif 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate #ifdef RECVFROM_BUG /* no address family info */ 288*0Sstevel@tonic-gate #define recvfrom fix_recvfrom 289*0Sstevel@tonic-gate extern int fix_recvfrom(); 290*0Sstevel@tonic-gate #endif 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate #ifdef GETPEERNAME_BUG /* claims success with UDP */ 293*0Sstevel@tonic-gate #define getpeername fix_getpeername 294*0Sstevel@tonic-gate extern int fix_getpeername(); 295*0Sstevel@tonic-gate #endif 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate #ifdef SOLARIS_24_GETHOSTBYNAME_BUG /* lists addresses as aliases */ 298*0Sstevel@tonic-gate #define gethostbyname fix_gethostbyname 299*0Sstevel@tonic-gate extern struct hostent *fix_gethostbyname(); 300*0Sstevel@tonic-gate #endif 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate #ifdef USE_STRSEP /* libc calls strtok() */ 303*0Sstevel@tonic-gate #define strtok fix_strtok 304*0Sstevel@tonic-gate extern char *fix_strtok(); 305*0Sstevel@tonic-gate #endif 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate #ifdef LIBC_CALLS_STRTOK /* libc calls strtok() */ 308*0Sstevel@tonic-gate #define strtok my_strtok 309*0Sstevel@tonic-gate extern char *my_strtok(); 310*0Sstevel@tonic-gate #endif 311