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