client.c 33.6 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* client.c
2
3
4
5
6
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
7
 * Copyright (C) 1998, 1999, 2000 Niels Mller
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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
J.H.M. Dassen's avatar
J.H.M. Dassen committed
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Niels Möller's avatar
Niels Möller committed
22
23
24
 */

#include "client.h"
25

Niels Möller's avatar
Niels Möller committed
26
#include "abstract_io.h"
27
#include "channel.h"
28
#include "channel_commands.h"
29
#include "connection.h"
30
#include "environ.h"
31
32
33
#include "format.h"
#include "interact.h"
#include "io.h"
Niels Möller's avatar
Niels Möller committed
34
#include "pad.h"
35
36
#include "parse.h"
#include "ssh.h"
37
#include "suspend.h"
38
#include "tcpforward_commands.h"
Niels Möller's avatar
Niels Möller committed
39
#include "translate_signal.h"
40
41
#include "werror.h"
#include "xalloc.h"
42
#include "io.h"
Niels Möller's avatar
Niels Möller committed
43

44
#include <assert.h>
45
#include <string.h>
Niels Möller's avatar
Niels Möller committed
46

47
48
49
50
51
52
53
54
55
56
57
58
#include <signal.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "lsh_argp.h"

59
60
61
62
#define GABA_DEFINE
#include "client.h.x"
#undef GABA_DEFINE

63
64
#include "client.c.x"

65
66
#define DEFAULT_ESCAPE_CHAR '~'

67
68
69
70
71
72
static struct lsh_string *
format_service_request(int name)
{
  return ssh_format("%c%a", SSH_MSG_SERVICE_REQUEST, name);
}

73
74
/* Start a service that the server has accepted (for instance
 * ssh-userauth). */
75
/* GABA:
76
77
78
79
   (class
     (name accept_service_handler)
     (super packet_handler)
     (vars
Niels Möller's avatar
Niels Möller committed
80
       (service . int)
81
       (c object command_continuation)))
82
83
*/

Niels Möller's avatar
Niels Möller committed
84
85
86
87
static void
do_accept_service(struct packet_handler *c,
		  struct ssh_connection *connection,
		  struct lsh_string *packet)
88
{
89
  CAST(accept_service_handler, closure, c);
90
91

  struct simple_buffer buffer;
92
  unsigned msg_number;
Niels Möller's avatar
Niels Möller committed
93
  int name;
94

95
96
  simple_buffer_init(&buffer, packet->length, packet->data);
  
97
98
  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_SERVICE_ACCEPT)
99
100
101
102
103
104
105
106
107
      && (
#if DATAFELLOWS_WORKAROUNDS
	  (connection->peer_flags & PEER_SERVICE_ACCEPT_KLUDGE)
#else
	  0
#endif
	  || (parse_atom(&buffer, &name)
	      && (name == closure->service)))
      && parse_eod(&buffer))
108
    {
109
      connection->dispatch[SSH_MSG_SERVICE_ACCEPT] = &connection_fail_handler;
110
      
Niels Möller's avatar
Niels Möller committed
111
      COMMAND_RETURN(closure->c, connection);
112
    }
Niels Möller's avatar
Niels Möller committed
113
  else
114
    PROTOCOL_ERROR(connection->e, "Invalid SSH_MSG_SERVICE_ACCEPT message");
115
116
}

117
struct packet_handler *
118
make_accept_service_handler(UINT32 service,
119
			    struct command_continuation *c)
120
{
121
  NEW(accept_service_handler, closure);
122

Niels Möller's avatar
Niels Möller committed
123
  closure->super.handler = do_accept_service;
124
  closure->service = service;
125
  closure->c = c;
126
  
127
128
129
  return &closure->super;
}

130
void
Niels Möller's avatar
Niels Möller committed
131
132
133
do_request_service(struct command *s,
		   struct lsh_object *x,
		   struct command_continuation *c,
134
		   struct exception_handler *e UNUSED)
135
{
136
137
  CAST(request_service, self, s);
  CAST(ssh_connection, connection, x);
138
139
140

  /* NOTE: Uses the connection's exception handler, not the one passed
   * in. */
Niels Möller's avatar
Niels Möller committed
141
  connection->dispatch[SSH_MSG_SERVICE_ACCEPT]
142
    = make_accept_service_handler(self->service, c);
Niels Möller's avatar
Niels Möller committed
143
  
Niels Möller's avatar
Niels Möller committed
144
145
  C_WRITE(connection,
	  format_service_request(self->service));
146
147
}

148
149
struct command *
make_request_service(int service)
150
{
151
  NEW(request_service, closure);
152

153
  closure->super.call = do_request_service;
154
155
156
157
158
  closure->service = service;

  return &closure->super;
}

159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* GABA:
   (class
     (name detach_callback)
     (super lsh_callback)
     (vars 
       (channel_flag . int)
       (fd_flag . int)
       (exit_status . "int *")))
*/

/* GABA:
   (class
     (name detach_resource)
     (super resource)
     (vars
       (c object detach_callback)))
*/

static void 
do_detach_res_kill(struct resource *r)
{
  CAST(detach_resource,self,r);

  trace("client.c:do_detach_res\n");
  self->c->channel_flag = 1;

  if (self->c->channel_flag && self->c->fd_flag)
    /* If the fd_flag is set, the callback should be changed */
    io_callout(&self->c->super, 0);
}

static struct resource*
make_detach_resource(struct lsh_callback *c)
{
   NEW(detach_resource, self);
   CAST(detach_callback, cb, c);

   trace("client.c:make_detach_resource\n");
   init_resource(&self->super, do_detach_res_kill);

   self->c = cb;

   return &self->super;
}


static void 
207
do_detach_cb(struct lsh_callback *c)
208
209
210
{
  CAST(detach_callback,self,c);

211
  trace("client.c: do_detach_cb\n");
212
  
213
  if (!self->fd_flag) /* First time around? */
214
    {
215
      self->fd_flag = 1; /* Note */
216
      
217
218
219
    if (self->channel_flag && self->fd_flag)
      /* If the fd is closed already, asked to be called from the main loop */ 
      io_callout(c, 0);
220
221

    }
222
223
224
  else
    {
      int pid = fork();
225

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
      /* Ignore any errors, what can we do? */
      
      switch(pid)
	{
	case -1: /* Fork failed, this we can handle by doing nothing */
	  werror("Fork failed, not detaching.\n");
	  break;
	  
	case 0:
	  /* Detach, lsh doesn't actually use these fds but dup(2)s them */
	  
	  close(STDIN_FILENO); 
	  close(STDOUT_FILENO); 
	  close(STDERR_FILENO); 
	  
	  /* Make sure they aren't used by any file lsh opens */
	  
	  open("/dev/null", O_RDONLY);
	  open("/dev/null", O_RDONLY);
	  open("/dev/null", O_RDONLY);
	  break;
	  
	default:
	  /* It's a good idea to reset std* to blocking mode. */
	  io_set_blocking(STDIN_FILENO);
	  io_set_blocking(STDOUT_FILENO);
	  io_set_blocking(STDERR_FILENO);
	  
	  exit(*self->exit_status);
	}
    }
257
258
259
260
261
262
263
}

static struct lsh_callback* 
make_detach_callback(int *exit_status)
{
   NEW(detach_callback, self);

264
   self->super.f = do_detach_cb;
265
266
267
268
269
270
271
   self->exit_status = exit_status;
   self->fd_flag = 0;
   self->channel_flag = 0;

   return &self->super;
}

272
/* GABA:
273
274
275
276
   (class
     (name exit_handler)
     (super channel_request)
     (vars
Niels Möller's avatar
Niels Möller committed
277
       (exit_status . "int *")))
278
279
*/

Niels Möller's avatar
Niels Möller committed
280
281
282
static void
do_exit_status(struct channel_request *c,
	       struct ssh_channel *channel,
Niels Möller's avatar
Niels Möller committed
283
	       struct channel_request_info *info,
284
285
286
	       struct simple_buffer *args,
	       struct command_continuation *s,
	       struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
287
{
288
  CAST(exit_handler, closure, c);
289
  UINT32 status;
Niels Möller's avatar
Niels Möller committed
290

Niels Möller's avatar
Niels Möller committed
291
  if (!info->want_reply
Niels Möller's avatar
Niels Möller committed
292
293
294
      && parse_uint32(args, &status)
      && parse_eod(args))
    {
295
296
      verbose("client.c: Receiving exit-status %i on channel %i\n",
	      status, channel->channel_number);
Niels Möller's avatar
Niels Möller committed
297

298
      *closure->exit_status = status;
299
300
      ALIST_SET(channel->request_types, ATOM_EXIT_STATUS, NULL);
      ALIST_SET(channel->request_types, ATOM_EXIT_SIGNAL, NULL);
301
      
302
      COMMAND_RETURN(s, NULL);
Niels Möller's avatar
Niels Möller committed
303
    }
Niels Möller's avatar
Niels Möller committed
304
305
  else
    /* Invalid request */
306
    PROTOCOL_ERROR(e, "Invalid exit-status message");
Niels Möller's avatar
Niels Möller committed
307
308
}

Niels Möller's avatar
Niels Möller committed
309
310
311
static void
do_exit_signal(struct channel_request *c,
	       struct ssh_channel *channel,
Niels Möller's avatar
Niels Möller committed
312
	       struct channel_request_info *info,
313
314
315
	       struct simple_buffer *args,
	       struct command_continuation *s,
	       struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
316
{
317
318
  CAST(exit_handler, closure, c);

319
  UINT32 signal;
Niels Möller's avatar
Niels Möller committed
320
321
  int core;

Niels Möller's avatar
Niels Möller committed
322
  const UINT8 *msg;
Niels Möller's avatar
Niels Möller committed
323
324
  UINT32 length;

Niels Möller's avatar
Niels Möller committed
325
  const UINT8 *language;
Niels Möller's avatar
Niels Möller committed
326
327
  UINT32 language_length;
  
Niels Möller's avatar
Niels Möller committed
328
  if (!info->want_reply
329
      && parse_atom(args, &signal)
Niels Möller's avatar
Niels Möller committed
330
331
332
333
334
335
336
337
338
339
      && parse_boolean(args, &core)
      && parse_string(args, &length, &msg)
      && parse_string(args, &language_length, &language)
      && parse_eod(args))
    {
      /* FIXME: What exit status should be returned when the remote
       * process dies violently? */

      *closure->exit_status = 7;

340
341
      werror("Remote process was killed by signal: %ups %z\n",
	     length, msg,
342
	     core ? "(core dumped remotely)\n": "");
343
      
344
345
      ALIST_SET(channel->request_types, ATOM_EXIT_STATUS, NULL);
      ALIST_SET(channel->request_types, ATOM_EXIT_SIGNAL, NULL);
Niels Möller's avatar
Niels Möller committed
346

347
      COMMAND_RETURN(s, NULL);
Niels Möller's avatar
Niels Möller committed
348
    }
Niels Möller's avatar
Niels Möller committed
349
350
  else
    /* Invalid request */
351
    PROTOCOL_ERROR(e, "Invalid exit-signal message");
Niels Möller's avatar
Niels Möller committed
352
353
}

354
355
struct channel_request *
make_handle_exit_status(int *exit_status)
Niels Möller's avatar
Niels Möller committed
356
{
357
  NEW(exit_handler, self);
Niels Möller's avatar
Niels Möller committed
358
359
360
361
362
363
364
365

  self->super.handler = do_exit_status;

  self->exit_status = exit_status;

  return &self->super;
}

366
367
struct channel_request *
make_handle_exit_signal(int *exit_status)
Niels Möller's avatar
Niels Möller committed
368
{
369
  NEW(exit_handler, self);
Niels Möller's avatar
Niels Möller committed
370
371
372
373
374
375
376
377

  self->super.handler = do_exit_signal;

  self->exit_status = exit_status;

  return &self->super;
}

378
379
380
381
382
383
384
/* GABA:
   (class
     (name session_open_command)
     (super channel_open_command)
     (vars
       ; This command can only be executed once,
       ; so we can allocate the session object in advance.
385
       (session object ssh_channel)))
386
387
388
389
390
*/

static struct ssh_channel *
new_session(struct channel_open_command *s,
	    struct ssh_connection *connection,
391
	    UINT32 local_channel_number,
392
393
394
395
	    struct lsh_string **request)
{
  CAST(session_open_command, self, s);
  struct ssh_channel *res;
396

397
  self->session->connection = connection;
398
  
399
400
401
  *request = format_channel_open(ATOM_SESSION,
				 local_channel_number,
				 self->session, "");
402
  
403
  res = self->session;
404
405
406
407
408
409
410

  /* Make sure this command can not be invoked again */
  self->session = NULL;

  return res;
}

411
412
struct command *
make_open_session_command(struct ssh_channel *session)
413
414
415
416
417
{
  NEW(session_open_command, self);
  self->super.super.call = do_channel_open_command;
  self->super.new_channel = new_session;
  self->session = session;
418
419

  return &self->super.super;
420
421
}

422
423
424
425
426
427
428
429

static struct lsh_string *
do_format_shell_request(struct channel_request_command *s UNUSED,
			struct ssh_channel *channel,
			struct command_continuation **c)
{
  return format_channel_request(ATOM_SHELL, channel, !!*c, "");
}
430

431
432
struct channel_request_command request_shell =
{ { STATIC_HEADER, do_channel_request_command }, do_format_shell_request };
433

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466

/* GABA:
   (class
     (name exec_request)
     (super channel_request_command)
     (vars
       (command string)))
*/

static struct lsh_string *
do_format_exec_request(struct channel_request_command *s,
		       struct ssh_channel *channel,
		       struct command_continuation **c)
{
  CAST(exec_request, self, s);

  verbose("lsh: Requesting remote exec.\n");

  return format_channel_request(ATOM_EXEC, channel,
				!!*c, "%S", self->command);
}

struct command *
make_exec_request(struct lsh_string *command)
{
  NEW(exec_request, req);

  req->super.format_request = do_format_exec_request;
  req->super.super.call = do_channel_request_command;
  req->command = command;

  return &req->super.super;
}
467

468

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* GABA:
   (class
     (name subsystem_request)
     (super channel_request_command)
     (vars
       (subsystem string)))
*/

static struct lsh_string *
do_format_subsystem_request(struct channel_request_command *s,
			    struct ssh_channel *channel,
			    struct command_continuation **c)
{
  CAST(subsystem_request, self, s);

  verbose("lsh: Requesting remote subsystem.\n");

  return format_channel_request(ATOM_SUBSYSTEM, channel,
				!!*c, "%S", self->subsystem);
}

struct command *
make_subsystem_request(struct lsh_string *subsystem)
{
  NEW(subsystem_request, req);

  req->super.format_request = do_format_subsystem_request;
  req->super.super.call = do_channel_request_command;
  req->subsystem = subsystem;

  return &req->super.super;
}


503
/* Handling of options and operations shared by the plain lsh client
504
 * and lshg. */
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

/* Forward declaration */

static struct ssh_channel *
make_client_session(struct client_options *options);

/* Block size for stdout and stderr buffers */
#define BLOCK_SIZE 32768

/* Window size for the session channel
 *
 * NOTE: Large windows seem to trig a bug in sshd2. */
#define WINDOW_SIZE 10000

#define ARG_NOT 0x400

#define OPT_STDIN 0x210
#define OPT_STDOUT 0x211
#define OPT_STDERR 0x212

#define OPT_FORK_STDIO 0x213

527
#define OPT_SUBSYSTEM 0x214
528
#define OPT_DETACH 0x215
529

530
531
void
init_client_options(struct client_options *self,
532
		    struct randomness *random,
533
534
535
		    struct exception_handler *handler,
		    int *exit_code)			 
{
536
537
  self->random = random;
  
538
  self->tty = make_unix_interact();
539
540
  self->escape = -1;
  
541
542
543
544
545
  self->handler = handler;

  self->exit_code = exit_code;
  
  self->not = 0;
546
  self->port = NULL;
547
548
  self->remote = NULL;

549
  self->local_user = self->user = getenv(ENV_LOGNAME);
550
551
552

  self->with_remote_peers = 0; 
  self->with_pty = -1;
553
554
  self->with_x11 = 0;
    
555
556
557
558
559
560
  self->stdin_file = NULL;
  self->stdout_file = NULL;
  self->stderr_file = NULL;

  self->stdin_fork = 0;
  self->stdout_fork = 0;
561
  self->stderr_fork = 1;
562
563
564

  self->used_stdin = 0;
  self->used_pty = 0;
565
566
  self->used_x11 = 0;
  
567
  self->detach_end = 0;
568
  self->start_shell = 1;
569
  self->remote_forward = 0;
570

571
572
  self->inhibit_actions = 0;

573
574
575
576
  object_queue_init(&self->actions);  
}

/* Host to connect to */
577
578
579
580
581
DEFINE_COMMAND(client_options2remote)
     (struct command *s UNUSED,
      struct lsh_object *a,
      struct command_continuation *c,
      struct exception_handler *e UNUSED)
582
583
584
585
{
  CAST_SUBTYPE(client_options, options, a);
  trace("client.c: client_options2remote\n");
  
586
  COMMAND_RETURN(c, options->remote);
587
588
589
}

/* Host to connect to */
590
591
592
593
594
DEFINE_COMMAND(client_options2actions)
     (struct command *s UNUSED,
      struct lsh_object *a,
      struct command_continuation *c,
      struct exception_handler *e UNUSED)
595
596
597
598
599
600
{
  CAST_SUBTYPE(client_options, options, a);

  trace("client.c: client_options2actions, %i actions\n",
	options->actions.length);
  
601
  COMMAND_RETURN(c, queue_to_list(&options->actions));
602
603
604
605
606
607
608
609
610
611
612
}

static const struct argp_option
client_options[] =
{
  /* Name, key, arg-name, flags, doc, group */
  { "port", 'p', "Port", 0, "Connect to this port.", 0 },
  { "user", 'l', "User name", 0, "Login as this user.", 0 },

  { NULL, 0, NULL, 0, "Actions:", CLIENT_ARGP_ACTION_GROUP },
  { "forward-local-port", 'L', "local-port:target-host:target-port", 0, "", 0 },
613
#if 0
614
615
616
617
  { "forward-remote-port", 'R', "remote-port:target-host:target-port", 0, "", 0 },
#endif
  { "nop", 'N', NULL, 0, "No operation (suppresses the default action, "
    "which is to spawn a remote shell)", 0 },
618
  { "background", 'B', NULL, 0, "Put process into the background. Implies -N.", 0 },
619
620
  { "execute", 'E', "command", 0, "Execute a command on the remote machine", 0 },
  { "shell", 'S', "command", 0, "Spawn a remote shell", 0 },
621
  { "subsystem", OPT_SUBSYSTEM, "subsystem-name", 0,
622
#if WITH_PTY_SUPPORT 
623
    "Connect to given subsystem. Implies --no-pty.",
624
#else
625
    "Connect to given subsystem.",
626
#endif
627
    0 },
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
  /* { "gateway", 'G', NULL, 0, "Setup a local gateway", 0 }, */
  { NULL, 0, NULL, 0, "Universal not:", 0 },
  { "no", 'n', NULL, 0, "Inverts the effect of the next modifier", 0 },

  { NULL, 0, NULL, 0, "Modifiers that apply to port forwarding:",
    CLIENT_ARGP_MODIFIER_GROUP - 10 },
  { "remote-peers", 'g', NULL, 0, "Allow remote access to forwarded ports", 0 },
  { "no-remote-peers", 'g' | ARG_NOT, NULL, 0, 
    "Disallow remote access to forwarded ports (default).", 0 },

  { NULL, 0, NULL, 0, "Modifiers that apply to remote execution:", 0 },
  { "stdin", OPT_STDIN, "Filename", 0, "Redirect stdin", 0},
  { "no-stdin", OPT_STDIN | ARG_NOT, NULL, 0, "Redirect stdin from /dev/null", 0}, 
  { "stdout", OPT_STDOUT, "Filename", 0, "Redirect stdout", 0},
  { "no-stdout", OPT_STDOUT | ARG_NOT, NULL, 0, "Redirect stdout to /dev/null", 0}, 
  { "stderr", OPT_STDERR, "Filename", 0, "Redirect stderr", 0},
  { "no-stderr", OPT_STDERR | ARG_NOT, NULL, 0, "Redirect stderr to /dev/null", 0}, 
645

646
  { "cvs-workaround", OPT_FORK_STDIO, "i?o?e?", OPTION_ARG_OPTIONAL,
647
648
649
650
651
652
    "Avoid setting one or more of the stdio file descriptors into "
    "non-blocking mode. If no argument is provided, the workaround is "
    "applied to all three file descriptors. By default, the workaround "
    "is applied to stderr only.", 0 },
  { "no-cvs-workaround", OPT_FORK_STDIO | ARG_NOT, NULL, 0,
    "Disable the cvs workaround.", 0 },
653
654
655
  { "detach", OPT_DETACH, NULL, 0, "Detach from terminal at session end.", 0},
  { "no-detach", OPT_DETACH | ARG_NOT, NULL, 0, "Do not detach session at end," 
    " wait for all open channels (default).", 0},
656

657
658
659
660
#if WITH_PTY_SUPPORT
  { "pty", 't', NULL, 0, "Request a remote pty (default).", 0 },
  { "no-pty", 't' | ARG_NOT, NULL, 0, "Don't request a remote pty.", 0 },
#endif /* WITH_PTY_SUPPORT */
661
662
  { "escape-char", 'e', "Character", 0, "Escape char. `none' means disable. "
    "Default is to use `~' if we have a tty, otherwise none.", 0 },
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
  { NULL, 0, NULL, 0, NULL, 0 }
};


/* GABA:
   (expr
     (name make_start_session)
     (params
       (open_session object command)
       (requests object object_list))
     (expr (lambda (connection)
       ((progn requests)
         ; Create a "session" channel
         (open_session connection)))))
*/
678

679
680
681
682
683
684
685
686
687
/* Requests a shell or command, and connects the channel to our stdio. */
/* GABA:
   (expr
     (name client_start_session)
     (params
       (request object command))
     (expr
       (lambda (session)
          (client_start_io (request session)))))
688
689
*/

690
691
692
693
694
695
696
static struct command *
make_client_start_session(struct command *request)
{
  CAST_SUBTYPE(command, r, client_start_session(request));
  return r;
}

697
698
static void
client_maybe_pty(struct client_options *options,
699
		 int default_pty,
700
		 struct object_queue *q)
701
702
{
#if WITH_PTY_SUPPORT
703
704
705
706
707
  int with_pty = options->with_pty;
  if (with_pty < 0)
    with_pty = default_pty;
  
  if (with_pty && !options->used_pty)
708
709
710
711
712
    {
      options->used_pty = 1;
      
      if (options->tty && INTERACT_IS_TTY(options->tty))
	{
713
714
715
716
717
718
719
720
721
722
723
	  struct command *get_pty = make_pty_request(options->tty);

	  if (get_pty)
	    object_queue_add_tail(q,
				  /* Ignore EXC_CHANNEL_REQUEST for the pty allocation call. */
				  &make_catch_apply
				  (make_catch_handler_info(EXC_ALL, EXC_CHANNEL_REQUEST,
							   0, NULL),
				   get_pty)->super);
	  else
	    werror("lsh: Can't use tty (probably getattr or atexit failed).\n");
724
725
	}
      else
726
727
728
729
730
731
732
733
734
735
736
	werror("lsh: No tty available.\n");
    }
#endif
}

static void
client_maybe_x11(struct client_options *options,
		 struct object_queue *q)
{
  if (options->with_x11)
    {
737
      char *display = getenv(ENV_DISPLAY);
738
739
      struct command *request = NULL;
      
740
      assert(options->random);
741
      if (display)
742
	request = make_forward_x11(display, options->random);
743
	  
744
745
746
747
      if (request)
	{
	  object_queue_add_tail(q, &request->super);
	  options->used_x11 = 1;
748
	}
749
750
      else
	werror("Can't find any local X11 display to forward.\n");
751
    }
752
}
753

754
755
756
757
758
/* Create an interactive session */
static struct command *
client_shell_session(struct client_options *options)
{
  struct ssh_channel *session = make_client_session(options);
759
  
760
761
762
  if (session)
    {
      struct object_queue session_requests;
763

764
      object_queue_init(&session_requests);
765

766
      client_maybe_pty(options, 1, &session_requests);
767
768
769
770
771
772
773
774
775
776
777
778
779
      client_maybe_x11(options, &session_requests);
  
      object_queue_add_tail(&session_requests,
			    &make_client_start_session(&request_shell.super)->super);
  
      {
	CAST_SUBTYPE(command, r,
		     make_start_session
		     (make_open_session_command(session),
		      queue_to_list_and_kill(&session_requests)));
	return r;
      }
    }
780
  else
781
    return NULL;
782
783
}

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/* Create a session for a subsystem */
static struct command *
client_subsystem_session(struct client_options *options,
		       struct lsh_string *subsystem)
{
  struct ssh_channel *session = make_client_session(options);
  
  if (session)
    {
      CAST_SUBTYPE(command, r,
		   make_start_session
		   (make_open_session_command(session),
		    make_object_list(1,
				     make_client_start_session
				     (make_subsystem_request(subsystem)),
				     -1)));
      return r;
    }
  
  return NULL;
}

806
807
808
809
/* Create a session executing a command line */
static struct command *
client_command_session(struct client_options *options,
		       struct lsh_string *command)
810
{
811
  struct ssh_channel *session = make_client_session(options);
812
  
813
814
  if (session)
    {
815
816
817
818
      struct object_queue session_requests;
    
      object_queue_init(&session_requests);
  
819
820
      /* NOTE: Doesn't ask for a pty by default. That's traditional
       * behaviour, although perhaps not the Right Thing. */
821
      
822
      client_maybe_pty(options, 0, &session_requests);
823
824
825
826
827
828
829
830
831
832
833
      client_maybe_x11(options, &session_requests);

      object_queue_add_tail(&session_requests,
			    &make_client_start_session(make_exec_request(command))->super);
      {
	CAST_SUBTYPE(command, r,
		     make_start_session
		     (make_open_session_command(session),
		      queue_to_list_and_kill(&session_requests)));
	return r;
      }
834
835
    }
  
836
837
838
  return NULL;
}

839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
struct command *
client_add_action(struct client_options *options,
		  struct command *action)
{
  if (action)
    object_queue_add_tail(&options->actions, &action->super);

  return action;
}

/* NOTE: Some of the original quoting is lost here. */
static struct lsh_string *
rebuild_command_line(unsigned argc, char **argv)
{
  unsigned length;
  unsigned i;
  unsigned pos;
  struct lsh_string *r;
  unsigned *alengths = alloca(sizeof(unsigned) * argc);
  
  assert (argc);
  length = argc - 1; /* Number of separating spaces. */

  for (i = 0; i<argc; i++)
    {
      alengths[i] = strlen(argv[i]);
      length += alengths[i];
    }

  r = lsh_string_alloc(length);
  memcpy(r->data, argv[0], alengths[0]);
  pos = alengths[0];
  for (i = 1; i<argc; i++)
    {
      r->data[pos++] = ' ';
      memcpy(r->data + pos, argv[i], alengths[i]);
      pos += alengths[i];
    }

  assert(pos == r->length);

  return r;
}

static int
fork_input(int in)
{
  /* pipe[0] for reading, pipe[1] for writing. */
  int pipe[2];

  if (!lsh_make_pipe(pipe))
    return -1;

  switch (fork())
    {
    case -1:
      /* Error */
      return -1;
    case 0:
      close(pipe[0]);
      if (lsh_copy_file(in, pipe[1]))
	_exit(EXIT_SUCCESS);
      else
	_exit(EXIT_FAILURE);
    default:
      /* Parent */
      close(pipe[1]);
      return pipe[0];
    }
}

static int
fork_output(int out)
{
  /* pipe[0] for reading, pipe[1] for writing. */
  int pipe[2];

  if (!lsh_make_pipe(pipe))
    return -1;

  switch (fork())
    {
    case -1:
      /* Error */
      return -1;
    case 0:
      close(pipe[1]);
      if (lsh_copy_file(pipe[0], out))
	_exit(EXIT_SUCCESS);
      else
	_exit(EXIT_FAILURE);
    default:
      /* Parent */
      close(pipe[0]);
      return pipe[1];
    }
}

937
938
939
940
941
942
/* FIXME: Use const? */

#define DEFINE_CALLBACK(name) \
static void do_##name(struct lsh_callback *self); \
static struct lsh_callback \
name = { STATIC_HEADER, do_##name }; \
943
static void do_##name(struct lsh_callback *self UNUSED)
944

945
/* A callback that exits the process immediately. */
946
DEFINE_CALLBACK(exit_callback)
947
948
949
950
{
  exit(EXIT_SUCCESS);
}

951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
DEFINE_CALLBACK(verbose_callback)
{
  verbose_flag = !verbose_flag;
  if (verbose_flag)
    verbose("Enabling verbose messages\n");
}

DEFINE_CALLBACK(debug_callback)
{
  debug_flag = !debug_flag;
  if (debug_flag)
    debug("Enabling debug messages\n");
}

DEFINE_CALLBACK(quiet_callback)
{
  quiet_flag = !quiet_flag;
  if (!quiet_flag)
    werror("Enabling warning messages\n");
}
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987

DEFINE_COMMAND(background_process)
     (struct command *s UNUSED,
      struct lsh_object *a,
      struct command_continuation *c,
      struct exception_handler *e UNUSED)
{
  switch (fork())
    {
    case 0:
      /* Child */
      /* FIXME: Should we create a new process group, close our tty
       * and stdio, etc? */
      COMMAND_RETURN(c, a);
      break;
    case -1:
      /* Error */
988
      werror("background_process: fork failed %e\n", errno);
989
990
991
992
993
994
995
      COMMAND_RETURN(c, a);
      break;
    default:
      /* Parent */
      _exit(EXIT_SUCCESS);
    }
}
996

997
998
999
1000
/* Create a session object. stdout and stderr are shared (although
 * with independent lsh_fd objects). stdin can be used by only one
 * session (until something "session-control"/"job-control" is added).
 * */
For faster browsing, not all history is shown. View entire blame