lshg.c 6.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* lshg.c
 *
 * $Id$
 *
 * Connect to a gateway. */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 2000 Niels Möller
 *
 * 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
 */

#include "charset.h"
27
#include "client.h"
28
29
30
31
32
#include "connection.h"
#include "debug.h"
#include "format.h"
#include "gateway.h"
#include "gateway_commands.h"
33
#include "interact.h"
34
35
36
37
38
39
40
41
42
43
44
45
#include "io_commands.h"
#include "ssh.h"
#include "version.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>
#include <locale.h>

#include "lsh_argp.h"

/* Forward declarations */
46
47
struct command options2info;
#define OPTIONS2INFO (&options2info.super)
48
49
50
51
52
53
54


#include "lshg.c.x"

/* GABA:
   (class
     (name lshg_options)
55
     (super client_options)
56
     (vars
57
       (gateway object local_info)))
58
59
60
*/

static struct lshg_options *
61
62
63
make_options(struct io_backend *backend,
	     struct exception_handler *handler,
	     int *exit_code) 
64
65
{
  NEW(lshg_options, self);
66
  init_client_options(&self->super, backend, handler, exit_code);
67

68
  self->gateway = NULL;
69
70
71
72

  return self;
}

73
74
75
76
77
DEFINE_COMMAND(options2info)
     (struct command *s UNUSED,
      struct lsh_object *a,
      struct command_continuation *c,
      struct exception_handler *e UNUSED)
78
79
{
  CAST(lshg_options, self, a);
80
81
82
83
  COMMAND_RETURN(c,
		 make_gateway_address(self->super.local_user,
				      self->super.user,
				      self->super.remote));
84
85
86
87
88
89
90
91
92
93
94
95
96
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:
   (expr
     (name make_lshg_connect)
     (params
       (backend object io_backend))
     (expr
       (lambda (options)
         ((progn (options2actions options))
	  (gateway_init
	    (connect_local backend (options2info options)))))))
*/

/* GABA:
   (class
     (name lshg_simple_action)
     (super command)
     (vars
       (msg . "const char *")))
*/

static void
do_lshg_send_debug(struct command *s,
		   struct lsh_object *x,
		   struct command_continuation *c UNUSED,
		   struct exception_handler *e UNUSED)
{
  CAST(lshg_simple_action, self, s);
  CAST(ssh_connection, connection, x);

  send_debug_message(connection->write, self->msg, 1);
}

static struct command *
make_lshg_send_debug(const char *msg)
{
  NEW(lshg_simple_action, self);
  self->msg = msg;
  self->super.call = do_lshg_send_debug;

  return &self->super;
}

static void
do_lshg_send_ignore(struct command *s,
		    struct lsh_object *x,
		    struct command_continuation *c UNUSED,
		    struct exception_handler *e UNUSED)
{
  CAST(lshg_simple_action, self, s);
  CAST(ssh_connection, connection, x);

  C_WRITE(connection, ssh_format("%c%z", SSH_MSG_IGNORE, self->msg));
}

static struct command *
make_lshg_send_ignore(const char *msg)
{
  NEW(lshg_simple_action, self);
  self->msg = msg;
  self->super.call = do_lshg_send_ignore;

  return &self->super;
}


/* Option parsing */

const char *argp_program_version
= "lshg-" VERSION ", secsh protocol version " CLIENT_PROTOCOL_VERSION;

const char *argp_program_bug_address = BUG_ADDRESS;

#define ARG_NOT 0x400

static const struct argp_option
main_options[] =
{
  /* Name, key, arg-name, flags, doc, group */
  { "send-debug", 'D', "Message", 0, "Send a debug message "
166
    "to the remote machine.", CLIENT_ARGP_ACTION_GROUP },
167
168
169
170
171
172
173
174
  { "send-ignore", 'I', "Message", 0, "Send an ignore message "
    "to the remote machine.", 0 },
  { NULL, 0, NULL, 0, NULL, 0 }
};

static const struct argp_child
main_argp_children[] =
{
175
  { &client_argp, 0, "", 0 },
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  { &werror_argp, 0, "", 0 },
  { NULL, 0, NULL, 0}
};


static error_t
main_argp_parser(int key, char *arg, struct argp_state *state)
{
  CAST(lshg_options, self, state->input);
  
  switch(key)
    {
    default:
      return ARGP_ERR_UNKNOWN;
    case ARGP_KEY_INIT:
191
192
      state->child_inputs[0] = &self->super;
      state->child_inputs[1] = NULL;
193
      break;
194

195
    case ARGP_KEY_END:
196
      if (!self->super.local_user)
197
198
199
200
	{
	  argp_error(state, "You have to set LOGNAME in the environment.");
	  break;
	}
201
202
203
      assert(self->super.user);
      assert(self->super.remote);
      
204
205
206
207
208
209
      if (object_queue_is_empty(&self->super.actions))
	{
	  argp_error(state, "No actions given.");
	  break;
	}

210
211
212
      self->gateway = make_gateway_address(self->super.local_user,
					   self->super.user,
					   self->super.remote);
213

214
      break;
215
216

    case 'D':
217
      client_add_action(&self->super, make_lshg_send_debug(arg));
218
219
220
      break;

    case 'I':
221
      client_add_action(&self->super, make_lshg_send_ignore(arg));
222
223
224
225
226
227
228
229
      break;
    }
  return 0;
}

static const struct argp
main_argp =
{ main_options, main_argp_parser, 
230
  "host\nhost command",
231
232
  "Connects to a remote machine, using a gateway\v"
  "Connects to the remote machine, using a local gateway, previously setup"
233
  "by running lsh -G.",
234
235
236
237
238
239
240
241
  main_argp_children,
  NULL, NULL
};

static void
do_exc_lshg_handler(struct exception_handler *s UNUSED,
		    const struct exception *e)
{
242
  werror("Exiting: %z\n", e->msg);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  exit(EXIT_FAILURE);
}

static struct exception_handler *
make_lshg_exception_handler(struct exception_handler *parent,
			    const char *context)
{
  return make_exception_handler(do_exc_lshg_handler, parent, context);
}

int
main(int argc, char **argv)
{
  struct lshg_options *options;
  struct io_backend *backend = make_io_backend();
258
259

  int exit_code = 17;
260
261
262
263
264
265
266
267
  
  /* For filtering messages. Could perhaps also be used when converting
   * strings to and from UTF8. */
  setlocale(LC_CTYPE, "");

  /* FIXME: Choose character set depending on the locale */
  set_local_charset(CHARSET_LATIN1);

268
269
270
271
272
273
  options = make_options(backend,
			 make_lshg_exception_handler
			 (&default_exception_handler,
			  HANDLER_CONTEXT),
			 &exit_code);
  
274
275
276
277
278
279
280
281
282
283
284
285
  argp_parse(&main_argp, argc, argv, ARGP_IN_ORDER, NULL, options);

  {
    CAST_SUBTYPE(command, lshg_connect,
		 make_lshg_connect(backend));

    COMMAND_CALL(lshg_connect, options, &discard_continuation,
		 make_lshg_exception_handler(&default_exception_handler,
					     HANDLER_CONTEXT));
  }

  io_run(backend);
286

Niels Möller's avatar
Niels Möller committed
287
288
289
  /* Close all files and other resources associated with the backend. */
  io_final(backend);

290
  gc_final();
291
  
292
  return exit_code;
293
}