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