server.c 13.5 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* server.c
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 * $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.
Niels Möller's avatar
Niels Möller committed
24 25 26 27 28
 */

#include "server.h"

#include "abstract_io.h"
29
#include "channel.h"
30
#include "connection.h"
Niels Möller's avatar
Niels Möller committed
31 32
#include "debug.h"
#include "format.h"
33 34 35
#include "keyexchange.h"
#include "read_line.h"
#include "read_packet.h"
36
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
37
#include "unpad.h"
38
#include "version.h"
Niels Möller's avatar
Niels Möller committed
39 40 41
#include "werror.h"
#include "xalloc.h"

42 43 44 45 46 47 48
#include <assert.h>
#include <string.h>
#include <errno.h>

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

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
struct server_callback
{
  struct fd_callback super;
  struct io_backend *backend;

  struct signer *secret;        /* secret key */
  struct lsh_string *host_key;  /* public key */
  UINT32 block_size;
  char *id_comment;

  struct randomness *random;
  struct make_kexinit *init;
  struct packet_handler *kexinit_handler;
};

Niels Möller's avatar
Niels Möller committed
64
static int server_initiate(struct fd_callback **c,
Niels Möller's avatar
Niels Möller committed
65 66
			   int fd)
{
Niels Möller's avatar
Niels Möller committed
67 68
  struct server_callback *closure = (struct server_callback *) *c;
  
69 70 71
  struct ssh_connection *connection
    = make_ssh_connection(closure->kexinit_handler);

Niels Möller's avatar
Niels Möller committed
72 73
  int res;
  
74
  verbose("server_initiate()\n");
Niels Möller's avatar
Niels Möller committed
75 76 77 78 79 80 81 82

  connection_init_io(connection,
		     io_read_write(closure->backend, fd,
				   make_server_read_line(connection),
				   closure->block_size,
				   make_server_close_handler()),
		     closure->random);

Niels Möller's avatar
Niels Möller committed
83 84
  
  connection->server_version
Niels Möller's avatar
Niels Möller committed
85
    = ssh_format("SSH-%lz-%lz %lz",
Niels Möller's avatar
Niels Möller committed
86 87 88
		 PROTOCOL_VERSION,
		 SOFTWARE_SERVER_VERSION,
		 closure->id_comment);
Niels Möller's avatar
Niels Möller committed
89

Niels Möller's avatar
Niels Möller committed
90 91
  res = A_WRITE(connection->raw,
		 ssh_format("%lS\r\n", connection->server_version));
92
  if (LSH_CLOSEDP(res))
Niels Möller's avatar
Niels Möller committed
93 94
    return res;

95 96 97
  return res | initiate_keyexchange(connection, CONNECTION_SERVER,
				    MAKE_KEXINIT(closure->init),
				    NULL);
Niels Möller's avatar
Niels Möller committed
98 99 100 101 102 103 104 105
}

struct server_line_handler
{
  struct line_handler super;
  struct ssh_connection *connection;
};

