comparison cli-tcpfwd.c @ 579:8c737cd7c1af

merge of '48fdaa8706d1acda35e9d564adc9a1fbc96c18c8' and '658fd03abd21e0da7c4c89b9fff9dc693c72daae'
author Matt Johnston <matt@ucc.asn.au>
date Sat, 27 Feb 2010 11:53:18 +0000
parents 69e98c45db7c 44f486b72427
children 345aaed42ef4
comparison
equal deleted inserted replaced
577:69e98c45db7c 579:8c737cd7c1af
59 }; 59 };
60 #endif 60 #endif
61 61
62 #ifdef ENABLE_CLI_LOCALTCPFWD 62 #ifdef ENABLE_CLI_LOCALTCPFWD
63 void setup_localtcp() { 63 void setup_localtcp() {
64 64 m_list_elem *iter;
65 int ret; 65 int ret;
66 66
67 TRACE(("enter setup_localtcp")) 67 TRACE(("enter setup_localtcp"))
68 68
69 if (cli_opts.localfwds == NULL) { 69 for (iter = cli_opts.localfwds->first; iter; iter = iter->next) {
70 TRACE(("cli_opts.localfwds == NULL")) 70 struct TCPFwdEntry * fwd = (struct TCPFwdEntry*)iter->item;
71 }
72
73 while (cli_opts.localfwds != NULL) {
74 ret = cli_localtcp( 71 ret = cli_localtcp(
75 cli_opts.localfwds->listenaddr, 72 fwd->listenaddr,
76 cli_opts.localfwds->listenport, 73 fwd->listenport,
77 cli_opts.localfwds->connectaddr, 74 fwd->connectaddr,
78 cli_opts.localfwds->connectport); 75 fwd->connectport);
79 if (ret == DROPBEAR_FAILURE) { 76 if (ret == DROPBEAR_FAILURE) {
80 dropbear_log(LOG_WARNING, "Failed local port forward %d:%s:%d", 77 dropbear_log(LOG_WARNING, "Failed local port forward %s:%d:%s:%d",
81 cli_opts.localfwds->listenaddr, 78 fwd->listenaddr,
82 cli_opts.localfwds->listenport, 79 fwd->listenport,
83 cli_opts.localfwds->connectaddr, 80 fwd->connectaddr,
84 cli_opts.localfwds->connectport); 81 fwd->connectport);
85 } 82 }
86
87 cli_opts.localfwds = cli_opts.localfwds->next;
88 } 83 }
89 TRACE(("leave setup_localtcp")) 84 TRACE(("leave setup_localtcp"))
90 85
91 } 86 }
92 87
154 /* The only global success/failure messages are for remotetcp. 149 /* The only global success/failure messages are for remotetcp.
155 * Since there isn't any identifier in these messages, we have to rely on them 150 * Since there isn't any identifier in these messages, we have to rely on them
156 * being in the same order as we sent the requests. This is the ordering 151 * being in the same order as we sent the requests. This is the ordering
157 * of the cli_opts.remotefwds list */ 152 * of the cli_opts.remotefwds list */
158 void cli_recv_msg_request_success() { 153 void cli_recv_msg_request_success() {
159
160 /* Nothing in the packet. We just mark off that we have received the reply, 154 /* Nothing in the packet. We just mark off that we have received the reply,
161 * so that we can report failure for later ones. */ 155 * so that we can report failure for later ones. */
162 struct TCPFwdList * iter = NULL; 156 m_list_elem * iter = NULL;
163 157 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
164 iter = cli_opts.remotefwds; 158 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
165 while (iter != NULL) { 159 if (!fwd->have_reply) {
166 if (!iter->have_reply) 160 fwd->have_reply = 1;
167 {
168 iter->have_reply = 1;
169 return; 161 return;
170 } 162 }
171 iter = iter->next;
172 } 163 }
173 } 164 }
174 165
175 void cli_recv_msg_request_failure() { 166 void cli_recv_msg_request_failure() {
176 struct TCPFwdList * iter = NULL; 167 m_list_elem *iter;
177 168 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
178 iter = cli_opts.remotefwds; 169 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
179 while (iter != NULL) { 170 if (!fwd->have_reply) {
180 if (!iter->have_reply) 171 fwd->have_reply = 1;
181 { 172 dropbear_log(LOG_WARNING, "Remote TCP forward request failed (port %d -> %s:%d)", fwd->listenport, fwd->connectaddr, fwd->connectport);
182 iter->have_reply = 1;
183 dropbear_log(LOG_WARNING, "Remote TCP forward request failed (port %d -> %s:%d)", iter->listenport, iter->connectaddr, iter->connectport);
184 return; 173 return;
185 } 174 }
186 iter = iter->next;
187 } 175 }
188 } 176 }
189 177
190 void setup_remotetcp() { 178 void setup_remotetcp() {
191 179 m_list_elem *iter;
192 struct TCPFwdList * iter = NULL;
193
194 TRACE(("enter setup_remotetcp")) 180 TRACE(("enter setup_remotetcp"))
195 181
196 if (cli_opts.remotefwds == NULL) { 182 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
197 TRACE(("cli_opts.remotefwds == NULL")) 183 struct TCPFwdEntry *fwd = (struct TCPFwdEntry*)iter->item;
198 } 184 if (!fwd->listenaddr)
199
200 iter = cli_opts.remotefwds;
201
202 while (iter != NULL) {
203 if (!iter->listenaddr)
204 { 185 {
205 // we store the addresses so that we can compare them 186 // we store the addresses so that we can compare them
206 // when the server sends them back 187 // when the server sends them back
207 if (opts.listen_fwd_all) { 188 if (opts.listen_fwd_all) {
208 iter->listenaddr = m_strdup(""); 189 fwd->listenaddr = m_strdup("");
209 } else { 190 } else {
210 iter->listenaddr = m_strdup("localhost"); 191 fwd->listenaddr = m_strdup("localhost");
211 } 192 }
212 } 193 }
213 send_msg_global_request_remotetcp(iter->listenaddr, iter->listenport); 194 send_msg_global_request_remotetcp(fwd->listenaddr, fwd->listenport);
214 iter = iter->next; 195 }
215 } 196
216 TRACE(("leave setup_remotetcp")) 197 TRACE(("leave setup_remotetcp"))
217 } 198 }
218 199
219 static int newtcpforwarded(struct Channel * channel) { 200 static int newtcpforwarded(struct Channel * channel) {
220 201
221 char *origaddr = NULL; 202 char *origaddr = NULL;
222 unsigned int origport; 203 unsigned int origport;
223 struct TCPFwdList * iter = NULL; 204 m_list_elem * iter = NULL;
205 struct TCPFwdEntry *fwd;
224 char portstring[NI_MAXSERV]; 206 char portstring[NI_MAXSERV];
225 int sock; 207 int sock;
226 int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; 208 int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
227 209
228 origaddr = buf_getstring(ses.payload, NULL); 210 origaddr = buf_getstring(ses.payload, NULL);
229 origport = buf_getint(ses.payload); 211 origport = buf_getint(ses.payload);
230 212
231 /* Find which port corresponds */ 213 /* Find which port corresponds */
232 iter = cli_opts.remotefwds; 214 for (iter = cli_opts.remotefwds->first; iter; iter = iter->next) {
233 215 fwd = (struct TCPFwdEntry*)iter->item;
234 while (iter != NULL) { 216 if (origport == fwd->listenport
235 if (origport == iter->listenport 217 && (strcmp(origaddr, fwd->listenaddr) == 0)) {
236 && (strcmp(origaddr, iter->listenaddr) == 0)) {
237 break; 218 break;
238 } 219 }
239 iter = iter->next;
240 } 220 }
241 221
242 if (iter == NULL) { 222 if (iter == NULL) {
243 /* We didn't request forwarding on that port */ 223 /* We didn't request forwarding on that port */
244 cleantext(origaddr); 224 cleantext(origaddr);
245 dropbear_log(LOG_INFO, "Server sent unrequested forward from \"%s:%d\"", 225 dropbear_log(LOG_INFO, "Server sent unrequested forward from \"%s:%d\"",
246 origaddr, origport); 226 origaddr, origport);
247 goto out; 227 goto out;
248 } 228 }
249 229
250 snprintf(portstring, sizeof(portstring), "%d", iter->connectport); 230 snprintf(portstring, sizeof(portstring), "%d", fwd->connectport);
251 sock = connect_remote(iter->connectaddr, portstring, 1, NULL); 231 sock = connect_remote(fwd->connectaddr, portstring, 1, NULL);
252 if (sock < 0) { 232 if (sock < 0) {
253 TRACE(("leave newtcpdirect: sock failed")) 233 TRACE(("leave newtcpdirect: sock failed"))
254 err = SSH_OPEN_CONNECT_FAILED; 234 err = SSH_OPEN_CONNECT_FAILED;
255 goto out; 235 goto out;
256 } 236 }
263 channel->initconn = 1; 243 channel->initconn = 1;
264 244
265 err = SSH_OPEN_IN_PROGRESS; 245 err = SSH_OPEN_IN_PROGRESS;
266 246
267 out: 247 out:
268 m_free(origaddr); 248 m_free(origaddr);
269 TRACE(("leave newtcpdirect: err %d", err)) 249 TRACE(("leave newtcpdirect: err %d", err))
270 return err; 250 return err;
271 } 251 }
272 #endif /* ENABLE_CLI_REMOTETCPFWD */ 252 #endif /* ENABLE_CLI_REMOTETCPFWD */