Mercurial > dropbear
comparison tcpfwd-remote.c @ 9:7f77962de998
- Reworked non-channel fd handling to listener.c
- More channel cleaning up
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Jun 2004 16:45:53 +0000 |
parents | |
children | f76c9389e9e0 |
comparison
equal
deleted
inserted
replaced
7:425ed5c20157 | 9:7f77962de998 |
---|---|
1 #include "includes.h" | |
2 #include "ssh.h" | |
3 #include "tcpfwd-remote.h" | |
4 #include "dbutil.h" | |
5 #include "session.h" | |
6 #include "buffer.h" | |
7 #include "packet.h" | |
8 #include "listener.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 listen_tcpfwd(unsigned char* bindaddr, unsigned int port); | |
24 static void acceptremote(struct Listener *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 const struct ChanType chan_tcpremote = { | |
74 0, /* sepfds */ | |
75 "forwarded-tcpip", | |
76 NULL, | |
77 NULL, | |
78 NULL, | |
79 NULL | |
80 }; | |
81 | |
82 | |
83 static void acceptremote(struct Listener *listener) { | |
84 | |
85 int fd; | |
86 struct sockaddr addr; | |
87 int len; | |
88 char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; | |
89 struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); | |
90 | |
91 len = sizeof(addr); | |
92 | |
93 fd = accept(listener->sock, &addr, &len); | |
94 if (fd < 0) { | |
95 return; | |
96 } | |
97 | |
98 if (getnameinfo(&addr, len, ipstring, sizeof(ipstring), portstring, | |
99 sizeof(portstring), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
100 return; | |
101 } | |
102 | |
103 if (send_msg_channel_open_init(fd, &chan_tcpremote) == DROPBEAR_SUCCESS) { | |
104 | |
105 buf_putstring(ses.writepayload, tcpinfo->addr, | |
106 strlen(tcpinfo->addr)); | |
107 buf_putint(ses.writepayload, tcpinfo->port); | |
108 buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); | |
109 buf_putint(ses.writepayload, atol(portstring)); | |
110 encrypt_packet(); | |
111 | |
112 } else { | |
113 /* XXX debug? */ | |
114 close(fd); | |
115 } | |
116 } | |
117 | |
118 static void cleanupremote(struct Listener *listener) { | |
119 | |
120 struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); | |
121 | |
122 m_free(tcpinfo->addr); | |
123 m_free(tcpinfo); | |
124 } | |
125 | |
126 static void send_msg_request_success() { | |
127 | |
128 CHECKCLEARTOWRITE(); | |
129 buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS); | |
130 encrypt_packet(); | |
131 | |
132 } | |
133 | |
134 static void send_msg_request_failure() { | |
135 | |
136 CHECKCLEARTOWRITE(); | |
137 buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE); | |
138 encrypt_packet(); | |
139 | |
140 } | |
141 | |
142 static int matchtcp(void* typedata1, void* typedata2) { | |
143 | |
144 const struct RemoteTCP *info1 = (struct RemoteTCP*)typedata1; | |
145 const struct RemoteTCP *info2 = (struct RemoteTCP*)typedata2; | |
146 | |
147 return info1->port == info2->port | |
148 && (strcmp(info1->addr, info2->addr) == 0); | |
149 } | |
150 | |
151 static int cancelremotetcp() { | |
152 | |
153 int ret = DROPBEAR_FAILURE; | |
154 unsigned char * bindaddr = NULL; | |
155 unsigned int addrlen; | |
156 unsigned int port; | |
157 struct Listener * listener = NULL; | |
158 struct RemoteTCP tcpinfo; | |
159 | |
160 TRACE(("enter cancelremotetcp")); | |
161 | |
162 bindaddr = buf_getstring(ses.payload, &addrlen); | |
163 if (addrlen > MAX_IP_LEN) { | |
164 TRACE(("addr len too long: %d", addrlen)); | |
165 goto out; | |
166 } | |
167 | |
168 port = buf_getint(ses.payload); | |
169 | |
170 tcpinfo.addr = bindaddr; | |
171 tcpinfo.port = port; | |
172 listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp); | |
173 if (listener) { | |
174 remove_listener( listener ); | |
175 ret = DROPBEAR_SUCCESS; | |
176 } | |
177 | |
178 out: | |
179 m_free(bindaddr); | |
180 TRACE(("leave cancelremotetcp")); | |
181 return ret; | |
182 } | |
183 | |
184 static int remotetcpreq() { | |
185 | |
186 int ret = DROPBEAR_FAILURE; | |
187 unsigned char * bindaddr = NULL; | |
188 unsigned int addrlen; | |
189 unsigned int port; | |
190 | |
191 TRACE(("enter remotetcpreq")); | |
192 | |
193 bindaddr = buf_getstring(ses.payload, &addrlen); | |
194 if (addrlen > MAX_IP_LEN) { | |
195 TRACE(("addr len too long: %d", addrlen)); | |
196 goto out; | |
197 } | |
198 | |
199 port = buf_getint(ses.payload); | |
200 | |
201 if (port == 0) { | |
202 dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported"); | |
203 goto out; | |
204 } | |
205 | |
206 if (port < 1 || port > 65535) { | |
207 TRACE(("invalid port: %d", port)); | |
208 goto out; | |
209 } | |
210 | |
211 /* XXX matt - server change | |
212 if (ses.authstate.pw->pw_uid != 0 | |
213 && port < IPPORT_RESERVED) { | |
214 TRACE(("can't assign port < 1024 for non-root")); | |
215 goto out; | |
216 } | |
217 */ | |
218 | |
219 ret = listen_tcpfwd(bindaddr, port); | |
220 | |
221 out: | |
222 if (ret == DROPBEAR_FAILURE) { | |
223 /* we only free it if a listener wasn't created, since the listener | |
224 * has to remember it if it's to be cancelled */ | |
225 m_free(bindaddr); | |
226 } | |
227 TRACE(("leave remotetcpreq")); | |
228 return ret; | |
229 } | |
230 | |
231 static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port) { | |
232 | |
233 struct RemoteTCP * tcpinfo = NULL; | |
234 char portstring[6]; /* "65535\0" */ | |
235 struct addrinfo *res = NULL, *ai = NULL; | |
236 struct addrinfo hints; | |
237 int sock = -1; | |
238 struct Listener *listener = NULL; | |
239 | |
240 TRACE(("enter listen_tcpfwd")); | |
241 | |
242 /* first we try to bind, so don't need to do so much cleanup on failure */ | |
243 snprintf(portstring, sizeof(portstring), "%d", port); | |
244 memset(&hints, 0x0, sizeof(hints)); | |
245 hints.ai_socktype = SOCK_STREAM; | |
246 hints.ai_family = PF_INET; | |
247 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; | |
248 | |
249 if (getaddrinfo(bindaddr, portstring, &hints, &res) < 0) { | |
250 TRACE(("leave listen_tcpfwd: getaddrinfo failed: %s", | |
251 strerror(errno))); | |
252 goto done; | |
253 } | |
254 | |
255 /* find the first one which works */ | |
256 for (ai = res; ai != NULL; ai = ai->ai_next) { | |
257 if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { | |
258 continue; | |
259 } | |
260 | |
261 sock = socket(ai->ai_family, SOCK_STREAM, 0); | |
262 if (sock < 0) { | |
263 TRACE(("socket failed: %s", strerror(errno))); | |
264 goto fail; | |
265 } | |
266 | |
267 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { | |
268 TRACE(("bind failed: %s", strerror(errno))); | |
269 goto fail; | |
270 } | |
271 | |
272 if (listen(sock, 20) < 0) { | |
273 TRACE(("listen failed: %s", strerror(errno))); | |
274 goto fail; | |
275 } | |
276 | |
277 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { | |
278 TRACE(("fcntl nonblocking failed: %s", strerror(errno))); | |
279 goto fail; | |
280 } | |
281 | |
282 /* success */ | |
283 break; | |
284 | |
285 fail: | |
286 close(sock); | |
287 } | |
288 | |
289 | |
290 if (ai == NULL) { | |
291 TRACE(("no successful sockets")); | |
292 goto done; | |
293 } | |
294 | |
295 tcpinfo = (struct RemoteTCP*)m_malloc(sizeof(struct RemoteTCP)); | |
296 tcpinfo->addr = bindaddr; | |
297 tcpinfo->port = port; | |
298 | |
299 listener = new_listener(sock, CHANNEL_ID_TCPFORWARDED, tcpinfo, | |
300 acceptremote, cleanupremote); | |
301 | |
302 if (listener == NULL) { | |
303 m_free(tcpinfo); | |
304 } | |
305 | |
306 done: | |
307 if (res) { | |
308 freeaddrinfo(res); | |
309 } | |
310 | |
311 TRACE(("leave listen_tcpfwd")); | |
312 if (listener == NULL) { | |
313 return DROPBEAR_FAILURE; | |
314 } else { | |
315 return DROPBEAR_SUCCESS; | |
316 } | |
317 } | |
318 | |
319 #endif /* DISABLE_REMOTETCPFWD */ |