4 * Copyright (c) 2002,2003 Matt Johnston
5 * Copyright (c) 2004 by Mihnea Stoenescu
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 static void send_msg_request_failure();
39 static void send_msg_request_failure() {
41 buf_putbyte(ses
.writepayload
, SSH_MSG_REQUEST_FAILURE
);
45 #ifndef ENABLE_SVR_REMOTETCPFWD
47 /* This is better than SSH_MSG_UNIMPLEMENTED */
48 void recv_msg_global_request_remotetcp() {
49 TRACE(("recv_msg_global_request_remotetcp: remote tcp forwarding not compiled in"))
50 send_msg_request_failure();
54 #endif /* !ENABLE_SVR_REMOTETCPFWD */
56 static void send_msg_request_success();
57 static int svr_cancelremotetcp();
58 static int svr_remotetcpreq();
59 static int newtcpdirect(struct Channel
* channel
);
61 #ifdef ENABLE_SVR_REMOTETCPFWD
62 static const struct ChanType svr_chan_tcpremote
= {
71 /* At the moment this is completely used for tcp code (with the name reflecting
72 * that). If new request types are added, this should be replaced with code
73 * similar to the request-switching in chansession.c */
74 void recv_msg_global_request_remotetcp() {
76 unsigned char* reqname
= NULL
;
78 unsigned int wantreply
= 0;
79 int ret
= DROPBEAR_FAILURE
;
81 TRACE(("enter recv_msg_global_request_remotetcp"))
83 if (svr_opts
.noremotetcp
|| !svr_pubkey_allows_tcpfwd()) {
84 TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled"))
88 reqname
= buf_getstring(ses
.payload
, &namelen
);
89 wantreply
= buf_getbool(ses
.payload
);
91 if (namelen
> MAX_NAME_LEN
) {
92 TRACE(("name len is wrong: %d", namelen
))
96 if (strcmp("tcpip-forward", reqname
) == 0) {
97 ret
= svr_remotetcpreq();
98 } else if (strcmp("cancel-tcpip-forward", reqname
) == 0) {
99 ret
= svr_cancelremotetcp();
101 TRACE(("reqname isn't tcpip-forward: '%s'", reqname
))
106 if (ret
== DROPBEAR_SUCCESS
) {
107 send_msg_request_success();
109 send_msg_request_failure();
115 TRACE(("leave recv_msg_global_request"))
119 static void send_msg_request_success() {
122 buf_putbyte(ses
.writepayload
, SSH_MSG_REQUEST_SUCCESS
);
127 static int matchtcp(void* typedata1
, void* typedata2
) {
129 const struct TCPListener
*info1
= (struct TCPListener
*)typedata1
;
130 const struct TCPListener
*info2
= (struct TCPListener
*)typedata2
;
132 return (info1
->listenport
== info2
->listenport
)
133 && (info1
->chantype
== info2
->chantype
)
134 && (strcmp(info1
->listenaddr
, info2
->listenaddr
) == 0);
137 static int svr_cancelremotetcp() {
139 int ret
= DROPBEAR_FAILURE
;
140 unsigned char * bindaddr
= NULL
;
141 unsigned int addrlen
;
143 struct Listener
* listener
= NULL
;
144 struct TCPListener tcpinfo
;
146 TRACE(("enter cancelremotetcp"))
148 bindaddr
= buf_getstring(ses
.payload
, &addrlen
);
149 if (addrlen
> MAX_IP_LEN
) {
150 TRACE(("addr len too long: %d", addrlen
))
154 port
= buf_getint(ses
.payload
);
156 tcpinfo
.sendaddr
= NULL
;
157 tcpinfo
.sendport
= 0;
158 tcpinfo
.listenaddr
= bindaddr
;
159 tcpinfo
.listenport
= port
;
160 listener
= get_listener(CHANNEL_ID_TCPFORWARDED
, &tcpinfo
, matchtcp
);
162 remove_listener( listener
);
163 ret
= DROPBEAR_SUCCESS
;
168 TRACE(("leave cancelremotetcp"))
172 static int svr_remotetcpreq() {
174 int ret
= DROPBEAR_FAILURE
;
175 unsigned char * request_addr
= NULL
;
176 unsigned int addrlen
;
177 struct TCPListener
*tcpinfo
= NULL
;
180 TRACE(("enter remotetcpreq"))
182 request_addr
= buf_getstring(ses
.payload
, &addrlen
);
183 if (addrlen
> MAX_IP_LEN
) {
184 TRACE(("addr len too long: %d", addrlen
))
188 port
= buf_getint(ses
.payload
);
191 dropbear_log(LOG_INFO
, "Server chosen tcpfwd ports are unsupported");
195 if (port
< 1 || port
> 65535) {
196 TRACE(("invalid port: %d", port
))
200 if (!ses
.allowprivport
&& port
< IPPORT_RESERVED
) {
201 TRACE(("can't assign port < 1024 for non-root"))
205 tcpinfo
= (struct TCPListener
*)m_malloc(sizeof(struct TCPListener
));
206 tcpinfo
->sendaddr
= NULL
;
207 tcpinfo
->sendport
= 0;
208 tcpinfo
->listenport
= port
;
209 tcpinfo
->chantype
= &svr_chan_tcpremote
;
210 tcpinfo
->tcp_type
= forwarded
;
212 tcpinfo
->request_listenaddr
= request_addr
;
213 if (!opts
.listen_fwd_all
|| (strcmp(request_addr
, "localhost") == 0) ) {
214 /* NULL means "localhost only" */
215 tcpinfo
->listenaddr
= NULL
;
219 tcpinfo
->listenaddr
= request_addr
;
222 ret
= listen_tcpfwd(tcpinfo
);
225 if (ret
== DROPBEAR_FAILURE
) {
226 /* we only free it if a listener wasn't created, since the listener
227 * has to remember it if it's to be cancelled */
228 m_free(request_addr
);
231 TRACE(("leave remotetcpreq"))
235 #endif /* ENABLE_SVR_REMOTETCPFWD */
237 #ifdef ENABLE_SVR_LOCALTCPFWD
239 const struct ChanType svr_chan_tcpdirect
= {
242 newtcpdirect
, /* init */
243 NULL
, /* checkclose */
244 NULL
, /* reqhandler */
245 NULL
/* closehandler */
248 /* Called upon creating a new direct tcp channel (ie we connect out to an
250 static int newtcpdirect(struct Channel
* channel
) {
252 unsigned char* desthost
= NULL
;
253 unsigned int destport
;
254 unsigned char* orighost
= NULL
;
255 unsigned int origport
;
256 char portstring
[NI_MAXSERV
];
259 int err
= SSH_OPEN_ADMINISTRATIVELY_PROHIBITED
;
261 if (svr_opts
.nolocaltcp
|| !svr_pubkey_allows_tcpfwd()) {
262 TRACE(("leave newtcpdirect: local tcp forwarding disabled"))
266 desthost
= buf_getstring(ses
.payload
, &len
);
267 if (len
> MAX_HOST_LEN
) {
268 TRACE(("leave newtcpdirect: desthost too long"))
272 destport
= buf_getint(ses
.payload
);
274 orighost
= buf_getstring(ses
.payload
, &len
);
275 if (len
> MAX_HOST_LEN
) {
276 TRACE(("leave newtcpdirect: orighost too long"))
280 origport
= buf_getint(ses
.payload
);
283 if (origport
> 65535 || destport
> 65535) {
284 TRACE(("leave newtcpdirect: port > 65535"))
288 snprintf(portstring
, sizeof(portstring
), "%d", destport
);
289 sock
= connect_remote(desthost
, portstring
, 1, NULL
);
291 err
= SSH_OPEN_CONNECT_FAILED
;
292 TRACE(("leave newtcpdirect: sock failed"))
296 ses
.maxfd
= MAX(ses
.maxfd
, sock
);
298 /* We don't set readfd, that will get set after the connection's
299 * progress succeeds */
300 channel
->writefd
= sock
;
301 channel
->initconn
= 1;
303 err
= SSH_OPEN_IN_PROGRESS
;
308 TRACE(("leave newtcpdirect: err %d", err
))
312 #endif /* ENABLE_SVR_LOCALTCPFWD */