rfc1413.c revision c49c786dc58938f915f20356d954ecec1de2d205
176N/A/* ====================================================================
176N/A * The Apache Software License, Version 1.1
176N/A *
176N/A * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
176N/A * reserved.
176N/A *
176N/A * Redistribution and use in source and binary forms, with or without
176N/A * modification, are permitted provided that the following conditions
176N/A * are met:
176N/A *
176N/A * 1. Redistributions of source code must retain the above copyright
176N/A * notice, this list of conditions and the following disclaimer.
176N/A *
176N/A * 2. Redistributions in binary form must reproduce the above copyright
176N/A * notice, this list of conditions and the following disclaimer in
176N/A * the documentation and/or other materials provided with the
176N/A * distribution.
176N/A *
176N/A * 3. The end-user documentation included with the redistribution,
176N/A * if any, must include the following acknowledgment:
3813N/A * "This product includes software developed by the
176N/A * Apache Software Foundation (http://www.apache.org/)."
176N/A * Alternately, this acknowledgment may appear in the software itself,
176N/A * if and wherever such third-party acknowledgments normally appear.
2239N/A *
176N/A * 4. The names "Apache" and "Apache Software Foundation" must
176N/A * not be used to endorse or promote products derived from this
176N/A * software without prior written permission. For written
176N/A * permission, please contact apache@apache.org.
176N/A *
176N/A * 5. Products derived from this software may not be called "Apache",
176N/A * nor may "Apache" appear in their name, without prior written
176N/A * permission of the Apache Software Foundation.
176N/A *
176N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
176N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
176N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
3813N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3813N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3813N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3813N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3813N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3813N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3813N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2239N/A * SUCH DAMAGE.
2239N/A * ====================================================================
2239N/A *
2239N/A * This software consists of voluntary contributions made by many
2239N/A * individuals on behalf of the Apache Software Foundation. For more
1505N/A * information on the Apache Software Foundation, please see
1505N/A * <http://www.apache.org/>.
1505N/A *
1505N/A * Portions of this software are based upon public domain software
1505N/A * originally written at the National Center for Supercomputing Applications,
1505N/A * University of Illinois, Urbana-Champaign.
2239N/A */
2239N/A
2239N/A/* TODO - put timeouts back in */
2239N/A/*
2239N/A * rfc1413() speaks a common subset of the RFC 1413, AUTH, TAP and IDENT
2239N/A * protocols. The code queries an RFC 1413 etc. compatible daemon on a remote
2239N/A * host to look up the owner of a connection. The information should not be
2239N/A * used for authentication purposes. This routine intercepts alarm signals.
2239N/A *
2239N/A * Diagnostics are reported through syslog(3).
2239N/A *
2239N/A * Author: Wietse Venema, Eindhoven University of Technology,
2239N/A * The Netherlands.
2239N/A */
2239N/A
2239N/A/* Some small additions for Apache --- ditch the "sccsid" var if
3856N/A * compiling with gcc (it *has* changed), include ap_config.h for the
3856N/A * prototypes it defines on at least one system (SunlOSs) which has
3856N/A * them missing from the standard header files, and one minor change
2239N/A * below (extra parens around assign "if (foo = bar) ..." to shut up
2239N/A * gcc -Wall).
1505N/A */
2239N/A
2239N/A/* Rewritten by David Robinson */
2239N/A
2239N/A#include "apr.h"
2239N/A#include "apr_network_io.h"
2239N/A#include "apr_strings.h"
2239N/A#include "apr_lib.h"
2239N/A#include "apr_inherit.h"
2239N/A
2239N/A#define APR_WANT_STDIO
2239N/A#define APR_WANT_STRFUNC
2239N/A#include "apr_want.h"
2239N/A
2239N/A#include "ap_config.h"
2239N/A#include "httpd.h" /* for server_rec, conn_rec, etc. */
2239N/A#include "http_log.h" /* for aplog_error */
2239N/A#include "rfc1413.h"
2239N/A#include "http_main.h" /* set_callback_and_alarm */
2239N/A#include "util_ebcdic.h"
2239N/A
2239N/A/* Local stuff. */
2239N/A/* Semi-well-known port */
2239N/A#define RFC1413_PORT 113
2239N/A/* maximum allowed length of userid */
2239N/A#define RFC1413_USERLEN 512
2239N/A/* rough limit on the amount of data we accept. */
2239N/A#define RFC1413_MAXDATA 1000
2239N/A
2239N/A#ifndef RFC1413_TIMEOUT
2239N/A#define RFC1413_TIMEOUT 30
2239N/A#endif
2239N/A#define FROM_UNKNOWN "unknown"
2239N/A
2239N/Aint ap_rfc1413_timeout = RFC1413_TIMEOUT; /* Global so it can be changed */
2239N/A
2239N/Astatic apr_status_t rfc1413_connect(apr_socket_t **newsock, conn_rec *conn,
3856N/A server_rec *srv)
3856N/A{
2239N/A apr_status_t rv;
2239N/A apr_sockaddr_t *localsa, *destsa;
2239N/A
2239N/A if ((rv = apr_sockaddr_info_get(&localsa, conn->local_ip, APR_UNSPEC,
2239N/A 0, /* ephemeral port */
2239N/A 0, conn->pool)) != APR_SUCCESS) {
2239N/A /* This should not fail since we have a numeric address string
2239N/A * as the host. */
2239N/A ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv,
2239N/A "rfc1413: apr_sockaddr_info_get(%s) failed",
2239N/A conn->local_ip);
2239N/A return rv;
2239N/A }
2239N/A
2239N/A if ((rv = apr_sockaddr_info_get(&destsa, conn->remote_ip,
2239N/A localsa->sa.sin.sin_family, /* has to match */
2239N/A RFC1413_PORT, 0, conn->pool)) != APR_SUCCESS) {
1505N/A /* This should not fail since we have a numeric address string
176N/A * as the host. */
176N/A ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv,
176N/A "rfc1413: apr_sockaddr_info_get(%s) failed",
176N/A conn->remote_ip);
176N/A return rv;
176N/A }
176N/A
176N/A if ((rv = apr_socket_create(newsock,
176N/A localsa->sa.sin.sin_family, /* has to match */
176N/A SOCK_STREAM, conn->pool)) != APR_SUCCESS) {
176N/A ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv,
176N/A "rfc1413: error creating query socket");
176N/A return rv;
176N/A }
176N/A
176N/A if ((rv = apr_setsocketopt(*newsock, APR_SO_TIMEOUT,
176N/A (apr_int32_t)(ap_rfc1413_timeout
176N/A * APR_USEC_PER_SEC)))
176N/A != APR_SUCCESS) {
176N/A ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv,
176N/A "rfc1413: error setting query socket timeout");
176N/A apr_socket_close(*newsock);
176N/A return rv;
176N/A }
176N/A
176N/A/*
176N/A * Bind the local and remote ends of the query socket to the same
2239N/A * IP addresses as the connection under investigation. We go
2239N/A * through all this trouble because the local or remote system
2239N/A * might have more than one network address. The RFC1413 etc.
2239N/A * client sends only port numbers; the server takes the IP
2239N/A * addresses from the query socket.
2239N/A */
2239N/A
2239N/A if ((rv = apr_bind(*newsock, localsa)) != APR_SUCCESS) {
2239N/A ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv,
2239N/A "rfc1413: Error binding query socket to local port");
2239N/A apr_socket_close(*newsock);
2239N/A return rv;
2239N/A }
2239N/A
2239N/A/*
176N/A * errors from connect usually imply the remote machine doesn't support
176N/A * the service; don't log such an error
176N/A */
176N/A if ((rv = apr_connect(*newsock, destsa)) != APR_SUCCESS) {
176N/A apr_socket_close(*newsock);
2239N/A return rv;
176N/A }
2239N/A
2239N/A return APR_SUCCESS;
2239N/A}
2239N/A
176N/Astatic apr_status_t rfc1413_query(apr_socket_t *sock, conn_rec *conn,
2239N/A server_rec *srv)
176N/A{
176N/A apr_port_t rmt_port, our_port;
176N/A apr_port_t sav_rmt_port, sav_our_port;
176N/A apr_size_t i;
176N/A char *cp;
176N/A char buffer[RFC1413_MAXDATA + 1];
176N/A char user[RFC1413_USERLEN + 1]; /* XXX */
176N/A apr_size_t buflen;
176N/A apr_sockaddr_t *localsa;
176N/A
176N/A apr_socket_addr_get(&localsa, APR_LOCAL, sock);
176N/A apr_sockaddr_port_get(&sav_our_port, localsa);
176N/A sav_rmt_port = RFC1413_PORT;
176N/A
176N/A /* send the data */
176N/A buflen = apr_snprintf(buffer, sizeof(buffer), "%hu,%hu\r\n", sav_rmt_port,
sav_our_port);
ap_xlate_proto_to_ascii(buffer, buflen);
/* send query to server. Handle short write. */
i = 0;
while (i < buflen) {
apr_size_t j = strlen(buffer + i);
apr_status_t status;
status = apr_send(sock, buffer+i, &j);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, srv,
"write: rfc1413: error sending request");
return status;
}
else if (j > 0) {
i+=j;
}
}
/*
* Read response from server. - the response should be newline
* terminated according to rfc - make sure it doesn't stomp its
* way out of the buffer.
*/
i = 0;
memset(buffer, '\0', sizeof(buffer));
/*
* Note that the strchr function below checks for \012 instead of '\n'
* this allows it to work on both ASCII and EBCDIC machines.
*/
while((cp = strchr(buffer, '\012')) == NULL && i < sizeof(buffer) - 1) {
apr_size_t j = sizeof(buffer) - 1 - i;
apr_status_t status;
status = apr_recv(sock, buffer+i, &j);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, srv,
"read: rfc1413: error reading response");
return status;
}
else if (j > 0) {
i+=j;
}
else if (status == APR_SUCCESS && j == 0) {
/* Oops... we ran out of data before finding newline */
return APR_EINVAL;
}
}
/* RFC1413_USERLEN = 512 */
ap_xlate_proto_from_ascii(buffer, i);
if (sscanf(buffer, "%hu , %hu : USERID :%*[^:]:%512s", &rmt_port, &our_port,
user) != 3 || sav_rmt_port != rmt_port
|| sav_our_port != our_port)
return APR_EINVAL;
/*
* Strip trailing carriage return. It is part of the
* protocol, not part of the data.
*/
if ((cp = strchr(user, '\r')))
*cp = '\0';
conn->remote_logname = apr_pstrdup(conn->pool, user);
return APR_SUCCESS;
}
char *ap_rfc1413(conn_rec *conn, server_rec *srv)
{
apr_socket_t *sock;
apr_status_t rv;
rv = rfc1413_connect(&sock, conn, srv);
if (rv == APR_SUCCESS) {
rv = rfc1413_query(sock, conn, srv);
apr_socket_close(sock);
}
if (rv != APR_SUCCESS) {
conn->remote_logname = FROM_UNKNOWN;
}
return conn->remote_logname;
}