home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Servers / apache-1.2.4-MIHS / original-source / src / rfc1413.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-29  |  7.2 KB  |  227 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission.
  24.  *
  25.  * 5. Redistributions of any form whatsoever must retain the following
  26.  *    acknowledgment:
  27.  *    "This product includes software developed by the Apache Group
  28.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  31.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  34.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  * ====================================================================
  43.  *
  44.  * This software consists of voluntary contributions made by many
  45.  * individuals on behalf of the Apache Group and was originally based
  46.  * on public domain software written at the National Center for
  47.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  48.  * For more information on the Apache Group and the Apache HTTP server
  49.  * project, please see <http://www.apache.org/>.
  50.  *
  51.  */
  52.  
  53. /*
  54.  * rfc1413() speaks a common subset of the RFC 1413, AUTH, TAP and IDENT
  55.  * protocols. The code queries an RFC 1413 etc. compatible daemon on a remote
  56.  * host to look up the owner of a connection. The information should not be
  57.  * used for authentication purposes. This routine intercepts alarm signals.
  58.  * 
  59.  * Diagnostics are reported through syslog(3).
  60.  * 
  61.  * Author: Wietse Venema, Eindhoven University of Technology,
  62.  * The Netherlands.
  63.  */
  64.  
  65. /* Some small additions for Apache --- ditch the "sccsid" var if
  66.  * compiling with gcc (it *has* changed), include conf.h for the
  67.  * prototypes it defines on at least one system (SunlOSs) which has
  68.  * them missing from the standard header files, and one minor change
  69.  * below (extra parens around assign "if (foo = bar) ..." to shut up
  70.  * gcc -Wall).
  71.  */
  72.  
  73. /* Rewritten by David Robinson */
  74.  
  75. #include "httpd.h"    /* for server_rec, conn_rec, ap_longjmp, etc. */
  76. #include "http_log.h" /* for log_unixerr */
  77. #include "rfc1413.h"
  78.  
  79. #ifndef SCO
  80. extern char *strchr();
  81. extern char *inet_ntoa();
  82. #endif
  83.  
  84. /* Local stuff. */
  85. /* Semi-well-known port */
  86. #define    RFC1413_PORT    113
  87. /* maximum allowed length of userid */
  88. #define RFC1413_USERLEN 512
  89. /* rough limit on the amount of data we accept. */
  90. #define RFC1413_MAXDATA 1000
  91.  
  92. #ifndef RFC1413_TIMEOUT
  93. #define RFC1413_TIMEOUT    30
  94. #endif
  95. #define    ANY_PORT    0        /* Any old port will do */
  96. #define FROM_UNKNOWN  "unknown"
  97.  
  98. int rfc1413_timeout = RFC1413_TIMEOUT;  /* Global so it can be changed */
  99.  
  100. JMP_BUF timebuf;
  101.  
  102. /* bind_connect - bind both ends of a socket */
  103.  
  104. static int
  105. get_rfc1413(int sock, const struct sockaddr_in *our_sin,
  106.       const struct sockaddr_in *rmt_sin, char user[256], server_rec *srv)
  107. {
  108.     struct sockaddr_in rmt_query_sin, our_query_sin;
  109.     unsigned int rmt_port, our_port;
  110.     int i;
  111.     char *cp;
  112.     char buffer[RFC1413_MAXDATA+1];
  113.  
  114.     /*
  115.      * Bind the local and remote ends of the query socket to the same
  116.      * IP addresses as the connection under investigation. We go
  117.      * through all this trouble because the local or remote system
  118.      * might have more than one network address. The RFC1413 etc.
  119.      * client sends only port numbers; the server takes the IP
  120.      * addresses from the query socket.
  121.      */
  122.  
  123.     our_query_sin = *our_sin;
  124.     our_query_sin.sin_port = htons(ANY_PORT);
  125.     rmt_query_sin = *rmt_sin;
  126.     rmt_query_sin.sin_port = htons(RFC1413_PORT);
  127.  
  128.     if (bind(sock, (struct sockaddr *)&our_query_sin,
  129.          sizeof(struct sockaddr_in)) < 0)
  130.     {
  131.     log_unixerr("bind", NULL, "rfc1413: Error binding to local port", srv);
  132.     return -1;
  133.     }
  134.  
  135. /*
  136.  * errors from connect usually imply the remote machine doesn't support
  137.  * the service
  138.  */
  139.     if (connect(sock, (struct sockaddr *)&rmt_query_sin,
  140.         sizeof(struct sockaddr_in)) < 0)
  141.     return -1;
  142.  
  143. /* send the data */
  144.     ap_snprintf(buffer, sizeof(buffer), "%u,%u\r\n", ntohs(rmt_sin->sin_port),
  145.         ntohs(our_sin->sin_port));
  146.     do i = write(sock, buffer, strlen(buffer));
  147.     while (i == -1 && errno == EINTR);
  148.     if (i == -1)
  149.     {
  150.     log_unixerr("write", NULL, "rfc1413: error sending request", srv);
  151.     return -1;
  152.     }
  153.  
  154.     /*
  155.      * Read response from server. We assume that all the data
  156.      * comes in a single packet.
  157.      */
  158.     
  159.     do i = read(sock, buffer, RFC1413_MAXDATA);
  160.     while (i == -1 && errno == EINTR);
  161.     if (i == -1)
  162.     {
  163.     log_unixerr("read", NULL, "rfc1413: error reading response", srv);
  164.     return -1;
  165.     }
  166.  
  167.     buffer[i] = '\0';
  168. /* RFC1413_USERLEN = 512 */
  169.     if (sscanf(buffer, "%u , %u : USERID :%*[^:]:%512s", &rmt_port, &our_port,
  170.            user) != 3 || ntohs(rmt_sin->sin_port) != rmt_port
  171.     || ntohs(our_sin->sin_port) != our_port) return -1;
  172.  
  173.     /*
  174.      * Strip trailing carriage return. It is part of the
  175.      * protocol, not part of the data.
  176.      */
  177.     
  178.     if ((cp = strchr(user, '\r'))) *cp = '\0';
  179.  
  180.     return 0;
  181. }
  182.  
  183. /* ident_timeout - handle timeouts */
  184. static void
  185. ident_timeout(int sig)
  186. {
  187.     ap_longjmp(timebuf, sig);
  188. }
  189.  
  190. /* rfc1413 - return remote user name, given socket structures */
  191. char *
  192. rfc1413(conn_rec *conn, server_rec *srv)
  193. {
  194.     static char user[RFC1413_USERLEN+1]; /* XXX */
  195.     static char *result;
  196.     static int sock;
  197.  
  198.     result = FROM_UNKNOWN;
  199.  
  200.     sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  201.     if (sock < 0)
  202.     {
  203.     log_unixerr("socket", NULL, "rfc1413: error creating socket", srv);
  204.     conn->remote_logname = result;
  205.     }
  206.  
  207.     /*
  208.      * Set up a timer so we won't get stuck while waiting for the server.
  209.      */
  210.     if (ap_setjmp(timebuf) == 0)
  211.     {
  212.     signal(SIGALRM, ident_timeout);
  213.     alarm(rfc1413_timeout);
  214.     
  215.     if (get_rfc1413(sock, &conn->local_addr, &conn->remote_addr, user,
  216.               srv)
  217.         >= 0)
  218.         result = user;
  219.  
  220.     alarm(0);
  221.     }
  222.     close(sock);
  223.     conn->remote_logname = result;
  224.  
  225.     return conn->remote_logname;
  226. }
  227.