home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / rfc1413.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-18  |  7.2 KB  |  234 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995,1996 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. /*
  55.  * rfc1413() speaks a common subset of the RFC 1413, AUTH, TAP and IDENT
  56.  * protocols. The code queries an RFC 1413 etc. compatible daemon on a remote
  57.  * host to look up the owner of a connection. The information should not be
  58.  * used for authentication purposes. This routine intercepts alarm signals.
  59.  * 
  60.  * Diagnostics are reported through syslog(3).
  61.  * 
  62.  * Author: Wietse Venema, Eindhoven University of Technology,
  63.  * The Netherlands.
  64.  */
  65.  
  66. /* Some small additions for Shambhala --- ditch the "sccsid" var if
  67.  * compiling with gcc (it *has* changed), include conf.h for the
  68.  * prototypes it defines on at least one system (SunlOSs) which has
  69.  * them missing from the standard header files, and one minor change
  70.  * below (extra parens around assign "if (foo = bar) ..." to shut up
  71.  * gcc -Wall).
  72.  */
  73.  
  74. /* Rewritten by David Robinson */
  75.  
  76. #include "httpd.h"    /* for server_rec, conn_rec */
  77. #include "http_log.h" /* for log_unixerr */
  78. #include "rfc1413.h"
  79.  
  80. #ifndef _HPUX_SOURCE
  81. #define _HPUX_SOURCE
  82. #endif
  83.  
  84. /* System libraries. */
  85.  
  86. #include <setjmp.h>
  87.  
  88. #ifndef SCO
  89. extern char *strchr();
  90. extern char *inet_ntoa();
  91. #endif
  92.  
  93. /* Local stuff. */
  94. /* Semi-well-known port */
  95. #define    RFC1413_PORT    113
  96. /* maximum allowed length of userid */
  97. #define RFC1413_USERLEN 512
  98. /* rough limit on the amount of data we accept. */
  99. #define RFC1413_MAXDATA 1000
  100.  
  101. #define RFC1413_TIMEOUT    60
  102. #define    ANY_PORT    0        /* Any old port will do */
  103. #define FROM_UNKNOWN  "unknown"
  104.  
  105. int rfc1413_timeout = RFC1413_TIMEOUT;/* Global so it can be changed */
  106.  
  107. static jmp_buf timebuf;
  108.  
  109. /* bind_connect - bind both ends of a socket */
  110.  
  111. static int
  112. get_rfc1413(int sock, const struct sockaddr_in *our_sin,
  113.       const struct sockaddr_in *rmt_sin, char user[256], server_rec *srv)
  114. {
  115.     struct sockaddr_in rmt_query_sin, our_query_sin;
  116.     unsigned int rmt_port, our_port;
  117.     int i;
  118.     char *cp;
  119.     char buffer[RFC1413_MAXDATA+1];
  120.  
  121.     /*
  122.      * Bind the local and remote ends of the query socket to the same
  123.      * IP addresses as the connection under investigation. We go
  124.      * through all this trouble because the local or remote system
  125.      * might have more than one network address. The RFC1413 etc.
  126.      * client sends only port numbers; the server takes the IP
  127.      * addresses from the query socket.
  128.      */
  129.  
  130.     our_query_sin = *our_sin;
  131.     our_query_sin.sin_port = htons(ANY_PORT);
  132.     rmt_query_sin = *rmt_sin;
  133.     rmt_query_sin.sin_port = htons(RFC1413_PORT);
  134.  
  135.     if (bind(sock, (struct sockaddr *)&our_query_sin,
  136.          sizeof(struct sockaddr_in)) < 0)
  137.     {
  138.     log_unixerr("bind", NULL, "rfc1413: Error binding to local port", srv);
  139.     return -1;
  140.     }
  141.  
  142. /*
  143.  * errors from connect usually imply the remote machine doesn't support
  144.  * the service
  145.  */
  146.     if (connect(sock, (struct sockaddr *)&rmt_query_sin,
  147.         sizeof(struct sockaddr_in)) < 0)
  148.     return -1;
  149.  
  150. /* send the data */
  151.     sprintf(buffer, "%u,%u\r\n", ntohs(rmt_sin->sin_port),
  152.         ntohs(our_sin->sin_port));
  153.     do i = write(sock, buffer, strlen(buffer));
  154.     while (i == -1 && errno == EINTR);
  155.     if (i == -1)
  156.     {
  157.     log_unixerr("write", NULL, "rfc1413: error sending request", srv);
  158.     return -1;
  159.     }
  160.  
  161.     /*
  162.      * Read response from server. We assume that all the data
  163.      * comes in a single packet.
  164.      */
  165.     
  166.     do i = read(sock, buffer, RFC1413_MAXDATA);
  167.     while (i == -1 && errno == EINTR);
  168.     if (i == -1)
  169.     {
  170.     log_unixerr("read", NULL, "rfc1413: error reading response", srv);
  171.     return -1;
  172.     }
  173.  
  174.     buffer[i] = '\0';
  175. /* RFC1413_USERLEN = 512 */
  176.     if (sscanf(buffer, "%u , %u : USERID :%*[^:]:%512s", &rmt_port, &our_port,
  177.            user) != 3 || ntohs(rmt_sin->sin_port) != rmt_port
  178.     || ntohs(our_sin->sin_port) != our_port) return -1;
  179.  
  180.     /*
  181.      * Strip trailing carriage return. It is part of the
  182.      * protocol, not part of the data.
  183.      */
  184.     
  185.     if ((cp = strchr(user, '\r'))) *cp = '\0';
  186.  
  187.     return 0;
  188. }
  189.  
  190. /* timeout - handle timeouts */
  191. static void
  192. timeout(int sig)
  193. {
  194.     longjmp(timebuf, sig);
  195. }
  196.  
  197. /* rfc1413 - return remote user name, given socket structures */
  198. char *
  199. rfc1413(conn_rec *conn, server_rec *srv)
  200. {
  201.     static char user[RFC1413_USERLEN+1]; /* XXX */
  202.     static char *result;
  203.     static int sock;
  204.  
  205.     result = FROM_UNKNOWN;
  206.  
  207.     sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  208.     if (sock < 0)
  209.     {
  210.     log_unixerr("socket", NULL, "rfc1413: error creating socket", srv);
  211.     conn->remote_logname = result;
  212.     }
  213.  
  214.     /*
  215.      * Set up a timer so we won't get stuck while waiting for the server.
  216.      */
  217.     if (setjmp(timebuf) == 0)
  218.     {
  219.     signal(SIGALRM, timeout);
  220.     alarm(rfc1413_timeout);
  221.     
  222.     if (get_rfc1413(sock, &conn->local_addr, &conn->remote_addr, user,
  223.               srv)
  224.         >= 0)
  225.         result = user;
  226.  
  227.     alarm(0);
  228.     }
  229.     close(sock);
  230.     conn->remote_logname = result;
  231.  
  232.     return conn->remote_logname;
  233. }
  234.