1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3*0Sstevel@tonic-gate * Use is subject to license terms.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate * rfc931() speaks a common subset of the RFC 931, AUTH, TAP, IDENT and RFC
10*0Sstevel@tonic-gate * 1413 protocols. It queries an RFC 931 etc. compatible daemon on a remote
11*0Sstevel@tonic-gate * host to look up the owner of a connection. The information should not be
12*0Sstevel@tonic-gate * used for authentication purposes. This routine intercepts alarm signals.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * Diagnostics are reported through syslog(3).
15*0Sstevel@tonic-gate *
16*0Sstevel@tonic-gate * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
17*0Sstevel@tonic-gate */
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gate #ifndef lint
20*0Sstevel@tonic-gate static char sccsid[] = "@(#) rfc931.c 1.10 95/01/02 16:11:34";
21*0Sstevel@tonic-gate #endif
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate /* System libraries. */
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate #include <stdio.h>
26*0Sstevel@tonic-gate #include <stdlib.h>
27*0Sstevel@tonic-gate #include <unistd.h>
28*0Sstevel@tonic-gate #include <syslog.h>
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/socket.h>
31*0Sstevel@tonic-gate #include <netinet/in.h>
32*0Sstevel@tonic-gate #include <setjmp.h>
33*0Sstevel@tonic-gate #include <signal.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate /* Local stuff. */
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #include "tcpd.h"
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #define RFC931_PORT 113 /* Semi-well-known port */
41*0Sstevel@tonic-gate #define ANY_PORT 0 /* Any old port will do */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate int rfc931_timeout = RFC931_TIMEOUT;/* Global so it can be changed */
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate static jmp_buf timebuf;
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /* fsocket - open stdio stream on top of socket */
48*0Sstevel@tonic-gate
fsocket(domain,type,protocol)49*0Sstevel@tonic-gate static FILE *fsocket(domain, type, protocol)
50*0Sstevel@tonic-gate int domain;
51*0Sstevel@tonic-gate int type;
52*0Sstevel@tonic-gate int protocol;
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate int s;
55*0Sstevel@tonic-gate FILE *fp;
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate if ((s = socket(domain, type, protocol)) < 0) {
58*0Sstevel@tonic-gate tcpd_warn("socket: %m");
59*0Sstevel@tonic-gate return (0);
60*0Sstevel@tonic-gate } else {
61*0Sstevel@tonic-gate if ((fp = fdopen(s, "r+")) == 0) {
62*0Sstevel@tonic-gate tcpd_warn("fdopen: %m");
63*0Sstevel@tonic-gate close(s);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate return (fp);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate /* timeout - handle timeouts */
70*0Sstevel@tonic-gate
timeout(sig)71*0Sstevel@tonic-gate static void timeout(sig)
72*0Sstevel@tonic-gate int sig;
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate longjmp(timebuf, sig);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate /* rfc931 - return remote user name, given socket structures */
78*0Sstevel@tonic-gate
rfc931(rmt_sin,our_sin,dest)79*0Sstevel@tonic-gate void rfc931(rmt_sin, our_sin, dest)
80*0Sstevel@tonic-gate struct sockaddr_gen *rmt_sin;
81*0Sstevel@tonic-gate struct sockaddr_gen *our_sin;
82*0Sstevel@tonic-gate char *dest;
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate unsigned rmt_port;
85*0Sstevel@tonic-gate unsigned our_port;
86*0Sstevel@tonic-gate struct sockaddr_gen rmt_query_sin;
87*0Sstevel@tonic-gate struct sockaddr_gen our_query_sin;
88*0Sstevel@tonic-gate char user[256]; /* XXX */
89*0Sstevel@tonic-gate char buffer[512]; /* XXX */
90*0Sstevel@tonic-gate char *cp;
91*0Sstevel@tonic-gate char *result = unknown;
92*0Sstevel@tonic-gate FILE *fp;
93*0Sstevel@tonic-gate unsigned saved_timeout = 0;
94*0Sstevel@tonic-gate struct sigaction nact, oact;
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate * Use one unbuffered stdio stream for writing to and for reading from
98*0Sstevel@tonic-gate * the RFC931 etc. server. This is done because of a bug in the SunOS
99*0Sstevel@tonic-gate * 4.1.x stdio library. The bug may live in other stdio implementations,
100*0Sstevel@tonic-gate * too. When we use a single, buffered, bidirectional stdio stream ("r+"
101*0Sstevel@tonic-gate * or "w+" mode) we read our own output. Such behaviour would make sense
102*0Sstevel@tonic-gate * with resources that support random-access operations, but not with
103*0Sstevel@tonic-gate * sockets.
104*0Sstevel@tonic-gate */
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate if ((fp = fsocket(SGFAM(rmt_sin), SOCK_STREAM, 0)) != 0) {
107*0Sstevel@tonic-gate setbuf(fp, (char *) 0);
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate * Set up a timer so we won't get stuck while waiting for the server.
111*0Sstevel@tonic-gate */
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate if (setjmp(timebuf) == 0) {
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate * save the pending time in case the caller has armed an alarm.
116*0Sstevel@tonic-gate */
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate saved_timeout = alarm(0);
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate /*
121*0Sstevel@tonic-gate * It's guaranteed to enter this 'if' condition on the direct
122*0Sstevel@tonic-gate * invocation of setjmp and hence no additional checks while
123*0Sstevel@tonic-gate * restoring the signal handler.
124*0Sstevel@tonic-gate * Now, get the old handler and set the new one
125*0Sstevel@tonic-gate */
126*0Sstevel@tonic-gate nact.sa_handler = timeout;
127*0Sstevel@tonic-gate nact.sa_flags = 0;
128*0Sstevel@tonic-gate (void) sigemptyset(&nact.sa_mask);
129*0Sstevel@tonic-gate (void) sigaction(SIGALRM, &nact, &oact);
130*0Sstevel@tonic-gate alarm(rfc931_timeout);
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate * Bind the local and remote ends of the query socket to the same
134*0Sstevel@tonic-gate * IP addresses as the connection under investigation. We go
135*0Sstevel@tonic-gate * through all this trouble because the local or remote system
136*0Sstevel@tonic-gate * might have more than one network address. The RFC931 etc.
137*0Sstevel@tonic-gate * client sends only port numbers; the server takes the IP
138*0Sstevel@tonic-gate * addresses from the query socket.
139*0Sstevel@tonic-gate */
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate our_query_sin = *our_sin;
142*0Sstevel@tonic-gate SGPORT(&our_query_sin) = htons(ANY_PORT);
143*0Sstevel@tonic-gate rmt_query_sin = *rmt_sin;
144*0Sstevel@tonic-gate SGPORT(&rmt_query_sin) = htons(RFC931_PORT);
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate if (bind(fileno(fp), (struct sockaddr *) &our_query_sin,
147*0Sstevel@tonic-gate SGSOCKADDRSZ(&our_query_sin)) >= 0 &&
148*0Sstevel@tonic-gate connect(fileno(fp), (struct sockaddr *) &rmt_query_sin,
149*0Sstevel@tonic-gate SGSOCKADDRSZ(&rmt_query_sin)) >= 0) {
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /*
152*0Sstevel@tonic-gate * Send query to server. Neglect the risk that a 13-byte
153*0Sstevel@tonic-gate * write would have to be fragmented by the local system and
154*0Sstevel@tonic-gate * cause trouble with buggy System V stdio libraries.
155*0Sstevel@tonic-gate */
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate fprintf(fp, "%u,%u\r\n",
158*0Sstevel@tonic-gate ntohs(SGPORT(rmt_sin)),
159*0Sstevel@tonic-gate ntohs(SGPORT(our_sin)));
160*0Sstevel@tonic-gate fflush(fp);
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate * Read response from server. Use fgets()/sscanf() so we can
164*0Sstevel@tonic-gate * work around System V stdio libraries that incorrectly
165*0Sstevel@tonic-gate * assume EOF when a read from a socket returns less than
166*0Sstevel@tonic-gate * requested.
167*0Sstevel@tonic-gate */
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate if (fgets(buffer, sizeof(buffer), fp) != 0
170*0Sstevel@tonic-gate && ferror(fp) == 0 && feof(fp) == 0
171*0Sstevel@tonic-gate && sscanf(buffer, "%u , %u : USERID :%*[^:]:%255s",
172*0Sstevel@tonic-gate &rmt_port, &our_port, user) == 3
173*0Sstevel@tonic-gate && ntohs(SGPORT(rmt_sin)) == rmt_port
174*0Sstevel@tonic-gate && ntohs(SGPORT(our_sin)) == our_port) {
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate * Strip trailing carriage return. It is part of the
178*0Sstevel@tonic-gate * protocol, not part of the data.
179*0Sstevel@tonic-gate */
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate if (cp = strchr(user, '\r'))
182*0Sstevel@tonic-gate *cp = 0;
183*0Sstevel@tonic-gate result = user;
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate alarm(0);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate /* Restore the old handler */
189*0Sstevel@tonic-gate (void) sigaction(SIGALRM, &oact, NULL);
190*0Sstevel@tonic-gate if (saved_timeout > 0)
191*0Sstevel@tonic-gate alarm(saved_timeout);
192*0Sstevel@tonic-gate fclose(fp);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate STRN_CPY(dest, result, STRING_LENGTH);
195*0Sstevel@tonic-gate }
196