comparison remotetcpfwd.c @ 4:fe6bca95afa7

Makefile.in contains updated files required
author Matt Johnston <matt@ucc.asn.au>
date Tue, 01 Jun 2004 02:46:09 +0000
parents
children 425ed5c20157
comparison
equal deleted inserted replaced
-1:000000000000 4:fe6bca95afa7
1 #include "includes.h"
2 #include "ssh.h"
3 #include "remotetcpfwd.h"
4 #include "dbutil.h"
5 #include "session.h"
6 #include "buffer.h"
7 #include "packet.h"
8 #include "tcpfwd.h"
9
10 #ifndef DISABLE_REMOTETCPFWD
11
12 struct RemoteTCP {
13
14 unsigned char* addr;
15 unsigned int port;
16
17 };
18
19 static void send_msg_request_success();
20 static void send_msg_request_failure();
21 static int cancelremotetcp();
22 static int remotetcpreq();
23 static int newlistener(unsigned char* bindaddr, unsigned int port);
24 static void acceptremote(struct TCPListener *listener);
25
26 /* At the moment this is completely used for tcp code (with the name reflecting
27 * that). If new request types are added, this should be replaced with code
28 * similar to the request-switching in chansession.c */
29 void recv_msg_global_request_remotetcp() {
30
31 unsigned char* reqname = NULL;
32 unsigned int namelen;
33 unsigned int wantreply = 0;
34 int ret = DROPBEAR_FAILURE;
35
36 TRACE(("enter recv_msg_global_request_remotetcp"));
37
38 if (ses.opts->noremotetcp) {
39 TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled"));
40 goto out;
41 }
42
43 reqname = buf_getstring(ses.payload, &namelen);
44 wantreply = buf_getbyte(ses.payload);
45
46 if (namelen > MAXNAMLEN) {
47 TRACE(("name len is wrong: %d", namelen));
48 goto out;
49 }
50
51 if (strcmp("tcpip-forward", reqname) == 0) {
52 ret = remotetcpreq();
53 } else if (strcmp("cancel-tcpip-forward", reqname) == 0) {
54 ret = cancelremotetcp();
55 } else {
56 TRACE(("reqname isn't tcpip-forward: '%s'", reqname));
57 }
58
59 out:
60 if (wantreply) {
61 if (ret == DROPBEAR_SUCCESS) {
62 send_msg_request_success();
63 } else {
64 send_msg_request_failure();
65 }
66 }
67
68 m_free(reqname);
69
70 TRACE(("leave recv_msg_global_request"));
71 }
72
73 static void acceptremote(struct TCPListener *listener) {
74
75 int fd;
76 struct sockaddr addr;
77 int len;
78 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV];
79 struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata);
80
81 len = sizeof(addr);
82
83 fd = accept(listener->sock, &addr, &len);
84 if (fd < 0) {
85 return;
86 }
87
88 if (getnameinfo(&addr, len, ipstring, sizeof(ipstring), portstring,
89 sizeof(portstring), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
90 return;
91 }
92
93 if (send_msg_channel_open_init(fd, CHANNEL_ID_TCPFORWARDED,
94 "forwarded-tcpip") == DROPBEAR_SUCCESS) {
95 buf_putstring(ses.writepayload, tcpinfo->addr,
96 strlen(tcpinfo->addr));
97 buf_putint(ses.writepayload, tcpinfo->port);
98 buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
99 buf_putint(ses.writepayload, atol(portstring));
100 encrypt_packet();
101 }
102 }
103
104 static void cleanupremote(struct TCPListener *listener) {
105
106 struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata);
107
108 m_free(tcpinfo->addr);
109 m_free(tcpinfo);
110 }
111
112 static void send_msg_request_success() {
113
114 CHECKCLEARTOWRITE();
115 buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS);
116 encrypt_packet();
117
118 }
119
120 static void send_msg_request_failure() {
121
122 CHECKCLEARTOWRITE();
123 buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE);
124 encrypt_packet();
125
126 }
127
128 static int matchtcp(void* typedata1, void* typedata2) {
129
130 const struct RemoteTCP *info1 = (struct RemoteTCP*)typedata1;
131 const struct RemoteTCP *info2 = (struct RemoteTCP*)typedata2;
132
133 return info1->port == info2->port
134 && (strcmp(info1->addr, info2->addr) == 0);
135 }
136
137 static int cancelremotetcp() {
138
139 int ret = DROPBEAR_FAILURE;
140 unsigned char * bindaddr = NULL;
141 unsigned int addrlen;
142 unsigned int port;
143 struct TCPListener * listener = NULL;
144 struct RemoteTCP tcpinfo;
145
146 TRACE(("enter cancelremotetcp"));
147
148 bindaddr = buf_getstring(ses.payload, &addrlen);
149 if (addrlen > MAX_IP_LEN) {
150 TRACE(("addr len too long: %d", addrlen));
151 goto out;
152 }
153
154 port = buf_getint(ses.payload);
155
156 tcpinfo.addr = bindaddr;
157 tcpinfo.port = port;
158 listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp);
159 if (listener) {
160 remove_listener( listener );
161 ret = DROPBEAR_SUCCESS;
162 }
163
164 out:
165 m_free(bindaddr);
166 TRACE(("leave cancelremotetcp"));
167 return ret;
168 }
169
170 static int remotetcpreq() {
171
172 int ret = DROPBEAR_FAILURE;
173 unsigned char * bindaddr = NULL;
174 unsigned int addrlen;
175 unsigned int port;
176
177 TRACE(("enter remotetcpreq"));
178
179 bindaddr = buf_getstring(ses.payload, &addrlen);
180 if (addrlen > MAX_IP_LEN) {
181 TRACE(("addr len too long: %d", addrlen));
182 goto out;
183 }
184
185 port = buf_getint(ses.payload);
186
187 if (port == 0) {
188 dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported");
189 goto out;
190 }
191
192 if (port < 1 || port > 65535) {
193 TRACE(("invalid port: %d", port));
194 goto out;
195 }
196
197 /* XXX matt - server change
198 if (ses.authstate.pw->pw_uid != 0
199 && port < IPPORT_RESERVED) {
200 TRACE(("can't assign port < 1024 for non-root"));
201 goto out;
202 }
203 */
204
205 ret = newlistener(bindaddr, port);
206
207 out:
208 if (ret == DROPBEAR_FAILURE) {
209 /* we only free it if a listener wasn't created, since the listener
210 * has to remember it if it's to be cancelled */
211 m_free(bindaddr);
212 }
213 TRACE(("leave remotetcpreq"));
214 return ret;
215 }
216
217 static int newlistener(unsigned char* bindaddr, unsigned int port) {
218
219 struct RemoteTCP * tcpinfo = NULL;
220 char portstring[6]; /* "65535\0" */
221 struct addrinfo *res = NULL, *ai = NULL;
222 struct addrinfo hints;
223 int sock = -1;
224 int ret = DROPBEAR_FAILURE;
225
226 TRACE(("enter newlistener"));
227
228 /* first we try to bind, so don't need to do so much cleanup on failure */
229 snprintf(portstring, sizeof(portstring), "%d", port);
230 memset(&hints, 0x0, sizeof(hints));
231 hints.ai_socktype = SOCK_STREAM;
232 hints.ai_family = PF_INET;
233 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
234
235 if (getaddrinfo(bindaddr, portstring, &hints, &res) < 0) {
236 TRACE(("leave newlistener: getaddrinfo failed: %s",
237 strerror(errno)));
238 goto done;
239 }
240
241 /* find the first one which works */
242 for (ai = res; ai != NULL; ai = ai->ai_next) {
243 if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) {
244 continue;
245 }
246
247 sock = socket(ai->ai_family, SOCK_STREAM, 0);
248 if (sock < 0) {
249 TRACE(("socket failed: %s", strerror(errno)));
250 goto fail;
251 }
252
253 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
254 TRACE(("bind failed: %s", strerror(errno)));
255 goto fail;
256 }
257
258 if (listen(sock, 20) < 0) {
259 TRACE(("listen failed: %s", strerror(errno)));
260 goto fail;
261 }
262
263 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
264 TRACE(("fcntl nonblocking failed: %s", strerror(errno)));
265 goto fail;
266 }
267
268 /* success */
269 break;
270
271 fail:
272 close(sock);
273 }
274
275
276 if (ai == NULL) {
277 TRACE(("no successful sockets"));
278 goto done;
279 }
280
281 tcpinfo = (struct RemoteTCP*)m_malloc(sizeof(struct RemoteTCP));
282 tcpinfo->addr = bindaddr;
283 tcpinfo->port = port;
284
285 ret = new_fwd(sock, CHANNEL_ID_TCPFORWARDED, tcpinfo,
286 acceptremote, cleanupremote);
287
288 if (ret == DROPBEAR_FAILURE) {
289 m_free(tcpinfo);
290 }
291
292 done:
293 if (res) {
294 freeaddrinfo(res);
295 }
296
297 TRACE(("leave newlistener"));
298 return ret;
299 }
300
301 #endif /* DISABLE_REMOTETCPFWD */