service.c 3.11 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
/* service.c
 *
 */

/* 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
25
26

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

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

38
39
40
41
42
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
43
44
45
46
47
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
48
49
50
51
52
53
54
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
55
  int msg_number;
Niels Möller's avatar
Niels Möller committed
56
57
58
59
60
61
62
63
64
65
66
67
68
  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;

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

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

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

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

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;
}