Home | History | Annotate | Line # | Download | only in libwrap
rfc931.c revision 1.2
      1 /*	$NetBSD: rfc931.c,v 1.2 1997/10/09 21:20:46 christos Exp $	*/
      2 
      3  /*
      4   * rfc931() speaks a common subset of the RFC 931, AUTH, TAP, IDENT and RFC
      5   * 1413 protocols. It queries an RFC 931 etc. compatible daemon on a remote
      6   * host to look up the owner of a connection. The information should not be
      7   * used for authentication purposes. This routine intercepts alarm signals.
      8   *
      9   * Diagnostics are reported through syslog(3).
     10   *
     11   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
     12   */
     13 
     14 #include <sys/cdefs.h>
     15 #ifndef lint
     16 #if 0
     17 static char sccsid[] = "@(#) rfc931.c 1.10 95/01/02 16:11:34";
     18 #else
     19 __RCSID("$NetBSD: rfc931.c,v 1.2 1997/10/09 21:20:46 christos Exp $");
     20 #endif
     21 #endif
     22 
     23 /* System libraries. */
     24 
     25 #include <stdio.h>
     26 #include <syslog.h>
     27 #include <sys/types.h>
     28 #include <sys/socket.h>
     29 #include <netinet/in.h>
     30 #include <stdlib.h>
     31 #include <unistd.h>
     32 #include <setjmp.h>
     33 #include <signal.h>
     34 #include <string.h>
     35 
     36 /* Local stuff. */
     37 
     38 #include "tcpd.h"
     39 
     40 #define	RFC931_PORT	113		/* Semi-well-known port */
     41 #define	ANY_PORT	0		/* Any old port will do */
     42 
     43 int     rfc931_timeout = RFC931_TIMEOUT;/* Global so it can be changed */
     44 
     45 static jmp_buf timebuf;
     46 
     47 static FILE *fsocket __P((int, int, int));
     48 static void timeout __P((int));
     49 
     50 /* fsocket - open stdio stream on top of socket */
     51 
     52 static FILE *fsocket(domain, type, protocol)
     53 int     domain;
     54 int     type;
     55 int     protocol;
     56 {
     57     int     s;
     58     FILE   *fp;
     59 
     60     if ((s = socket(domain, type, protocol)) < 0) {
     61 	tcpd_warn("socket: %m");
     62 	return (0);
     63     } else {
     64 	if ((fp = fdopen(s, "r+")) == 0) {
     65 	    tcpd_warn("fdopen: %m");
     66 	    close(s);
     67 	}
     68 	return (fp);
     69     }
     70 }
     71 
     72 /* timeout - handle timeouts */
     73 
     74 static void timeout(sig)
     75 int     sig;
     76 {
     77     longjmp(timebuf, sig);
     78 }
     79 
     80 /* rfc931 - return remote user name, given socket structures */
     81 
     82 void    rfc931(rmt_sin, our_sin, dest)
     83 struct sockaddr_in *rmt_sin;
     84 struct sockaddr_in *our_sin;
     85 char   *dest;
     86 {
     87     unsigned rmt_port;
     88     unsigned our_port;
     89     struct sockaddr_in rmt_query_sin;
     90     struct sockaddr_in our_query_sin;
     91     char    user[256];			/* XXX */
     92     char    buffer[512];		/* XXX */
     93     char   *cp;
     94     char   *result = unknown;
     95     FILE   *fp;
     96 
     97 #ifdef __GNUC__
     98     (void) &result; /* Avoid longjmp clobbering */
     99 #endif
    100 
    101     /*
    102      * Use one unbuffered stdio stream for writing to and for reading from
    103      * the RFC931 etc. server. This is done because of a bug in the SunOS
    104      * 4.1.x stdio library. The bug may live in other stdio implementations,
    105      * too. When we use a single, buffered, bidirectional stdio stream ("r+"
    106      * or "w+" mode) we read our own output. Such behaviour would make sense
    107      * with resources that support random-access operations, but not with
    108      * sockets.
    109      */
    110 
    111     if ((fp = fsocket(AF_INET, SOCK_STREAM, 0)) != 0) {
    112 	setbuf(fp, (char *) 0);
    113 
    114 	/*
    115 	 * Set up a timer so we won't get stuck while waiting for the server.
    116 	 */
    117 
    118 	if (setjmp(timebuf) == 0) {
    119 	    signal(SIGALRM, timeout);
    120 	    alarm(rfc931_timeout);
    121 
    122 	    /*
    123 	     * Bind the local and remote ends of the query socket to the same
    124 	     * IP addresses as the connection under investigation. We go
    125 	     * through all this trouble because the local or remote system
    126 	     * might have more than one network address. The RFC931 etc.
    127 	     * client sends only port numbers; the server takes the IP
    128 	     * addresses from the query socket.
    129 	     */
    130 
    131 	    our_query_sin = *our_sin;
    132 	    our_query_sin.sin_port = htons(ANY_PORT);
    133 	    rmt_query_sin = *rmt_sin;
    134 	    rmt_query_sin.sin_port = htons(RFC931_PORT);
    135 
    136 	    if (bind(fileno(fp), (struct sockaddr *) & our_query_sin,
    137 		     sizeof(our_query_sin)) >= 0 &&
    138 		connect(fileno(fp), (struct sockaddr *) & rmt_query_sin,
    139 			sizeof(rmt_query_sin)) >= 0) {
    140 
    141 		/*
    142 		 * Send query to server. Neglect the risk that a 13-byte
    143 		 * write would have to be fragmented by the local system and
    144 		 * cause trouble with buggy System V stdio libraries.
    145 		 */
    146 
    147 		fprintf(fp, "%u,%u\r\n",
    148 			ntohs(rmt_sin->sin_port),
    149 			ntohs(our_sin->sin_port));
    150 		fflush(fp);
    151 
    152 		/*
    153 		 * Read response from server. Use fgets()/sscanf() so we can
    154 		 * work around System V stdio libraries that incorrectly
    155 		 * assume EOF when a read from a socket returns less than
    156 		 * requested.
    157 		 */
    158 
    159 		if (fgets(buffer, sizeof(buffer), fp) != 0
    160 		    && ferror(fp) == 0 && feof(fp) == 0
    161 		    && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s",
    162 			      &rmt_port, &our_port, user) == 3
    163 		    && ntohs(rmt_sin->sin_port) == rmt_port
    164 		    && ntohs(our_sin->sin_port) == our_port) {
    165 
    166 		    /*
    167 		     * Strip trailing carriage return. It is part of the
    168 		     * protocol, not part of the data.
    169 		     */
    170 
    171 		    if ((cp = strchr(user, '\r')) != NULL)
    172 			*cp = '\0';
    173 		    result = user;
    174 		}
    175 	    }
    176 	    alarm(0);
    177 	}
    178 	fclose(fp);
    179     }
    180     STRN_CPY(dest, result, STRING_LENGTH);
    181 }
    182