remove unused variable
[dropbear.git] / cli-tcpfwd.c
blob6e451c4455cfbbe7717fbbc018dae8b7026b9efb
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 TRACE(("enter send_msg_global_request_remotetcp"))
136 CHECKCLEARTOWRITE();
137 buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST);
138 buf_putstring(ses.writepayload, "tcpip-forward", 13);
139 buf_putbyte(ses.writepayload, 1); /* want_reply */
140 buf_putstring(ses.writepayload, addr, strlen(addr));
141 buf_putint(ses.writepayload, port);
143 encrypt_packet();
145 TRACE(("leave send_msg_global_request_remotetcp"))
148 /* The only global success/failure messages are for remotetcp.
149 * Since there isn't any identifier in these messages, we have to rely on them
150 * being in the same order as we sent the requests. This is the ordering
151 * of the cli_opts.remotefwds list */
152 void cli_recv_msg_request_success() {
153 /* Nothing in the packet. We just mark off that we have received the reply,
154 * so that we can report failure for later ones. */
155 m_list_elem * iter = NULL;
156 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
157 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
158 if (!fwd->have_reply) {
159 fwd->have_reply = 1;
160 return;
165 void cli_recv_msg_request_failure() {
166 m_list_elem *iter;
167 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
168 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
169 if (!fwd->have_reply) {
170 fwd->have_reply = 1;
171 dropbear_log(LOG_WARNING, "Remote TCP forward request failed (port %d -> %s:%d)", fwd->listenport, fwd->connectaddr, fwd->connectport);
172 return;
177 void setup_remotetcp() {
178 m_list_elem *iter;
179 TRACE(("enter setup_remotetcp"))
181 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
182 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
183 if (!fwd->listenaddr)
185 // we store the addresses so that we can compare them
186 // when the server sends them back
187 if (opts.listen_fwd_all) {
188 fwd->listenaddr = m_strdup("");
189 } else {
190 fwd->listenaddr = m_strdup("localhost");
193 send_msg_global_request_remotetcp(fwd->listenaddr, fwd->listenport);
196 TRACE(("leave setup_remotetcp"))
199 static int newtcpforwarded(struct Channel * channel) {
201 char *origaddr = NULL;
202 unsigned int origport;
203 m_list_elem * iter = NULL;
204 struct TCPFwdEntry *fwd;
205 char portstring[NI_MAXSERV];
206 int sock;
207 int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
209 origaddr = buf_getstring(ses.payload, NULL);
210 origport = buf_getint(ses.payload);
212 /* Find which port corresponds */
213 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
214 fwd = (struct TCPFwdEntry*)iter->item;
215 if (origport == fwd->listenport
216 && (strcmp(origaddr, fwd->listenaddr) == 0)) {
217 break;
221 if (iter == NULL) {
222 /* We didn't request forwarding on that port */
223 cleantext(origaddr);
224 dropbear_log(LOG_INFO, "Server sent unrequested forward from \"%s:%d\"",
225 origaddr, origport);
226 goto out;
229 snprintf(portstring, sizeof(portstring), "%d", fwd->connectport);
230 sock = connect_remote(fwd->connectaddr, portstring, 1, NULL);
231 if (sock < 0) {
232 TRACE(("leave newtcpdirect: sock failed"))
233 err = SSH_OPEN_CONNECT_FAILED;
234 goto out;
237 ses.maxfd = MAX(ses.maxfd, sock);
239 /* We don't set readfd, that will get set after the connection's
240 * progress succeeds */
241 channel->writefd = sock;
242 channel->initconn = 1;
244 err = SSH_OPEN_IN_PROGRESS;
246 out:
247 m_free(origaddr);
248 TRACE(("leave newtcpdirect: err %d", err))
249 return err;
251 #endif /* ENABLE_CLI_REMOTETCPFWD */