2 * Routines for ldap packet dissection
4 * $Id: packet-ldap.c,v 1.12 2000/05/31 05:07:16 guy 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
64 # include "snprintf.h"
69 #include "packet-ldap.h"
72 static int proto_ldap = -1;
73 static int hf_ldap_length = -1;
74 static int hf_ldap_message_id = -1;
75 static int hf_ldap_message_type = -1;
76 static int hf_ldap_message_length = -1;
78 static int hf_ldap_message_result = -1;
79 static int hf_ldap_message_result_matcheddn = -1;
80 static int hf_ldap_message_result_errormsg = -1;
81 static int hf_ldap_message_result_referral = -1;
83 static int hf_ldap_message_bind_version = -1;
84 static int hf_ldap_message_bind_dn = -1;
85 static int hf_ldap_message_bind_auth = -1;
86 static int hf_ldap_message_bind_auth_password = -1;
88 static int hf_ldap_message_search_base = -1;
89 static int hf_ldap_message_search_scope = -1;
90 static int hf_ldap_message_search_deref = -1;
91 static int hf_ldap_message_search_sizeLimit = -1;
92 static int hf_ldap_message_search_timeLimit = -1;
93 static int hf_ldap_message_search_typesOnly = -1;
94 static int hf_ldap_message_search_filter = -1;
96 static int hf_ldap_message_dn = -1;
97 static int hf_ldap_message_attribute = -1;
98 static int hf_ldap_message_value = -1;
100 static int hf_ldap_message_modrdn_name = -1;
101 static int hf_ldap_message_modrdn_delete = -1;
102 static int hf_ldap_message_modrdn_superior = -1;
104 static int hf_ldap_message_compare = -1;
106 static int hf_ldap_message_modify_add = -1;
107 static int hf_ldap_message_modify_replace = -1;
108 static int hf_ldap_message_modify_delete = -1;
110 static int hf_ldap_message_abandon_msgid = -1;
112 static gint ett_ldap = -1;
113 static gint ett_ldap_message = -1;
114 static gint ett_ldap_referrals = -1;
115 static gint ett_ldap_attribute = -1;
117 #define TCP_PORT_LDAP 389
119 static value_string msgTypes [] = {
120 {LDAP_REQ_BIND, "Bind Request"},
121 {LDAP_REQ_UNBIND, "Unbind Request"},
122 {LDAP_REQ_SEARCH, "Search Request"},
123 {LDAP_REQ_MODIFY, "Modify Request"},
124 {LDAP_REQ_ADD, "Add Request"},
125 {LDAP_REQ_DELETE, "Delete Request"},
126 {LDAP_REQ_MODRDN, "Modify RDN Request"},
127 {LDAP_REQ_COMPARE, "Compare Request"},
128 {LDAP_REQ_ABANDON, "Abandon Request"},
129 {LDAP_REQ_EXTENDED, "Extended Request"},
131 {LDAP_RES_BIND, "Bind Result"},
132 {LDAP_RES_SEARCH_ENTRY, "Search Entry"},
133 {LDAP_RES_SEARCH_RESULT, "Search Result"},
134 {LDAP_RES_SEARCH_REF, "Search Result Reference"},
135 {LDAP_RES_MODIFY, "Modify Result"},
136 {LDAP_RES_ADD, "Add Result"},
137 {LDAP_RES_DELETE, "Delete Result"},
138 {LDAP_RES_MODRDN, "Modify RDN Result"},
139 {LDAP_RES_COMPARE, "Compare Result"},
140 {LDAP_REQ_EXTENDED, "Extended Response"},
143 static int read_length(ASN1_SCK *a, proto_tree *tree, int hf_id, guint *len)
146 gboolean def = FALSE;
147 const guchar *start = a->pointer;
149 asn1_length_decode(a, &def, &length);
155 proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, length);
160 static int read_sequence(ASN1_SCK *a, guint *len)
166 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
168 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
177 static int read_set(ASN1_SCK *a, guint *len)
183 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
185 if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SET)
194 static int read_integer_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
195 proto_tree **new_tree, guint *i, const guchar *start, guint length)
199 asn1_uint32_value_decode(a, length, &integer);
206 proto_tree *temp_tree = 0;
207 temp_tree = proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
209 *new_tree = temp_tree;
215 static int read_integer(ASN1_SCK *a, proto_tree *tree, int hf_id,
216 proto_tree **new_tree, guint *i, guint expected_tag)
221 const guchar *start = a->pointer;
223 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
225 if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
228 return read_integer_value(a, tree, hf_id, new_tree, i, start, length);
231 static int read_boolean_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
232 proto_tree **new_tree, guint *i, const guchar *start, guint length)
236 asn1_uint32_value_decode(a, length, &integer);
243 proto_tree *temp_tree = 0;
244 temp_tree = proto_tree_add_boolean(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
246 *new_tree = temp_tree;
252 static int read_boolean(ASN1_SCK *a, proto_tree *tree, int hf_id,
253 proto_tree **new_tree, guint *i)
258 const guchar *start = a->pointer;
260 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
262 if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_BOL)
265 return read_boolean_value(a, tree, hf_id, new_tree, i, start, length);
268 static void read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
269 proto_tree **new_tree, char **s, const guchar *start, guint length)
275 asn1_octet_string_value_decode(a, length, &string);
276 string = g_realloc(string, length + 1);
277 string[length] = '\0';
284 proto_tree *temp_tree;
285 temp_tree = proto_tree_add_string(tree, hf_id, NullTVB, start - a->begin, a->pointer - start, string);
287 *new_tree = temp_tree;
296 static int read_string(ASN1_SCK *a, proto_tree *tree, int hf_id,
297 proto_tree **new_tree, char **s, guint expected_cls, guint expected_tag)
302 const guchar *start = a->pointer;
305 ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
306 if (ret != ASN1_ERR_NOERROR)
308 if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
309 return ASN1_ERR_WRONG_TYPE;
311 read_string_value(a, tree, hf_id, new_tree, s, start, length);
312 return ASN1_ERR_NOERROR;
315 static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length, const guchar *operation)
324 ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
325 if (ret != ASN1_ERR_NOERROR)
327 ret = asn1_octet_string_decode(a, &string2, &string2_length, &string_bytes);
328 if (ret != ASN1_ERR_NOERROR)
330 *filter_length += 2 + strlen(operation) + string_length + string2_length;
331 *filter = g_realloc(*filter, *filter_length);
332 sprintf(*filter + strlen(*filter), "(%.*s%s%.*s)", string_length, string, operation, string2_length, string2);
335 return ASN1_ERR_NOERROR;
338 /* Richard Dawe: To parse substring filters, I added this function. */
339 static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_length)
342 guchar *string = NULL;
349 /* For ASN.1 parsing of octet strings */
355 ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
356 if (ret != ASN1_ERR_NOERROR)
359 ret = asn1_sequence_decode(a, &seq_len, &header_bytes);
360 if (ret != ASN1_ERR_NOERROR)
363 *filter_length += 2 + 1 + strlen(string);
364 *filter = g_realloc(*filter, *filter_length);
365 sprintf(*filter + strlen(*filter), "(%.*s=", string_length, string);
368 /* Now decode seq_len's worth of octet strings. */
370 end = (guchar *) (a->pointer + seq_len);
372 while (a->pointer < end) {
373 /* Octet strings here are context-specific, which
374 * asn1_octet_string_decode() barfs on. Emulate it, but don't barf. */
375 ret = asn1_header_decode (a, &cls, &con, &tag, &def, &string_length);
376 if (ret != ASN1_ERR_NOERROR)
379 if (cls != ASN1_CTX || con != ASN1_PRI) {
380 /* XXX - handle the constructed encoding? */
381 return ASN1_ERR_WRONG_TYPE;
384 return ASN1_ERR_LENGTH_NOT_DEFINITE;
386 ret = asn1_octet_string_value_decode(a, (int) string_length, &string);
387 if (ret != ASN1_ERR_NOERROR)
390 /* If we have an 'any' component with a string value, we need to append
391 * an extra asterisk before final component. */
392 if ((tag == 1) && (string_length > 0))
395 if ( (tag == 1) || ((tag == 2) && any_valued) )
397 *filter_length += strlen(string);
398 *filter = g_realloc(*filter, *filter_length);
400 if ( (tag == 1) || ((tag == 2) && any_valued) )
401 strcat(*filter, "*");
404 sprintf(*filter + strlen(*filter), "%.*s", string_length, string);
411 *filter = g_realloc(*filter, *filter_length);
412 strcat(*filter, "*");
415 /* NB: Allocated byte for this earlier */
416 strcat(*filter, ")");
418 return ASN1_ERR_NOERROR;
421 /* Returns -1 if we're at the end, returns an ASN1_ERR value otherwise. */
422 static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const guchar **end)
429 ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
430 if (ret != ASN1_ERR_NOERROR)
435 *end = a->pointer + length;
437 *filter = g_malloc0(*filter_length);
440 if (cls == ASN1_CTX) /* XXX - handle other types as errors? */
444 case LDAP_FILTER_AND:
446 const guchar *add_end;
449 return ASN1_ERR_WRONG_TYPE;
450 add_end = a->pointer + length;
452 *filter = g_realloc(*filter, *filter_length);
453 strcat(*filter, "(&");
454 while ((ret = parse_filter(a, filter, filter_length, &add_end))
459 strcat(*filter, ")");
464 const guchar *or_end;
467 return ASN1_ERR_WRONG_TYPE;
468 or_end = a->pointer + length;
470 *filter = g_realloc(*filter, *filter_length);
471 strcat(*filter, "(|");
472 while ((ret = parse_filter(a, filter, filter_length, &or_end))
477 strcat(*filter, ")");
480 case LDAP_FILTER_NOT:
482 const guchar *not_end;
485 return ASN1_ERR_WRONG_TYPE;
486 not_end = a->pointer + length;
488 *filter = g_realloc(*filter, *filter_length);
489 strcat(*filter, "(!");
490 ret = parse_filter(a, filter, filter_length, ¬_end);
491 if (ret != -1 && ret != ASN1_ERR_NOERROR)
493 strcat(*filter, ")");
496 case LDAP_FILTER_EQUALITY:
498 return ASN1_ERR_WRONG_TYPE;
499 ret = parse_filter_strings(a, filter, filter_length, "=");
500 if (ret != -1 && ret != ASN1_ERR_NOERROR)
505 return ASN1_ERR_WRONG_TYPE;
506 ret = parse_filter_strings(a, filter, filter_length, ">=");
507 if (ret != -1 && ret != ASN1_ERR_NOERROR)
512 return ASN1_ERR_WRONG_TYPE;
513 ret = parse_filter_strings(a, filter, filter_length, "<=");
514 if (ret != -1 && ret != ASN1_ERR_NOERROR)
517 case LDAP_FILTER_APPROX:
519 return ASN1_ERR_WRONG_TYPE;
520 ret = parse_filter_strings(a, filter, filter_length, "~=");
521 if (ret != -1 && ret != ASN1_ERR_NOERROR)
524 case LDAP_FILTER_PRESENT:
529 return ASN1_ERR_WRONG_TYPE;
530 ret = asn1_octet_string_value_decode(a, length, &string);
531 if (ret != ASN1_ERR_NOERROR)
533 *filter_length += 4 + length;
534 *filter = g_realloc(*filter, *filter_length);
535 sprintf(*filter + strlen(*filter), "(%.*s=*)", (int)length, string);
539 case LDAP_FILTER_SUBSTRINGS:
541 return ASN1_ERR_WRONG_TYPE;
542 /* Richard Dawe: Handle substrings */
543 ret = parse_filter_substrings(a, filter, filter_length);
544 if (ret != -1 && ret != ASN1_ERR_NOERROR)
548 return ASN1_ERR_WRONG_TYPE;
552 if (a->pointer == *end)
558 static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
560 const guchar *start = a->pointer;
562 guint filter_length = 0;
563 const guchar *end = 0;
566 while ((ret = parse_filter(a, &filter, &filter_length, &end))
572 proto_tree_add_text(tree, NullTVB, start-a->begin, 0,
573 "Error parsing filter (%d)", ret);
575 proto_tree_add_string(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, filter);
583 /********************************************************************************************/
585 static int dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
587 guint resultCode = 0;
589 read_integer(a, tree, hf_ldap_message_result, 0, &resultCode, ASN1_ENUM);
590 read_string(a, tree, hf_ldap_message_result_matcheddn, 0, 0, ASN1_UNI, ASN1_OTS);
591 read_string(a, tree, hf_ldap_message_result_errormsg, 0, 0, ASN1_UNI, ASN1_OTS);
593 if (resultCode == 10) /* Referral */
595 const guchar *start = a->pointer;
598 proto_tree *t, *referralTree;
600 read_sequence(a, &length);
601 t = proto_tree_add_text(tree, NullTVB, start-a->begin, length, "Referral URLs");
602 referralTree = proto_item_add_subtree(t, ett_ldap_referrals);
604 end = a->pointer + length;;
605 while (a->pointer < end)
606 read_string(a, referralTree, hf_ldap_message_result_referral, 0, 0, ASN1_UNI, ASN1_OTS);
612 static int dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
618 read_integer(a, tree, hf_ldap_message_bind_version, 0, 0, ASN1_INT);
619 read_string(a, tree, hf_ldap_message_bind_dn, 0, 0, ASN1_UNI, ASN1_OTS);
622 if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
623 return 1; /* XXX - right return value for an error? */
625 return 1; /* RFCs 1777 and 2251 say these are context-specific types */
626 proto_tree_add_uint(tree, hf_ldap_message_bind_auth, NullTVB, start - a->begin,
627 a->pointer - start, tag);
630 case LDAP_AUTH_SIMPLE:
631 read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL, NULL,
635 /* For Kerberos V4, dissect it as a ticket. */
636 /* For SASL, dissect it as SaslCredentials. */
642 static int dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
644 dissect_ldap_result(a, tree);
645 /* FIXME: handle SASL data */
649 static int dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
655 read_string(a, tree, hf_ldap_message_search_base, 0, 0, ASN1_UNI, ASN1_OTS);
656 read_integer(a, tree, hf_ldap_message_search_scope, 0, 0, ASN1_ENUM);
657 read_integer(a, tree, hf_ldap_message_search_deref, 0, 0, ASN1_ENUM);
658 read_integer(a, tree, hf_ldap_message_search_sizeLimit, 0, 0, ASN1_INT);
659 read_integer(a, tree, hf_ldap_message_search_timeLimit, 0, 0, ASN1_INT);
660 read_boolean(a, tree, hf_ldap_message_search_typesOnly, 0, 0);
661 ret = read_filter(a, tree, hf_ldap_message_search_filter);
662 if (ret != ASN1_ERR_NOERROR)
664 read_sequence(a, &seq_length);
665 end = a->pointer + seq_length;
666 while (a->pointer < end) {
667 ret = read_string(a, tree, hf_ldap_message_attribute, 0, 0, ASN1_UNI, ASN1_OTS);
668 if (ret != ASN1_ERR_NOERROR)
671 return ASN1_ERR_NOERROR;
674 static int dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
677 const guchar *end_of_sequence;
679 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
680 read_sequence(a, &seq_length);
682 end_of_sequence = a->pointer + seq_length;
683 while (a->pointer < end_of_sequence)
685 proto_tree *t, *attr_tree;
687 const guchar *end_of_set;
690 read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
691 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
693 read_set(a, &set_length);
694 end_of_set = a->pointer + set_length;
695 while (a->pointer < end_of_set)
696 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
702 static int dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
705 const guchar *end_of_sequence;
707 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
709 read_sequence(a, &seq_length);
710 end_of_sequence = a->pointer + seq_length;
711 while (a->pointer < end_of_sequence)
713 proto_tree *t, *attr_tree;
715 const guchar *end_of_set;
718 read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
719 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
721 read_set(a, &set_length);
722 end_of_set = a->pointer + set_length;
723 while (a->pointer < end_of_set)
724 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
730 static int dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
731 const guchar *start, guint length)
733 read_string_value(a, tree, hf_ldap_message_dn, NULL, NULL, start, length);
737 static int dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
740 const guchar *start = a->pointer;
742 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
743 read_string(a, tree, hf_ldap_message_modrdn_name, 0, 0, ASN1_UNI, ASN1_OTS);
744 read_boolean(a, tree, hf_ldap_message_modrdn_delete, 0, 0);
746 if (a->pointer < (start + length)) {
747 /* LDAP V3 Modify DN operation, with newSuperior */
748 read_string(a, tree, hf_ldap_message_modrdn_superior, 0, 0, ASN1_UNI, ASN1_OTS);
754 static int dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
762 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
766 read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
767 read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
769 length = 2 + strlen(string1) + strlen(string2);
770 compare = g_malloc0(length);
771 snprintf(compare, length, "%s=%s", string1, string2);
772 proto_tree_add_string(tree, hf_ldap_message_compare, NullTVB, start-a->begin, a->pointer-start, compare);
781 static int dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
784 const guchar *end_of_sequence;
786 read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
787 read_sequence(a, &seq_length);
788 end_of_sequence = a->pointer + seq_length;
789 while (a->pointer < end_of_sequence)
791 proto_tree *t = 0, *attr_tree;
793 const guchar *end_of_set;
797 read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
803 read_string(a, tree, hf_ldap_message_modify_add, &t, 0, ASN1_UNI, ASN1_OTS);
805 case LDAP_MOD_REPLACE:
806 read_string(a, tree, hf_ldap_message_modify_replace, &t, 0, ASN1_UNI, ASN1_OTS);
808 case LDAP_MOD_DELETE:
809 read_string(a, tree, hf_ldap_message_modify_delete, &t, 0, ASN1_UNI, ASN1_OTS);
812 attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
814 read_set(a, &set_length);
815 end_of_set = a->pointer + set_length;
816 while (a->pointer < end_of_set)
817 read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
823 static int dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
824 const guchar *start, guint length)
826 read_integer_value(a, tree, hf_ldap_message_abandon_msgid, NULL, NULL,
832 dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
834 proto_tree *ldap_tree = 0, *ti, *msg_tree;
837 guint protocolOpCls, protocolOpCon, protocolOpTag;
847 ti = proto_tree_add_item(tree, proto_ldap, NullTVB, offset, END_OF_FRAME, FALSE);
848 ldap_tree = proto_item_add_subtree(ti, ett_ldap);
851 asn1_open(&a, pd, pi.captured_len);
854 while (a.pointer < a.end)
856 int message_id_start;
857 int message_id_length;
860 message_start = a.pointer - a.begin;
861 if (read_sequence(&a, &messageLength))
864 proto_tree_add_text(ldap_tree, NullTVB, offset, 1, "Invalid LDAP packet");
868 if (messageLength > (a.end - a.pointer))
871 proto_tree_add_text(ldap_tree, NullTVB, message_start, END_OF_FRAME, "Short message! (expected: %u, actual: %u)",
872 messageLength, a.end - a.pointer);
876 message_id_start = a.pointer - a.begin;
877 read_integer(&a, 0, -1, 0, &messageId, ASN1_INT);
878 message_id_length = (a.pointer - a.begin) - message_id_start;
881 asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
882 if (protocolOpCls != ASN1_APL)
883 typestr = "Bad message type (not Application)";
885 typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
889 if (check_col(fd, COL_PROTOCOL))
890 col_add_str(fd, COL_PROTOCOL, "LDAP");
892 if (check_col(fd, COL_INFO))
893 col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
902 proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_id, NullTVB, message_id_start, message_id_length, messageId);
903 proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_type, NullTVB,
904 start - a.begin, a.pointer - start, protocolOpTag);
905 ti = proto_tree_add_text(ldap_tree, NullTVB, message_id_start, messageLength, "Message: Id=%u %s", messageId, typestr);
906 msg_tree = proto_item_add_subtree(ti, ett_ldap_message);
908 read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
910 switch (protocolOpTag)
913 dissect_ldap_request_bind(&a, msg_tree);
915 case LDAP_REQ_SEARCH:
916 ret = dissect_ldap_request_search(&a, msg_tree);
917 if (ret != ASN1_ERR_NOERROR)
921 dissect_ldap_request_add(&a, msg_tree);
923 case LDAP_REQ_DELETE:
924 dissect_ldap_request_delete(&a, msg_tree, start, opLen);
926 case LDAP_REQ_MODRDN:
927 dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
929 case LDAP_REQ_COMPARE:
930 dissect_ldap_request_compare(&a, msg_tree);
932 case LDAP_REQ_MODIFY:
933 dissect_ldap_request_modify(&a, msg_tree);
935 case LDAP_REQ_ABANDON:
936 dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
939 dissect_ldap_response_bind(&a, msg_tree);
941 case LDAP_RES_SEARCH_ENTRY:
942 dissect_ldap_response_search_entry(&a, msg_tree);
944 case LDAP_RES_SEARCH_RESULT:
945 case LDAP_RES_MODIFY:
947 case LDAP_RES_DELETE:
948 case LDAP_RES_MODRDN:
949 case LDAP_RES_COMPARE:
950 dissect_ldap_result(&a, msg_tree);
958 proto_register_ldap(void)
960 static value_string result_codes[] = {
962 {1, "Operations error"},
963 {2, "Protocol error"},
964 {3, "Time limit exceeded"},
965 {4, "Size limit exceeded"},
966 {5, "Compare false"},
968 {7, "Authentication method not supported"},
969 {8, "Strong authentication required"},
971 {11, "Administrative limit exceeded"},
972 {12, "Unavailable critical extension"},
973 {13, "Confidentiality required"},
974 {14, "SASL bind in progress"},
975 {16, "No such attribute"},
976 {17, "Undefined attribute type"},
977 {18, "Inappropriate matching"},
978 {19, "Constraint violation"},
979 {20, "Attribute or value exists"},
980 {21, "Invalid attribute syntax"},
981 {32, "No such object"},
982 {33, "Alias problem"},
983 {34, "Invalid DN syntax"},
984 {36, "Alias derefetencing problem"},
985 {48, "Inappropriate authentication"},
986 {49, "Invalid credentials"},
987 {50, "Insufficient access rights"},
990 {53, "Unwilling to perform"},
991 {54, "Loop detected"},
992 {64, "Naming violation"},
993 {65, "Objectclass violation"},
994 {66, "Not allowed on non-leaf"},
995 {67, "Not allowed on RDN"},
996 {68, "Entry already exists"},
997 {69, "Objectclass modification prohibited"},
998 {71, "Affects multiple DSAs"},
1002 static value_string auth_types[] = {
1003 {LDAP_AUTH_SIMPLE, "Simple"},
1004 {LDAP_AUTH_KRBV4LDAP, "Kerberos V4 to the LDAP server"},
1005 {LDAP_AUTH_KRBV4DSA, "Kerberos V4 to the DSA"},
1006 {LDAP_AUTH_SASL, "SASL"},
1009 static value_string search_scope[] = {
1015 static value_string search_dereference[] = {
1017 {0x01, "Searching"},
1018 {0x02, "Base Object"},
1022 static hf_register_info hf[] = {
1024 { "Length", "ldap.length",
1025 FT_UINT32, BASE_DEC, NULL, 0x0,
1028 { &hf_ldap_message_id,
1029 { "Message Id", "ldap.message_id",
1030 FT_UINT32, BASE_DEC, NULL, 0x0,
1031 "LDAP Message Id" }},
1032 { &hf_ldap_message_type,
1033 { "Message Type", "ldap.message_type",
1034 FT_UINT8, BASE_HEX, &msgTypes, 0x0,
1035 "LDAP Message Type" }},
1036 { &hf_ldap_message_length,
1037 { "Message Length", "ldap.message_length",
1038 FT_UINT32, BASE_DEC, NULL, 0x0,
1039 "LDAP Message Length" }},
1041 { &hf_ldap_message_result,
1042 { "Result Code", "ldap.result.code",
1043 FT_UINT8, BASE_HEX, result_codes, 0x0,
1044 "LDAP Result Code" }},
1045 { &hf_ldap_message_result_matcheddn,
1046 { "Matched DN", "ldap.result.matcheddn",
1047 FT_STRING, BASE_NONE, NULL, 0x0,
1048 "LDAP Result Matched DN" }},
1049 { &hf_ldap_message_result_errormsg,
1050 { "Error Message", "ldap.result.errormsg",
1051 FT_STRING, BASE_NONE, NULL, 0x0,
1052 "LDAP Result Error Message" }},
1053 { &hf_ldap_message_result_referral,
1054 { "Referral", "ldap.result.referral",
1055 FT_STRING, BASE_NONE, NULL, 0x0,
1056 "LDAP Result Referral URL" }},
1058 { &hf_ldap_message_bind_version,
1059 { "Version", "ldap.bind.version",
1060 FT_UINT32, BASE_DEC, NULL, 0x0,
1061 "LDAP Bind Version" }},
1062 { &hf_ldap_message_bind_dn,
1063 { "DN", "ldap.bind.dn",
1064 FT_STRING, BASE_NONE, NULL, 0x0,
1065 "LDAP Bind Distinguished Name" }},
1066 { &hf_ldap_message_bind_auth,
1067 { "Auth Type", "ldap.bind.auth_type",
1068 FT_UINT8, BASE_HEX, auth_types, 0x0,
1069 "LDAP Bind Auth Type" }},
1070 { &hf_ldap_message_bind_auth_password,
1071 { "Password", "ldap.bind.password",
1072 FT_STRING, BASE_NONE, NULL, 0x0,
1073 "LDAP Bind Password" }},
1075 { &hf_ldap_message_search_base,
1076 { "Base DN", "ldap.search.basedn",
1077 FT_STRING, BASE_NONE, NULL, 0x0,
1078 "LDAP Search Base Distinguished Name" }},
1079 { &hf_ldap_message_search_scope,
1080 { "Scope", "ldap.search.scope",
1081 FT_UINT8, BASE_HEX, search_scope, 0x0,
1082 "LDAP Search Scope" }},
1083 { &hf_ldap_message_search_deref,
1084 { "Dereference", "ldap.search.dereference",
1085 FT_UINT8, BASE_HEX, search_dereference, 0x0,
1086 "LDAP Search Dereference" }},
1087 { &hf_ldap_message_search_sizeLimit,
1088 { "Size Limit", "ldap.search.sizelimit",
1089 FT_UINT32, BASE_DEC, NULL, 0x0,
1090 "LDAP Search Size Limit" }},
1091 { &hf_ldap_message_search_timeLimit,
1092 { "Time Limit", "ldap.search.timelimit",
1093 FT_UINT32, BASE_DEC, NULL, 0x0,
1094 "LDAP Search Time Limit" }},
1095 { &hf_ldap_message_search_typesOnly,
1096 { "Attributes Only", "ldap.search.typesonly",
1097 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1098 "LDAP Search Attributes Only" }},
1099 { &hf_ldap_message_search_filter,
1100 { "Filter", "ldap.search.filter",
1101 FT_STRING, BASE_NONE, NULL, 0x0,
1102 "LDAP Search Filter" }},
1103 { &hf_ldap_message_dn,
1104 { "Distinguished Name", "ldap.dn",
1105 FT_STRING, BASE_NONE, NULL, 0x0,
1106 "LDAP Distinguished Name" }},
1107 { &hf_ldap_message_attribute,
1108 { "Attribute", "ldap.attribute",
1109 FT_STRING, BASE_NONE, NULL, 0x0,
1110 "LDAP Attribute" }},
1111 { &hf_ldap_message_value,
1112 { "Value", "ldap.value",
1113 FT_STRING, BASE_NONE, NULL, 0x0,
1116 { &hf_ldap_message_modrdn_name,
1117 { "New Name", "ldap.modrdn.name",
1118 FT_STRING, BASE_NONE, NULL, 0x0,
1120 { &hf_ldap_message_modrdn_delete,
1121 { "Delete Values", "ldap.modrdn.delete",
1122 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1123 "LDAP Modify RDN - Delete original values" }},
1124 { &hf_ldap_message_modrdn_superior,
1125 { "New Location", "ldap.modrdn.superior",
1126 FT_STRING, BASE_NONE, NULL, 0x0,
1127 "LDAP Modify RDN - New Location" }},
1129 { &hf_ldap_message_compare,
1130 { "Test", "ldap.compare.test",
1131 FT_STRING, BASE_NONE, NULL, 0x0,
1132 "LDAP Compare Test" }},
1134 { &hf_ldap_message_modify_add,
1135 { "Add", "ldap.modify.add",
1136 FT_STRING, BASE_NONE, NULL, 0x0,
1138 { &hf_ldap_message_modify_replace,
1139 { "Replace", "ldap.modify.replace",
1140 FT_STRING, BASE_NONE, NULL, 0x0,
1142 { &hf_ldap_message_modify_delete,
1143 { "Delete", "ldap.modify.delete",
1144 FT_STRING, BASE_NONE, NULL, 0x0,
1147 { &hf_ldap_message_abandon_msgid,
1148 { "Abandon Msg Id", "ldap.abandon.msgid",
1149 FT_UINT32, BASE_DEC, NULL, 0x0,
1150 "LDAP Abandon Msg Id" }},
1153 static gint *ett[] = {
1156 &ett_ldap_referrals,
1160 proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol", "ldap");
1161 proto_register_field_array(proto_ldap, hf, array_length(hf));
1162 proto_register_subtree_array(ett, array_length(ett));
1166 proto_reg_handoff_ldap(void)
1168 dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap);