[BACK]Return to eval.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libwrap

File: [cvs.NetBSD.org] / src / lib / libwrap / eval.c (download)

Revision 1.7, Wed Mar 21 10:10:37 2012 UTC (7 years, 5 months ago) by matt
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9, netbsd-8-base, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver, HEAD
Changes since 1.6: +14 -14 lines

Use C89 function definition

/*	$NetBSD: eval.c,v 1.7 2012/03/21 10:10:37 matt Exp $	*/

 /*
  * Routines for controlled evaluation of host names, user names, and so on.
  * They are, in fact, wrappers around the functions that are specific for
  * the sockets or TLI programming interfaces. The request_info and host_info
  * structures are used for result cacheing.
  * 
  * These routines allows us to postpone expensive operations until their
  * results are really needed. Examples are hostname lookups and double
  * checks, or username lookups. Information that cannot be retrieved is
  * given the value "unknown" ("paranoid" in case of hostname problems).
  * 
  * When ALWAYS_HOSTNAME is off, hostname lookup is done only when required by
  * tcpd paranoid mode, by access control patterns, or by %letter expansions.
  * 
  * When ALWAYS_RFC931 mode is off, user lookup is done only when required by
  * access control patterns or %letter expansions.
  * 
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */

#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#) eval.c 1.3 95/01/30 19:51:45";
#else
__RCSID("$NetBSD: eval.c,v 1.7 2012/03/21 10:10:37 matt Exp $");
#endif
#endif

/* System libraries. */

#include <stdio.h>
#include <string.h>

/* Local stuff. */

#include "tcpd.h"

 /*
  * When a string has the value STRING_UNKNOWN, it means: don't bother, I
  * tried to look up the data but it was unavailable for some reason. When a
  * host name has the value STRING_PARANOID it means there was a name/address
  * conflict.
  */
char    unknown[] = STRING_UNKNOWN;
char    paranoid[] = STRING_PARANOID;

/* eval_user - look up user name */

char   *
eval_user(struct request_info *request)
{
    if (request->user[0] == 0) {
	(void)strlcpy(request->user, unknown, sizeof(request->user));
	if (request->sink == 0 && request->client->sin && request->server->sin)
	    rfc931(request->client->sin, request->server->sin, request->user);
    }
    return (request->user);
}

/* eval_hostaddr - look up printable address */

char   *
eval_hostaddr(struct host_info *host)
{
    if (host->addr[0] == 0) {
	(void)strlcpy(host->addr, unknown, sizeof(host->addr));
	if (host->request->hostaddr != 0)
	    host->request->hostaddr(host);
    }
    return (host->addr);
}

/* eval_hostname - look up host name */

char   *
eval_hostname(struct host_info *host)
{
    if (host->name[0] == 0) {
	(void)strlcpy(host->name, unknown, sizeof(host->name));
	if (host->request->hostname != 0)
	    host->request->hostname(host);
    }
    return (host->name);
}

/* eval_hostinfo - return string with host name (preferred) or address */

char   *
eval_hostinfo(struct host_info *host)
{
    char   *hostname;

#ifndef ALWAYS_HOSTNAME				/* no implicit host lookups */
    if (host->name[0] == 0)
	return (eval_hostaddr(host));
#endif
    hostname = eval_hostname(host);
    if (HOSTNAME_KNOWN(hostname)) {
	return (host->name);
    } else {
	return (eval_hostaddr(host));
    }
}

/* eval_client - return string with as much about the client as we know */

char   *
eval_client(struct request_info *request)
{
    static char both[2 * STRING_LENGTH];
    char   *hostinfo = eval_hostinfo(request->client);

#ifndef ALWAYS_RFC931				/* no implicit user lookups */
    if (request->user[0] == 0)
	return (hostinfo);
#endif
    if (STR_NE(eval_user(request), unknown)) {
	(void)snprintf(both, sizeof both, "%s@%s", request->user, hostinfo);
	return (both);
    } else {
	return (hostinfo);
    }
}

/* eval_server - return string with as much about the server as we know */

char   *
eval_server(struct request_info *request)
{
    static char both[2 * STRING_LENGTH];
    char   *host = eval_hostinfo(request->server);
    char   *daemon = eval_daemon(request);

    if (STR_NE(host, unknown)) {
	(void)snprintf(both, sizeof both, "%s@%s", daemon, host);
	return (both);
    } else {
	return (daemon);
    }
}