413570451b56ebc883cab9b4d6af9e4e061b1694
[metze/wireshark/wip.git] / epan / dissectors / packet-tds.c
1 /* packet-tds.c
2  * Routines for TDS NetLib dissection
3  * Copyright 2000-2002, Brian Bruns <camber@ais.org>
4  * Copyright 2002, Steve Langasek <vorlon@netexpress.net>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*
28  * The NETLIB protocol is a small blocking protocol designed to allow TDS
29  * to be placed within different transports (TCP, DECNet, IPX/SPX).  A
30  * NETLIB packet starts with an eight byte header containing:
31  *
32  *      a one-byte packet type field;
33  *
34  *      a one-byte status field;
35  *
36  *      a two-byte big-endian size field giving the size of the packet,
37  *      including the header;
38  *
39  *      a two-byte big-endian channel number, used when multiple sessions
40  *      are being multiplexed on a single connection;
41  *
42  *      a one-byte packet number, giving "the frame number of a multiplexed
43  *      message, modulo 256";
44  *
45  *      a one-byte window, which is the number of frames to be sent
46  *      before an acknowledgment message is received.
47  *
48  * followed by payload whose size is the value in the size field minus
49  * 8.
50  *
51  * Microsoft Network Monitor 2.x dissects the 4 byte field (and indicates
52  * that the one-byte last packet indicator also contains other bits).
53  *
54  * The TDS protocol consists of a number of protocol data units (PDUs) that
55  * appear to be assembled from NETLIB packets, in the form of zero or more
56  * NETLIB packets with the last packet indicator clear and a final NETLIB
57  * packet with the last packet indicator set.  The type of the TDS PDU is
58  * specified by the packet type field of the NETLIB header (presumably that
59  * field has the same value for all NETLIB packets that make up a TDS PDU).
60  *
61  * The "server response" PDU consists of a sequence of multiple items, each
62  * one beginning with a one byte type field at the start of the PDU.  Some
63  * items are fixed length, some are variable length with a two byte size
64  * field following the item type, and then there is TDS_ROW_TOKEN in which
65  * size is determined by analyzing the result set returned from the server.
66  * This in effect means that we are hopelessly lost if we haven't seen the
67  * result set.  Also, TDS 4/5 is byte order negotiable, which is specified
68  * in the login packet.  We can attempt to determine it later on, but not
69  * with 100% accuracy.
70  *
71  * Some preliminary documentation on the packet format can be found at
72  * http://www.freetds.org/tds.html
73  *
74  * Some more information can be found in
75  * http://download.nai.com/products/media/sniffer/support/sdos/sybase.pdf
76  *
77  * Much of this code was originally developed for the FreeTDS project.
78  * http://www.freetds.org
79  */
80
81 /*
82  * Excerpts from Brian's posting to wireshark-dev:
83  *
84  * The TDS Protocol is actually a protocol within a protocol.  On the outside
85  * there is netlib which is not so much a encapsulation as a blocking of the
86  * data, typically to 512 or 4096 bytes.  Between this are the protocol data
87  * units for TDS.  Netlib packets may be split over real packets, multiple
88  * netlib packets may appear in single real packets.  TDS PDUs may be split
89  * over netlib packets (and real packets) and most certainly can appear
90  * multiple times within a netlib packet.
91  *
92  * Because of this, I abandoned my earlier attempt at making two dissectors,
93  * one for netlib and one for TDS. Counterintuitively, a single dissector
94  * turned out to be simpler than splitting it up.
95  *
96  * Here are some of the (hefty) limitations of the current code
97  *
98  * . We currently do not handle netlib headers that cross packet boundaries.
99  *   This should be an easy fix.
100  * . I probably could have used the packet reassembly stuff, but I started
101  *   this at version 0.8.20, so c'est la vie. It wouldn't have covered the
102  *   netlib stuff anyway, so no big loss.
103  * . The older two layer version of the code dissected the PDU's, but the new
104  *   version does not yet, it only labels the names. I need an elegant way to
105  *   deal with dissecting data crossing (netlib and tcp) packet boundries.  I
106  *   think I have one, but ran out of time to do it.
107  * . It will only work on little endian platforms.  Or rather I should say,
108  *   the client that was captured must be little endian.  TDS 7.0/8.0 is
109  *   always LE; for TDS 4.2/5.0 look in the code for tvb_get_le*() functions,
110  *   there are fields in the login packet which determine byte order.
111  * . result sets that span netlib packets are not working
112  * . TDS 7 and 4.2 result sets are not working yet
113  *
114  * All that said, the code does deal gracefully with different boudary
115  * conditions and what remains are the easier bits, IMHO.
116  *
117  * XXX - "real packets" means "TCP segments", for TCP.
118  *
119  * XXX - is it *REALLY* true that you can have more than one TDS PDU (as
120  * opposed to more than one server response item) per NETLIB packet?  Or is
121  * all the data in a NETLIB packet put into a single TDS PDU?  If so, then
122  * we can reassemble NETLIB packets using the standard TCP desegmentation
123  * code, and can reassemble TDS PDUs using "fragment_add_seq_check()",
124  * and more cleanly separate the NETLIB and TDS dissectors (although the
125  * "is this NETLIB" heuristic would have to look at TDS information past
126  * the NETLIB header, in order to make the heuristic strong enough not
127  * to get too many false positives; note that the heuristic should reject
128  * any putative NETLIB packet with a length field with a value < 8).
129  *
130  * That would substantially clean the dissector up, eliminating most of
131  * the per-packet data (we might still need information to handle
132  * TDS_ROW_TOKEN), getting rid of the stuff to handle data split across
133  * TCP segment boundaries in favor of simple reassembly code, and
134  * fixing some otherwise nasty-looking crashing bugs.
135  *
136  * NOTE: we assume that all the data in a NETLIB packet *can* be put into
137  * a single TDS PTU, so that we have separate reassembly of NETLIB
138  * packets and TDS PDUs; it seems to work, and it really did clean stuff
139  * up and fix crashes.
140  */
141
142 #ifdef HAVE_CONFIG_H
143 # include "config.h"
144 #endif
145
146 #include <stdio.h>
147 #include <stdlib.h>
148 #include <string.h>
149 #include <ctype.h>
150
151 #include <glib.h>
152
153 #include "isprint.h"
154
155 #include <epan/packet.h>
156 #include <epan/conversation.h>
157 #include <epan/strutil.h>
158
159 #include "packet-frame.h"
160 #include <epan/reassemble.h>
161 #include <epan/prefs.h>
162 #include <epan/emem.h>
163
164 #define TDS_QUERY_PKT        1
165 #define TDS_LOGIN_PKT        2
166 #define TDS_RPC_PKT          3
167 #define TDS_RESP_PKT         4
168 #define TDS_RAW_PKT          5
169 #define TDS_CANCEL_PKT       6
170 #define TDS_BULK_DATA_PKT    7
171 #define TDS_OPEN_CHN_PKT     8
172 #define TDS_CLOSE_CHN_PKT    9
173 #define TDS_RES_ERROR_PKT   10
174 #define TDS_LOG_CHN_ACK_PKT 11
175 #define TDS_ECHO_PKT        12
176 #define TDS_LOGOUT_CHN_PKT  13
177 #define TDS_QUERY5_PKT      15  /* or "Normal tokenized request or response */
178 #define TDS_LOGIN7_PKT      16  /* or "Urgent tokenized request or response */
179 #define TDS_NTLMAUTH_PKT    17
180 #define TDS_XXX7_PKT        18  /* seen in one capture */
181
182 #define is_valid_tds_type(x) ((x) >= TDS_QUERY_PKT && (x) <= TDS_XXX7_PKT)
183
184 /* The following constants are imported more or less directly from FreeTDS */
185 /*      Updated from FreeTDS v0.63 tds.h                                   */
186 /*         "$Id: tds.h,v 1.192 2004/10/28 12:42:12 freddy77]"              */
187 /* Note: [###] below means 'not defined in FreeTDS tds.h'                  */
188
189 #define TDS5_PARAMFMT2_TOKEN       32  /* 0x20    TDS 5.0 only              */
190 #define TDS_LANG_TOKEN             33  /* 0x21    TDS 5.0 only              */
191 #define TDS5_ORDERBY2_TOKEN        34  /* 0x22    TDS 5.0 only              */
192 #define TDS5_CURDECLARE2_TOKEN     35  /* 0x23    TDS 5.0 only        [###] */
193 #define TDS5_ROWFMT2_TOKEN         97  /* 0x61    TDS 5.0 only              */
194 #define TDS5_MSG_TOKEN            101  /* 0x65    TDS 5.0 only        [###] */
195 #define TDS_LOGOUT_TOKEN          113  /* 0x71    TDS 5.0 only? ct_close()  */
196 #define TDS_RET_STAT_TOKEN        121  /* 0x79                              */
197 #define TDS_PROCID_TOKEN          124  /* 0x7C    TDS 4.2 only - TDS_PROCID */
198 #define TDS_CURCLOSE_TOKEN        128  /* 0x80    TDS 5.0 only              */
199 #define TDS7_RESULT_TOKEN         129  /* 0x81    TDS 7.0 only              */
200 #define TDS_CURFETCH_TOKEN        130  /* 0x82    TDS 5.0 only              */
201 #define TDS_CURINFO_TOKEN         131  /* 0x83    TDS 5.0 only              */
202 #define TDS_CUROPEN_TOKEN         132  /* 0x84    TDS 5.0 only              */
203 #define TDS_CURDECLARE_TOKEN      134  /* 0x86    TDS 5.0 only              */
204 #define TDS7_COMPUTE_RESULT_TOKEN 136  /* 0x88    TDS 7.0 only              */
205 #define TDS_COL_NAME_TOKEN        160  /* 0xA0    TDS 4.2 only              */
206 #define TDS_COL_INFO_TOKEN        161  /* 0xA1    TDS 4.2 only - TDS_COLFMT */
207 #define TDS5_DYNAMIC2_TOKEN       163  /* 0xA3    TDS 5.0 only              */
208 #if 0 /* XX: Why commented out ? These are 'live' in FreeTDS tds.h */
209  #define TDS_TABNAME               164  /* 0xA4                              */
210  #define TDS_COL_INFO              165  /* 0xA5                              */
211 #endif
212 #define TDS_OPTIONCMD_TOKEN       166  /* 0xA6 */
213 #define TDS_COMPUTE_NAMES_TOKEN   167  /* 0xA7 */
214 #define TDS_COMPUTE_RESULT_TOKEN  168  /* 0xA8 */
215 #define TDS_ORDER_BY_TOKEN        169  /* 0xA9    TDS_ORDER                 */
216 #define TDS_ERR_TOKEN             170  /* 0xAA                              */
217 #define TDS_MSG_TOKEN             171  /* 0xAB                              */
218 #define TDS_PARAM_TOKEN           172  /* 0xAC    RETURNVALUE?              */
219 #define TDS_LOGIN_ACK_TOKEN       173  /* 0xAD                              */
220 #define TDS_CONTROL_TOKEN         174  /* 0xAE    TDS_CONTROL               */
221 #define TDS_KEY_TOKEN             202  /* 0xCA                        [###] */
222 #define TDS_ROW_TOKEN             209  /* 0xD1                              */
223 #define TDS_CMP_ROW_TOKEN         211  /* 0xD3                              */
224 #define TDS5_PARAMS_TOKEN         215  /* 0xD7    TDS 5.0 only              */
225 #define TDS_CAP_TOKEN             226  /* 0xE2                              */
226 #define TDS_ENV_CHG_TOKEN         227  /* 0xE3                              */
227 #define TDS_EED_TOKEN             229  /* 0xE5                              */
228 #define TDS_DBRPC_TOKEN           230  /* 0xE6                              */
229 #define TDS5_DYNAMIC_TOKEN        231  /* 0xE7    TDS 5.0 only              */
230 #define TDS5_PARAMFMT_TOKEN       236  /* 0xEC    TDS 5.0 only              */
231 #define TDS_AUTH_TOKEN            237  /* 0xED                              */
232 #define TDS_RESULT_TOKEN          238  /* 0xEE                              */
233 #define TDS_DONE_TOKEN            253  /* 0xFD    TDS_DONE                  */
234 #define TDS_DONEPROC_TOKEN        254  /* 0xFE    TDS_DONEPROC              */
235 #define TDS_DONEINPROC_TOKEN      255  /* 0xFF    TDS_DONEINPROC            */
236
237 /* Microsoft internal stored procedure id's */
238
239 #define TDS_SP_CURSOR           1
240 #define TDS_SP_CURSOROPEN       2
241 #define TDS_SP_CURSORPREPARE    3
242 #define TDS_SP_CURSOREXECUTE    4
243 #define TDS_SP_CURSORPREPEXEC   5
244 #define TDS_SP_CURSORUNPREPARE  6
245 #define TDS_SP_CURSORFETCH      7
246 #define TDS_SP_CURSOROPTION     8
247 #define TDS_SP_CURSORCLOSE      9
248 #define TDS_SP_EXECUTESQL      10
249 #define TDS_SP_PREPARE         11
250 #define TDS_SP_EXECUTE         12
251 #define TDS_SP_PREPEXEC        13
252 #define TDS_SP_PREPEXECRPC     14
253 #define TDS_SP_UNPREPARE       15
254
255 /* Sybase Data Types */
256
257 #define SYBCHAR        47  /* 0x2F */
258 #define SYBVARCHAR     39  /* 0x27 */
259 #define SYBINTN        38  /* 0x26 */
260 #define SYBINT1        48  /* 0x30 */
261 #define SYBINT2        52  /* 0x34 */
262 #define SYBINT4        56  /* 0x38 */
263 #define SYBINT8       127  /* 0x7F */
264 #define SYBFLT8        62  /* 0x3E */
265 #define SYBDATETIME    61  /* 0x3D */
266 #define SYBBIT         50  /* 0x32 */
267 #define SYBTEXT        35  /* 0x23 */
268 #define SYBNTEXT       99  /* 0x63 */
269 #define SYBIMAGE       34  /* 0x22 */
270 #define SYBMONEY4     122  /* 0x7A */
271 #define SYBMONEY       60  /* 0x3C */
272 #define SYBDATETIME4   58  /* 0x3A */
273 #define SYBREAL        59  /* 0x3B */
274 #define SYBBINARY      45  /* 0x2D */
275 #define SYBVOID        31  /* 0x1F */
276 #define SYBVARBINARY   37  /* 0x25 */
277 #define SYBNVARCHAR   103  /* 0x67 */
278 #define SYBBITN       104  /* 0x68 */
279 #define SYBNUMERIC    108  /* 0x6C */
280 #define SYBDECIMAL    106  /* 0x6A */
281 #define SYBFLTN       109  /* 0x6D */
282 #define SYBMONEYN     110  /* 0x6E */
283 #define SYBDATETIMN   111  /* 0x6F */
284 #define XSYBCHAR      175  /* 0xA7 */
285 #define XSYBVARCHAR   167  /* 0xAF */
286 #define XSYBNVARCHAR  231  /* 0xE7 */
287 #define XSYBNCHAR     239  /* 0xEF */
288 #define XSYBVARBINARY 165  /* 0xA5 */
289 #define XSYBBINARY    173  /* 0xAD */
290 #define SYBLONGBINARY 225  /* 0xE1 */
291 #define SYBSINT1       64  /* 0x40 */
292 #define SYBUINT2       65  /* 0x41 */
293 #define SYBUINT4       66  /* 0x42 */
294 #define SYBUINT8       67  /* 0x43 */
295 #define SYBUNIQUE      36  /* 0x24 */
296 #define SYBVARIANT     98  /* 0x62 */
297
298 #define is_fixed_coltype(x) (x==SYBINT1    || \
299                         x==SYBINT2      || \
300                         x==SYBINT4      || \
301                         x==SYBINT8      || \
302                         x==SYBREAL       || \
303                         x==SYBFLT8      || \
304                         x==SYBDATETIME  || \
305                         x==SYBDATETIME4 || \
306                         x==SYBBIT       || \
307                         x==SYBMONEY     || \
308                         x==SYBMONEY4    || \
309                         x==SYBUNIQUE)
310
311 /* Initialize the protocol and registered fields */
312 static int proto_tds = -1;
313 static int hf_tds_type = -1;
314 static int hf_tds_status = -1;
315 static int hf_tds_size = -1;
316 static int hf_tds_channel = -1;
317 static int hf_tds_packet_number = -1;
318 static int hf_tds_window = -1;
319 static int hf_tds_reassembled_in = -1;
320 static int hf_tds_fragments = -1;
321 static int hf_tds_fragment = -1;
322 static int hf_tds_fragment_overlap = -1;
323 static int hf_tds_fragment_overlap_conflict = -1;
324 static int hf_tds_fragment_multiple_tails = -1;
325 static int hf_tds_fragment_too_long_fragment = -1;
326 static int hf_tds_fragment_error = -1;
327
328 static int hf_tds7_login_total_size = -1;
329 static int hf_tds7_version = -1;
330 static int hf_tds7_packet_size = -1;
331 static int hf_tds7_client_version = -1;
332 static int hf_tds7_client_pid = -1;
333 static int hf_tds7_connection_id = -1;
334 static int hf_tds7_option_flags1 = -1;
335 static int hf_tds7_option_flags2 = -1;
336 static int hf_tds7_sql_type_flags = -1;
337 static int hf_tds7_reserved_flags = -1;
338 static int hf_tds7_time_zone = -1;
339 static int hf_tds7_collation = -1;
340 static int hf_tds7_message = -1;
341
342 /* Initialize the subtree pointers */
343 static gint ett_tds = -1;
344 static gint ett_tds_fragments = -1;
345 static gint ett_tds_fragment = -1;
346 static gint ett_tds_token = -1;
347 static gint ett_tds7_login = -1;
348 static gint ett_tds7_query = 0;
349 static gint ett_tds7_hdr = -1;
350
351 /* Desegmentation of Netlib buffers crossing TCP segment boundaries. */
352 static gboolean tds_desegment = TRUE;
353
354 static const fragment_items tds_frag_items = {
355         &ett_tds_fragment,
356         &ett_tds_fragments,
357         &hf_tds_fragments,
358         &hf_tds_fragment,
359         &hf_tds_fragment_overlap,
360         &hf_tds_fragment_overlap_conflict,
361         &hf_tds_fragment_multiple_tails,
362         &hf_tds_fragment_too_long_fragment,
363         &hf_tds_fragment_error,
364         &hf_tds_reassembled_in,
365         "fragments"
366 };
367
368 /* Tables for reassembly of fragments. */
369 static GHashTable *tds_fragment_table = NULL;
370 static GHashTable *tds_reassembled_table = NULL;
371
372 /* defragmentation of multi-buffer TDS PDUs */
373 static gboolean tds_defragment = TRUE;
374
375 static dissector_handle_t tds_tcp_handle;
376 static dissector_handle_t ntlmssp_handle;
377 static dissector_handle_t gssapi_handle;
378 static dissector_handle_t data_handle;
379
380 /* TDS protocol type preference */
381 /*   XXX: This preference is used as a 'hint' for cases where interpretation is ambiguous */
382 /*        Currently the hint is global                                                    */
383 /*   TODO: Consider storing protocol type with each conversation                          */
384 /*        (when type is determined and using the preference as a default) ??              */
385
386 #define TDS_PROTOCOL_NOT_SPECIFIED   0
387 #define TDS_PROTOCOL_4      4
388 #define TDS_PROTOCOL_5      5
389 #define TDS_PROTOCOL_7      7
390 #define TDS_PROTOCOL_8      8
391
392 static gint tds_protocol_type = TDS_PROTOCOL_NOT_SPECIFIED;
393
394 const enum_val_t tds_protocol_type_options[] = {
395   {"not_specified", "Not Specified", TDS_PROTOCOL_NOT_SPECIFIED},
396   {"tds4", "TDS 4", TDS_PROTOCOL_4},  /* TDS 4.2 and TDS 4.6 */
397   {"tds5", "TDS 5", TDS_PROTOCOL_5},
398   {"tds7", "TDS 7", TDS_PROTOCOL_7},
399   {"tds8", "TDS 8", TDS_PROTOCOL_8},
400   {NULL, NULL, -1}
401 };
402
403 #define TDS_PROTO_PREF_NOT_SPECIFIED (tds_protocol_type == TDS_NOT_SPECIFIED)
404 #define TDS_PROTO_PREF_TDS4 (tds_protocol_type == TDS_PROTOCOL_4)
405 #define TDS_PROTO_PREF_TDS5 (tds_protocol_type == TDS_PROTOCOL_5)
406 #define TDS_PROTO_PREF_TDS7 (tds_protocol_type == TDS_PROTOCOL_7)
407 #define TDS_PROTO_PREF_TDS8 (tds_protocol_type == TDS_PROTOCOL_8)
408 #define TDS_PROTO_PREF_TDS7_TDS8 ( TDS_PROTO_PREF_TDS7 || TDS_PROTO_PREF_TDS8 )
409
410 /* TDS "endian type" */
411 /*   XXX: Assumption is that all TDS conversations being decoded in a particular capture */
412 /*        have the same endian type                                                      */
413 /*   TODO: consider storing endian type with each conversation                           */
414 /*         (using pref as the default)                                                   */
415
416 static gint tds_little_endian = TRUE;
417
418 const enum_val_t tds_endian_type_options[] = {
419     {"little_endian", "Little Endian", TRUE},
420     {"big_endian"   , "Big Endian"   , FALSE},
421     {NULL, NULL, -1}
422 };
423
424
425 /* TCP port preferences for TDS decode */
426
427 static range_t *tds_tcp_ports = NULL;
428
429 /* These correspond to the netlib packet type field */
430 static const value_string packet_type_names[] = {
431         {TDS_QUERY_PKT,  "Query Packet"},
432         {TDS_LOGIN_PKT,  "Login Packet"},
433         {TDS_RPC_PKT,    "Remote Procedure Call Packet"},
434         {TDS_RESP_PKT,   "Response Packet"},
435         {TDS_CANCEL_PKT, "Cancel Packet"},
436         {TDS_QUERY5_PKT, "TDS5 Query Packet"},
437         {TDS_LOGIN7_PKT, "TDS7/8 Login Packet"},
438         {TDS_XXX7_PKT, "TDS7/8 0x12 Packet"},
439         {TDS_NTLMAUTH_PKT, "NT Authentication Packet"},
440         {0, NULL},
441 };
442
443 /* The status field */
444
445 #define is_valid_tds_status(x) ((x) <= STATUS_EVENT_NOTIFICATION)
446
447 #define STATUS_NOT_LAST_BUFFER          0x00
448 #define STATUS_LAST_BUFFER              0x01
449 #define STATUS_ATTN_REQUEST_ACK         0x02
450 #define STATUS_ATTN_REQUEST             0x03
451 #define STATUS_EVENT_NOTIFICATION       0x04
452
453 static const value_string status_names[] = {
454         {STATUS_NOT_LAST_BUFFER,    "Not last buffer"},
455         {STATUS_LAST_BUFFER,        "Last buffer in request or response"},
456         {STATUS_ATTN_REQUEST_ACK,   "Acknowledgment of last attention request"},
457         {STATUS_ATTN_REQUEST,       "Attention request"},
458         {STATUS_EVENT_NOTIFICATION, "Event notification"},
459         {0, NULL},
460 };
461
462 /* The one byte token at the start of each TDS PDU */
463 static const value_string token_names[] = {
464         {TDS5_DYNAMIC_TOKEN, "TDS5 Dynamic SQL"},
465         {TDS5_PARAMFMT_TOKEN, "TDS5 Parameter Format"},
466         {TDS5_PARAMFMT2_TOKEN, "TDS5 Parameter2 Format"},
467         {TDS5_PARAMS_TOKEN, "TDS5 Parameters"},
468         {TDS_LANG_TOKEN, "Language"},
469         {TDS_LOGOUT_TOKEN, "Logout"},
470         {TDS_RET_STAT_TOKEN, "Return Status"},
471         {TDS_PROCID_TOKEN, "Proc ID"},
472         {TDS7_RESULT_TOKEN, "TDS7+ Results"},
473         {TDS_COL_NAME_TOKEN, "Column Names"},
474         {TDS_COL_INFO_TOKEN, "Column Info"},
475         {TDS_COMPUTE_NAMES_TOKEN, "Compute Names"},
476         {TDS_COMPUTE_RESULT_TOKEN, "Compute Results"},
477         {TDS_ORDER_BY_TOKEN, "Order By"},
478         {TDS_ERR_TOKEN, "Error Message"},
479         {TDS_MSG_TOKEN, "Info Message"},
480         {TDS_PARAM_TOKEN, "Parameter"},
481         {TDS_LOGIN_ACK_TOKEN, "Login Acknowledgement"},
482         {TDS_CONTROL_TOKEN, "TDS Control"},
483         {TDS_KEY_TOKEN, "TDS Key"},
484         {TDS_ROW_TOKEN, "Row"},
485         {TDS_CMP_ROW_TOKEN, "Compute Row"},
486         {TDS_CAP_TOKEN, "Capabilities"},
487         {TDS_ENV_CHG_TOKEN, "Environment Change"},
488         {TDS_EED_TOKEN, "Extended Error"},
489         {TDS_AUTH_TOKEN, "Authentication"},
490         {TDS_RESULT_TOKEN, "Results"},
491         {TDS_DONE_TOKEN, "Done"},
492         {TDS_DONEPROC_TOKEN, "Done Proc"},
493         {TDS_DONEINPROC_TOKEN, "Done In Proc"},
494         {TDS5_DYNAMIC2_TOKEN, "TDS5 Dynamic2"},
495         {TDS5_ORDERBY2_TOKEN, "TDS5 OrderBy2"},
496         {TDS5_CURDECLARE2_TOKEN, "TDS5 CurDeclare2"},
497         {TDS5_ROWFMT2_TOKEN, "TDS5 RowFmt2"},
498         {TDS5_MSG_TOKEN, "TDS5 Msg"},
499         {0, NULL},
500 };
501
502
503 static const value_string internal_stored_proc_id_names[] = {
504     {TDS_SP_CURSOR,          "sp_cursor"         },
505     {TDS_SP_CURSOROPEN,      "sp_cursoropen"     },
506     {TDS_SP_CURSORPREPARE,   "sp_cursorprepare"  },
507     {TDS_SP_CURSOREXECUTE,   "sp_cursorexecute"  },
508     {TDS_SP_CURSORPREPEXEC,  "sp_cursorprepexec" },
509     {TDS_SP_CURSORUNPREPARE, "sp_cursorunprepare"},
510     {TDS_SP_CURSORFETCH,     "sp_cursorfetch"    },
511     {TDS_SP_CURSOROPTION,    "sp_cursoroption"   },
512     {TDS_SP_CURSORCLOSE,     "sp_cursorclose"    },
513     {TDS_SP_EXECUTESQL,      "sp_executesql"     },
514     {TDS_SP_PREPARE,         "sp_prepare"        },
515     {TDS_SP_EXECUTE,         "sp_execute"        },
516     {TDS_SP_PREPEXEC,        "sp_prepexec"       },
517     {TDS_SP_PREPEXECRPC,     "sp_prepexecrpc"    },
518     {TDS_SP_UNPREPARE,       "sp_unprepare"      },
519         {0,                      NULL                },
520 };
521
522 static const value_string env_chg_names[] = {
523         {1, "Database"},
524         {2, "Language"},
525         {3, "Sort Order"},
526         {4, "Blocksize"},
527         {5, "Unicode Locale ID"},
528         {6, "Unicode Comparison Style"},
529         {7, "Collation Info"},
530         {0, NULL},
531 };
532
533 static const value_string login_field_names[] = {
534         {0, "Client Name"},
535         {1, "Username"},
536         {2, "Password"},
537         {3, "App Name"},
538         {4, "Server Name"},
539         {5, "Unknown1"},
540         {6, "Library Name"},
541         {7, "Locale"},
542         {8, "Database Name"},
543         {0, NULL},
544 };
545
546
547 #define MAX_COLUMNS 256
548
549 /*
550  * This is where we store the column information to be used in decoding the
551  * TDS_ROW_TOKEN tokens.
552  */
553 struct _tds_col {
554      gchar name[256];
555      guint16 utype;
556      guint8 ctype;
557      guint csize;
558 };
559
560 struct _netlib_data {
561         guint num_cols;
562         struct _tds_col *columns[MAX_COLUMNS];
563 };
564
565 struct tds7_login_packet_hdr {
566         guint32 total_packet_size;
567         guint32 tds_version;
568         guint32 packet_size;
569         guint32 client_version;
570         guint32 client_pid;
571         guint32 connection_id;
572         guint8  option_flags1;
573         guint8  option_flags2;
574         guint8  sql_type_flags;
575         guint8  reserved_flags;
576         guint32 time_zone;
577         guint32 collation;
578 };
579
580 /* support routines */
581
582 static void
583 dissect_tds_nt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
584     guint offset, guint length)
585 {
586         tvbuff_t *nt_tvb;
587
588         nt_tvb = tvb_new_subset(tvb, offset, -1, length);
589         if(tvb_strneql(tvb, offset, "NTLMSSP", 7) == 0)
590                 call_dissector(ntlmssp_handle, nt_tvb, pinfo, tree);
591         else
592                 call_dissector(gssapi_handle, nt_tvb, pinfo, tree);
593 }
594
595 /*  */
596
597 static guint16
598 tds_tvb_get_xxtohs(tvbuff_t *tvb, gint offset, gint tds_little_endian) {
599     if (tds_little_endian)
600         return tvb_get_letohs(tvb, offset);
601     else
602         return tvb_get_ntohs(tvb, offset);
603 }
604
605 static guint32
606 tds_tvb_get_xxtohl(tvbuff_t *tvb, gint offset, gint tds_little_endian) {
607     if (tds_little_endian)
608         return tvb_get_letohl(tvb, offset);
609     else
610         return tvb_get_ntohl(tvb, offset);
611 }
612
613
614 static int tds_token_is_fixed_size(guint8 token)
615 {
616      switch (token) {
617           case TDS_DONE_TOKEN:
618           case TDS_DONEPROC_TOKEN:
619           case TDS_DONEINPROC_TOKEN:
620           case TDS_RET_STAT_TOKEN:
621           case TDS7_RESULT_TOKEN:
622           case TDS_PROCID_TOKEN:
623           case TDS_LOGOUT_TOKEN:
624                return 1;
625           default:
626                return 0;
627      }
628 }
629
630
631 static int tds_get_fixed_token_size(guint8 token)
632 {
633      switch(token) {
634           case TDS_DONE_TOKEN:
635           case TDS_DONEPROC_TOKEN:
636           case TDS_DONEINPROC_TOKEN:
637           case TDS_PROCID_TOKEN:
638                return 8;
639           case TDS_RET_STAT_TOKEN:
640                return 4;
641           case TDS_LOGOUT_TOKEN:
642                return 1;
643           case TDS7_RESULT_TOKEN:
644           default:
645                return 0;
646      }
647 }
648
649 static guint
650 tds_get_variable_token_size(tvbuff_t *tvb, gint offset, guint8 token,
651                             guint *len_field_size_p, guint *len_field_val_p)
652 {
653     switch(token) {
654         /* some tokens have a 4 byte length field */
655         case TDS5_PARAMFMT2_TOKEN:
656         case TDS_LANG_TOKEN:
657         case TDS5_ORDERBY2_TOKEN:
658         case TDS5_CURDECLARE2_TOKEN:
659         case TDS5_ROWFMT2_TOKEN:
660         case TDS5_DYNAMIC2_TOKEN:
661             *len_field_size_p = 4;
662             *len_field_val_p = tds_tvb_get_xxtohl(tvb, offset, tds_little_endian);
663             break;
664         /* some have a 1 byte length field */
665         case TDS5_MSG_TOKEN:
666             *len_field_size_p = 1;
667             *len_field_val_p = tvb_get_guint8(tvb, offset);
668             break;
669         /* and most have a 2 byte length field */
670         default:
671             *len_field_size_p = 2;
672             *len_field_val_p = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
673             break;
674     }
675     return *len_field_val_p + *len_field_size_p + 1;
676 }
677
678
679 static void
680 dissect_tds_query_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
681 {
682         guint offset, len;
683         gboolean is_unicode = TRUE;
684         char *msg;
685
686         proto_item *query_hdr;
687         proto_tree *query_tree;
688
689         offset = 0;
690         query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS Query Packet");
691         query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
692         len = tvb_reported_length_remaining(tvb, offset);
693
694         if (TDS_PROTO_PREF_TDS4 ||
695             (!TDS_PROTO_PREF_TDS7_TDS8 &&
696              ((len < 2) || tvb_get_guint8(tvb, offset+1) != 0)))
697                 is_unicode = FALSE;
698
699         if (is_unicode)
700                 msg = tvb_get_ephemeral_faked_unicode(tvb, offset, len/2, TRUE);
701         else
702                 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
703
704         proto_tree_add_text(query_tree, tvb, offset, len, "Query: %s", msg);
705         offset += len;
706 }
707
708
709 static void
710 dissect_tds5_lang_token(tvbuff_t *tvb, guint offset, guint len, proto_tree *tree) {
711     gboolean is_unicode = FALSE;
712     char *msg;
713
714     proto_tree_add_text(tree, tvb, offset, 1 , "Status: %u", tvb_get_guint8(tvb, offset));
715     offset += 1;
716     len    -= 1;
717
718     if (is_unicode)
719         msg = tvb_get_ephemeral_faked_unicode(tvb, offset, (len)/2, TRUE);
720     else
721         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
722
723     proto_tree_add_text(tree, tvb, offset, len, "Language text: %s", msg);
724 }
725
726 static void
727 dissect_tds_query5_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
728 {
729     guint offset;
730     guint pos;
731     guint token_len_field_size = 2;
732     guint token_len_field_val = 0;
733     guint8 token;
734     guint token_sz;
735     proto_item *query_hdr;
736     proto_tree *query_tree;
737     proto_item *token_item;
738     proto_tree *token_tree;
739
740     offset = 0;
741     query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS5 Query Packet");
742     query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
743
744     /*
745      * Until we reach the end of the packet, read tokens.
746      */
747     pos = offset;
748     while (tvb_reported_length_remaining(tvb, pos) > 0) {
749
750         /* our token */
751         token = tvb_get_guint8(tvb, pos);
752         if (tds_token_is_fixed_size(token))
753             token_sz = tds_get_fixed_token_size(token) + 1;
754         else
755             token_sz = tds_get_variable_token_size(tvb, pos+1, token, &token_len_field_size,
756                                                    &token_len_field_val);
757
758         /* XXX - Should this check be done in tds_get_variable_token_size()
759          * instead? */
760         if ((int) token_sz < 0) {
761             proto_tree_add_text(tree, tvb, 0, 0, "Bogus token size: %u",
762                 token_sz);
763             break;
764         }
765
766         token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
767                     "Token 0x%02x %s", token,
768                     val_to_str(token, token_names, "Unknown Token Type"));
769         token_tree = proto_item_add_subtree(token_item, ett_tds_token);
770
771         /*
772          * If it's a variable token, put the length field in here
773          * instead of replicating this for each token subdissector.
774          */
775         if (!tds_token_is_fixed_size(token))
776             proto_tree_add_text(token_tree, tvb, pos+1, token_len_field_size, "Length: %u", token_len_field_val);
777
778         switch (token) {
779             case TDS_LANG_TOKEN:
780                 dissect_tds5_lang_token(tvb, pos + 5, token_sz -5, token_tree);
781                 break;
782             default:
783                 break;
784         }
785
786         pos += token_sz;
787
788     }  /* while */
789 }
790
791
792 static void
793 dissect_tds7_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
794 {
795         guint offset, i, j, k, offset2, len;
796         char *val, *val2;
797
798         proto_item *login_hdr;
799         proto_tree *login_tree;
800         proto_item *header_hdr;
801         proto_tree *header_tree;
802         proto_item *length_hdr;
803         proto_tree *length_tree;
804
805         struct tds7_login_packet_hdr td7hdr;
806         gint length_remaining;
807
808
809         /* create display subtree for the protocol */
810         offset = 0;
811         login_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS7 Login Packet");
812         login_tree = proto_item_add_subtree(login_hdr, ett_tds7_login);
813         header_hdr = proto_tree_add_text(login_tree, tvb, offset, 36, "Login Packet Header");
814         header_tree = proto_item_add_subtree(header_hdr, ett_tds7_hdr);
815
816         td7hdr.total_packet_size = tvb_get_letohl(tvb, offset);
817         proto_tree_add_uint(header_tree, hf_tds7_login_total_size, tvb, offset,
818         sizeof(td7hdr.total_packet_size), td7hdr.total_packet_size);
819         offset += sizeof(td7hdr.total_packet_size);
820
821         td7hdr.tds_version = tvb_get_ntohl(tvb, offset);
822         proto_tree_add_uint(header_tree, hf_tds7_version, tvb, offset, sizeof(td7hdr.tds_version), td7hdr.tds_version);
823         offset += sizeof(td7hdr.tds_version);
824
825         td7hdr.packet_size = tvb_get_ntohl(tvb, offset);
826         proto_tree_add_uint(header_tree, hf_tds7_packet_size, tvb, offset, sizeof(td7hdr.packet_size), td7hdr.packet_size);
827         offset += sizeof(td7hdr.packet_size);
828
829         td7hdr.client_version = tvb_get_ntohl(tvb, offset);
830         proto_tree_add_uint(header_tree, hf_tds7_client_version, tvb, offset, sizeof(td7hdr.client_version), td7hdr.client_version);
831         offset += sizeof(td7hdr.client_version);
832
833         td7hdr.client_pid = tvb_get_letohl(tvb, offset);
834         proto_tree_add_uint(header_tree, hf_tds7_client_pid, tvb, offset, sizeof(td7hdr.client_pid), td7hdr.client_pid);
835         offset += sizeof(td7hdr.client_pid);
836
837         td7hdr.connection_id= tvb_get_letohl(tvb, offset);
838         proto_tree_add_uint(header_tree, hf_tds7_connection_id, tvb, offset, sizeof(td7hdr.connection_id), td7hdr.connection_id);
839         offset += sizeof(td7hdr.connection_id);
840
841         td7hdr.option_flags1 = tvb_get_guint8(tvb, offset);
842         proto_tree_add_uint(header_tree, hf_tds7_option_flags1, tvb, offset, sizeof(td7hdr.option_flags1), td7hdr.option_flags1);
843         offset += sizeof(td7hdr.option_flags1);
844
845         td7hdr.option_flags2 = tvb_get_guint8(tvb, offset);
846         proto_tree_add_uint(header_tree, hf_tds7_option_flags2, tvb, offset, sizeof(td7hdr.option_flags2), td7hdr.option_flags2);
847         offset += sizeof(td7hdr.option_flags2);
848
849         td7hdr.sql_type_flags = tvb_get_guint8(tvb, offset);
850         proto_tree_add_uint(header_tree, hf_tds7_sql_type_flags, tvb, offset, sizeof(td7hdr.sql_type_flags), td7hdr.sql_type_flags);
851         offset += sizeof(td7hdr.sql_type_flags);
852
853         td7hdr.reserved_flags = tvb_get_guint8(tvb, offset);
854         proto_tree_add_uint(header_tree, hf_tds7_reserved_flags, tvb, offset, sizeof(td7hdr.reserved_flags), td7hdr.reserved_flags);
855         offset += sizeof(td7hdr.reserved_flags);
856
857         td7hdr.time_zone = tvb_get_ntohl(tvb, offset);
858         proto_tree_add_uint(header_tree, hf_tds7_time_zone, tvb, offset, sizeof(td7hdr.time_zone), td7hdr.time_zone);
859         offset += sizeof(td7hdr.time_zone);
860
861         td7hdr.collation = tvb_get_ntohl(tvb, offset);
862         proto_tree_add_uint(header_tree, hf_tds7_collation, tvb, offset, sizeof(td7hdr.collation), td7hdr.collation);
863         offset += sizeof(td7hdr.collation);
864
865         length_hdr = proto_tree_add_text(login_tree, tvb, offset, 50, "Lengths and offsets");
866         length_tree = proto_item_add_subtree(length_hdr, ett_tds7_hdr);
867
868         for (i = 0; i < 9; i++) {
869                 offset2 = tvb_get_letohs(tvb, offset + i*4);
870                 len = tvb_get_letohs(tvb, offset + i*4 + 2);
871                 proto_tree_add_text(length_tree, tvb, offset + i*4, 2,
872                     "%s offset: %u",
873                     val_to_str(i, login_field_names, "Unknown"),
874                     offset2);
875                 proto_tree_add_text(length_tree, tvb, offset + i*4 + 2, 2,
876                         "%s length: %u",
877                         val_to_str(i, login_field_names, "Unknown"),
878                         len);
879                 if (len != 0) {
880                         if( i != 2) {
881                                 /* tds 7 is always unicode */
882                                 val = tvb_get_ephemeral_faked_unicode(tvb, offset2, len, TRUE);
883                                 len *= 2;
884                                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val);
885                         } else {
886                                 /* This field is the password.  We retrieve it from the packet
887                                  * as a non-unicode string and then perform two operations on it
888                                  * to "decrypt" it.  Finally, we create a new string that consists
889                                  * of ASCII characters instead of unicode by skipping every other
890                                  * byte in the original string.
891                                  */
892
893                                 len *= 2;
894                                 val = (gchar*)tvb_get_ephemeral_string(tvb, offset2, len);
895                                 val2 = g_malloc((len/2)+1);
896
897                                 for(j = 0, k = 0; j < len; j += 2, k++) {
898                                         val[j] ^= 0xA5;
899
900                                         /* Swap the most and least significant bits */
901                                         val[j] = ((val[j] & 0x0F) << 4) | ((val[j] & 0xF0) >> 4);
902
903                                         val2[k] = val[j];
904                                 }
905                                 val2[k] = '\0'; /* Null terminate our new string */
906
907                                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val2);
908                                 g_free(val2);
909                         }
910                 }
911         }
912
913         /*
914          * XXX - what about the client MAC address, etc.?
915          */
916         length_remaining = tvb_reported_length_remaining(tvb, offset2 + len);
917         if (length_remaining > 0) {
918                 dissect_tds_nt(tvb, pinfo, login_tree, offset2 + len,
919                     length_remaining);
920         }
921 }
922
923 static int get_size_by_coltype(int servertype)
924 {
925    switch(servertype)
926    {
927       case SYBINT1:        return 1;
928       case SYBINT2:        return 2;
929       case SYBINT4:        return 4;
930       case SYBINT8:        return 8;
931       case SYBREAL:        return 4;
932       case SYBFLT8:        return 8;
933       case SYBDATETIME:    return 8;
934       case SYBDATETIME4:   return 4;
935       case SYBBIT:         return 1;
936       case SYBBITN:        return 1;
937       case SYBMONEY:       return 8;
938       case SYBMONEY4:      return 4;
939       case SYBUNIQUE:      return 16;
940       default:             return -1;
941    }
942 }
943 # if 0
944 /*
945  * data_to_string should take column data and turn it into something we can
946  * display on the tree.
947  */
948 static char *data_to_string(void *data, guint col_type, guint col_size)
949 {
950    char *result;
951    guint i;
952
953    result=ep_alloc(256);
954    switch(col_type) {
955       case SYBVARCHAR:
956          /* strncpy(result, (char *)data, col_size); */
957          for (i=0;i<col_size && i<(256-1);i++)
958                 if (!isprint(((char *)data)[i])) result[i]='.';
959                 else result[i]=((char *)data)[i];
960          result[i] = '\0';
961          break;
962       case SYBINT2:
963          g_snprintf(result, 256, "%d", *(short *)data);
964          break;
965       case SYBINT4:
966          g_snprintf(result, 256, "%d", *(int *)data);
967          break;
968       default:
969          g_snprintf(result, 256, "Unexpected column_type %d", col_type);
970          break;
971    }
972    return result;
973 }
974 #endif
975
976 /*
977  * Since rows are special PDUs in that they are not fixed and lack a size field,
978  * the length must be computed using the column information seen in the result
979  * PDU. This function does just that.
980  */
981 static guint
982 tds_get_row_size(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
983 {
984         guint cur, i, csize;
985
986         cur = offset;
987         for (i = 0; i < nl_data->num_cols; i++) {
988                 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
989                         csize = tvb_get_guint8(tvb, cur);
990                         cur++;
991                 } else
992                         csize = get_size_by_coltype(nl_data->columns[i]->ctype);
993                 cur += csize;
994         }
995
996         return (cur - offset + 1);
997 }
998
999 /*
1000  * Process TDS 4 "COL_INFO" token and store relevant information in the
1001  * _netlib_data structure for later use (see tds_get_row_size)
1002  *
1003  * XXX Can TDS 4 be "big-endian" ? we'll assume yes.
1004  *
1005  */
1006 static gboolean
1007 dissect_tds_col_info_token(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
1008 {
1009     guint next, cur;
1010     guint col;
1011
1012     next = offset + tds_tvb_get_xxtohs(tvb, offset+1, tds_little_endian) + 3;
1013     cur = offset + 3;
1014
1015     col = 0;
1016     while (cur < next) {
1017
1018         if (col >= MAX_COLUMNS) {
1019             nl_data->num_cols = 0;
1020             return FALSE;
1021         }
1022
1023         nl_data->columns[col] = ep_alloc(sizeof(struct _tds_col));
1024
1025         nl_data->columns[col]->name[0] ='\0';
1026
1027         nl_data->columns[col]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1028         cur += 2;
1029
1030         cur += 2; /* unknown */
1031
1032         nl_data->columns[col]->ctype = tvb_get_guint8(tvb,cur);
1033         cur++;
1034
1035         if (!is_fixed_coltype(nl_data->columns[col]->ctype)) {
1036             nl_data->columns[col]->csize = tvb_get_guint8(tvb,cur);
1037             cur ++;
1038         } else {
1039             nl_data->columns[col]->csize =
1040                 get_size_by_coltype(nl_data->columns[col]->ctype);
1041         }
1042
1043         col += 1;
1044
1045     } /* while */
1046
1047     nl_data->num_cols = col;
1048     return TRUE;
1049 }
1050
1051
1052 /*
1053  * Read the results token and store the relevant information in the
1054  * _netlib_data structure for later use (see tds_get_row_size).
1055  *
1056  * TODO: check we don't go past end of the token
1057  */
1058 static gboolean
1059 read_results_tds5(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset, guint len _U_)
1060 {
1061         guint name_len;
1062         guint cur;
1063         guint i;
1064
1065         cur = offset;
1066
1067         /*
1068          * This would be the logical place to check for little/big endianess
1069          * if we didn't see the login packet.
1070          * XXX: We'll take a hint
1071          */
1072         nl_data->num_cols = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1073         if (nl_data->num_cols > MAX_COLUMNS) {
1074                 nl_data->num_cols = 0;
1075                 return FALSE;
1076         }
1077
1078         cur += 2;
1079
1080         for (i = 0; i < nl_data->num_cols; i++) {
1081                 nl_data->columns[i] = ep_alloc(sizeof(struct _tds_col));
1082                 name_len = tvb_get_guint8(tvb,cur);
1083                 cur ++;
1084                 cur += name_len;
1085
1086                 cur++; /* unknown */
1087
1088                 nl_data->columns[i]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1089                 cur += 2;
1090
1091                 cur += 2; /* unknown */
1092
1093                 nl_data->columns[i]->ctype = tvb_get_guint8(tvb,cur);
1094                 cur++;
1095
1096                 if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
1097                         nl_data->columns[i]->csize = tvb_get_guint8(tvb,cur);
1098                         cur ++;
1099                 } else {
1100                         nl_data->columns[i]->csize =
1101                             get_size_by_coltype(nl_data->columns[i]->ctype);
1102                 }
1103                 cur++; /* unknown */
1104         }
1105         return TRUE;
1106 }
1107
1108 /*
1109  * If the packet type from the netlib header is a login packet, then dig into
1110  * the packet to see if this is a supported TDS version and verify the otherwise
1111  * weak heuristics of the netlib check.
1112  */
1113 static gboolean
1114 netlib_check_login_pkt(tvbuff_t *tvb, guint offset, packet_info *pinfo, guint8 type)
1115 {
1116         guint tds_major, bytes_avail;
1117
1118         bytes_avail = tvb_length(tvb) - offset;
1119         /*
1120          * we have two login packet styles, one for TDS 4.2 and 5.0
1121          */
1122         if (type==TDS_LOGIN_PKT) {
1123                 /* Use major version number to validate TDS 4/5 login
1124                  * packet */
1125
1126                 /* Login packet is first in stream and should not be fragmented...
1127                  * if it is we are screwed */
1128                 if (bytes_avail < 467) return FALSE;
1129                 tds_major = tvb_get_guint8(tvb, 466);
1130                 if (tds_major != 4 && tds_major != 5) {
1131                         return FALSE;
1132                 }
1133         /*
1134          * and one added by Microsoft in SQL Server 7
1135          */
1136         } else if (type==TDS_LOGIN7_PKT) {
1137                 if (bytes_avail < 16) return FALSE;
1138                 tds_major = tvb_get_guint8(tvb, 15);
1139                 if (tds_major != 0x70 && tds_major != 0x80) {
1140                         return FALSE;
1141                 }
1142         } else if (type==TDS_QUERY5_PKT) {
1143                 if (bytes_avail < 9) return FALSE;
1144                 /* if this is a TDS 5.0 query check the token */
1145                 if (tvb_get_guint8(tvb, 8) != TDS_LANG_TOKEN) {
1146                         return FALSE;
1147                 }
1148         }
1149         /*
1150          * See if either tcp.destport or tcp.srcport is specified
1151          * in the preferences as being a TDS port.
1152          */
1153         else if (!value_is_in_range(tds_tcp_ports, pinfo->srcport) &&
1154                  !value_is_in_range(tds_tcp_ports, pinfo->destport)) {
1155                 return FALSE;
1156         }
1157
1158         return TRUE;
1159 }
1160
1161 static void
1162 dissect_tds_env_chg(tvbuff_t *tvb, guint offset, guint token_sz,
1163     proto_tree *tree)
1164 {
1165         guint8 env_type;
1166         guint old_len, new_len, old_len_offset;
1167         char *new_val = NULL, *old_val = NULL;
1168         guint32 string_offset;
1169         gboolean is_unicode = FALSE;
1170         guint16 collate_codepage, collate_flags;
1171         guint8 collate_charset_id;
1172
1173         env_type = tvb_get_guint8(tvb, offset);
1174         proto_tree_add_text(tree, tvb, offset, 1, "Type: %u (%s)", env_type,
1175                 val_to_str(env_type, env_chg_names, "Unknown"));
1176
1177         new_len = tvb_get_guint8(tvb, offset+1);
1178         old_len_offset = offset + new_len + 2;
1179         old_len = tvb_get_guint8(tvb, old_len_offset);
1180
1181         /*
1182          * If our lengths plus the lengths of the type and the lengths
1183          * don't add up to the token size, it must be UCS2.
1184          */
1185         if (old_len + new_len + 3 != token_sz) {
1186                 is_unicode = TRUE;
1187                 old_len_offset = offset + (new_len * 2) + 2;
1188                 old_len = tvb_get_guint8(tvb, old_len_offset);
1189         }
1190
1191         proto_tree_add_text(tree, tvb, offset + 1, 1, "New Value Length: %u",
1192             new_len);
1193         if (new_len) {
1194                 if (env_type != 7) { /* if it's not 'Collation Info - which is not textual! */
1195                         string_offset = offset + 2;
1196                         if (is_unicode == TRUE) {
1197                                 new_val = tvb_get_ephemeral_faked_unicode(tvb, string_offset,
1198                                         new_len, TRUE);
1199                                 new_len *= 2;
1200                         } else
1201                                 new_val = (gchar*)tvb_get_ephemeral_string(tvb, string_offset, new_len);
1202                         proto_tree_add_text(tree, tvb, string_offset, new_len,
1203                                 "New Value: %s", new_val);
1204                 }
1205                 else { /* parse collation info structure. From http://www.freetds.org/tds.html#collate */
1206                         offset +=2;
1207                         collate_codepage = tvb_get_letohs(tvb, offset);
1208                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1209                         offset += 2;
1210                         collate_flags = tvb_get_letohs(tvb, offset);
1211                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1212                         offset += 2;
1213                         collate_charset_id = tvb_get_guint8(tvb, offset);
1214                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1215                         offset +=1;
1216                 }
1217         }
1218
1219         proto_tree_add_text(tree, tvb, old_len_offset, 1, "Old Value Length: %u",
1220             old_len);
1221         if (old_len) {
1222                 string_offset = old_len_offset + 1;
1223                 if (is_unicode == TRUE) {
1224                         old_val = tvb_get_ephemeral_faked_unicode(tvb, string_offset,
1225                             old_len, TRUE);
1226                         old_len *= 2;
1227                 } else
1228                         old_val = (gchar*)tvb_get_ephemeral_string(tvb, string_offset, old_len);
1229                 proto_tree_add_text(tree, tvb, string_offset, old_len,
1230                     "Old Value: %s", old_val);
1231          }
1232 }
1233
1234 static void
1235 dissect_tds_err_token(tvbuff_t *tvb, guint offset, guint token_sz _U_, proto_tree *tree)
1236 {
1237         guint16 msg_len;
1238         guint8 srvr_len, proc_len;
1239         char *msg;
1240         gboolean is_unicode = FALSE;
1241
1242         proto_tree_add_text(tree, tvb, offset, 4, "SQL Error Number: %d", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1243         offset += 4;
1244         proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
1245         offset +=1;
1246         proto_tree_add_text(tree, tvb, offset, 1, "Severity Level: %u", tvb_get_guint8(tvb, offset));
1247         offset +=1;
1248
1249         msg_len = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
1250         proto_tree_add_text(tree, tvb, offset, 1, "Error message length: %u characters", msg_len);
1251         offset +=2;
1252
1253         if(tvb_get_guint8(tvb, offset+1) == 0) /* FIXME: It's probably unicode, if the 2nd byte of the message is zero. It's not a good detection method, but it works */
1254                 is_unicode = TRUE;
1255
1256         if(is_unicode) {
1257                 msg = tvb_get_ephemeral_faked_unicode(tvb, offset, msg_len, TRUE);
1258                 msg_len *= 2;
1259         } else {
1260                 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, msg_len);
1261         }
1262         proto_tree_add_text(tree, tvb, offset, msg_len, "Error: %s", format_text((guchar*)msg, strlen(msg)));
1263         offset += msg_len;
1264
1265         srvr_len = tvb_get_guint8(tvb, offset);
1266
1267         proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
1268         offset +=1;
1269         if(srvr_len) {
1270                 if (is_unicode) {
1271                         msg = tvb_get_ephemeral_faked_unicode(tvb, offset, srvr_len, TRUE);
1272                         srvr_len *=2;
1273                 } else {
1274                         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, srvr_len);
1275                 }
1276                 proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
1277                 offset += srvr_len;
1278         }
1279
1280         proc_len = tvb_get_guint8(tvb, offset);
1281
1282         proto_tree_add_text(tree, tvb, offset, 1, "Process name length: %u characters", proc_len);
1283         offset +=1;
1284         if(proc_len) {
1285                 if (is_unicode) {
1286                         msg = tvb_get_ephemeral_faked_unicode(tvb, offset, proc_len, TRUE);
1287                         proc_len *=2;
1288                 } else {
1289                         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, proc_len);
1290                 }
1291                 proto_tree_add_text(tree, tvb, offset, proc_len, "Process name: %s", msg);
1292                 offset += proc_len;
1293         }
1294
1295         proto_tree_add_text(tree, tvb, offset, 2, "line number: %d", tds_tvb_get_xxtohs(tvb, offset, tds_little_endian));
1296 }
1297
1298 static void
1299 dissect_tds_login_ack_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
1300 {
1301         guint8 msg_len;
1302         char *msg;
1303         gboolean is_unicode = FALSE;
1304
1305         proto_tree_add_text(tree, tvb, offset, 1, "Ack: %u", tvb_get_guint8(tvb, offset));
1306         offset +=1;
1307         proto_tree_add_text(tree, tvb, offset, 1, "Major version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1308         offset +=1;
1309         proto_tree_add_text(tree, tvb, offset, 1, "Minor version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1310         offset +=1;
1311         proto_tree_add_text(tree, tvb, offset, 2, "zero usually");
1312         offset +=2;
1313
1314         msg_len = tvb_get_guint8(tvb, offset);
1315         proto_tree_add_text(tree, tvb, offset, 1, "Text length: %u characters", msg_len);
1316         offset +=1;
1317
1318         if(msg_len + 6U + 3U != token_sz - 1) /* 6 is the length of ack(1), version (4), text length (1) fields */
1319                 is_unicode = TRUE;
1320         proto_tree_add_text(tree, tvb, offset, 0, "msg_len: %d, token_sz: %d, total: %d",msg_len, token_sz, msg_len + 6U + 3U);
1321         if(is_unicode) {
1322                 msg = tvb_get_ephemeral_faked_unicode(tvb, offset, msg_len, TRUE);
1323                 msg_len *= 2;
1324         } else {
1325                 msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, msg_len);
1326         }
1327         proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", format_text((guchar*)msg, strlen(msg)));
1328         offset += msg_len;
1329
1330         proto_tree_add_text(tree, tvb, offset, 4, "Server Version");
1331         offset += 4;
1332 }
1333
1334 static int
1335 dissect_tds7_results_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1336 {
1337         guint16 num_columns, table_len;
1338         guint8 type, msg_len;
1339         int i;
1340         char *msg;
1341         guint16 collate_codepage, collate_flags;
1342         guint8 collate_charset_id;
1343
1344         num_columns = tvb_get_letohs(tvb, offset);
1345         proto_tree_add_text(tree, tvb, offset, 2, "Columns: %u", tvb_get_letohs(tvb, offset));
1346         offset +=2;
1347         for(i=0; i != num_columns; i++) {
1348                 proto_tree_add_text(tree, tvb, offset, 0, "Column %d", i + 1);
1349                 proto_tree_add_text(tree, tvb, offset, 2, "usertype: %d", tvb_get_letohs(tvb, offset));
1350                 offset +=2;
1351                 proto_tree_add_text(tree, tvb, offset, 2, "flags: %d", tvb_get_letohs(tvb, offset));
1352                 offset +=2;
1353                 type  = tvb_get_guint8(tvb, offset);
1354                 proto_tree_add_text(tree, tvb, offset, 1, "Type: %d", type);
1355                 offset +=1;
1356                 if(type == 38 || type == 104 || type == 109 || type == 111) { /* ugly, ugly hack. Wish I knew what it really means!*/
1357                         proto_tree_add_text(tree, tvb, offset, 1, "unknown 1 byte (%x)", tvb_get_guint8(tvb, offset));
1358                         offset +=1;
1359                 }
1360                 else if (type == 35) {
1361                         proto_tree_add_text(tree, tvb, offset, 4, "unknown 4 bytes (%x)", tvb_get_letohl(tvb, offset));
1362                         offset += 4;
1363                         collate_codepage = tvb_get_letohs(tvb, offset);
1364                         proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1365                         offset += 2;
1366                         collate_flags = tvb_get_letohs(tvb, offset);
1367                         proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1368                         offset += 2;
1369                         collate_charset_id = tvb_get_guint8(tvb, offset);
1370                         proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1371                         offset +=1;
1372                         table_len = tvb_get_letohs(tvb, offset);
1373                         offset +=2;
1374                         if(table_len != 0) {
1375                                 msg = tvb_get_ephemeral_faked_unicode(tvb, offset, table_len, TRUE);
1376                                 proto_tree_add_text(tree, tvb, offset, table_len*2, "Table name: %s", msg);
1377                                 offset += table_len*2;
1378                         }
1379                 }
1380                 else if (type == 106 || type == 108) {
1381                         proto_tree_add_text(tree, tvb, offset, 3, "unknown 3 bytes");
1382                         offset +=3;
1383                 }
1384                 else if(type > 128) {
1385                         proto_tree_add_text(tree, tvb, offset, 2, "Large type size: 0x%x", tvb_get_letohs(tvb, offset));
1386                         offset += 2;
1387                         if (type != 165) {
1388                                 collate_codepage = tvb_get_letohs(tvb, offset);
1389                                 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1390                                 offset += 2;
1391                                 collate_flags = tvb_get_letohs(tvb, offset);
1392                                 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1393                                 offset += 2;
1394                                 collate_charset_id = tvb_get_guint8(tvb, offset);
1395                                 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1396                                 offset +=1;
1397                         }
1398                 }
1399                 msg_len = tvb_get_guint8(tvb, offset);
1400                 proto_tree_add_text(tree, tvb, offset, 1, "message length: %d",msg_len);
1401                 offset += 1;
1402                 if(msg_len != 0) {
1403                         msg = tvb_get_ephemeral_faked_unicode(tvb, offset, msg_len, TRUE);
1404                         proto_tree_add_text(tree, tvb, offset, msg_len*2, "Text: %s", msg);
1405                         offset += msg_len*2;
1406                 }
1407         }
1408         return offset;
1409 }
1410
1411 static void
1412 dissect_tds_done_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1413 {
1414         proto_tree_add_text(tree, tvb, offset, 2, "Status flags");
1415         offset += 2;
1416         proto_tree_add_text(tree, tvb, offset, 2, "Operation");
1417         offset += 2;
1418         proto_tree_add_text(tree, tvb, offset, 4, "row count: %u", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1419         offset += 2;
1420 }
1421
1422 static void
1423 dissect_tds_rpc(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1424 {
1425         int offset = 0;
1426         guint len;
1427         guint16 sp_id;
1428         char *val;
1429
1430         /*
1431          * RPC name.
1432          */
1433         switch(tds_protocol_type) {
1434                 case TDS_PROTOCOL_4:
1435                         len = tvb_get_guint8(tvb, offset);
1436                         proto_tree_add_text(tree, tvb, offset, 1, "RPC Name Length: %u", len);
1437                         offset += 1;
1438                         val = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
1439                         proto_tree_add_text(tree, tvb, offset, len, "RPC Name: %s", val);
1440                         offset += len;
1441                         break;
1442
1443                 case TDS_PROTOCOL_7:
1444                 case TDS_PROTOCOL_8:
1445                 default:          /* unspecified: try as if TDS7/TDS8 */
1446                         len = tvb_get_letohs(tvb, offset);
1447                         proto_tree_add_text(tree, tvb, offset, 2, "RPC Name Length: %u", len);
1448                         offset += 2;
1449                         if (len == 0xFFFF) {
1450                                 sp_id = tvb_get_letohs(tvb, offset);
1451                                 proto_tree_add_text(tree, tvb, offset, 2, "RPC Stored Proc ID: %u (%s)",
1452                                     sp_id,
1453                                     val_to_str(sp_id, internal_stored_proc_id_names, "Unknown"));
1454                                 offset += 2;
1455                         }
1456                         else if (len != 0) {
1457                                 val = tvb_get_ephemeral_faked_unicode(tvb, offset, len, TRUE);
1458                                 len *= 2;
1459                                 proto_tree_add_text(tree, tvb, offset, len, "RPC Name: %s", val);
1460                                 offset += len;
1461                         }
1462                         break;
1463         }
1464         proto_tree_add_text(tree, tvb, offset, -1, "Params (not dissected)");
1465 }
1466
1467 static void
1468 dissect_tds_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1469 {
1470         int offset = 0;
1471         proto_item *token_item;
1472         proto_tree *token_tree;
1473         guint pos, token_sz = 0;
1474         guint token_len_field_size = 2;
1475         guint token_len_field_val = 0;
1476         guint8 token;
1477         struct _netlib_data nl_data;
1478         gint length_remaining;
1479
1480         memset(&nl_data, '\0', sizeof nl_data);
1481
1482         /*
1483          * Until we reach the end of the packet, read tokens.
1484          */
1485         pos = offset;
1486         while (tvb_reported_length_remaining(tvb, pos) > 0) {
1487                 /* our token */
1488                 token = tvb_get_guint8(tvb, pos);
1489
1490                 /* TODO Handle TDS_PARAMFMT, TDS_PARAMS [similar to TDS_RESULTS, TDS_ROW] */
1491                 if (tds_token_is_fixed_size(token)) {
1492                         token_sz = tds_get_fixed_token_size(token) + 1;
1493                 } else if (token == TDS_ROW_TOKEN) {
1494                         /*
1495                          * Rows are special; they have no size field and
1496                          * aren't fixed length.
1497                          */
1498                         token_sz = tds_get_row_size(tvb, &nl_data, pos + 1);
1499                 } else
1500                         token_sz = tds_get_variable_token_size(tvb, pos + 1,
1501                             token, &token_len_field_size, &token_len_field_val);
1502
1503                 length_remaining = tvb_ensure_length_remaining(tvb, pos);
1504
1505                 if ((int) token_sz < 0) {
1506                     proto_tree_add_text(tree, tvb, pos, 0, "Bogus token size: %u",
1507                         token_sz);
1508                     break;
1509                 }
1510                 if ((int) token_len_field_size < 0) {
1511                     proto_tree_add_text(tree, tvb, pos, 0, "Bogus token length field size: %u",
1512                         token_len_field_size);
1513                     break;
1514                 }
1515                 token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
1516                     "Token 0x%02x %s", token,
1517                     val_to_str(token, token_names, "Unknown Token Type"));
1518                 token_tree = proto_item_add_subtree(token_item, ett_tds_token);
1519
1520                 /*
1521                  * If it's a variable token, put the length field in here
1522                  * instead of replicating this for each token subdissector.
1523                  */
1524                 if (!tds_token_is_fixed_size(token) && token != TDS_ROW_TOKEN) {
1525                         proto_tree_add_text(token_tree, tvb, pos + 1,
1526                             token_len_field_size, "Length: %u",
1527                             token_len_field_val);
1528                 }
1529
1530                 if (token_sz > (guint)length_remaining)
1531                         token_sz = (guint)length_remaining;
1532
1533                 switch (token) {
1534
1535                 case TDS_COL_NAME_TOKEN:
1536                         /*
1537                          * TDS 4.2
1538                          * TODO dissect token to get "column names" to fill in _netlib_data
1539                          */
1540                         break;
1541
1542                 case TDS_COL_INFO_TOKEN:
1543                         /*
1544                          * TDS 4.2: get the column info
1545                          */
1546                         dissect_tds_col_info_token(tvb, &nl_data, pos);
1547                         break;
1548
1549                 case TDS_RESULT_TOKEN:
1550                         /*
1551                          * If it's a result token, we need to stash the
1552                          * column info.
1553                          */
1554                         read_results_tds5(tvb, &nl_data, pos + 3, token_sz - 3);
1555                         break;
1556
1557                 case TDS_ENV_CHG_TOKEN:
1558                         dissect_tds_env_chg(tvb, pos + 3, token_sz - 3, token_tree);
1559                         break;
1560
1561                 case TDS_AUTH_TOKEN:
1562                         dissect_tds_nt(tvb, pinfo, token_tree, pos + 3, token_sz - 3);
1563                         break;
1564                 case TDS_ERR_TOKEN:
1565                 case TDS_MSG_TOKEN:
1566                         dissect_tds_err_token(tvb, pos + 3, token_sz - 3, token_tree);
1567                         break;
1568
1569                 case TDS_DONE_TOKEN:
1570                 case TDS_DONEPROC_TOKEN:
1571                 case TDS_DONEINPROC_TOKEN:
1572                         dissect_tds_done_token(tvb, pos + 1, token_tree);
1573                         break;
1574                 case TDS_LOGIN_ACK_TOKEN:
1575                         dissect_tds_login_ack_token(tvb, pos + 3, token_sz - 3, token_tree);
1576                         break;
1577                 case TDS7_RESULT_TOKEN:
1578                         pos = (dissect_tds7_results_token(tvb, pos + 1, token_tree)-1);
1579                         break;
1580                 }
1581
1582                 /* and step to the end of the token, rinse, lather, repeat */
1583                 pos += token_sz;
1584         }
1585 }
1586
1587 static void
1588 dissect_netlib_buffer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1589 {
1590         int offset = 0;
1591         proto_item *tds_item = NULL;
1592         proto_tree *tds_tree = NULL;
1593         guint8 type;
1594         guint8 status;
1595         guint16 size;
1596         guint16 channel;
1597         guint8 packet_number;
1598         gboolean save_fragmented;
1599         int len;
1600         fragment_data *fd_head;
1601         tvbuff_t *next_tvb;
1602
1603         if (tree) {
1604                 /* create display subtree for the protocol */
1605                 tds_item = proto_tree_add_item(tree, proto_tds, tvb, offset, -1,
1606                     FALSE);
1607
1608                 tds_tree = proto_item_add_subtree(tds_item, ett_tds);
1609         }
1610         type = tvb_get_guint8(tvb, offset);
1611         if (tree) {
1612                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb, offset, 1,
1613                     type);
1614         }
1615         status = tvb_get_guint8(tvb, offset + 1);
1616         if (tree) {
1617                 proto_tree_add_uint(tds_tree, hf_tds_status, tvb, offset + 1, 1,
1618                     status);
1619         }
1620         size = tvb_get_ntohs(tvb, offset + 2);
1621         if (tree) {
1622                 proto_tree_add_uint(tds_tree, hf_tds_size, tvb, offset + 2, 2,
1623                         size);
1624         }
1625         channel = tvb_get_ntohs(tvb, offset + 4);
1626         if (tree) {
1627                 proto_tree_add_uint(tds_tree, hf_tds_channel, tvb, offset + 4, 2,
1628                         channel);
1629         }
1630         packet_number = tvb_get_guint8(tvb, offset + 6);
1631         if (tree) {
1632                 proto_tree_add_uint(tds_tree, hf_tds_packet_number, tvb, offset + 6, 1,
1633                         packet_number);
1634                 proto_tree_add_item(tds_tree, hf_tds_window, tvb, offset + 7, 1,
1635                         FALSE);
1636         }
1637         offset += 8;    /* skip Netlib header */
1638
1639         /*
1640          * Deal with fragmentation.
1641          *
1642          * TODO: handle case where netlib headers 'packet-number'.is always 0
1643          *       use fragment_add_seq_next in this case ?
1644          *
1645          */
1646         save_fragmented = pinfo->fragmented;
1647         if (tds_defragment &&
1648             (packet_number > 1 || status == STATUS_NOT_LAST_BUFFER)) {
1649                 if (status == STATUS_NOT_LAST_BUFFER) {
1650                         if (check_col(pinfo->cinfo, COL_INFO))
1651                                 col_append_str(pinfo->cinfo, COL_INFO,
1652                                     " (Not last buffer)");
1653                 }
1654                 len = tvb_reported_length_remaining(tvb, offset);
1655                 /*
1656                  * XXX - I've seen captures that start with a login
1657                  * packet with a sequence number of 2.
1658                  */
1659                 fd_head = fragment_add_seq_check(tvb, offset, pinfo, channel,
1660                     tds_fragment_table, tds_reassembled_table,
1661                     packet_number - 1, len, status == STATUS_NOT_LAST_BUFFER);
1662                 next_tvb = process_reassembled_data(tvb, offset, pinfo,
1663                     "Reassembled TDS", fd_head, &tds_frag_items, NULL,
1664                     tds_tree);
1665         } else {
1666                 /*
1667                  * If this isn't the last buffer, just show it as a fragment.
1668                  * (XXX - it'd be nice to dissect it if it's the first
1669                  * buffer, but we'd need to do reassembly in order to
1670                  * discover that.)
1671                  *
1672                  * If this is the last buffer, dissect it.
1673                  * (XXX - it'd be nice to show it as a fragment if it's part
1674                  * of a fragmented message, but we'd need to do reassembly
1675                  * in order to discover that.)
1676                  */
1677                 if (status == STATUS_NOT_LAST_BUFFER)
1678                         next_tvb = NULL;
1679                 else {
1680                         next_tvb = tvb_new_subset_remaining(tvb, offset);
1681                 }
1682         }
1683
1684         if (next_tvb != NULL) {
1685
1686                 switch (type) {
1687
1688                 case TDS_RPC_PKT:
1689                         dissect_tds_rpc(next_tvb, pinfo, tds_tree);
1690                         break;
1691
1692                 case TDS_RESP_PKT:
1693                         dissect_tds_resp(next_tvb, pinfo, tds_tree);
1694                         break;
1695
1696                 case TDS_LOGIN7_PKT:
1697                         dissect_tds7_login(next_tvb, pinfo, tds_tree);
1698                         break;
1699                 case TDS_QUERY_PKT:
1700                         dissect_tds_query_packet(next_tvb, pinfo, tds_tree);
1701                         break;
1702                 case TDS_QUERY5_PKT:
1703                         dissect_tds_query5_packet(next_tvb, pinfo, tds_tree);
1704                         break;
1705                 case TDS_NTLMAUTH_PKT:
1706                         dissect_tds_nt(next_tvb, pinfo, tds_tree, offset - 8, -1);
1707                         break;
1708                 default:
1709                         proto_tree_add_text(tds_tree, next_tvb, 0, -1,
1710                             "TDS Packet");
1711                         break;
1712                 }
1713         } else {
1714                 next_tvb = tvb_new_subset_remaining (tvb, offset);
1715                 call_dissector(data_handle, next_tvb, pinfo, tds_tree);
1716         }
1717 }
1718
1719 static void
1720 dissect_tds_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1721 {
1722         volatile gboolean first_time = TRUE;
1723         volatile int offset = 0;
1724         guint length_remaining;
1725         guint8 type;
1726         guint16 plen;
1727         guint length;
1728         tvbuff_t *volatile next_tvb;
1729         proto_item *tds_item = NULL;
1730         proto_tree *tds_tree = NULL;
1731
1732         while (tvb_reported_length_remaining(tvb, offset) != 0) {
1733                 length_remaining = tvb_ensure_length_remaining(tvb, offset);
1734
1735                 /*
1736                  * Can we do reassembly?
1737                  */
1738                 if (tds_desegment && pinfo->can_desegment) {
1739                         /*
1740                          * Yes - is the fixed-length part of the PDU
1741                          * split across segment boundaries?
1742                          */
1743                         if (length_remaining < 8) {
1744                                 /*
1745                                  * Yes.  Tell the TCP dissector where the
1746                                  * data for this message starts in the data
1747                                  * it handed us, and how many more bytes we
1748                                  * need, and return.
1749                                  */
1750                                 pinfo->desegment_offset = offset;
1751                                 pinfo->desegment_len = 8 - length_remaining;
1752                                 return;
1753                         }
1754                 }
1755
1756                 type = tvb_get_guint8(tvb, offset);
1757
1758                 /*
1759                  * Get the length of the PDU.
1760                  */
1761                 plen = tvb_get_ntohs(tvb, offset + 2);
1762                 if (plen < 8) {
1763                         /*
1764                          * The length is less than the header length.
1765                          * Put in the type, status, and length, and
1766                          * report the length as bogus.
1767                          */
1768                         if (tree) {
1769                                 /* create display subtree for the protocol */
1770                                 tds_item = proto_tree_add_item(tree, proto_tds,
1771                                     tvb, offset, -1, FALSE);
1772
1773                                 tds_tree = proto_item_add_subtree(tds_item,
1774                                     ett_tds);
1775                                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb,
1776                                     offset, 1, type);
1777                                 proto_tree_add_item(tds_tree, hf_tds_status,
1778                                     tvb, offset + 1, 1, FALSE);
1779                                 proto_tree_add_uint_format(tds_tree,
1780                                     hf_tds_size, tvb, offset + 2, 2, plen,
1781                                     "Size: %u (bogus, should be >= 8)", plen);
1782                         }
1783
1784                         /*
1785                          * Give up - we can't dissect any more of this
1786                          * data.
1787                          */
1788                         break;
1789                 }
1790
1791                 /*
1792                  * Can we do reassembly?
1793                  */
1794                 if (tds_desegment && pinfo->can_desegment) {
1795                         /*
1796                          * Yes - is the PDU split across segment boundaries?
1797                          */
1798                         if (length_remaining < plen) {
1799                                 /*
1800                                  * Yes.  Tell the TCP dissector where the
1801                                  * data for this message starts in the data
1802                                  * it handed us, and how many more bytes we
1803                                  * need, and return.
1804                                  */
1805                                 pinfo->desegment_offset = offset;
1806                                 pinfo->desegment_len = plen - length_remaining;
1807                                 return;
1808                         }
1809                 }
1810
1811                 if (first_time) {
1812                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDS");
1813
1814                         /*
1815                          * Set the packet description based on its TDS packet
1816                          * type.
1817                          */
1818                         if (check_col(pinfo->cinfo, COL_INFO)) {
1819                                 col_add_str(pinfo->cinfo, COL_INFO,
1820                                     val_to_str(type, packet_type_names,
1821                                       "Unknown Packet Type: %u"));
1822                         }
1823                         first_time = FALSE;
1824                 }
1825
1826                 /*
1827                  * Construct a tvbuff containing the amount of the payload
1828                  * we have available.  Make its reported length the amount
1829                  * of data in the PDU.
1830                  *
1831                  * XXX - if reassembly isn't enabled. the subdissector will
1832                  * throw a BoundsError exception, rather than a
1833                  * ReportedBoundsError exception.  We really want a tvbuff
1834                  * where the length is "length", the reported length is
1835                  * "plen", and the "if the snapshot length were infinite"
1836                  * length is the minimum of the reported length of the tvbuff
1837                  * handed to us and "plen", with a new type of exception
1838                  * thrown if the offset is within the reported length but
1839                  * beyond that third length, with that exception getting the
1840                  * "Unreassembled Packet" error.
1841                  */
1842                 length = length_remaining;
1843                 if (length > plen)
1844                         length = plen;
1845                 next_tvb = tvb_new_subset(tvb, offset, length, plen);
1846
1847                 /*
1848                  * Dissect the Netlib buffer.
1849                  *
1850                  * Catch the ReportedBoundsError exception; if this
1851                  * particular Netlib buffer happens to get a
1852                  * ReportedBoundsError exception, that doesn't mean
1853                  * that we should stop dissecting PDUs within this frame
1854                  * or chunk of reassembled data.
1855                  *
1856                  * If it gets a BoundsError, we can stop, as there's nothing
1857                  * more to see, so we just re-throw it.
1858                  */
1859                 TRY {
1860                         dissect_netlib_buffer(next_tvb, pinfo, tree);
1861                 }
1862                 CATCH(BoundsError) {
1863                         RETHROW;
1864                 }
1865                 CATCH(ReportedBoundsError) {
1866                         show_reported_bounds_error(tvb, pinfo, tree);
1867                 }
1868                 ENDTRY;
1869
1870                 /*
1871                  * Step to the next Netlib buffer.
1872                  */
1873                 offset += plen;
1874         }
1875 }
1876
1877 static gboolean
1878 dissect_tds_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1879 {
1880         int offset = 0;
1881         guint8 type;
1882         guint8 status;
1883         guint16 plen;
1884         conversation_t *conv;
1885
1886         /*
1887          * If we don't have even enough data for a Netlib header,
1888          * just say it's not TDS.
1889          */
1890         if (tvb_length(tvb) < 8)
1891                 return FALSE;
1892
1893         /*
1894          * Quickly scan all the data we have in order to see if
1895          * everything in it looks like Netlib traffic.
1896          */
1897         while (tvb_bytes_exist(tvb, offset, 1)) {
1898                 /*
1899                  * Check the type field.
1900                  */
1901                 type = tvb_get_guint8(tvb, offset);
1902                 if (!is_valid_tds_type(type))
1903                         return FALSE;
1904
1905                 /*
1906                  * Check the status field, if it's present.
1907                  */
1908                 if (!tvb_bytes_exist(tvb, offset + 1, 1))
1909                         break;
1910                 status = tvb_get_guint8(tvb, offset + 1);
1911                 if (!is_valid_tds_status(status))
1912                         return FALSE;
1913
1914                 /*
1915                  * Get the length of the PDU.
1916                  */
1917                 if (!tvb_bytes_exist(tvb, offset + 2, 2))
1918                         break;
1919                 plen = tvb_get_ntohs(tvb, offset + 2);
1920                 if (plen < 8) {
1921                         /*
1922                          * The length is less than the header length.
1923                          * That's bogus.
1924                          */
1925                         return FALSE;
1926                 }
1927
1928                 /*
1929                  * If we're at the beginning of the segment, check the
1930                  * payload if it's a login packet.
1931                  */
1932                 if (offset == 0) {
1933                         if (!netlib_check_login_pkt(tvb, offset, pinfo, type))
1934                                 return FALSE;
1935                 }
1936
1937                 /*
1938                  * Step to the next Netlib buffer.
1939                  */
1940                 offset += plen;
1941         }
1942
1943         /*
1944          * OK, it passes the test; assume the rest of this conversation
1945          * is TDS.
1946          */
1947         conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
1948             pinfo->srcport, pinfo->destport, 0);
1949         if (conv == NULL) {
1950                 /*
1951                  * No conversation exists yet - create one.
1952                  */
1953                 conv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst,
1954                     pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
1955         }
1956         conversation_set_dissector(conv, tds_tcp_handle);
1957
1958         /*
1959          * Now dissect it as TDS.
1960          */
1961         dissect_tds_tcp(tvb, pinfo, tree);
1962         return TRUE;
1963 }
1964
1965 static void
1966 tds_init(void)
1967 {
1968         /*
1969          * Initialize the fragment and reassembly tables.
1970          */
1971         fragment_table_init(&tds_fragment_table);
1972         reassembled_table_init(&tds_reassembled_table);
1973
1974 }
1975
1976 /* Register the protocol with Wireshark */
1977
1978 /* this format is required because a script is used to build the C function
1979    that calls all the protocol registration.
1980 */
1981
1982 void
1983 proto_register_tds(void)
1984 {
1985         static hf_register_info hf[] = {
1986                 { &hf_tds_type,
1987                         { "Type",           "tds.type",
1988                         FT_UINT8, BASE_HEX, VALS(packet_type_names), 0x0,
1989                         "Packet Type", HFILL }
1990                 },
1991                 { &hf_tds_status,
1992                         { "Status",         "tds.status",
1993                         FT_UINT8, BASE_DEC, VALS(status_names), 0x0,
1994                         "Frame status", HFILL }
1995                 },
1996                 { &hf_tds_size,
1997                         { "Size",           "tds.size",
1998                         FT_UINT16, BASE_DEC, NULL, 0x0,
1999                         "Packet Size", HFILL }
2000                 },
2001                 { &hf_tds_channel,
2002                         { "Channel",        "tds.channel",
2003                         FT_UINT16, BASE_DEC, NULL, 0x0,
2004                         "Channel Number", HFILL }
2005                 },
2006                 { &hf_tds_packet_number,
2007                         { "Packet Number",  "tds.packet_number",
2008                         FT_UINT8, BASE_DEC, NULL, 0x0,
2009                         NULL, HFILL }
2010                 },
2011                 { &hf_tds_window,
2012                         { "Window",         "tds.window",
2013                         FT_UINT8, BASE_DEC, NULL, 0x0,
2014                         NULL, HFILL }
2015                 },
2016                 { &hf_tds_fragment_overlap,
2017                         { "Segment overlap",    "tds.fragment.overlap",
2018                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2019                         "Fragment overlaps with other fragments", HFILL }
2020                 },
2021                 { &hf_tds_fragment_overlap_conflict,
2022                         { "Conflicting data in fragment overlap", "tds.fragment.overlap.conflict",
2023                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2024                         "Overlapping fragments contained conflicting data", HFILL }
2025                 },
2026                 { &hf_tds_fragment_multiple_tails,
2027                         { "Multiple tail fragments found", "tds.fragment.multipletails",
2028                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2029                         "Several tails were found when defragmenting the packet", HFILL }
2030                 },
2031                 { &hf_tds_fragment_too_long_fragment,
2032                         { "Segment too long",   "tds.fragment.toolongfragment",
2033                         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2034                         "Segment contained data past end of packet", HFILL }
2035                 },
2036                 { &hf_tds_fragment_error,
2037                         { "Defragmentation error",      "tds.fragment.error",
2038                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2039                         "Defragmentation error due to illegal fragments", HFILL }
2040                 },
2041                 { &hf_tds_fragment,
2042                         { "TDS Fragment",       "tds.fragment",
2043                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2044                         NULL, HFILL }
2045                 },
2046                 { &hf_tds_fragments,
2047                         { "TDS Fragments",      "tds.fragments",
2048                         FT_NONE, BASE_NONE, NULL, 0x0,
2049                         NULL, HFILL }
2050                 },
2051                 { &hf_tds_reassembled_in,
2052                         { "Reassembled TDS in frame", "tds.reassembled_in",
2053                         FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2054                         "This TDS packet is reassembled in this frame", HFILL }
2055                 },
2056                 { &hf_tds7_login_total_size,
2057                         { "Total Packet Length", "tds7login.total_len",
2058                         FT_UINT32, BASE_DEC, NULL, 0x0,
2059                         "TDS7 Login Packet total packet length", HFILL }
2060                 },
2061                 { &hf_tds7_version,
2062                         { "TDS version", "tds7login.version",
2063                         FT_UINT32, BASE_HEX, NULL, 0x0,
2064                         NULL, HFILL }
2065                 },
2066                 { &hf_tds7_packet_size,
2067                         { "Packet Size", "tds7login.packet_size",
2068                         FT_UINT32, BASE_DEC, NULL, 0x0,
2069                         "Packet size", HFILL }
2070                 },
2071                 { &hf_tds7_client_version,
2072                         { "Client version", "tds7login.client_version",
2073                         FT_UINT32, BASE_DEC, NULL, 0x0,
2074                         NULL, HFILL }
2075                 },
2076                 { &hf_tds7_client_pid,
2077                         { "Client PID", "tds7login.client_pid",
2078                         FT_UINT32, BASE_DEC, NULL, 0x0,
2079                         NULL, HFILL }
2080                 },
2081                 { &hf_tds7_connection_id,
2082                         { "Connection ID", "tds7login.connection_id",
2083                         FT_UINT32, BASE_DEC, NULL, 0x0,
2084                         NULL, HFILL }
2085                 },
2086                 { &hf_tds7_option_flags1,
2087                         { "Option Flags 1", "tds7login.option_flags1",
2088                         FT_UINT8, BASE_HEX, NULL, 0x0,
2089                         NULL, HFILL }
2090                 },
2091                 { &hf_tds7_option_flags2,
2092                         { "Option Flags 2", "tds7login.option_flags2",
2093                         FT_UINT8, BASE_HEX, NULL, 0x0,
2094                         NULL, HFILL }
2095                 },
2096                 { &hf_tds7_sql_type_flags,
2097                         { "SQL Type Flags", "tds7login.sql_type_flags",
2098                         FT_UINT8, BASE_HEX, NULL, 0x0,
2099                         NULL, HFILL }
2100                 },
2101                 { &hf_tds7_reserved_flags,
2102                         { "Reserved Flags", "tds7login.reserved_flags",
2103                         FT_UINT8, BASE_HEX, NULL, 0x0,
2104                         "reserved flags", HFILL }
2105                 },
2106                 { &hf_tds7_time_zone,
2107                         { "Time Zone", "tds7login.time_zone",
2108                         FT_UINT32, BASE_HEX, NULL, 0x0,
2109                         NULL, HFILL }
2110                 },
2111                 { &hf_tds7_collation,
2112                         { "Collation", "tds7login.collation",
2113                         FT_UINT32, BASE_HEX, NULL, 0x0,
2114                         NULL, HFILL }
2115                 },
2116                 { &hf_tds7_message,
2117                         { "Message", "tds7.message",
2118                         FT_STRING, BASE_NONE, NULL, 0x0,
2119                         NULL, HFILL }
2120                 },
2121         };
2122
2123         static gint *ett[] = {
2124                 &ett_tds,
2125                 &ett_tds_fragments,
2126                 &ett_tds_fragment,
2127                 &ett_tds_token,
2128                 &ett_tds7_login,
2129                 &ett_tds7_hdr,
2130         };
2131         module_t *tds_module;
2132
2133 /* Register the protocol name and description */
2134         proto_tds = proto_register_protocol("Tabular Data Stream",
2135             "TDS", "tds");
2136
2137 /* Required function calls to register the header fields and subtrees used */
2138         proto_register_field_array(proto_tds, hf, array_length(hf));
2139         proto_register_subtree_array(ett, array_length(ett));
2140
2141 /* Allow dissector to be found by name. */
2142         register_dissector("tds", dissect_tds_tcp, proto_tds);
2143
2144         tds_module = prefs_register_protocol(proto_tds, NULL);
2145         prefs_register_bool_preference(tds_module, "desegment_buffers",
2146             "Reassemble TDS buffers spanning multiple TCP segments",
2147             "Whether the TDS dissector should reassemble TDS buffers spanning multiple TCP segments. "
2148             "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
2149             &tds_desegment);
2150         prefs_register_bool_preference(tds_module, "defragment",
2151             "Reassemble fragmented TDS messages with multiple buffers",
2152             "Whether the TDS dissector should defragment messages spanning multiple Netlib buffers",
2153             &tds_defragment);
2154         prefs_register_enum_preference(tds_module, "protocol_type",
2155             "TDS Protocol Type",
2156             "Hint as to version of TDS protocol being decoded",
2157             &tds_protocol_type, tds_protocol_type_options, FALSE);
2158         prefs_register_enum_preference(tds_module, "endian_type",
2159             "TDS decode as",
2160             "Hint as to whether to decode TDS protocol as little-endian or big-endian. (TDS7/8 always decoded as little-endian)",
2161             &tds_little_endian, tds_endian_type_options, FALSE);
2162         prefs_register_range_preference(tds_module, "tcp_ports",
2163             "TDS TCP ports",
2164             "Additional TCP ports to decode as TDS",
2165             &tds_tcp_ports, 0xFFFF);
2166
2167         register_init_routine(tds_init);
2168 }
2169
2170 /* If this dissector uses sub-dissector registration add a registration routine.
2171    This format is required because a script is used to find these routines and
2172    create the code that calls these routines.
2173 */
2174 void
2175 proto_reg_handoff_tds(void)
2176 {
2177         tds_tcp_handle = create_dissector_handle(dissect_tds_tcp, proto_tds);
2178
2179         /* Initial TDS ports: MS SQL default ports */
2180         dissector_add("tcp.port", 1433, tds_tcp_handle);
2181         dissector_add("tcp.port", 2433, tds_tcp_handle);
2182
2183         heur_dissector_add("tcp", dissect_tds_tcp_heur, proto_tds);
2184
2185         ntlmssp_handle = find_dissector("ntlmssp");
2186         gssapi_handle = find_dissector("gssapi");
2187         data_handle = find_dissector("data");
2188 }