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