rfc931.c revision 1.11 1 1.11 christos /* $NetBSD: rfc931.c,v 1.11 2016/03/16 21:38:22 christos 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.11 christos __RCSID("$NetBSD: rfc931.c,v 1.11 2016/03/16 21:38:22 christos 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.9 matt static FILE *fsocket(int, int, int);
48 1.10 joerg static void timeout(int) __dead;
49 1.2 christos
50 1.1 mrg /* fsocket - open stdio stream on top of socket */
51 1.1 mrg
52 1.9 matt static FILE *
53 1.9 matt 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.9 matt static void
73 1.9 matt 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.9 matt void
81 1.9 matt 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.11 christos static char *result = unknown;
91 1.1 mrg FILE *fp;
92 1.9 matt volatile int salen;
93 1.9 matt u_short * volatile rmt_portp;
94 1.9 matt 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.11 christos strlcpy(dest, unknown, 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.11 christos strlcpy(dest, unknown, 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.11 christos strlcpy(dest, unknown, STRING_LENGTH);
127 1.6 itojun return;
128 1.6 itojun }
129 1.1 mrg
130 1.1 mrg /*
131 1.1 mrg * Use one unbuffered stdio stream for writing to and for reading from
132 1.1 mrg * the RFC931 etc. server. This is done because of a bug in the SunOS
133 1.1 mrg * 4.1.x stdio library. The bug may live in other stdio implementations,
134 1.1 mrg * too. When we use a single, buffered, bidirectional stdio stream ("r+"
135 1.1 mrg * or "w+" mode) we read our own output. Such behaviour would make sense
136 1.1 mrg * with resources that support random-access operations, but not with
137 1.1 mrg * sockets.
138 1.1 mrg */
139 1.1 mrg
140 1.6 itojun if ((fp = fsocket(rmt_sin->sa_family, SOCK_STREAM, 0)) != 0) {
141 1.1 mrg setbuf(fp, (char *) 0);
142 1.1 mrg
143 1.1 mrg /*
144 1.1 mrg * Set up a timer so we won't get stuck while waiting for the server.
145 1.1 mrg */
146 1.1 mrg
147 1.1 mrg if (setjmp(timebuf) == 0) {
148 1.1 mrg signal(SIGALRM, timeout);
149 1.1 mrg alarm(rfc931_timeout);
150 1.1 mrg
151 1.1 mrg /*
152 1.1 mrg * Bind the local and remote ends of the query socket to the same
153 1.1 mrg * IP addresses as the connection under investigation. We go
154 1.1 mrg * through all this trouble because the local or remote system
155 1.1 mrg * might have more than one network address. The RFC931 etc.
156 1.1 mrg * client sends only port numbers; the server takes the IP
157 1.1 mrg * addresses from the query socket.
158 1.1 mrg */
159 1.1 mrg
160 1.6 itojun memcpy(&our_query_sin, our_sin, salen);
161 1.6 itojun switch (our_query_sin.ss_family) {
162 1.6 itojun case AF_INET:
163 1.6 itojun ((struct sockaddr_in *)&our_query_sin)->sin_port =
164 1.6 itojun htons(ANY_PORT);
165 1.6 itojun break;
166 1.6 itojun #ifdef INET6
167 1.6 itojun case AF_INET6:
168 1.6 itojun ((struct sockaddr_in6 *)&our_query_sin)->sin6_port =
169 1.6 itojun htons(ANY_PORT);
170 1.6 itojun break;
171 1.6 itojun #endif
172 1.6 itojun }
173 1.6 itojun memcpy(&rmt_query_sin, rmt_sin, salen);
174 1.6 itojun switch (rmt_query_sin.ss_family) {
175 1.6 itojun case AF_INET:
176 1.6 itojun ((struct sockaddr_in *)&rmt_query_sin)->sin_port =
177 1.6 itojun htons(RFC931_PORT);
178 1.6 itojun break;
179 1.6 itojun #ifdef INET6
180 1.6 itojun case AF_INET6:
181 1.6 itojun ((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port =
182 1.6 itojun htons(RFC931_PORT);
183 1.6 itojun break;
184 1.6 itojun #endif
185 1.6 itojun }
186 1.1 mrg
187 1.1 mrg if (bind(fileno(fp), (struct sockaddr *) & our_query_sin,
188 1.6 itojun salen) >= 0 &&
189 1.1 mrg connect(fileno(fp), (struct sockaddr *) & rmt_query_sin,
190 1.6 itojun salen) >= 0) {
191 1.1 mrg
192 1.1 mrg /*
193 1.1 mrg * Send query to server. Neglect the risk that a 13-byte
194 1.1 mrg * write would have to be fragmented by the local system and
195 1.1 mrg * cause trouble with buggy System V stdio libraries.
196 1.1 mrg */
197 1.1 mrg
198 1.1 mrg fprintf(fp, "%u,%u\r\n",
199 1.6 itojun ntohs(*rmt_portp),
200 1.6 itojun ntohs(*our_portp));
201 1.1 mrg fflush(fp);
202 1.1 mrg
203 1.1 mrg /*
204 1.1 mrg * Read response from server. Use fgets()/sscanf() so we can
205 1.1 mrg * work around System V stdio libraries that incorrectly
206 1.1 mrg * assume EOF when a read from a socket returns less than
207 1.1 mrg * requested.
208 1.1 mrg */
209 1.1 mrg
210 1.1 mrg if (fgets(buffer, sizeof(buffer), fp) != 0
211 1.1 mrg && ferror(fp) == 0 && feof(fp) == 0
212 1.1 mrg && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s",
213 1.1 mrg &rmt_port, &our_port, user) == 3
214 1.6 itojun && ntohs(*rmt_portp) == rmt_port
215 1.6 itojun && ntohs(*our_portp) == our_port) {
216 1.1 mrg
217 1.1 mrg /*
218 1.1 mrg * Strip trailing carriage return. It is part of the
219 1.1 mrg * protocol, not part of the data.
220 1.1 mrg */
221 1.1 mrg
222 1.2 christos if ((cp = strchr(user, '\r')) != NULL)
223 1.2 christos *cp = '\0';
224 1.1 mrg result = user;
225 1.1 mrg }
226 1.1 mrg }
227 1.1 mrg alarm(0);
228 1.1 mrg }
229 1.1 mrg fclose(fp);
230 1.1 mrg }
231 1.8 itojun strlcpy(dest, result, STRING_LENGTH);
232 1.1 mrg }
233