Fix a core dump causing the buildbot test menagerie to fail. It was caused by incorre...
[obnox/wireshark/wip.git] / epan / dissectors / packet-pgsql.c
1 /* packet-pgsql.c
2  * Routines for PostgreSQL v3 protocol dissection.
3  * <http://www.postgresql.org/docs/current/static/protocol.html>
4  * Copyright 2004 Abhijit Menon-Sen <ams@oryx.com>
5  *
6  * $Id $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/conversation.h>
34 #include <epan/prefs.h>
35
36 #include "packet-tcp.h"
37 #include "reassemble.h"
38
39
40 static int proto_pgsql = -1;
41 static int hf_frontend = -1;
42 static int hf_type = -1;
43 static int hf_length = -1;
44 static int hf_parameter_name = -1;
45 static int hf_parameter_value = -1;
46 static int hf_query = -1;
47 static int hf_authtype = -1;
48 static int hf_passwd = -1;
49 static int hf_salt = -1;
50 static int hf_statement = -1;
51 static int hf_portal = -1;
52 static int hf_tag = -1;
53 static int hf_status = -1;
54 static int hf_copydata = -1;
55 static int hf_error = -1;
56 static int hf_pid = -1;
57 static int hf_key = -1;
58 static int hf_condition = -1;
59 static int hf_text = -1;
60 static int hf_tableoid = -1;
61 static int hf_typeoid = -1;
62 static int hf_oid = -1;
63 static int hf_format = -1;
64 static int hf_val_name = -1;
65 static int hf_val_idx = -1;
66 static int hf_val_length = -1;
67 static int hf_val_data = -1;
68 static int hf_val_mod = -1;
69 static int hf_severity = -1;
70 static int hf_code = -1;
71 static int hf_message = -1;
72 static int hf_detail = -1;
73 static int hf_hint = -1;
74 static int hf_position = -1;
75 static int hf_where = -1;
76 static int hf_file = -1;
77 static int hf_line = -1;
78 static int hf_routine = -1;
79
80 static gint ett_pgsql = -1;
81 static gint ett_values = -1;
82
83 static guint pgsql_port = 5432;
84 static gboolean pgsql_desegment = TRUE;
85 static gboolean first_message = TRUE;
86
87 static void dissect_pgsql_fe_msg(guchar, guint32, guint32, tvbuff_t *, proto_tree *);
88 static void dissect_pgsql_be_msg(guchar, guint32, guint32, tvbuff_t *, proto_tree *);
89 static void dissect_pgsql_msg(tvbuff_t *, packet_info *, proto_tree *);
90 static void dissect_pgsql(tvbuff_t *, packet_info *, proto_tree *);
91 static char *identify(gboolean, guchar);
92 static guint pgsql_length(tvbuff_t *, int);
93
94 static const value_string auth_types[] = {
95     { 0, "Success" },
96     { 1, "Kerberos V4" },
97     { 2, "Kerberos V5" },
98     { 3, "Plaintext password" },
99     { 4, "crypt()ed password" },
100     { 5, "MD5 password" },
101     { 6, "SCM credentials" },
102     { 0, NULL }
103 };
104
105 static const value_string status_vals[] = {
106     { 'I', "Idle" },
107     { 'T', "In a transaction" },
108     { 'E', "In a failed transaction" },
109     { 0, NULL }
110 };
111
112 static const value_string format_vals[] = {
113     { 0, "Text" },
114     { 1, "Binary" },
115     { 0, NULL }
116 };
117
118
119 void
120 proto_reg_handoff_pgsql(void)
121 {
122     dissector_handle_t pgsql_handle;
123
124     pgsql_handle = create_dissector_handle(dissect_pgsql, proto_pgsql);
125     dissector_add("tcp.port", pgsql_port, pgsql_handle);
126 }
127
128
129 void
130 proto_register_pgsql(void)
131 {
132     static hf_register_info hf[] = {
133         { &hf_frontend,
134           { "Frontend", "pgsql.frontend", FT_BOOLEAN, BASE_NONE, NULL, 0,
135             "True for messages from the frontend, false otherwise.",
136             HFILL }
137         },
138         { &hf_type,
139           { "Type", "pgsql.type", FT_STRING, BASE_NONE, NULL, 0,
140             "A one-byte message type identifier.", HFILL }
141         },
142         { &hf_length,
143           { "Length", "pgsql.length", FT_UINT32, BASE_DEC, NULL, 0,
144             "The length of the message (not including the type).",
145             HFILL }
146         },
147         { &hf_parameter_name,
148           { "Parameter name", "pgsql.parameter_name", FT_STRINGZ,
149             BASE_NONE, NULL, 0, "The name of a database parameter.",
150             HFILL }
151         },
152         { &hf_parameter_value,
153           { "Parameter value", "pgsql.parameter_value", FT_STRINGZ,
154             BASE_NONE, NULL, 0, "The value of a database parameter.",
155             HFILL }
156         },
157         { &hf_query,
158           { "Query", "pgsql.query", FT_STRINGZ, BASE_NONE, NULL, 0,
159             "A query string.", HFILL }
160         },
161         { &hf_passwd,
162           { "Password", "pgsql.password", FT_STRINGZ, BASE_NONE, NULL, 0,
163             "A password.", HFILL }
164         },
165         { &hf_authtype,
166           { "Authentication type", "pgsql.authtype", FT_INT32, BASE_DEC,
167             VALS(auth_types), 0,
168             "The type of authentication requested by the backend.", HFILL }
169         },
170         { &hf_salt,
171           { "Salt value", "pgsql.salt", FT_BYTES, BASE_HEX, NULL, 0,
172             "The salt to use while encrypting a password.", HFILL }
173         },
174         { &hf_statement,
175           { "Statement", "pgsql.statement", FT_STRINGZ, BASE_NONE, NULL, 0,
176             "The name of a prepared statement.", HFILL }
177         },
178         { &hf_portal,
179           { "Portal", "pgsql.portal", FT_STRINGZ, BASE_NONE, NULL, 0,
180             "The name of a portal.", HFILL }
181         },
182         { &hf_tag,
183           { "Tag", "pgsql.tag", FT_STRINGZ, BASE_NONE, NULL, 0,
184             "A completion tag.", HFILL }
185         },
186         { &hf_status,
187           { "Status", "pgsql.status", FT_UINT8, BASE_DEC, VALS(status_vals),
188             0, "The transaction status of the backend.", HFILL }
189         },
190         { &hf_copydata,
191           { "Copy data", "pgsql.copydata", FT_BYTES, BASE_NONE, NULL, 0,
192             "Data sent following a Copy-in or Copy-out response.", HFILL }
193         },
194         { &hf_error,
195           { "Error", "pgsql.error", FT_STRINGZ, BASE_NONE, NULL, 0,
196             "An error message.", HFILL }
197         },
198         { &hf_pid,
199           { "PID", "pgsql.pid", FT_UINT32, BASE_DEC, NULL, 0,
200             "The process ID of a backend.", HFILL }
201         },
202         { &hf_key,
203           { "Key", "pgsql.key", FT_UINT32, BASE_DEC, NULL, 0,
204             "The secret key used by a particular backend.", HFILL }
205         },
206         { &hf_condition,
207           { "Condition", "pgsql.condition", FT_STRINGZ, BASE_NONE, NULL, 0,
208             "The name of a NOTIFY condition.", HFILL }
209         },
210         { &hf_text,
211           { "Text", "pgsql.text", FT_STRINGZ, BASE_NONE, NULL, 0,
212             "Text from the backend.", HFILL }
213         },
214         { &hf_tableoid,
215           { "Table OID", "pgsql.oid.table", FT_UINT32, BASE_DEC, NULL, 0,
216             "The object identifier of a table.", HFILL }
217         },
218         { &hf_typeoid,
219           { "Type OID", "pgsql.oid.type", FT_UINT32, BASE_DEC, NULL, 0,
220             "The object identifier of a type.", HFILL }
221         },
222         { &hf_oid,
223           { "OID", "pgsql.oid", FT_UINT32, BASE_DEC, NULL, 0,
224             "An object identifier.", HFILL }
225         },
226         { &hf_format,
227           { "Format", "pgsql.format", FT_UINT16, BASE_DEC, VALS(format_vals),
228             0, "A format specifier.", HFILL }
229         },
230         { &hf_val_name,
231           { "Column name", "pgsql.col.name", FT_STRINGZ, BASE_NONE, NULL, 0,
232             "The name of a column.", HFILL }
233         },
234         { &hf_val_idx,
235           { "Column index", "pgsql.col.index", FT_UINT32, BASE_DEC, NULL, 0,
236             "The position of a column within a row.", HFILL }
237         },
238         { &hf_val_length,
239           { "Column length", "pgsql.val.length", FT_INT32, BASE_DEC, NULL, 0,
240             "The length of a parameter value, in bytes. -1 means NULL.",
241             HFILL }
242         },
243         { &hf_val_data,
244           { "Data", "pgsql.val.data", FT_BYTES, BASE_NONE, NULL, 0,
245             "Parameter data.", HFILL }
246         },
247         { &hf_val_mod,
248           { "Type modifier", "pgsql.col.typemod", FT_INT32, BASE_DEC, NULL, 0,
249             "The type modifier for a column.", HFILL }
250         },
251         { &hf_severity,
252           { "Severity", "pgsql.severity", FT_STRINGZ, BASE_NONE, NULL, 0,
253             "Message severity.", HFILL }
254         },
255         { &hf_code,
256           { "Code", "pgsql.code", FT_STRINGZ, BASE_NONE, NULL, 0,
257             "SQLState code.", HFILL }
258         },
259         { &hf_message,
260           { "Message", "pgsql.message", FT_STRINGZ, BASE_NONE, NULL, 0,
261             "Error message.", HFILL }
262         },
263         { &hf_detail,
264           { "Detail", "pgsql.detail", FT_STRINGZ, BASE_NONE, NULL, 0,
265             "Detailed error message.", HFILL }
266         },
267         { &hf_hint,
268           { "Hint", "pgsql.hint", FT_STRINGZ, BASE_NONE, NULL, 0,
269             "A suggestion to resolve an error.", HFILL }
270         },
271         { &hf_position,
272           { "Position", "pgsql.position", FT_STRINGZ, BASE_NONE, NULL, 0,
273             "The index of the error within the query string.", HFILL }
274         },
275         { &hf_where,
276           { "Context", "pgsql.where", FT_STRINGZ, BASE_NONE, NULL, 0,
277             "The context in which an error occurred.", HFILL }
278         },
279         { &hf_file,
280           { "File", "pgsql.file", FT_STRINGZ, BASE_NONE, NULL, 0,
281             "The source-code file where an error was reported.", HFILL }
282         },
283         { &hf_line,
284           { "Line", "pgsql.line", FT_STRINGZ, BASE_NONE, NULL, 0,
285             "The line number on which an error was reported.", HFILL }
286         },
287         { &hf_routine,
288           { "Routine", "pgsql.routine", FT_STRINGZ, BASE_NONE, NULL, 0,
289             "The routine that reported an error.", HFILL }
290         }
291     };
292
293     static gint *ett[] = {
294         &ett_pgsql,
295         &ett_values
296     };
297
298     module_t *mod_pgsql;
299
300     proto_pgsql = proto_register_protocol("PostgreSQL", "PGSQL", "pgsql");
301     proto_register_field_array(proto_pgsql, hf, array_length(hf));
302     proto_register_subtree_array(ett, array_length(ett));
303
304     mod_pgsql = prefs_register_protocol(proto_pgsql, NULL);
305     prefs_register_uint_preference(
306         mod_pgsql, "tcp.port", "PGSQL TCP port", "Set the port for PGSQL "
307         "messages (if different from the default of 5432)", 10, &pgsql_port
308     );
309 }
310
311
312 /* This function is called once per TCP packet. It sets COL_PROTOCOL and
313  * identifies FE/BE messages by adding a ">" or "<" to COL_INFO. Then it
314  * arranges for each message to be dissected individually. */
315
316 static void
317 dissect_pgsql(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
318 {
319     conversation_t *cv;
320
321     first_message = TRUE;
322
323     /* We don't use conversation data yet, but... */
324     cv = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
325                            pinfo->srcport, pinfo->destport, 0);
326     if (!cv) {
327         cv = conversation_new(&pinfo->src, &pinfo->dst, pinfo->ptype,
328                               pinfo->srcport, pinfo->destport, 0);
329     }
330
331     if (check_col(pinfo->cinfo, COL_PROTOCOL))
332         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PGSQL");
333     if (check_col(pinfo->cinfo, COL_INFO))
334         col_add_str(pinfo->cinfo, COL_INFO,
335                     (pinfo->match_port == pinfo->destport) ?
336                      ">" : "<");
337
338     tcp_dissect_pdus(tvb, pinfo, tree, pgsql_desegment, 5,
339                      pgsql_length, dissect_pgsql_msg);
340 }
341
342
343 /* This function is called by tcp_dissect_pdus() to find the size of the
344    message starting at tvb[offset]. */
345
346 static guint
347 pgsql_length(tvbuff_t *tvb, int offset)
348 {
349     gint n = 0;
350     guchar type;
351     guint32 length;
352
353     /* The length is either the four bytes after the type, or, if the
354        type is 0, the first four bytes. */
355     type = tvb_get_guint8(tvb, offset);
356     if (type != '\0')
357         n = 1;
358     length = tvb_get_ntohl(tvb, offset+n);
359     return length+n;
360 }
361
362
363 /* This function is responsible for dissecting a single message. */
364
365 static void
366 dissect_pgsql_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
367 {
368     proto_item *ti;
369     proto_tree *ptree;
370
371     gint n;
372     guchar type;
373     char *typestr;
374     guint32 length;
375     gboolean info = check_col(pinfo->cinfo, COL_INFO);
376     gboolean fe = (pinfo->match_port == pinfo->destport);
377
378     n = 0;
379     type = tvb_get_guint8(tvb, 0);
380     if (type != '\0')
381         n += 1;
382     length = tvb_get_ntohl(tvb, n);
383
384     /* There are a few frontend messages that have no leading type byte.
385        We identify them by the fact that the first byte of their length
386        must be zero, and that the next four bytes are a unique tag. */
387     if (fe && type == '\0') {
388         guint32 tag = tvb_get_ntohl(tvb, 4);
389
390         if (length == 16 && tag == 80877102)
391             typestr = "Cancel request";
392         else if (length == 8 && tag == 80877103)
393             typestr = "SSL request";
394         else if (tag == 196608)
395             typestr = "Startup message";
396         else
397             typestr = "Unknown";
398     }
399     else {
400         typestr = identify(fe, type);
401     }
402
403     if (info) {
404         /* This is a terrible hack. It makes the "Info" column reflect
405            the contents of every message in a TCP packet. Could it be
406            done any better? */
407         col_append_fstr(pinfo->cinfo, COL_INFO, "%s%c",
408                         ( first_message ? "" : "/" ), type);
409         first_message = FALSE;
410     }
411
412     if (tree) {
413         ti = proto_tree_add_item(tree, proto_pgsql, tvb, 0, -1, FALSE);
414         ptree = proto_item_add_subtree(ti, ett_pgsql);
415
416         n = 1;
417         if (type == '\0')
418             n = 0;
419         proto_tree_add_text(ptree, tvb, 0, n, "Type: %s", typestr);
420         proto_tree_add_item_hidden(ptree, hf_type, tvb, 0, n, FALSE);
421         proto_tree_add_item(ptree, hf_length, tvb, n, 4, FALSE);
422         proto_tree_add_boolean_hidden(ptree, hf_frontend, tvb, 0, 0, fe);
423         n += 4;
424
425         if (fe)
426             dissect_pgsql_fe_msg(type, n, length, tvb, ptree);
427         else
428             dissect_pgsql_be_msg(type, n, length, tvb, ptree);
429     }
430 }
431
432
433 static const value_string fe_messages[] = {
434     { 'p', "Password message" },
435     { 'Q', "Simple query" },
436     { 'P', "Parse" },
437     { 'B', "Bind" },
438     { 'E', "Execute" },
439     { 'D', "Describe" },
440     { 'C', "Close" },
441     { 'H', "Flush" },
442     { 'S', "Sync" },
443     { 'F', "Function call" },
444     { 'd', "Copy data" },
445     { 'c', "Copy completion" },
446     { 'f', "Copy failure" },
447     { 'X', "Termination" },
448     { 0, NULL }
449 };
450
451
452 static void dissect_pgsql_fe_msg(guchar type, guint32 n, guint32 length,
453                           tvbuff_t *tvb, proto_tree *tree)
454 {
455     guchar c;
456     char *s, *t;
457     guint32 i, l;
458     proto_item *ti;
459     proto_tree *shrub;
460
461     switch (type) {
462     /* Password */
463     case 'p':
464         s = tvb_get_stringz(tvb, n, &l);
465         proto_tree_add_string(tree, hf_passwd, tvb, n, l, s);
466         g_free(s);
467         break;
468
469     /* Simple query */
470     case 'Q':
471         s = tvb_get_stringz(tvb, n, &l);
472         proto_tree_add_string(tree, hf_query, tvb, n, l, s);
473         g_free(s);
474         break;
475
476     /* Parse */
477     case 'P':
478         s = tvb_get_stringz(tvb, n, &l);
479         proto_tree_add_string(tree, hf_statement, tvb, n, l, s);
480         g_free(s);
481         n += l;
482
483         s = tvb_get_stringz(tvb, n, &l);
484         proto_tree_add_string(tree, hf_query, tvb, n, l, s);
485         g_free(s);
486         n += l;
487
488         i = tvb_get_ntohs(tvb, n);
489         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameters: %d", i);
490         shrub = proto_item_add_subtree(ti, ett_values);
491         n += 2;
492         while (i-- > 0) {
493             proto_tree_add_item(shrub, hf_typeoid, tvb, n, 4, FALSE);
494             n += 4;
495         }
496         break;
497
498     /* Bind */
499     case 'B':
500         s = tvb_get_stringz(tvb, n, &l);
501         proto_tree_add_string(tree, hf_portal, tvb, n, l, s);
502         g_free(s);
503         n += l;
504
505         s = tvb_get_stringz(tvb, n, &l);
506         proto_tree_add_string(tree, hf_statement, tvb, n, l, s);
507         g_free(s);
508         n += l;
509
510         i = tvb_get_ntohs(tvb, n);
511         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter formats: %d", i);
512         shrub = proto_item_add_subtree(ti, ett_values);
513         n += 2;
514         while (i-- > 0) {
515             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
516             n += 2;
517         }
518
519         i = tvb_get_ntohs(tvb, n);
520         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter values: %d", i);
521         shrub = proto_item_add_subtree(ti, ett_values);
522         n += 2;
523         while (i-- > 0) {
524             l = tvb_get_ntohl(tvb, n);
525             proto_tree_add_int(shrub, hf_val_length, tvb, n, 4, l);
526             n += 4;
527             /* XXX - if we don't limit l here, the function will assert on very large values */
528             if (l > 0 && l < 1000000)
529                 proto_tree_add_item(shrub, hf_val_data, tvb, n, l, FALSE);
530             n += l;
531         }
532
533         i = tvb_get_ntohs(tvb, n);
534         ti = proto_tree_add_text(tree, tvb, n, 2, "Result formats: %d", i);
535         shrub = proto_item_add_subtree(ti, ett_values);
536         n += 2;
537         while (i-- > 0) {
538             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
539             n += 2;
540         }
541         break;
542
543     /* Execute */
544     case 'E':
545         s = tvb_get_stringz(tvb, n, &l);
546         proto_tree_add_string(tree, hf_portal, tvb, n, l, s);
547         g_free(s);
548         n += l;
549
550         ti = proto_tree_add_text(tree, tvb, n, 4, "Returns: ");
551         i = tvb_get_ntohl(tvb, n);
552         if (i == 0)
553             proto_item_append_text(ti, "all");
554         else
555             proto_item_append_text(ti, "%d", i);
556         proto_item_append_text(ti, " rows");
557         break;
558
559     /* Describe, Close */
560     case 'D':
561     case 'C':
562         i = 0;
563         c = tvb_get_guint8(tvb, n);
564         if (c == 'P')
565             i = hf_portal;
566         else
567             i = hf_statement;
568
569         if (i != 0) {
570             n += 1;
571             s = tvb_get_stringz(tvb, n, &l);
572             proto_tree_add_string_hidden(tree, i, tvb, n, l, s);
573             proto_tree_add_text(
574                 tree, tvb, n-1, l, "%s: %s",
575                 (c == 'P' ? "Portal" : "Statement"), s
576             );
577             g_free(s);
578         }
579         break;
580
581     /* Messages without a type identifier */
582     case '\0':
583         i = tvb_get_ntohl(tvb, n);
584         n += 4;
585         length -= n;
586         switch (i) {
587         /* Startup message */
588         case 196608:
589             while (length > 0) {
590                 s = tvb_get_stringz(tvb, n, &l);
591                 length -= l;
592                 if (length <= 0) {
593                     g_free(s);
594                     break;
595                 }
596                 t = tvb_get_stringz(tvb, n+l, &i);
597                 proto_tree_add_text(tree, tvb, n, l+i, "%s: %s", s, t);
598                 g_free(s);
599                 g_free(t);
600                 n += l+i;
601                 length -= i;
602                 if (length == 1 && tvb_get_guint8(tvb, n) == 0)
603                     break;
604             }
605             break;
606
607         /* SSL request */
608         case 80877103:
609             /* There's nothing to parse here, but what do we do if the
610                SSL negotiation succeeds? */
611             break;
612
613         /* Cancellation request */
614         case 80877102:
615             proto_tree_add_item(tree, hf_pid, tvb, n, 4, FALSE);
616             proto_tree_add_item(tree, hf_key, tvb, n+4, 4, FALSE);
617             break;
618         }
619         break;
620
621     /* Copy data */
622     case 'd':
623         proto_tree_add_item(tree, hf_copydata, tvb, n, length-n+1, FALSE);
624         break;
625
626     /* Copy failure */
627     case 'f':
628         s = tvb_get_stringz(tvb, n, &l);
629         proto_tree_add_string(tree, hf_error, tvb, n, l, s);
630         g_free(s);
631         break;
632
633     /* Function call */
634     case 'F':
635         proto_tree_add_item(tree, hf_oid, tvb, n, 4, FALSE);
636         n += 4;
637
638         i = tvb_get_ntohs(tvb, n);
639         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter formats: %d", i);
640         shrub = proto_item_add_subtree(ti, ett_values);
641         n += 2;
642         while (i-- > 0) {
643             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
644             n += 2;
645         }
646
647         i = tvb_get_ntohs(tvb, n);
648         ti = proto_tree_add_text(tree, tvb, n, 2, "Parameter values: %d", i);
649         shrub = proto_item_add_subtree(ti, ett_values);
650         n += 2;
651         while (i-- > 0) {
652             l = tvb_get_ntohl(tvb, n);
653             proto_tree_add_item(shrub, hf_val_length, tvb, n, 4, FALSE);
654             n += 4;
655             if (l > 0)
656                 proto_tree_add_item(shrub, hf_val_data, tvb, n, l, FALSE);
657             n += l;
658         }
659
660         proto_tree_add_item(tree, hf_format, tvb, n, 2, FALSE);
661         break;
662     }
663 }
664
665
666 static const value_string be_messages[] = {
667     { 'R', "Authentication request" },
668     { 'K', "Backend key data" },
669     { 'S', "Parameter status" },
670     { '1', "Parse completion" },
671     { '2', "Bind completion" },
672     { '3', "Close completion" },
673     { 'C', "Command completion" },
674     { 't', "Parameter description" },
675     { 'T', "Row description" },
676     { 'D', "Data row" },
677     { 'I', "Empty query" },
678     { 'n', "No data" },
679     { 'E', "Error" },
680     { 'N', "Notice" },
681     { 's', "Portal suspended" },
682     { 'Z', "Ready for query" },
683     { 'A', "Notification" },
684     { 'V', "Function call response" },
685     { 'G', "CopyIn response" },
686     { 'H', "CopyOut response" },
687     { 'd', "Copy data" },
688     { 'c', "Copy completion" },
689     { 0, NULL }
690 };
691
692
693 static void dissect_pgsql_be_msg(guchar type, guint32 n, guint32 length,
694                           tvbuff_t *tvb, proto_tree *tree)
695 {
696     guchar c;
697     char *s, *t;
698     guint32 i, l;
699     proto_item *ti;
700     proto_tree *shrub;
701
702     switch (type) {
703     /* Authentication request */
704     case 'R':
705         proto_tree_add_item(tree, hf_authtype, tvb, n, 4, FALSE);
706         i = tvb_get_ntohl(tvb, n);
707         if (i == 4 || i == 5) {
708             /* i -= (6-i); :-) */
709             n += 4;
710             l = (i == 4 ? 2 : 4);
711             proto_tree_add_item(tree, hf_salt, tvb, n, l, FALSE);
712         }
713         break;
714
715     /* Key data */
716     case 'K':
717         proto_tree_add_item(tree, hf_pid, tvb, n, 4, FALSE);
718         proto_tree_add_item(tree, hf_key, tvb, n+4, 4, FALSE);
719         break;
720
721     /* Parameter status */
722     case 'S':
723         s = tvb_get_stringz(tvb, n, &l);
724         proto_tree_add_string_hidden(tree, hf_parameter_name, tvb, n, l, s);
725         n += l;
726         t = tvb_get_stringz(tvb, n, &i);
727         proto_tree_add_string_hidden(tree, hf_parameter_value, tvb, n, i, t);
728         proto_tree_add_text(tree, tvb, n-l, l+i, "%s: %s", s, t);
729         g_free(s);
730         g_free(t);
731         break;
732
733     /* Parameter description */
734     case 't':
735         i = tvb_get_ntohs(tvb, n);
736         proto_tree_add_text(tree, tvb, n, 2, "Parameters: %d", i);
737         n += 2;
738         while (i-- > 0) {
739             proto_tree_add_item(tree, hf_typeoid, tvb, n, 4, FALSE);
740             n += 4;
741         }
742         break;
743
744     /* Row description */
745     case 'T':
746         i = tvb_get_ntohs(tvb, n);
747         ti = proto_tree_add_text(tree, tvb, n, 2, "Columns: %d", i);
748         shrub = proto_item_add_subtree(ti, ett_values);
749         n += 2;
750         while (i-- > 0) {
751             proto_tree *twig;
752             s = tvb_get_stringz(tvb, n, &l);
753             ti = proto_tree_add_string(shrub, hf_val_name, tvb, n, l, s);
754             twig = proto_item_add_subtree(ti, ett_values);
755             g_free(s);
756             n += l;
757             proto_tree_add_item(twig, hf_tableoid, tvb, n, 4, FALSE);
758             n += 4;
759             proto_tree_add_item(twig, hf_val_idx, tvb, n, 2, FALSE);
760             n += 2;
761             proto_tree_add_item(twig, hf_typeoid, tvb, n, 4, FALSE);
762             n += 4;
763             proto_tree_add_item(twig, hf_val_length, tvb, n, 2, FALSE);
764             n += 2;
765             proto_tree_add_item(twig, hf_val_mod, tvb, n, 4, FALSE);
766             n += 4;
767             proto_tree_add_item(twig, hf_format, tvb, n, 2, FALSE);
768             n += 2;
769         }
770         break;
771
772     /* Data row */
773     case 'D':
774         i = tvb_get_ntohs(tvb, n);
775         ti = proto_tree_add_text(tree, tvb, n, 2, "Columns: %d", i);
776         shrub = proto_item_add_subtree(ti, ett_values);
777         n += 2;
778         while (i-- > 0) {
779             l = tvb_get_ntohl(tvb, n);
780             proto_tree_add_int(shrub, hf_val_length, tvb, n, 4, l);
781             n += 4;
782             /* XXX - if we don't limit l here, the function will assert on very large values */
783             if (l > 0 && l < 1000000)
784                 proto_tree_add_item(shrub, hf_val_data, tvb, n, l, FALSE);
785             n += l;
786         }
787         break;
788
789     /* Command completion */
790     case 'C':
791         s = tvb_get_stringz(tvb, n, &l);
792         proto_tree_add_string(tree, hf_tag, tvb, n, l, s);
793         g_free(s);
794         break;
795
796     /* Ready */
797     case 'Z':
798         proto_tree_add_item(tree, hf_status, tvb, n, 1, FALSE);
799         break;
800
801     /* Error, Notice */
802     case 'E':
803     case 'N':
804         length -= 4;
805         while (length > 0) {
806             c = tvb_get_guint8(tvb, n);
807             if (c == '\0')
808                 break;
809             s = tvb_get_stringz(tvb, n+1, &l);
810             i = hf_text;
811             switch (c) {
812             case 'S': i = hf_severity; break;
813             case 'C': i = hf_code; break;
814             case 'M': i = hf_message; break;
815             case 'D': i = hf_detail; break;
816             case 'H': i = hf_hint; break;
817             case 'P': i = hf_position; break;
818             case 'W': i = hf_where; break;
819             case 'F': i = hf_file; break;
820             case 'L': i = hf_line; break;
821             case 'R': i = hf_routine; break;
822             }
823             proto_tree_add_string(tree, i, tvb, n, l+1, s);
824             g_free(s);
825             n += l+1;
826         }
827         break;
828
829     /* NOTICE response */
830     case 'A':
831         proto_tree_add_item(tree, hf_pid, tvb, n, 4, FALSE);
832         n += 4;
833         s = tvb_get_stringz(tvb, n, &l);
834         proto_tree_add_string(tree, hf_condition, tvb, n, l, s);
835         g_free(s);
836         n += l;
837         s = tvb_get_stringz(tvb, n, &l);
838         if (l > 1)
839             proto_tree_add_string(tree, hf_text, tvb, n, l, s);
840         g_free(s);
841         break;
842
843     /* Copy in/out */
844     case 'G':
845     case 'H':
846         proto_tree_add_item(tree, hf_format, tvb, n, 1, FALSE);
847         n += 1;
848         i = tvb_get_ntohs(tvb, n);
849         ti = proto_tree_add_text(tree, tvb, n, 2, "Columns: %d", i);
850         shrub = proto_item_add_subtree(ti, ett_values);
851         n += 2;
852         while (i-- > 2) {
853             proto_tree_add_item(shrub, hf_format, tvb, n, 2, FALSE);
854             n += 2;
855         }
856         break;
857
858     /* Copy data */
859     case 'd':
860         proto_tree_add_item(tree, hf_copydata, tvb, n, length-n+1, FALSE);
861         break;
862
863     /* Function call response */
864     case 'V':
865         l = tvb_get_ntohl(tvb, n);
866         proto_tree_add_int(tree, hf_val_length, tvb, n, 4, l);
867         if (l > 0)
868             proto_tree_add_item(tree, hf_val_data, tvb, n+4, l, FALSE);
869         break;
870     }
871 }
872
873
874 /* This is like specifying VALS(messages) for hf_type, which we can't do
875    directly because of messages without type bytes, and because the type
876    interpretation depends on fe. */
877
878 static char *identify(gboolean fe, guchar type)
879 {
880     int i = 0;
881     const value_string *messages;
882
883     if (fe)
884         messages = fe_messages;
885     else
886         messages = be_messages;
887
888     while (messages[i].strptr) {
889         if (messages[i].value == type)
890             return messages[i].strptr;
891         i++;
892     }
893
894     return "Unknown";
895 }