debug.c 3.1 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

/* 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
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23
 */

24
25
26
27
#if HAVE_CONFIG_H
#include "config.h"
#endif

28
29
30
#include "connection.h"
#include "format.h"
#include "parse.h"
31
#include "lsh_string.h"
32
#include "ssh.h"
Niels Möller's avatar
Niels Möller committed
33
#include "xalloc.h"
34
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
35

36
37
#include "debug.c.x"

38
/* GABA:
39
40
41
42
   (class
     (name packet_debug)
     (super abstract_write_pipe)
     (vars
43
       (prefix string)))
44
45
*/

Niels Möller's avatar
Niels Möller committed
46
47
static void
do_debug(struct abstract_write *w,
48
	 struct lsh_string *packet)
49
{
50
  CAST(packet_debug, closure, w);
51

52
  if (!lsh_string_length(packet))
53
    debug("DEBUG: %S empty packet\n", closure->prefix);
54
55
  else
    {
56
      uint8_t type = lsh_string_data(packet)[0];
57
58
      if (type == SSH_MSG_USERAUTH_REQUEST
	  || type == SSH_MSG_USERAUTH_INFO_RESPONSE)
59
	debug("DEBUG: %S %z *****\n",
60
61
	      closure->prefix, packet_types[type]);
      else
62
	debug("DEBUG: %S %z %xS\n",
63
64
65
	      closure->prefix, packet_types[type],
	      packet);
    }
66
  A_WRITE(closure->super.next, packet);
67
68
}

Niels Möller's avatar
Niels Möller committed
69
struct abstract_write *
70
make_packet_debug(struct abstract_write *next,
71
		  struct lsh_string *prefix)
72
{
73
  NEW(packet_debug, closure);
74

75
  closure->super.super.write = do_debug;
76
  closure->super.next = next;
77
  closure->prefix = prefix;
78

79
  return &closure->super.super;
80
81
}

82
83
84

static struct lsh_string *
make_debug_packet(const char *msg, int always_display)
85
86
87
88
89
90
91
92
93
94
{
  return ssh_format("%c%c%z%z",
		    SSH_MSG_DEBUG,
		    always_display,
		    msg,
		    /* Empty language tag */ 
		    "");
}

/* Send a debug message to the other end. */
95
void
96
97
send_debug_message(struct ssh_connection *connection,
		   const char *msg, int always_display)
98
{
99
  /* Can be sent even during key exchange. */
100
  connection_send_kex(connection, make_debug_packet(msg, always_display));
101
102
}

103
DEFINE_PACKET_HANDLER(, connection_debug_handler, connection UNUSED, packet)
104
105
{
  struct simple_buffer buffer;
Niels Möller's avatar
Niels Möller committed
106
107
  unsigned msg_number;
  unsigned always_display;
108
109
  uint32_t length;
  const uint8_t *msg;
110
111
  int language;
  
112
  simple_buffer_init(&buffer, STRING_LD(packet));
113
114
115
116
117
118
119

  if (!(parse_uint8(&buffer, &msg_number)
	&& parse_uint8(&buffer, &always_display)
	&& parse_string(&buffer, &length, &msg)
	&& parse_atom(&buffer, &language)
	&& parse_eod(&buffer)))
    {
120
      PROTOCOL_ERROR(connection->e, "Invalid DEBUG message.");
121
122
    }
  else
Niels Möller's avatar
Niels Möller committed
123
124
125
    {
      if (always_display)
	werror("Received debug: %ups\n", length, msg);
126

Niels Möller's avatar
Niels Möller committed
127
128
129
      else
	verbose("Received debug: %ups\n", length, msg);
    }
130
}