1*f1fab66eSDavid van Moolenbroek /* $NetBSD: tcpd.h,v 1.14 2012/03/22 22:59:43 joerg Exp $ */ 2*f1fab66eSDavid van Moolenbroek /* 3*f1fab66eSDavid van Moolenbroek * @(#) tcpd.h 1.5 96/03/19 16:22:24 4*f1fab66eSDavid van Moolenbroek * 5*f1fab66eSDavid van Moolenbroek * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 6*f1fab66eSDavid van Moolenbroek */ 7*f1fab66eSDavid van Moolenbroek 8*f1fab66eSDavid van Moolenbroek #include <sys/cdefs.h> 9*f1fab66eSDavid van Moolenbroek #include <stdio.h> 10*f1fab66eSDavid van Moolenbroek 11*f1fab66eSDavid van Moolenbroek /* Structure to describe one communications endpoint. */ 12*f1fab66eSDavid van Moolenbroek 13*f1fab66eSDavid van Moolenbroek #define STRING_LENGTH 128 /* hosts, users, processes */ 14*f1fab66eSDavid van Moolenbroek 15*f1fab66eSDavid van Moolenbroek struct host_info { 16*f1fab66eSDavid van Moolenbroek char name[STRING_LENGTH]; /* access via eval_hostname(host) */ 17*f1fab66eSDavid van Moolenbroek char addr[STRING_LENGTH]; /* access via eval_hostaddr(host) */ 18*f1fab66eSDavid van Moolenbroek struct sockaddr *sin; /* socket address or 0 */ 19*f1fab66eSDavid van Moolenbroek struct t_unitdata *unit; /* TLI transport address or 0 */ 20*f1fab66eSDavid van Moolenbroek struct request_info *request; /* for shared information */ 21*f1fab66eSDavid van Moolenbroek }; 22*f1fab66eSDavid van Moolenbroek 23*f1fab66eSDavid van Moolenbroek /* Structure to describe what we know about a service request. */ 24*f1fab66eSDavid van Moolenbroek 25*f1fab66eSDavid van Moolenbroek struct request_info { 26*f1fab66eSDavid van Moolenbroek int fd; /* socket handle */ 27*f1fab66eSDavid van Moolenbroek char user[STRING_LENGTH]; /* access via eval_user(request) */ 28*f1fab66eSDavid van Moolenbroek char daemon[STRING_LENGTH]; /* access via eval_daemon(request) */ 29*f1fab66eSDavid van Moolenbroek char pid[10]; /* access via eval_pid(request) */ 30*f1fab66eSDavid van Moolenbroek struct host_info client[1]; /* client endpoint info */ 31*f1fab66eSDavid van Moolenbroek struct host_info server[1]; /* server endpoint info */ 32*f1fab66eSDavid van Moolenbroek void (*sink)(int); /* datagram sink function or 0 */ 33*f1fab66eSDavid van Moolenbroek void (*hostname)(struct host_info *); /* address to printable hostname */ 34*f1fab66eSDavid van Moolenbroek void (*hostaddr)(struct host_info *); /* address to printable address */ 35*f1fab66eSDavid van Moolenbroek void (*cleanup)(void); /* cleanup function or 0 */ 36*f1fab66eSDavid van Moolenbroek struct netconfig *config; /* netdir handle */ 37*f1fab66eSDavid van Moolenbroek }; 38*f1fab66eSDavid van Moolenbroek 39*f1fab66eSDavid van Moolenbroek /* Common string operations. Less clutter should be more readable. */ 40*f1fab66eSDavid van Moolenbroek 41*f1fab66eSDavid van Moolenbroek #define STRN_CPY(d,s,l) { strncpy((d),(s),(l)); (d)[(l)-1] = 0; } 42*f1fab66eSDavid van Moolenbroek 43*f1fab66eSDavid van Moolenbroek #define STRN_EQ(x,y,l) (strncasecmp((x),(y),(l)) == 0) 44*f1fab66eSDavid van Moolenbroek #define STRN_NE(x,y,l) (strncasecmp((x),(y),(l)) != 0) 45*f1fab66eSDavid van Moolenbroek #define STR_EQ(x,y) (strcasecmp((x),(y)) == 0) 46*f1fab66eSDavid van Moolenbroek #define STR_NE(x,y) (strcasecmp((x),(y)) != 0) 47*f1fab66eSDavid van Moolenbroek 48*f1fab66eSDavid van Moolenbroek /* 49*f1fab66eSDavid van Moolenbroek * Initially, all above strings have the empty value. Information that 50*f1fab66eSDavid van Moolenbroek * cannot be determined at runtime is set to "unknown", so that we can 51*f1fab66eSDavid van Moolenbroek * distinguish between `unavailable' and `not yet looked up'. A hostname 52*f1fab66eSDavid van Moolenbroek * that we do not believe in is set to "paranoid". 53*f1fab66eSDavid van Moolenbroek */ 54*f1fab66eSDavid van Moolenbroek 55*f1fab66eSDavid van Moolenbroek #define STRING_UNKNOWN "unknown" /* lookup failed */ 56*f1fab66eSDavid van Moolenbroek #define STRING_PARANOID "paranoid" /* hostname conflict */ 57*f1fab66eSDavid van Moolenbroek 58*f1fab66eSDavid van Moolenbroek __BEGIN_DECLS 59*f1fab66eSDavid van Moolenbroek extern char unknown[]; 60*f1fab66eSDavid van Moolenbroek extern char paranoid[]; 61*f1fab66eSDavid van Moolenbroek __END_DECLS 62*f1fab66eSDavid van Moolenbroek 63*f1fab66eSDavid van Moolenbroek #define HOSTNAME_KNOWN(s) (STR_NE((s),unknown) && STR_NE((s),paranoid)) 64*f1fab66eSDavid van Moolenbroek 65*f1fab66eSDavid van Moolenbroek #define NOT_INADDR(s) (s[strspn(s,"01234567890./")] != 0) 66*f1fab66eSDavid van Moolenbroek 67*f1fab66eSDavid van Moolenbroek /* Global functions. */ 68*f1fab66eSDavid van Moolenbroek 69*f1fab66eSDavid van Moolenbroek __BEGIN_DECLS 70*f1fab66eSDavid van Moolenbroek #define fromhost sock_host /* no TLI support needed */ 71*f1fab66eSDavid van Moolenbroek 72*f1fab66eSDavid van Moolenbroek extern int hosts_access /* access control */ 73*f1fab66eSDavid van Moolenbroek (struct request_info *); 74*f1fab66eSDavid van Moolenbroek extern int hosts_ctl /* limited interface to hosts_access */ 75*f1fab66eSDavid van Moolenbroek (char *, char *, char *, char *); 76*f1fab66eSDavid van Moolenbroek extern void shell_cmd /* execute shell command */ 77*f1fab66eSDavid van Moolenbroek (char *); 78*f1fab66eSDavid van Moolenbroek extern char *percent_x /* do %<char> expansion */ 79*f1fab66eSDavid van Moolenbroek (char *, int, char *, struct request_info *); 80*f1fab66eSDavid van Moolenbroek extern void rfc931 /* client name from RFC 931 daemon */ 81*f1fab66eSDavid van Moolenbroek (struct sockaddr *, struct sockaddr *, char *); 82*f1fab66eSDavid van Moolenbroek __dead extern void clean_exit /* clean up and exit */ 83*f1fab66eSDavid van Moolenbroek (struct request_info *); 84*f1fab66eSDavid van Moolenbroek __dead extern void refuse /* clean up and exit */ 85*f1fab66eSDavid van Moolenbroek (struct request_info *); 86*f1fab66eSDavid van Moolenbroek extern char *xgets /* fgets() on steroids */ 87*f1fab66eSDavid van Moolenbroek (char *, int, FILE *); 88*f1fab66eSDavid van Moolenbroek extern char *split_at /* strchr() and split */ 89*f1fab66eSDavid van Moolenbroek (char *, int); 90*f1fab66eSDavid van Moolenbroek extern int dot_quad_addr /* restricted inet_aton() */ 91*f1fab66eSDavid van Moolenbroek (char *, unsigned long *); 92*f1fab66eSDavid van Moolenbroek 93*f1fab66eSDavid van Moolenbroek /* Global variables. */ 94*f1fab66eSDavid van Moolenbroek 95*f1fab66eSDavid van Moolenbroek extern int allow_severity; /* for connection logging */ 96*f1fab66eSDavid van Moolenbroek extern int deny_severity; /* for connection logging */ 97*f1fab66eSDavid van Moolenbroek extern const char *hosts_allow_table; /* for verification mode redirection */ 98*f1fab66eSDavid van Moolenbroek extern const char *hosts_deny_table; /* for verification mode redirection */ 99*f1fab66eSDavid van Moolenbroek extern int hosts_access_verbose; /* for verbose matching mode */ 100*f1fab66eSDavid van Moolenbroek extern int rfc931_timeout; /* user lookup timeout */ 101*f1fab66eSDavid van Moolenbroek extern int resident; /* > 0 if resident process */ 102*f1fab66eSDavid van Moolenbroek 103*f1fab66eSDavid van Moolenbroek /* 104*f1fab66eSDavid van Moolenbroek * Routines for controlled initialization and update of request structure 105*f1fab66eSDavid van Moolenbroek * attributes. Each attribute has its own key. 106*f1fab66eSDavid van Moolenbroek */ 107*f1fab66eSDavid van Moolenbroek 108*f1fab66eSDavid van Moolenbroek extern struct request_info *request_init /* initialize request */ 109*f1fab66eSDavid van Moolenbroek (struct request_info *,...); 110*f1fab66eSDavid van Moolenbroek extern struct request_info *request_set /* update request structure */ 111*f1fab66eSDavid van Moolenbroek (struct request_info *,...); 112*f1fab66eSDavid van Moolenbroek 113*f1fab66eSDavid van Moolenbroek #define RQ_FILE 1 /* file descriptor */ 114*f1fab66eSDavid van Moolenbroek #define RQ_DAEMON 2 /* server process (argv[0]) */ 115*f1fab66eSDavid van Moolenbroek #define RQ_USER 3 /* client user name */ 116*f1fab66eSDavid van Moolenbroek #define RQ_CLIENT_NAME 4 /* client host name */ 117*f1fab66eSDavid van Moolenbroek #define RQ_CLIENT_ADDR 5 /* client host address */ 118*f1fab66eSDavid van Moolenbroek #define RQ_CLIENT_SIN 6 /* client endpoint (internal) */ 119*f1fab66eSDavid van Moolenbroek #define RQ_SERVER_NAME 7 /* server host name */ 120*f1fab66eSDavid van Moolenbroek #define RQ_SERVER_ADDR 8 /* server host address */ 121*f1fab66eSDavid van Moolenbroek #define RQ_SERVER_SIN 9 /* server endpoint (internal) */ 122*f1fab66eSDavid van Moolenbroek 123*f1fab66eSDavid van Moolenbroek /* 124*f1fab66eSDavid van Moolenbroek * Routines for delayed evaluation of request attributes. Each attribute 125*f1fab66eSDavid van Moolenbroek * type has its own access method. The trivial ones are implemented by 126*f1fab66eSDavid van Moolenbroek * macros. The other ones are wrappers around the transport-specific host 127*f1fab66eSDavid van Moolenbroek * name, address, and client user lookup methods. The request_info and 128*f1fab66eSDavid van Moolenbroek * host_info structures serve as caches for the lookup results. 129*f1fab66eSDavid van Moolenbroek */ 130*f1fab66eSDavid van Moolenbroek 131*f1fab66eSDavid van Moolenbroek extern char *eval_user /* client user */ 132*f1fab66eSDavid van Moolenbroek (struct request_info *); 133*f1fab66eSDavid van Moolenbroek extern char *eval_hostname /* printable hostname */ 134*f1fab66eSDavid van Moolenbroek (struct host_info *); 135*f1fab66eSDavid van Moolenbroek extern char *eval_hostaddr /* printable host address */ 136*f1fab66eSDavid van Moolenbroek (struct host_info *); 137*f1fab66eSDavid van Moolenbroek extern char *eval_hostinfo /* host name or address */ 138*f1fab66eSDavid van Moolenbroek (struct host_info *); 139*f1fab66eSDavid van Moolenbroek extern char *eval_client /* whatever is available */ 140*f1fab66eSDavid van Moolenbroek (struct request_info *); 141*f1fab66eSDavid van Moolenbroek extern char *eval_server /* whatever is available */ 142*f1fab66eSDavid van Moolenbroek (struct request_info *); 143*f1fab66eSDavid van Moolenbroek #define eval_daemon(r) ((r)->daemon) /* daemon process name */ 144*f1fab66eSDavid van Moolenbroek #define eval_pid(r) ((r)->pid) /* process id */ 145*f1fab66eSDavid van Moolenbroek 146*f1fab66eSDavid van Moolenbroek /* Socket-specific methods, including DNS hostname lookups. */ 147*f1fab66eSDavid van Moolenbroek 148*f1fab66eSDavid van Moolenbroek extern void sock_host /* look up endpoint addresses */ 149*f1fab66eSDavid van Moolenbroek (struct request_info *); 150*f1fab66eSDavid van Moolenbroek extern void sock_hostname /* translate address to hostname */ 151*f1fab66eSDavid van Moolenbroek (struct host_info *); 152*f1fab66eSDavid van Moolenbroek extern void sock_hostaddr /* address to printable address */ 153*f1fab66eSDavid van Moolenbroek (struct host_info *); 154*f1fab66eSDavid van Moolenbroek #define sock_methods(r) \ 155*f1fab66eSDavid van Moolenbroek { (r)->hostname = sock_hostname; (r)->hostaddr = sock_hostaddr; } 156*f1fab66eSDavid van Moolenbroek 157*f1fab66eSDavid van Moolenbroek /* The System V Transport-Level Interface (TLI) interface. */ 158*f1fab66eSDavid van Moolenbroek 159*f1fab66eSDavid van Moolenbroek #if defined(TLI) || defined(PTX) || defined(TLI_SEQUENT) 160*f1fab66eSDavid van Moolenbroek extern void tli_host /* look up endpoint addresses etc. */ 161*f1fab66eSDavid van Moolenbroek (struct request_info *); 162*f1fab66eSDavid van Moolenbroek #endif 163*f1fab66eSDavid van Moolenbroek 164*f1fab66eSDavid van Moolenbroek /* 165*f1fab66eSDavid van Moolenbroek * Problem reporting interface. Additional file/line context is reported 166*f1fab66eSDavid van Moolenbroek * when available. The jump buffer (tcpd_buf) is not declared here, or 167*f1fab66eSDavid van Moolenbroek * everyone would have to include <setjmp.h>. 168*f1fab66eSDavid van Moolenbroek */ 169*f1fab66eSDavid van Moolenbroek 170*f1fab66eSDavid van Moolenbroek /* Report problem and proceed */ 171*f1fab66eSDavid van Moolenbroek void tcpd_warn(const char *, ...) __printflike(1, 2); 172*f1fab66eSDavid van Moolenbroek 173*f1fab66eSDavid van Moolenbroek /* Report problem and jump */ 174*f1fab66eSDavid van Moolenbroek void tcpd_jump(const char *, ...) __dead __printflike(1, 2); 175*f1fab66eSDavid van Moolenbroek __END_DECLS 176*f1fab66eSDavid van Moolenbroek 177*f1fab66eSDavid van Moolenbroek struct tcpd_context { 178*f1fab66eSDavid van Moolenbroek const char *file; /* current file */ 179*f1fab66eSDavid van Moolenbroek int line; /* current line */ 180*f1fab66eSDavid van Moolenbroek }; 181*f1fab66eSDavid van Moolenbroek __BEGIN_DECLS 182*f1fab66eSDavid van Moolenbroek extern struct tcpd_context tcpd_context; 183*f1fab66eSDavid van Moolenbroek __END_DECLS 184*f1fab66eSDavid van Moolenbroek 185*f1fab66eSDavid van Moolenbroek /* 186*f1fab66eSDavid van Moolenbroek * While processing access control rules, error conditions are handled by 187*f1fab66eSDavid van Moolenbroek * jumping back into the hosts_access() routine. This is cleaner than 188*f1fab66eSDavid van Moolenbroek * checking the return value of each and every silly little function. The 189*f1fab66eSDavid van Moolenbroek * (-1) returns are here because zero is already taken by longjmp(). 190*f1fab66eSDavid van Moolenbroek */ 191*f1fab66eSDavid van Moolenbroek 192*f1fab66eSDavid van Moolenbroek #define AC_PERMIT 1 /* permit access */ 193*f1fab66eSDavid van Moolenbroek #define AC_DENY (-1) /* deny_access */ 194*f1fab66eSDavid van Moolenbroek #define AC_ERROR AC_DENY /* XXX */ 195*f1fab66eSDavid van Moolenbroek 196*f1fab66eSDavid van Moolenbroek /* 197*f1fab66eSDavid van Moolenbroek * In verification mode an option function should just say what it would do, 198*f1fab66eSDavid van Moolenbroek * instead of really doing it. An option function that would not return 199*f1fab66eSDavid van Moolenbroek * should clear the dry_run flag to inform the caller of this unusual 200*f1fab66eSDavid van Moolenbroek * behavior. 201*f1fab66eSDavid van Moolenbroek */ 202*f1fab66eSDavid van Moolenbroek 203*f1fab66eSDavid van Moolenbroek __BEGIN_DECLS 204*f1fab66eSDavid van Moolenbroek extern void process_options /* execute options */ 205*f1fab66eSDavid van Moolenbroek (char *, struct request_info *); 206*f1fab66eSDavid van Moolenbroek extern int dry_run; /* verification flag */ 207*f1fab66eSDavid van Moolenbroek extern void fix_options /* get rid of IP-level socket options */ 208*f1fab66eSDavid van Moolenbroek (struct request_info *); 209*f1fab66eSDavid van Moolenbroek __END_DECLS 210