Niels Möller's avatar
Niels Möller committed
106
static struct read_handler *do_line(struct line_handler **h,
Niels Möller's avatar
Niels Möller committed
107 108 109
				    UINT32 length,
				    UINT8 *line)
{
Niels Möller's avatar
Niels Möller committed
110 111
  struct server_line_handler *closure = (struct server_line_handler *) *h;
  
Niels Möller's avatar
Niels Möller committed
112 113
  MDEBUG(closure);
  
Niels Möller's avatar
Niels Möller committed
114 115 116 117 118
  if ( (length >= 4) && !memcmp(line, "SSH-", 4))
    {
      /* Parse and remember format string */
      if ((length >= 8) && !memcmp(line + 4, "2.0-", 4))
	{
Niels Möller's avatar
Niels Möller committed
119 120 121
	  struct read_handler *new = make_read_packet
	    (make_packet_unpad
	     (make_packet_debug(&closure->connection->super,
122
				"recieved")),
Niels Möller's avatar
Niels Möller committed
123
	     closure->connection);
Niels Möller's avatar
Niels Möller committed
124 125
	  
	  closure->connection->client_version
Niels Möller's avatar
Niels Möller committed
126
	    = ssh_format("%ls", length, line);
Niels Möller's avatar
Niels Möller committed
127

Niels Möller's avatar
Niels Möller committed
128 129 130 131 132
	  verbose("Client version: ");
	  verbose_safe(closure->connection->client_version->length,
		       closure->connection->client_version->data);
	  verbose("\n");
	  
Niels Möller's avatar
Niels Möller committed
133
	  /* FIXME: Cleanup properly. */
Niels Möller's avatar
Niels Möller committed
134
	  lsh_object_free(closure);
Niels Möller's avatar
Niels Möller committed
135 136 137 138 139 140 141 142 143

	  return new;
	}
      else
	{
	  werror("Unsupported protocol version: ");
	  werror_safe(length, line);
	  werror("\n");

Niels Möller's avatar
Niels Möller committed
144
	  /* FIXME: Clean up properly */
Niels Möller's avatar
Niels Möller committed
145
	  lsh_object_free(closure);
Niels Möller's avatar
Niels Möller committed
146 147
	  *h = 0;
		  
Niels Möller's avatar
Niels Möller committed
148 149 150 151 152 153 154 155 156 157 158 159
	  return 0;
	}
    }
  else
    {
      /* Display line */
      werror_safe(length, line);

      /* Read next line */
      return 0;
    }
}
Niels Möller's avatar
Niels Möller committed
160

Niels Möller's avatar
Niels Möller committed
161
struct read_handler *make_server_read_line(struct ssh_connection *c)
Niels Möller's avatar
Niels Möller committed
162
{
Niels Möller's avatar
Niels Möller committed
163 164 165
  struct server_line_handler *closure;

  NEW(closure);
Niels Möller's avatar
Niels Möller committed
166 167
  
  closure->super.handler = do_line;
Niels Möller's avatar
Niels Möller committed
168
  closure->connection = c;
Niels Möller's avatar
Niels Möller committed
169 170 171 172
  
  return make_read_line(&closure->super);
}

173 174 175 176
struct fd_callback *
make_server_callback(struct io_backend *b,
		     char *comment,
		     UINT32 block_size,
Niels Möller's avatar
Niels Möller committed
177 178
		     struct randomness *random,
		     struct make_kexinit *init,
179
		     struct packet_handler *kexinit_handler)
Niels Möller's avatar
Niels Möller committed
180
{
Niels Möller's avatar
Niels Möller committed
181 182 183
  struct server_callback *connected;

  NEW(connected);
Niels Möller's avatar
Niels Möller committed
184 185 186 187 188

  connected->super.f = server_initiate;
  connected->backend = b;
  connected->block_size = block_size;
  connected->id_comment = comment;
189

Niels Möller's avatar
Niels Möller committed
190 191
  connected->random = random;  
  connected->init = init;
192
  connected->kexinit_handler = kexinit_handler;
Niels Möller's avatar
Niels Möller committed
193 194 195 196
  
  return &connected->super;
}

197
static int server_die(struct close_callback *closure, int reason)
Niels Möller's avatar
Niels Möller committed
198
{
199 200 201 202
  verbose("Connection died, for reason %d.\n", reason);
  if (reason != CLOSE_EOF)
    werror("Connection died.\n");

Niels Möller's avatar
Niels Möller committed
203 204 205
  return 0;  /* Ignored */
}

206
struct close_callback *make_server_close_handler(void)
Niels Möller's avatar
Niels Möller committed
207
{
Niels Möller's avatar
Niels Möller committed
208 209 210
  struct close_callback *c;

  NEW(c);
Niels Möller's avatar
Niels Möller committed
211 212 213 214 215 216

  c->f = server_die;

  return c;
}

