tcpforward.c 5.25 KB
Newer Older
1
/* tcpforward.c
2
3
4
5
6
 *
 */

/* lsh, an implementation of the ssh protocol
 *
7
 * Copyright (C) 1998, 2005, 2008 Balzs Scheidler, Niels Mller
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23

24
25
26
27
28
#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
29
#include <string.h>
30

31
32
#include "tcpforward.h"

33
#include "channel_forward.h"
34
#include "format.h"
35
#include "io.h"
36
#include "lsh_string.h"
37
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
38

39
40
#include "werror.h"

41

42
#define GABA_DEFINE
43
#include "tcpforward.h.x"
44
#undef GABA_DEFINE
45
46
47

#include "tcpforward.c.x"

48
49
/* Structures used to keep track of forwarded ports */

50
51
52
struct forwarded_port *
tcpforward_lookup(struct object_queue *q,
		  uint32_t length, const uint8_t *ip, uint32_t port)
53
54
55
{
  FOR_OBJECT_QUEUE(q, n)
    {
56
      CAST_SUBTYPE(forwarded_port, f, n);
57
      
58
59
      if ( (port == f->address->port)
	   && lsh_string_eq_l(f->address->ip, length, ip) )
60
61
62
63
64
	return f;
    }
  return NULL;
}

65
66
67
68
69
70
struct forwarded_port *
tcpforward_remove(struct object_queue *q,
		  uint32_t length, const uint8_t *ip, uint32_t port)
{
  FOR_OBJECT_QUEUE(q, n)
    {
71
      CAST_SUBTYPE(forwarded_port, p, n);
72
73
74
75
76
77
78
79
80
81
82

      if ( (port == p->address->port)
	   && lsh_string_eq_l(p->address->ip, length, ip) )
	{
	  FOR_OBJECT_QUEUE_REMOVE(q, n);
	  return p;
	}
    }
  return NULL;
}

83
84
int
tcpforward_remove_port(struct object_queue *q, struct forwarded_port *port)
85
86
87
{
  FOR_OBJECT_QUEUE(q, n)
    {
88
      CAST_SUBTYPE(forwarded_port, f, n);
89
      
90
      if (port == f)
91
	{
92
93
	  FOR_OBJECT_QUEUE_REMOVE(q, n);
	  return 1;
94
95
	}
    }
96
  return 0;
97
98
}

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* GABA:
   (class
     (name tcpforward_listen_port)
     (super io_listen_port)
     (vars
       (type . int)
       (connection object ssh_connection)
       (forward const object address_info)))       
*/

static void
do_tcpforward_listen_port_accept(struct io_listen_port *s,
				 int fd,
				 socklen_t addr_length,
				 const struct sockaddr *addr)  
{
  CAST(tcpforward_listen_port, self, s);

  struct channel_forward *channel;
  struct address_info *peer = sockaddr2info(addr_length, addr);
  trace("forward_local_port\n");

  io_register_fd(fd, "forwarded socket");
  channel = make_channel_forward(fd, TCPIP_WINDOW_SIZE);

  if (!channel_open_new_type(self->connection, &channel->super,
			     ATOM_LD(self->type),
			     "%S%i%S%i",
			     self->forward->ip, self->forward->port,
			     peer->ip, peer->port))
    {
      werror("tcpforward_listen_port: Allocating a local channel number failed.");
      KILL_RESOURCE(&channel->super.super);
    }
}

struct io_listen_port *
make_tcpforward_listen_port(struct ssh_connection *connection,
			    int type,
			    const struct address_info *local,
			    const struct address_info *forward)
{
  struct sockaddr *addr;
  socklen_t addr_length;
  int fd;

  addr = io_make_sockaddr(&addr_length,
			  lsh_get_cstring(local->ip), local->port);
  if (!addr)
    return NULL;

  fd = io_bind_sockaddr((struct sockaddr *) addr, addr_length);
  if (fd < 0)
    return NULL;

  {
    NEW(tcpforward_listen_port, self);
    init_io_listen_port(&self->super, fd, do_tcpforward_listen_port_accept);

    self->connection = connection;
    self->type = type;
    self->forward = forward;

    return &self->super;
  }
}

166
167
168
169
170
/* GABA:
   (class
     (name tcpforward_connect_state)
     (super io_connect_state)
     (vars
171
       (info const object channel_open_info)))
172
173
174
175
176
177
178
*/

static void
tcpforward_connect_done(struct io_connect_state *s, int fd)
{
  CAST(tcpforward_connect_state, self, s);

179
180
181
182
183
  struct channel_forward *channel
    = make_channel_forward(fd, TCPIP_WINDOW_SIZE);
  
  channel_open_confirm(self->info, &channel->super);
  channel_forward_start_io(channel);  
184
185
186
187
188
189
190
191
}

static void
tcpforward_connect_error(struct io_connect_state *s, int error)
{
  CAST(tcpforward_connect_state, self, s);
  
  werror("Connection failed, socket error %i\n", error);
192
193
  channel_open_deny(self->info,
		    SSH_OPEN_CONNECT_FAILED, "Connection failed");
194
195
196
}

struct resource *
197
tcpforward_connect(const struct address_info *a,
198
		   const struct channel_open_info *info)
199
200
201
202
203
204
205
{
  struct sockaddr *addr;
  socklen_t addr_length;

  addr = io_make_sockaddr(&addr_length, lsh_get_cstring(a->ip), a->port);
  if (!addr)
    {
206
      channel_open_deny(info, SSH_OPEN_CONNECT_FAILED, "Invalid address");
207
208
209
210
211
212
213
214
215
216
      return NULL;
    }

  {
    NEW(tcpforward_connect_state, self);
    init_io_connect_state(&self->super,
			  tcpforward_connect_done,
			  tcpforward_connect_error);
    int res;
    
217
    self->info = info;
218
219
220
221
222
223

    res = io_connect(&self->super, addr_length, addr);
    lsh_space_free(addr);

    if (!res)
      {
224
	channel_open_deny(info, SSH_OPEN_CONNECT_FAILED, STRERROR(res));
225
226
	return NULL;
      }
227
    return &self->super.super.super;
228
229
  }
}