debug.c 3.52 KB
Newer Older
1
2
/* debug.c
 *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 *
 * $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
J.H.M. Dassen's avatar
J.H.M. Dassen committed
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25
 */

Niels Möller's avatar
Niels Möller committed
26
#include "debug.h"
27
28
29
30
31

#include "connection.h"
#include "format.h"
#include "parse.h"
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
32
#include "xalloc.h"
33
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
34

35
36
#include "debug.c.x"

37
/* GABA:
38
39
40
41
   (class
     (name packet_debug)
     (super abstract_write_pipe)
     (vars
42
       (prefix simple "const char *")))
43
44
*/

Niels Möller's avatar
Niels Möller committed
45
46
static void
do_debug(struct abstract_write *w,
47
	 struct lsh_string *packet)
48
{
49
  CAST(packet_debug, closure, w);
50
51
52
53
54
55
56
57
58
59
60
61
62
63

  if (!packet->length)
    debug("DEBUG: %z empty packet\n", closure->prefix);
  else
    {
      UINT8 type = packet->data[0];
      if (type == SSH_MSG_USERAUTH_REQUEST)
	debug("DEBUG: %z %z *****\n",
	      closure->prefix, packet_types[type]);
      else
	debug("DEBUG: %z %z %xS\n",
	      closure->prefix, packet_types[type],
	      packet);
    }
64
  A_WRITE(closure->super.next, packet);
65
66
}

Niels Möller's avatar
Niels Möller committed
67
struct abstract_write *
68
69
make_packet_debug(struct abstract_write *next,
		  const char *prefix)
70
{
71
  NEW(packet_debug, closure);
72

73
  closure->super.super.write = do_debug;
74
  closure->super.next = next;
75
  closure->prefix = prefix;
76

77
  return &closure->super.super;
78
79
}

80
static struct lsh_string *make_debug_packet(const char *msg, int always_display)
81
82
83
84
85
86
87
88
89
90
{
  return ssh_format("%c%c%z%z",
		    SSH_MSG_DEBUG,
		    always_display,
		    msg,
		    /* Empty language tag */ 
		    "");
}

/* Send a debug message to the other end. */
91
92
93
94
95
96
void send_debug_message(struct abstract_write *write, const char *msg, int always_display)
{
  A_WRITE(write, make_debug_packet(msg, always_display));
}

void send_debug(struct abstract_write *write, const char *msg, int always_display)
97
{
Niels Möller's avatar
Niels Möller committed
98
  if (debug_flag)
99
    send_debug_message(write, msg, always_display);
100
101
}

102
void send_verbose(struct abstract_write *write, const char *msg, int always_display)
103
{
Niels Möller's avatar
Niels Möller committed
104
  if (verbose_flag)
105
    send_debug_message(write, msg, always_display);
106
107
}

Niels Möller's avatar
Niels Möller committed
108
109
110
111
static void
do_rec_debug(struct packet_handler *self UNUSED,
	     struct ssh_connection *connection UNUSED,
	     struct lsh_string *packet)
112
113
{
  struct simple_buffer buffer;
Niels Möller's avatar
Niels Möller committed
114
115
  unsigned msg_number;
  unsigned always_display;
116
117
118
119
120
121
122
123
124
125
126
127
128
  UINT32 length;
  UINT8 *msg;
  int language;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (!(parse_uint8(&buffer, &msg_number)
	&& parse_uint8(&buffer, &always_display)
	&& parse_string(&buffer, &length, &msg)
	&& parse_atom(&buffer, &language)
	&& parse_eod(&buffer)))
    {
      lsh_string_free(packet);
129
      PROTOCOL_ERROR(connection->e, "Invalid DEBUG message.");
Niels Möller's avatar
Niels Möller committed
130
				 
131
132
    }
  else
Niels Möller's avatar
Niels Möller committed
133
134
135
    {
      if (always_display)
	werror("Received debug: %ups\n", length, msg);
136

Niels Möller's avatar
Niels Möller committed
137
138
139
140
141
      else
	verbose("Received debug: %ups\n", length, msg);
      
      lsh_string_free(packet);
    }
142
143
144
145
}

struct packet_handler *make_rec_debug_handler(void)
{
146
  NEW(packet_handler, self);
147
148
149
150
151

  self->handler = do_rec_debug;

  return self;
}