summaryrefslogtreecommitdiffhomepage
path: root/tcpfwd-remote.c
blob: d0a67a1420c97704ea080a368f4e19980fe9abab (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "includes.h"
#include "ssh.h"
#include "tcpfwd-remote.h"
#include "dbutil.h"
#include "session.h"
#include "buffer.h"
#include "packet.h"
#include "listener.h"
#include "runopts.h"

#ifndef DISABLE_REMOTETCPFWD

struct RemoteTCP {

	unsigned char* addr;
	unsigned int port;

};

static void send_msg_request_success();
static void send_msg_request_failure();
static int cancelremotetcp();
static int remotetcpreq();
static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port);
static void acceptremote(struct Listener *listener);

/* At the moment this is completely used for tcp code (with the name reflecting
 * that). If new request types are added, this should be replaced with code
 * similar to the request-switching in chansession.c */
void recv_msg_global_request_remotetcp() {

	unsigned char* reqname = NULL;
	unsigned int namelen;
	unsigned int wantreply = 0;
	int ret = DROPBEAR_FAILURE;

	TRACE(("enter recv_msg_global_request_remotetcp"));

	if (opts.noremotetcp) {
		TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled"));
		goto out;
	}

	reqname = buf_getstring(ses.payload, &namelen);
	wantreply = buf_getbyte(ses.payload);

	if (namelen > MAXNAMLEN) {
		TRACE(("name len is wrong: %d", namelen));
		goto out;
	}

	if (strcmp("tcpip-forward", reqname) == 0) {
		ret = remotetcpreq();
	} else if (strcmp("cancel-tcpip-forward", reqname) == 0) {
		ret = cancelremotetcp();
	} else {
		TRACE(("reqname isn't tcpip-forward: '%s'", reqname));
	}

out:
	if (wantreply) {
		if (ret == DROPBEAR_SUCCESS) {
			send_msg_request_success();
		} else {
			send_msg_request_failure();
		}
	}

	m_free(reqname);

	TRACE(("leave recv_msg_global_request"));
}

static const struct ChanType chan_tcpremote = {
	1, /* sepfds */
	"forwarded-tcpip",
	NULL,
	NULL,
	NULL,
	NULL
};


static void acceptremote(struct Listener *listener) {

	int fd;
	struct sockaddr addr;
	int len;
	char ipstring[NI_MAXHOST], portstring[NI_MAXSERV];
	struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata);

	len = sizeof(addr);

	fd = accept(listener->sock, &addr, &len);
	if (fd < 0) {
		return;
	}

	if (getnameinfo(&addr, len, ipstring, sizeof(ipstring), portstring,
				sizeof(portstring), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
		return;
	}

	if (send_msg_channel_open_init(fd, &chan_tcpremote) == DROPBEAR_SUCCESS) {

		buf_putstring(ses.writepayload, tcpinfo->addr,
				strlen(tcpinfo->addr));
		buf_putint(ses.writepayload, tcpinfo->port);
		buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
		buf_putint(ses.writepayload, atol(portstring));
		encrypt_packet();

	} else {
		/* XXX debug? */
		close(fd);
	}
}

static void cleanupremote(struct Listener *listener) {

	struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata);

	m_free(tcpinfo->addr);
	m_free(tcpinfo);
}

static void send_msg_request_success() {

	CHECKCLEARTOWRITE();
	buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS);
	encrypt_packet();

}

static void send_msg_request_failure() {

	CHECKCLEARTOWRITE();
	buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE);
	encrypt_packet();

}

static int matchtcp(void* typedata1, void* typedata2) {

	const struct RemoteTCP *info1 = (struct RemoteTCP*)typedata1;
	const struct RemoteTCP *info2 = (struct RemoteTCP*)typedata2;

	return info1->port == info2->port 
			&& (strcmp(info1->addr, info2->addr) == 0);
}

