xref: /illumos-gate/usr/src/lib/libwrap/eval.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate  /*
77c478bd9Sstevel@tonic-gate   * Routines for controlled evaluation of host names, user names, and so on.
87c478bd9Sstevel@tonic-gate   * They are, in fact, wrappers around the functions that are specific for
97c478bd9Sstevel@tonic-gate   * the sockets or TLI programming interfaces. The request_info and host_info
107c478bd9Sstevel@tonic-gate   * structures are used for result cacheing.
11*1da57d55SToomas Soome   *
127c478bd9Sstevel@tonic-gate   * These routines allows us to postpone expensive operations until their
137c478bd9Sstevel@tonic-gate   * results are really needed. Examples are hostname lookups and double
147c478bd9Sstevel@tonic-gate   * checks, or username lookups. Information that cannot be retrieved is
157c478bd9Sstevel@tonic-gate   * given the value "unknown" ("paranoid" in case of hostname problems).
16*1da57d55SToomas Soome   *
177c478bd9Sstevel@tonic-gate   * When ALWAYS_HOSTNAME is off, hostname lookup is done only when required by
187c478bd9Sstevel@tonic-gate   * tcpd paranoid mode, by access control patterns, or by %letter expansions.
19*1da57d55SToomas Soome   *
207c478bd9Sstevel@tonic-gate   * When ALWAYS_RFC931 mode is off, user lookup is done only when required by
217c478bd9Sstevel@tonic-gate   * access control patterns or %letter expansions.
22*1da57d55SToomas Soome   *
237c478bd9Sstevel@tonic-gate   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
247c478bd9Sstevel@tonic-gate   */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef lint
277c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#) eval.c 1.3 95/01/30 19:51:45";
287c478bd9Sstevel@tonic-gate #endif
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* System libraries. */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* Local stuff. */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "tcpd.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate  /*
407c478bd9Sstevel@tonic-gate   * When a string has the value STRING_UNKNOWN, it means: don't bother, I
417c478bd9Sstevel@tonic-gate   * tried to look up the data but it was unavailable for some reason. When a
427c478bd9Sstevel@tonic-gate   * host name has the value STRING_PARANOID it means there was a name/address
437c478bd9Sstevel@tonic-gate   * conflict.
447c478bd9Sstevel@tonic-gate   */
457c478bd9Sstevel@tonic-gate char    unknown[] = STRING_UNKNOWN;
467c478bd9Sstevel@tonic-gate char    paranoid[] = STRING_PARANOID;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* eval_user - look up user name */
497c478bd9Sstevel@tonic-gate 
eval_user(request)507c478bd9Sstevel@tonic-gate char   *eval_user(request)
517c478bd9Sstevel@tonic-gate struct request_info *request;
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate     if (request->user[0] == 0) {
547c478bd9Sstevel@tonic-gate 	strcpy(request->user, unknown);
557c478bd9Sstevel@tonic-gate 	if (request->sink == 0 && request->client->sin && request->server->sin)
567c478bd9Sstevel@tonic-gate 	    rfc931(request->client->sin, request->server->sin, request->user);
577c478bd9Sstevel@tonic-gate     }
587c478bd9Sstevel@tonic-gate     return (request->user);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* eval_hostaddr - look up printable address */
627c478bd9Sstevel@tonic-gate 
eval_hostaddr(host)637c478bd9Sstevel@tonic-gate char   *eval_hostaddr(host)
647c478bd9Sstevel@tonic-gate struct host_info *host;
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate     if (host->addr[0] == 0) {
677c478bd9Sstevel@tonic-gate 	strcpy(host->addr, unknown);
687c478bd9Sstevel@tonic-gate 	if (host->request->hostaddr != 0)
697c478bd9Sstevel@tonic-gate 	    host->request->hostaddr(host);
707c478bd9Sstevel@tonic-gate     }
717c478bd9Sstevel@tonic-gate     return (host->addr);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* eval_hostname - look up host name */
757c478bd9Sstevel@tonic-gate 
eval_hostname(host)767c478bd9Sstevel@tonic-gate char   *eval_hostname(host)
777c478bd9Sstevel@tonic-gate struct host_info *host;
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate     if (host->name[0] == 0) {
807c478bd9Sstevel@tonic-gate 	strcpy(host->name, unknown);
817c478bd9Sstevel@tonic-gate 	if (host->request->hostname != 0)
827c478bd9Sstevel@tonic-gate 	    host->request->hostname(host);
837c478bd9Sstevel@tonic-gate     }
847c478bd9Sstevel@tonic-gate     return (host->name);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* eval_hostinfo - return string with host name (preferred) or address */
887c478bd9Sstevel@tonic-gate 
eval_hostinfo(host)897c478bd9Sstevel@tonic-gate char   *eval_hostinfo(host)
907c478bd9Sstevel@tonic-gate struct host_info *host;
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate     char   *hostname;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #ifndef ALWAYS_HOSTNAME				/* no implicit host lookups */
957c478bd9Sstevel@tonic-gate     if (host->name[0] == 0)
967c478bd9Sstevel@tonic-gate 	return (eval_hostaddr(host));
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate     hostname = eval_hostname(host);
997c478bd9Sstevel@tonic-gate     if (HOSTNAME_KNOWN(hostname)) {
1007c478bd9Sstevel@tonic-gate 	return (host->name);
1017c478bd9Sstevel@tonic-gate     } else {
1027c478bd9Sstevel@tonic-gate 	return (eval_hostaddr(host));
1037c478bd9Sstevel@tonic-gate     }
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* eval_client - return string with as much about the client as we know */
1077c478bd9Sstevel@tonic-gate 
eval_client(request)1087c478bd9Sstevel@tonic-gate char   *eval_client(request)
1097c478bd9Sstevel@tonic-gate struct request_info *request;
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate     static char both[2 * STRING_LENGTH];
1127c478bd9Sstevel@tonic-gate     char   *hostinfo = eval_hostinfo(request->client);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifndef ALWAYS_RFC931				/* no implicit user lookups */
1157c478bd9Sstevel@tonic-gate     if (request->user[0] == 0)
1167c478bd9Sstevel@tonic-gate 	return (hostinfo);
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate     if (STR_NE(eval_user(request), unknown)) {
1197c478bd9Sstevel@tonic-gate 	sprintf(both, "%s@%s", request->user, hostinfo);
1207c478bd9Sstevel@tonic-gate 	return (both);
1217c478bd9Sstevel@tonic-gate     } else {
1227c478bd9Sstevel@tonic-gate 	return (hostinfo);
1237c478bd9Sstevel@tonic-gate     }
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* eval_server - return string with as much about the server as we know */
1277c478bd9Sstevel@tonic-gate 
eval_server(request)1287c478bd9Sstevel@tonic-gate char   *eval_server(request)
1297c478bd9Sstevel@tonic-gate struct request_info *request;
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate     static char both[2 * STRING_LENGTH];
1327c478bd9Sstevel@tonic-gate     char   *host = eval_hostinfo(request->server);
1337c478bd9Sstevel@tonic-gate     char   *daemon = eval_daemon(request);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate     if (STR_NE(host, unknown)) {
1367c478bd9Sstevel@tonic-gate 	sprintf(both, "%s@%s", daemon, host);
1377c478bd9Sstevel@tonic-gate 	return (both);
1387c478bd9Sstevel@tonic-gate     } else {
1397c478bd9Sstevel@tonic-gate 	return (daemon);
1407c478bd9Sstevel@tonic-gate     }
1417c478bd9Sstevel@tonic-gate }
142