From Graeme Hewson:
[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.36 2004/03/30 19:37:31 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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <string.h>
33 #include "packet-smb-common.h"
34 #include "packet-smb-mailslot.h"
35 #include "packet-smb-browse.h"
36 #include "packet-smb-pipe.h"
37
38 static int proto_smb_msp = -1;
39 static int hf_opcode = -1;
40 static int hf_priority = -1;
41 static int hf_class = -1;
42 static int hf_size = -1;
43 static int hf_name = -1;
44
45 static int ett_smb_msp = -1;
46
47 static dissector_handle_t mailslot_browse_handle;
48 static dissector_handle_t mailslot_lanman_handle;
49 static dissector_handle_t netlogon_handle;
50 static dissector_handle_t data_handle;
51
52 #define MAILSLOT_UNKNOWN              0
53 #define MAILSLOT_BROWSE               1
54 #define MAILSLOT_LANMAN               2
55 #define MAILSLOT_NET                  3
56 #define MAILSLOT_TEMP_NETLOGON        4
57 #define MAILSLOT_MSSP                 5
58
59 static const value_string opcode_vals[] = {
60         {1,     "Write Mail Slot"},
61         {0,     NULL}
62 };
63
64 static const value_string class_vals[] = {
65         {1,     "Reliable"},
66         {2,     "Unreliable & Broadcast"},
67         {0,     NULL}
68 };
69
70 /* decode the SMB mail slot protocol
71    for requests
72      mailslot is the name of the mailslot, e.g. BROWSE
73      si->trans_subcmd is set to the symbolic constant matching the mailslot name.
74    for responses
75      mailslot is NULL
76      si->trans_subcmd gives us which mailslot this response refers to.
77 */
78
79 gboolean
80 dissect_mailslot_smb(tvbuff_t *mshdr_tvb, tvbuff_t *setup_tvb,
81                      tvbuff_t *tvb, const char *mailslot, packet_info *pinfo,
82                      proto_tree *parent_tree)
83 {
84         smb_info_t *smb_info;
85         smb_transact_info_t *tri;
86         int             trans_subcmd;
87         proto_tree      *tree = NULL;
88         proto_item      *item = NULL;
89         guint16         opcode;
90         int             offset = 0;
91         int             len;
92
93         if (!proto_is_protocol_enabled(find_protocol_by_id(proto_smb_msp))) {
94                 return FALSE;
95         }
96         pinfo->current_proto = "SMB Mailslot";
97
98         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
99                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMB Mailslot");
100         }
101
102         if ((tvb==NULL) || (tvb_reported_length(tvb)==0)) {
103                 /* Interim reply */
104                 col_set_str(pinfo->cinfo, COL_INFO, "Interim reply");
105                 return TRUE;
106         }
107
108         if (check_col(pinfo->cinfo, COL_INFO)) {
109                 col_clear(pinfo->cinfo, COL_INFO);
110         }
111
112         smb_info = pinfo->private_data;
113         if (smb_info->sip != NULL)
114                 tri = smb_info->sip->extra_info;
115         else
116                 tri = NULL;
117
118         /* check which mailslot this is about */
119         trans_subcmd=MAILSLOT_UNKNOWN;
120         if(smb_info->request){
121                 if(strncmp(mailslot,"BROWSE",6) == 0){
122                         trans_subcmd=MAILSLOT_BROWSE;
123                 } else if(strncmp(mailslot,"LANMAN",6) == 0){
124                         trans_subcmd=MAILSLOT_LANMAN;
125                 } else if(strncmp(mailslot,"NET",3) == 0){
126                         trans_subcmd=MAILSLOT_NET;
127                 } else if(strncmp(mailslot,"TEMP\\NETLOGON",13) == 0){
128                         trans_subcmd=MAILSLOT_TEMP_NETLOGON;
129                 } else if(strncmp(mailslot,"MSSP",4) == 0){
130                         trans_subcmd=MAILSLOT_MSSP;
131                 }
132                 if (!pinfo->fd->flags.visited) {
133                         if (tri != NULL)
134                                 tri->trans_subcmd = trans_subcmd;
135                 }
136         } else {
137                 trans_subcmd = tri->trans_subcmd;
138         }
139
140         /* Only do these ones if we have them. For fragmented SMB Transactions
141            we may only have the setup area for the first fragment
142         */
143         if(mshdr_tvb && setup_tvb){
144                 if (parent_tree) {
145                         item = proto_tree_add_item(parent_tree, proto_smb_msp,
146                                                    mshdr_tvb, 0, -1, FALSE);
147                         tree = proto_item_add_subtree(item, ett_smb_msp);
148                 }
149
150                 /* do the opcode field */
151                 opcode = tvb_get_letohs(setup_tvb, offset);
152
153                 if (check_col(pinfo->cinfo, COL_INFO)) {
154                         col_add_str(pinfo->cinfo, COL_INFO,
155                                     val_to_str(opcode, opcode_vals, "Unknown opcode: 0x%04x"));
156                 }
157
158
159                 /* These are in the setup words; use "setup_tvb". */
160
161                 /* opcode */
162                 proto_tree_add_uint(tree, hf_opcode, setup_tvb, offset, 2,
163                     opcode);
164                 offset += 2;
165
166                 /* priority */
167                 proto_tree_add_item(tree, hf_priority, setup_tvb, offset, 2,
168                     TRUE);
169                 offset += 2;
170
171                 /* class */
172                 proto_tree_add_item(tree, hf_class, setup_tvb, offset, 2, TRUE);
173                 offset += 2;
174
175                 /* These are in the rest of the data; use "mshdr_tvb", which
176                    starts at the same place "setup_tvb" does. */
177
178                 /* size */
179                 /* this is actually bytecount in the SMB Transaction command */
180                 proto_tree_add_item(tree, hf_size, mshdr_tvb, offset, 2, TRUE);
181                 offset += 2;
182
183                 /* mailslot name */
184                 len = tvb_strsize(mshdr_tvb, offset);
185                 proto_tree_add_item(tree, hf_name, mshdr_tvb, offset, len, TRUE);
186                 offset += len;
187                 proto_item_set_len(item, offset);
188         }
189
190         switch(trans_subcmd){
191         case MAILSLOT_BROWSE:
192                 call_dissector(mailslot_browse_handle, tvb, pinfo,
193                     parent_tree);
194                 break;
195
196         case MAILSLOT_LANMAN:
197                 call_dissector(mailslot_lanman_handle, tvb, pinfo,
198                     parent_tree);
199                 break;
200
201         case MAILSLOT_NET:
202         case MAILSLOT_TEMP_NETLOGON:
203         case MAILSLOT_MSSP:
204                 call_dissector(netlogon_handle, tvb, pinfo,
205                     parent_tree);
206                 break;
207
208         default:
209                 /*
210                  * We dissected the mailslot header, but we don't know
211                  * how to dissect the message; dissect the latter as data,
212                  * but indicate that we successfully dissected the mailslot
213                  * stuff.
214                  */
215                 call_dissector(data_handle,tvb, pinfo, parent_tree);
216                 break;
217         }
218         return TRUE;
219 }
220
221 void
222 proto_register_smb_mailslot(void)
223 {
224         static hf_register_info hf[] = {
225                 { &hf_opcode,
226                         { "Opcode", "mailslot.opcode", FT_UINT16, BASE_DEC,
227                         VALS(opcode_vals), 0, "MAILSLOT OpCode", HFILL }},
228
229                 { &hf_priority,
230                         { "Priority", "mailslot.priority", FT_UINT16, BASE_DEC,
231                         NULL, 0, "MAILSLOT Priority of transaction", HFILL }},
232
233                 { &hf_class,
234                         { "Class", "mailslot.class", FT_UINT16, BASE_DEC,
235                         VALS(class_vals), 0, "MAILSLOT Class of transaction", HFILL }},
236
237                 { &hf_size,
238                         { "Size", "mailslot.size", FT_UINT16, BASE_DEC,
239                         NULL, 0, "MAILSLOT Total size of mail data", HFILL }},
240
241                 { &hf_name,
242                         { "Mailslot Name", "mailslot.name", FT_STRING, BASE_NONE,
243                         NULL, 0, "MAILSLOT Name of mailslot", HFILL }},
244
245         };
246
247         static gint *ett[] = {
248                 &ett_smb_msp
249         };
250
251         proto_smb_msp = proto_register_protocol(
252                 "SMB MailSlot Protocol", "SMB Mailslot", "mailslot");
253
254         proto_register_field_array(proto_smb_msp, hf, array_length(hf));
255         proto_register_subtree_array(ett, array_length(ett));
256 }
257
258 void
259 proto_reg_handoff_smb_mailslot(void)
260 {
261         mailslot_browse_handle = find_dissector("mailslot_browse");
262         mailslot_lanman_handle = find_dissector("mailslot_lanman");
263         netlogon_handle = find_dissector("netlogon");
264         data_handle = find_dissector("data");
265 }