Merge branch 'vendor/OPENSSH'
[dragonfly.git] / contrib / tcp_wrappers / eval.c
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 /* System libraries. */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 /* Local stuff. */
27
28 #include "tcpd.h"
29
30  /*
31   * When a string has the value STRING_UNKNOWN, it means: don't bother, I
32   * tried to look up the data but it was unavailable for some reason. When a
33   * host name has the value STRING_PARANOID it means there was a name/address
34   * conflict.
35   */
36 char    unknown[] = STRING_UNKNOWN;
37 char    paranoid[] = STRING_PARANOID;
38
39 /* eval_user - look up user name */
40
41 char   *eval_user(request)
42 struct request_info *request;
43 {
44     if (request->user[0] == 0) {
45         strcpy(request->user, unknown);
46         if (request->sink == 0 && request->client->sin && request->server->sin)
47             rfc931(request->client->sin, request->server->sin, request->user);
48     }
49     return (request->user);
50 }
51
52 /* eval_hostaddr - look up printable address */
53
54 char   *eval_hostaddr(host)
55 struct host_info *host;
56 {
57     if (host->addr[0] == 0) {
58         strcpy(host->addr, unknown);
59         if (host->request->hostaddr != 0)
60             host->request->hostaddr(host);
61     }
62     return (host->addr);
63 }
64
65 /* eval_hostname - look up host name */
66
67 char   *eval_hostname(host)
68 struct host_info *host;
69 {
70     if (host->name[0] == 0) {
71         strcpy(host->name, unknown);
72         if (host->request->hostname != 0)
73             host->request->hostname(host);
74     }
75     return (host->name);
76 }
77
78 /* eval_hostinfo - return string with host name (preferred) or address */
79
80 char   *eval_hostinfo(host)
81 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(request)
100 struct request_info *request;
101 {
102     static char both[2 * STRING_LENGTH];
103     char   *hostinfo = eval_hostinfo(request->client);
104
105 #ifndef ALWAYS_RFC931                           /* no implicit user lookups */
106     if (request->user[0] == 0)
107         return (hostinfo);
108 #endif
109     if (STR_NE(eval_user(request), unknown)) {
110         sprintf(both, "%s@%s", request->user, hostinfo);
111         return (both);
112     } else {
113         return (hostinfo);
114     }
115 }
116
117 /* eval_server - return string with as much about the server as we know */
118
119 char   *eval_server(request)
120 struct request_info *request;
121 {
122     static char both[2 * STRING_LENGTH];
123     char   *host = eval_hostinfo(request->server);
124     char   *daemon = eval_daemon(request);
125
126     if (STR_NE(host, unknown)) {
127         sprintf(both, "%s@%s", daemon, host);
128         return (both);
129     } else {
130         return (daemon);
131     }
132 }