rfc931.c revision 1.5 1 1.4 simonb /* $NetBSD: rfc931.c,v 1.5 1999/07/03 12:30:41 simonb 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.4 simonb __RCSID("$NetBSD: rfc931.c,v 1.5 1999/07/03 12:30:41 simonb 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.1 mrg struct sockaddr_in *rmt_sin;
84 1.1 mrg struct sockaddr_in *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.1 mrg struct sockaddr_in rmt_query_sin;
90 1.1 mrg struct sockaddr_in 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.1 mrg
97 1.2 christos #ifdef __GNUC__
98 1.3 mrg (void)&result; /* Avoid longjmp clobbering */
99 1.3 mrg (void)&fp; /* XXX gcc */
100 1.2 christos #endif
101 1.2 christos
102 1.1 mrg /*
103 1.1 mrg * Use one unbuffered stdio stream for writing to and for reading from
104 1.1 mrg * the RFC931 etc. server. This is done because of a bug in the SunOS
105 1.1 mrg * 4.1.x stdio library. The bug may live in other stdio implementations,
106 1.1 mrg * too. When we use a single, buffered, bidirectional stdio stream ("r+"
107 1.1 mrg * or "w+" mode) we read our own output. Such behaviour would make sense
108 1.1 mrg * with resources that support random-access operations, but not with
109 1.1 mrg * sockets.
110 1.1 mrg */
111 1.1 mrg
112 1.1 mrg if ((fp = fsocket(AF_INET, SOCK_STREAM, 0)) != 0) {
113 1.1 mrg setbuf(fp, (char *) 0);
114 1.1 mrg
115 1.1 mrg /*
116 1.1 mrg * Set up a timer so we won't get stuck while waiting for the server.
117 1.1 mrg */
118 1.1 mrg
119 1.1 mrg if (setjmp(timebuf) == 0) {
120 1.1 mrg signal(SIGALRM, timeout);
121 1.1 mrg alarm(rfc931_timeout);
122 1.1 mrg
123 1.1 mrg /*
124 1.1 mrg * Bind the local and remote ends of the query socket to the same
125 1.1 mrg * IP addresses as the connection under investigation. We go
126 1.1 mrg * through all this trouble because the local or remote system
127 1.1 mrg * might have more than one network address. The RFC931 etc.
128 1.1 mrg * client sends only port numbers; the server takes the IP
129 1.1 mrg * addresses from the query socket.
130 1.1 mrg */
131 1.1 mrg
132 1.1 mrg our_query_sin = *our_sin;
133 1.1 mrg our_query_sin.sin_port = htons(ANY_PORT);
134 1.1 mrg rmt_query_sin = *rmt_sin;
135 1.1 mrg rmt_query_sin.sin_port = htons(RFC931_PORT);
136 1.1 mrg
137 1.1 mrg if (bind(fileno(fp), (struct sockaddr *) & our_query_sin,
138 1.1 mrg sizeof(our_query_sin)) >= 0 &&
139 1.1 mrg connect(fileno(fp), (struct sockaddr *) & rmt_query_sin,
140 1.1 mrg sizeof(rmt_query_sin)) >= 0) {
141 1.1 mrg
142 1.1 mrg /*
143 1.1 mrg * Send query to server. Neglect the risk that a 13-byte
144 1.1 mrg * write would have to be fragmented by the local system and
145 1.1 mrg * cause trouble with buggy System V stdio libraries.
146 1.1 mrg */
147 1.1 mrg
148 1.1 mrg fprintf(fp, "%u,%u\r\n",
149 1.1 mrg ntohs(rmt_sin->sin_port),
150 1.1 mrg ntohs(our_sin->sin_port));
151 1.1 mrg fflush(fp);
152 1.1 mrg
153 1.1 mrg /*
154 1.1 mrg * Read response from server. Use fgets()/sscanf() so we can
155 1.1 mrg * work around System V stdio libraries that incorrectly
156 1.1 mrg * assume EOF when a read from a socket returns less than
157 1.1 mrg * requested.
158 1.1 mrg */
159 1.1 mrg
160 1.1 mrg if (fgets(buffer, sizeof(buffer), fp) != 0
161 1.1 mrg && ferror(fp) == 0 && feof(fp) == 0
162 1.1 mrg && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s",
163 1.1 mrg &rmt_port, &our_port, user) == 3
164 1.1 mrg && ntohs(rmt_sin->sin_port) == rmt_port
165 1.1 mrg && ntohs(our_sin->sin_port) == our_port) {
166 1.1 mrg
167 1.1 mrg /*
168 1.1 mrg * Strip trailing carriage return. It is part of the
169 1.1 mrg * protocol, not part of the data.
170 1.1 mrg */
171 1.1 mrg
172 1.2 christos if ((cp = strchr(user, '\r')) != NULL)
173 1.2 christos *cp = '\0';
174 1.1 mrg result = user;
175 1.1 mrg }
176 1.1 mrg }
177 1.1 mrg alarm(0);
178 1.1 mrg }
179 1.1 mrg fclose(fp);
180 1.1 mrg }
181 1.1 mrg STRN_CPY(dest, result, STRING_LENGTH);
182 1.1 mrg }
183