Ensure that all value_string arrays end in {0, NULL}. Dissectors got away
[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.20 2001/01/03 16:41:06 gram 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   {0, NULL},
137 };
138
139 static int read_length(ASN1_SCK *a, proto_tree *tree, int hf_id, guint *len)
140 {
141   guint length = 0;
142   gboolean def = FALSE;
143   const guchar *start = a->pointer;
144   
145   asn1_length_decode(a, &def, &length);
146
147   if (len)
148     *len = length;
149
150   if (tree)
151     proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, length);
152
153   return 0;
154 }
155
156 static int read_sequence(ASN1_SCK *a, guint *len)
157 {
158   guint cls, con, tag;
159   gboolean def;
160   guint length;
161   
162   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
163     return 1;
164   if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SEQ)
165     return 1;
166   
167   if (len)
168     *len = length;
169   
170   return 0;
171 }
172
173 static int read_set(ASN1_SCK *a, guint *len)
174 {
175   guint cls, con, tag;
176   gboolean def;
177   guint length;
178   
179   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
180     return 1;
181   if (cls != ASN1_UNI || con != ASN1_CON || tag != ASN1_SET)
182     return 1;
183   
184   if (len)
185     *len = length;
186   
187   return 0;
188 }
189
190 static int read_integer_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
191         proto_tree **new_tree, guint *i, const guchar *start, guint length)
192 {
193   guint integer = 0;
194
195   asn1_uint32_value_decode(a, length, &integer);
196
197   if (i)
198     *i = integer;
199
200   if (tree)
201   {
202     proto_tree *temp_tree = 0;
203     temp_tree = proto_tree_add_uint(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
204     if (new_tree)
205       *new_tree = temp_tree;
206   }
207
208   return 0;
209 }
210
211 static int read_integer(ASN1_SCK *a, proto_tree *tree, int hf_id,
212         proto_tree **new_tree, guint *i, guint expected_tag)
213 {
214   guint cls, con, tag;
215   gboolean def;
216   guint length;
217   const guchar *start = a->pointer;
218   
219   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
220     return 1;
221   if (cls != ASN1_UNI || con != ASN1_PRI || tag != expected_tag)
222     return 1;
223
224   return read_integer_value(a, tree, hf_id, new_tree, i, start, length);
225 }
226
227 static int read_boolean_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
228         proto_tree **new_tree, guint *i, const guchar *start, guint length)
229 {
230   guint integer = 0;
231
232   asn1_uint32_value_decode(a, length, &integer);
233
234   if (i)
235     *i = integer;
236
237   if (tree)
238   {
239     proto_tree *temp_tree = 0;
240     temp_tree = proto_tree_add_boolean(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, integer);
241     if (new_tree)
242       *new_tree = temp_tree;
243   }
244
245   return 0;
246 }
247
248 static int read_boolean(ASN1_SCK *a, proto_tree *tree, int hf_id,
249         proto_tree **new_tree, guint *i)
250 {
251   guint cls, con, tag;
252   gboolean def;
253   guint length;
254   const guchar *start = a->pointer;
255   
256   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
257     return 1;
258   if (cls != ASN1_UNI || con != ASN1_PRI || tag != ASN1_BOL)
259     return 1;
260
261   return read_boolean_value(a, tree, hf_id, new_tree, i, start, length);
262 }
263
264 static void read_string_value(ASN1_SCK *a, proto_tree *tree, int hf_id,
265         proto_tree **new_tree, char **s, const guchar *start, guint length)
266 {
267   guchar *string;
268   
269   if (length)
270   {
271     asn1_string_value_decode(a, length, &string);
272     string = g_realloc(string, length + 1);
273     string[length] = '\0';
274   }
275   else
276     string = "(null)";
277     
278   if (tree)
279   {
280     proto_tree *temp_tree;
281     temp_tree = proto_tree_add_string(tree, hf_id, NullTVB, start - a->begin, a->pointer - start, string);
282     if (new_tree)
283       *new_tree = temp_tree;
284   }
285
286   if (s && length)
287     *s = string;
288   else if (length)
289     g_free(string);
290 }
291
292 static int read_string(ASN1_SCK *a, proto_tree *tree, int hf_id,
293         proto_tree **new_tree, char **s, guint expected_cls, guint expected_tag)
294 {
295   guint cls, con, tag;
296   gboolean def;
297   guint length;
298   const guchar *start = a->pointer;
299   int ret;
300   
301   ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
302   if (ret != ASN1_ERR_NOERROR)
303     return ret;
304   if (cls != expected_cls || con != ASN1_PRI || tag != expected_tag)
305     return ASN1_ERR_WRONG_TYPE;
306
307   read_string_value(a, tree, hf_id, new_tree, s, start, length);
308   return ASN1_ERR_NOERROR;
309 }
310
311 static int parse_filter_strings(ASN1_SCK *a, char **filter, guint *filter_length, const guchar *operation)
312 {
313   guchar *string;
314   guchar *string2;
315   guint string_length;
316   guint string2_length;
317   guint string_bytes;
318   char *filterp;
319   int ret;
320
321   ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
322   if (ret != ASN1_ERR_NOERROR)
323     return ret;
324   ret = asn1_octet_string_decode(a, &string2, &string2_length, &string_bytes);
325   if (ret != ASN1_ERR_NOERROR)
326     return ret;
327   *filter_length += 2 + strlen(operation) + string_length + string2_length;
328   *filter = g_realloc(*filter, *filter_length);
329   filterp = *filter + strlen(*filter);
330   *filterp++ = '(';
331   if (string_length != 0) {
332         memcpy(filterp, string, string_length);
333         filterp += string_length;
334   }
335   strcpy(filterp, operation);
336   filterp += strlen(operation);
337   if (string2_length != 0) {
338         memcpy(filterp, string2, string2_length);
339         filterp += string2_length;
340   }
341   *filterp++ = ')';
342   *filterp = '\0';
343   g_free(string);
344   g_free(string2);
345   return ASN1_ERR_NOERROR;
346 }
347
348 /* Richard Dawe: To parse substring filters, I added this function. */
349 static int parse_filter_substrings(ASN1_SCK *a, char **filter, guint *filter_length)
350 {
351   guchar *end;
352   guchar *string;
353   char *filterp;
354   guint string_length;
355   guint string_bytes;
356   guint seq_len;
357   guint header_bytes;  
358   int ret, any_valued;
359
360   /* For ASN.1 parsing of octet strings */
361   guint        cls;
362   guint        con;
363   guint        tag;
364   gboolean     def;
365
366   ret = asn1_octet_string_decode(a, &string, &string_length, &string_bytes);
367   if (ret != ASN1_ERR_NOERROR)
368     return ret;
369
370   ret = asn1_sequence_decode(a, &seq_len, &header_bytes);
371   if (ret != ASN1_ERR_NOERROR)
372     return ret;
373
374   *filter_length += 2 + 1 + string_length;
375   *filter = g_realloc(*filter, *filter_length);
376   
377   filterp = *filter + strlen(*filter);
378   *filterp++ = '(';
379   if (string_length != 0) {
380     memcpy(filterp, string, string_length);
381     filterp += string_length;
382   }
383   *filterp++ = '=';
384   *filterp = '\0';
385   g_free(string);
386
387   /* Now decode seq_len's worth of octet strings. */
388   any_valued = 0;
389   end = (guchar *) (a->pointer + seq_len);
390
391   while (a->pointer < end) {
392     /* Octet strings here are context-specific, which
393      * asn1_octet_string_decode() barfs on. Emulate it, but don't barf. */
394     ret = asn1_header_decode (a, &cls, &con, &tag, &def, &string_length);
395     if (ret != ASN1_ERR_NOERROR)
396       return ret;
397
398     /* XXX - check the tag? */
399     if (cls != ASN1_CTX || con != ASN1_PRI) {
400         /* XXX - handle the constructed encoding? */
401         return ASN1_ERR_WRONG_TYPE;
402     }
403     if (!def)
404         return ASN1_ERR_LENGTH_NOT_DEFINITE;
405
406     ret = asn1_string_value_decode(a, (int) string_length, &string);
407     if (ret != ASN1_ERR_NOERROR)
408       return ret;
409
410     /* If we have an 'any' component with a string value, we need to append
411      * an extra asterisk before final component. */
412     if ((tag == 1) && (string_length != 0))
413       any_valued = 1;
414
415     if ( (tag == 1) || ((tag == 2) && any_valued) )
416       (*filter_length)++;
417     *filter_length += string_length;
418     *filter = g_realloc(*filter, *filter_length);
419
420     filterp = *filter + strlen(*filter);
421     if ( (tag == 1) || ((tag == 2) && any_valued) )
422       *filterp++ = '*';
423     if (tag == 2)
424       any_valued = 0;
425     if (string_length != 0) {
426       memcpy(filterp, string, string_length);
427       filterp += string_length;
428     }
429     *filterp = '\0';
430     g_free(string);
431   }
432
433   if (any_valued)
434   {
435     (*filter_length)++;
436     *filter = g_realloc(*filter, *filter_length);
437     filterp = *filter + strlen(*filter);
438     *filterp++ = '*';
439   }
440   
441   /* NB: Allocated byte for this earlier */
442   *filterp++ = ')';
443   *filterp = '\0';
444
445   return ASN1_ERR_NOERROR;
446 }
447
448 /* Returns -1 if we're at the end, returns an ASN1_ERR value otherwise. */
449 static int parse_filter(ASN1_SCK *a, char **filter, guint *filter_length, const guchar **end)
450 {
451   guint cls, con, tag;
452   guint length;
453   gboolean def;
454   int ret;
455
456   ret = asn1_header_decode(a, &cls, &con, &tag, &def, &length);
457   if (ret != ASN1_ERR_NOERROR)
458     return ret;
459   
460   if (*end == 0)
461   {
462     *end = a->pointer + length;
463     *filter_length = 1;
464     *filter = g_malloc0(*filter_length);
465   }
466
467   if (cls == ASN1_CTX)  /* XXX - handle other types as errors? */
468   {
469     switch (tag)
470     {
471      case LDAP_FILTER_AND:
472       {
473         const guchar *add_end;
474
475         if (con != ASN1_CON)
476           return ASN1_ERR_WRONG_TYPE;
477         add_end = a->pointer + length;
478         *filter_length += 3;
479         *filter = g_realloc(*filter, *filter_length);
480         strcat(*filter, "(&");
481         while ((ret = parse_filter(a, filter, filter_length, &add_end))
482                 == ASN1_ERR_NOERROR)
483           continue;
484         if (ret != -1)
485           return ret;
486         strcat(*filter, ")");
487       }
488       break;
489      case LDAP_FILTER_OR:
490       {
491         const guchar *or_end;
492
493         if (con != ASN1_CON)
494           return ASN1_ERR_WRONG_TYPE;
495         or_end = a->pointer + length;
496         *filter_length += 3;
497         *filter = g_realloc(*filter, *filter_length);
498         strcat(*filter, "(|");
499         while ((ret = parse_filter(a, filter, filter_length, &or_end))
500                 == ASN1_ERR_NOERROR)
501           continue;
502         if (ret != -1)
503           return ret;
504         strcat(*filter, ")");
505       }
506       break;
507      case LDAP_FILTER_NOT:
508       {
509         const guchar *not_end;
510
511         if (con != ASN1_CON)
512           return ASN1_ERR_WRONG_TYPE;
513         not_end = a->pointer + length;
514         *filter_length += 3;
515         *filter = g_realloc(*filter, *filter_length);
516         strcat(*filter, "(!");
517         ret = parse_filter(a, filter, filter_length, &not_end);
518         if (ret != -1 && ret != ASN1_ERR_NOERROR)
519           return ret;
520         strcat(*filter, ")");
521       }
522       break;
523      case LDAP_FILTER_EQUALITY:
524       if (con != ASN1_CON)
525         return ASN1_ERR_WRONG_TYPE;
526       ret = parse_filter_strings(a, filter, filter_length, "=");
527       if (ret != -1 && ret != ASN1_ERR_NOERROR)
528         return ret;
529       break;
530      case LDAP_FILTER_GE:
531       if (con != ASN1_CON)
532         return ASN1_ERR_WRONG_TYPE;
533       ret = parse_filter_strings(a, filter, filter_length, ">=");
534       if (ret != -1 && ret != ASN1_ERR_NOERROR)
535         return ret;
536       break;
537      case LDAP_FILTER_LE:
538       if (con != ASN1_CON)
539         return ASN1_ERR_WRONG_TYPE;
540       ret = parse_filter_strings(a, filter, filter_length, "<=");
541       if (ret != -1 && ret != ASN1_ERR_NOERROR)
542         return ret;
543       break;
544      case LDAP_FILTER_APPROX:
545       if (con != ASN1_CON)
546         return ASN1_ERR_WRONG_TYPE;
547       ret = parse_filter_strings(a, filter, filter_length, "~=");
548       if (ret != -1 && ret != ASN1_ERR_NOERROR)
549         return ret;
550       break;
551      case LDAP_FILTER_PRESENT:
552       {
553         guchar *string;
554         char *filterp;
555     
556         if (con != ASN1_PRI)
557           return ASN1_ERR_WRONG_TYPE;
558         ret = asn1_string_value_decode(a, length, &string);
559         if (ret != ASN1_ERR_NOERROR)
560           return ret;
561         *filter_length += 4 + length;
562         *filter = g_realloc(*filter, *filter_length);
563         filterp = *filter + strlen(*filter);
564         *filterp++ = '(';
565         if (length != 0) {
566           memcpy(filterp, string, length);
567           filterp += length;
568         }
569         *filterp++ = '=';
570         *filterp++ = '*';
571         *filterp++ = ')';
572         *filterp = '\0';
573         g_free(string);
574       }
575       break;
576      case LDAP_FILTER_SUBSTRINGS:
577       if (con != ASN1_CON)
578         return ASN1_ERR_WRONG_TYPE;
579       /* Richard Dawe: Handle substrings */
580       ret = parse_filter_substrings(a, filter, filter_length);
581       if (ret != -1 && ret != ASN1_ERR_NOERROR)
582         return ret;
583       break;
584      default:
585       return ASN1_ERR_WRONG_TYPE;
586     }
587   }
588   
589   if (a->pointer == *end)
590     return -1;
591   else
592     return ret;
593 }
594
595 static int read_filter(ASN1_SCK *a, proto_tree *tree, int hf_id)
596 {
597   const guchar *start = a->pointer;
598   char *filter = 0;
599   guint filter_length = 0;
600   const guchar *end = 0;
601   int ret;
602      
603   while ((ret = parse_filter(a, &filter, &filter_length, &end))
604         == ASN1_ERR_NOERROR)
605     continue;
606
607   if (tree) {
608     if (ret != -1) {
609       proto_tree_add_text(tree, NullTVB, start-a->begin, 0,
610         "Error parsing filter (%d)", ret);
611     } else
612       proto_tree_add_string(tree, hf_id, NullTVB, start-a->begin, a->pointer-start, filter);
613   }
614
615   g_free(filter);
616
617   return 0;
618 }
619
620 /********************************************************************************************/
621
622 static int dissect_ldap_result(ASN1_SCK *a, proto_tree *tree)
623 {
624   guint resultCode = 0;
625   
626   read_integer(a, tree, hf_ldap_message_result, 0, &resultCode, ASN1_ENUM);
627   read_string(a, tree, hf_ldap_message_result_matcheddn, 0, 0, ASN1_UNI, ASN1_OTS);
628   read_string(a, tree, hf_ldap_message_result_errormsg, 0, 0, ASN1_UNI, ASN1_OTS);
629
630   if (resultCode == 10)         /* Referral */
631   {
632     const guchar *start = a->pointer;
633     const guchar *end;
634     guint length;
635     proto_tree *t, *referralTree;
636     
637     read_sequence(a, &length);
638     t = proto_tree_add_text(tree, NullTVB, start-a->begin, length, "Referral URLs");
639     referralTree = proto_item_add_subtree(t, ett_ldap_referrals);
640
641     end = a->pointer + length;;
642     while (a->pointer < end)
643       read_string(a, referralTree, hf_ldap_message_result_referral, 0, 0, ASN1_UNI, ASN1_OTS);
644   }
645     
646   return 0;
647 }
648
649 static int dissect_ldap_request_bind(ASN1_SCK *a, proto_tree *tree)
650 {
651   guint cls, con, tag;
652   guint def, length;
653   const guchar *start;
654
655   read_integer(a, tree, hf_ldap_message_bind_version, 0, 0, ASN1_INT);
656   read_string(a, tree, hf_ldap_message_bind_dn, 0, 0, ASN1_UNI, ASN1_OTS);
657
658   start = a->pointer;
659   if (asn1_header_decode(a, &cls, &con, &tag, &def, &length) != ASN1_ERR_NOERROR)
660     return 1;   /* XXX - right return value for an error? */
661   if (cls != ASN1_CTX)
662     return 1;   /* RFCs 1777 and 2251 say these are context-specific types */
663   proto_tree_add_uint(tree, hf_ldap_message_bind_auth, NullTVB, start - a->begin,
664                         a->pointer - start, tag);
665   switch (tag)
666   {
667    case LDAP_AUTH_SIMPLE:
668     read_string_value(a, tree, hf_ldap_message_bind_auth_password, NULL, NULL,
669                         start, length);
670     break;
671
672     /* For Kerberos V4, dissect it as a ticket. */
673     /* For SASL, dissect it as SaslCredentials. */
674   }
675   
676   return 0;
677 }
678
679 static int dissect_ldap_response_bind(ASN1_SCK *a, proto_tree *tree)
680 {
681   dissect_ldap_result(a, tree);
682   /* FIXME: handle SASL data */
683   return 0;
684 }
685
686 static int dissect_ldap_request_search(ASN1_SCK *a, proto_tree *tree)
687 {
688   guint seq_length;
689   const guchar *end;
690   int ret;
691   
692   read_string(a, tree, hf_ldap_message_search_base, 0, 0, ASN1_UNI, ASN1_OTS);
693   read_integer(a, tree, hf_ldap_message_search_scope, 0, 0, ASN1_ENUM);
694   read_integer(a, tree, hf_ldap_message_search_deref, 0, 0, ASN1_ENUM);
695   read_integer(a, tree, hf_ldap_message_search_sizeLimit, 0, 0, ASN1_INT);
696   read_integer(a, tree, hf_ldap_message_search_timeLimit, 0, 0, ASN1_INT);
697   read_boolean(a, tree, hf_ldap_message_search_typesOnly, 0, 0);
698   ret = read_filter(a, tree, hf_ldap_message_search_filter);
699   if (ret != ASN1_ERR_NOERROR)
700     return ret;
701   read_sequence(a, &seq_length);
702   end = a->pointer + seq_length;
703   while (a->pointer < end) {
704     ret = read_string(a, tree, hf_ldap_message_attribute, 0, 0, ASN1_UNI, ASN1_OTS);
705     if (ret != ASN1_ERR_NOERROR)
706       return ret;
707   }
708   return ASN1_ERR_NOERROR;
709 }
710
711 static int dissect_ldap_response_search_entry(ASN1_SCK *a, proto_tree *tree)
712 {
713   guint seq_length;
714   const guchar *end_of_sequence;
715  
716   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
717   read_sequence(a, &seq_length);
718
719   end_of_sequence = a->pointer + seq_length;
720   while (a->pointer < end_of_sequence)
721   {
722     proto_tree *t, *attr_tree;
723     guint set_length;
724     const guchar *end_of_set;
725
726     read_sequence(a, 0);
727     read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
728     attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
729
730     read_set(a, &set_length);
731     end_of_set = a->pointer + set_length;
732     while (a->pointer < end_of_set)
733       read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
734   }
735
736   return 0;
737 }
738
739 static int dissect_ldap_request_add(ASN1_SCK *a, proto_tree *tree)
740 {
741   guint seq_length;
742   const guchar *end_of_sequence;
743   
744   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
745
746   read_sequence(a, &seq_length);
747   end_of_sequence = a->pointer + seq_length;
748   while (a->pointer < end_of_sequence)
749   {
750     proto_tree *t, *attr_tree;
751     guint set_length;
752     const guchar *end_of_set;
753
754     read_sequence(a, 0);
755     read_string(a, tree, hf_ldap_message_attribute, &t, 0, ASN1_UNI, ASN1_OTS);
756     attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
757
758     read_set(a, &set_length);
759     end_of_set = a->pointer + set_length;
760     while (a->pointer < end_of_set)
761       read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
762   }
763
764   return 0;
765 }
766
767 static int dissect_ldap_request_delete(ASN1_SCK *a, proto_tree *tree,
768                 const guchar *start, guint length)
769 {
770   read_string_value(a, tree, hf_ldap_message_dn, NULL, NULL, start, length);
771   return 0;
772 }
773
774 static int dissect_ldap_request_modifyrdn(ASN1_SCK *a, proto_tree *tree,
775                 guint length)
776 {
777   const guchar *start = a->pointer;
778
779   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
780   read_string(a, tree, hf_ldap_message_modrdn_name, 0, 0, ASN1_UNI, ASN1_OTS);
781   read_boolean(a, tree, hf_ldap_message_modrdn_delete, 0, 0);
782   
783   if (a->pointer < (start + length)) {
784     /* LDAP V3 Modify DN operation, with newSuperior */
785     read_string(a, tree, hf_ldap_message_modrdn_superior, 0, 0, ASN1_UNI, ASN1_OTS);
786   }
787
788   return 0;
789 }
790
791 static int dissect_ldap_request_compare(ASN1_SCK *a, proto_tree *tree)
792 {
793   const guchar *start;
794   int length;
795   char *string1 = 0;
796   char *string2 = 0;
797   char *compare;
798   
799   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
800   read_sequence(a, 0);
801
802   start = a->pointer;
803   read_string(a, 0, -1, 0, &string1, ASN1_UNI, ASN1_OTS);
804   read_string(a, 0, -1, 0, &string2, ASN1_UNI, ASN1_OTS);
805
806   length = 2 + strlen(string1) + strlen(string2);
807   compare = g_malloc0(length);
808   snprintf(compare, length, "%s=%s", string1, string2);
809   proto_tree_add_string(tree, hf_ldap_message_compare, NullTVB, start-a->begin, a->pointer-start, compare);
810   
811   g_free(string1);
812   g_free(string2);
813   g_free(compare);
814   
815   return 0;
816 }
817
818 static int dissect_ldap_request_modify(ASN1_SCK *a, proto_tree *tree)
819 {
820   guint seq_length;
821   const guchar *end_of_sequence;
822   
823   read_string(a, tree, hf_ldap_message_dn, 0, 0, ASN1_UNI, ASN1_OTS);
824   read_sequence(a, &seq_length);
825   end_of_sequence = a->pointer + seq_length;
826   while (a->pointer < end_of_sequence)
827   {
828     proto_tree *t = 0, *attr_tree;
829     guint set_length;
830     const guchar *end_of_set;
831     guint operation;
832
833     read_sequence(a, 0);
834     read_integer(a, 0, -1, 0, &operation, ASN1_ENUM);
835     read_sequence(a, 0);
836
837     switch (operation)
838     {
839      case LDAP_MOD_ADD:
840       read_string(a, tree, hf_ldap_message_modify_add, &t, 0, ASN1_UNI, ASN1_OTS);
841       break;
842      case LDAP_MOD_REPLACE:
843       read_string(a, tree, hf_ldap_message_modify_replace, &t, 0, ASN1_UNI, ASN1_OTS);
844       break;
845      case LDAP_MOD_DELETE:
846       read_string(a, tree, hf_ldap_message_modify_delete, &t, 0, ASN1_UNI, ASN1_OTS);
847       break;
848     }
849     attr_tree = proto_item_add_subtree(t, ett_ldap_attribute);
850
851     read_set(a, &set_length);
852     end_of_set = a->pointer + set_length;
853     while (a->pointer < end_of_set)
854       read_string(a, attr_tree, hf_ldap_message_value, 0, 0, ASN1_UNI, ASN1_OTS);
855   }
856
857   return 0;
858 }
859
860 static int dissect_ldap_request_abandon(ASN1_SCK *a, proto_tree *tree,
861                 const guchar *start, guint length)
862 {
863   read_integer_value(a, tree, hf_ldap_message_abandon_msgid, NULL, NULL,
864                         start, length); 
865   return 0;
866 }
867
868 static void
869 dissect_ldap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
870 {
871   proto_tree *ldap_tree = 0, *ti, *msg_tree;
872   guint messageLength;
873   guint messageId;
874   guint protocolOpCls, protocolOpCon, protocolOpTag;
875   gchar *typestr;
876   guint opLen;
877   ASN1_SCK a;
878   const guchar *start;
879   int first_time = 1;
880   int ret;
881
882   OLD_CHECK_DISPLAY_AS_DATA(proto_ldap, pd, offset, fd, tree);
883
884   if (tree) 
885   {
886     ti = proto_tree_add_item(tree, proto_ldap, NullTVB, offset, END_OF_FRAME, FALSE);
887     ldap_tree = proto_item_add_subtree(ti, ett_ldap);
888   }
889
890   asn1_open(&a, pd, pi.captured_len);
891   a.pointer += offset;
892
893   while (a.pointer < a.end)
894   {
895     int message_id_start;
896     int message_id_length;
897     int message_start;
898     
899     message_start = a.pointer - a.begin;
900     if (read_sequence(&a, &messageLength))
901     {
902       if (ldap_tree)
903         proto_tree_add_text(ldap_tree, NullTVB, offset, 1, "Invalid LDAP packet");
904       break;
905     }
906
907     if (messageLength > (a.end - a.pointer))
908     {
909       if (ldap_tree)
910         proto_tree_add_text(ldap_tree, NullTVB, message_start, END_OF_FRAME, "Short message! (expected: %u, actual: %u)",
911                             messageLength, a.end - a.pointer);
912       break;
913     }
914   
915     message_id_start = a.pointer - a.begin;
916     read_integer(&a, 0, -1, 0, &messageId, ASN1_INT);
917     message_id_length = (a.pointer - a.begin) - message_id_start;
918
919     start = a.pointer;
920     asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
921     if (protocolOpCls != ASN1_APL)
922       typestr = "Bad message type (not Application)";
923     else
924       typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
925
926     if (first_time)
927     {
928       if (check_col(fd, COL_PROTOCOL))
929         col_set_str(fd, COL_PROTOCOL, "LDAP");
930
931       if (check_col(fd, COL_INFO))
932         col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
933                      messageId, typestr);
934       first_time = 0;
935       if (!tree)
936         return;
937     }
938
939     if (ldap_tree) 
940     {
941       proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_id, NullTVB, message_id_start, message_id_length, messageId);
942       proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_type, NullTVB,
943                                  start - a.begin, a.pointer - start, protocolOpTag);
944       ti = proto_tree_add_text(ldap_tree, NullTVB, message_id_start, messageLength, "Message: Id=%u  %s", messageId, typestr);
945       msg_tree = proto_item_add_subtree(ti, ett_ldap_message);
946       start = a.pointer;
947       read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
948
949       switch (protocolOpTag)
950       {
951        case LDAP_REQ_BIND:
952         dissect_ldap_request_bind(&a, msg_tree);
953         break;
954        case LDAP_REQ_SEARCH:
955         ret = dissect_ldap_request_search(&a, msg_tree);
956         if (ret != ASN1_ERR_NOERROR)
957           break;
958         break;
959        case LDAP_REQ_ADD:
960         dissect_ldap_request_add(&a, msg_tree);
961         break;
962        case LDAP_REQ_DELETE:
963         dissect_ldap_request_delete(&a, msg_tree, start, opLen);
964         break;
965        case LDAP_REQ_MODRDN:
966         dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
967         break;
968        case LDAP_REQ_COMPARE:
969         dissect_ldap_request_compare(&a, msg_tree);
970         break;
971        case LDAP_REQ_MODIFY:
972         dissect_ldap_request_modify(&a, msg_tree);
973         break;
974        case LDAP_REQ_ABANDON:
975         dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
976         break;
977        case LDAP_RES_BIND:
978         dissect_ldap_response_bind(&a, msg_tree);
979         break;
980        case LDAP_RES_SEARCH_ENTRY:
981         dissect_ldap_response_search_entry(&a, msg_tree);
982         break;
983        case LDAP_RES_SEARCH_RESULT:
984        case LDAP_RES_MODIFY:
985        case LDAP_RES_ADD:
986        case LDAP_RES_DELETE:
987        case LDAP_RES_MODRDN:
988        case LDAP_RES_COMPARE:
989         dissect_ldap_result(&a, msg_tree);
990         break;
991       }
992     }
993   }
994 }
995
996 void
997 proto_register_ldap(void)
998 {
999   static value_string result_codes[] = {
1000     {0, "Success"},
1001     {1, "Operations error"},
1002     {2, "Protocol error"},
1003     {3, "Time limit exceeded"},
1004     {4, "Size limit exceeded"},
1005     {5, "Compare false"},
1006     {6, "Compare true"},
1007     {7, "Authentication method not supported"},
1008     {8, "Strong authentication required"},
1009     {10, "Referral"},
1010     {11, "Administrative limit exceeded"},
1011     {12, "Unavailable critical extension"},
1012     {13, "Confidentiality required"},
1013     {14, "SASL bind in progress"},
1014     {16, "No such attribute"},
1015     {17, "Undefined attribute type"},
1016     {18, "Inappropriate matching"},
1017     {19, "Constraint violation"},
1018     {20, "Attribute or value exists"},
1019     {21, "Invalid attribute syntax"},
1020     {32, "No such object"},
1021     {33, "Alias problem"},
1022     {34, "Invalid DN syntax"},
1023     {36, "Alias derefetencing problem"},
1024     {48, "Inappropriate authentication"},
1025     {49, "Invalid credentials"},
1026     {50, "Insufficient access rights"},
1027     {51, "Busy"},
1028     {52, "Unavailable"},
1029     {53, "Unwilling to perform"},
1030     {54, "Loop detected"},
1031     {64, "Naming violation"},
1032     {65, "Objectclass violation"},
1033     {66, "Not allowed on non-leaf"},
1034     {67, "Not allowed on RDN"},
1035     {68, "Entry already exists"},
1036     {69, "Objectclass modification prohibited"},
1037     {71, "Affects multiple DSAs"},
1038     {80, "Other"},
1039     {0,  NULL},
1040   };
1041
1042   static value_string auth_types[] = {
1043     {LDAP_AUTH_SIMPLE,    "Simple"},
1044     {LDAP_AUTH_KRBV4LDAP, "Kerberos V4 to the LDAP server"},
1045     {LDAP_AUTH_KRBV4DSA,  "Kerberos V4 to the DSA"},
1046     {LDAP_AUTH_SASL,      "SASL"},
1047     {0, NULL},
1048   };
1049   
1050   static value_string search_scope[] = {
1051     {0x00, "Base"},
1052     {0x01, "Single"},
1053     {0x02, "Subtree"},
1054     {0x00, NULL},
1055   };
1056     
1057   static value_string search_dereference[] = {
1058     {0x00, "Never"},
1059     {0x01, "Searching"},
1060     {0x02, "Base Object"},
1061     {0x03, "Always"},
1062     {0x00, NULL},
1063   };
1064   
1065   static hf_register_info hf[] = {
1066     { &hf_ldap_length,
1067       { "Length",               "ldap.length",
1068         FT_UINT32, BASE_DEC, NULL, 0x0,
1069         "LDAP Length" }},
1070           
1071     { &hf_ldap_message_id,
1072       { "Message Id",           "ldap.message_id",
1073         FT_UINT32, BASE_DEC, NULL, 0x0,
1074         "LDAP Message Id" }},
1075     { &hf_ldap_message_type,
1076       { "Message Type",         "ldap.message_type",
1077         FT_UINT8, BASE_HEX, &msgTypes, 0x0,
1078         "LDAP Message Type" }},
1079     { &hf_ldap_message_length,
1080       { "Message Length",               "ldap.message_length",
1081         FT_UINT32, BASE_DEC, NULL, 0x0,
1082         "LDAP Message Length" }},
1083
1084     { &hf_ldap_message_result,
1085       { "Result Code",          "ldap.result.code",
1086         FT_UINT8, BASE_HEX, result_codes, 0x0,
1087         "LDAP Result Code" }},
1088     { &hf_ldap_message_result_matcheddn,
1089       { "Matched DN",           "ldap.result.matcheddn",
1090         FT_STRING, BASE_NONE, NULL, 0x0,
1091         "LDAP Result Matched DN" }},
1092     { &hf_ldap_message_result_errormsg,
1093       { "Error Message",                "ldap.result.errormsg",
1094         FT_STRING, BASE_NONE, NULL, 0x0,
1095         "LDAP Result Error Message" }},
1096     { &hf_ldap_message_result_referral,
1097       { "Referral",             "ldap.result.referral",
1098         FT_STRING, BASE_NONE, NULL, 0x0,
1099         "LDAP Result Referral URL" }},
1100
1101     { &hf_ldap_message_bind_version,
1102       { "Version",              "ldap.bind.version",
1103         FT_UINT32, BASE_DEC, NULL, 0x0,
1104         "LDAP Bind Version" }},
1105     { &hf_ldap_message_bind_dn,
1106       { "DN",                   "ldap.bind.dn",
1107         FT_STRING, BASE_NONE, NULL, 0x0,
1108         "LDAP Bind Distinguished Name" }},
1109     { &hf_ldap_message_bind_auth,
1110       { "Auth Type",            "ldap.bind.auth_type",
1111         FT_UINT8, BASE_HEX, auth_types, 0x0,
1112         "LDAP Bind Auth Type" }},
1113     { &hf_ldap_message_bind_auth_password,
1114       { "Password",             "ldap.bind.password",
1115         FT_STRING, BASE_NONE, NULL, 0x0,
1116         "LDAP Bind Password" }},
1117
1118     { &hf_ldap_message_search_base,
1119       { "Base DN",              "ldap.search.basedn",
1120         FT_STRING, BASE_NONE, NULL, 0x0,
1121         "LDAP Search Base Distinguished Name" }},
1122     { &hf_ldap_message_search_scope,
1123       { "Scope",                        "ldap.search.scope",
1124         FT_UINT8, BASE_HEX, search_scope, 0x0,
1125         "LDAP Search Scope" }},
1126     { &hf_ldap_message_search_deref,
1127       { "Dereference",          "ldap.search.dereference",
1128         FT_UINT8, BASE_HEX, search_dereference, 0x0,
1129         "LDAP Search Dereference" }},
1130     { &hf_ldap_message_search_sizeLimit,
1131       { "Size Limit",           "ldap.search.sizelimit",
1132         FT_UINT32, BASE_DEC, NULL, 0x0,
1133         "LDAP Search Size Limit" }},
1134     { &hf_ldap_message_search_timeLimit,
1135       { "Time Limit",           "ldap.search.timelimit",
1136         FT_UINT32, BASE_DEC, NULL, 0x0,
1137         "LDAP Search Time Limit" }},
1138     { &hf_ldap_message_search_typesOnly,
1139       { "Attributes Only",      "ldap.search.typesonly",
1140         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1141         "LDAP Search Attributes Only" }},
1142     { &hf_ldap_message_search_filter,
1143       { "Filter",               "ldap.search.filter",
1144         FT_STRING, BASE_NONE, NULL, 0x0,
1145         "LDAP Search Filter" }},
1146     { &hf_ldap_message_dn,
1147       { "Distinguished Name",   "ldap.dn",
1148         FT_STRING, BASE_NONE, NULL, 0x0,
1149         "LDAP Distinguished Name" }},
1150     { &hf_ldap_message_attribute,
1151       { "Attribute",            "ldap.attribute",
1152         FT_STRING, BASE_NONE, NULL, 0x0,
1153         "LDAP Attribute" }},
1154     { &hf_ldap_message_value,
1155       { "Value",                "ldap.value",
1156         FT_STRING, BASE_NONE, NULL, 0x0,
1157         "LDAP Value" }},
1158
1159     { &hf_ldap_message_modrdn_name,
1160       { "New Name",             "ldap.modrdn.name",
1161         FT_STRING, BASE_NONE, NULL, 0x0,
1162         "LDAP New Name" }},
1163     { &hf_ldap_message_modrdn_delete,
1164       { "Delete Values",        "ldap.modrdn.delete",
1165         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1166         "LDAP Modify RDN - Delete original values" }},
1167     { &hf_ldap_message_modrdn_superior,
1168       { "New Location",         "ldap.modrdn.superior",
1169         FT_STRING, BASE_NONE, NULL, 0x0,
1170         "LDAP Modify RDN - New Location" }},
1171
1172     { &hf_ldap_message_compare,
1173       { "Test",         "ldap.compare.test",
1174         FT_STRING, BASE_NONE, NULL, 0x0,
1175         "LDAP Compare Test" }},
1176
1177     { &hf_ldap_message_modify_add,
1178       { "Add",                  "ldap.modify.add",
1179         FT_STRING, BASE_NONE, NULL, 0x0,
1180         "LDAP Add" }},
1181     { &hf_ldap_message_modify_replace,
1182       { "Replace",              "ldap.modify.replace",
1183         FT_STRING, BASE_NONE, NULL, 0x0,
1184         "LDAP Replace" }},
1185     { &hf_ldap_message_modify_delete,
1186       { "Delete",               "ldap.modify.delete",
1187         FT_STRING, BASE_NONE, NULL, 0x0,
1188         "LDAP Delete" }},
1189
1190     { &hf_ldap_message_abandon_msgid,
1191       { "Abandon Msg Id",       "ldap.abandon.msgid",
1192         FT_UINT32, BASE_DEC, NULL, 0x0,
1193         "LDAP Abandon Msg Id" }},
1194   };
1195
1196   static gint *ett[] = {
1197     &ett_ldap,
1198     &ett_ldap_message,
1199     &ett_ldap_referrals,
1200     &ett_ldap_attribute
1201   };
1202
1203   proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol",
1204                                        "LDAP", "ldap");
1205   proto_register_field_array(proto_ldap, hf, array_length(hf));
1206   proto_register_subtree_array(ett, array_length(ett));
1207 }
1208
1209 void
1210 proto_reg_handoff_ldap(void)
1211 {
1212   old_dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap);
1213 }