service.c 3.12 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* service.c
 *
Niels Möller's avatar
Niels Möller committed
3
 * $Id$
Niels Möller's avatar
Niels Möller committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 */

/* 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 "service.h"
Niels Möller's avatar
Niels Möller committed
26
27

#include "disconnect.h"
Niels Möller's avatar
Niels Möller committed
28
#include "format.h"
Niels Möller's avatar
Niels Möller committed
29
30
31
#include "parse.h"
#include "ssh.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
32
33
34
35
36
37
38

struct service_handler
{
  struct packet_handler super;
  struct alist *services;
};

39
40
41
42
43
struct lsh_string *format_service_request(int name)
{
  return ssh_format("%c%a", SSH_MSG_SERVICE_REQUEST, name);
}

Niels Möller's avatar
Niels Möller committed
44
45
46
47
48
struct lsh_string *format_service_accept(int name)
{
  return ssh_format("%c%a", SSH_MSG_SERVICE_ACCEPT, name);
}

Niels Möller's avatar
Niels Möller committed
49
50
51
52
53
54
55
static int do_service(struct packet_handler *c,
		      struct ssh_connection *connection,
		      struct lsh_string *packet)
{
  struct service_handler *closure = (struct service_handler *) c;

  struct simple_buffer buffer;
Niels Möller's avatar
Niels Möller committed
56
  int msg_number;
Niels Möller's avatar
Niels Möller committed
57
58
59
60
61
62
63
64
65
66
67
68
69
  int name;
  
  MDEBUG(closure);

  simple_buffer_init(&buffer, packet->length, packet->data);

  if (parse_uint8(&buffer, &msg_number)
      && (msg_number == SSH_MSG_SERVICE_REQUEST)
      && parse_atom(&buffer, &name)
      && parse_eod(&buffer))
    {
      struct ssh_service *service;

70
71
      lsh_string_free(packet);
      
Niels Möller's avatar
Niels Möller committed
72
      if (!name
Niels Möller's avatar
Niels Möller committed
73
74
75
	  || !(service = ALIST_GET(closure->services, name))
	  || !SERVICE_INIT(service, connection))
	{
Niels Möller's avatar
Niels Möller committed
76
77
	  return (LSH_FAIL | LSH_CLOSE)
	    | A_WRITE(connection->write,
Niels Möller's avatar
Niels Möller committed
78
		      format_disconnect(SSH_DISCONNECT_SERVICE_NOT_AVAILABLE,
Niels Möller's avatar
Niels Möller committed
79
					"Service not available.", ""));
Niels Möller's avatar
Niels Möller committed
80
	}
81
      /* Don't accept any further service requests */
Niels Möller's avatar
Niels Möller committed
82
      connection->dispatch[SSH_MSG_SERVICE_REQUEST]
83
84
	= connection->fail;
      
Niels Möller's avatar
Niels Möller committed
85
      return A_WRITE(connection->write, format_service_accept(name));
Niels Möller's avatar
Niels Möller committed
86
87
88
89
90
91
    }
  return LSH_FAIL | LSH_DIE;
}
      
struct packet_handler *make_service_handler(struct alist *services)
{
Niels Möller's avatar
Niels Möller committed
92
93
94
  struct service_handler *self;

  NEW(self);
Niels Möller's avatar
Niels Möller committed
95
96
97
98
99
100

  self->super.handler = do_service;
  self->services = services;

  return &self->super;
}
Niels Möller's avatar
Niels Möller committed
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

struct meta_service
{
  struct ssh_service super;

  struct packet_handler *service_handler;
};

static int init_meta_service(struct ssh_service *c,
				   struct ssh_connection *connection)
{
  struct meta_service *closure = (struct meta_service *) c;

  MDEBUG(closure);
  
  connection->dispatch[SSH_MSG_SERVICE_REQUEST] = closure->service_handler;

  return LSH_OK | LSH_GOON;
}
  
struct ssh_service *make_meta_service(struct alist *services)
{
  struct meta_service *self;

  NEW(self);

  self->super.init = init_meta_service;
  self->service_handler = make_service_handler(services);

  return &self->super;
}