connection_commands.c 3.32 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
/* connection_commands.c
 *
 * $Id$
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 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
 */

25 26 27 28 29 30 31 32 33
#include "connection_commands.h"

#include "connection.h"
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>

34 35 36 37
#define GABA_DEFINE
#include "connection_commands.h.x"
#undef GABA_DEFINE

38 39
#include "connection_commands.c.x"

40

41 42 43 44 45
static void
do_connection_remember(struct command *s,
		       struct lsh_object *x,
		       struct command_continuation *c,
		       struct exception_handler *e UNUSED)
46
{
47
  CAST(connection_command, self, s);
48 49 50 51 52
  CAST_SUBTYPE(resource, resource, x);

  if (resource)
    REMEMBER_RESOURCE(self->connection->resources, resource);

53
  COMMAND_RETURN(c, resource);
54 55
}

56
DEFINE_COMMAND_SIMPLE(connection_remember, a)
57
{
58
  CAST(ssh_connection, connection, a);
59
  NEW(connection_command, self);
60 61 62 63 64 65 66

  self->super.call = do_connection_remember;
  self->connection = connection;

  return &self->super.super;
}

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 112 113 114 115 116 117 118
/* (connection_if_srp then_f else_f connection)
 *
 * Invokes either (then_f connection) or (else_f connection)
 * depending on whether or not the CONNECTION_SRP flag is set.
 */

/* GABA:
   (class
     (name connection_if_srp)
     (super command)
     (vars
       (then_f object command)
       (else_f object command)))
*/

static void
do_connection_if_srp(struct command *s,
		     struct lsh_object *a,
		     struct command_continuation *c,
		     struct exception_handler *e)
{
  CAST(connection_if_srp, self, s);
  CAST(ssh_connection, connection, a);

  COMMAND_CALL(( (connection->flags & CONNECTION_SRP)
		 ? self->then_f : self->else_f),
	       connection, c, e);
}

struct command *
make_connection_if_srp(struct command *then_f,
		       struct command *else_f)
{
  NEW(connection_if_srp, self);
  self->super.call = do_connection_if_srp;
  self->then_f = then_f;
  self->else_f = else_f;

  return &self->super;
}

static struct lsh_object *
collect_if_srp_2(struct collect_info_2 *info,
		 struct lsh_object *t,
		 struct lsh_object *e)
{     
  CAST_SUBTYPE(command, then_f, t);
  CAST_SUBTYPE(command, else_f, e);
  
  assert(!info->next);
  return &make_connection_if_srp(then_f, else_f)->super;
}
119

120 121 122 123 124 125
static struct collect_info_2 collect_info_if_srp_2 =
STATIC_COLLECT_2_FINAL(collect_if_srp_2);

struct collect_info_1 connection_if_srp_command =
STATIC_COLLECT_1(&collect_info_if_srp_2);

126
DEFINE_COMMAND(connection_require_userauth, a, c, e UNUSED)
127 128 129 130 131 132 133 134 135 136
{
  CAST(ssh_connection, connection, a);

  if (connection->user)
    COMMAND_RETURN(c, connection);
  else
    EXCEPTION_RAISE(connection->e,
		    make_protocol_exception(SSH_DISCONNECT_SERVICE_NOT_AVAILABLE,
					    "Access denied."));
}