217 218 219 220 221 222 223 224 225 226
/* Session */
struct server_session
{
  struct ssh_channel super;

  /* User information */
  struct unix_user *user;

  /* Non-zero if a shell or command has been started. */
  int running;
Niels Möller's avatar
Niels Möller committed
227 228 229 230 231

  /* Child process's stdio */
  struct abstract_write *in;
  struct io_fd *out;
  struct io_fd *err;
232 233
};

Niels Möller's avatar
Niels Möller committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/* Recieve channel data */
static int do_recieve(struct ssh_channel *c,
		      int type, struct lsh_string *data)
{
  struct server_session *closure = (struct server_session *) c;
  
  MDEBUG(closure);

  switch(type)
    {
    case CHANNEL_DATA:
      return A_WRITE(closure->in, data);
    case CHANNEL_STDERR_DATA:
      werror("Ignoring unexpected stderr data.\n");
      lsh_string_free(data);
      return LSH_OK | LSH_GOON;
    default:
      fatal("Internal error!\n");
    }
}

/* We may send more data */
static int do_send(struct ssh_channel *c)
{
  struct server_session *closure = (struct server_session *) c;

  MDEBUG(closure);

  closure->out->on_hold = 0;
  closure->err->on_hold = 0;
  
  return LSH_OK | LSH_GOON;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
struct ssh_channel *make_server_session(struct unix_user *user,
					UINT32 max_window,
					struct alist *request_types)
{
  struct server_session *self;

  NEW(self);

  init_channel(&self->super);

  self->super.max_window = max_window;
  self->super.rec_window_size = max_window;

  /* FIXME: Make maximum packet size configurable. */
  self->super.rec_max_packet = SSH_MAX_PACKET;

  self->super.request_types = request_types;
  self->user = user;

  self->running = 0;
Niels Möller's avatar
Niels Möller committed
288 289 290 291

  self->in = NULL;
  self->out = NULL;
  self->err = NULL;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  
  return &self->super;
}

struct open_session
{
  struct channel_open super;

  struct unix_user *user;
  struct alist *session_requests;
};

#define WINDOW_SIZE (SSH_MAX_PACKET << 3)

static struct ssh_channel *do_open_session(struct channel_open *c,
					   struct simple_buffer *args,
					   UINT32 *error,
					   char **error_msg,
					   struct lsh_string **data)
{
  struct open_session *closure = (struct open_session *) c;
  
  MDEBUG(closure);

  debug("server.c: do_open_session()\n");
  
  if (!parse_eod(args))
    return 0;
  
  return make_server_session(closure->user, WINDOW_SIZE, closure->session_requests);
}

struct channel_open *make_open_session(struct unix_user *user,
				       struct alist *session_requests)
{
  struct open_session *closure;

  NEW(closure);

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

struct server_connection_service
{
  struct unix_service super;

  struct alist *global_requests;

  /* Requests specific to session channels */
  struct alist *session_requests; 

  /* FIXME: Doesn't support any channel types but "session".
   * This must be fixed to support for "direct-tcpip" channels. */
};

/* Start an authenticated ssh-connection service */
static struct ssh_service *do_login(struct unix_service *c,
				    struct unix_user *user)
{
  struct server_connection_service *closure
    = (struct server_connection_service *) c;

  MDEBUG(closure);

  debug("server.c: do_login()\n");
  
  return
    make_connection_service(closure->global_requests,
			    make_alist(1, ATOM_SESSION,
				       make_open_session(user,
							 closure->session_requests),
				       -1),
			    NULL);
}

struct unix_service *make_server_session_service(struct alist *global_requests,
						 struct alist *session_requests)
{
  struct server_connection_service *closure;

  NEW(closure);

  closure->super.login = do_login;
  closure->global_requests = global_requests;
  closure->session_requests = session_requests;
  
  return &closure->super;
}

struct shell_request
{
  struct channel_request super;

  struct io_backend *backend;
};

/* Creates a one-way socket connection. Returns 1 on successm 0 on
 * failure. fds[0] is for reading, fds[1] for writing (like for the
 * pipe() system call). */
static int make_pipe(int *fds)
{
  /* From the shutdown(2) man page */
#define REC 0
#define SEND 1

Niels Möller's avatar
Niels Möller committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
    {
      werror("socketpair() failed: %s\n", strerror(errno));
      return 0;
    }
  debug("Created socket pair. Using fd:s %d <-- %d\n", fds[0], fds[1]);

  if(shutdown(fds[0], SEND) < 0)
    {
      werror("shutdown(%d, SEND) failed: %s\n", fds[0], strerror(errno));
      return 0;
    }
  if (shutdown(fds[1], REC) < 0)
    {
      werror("shutdown(%d, SEND) failed: %s\n", fds[0], strerror(errno));
      return 0;
    }
  return 1;
419 420 421 422
}

static char *make_env_pair(char *name, struct lsh_string *value)
{
Niels Möller's avatar
Niels Möller committed
423
  return ssh_format("%z=%lS%c", name, value, 0)->data;
424 425 426 427
}

static char *make_env_pair_c(char *name, char *value)
{
Niels Möller's avatar
Niels Möller committed
428
  return ssh_format("%z=%z%c", name, value, 0)->data;
429 430 431 432 433 434 435 436 437 438
}

static int do_spawn_shell(struct channel_request *c,
			  struct ssh_channel *channel,
			  int want_reply,
			  struct simple_buffer *args)
{
  struct shell_request *closure = (struct shell_request *) c;
  struct server_session *session = (struct server_session *) channel;

Niels Möller's avatar
Niels Möller committed
439 440 441
  int in[2];
  int out[2];
  int err[2];
442 443

  MDEBUG(closure);
Niels Möller's avatar
Niels Möller committed
444
  MDEBUG(session);
445 446 447 448 449 450 451 452

  if (!parse_eod(args))
    return LSH_FAIL | LSH_DIE;

  if (session->running)
    /* Already spawned a shell or command */
    goto fail;
  
Niels Möller's avatar
Niels Möller committed
453 454
  /* {in|out|err}[0] is for reading,
   * {in|out|err}[1] for writing. */
455

Niels Möller's avatar
Niels Möller committed
456
  if (make_pipe(in))
457
    {
Niels Möller's avatar
Niels Möller committed
458
      if (make_pipe(out))
459
	{
Niels Möller's avatar
Niels Möller committed
460
	  if (make_pipe(err))
461 462 463 464 465 466 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
	    {
	      pid_t child;
	      
	      switch(child = fork())
		{
		case -1:
		  werror("fork() failed: %s\n", strerror(errno));
		  /* Close and return channel_failure */
		  break; 
		case 0:
		  /* Child */
		  if (!session->user->shell)
		    {
		      werror("No login shell!\n");
		      exit(EXIT_FAILURE);
		    }
		      
		  if (getuid() != session->user->uid)
		    if (!change_uid(session->user))
		      {
			werror("Changing uid failed!\n");
			exit(EXIT_FAILURE);
		      }
		  
		  assert(getuid() == session->user->uid);

		  if (!change_dir(session->user))
		    {
		      werror("Could not change to home (or root) directory!\n");
		      exit(EXIT_FAILURE);
		    }

		  /* 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. */

		  close(STDIN_FILENO);
Niels Möller's avatar
Niels Möller committed
499
		  if (dup2(in[0], STDIN_FILENO) < 0)
500 501 502 503
		    {
		      werror("Can't dup stdin!\n");
		      exit(EXIT_FAILURE);
		    }
Niels Möller's avatar
Niels Möller committed
504 505
		  close(in[0]);
		  close(in[1]);
506 507

		  close(STDOUT_FILENO);
Niels Möller's avatar
Niels Möller committed
508
		  if (dup2(out[1], STDOUT_FILENO) < 0)
509 510 511 512
		    {
		      werror("Can't dup stdout!\n");
		      exit(EXIT_FAILURE);
		    }
Niels Möller's avatar
Niels Möller committed
513 514
		  close(out[0]);
		  close(out[1]);
515 516

		  close(STDERR_FILENO);
Niels Möller's avatar
Niels Möller committed
517
		  if (dup2(err[1], STDERR_FILENO) < 0)
518 519 520 521
		    {
		      /* Can't write any message to stderr. */ 
		      exit(EXIT_FAILURE);
		    }
Niels Möller's avatar
Niels Möller committed
522 523
		  close(err[0]);
		  close(err[1]);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

		  {
		    char *shell = session->user->shell->data;
#define MAX_ENV 7
		    char *env[MAX_ENV];
		    char *tz = getenv("TZ");
		    int i = 0;

		    env[i++] = make_env_pair("LOGNAME", session->user->name);
		    env[i++] = make_env_pair("USER", session->user->name);
		    env[i++] = make_env_pair("SHELL", session->user->shell);
		    if (session->user->home)
		      env[i++] = make_env_pair("HOME", session->user->home);
		    if (tz)
		      env[i++] = make_env_pair_c("TZ", tz);

		    /* FIXME: The value of $PATH should not be hard-coded */
		    env[i++] = "PATH=/bin:/usr/bin";
		    env[i++] = NULL;
		    
		    assert(i <= MAX_ENV);

		    if (execle(shell, shell, NULL, env) < 0)
		      exit(EXIT_FAILURE);
#undef MAX_ENV
		  }
		default:
		  /* Parent */
Niels Möller's avatar
Niels Möller committed
552
		  /* FIXME: Install a callback to catch dying children */
553 554
		    
		  /* Close the child's fd:s */
Niels Möller's avatar
Niels Möller committed
555 556
		  close(in[0]);
		  close(out[1]);
Niels Möller's avatar
Niels Möller committed
557
		  close(err[1]);
558

Niels Möller's avatar
Niels Möller committed
559 560 561 562 563
		  
		  session->in
		    = &io_write(closure->backend, in[1],
				SSH_MAX_PACKET,
				/* FIXME: Use a proper close callback */
Niels Möller's avatar
Niels Möller committed
564 565
				make_channel_close(channel))
		    ->buffer->super;
Niels Möller's avatar
Niels Möller committed
566 567 568 569 570 571 572 573 574 575 576 577
		  session->out
		    = io_read(closure->backend, out[0],
			      make_channel_read_data(channel),
			      NULL);
		  session->err
		    = io_read(closure->backend, err[0],
			      make_channel_read_stderr(channel),
			      NULL);

		  channel->recieve = do_recieve;
		  channel->send = do_send;
		  
578 579 580 581 582 583 584
		  session->running = 1;
		  return want_reply
		    ? A_WRITE(channel->write,
			      format_channel_success(channel->channel_number))
		    : LSH_OK | LSH_GOON;
		  
		}
Niels Möller's avatar
Niels Möller committed
585 586
	      close(err[0]);
	      close(err[1]);
587
	    }
Niels Möller's avatar
Niels Möller committed
588 589
	  close(out[0]);
	  close(out[1]);
590
	}
Niels Möller's avatar
Niels Möller committed
591 592
      close(in[0]);
      close(in[1]);
593 594 595 596 597 598 599 600 601 602 603 604 605
    }
 fail:
  return want_reply
    ? A_WRITE(channel->write, format_channel_failure(channel->channel_number))
    : LSH_OK | LSH_GOON;
}

struct channel_request *make_shell_handler(struct io_backend *backend)
{
  struct shell_request *closure;

  NEW(closure);
  closure->super.handler = do_spawn_shell;
Niels Möller's avatar
Niels Möller committed
606 607
  closure->backend = backend;
  
608 609 610
  return &closure->super;
}