server_session.c 22.1 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
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
/* server_session.c
 *
 * $Id$
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "server_session.h"

27
#include "channel_commands.h"
Niels Möller's avatar
Niels Möller committed
28
#include "format.h"
29
#include "read_data.h"
30
#include "reaper.h"
Niels Möller's avatar
Niels Möller committed
31
32
33
34
35
36
37
38
39
40
41
#include "server_pty.h"
#include "ssh.h"
#include "tcpforward.h"
#include "translate_signal.h"
#include "tty.h"
#include "werror.h"
#include "xalloc.h"

#include <errno.h>

/* For debug */
42

Niels Möller's avatar
Niels Möller committed
43
44
45
46
47
48
49
50
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/socket.h>

51
#include <signal.h>
Niels Möller's avatar
Niels Möller committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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

/* Socket workround */
#ifndef SHUTDOWN_WORKS_WITH_UNIX_SOCKETS

/* There's an how++ missing in the af_unix shutdown implementation of
 * some linux versions. Try an ugly workaround. */
#ifdef linux

/* From src/linux/include/net/sock.h */
#define RCV_SHUTDOWN	1
#define SEND_SHUTDOWN	2

#undef SHUT_RD
#undef SHUT_WR
#undef SHUT_RD_WR

#define SHUT_RD RCV_SHUTDOWN
#define SHUT_WR SEND_SHUTDOWN
#define SHUT_RD_WR (RCV_SHUTDOWN | SEND_SHUTDOWN)

#else /* !linux */

/* Don't know how to work around the broken shutdown(). So disable it
 * completely. */

#define SHUTDOWN(fd, how) 0

#endif /* !linux */
#endif /* !SHUTDOWN_WORKS_WITH_UNIX_SOCKETS */

#ifndef SHUTDOWN
#define SHUTDOWN(fd, how) (shutdown((fd), (how)))
#endif

#ifndef SHUT_RD
#define SHUT_RD 0
#endif

#ifndef SHUT_WR
#define SHUT_WR 1
#endif

#ifndef SHUT_RD_WR
#define SHUT_RD_WR 2
#endif

#ifndef UT_NAMESIZE
#  define UT_NAMESIZE 8		/* FIXME: sane default value */
#endif

#include "server_session.c.x"


/* Session */
/* GABA:
   (class
     (name server_session)
     (super ssh_channel)
     (vars
       ; User information
112
       ;; (user object lsh_user)
Niels Möller's avatar
Niels Möller committed
113

114
       (initial_window . UINT32)
Niels Möller's avatar
Niels Möller committed
115
116
117
118
119
120
121
122
123
124

       ; Resource to kill when the channel is closed. 
       (process object resource)

       ; pty
       (pty object pty_info)
       ; value of the TERM environment variable
       (term string)

       ; Child process's stdio 
125
126
       (in object lsh_fd)
       (out object lsh_fd)
127
128
       ;; err may be NULL, if there's no separate stderr channel.
       ;; This happens if we use a pty, and the bash workaround
129
       (err object lsh_fd)))
Niels Möller's avatar
Niels Möller committed
130
131
132
*/

/* Receive channel data */
Niels Möller's avatar
Niels Möller committed
133
134
135
static void
do_receive(struct ssh_channel *c,
	   int type, struct lsh_string *data)
Niels Möller's avatar
Niels Möller committed
136
137
138
139
140
141
{
  CAST(server_session, closure, c);

  switch(type)
    {
    case CHANNEL_DATA:
142
      A_WRITE(&closure->in->write_buffer->super, data);
Niels Möller's avatar
Niels Möller committed
143
      break;
Niels Möller's avatar
Niels Möller committed
144
145
146
    case CHANNEL_STDERR_DATA:
      werror("Ignoring unexpected stderr data.\n");
      lsh_string_free(data);
Niels Möller's avatar
Niels Möller committed
147
      break;
Niels Möller's avatar
Niels Möller committed
148
149
150
151
152
153
    default:
      fatal("Internal error!\n");
    }
}

/* We may send more data */
Niels Möller's avatar
Niels Möller committed
154
static void
155
156
do_send_adjust(struct ssh_channel *s,
	       UINT32 i UNUSED)
Niels Möller's avatar
Niels Möller committed
157
{
158
  CAST(server_session, session, s);
Niels Möller's avatar
Niels Möller committed
159

160
161
162
163
164
  /* FIXME: Perhaps it's better to just check the read pointers, and
   * not bother with the alive-flags? */
  if (session->out->super.alive)
    {
      assert(session->out->read);
165

166
167
168
169
      session->out->want_read = 1;
    }
  
  if (session->err && session->err->super.alive)
170
    {
171
      assert(session->err->read);
Niels Möller's avatar
Niels Möller committed
172
  
173
      session->err->want_read = 1;
174
    }
Niels Möller's avatar
Niels Möller committed
175
176
}

Niels Möller's avatar
Niels Möller committed
177
178
static void
do_eof(struct ssh_channel *channel)
Niels Möller's avatar
Niels Möller committed
179
180
181
{
  CAST(server_session, session, channel);

Niels Möller's avatar
Niels Möller committed
182
183
  trace("server_session.c: do_eof()\n");

Niels Möller's avatar
Niels Möller committed
184
  write_buffer_close(session->in->write_buffer);
Niels Möller's avatar
Niels Möller committed
185
186
187

  if ( (channel->flags & CHANNEL_SENT_EOF)
       && (channel->flags & CHANNEL_CLOSE_AT_EOF))
Niels Möller's avatar
Niels Möller committed
188
    channel_close(channel);
Niels Möller's avatar
Niels Möller committed
189
190
}

191
struct ssh_channel *
192
make_server_session(UINT32 initial_window,
193
		    struct alist *request_types)
Niels Möller's avatar
Niels Möller committed
194
195
196
197
198
{
  NEW(server_session, self);

  init_channel(&self->super);

199
200
  self->initial_window = initial_window;

Niels Möller's avatar
Niels Möller committed
201
202
203
204
205
  /* We don't want to receive any data before we have forked some
   * process to receive it. */
  self->super.rec_window_size = 0;

  /* FIXME: Make maximum packet size configurable. */
206
  self->super.rec_max_packet = SSH_MAX_PACKET - SSH_CHANNEL_MAX_PACKET_FUZZ;
Niels Möller's avatar
Niels Möller committed
207
208
  self->super.request_types = request_types;

209
210
  /* Note: We don't need a close handler; the channels resource list
   * is taken care of automatically. */
Niels Möller's avatar
Niels Möller committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  
  self->process = NULL;
  
  self->in = NULL;
  self->out = NULL;
  self->err = NULL;
  
  return &self->super;
}


/* GABA:
   (class
     (name open_session)
     (super channel_open)
     (vars
       (session_requests object alist)))
*/

230
#define WINDOW_SIZE 10000
Niels Möller's avatar
Niels Möller committed
231

Niels Möller's avatar
Niels Möller committed
232
233
234
static void
do_open_session(struct channel_open *s,
		struct ssh_connection *connection UNUSED,
235
		UINT32 type UNUSED,
236
237
		UINT32 send_window_size UNUSED,
		UINT32 send_max_packet UNUSED,
Niels Möller's avatar
Niels Möller committed
238
239
240
		struct simple_buffer *args,
		struct command_continuation *c,
		struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
241
{
242
  CAST(open_session, self, s);
Niels Möller's avatar
Niels Möller committed
243
244

  debug("server.c: do_open_session()\n");
245
246

  assert(connection->user);
247
  
Niels Möller's avatar
Niels Möller committed
248
249
  if (parse_eod(args))
    {
Niels Möller's avatar
Niels Möller committed
250
      COMMAND_RETURN(c,
251
		     make_server_session(WINDOW_SIZE, self->session_requests));
Niels Möller's avatar
Niels Möller committed
252
253
254
    }
  else
    {
255
      PROTOCOL_ERROR(e, "trailing garbage in open message");
Niels Möller's avatar
Niels Möller committed
256
257
258
    }
}

259
struct channel_open *
260
make_open_session(struct alist *session_requests)
Niels Möller's avatar
Niels Möller committed
261
262
263
264
265
266
267
268
269
270
{
  NEW(open_session, closure);

  closure->super.handler = do_open_session;
  closure->session_requests = session_requests;
  
  return &closure->super;
}


271
272
273
struct lsh_string *
format_exit_signal(struct ssh_channel *channel,
		   int core, int signal)
Niels Möller's avatar
Niels Möller committed
274
275
{
  struct lsh_string *msg = ssh_format("Process killed by %lz.\n",
276
				      STRSIGNAL(signal));
Niels Möller's avatar
Niels Möller committed
277
278
279
280
281
282
283
284
285
286
  
  return format_channel_request(ATOM_EXIT_SIGNAL,
				channel,
				0,
				"%i%c%fS%z",
				signal_local_to_network(signal),
				core,
				msg, "");
}

287
288
struct lsh_string *
format_exit(struct ssh_channel *channel, int value)
Niels Möller's avatar
Niels Möller committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
{
  return format_channel_request(ATOM_EXIT_STATUS,
				channel,
				0,
				"%i", value);
}

/* GABA:
   (class
     (name exit_shell)
     (super exit_callback)
     (vars
       (session object server_session)))
*/

304
305
306
static void
do_exit_shell(struct exit_callback *c, int signaled,
	      int core, int value)
Niels Möller's avatar
Niels Möller committed
307
308
309
310
311
{
  CAST(exit_shell, closure, c);
  struct server_session *session = closure->session;
  struct ssh_channel *channel = &session->super;
  
Niels Möller's avatar
Niels Möller committed
312
  trace("server_session.c: do_exit_shell()\n");
Niels Möller's avatar
Niels Möller committed
313
  
314
315
316
317
  /* NOTE: We don't close the child's stdio here. The io-backend
   * should notice EOF anyway, and the client should send EOF when it
   * receives news of the process's death, unless it really wants to
   * talk to any live grand children processes. */
Niels Möller's avatar
Niels Möller committed
318
319
320
321
322
323

  /* We close when we have both sent and received eof. */
  channel->flags |= CHANNEL_CLOSE_AT_EOF;
  
  if (!(channel->flags & CHANNEL_SENT_CLOSE))
    {
Niels Möller's avatar
Niels Möller committed
324
325
326
      A_WRITE(channel->write,
	      (signaled
	       ? format_exit_signal(channel, core, value)
327
	       : format_exit(channel, value)) );
Niels Möller's avatar
Niels Möller committed
328
329
330

      if ( (channel->flags & CHANNEL_SENT_EOF)
	   && (channel->flags & CHANNEL_RECEIVED_EOF))
Niels Möller's avatar
Niels Möller committed
331
332
	{
	  /* We have sent EOF already, so initiate close */
Niels Möller's avatar
Niels Möller committed
333
	  channel_close(channel);
Niels Möller's avatar
Niels Möller committed
334
335
336
337
	}
    }
}

338
339
static struct exit_callback *
make_exit_shell(struct server_session *session)
Niels Möller's avatar
Niels Möller committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
  NEW(exit_shell, self);

  self->super.exit = do_exit_shell;
  self->session = session;

  return &self->super;
}

/* GABA:
   (class
     (name shell_request)
     (super channel_request)
     (vars
       (backend object io_backend)
355
       (reaper object reap)))
Niels Möller's avatar
Niels Möller committed
356
357
358
359
360
*/

/* Creates a one-way socket connection. Returns 1 on success, 0 on
 * failure. fds[0] is for reading, fds[1] for writing (like for the
 * pipe() system call). */
361
362
static int
make_pipe(int *fds)
Niels Möller's avatar
Niels Möller committed
363
364
365
{
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
    {
366
      werror("socketpair() failed: %z\n", STRERROR(errno));
Niels Möller's avatar
Niels Möller committed
367
368
369
370
371
372
      return 0;
    }
  debug("Created socket pair. Using fd:s %i <-- %i\n", fds[0], fds[1]);

  if (SHUTDOWN(fds[0], SHUT_WR) < 0)
    {
373
      werror("shutdown(%i, SEND) failed: %z\n", fds[0], STRERROR(errno));
Niels Möller's avatar
Niels Möller committed
374
375
376
377
      goto fail;
    }
  if (SHUTDOWN(fds[1], SHUT_RD) < 0)
    {
378
      werror("shutdown(%i, REC) failed: %z\n", fds[0], STRERROR(errno));
Niels Möller's avatar
Niels Möller committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    fail:
      {
	int saved_errno = errno;

	close(fds[0]);
	close(fds[1]);

	errno = saved_errno;
	return 0;
      }
    }
  
  return 1;
}

394
395
static int
make_pipes(int *in, int *out, int *err)
Niels Möller's avatar
Niels Möller committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
{
  int saved_errno;
  
  if (make_pipe(in))
    {
      if (make_pipe(out))
	{
	  if (make_pipe(err))
	    {
              return 1;
            }
	  saved_errno = errno;
          close(out[0]);
          close(out[1]);
        }
      else
	saved_errno = errno;
      close(in[0]);
      close(in[1]);
    }
  else
    saved_errno = errno;
  
  errno = saved_errno;
  return 0;
}

423
424
#define BASH_WORKAROUND 1

Niels Möller's avatar
Niels Möller committed
425
426
427
428
429
430
431
432
433
434
435
436
437
#if WITH_PTY_SUPPORT
static int make_pty(struct pty_info *pty, int *in, int *out, int *err)
{
  int saved_errno = 0;

  debug("make_pty... ");
  if (pty)
    debug("exists: \n"
	  "  alive = %i\n"
	  "  master = %i\n"
	  "  slave = %i\n"
	  "... ",
	  pty->super.alive, pty->master, pty->slave);
438
439
  debug("\n");
  
Niels Möller's avatar
Niels Möller committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  if (pty && pty->super.alive)
    {
      debug("make_pty: Using allocated pty.\n");
      in[0] = pty->slave;
      in[1] = pty->master;

      /* Ownership of the fd:s passes on to some file objects. */
      pty->super.alive = 0;

      /* FIXME: It seems unnecessary to dup the fd:s here. But perhaps
       * having equal in and out fds may confuse the cleanup code, so
       * we leave it for now. */
      if ((out[0] = dup(pty->master)) != -1)
        {
          if ((out[1] = dup(pty->slave)) != -1) 
            {
456
457
458
459
460
461
462
463
464
#if BASH_WORKAROUND
	      /* Don't use a separate stderr channel; just dup the
	       * stdout pty to stderr. */
	      if ((err[1] = dup(pty->slave)) != -1)
                {
                  err[0] = -1;
                  return 1;
                } 
#else /* !BASH_WORKAROUND */
Niels Möller's avatar
Niels Möller committed
465
466
467
468
469
	      if (make_pipe(err))
		{
		  /* Success! */
		  return 1;
		}
470
#endif /* !BASH_WORKAROUND */
Niels Möller's avatar
Niels Möller committed
471
              saved_errno = errno;
472
	      
Niels Möller's avatar
Niels Möller committed
473
474
475
476
477
478
479
480
481
482
483
            }
	  else
	    saved_errno = errno;
	  close(out[0]);
	}
      else 
	saved_errno = errno;
      close(in[0]);
      close(in[1]);

      werror("make_pty: duping pty filedescriptors failed (errno = %i): %z\n",
484
	     errno, STRERROR(errno));
Niels Möller's avatar
Niels Möller committed
485
486
487
488
489
490
491
492
493
494
    }
  errno = saved_errno;
  return 0;
}
#else /* !WITH_PTY_SUPPORT */
static int make_pty(struct pty_info *pty UNUSED,
		    int *in UNUSED, int *out UNUSED, int *err UNUSED)
{ return 0; }
#endif /* !WITH_PTY_SUPPORT */

495
496
497
/* Returns -1 on failure, 0 for child and +1 for parent */
static int
spawn_process(struct server_session *session,
498
	      struct lsh_user *user,
499
	      struct address_info *peer,
500
	      struct io_backend *backend,
501
	      struct reap *reaper)
