Additional RAP error code for password changes sent to a BDC, from Devin
[obnox/wireshark/wip.git] / packet-ldap.c
index d70aee982441f4d457665b71ba97e70a4c0ebacd..9510a08248e794291400bdbfe62a8676e23f3034 100644 (file)
@@ -1,10 +1,10 @@
 /* packet-ldap.c
  * Routines for ldap packet dissection
  *
- * $Id: packet-ldap.c,v 1.11 2000/05/12 08:04:29 guy Exp $
+ * $Id: packet-ldap.c,v 1.41 2002/03/31 22:17:37 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
 #include <glib.h>
 
 #ifdef NEED_SNPRINTF_H
-# ifdef HAVE_STDARG_H
-#  include <stdarg.h>
-# else
-#  include <varargs.h>
-# endif
 # include "snprintf.h"
 #endif
 
-#include "packet.h"
+#include <epan/packet.h>
 
 #include "packet-ldap.h"
 #include "asn1.h"
+#include "prefs.h"
 
 static int proto_ldap = -1;
 static int hf_ldap_length = -1;
@@ -114,6 +110,9 @@ static gint ett_ldap_message = -1;
 static gint ett_ldap_referrals = -1;
 static gint ett_ldap_attribute = -1;
 
+/* desegmentation of LDAP */
+static gboolean ldap_desegment = TRUE;
+
 #define TCP_PORT_LDAP                  389
 
 static value_string msgTypes [] = {
@@ -138,23 +137,33 @@ static value_string msgTypes [] = {
   {LDAP_RES_MODRDN, "Modify RDN Result"},
   {LDAP_RES_COMPARE, "Compare Result"},
   {LDAP_REQ_EXTENDED, "Extended Response"},
+  {0, NULL},
 };
 
 static int read_length(ASN1_SCK *a, proto_tree *tree, int hf_id, guint *len)
 {
   guint length = 0;
   gboolean def = FALSE;
-  const guchar *start = a->pointer;
+  int start = a->offset;
+  int ret;
   
-  asn1_length_decode(a, &def, &length);
+  ret = asn1_length_decode(a, &def, &length);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "%s: ERROR: Couldn't parse length: %s",
+        proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
+    }
+    return ret;
+  }
 
   if (len)
     *len = length;
 
   if (tree)
-    proto_tree_add_item(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, length);
+    proto_tree_add_uint(tree, hf_id, a->tvb, start, a->offset-start, length);
 
-  return 0;
+  return ASN1_ERR_NOERROR;
 }
 
 static int read_sequence(ASN1_SCK *a, guint *len)
@@ -162,16 +171,18 @@ static int read_sequence(ASN1_SCK *a, guint *len)
   guint cls, con, tag;
   gboolean def;
   guint length;
+  int ret;
   
-  if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
-    return 1;
+  ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
+  if (ret != ASN1_ERR_NOERROR)
+    return ret;
   if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
-    return 1;
-  
+    return ASN1_ERR_WRONG_TYPE;
+
   if (len)
     *len = length;
   
-  return 0;
+  return ASN1_ERR_NOERROR;
 }
 
 static int read_set(ASN1_SCK *a, guint *len)
@@ -179,63 +190,148 @@ static int read_set(ASN1_SCK *a, guint *len)
   guint cls, con, tag;
   gboolean def;
   guint length;
+  int ret;
   
-  if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
-    return 1;
+  ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
+  if (ret != ASN1_ERR_NOERROR)
+    return ret;
   if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SET)
-    return 1;
+    return ASN1_ERR_WRONG_TYPE;
   
   if (len)
     *len = length;
   
-  return 0;
+  return ASN1_ERR_NOERROR;
 }
 
 static int read_integer_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
-       proto_tree **new_tree, guint *i, const guchar *start, guint length)
+       proto_item **new_item, guint *i, int start, guint length)
 {
   guint integer = 0;
+  proto_item *temp_item = NULL;
+  int ret;
 
-  asn1_uint32_value_decode(a, length, &integer);
+  ret = asn1_uint32_value_decode(a, length, &integer);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+       "%s: ERROR: Couldn't parse value: %s",
+        proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
+    }
+    return ret;
+  }
 
   if (i)
     *i = integer;
 
   if (tree)
-  {
-    proto_tree *temp_tree = 0;
-    temp_tree = proto_tree_add_item(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
-    if (new_tree)
-      *new_tree = temp_tree;
-  }
+    temp_item = proto_tree_add_uint(tree, hf_id, a->tvb, start, a->offset-start, integer);
+
+  if (new_item)
+    *new_item = temp_item;
 
-  return 0;
+  return ASN1_ERR_NOERROR;
 }
 
 static int read_integer(ASN1_SCK *a, proto_tree *tree, int hf_id,
-       proto_tree **new_tree, guint *i, guint expected_tag)
+       proto_item **new_item, guint *i, guint expected_tag)
+{
+  guint cls, con, tag;
+  gboolean def;
+  guint length;
+  int start = a->offset;
+  int ret;
+  
+  ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
+  if (ret == ASN1_ERR_NOERROR) {
+    if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
+      ret = ASN1_ERR_WRONG_TYPE;
+  }
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "%s: ERROR: Couldn't parse header: %s",
+        (hf_id != -1) ? proto_registrar_get_name(hf_id) : "LDAP message",
+        asn1_err_to_str(ret));
+    }
+    return ret;
+  }
+
+  return read_integer_value(a, tree, hf_id, new_item, i, start, length);
+}
+
+static int read_boolean_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
+       proto_item **new_item, guint *i, int start, guint length)
+{
+  guint integer = 0;
+  proto_item *temp_item = NULL;
+  int ret;
+
+  ret = asn1_uint32_value_decode(a, length, &integer);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "%s: ERROR: Couldn't parse value: %s",
+        proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
+    }
+    return ret;
+  }
+
+  if (i)
+    *i = integer;
+
+  if (tree)
+    temp_item = proto_tree_add_boolean(tree, hf_id, a->tvb, start, a->offset-start, integer);
+  if (new_item)
+    *new_item = temp_item;
+
+  return ASN1_ERR_NOERROR;
+}
+
+static int read_boolean(ASN1_SCK *a, proto_tree *tree, int hf_id,
+       proto_item **new_item, guint *i)
 {
   guint cls, con, tag;
   gboolean def;
   guint length;
-  const guchar *start = a->pointer;
+  int start = a->offset;
+  int ret;
   
-  if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
-    return 1;
-  if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
-    return 1;
+  ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
+  if (ret == ASN1_ERR_NOERROR) {
+    if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_BOL)
+      ret = ASN1_ERR_WRONG_TYPE;
+  }
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "%s: ERROR: Couldn't parse header: %s",
+        proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
+    }
+    return ret;
+  }
 
