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