Miscellaneous code cleaning
[obnox/wireshark/wip.git] / packet-ldap.c
1 /* packet-ldap.c
2  * Routines for ldap packet dissection
3  *
4  * $Id: packet-ldap.c,v 1.14 2000/08/11 13:35:15 deniel Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  * 
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.
19  * 
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.
23  */
24
25 /*
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.
29  * 
30  * There should probably be alot more error checking, I simply assume that if we have a full packet, it will be a complete
31  * and correct packet.
32  * 
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
35  * I can do.
36  * 
37  * Doug Nazar
38  * nazard@dragoninc.on.ca
39  */
40
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #include <stdio.h>
46
47 #ifdef HAVE_SYS_TYPES_H
48 # include <sys/types.h>
49 #endif
50
51 #ifdef HAVE_NETINET_IN_H
52 # include <netinet/in.h>
53 #endif
54
55 #include <string.h>
56 #include <glib.h>
57
58 #ifdef NEED_SNPRINTF_H
59 # include "snprintf.h"
60 #endif
61
62 #include "packet.h"
63
64 #include "packet-ldap.h"
65 #include "asn1.h"
66
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;
72
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;
77
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;
82
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;
90
91 static int hf_ldap_message_dn = -1;
92 static int hf_ldap_message_attribute = -1;
93 static int hf_ldap_message_value = -1;
94
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;
98
99 static int hf_ldap_message_compare = -1;
100
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;
104
105 static int hf_ldap_message_abandon_msgid = -1;
106
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;
111
112 #define TCP_PORT_LDAP                   389
113
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"},
125     
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"},
136 };
137
138 static int read_length(ASN1_SCK *a, proto_tree *tree, int hf_id, guint *len)
139 {
140   guint length = 0;
141   gboolean def = FALSE;
142   const guchar *start = a->pointer;
143   
144   asn1_length_decode(a, &def, &length);
145
146   if (len)
147     *len = length;
148
149   if (tree)
150     proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, length);
151
152   return 0;
153 }
154
155 static int read_sequence(ASN1_SCK *a, guint *len)
156 {
157   guint cls, con, tag;
158   gboolean def;
159   guint length;
160   
161   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
162     return 1;
163   if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
164     return 1;
165   
166   if (len)
167     *len = length;
168   
169   return 0;
170 }
171
172 static int read_set(ASN1_SCK *a, guint *len)
173 {
174   guint cls, con, tag;
175   gboolean def;
176   guint length;
177   
178   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
179     return 1;
180   if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SET)
181     return 1;
182   
183   if (len)
184     *len = length;
185   
186   return 0;
187 }
188
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)
191 {
192   guint integer = 0;
193
194   asn1_uint32_value_decode(a, length, &integer);
195
196   if (i)
197     *i = integer;
198
199   if (tree)
200   {
201     proto_tree *temp_tree = 0;
202     temp_tree = proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
203     if (new_tree)
204       *new_tree = temp_tree;
205   }
206
207   return 0;
208 }
209
210 static int read_integer(ASN1_SCK *a, proto_tree *tree, int hf_id,
211         proto_tree **new_tree, guint *i, guint expected_tag)
212 {
213   guint cls, con, tag;
214   gboolean def;
215   guint length;
216   const guchar *start = a->pointer;
217   
218   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
219     return 1;
220   if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
221     return 1;
222
223   return read_integer_value(a, tree, hf_id, new_tree, i, start, length);
224 }
225
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)
228 {
229   guint integer = 0;
230
231   asn1_uint32_value_decode(a, length, &integer);
232
233   if (i)
234     *i = integer;
235
236   if (tree)
237   {
238     proto_tree *temp_tree = 0;
239     temp_tree = proto_tree_add_boolean(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
240     if (new_tree)
241       *new_tree = temp_tree;
242   }
243
244   return 0;
245 }
246
247 static int read_boolean(ASN1_SCK *a, proto_tree *tree, int hf_id,
248         proto_tree **new_tree, guint *i)
249 {
250   guint cls, con, tag;
251   gboolean def;
252   guint length;
253   const guchar *start = a->pointer;
254   
255   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
256     return 1;
257   if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_BOL)
258     return 1;
259
260   return read_boolean_value(a, tree, hf_id, new_tree, i, start, length);
261 }
262
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)
265 {
266   guchar *string;
267   
268   if (length)
269   {
270     asn1_octet_string_value_decode(a, length, &string);
271     string = g_realloc(string, length + 1);
272     string[length] = '\0';
273   }
274   else
275     string = "(null)";
276     
277   if (tree)
278   {
279     proto_tree *temp_tree;
280     temp_tree = proto_tree_add_string(tree, hf_id, NullTVB, start - a->begin, a->pointer - start, string);
281     if (new_tree)
282       *new_tree = temp_tree;
283   }
284
285   if (s && length)
286     *s = string;
287   else if (length)
288     g_free(string);
289 }
290
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)
293 {
294   guint cls, con, tag;
295   gboolean def;
296   guint length;
297   const guchar *start = a->pointer;
298   int ret;
299   
300   ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
301   if (ret != ASN1_ERR_NOERROR)
302     return ret;
303   if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
304     return ASN1_ERR_WRONG_TYPE;
305
306   read_string_value(a, tree, hf_id, new_tree, s, start, length);
307   return ASN1_ERR_NOERROR;
308 }
309
310 static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length, const guchar *operation)
311 {
312   guchar *string;
313   guchar *string2;
314   gint string_length;
315   gint string2_length;
316   guint string_bytes;
317   int ret;
318
319   ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
320   if (ret != ASN1_ERR_NOERROR)
321     return ret;
322   ret = asn1_octet_string_decode(a, &string2, &string2_length, &string_bytes);
323   if (ret != ASN1_ERR_NOERROR)
324     return ret;
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);
328   g_free(string);
329   g_free(string2);
330   return ASN1_ERR_NOERROR;
331 }
332
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)
335 {
336   guchar *end;
337   guchar *string = NULL;
338   gint string_length;
339   guint string_bytes;
340   guint seq_len;
341   guint header_bytes;  
342   int ret, any_valued;
343
344   /* For ASN.1 parsing of octet strings */
345   guint        cls;
346   guint        con;
347   guint        tag;
348   gboolean     def;
349
350   ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
351   if (ret != ASN1_ERR_NOERROR)
352     return ret;
353
354   ret = asn1_sequence_decode(a, &seq_len, &header_bytes);
355   if (ret != ASN1_ERR_NOERROR)
356     return ret;
357
358   *filter_length += 2 + 1 + strlen(string);
359   *filter = g_realloc(*filter, *filter_length);
360   sprintf(*filter + strlen(*filter), "(%.*s=", string_length, string);
361   g_free(string);
362
363   /* Now decode seq_len's worth of octet strings. */
364   any_valued = 0;
365   end = (guchar *) (a->pointer + seq_len);
366
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)
372       return ret;
373
374     if (cls != ASN1_CTX || con != ASN1_PRI) {
375         /* XXX - handle the constructed encoding? */
376         return ASN1_ERR_WRONG_TYPE;
377     }
378     if (!def)
379         return ASN1_ERR_LENGTH_NOT_DEFINITE;
380
381     ret = asn1_octet_string_value_decode(a, (int) string_length, &string);
382     if (ret != ASN1_ERR_NOERROR)
383       return ret;
384
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))
388       any_valued = 1;
389
390     if ( (tag == 1) || ((tag == 2) && any_valued) )
391       (*filter_length)++;
392     *filter_length += strlen(string);
393     *filter = g_realloc(*filter, *filter_length);
394
395     if ( (tag == 1) || ((tag == 2) && any_valued) )
396       strcat(*filter, "*");
397     if (tag == 2)
398       any_valued = 0;
399     sprintf(*filter + strlen(*filter), "%.*s", string_length, string);
400     g_free(string);
401   }
402
403   if (any_valued)
404   {
405     (*filter_length)++;
406     *filter = g_realloc(*filter, *filter_length);
407     strcat(*filter, "*");
408   }
409   
410   /* NB: Allocated byte for this earlier */
411   strcat(*filter, ")");
412
413   return ASN1_ERR_NOERROR;
414 }
415
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)
418 {
419   guint cls, con, tag;
420   guint length;
421   gboolean def;
422   int ret;
423
424   ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
425   if (ret != ASN1_ERR_NOERROR)
426     return ret;
427   
428   if (*end == 0)
429   {
430     *end = a->pointer + length;
431     *filter_length = 1;
432     *filter = g_malloc0(*filter_length);
433   }
434
435   if (cls == ASN1_CTX)  /* XXX - handle other types as errors? */
436   {
437     switch (tag)
438     {
439      case LDAP_FILTER_AND:
440       {
441         const guchar *add_end;
442
443         if (con != ASN1_CON)
444           return ASN1_ERR_WRONG_TYPE;
445         add_end = a->pointer + length;
446         *filter_length += 3;
447         *filter = g_realloc(*filter, *filter_length);
448         strcat(*filter, "(&");
449         while ((ret = parse_filter(a, filter, filter_length, &add_end))
450                 == ASN1_ERR_NOERROR)
451           continue;
452         if (ret != -1)
453           return ret;
454         strcat(*filter, ")");
455       }
456       break;
457      case LDAP_FILTER_OR:
458       {
459         const guchar *or_end;
460
461         if (con != ASN1_CON)
462           return ASN1_ERR_WRONG_TYPE;
463         or_end = a->pointer + length;
464         *filter_length += 3;
465         *filter = g_realloc(*filter, *filter_length);
466         strcat(*filter, "(|");
467         while ((ret = parse_filter(a, filter, filter_length, &or_end))
468                 == ASN1_ERR_NOERROR)
469           continue;
470         if (ret != -1)
471           return ret;
472         strcat(*filter, ")");
473       }
474       break;
475      case LDAP_FILTER_NOT:
476       {
477         const guchar *not_end;
478
479         if (con != ASN1_CON)
480           return ASN1_ERR_WRONG_TYPE;
481         not_end = a->pointer + length;
482         *filter_length += 3;
483         *filter = g_realloc(*filter, *filter_length);
484         strcat(*filter, "(!");
485         ret = parse_filter(a, filter, filter_length, &not_end);
486         if (ret != -1 && ret != ASN1_ERR_NOERROR)
487           return ret;
488         strcat(*filter, ")");
489       }
490       break;
491      case LDAP_FILTER_EQUALITY:
492       if (con != ASN1_CON)
493         return ASN1_ERR_WRONG_TYPE;
494       ret = parse_filter_strings(a, filter, filter_length, "=");
495       if (ret != -1 && ret != ASN1_ERR_NOERROR)
496         return ret;
497       break;
498      case LDAP_FILTER_GE:
499       if (con != ASN1_CON)
500         return ASN1_ERR_WRONG_TYPE;
501       ret = parse_filter_strings(a, filter, filter_length, ">=");
502       if (ret != -1 && ret != ASN1_ERR_NOERROR)
503         return ret;
504       break;
505      case LDAP_FILTER_LE:
506       if (con != ASN1_CON)
507         return ASN1_ERR_WRONG_TYPE;
508       ret = parse_filter_strings(a, filter, filter_length, "<=");
509       if (ret != -1 && ret != ASN1_ERR_NOERROR)
510         return ret;
511       break;
512      case LDAP_FILTER_APPROX:
513       if (con != ASN1_CON)
514         return ASN1_ERR_WRONG_TYPE;
515       ret = parse_filter_strings(a, filter, filter_length, "~=");
516       if (ret != -1 && ret != ASN1_ERR_NOERROR)
517         return ret;
518       break;
519      case LDAP_FILTER_PRESENT:
520       {
521         guchar *string;
522     
523         if (con != ASN1_PRI)
524           return ASN1_ERR_WRONG_TYPE;
525         ret = asn1_octet_string_value_decode(a, length, &string);
526         if (ret != ASN1_ERR_NOERROR)
527           return ret;
528         *filter_length += 4 + length;
529         *filter = g_realloc(*filter, *filter_length);
530         sprintf(*filter + strlen(*filter), "(%.*s=*)", (int)length, string);
531         g_free(string);
532       }
533       break;
534      case LDAP_FILTER_SUBSTRINGS:
535       if (con != ASN1_CON)
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)
540         return ret;
541       break;
542      default:
543       return ASN1_ERR_WRONG_TYPE;
544     }
545   }
546   
547   if (a->pointer == *end)
548     return -1;
549   else
550     return ret;
551 }
552
553 static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
554 {
555   const guchar *start = a->pointer;
556   char *filter = 0;
557   guint filter_length = 0;
558   const guchar *end = 0;
559   int ret;
560      
561   while ((ret = parse_filter(a, &filter, &filter_length, &end))
562         == ASN1_ERR_NOERROR)
563     continue;
564
565   if (tree) {
566     if (ret != -1) {
567       proto_tree_add_text(tree, NullTVB, start-a->begin, 0,
568         "Error parsing filter (%d)", ret);
569     } else
570       proto_tree_add_string(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, filter);
571   }
572
573   g_free(filter);
574
575   return 0;
576 }
577
578 /********************************************************************************************/
579
580 static int dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
581 {
582   guint resultCode = 0;
583   
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);
587
588   if (resultCode == 10)         /* Referral */
589   {
590     const guchar *start = a->pointer;
591     const guchar *end;
592     guint length;
593     proto_tree *t, *referralTree;
594     
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);
598
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);
602   }
603     
604   return 0;
605 }
606
607 static int dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
608 {
609   guint cls, con, tag;
610   guint def, length;
611   const guchar *start;
612
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);
615
616   start = a->pointer;
617   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
618     return 1;   /* XXX - right return value for an error? */
619   if (cls != ASN1_CTX)
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);
623   switch (tag)
624   {
625    case LDAP_AUTH_SIMPLE:
626     read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL, NULL,
627                         start, length);
628     break;
629
630     /* For Kerberos V4, dissect it as a ticket. */
631     /* For SASL, dissect it as SaslCredentials. */
632   }
633   
634   return 0;
635 }
636
637 static int dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
638 {
639   dissect_ldap_result(a, tree);
640   /* FIXME: handle SASL data */
641   return 0;
642 }
643
644 static int dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
645 {
646   guint seq_length;
647   const guchar *end;
648   int ret;
649   
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)
658     return ret;
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)
664       return ret;
665   }
666   return ASN1_ERR_NOERROR;
667 }
668
669 static int dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
670 {
671   guint seq_length;
672   const guchar *end_of_sequence;
673  
674   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
675   read_sequence(a, &seq_length);
676
677   end_of_sequence = a->pointer + seq_length;
678   while (a->pointer < end_of_sequence)
679   {
680     proto_tree *t, *attr_tree;
681     guint set_length;
682     const guchar *end_of_set;
683
684     read_sequence(a, 0);
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);
687
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);
692   }
693
694   return 0;
695 }
696
697 static int dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
698 {
699   guint seq_length;
700   const guchar *end_of_sequence;
701   
702   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
703
704   read_sequence(a, &seq_length);
705   end_of_sequence = a->pointer + seq_length;
706   while (a->pointer < end_of_sequence)
707   {
708     proto_tree *t, *attr_tree;
709     guint set_length;
710     const guchar *end_of_set;
711
712     read_sequence(a, 0);
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);
715
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);
720   }
721
722   return 0;
723 }
724
725 static int dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
726                 const guchar *start, guint length)
727 {
728   read_string_value(a, tree, hf_ldap_message_dn, NULL, NULL, start, length);
729   return 0;
730 }
731
732 static int dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
733                 guint length)
734 {
735   const guchar *start = a->pointer;
736
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);
740   
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);
744   }
745
746   return 0;
747 }
748
749 static int dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
750 {
751   const guchar *start;
752   int length;
753   char *string1 = 0;
754   char *string2 = 0;
755   char *compare;
756   
757   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
758   read_sequence(a, 0);
759
760   start = a->pointer;
761   read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
762   read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
763
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);
768   
769   g_free(string1);
770   g_free(string2);
771   g_free(compare);
772   
773   return 0;
774 }
775
776 static int dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
777 {
778   guint seq_length;
779   const guchar *end_of_sequence;
780   
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)
785   {
786     proto_tree *t = 0, *attr_tree;
787     guint set_length;
788     const guchar *end_of_set;
789     guint operation;
790
791     read_sequence(a, 0);
792     read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
793     read_sequence(a, 0);
794
795     switch (operation)
796     {
797      case LDAP_MOD_ADD:
798       read_string(a, tree, hf_ldap_message_modify_add, &t, 0, ASN1_UNI, ASN1_OTS);
799       break;
800      case LDAP_MOD_REPLACE:
801       read_string(a, tree, hf_ldap_message_modify_replace, &t, 0, ASN1_UNI, ASN1_OTS);
802       break;
803      case LDAP_MOD_DELETE:
804       read_string(a, tree, hf_ldap_message_modify_delete, &t, 0, ASN1_UNI, ASN1_OTS);
805       break;
806     }
807     attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
808
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);
813   }
814
815   return 0;
816 }
817
818 static int dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
819                 const guchar *start, guint length)
820 {
821   read_integer_value(a, tree, hf_ldap_message_abandon_msgid, NULL, NULL,
822                         start, length); 
823   return 0;
824 }
825
826 static void
827 dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
828 {
829   proto_tree *ldap_tree = 0, *ti, *msg_tree;
830   guint messageLength;
831   guint messageId;
832   guint protocolOpCls, protocolOpCon, protocolOpTag;
833   gchar *typestr;
834   guint opLen;
835   ASN1_SCK a;
836   const guchar *start;
837   int first_time = 1;
838   int ret;
839
840   if (tree) 
841   {
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);
844   }
845
846   asn1_open(&a, pd, pi.captured_len);
847   a.pointer += offset;
848
849   while (a.pointer < a.end)
850   {
851     int message_id_start;
852     int message_id_length;
853     int message_start;
854     
855     message_start = a.pointer - a.begin;
856     if (read_sequence(&a, &messageLength))
857     {
858       if (ldap_tree)
859         proto_tree_add_text(ldap_tree, NullTVB, offset, 1, "Invalid LDAP packet");
860       break;
861     }
862
863     if (messageLength > (a.end - a.pointer))
864     {
865       if (ldap_tree)
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);
868       break;
869     }
870   
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;
874
875     start = a.pointer;
876     asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
877     if (protocolOpCls != ASN1_APL)
878       typestr = "Bad message type (not Application)";
879     else
880       typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
881
882     if (first_time)
883     {
884       if (check_col(fd, COL_PROTOCOL))
885         col_add_str(fd, COL_PROTOCOL, "LDAP");
886
887       if (check_col(fd, COL_INFO))
888         col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
889                      messageId, typestr);
890       first_time = 0;
891       if (!tree)
892         return;
893     }
894
895     if (ldap_tree) 
896     {
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);
902       start = a.pointer;
903       read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
904
905       switch (protocolOpTag)
906       {
907        case LDAP_REQ_BIND:
908         dissect_ldap_request_bind(&a, msg_tree);
909         break;
910        case LDAP_REQ_SEARCH:
911         ret = dissect_ldap_request_search(&a, msg_tree);
912         if (ret != ASN1_ERR_NOERROR)
913           break;
914         break;
915        case LDAP_REQ_ADD:
916         dissect_ldap_request_add(&a, msg_tree);
917         break;
918        case LDAP_REQ_DELETE:
919         dissect_ldap_request_delete(&a, msg_tree, start, opLen);
920         break;
921        case LDAP_REQ_MODRDN:
922         dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
923         break;
924        case LDAP_REQ_COMPARE:
925         dissect_ldap_request_compare(&a, msg_tree);
926         break;
927        case LDAP_REQ_MODIFY:
928         dissect_ldap_request_modify(&a, msg_tree);
929         break;
930        case LDAP_REQ_ABANDON:
931         dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
932         break;
933        case LDAP_RES_BIND:
934         dissect_ldap_response_bind(&a, msg_tree);
935         break;
936        case LDAP_RES_SEARCH_ENTRY:
937         dissect_ldap_response_search_entry(&a, msg_tree);
938         break;
939        case LDAP_RES_SEARCH_RESULT:
940        case LDAP_RES_MODIFY:
941        case LDAP_RES_ADD:
942        case LDAP_RES_DELETE:
943        case LDAP_RES_MODRDN:
944        case LDAP_RES_COMPARE:
945         dissect_ldap_result(&a, msg_tree);
946         break;
947       }
948     }
949   }
950 }
951
952 void
953 proto_register_ldap(void)
954 {
955   static value_string result_codes[] = {
956     {0, "Success"},
957     {1, "Operations error"},
958     {2, "Protocol error"},
959     {3, "Time limit exceeded"},
960     {4, "Size limit exceeded"},
961     {5, "Compare false"},
962     {6, "Compare true"},
963     {7, "Authentication method not supported"},
964     {8, "Strong authentication required"},
965     {10, "Referral"},
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"},
983     {51, "Busy"},
984     {52, "Unavailable"},
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"},
994     {80, "Other"},
995   };
996
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"},
1002   };
1003   
1004   static value_string search_scope[] = {
1005     {0x00, "Base"},
1006     {0x01, "Single"},
1007     {0x02, "Subtree"},
1008   };
1009     
1010   static value_string search_dereference[] = {
1011     {0x00, "Never"},
1012     {0x01, "Searching"},
1013     {0x02, "Base Object"},
1014     {0x03, "Always"},
1015   };
1016   
1017   static hf_register_info hf[] = {
1018     { &hf_ldap_length,
1019       { "Length",               "ldap.length",
1020         FT_UINT32, BASE_DEC, NULL, 0x0,
1021         "LDAP Length" }},
1022           
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" }},
1035
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" }},
1052
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" }},
1069
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,
1109         "LDAP Value" }},
1110
1111     { &hf_ldap_message_modrdn_name,
1112       { "New Name",             "ldap.modrdn.name",
1113         FT_STRING, BASE_NONE, NULL, 0x0,
1114         "LDAP New Name" }},
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" }},
1123
1124     { &hf_ldap_message_compare,
1125       { "Test",         "ldap.compare.test",
1126         FT_STRING, BASE_NONE, NULL, 0x0,
1127         "LDAP Compare Test" }},
1128
1129     { &hf_ldap_message_modify_add,
1130       { "Add",                  "ldap.modify.add",
1131         FT_STRING, BASE_NONE, NULL, 0x0,
1132         "LDAP Add" }},
1133     { &hf_ldap_message_modify_replace,
1134       { "Replace",              "ldap.modify.replace",
1135         FT_STRING, BASE_NONE, NULL, 0x0,
1136         "LDAP Replace" }},
1137     { &hf_ldap_message_modify_delete,
1138       { "Delete",               "ldap.modify.delete",
1139         FT_STRING, BASE_NONE, NULL, 0x0,
1140         "LDAP Delete" }},
1141
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" }},
1146   };
1147
1148   static gint *ett[] = {
1149     &ett_ldap,
1150     &ett_ldap_message,
1151     &ett_ldap_referrals,
1152     &ett_ldap_attribute
1153   };
1154
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));
1158 }
1159
1160 void
1161 proto_reg_handoff_ldap(void)
1162 {
1163   old_dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap);
1164 }