2 * Routines for ldap packet dissection
4 * $Id: packet-ldap.c,v 1.14 2000/08/11 13:35:15 deniel 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"},
138 static int read_length(ASN1_SCK *a, proto_tree *tree, int hf_id, guint *len)
141 gboolean def = FALSE;
142 const guchar *start = a->pointer;
144 asn1_length_decode(a, &def, &length);
150 proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, length);
155 static int read_sequence(ASN1_SCK *a, guint *len)
161 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
163 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
172 static int read_set(ASN1_SCK *a, guint *len)
178 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
180 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SET)
189 static int read_integer_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
190 proto_tree **new_tree, guint *i, const guchar *start, guint length)
194 asn1_uint32_value_decode(a, length, &integer);
201 proto_tree *temp_tree = 0;
202 temp_tree = proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
204 *new_tree = temp_tree;
210 static int read_integer(ASN1_SCK *a, proto_tree *tree, int hf_id,
211 proto_tree **new_tree, guint *i, guint expected_tag)
216 const guchar *start = a->pointer;
218 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
220 if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
223 return read_integer_value(a, tree, hf_id, new_tree, i, start, length);
226 static int read_boolean_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
227 proto_tree **new_tree, guint *i, const guchar *start, guint length)
231 asn1_uint32_value_decode(a, length, &integer);
238 proto_tree *temp_tree = 0;
239 temp_tree = proto_tree_add_boolean(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
241 *new_tree = temp_tree;
247 static int read_boolean(ASN1_SCK *a, proto_tree *tree, int hf_id,
248 proto_tree **new_tree, guint *i)
253 const guchar *start = a->pointer;
255 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
257 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_BOL)
260 return read_boolean_value(a, tree, hf_id, new_tree, i, start, length);
263 static void read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
264 proto_tree **new_tree, char **s, const guchar *start, guint length)
270 asn1_octet_string_value_decode(a, length, &string);
271 string = g_realloc(string, length + 1);
272 string[length] = '\0';
279 proto_tree *temp_tree;
280 temp_tree = proto_tree_add_string(tree, hf_id, NullTVB, start - a->begin, a->pointer - start, string);
282 *new_tree = temp_tree;
291 static int read_string(ASN1_SCK *a, proto_tree *tree, int hf_id,
292 proto_tree **new_tree, char **s, guint expected_cls, guint expected_tag)
297 const guchar *start = a->pointer;
300 ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
301 if (ret != ASN1_ERR_NOERROR)
303 if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
304 return ASN1_ERR_WRONG_TYPE;
306 read_string_value(a, tree, hf_id, new_tree, s, start, length);
307 return ASN1_ERR_NOERROR;
310 static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length, const guchar *operation)
319 ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
320 if (ret != ASN1_ERR_NOERROR)
322 ret = asn1_octet_string_decode(a, &string2, &string2_length, &string_bytes);
323 if (ret != ASN1_ERR_NOERROR)
325 *filter_length += 2 + strlen(operation) + string_length + string2_length;
326 *filter = g_realloc(*filter, *filter_length);
327 sprintf(*filter + strlen(*filter), "(%.*s%s%.*s)", string_length, string, operation, string2_length, string2);
330 return ASN1_ERR_NOERROR;
333 /* Richard Dawe: To parse substring filters, I added this function. */
334 static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_length)
337 guchar *string = NULL;
344 /* For ASN.1 parsing of octet strings */
350 ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
351 if (ret != ASN1_ERR_NOERROR)
354 ret = asn1_sequence_decode(a, &seq_len, &header_bytes);
355 if (ret != ASN1_ERR_NOERROR)
358 *filter_length += 2 + 1 + strlen(string);
359 *filter = g_realloc(*filter, *filter_length);
360 sprintf(*filter + strlen(*filter), "(%.*s=", string_length, string);
363 /* Now decode seq_len's worth of octet strings. */
365 end = (guchar *) (a->pointer + seq_len);
367 while (a->pointer < end) {
368 /* Octet strings here are context-specific, which
369 * asn1_octet_string_decode() barfs on. Emulate it, but don't barf. */
370 ret = asn1_header_decode (a, &cls, &con, &tag, &def, &string_length);
371 if (ret != ASN1_ERR_NOERROR)
374 if (cls != ASN1_CTX || con != ASN1_PRI) {
375 /* XXX - handle the constructed encoding? */
376 return ASN1_ERR_WRONG_TYPE;
379 return ASN1_ERR_LENGTH_NOT_DEFINITE;
381 ret = asn1_octet_string_value_decode(a, (int) string_length, &string);
382 if (ret != ASN1_ERR_NOERROR)
385 /* If we have an 'any' component with a string value, we need to append
386 * an extra asterisk before final component. */
387 if ((tag == 1) && (string_length > 0))
390 if ( (tag == 1) || ((tag == 2) && any_valued) )
392 *filter_length += strlen(string);
393 *filter = g_realloc(*filter, *filter_length);
395 if ( (tag == 1) || ((tag == 2) && any_valued) )
396 strcat(*filter, "*");
399 sprintf(*filter + strlen(*filter), "%.*s", string_length, string);
406 *filter = g_realloc(*filter, *filter_length);
407 strcat(*filter, "*");
410 /* NB: Allocated byte for this earlier */
411 strcat(*filter, ")");
413 return ASN1_ERR_NOERROR;
416 /* Returns -1 if we're at the end, returns an ASN1_ERR value otherwise. */
417 static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const guchar **end)
424 ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
425 if (ret != ASN1_ERR_NOERROR)
430 *end = a->pointer + length;
432 *filter = g_malloc0(*filter_length);
435 if (cls == ASN1_CTX) /* XXX - handle other types as errors? */
439 case LDAP_FILTER_AND:
441 const guchar *add_end;
444 return ASN1_ERR_WRONG_TYPE;
445 add_end = a->pointer + length;
447 *filter = g_realloc(*filter, *filter_length);
448 strcat(*filter, "(&");
449 while ((ret = parse_filter(a, filter, filter_length, &add_end))
454 strcat(*filter, ")");
459 const guchar *or_end;
462 return ASN1_ERR_WRONG_TYPE;
463 or_end = a->pointer + length;
465 *filter = g_realloc(*filter, *filter_length);
466 strcat(*filter, "(|");
467 while ((ret = parse_filter(a, filter, filter_length, &or_end))
472 strcat(*filter, ")");
475 case LDAP_FILTER_NOT:
477 const guchar *not_end;
480 return ASN1_ERR_WRONG_TYPE;
481 not_end = a->pointer + length;
483 *filter = g_realloc(*filter, *filter_length);
484 strcat(*filter, "(!");
485 ret = parse_filter(a, filter, filter_length, ¬_end);
486 if (ret != -1 && ret != ASN1_ERR_NOERROR)
488 strcat(*filter, ")");
491 case LDAP_FILTER_EQUALITY:
493 return ASN1_ERR_WRONG_TYPE;
494 ret = parse_filter_strings(a, filter, filter_length, "=");
495 if (ret != -1 && ret != ASN1_ERR_NOERROR)
500 return ASN1_ERR_WRONG_TYPE;
501 ret = parse_filter_strings(a, filter, filter_length, ">=");
502 if (ret != -1 && ret != ASN1_ERR_NOERROR)
507 return ASN1_ERR_WRONG_TYPE;
508 ret = parse_filter_strings(a, filter, filter_length, "<=");
509 if (ret != -1 && ret != ASN1_ERR_NOERROR)
512 case LDAP_FILTER_APPROX:
514 return ASN1_ERR_WRONG_TYPE;
515 ret = parse_filter_strings(a, filter, filter_length, "~=");
516 if (ret != -1 && ret != ASN1_ERR_NOERROR)
519 case LDAP_FILTER_PRESENT:
524 return ASN1_ERR_WRONG_TYPE;
525 ret = asn1_octet_string_value_decode(a, length, &string);
526 if (ret != ASN1_ERR_NOERROR)
528 *filter_length += 4 + length;
529 *filter = g_realloc(*filter, *filter_length);
530 sprintf(*filter + strlen(*filter), "(%.*s=*)", (int)length, string);
534 case LDAP_FILTER_SUBSTRINGS:
536 return ASN1_ERR_WRONG_TYPE;
537 /* Richard Dawe: Handle substrings */
538 ret = parse_filter_substrings(a, filter, filter_length);
539 if (ret != -1 && ret != ASN1_ERR_NOERROR)
543 return ASN1_ERR_WRONG_TYPE;
547 if (a->pointer == *end)
553 static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
555 const guchar *start = a->pointer;
557 guint filter_length = 0;
558 const guchar *end = 0;
561 while ((ret = parse_filter(a, &filter, &filter_length, &end))
567 proto_tree_add_text(tree, NullTVB, start-a->begin, 0,
568 "Error parsing filter (%d)", ret);
570 proto_tree_add_string(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, filter);
578 /********************************************************************************************/
580 static int dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
582 guint resultCode = 0;
584 read_integer(a, tree, hf_ldap_message_result, 0, &resultCode, ASN1_ENUM);
585 read_string(a, tree, hf_ldap_message_result_matcheddn, 0, 0, ASN1_UNI, ASN1_OTS);
586 read_string(a, tree, hf_ldap_message_result_errormsg, 0, 0, ASN1_UNI, ASN1_OTS);
588 if (resultCode == 10) /* Referral */
590 const guchar *start = a->pointer;
593 proto_tree *t, *referralTree;
595 read_sequence(a, &length);
596 t = proto_tree_add_text(tree, NullTVB, start-a->begin, length, "Referral URLs");
597 referralTree = proto_item_add_subtree(t, ett_ldap_referrals);
599 end = a->pointer + length;;
600 while (a->pointer < end)
601 read_string(a, referralTree, hf_ldap_message_result_referral, 0, 0, ASN1_UNI, ASN1_OTS);
607 static int dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
613 read_integer(a, tree, hf_ldap_message_bind_version, 0, 0, ASN1_INT);
614 read_string(a, tree, hf_ldap_message_bind_dn, 0, 0, ASN1_UNI, ASN1_OTS);
617 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
618 return 1; /* XXX - right return value for an error? */
620 return 1; /* RFCs 1777 and 2251 say these are context-specific types */
621 proto_tree_add_uint(tree, hf_ldap_message_bind_auth, NullTVB, start - a->begin,
622 a->pointer - start, tag);
625 case LDAP_AUTH_SIMPLE:
626 read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL, NULL,
630 /* For Kerberos V4, dissect it as a ticket. */
631 /* For SASL, dissect it as SaslCredentials. */
637 static int dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
639 dissect_ldap_result(a, tree);
640 /* FIXME: handle SASL data */
644 static int dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
650 read_string(a, tree, hf_ldap_message_search_base, 0, 0, ASN1_UNI, ASN1_OTS);
651 read_integer(a, tree, hf_ldap_message_search_scope, 0, 0, ASN1_ENUM);
652 read_integer(a, tree, hf_ldap_message_search_deref, 0, 0, ASN1_ENUM);
653 read_integer(a, tree, hf_ldap_message_search_sizeLimit, 0, 0, ASN1_INT);
654 read_integer(a, tree, hf_ldap_message_search_timeLimit, 0, 0, ASN1_INT);
655 read_boolean(a, tree, hf_ldap_message_search_typesOnly, 0, 0);
656 ret = read_filter(a, tree, hf_ldap_message_search_filter);
657 if (ret != ASN1_ERR_NOERROR)
659 read_sequence(a, &seq_length);
660 end = a->pointer + seq_length;
661 while (a->pointer < end) {
662 ret = read_string(a, tree, hf_ldap_message_attribute, 0, 0, ASN1_UNI, ASN1_OTS);
663 if (ret != ASN1_ERR_NOERROR)
666 return ASN1_ERR_NOERROR;
669 static int dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
672 const guchar *end_of_sequence;
674 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
675 read_sequence(a, &seq_length);
677 end_of_sequence = a->pointer + seq_length;
678 while (a->pointer < end_of_sequence)
680 proto_tree *t, *attr_tree;
682 const guchar *end_of_set;
685 read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
686 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
688 read_set(a, &set_length);
689 end_of_set = a->pointer + set_length;
690 while (a->pointer < end_of_set)
691 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
697 static int dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
700 const guchar *end_of_sequence;
702 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
704 read_sequence(a, &seq_length);
705 end_of_sequence = a->pointer + seq_length;
706 while (a->pointer < end_of_sequence)
708 proto_tree *t, *attr_tree;
710 const guchar *end_of_set;
713 read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
714 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
716 read_set(a, &set_length);
717 end_of_set = a->pointer + set_length;
718 while (a->pointer < end_of_set)
719 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
725 static int dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
726 const guchar *start, guint length)
728 read_string_value(a, tree, hf_ldap_message_dn, NULL, NULL, start, length);
732 static int dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
735 const guchar *start = a->pointer;
737 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
738 read_string(a, tree, hf_ldap_message_modrdn_name, 0, 0, ASN1_UNI, ASN1_OTS);
739 read_boolean(a, tree, hf_ldap_message_modrdn_delete, 0, 0);
741 if (a->pointer < (start + length)) {
742 /* LDAP V3 Modify DN operation, with newSuperior */
743 read_string(a, tree, hf_ldap_message_modrdn_superior, 0, 0, ASN1_UNI, ASN1_OTS);
749 static int dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
757 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
761 read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
762 read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
764 length = 2 + strlen(string1) + strlen(string2);
765 compare = g_malloc0(length);
766 snprintf(compare, length, "%s=%s", string1, string2);
767 proto_tree_add_string(tree, hf_ldap_message_compare, NullTVB, start-a->begin, a->pointer-start, compare);
776 static int dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
779 const guchar *end_of_sequence;
781 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
782 read_sequence(a, &seq_length);
783 end_of_sequence = a->pointer + seq_length;
784 while (a->pointer < end_of_sequence)
786 proto_tree *t = 0, *attr_tree;
788 const guchar *end_of_set;
792 read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
798 read_string(a, tree, hf_ldap_message_modify_add, &t, 0, ASN1_UNI, ASN1_OTS);
800 case LDAP_MOD_REPLACE:
801 read_string(a, tree, hf_ldap_message_modify_replace, &t, 0, ASN1_UNI, ASN1_OTS);
803 case LDAP_MOD_DELETE:
804 read_string(a, tree, hf_ldap_message_modify_delete, &t, 0, ASN1_UNI, ASN1_OTS);
807 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
809 read_set(a, &set_length);
810 end_of_set = a->pointer + set_length;
811 while (a->pointer < end_of_set)
812 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
818 static int dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
819 const guchar *start, guint length)
821 read_integer_value(a, tree, hf_ldap_message_abandon_msgid, NULL, NULL,
827 dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
829 proto_tree *ldap_tree = 0, *ti, *msg_tree;
832 guint protocolOpCls, protocolOpCon, protocolOpTag;
842 ti = proto_tree_add_item(tree, proto_ldap, NullTVB, offset, END_OF_FRAME, FALSE);
843 ldap_tree = proto_item_add_subtree(ti, ett_ldap);
846 asn1_open(&a, pd, pi.captured_len);
849 while (a.pointer < a.end)
851 int message_id_start;
852 int message_id_length;
855 message_start = a.pointer - a.begin;
856 if (read_sequence(&a, &messageLength))
859 proto_tree_add_text(ldap_tree, NullTVB, offset, 1, "Invalid LDAP packet");
863 if (messageLength > (a.end - a.pointer))
866 proto_tree_add_text(ldap_tree, NullTVB, message_start, END_OF_FRAME, "Short message! (expected: %u, actual: %u)",
867 messageLength, a.end - a.pointer);
871 message_id_start = a.pointer - a.begin;
872 read_integer(&a, 0, -1, 0, &messageId, ASN1_INT);
873 message_id_length = (a.pointer - a.begin) - message_id_start;
876 asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
877 if (protocolOpCls != ASN1_APL)
878 typestr = "Bad message type (not Application)";
880 typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
884 if (check_col(fd, COL_PROTOCOL))
885 col_add_str(fd, COL_PROTOCOL, "LDAP");
887 if (check_col(fd, COL_INFO))
888 col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
897 proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_id, NullTVB, message_id_start, message_id_length, messageId);
898 proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_type, NullTVB,
899 start - a.begin, a.pointer - start, protocolOpTag);
900 ti = proto_tree_add_text(ldap_tree, NullTVB, message_id_start, messageLength, "Message: Id=%u %s", messageId, typestr);
901 msg_tree = proto_item_add_subtree(ti, ett_ldap_message);
903 read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
905 switch (protocolOpTag)
908 dissect_ldap_request_bind(&a, msg_tree);
910 case LDAP_REQ_SEARCH:
911 ret = dissect_ldap_request_search(&a, msg_tree);
912 if (ret != ASN1_ERR_NOERROR)
916 dissect_ldap_request_add(&a, msg_tree);
918 case LDAP_REQ_DELETE:
919 dissect_ldap_request_delete(&a, msg_tree, start, opLen);
921 case LDAP_REQ_MODRDN:
922 dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
924 case LDAP_REQ_COMPARE:
925 dissect_ldap_request_compare(&a, msg_tree);
927 case LDAP_REQ_MODIFY:
928 dissect_ldap_request_modify(&a, msg_tree);
930 case LDAP_REQ_ABANDON:
931 dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
934 dissect_ldap_response_bind(&a, msg_tree);
936 case LDAP_RES_SEARCH_ENTRY:
937 dissect_ldap_response_search_entry(&a, msg_tree);
939 case LDAP_RES_SEARCH_RESULT:
940 case LDAP_RES_MODIFY:
942 case LDAP_RES_DELETE:
943 case LDAP_RES_MODRDN:
944 case LDAP_RES_COMPARE:
945 dissect_ldap_result(&a, msg_tree);
953 proto_register_ldap(void)
955 static value_string result_codes[] = {
957 {1, "Operations error"},
958 {2, "Protocol error"},
959 {3, "Time limit exceeded"},
960 {4, "Size limit exceeded"},
961 {5, "Compare false"},
963 {7, "Authentication method not supported"},
964 {8, "Strong authentication required"},
966 {11, "Administrative limit exceeded"},
967 {12, "Unavailable critical extension"},
968 {13, "Confidentiality required"},
969 {14, "SASL bind in progress"},
970 {16, "No such attribute"},
971 {17, "Undefined attribute type"},
972 {18, "Inappropriate matching"},
973 {19, "Constraint violation"},
974 {20, "Attribute or value exists"},
975 {21, "Invalid attribute syntax"},
976 {32, "No such object"},
977 {33, "Alias problem"},
978 {34, "Invalid DN syntax"},
979 {36, "Alias derefetencing problem"},
980 {48, "Inappropriate authentication"},
981 {49, "Invalid credentials"},
982 {50, "Insufficient access rights"},
985 {53, "Unwilling to perform"},
986 {54, "Loop detected"},
987 {64, "Naming violation"},
988 {65, "Objectclass violation"},
989 {66, "Not allowed on non-leaf"},
990 {67, "Not allowed on RDN"},
991 {68, "Entry already exists"},
992 {69, "Objectclass modification prohibited"},
993 {71, "Affects multiple DSAs"},
997 static value_string auth_types[] = {
998 {LDAP_AUTH_SIMPLE, "Simple"},
999 {LDAP_AUTH_KRBV4LDAP, "Kerberos V4 to the LDAP server"},
1000 {LDAP_AUTH_KRBV4DSA, "Kerberos V4 to the DSA"},
1001 {LDAP_AUTH_SASL, "SASL"},
1004 static value_string search_scope[] = {
1010 static value_string search_dereference[] = {
1012 {0x01, "Searching"},
1013 {0x02, "Base Object"},
1017 static hf_register_info hf[] = {
1019 { "Length", "ldap.length",
1020 FT_UINT32, BASE_DEC, NULL, 0x0,
1023 { &hf_ldap_message_id,
1024 { "Message Id", "ldap.message_id",
1025 FT_UINT32, BASE_DEC, NULL, 0x0,
1026 "LDAP Message Id" }},
1027 { &hf_ldap_message_type,
1028 { "Message Type", "ldap.message_type",
1029 FT_UINT8, BASE_HEX, &msgTypes, 0x0,
1030 "LDAP Message Type" }},
1031 { &hf_ldap_message_length,
1032 { "Message Length", "ldap.message_length",
1033 FT_UINT32, BASE_DEC, NULL, 0x0,
1034 "LDAP Message Length" }},
1036 { &hf_ldap_message_result,
1037 { "Result Code", "ldap.result.code",
1038 FT_UINT8, BASE_HEX, result_codes, 0x0,
1039 "LDAP Result Code" }},
1040 { &hf_ldap_message_result_matcheddn,
1041 { "Matched DN", "ldap.result.matcheddn",
1042 FT_STRING, BASE_NONE, NULL, 0x0,
1043 "LDAP Result Matched DN" }},
1044 { &hf_ldap_message_result_errormsg,
1045 { "Error Message", "ldap.result.errormsg",
1046 FT_STRING, BASE_NONE, NULL, 0x0,
1047 "LDAP Result Error Message" }},
1048 { &hf_ldap_message_result_referral,
1049 { "Referral", "ldap.result.referral",
1050 FT_STRING, BASE_NONE, NULL, 0x0,
1051 "LDAP Result Referral URL" }},
1053 { &hf_ldap_message_bind_version,
1054 { "Version", "ldap.bind.version",
1055 FT_UINT32, BASE_DEC, NULL, 0x0,
1056 "LDAP Bind Version" }},
1057 { &hf_ldap_message_bind_dn,
1058 { "DN", "ldap.bind.dn",
1059 FT_STRING, BASE_NONE, NULL, 0x0,
1060 "LDAP Bind Distinguished Name" }},
1061 { &hf_ldap_message_bind_auth,
1062 { "Auth Type", "ldap.bind.auth_type",
1063 FT_UINT8, BASE_HEX, auth_types, 0x0,
1064 "LDAP Bind Auth Type" }},
1065 { &hf_ldap_message_bind_auth_password,
1066 { "Password", "ldap.bind.password",
1067 FT_STRING, BASE_NONE, NULL, 0x0,
1068 "LDAP Bind Password" }},
1070 { &hf_ldap_message_search_base,
1071 { "Base DN", "ldap.search.basedn",
1072 FT_STRING, BASE_NONE, NULL, 0x0,
1073 "LDAP Search Base Distinguished Name" }},
1074 { &hf_ldap_message_search_scope,
1075 { "Scope", "ldap.search.scope",
1076 FT_UINT8, BASE_HEX, search_scope, 0x0,
1077 "LDAP Search Scope" }},
1078 { &hf_ldap_message_search_deref,
1079 { "Dereference", "ldap.search.dereference",
1080 FT_UINT8, BASE_HEX, search_dereference, 0x0,
1081 "LDAP Search Dereference" }},
1082 { &hf_ldap_message_search_sizeLimit,
1083 { "Size Limit", "ldap.search.sizelimit",
1084 FT_UINT32, BASE_DEC, NULL, 0x0,
1085 "LDAP Search Size Limit" }},
1086 { &hf_ldap_message_search_timeLimit,
1087 { "Time Limit", "ldap.search.timelimit",
1088 FT_UINT32, BASE_DEC, NULL, 0x0,
1089 "LDAP Search Time Limit" }},
1090 { &hf_ldap_message_search_typesOnly,
1091 { "Attributes Only", "ldap.search.typesonly",
1092 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1093 "LDAP Search Attributes Only" }},
1094 { &hf_ldap_message_search_filter,
1095 { "Filter", "ldap.search.filter",
1096 FT_STRING, BASE_NONE, NULL, 0x0,
1097 "LDAP Search Filter" }},
1098 { &hf_ldap_message_dn,
1099 { "Distinguished Name", "ldap.dn",
1100 FT_STRING, BASE_NONE, NULL, 0x0,
1101 "LDAP Distinguished Name" }},
1102 { &hf_ldap_message_attribute,
1103 { "Attribute", "ldap.attribute",
1104 FT_STRING, BASE_NONE, NULL, 0x0,
1105 "LDAP Attribute" }},
1106 { &hf_ldap_message_value,
1107 { "Value", "ldap.value",
1108 FT_STRING, BASE_NONE, NULL, 0x0,
1111 { &hf_ldap_message_modrdn_name,
1112 { "New Name", "ldap.modrdn.name",
1113 FT_STRING, BASE_NONE, NULL, 0x0,
1115 { &hf_ldap_message_modrdn_delete,
1116 { "Delete Values", "ldap.modrdn.delete",
1117 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1118 "LDAP Modify RDN - Delete original values" }},
1119 { &hf_ldap_message_modrdn_superior,
1120 { "New Location", "ldap.modrdn.superior",
1121 FT_STRING, BASE_NONE, NULL, 0x0,
1122 "LDAP Modify RDN - New Location" }},
1124 { &hf_ldap_message_compare,
1125 { "Test", "ldap.compare.test",
1126 FT_STRING, BASE_NONE, NULL, 0x0,
1127 "LDAP Compare Test" }},
1129 { &hf_ldap_message_modify_add,
1130 { "Add", "ldap.modify.add",
1131 FT_STRING, BASE_NONE, NULL, 0x0,
1133 { &hf_ldap_message_modify_replace,
1134 { "Replace", "ldap.modify.replace",
1135 FT_STRING, BASE_NONE, NULL, 0x0,
1137 { &hf_ldap_message_modify_delete,
1138 { "Delete", "ldap.modify.delete",
1139 FT_STRING, BASE_NONE, NULL, 0x0,
1142 { &hf_ldap_message_abandon_msgid,
1143 { "Abandon Msg Id", "ldap.abandon.msgid",
1144 FT_UINT32, BASE_DEC, NULL, 0x0,
1145 "LDAP Abandon Msg Id" }},
1148 static gint *ett[] = {
1151 &ett_ldap_referrals,
1155 proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol", "ldap");
1156 proto_register_field_array(proto_ldap, hf, array_length(hf));
1157 proto_register_subtree_array(ett, array_length(ett));
1161 proto_reg_handoff_ldap(void)
1163 old_dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap);