merge of '48fdaa8706d1acda35e9d564adc9a1fbc96c18c8'
[dropbear.git] / cli-tcpfwd.c
blob63eb70ef598594bfad188589ddc058ce0068fa15
1 /*
2 * Dropbear SSH
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 #include "includes.h"
26 #include "options.h"
27 #include "dbutil.h"
28 #include "tcpfwd.h"
29 #include "channel.h"
30 #include "runopts.h"
31 #include "session.h"
32 #include "ssh.h"
34 #ifdef ENABLE_CLI_REMOTETCPFWD
35 static int newtcpforwarded(struct Channel * channel);
37 const struct ChanType cli_chan_tcpremote = {
38 1, /* sepfds */
39 "forwarded-tcpip",
40 newtcpforwarded,
41 NULL,
42 NULL,
43 NULL
45 #endif
47 #ifdef ENABLE_CLI_LOCALTCPFWD
48 static int cli_localtcp(const char* listenaddr,
49 unsigned int listenport,
50 const char* remoteaddr,
51 unsigned int remoteport);
52 static const struct ChanType cli_chan_tcplocal = {
53 1, /* sepfds */
54 "direct-tcpip",
55 NULL,
56 NULL,
57 NULL,
58 NULL
60 #endif
62 #ifdef ENABLE_CLI_LOCALTCPFWD
63 void setup_localtcp() {
64 m_list_elem *iter;
65 int ret;
67 TRACE(("enter setup_localtcp"))
69 for (iter = cli_opts.localfwds->first; iter; iter = iter->next) {
70 struct TCPFwdEntry * fwd = (struct TCPFwdEntry*)iter->item;
71 ret = cli_localtcp(
72 fwd->listenaddr,
73 fwd->listenport,
74 fwd->connectaddr,
75 fwd->connectport);
76 if (ret == DROPBEAR_FAILURE) {
77 dropbear_log(LOG_WARNING, "Failed local port forward %s:%d:%s:%d",
78 fwd->listenaddr,
79 fwd->listenport,
80 fwd->connectaddr,
81 fwd->connectport);
84 TRACE(("leave setup_localtcp"))
88 static int cli_localtcp(const char* listenaddr,
89 unsigned int listenport,
90 const char* remoteaddr,
91 unsigned int remoteport) {
93 struct TCPListener* tcpinfo = NULL;
94 int ret;
96 TRACE(("enter cli_localtcp: %d %s %d", listenport, remoteaddr,
97 remoteport));
99 tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener));
101 tcpinfo->sendaddr = m_strdup(remoteaddr);
102 tcpinfo->sendport = remoteport;
104 if (listenaddr)
106 tcpinfo->listenaddr = m_strdup(listenaddr);
108 else
110 if (opts.listen_fwd_all) {
111 tcpinfo->listenaddr = m_strdup("");
112 } else {
113 tcpinfo->listenaddr = m_strdup("localhost");
116 tcpinfo->listenport = listenport;
118 tcpinfo->chantype = &cli_chan_tcplocal;
119 tcpinfo->tcp_type = direct;
121 ret = listen_tcpfwd(tcpinfo);
123 if (ret == DROPBEAR_FAILURE) {
124 m_free(tcpinfo);
126 TRACE(("leave cli_localtcp: %d", ret))
127 return ret;
129 #endif /* ENABLE_CLI_LOCALTCPFWD */
131 #ifdef ENABLE_CLI_REMOTETCPFWD
132 static void send_msg_global_request_remotetcp(const char *addr, int port) {
134 char* listenspec = NULL;
135 TRACE(("enter send_msg_global_request_remotetcp"))
137 CHECKCLEARTOWRITE();
138 buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST);
139 buf_putstring(ses.writepayload, "tcpip-forward", 13);
140 buf_putbyte(ses.writepayload, 1); /* want_reply */
141 buf_putstring(ses.writepayload, addr, strlen(addr));
142 buf_putint(ses.writepayload, port);
144 encrypt_packet();
146 TRACE(("leave send_msg_global_request_remotetcp"))
149 /* The only global success/failure messages are for remotetcp.
150 * Since there isn't any identifier in these messages, we have to rely on them
151 * being in the same order as we sent the requests. This is the ordering
152 * of the cli_opts.remotefwds list */
153 void cli_recv_msg_request_success() {
154 /* Nothing in the packet. We just mark off that we have received the reply,
155 * so that we can report failure for later ones. */
156 m_list_elem * iter = NULL;
157 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
158 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
159 if (!fwd->have_reply) {
160 fwd->have_reply = 1;
161 return;
166 void cli_recv_msg_request_failure() {
167 m_list_elem *iter;
168 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
169 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
170 if (!fwd->have_reply) {
171 fwd->have_reply = 1;
172 dropbear_log(LOG_WARNING, "Remote TCP forward request failed (port %d -> %s:%d)", fwd->listenport, fwd->connectaddr, fwd->connectport);
173 return;
178 void setup_remotetcp() {
179 m_list_elem *iter;
180 TRACE(("enter setup_remotetcp"))
182 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
183 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
184 if (!fwd->listenaddr)
186 // we store the addresses so that we can compare them
187 // when the server sends them back
188 if (opts.listen_fwd_all) {
189 fwd->listenaddr = m_strdup("");
190 } else {
191 fwd->listenaddr = m_strdup("localhost");
194 send_msg_global_request_remotetcp(fwd->listenaddr, fwd->listenport);
197 TRACE(("leave setup_remotetcp"))
200 static int newtcpforwarded(struct Channel * channel) {
202 char *origaddr = NULL;
203 unsigned int origport;
204 m_list_elem * iter = NULL;
205 struct TCPFwdEntry *fwd;
206 char portstring[NI_MAXSERV];
207 int sock;
208 int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
210 origaddr = buf_getstring(ses.payload, NULL);
211 origport = buf_getint(ses.payload);
213 /* Find which port corresponds */
214 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
215 fwd = (struct TCPFwdEntry*)iter->item;
216 if (origport == fwd->listenport
217 && (strcmp(origaddr, fwd->listenaddr) == 0)) {
218 break;
222 if (iter == NULL) {
223 /* We didn't request forwarding on that port */
224 cleantext(origaddr);
225 dropbear_log(LOG_INFO, "Server sent unrequested forward from \"%s:%d\"",
226 origaddr, origport);
227 goto out;
230 snprintf(portstring, sizeof(portstring), "%d", fwd->connectport);
231 sock = connect_remote(fwd->connectaddr, portstring, 1, NULL);
232 if (sock < 0) {
233 TRACE(("leave newtcpdirect: sock failed"))
234 err = SSH_OPEN_CONNECT_FAILED;
235 goto out;
238 ses.maxfd = MAX(ses.maxfd, sock);
240 /* We don't set readfd, that will get set after the connection's
241 * progress succeeds */
242 channel->writefd = sock;
243 channel->initconn = 1;
245 err = SSH_OPEN_IN_PROGRESS;
247 out:
248 m_free(origaddr);
249 TRACE(("leave newtcpdirect: err %d", err))
250 return err;
252 #endif /* ENABLE_CLI_REMOTETCPFWD */