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