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