Mercurial > dropbear
annotate cli-tcpfwd.c @ 108:10f4d3319780
- added circular buffering for channels
- added stderr support for the client
- cleaned up a bunch of "unused" warnings, duplicated header definitions
- added exit-status support for the client
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 26 Aug 2004 13:16:40 +0000 |
parents | e3adf4cf5465 |
children | 8c2b3506f112 |
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 |
64 | 34 static int cli_localtcp(unsigned int listenport, const char* remoteaddr, |
35 unsigned int remoteport); | |
36 static int newtcpforwarded(struct Channel * channel); | |
37 | |
38 const struct ChanType cli_chan_tcpremote = { | |
39 1, /* sepfds */ | |
40 "forwarded-tcpip", | |
41 newtcpforwarded, | |
42 NULL, | |
43 NULL, | |
44 NULL | |
45 }; | |
62 | 46 static const struct ChanType cli_chan_tcplocal = { |
47 1, /* sepfds */ | |
48 "direct-tcpip", | |
49 NULL, | |
50 NULL, | |
64 | 51 NULL, |
62 | 52 NULL |
53 }; | |
54 | |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
55 void setup_localtcp() { |
62 | 56 |
64 | 57 int ret; |
58 | |
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
|
59 TRACE(("enter setup_localtcp")); |
38c3146aa23d
Some more sanity-checking of args, and just warn and ignore OpenSSH args
Matt Johnston <matt@ucc.asn.au>
parents:
64
diff
changeset
|
60 |
64 | 61 if (cli_opts.localfwds == NULL) { |
62 TRACE(("cli_opts.localfwds == NULL")); | |
63 } | |
64 | |
65 while (cli_opts.localfwds != NULL) { | |
66 ret = cli_localtcp(cli_opts.localfwds->listenport, | |
67 cli_opts.localfwds->connectaddr, | |
68 cli_opts.localfwds->connectport); | |
69 if (ret == DROPBEAR_FAILURE) { | |
70 dropbear_log(LOG_WARNING, "Failed local port forward %d:%s:%d", | |
71 cli_opts.localfwds->listenport, | |
72 cli_opts.localfwds->connectaddr, | |
73 cli_opts.localfwds->connectport); | |
74 } | |
75 | |
76 cli_opts.localfwds = cli_opts.localfwds->next; | |
77 } | |
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
|
78 TRACE(("leave setup_localtcp")); |
62 | 79 |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
80 } |
62 | 81 |
63
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
82 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
|
83 unsigned int remoteport) { |
62 | 84 |
85 struct TCPListener* tcpinfo = NULL; | |
64 | 86 int ret; |
87 | |
88 TRACE(("enter cli_localtcp: %d %s %d", listenport, remoteaddr, | |
89 remoteport)); | |
62 | 90 |
91 tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener*)); | |
108
10f4d3319780
- added circular buffering for channels
Matt Johnston <matt@ucc.asn.au>
parents:
74
diff
changeset
|
92 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
|
93 tcpinfo->sendport = remoteport; |
dcc43965928f
- A nice cleaner structure for tcp (acceptor) forwarding.
Matt Johnston <matt@ucc.asn.au>
parents:
62
diff
changeset
|
94 tcpinfo->listenport = listenport; |
62 | 95 tcpinfo->chantype = &cli_chan_tcplocal; |
96 | |
97 ret = listen_tcpfwd(tcpinfo); | |
98 | |
99 if (ret == DROPBEAR_FAILURE) { | |
100 m_free(tcpinfo); | |
101 } | |
64 | 102 TRACE(("leave cli_localtcp: %d", ret)); |
62 | 103 return ret; |
104 } | |
64 | 105 |
106 static void send_msg_global_request_remotetcp(int port) { | |
107 | |
108 TRACE(("enter send_msg_global_request_remotetcp")); | |
109 | |
110 CHECKCLEARTOWRITE(); | |
111 buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST); | |
112 buf_putstring(ses.writepayload, "tcpip-forward", 13); | |
113 buf_putbyte(ses.writepayload, 0); | |
114 buf_putstring(ses.writepayload, "0.0.0.0", 7); /* TODO: IPv6? */ | |
115 buf_putint(ses.writepayload, port); | |
116 | |
117 encrypt_packet(); | |
118 | |
119 TRACE(("leave send_msg_global_request_remotetcp")); | |
120 } | |
121 | |
122 void setup_remotetcp() { | |
123 | |
124 struct TCPFwdList * iter = NULL; | |
125 | |
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
|
126 TRACE(("enter setup_remotetcp")); |
38c3146aa23d
Some more sanity-checking of args, and just warn and ignore OpenSSH args
Matt Johnston <matt@ucc.asn.au>
parents:
64
diff
changeset
|
127 |
64 | 128 if (cli_opts.remotefwds == NULL) { |
129 TRACE(("cli_opts.remotefwds == NULL")); | |
130 } | |
131 | |
132 iter = cli_opts.remotefwds; | |
133 | |
134 while (iter != NULL) { | |
135 send_msg_global_request_remotetcp(iter->listenport); | |
136 iter = iter->next; | |
137 } | |
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
|
138 TRACE(("leave setup_remotetcp")); |
64 | 139 } |
140 | |
141 static int newtcpforwarded(struct Channel * channel) { | |
142 | |
143 unsigned int origport; | |
144 struct TCPFwdList * iter = NULL; | |
145 char portstring[NI_MAXSERV]; | |
146 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
|
147 int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; |
64 | 148 |
149 /* We don't care what address they connected to */ | |
150 buf_eatstring(ses.payload); | |
151 | |
152 origport = buf_getint(ses.payload); | |
153 | |
154 /* Find which port corresponds */ | |
155 iter = cli_opts.remotefwds; | |
156 | |
157 while (iter != NULL) { | |
158 if (origport == iter->listenport) { | |
159 break; | |
160 } | |
161 iter = iter->next; | |
162 } | |
163 | |
164 if (iter == NULL) { | |
165 /* We didn't request forwarding on that port */ | |
166 dropbear_log(LOG_INFO, "Server send unrequested port, from port %d", | |
167 origport); | |
168 goto out; | |
169 } | |
170 | |
171 snprintf(portstring, sizeof(portstring), "%d", iter->connectport); | |
172 sock = connect_remote(iter->connectaddr, portstring, 1, NULL); | |
173 if (sock < 0) { | |
174 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
|
175 err = SSH_OPEN_CONNECT_FAILED; |
64 | 176 goto out; |
177 } | |
178 | |
179 ses.maxfd = MAX(ses.maxfd, sock); | |
180 | |
181 /* Note that infd is actually the "outgoing" direction on the | |
182 * tcp connection, vice versa for outfd. | |
183 * We don't set outfd, that will get set after the connection's | |
184 * progress succeeds */ | |
185 channel->infd = sock; | |
186 channel->initconn = 1; | |
187 | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
188 err = SSH_OPEN_IN_PROGRESS; |
64 | 189 |
190 out: | |
70
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
191 TRACE(("leave newtcpdirect: err %d", err)); |
b0316ce64e4b
Merging in the changes from 0.41-0.43 main Dropbear tree
Matt Johnston <matt@ucc.asn.au>
parents:
66
diff
changeset
|
192 return err; |
64 | 193 } |