502
503
504
505
506
{
  int in[2];
  int out[2];
  int err[2];

507
  int using_pty = 0;
508
509
510
  
  if (session->process)
    /* Already spawned a shell or command */
511
    return -1;
512
513
514
515
516
  
  /* {in|out|err}[0] is for reading,
   * {in|out|err}[1] for writing. */

  if (make_pty(session->pty, in, out, err))
517
    using_pty = 1;
518

519
  else if (!make_pipes(in, out, err))
520
    return -1;
521

522
  {
523
    struct resource *child;
524
    
525
    if (USER_FORK(user, &child,
526
527
		  reaper, make_exit_shell(session),
		  peer, using_pty ? session->pty->tty_name : NULL))
528
529
530
      {
	if (child)
	  { /* Parent */
531
	    struct ssh_channel *channel = &session->super;
Niels Möller's avatar
Niels Möller committed
532
533
	    trace("spawn_process: Parent process\n");
	    
534
	    session->process = child;
535

536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
	    /* Close the child's fd:s */
	    close(in[0]);
	    close(out[1]);
	    close(err[1]);

	    {
	      /* Exception handlers */
	      struct exception_handler *io_exception_handler
		= make_channel_io_exception_handler(channel,
						    "lshd: Child stdio: ",
						    &default_exception_handler,
						    HANDLER_CONTEXT);

	      /* Close callback for stderr and stdout */
	      struct close_callback *read_close_callback
		= make_channel_read_close_callback(channel);

	      session->in
554
		= io_write(make_lsh_fd(backend, in[1],
555
556
557
558
559
560
561
562
563
564
				       io_exception_handler),
			   SSH_MAX_PACKET, NULL);
	  
	      /* Flow control */
	      session->in->write_buffer->report = &session->super.super;

	      /* FIXME: Should we really use the same exception handler,
	       * which will close the channel on read errors, or is it
	       * better to just send EOF on read errors? */
	      session->out
565
		= io_read(make_lsh_fd(backend, out[0], io_exception_handler),
566
567
568
569
			  make_channel_read_data(channel),
			  read_close_callback);
	      session->err 
		= ( (err[0] != -1)
570
		    ? io_read(make_lsh_fd(backend, err[0], io_exception_handler),
571
572
573
574
575
576
			      make_channel_read_stderr(channel),
			      read_close_callback)
		    : NULL);
	    }
	
	    channel->receive = do_receive;
577
	    channel->send_adjust = do_send_adjust;
578
579
580
581
582
	    channel->eof = do_eof;
	  
	    /* Make sure that the process and it's stdio is
	     * cleaned up if the channel or connection dies. */
	    REMEMBER_RESOURCE
583
	      (channel->resources, child);
584

585
586
587
588
589
590
591
592
593
594
	    /* FIXME: How to do this properly if in and out may use the
	     * same fd? */
	    REMEMBER_RESOURCE
	      (channel->resources, &session->in->super);
	    REMEMBER_RESOURCE
	      (channel->resources, &session->out->super);
	    if (session->err)
	      REMEMBER_RESOURCE
		(channel->resources, &session->err->super);

595
	    return 1;
596
597
598
	  }
	else
	  { /* Child */
Niels Möller's avatar
Niels Möller committed
599
	    trace("spawn_process: Child process\n");
600
	    assert(getuid() == user->uid);
Niels Möller's avatar
Niels Möller committed
601
602
603
604
605
606
607
608
609
610

#if 0
	    /* Debug timing problems */
	    if (sleep(5))
	      {
		trace("server_session.c: sleep interrupted\n");

		sleep(5);
	      }
#endif    
611
	    if (!USER_CHDIR_HOME(user))
612
613
614
615
616
617
	      {
		werror("Could not change to home (or root) directory!\n");
		_exit(EXIT_FAILURE);
	      }
	    
#if WITH_PTY_SUPPORT
618
	    if (using_pty)
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
	      {
		debug("lshd: server.c: Setting controlling tty...\n");
		if (!tty_setctty(session->pty))
		  {
		    debug("lshd: server.c: "
			  "Setting controlling tty... Failed!\n");
		    werror("lshd: Can't set controlling tty for child!\n");
		    _exit(EXIT_FAILURE);
		  }
		else
		  debug("lshd: server.c: Setting controlling tty... Ok.\n");
	      }
#endif /* WITH_PTY_SUPPORT */
	  
	    /* Close all descriptors but those used for
	     * communicationg with parent. We rely on the
	     * close-on-exec flag for all fd:s handled by the
	     * backend. */
	    
	    if (dup2(in[0], STDIN_FILENO) < 0)
	      {
		werror("Can't dup stdin!\n");
		_exit(EXIT_FAILURE);
	      }
	    close(in[0]);
	    close(in[1]);
	    
	    if (dup2(out[1], STDOUT_FILENO) < 0)
	      {
		werror("Can't dup stdout!\n");
		_exit(EXIT_FAILURE);
	      }
	    close(out[0]);
	    close(out[1]);

	    if (!dup_error_stream())
	      {
		werror("server_session: Failed to dup old stderr. Bye.\n");
		set_error_ignore();
	      }

	    if (dup2(err[1], STDERR_FILENO) < 0)
	      {
		werror("Can't dup stderr!\n");
		_exit(EXIT_FAILURE);
	      }
	    close(err[0]);
	    close(err[1]);

668
	    return 0;
669
670
671
672
673
674
675
676
677
678
679
680
	  }
      }
    /* fork() failed */
    /* Close and return channel_failure */

    close(err[0]);
    close(err[1]);
    close(out[0]);
    close(out[1]);
    close(in[0]);
    close(in[1]);
  }
681
  return -1;
682
683
}

