1 1.7 matt /* $NetBSD: eval.c,v 1.7 2012/03/21 10:10:37 matt Exp $ */ 2 1.3 christos 3 1.1 mrg /* 4 1.1 mrg * Routines for controlled evaluation of host names, user names, and so on. 5 1.1 mrg * They are, in fact, wrappers around the functions that are specific for 6 1.1 mrg * the sockets or TLI programming interfaces. The request_info and host_info 7 1.1 mrg * structures are used for result cacheing. 8 1.5 simonb * 9 1.1 mrg * These routines allows us to postpone expensive operations until their 10 1.1 mrg * results are really needed. Examples are hostname lookups and double 11 1.1 mrg * checks, or username lookups. Information that cannot be retrieved is 12 1.1 mrg * given the value "unknown" ("paranoid" in case of hostname problems). 13 1.5 simonb * 14 1.1 mrg * When ALWAYS_HOSTNAME is off, hostname lookup is done only when required by 15 1.1 mrg * tcpd paranoid mode, by access control patterns, or by %letter expansions. 16 1.5 simonb * 17 1.1 mrg * When ALWAYS_RFC931 mode is off, user lookup is done only when required by 18 1.1 mrg * access control patterns or %letter expansions. 19 1.5 simonb * 20 1.1 mrg * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 21 1.1 mrg */ 22 1.1 mrg 23 1.3 christos #include <sys/cdefs.h> 24 1.1 mrg #ifndef lint 25 1.3 christos #if 0 26 1.1 mrg static char sccsid[] = "@(#) eval.c 1.3 95/01/30 19:51:45"; 27 1.3 christos #else 28 1.7 matt __RCSID("$NetBSD: eval.c,v 1.7 2012/03/21 10:10:37 matt Exp $"); 29 1.3 christos #endif 30 1.1 mrg #endif 31 1.1 mrg 32 1.1 mrg /* System libraries. */ 33 1.1 mrg 34 1.1 mrg #include <stdio.h> 35 1.1 mrg #include <string.h> 36 1.1 mrg 37 1.1 mrg /* Local stuff. */ 38 1.1 mrg 39 1.1 mrg #include "tcpd.h" 40 1.1 mrg 41 1.1 mrg /* 42 1.1 mrg * When a string has the value STRING_UNKNOWN, it means: don't bother, I 43 1.1 mrg * tried to look up the data but it was unavailable for some reason. When a 44 1.1 mrg * host name has the value STRING_PARANOID it means there was a name/address 45 1.1 mrg * conflict. 46 1.1 mrg */ 47 1.1 mrg char unknown[] = STRING_UNKNOWN; 48 1.1 mrg char paranoid[] = STRING_PARANOID; 49 1.1 mrg 50 1.1 mrg /* eval_user - look up user name */ 51 1.1 mrg 52 1.7 matt char * 53 1.7 matt eval_user(struct request_info *request) 54 1.1 mrg { 55 1.1 mrg if (request->user[0] == 0) { 56 1.6 itojun (void)strlcpy(request->user, unknown, sizeof(request->user)); 57 1.1 mrg if (request->sink == 0 && request->client->sin && request->server->sin) 58 1.1 mrg rfc931(request->client->sin, request->server->sin, request->user); 59 1.1 mrg } 60 1.1 mrg return (request->user); 61 1.1 mrg } 62 1.1 mrg 63 1.1 mrg /* eval_hostaddr - look up printable address */ 64 1.1 mrg 65 1.7 matt char * 66 1.7 matt eval_hostaddr(struct host_info *host) 67 1.1 mrg { 68 1.1 mrg if (host->addr[0] == 0) { 69 1.6 itojun (void)strlcpy(host->addr, unknown, sizeof(host->addr)); 70 1.1 mrg if (host->request->hostaddr != 0) 71 1.1 mrg host->request->hostaddr(host); 72 1.1 mrg } 73 1.1 mrg return (host->addr); 74 1.1 mrg } 75 1.1 mrg 76 1.1 mrg /* eval_hostname - look up host name */ 77 1.1 mrg 78 1.7 matt char * 79 1.7 matt eval_hostname(struct host_info *host) 80 1.1 mrg { 81 1.1 mrg if (host->name[0] == 0) { 82 1.6 itojun (void)strlcpy(host->name, unknown, sizeof(host->name)); 83 1.1 mrg if (host->request->hostname != 0) 84 1.1 mrg host->request->hostname(host); 85 1.1 mrg } 86 1.1 mrg return (host->name); 87 1.1 mrg } 88 1.1 mrg 89 1.1 mrg /* eval_hostinfo - return string with host name (preferred) or address */ 90 1.1 mrg 91 1.7 matt char * 92 1.7 matt eval_hostinfo(struct host_info *host) 93 1.1 mrg { 94 1.1 mrg char *hostname; 95 1.1 mrg 96 1.1 mrg #ifndef ALWAYS_HOSTNAME /* no implicit host lookups */ 97 1.1 mrg if (host->name[0] == 0) 98 1.1 mrg return (eval_hostaddr(host)); 99 1.1 mrg #endif 100 1.1 mrg hostname = eval_hostname(host); 101 1.1 mrg if (HOSTNAME_KNOWN(hostname)) { 102 1.1 mrg return (host->name); 103 1.1 mrg } else { 104 1.1 mrg return (eval_hostaddr(host)); 105 1.1 mrg } 106 1.1 mrg } 107 1.1 mrg 108 1.1 mrg /* eval_client - return string with as much about the client as we know */ 109 1.1 mrg 110 1.7 matt char * 111 1.7 matt eval_client(struct request_info *request) 112 1.1 mrg { 113 1.1 mrg static char both[2 * STRING_LENGTH]; 114 1.1 mrg char *hostinfo = eval_hostinfo(request->client); 115 1.1 mrg 116 1.1 mrg #ifndef ALWAYS_RFC931 /* no implicit user lookups */ 117 1.1 mrg if (request->user[0] == 0) 118 1.1 mrg return (hostinfo); 119 1.1 mrg #endif 120 1.1 mrg if (STR_NE(eval_user(request), unknown)) { 121 1.2 mrg (void)snprintf(both, sizeof both, "%s@%s", request->user, hostinfo); 122 1.1 mrg return (both); 123 1.1 mrg } else { 124 1.1 mrg return (hostinfo); 125 1.1 mrg } 126 1.1 mrg } 127 1.1 mrg 128 1.1 mrg /* eval_server - return string with as much about the server as we know */ 129 1.1 mrg 130 1.7 matt char * 131 1.7 matt eval_server(struct request_info *request) 132 1.1 mrg { 133 1.1 mrg static char both[2 * STRING_LENGTH]; 134 1.1 mrg char *host = eval_hostinfo(request->server); 135 1.1 mrg char *daemon = eval_daemon(request); 136 1.1 mrg 137 1.1 mrg if (STR_NE(host, unknown)) { 138 1.2 mrg (void)snprintf(both, sizeof both, "%s@%s", daemon, host); 139 1.1 mrg return (both); 140 1.1 mrg } else { 141 1.1 mrg return (daemon); 142 1.1 mrg } 143 1.1 mrg } 144