1285612Sdelphij /*
2132451Sroberto  * Routines for controlled evaluation of host names, user names, and so on.
354359Sroberto  * They are, in fact, wrappers around the functions that are specific for
4285612Sdelphij  * the sockets or TLI programming interfaces. The request_info and host_info
5285612Sdelphij  * structures are used for result cacheing.
654359Sroberto  *
754359Sroberto  * These routines allows us to postpone expensive operations until their
854359Sroberto  * results are really needed. Examples are hostname lookups and double
954359Sroberto  * checks, or username lookups. Information that cannot be retrieved is
1054359Sroberto  * given the value "unknown" ("paranoid" in case of hostname problems).
1154359Sroberto  *
1254359Sroberto  * When ALWAYS_HOSTNAME is off, hostname lookup is done only when required by
1354359Sroberto  * tcpd paranoid mode, by access control patterns, or by %letter expansions.
1454359Sroberto  *
15106163Sroberto  * When ALWAYS_RFC931 mode is off, user lookup is done only when required by
16106163Sroberto  * access control patterns or %letter expansions.
1754359Sroberto  *
18285612Sdelphij  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
19285612Sdelphij  */
20285612Sdelphij
21285612Sdelphij#ifndef lint
22285612Sdelphijstatic char sccsid[] = "@(#) eval.c 1.3 95/01/30 19:51:45";
23285612Sdelphij#endif
24285612Sdelphij
25285612Sdelphij/* System libraries. */
26285612Sdelphij
27285612Sdelphij#include <stdio.h>
28285612Sdelphij#include <string.h>
29285612Sdelphij
30285612Sdelphij/* Local stuff. */
31285612Sdelphij
32285612Sdelphij#include "tcpd.h"
33285612Sdelphij
34285612Sdelphij /*
35285612Sdelphij  * When a string has the value STRING_UNKNOWN, it means: don't bother, I
36285612Sdelphij  * tried to look up the data but it was unavailable for some reason. When a
37285612Sdelphij  * host name has the value STRING_PARANOID it means there was a name/address
38285612Sdelphij  * conflict.
39285612Sdelphij  */
40285612Sdelphijchar    unknown[] = STRING_UNKNOWN;
41285612Sdelphijchar    paranoid[] = STRING_PARANOID;
42285612Sdelphij
43285612Sdelphij/* eval_user - look up user name */
44285612Sdelphij
45285612Sdelphijchar   *eval_user(request)
46285612Sdelphijstruct request_info *request;
47285612Sdelphij{
48285612Sdelphij    if (request->user[0] == 0) {
49285612Sdelphij	strcpy(request->user, unknown);
50285612Sdelphij	if (request->sink == 0 && request->client->sin && request->server->sin)
51285612Sdelphij	    rfc931(request->client->sin, request->server->sin, request->user);
52285612Sdelphij    }
53285612Sdelphij    return (request->user);
54285612Sdelphij}
55285612Sdelphij
56285612Sdelphij/* eval_hostaddr - look up printable address */
57285612Sdelphij
58285612Sdelphijchar   *eval_hostaddr(host)
59285612Sdelphijstruct host_info *host;
60285612Sdelphij{
61285612Sdelphij    if (host->addr[0] == 0) {
62285612Sdelphij	strcpy(host->addr, unknown);
63285612Sdelphij	if (host->request->hostaddr != 0)
64285612Sdelphij	    host->request->hostaddr(host);
65285612Sdelphij    }
66285612Sdelphij    return (host->addr);
67285612Sdelphij}
68285612Sdelphij
69285612Sdelphij/* eval_hostname - look up host name */
70285612Sdelphij
71285612Sdelphijchar   *eval_hostname(host)
72285612Sdelphijstruct host_info *host;
7354359Sroberto{
74200576Sroberto    if (host->name[0] == 0) {
7554359Sroberto	strcpy(host->name, unknown);
76200576Sroberto	if (host->request->hostname != 0)
77132451Sroberto	    host->request->hostname(host);
78132451Sroberto    }
79132451Sroberto    return (host->name);
80132451Sroberto}
8182498Sroberto
82132451Sroberto/* eval_hostinfo - return string with host name (preferred) or address */
8354359Sroberto
8454359Srobertochar   *eval_hostinfo(host)
8554359Srobertostruct host_info *host;
8654359Sroberto{
8754359Sroberto    char   *hostname;
8854359Sroberto
8954359Sroberto#ifndef ALWAYS_HOSTNAME				/* no implicit host lookups */
9054359Sroberto    if (host->name[0] == 0)
91285612Sdelphij	return (eval_hostaddr(host));
92182007Sroberto#endif
93182007Sroberto    hostname = eval_hostname(host);
94285612Sdelphij    if (HOSTNAME_KNOWN(hostname)) {
95285612Sdelphij	return (host->name);
96285612Sdelphij    } else {
97285612Sdelphij	return (eval_hostaddr(host));
98285612Sdelphij    }
99285612Sdelphij}
100285612Sdelphij
101285612Sdelphij/* eval_client - return string with as much about the client as we know */
102285612Sdelphij
103285612Sdelphijchar   *eval_client(request)
104330141Sdelphijstruct request_info *request;
105285612Sdelphij{
106285612Sdelphij    static char both[2 * STRING_LENGTH];
107285612Sdelphij    char   *hostinfo = eval_hostinfo(request->client);
108285612Sdelphij
109285612Sdelphij#ifndef ALWAYS_RFC931				/* no implicit user lookups */
110285612Sdelphij    if (request->user[0] == 0)
111285612Sdelphij	return (hostinfo);
112285612Sdelphij#endif
113316069Sdelphij    if (STR_NE(eval_user(request), unknown)) {
114285612Sdelphij	sprintf(both, "%s@%s", request->user, hostinfo);
115285612Sdelphij	return (both);
116285612Sdelphij    } else {
117285612Sdelphij	return (hostinfo);
118285612Sdelphij    }
119285612Sdelphij}
120285612Sdelphij
121285612Sdelphij/* eval_server - return string with as much about the server as we know */
122289997Sglebius
123285612Sdelphijchar   *eval_server(request)
124285612Sdelphijstruct request_info *request;
125285612Sdelphij{
126285612Sdelphij    static char both[2 * STRING_LENGTH];
127285612Sdelphij    char   *host = eval_hostinfo(request->server);
128285612Sdelphij    char   *daemon = eval_daemon(request);
129285612Sdelphij
130285612Sdelphij    if (STR_NE(host, unknown)) {
131285612Sdelphij	sprintf(both, "%s@%s", daemon, host);
132285612Sdelphij	return (both);
133285612Sdelphij    } else {
134182007Sroberto	return (daemon);
135182007Sroberto    }
136285612Sdelphij}
137182007Sroberto