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.
7 * Diagnostics are reported through syslog(3).
9 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
11 * $FreeBSD: src/contrib/tcp_wrappers/rfc931.c,v 1.2.2.1 2000/07/18 16:41:11 dwmalone Exp $
12 * $DragonFly: src/contrib/tcp_wrappers/rfc931.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
16 static char sccsid[] = "@(#) rfc931.c 1.10 95/01/02 16:11:34";
19 /* System libraries. */
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
38 #define RFC931_PORT 113 /* Semi-well-known port */
39 #define ANY_PORT 0 /* Any old port will do */
41 int rfc931_timeout = RFC931_TIMEOUT;/* Global so it can be changed */
43 static jmp_buf timebuf;
45 /* fsocket - open stdio stream on top of socket */
47 static FILE *fsocket(domain, type, protocol)
55 if ((s = socket(domain, type, protocol)) < 0) {
56 tcpd_warn("socket: %m");
59 if ((fp = fdopen(s, "r+")) == 0) {
60 tcpd_warn("fdopen: %m");
67 /* timeout - handle timeouts */
69 static void timeout(sig)
72 longjmp(timebuf, sig);
75 /* rfc931 - return remote user name, given socket structures */
77 void rfc931(rmt_sin, our_sin, dest)
79 struct sockaddr *rmt_sin;
80 struct sockaddr *our_sin;
82 struct sockaddr_in *rmt_sin;
83 struct sockaddr_in *our_sin;
90 struct sockaddr_storage rmt_query_sin;
91 struct sockaddr_storage our_query_sin;
94 struct sockaddr_in rmt_query_sin;
95 struct sockaddr_in our_query_sin;
97 char user[256]; /* XXX */
98 char buffer[512]; /* XXX */
100 char *result = unknown;
104 /* address family must be the same */
105 if (rmt_sin->sa_family != our_sin->sa_family) {
106 STRN_CPY(dest, result, STRING_LENGTH);
109 switch (our_sin->sa_family) {
111 alen = sizeof(struct sockaddr_in);
114 alen = sizeof(struct sockaddr_in6);
117 STRN_CPY(dest, result, STRING_LENGTH);
123 * If we use a single, buffered, bidirectional stdio stream ("r+" or
124 * "w+" mode) we may read our own output. Such behaviour would make sense
125 * with resources that support random-access operations, but not with
126 * sockets. ANSI C suggests several functions which can be called when
127 * you want to change IO direction, fseek seems the most portable.
131 if ((fp = fsocket(our_sin->sa_family, SOCK_STREAM, 0)) != 0) {
133 if ((fp = fsocket(AF_INET, SOCK_STREAM, 0)) != 0) {
136 * Set up a timer so we won't get stuck while waiting for the server.
139 if (setjmp(timebuf) == 0) {
140 signal(SIGALRM, timeout);
141 alarm(rfc931_timeout);
144 * Bind the local and remote ends of the query socket to the same
145 * IP addresses as the connection under investigation. We go
146 * through all this trouble because the local or remote system
147 * might have more than one network address. The RFC931 etc.
148 * client sends only port numbers; the server takes the IP
149 * addresses from the query socket.
153 memcpy(&our_query_sin, our_sin, alen);
154 memcpy(&rmt_query_sin, rmt_sin, alen);
155 switch (our_sin->sa_family) {
157 ((struct sockaddr_in *)&our_query_sin)->sin_port = htons(ANY_PORT);
158 ((struct sockaddr_in *)&rmt_query_sin)->sin_port = htons(RFC931_PORT);
161 ((struct sockaddr_in6 *)&our_query_sin)->sin6_port = htons(ANY_PORT);
162 ((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port = htons(RFC931_PORT);
166 if (bind(fileno(fp), (struct sockaddr *) & our_query_sin,
168 connect(fileno(fp), (struct sockaddr *) & rmt_query_sin,
171 our_query_sin = *our_sin;
172 our_query_sin.sin_port = htons(ANY_PORT);
173 rmt_query_sin = *rmt_sin;
174 rmt_query_sin.sin_port = htons(RFC931_PORT);
176 if (bind(fileno(fp), (struct sockaddr *) & our_query_sin,
177 sizeof(our_query_sin)) >= 0 &&
178 connect(fileno(fp), (struct sockaddr *) & rmt_query_sin,
179 sizeof(rmt_query_sin)) >= 0) {
183 * Send query to server. Neglect the risk that a 13-byte
184 * write would have to be fragmented by the local system and
185 * cause trouble with buggy System V stdio libraries.
188 fprintf(fp, "%u,%u\r\n",
190 ntohs(((struct sockaddr_in *)rmt_sin)->sin_port),
191 ntohs(((struct sockaddr_in *)our_sin)->sin_port));
193 ntohs(rmt_sin->sin_port),
194 ntohs(our_sin->sin_port));
197 fseek(fp, 0, SEEK_SET);
200 * Read response from server. Use fgets()/sscanf() so we can
201 * work around System V stdio libraries that incorrectly
202 * assume EOF when a read from a socket returns less than
206 if (fgets(buffer, sizeof(buffer), fp) != 0
207 && ferror(fp) == 0 && feof(fp) == 0
208 && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s",
209 &rmt_port, &our_port, user) == 3
211 && ntohs(((struct sockaddr_in *)rmt_sin)->sin_port) == rmt_port
212 && ntohs(((struct sockaddr_in *)our_sin)->sin_port) == our_port) {
214 && ntohs(rmt_sin->sin_port) == rmt_port
215 && ntohs(our_sin->sin_port) == our_port) {
219 * Strip trailing carriage return. It is part of the
220 * protocol, not part of the data.
223 if (cp = strchr(user, '\r'))
232 STRN_CPY(dest, result, STRING_LENGTH);