684
685
686
static struct exception shell_request_failed =
STATIC_EXCEPTION(EXC_CHANNEL_REQUEST, "Shell request failed");

Niels Möller's avatar
Niels Möller committed
687
688
689
static void
do_spawn_shell(struct channel_request *c,
	       struct ssh_channel *channel,
690
	       struct ssh_connection *connection,
691
692
693
694
695
	       UINT32 type UNUSED,
	       int want_reply UNUSED,
	       struct simple_buffer *args,
	       struct command_continuation *s,
	       struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
696
697
{
  CAST(shell_request, closure, c);
698
  CAST(server_session, session, channel);
Niels Möller's avatar
Niels Möller committed
699
700

  if (!parse_eod(args))
Niels Möller's avatar
Niels Möller committed
701
    {
702
      PROTOCOL_ERROR(e, "Invalid shell CHANNEL_REQUEST message.");
Niels Möller's avatar
Niels Möller committed
703
704
705
      return;
    }
    
Niels Möller's avatar
Niels Möller committed
706
707
708
709
  if (session->process)
    /* Already spawned a shell or command */
    goto fail;

710
711
  switch (spawn_process(session, connection->user,
			connection->peer,
712
			closure->backend, closure->reaper))
713
714
715
716
    {
    case 1: /* Parent */
      /* NOTE: The return value is not used. */
      COMMAND_RETURN(s, channel);
717
      channel_start_receive(channel, session->initial_window);
718
719
720
721
722
723
724
725
726
727
728
      return;
    case 0:
      { /* Child */
#define MAX_ENV 1
	/* No args, end the USER_EXEC method fills in argv[0]. */
	char *argv[] = { NULL, NULL };
	
	struct env_value env[MAX_ENV];
	int env_length = 0;
	
	debug("do_spawn_shell: Child process\n");
729
	assert(getuid() == connection->user->uid);
730
731
732
733
734
735
736
737
738
	    	    
	if (session->term)
	  {
	    env[env_length].name ="TERM";
	    env[env_length].value = session->term;
	    env_length++;
	  }
	assert(env_length <= MAX_ENV);
#undef MAX_ENV
Niels Möller's avatar
Niels Möller committed
739

740
#if 1
741
	USER_EXEC(connection->user, 1, argv, env_length, env);
742
743
744
745
746
	
	/* exec failed! */
	verbose("server_session: exec() failed (errno = %i): %z\n",
		errno, STRERROR(errno));
	_exit(EXIT_FAILURE);
Niels Möller's avatar
Niels Möller committed
747

748
749
750
751
752
753
754
755
756
757
758
759
760
#else
# define GREETING "Hello world!\n"
	if (write(STDOUT_FILENO, GREETING, strlen(GREETING)) < 0)
	  _exit(errno);
	kill(getuid(), SIGSTOP);
	if (write(STDOUT_FILENO, shell, strlen(shell)) < 0)
	  _exit(125);
	_exit(126);
# undef GREETING
#endif
      }
    case -1:
      /* fork() failed */
Niels Möller's avatar
Niels Möller committed
761

762
763
764
765
766
767
768
      break;
    default:
      fatal("Internal error!");
  }
 fail:
  EXCEPTION_RAISE(e, &shell_request_failed);
}
Niels Möller's avatar
Niels Möller committed
769

770
771
struct channel_request *
make_shell_handler(struct io_backend *backend,
772
		   struct reap *reaper)
773
774
{
  NEW(shell_request, closure);
Niels Möller's avatar
Niels Möller committed
775

776
777
  closure->super.handler = do_spawn_shell;
  closure->backend = backend;
778
  closure->reaper = reaper;
779
780
781
  
  return &closure->super;
}
Niels Möller's avatar
Niels Möller committed
782

783
784
static struct exception exec_request_failed =
STATIC_EXCEPTION(EXC_CHANNEL_REQUEST, "Exec request failed");
785

786
787
788
static void
do_spawn_exec(struct channel_request *c,
	      struct ssh_channel *channel,
789
	      struct ssh_connection *connection,
790
791
792
793
794
795
796
797
	      UINT32 type UNUSED,
	      int want_reply UNUSED,
	      struct simple_buffer *args,
	      struct command_continuation *s,
	      struct exception_handler *e)
{
  CAST(shell_request, closure, c);
  CAST(server_session, session, channel);
Niels Möller's avatar
Niels Möller committed
798

799
  UINT32 command_len;
Niels Möller's avatar
Niels Möller committed
800
  const UINT8 *command;
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818

  struct lsh_string *command_line;
  
  if (!(parse_string(args, &command_len, &command)
	&& parse_eod(args)))
    {
      PROTOCOL_ERROR(e, "Invalid exec CHANNEL_REQUEST message.");
      return;
    }
    
  if (session->process)
    /* Already spawned a shell or command */
    goto fail;

  command_line = make_cstring_l(command_len, command);
  if (!command_line)
    EXCEPTION_RAISE(e, &exec_request_failed);
  else
819
820
    switch (spawn_process(session, connection->user,
			  connection->peer,
821
			  closure->backend, closure->reaper))
822
823
824
825
826
827
    {
    case 1: /* Parent */
      lsh_string_free(command_line);

      /* NOTE: The return value is not used. */
      COMMAND_RETURN(s, channel);
828
      channel_start_receive(channel, session->initial_window);
829
830
831
832
833
      return;
    case 0:
      { /* Child */
#define MAX_ENV 1
	/* No args, end the USER_EXEC method fills in argv[0]. */
834
835
836

#if 0
	/* NOTE: HPUX compiler can't handle array initialization. */
837
	char *argv[] = { NULL, "-c", command_line->data, NULL };
838
839
840
841
842
843
844
#else
	char *argv[4];
	argv[0] = NULL;
	argv[1] = "-c";
	argv[2] = command_line->data;
	argv[3] = NULL;
#endif
845
846
847
848
	struct env_value env[MAX_ENV];
	int env_length = 0;
	
	debug("do_spawn_shell: Child process\n");
849
	assert(getuid() == connection->user->uid);
850
851
852
853
854
855
	    	    
	if (session->term)
	  {
	    env[env_length].name ="TERM";
	    env[env_length].value = session->term;
	    env_length++;
Niels Möller's avatar
Niels Möller committed
856
	  }
857
	assert(env_length <= MAX_ENV);
Niels Möller's avatar
Niels Möller committed
858
859
#undef MAX_ENV

860
	USER_EXEC(connection->user, 0, argv, env_length, env);
861
	
862
863
864
865
	/* exec failed! */
	verbose("server_session: exec() failed (errno = %i): %z\n",
		errno, STRERROR(errno));
	_exit(EXIT_FAILURE);
Niels Möller's avatar
Niels Möller committed
866
      }
867
868
869
870
871
872
873
    case -1:
      /* fork() failed */
      lsh_string_free(command_line);

      break;
    default:
      fatal("Internal error!");
Niels Möller's avatar
Niels Möller committed
874
875
  }
 fail:
876
  EXCEPTION_RAISE(e, &shell_request_failed);
Niels Möller's avatar
Niels Möller committed
877
878
}

879
struct channel_request *
880
make_exec_handler(struct io_backend *backend,
881
		  struct reap *reaper)
Niels Möller's avatar
Niels Möller committed
882
883
884
{
  NEW(shell_request, closure);

885
  closure->super.handler = do_spawn_exec;
Niels Möller's avatar
Niels Möller committed
886
  closure->backend = backend;
887
  closure->reaper = reaper;
Niels Möller's avatar
Niels Möller committed
888
889
890
891
892
  
  return &closure->super;
}

#if WITH_PTY_SUPPORT
893
894
895
896

static struct exception pty_request_failed =
STATIC_EXCEPTION(EXC_CHANNEL_REQUEST, "pty request failed");

Niels Möller's avatar
Niels Möller committed
897
/* pty_handler */
Niels Möller's avatar
Niels Möller committed
898
899
900
static void
do_alloc_pty(struct channel_request *c UNUSED,
	     struct ssh_channel *channel,
901
	     struct ssh_connection *connection,
902
903
904
905
906
	     UINT32 type UNUSED,
	     int want_reply UNUSED,
	     struct simple_buffer *args,
	     struct command_continuation *s,
	     struct exception_handler *e)
Niels Möller's avatar
Niels Möller committed
907
908
{
  UINT32 width, height, width_p, height_p;
Niels Möller's avatar
Niels Möller committed
909
  const UINT8 *mode;
Niels Möller's avatar
Niels Möller committed
910
911
912
  UINT32 mode_length;
  struct lsh_string *term = NULL;

913
  CAST(server_session, session, channel);
Niels Möller's avatar
Niels Möller committed
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928

  verbose("Client requesting a tty...\n");
  
  /* The client may only request a tty once. */
  if (!session->pty &&
      (term = parse_string_copy(args)) &&
      parse_uint32(args, &width) &&
      parse_uint32(args, &height) &&
      parse_uint32(args, &width_p) &&
      parse_uint32(args, &height_p) &&
      parse_string(args, &mode_length, &mode) &&
      parse_eod(args))
    {
      struct pty_info *pty = make_pty_info();

929
      if (pty_allocate(pty, connection->user->uid))
Niels Möller's avatar
Niels Möller committed
930
931
932
933
934
935
936
937
938
939
940
941
        {
          struct termios ios;

          if (tty_getattr(pty->slave, &ios))
            {
	      pty->super.alive = 1;
              session->pty = pty;

	      /* Don't set TERM if the value is empty. */
	      if (!term->length)
		{
		  lsh_string_free(term);
942
		  term = NULL;
Niels Möller's avatar
Niels Möller committed
943
944
945
946
947
948
949
950
951
952
		}
	      
              session->term = term;
              tty_decode_term_mode(&ios, mode_length, mode); 
	      
	      /* cfmakeraw(&ios); */
              if (tty_setattr(pty->slave, &ios) &&
                  tty_setwinsize(pty->slave,
				 width, height, width_p, height_p))
		{
953
		  REMEMBER_RESOURCE(channel->resources, &pty->super);
Niels Möller's avatar
Niels Möller committed
954
955

		  verbose(" granted.\n");
956
957
		  COMMAND_RETURN(s, NULL);

Niels Möller's avatar
Niels Möller committed
958
		  return;
Niels Möller's avatar
Niels Möller committed
959
960
961
962
963
964
965
966
967
		}
	      else
		/* Close fd:s and mark the pty-struct as dead */
		KILL_RESOURCE(&pty->super);
            }
        }
      KILL(pty);
    }

Niels Möller's avatar
Niels Möller committed
968
  verbose("Pty allocation failed.\n");
Niels Möller's avatar
Niels Möller committed
969
  lsh_string_free(term);
Niels Möller's avatar
Niels Möller committed
970

971
  EXCEPTION_RAISE(e, &pty_request_failed);
Niels Möller's avatar
Niels Möller committed
972
973
}

974
975
976
struct channel_request
pty_request_handler =
{ STATIC_HEADER, do_alloc_pty };
Niels Möller's avatar
Niels Möller committed
977
978

#endif /* WITH_PTY_SUPPORT */