#include <stdio.h> not needed.
[obnox/wireshark/wip.git] / asn1 / dop / packet-dop-template.c
1 /* packet-dop.c
2  * Routines for X.501 (DSA Operational Attributes)  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/oids.h>
34 #include <epan/asn1.h>
35 #include <epan/expert.h>
36
37 #include <string.h>
38
39 #include "packet-ber.h"
40 #include "packet-acse.h"
41 #include "packet-ros.h"
42
43 #include "packet-x509sat.h"
44 #include "packet-x509af.h"
45 #include "packet-x509if.h"
46 #include "packet-dap.h"
47 #include "packet-dsp.h"
48 #include "packet-crmf.h"
49
50
51 #include "packet-dop.h"
52
53 #define PNAME  "X.501 Directory Operational Binding Management Protocol"
54 #define PSNAME "DOP"
55 #define PFNAME "dop"
56
57 static guint global_dop_tcp_port = 102;
58 static dissector_handle_t tpkt_handle;
59 void prefs_register_dop(void); /* forward declaration for use in preferences registration */
60
61 /* Initialize the protocol and registered fields */
62 static int proto_dop = -1;
63
64 static struct SESSION_DATA_STRUCTURE* session = NULL;
65 static const char *binding_type = NULL; /* binding_type */
66
67 static int call_dop_oid_callback(char *base_string, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, char *col_info);
68
69 #include "packet-dop-hf.c"
70
71 /* Initialize the subtree pointers */
72 static gint ett_dop = -1;
73 static gint ett_dop_unknown = -1;
74 #include "packet-dop-ett.c"
75
76 /* Dissector table */
77 static dissector_table_t dop_dissector_table;
78
79 static void append_oid(packet_info *pinfo, const char *oid)
80 {
81         const char *name = NULL;
82
83     name = oid_resolved_from_string(oid);
84     col_append_fstr(pinfo->cinfo, COL_INFO, " %s", name ? name : oid);
85 }
86
87 #include "packet-dop-fn.c"
88
89 static int
90 call_dop_oid_callback(char *base_string, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, char *col_info)
91 {
92   char* binding_param;
93
94   binding_param = ep_strdup_printf("%s.%s", base_string, binding_type ? binding_type : "");
95
96   col_append_fstr(pinfo->cinfo, COL_INFO, " %s", col_info);
97
98   if (dissector_try_string(dop_dissector_table, binding_param, tvb, pinfo, tree)) {
99      offset += tvb_length_remaining (tvb, offset);
100   } else {
101      proto_item *item=NULL;
102      proto_tree *next_tree=NULL;
103
104      item = proto_tree_add_text(tree, tvb, 0, tvb_length_remaining(tvb, offset), "Dissector for parameter %s OID:%s not implemented. Contact Wireshark developers if you want this supported", base_string, binding_type ? binding_type : "<empty>");
105      if (item) {
106         next_tree = proto_item_add_subtree(item, ett_dop_unknown);
107      }
108      offset = dissect_unknown_ber(pinfo, tvb, offset, next_tree);
109      expert_add_info_format(pinfo, item, PI_UNDECODED, PI_WARN, "Unknown binding-parameter");
110    }
111
112    return offset;
113 }
114
115
116 /*
117 * Dissect DOP PDUs inside a ROS PDUs
118 */
119 static void
120 dissect_dop(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
121 {
122         int offset = 0;
123         int old_offset;
124         proto_item *item=NULL;
125         proto_tree *tree=NULL;
126         int (*dop_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_) = NULL;
127         char *dop_op_name;
128         asn1_ctx_t asn1_ctx;
129
130         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
131
132         /* do we have operation information from the ROS dissector?  */
133         if( !pinfo->private_data ){
134                 if(parent_tree){
135                         proto_tree_add_text(parent_tree, tvb, offset, -1,
136                                 "Internal error: can't get operation information from ROS dissector.");
137                 } 
138                 return  ;
139         } else {
140                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
141         }
142
143         if(parent_tree){
144                 item = proto_tree_add_item(parent_tree, proto_dop, tvb, 0, -1, FALSE);
145                 tree = proto_item_add_subtree(item, ett_dop);
146         }
147         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DOP");
148         col_clear(pinfo->cinfo, COL_INFO);
149
150         switch(session->ros_op & ROS_OP_MASK) {
151         case (ROS_OP_BIND | ROS_OP_ARGUMENT):   /*  BindInvoke */
152           dop_dissector = dissect_dop_DSAOperationalManagementBindArgument;
153           dop_op_name = "DSA-Operational-Bind-Argument";
154           break;
155         case (ROS_OP_BIND | ROS_OP_RESULT):     /*  BindResult */
156           dop_dissector = dissect_dop_DSAOperationalManagementBindResult;
157           dop_op_name = "DSA-Operational-Bind-Result";
158           break;
159         case (ROS_OP_BIND | ROS_OP_ERROR):      /*  BindError */
160           dop_dissector = dissect_dop_DSAOperationalManagementBindError;
161           dop_op_name = "DSA-Operational-Management-Bind-Error";
162           break;
163         case (ROS_OP_INVOKE | ROS_OP_ARGUMENT): /*  Invoke Argument */
164           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
165           case 100: /* establish */
166             dop_dissector = dissect_dop_EstablishOperationalBindingArgument;
167             dop_op_name = "Establish-Operational-Binding-Argument";
168             break;
169           case 101: /* terminate */
170             dop_dissector = dissect_dop_TerminateOperationalBindingArgument;
171             dop_op_name = "Terminate-Operational-Binding-Argument";
172             break;
173           case 102: /* modify */
174             dop_dissector = dissect_dop_ModifyOperationalBindingArgument;
175             dop_op_name = "Modify-Operational-Binding-Argument";
176             break;
177           default:
178             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DOP Argument opcode (%d)",
179                                 session->ros_op & ROS_OP_OPCODE_MASK);
180             break;
181           }
182           break;
183         case (ROS_OP_INVOKE | ROS_OP_RESULT):   /*  Return Result */
184           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
185           case 100: /* establish */
186             dop_dissector = dissect_dop_EstablishOperationalBindingResult;
187             dop_op_name = "Establish-Operational-Binding-Result";
188             break;
189           case 101: /* terminate */
190             dop_dissector = dissect_dop_TerminateOperationalBindingResult;
191             dop_op_name = "Terminate-Operational-Binding-Result";
192             break;
193           case 102: /* modify */
194             dop_dissector = dissect_dop_ModifyOperationalBindingResult;
195             dop_op_name = "Modify-Operational-Binding-Result";
196             break;
197           default:
198             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DOP Result opcode (%d)",
199                                 session->ros_op & ROS_OP_OPCODE_MASK);
200             break;
201           }
202           break;
203         case (ROS_OP_INVOKE | ROS_OP_ERROR):    /*  Return Error */
204           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
205           case 100: /* operational-binding */
206             dop_dissector = dissect_dop_OpBindingErrorParam;
207             dop_op_name = "Operational-Binding-Error";
208             break;
209           default:
210             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DOP Error opcode (%d)",
211                                 session->ros_op & ROS_OP_OPCODE_MASK);
212             break;
213           }
214           break;
215         default:
216           proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DOP PDU");
217           return;
218         }
219
220         if(dop_dissector) {
221       col_set_str(pinfo->cinfo, COL_INFO, dop_op_name);
222
223           while (tvb_reported_length_remaining(tvb, offset) > 0){
224             old_offset=offset;
225             offset=(*dop_dissector)(FALSE, tvb, offset, &asn1_ctx, tree, -1);
226             if(offset == old_offset){
227               proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte DOP PDU");
228               break;
229             }
230           }
231         }
232 }
233
234
235
236 /*--- proto_register_dop -------------------------------------------*/
237 void proto_register_dop(void) {
238
239   /* List of fields */
240   static hf_register_info hf[] =
241   {
242 #include "packet-dop-hfarr.c"
243   };
244
245   /* List of subtrees */
246   static gint *ett[] = {
247     &ett_dop,
248     &ett_dop_unknown,
249 #include "packet-dop-ettarr.c"
250   };
251
252   module_t *dop_module;
253
254   /* Register protocol */
255   proto_dop = proto_register_protocol(PNAME, PSNAME, PFNAME);
256
257   register_dissector("dop", dissect_dop, proto_dop);
258
259   dop_dissector_table = register_dissector_table("dop.oid", "DOP OID Dissectors", FT_STRING, BASE_NONE);
260
261   /* Register fields and subtrees */
262   proto_register_field_array(proto_dop, hf, array_length(hf));
263   proto_register_subtree_array(ett, array_length(ett));
264
265   /* Register our configuration options for DOP, particularly our port */
266
267   dop_module = prefs_register_protocol_subtree("OSI/X.500", proto_dop, prefs_register_dop);
268
269   prefs_register_uint_preference(dop_module, "tcp.port", "DOP TCP Port",
270                                  "Set the port for DOP operations (if other"
271                                  " than the default of 102)",
272                                  10, &global_dop_tcp_port);
273
274
275 }
276
277
278 /*--- proto_reg_handoff_dop --- */
279 void proto_reg_handoff_dop(void) {
280   dissector_handle_t dop_handle;
281
282 #include "packet-dop-dis-tab.c" 
283   /* APPLICATION CONTEXT */
284
285   oid_add_from_string("id-ac-directory-operational-binding-management","2.5.3.3");
286
287   /* ABSTRACT SYNTAXES */
288     
289   /* Register DOP with ROS (with no use of RTSE) */
290   dop_handle = find_dissector("dop");
291   register_ros_oid_dissector_handle("2.5.9.4", dop_handle, 0, "id-as-directory-operational-binding-management", FALSE); 
292
293   /* BINDING TYPES */
294
295   oid_add_from_string("shadow-agreement","2.5.19.1");
296   oid_add_from_string("hierarchical-agreement","2.5.19.2");
297   oid_add_from_string("non-specific-hierarchical-agreement","2.5.19.3");
298
299   /* ACCESS CONTROL SCHEMES */
300   oid_add_from_string("basic-ACS","2.5.28.1");
301   oid_add_from_string("simplified-ACS","2.5.28.2");
302   oid_add_from_string("ruleBased-ACS","2.5.28.3");
303   oid_add_from_string("ruleAndBasic-ACS","2.5.28.4");
304   oid_add_from_string("ruleAndSimple-ACS","2.5.28.5");
305
306   /* ADMINISTRATIVE ROLES */
307   oid_add_from_string("id-ar-autonomousArea","2.5.23.1");
308   oid_add_from_string("id-ar-accessControlSpecificArea","2.5.23.2");
309   oid_add_from_string("id-ar-accessControlInnerArea","2.5.23.3");
310   oid_add_from_string("id-ar-subschemaAdminSpecificArea","2.5.23.4");
311   oid_add_from_string("id-ar-collectiveAttributeSpecificArea","2.5.23.5");
312   oid_add_from_string("id-ar-collectiveAttributeInnerArea","2.5.23.6");
313   oid_add_from_string("id-ar-contextDefaultSpecificArea","2.5.23.7");
314   oid_add_from_string("id-ar-serviceSpecificArea","2.5.23.8");
315
316   /* remember the tpkt handler for change in preferences */
317   tpkt_handle = find_dissector("tpkt");
318
319 }
320
321 void prefs_register_dop(void) {
322   static guint tcp_port = 0;
323
324   /* de-register the old port */
325   /* port 102 is registered by TPKT - don't undo this! */
326   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
327     dissector_delete("tcp.port", tcp_port, tpkt_handle);
328
329   /* Set our port number for future use */
330   tcp_port = global_dop_tcp_port;
331
332   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
333     dissector_add("tcp.port", tcp_port, tpkt_handle);
334
335 }