-  return read_integer_value(a, tree, hf_id, new_tree, i, start, length);
+  return read_boolean_value(a, tree, hf_id, new_item, i, start, length);
 }
 
-static void read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
-       proto_tree **new_tree, char **s, const guchar *start, guint length)
+static int read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
+       proto_item **new_item, char **s, int start, guint length)
 {
   guchar *string;
+  proto_item *temp_item = NULL;
+  int ret;
   
   if (length)
   {
-    asn1_octet_string_value_decode(a, length, &string);
+    ret = asn1_string_value_decode(a, length, &string);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, start, 0,
+          "%s: ERROR: Couldn't parse value: %s",
+          proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
+      }
+      return ret;
+    }
     string = g_realloc(string, length + 1);
     string[length] = '\0';
   }
@@ -243,45 +339,52 @@ static void read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
     string = "(null)";
     
   if (tree)
-  {
-    proto_tree *temp_tree;
-    temp_tree = proto_tree_add_item(tree, hf_id, NullTVB, start - a->begin, a->pointer - start, string);
-    if (new_tree)
-      *new_tree = temp_tree;
-  }
+    temp_item = proto_tree_add_string(tree, hf_id, a->tvb, start, a->offset - start, string);
+  if (new_item)
+    *new_item = temp_item;
 
   if (s && length)
     *s = string;
   else if (length)
     g_free(string);
+
+  return ASN1_ERR_NOERROR;
 }
 
 static int read_string(ASN1_SCK *a, proto_tree *tree, int hf_id,
-       proto_tree **new_tree, char **s, guint expected_cls, guint expected_tag)
+       proto_item **new_item, char **s, guint expected_cls, guint expected_tag)
 {
   guint cls, con, tag;
   gboolean def;
   guint length;
-  const guchar *start = a->pointer;
+  int start = a->offset;
   int ret;
   
   ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
-  if (ret != ASN1_ERR_NOERROR)
+  if (ret == ASN1_ERR_NOERROR) {
+    if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
+      ret = ASN1_ERR_WRONG_TYPE;
+  }
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "%s: ERROR: Couldn't parse header: %s",
+        proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
+    }
     return ret;
-  if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
-    return ASN1_ERR_WRONG_TYPE;
+  }
 