static int cancelremotetcp() {

	int ret = DROPBEAR_FAILURE;
	unsigned char * bindaddr = NULL;
	unsigned int addrlen;
	unsigned int port;
	struct Listener * listener = NULL;
	struct RemoteTCP tcpinfo;

	TRACE(("enter cancelremotetcp"));

	bindaddr = buf_getstring(ses.payload, &addrlen);
	if (addrlen > MAX_IP_LEN) {
		TRACE(("addr len too long: %d", addrlen));
		goto out;
	}

	port = buf_getint(ses.payload);

	tcpinfo.addr = bindaddr;
	tcpinfo.port = port;
	listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp);
	if (listener) {
		remove_listener( listener );
		ret = DROPBEAR_SUCCESS;
	}

out:
	m_free(bindaddr);
	TRACE(("leave cancelremotetcp"));
	return ret;
}

static int remotetcpreq() {

	int ret = DROPBEAR_FAILURE;
	unsigned char * bindaddr = NULL;
	unsigned int addrlen;
	unsigned int port;

	TRACE(("enter remotetcpreq"));

	bindaddr = buf_getstring(ses.payload, &addrlen);
	if (addrlen > MAX_IP_LEN) {
		TRACE(("addr len too long: %d", addrlen));
		goto out;
	}

	port = buf_getint(ses.payload);

	if (port == 0) {
		dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported");
		goto out;
	}

	if (port < 1 || port > 65535) {
		TRACE(("invalid port: %d", port));
		goto out;
	}

	if (!ses.allowprivport && port < IPPORT_RESERVED) {
		TRACE(("can't assign port < 1024 for non-root"));
		goto out;
	}

	ret = listen_tcpfwd(bindaddr, port);

out:
	if (ret == DROPBEAR_FAILURE) {
		/* we only free it if a listener wasn't created, since the listener
		 * has to remember it if it's to be cancelled */
		m_free(bindaddr);
	}
	TRACE(("leave remotetcpreq"));
	return ret;
}

static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port) {

	struct RemoteTCP * tcpinfo = NULL;
	char portstring[6]; /* "65535\0" */
	struct addrinfo *res = NULL, *ai = NULL;
	struct addrinfo hints;
	int sock = -1;
	struct Listener *listener = NULL;

	TRACE(("enter listen_tcpfwd"));

	/* first we try to bind, so don't need to do so much cleanup on failure */
	snprintf(portstring, sizeof(portstring), "%d", port);
	memset(&hints, 0x0, sizeof(hints));
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_family = PF_INET;
	hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;

	if (getaddrinfo(bindaddr, portstring, &hints, &res) < 0) {
		TRACE(("leave listen_tcpfwd: getaddrinfo failed: %s",
					strerror(errno)));
		goto done;
	}

	/* find the first one which works */
	for (ai = res; ai != NULL; ai = ai->ai_next) {
		if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) {
			continue;
		}

		sock = socket(ai->ai_family, SOCK_STREAM, 0);
		if (sock < 0) {
			TRACE(("socket failed: %s", strerror(errno)));
			goto fail;
		}

		if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
			TRACE(("bind failed: %s", strerror(errno)));
			goto fail;
		}

		if (listen(sock, 20) < 0) {
			TRACE(("listen failed: %s", strerror(errno)));
			goto fail;
		}

		if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
			TRACE(("fcntl nonblocking failed: %s", strerror(errno)));
			goto fail;
		}

		/* success */
		break;

fail:
		close(sock);
	}


	if (ai == NULL) {
		TRACE(("no successful sockets"));
		goto done;
	}

	tcpinfo = (struct RemoteTCP*)m_malloc(sizeof(struct RemoteTCP));
	tcpinfo->addr = bindaddr;
	tcpinfo->port = port;

	listener = new_listener(sock, CHANNEL_ID_TCPFORWARDED, tcpinfo, 
			acceptremote, cleanupremote);

	if (listener == NULL) {
		m_free(tcpinfo);
	}

done:
	if (res) {
		freeaddrinfo(res);
	}
	
	TRACE(("leave listen_tcpfwd"));
	if (listener == NULL) {
		return DROPBEAR_FAILURE;
	} else {
		return DROPBEAR_SUCCESS;
	}
}

#endif /* DISABLE_REMOTETCPFWD */