Mercurial > dropbear
annotate cli-tcpfwd.c @ 340:454a34b2dfd1
Fixes from Erik Hovland:
cli-authpubkey.c:
fix leak of keybuf
cli-kex.c:
fix leak of fingerprint fp
cli-service.c:
remove commented out code
dropbearkey.c:
don't attepmt to free NULL key on failure
common-kex.c:
only free key if it is initialised
keyimport.c:
remove dead encrypted-key code
don't leak a FILE* loading OpenSSH keys
rsa.c, dss.c:
check return values for some libtommath functions
svr-kex.c:
check return value retrieving DH kex mpint
svr-tcpfwd.c:
fix null-dereference if remote tcp forward request fails
tcp-accept.c:
don't incorrectly free the tcpinfo var
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 07 Jul 2006 09:17:18 +0000 |
parents | c049490e43fe |
children | 805e557fdff7 |
rev | line source |
---|---|
74
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
1 /* |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
2 * Dropbear SSH |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
3 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
4 * Copyright (c) 2002,2003 Matt Johnston |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
5 * All rights reserved. |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
6 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
8 * of this software and associated documentation files (the "Software"), to deal |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
9 * in the Software without restriction, including without limitation the rights |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
11 * copies of the Software, and to permit persons to whom the Software is |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
12 * furnished to do so, subject to the following conditions: |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
13 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
14 * The above copyright notice and this permission notice shall be included in |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
15 * all copies or substantial portions of the Software. |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
16 * |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
23 * SOFTWARE. */ |
e3adf4cf5465
License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
24 |
62 | 25 #include "includes.h" |
26 #include "options.h" | |
64 | 27 #include "dbutil.h" |
28 #include "tcpfwd.h" | |
62 | 29 #include "channel.h" |
64 | 30 #include "runopts.h" |
31 #include "session.h" | |
32 #include "ssh.h" | |
62 | 33 |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
34 #ifdef ENABLE_CLI_REMOTETCPFWD |
64 | 35 static int newtcpforwarded(struct Channel * channel); |
36 | |
37 const struct ChanType cli_chan_tcpremote = { | |
38 1, /* sepfds */ | |
39 "forwarded-tcpip", | |
40 newtcpforwarded, | |
41 NULL, | |
42 NULL, | |
43 NULL | |
44 }; | |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
45 #endif |
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
46 |
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
47 #ifdef ENABLE_CLI_LOCALTCPFWD |
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
48 static int cli_localtcp(unsigned int listenport, const char* remoteaddr, |
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
49 unsigned int remoteport); |
62 | 50 static const struct ChanType cli_chan_tcplocal = { |
51 1, /* sepfds */ | |
52 "direct-tcpip", | |
53 NULL, | |
54 NULL, | |
64 | 55 NULL, |
62 | 56 NULL |
57 }; | |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
58 #endif |
62 | 59 |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
60 #ifdef ENABLE_CLI_LOCALTCPFWD |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
61 void setup_localtcp() { |
62 | 62 |
64 | 63 int ret; |
64 | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
65 TRACE(("enter setup_localtcp")) |
66
38c3146aa23d
Some more sanity-checking of args, and just warn and ignore OpenSSH args
Matt Johnston <matt@ucc.asn.au>
parents:
64
diff
changeset
|
66 |
64 | 67 if (cli_opts.localfwds == NULL) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
68 TRACE(("cli_opts.localfwds == NULL")) |
64 | 69 } |
70 | |
71 while (cli_opts.localfwds != NULL) { | |
72 ret = cli_localtcp(cli_opts.localfwds->listenport, | |
73 cli_opts.localfwds->connectaddr, | |
74 cli_opts.localfwds->connectport); | |
75 if (ret == DROPBEAR_FAILURE) { | |
76 dropbear_log(LOG_WARNING, "Failed local port forward %d:%s:%d", | |
77 cli_opts.localfwds->listenport, | |
78 cli_opts.localfwds->connectaddr, | |
79 cli_opts.localfwds->connectport); | |
80 } | |
81 | |
82 cli_opts.localfwds = cli_opts.localfwds->next; | |
83 } | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
84 TRACE(("leave setup_localtcp")) |
62 | 85 |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
86 } |
62 | 87 |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
88 static int cli_localtcp(unsigned int listenport, const char* remoteaddr, |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
89 unsigned int remoteport) { |
62 | 90 |
91 struct TCPListener* tcpinfo = NULL; | |
64 | 92 int ret; |
93 | |
94 TRACE(("enter cli_localtcp: %d %s %d", listenport, remoteaddr, | |
95 remoteport)); | |
62 | 96 |
184
ca55377e4f7e
- allocate correct buffer size for channel info, rather than sizeof(pointer).
Matt Johnston <matt@ucc.asn.au>
parents:
165
diff
changeset
|
97 tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener)); |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
98 |
108
10f4d3319780
- added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents:
74
diff
changeset
|
99 tcpinfo->sendaddr = m_strdup(remoteaddr); |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
100 tcpinfo->sendport = remoteport; |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
101 |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
102 if (opts.listen_fwd_all) { |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
103 tcpinfo->listenaddr = m_strdup(""); |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
104 } else { |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
105 tcpinfo->listenaddr = m_strdup("localhost"); |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
106 } |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
107 tcpinfo->listenport = listenport; |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
108 |
62 | 109 tcpinfo->chantype = &cli_chan_tcplocal; |
259
c049490e43fe
* fix -L forwarding on the client, broke last rev
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
110 tcpinfo->tcp_type = direct; |
62 | 111 |
112 ret = listen_tcpfwd(tcpinfo); | |
113 | |
114 if (ret == DROPBEAR_FAILURE) { | |
115 m_free(tcpinfo); | |
116 } | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
117 TRACE(("leave cli_localtcp: %d", ret)) |
62 | 118 return ret; |
119 } | |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
120 #endif /* ENABLE_CLI_LOCALTCPFWD */ |
64 | 121 |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
122 #ifdef ENABLE_CLI_REMOTETCPFWD |
64 | 123 static void send_msg_global_request_remotetcp(int port) { |
124 | |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
125 char* listenspec = NULL; |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
126 TRACE(("enter send_msg_global_request_remotetcp")) |
64 | 127 |
128 CHECKCLEARTOWRITE(); | |
129 buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST); | |
130 buf_putstring(ses.writepayload, "tcpip-forward", 13); | |
131 buf_putbyte(ses.writepayload, 0); | |
258
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
132 if (opts.listen_fwd_all) { |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
133 listenspec = ""; |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
134 } else { |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
135 listenspec = "localhost"; |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
136 } |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
137 /* TODO: IPv6? */; |
306499676384
* add -g (dbclient) and -a (dropbear) options for allowing non-local
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
138 buf_putstring(ses.writepayload, listenspec, strlen(listenspec)); |
64 | 139 buf_putint(ses.writepayload, port); |
140 | |
141 encrypt_packet(); | |
142 | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
143 TRACE(("leave send_msg_global_request_remotetcp")) |
64 | 144 } |
145 | |
146 void setup_remotetcp() { | |
147 | |
148 struct TCPFwdList * iter = NULL; | |
149 | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
150 TRACE(("enter setup_remotetcp")) |
66
38c3146aa23d
Some more sanity-checking of args, and just warn and ignore OpenSSH args
Matt Johnston <matt@ucc.asn.au>
parents:
64
diff
changeset
|
151 |
64 | 152 if (cli_opts.remotefwds == NULL) { |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
153 TRACE(("cli_opts.remotefwds == NULL")) |
64 | 154 } |
155 | |
156 iter = cli_opts.remotefwds; | |
157 | |
158 while (iter != NULL) { | |
159 send_msg_global_request_remotetcp(iter->listenport); | |
160 iter = iter->next; | |
161 } | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
162 TRACE(("leave setup_remotetcp")) |
64 | 163 } |
164 | |
165 static int newtcpforwarded(struct Channel * channel) { | |
166 | |
167 unsigned int origport; | |
168 struct TCPFwdList * iter = NULL; | |
169 char portstring[NI_MAXSERV]; | |
170 int sock; | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
171 int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; |
64 | 172 |
173 /* We don't care what address they connected to */ | |
174 buf_eatstring(ses.payload); | |
175 | |
176 origport = buf_getint(ses.payload); | |
177 | |
178 /* Find which port corresponds */ | |
179 iter = cli_opts.remotefwds; | |
180 | |
181 while (iter != NULL) { | |
182 if (origport == iter->listenport) { | |
183 break; | |
184 } | |
185 iter = iter->next; | |
186 } | |
187 | |
188 if (iter == NULL) { | |
189 /* We didn't request forwarding on that port */ | |
190 dropbear_log(LOG_INFO, "Server send unrequested port, from port %d", | |
191 origport); | |
192 goto out; | |
193 } | |
194 | |
195 snprintf(portstring, sizeof(portstring), "%d", iter->connectport); | |
196 sock = connect_remote(iter->connectaddr, portstring, 1, NULL); | |
197 if (sock < 0) { | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
198 TRACE(("leave newtcpdirect: sock failed")) |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
199 err = SSH_OPEN_CONNECT_FAILED; |
64 | 200 goto out; |
201 } | |
202 | |
203 ses.maxfd = MAX(ses.maxfd, sock); | |
204 | |
253
84925eceeb13
* rename infd/outfd to writefd/readfd, to avoid confusion
Matt Johnston <matt@ucc.asn.au>
parents:
184
diff
changeset
|
205 /* We don't set readfd, that will get set after the connection's |
64 | 206 * progress succeeds */ |
253
84925eceeb13
* rename infd/outfd to writefd/readfd, to avoid confusion
Matt Johnston <matt@ucc.asn.au>
parents:
184
diff
changeset
|
207 channel->writefd = sock; |
64 | 208 channel->initconn = 1; |
209 | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
210 err = SSH_OPEN_IN_PROGRESS; |
64 | 211 |
212 out: | |
165
0cfba3034be5
Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents:
156
diff
changeset
|
213 TRACE(("leave newtcpdirect: err %d", err)) |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
214 return err; |
64 | 215 } |
156
8c2b3506f112
Rearrange preprocessor parts so that compilation with various options
Matt Johnston <matt@ucc.asn.au>
parents:
108
diff
changeset
|
216 #endif /* ENABLE_CLI_REMOTETCPFWD */ |