Added two new arguments to epan_init() and proto_init() to
[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.22 2001/01/10 23:42:12 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   {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,
911                             "Short message! (expected: %u, actual: %lu)",
912                             messageLength, (unsigned long)(a.end - a.pointer));
913       break;
914     }
915   
916     message_id_start = a.pointer - a.begin;
917     read_integer(&a, 0, -1, 0, &messageId, ASN1_INT);
918     message_id_length = (a.pointer - a.begin) - message_id_start;
919
920     start = a.pointer;
921     asn1_id_decode(&a, &protocolOpCls, &protocolOpCon, &protocolOpTag);
922     if (protocolOpCls != ASN1_APL)
923       typestr = "Bad message type (not Application)";
924     else
925       typestr = val_to_str(protocolOpTag, msgTypes, "Bad message type (%u)");
926
927     if (first_time)
928     {
929       if (check_col(fd, COL_PROTOCOL))
930         col_set_str(fd, COL_PROTOCOL, "LDAP");
931
932       if (check_col(fd, COL_INFO))
933         col_add_fstr(fd, COL_INFO, "MsgId=%u MsgType=%s",
934                      messageId, typestr);
935       first_time = 0;
936       if (!tree)
937         return;
938     }
939
940     if (ldap_tree) 
941     {
942       proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_id, NullTVB, message_id_start, message_id_length, messageId);
943       proto_tree_add_uint_hidden(ldap_tree, hf_ldap_message_type, NullTVB,
944                                  start - a.begin, a.pointer - start, protocolOpTag);
945       ti = proto_tree_add_text(ldap_tree, NullTVB, message_id_start, messageLength, "Message: Id=%u  %s", messageId, typestr);
946       msg_tree = proto_item_add_subtree(ti, ett_ldap_message);
947       start = a.pointer;
948       read_length(&a, msg_tree, hf_ldap_message_length, &opLen);
949
950       switch (protocolOpTag)
951       {
952        case LDAP_REQ_BIND:
953         dissect_ldap_request_bind(&a, msg_tree);
954         break;
955        case LDAP_REQ_SEARCH:
956         ret = dissect_ldap_request_search(&a, msg_tree);
957         if (ret != ASN1_ERR_NOERROR)
958           break;
959         break;
960        case LDAP_REQ_ADD:
961         dissect_ldap_request_add(&a, msg_tree);
962         break;
963        case LDAP_REQ_DELETE:
964         dissect_ldap_request_delete(&a, msg_tree, start, opLen);
965         break;
966        case LDAP_REQ_MODRDN:
967         dissect_ldap_request_modifyrdn(&a, msg_tree, opLen);
968         break;
969        case LDAP_REQ_COMPARE:
970         dissect_ldap_request_compare(&a, msg_tree);
971         break;
972        case LDAP_REQ_MODIFY:
973         dissect_ldap_request_modify(&a, msg_tree);
974         break;
975        case LDAP_REQ_ABANDON:
976         dissect_ldap_request_abandon(&a, msg_tree, start, opLen);
977         break;
978        case LDAP_RES_BIND:
979         dissect_ldap_response_bind(&a, msg_tree);
980         break;
981        case LDAP_RES_SEARCH_ENTRY:
982         dissect_ldap_response_search_entry(&a, msg_tree);
983         break;
984        case LDAP_RES_SEARCH_RESULT:
985        case LDAP_RES_MODIFY:
986        case LDAP_RES_ADD:
987        case LDAP_RES_DELETE:
988        case LDAP_RES_MODRDN:
989        case LDAP_RES_COMPARE:
990         dissect_ldap_result(&a, msg_tree);
991         break;
992       }
993     }
994   }
995 }
996
997 void
998 proto_register_ldap(void)
999 {
1000   static value_string result_codes[] = {
1001     {0, "Success"},
1002     {1, "Operations error"},
1003     {2, "Protocol error"},
1004     {3, "Time limit exceeded"},
1005     {4, "Size limit exceeded"},
1006     {5, "Compare false"},
1007     {6, "Compare true"},
1008     {7, "Authentication method not supported"},
1009     {8, "Strong authentication required"},
1010     {10, "Referral"},
1011     {11, "Administrative limit exceeded"},
1012     {12, "Unavailable critical extension"},
1013     {13, "Confidentiality required"},
1014     {14, "SASL bind in progress"},
1015     {16, "No such attribute"},
1016     {17, "Undefined attribute type"},
1017     {18, "Inappropriate matching"},
1018     {19, "Constraint violation"},
1019     {20, "Attribute or value exists"},
1020     {21, "Invalid attribute syntax"},
1021     {32, "No such object"},
1022     {33, "Alias problem"},
1023     {34, "Invalid DN syntax"},
1024     {36, "Alias derefetencing problem"},
1025     {48, "Inappropriate authentication"},
1026     {49, "Invalid credentials"},
1027     {50, "Insufficient access rights"},
1028     {51, "Busy"},
1029     {52, "Unavailable"},
1030     {53, "Unwilling to perform"},
1031     {54, "Loop detected"},
1032     {64, "Naming violation"},
1033     {65, "Objectclass violation"},
1034     {66, "Not allowed on non-leaf"},
1035     {67, "Not allowed on RDN"},
1036     {68, "Entry already exists"},
1037     {69, "Objectclass modification prohibited"},
1038     {71, "Affects multiple DSAs"},
1039     {80, "Other"},
1040     {0,  NULL},
1041   };
1042
1043   static value_string auth_types[] = {
1044     {LDAP_AUTH_SIMPLE,    "Simple"},
1045     {LDAP_AUTH_KRBV4LDAP, "Kerberos V4 to the LDAP server"},
1046     {LDAP_AUTH_KRBV4DSA,  "Kerberos V4 to the DSA"},
1047     {LDAP_AUTH_SASL,      "SASL"},
1048     {0, NULL},
1049   };
1050   
1051   static value_string search_scope[] = {
1052     {0x00, "Base"},
1053     {0x01, "Single"},
1054     {0x02, "Subtree"},
1055     {0x00, NULL},
1056   };
1057     
1058   static value_string search_dereference[] = {
1059     {0x00, "Never"},
1060     {0x01, "Searching"},
1061     {0x02, "Base Object"},
1062     {0x03, "Always"},
1063     {0x00, NULL},
1064   };
1065   
1066   static hf_register_info hf[] = {
1067     { &hf_ldap_length,
1068       { "Length",               "ldap.length",
1069         FT_UINT32, BASE_DEC, NULL, 0x0,
1070         "LDAP Length" }},
1071           
1072     { &hf_ldap_message_id,
1073       { "Message Id",           "ldap.message_id",
1074         FT_UINT32, BASE_DEC, NULL, 0x0,
1075         "LDAP Message Id" }},
1076     { &hf_ldap_message_type,
1077       { "Message Type",         "ldap.message_type",
1078         FT_UINT8, BASE_HEX, &msgTypes, 0x0,
1079         "LDAP Message Type" }},
1080     { &hf_ldap_message_length,
1081       { "Message Length",               "ldap.message_length",
1082         FT_UINT32, BASE_DEC, NULL, 0x0,
1083         "LDAP Message Length" }},
1084
1085     { &hf_ldap_message_result,
1086       { "Result Code",          "ldap.result.code",
1087         FT_UINT8, BASE_HEX, result_codes, 0x0,
1088         "LDAP Result Code" }},
1089     { &hf_ldap_message_result_matcheddn,
1090       { "Matched DN",           "ldap.result.matcheddn",
1091         FT_STRING, BASE_NONE, NULL, 0x0,
1092         "LDAP Result Matched DN" }},
1093     { &hf_ldap_message_result_errormsg,
1094       { "Error Message",                "ldap.result.errormsg",
1095         FT_STRING, BASE_NONE, NULL, 0x0,
1096         "LDAP Result Error Message" }},
1097     { &hf_ldap_message_result_referral,
1098       { "Referral",             "ldap.result.referral",
1099         FT_STRING, BASE_NONE, NULL, 0x0,
1100         "LDAP Result Referral URL" }},
1101
1102     { &hf_ldap_message_bind_version,
1103       { "Version",              "ldap.bind.version",
1104         FT_UINT32, BASE_DEC, NULL, 0x0,
1105         "LDAP Bind Version" }},
1106     { &hf_ldap_message_bind_dn,
1107       { "DN",                   "ldap.bind.dn",
1108         FT_STRING, BASE_NONE, NULL, 0x0,
1109         "LDAP Bind Distinguished Name" }},
1110     { &hf_ldap_message_bind_auth,
1111       { "Auth Type",            "ldap.bind.auth_type",
1112         FT_UINT8, BASE_HEX, auth_types, 0x0,
1113         "LDAP Bind Auth Type" }},
1114     { &hf_ldap_message_bind_auth_password,
1115       { "Password",             "ldap.bind.password",
1116         FT_STRING, BASE_NONE, NULL, 0x0,
1117         "LDAP Bind Password" }},
1118
1119     { &hf_ldap_message_search_base,
1120       { "Base DN",              "ldap.search.basedn",
1121         FT_STRING, BASE_NONE, NULL, 0x0,
1122         "LDAP Search Base Distinguished Name" }},
1123     { &hf_ldap_message_search_scope,
1124       { "Scope",                        "ldap.search.scope",
1125         FT_UINT8, BASE_HEX, search_scope, 0x0,
1126         "LDAP Search Scope" }},
1127     { &hf_ldap_message_search_deref,
1128       { "Dereference",          "ldap.search.dereference",
1129         FT_UINT8, BASE_HEX, search_dereference, 0x0,
1130         "LDAP Search Dereference" }},
1131     { &hf_ldap_message_search_sizeLimit,
1132       { "Size Limit",           "ldap.search.sizelimit",
1133         FT_UINT32, BASE_DEC, NULL, 0x0,
1134         "LDAP Search Size Limit" }},
1135     { &hf_ldap_message_search_timeLimit,
1136       { "Time Limit",           "ldap.search.timelimit",
1137         FT_UINT32, BASE_DEC, NULL, 0x0,
1138         "LDAP Search Time Limit" }},
1139     { &hf_ldap_message_search_typesOnly,
1140       { "Attributes Only",      "ldap.search.typesonly",
1141         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1142         "LDAP Search Attributes Only" }},
1143     { &hf_ldap_message_search_filter,
1144       { "Filter",               "ldap.search.filter",
1145         FT_STRING, BASE_NONE, NULL, 0x0,
1146         "LDAP Search Filter" }},
1147     { &hf_ldap_message_dn,
1148       { "Distinguished Name",   "ldap.dn",
1149         FT_STRING, BASE_NONE, NULL, 0x0,
1150         "LDAP Distinguished Name" }},
1151     { &hf_ldap_message_attribute,
1152       { "Attribute",            "ldap.attribute",
1153         FT_STRING, BASE_NONE, NULL, 0x0,
1154         "LDAP Attribute" }},
1155     { &hf_ldap_message_value,
1156       { "Value",                "ldap.value",
1157         FT_STRING, BASE_NONE, NULL, 0x0,
1158         "LDAP Value" }},
1159
1160     { &hf_ldap_message_modrdn_name,
1161       { "New Name",             "ldap.modrdn.name",
1162         FT_STRING, BASE_NONE, NULL, 0x0,
1163         "LDAP New Name" }},
1164     { &hf_ldap_message_modrdn_delete,
1165       { "Delete Values",        "ldap.modrdn.delete",
1166         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1167         "LDAP Modify RDN - Delete original values" }},
1168     { &hf_ldap_message_modrdn_superior,
1169       { "New Location",         "ldap.modrdn.superior",
1170         FT_STRING, BASE_NONE, NULL, 0x0,
1171         "LDAP Modify RDN - New Location" }},
1172
1173     { &hf_ldap_message_compare,
1174       { "Test",         "ldap.compare.test",
1175         FT_STRING, BASE_NONE, NULL, 0x0,
1176         "LDAP Compare Test" }},
1177
1178     { &hf_ldap_message_modify_add,
1179       { "Add",                  "ldap.modify.add",
1180         FT_STRING, BASE_NONE, NULL, 0x0,
1181         "LDAP Add" }},
1182     { &hf_ldap_message_modify_replace,
1183       { "Replace",              "ldap.modify.replace",
1184         FT_STRING, BASE_NONE, NULL, 0x0,
1185         "LDAP Replace" }},
1186     { &hf_ldap_message_modify_delete,
1187       { "Delete",               "ldap.modify.delete",
1188         FT_STRING, BASE_NONE, NULL, 0x0,
1189         "LDAP Delete" }},
1190
1191     { &hf_ldap_message_abandon_msgid,
1192       { "Abandon Msg Id",       "ldap.abandon.msgid",
1193         FT_UINT32, BASE_DEC, NULL, 0x0,
1194         "LDAP Abandon Msg Id" }},
1195   };
1196
1197   static gint *ett[] = {
1198     &ett_ldap,
1199     &ett_ldap_message,
1200     &ett_ldap_referrals,
1201     &ett_ldap_attribute
1202   };
1203
1204   proto_ldap = proto_register_protocol("Lightweight Directory Access Protocol",
1205                                        "LDAP", "ldap");
1206   proto_register_field_array(proto_ldap, hf, array_length(hf));
1207   proto_register_subtree_array(ett, array_length(ett));
1208 }
1209
1210 void
1211 proto_reg_handoff_ldap(void)
1212 {
1213   old_dissector_add("tcp.port", TCP_PORT_LDAP, dissect_ldap, proto_ldap);
1214 }