File: chap.c

package info (click to toggle)
ipgrab 0.9.9-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 1,712 kB
  • ctags: 2,059
  • sloc: ansic: 11,983; sh: 878; makefile: 52
file content (210 lines) | stat: -rw-r--r-- 4,720 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**************************************************************************** 
** File: chap.c
**
** Author: Mike Borella
**
** Comments: CHAP decoding.
**
** $Id: chap.c,v 1.8 2001/10/02 18:37:43 mborella Exp $
**
** 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 Library 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.
**
*****************************************************************************/

#include "chap.h"

/*
 * CHAP header
 */

typedef struct chap
{
  u_int8_t  code;
  u_int8_t  identifier;
  u_int16_t length;
} chap_t;

/*
 * CHAP codes
 */

#define CHAP_CODE_CHALLENGE   1
#define CHAP_CODE_RESPONSE    2
#define CHAP_CODE_SUCCESS     3
#define CHAP_CODE_FAILURE     4

/*
 * CHAP code map
 */

strmap_t chap_code_map[] =
{
  { CHAP_CODE_CHALLENGE,      "challenge" },
  { CHAP_CODE_RESPONSE,       "response" },
  { CHAP_CODE_SUCCESS,        "success" },
  { CHAP_CODE_FAILURE,        "failure" },
  { 0, "" }
};

extern struct arg_t * my_args;

/*----------------------------------------------------------------------------
**
** dump_chap()
**
** Dump CHAP headers and payload.
**
**----------------------------------------------------------------------------
*/

void dump_chap(packet_t *pkt)
{
  chap_t chap;
  
  /* Set the layer */
  set_layer(LAYER_DATALINK);

  /*
   * Get the header
   */
  
  if (get_packet_bytes((u_int8_t *) &chap, pkt, sizeof(chap_t)) == 0)
    return;
      
  /*
   * Conversion
   */
  
  chap.length = ntohs(chap.length);
      
  /*
   * Dump the header
   */
  
  if (my_args->m)
    {
      display_minimal_string("| CHAP ");
    }
  else
    {
      display_header_banner("CHAP Header");
      display_strmap("Code", chap.code, chap_code_map);
      display("Identifier", &chap.identifier, 1, DISP_DEC);
      display("Length", (u_int8_t *) &chap.length, 2, DISP_DEC);
    }

  /*
   * Get the CHAP payload 
   */

  switch(chap.code)
    {
    case CHAP_CODE_CHALLENGE:
    case CHAP_CODE_RESPONSE:
      {
	u_int8_t * value;
	u_int8_t   value_size;
	u_int8_t * name;
	u_int16_t  name_size;

	/* Get the value size */
	if (get_packet_bytes(&value_size, pkt, 1) == 0)
	  return;
	
	/* Get the value */
	value = (u_int8_t *) my_malloc(value_size);
	if (get_packet_bytes(value, pkt, value_size) == 0)
	  {
	    my_free(value);
	    return;
	  }

	/* Figure out the size of the name and get it */
	name_size = chap.length - sizeof(chap_t) - value_size - 1;
	name = (u_int8_t *) my_malloc(name_size+1);
	if (get_packet_bytes(name, pkt, name_size) == 0)
	  {
	    my_free(value);
	    my_free(name);
	    return;
	  }
	name[name_size] = '\0';

	/* display everything */
	if (my_args->m)
	  {
	    if (chap.code == CHAP_CODE_CHALLENGE)
	      display_minimal_string("challenge ");
	    if (chap.code == CHAP_CODE_RESPONSE)
	      display_minimal_string("response ");
	    display_minimal_string("(");
	    display_minimal(value, value_size, DISP_HEX);
	    display_minimal_string(", ");
	    display_minimal_string(name);
	    display_minimal_string(")");
	  }
	else
	  {
	    display("Value size", &value_size, 1, DISP_DEC);
	    display("Value", value, value_size, DISP_HEX_MULTILINE);
	    display_string("Name", name);
	  }
	
	/* free memory */
	my_free(value);
	my_free(name);
      }
      break;

    case CHAP_CODE_SUCCESS:
    case CHAP_CODE_FAILURE:
      {
	u_int8_t * message;
	u_int8_t   message_len;

	/* figure out length of the message and then grab it */
	message_len = chap.length - sizeof(chap_t);
	message = (u_int8_t *) my_malloc (message_len + 1);
	if (get_packet_bytes(message, pkt, message_len) == 0)
	  {
	    my_free(message);
	    return;
	  }
	message[message_len] = '\0';

	/* display it */
	if (my_args->m)
	  {
	    if (chap.code == CHAP_CODE_SUCCESS)
	      display_minimal_string("success ");
	    if (chap.code == CHAP_CODE_FAILURE)
	      display_minimal_string("failure ");
	    display_minimal_string("(");
	    display_minimal_string(message);
	    display_minimal_string(")");
	  }
	else
	  {
	    display_string("Message", message);
	  }

	/* free memory */
	my_free(message);
      }
      break;

    }

}