d2fb2049f0543701fba60b67fa481d41cb6be8a8
[gd/wireshark/.git] / asn1 / dap / packet-dap-template.c
1 /* packet-dap.c
2  * Routines for X.511 (X.500 Directory Asbtract Service) and X.519 DAP  packet dissection
3  * Graeme Lunt 2005
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/conversation.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "packet-ber.h"
39 #include "packet-acse.h"
40 #include "packet-ros.h"
41
42 #include "packet-x509if.h"
43 #include "packet-x509af.h"
44 #include "packet-x509sat.h"
45 #include "packet-crmf.h"
46
47 #include "packet-dsp.h"
48 #include "packet-disp.h"
49 #include "packet-dap.h"
50 #include <epan/strutil.h>
51
52 /* we don't have a separate dissector for X519 - 
53    most of DAP is defined in X511 */
54 #define PNAME  "X.519 Directory Access Protocol"
55 #define PSNAME "DAP"
56 #define PFNAME "dap"
57
58 static guint global_dap_tcp_port = 102;
59 static guint tcp_port = 0;
60 static dissector_handle_t tpkt_handle = NULL;
61 void prefs_register_dap(void); /* forwad declaration for use in preferences registration */
62
63
64 /* Initialize the protocol and registered fields */
65 int proto_dap = -1;
66
67 static struct SESSION_DATA_STRUCTURE* session = NULL;
68
69 #include "packet-dap-hf.c"
70
71 /* Initialize the subtree pointers */
72 static gint ett_dap = -1;
73 #include "packet-dap-ett.c"
74
75 #include "packet-dap-fn.c"
76
77 /*
78 * Dissect DAP PDUs inside a ROS PDUs
79 */
80 static void
81 dissect_dap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
82 {
83         int offset = 0;
84         int old_offset;
85         proto_item *item=NULL;
86         proto_tree *tree=NULL;
87         int (*dap_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index _U_) = NULL;
88         char *dap_op_name;
89
90         /* do we have operation information from the ROS dissector?  */
91         if( !pinfo->private_data ){
92                 if(parent_tree){
93                         proto_tree_add_text(parent_tree, tvb, offset, -1,
94                                 "Internal error: can't get operation information from ROS dissector.");
95                 } 
96                 return  ;
97         } else {
98                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
99         }
100
101         if(parent_tree){
102                 item = proto_tree_add_item(parent_tree, proto_dap, tvb, 0, -1, FALSE);
103                 tree = proto_item_add_subtree(item, ett_dap);
104         }
105         if (check_col(pinfo->cinfo, COL_PROTOCOL))
106                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "DAP");
107         if (check_col(pinfo->cinfo, COL_INFO))
108                 col_clear(pinfo->cinfo, COL_INFO);
109
110         switch(session->ros_op & ROS_OP_MASK) {
111         case (ROS_OP_BIND | ROS_OP_ARGUMENT):   /*  BindInvoke */
112           dap_dissector = dissect_dap_DirectoryBindArgument;
113           dap_op_name = "Bind-Argument";
114           break;
115         case (ROS_OP_BIND | ROS_OP_RESULT):     /*  BindResult */
116           dap_dissector = dissect_dap_DirectoryBindResult;
117           dap_op_name = "Bind-Result";
118           break;
119         case (ROS_OP_BIND | ROS_OP_ERROR):      /*  BindError */
120           dap_dissector = dissect_dap_DirectoryBindError;
121           dap_op_name = "Bind-Error";
122           break;
123         case (ROS_OP_INVOKE | ROS_OP_ARGUMENT): /*  Invoke Argument */
124           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
125           case 1: /* read */
126             dap_dissector = dissect_dap_ReadArgument;
127             dap_op_name = "Read-Argument";
128             break;
129           case 2: /* compare */
130             dap_dissector = dissect_dap_CompareArgument;
131             dap_op_name = "Compare-Argument";
132             break;
133           case 3: /* abandon */
134             dap_dissector = dissect_dap_AbandonArgument;
135             dap_op_name = "Abandon-Argument";
136             break;
137           case 4: /* list */
138             dap_dissector = dissect_dap_ListArgument;
139             dap_op_name = "List-Argument";
140             break;
141           case 5: /* search */
142             dap_dissector = dissect_dap_SearchArgument;
143             dap_op_name = "Search-Argument";
144             break;
145           case 6: /* addEntry */
146             dap_dissector = dissect_dap_AddEntryArgument;
147             dap_op_name = "Add-Entry-Argument";
148             break;
149           case 7: /* removeEntry */
150             dap_dissector = dissect_dap_RemoveEntryArgument;
151             dap_op_name = "Remove-Entry-Argument";
152             break;
153           case 8: /* modifyEntry */
154             dap_dissector = dissect_dap_ModifyEntryArgument;
155             dap_op_name = "Modify-Entry-Argument";
156             break;
157           case 9: /* modifyDN */
158             dap_dissector = dissect_dap_ModifyDNArgument;
159             dap_op_name = "Modify-DN-Argument";
160             break;
161           default:
162             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DAP opcode (%d)",
163                                 session->ros_op & ROS_OP_OPCODE_MASK);
164             break;
165           }
166           break;
167         case (ROS_OP_INVOKE | ROS_OP_RESULT):   /*  Return Result */
168           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
169           case 1: /* read */
170             dap_dissector = dissect_dap_ReadResult;
171             dap_op_name = "Read-Result";
172             break;
173           case 2: /* compare */
174             dap_dissector = dissect_dap_CompareResult;
175             dap_op_name = "Compare-Result";
176             break;
177           case 3: /* abandon */
178             dap_dissector = dissect_dap_AbandonResult;
179             dap_op_name = "Abandon-Result";
180             break;
181           case 4: /* list */
182             dap_dissector = dissect_dap_ListResult;
183             dap_op_name = "List-Result";
184             break;
185           case 5: /* search */
186             dap_dissector = dissect_dap_SearchResult;
187             dap_op_name = "Search-Result";
188             break;
189           case 6: /* addEntry */
190             dap_dissector = dissect_dap_AddEntryResult;
191             dap_op_name = "Add-Entry-Result";
192             break;
193           case 7: /* removeEntry */
194             dap_dissector = dissect_dap_RemoveEntryResult;
195             dap_op_name = "Remove-Entry-Result";
196             break;
197           case 8: /* modifyEntry */
198             dap_dissector = dissect_dap_ModifyEntryResult;
199             dap_op_name = "Modify-Entry-Result";
200             break;
201           case 9: /* modifyDN */
202             dap_dissector = dissect_dap_ModifyDNResult;
203             dap_op_name = "Modify-DN-Result";
204             break;
205           default:
206             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DAP opcode");
207             break;
208           }
209           break;
210         case (ROS_OP_INVOKE | ROS_OP_ERROR):    /*  Return Error */
211           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
212           case 1: /* attributeError */
213             dap_dissector = dissect_dap_AttributeError;
214             dap_op_name = "Attribute-Error";
215             break;
216           case 2: /* nameError */
217             dap_dissector = dissect_dap_NameError;
218             dap_op_name = "Name-Error";
219             break;
220           case 3: /* serviceError */
221             dap_dissector = dissect_dap_ServiceError;
222             dap_op_name = "Service-Error";
223             break;
224           case 4: /* referral */
225             dap_dissector = dissect_dap_Referral;
226             dap_op_name = "Referral";
227             break;
228           case 5: /* abandoned */
229             dap_dissector = dissect_dap_Abandoned;
230             dap_op_name = "Abandoned";
231             break;
232           case 6: /* securityError */
233             dap_dissector = dissect_dap_SecurityError;
234             dap_op_name = "Security-Error";
235             break;
236           case 7: /* abandonFailed */
237             dap_dissector = dissect_dap_AbandonFailedError;
238             dap_op_name = "Abandon-Failed-Error";
239             break;
240           case 8: /* updateError */
241             dap_dissector = dissect_dap_UpdateError;
242             dap_op_name = "Update-Error";
243             break;
244           default:
245             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DAP errcode");
246             break;
247           }
248           break;
249         default:
250           proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DAP PDU");
251           return;
252         }
253
254         if(dap_dissector) {
255           if (check_col(pinfo->cinfo, COL_INFO))
256             col_add_str(pinfo->cinfo, COL_INFO, dap_op_name);
257
258           while (tvb_reported_length_remaining(tvb, offset) > 0){
259             old_offset=offset;
260             offset=(*dap_dissector)(FALSE, tvb, offset, pinfo , tree, -1);
261             if(offset == old_offset){
262               proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte DAP PDU");
263               offset = tvb_length(tvb);
264               break;
265             }
266           }
267         }
268 }
269
270
271 /*--- proto_register_dap -------------------------------------------*/
272 void proto_register_dap(void) {
273
274   /* List of fields */
275   static hf_register_info hf[] =
276   {
277 #include "packet-dap-hfarr.c"
278   };
279
280   /* List of subtrees */
281   static gint *ett[] = {
282     &ett_dap,
283 #include "packet-dap-ettarr.c"
284   };
285   module_t *dap_module;
286
287   /* Register protocol */
288   proto_dap = proto_register_protocol(PNAME, PSNAME, PFNAME);
289   register_dissector("dap", dissect_dap, proto_dap);
290
291   /* Register fields and subtrees */
292   proto_register_field_array(proto_dap, hf, array_length(hf));
293   proto_register_subtree_array(ett, array_length(ett));
294
295   /* Register our configuration options for DAP, particularly our port */
296
297 #ifdef PREFERENCE_GROUPING
298   dap_module = prefs_register_protocol_subtree("OSI/X.500", proto_dap, prefs_register_dap);
299 #else
300   dap_module = prefs_register_protocol(proto_dap, prefs_register_dap);
301 #endif
302
303   prefs_register_uint_preference(dap_module, "tcp.port", "DAP TCP Port",
304                                  "Set the port for DAP operations (if other"
305                                  " than the default of 102)",
306                                  10, &global_dap_tcp_port);
307
308 }
309
310
311 /*--- proto_reg_handoff_dap --- */
312 void proto_reg_handoff_dap(void) {
313   dissector_handle_t handle = NULL;
314
315   /* #include "packet-dap-dis-tab.c" */
316
317   /* APPLICATION CONTEXT */
318
319   register_ber_oid_name("2.5.3.1", "id-ac-directory-access");
320
321   /* ABSTRACT SYNTAXES */
322     
323   /* Register DAP with ROS (with no use of RTSE) */
324   if((handle = find_dissector("dap"))) {
325     register_ros_oid_dissector_handle("2.5.9.1", handle, 0, "id-as-directory-access", FALSE); 
326   }
327
328   /* remember the tpkt handler for change in preferences */
329   tpkt_handle = find_dissector("tpkt");
330
331   /* AttributeValueAssertions */
332   x509if_register_fmt(hf_dap_equality, "=");
333   x509if_register_fmt(hf_dap_greaterOrEqual, ">=");
334   x509if_register_fmt(hf_dap_lessOrEqual, "<=");
335   x509if_register_fmt(hf_dap_approximateMatch, "=~");
336   /* AttributeTypes */
337   x509if_register_fmt(hf_dap_present, "= *");
338
339
340 }
341
342
343 void prefs_register_dap(void) {
344
345   /* de-register the old port */
346   /* port 102 is registered by TPKT - don't undo this! */
347   if((tcp_port != 102) && tpkt_handle)
348     dissector_delete("tcp.port", tcp_port, tpkt_handle);
349
350   /* Set our port number for future use */
351   tcp_port = global_dap_tcp_port;
352
353   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
354     dissector_add("tcp.port", global_dap_tcp_port, tpkt_handle);
355
356 }