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 #ifdef ENABLE_SVR_REMOTETCPFWD
39 static void send_msg_request_success();
40 static void send_msg_request_failure();
41 static int svr_cancelremotetcp();
42 static int svr_remotetcpreq();
43 static int newtcpdirect(struct Channel
* channel
);
46 const struct ChanType svr_chan_tcpdirect
= {
49 newtcpdirect
, /* init */
50 NULL
, /* checkclose */
51 NULL
, /* reqhandler */
52 NULL
/* closehandler */
55 static const struct ChanType svr_chan_tcpremote
= {
64 /* At the moment this is completely used for tcp code (with the name reflecting
65 * that). If new request types are added, this should be replaced with code
66 * similar to the request-switching in chansession.c */
67 void recv_msg_global_request_remotetcp() {
69 unsigned char* reqname
= NULL
;
71 unsigned int wantreply
= 0;
72 int ret
= DROPBEAR_FAILURE
;
74 TRACE(("enter recv_msg_global_request_remotetcp"))
76 if (svr_opts
.noremotetcp
|| !svr_pubkey_allows_tcpfwd()) {
77 TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled"))
81 reqname
= buf_getstring(ses
.payload
, &namelen
);
82 wantreply
= buf_getbool(ses
.payload
);
84 if (namelen
> MAX_NAME_LEN
) {
85 TRACE(("name len is wrong: %d", namelen
))
89 if (strcmp("tcpip-forward", reqname
) == 0) {
90 ret
= svr_remotetcpreq();
91 } else if (strcmp("cancel-tcpip-forward", reqname
) == 0) {
92 ret
= svr_cancelremotetcp();
94 TRACE(("reqname isn't tcpip-forward: '%s'", reqname
))
99 if (ret
== DROPBEAR_SUCCESS
) {
100 send_msg_request_success();
102 send_msg_request_failure();
108 TRACE(("leave recv_msg_global_request"))
112 static void send_msg_request_success() {
115 buf_putbyte(ses
.writepayload
, SSH_MSG_REQUEST_SUCCESS
);
120 static void send_msg_request_failure() {
123 buf_putbyte(ses
.writepayload
, SSH_MSG_REQUEST_FAILURE
);
128 static int matchtcp(void* typedata1
, void* typedata2
) {
130 const struct TCPListener
*info1
= (struct TCPListener
*)typedata1
;
131 const struct TCPListener
*info2
= (struct TCPListener
*)typedata2
;
133 return (info1
->listenport
== info2
->listenport
)
134 && (info1
->chantype
== info2
->chantype
)
135 && (strcmp(info1
->listenaddr
, info2
->listenaddr
) == 0);
138 static int svr_cancelremotetcp() {
140 int ret
= DROPBEAR_FAILURE
;
141 unsigned char * bindaddr
= NULL
;
142 unsigned int addrlen
;
144 struct Listener
* listener
= NULL
;
145 struct TCPListener tcpinfo
;
147 TRACE(("enter cancelremotetcp"))
149 bindaddr
= buf_getstring(ses
.payload
, &addrlen
);
150 if (addrlen
> MAX_IP_LEN
) {
151 TRACE(("addr len too long: %d", addrlen
))
155 port
= buf_getint(ses
.payload
);
157 tcpinfo
.sendaddr
= NULL
;
158 tcpinfo
.sendport
= 0;
159 tcpinfo
.listenaddr
= bindaddr
;
160 tcpinfo
.listenport
= port
;
161 listener
= get_listener(CHANNEL_ID_TCPFORWARDED
, &tcpinfo
, matchtcp
);
163 remove_listener( listener
);
164 ret
= DROPBEAR_SUCCESS
;
169 TRACE(("leave cancelremotetcp"))
173 static int svr_remotetcpreq() {
175 int ret
= DROPBEAR_FAILURE
;
176 unsigned char * bindaddr
= NULL
;
177 unsigned int addrlen
;
178 struct TCPListener
*tcpinfo
= NULL
;
181 TRACE(("enter remotetcpreq"))
183 bindaddr
= buf_getstring(ses
.payload
, &addrlen
);
184 if (addrlen
> MAX_IP_LEN
) {
185 TRACE(("addr len too long: %d", addrlen
))
189 port
= buf_getint(ses
.payload
);
192 dropbear_log(LOG_INFO
, "Server chosen tcpfwd ports are unsupported");
196 if (port
< 1 || port
> 65535) {
197 TRACE(("invalid port: %d", port
))
201 if (!ses
.allowprivport
&& port
< IPPORT_RESERVED
) {
202 TRACE(("can't assign port < 1024 for non-root"))
206 tcpinfo
= (struct TCPListener
*)m_malloc(sizeof(struct TCPListener
));
207 tcpinfo
->sendaddr
= NULL
;
208 tcpinfo
->sendport
= 0;
209 tcpinfo
->listenport
= port
;
210 tcpinfo
->chantype
= &svr_chan_tcpremote
;
211 tcpinfo
->tcp_type
= forwarded
;
213 if (!opts
.listen_fwd_all
|| (strcmp(bindaddr
, "localhost") == 0) ) {
214 // NULL means "localhost only"
218 tcpinfo
->listenaddr
= bindaddr
;
220 ret
= listen_tcpfwd(tcpinfo
);
223 if (ret
== DROPBEAR_FAILURE
) {
224 /* we only free it if a listener wasn't created, since the listener
225 * has to remember it if it's to be cancelled */
229 TRACE(("leave remotetcpreq"))
233 /* Called upon creating a new direct tcp channel (ie we connect out to an
235 static int newtcpdirect(struct Channel
* channel
) {
237 unsigned char* desthost
= NULL
;
238 unsigned int destport
;
239 unsigned char* orighost
= NULL
;
240 unsigned int origport
;
241 char portstring
[NI_MAXSERV
];
244 int err
= SSH_OPEN_ADMINISTRATIVELY_PROHIBITED
;
246 if (svr_opts
.nolocaltcp
|| !svr_pubkey_allows_tcpfwd()) {
247 TRACE(("leave newtcpdirect: local tcp forwarding disabled"))
251 desthost
= buf_getstring(ses
.payload
, &len
);
252 if (len
> MAX_HOST_LEN
) {
253 TRACE(("leave newtcpdirect: desthost too long"))
257 destport
= buf_getint(ses
.payload
);
259 orighost
= buf_getstring(ses
.payload
, &len
);
260 if (len
> MAX_HOST_LEN
) {
261 TRACE(("leave newtcpdirect: orighost too long"))
265 origport
= buf_getint(ses
.payload
);
268 if (origport
> 65535 || destport
> 65535) {
269 TRACE(("leave newtcpdirect: port > 65535"))
273 snprintf(portstring
, sizeof(portstring
), "%d", destport
);
274 sock
= connect_remote(desthost
, portstring
, 1, NULL
);
276 err
= SSH_OPEN_CONNECT_FAILED
;
277 TRACE(("leave newtcpdirect: sock failed"))
281 ses
.maxfd
= MAX(ses
.maxfd
, sock
);
283 /* We don't set readfd, that will get set after the connection's
284 * progress succeeds */
285 channel
->writefd
= sock
;
286 channel
->initconn
= 1;
288 err
= SSH_OPEN_IN_PROGRESS
;
293 TRACE(("leave newtcpdirect: err %d", err
))