lshg.c 7.71 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.c"
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
46
47
48
#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 */
struct command_simple options2info;
#define OPTIONS2INFO (&options2info.super.super)

49
#if 0
50
51
struct command_simple options2actions;
#define OPTIONS2ACTIONS (&options2actions.super.super)
52
#endif
53
54
55
56
57
58

#include "lshg.c.x"

/* GABA:
   (class
     (name lshg_options)
59
     (super client_options)
60
     (vars
61
       (gateway object local_info)))
62
63
64
*/

static struct lshg_options *
65
66
67
make_options(struct io_backend *backend,
	     struct exception_handler *handler,
	     int *exit_code) 
68
69
{
  NEW(lshg_options, self);
70
  init_client_options(&self->super, backend, handler, exit_code);
71

72
  self->gateway = NULL;
73
74
75
76
77
78
79

  return self;
}

DEFINE_COMMAND_SIMPLE(options2info, a)
{
  CAST(lshg_options, self, a);
80
81
82
  return &make_gateway_address(self->super.local_user,
			       self->super.user,
			       self->super.remote)->super;
83
84
}

85
#if 0
86
87
88
89
90
DEFINE_COMMAND_SIMPLE(options2actions, a)
{
  CAST(lshg_options, self, a);
  return &queue_to_list(&self->actions)->super.super;
}
91
#endif
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
166
167
168
169
170

/* 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 */
171
#if 0
172
173
174
  { "port", 'p', "Port", 0, "Connect to this port.", 0 },
  { "user", 'l', "User name", 0, "Login as this user.", 0 },
  { NULL, 0, NULL, 0, "Actions:", 0 },
175
176
  { "execute", 'E', "command", 0, "Execute a command on the remote machine", 0 },  
  { "shell", 'S', "command", 0, "Spawn a remote shell", 0 },
177
#endif
178
  { "send-debug", 'D', "Message", 0, "Send a debug message "
179
    "to the remote machine.", CLIENT_ARGP_ACTION_GROUP },
180
181
182
183
184
185
186
187
  { "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[] =
{
188
  { &client_argp, 0, "", 0 },
189
190
191
192
  { &werror_argp, 0, "", 0 },
  { NULL, 0, NULL, 0}
};

193
#if 0
194
195
196
197
198
199
200
201
202
static struct command *
lshg_add_action(struct lshg_options *self,
		struct command *action)
{
  if (action)
    object_queue_add_tail(&self->actions, &action->super);

  return action;
}
203
#endif
204
205
206
207
208
209
210
211
212
213
214

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:
215
216
      state->child_inputs[0] = &self->super;
      state->child_inputs[1] = NULL;
217
      break;
218
#if 0
219
220
221
    case ARGP_KEY_NO_ARGS:
      argp_usage(state);
      break;
222

223
224
225
226
227
228
229
230
231
232
    case ARGP_KEY_ARG:
      if (!state->arg_num)
	{
	  self->remote = make_address_info_c(arg, "ssh", 22);
	  assert(self->remote);
	  break;
	}
      else
	/* Let the next case parse it.  */
	return ARGP_ERR_UNKNOWN;
233

234
235
236
237
238
    case ARGP_KEY_ARGS:
      /* Handle command line. */
      break;
#endif
    case ARGP_KEY_END:
239
      if (!self->super.local_user)
240
241
242
243
	{
	  argp_error(state, "You have to set LOGNAME in the environment.");
	  break;
	}
244
245
246
247
248
249
      assert(self->super.user);
      assert(self->super.remote);
      
      self->gateway = make_gateway_address(self->super.local_user,
					   self->super.user,
					   self->super.remote);
250

251
#if 0
252
      if (object_queue_is_empty(&self->super.actions))
253
254
255
256
	{
	  argp_error(state, "No actions given.");
	    break;
	}
257
#endif 
258
      break;
259
260
261
262
#if 0
    case 'E':
      lshg_add_action(self, lsh_command_session(self, ssh_format("%lz", arg)));
      break;
263

264
265
266
267
    case 'S':
      lshg_add_action(self, lsh_shell_session(self));
      break;
#endif
268
    case 'D':
269
      client_add_action(&self->super, make_lshg_send_debug(arg));
270
271
272
      break;

    case 'I':
273
      client_add_action(&self->super, make_lshg_send_ignore(arg));
274
275
276
277
278
279
280
281
282
283
284
285
286
      break;
#if 0
    case 'n':
      self->not = !self->not;
      break;
#endif      
    }
  return 0;
}

static const struct argp
main_argp =
{ main_options, main_argp_parser, 
287
  "host\nhost command",
288
289
  "Connects to a remote machine, using a gateway\v"
  "Connects to the remote machine, using a local gateway, previously setup"
290
  "by running lsh -G.",
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  main_argp_children,
  NULL, NULL
};

static void
do_exc_lshg_handler(struct exception_handler *s UNUSED,
		    const struct exception *e)
{
  werror(e->msg);
  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();
315
316

  int exit_code = 17;
317
318
319
320
321
322
323
324
  
  /* 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);

325
326
327
328
329
330
  options = make_options(backend,
			 make_lshg_exception_handler
			 (&default_exception_handler,
			  HANDLER_CONTEXT),
			 &exit_code);
  
331
332
333
334
335
336
337
338
339
340
341
342
343
  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);
  
344
  return exit_code;
345
}