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