Make sure the keysym list is present before allocating a buffer to hold
[obnox/wireshark/wip.git] / packet-smb-mailslot.c
1 /* packet-smb-mailslot.c
2  * Routines for SMB mailslot packet dissection
3  * Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
4  *
5  * $Id: packet-smb-mailslot.c,v 1.35 2003/11/19 03:53:33 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-pop.c
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #include <string.h>
29 #include "packet-smb-common.h"
30 #include "packet-smb-mailslot.h"
31 #include "packet-smb-browse.h"
32 #include "packet-smb-pipe.h"
33
34 static int proto_smb_msp = -1;
35 static int hf_opcode = -1;
36 static int hf_priority = -1;
37 static int hf_class = -1;
38 static int hf_size = -1;
39 static int hf_name = -1;
40
41 static int ett_smb_msp = -1;
42
43 static dissector_handle_t mailslot_browse_handle;
44 static dissector_handle_t mailslot_lanman_handle;
45 static dissector_handle_t netlogon_handle;
46 static dissector_handle_t data_handle;
47
48 #define MAILSLOT_UNKNOWN              0
49 #define MAILSLOT_BROWSE               1
50 #define MAILSLOT_LANMAN               2
51 #define MAILSLOT_NET                  3
52 #define MAILSLOT_TEMP_NETLOGON        4
53 #define MAILSLOT_MSSP                 5
54
55 static const value_string opcode_vals[] = {
56         {1,     "Write Mail Slot"},
57         {0,     NULL}
58 };
59
60 static const value_string class_vals[] = {
61         {1,     "Reliable"},
62         {2,     "Unreliable & Broadcast"},
63         {0,     NULL}
64 };
65
66 /* decode the SMB mail slot protocol
67    for requests
68      mailslot is the name of the mailslot, e.g. BROWSE
69      si->trans_subcmd is set to the symbolic constant matching the mailslot name.
70    for responses
71      mailslot is NULL
72      si->trans_subcmd gives us which mailslot this response refers to.
73 */
74
75 gboolean
76 dissect_mailslot_smb(tvbuff_t *mshdr_tvb, tvbuff_t *setup_tvb,
77                      tvbuff_t *tvb, const char *mailslot, packet_info *pinfo,
78                      proto_tree *parent_tree)
79 {
80         smb_info_t *smb_info;
81         smb_transact_info_t *tri;
82         int             trans_subcmd;
83         proto_tree      *tree = NULL;
84         proto_item      *item = NULL;
85         guint16         opcode;
86         int             offset = 0;
87         int             len;
88
89         if (!proto_is_protocol_enabled(find_protocol_by_id(proto_smb_msp))) {
90                 return FALSE;
91         }
92         pinfo->current_proto = "SMB Mailslot";
93
94         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
95                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMB Mailslot");
96         }
97
98         if ((tvb==NULL) || (tvb_reported_length(tvb)==0)) {
99                 /* Interim reply */
100                 col_set_str(pinfo->cinfo, COL_INFO, "Interim reply");
101                 return TRUE;
102         }
103
104         if (check_col(pinfo->cinfo, COL_INFO)) {
105                 col_clear(pinfo->cinfo, COL_INFO);
106         }
107
108         smb_info = pinfo->private_data;
109         if (smb_info->sip != NULL)
110                 tri = smb_info->sip->extra_info;
111         else
112                 tri = NULL;
113
114         /* check which mailslot this is about */
115         trans_subcmd=MAILSLOT_UNKNOWN;
116         if(smb_info->request){
117                 if(strncmp(mailslot,"BROWSE",6) == 0){
118                         trans_subcmd=MAILSLOT_BROWSE;
119                 } else if(strncmp(mailslot,"LANMAN",6) == 0){
120                         trans_subcmd=MAILSLOT_LANMAN;
121                 } else if(strncmp(mailslot,"NET",3) == 0){
122                         trans_subcmd=MAILSLOT_NET;
123                 } else if(strncmp(mailslot,"TEMP\\NETLOGON",13) == 0){
124                         trans_subcmd=MAILSLOT_TEMP_NETLOGON;
125                 } else if(strncmp(mailslot,"MSSP",4) == 0){
126                         trans_subcmd=MAILSLOT_MSSP;
127                 }
128                 if (!pinfo->fd->flags.visited) {
129                         if (tri != NULL)
130                                 tri->trans_subcmd = trans_subcmd;
131                 }
132         } else {
133                 trans_subcmd = tri->trans_subcmd;
134         }
135
136         /* Only do these ones if we have them. For fragmented SMB Transactions
137            we may only have the setup area for the first fragment
138         */
139         if(mshdr_tvb && setup_tvb){
140                 if (parent_tree) {
141                         item = proto_tree_add_item(parent_tree, proto_smb_msp,
142                                                    mshdr_tvb, 0, -1, FALSE);
143                         tree = proto_item_add_subtree(item, ett_smb_msp);
144                 }
145
146                 /* do the opcode field */
147                 opcode = tvb_get_letohs(setup_tvb, offset);
148
149                 if (check_col(pinfo->cinfo, COL_INFO)) {
150                         col_add_str(pinfo->cinfo, COL_INFO,
151                                     val_to_str(opcode, opcode_vals, "Unknown opcode: 0x%04x"));
152                 }
153
154
155                 /* These are in the setup words; use "setup_tvb". */
156
157                 /* opcode */
158                 proto_tree_add_uint(tree, hf_opcode, setup_tvb, offset, 2,
159                     opcode);
160                 offset += 2;
161
162                 /* priority */
163                 proto_tree_add_item(tree, hf_priority, setup_tvb, offset, 2,
164                     TRUE);
165                 offset += 2;
166
167                 /* class */
168                 proto_tree_add_item(tree, hf_class, setup_tvb, offset, 2, TRUE);
169                 offset += 2;
170
171                 /* These are in the rest of the data; use "mshdr_tvb", which
172                    starts at the same place "setup_tvb" does. */
173
174                 /* size */
175                 /* this is actually bytecount in the SMB Transaction command */
176                 proto_tree_add_item(tree, hf_size, mshdr_tvb, offset, 2, TRUE);
177                 offset += 2;
178
179                 /* mailslot name */
180                 len = tvb_strsize(mshdr_tvb, offset);
181                 proto_tree_add_item(tree, hf_name, mshdr_tvb, offset, len, TRUE);
182                 offset += len;
183                 proto_item_set_len(item, offset);
184         }
185
186         switch(trans_subcmd){
187         case MAILSLOT_BROWSE:
188                 call_dissector(mailslot_browse_handle, tvb, pinfo,
189                     parent_tree);
190                 break;
191
192         case MAILSLOT_LANMAN:
193                 call_dissector(mailslot_lanman_handle, tvb, pinfo,
194                     parent_tree);
195                 break;
196
197         case MAILSLOT_NET:
198         case MAILSLOT_TEMP_NETLOGON:
199         case MAILSLOT_MSSP:
200                 call_dissector(netlogon_handle, tvb, pinfo,
201                     parent_tree);
202                 break;
203
204         default:
205                 /*
206                  * We dissected the mailslot header, but we don't know
207                  * how to dissect the message; dissect the latter as data,
208                  * but indicate that we successfully dissected the mailslot
209                  * stuff.
210                  */
211                 call_dissector(data_handle,tvb, pinfo, parent_tree);
212                 break;
213         }
214         return TRUE;
215 }
216
217 void
218 proto_register_smb_mailslot(void)
219 {
220         static hf_register_info hf[] = {
221                 { &hf_opcode,
222                         { "Opcode", "mailslot.opcode", FT_UINT16, BASE_DEC,
223                         VALS(opcode_vals), 0, "MAILSLOT OpCode", HFILL }},
224
225                 { &hf_priority,
226                         { "Priority", "mailslot.priority", FT_UINT16, BASE_DEC,
227                         NULL, 0, "MAILSLOT Priority of transaction", HFILL }},
228
229                 { &hf_class,
230                         { "Class", "mailslot.class", FT_UINT16, BASE_DEC,
231                         VALS(class_vals), 0, "MAILSLOT Class of transaction", HFILL }},
232
233                 { &hf_size,
234                         { "Size", "mailslot.size", FT_UINT16, BASE_DEC,
235                         NULL, 0, "MAILSLOT Total size of mail data", HFILL }},
236
237                 { &hf_name,
238                         { "Mailslot Name", "mailslot.name", FT_STRING, BASE_NONE,
239                         NULL, 0, "MAILSLOT Name of mailslot", HFILL }},
240
241         };
242
243         static gint *ett[] = {
244                 &ett_smb_msp
245         };
246
247         proto_smb_msp = proto_register_protocol(
248                 "SMB MailSlot Protocol", "SMB Mailslot", "mailslot");
249
250         proto_register_field_array(proto_smb_msp, hf, array_length(hf));
251         proto_register_subtree_array(ett, array_length(ett));
252 }
253
254 void
255 proto_reg_handoff_smb_mailslot(void)
256 {
257         mailslot_browse_handle = find_dissector("mailslot_browse");
258         mailslot_lanman_handle = find_dissector("mailslot_lanman");
259         netlogon_handle = find_dissector("netlogon");
260         data_handle = find_dissector("data");
261 }