2 * Routines for ldap packet dissection
4 * $Id: packet-ldap.c,v 1.20 2001/01/03 16:41:06 gram Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * This is not a complete implementation. It doesn't handle the full version 3, more specifically,
27 * it handles only the commands of version 2, but any additional characteristics of the ver3 command are supported.
28 * It's also missing extensible search filters.
30 * There should probably be alot more error checking, I simply assume that if we have a full packet, it will be a complete
33 * AFAIK, it will handle all messages used by the OpenLDAP 1.2.9 server and libraries which was my goal. I do plan to add
34 * the remaining commands as time permits but this is not a priority to me. Send me an email if you need it and I'll see what
38 * nazard@dragoninc.on.ca
47 #ifdef HAVE_SYS_TYPES_H
48 # include <sys/types.h>
51 #ifdef HAVE_NETINET_IN_H
52 # include <netinet/in.h>
58 #ifdef NEED_SNPRINTF_H
59 # include "snprintf.h"
64 #include "packet-ldap.h"
67 static int proto_ldap = -1;
68 static int hf_ldap_length = -1;
69 static int hf_ldap_message_id = -1;
70 static int hf_ldap_message_type = -1;
71 static int hf_ldap_message_length = -1;
73 static int hf_ldap_message_result = -1;
74 static int hf_ldap_message_result_matcheddn = -1;
75 static int hf_ldap_message_result_errormsg = -1;
76 static int hf_ldap_message_result_referral = -1;
78 static int hf_ldap_message_bind_version = -1;
79 static int hf_ldap_message_bind_dn = -1;
80 static int hf_ldap_message_bind_auth = -1;
81 static int hf_ldap_message_bind_auth_password = -1;
83 static int hf_ldap_message_search_base = -1;
84 static int hf_ldap_message_search_scope = -1;
85 static int hf_ldap_message_search_deref = -1;
86 static int hf_ldap_message_search_sizeLimit = -1;
87 static int hf_ldap_message_search_timeLimit = -1;
88 static int hf_ldap_message_search_typesOnly = -1;
89 static int hf_ldap_message_search_filter = -1;
91 static int hf_ldap_message_dn = -1;
92 static int hf_ldap_message_attribute = -1;
93 static int hf_ldap_message_value = -1;
95 static int hf_ldap_message_modrdn_name = -1;
96 static int hf_ldap_message_modrdn_delete = -1;
97 static int hf_ldap_message_modrdn_superior = -1;
99 static int hf_ldap_message_compare = -1;
101 static int hf_ldap_message_modify_add = -1;
102 static int hf_ldap_message_modify_replace = -1;
103 static int hf_ldap_message_modify_delete = -1;
105 static int hf_ldap_message_abandon_msgid = -1;
107 static gint ett_ldap = -1;
108 static gint ett_ldap_message = -1;
109 static gint ett_ldap_referrals = -1;
110 static gint ett_ldap_attribute = -1;
112 #define TCP_PORT_LDAP 389
114 static value_string msgTypes [] = {
115 {LDAP_REQ_BIND, "Bind Request"},
116 {LDAP_REQ_UNBIND, "Unbind Request"},
117 {LDAP_REQ_SEARCH, "Search Request"},
118 {LDAP_REQ_MODIFY, "Modify Request"},
119 {LDAP_REQ_ADD, "Add Request"},
120 {LDAP_REQ_DELETE, "Delete Request"},
121 {LDAP_REQ_MODRDN, "Modify RDN Request"},
122 {LDAP_REQ_COMPARE, "Compare Request"},
123 {LDAP_REQ_ABANDON, "Abandon Request"},
124 {LDAP_REQ_EXTENDED, "Extended Request"},
126 {LDAP_RES_BIND, "Bind Result"},
127 {LDAP_RES_SEARCH_ENTRY, "Search Entry"},
128 {LDAP_RES_SEARCH_RESULT, "Search Result"},
129 {LDAP_RES_SEARCH_REF, "Search Result Reference"},
130 {LDAP_RES_MODIFY, "Modify Result"},
131 {LDAP_RES_ADD, "Add Result"},
132 {LDAP_RES_DELETE, "Delete Result"},
133 {LDAP_RES_MODRDN, "Modify RDN Result"},
134 {LDAP_RES_COMPARE, "Compare Result"},
135 {LDAP_REQ_EXTENDED, "Extended Response"},
139 static int read_length(ASN1_SCK *a, proto_tree *tree, int hf_id, guint *len)
142 gboolean def = FALSE;
143 const guchar *start = a->pointer;
145 asn1_length_decode(a, &def, &length);
151 proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, length);
156 static int read_sequence(ASN1_SCK *a, guint *len)
162 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
164 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
173 static int read_set(ASN1_SCK *a, guint *len)
179 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
181 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SET)
190 static int read_integer_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
191 proto_tree **new_tree, guint *i, const guchar *start, guint length)
195 asn1_uint32_value_decode(a, length, &integer);
202 proto_tree *temp_tree = 0;
203 temp_tree = proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
205 *new_tree = temp_tree;
211 static int read_integer(ASN1_SCK *a, proto_tree *tree, int hf_id,
212 proto_tree **new_tree, guint *i, guint expected_tag)
217 const guchar *start = a->pointer;
219 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
221 if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
224 return read_integer_value(a, tree, hf_id, new_tree, i, start, length);
227 static int read_boolean_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
228 proto_tree **new_tree, guint *i, const guchar *start, guint length)
232 asn1_uint32_value_decode(a, length, &integer);
239 proto_tree *temp_tree = 0;
240 temp_tree = proto_tree_add_boolean(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
242 *new_tree = temp_tree;
248 static int read_boolean(ASN1_SCK *a, proto_tree *tree, int hf_id,
249 proto_tree **new_tree, guint *i)
254 const guchar *start = a->pointer;
256 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
258 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_BOL)
261 return read_boolean_value(a, tree, hf_id, new_tree, i, start, length);
264 static void read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
265 proto_tree **new_tree, char **s, const guchar *start, guint length)
271 asn1_string_value_decode(a, length, &string);
272 string = g_realloc(string, length + 1);
273 string[length] = '\0';
280 proto_tree *temp_tree;
281 temp_tree = proto_tree_add_string(tree, hf_id, NullTVB, start - a->begin, a->pointer - start, string);
283 *new_tree = temp_tree;
292 static int read_string(ASN1_SCK *a, proto_tree *tree, int hf_id,
293 proto_tree **new_tree, char **s, guint expected_cls, guint expected_tag)
298 const guchar *start = a->pointer;
301 ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
302 if (ret != ASN1_ERR_NOERROR)
304 if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
305 return ASN1_ERR_WRONG_TYPE;
307 read_string_value(a, tree, hf_id, new_tree, s, start, length);
308 return ASN1_ERR_NOERROR;
311 static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length, const guchar *operation)
316 guint string2_length;
321 ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
322 if (ret != ASN1_ERR_NOERROR)
324 ret = asn1_octet_string_decode(a, &string2, &string2_length, &string_bytes);
325 if (ret != ASN1_ERR_NOERROR)
327 *filter_length += 2 + strlen(operation) + string_length + string2_length;
328 *filter = g_realloc(*filter, *filter_length);
329 filterp = *filter + strlen(*filter);
331 if (string_length != 0) {
332 memcpy(filterp, string, string_length);
333 filterp += string_length;
335 strcpy(filterp, operation);
336 filterp += strlen(operation);
337 if (string2_length != 0) {
338 memcpy(filterp, string2, string2_length);
339 filterp += string2_length;
345 return ASN1_ERR_NOERROR;
348 /* Richard Dawe: To parse substring filters, I added this function. */
349 static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_length)
360 /* For ASN.1 parsing of octet strings */
366 ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
367 if (ret != ASN1_ERR_NOERROR)
370 ret = asn1_sequence_decode(a, &seq_len, &header_bytes);
371 if (ret != ASN1_ERR_NOERROR)
374 *filter_length += 2 + 1 + string_length;
375 *filter = g_realloc(*filter, *filter_length);
377 filterp = *filter + strlen(*filter);
379 if (string_length != 0) {
380 memcpy(filterp, string, string_length);
381 filterp += string_length;
387 /* Now decode seq_len's worth of octet strings. */
389 end = (guchar *) (a->pointer + seq_len);
391 while (a->pointer < end) {
392 /* Octet strings here are context-specific, which
393 * asn1_octet_string_decode() barfs on. Emulate it, but don't barf. */
394 ret = asn1_header_decode (a, &cls, &con, &tag, &def, &string_length);
395 if (ret != ASN1_ERR_NOERROR)
398 /* XXX - check the tag? */
399 if (cls != ASN1_CTX || con != ASN1_PRI) {
400 /* XXX - handle the constructed encoding? */
401 return ASN1_ERR_WRONG_TYPE;
404 return ASN1_ERR_LENGTH_NOT_DEFINITE;
406 ret = asn1_string_value_decode(a, (int) string_length, &string);
407 if (ret != ASN1_ERR_NOERROR)
410 /* If we have an 'any' component with a string value, we need to append
411 * an extra asterisk before final component. */
412 if ((tag == 1) && (string_length != 0))
415 if ( (tag == 1) || ((tag == 2) && any_valued) )
417 *filter_length += string_length;
418 *filter = g_realloc(*filter, *filter_length);
420 filterp = *filter + strlen(*filter);
421 if ( (tag == 1) || ((tag == 2) && any_valued) )
425 if (string_length != 0) {
426 memcpy(filterp, string, string_length);
427 filterp += string_length;
436 *filter = g_realloc(*filter, *filter_length);
437 filterp = *filter + strlen(*filter);
441 /* NB: Allocated byte for this earlier */
445 return ASN1_ERR_NOERROR;
448 /* Returns -1 if we're at the end, returns an ASN1_ERR value otherwise. */
449 static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const guchar **end)
456 ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
457 if (ret != ASN1_ERR_NOERROR)
462 *end = a->pointer + length;
464 *filter = g_malloc0(*filter_length);
467 if (cls == ASN1_CTX) /* XXX - handle other types as errors? */
471 case LDAP_FILTER_AND:
473 const guchar *add_end;
476 return ASN1_ERR_WRONG_TYPE;
477 add_end = a->pointer + length;
479 *filter = g_realloc(*filter, *filter_length);
480 strcat(*filter, "(&");
481 while ((ret = parse_filter(a, filter, filter_length, &add_end))
486 strcat(*filter, ")");
491 const guchar *or_end;
494 return ASN1_ERR_WRONG_TYPE;
495 or_end = a->pointer + length;
497 *filter = g_realloc(*filter, *filter_length);
498 strcat(*filter, "(|");
499 while ((ret = parse_filter(a, filter, filter_length, &or_end))
504 strcat(*filter, ")");
507 case LDAP_FILTER_NOT:
509 const guchar *not_end;
512 return ASN1_ERR_WRONG_TYPE;
513 not_end = a->pointer + length;
515 *filter = g_realloc(*filter, *filter_length);
516 strcat(*filter, "(!");
517 ret = parse_filter(a, filter, filter_length, ¬_end);
518 if (ret != -1 && ret != ASN1_ERR_NOERROR)
520 strcat(*filter, ")");
523 case LDAP_FILTER_EQUALITY:
525 return ASN1_ERR_WRONG_TYPE;
526 ret = parse_filter_strings(a, filter, filter_length, "=");
527 if (ret != -1 && ret != ASN1_ERR_NOERROR)
532 return ASN1_ERR_WRONG_TYPE;
533 ret = parse_filter_strings(a, filter, filter_length, ">=");
534 if (ret != -1 && ret != ASN1_ERR_NOERROR)
539 return ASN1_ERR_WRONG_TYPE;
540 ret = parse_filter_strings(a, filter, filter_length, "<=");
541 if (ret != -1 && ret != ASN1_ERR_NOERROR)
544 case LDAP_FILTER_APPROX:
546 return ASN1_ERR_WRONG_TYPE;
547 ret = parse_filter_strings(a, filter, filter_length, "~=");
548 if (ret != -1 && ret != ASN1_ERR_NOERROR)
551 case LDAP_FILTER_PRESENT:
557 return ASN1_ERR_WRONG_TYPE;
558 ret = asn1_string_value_decode(a, length, &string);
559 if (ret != ASN1_ERR_NOERROR)
561 *filter_length += 4 + length;
562 *filter = g_realloc(*filter, *filter_length);
563 filterp = *filter + strlen(*filter);
566 memcpy(filterp, string, length);
576 case LDAP_FILTER_SUBSTRINGS:
578 return ASN1_ERR_WRONG_TYPE;
579 /* Richard Dawe: Handle substrings */
580 ret = parse_filter_substrings(a, filter, filter_length);
581 if (ret != -1 && ret != ASN1_ERR_NOERROR)
585 return ASN1_ERR_WRONG_TYPE;
589 if (a->pointer == *end)
595 static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
597 const guchar *start = a->pointer;
599 guint filter_length = 0;
600 const guchar *end = 0;
603 while ((ret = parse_filter(a, &filter, &filter_length, &end))
609 proto_tree_add_text(tree, NullTVB, start-a->begin, 0,
610 "Error parsing filter (%d)", ret);
612 proto_tree_add_string(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, filter);
620 /********************************************************************************************/
622 static int dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
624 guint resultCode = 0;
626 read_integer(a, tree, hf_ldap_message_result, 0, &resultCode, ASN1_ENUM);
627 read_string(a, tree, hf_ldap_message_result_matcheddn, 0, 0, ASN1_UNI, ASN1_OTS);
628 read_string(a, tree, hf_ldap_message_result_errormsg, 0, 0, ASN1_UNI, ASN1_OTS);
630 if (resultCode == 10) /* Referral */
632 const guchar *start = a->pointer;
635 proto_tree *t, *referralTree;
637 read_sequence(a, &length);
638 t = proto_tree_add_text(tree, NullTVB, start-a->begin, length, "Referral URLs");
639 referralTree = proto_item_add_subtree(t, ett_ldap_referrals);
641 end = a->pointer + length;;
642 while (a->pointer < end)
643 read_string(a, referralTree, hf_ldap_message_result_referral, 0, 0, ASN1_UNI, ASN1_OTS);
649 static int dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
655 read_integer(a, tree, hf_ldap_message_bind_version, 0, 0, ASN1_INT);
656 read_string(a, tree, hf_ldap_message_bind_dn, 0, 0, ASN1_UNI, ASN1_OTS);
659 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
660 return 1; /* XXX - right return value for an error? */
662 return 1; /* RFCs 1777 and 2251 say these are context-specific types */
663 proto_tree_add_uint(tree, hf_ldap_message_bind_auth, NullTVB, start - a->begin,
664 a->pointer - start, tag);
667 case LDAP_AUTH_SIMPLE:
668 read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL, NULL,
672 /* For Kerberos V4, dissect it as a ticket. */
673 /* For SASL, dissect it as SaslCredentials. */
679 static int dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
681 dissect_ldap_result(a, tree);
682 /* FIXME: handle SASL data */
686 static int dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
692 read_string(a, tree, hf_ldap_message_search_base, 0, 0, ASN1_UNI, ASN1_OTS);
693 read_integer(a, tree, hf_ldap_message_search_scope, 0, 0, ASN1_ENUM);
694 read_integer(a, tree, hf_ldap_message_search_deref, 0, 0, ASN1_ENUM);
695 read_integer(a, tree, hf_ldap_message_search_sizeLimit, 0, 0, ASN1_INT);
696 read_integer(a, tree, hf_ldap_message_search_timeLimit, 0, 0, ASN1_INT);
697 read_boolean(a, tree, hf_ldap_message_search_typesOnly, 0, 0);
698 ret = read_filter(a, tree, hf_ldap_message_search_filter);
699 if (ret != ASN1_ERR_NOERROR)
701 read_sequence(a, &seq_length);
702 end = a->pointer + seq_length;
703 while (a->pointer < end) {
704 ret = read_string(a, tree, hf_ldap_message_attribute, 0, 0, ASN1_UNI, ASN1_OTS);
705 if (ret != ASN1_ERR_NOERROR)
708 return ASN1_ERR_NOERROR;
711 static int dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
714 const guchar *end_of_sequence;
716 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
717 read_sequence(a, &seq_length);
719 end_of_sequence = a->pointer + seq_length;
720 while (a->pointer < end_of_sequence)
722 proto_tree *t, *attr_tree;
724 const guchar *end_of_set;
727 read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
728 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
730 read_set(a, &set_length);
731 end_of_set = a->pointer + set_length;
732 while (a->pointer < end_of_set)
733 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
739 static int dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
742 const guchar *end_of_sequence;
744 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
746 read_sequence(a, &seq_length);
747 end_of_sequence = a->pointer + seq_length;
748 while (a->pointer < end_of_sequence)
750 proto_tree *t, *attr_tree;
752 const guchar *end_of_set;
755 read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
756 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
758 read_set(a, &set_length);
759 end_of_set = a->pointer + set_length;
760 while (a->pointer < end_of_set)
761 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
767 static int dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
768 const guchar *start, guint length)
770 read_string_value(a, tree, hf_ldap_message_dn, NULL, NULL, start, length);
774 static int dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
777 const guchar *start = a->pointer;
779 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
780 read_string(a, tree, hf_ldap_message_modrdn_name, 0, 0, ASN1_UNI, ASN1_OTS);
781 read_boolean(a, tree, hf_ldap_message_modrdn_delete, 0, 0);
783 if (a->pointer < (start + length)) {
784 /* LDAP V3 Modify DN operation, with newSuperior */
785 read_string(a, tree, hf_ldap_message_modrdn_superior, 0, 0, ASN1_UNI, ASN1_OTS);
791 static int dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
799 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
803 read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
804 read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
806 length = 2 + strlen(string1) + strlen(string2);
807 compare = g_malloc0(length);
808 snprintf(compare, length, "%s=%s", string1, string2);
809 proto_tree_add_string(tree, hf_ldap_message_compare, NullTVB, start-a->begin, a->pointer-start, compare);
818 static int dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
821 const guchar *end_of_sequence;
823 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
824 read_sequence(a, &seq_length);
825 end_of_sequence = a->pointer + seq_length;
826 while (a->pointer < end_of_sequence)
828 proto_tree *t = 0, *attr_tree;
830 const guchar *end_of_set;
834 read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
840 read_string(a, tree, hf_ldap_message_modify_add, &t, 0, ASN1_UNI, ASN1_OTS);
842 case LDAP_MOD_REPLACE:
843 read_string(a, tree, hf_ldap_message_modify_replace, &t, 0, ASN1_UNI, ASN1_OTS);
845 case LDAP_MOD_DELETE:
846 read_string(a, tree, hf_ldap_message_modify_delete, &t, 0, ASN1_UNI, ASN1_OTS);
849 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
851 read_set(a, &set_length);
852 end_of_set = a->pointer + set_length;
853 while (a->pointer < end_of_set)
854 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
860 static int dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
861 const guchar *start, guint length)
863 read_integer_value(a, tree, hf_ldap_message_abandon_msgid, NULL, NULL,
869 dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
871 proto_tree *ldap_tree = 0, *ti, *msg_tree;
874 guint protocolOpCls, protocolOpCon, protocolOpTag;
882 OLD_CHECK_DISPLAY_AS_DATA(proto_ldap, pd, offset, fd, tree);
886 ti = proto_tree_add_item(tree, proto_ldap, NullTVB, offset, END_OF_FRAME, FALSE);
887 ldap_tree = proto_item_add_subtree(ti, ett_ldap);
890 asn1_open(&a, pd, pi.captured_len);
893 while (a.pointer < a.end)
895 int message_id_start;
896 int message_id_length;
899 message_start = a.pointer - a.begin;
900 if (read_sequence(&a, &messageLength))
903 proto_tree_add_text(ldap_tree, NullTVB, offset, 1, "Invalid LDAP packet");
907 if (messageLength > (a.end - a.pointer))
910 proto_tree_add_text(ldap_tree, NullTVB, message_start, END_OF_FRAME, "Short message! (expected: %u, actual: %u)",
911 messageLength, a.end - a.pointer);
915 message_id_start = a.pointer - a.begin;
916 read_integer(&a, 0, -1, 0, &messageId, ASN1_INT);
917 message_id_length = (a.pointer - a.begin) - message_id_start;
920 asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
921 if (protocolOpCls != ASN1_APL)
922 typestr = "Bad message type (not Application)";
924 typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
928 if (check_col(fd, COL_PROTOCOL))
929 col_set_str(fd, COL_PROTOCOL, "LDAP");
931 if (check_col(fd, COL_INFO))
932 col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
941 proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_id, NullTVB, message_id_start, message_id_length, messageId);
942 proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_type, NullTVB,
943 start - a.begin, a.pointer - start, protocolOpTag);
944 ti = proto_tree_add_text(ldap_tree, NullTVB, message_id_start, messageLength, "Message: Id=%u %s", messageId, typestr);
945 msg_tree = proto_item_add_subtree(ti, ett_ldap_message);
947 read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
949 switch (protocolOpTag)
952 dissect_ldap_request_bind(&a, msg_tree);
954 case LDAP_REQ_SEARCH:
955 ret = dissect_ldap_request_search(&a, msg_tree);
956 if (ret != ASN1_ERR_NOERROR)
960 dissect_ldap_request_add(&a, msg_tree);
962 case LDAP_REQ_DELETE:
963 dissect_ldap_request_delete(&a, msg_tree, start, opLen);
965 case LDAP_REQ_MODRDN:
966 dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
968 case LDAP_REQ_COMPARE:
969 dissect_ldap_request_compare(&a, msg_tree);
971 case LDAP_REQ_MODIFY:
972 dissect_ldap_request_modify(&a, msg_tree);
974 case LDAP_REQ_ABANDON:
975 dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
978 dissect_ldap_response_bind(&a, msg_tree);
980 case LDAP_RES_SEARCH_ENTRY:
981 dissect_ldap_response_search_entry(&a, msg_tree);
983 case LDAP_RES_SEARCH_RESULT:
984 case LDAP_RES_MODIFY:
986 case LDAP_RES_DELETE:
987 case LDAP_RES_MODRDN:
988 case LDAP_RES_COMPARE:
989 dissect_ldap_result(&a, msg_tree);
997 proto_register_ldap(void)
999 static value_string result_codes[] = {
1001 {1, "Operations error"},
1002 {2, "Protocol error"},
1003 {3, "Time limit exceeded"},
1004 {4, "Size limit exceeded"},
1005 {5, "Compare false"},
1006 {6, "Compare true"},
1007 {7, "Authentication method not supported"},
1008 {8, "Strong authentication required"},
1010 {11, "Administrative limit exceeded"},
1011 {12, "Unavailable critical extension"},
1012 {13, "Confidentiality required"},
1013 {14, "SASL bind in progress"},
1014 {16, "No such attribute"},
1015 {17, "Undefined attribute type"},
1016 {18, "Inappropriate matching"},
1017 {19, "Constraint violation"},
1018 {20, "Attribute or value exists"},
1019 {21, "Invalid attribute syntax"},
1020 {32, "No such object"},
1021 {33, "Alias problem"},
1022 {34, "Invalid DN syntax"},
1023 {36, "Alias derefetencing problem"},
1024 {48, "Inappropriate authentication"},
1025 {49, "Invalid credentials"},
1026 {50, "Insufficient access rights"},
1028 {52, "Unavailable"},
1029 {53, "Unwilling to perform"},
1030 {54, "Loop detected"},
1031 {64, "Naming violation"},
1032 {65, "Objectclass violation"},
1033 {66, "Not allowed on non-leaf"},
1034 {67, "Not allowed on RDN"},
1035 {68, "Entry already exists"},
1036 {69, "Objectclass modification prohibited"},
1037 {71, "Affects multiple DSAs"},
1042 static value_string auth_types[] = {
1043 {LDAP_AUTH_SIMPLE, "Simple"},
1044 {LDAP_AUTH_KRBV4LDAP, "Kerberos V4 to the LDAP server"},
1045 {LDAP_AUTH_KRBV4DSA, "Kerberos V4 to the DSA"},
1046 {LDAP_AUTH_SASL, "SASL"},
1050 static value_string search_scope[] = {
1057 static value_string search_dereference[] = {
1059 {0x01, "Searching"},
1060 {0x02, "Base Object"},
1065 static hf_register_info hf[] = {
1067 { "Length", "ldap.length",
1068 FT_UINT32, BASE_DEC, NULL, 0x0,
1071 { &hf_ldap_message_id,
1072 { "Message Id", "ldap.message_id",
1073 FT_UINT32, BASE_DEC, NULL, 0x0,
1074 "LDAP Message Id" }},
1075 { &hf_ldap_message_type,
1076 { "Message Type", "ldap.message_type",
1077 FT_UINT8, BASE_HEX, &msgTypes, 0x0,
1078 "LDAP Message Type" }},
1079 { &hf_ldap_message_length,
1080 { "Message Length", "ldap.message_length",
1081 FT_UINT32, BASE_DEC, NULL, 0x0,
1082 "LDAP Message Length" }},
1084 { &hf_ldap_message_result,
1085 { "Result Code", "ldap.result.code",
1086 FT_UINT8, BASE_HEX, result_codes, 0x0,
1087 "LDAP Result Code" }},
1088 { &hf_ldap_message_result_matcheddn,
1089 { "Matched DN", "ldap.result.matcheddn",
1090 FT_STRING, BASE_NONE, NULL, 0x0,
1091 "LDAP Result Matched DN" }},
1092 { &hf_ldap_message_result_errormsg,
1093 { "Error Message", "ldap.result.errormsg",
1094 FT_STRING, BASE_NONE, NULL, 0x0,
1095 "LDAP Result Error Message" }},
1096 { &hf_ldap_message_result_referral,
1097 { "Referral", "ldap.result.referral",
1098 FT_STRING, BASE_NONE, NULL, 0x0,
1099 "LDAP Result Referral URL" }},
1101 { &hf_ldap_message_bind_version,
1102 { "Version", "ldap.bind.version",
1103 FT_UINT32, BASE_DEC, NULL, 0x0,
1104 "LDAP Bind Version" }},
1105 { &hf_ldap_message_bind_dn,
1106 { "DN", "ldap.bind.dn",
1107 FT_STRING, BASE_NONE, NULL, 0x0,
1108 "LDAP Bind Distinguished Name" }},
1109 { &hf_ldap_message_bind_auth,
1110 { "Auth Type", "ldap.bind.auth_type",
1111 FT_UINT8, BASE_HEX, auth_types, 0x0,
1112 "LDAP Bind Auth Type" }},
1113 { &hf_ldap_message_bind_auth_password,
1114 { "Password", "ldap.bind.password",
1115 FT_STRING, BASE_NONE, NULL, 0x0,
1116 "LDAP Bind Password" }},
1118 { &hf_ldap_message_search_base,
1119 { "Base DN", "ldap.search.basedn",
1120 FT_STRING, BASE_NONE, NULL, 0x0,
1121 "LDAP Search Base Distinguished Name" }},
1122 { &hf_ldap_message_search_scope,
1123 { "Scope", "ldap.search.scope",
1124 FT_UINT8, BASE_HEX, search_scope, 0x0,
1125 "LDAP Search Scope" }},
1126 { &hf_ldap_message_search_deref,
1127 { "Dereference", "ldap.search.dereference",
1128 FT_UINT8, BASE_HEX, search_dereference, 0x0,
1129 "LDAP Search Dereference" }},
1130 { &hf_ldap_message_search_sizeLimit,
1131 { "Size Limit", "ldap.search.sizelimit",
1132 FT_UINT32, BASE_DEC, NULL, 0x0,
1133 "LDAP Search Size Limit" }},
1134 { &hf_ldap_message_search_timeLimit,
1135 { "Time Limit", "ldap.search.timelimit",
1136 FT_UINT32, BASE_DEC, NULL, 0x0,
1137 "LDAP Search Time Limit" }},
1138 { &hf_ldap_message_search_typesOnly,
1139 { "Attributes Only", "ldap.search.typesonly",
1140 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1141 "LDAP Search Attributes Only" }},
1142 { &hf_ldap_message_search_filter,
1143 { "Filter", "ldap.search.filter",
1144 FT_STRING, BASE_NONE, NULL, 0x0,
1145 "LDAP Search Filter" }},
1146 { &hf_ldap_message_dn,
1147 { "Distinguished Name", "ldap.dn",
1148 FT_STRING, BASE_NONE, NULL, 0x0,
1149 "LDAP Distinguished Name" }},
1150 { &hf_ldap_message_attribute,
1151 { "Attribute", "ldap.attribute",
1152 FT_STRING, BASE_NONE, NULL, 0x0,
1153 "LDAP Attribute" }},
1154 { &hf_ldap_message_value,
1155 { "Value", "ldap.value",
1156 FT_STRING, BASE_NONE, NULL, 0x0,
1159 { &hf_ldap_message_modrdn_name,
1160 { "New Name", "ldap.modrdn.name",
1161 FT_STRING, BASE_NONE, NULL, 0x0,
1163 { &hf_ldap_message_modrdn_delete,
1164 { "Delete Values", "ldap.modrdn.delete",
1165 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1166 "LDAP Modify RDN - Delete original values" }},
1167 { &hf_ldap_message_modrdn_superior,
1168 { "New Location", "ldap.modrdn.superior",
1169 FT_STRING, BASE_NONE, NULL, 0x0,
1170 "LDAP Modify RDN - New Location" }},
1172 { &hf_ldap_message_compare,
1173 { "Test", "ldap.compare.test",
1174 FT_STRING, BASE_NONE, NULL, 0x0,
1175 "LDAP Compare Test" }},
1177 { &hf_ldap_message_modify_add,
1178 { "Add", "ldap.modify.add",
1179 FT_STRING, BASE_NONE, NULL, 0x0,
1181 { &hf_ldap_message_modify_replace,
1182 { "Replace", "ldap.modify.replace",
1183 FT_STRING, BASE_NONE, NULL, 0x0,
1185 { &hf_ldap_message_modify_delete,
1186 { "Delete", "ldap.modify.delete",
1187 FT_STRING, BASE_NONE, NULL, 0x0,
1190 { &hf_ldap_message_abandon_msgid,
1191 { "Abandon Msg Id", "ldap.abandon.msgid",
1192 FT_UINT32, BASE_DEC, NULL, 0x0,
1193 "LDAP Abandon Msg Id" }},
1196 static gint *ett[] = {
1199 &ett_ldap_referrals,
1203 proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol",
1205 proto_register_field_array(proto_ldap, hf, array_length(hf));
1206 proto_register_subtree_array(ett, array_length(ett));
1210 proto_reg_handoff_ldap(void)
1212 old_dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap);