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