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