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