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