summaryrefslogtreecommitdiff
path: root/lib/libwrap/rfc931.c
blob: 951907adf479997c6c49353d49640ba59b2e61ba (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*	$OpenBSD: rfc931.c,v 1.7 2002/04/25 10:58:39 mpech Exp $	*/

/* rfc1413 does an attempt at an ident query to a client. Originally written
 * by Wietse Venema, rewritten by Bob Beck <beck@openbsd.org> to avoid 
 * potential longjmp problems and get rid of stdio usage on sockets. 
 */

#ifndef lint
static char rcsid[] = "$OpenBSD: rfc931.c,v 1.7 2002/04/25 10:58:39 mpech Exp $";
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

/* Local stuff for tcpd. */

#include "tcpd.h"

#define	IDENT_PORT	113

static volatile sig_atomic_t ident_timeout;

int rfc931_timeout = RFC931_TIMEOUT; /* global, legacy from tcpwrapper stuff */

static void
timeout(s)
	int s;
{     
	ident_timeout = 1;
}

/*
 * The old rfc931 from original libwrap for compatiblity. Now it calls
 * rfc1413 with the default global parameters, but puts in the string
 * "unknown" (from global unknown) on failure just like the original.
 */

void
rfc931(rmt_sin, our_sin, dest)
	struct sockaddr *rmt_sin;
	struct sockaddr *our_sin;
	char   *dest; 
{
	if (rfc1413(rmt_sin, our_sin, dest, STRING_LENGTH, rfc931_timeout) ==
	    -1)
		strlcpy(dest, unknown, STRING_LENGTH);
}


/*
 * rfc1413, an rfc1413 client request for user name given a socket
 * structure, with a timeout in seconds on the whole operation.  On
 * success returns 0 and saves dsize-1 characters of the username
 * provided by remote ident daemon into "dest", stripping off any
 * terminating CRLF, and terminating with a nul byte. Returns -1 on
 * failure (timeout, remote daemon didn't answer, etc). 
 */

int
rfc1413(rmt_sin, our_sin, dest, dsize, ident_timeout_time)
	struct sockaddr *rmt_sin;
	struct sockaddr *our_sin;
	char   *dest;
	size_t dsize;
	int ident_timeout_time;
{
	u_short rmt_port, our_port;
	int s, i, gotit, salen;
	char *cp;
	u_short *rmt_portp;
	u_short *our_portp;
	fd_set *readfds = NULL;
	fd_set *writefds = NULL;
	struct sockaddr_storage rmt_query_sin;
	struct sockaddr_storage our_query_sin;
	struct sigaction new_sa, old_sa;
	char user[256];	 
	char tbuf[1024];	 
	
	gotit = 0;
	s = -1;
	
	/* address family must be the same */
	if (rmt_sin->sa_family != our_sin->sa_family)
		goto out1;
	switch (rmt_sin->sa_family) {
	case AF_INET:
		salen = sizeof(struct sockaddr_in);
		rmt_portp = &(((struct sockaddr_in *)rmt_sin)->sin_port);
		break;
#ifdef INET6
	case AF_INET6:
		salen = sizeof(struct sockaddr_in6);
		rmt_portp = &(((struct sockaddr_in6 *)rmt_sin)->sin6_port);
		break;
#endif
	default:
		goto out1;
	}
	switch (our_sin->sa_family) {
	case AF_INET:
		our_portp = &(((struct sockaddr_in *)our_sin)->sin_port);
		break;
#ifdef INET6
	case AF_INET6:
		our_portp = &(((struct sockaddr_in6 *)our_sin)->sin6_port);
		break;
#endif
	default:
		goto out1;
	}

	if ((s = socket(rmt_sin->sa_family, SOCK_STREAM, 0)) == -1)
		goto out1;

	/*
	 * Set up a timer so we won't get stuck while waiting for the server.
	 * timer sets volatile sig_atomic_t ident_timeout when it fires. 
	 * this has to be checked after system calls in case we timed out.
	 */
	
	ident_timeout = 0;
	memset(&new_sa, 0, sizeof(new_sa));
	new_sa.sa_handler = timeout;
	new_sa.sa_flags = SA_RESTART;
	if (sigemptyset(&new_sa.sa_mask) == -1)
		goto out1;
	if (sigaction(SIGALRM, &new_sa, &old_sa) == -1)
		goto out1;
	alarm(ident_timeout_time);
	
	/*
	 * 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 IDENT etc.
	 * client sends only port numbers; the server takes the IP
	 * addresses from the query socket.
	 */
	
	memcpy(&our_query_sin, our_sin, salen);
	switch (our_query_sin.ss_family) {
	case AF_INET:
		((struct sockaddr_in *)&our_query_sin)->sin_port = htons(0);
		break;
#ifdef INET6
	case AF_INET6:
		((struct sockaddr_in6 *)&our_query_sin)->sin6_port = htons(0);
		break;
#endif
	}
	memcpy(&rmt_query_sin, rmt_sin, salen);
	switch (rmt_query_sin.ss_family) {
	case AF_INET:
		((struct sockaddr_in *)&rmt_query_sin)->sin_port =
		    htons(IDENT_PORT);
		break;
#ifdef INET6
	case AF_INET6:
		((struct sockaddr_in6 *)&rmt_query_sin)->sin6_port =
		    htons(IDENT_PORT);
		break;
#endif
	}
	
	if (bind(s, (struct sockaddr *) & our_query_sin, salen) == -1)
		goto out;
	if ((connect(s, (struct sockaddr *) & rmt_query_sin, salen) == -1) ||
	    ident_timeout)
		goto out;

	/* We are connected,  build an ident query and send it. */ 
	
	readfds = calloc(howmany(s+1, NFDBITS), sizeof(fd_mask));
	if (readfds == NULL) 
		goto out;
	writefds = calloc(howmany(s+1, NFDBITS), sizeof(fd_mask));
	if (writefds == NULL) 
		goto out;
	snprintf(tbuf, sizeof(tbuf), "%u,%u\r\n", ntohs(*rmt_portp),
	    ntohs(*our_portp));
	i = 0;
	while (i < strlen(tbuf)) { 
		int j;

		FD_ZERO(writefds);
		FD_SET(s, writefds);
		do { 
			j = select(s + 1, NULL, writefds, NULL, NULL);
			if (ident_timeout)			    
				goto out;
		} while ( j == -1 && (errno == EAGAIN || errno == EINTR ));
		if (j == -1)
			goto out;
		if (FD_ISSET(s, writefds)) {
			j = write(s, tbuf + i, strlen(tbuf + i));
			if ((j == -1 && errno != EAGAIN && errno != EINTR) ||
			    ident_timeout) 
				goto out;
			if  (j != -1) 
				i += j;
		} else 
			goto out;
	} 

	
	/* Read the answer back. */
	i = 0;
	tbuf[0] = '\0';
	while ((cp = strchr(tbuf, '\n')) == NULL && i < sizeof(tbuf) - 1) {
		int j;

		FD_ZERO(readfds);
		FD_SET(s, readfds);
		do { 
			j = select(s + 1, readfds, NULL, NULL, NULL);
			if (ident_timeout)
				goto out;
		} while ( j == -1 && (errno == EAGAIN || errno == EINTR ));
		if (j == -1)
			goto out;
		if (FD_ISSET(s, readfds)) {
			j = read(s, tbuf + i, sizeof(tbuf) - 1 - i);
			if ((j == -1 && errno != EAGAIN && errno != EINTR) ||
			    j == 0 || ident_timeout) 
				goto out;
			if  (j != -1) 
				i += j;
			tbuf[i] = '\0';
		} else
			goto out;
	}
	
	if ((sscanf(tbuf,"%u , %u : USERID :%*[^:]:%255s", &rmt_port,
	    &our_port, user) == 3) &&
	    (ntohs(*rmt_portp) == rmt_port) &&
	    (ntohs(*our_portp) == our_port)) {
		if ((cp = strchr(user, '\r')) != NULL)
			*cp = '\0';
		gotit = 1;
	}

out:
	alarm(0);
	sigaction(SIGALRM, &old_sa, NULL);
	if (readfds != NULL)
		free(readfds);
	if (writefds != NULL)
		free(writefds);
out1:
	if (s != -1) 
		close(s);
	if (gotit) {
		strlcpy(dest, user, dsize);
		return(0);
	}
	return(-1);
}