2014-12-10 21:56:49 +00:00
|
|
|
/*
|
|
|
|
* Dropbear SSH
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002,2003 Matt Johnston
|
|
|
|
* Copyright (c) 2004 by Mihnea Stoenescu
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE. */
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "tcpfwd.h"
|
|
|
|
#include "dbutil.h"
|
|
|
|
#include "session.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "listener.h"
|
|
|
|
#include "runopts.h"
|
|
|
|
#include "auth.h"
|
2019-06-09 20:44:26 +00:00
|
|
|
#include "netio.h"
|
2014-12-10 21:56:49 +00:00
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
#if !DROPBEAR_SVR_REMOTETCPFWD
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
/* This is better than SSH_MSG_UNIMPLEMENTED */
|
|
|
|
void recv_msg_global_request_remotetcp() {
|
|
|
|
TRACE(("recv_msg_global_request_remotetcp: remote tcp forwarding not compiled in"))
|
|
|
|
send_msg_request_failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* */
|
2019-06-09 20:44:26 +00:00
|
|
|
#endif /* !DROPBEAR_SVR_REMOTETCPFWD */
|
2014-12-10 21:56:49 +00:00
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
static int svr_cancelremotetcp(void);
|
|
|
|
static int svr_remotetcpreq(int *allocated_listen_port);
|
2014-12-10 21:56:49 +00:00
|
|
|
static int newtcpdirect(struct Channel * channel);
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
#if DROPBEAR_SVR_REMOTETCPFWD
|
2014-12-10 21:56:49 +00:00
|
|
|
static const struct ChanType svr_chan_tcpremote = {
|
|
|
|
1, /* sepfds */
|
|
|
|
"forwarded-tcpip",
|
|
|
|
tcp_prio_inithandler,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2019-06-09 20:44:26 +00:00
|
|
|
NULL,
|
2014-12-10 21:56:49 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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() {
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
char* reqname = NULL;
|
2014-12-10 21:56:49 +00:00
|
|
|
unsigned int namelen;
|
|
|
|
unsigned int wantreply = 0;
|
|
|
|
int ret = DROPBEAR_FAILURE;
|
|
|
|
|
|
|
|
TRACE(("enter recv_msg_global_request_remotetcp"))
|
|
|
|
|
|
|
|
if (svr_opts.noremotetcp || !svr_pubkey_allows_tcpfwd()) {
|
|
|
|
TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled"))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
reqname = buf_getstring(ses.payload, &namelen);
|
|
|
|
wantreply = buf_getbool(ses.payload);
|
|
|
|
|
|
|
|
if (namelen > MAX_NAME_LEN) {
|
|
|
|
TRACE(("name len is wrong: %d", namelen))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp("tcpip-forward", reqname) == 0) {
|
2019-06-09 20:44:26 +00:00
|
|
|
int allocated_listen_port = 0;
|
|
|
|
ret = svr_remotetcpreq(&allocated_listen_port);
|
|
|
|
/* client expects-port-number-to-make-use-of-server-allocated-ports */
|
|
|
|
if (DROPBEAR_SUCCESS == ret) {
|
|
|
|
CHECKCLEARTOWRITE();
|
|
|
|
buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS);
|
|
|
|
buf_putint(ses.writepayload, allocated_listen_port);
|
|
|
|
encrypt_packet();
|
|
|
|
wantreply = 0; /* avoid out: below sending another reply */
|
|
|
|
}
|
2014-12-10 21:56:49 +00:00
|
|
|
} else if (strcmp("cancel-tcpip-forward", reqname) == 0) {
|
|
|
|
ret = svr_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"))
|
|
|
|
}
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
static int matchtcp(const void* typedata1, const void* typedata2) {
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
const struct TCPListener *info1 = (struct TCPListener*)typedata1;
|
|
|
|
const struct TCPListener *info2 = (struct TCPListener*)typedata2;
|
|
|
|
|
|
|
|
return (info1->listenport == info2->listenport)
|
|
|
|
&& (info1->chantype == info2->chantype)
|
|
|
|
&& (strcmp(info1->listenaddr, info2->listenaddr) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int svr_cancelremotetcp() {
|
|
|
|
|
|
|
|
int ret = DROPBEAR_FAILURE;
|
2019-06-09 20:44:26 +00:00
|
|
|
char * bindaddr = NULL;
|
2014-12-10 21:56:49 +00:00
|
|
|
unsigned int addrlen;
|
|
|
|
unsigned int port;
|
|
|
|
struct Listener * listener = NULL;
|
|
|
|
struct TCPListener 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.sendaddr = NULL;
|
|
|
|
tcpinfo.sendport = 0;
|
|
|
|
tcpinfo.listenaddr = bindaddr;
|
|
|
|
tcpinfo.listenport = 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;
|
|
|
|
}
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
static int svr_remotetcpreq(int *allocated_listen_port) {
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
int ret = DROPBEAR_FAILURE;
|
2019-06-09 20:44:26 +00:00
|
|
|
char * request_addr = NULL;
|
2014-12-10 21:56:49 +00:00
|
|
|
unsigned int addrlen;
|
|
|
|
struct TCPListener *tcpinfo = NULL;
|
|
|
|
unsigned int port;
|
2019-06-09 20:44:26 +00:00
|
|
|
struct Listener *listener = NULL;
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
TRACE(("enter remotetcpreq"))
|
|
|
|
|
|
|
|
request_addr = buf_getstring(ses.payload, &addrlen);
|
|
|
|
if (addrlen > MAX_IP_LEN) {
|
|
|
|
TRACE(("addr len too long: %d", addrlen))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
port = buf_getint(ses.payload);
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
if (port != 0) {
|
|
|
|
if (port < 1 || port > 65535) {
|
|
|
|
TRACE(("invalid port: %d", port))
|
|
|
|
goto out;
|
|
|
|
}
|
2014-12-10 21:56:49 +00:00
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
if (!ses.allowprivport && port < IPPORT_RESERVED) {
|
|
|
|
TRACE(("can't assign port < 1024 for non-root"))
|
|
|
|
goto out;
|
|
|
|
}
|
2014-12-10 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener));
|
|
|
|
tcpinfo->sendaddr = NULL;
|
|
|
|
tcpinfo->sendport = 0;
|
|
|
|
tcpinfo->listenport = port;
|
|
|
|
tcpinfo->chantype = &svr_chan_tcpremote;
|
|
|
|
tcpinfo->tcp_type = forwarded;
|
|
|
|
|
|
|
|
tcpinfo->request_listenaddr = request_addr;
|
|
|
|
if (!opts.listen_fwd_all || (strcmp(request_addr, "localhost") == 0) ) {
|
2019-06-09 20:44:26 +00:00
|
|
|
/* NULL means "localhost only" */
|
2014-12-10 21:56:49 +00:00
|
|
|
tcpinfo->listenaddr = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-09 20:44:26 +00:00
|
|
|
tcpinfo->listenaddr = m_strdup(request_addr);
|
2014-12-10 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
ret = listen_tcpfwd(tcpinfo, &listener);
|
|
|
|
if (DROPBEAR_SUCCESS == ret) {
|
|
|
|
tcpinfo->listenport = get_sock_port(listener->socks[0]);
|
|
|
|
*allocated_listen_port = tcpinfo->listenport;
|
|
|
|
}
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
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(request_addr);
|
|
|
|
m_free(tcpinfo);
|
|
|
|
}
|
2019-06-09 20:44:26 +00:00
|
|
|
|
2014-12-10 21:56:49 +00:00
|
|
|
TRACE(("leave remotetcpreq"))
|
2019-06-09 20:44:26 +00:00
|
|
|
|
2014-12-10 21:56:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
#endif /* DROPBEAR_SVR_REMOTETCPFWD */
|
2014-12-10 21:56:49 +00:00
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
#if DROPBEAR_SVR_LOCALTCPFWD
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
const struct ChanType svr_chan_tcpdirect = {
|
|
|
|
1, /* sepfds */
|
|
|
|
"direct-tcpip",
|
|
|
|
newtcpdirect, /* init */
|
|
|
|
NULL, /* checkclose */
|
|
|
|
NULL, /* reqhandler */
|
2019-06-09 20:44:26 +00:00
|
|
|
NULL, /* closehandler */
|
|
|
|
NULL /* cleanup */
|
2014-12-10 21:56:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Called upon creating a new direct tcp channel (ie we connect out to an
|
|
|
|
* address */
|
|
|
|
static int newtcpdirect(struct Channel * channel) {
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
char* desthost = NULL;
|
2014-12-10 21:56:49 +00:00
|
|
|
unsigned int destport;
|
2019-06-09 20:44:26 +00:00
|
|
|
char* orighost = NULL;
|
2014-12-10 21:56:49 +00:00
|
|
|
unsigned int origport;
|
|
|
|
char portstring[NI_MAXSERV];
|
2019-06-09 20:44:26 +00:00
|
|
|
unsigned int len;
|
2014-12-10 21:56:49 +00:00
|
|
|
int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
|
|
|
|
|
|
|
|
TRACE(("newtcpdirect channel %d", channel->index))
|
|
|
|
|
|
|
|
if (svr_opts.nolocaltcp || !svr_pubkey_allows_tcpfwd()) {
|
|
|
|
TRACE(("leave newtcpdirect: local tcp forwarding disabled"))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
desthost = buf_getstring(ses.payload, &len);
|
|
|
|
if (len > MAX_HOST_LEN) {
|
|
|
|
TRACE(("leave newtcpdirect: desthost too long"))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
destport = buf_getint(ses.payload);
|
|
|
|
|
|
|
|
orighost = buf_getstring(ses.payload, &len);
|
|
|
|
if (len > MAX_HOST_LEN) {
|
|
|
|
TRACE(("leave newtcpdirect: orighost too long"))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
origport = buf_getint(ses.payload);
|
|
|
|
|
|
|
|
/* best be sure */
|
|
|
|
if (origport > 65535 || destport > 65535) {
|
|
|
|
TRACE(("leave newtcpdirect: port > 65535"))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
snprintf(portstring, sizeof(portstring), "%u", destport);
|
|
|
|
channel->conn_pending = connect_remote(desthost, portstring, channel_connect_done, channel, NULL, NULL);
|
2014-12-10 21:56:49 +00:00
|
|
|
|
|
|
|
channel->prio = DROPBEAR_CHANNEL_PRIO_UNKNOWABLE;
|
|
|
|
|
|
|
|
err = SSH_OPEN_IN_PROGRESS;
|
|
|
|
|
|
|
|
out:
|
|
|
|
m_free(desthost);
|
|
|
|
m_free(orighost);
|
|
|
|
TRACE(("leave newtcpdirect: err %d", err))
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-06-09 20:44:26 +00:00
|
|
|
#endif /* DROPBEAR_SVR_LOCALTCPFWD */
|