Home | History | Annotate | Line # | Download | only in libwrap
rfc931.c revision 1.8.56.1
      1  1.8.56.1       riz /*	$NetBSD: rfc931.c,v 1.8.56.1 2012/04/23 16:48:56 riz 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.8.56.1       riz __RCSID("$NetBSD: rfc931.c,v 1.8.56.1 2012/04/23 16:48:56 riz 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.8.56.1       riz static FILE *fsocket(int, int, int);
     48  1.8.56.1       riz static void timeout(int);
     49       1.2  christos 
     50       1.1       mrg /* fsocket - open stdio stream on top of socket */
     51       1.1       mrg 
     52  1.8.56.1       riz static FILE *
     53  1.8.56.1       riz fsocket(int domain, int type, int protocol)
     54       1.1       mrg {
     55       1.1       mrg     int     s;
     56       1.1       mrg     FILE   *fp;
     57       1.1       mrg 
     58       1.1       mrg     if ((s = socket(domain, type, protocol)) < 0) {
     59       1.1       mrg 	tcpd_warn("socket: %m");
     60       1.1       mrg 	return (0);
     61       1.1       mrg     } else {
     62       1.1       mrg 	if ((fp = fdopen(s, "r+")) == 0) {
     63       1.1       mrg 	    tcpd_warn("fdopen: %m");
     64       1.1       mrg 	    close(s);
     65       1.1       mrg 	}
     66       1.1       mrg 	return (fp);
     67       1.1       mrg     }
     68       1.1       mrg }
     69       1.1       mrg 
     70       1.1       mrg /* timeout - handle timeouts */
     71       1.1       mrg 
     72  1.8.56.1       riz static void
     73  1.8.56.1       riz timeout(int sig)
     74       1.1       mrg {
     75       1.1       mrg     longjmp(timebuf, sig);
     76       1.1       mrg }
     77       1.1       mrg 
     78       1.1       mrg /* rfc931 - return remote user name, given socket structures */
     79       1.1       mrg 
     80  1.8.56.1       riz void
     81  1.8.56.1       riz rfc931(struct sockaddr *rmt_sin, struct sockaddr *our_sin, char *dest)
     82       1.1       mrg {
     83       1.1       mrg     unsigned rmt_port;
     84       1.1       mrg     unsigned our_port;
     85       1.6    itojun     struct sockaddr_storage rmt_query_sin;
     86       1.6    itojun     struct sockaddr_storage our_query_sin;
     87       1.1       mrg     char    user[256];			/* XXX */
     88       1.1       mrg     char    buffer[512];		/* XXX */
     89       1.1       mrg     char   *cp;
     90       1.1       mrg     char   *result = unknown;
     91       1.1       mrg     FILE   *fp;
     92  1.8.56.1       riz     volatile int salen;
     93  1.8.56.1       riz     u_short * volatile rmt_portp;
     94  1.8.56.1       riz     u_short * volatile our_portp;
     95       1.6    itojun 
     96       1.6    itojun     /* address family must be the same */
     97       1.6    itojun     if (rmt_sin->sa_family != our_sin->sa_family) {
     98       1.8    itojun 	strlcpy(dest, result, STRING_LENGTH);
     99       1.6    itojun 	return;
    100       1.6    itojun     }
    101       1.6    itojun     switch (rmt_sin->sa_family) {
    102       1.6    itojun     case AF_INET:
    103       1.6    itojun 	salen = sizeof(struct sockaddr_in);
    104       1.7    itojun 	rmt_portp = &(((struct sockaddr_in *)rmt_sin)->sin_port);
    105       1.6    itojun 	break;
    106       1.6    itojun #ifdef INET6
    107       1.6    itojun     case AF_INET6:
    108       1.6    itojun 	salen = sizeof(struct sockaddr_in6);
    109       1.7    itojun 	rmt_portp = &(((struct sockaddr_in6 *)rmt_sin)->sin6_port);
    110       1.6    itojun 	break;
    111       1.6    itojun #endif
    112       1.6    itojun     default:
    113       1.8    itojun 	strlcpy(dest, result, STRING_LENGTH);
    114       1.6    itojun 	return;
    115       1.6    itojun     }
    116       1.6    itojun     switch (our_sin->sa_family) {
    117       1.6    itojun     case AF_INET:
    118       1.7    itojun 	our_portp = &(((struct sockaddr_in *)our_sin)->sin_port);
    119       1.6    itojun 	break;
    120       1.6    itojun #ifdef INET6
    121       1.6    itojun     case AF_INET6:
    122       1.7    itojun 	our_portp = &(((struct sockaddr_in6 *)our_sin)->sin6_port);
    123       1.6    itojun 	break;
    124       1.6    itojun #endif
    125       1.6    itojun     default:
    126       1.8    itojun 	strlcpy(dest, result, STRING_LENGTH);
    127       1.6    itojun 	return;
    128       1.6    itojun     }
    129       1.1       mrg 
    130       1.2  christos #ifdef __GNUC__
    131       1.3       mrg     (void)&result; /* Avoid longjmp clobbering */
    132       1.3       mrg     (void)&fp;	/* XXX gcc */
    133       1.2  christos #endif
    134       1.2  christos 
    135       1.1       mrg     /*
    136       1.1       mrg      * Use one unbuffered stdio stream for writing to and for reading from
    137       1.1       mrg      * the RFC931 etc. server. This is done because of a bug in the SunOS
    138       1.1       mrg      * 4.1.x stdio library. The bug may live in other stdio implementations,
    139       1.1       mrg      * too. When we use a single, buffered, bidirectional stdio stream ("r+"
    140       1.1       mrg      * or "w+" mode) we read our own output. Such behaviour would make sense
    141       1.1       mrg      * with resources that support random-access operations, but not with
    142       1.1       mrg      * sockets.
    143       1.1       mrg      */
    144       1.1       mrg 
    145       1.6    itojun     if ((fp = fsocket(rmt_sin->sa_family, SOCK_STREAM, 0)) != 0) {
    146       1.1       mrg 	setbuf(fp, (char *) 0);
    147       1.1       mrg 
    148       1.1       mrg 	/*
    149       1.1       mrg 	 * Set up a timer so we won't get stuck while waiting for the server.
    150       1.1       mrg 	 */
    151       1.1       mrg 
    152       1.1       mrg 	if (setjmp(timebuf) == 0) {
    153       1.1       mrg 	    signal(SIGALRM, timeout);
    154       1.1       mrg 	    alarm(rfc931_timeout);
    155       1.1       mrg 
    156       1.1       mrg 	    /*
    157       1.1       mrg 	     * Bind the local and remote ends of the query socket to the same
    158       1.1       mrg 	     * IP addresses as the connection under investigation. We go
    159       1.1       mrg 	     * through all this trouble because the local or remote system
    160       1.1       mrg 	     * might have more than one network address. The RFC931 etc.
    161       1.1       mrg 	     * client sends only port numbers; the server takes the IP
    162       1.1       mrg 	     * addresses from the query socket.
    163       1.1       mrg 	     */
    164       1.1       mrg 
    165       1.6    itojun 	    memcpy(&our_query_sin, our_sin, salen);
    166       1.6    itojun 	    switch (our_query_sin.ss_family) {
    167       1.6    itojun 	    case AF_INET:
    168       1.6    itojun 		((struct sockaddr_in *)&our_query_sin)->sin_port =
    169       1.6    itojun 			htons(ANY_PORT);
    170       1.6    itojun 		break;
    171       1.6    itojun #ifdef INET6
    172       1.6    itojun 	    case AF_INET6:
    173       1.6    itojun 		((struct sockaddr_in6 *)&our_query_sin)->sin6_port =
    174       1.6    itojun 			htons(ANY_PORT);
    175       1.6    itojun 		break;
    176       1.6    itojun #endif
    177       1.6    itojun 	    }
    178       1.6    itojun 	    memcpy(&rmt_query_sin, rmt_sin, salen);
    179       1.6    itojun 	    switch (rmt_query_sin.ss_family) {
    180       1.6    itojun 	    case AF_INET:
    181       1.6    itojun 		((struct sockaddr_in *)&rmt_query_sin)->sin_port =
    182       1.6    itojun 			htons(RFC931_PORT);
    183       1.6    itojun 		break;
    184       1.6    itojun #ifdef INET6
    185       1.6    itojun 	    case AF_INET6:
    186       1.6    itojun 		((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port =
    187       1.6    itojun 			htons(RFC931_PORT);
    188       1.6    itojun 		break;
    189       1.6    itojun #endif
    190       1.6    itojun 	    }
    191       1.1       mrg 
    192       1.1       mrg 	    if (bind(fileno(fp), (struct sockaddr *) & our_query_sin,
    193       1.6    itojun 		     salen) >= 0 &&
    194       1.1       mrg 		connect(fileno(fp), (struct sockaddr *) & rmt_query_sin,
    195       1.6    itojun 			salen) >= 0) {
    196       1.1       mrg 
    197       1.1       mrg 		/*
    198       1.1       mrg 		 * Send query to server. Neglect the risk that a 13-byte
    199       1.1       mrg 		 * write would have to be fragmented by the local system and
    200       1.1       mrg 		 * cause trouble with buggy System V stdio libraries.
    201       1.1       mrg 		 */
    202       1.1       mrg 
    203       1.1       mrg 		fprintf(fp, "%u,%u\r\n",
    204       1.6    itojun 			ntohs(*rmt_portp),
    205       1.6    itojun 			ntohs(*our_portp));
    206       1.1       mrg 		fflush(fp);
    207       1.1       mrg 
    208       1.1       mrg 		/*
    209       1.1       mrg 		 * Read response from server. Use fgets()/sscanf() so we can
    210       1.1       mrg 		 * work around System V stdio libraries that incorrectly
    211       1.1       mrg 		 * assume EOF when a read from a socket returns less than
    212       1.1       mrg 		 * requested.
    213       1.1       mrg 		 */
    214       1.1       mrg 
    215       1.1       mrg 		if (fgets(buffer, sizeof(buffer), fp) != 0
    216       1.1       mrg 		    && ferror(fp) == 0 && feof(fp) == 0
    217       1.1       mrg 		    && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s",
    218       1.1       mrg 			      &rmt_port, &our_port, user) == 3
    219       1.6    itojun 		    && ntohs(*rmt_portp) == rmt_port
    220       1.6    itojun 		    && ntohs(*our_portp) == our_port) {
    221       1.1       mrg 
    222       1.1       mrg 		    /*
    223       1.1       mrg 		     * Strip trailing carriage return. It is part of the
    224       1.1       mrg 		     * protocol, not part of the data.
    225       1.1       mrg 		     */
    226       1.1       mrg 
    227       1.2  christos 		    if ((cp = strchr(user, '\r')) != NULL)
    228       1.2  christos 			*cp = '\0';
    229       1.1       mrg 		    result = user;
    230       1.1       mrg 		}
    231       1.1       mrg 	    }
    232       1.1       mrg 	    alarm(0);
    233       1.1       mrg 	}
    234       1.1       mrg 	fclose(fp);
    235       1.1       mrg     }
    236       1.8    itojun     strlcpy(dest, result, STRING_LENGTH);
    237       1.1       mrg }
    238