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