-  read_string_value(a, tree, hf_id, new_tree, s, start, length);
-  return ASN1_ERR_NOERROR;
+  return read_string_value(a, tree, hf_id, new_item, s, start, length);
 }
 
 static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length, const guchar *operation)
 {
   guchar *string;
   guchar *string2;
-  gint string_length;
-  gint string2_length;
+  guint string_length;
+  guint string2_length;
   guint string_bytes;
+  char *filterp;
   int ret;
 
   ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
@@ -292,7 +395,20 @@ static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length
     return ret;
   *filter_length += 2 + strlen(operation) + string_length + string2_length;
   *filter = g_realloc(*filter, *filter_length);
-  sprintf(*filter + strlen(*filter), "(%.*s%s%.*s)", string_length, string, operation, string2_length, string2);
+  filterp = *filter + strlen(*filter);
+  *filterp++ = '(';
+  if (string_length != 0) {
+       memcpy(filterp, string, string_length);
+       filterp += string_length;
+  }
+  strcpy(filterp, operation);
+  filterp += strlen(operation);
+  if (string2_length != 0) {
+       memcpy(filterp, string2, string2_length);
+       filterp += string2_length;
+  }
+  *filterp++ = ')';
+  *filterp = '\0';
   g_free(string);
   g_free(string2);
   return ASN1_ERR_NOERROR;
@@ -301,9 +417,10 @@ static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length
 /* Richard Dawe: To parse substring filters, I added this function. */
 static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_length)
 {
-  guchar *end;
-  guchar *string = NULL;
-  gint string_length;
+  int end;
+  guchar *string;
+  char *filterp;
+  guint string_length;
   guint string_bytes;
   guint seq_len;
   guint header_bytes;  
@@ -323,22 +440,31 @@ static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_len
   if (ret != ASN1_ERR_NOERROR)
     return ret;
 
-  *filter_length += 2 + 1 + strlen(string);
+  *filter_length += 2 + 1 + string_length;
   *filter = g_realloc(*filter, *filter_length);
-  sprintf(*filter + strlen(*filter), "(%.*s=", string_length, string);
+  
+  filterp = *filter + strlen(*filter);
+  *filterp++ = '(';
+  if (string_length != 0) {
+    memcpy(filterp, string, string_length);
+    filterp += string_length;
+  }
+  *filterp++ = '=';
+  *filterp = '\0';
   g_free(string);
 
   /* Now decode seq_len's worth of octet strings. */
   any_valued = 0;
-  end = (guchar *) (a->pointer + seq_len);
+  end = a->offset + seq_len;
 
-  while (a->pointer < end) {
+  while (a->offset < end) {
     /* Octet strings here are context-specific, which
      * asn1_octet_string_decode() barfs on. Emulate it, but don't barf. */
     ret = asn1_header_decode (a, &cls, &con, &tag, &def, &string_length);
     if (ret != ASN1_ERR_NOERROR)
       return ret;
 
+    /* XXX - check the tag? */
     if (cls != ASN1_CTX || con != ASN1_PRI) {
        /* XXX - handle the constructed encoding? */
        return ASN1_ERR_WRONG_TYPE;
@@ -346,25 +472,30 @@ static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_len
     if (!def)
        return ASN1_ERR_LENGTH_NOT_DEFINITE;
 
-    ret = asn1_octet_string_value_decode(a, (int) string_length, &string);
+    ret = asn1_string_value_decode(a, (int) string_length, &string);
     if (ret != ASN1_ERR_NOERROR)
       return ret;
 
     /* If we have an 'any' component with a string value, we need to append
      * an extra asterisk before final component. */
-    if ((tag == 1) && (string_length > 0))
+    if ((tag == 1) && (string_length != 0))
       any_valued = 1;
 
     if ( (tag == 1) || ((tag == 2) && any_valued) )
       (*filter_length)++;
-    *filter_length += strlen(string);
+    *filter_length += string_length;
     *filter = g_realloc(*filter, *filter_length);
 
+    filterp = *filter + strlen(*filter);
     if ( (tag == 1) || ((tag == 2) && any_valued) )
-      strcat(*filter, "*");
+      *filterp++ = '*';
     if (tag == 2)
       any_valued = 0;
-    sprintf(*filter + strlen(*filter), "%.*s", string_length, string);
+    if (string_length != 0) {
+      memcpy(filterp, string, string_length);
+      filterp += string_length;
+    }
+    *filterp = '\0';
     g_free(string);
   }
 
@@ -372,17 +503,20 @@ static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_len
   {
     (*filter_length)++;
     *filter = g_realloc(*filter, *filter_length);
-    strcat(*filter, "*");
+    filterp = *filter + strlen(*filter);
+    *filterp++ = '*';
   }
   
   /* NB: Allocated byte for this earlier */
-  strcat(*filter, ")");
+  *filterp++ = ')';
+  *filterp = '\0';
 
   return ASN1_ERR_NOERROR;
 }
 
 /* Returns -1 if we're at the end, returns an ASN1_ERR value otherwise. */
-static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const guchar **end)
+static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length,
+                       int *end)
 {
   guint cls, con, tag;
   guint length;
@@ -395,7 +529,7 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
   
   if (*end == 0)
   {
-    *end = a->pointer + length;
+    *end = a->offset + length;
     *filter_length = 1;
     *filter = g_malloc0(*filter_length);
   }
@@ -406,11 +540,11 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
     {
      case LDAP_FILTER_AND:
       {
-        const guchar *add_end;
+        int add_end;
 
         if (con != ASN1_CON)
           return ASN1_ERR_WRONG_TYPE;
-        add_end = a->pointer + length;
+        add_end = a->offset + length;
         *filter_length += 3;
         *filter = g_realloc(*filter, *filter_length);
         strcat(*filter, "(&");
@@ -424,11 +558,11 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
       break;
      case LDAP_FILTER_OR:
       {
-        const guchar *or_end;
+        int or_end;
 
         if (con != ASN1_CON)
           return ASN1_ERR_WRONG_TYPE;
-        or_end = a->pointer + length;
+        or_end = a->offset + length;
         *filter_length += 3;
         *filter = g_realloc(*filter, *filter_length);
         strcat(*filter, "(|");
@@ -442,11 +576,11 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
       break;
      case LDAP_FILTER_NOT:
       {
-        const guchar *not_end;
+        int not_end;
 
         if (con != ASN1_CON)
           return ASN1_ERR_WRONG_TYPE;
-        not_end = a->pointer + length;
+        not_end = a->offset + length;
         *filter_length += 3;
         *filter = g_realloc(*filter, *filter_length);
         strcat(*filter, "(!");
@@ -460,14 +594,14 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
       if (con != ASN1_CON)
         return ASN1_ERR_WRONG_TYPE;
       ret = parse_filter_strings(a, filter, filter_length, "=");
-      if (ret != -1 && ret != ASN1_ERR_NOERROR)
+      if (ret != ASN1_ERR_NOERROR)
         return ret;
       break;
      case LDAP_FILTER_GE:
       if (con != ASN1_CON)
         return ASN1_ERR_WRONG_TYPE;
       ret = parse_filter_strings(a, filter, filter_length, ">=");
-      if (ret != -1 && ret != ASN1_ERR_NOERROR)
+      if (ret != ASN1_ERR_NOERROR)
         return ret;
       break;
      case LDAP_FILTER_LE:
@@ -481,21 +615,31 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
       if (con != ASN1_CON)
         return ASN1_ERR_WRONG_TYPE;
       ret = parse_filter_strings(a, filter, filter_length, "~=");
-      if (ret != -1 && ret != ASN1_ERR_NOERROR)
+      if (ret != ASN1_ERR_NOERROR)
         return ret;
       break;
      case LDAP_FILTER_PRESENT:
       {
         guchar *string;
+        char *filterp;
     
         if (con != ASN1_PRI)
           return ASN1_ERR_WRONG_TYPE;
-        ret = asn1_octet_string_value_decode(a, length, &string);
+        ret = asn1_string_value_decode(a, length, &string);
         if (ret != ASN1_ERR_NOERROR)
           return ret;
         *filter_length += 4 + length;
         *filter = g_realloc(*filter, *filter_length);
-        sprintf(*filter + strlen(*filter), "(%.*s=*)", (int)length, string);
+        filterp = *filter + strlen(*filter);
+        *filterp++ = '(';
+        if (length != 0) {
+          memcpy(filterp, string, length);
+          filterp += length;
+        }
+        *filterp++ = '=';
+        *filterp++ = '*';
+        *filterp++ = ')';
+        *filterp = '\0';
         g_free(string);
       }
       break;
@@ -504,7 +648,7 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
         return ASN1_ERR_WRONG_TYPE;
       /* Richard Dawe: Handle substrings */
       ret = parse_filter_substrings(a, filter, filter_length);
-      if (ret != -1 && ret != ASN1_ERR_NOERROR)
+      if (ret != ASN1_ERR_NOERROR)
         return ret;
       break;
      default:
@@ -512,18 +656,18 @@ static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const
     }
   }
   
-  if (a->pointer == *end)
+  if (a->offset == *end)
     return -1;
   else
     return ret;
 }
 
-static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
+static gboolean read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
 {
-  const guchar *start = a->pointer;
+  int start = a->offset;
   char *filter = 0;
   guint filter_length = 0;
-  const guchar *end = 0;
+  int end = 0;
   int ret;
      
   while ((ret = parse_filter(a, &filter, &filter_length, &end))
@@ -532,388 +676,638 @@ static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
 
   if (tree) {
     if (ret != -1) {
-      proto_tree_add_text(tree, NullTVB, start-a->begin, 0,
-        "Error parsing filter (%d)", ret);
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "%s: ERROR: Can't parse filter: %s",
+        proto_registrar_get_name(hf_id), asn1_err_to_str(ret));
     } else
-      proto_tree_add_item(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, filter);
+      proto_tree_add_string(tree, hf_id, a->tvb, start, a->offset-start, filter);
   }
 
   g_free(filter);
 
-  return 0;
+  return (ret == -1) ? TRUE : FALSE;
 }
 
 /********************************************************************************************/
 
-static int dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
 {
   guint resultCode = 0;
+  int ret;
   
-  read_integer(a, tree, hf_ldap_message_result, 0, &resultCode, ASN1_ENUM);
-  read_string(a, tree, hf_ldap_message_result_matcheddn, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_string(a, tree, hf_ldap_message_result_errormsg, 0, 0, ASN1_UNI, ASN1_OTS);
+  if (read_integer(a, tree, hf_ldap_message_result, 0, &resultCode, ASN1_ENUM) != ASN1_ERR_NOERROR)
+    return;
+  if (read_string(a, tree, hf_ldap_message_result_matcheddn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  if (read_string(a, tree, hf_ldap_message_result_errormsg, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
 
   if (resultCode == 10)                /* Referral */
   {
-    const guchar *start = a->pointer;
-    const guchar *end;
+    int start = a->offset;
+    int end;
     guint length;
-    proto_tree *t, *referralTree;
+    proto_item *ti;
+    proto_tree *referralTree;
     
-    read_sequence(a, &length);
-    t = proto_tree_add_text(tree, NullTVB, start-a->begin, length, "Referral URLs");
-    referralTree = proto_item_add_subtree(t, ett_ldap_referrals);
+    ret = read_sequence(a, &length);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, start, 0,
+            "ERROR: Couldn't parse referral URL sequence header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    ti = proto_tree_add_text(tree, a->tvb, start, length, "Referral URLs");
+    referralTree = proto_item_add_subtree(ti, ett_ldap_referrals);
 
-    end = a->pointer + length;;
-    while (a->pointer < end)
-      read_string(a, referralTree, hf_ldap_message_result_referral, 0, 0, ASN1_UNI, ASN1_OTS);
+    end = a->offset + length;
+    while (a->offset < end) {
+      if (read_string(a, referralTree, hf_ldap_message_result_referral, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
+    }
   }
-    
-  return 0;
 }
 
-static int dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
 {
   guint cls, con, tag;
   guint def, length;
-  const guchar *start;
-
-  read_integer(a, tree, hf_ldap_message_bind_version, 0, 0, ASN1_INT);
-  read_string(a, tree, hf_ldap_message_bind_dn, 0, 0, ASN1_UNI, ASN1_OTS);
-
-  start = a->pointer;
-  if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
-    return 1;  /* XXX - right return value for an error? */
-  if (cls != ASN1_CTX)
-    return 1;  /* RFCs 1777 and 2251 say these are context-specific types */
-  proto_tree_add_item(tree, hf_ldap_message_bind_auth, NullTVB, start - a->begin,
-                       a->pointer - start, tag);
+  int start;
+  int ret;
+
+  if (read_integer(a, tree, hf_ldap_message_bind_version, 0, 0, ASN1_INT) != ASN1_ERR_NOERROR)
+    return;
+  if (read_string(a, tree, hf_ldap_message_bind_dn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+
+  start = a->offset;
+  ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
+  if (ret == ASN1_ERR_NOERROR) {
+    if (cls != ASN1_CTX) {
+      /* RFCs 1777 and 2251 say these are context-specific types */
+      ret = ASN1_ERR_WRONG_TYPE;
+    }
+  }
+  if (ret != ASN1_ERR_NOERROR) {
+    proto_tree_add_text(tree, a->tvb, start, 0,
+      "%s: ERROR: Couldn't parse header: %s",
+      proto_registrar_get_name(hf_ldap_message_bind_auth),
+      asn1_err_to_str(ret));
+    return;
+  }
+  proto_tree_add_uint(tree, hf_ldap_message_bind_auth, a->tvb, start,
+                       a->offset - start, tag);
   switch (tag)
   {
    case LDAP_AUTH_SIMPLE:
-    read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL, NULL,
-                       start, length);
+    if (read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL,
+                          NULL, start, length) != ASN1_ERR_NOERROR)
+      return;
     break;
 
     /* For Kerberos V4, dissect it as a ticket. */
     /* For SASL, dissect it as SaslCredentials. */
   }
-  
-  return 0;
 }
 
-static int dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
 {
-  dissect_ldap_result(a, tree);
   /* FIXME: handle SASL data */
-  return 0;
+  dissect_ldap_result(a, tree);
 }
 
-static int dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
 {
   guint seq_length;
-  const guchar *end;
+  int end;
   int ret;
   
-  read_string(a, tree, hf_ldap_message_search_base, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_integer(a, tree, hf_ldap_message_search_scope, 0, 0, ASN1_ENUM);
-  read_integer(a, tree, hf_ldap_message_search_deref, 0, 0, ASN1_ENUM);
-  read_integer(a, tree, hf_ldap_message_search_sizeLimit, 0, 0, ASN1_INT);
-  read_integer(a, tree, hf_ldap_message_search_timeLimit, 0, 0, ASN1_INT);
-  read_integer(a, tree, hf_ldap_message_search_typesOnly, 0, 0, ASN1_BOL);
-  ret = read_filter(a, tree, hf_ldap_message_search_filter);
-  if (ret != ASN1_ERR_NOERROR)
-    return ret;
-  read_sequence(a, &seq_length);
-  end = a->pointer + seq_length;
-  while (a->pointer < end) {
-    ret = read_string(a, tree, hf_ldap_message_attribute, 0, 0, ASN1_UNI, ASN1_OTS);
-    if (ret != ASN1_ERR_NOERROR)
-      return ret;
+  if (read_string(a, tree, hf_ldap_message_search_base, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  if (read_integer(a, tree, hf_ldap_message_search_scope, 0, 0, ASN1_ENUM) != ASN1_ERR_NOERROR)
+    return;
+  if (read_integer(a, tree, hf_ldap_message_search_deref, 0, 0, ASN1_ENUM) != ASN1_ERR_NOERROR)
+    return;
+  if (read_integer(a, tree, hf_ldap_message_search_sizeLimit, 0, 0, ASN1_INT) != ASN1_ERR_NOERROR)
+    return;
+  if (read_integer(a, tree, hf_ldap_message_search_timeLimit, 0, 0, ASN1_INT) != ASN1_ERR_NOERROR)
+    return;
+  if (read_boolean(a, tree, hf_ldap_message_search_typesOnly, 0, 0) != ASN1_ERR_NOERROR)
+    return;
+  if (!read_filter(a, tree, hf_ldap_message_search_filter))
+    return;
+  ret = read_sequence(a, &seq_length);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, a->offset, 0,
+          "ERROR: Couldn't parse LDAP attribute sequence header: %s",
+          asn1_err_to_str(ret));
+    }
+    return;
+  }
+  end = a->offset + seq_length;
+  while (a->offset < end) {
+    if (read_string(a, tree, hf_ldap_message_attribute, 0, 0, ASN1_UNI,
+                    ASN1_OTS) != ASN1_ERR_NOERROR)
+      return;
   }
-  return ASN1_ERR_NOERROR;
 }
 
-static int dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
 {
   guint seq_length;
-  const guchar *end_of_sequence;
+  int end_of_sequence;
+  int ret;
  
-  read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_sequence(a, &seq_length);
+  if (read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  ret = read_sequence(a, &seq_length);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, a->offset, 0,
+          "ERROR: Couldn't parse search entry response sequence header: %s",
+          asn1_err_to_str(ret));
+    }
+    return;
+  }
 
-  end_of_sequence = a->pointer + seq_length;
-  while (a->pointer < end_of_sequence)
+  end_of_sequence = a->offset + seq_length;
+  while (a->offset < end_of_sequence)
   {
-    proto_tree *t, *attr_tree;
+    proto_item *ti;
+    proto_tree *attr_tree;
     guint set_length;
-    const guchar *end_of_set;
-
-    read_sequence(a, 0);
-    read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
-    attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
-
-    read_set(a, &set_length);
-    end_of_set = a->pointer + set_length;
-    while (a->pointer < end_of_set)
-      read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
+    int end_of_set;
+
+    ret = read_sequence(a, 0);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse LDAP attribute sequence header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    if (read_string(a, tree, hf_ldap_message_attribute, &ti, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+      return;
+    attr_tree = proto_item_add_subtree(ti, ett_ldap_attribute);
+
+    ret = read_set(a, &set_length);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(attr_tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse LDAP value set header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    end_of_set = a->offset + set_length;
+    while (a->offset < end_of_set) {
+      if (read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI,
+                      ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
+    }
   }
-
-  return 0;
 }
 
-static int dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
 {
   guint seq_length;
-  const guchar *end_of_sequence;
+  int end_of_sequence;
+  int ret;
   
-  read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
+  if (read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+
+  ret = read_sequence(a, &seq_length);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, a->offset, 0,
+          "ERROR: Couldn't parse add request sequence header: %s",
+          asn1_err_to_str(ret));
+    }
+    return;
+  }
 
-  read_sequence(a, &seq_length);
-  end_of_sequence = a->pointer + seq_length;
-  while (a->pointer < end_of_sequence)
+  end_of_sequence = a->offset + seq_length;
+  while (a->offset < end_of_sequence)
   {
-    proto_tree *t, *attr_tree;
+    proto_item *ti;
+    proto_tree *attr_tree;
     guint set_length;
-    const guchar *end_of_set;
-
-    read_sequence(a, 0);
-    read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
-    attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
-
-    read_set(a, &set_length);
-    end_of_set = a->pointer + set_length;
-    while (a->pointer < end_of_set)
-      read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
+    int end_of_set;
+
+    ret = read_sequence(a, 0);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse LDAP attribute sequence header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    if (read_string(a, tree, hf_ldap_message_attribute, &ti, 0, ASN1_UNI,
+                    ASN1_OTS) != ASN1_ERR_NOERROR)
+      return;
+    attr_tree = proto_item_add_subtree(ti, ett_ldap_attribute);
+
+    ret = read_set(a, &set_length);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(attr_tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse LDAP value set header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    end_of_set = a->offset + set_length;
+    while (a->offset < end_of_set) {
+      if (read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
+    }
   }
-
-  return 0;
 }
 
-static int dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
-               const guchar *start, guint length)
+static void dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
+               int start, guint length)
 {
   read_string_value(a, tree, hf_ldap_message_dn, NULL, NULL, start, length);
-  return 0;
 }
 
-static int dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
+static void dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
                guint length)
 {
-  const guchar *start = a->pointer;
-
-  read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_string(a, tree, hf_ldap_message_modrdn_name, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_integer(a, tree, hf_ldap_message_modrdn_delete, 0, 0, ASN1_BOL);
+  int start = a->offset;
+
+  if (read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  if (read_string(a, tree, hf_ldap_message_modrdn_name, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  if (read_boolean(a, tree, hf_ldap_message_modrdn_delete, 0, 0) != ASN1_ERR_NOERROR)
+    return;
   
-  if (a->pointer < (start + length)) {
+  if (a->offset < (int) (start + length)) {
     /* LDAP V3 Modify DN operation, with newSuperior */
-    read_string(a, tree, hf_ldap_message_modrdn_superior, 0, 0, ASN1_UNI, ASN1_OTS);
+    if (read_string(a, tree, hf_ldap_message_modrdn_superior, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+      return;
   }
-
-  return 0;
 }
 
-static int dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
 {
-  const guchar *start;
+  int start;
   int length;
   char *string1 = 0;
   char *string2 = 0;
   char *compare;
+  int ret;
   
-  read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_sequence(a, 0);
+  if (read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  ret = read_sequence(a, 0);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, a->offset, 0,
+          "ERROR: Couldn't parse compare request sequence header: %s",
+          asn1_err_to_str(ret));
+    }
+    return;
+  }
 
-  start = a->pointer;
-  read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
-  read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
+  start = a->offset;
+  ret = read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "ERROR: Couldn't parse compare type: %s", asn1_err_to_str(ret));
+    }
+    return;
+  }
+  ret = read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, start, 0,
+        "ERROR: Couldn't parse compare value: %s", asn1_err_to_str(ret));
+    }
+    return;
+  }
 
   length = 2 + strlen(string1) + strlen(string2);
   compare = g_malloc0(length);
   snprintf(compare, length, "%s=%s", string1, string2);
-  proto_tree_add_item(tree, hf_ldap_message_compare, NullTVB, start-a->begin, a->pointer-start, compare);
+  proto_tree_add_string(tree, hf_ldap_message_compare, a->tvb, start,
+      a->offset-start, compare);
   
   g_free(string1);
   g_free(string2);
   g_free(compare);
   
-  return 0;
+  return;
 }
 
-static int dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
+static void dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
 {
   guint seq_length;
-  const guchar *end_of_sequence;
+  int end_of_sequence;
+  int ret;
   
-  read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
-  read_sequence(a, &seq_length);
-  end_of_sequence = a->pointer + seq_length;
-  while (a->pointer < end_of_sequence)
+  if (read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+    return;
+  ret = read_sequence(a, &seq_length);
+  if (ret != ASN1_ERR_NOERROR) {
+    if (tree) {
+      proto_tree_add_text(tree, a->tvb, a->offset, 0,
+          "ERROR: Couldn't parse modify request sequence header: %s",
+          asn1_err_to_str(ret));
+    }
+    return;
+  }
+  end_of_sequence = a->offset + seq_length;
+  while (a->offset < end_of_sequence)
   {
-    proto_tree *t = 0, *attr_tree;
+    proto_item *ti;
+    proto_tree *attr_tree;
     guint set_length;
-    const guchar *end_of_set;
+    int end_of_set;
     guint operation;
 
-    read_sequence(a, 0);
-    read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
-    read_sequence(a, 0);
+    ret = read_sequence(a, 0);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse modify request item sequence header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    ret = read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, a->offset, 0,
+          "ERROR: Couldn't parse modify operation: %s",
+          asn1_err_to_str(ret));
+        return;
+      }
+    }
+    ret = read_sequence(a, 0);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse modify request operation sequence header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
 
     switch (operation)
     {
      case LDAP_MOD_ADD:
-      read_string(a, tree, hf_ldap_message_modify_add, &t, 0, ASN1_UNI, ASN1_OTS);
+      if (read_string(a, tree, hf_ldap_message_modify_add, &ti, 0, ASN1_UNI,
+                      ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
       break;
+
      case LDAP_MOD_REPLACE:
-      read_string(a, tree, hf_ldap_message_modify_replace, &t, 0, ASN1_UNI, ASN1_OTS);
+      if (read_string(a, tree, hf_ldap_message_modify_replace, &ti, 0,
+                      ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
       break;
+
      case LDAP_MOD_DELETE:
-      read_string(a, tree, hf_ldap_message_modify_delete, &t, 0, ASN1_UNI, ASN1_OTS);
+      if (read_string(a, tree, hf_ldap_message_modify_delete, &ti, 0,
+                      ASN1_UNI, ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
       break;
-    }
-    attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
 
-    read_set(a, &set_length);
-    end_of_set = a->pointer + set_length;
-    while (a->pointer < end_of_set)
-      read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
+     default:
+       proto_tree_add_text(tree, a->tvb, a->offset, 0,
+            "Unknown LDAP modify operation (%u)", operation);
+       return;
+    }
+    attr_tree = proto_item_add_subtree(ti, ett_ldap_attribute);
+
+    ret = read_set(a, &set_length);
+    if (ret != ASN1_ERR_NOERROR) {
+      if (tree) {
+        proto_tree_add_text(attr_tree, a->tvb, a->offset, 0,
+            "ERROR: Couldn't parse LDAP value set header: %s",
+            asn1_err_to_str(ret));
+      }
+      return;
+    }
+    end_of_set = a->offset + set_length;
+    while (a->offset < end_of_set) {
+      if (read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI,
+                      ASN1_OTS) != ASN1_ERR_NOERROR)
+        return;
+    }
   }
-
-  return 0;
 }
 
-static int dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
-               const guchar *start, guint length)
+static void dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
+               int start, guint length)
 {
   read_integer_value(a, tree, hf_ldap_message_abandon_msgid, NULL, NULL,
-                       start, length); 
-  return 0;
+                           start, length); 
 }
 
 static void
-dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+dissect_ldap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
   proto_tree *ldap_tree = 0, *ti, *msg_tree;
   guint messageLength;
   guint messageId;
+  int next_offset;
   guint protocolOpCls, protocolOpCon, protocolOpTag;
   gchar *typestr;
   guint opLen;
   ASN1_SCK a;
-  const guchar *start;
-  int first_time = 1;
+  int start;
+  gboolean first_time = TRUE;
   int ret;
 
-  if (tree) 
-  {
-    ti = proto_tree_add_item(tree, proto_ldap, NullTVB, offset, END_OF_FRAME, NULL);
-    ldap_tree = proto_item_add_subtree(ti, ett_ldap);
-  }
-
-  asn1_open(&a, pd, pi.captured_len);
-  a.pointer += offset;
+  asn1_open(&a, tvb, 0);
 
-  while (a.pointer < a.end)
+  while (tvb_reported_length_remaining(tvb, a.offset) > 0)
   {
     int message_id_start;
     int message_id_length;
     int message_start;
     
-    message_start = a.pointer - a.begin;
-    if (read_sequence(&a, &messageLength))
+    /*
+     * XXX - should handle the initial sequence specifier split across
+     * segment boundaries.
+     */
+    message_start = a.offset;
+    ret = read_sequence(&a, &messageLength);
+    if (ret != ASN1_ERR_NOERROR)
     {
-      if (ldap_tree)
-        proto_tree_add_text(ldap_tree, NullTVB, offset, 1, "Invalid LDAP packet");
+      if (first_time)
+      {
+        if (check_col(pinfo->cinfo, COL_PROTOCOL))
+          col_set_str(pinfo->cinfo, COL_PROTOCOL, "LDAP");
+        if (check_col(pinfo->cinfo, COL_INFO))
+        {
+          col_add_fstr(pinfo->cinfo, COL_INFO,
+                      "Invalid LDAP message (Can't parse sequence header: %s)",
+                      asn1_err_to_str(ret));
+        }
+      }
+      if (tree)
+      {
+        ti = proto_tree_add_item(tree, proto_ldap, tvb, message_start, -1,
+                                FALSE);
+        ldap_tree = proto_item_add_subtree(ti, ett_ldap);
+        proto_tree_add_text(ldap_tree, tvb, message_start, -1,
+                           "Invalid LDAP message (Can't parse sequence header: %s)",
+                           asn1_err_to_str(ret));
+      }
       break;
     }
 
-    if (messageLength > (a.end - a.pointer))
+    /*
+     * Desegmentation check.
+     */
+    if (ldap_desegment) {
+       if (pinfo->can_desegment
+           && messageLength > (guint)tvb_length_remaining(tvb, a.offset)) {
+           /*
+            * This frame doesn't have all of the data for this message,
+            * but we can do reassembly on it.
+            *
+            * Tell the TCP dissector where the data for this message
+            * starts in the data it handed us, and how many more bytes
+            * we need, and return.
+            */
+           pinfo->desegment_offset = message_start;
+           pinfo->desegment_len = messageLength -
+               tvb_length_remaining(tvb, a.offset);
+           return;
+       }
+    }
+    next_offset = a.offset + messageLength;
+
+    if (first_time)
+    {
+      if (check_col(pinfo->cinfo, COL_PROTOCOL))
+        col_set_str(pinfo->cinfo, COL_PROTOCOL, "LDAP");
+      if (check_col(pinfo->cinfo, COL_INFO))
+        col_clear(pinfo->cinfo, COL_INFO);
+    }
+
+    if (tree) 
     {
+      ti = proto_tree_add_item(tree, proto_ldap, tvb, message_start,
+                              next_offset - message_start, FALSE);
+      ldap_tree = proto_item_add_subtree(ti, ett_ldap);
+    }
+
+    message_id_start = a.offset;
+    ret = read_integer(&a, 0, hf_ldap_message_id, 0, &messageId, ASN1_INT);
+    if (ret != ASN1_ERR_NOERROR)
+    {
+      if (first_time && check_col(pinfo->cinfo, COL_INFO))
+        col_add_fstr(pinfo->cinfo, COL_INFO, "Invalid LDAP packet (Can't parse Message ID: %s)",
+                    asn1_err_to_str(ret));
       if (ldap_tree)
-        proto_tree_add_text(ldap_tree, NullTVB, message_start, END_OF_FRAME, "Short message! (expected: %u, actual: %u)",
-                           messageLength, a.end - a.pointer);
+        proto_tree_add_text(ldap_tree, tvb, message_id_start, 1,
+                            "Invalid LDAP packet (Can't parse Message ID: %s)",
+                            asn1_err_to_str(ret));
       break;
     }
-  
-    message_id_start = a.pointer - a.begin;
-    read_integer(&a, 0, -1, 0, &messageId, ASN1_INT);
-    message_id_length = (a.pointer - a.begin) - message_id_start;
+    message_id_length = a.offset - message_id_start;
 
-    start = a.pointer;
+    start = a.offset;
     asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
     if (protocolOpCls != ASN1_APL)
       typestr = "Bad message type (not Application)";
     else
-      typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
+      typestr = val_to_str(protocolOpTag, msgTypes, "Unknown message type (%u)");
 
     if (first_time)
     {
-      if (check_col(fd, COL_PROTOCOL))
-        col_add_str(fd, COL_PROTOCOL, "LDAP");
-
-      if (check_col(fd, COL_INFO))
-        col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
+      if (check_col(pinfo->cinfo, COL_INFO))
+        col_add_fstr(pinfo->cinfo, COL_INFO, "MsgId=%u MsgType=%s",
                     messageId, typestr);
-      first_time = 0;
-      if (!tree)
-       return;
+      first_time = FALSE;
     }
 
     if (ldap_tree) 
     {
-      proto_tree_add_item_hidden(ldap_tree, hf_ldap_message_id, NullTVB, message_id_start, message_id_length, messageId);
-      proto_tree_add_item_hidden(ldap_tree, hf_ldap_message_type, NullTVB,
-                                start - a.begin, a.pointer - start, protocolOpTag);
-      ti = proto_tree_add_text(ldap_tree, NullTVB, message_id_start, messageLength, "Message: Id=%u  %s", messageId, typestr);
+      proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_id, tvb, message_id_start, message_id_length, messageId);
+      proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_type, tvb,
+                                start, a.offset - start, protocolOpTag);
+      ti = proto_tree_add_text(ldap_tree, tvb, message_id_start, messageLength, "Message: Id=%u  %s", messageId, typestr);
       msg_tree = proto_item_add_subtree(ti, ett_ldap_message);
-      start = a.pointer;
-      read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
+      start = a.offset;
+      if (read_length(&a, msg_tree, hf_ldap_message_length, &opLen) != ASN1_ERR_NOERROR)
+        return;
 
-      switch (protocolOpTag)
+      if (protocolOpCls != ASN1_APL)
       {
-       case LDAP_REQ_BIND:
-        dissect_ldap_request_bind(&a, msg_tree);
-        break;
-       case LDAP_REQ_SEARCH:
-        ret = dissect_ldap_request_search(&a, msg_tree);
-        if (ret != ASN1_ERR_NOERROR)
+        proto_tree_add_text(msg_tree, a.tvb, a.offset, opLen,
+                           "%s", typestr);
+      }
+      else
+      {
+        switch (protocolOpTag)
+        {
+         case LDAP_REQ_BIND:
+          dissect_ldap_request_bind(&a, msg_tree);
+          break;
+         case LDAP_REQ_UNBIND:
+          /* Nothing to dissect */
+          break;
+         case LDAP_REQ_SEARCH:
+          dissect_ldap_request_search(&a, msg_tree);
+          break;
+         case LDAP_REQ_MODIFY:
+          dissect_ldap_request_modify(&a, msg_tree);
+          break;
+         case LDAP_REQ_ADD:
+          dissect_ldap_request_add(&a, msg_tree);
           break;
-        break;
-       case LDAP_REQ_ADD:
-        dissect_ldap_request_add(&a, msg_tree);
-        break;
-       case LDAP_REQ_DELETE:
-        dissect_ldap_request_delete(&a, msg_tree, start, opLen);
-        break;
-       case LDAP_REQ_MODRDN:
-        dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
-        break;
-       case LDAP_REQ_COMPARE:
-        dissect_ldap_request_compare(&a, msg_tree);
-        break;
-       case LDAP_REQ_MODIFY:
-        dissect_ldap_request_modify(&a, msg_tree);
-        break;
-       case LDAP_REQ_ABANDON:
-        dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
-        break;
-       case LDAP_RES_BIND:
-        dissect_ldap_response_bind(&a, msg_tree);
-        break;
-       case LDAP_RES_SEARCH_ENTRY:
-        dissect_ldap_response_search_entry(&a, msg_tree);
-        break;
-       case LDAP_RES_SEARCH_RESULT:
-       case LDAP_RES_MODIFY:
-       case LDAP_RES_ADD:
-       case LDAP_RES_DELETE:
-       case LDAP_RES_MODRDN:
-       case LDAP_RES_COMPARE:
-        dissect_ldap_result(&a, msg_tree);
-        break;
+         case LDAP_REQ_DELETE:
+          dissect_ldap_request_delete(&a, msg_tree, start, opLen);
+          break;
+         case LDAP_REQ_MODRDN:
+          dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
+          break;
+         case LDAP_REQ_COMPARE:
+          dissect_ldap_request_compare(&a, msg_tree);
+          break;
+         case LDAP_REQ_ABANDON:
+          dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
+          break;
+         case LDAP_RES_BIND:
+          dissect_ldap_response_bind(&a, msg_tree);
+          break;
+         case LDAP_RES_SEARCH_ENTRY:
+          dissect_ldap_response_search_entry(&a, msg_tree);
+          break;
+         case LDAP_RES_SEARCH_RESULT:
+         case LDAP_RES_MODIFY:
+         case LDAP_RES_ADD:
+         case LDAP_RES_DELETE:
+         case LDAP_RES_MODRDN:
+         case LDAP_RES_COMPARE:
+          dissect_ldap_result(&a, msg_tree);
+          break;
+         default:
+          proto_tree_add_text(msg_tree, a.tvb, a.offset, opLen,
+                             "Unknown LDAP operation (%u)", protocolOpTag);
+          break;
+        }
       }
     }
+
+    /*
+     * XXX - what if "a.offset" is past the offset of the next top-level
+     * sequence?  Show that as an error?
+     */
+    a.offset = next_offset;
   }
 }
 
@@ -960,6 +1354,7 @@ proto_register_ldap(void)
     {69, "Objectclass modification prohibited"},
     {71, "Affects multiple DSAs"},
     {80, "Other"},
+    {0,  NULL},
   };
 
   static value_string auth_types[] = {
@@ -967,12 +1362,14 @@ proto_register_ldap(void)
     {LDAP_AUTH_KRBV4LDAP, "Kerberos V4 to the LDAP server"},
     {LDAP_AUTH_KRBV4DSA,  "Kerberos V4 to the DSA"},
     {LDAP_AUTH_SASL,      "SASL"},
+    {0, NULL},
   };
   
   static value_string search_scope[] = {
     {0x00, "Base"},
     {0x01, "Single"},
     {0x02, "Subtree"},
+    {0x00, NULL},
   };
     
   static value_string search_dereference[] = {
@@ -980,137 +1377,138 @@ proto_register_ldap(void)
     {0x01, "Searching"},
     {0x02, "Base Object"},
     {0x03, "Always"},
+    {0x00, NULL},
   };
   
   static hf_register_info hf[] = {
     { &hf_ldap_length,
       { "Length",              "ldap.length",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Length" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Length", HFILL }},
          
     { &hf_ldap_message_id,
       { "Message Id",          "ldap.message_id",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Message Id" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Message Id", HFILL }},
     { &hf_ldap_message_type,
       { "Message Type",                "ldap.message_type",
        FT_UINT8, BASE_HEX, &msgTypes, 0x0,
-       "LDAP Message Type" }},
+       "LDAP Message Type", HFILL }},
     { &hf_ldap_message_length,
       { "Message Length",              "ldap.message_length",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Message Length" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Message Length", HFILL }},
 
     { &hf_ldap_message_result,
       { "Result Code",         "ldap.result.code",
-       FT_INT8, BASE_HEX, result_codes, 0x0,
-       "LDAP Result Code" }},
+       FT_UINT8, BASE_HEX, result_codes, 0x0,
+       "LDAP Result Code", HFILL }},
     { &hf_ldap_message_result_matcheddn,
       { "Matched DN",          "ldap.result.matcheddn",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Result Matched DN" }},
+       "LDAP Result Matched DN", HFILL }},
     { &hf_ldap_message_result_errormsg,
       { "Error Message",               "ldap.result.errormsg",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Result Error Message" }},
+       "LDAP Result Error Message", HFILL }},
     { &hf_ldap_message_result_referral,
       { "Referral",            "ldap.result.referral",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Result Referral URL" }},
+       "LDAP Result Referral URL", HFILL }},
 
     { &hf_ldap_message_bind_version,
       { "Version",             "ldap.bind.version",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Bind Version" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Bind Version", HFILL }},
     { &hf_ldap_message_bind_dn,
       { "DN",                  "ldap.bind.dn",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Bind Distinguished Name" }},
+       "LDAP Bind Distinguished Name", HFILL }},
     { &hf_ldap_message_bind_auth,
       { "Auth Type",           "ldap.bind.auth_type",
        FT_UINT8, BASE_HEX, auth_types, 0x0,
-       "LDAP Bind Auth Type" }},
+       "LDAP Bind Auth Type", HFILL }},
     { &hf_ldap_message_bind_auth_password,
       { "Password",            "ldap.bind.password",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Bind Password" }},
+       "LDAP Bind Password", HFILL }},
 
     { &hf_ldap_message_search_base,
       { "Base DN",             "ldap.search.basedn",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Search Base Distinguished Name" }},
+       "LDAP Search Base Distinguished Name", HFILL }},
     { &hf_ldap_message_search_scope,
       { "Scope",                       "ldap.search.scope",
        FT_UINT8, BASE_HEX, search_scope, 0x0,
-       "LDAP Search Scope" }},
+       "LDAP Search Scope", HFILL }},
     { &hf_ldap_message_search_deref,
       { "Dereference",         "ldap.search.dereference",
        FT_UINT8, BASE_HEX, search_dereference, 0x0,
-       "LDAP Search Dereference" }},
+       "LDAP Search Dereference", HFILL }},
     { &hf_ldap_message_search_sizeLimit,
       { "Size Limit",          "ldap.search.sizelimit",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Search Size Limit" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Search Size Limit", HFILL }},
     { &hf_ldap_message_search_timeLimit,
       { "Time Limit",          "ldap.search.timelimit",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Search Time Limit" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Search Time Limit", HFILL }},
     { &hf_ldap_message_search_typesOnly,
       { "Attributes Only",     "ldap.search.typesonly",
        FT_BOOLEAN, BASE_NONE, NULL, 0x0,
-       "LDAP Search Attributes Only" }},
+       "LDAP Search Attributes Only", HFILL }},
     { &hf_ldap_message_search_filter,
       { "Filter",              "ldap.search.filter",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Search Filter" }},
+       "LDAP Search Filter", HFILL }},
     { &hf_ldap_message_dn,
       { "Distinguished Name",  "ldap.dn",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Distinguished Name" }},
+       "LDAP Distinguished Name", HFILL }},
     { &hf_ldap_message_attribute,
       { "Attribute",           "ldap.attribute",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Attribute" }},
+       "LDAP Attribute", HFILL }},
     { &hf_ldap_message_value,
       { "Value",               "ldap.value",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Value" }},
+       "LDAP Value", HFILL }},
 
     { &hf_ldap_message_modrdn_name,
       { "New Name",            "ldap.modrdn.name",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP New Name" }},
+       "LDAP New Name", HFILL }},
     { &hf_ldap_message_modrdn_delete,
       { "Delete Values",       "ldap.modrdn.delete",
        FT_BOOLEAN, BASE_NONE, NULL, 0x0,
-       "LDAP Modify RDN - Delete original values" }},
+       "LDAP Modify RDN - Delete original values", HFILL }},
     { &hf_ldap_message_modrdn_superior,
       { "New Location",                "ldap.modrdn.superior",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Modify RDN - New Location" }},
+       "LDAP Modify RDN - New Location", HFILL }},
 
     { &hf_ldap_message_compare,
       { "Test",                "ldap.compare.test",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Compare Test" }},
+       "LDAP Compare Test", HFILL }},
 
     { &hf_ldap_message_modify_add,
       { "Add",                 "ldap.modify.add",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Add" }},
+       "LDAP Add", HFILL }},
     { &hf_ldap_message_modify_replace,
       { "Replace",             "ldap.modify.replace",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Replace" }},
+       "LDAP Replace", HFILL }},
     { &hf_ldap_message_modify_delete,
       { "Delete",              "ldap.modify.delete",
        FT_STRING, BASE_NONE, NULL, 0x0,
-       "LDAP Delete" }},
+       "LDAP Delete", HFILL }},
 
     { &hf_ldap_message_abandon_msgid,
       { "Abandon Msg Id",      "ldap.abandon.msgid",
-       FT_INT32, BASE_DEC, NULL, 0x0,
-       "LDAP Abandon Msg Id" }},
+       FT_UINT32, BASE_DEC, NULL, 0x0,
+       "LDAP Abandon Msg Id", HFILL }},
   };
 
   static gint *ett[] = {
@@ -1119,15 +1517,25 @@ proto_register_ldap(void)
     &ett_ldap_referrals,
     &ett_ldap_attribute
   };
+  module_t *ldap_module;
 
-  proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol", "ldap");
+  proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol",
+                                      "LDAP", "ldap");
   proto_register_field_array(proto_ldap, hf, array_length(hf));
   proto_register_subtree_array(ett, array_length(ett));
+
+  ldap_module = prefs_register_protocol(proto_ldap, NULL);
+  prefs_register_bool_preference(ldap_module, "desegment_ldap_messages",
+    "Desegment all LDAP messages spanning multiple TCP segments",
+    "Whether the LDAP dissector should desegment all messages spanning multiple TCP segments",
+    &ldap_desegment);
 }
 
 void
 proto_reg_handoff_ldap(void)
 {
-  dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap);
-}
+  dissector_handle_t ldap_handle;
 
+  ldap_handle = create_dissector_handle(dissect_ldap, proto_ldap);
+  dissector_add("tcp.port", TCP_PORT_LDAP, ldap_handle);
+}