bb68bd0cb71ac15c82376709983fdcd0a10d39a8
[obnox/wireshark/wip.git] / packet-mount.c
1 /* packet-mount.c
2  * Routines for mount dissection
3  *
4  * $Id: packet-mount.c,v 1.9 2000/01/07 22:05:32 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-smb.c
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35
36
37 #include "packet-rpc.h"
38 #include "packet-mount.h"
39 #include "packet-nfs.h"
40
41
42 static int proto_mount = -1;
43 static int hf_mount_path = -1;
44 static int hf_mount_status = -1;
45 static int hf_mount_pathconf_link_max = -1;
46 static int hf_mount_pathconf_max_canon = -1;
47 static int hf_mount_pathconf_max_input = -1;
48 static int hf_mount_pathconf_name_max = -1;
49 static int hf_mount_pathconf_path_max = -1;
50 static int hf_mount_pathconf_pipe_buf = -1;
51 static int hf_mount_pathconf_vdisable = -1;
52 static int hf_mount_pathconf_mask = -1;
53 static int hf_mount_pathconf_error_all = -1;
54 static int hf_mount_pathconf_error_link_max = -1;
55 static int hf_mount_pathconf_error_max_canon = -1;
56 static int hf_mount_pathconf_error_max_input = -1;
57 static int hf_mount_pathconf_error_name_max = -1;
58 static int hf_mount_pathconf_error_path_max = -1;
59 static int hf_mount_pathconf_error_pipe_buf = -1;
60 static int hf_mount_pathconf_chown_restricted = -1;
61 static int hf_mount_pathconf_no_trunc = -1;
62 static int hf_mount_pathconf_error_vdisable = -1;
63 static int hf_mount_flavors = -1;
64 static int hf_mount_flavor = -1;
65
66 static gint ett_mount = -1;
67 static gint ett_mount_pathconf_mask = -1;
68
69
70 /* RFC 1094, Page 24 */
71 static int
72 dissect_fhstatus(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
73 {
74         guint32 status;
75
76         if (!BYTES_ARE_IN_FRAME(offset,4)) return offset;
77         status = EXTRACT_UINT(pd, offset+0);
78         if (tree) {
79                 proto_tree_add_item(tree, hf_mount_status, offset, 4, status);
80         }
81         offset += 4;
82
83         switch (status) {
84                 case 0:
85                         offset = dissect_fhandle(pd,offset,fd,tree,"fhandle");
86                 break;
87                 default:
88                         /* void */
89                 break;
90         }
91
92         return offset;
93 }
94
95
96 static int
97 dissect_mount_dirpath_call(const u_char *pd, int offset, frame_data *fd,
98         proto_tree *tree)
99 {
100         if ( tree )
101         {
102                 offset = dissect_rpc_string(pd,offset,fd,tree,hf_mount_path);
103         }
104         
105         return offset;
106 }
107
108
109 /* RFC 1094, Page 25,26 */
110 static int
111 dissect_mount_mnt_reply(const u_char *pd, int offset, frame_data *fd,
112         proto_tree *tree)
113 {
114         offset = dissect_fhstatus(pd, offset, fd, tree);
115
116         return offset;
117 }
118
119
120 #define OFFS_MASK       32      /* offset of the "pc_mask" field */
121
122 #define PC_ERROR_ALL            0x0001
123 #define PC_ERROR_LINK_MAX       0x0002
124 #define PC_ERROR_MAX_CANON      0x0004
125 #define PC_ERROR_MAX_INPUT      0x0008
126 #define PC_ERROR_NAME_MAX       0x0010
127 #define PC_ERROR_PATH_MAX       0x0020
128 #define PC_ERROR_PIPE_BUF       0x0040
129 #define PC_CHOWN_RESTRICTED     0x0080
130 #define PC_NO_TRUNC             0x0100
131 #define PC_ERROR_VDISABLE       0x0200
132
133 static const true_false_string tos_error_all = {
134   "All info invalid",
135   "Some or all info valid"
136 };
137
138 static const true_false_string tos_error_link_max = {
139   "LINK_MAX invalid",
140   "LINK_MAX valid"
141 };
142
143 static const true_false_string tos_error_max_canon = {
144   "MAX_CANON invalid",
145   "MAX_CANON valid"
146 };
147
148 static const true_false_string tos_error_max_input = {
149   "MAX_INPUT invalid",
150   "MAX_INPUT valid"
151 };
152
153 static const true_false_string tos_error_name_max = {
154   "NAME_MAX invalid",
155   "NAME_MAX valid"
156 };
157
158 static const true_false_string tos_error_path_max = {
159   "PATH_MAX invalid",
160   "PATH_MAX valid"
161 };
162
163 static const true_false_string tos_error_pipe_buf = {
164   "PIPE_BUF invalid",
165   "PIPE_BUF valid"
166 };
167
168 static const true_false_string tos_chown_restricted = {
169   "Only a privileged user can change the ownership of a file",
170   "Users may give away their own files"
171 };
172
173 static const true_false_string tos_no_trunc = {
174   "File names that are too long will get an error",
175   "File names that are too long will be truncated"
176 };
177
178 static const true_false_string tos_error_vdisable = {
179   "VDISABLE invalid",
180   "VDISABLE valid"
181 };
182
183 static int
184 dissect_mount_pathconf_reply(const u_char *pd, int offset, frame_data *fd,
185         proto_tree *tree)
186 {
187         guint32 pc_mask;
188         proto_item *ti;
189         proto_tree *mask_tree;
190
191         /*
192          * Extract the mask first, so we know which other fields the
193          * server was able to return to us.
194          */
195         if (!BYTES_ARE_IN_FRAME(offset + OFFS_MASK, 4))
196                 return offset;
197         pc_mask = EXTRACT_UINT(pd, offset+OFFS_MASK) & 0xFFFF;
198
199         if (!BYTES_ARE_IN_FRAME(offset + 0,4))
200                 return offset;
201         if (!(pc_mask & (PC_ERROR_LINK_MAX|PC_ERROR_ALL))) {
202                 if (tree) {
203                         proto_tree_add_item(tree,
204                             hf_mount_pathconf_link_max, offset, 4,
205                             EXTRACT_UINT(pd, offset+0));
206                 }
207         }
208         offset += 4;
209
210         if (!BYTES_ARE_IN_FRAME(offset,4))
211                 return offset;
212         if (!(pc_mask & (PC_ERROR_MAX_CANON|PC_ERROR_ALL))) {
213                 if (tree) {
214                         proto_tree_add_item(tree,
215                             hf_mount_pathconf_max_canon, offset + 2, 2,
216                             (EXTRACT_UINT(pd, offset+0)) & 0xFFFF);
217                 }
218         }
219         
220         offset += 4;
221
222         if (!BYTES_ARE_IN_FRAME(offset,4))
223                 return offset;
224         if (!(pc_mask & (PC_ERROR_MAX_INPUT|PC_ERROR_ALL))) {
225                 if (tree) {
226                         proto_tree_add_item(tree,
227                             hf_mount_pathconf_max_input, offset + 2, 2,
228                             (EXTRACT_UINT(pd, offset+0)) & 0xFFFF);
229                 }
230         }
231         offset += 4;
232
233         if (!BYTES_ARE_IN_FRAME(offset,4))
234                 return offset;
235         if (!(pc_mask & (PC_ERROR_NAME_MAX|PC_ERROR_ALL))) {
236                 if (tree) {
237                         proto_tree_add_item(tree,
238                             hf_mount_pathconf_name_max, offset + 2, 2,
239                             (EXTRACT_UINT(pd, offset+0)) & 0xFFFF);
240                 }
241         }
242         offset += 4;
243
244         if (!BYTES_ARE_IN_FRAME(offset,4))
245                 return offset;
246         if (!(pc_mask & (PC_ERROR_PATH_MAX|PC_ERROR_ALL))) {
247                 if (tree) {
248                         proto_tree_add_item(tree,
249                             hf_mount_pathconf_path_max, offset + 2, 2,
250                             (EXTRACT_UINT(pd, offset+0)) & 0xFFFF);
251                 }
252         }
253         offset += 4;
254
255         if (!BYTES_ARE_IN_FRAME(offset,4))
256                 return offset;
257         if (!(pc_mask & (PC_ERROR_PIPE_BUF|PC_ERROR_ALL))) {
258                 if (tree) {
259                         proto_tree_add_item(tree,
260                             hf_mount_pathconf_pipe_buf, offset + 2, 2,
261                             (EXTRACT_UINT(pd, offset+0)) & 0xFFFF);
262                 }
263         }
264         offset += 4;
265
266         offset += 4;    /* skip "pc_xxx" pad field */
267
268         if (!BYTES_ARE_IN_FRAME(offset,4))
269                 return offset;
270         if (!(pc_mask & (PC_ERROR_VDISABLE|PC_ERROR_ALL))) {
271                 if (tree) {
272                         proto_tree_add_item(tree,
273                             hf_mount_pathconf_vdisable, offset + 3, 1,
274                             (EXTRACT_UINT(pd, offset+0)) & 0xFF);
275                 }
276         }
277         offset += 4;
278
279         if (tree) {
280                 ti = proto_tree_add_item(tree, hf_mount_pathconf_mask,
281                     offset + 2, 2, pc_mask);
282                 mask_tree = proto_item_add_subtree(ti, ett_mount_pathconf_mask);
283                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_all,
284                     offset + 2, 2, pc_mask);
285                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_link_max,
286                     offset + 2, 2, pc_mask);
287                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_max_canon,
288                     offset + 2, 2, pc_mask);
289                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_max_input,
290                     offset + 2, 2, pc_mask);
291                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_name_max,
292                     offset + 2, 2, pc_mask);
293                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_path_max,
294                     offset + 2, 2, pc_mask);
295                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_pipe_buf,
296                     offset + 2, 2, pc_mask);
297                 proto_tree_add_item(mask_tree, hf_mount_pathconf_chown_restricted,
298                     offset + 2, 2, pc_mask);
299                 proto_tree_add_item(mask_tree, hf_mount_pathconf_no_trunc,
300                     offset + 2, 2, pc_mask);
301                 proto_tree_add_item(mask_tree, hf_mount_pathconf_error_vdisable,
302                     offset + 2, 2, pc_mask);
303         }
304         offset += 4;
305         return offset;
306 }
307
308 /* proc number, "proc name", dissect_request, dissect_reply */
309 /* NULL as function pointer means: take the generic one. */
310
311 /* Mount protocol version 1, RFC 1094 */
312 static const vsff mount1_proc[] = {
313     { 0, "NULL", NULL, NULL },
314     { MOUNTPROC_MNT,        "MNT",      
315                 dissect_mount_dirpath_call, dissect_mount_mnt_reply },
316     { MOUNTPROC_DUMP,       "DUMP",
317                 NULL, NULL },
318     { MOUNTPROC_UMNT,      "UMNT",        
319                 dissect_mount_dirpath_call, NULL },
320     { MOUNTPROC_UMNTALL,   "UMNTALL",
321                 NULL, NULL },
322     { MOUNTPROC_EXPORT,    "EXPORT",
323                 NULL, NULL },
324     { MOUNTPROC_EXPORTALL, "EXPORTALL",
325                 NULL, NULL },
326     { 0, NULL, NULL, NULL }
327 };
328 /* end of mount version 1 */
329
330
331 /* Mount protocol version 2, private communication from somebody at Sun;
332    mount V2 is V1 plus MOUNTPROC_PATHCONF to fetch information for the
333    POSIX "pathconf()" call. */
334 static const vsff mount2_proc[] = {
335     { 0, "NULL", NULL, NULL },
336     { MOUNTPROC_MNT,        "MNT",      
337                 dissect_mount_dirpath_call, dissect_mount_mnt_reply },
338     { MOUNTPROC_DUMP,       "DUMP",
339                 NULL, NULL },
340     { MOUNTPROC_UMNT,      "UMNT",        
341                 dissect_mount_dirpath_call, NULL },
342     { MOUNTPROC_UMNTALL,   "UMNTALL",
343                 NULL, NULL },
344     { MOUNTPROC_EXPORT,    "EXPORT",
345                 NULL, NULL },
346     { MOUNTPROC_EXPORTALL, "EXPORTALL",
347                 NULL, NULL },
348     { MOUNTPROC_EXPORTALL, "EXPORTALL",
349                 NULL, NULL },
350     { MOUNTPROC_PATHCONF,  "PATHCONF",
351                 dissect_mount_dirpath_call, dissect_mount_pathconf_reply },
352     { 0, NULL, NULL, NULL }
353 };
354 /* end of mount version 2 */
355
356
357 /* RFC 1813, Page 107 */
358 static const value_string mount3_mountstat3[] = 
359 {
360         {       0,      "OK" },
361         {       1,      "ERR_PERM" },
362         {       2,      "ERR_NOENT" },
363         {       5,      "ERR_IO" },
364         {       13,     "ERR_ACCESS" },
365         {       20,     "ERR_NOTDIR" },
366         {       22,     "ERR_INVAL" },
367         {       63,     "ERR_NAMETOOLONG" },
368         {       10004,  "ERR_NOTSUPP" },
369         {       10006,  "ERR_SERVERFAULT" },
370         {       0,      NULL }
371 };
372
373
374 /* RFC 1813, Page 107 */
375 static int
376 dissect_mountstat3(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
377         int hfindex, guint32* status)
378 {
379         guint32 mountstat3;
380
381         if (!BYTES_ARE_IN_FRAME(offset,4)) return offset;
382         mountstat3 = EXTRACT_UINT(pd, offset+0);
383
384         if (tree) {
385                 proto_tree_add_item(tree, hfindex, offset, 4, mountstat3);
386         }
387         
388         offset += 4;
389         *status = mountstat3;
390         return offset;
391 }
392
393
394 /* RFC 1831, Page 109 */
395 static int
396 dissect_mount3_mnt_reply(const u_char *pd, int offset, frame_data *fd,
397         proto_tree *tree)
398 {
399         guint32 status;
400         guint32 auth_flavors;
401         guint32 auth_flavor;
402         guint32 auth_flavor_i;
403         
404         offset = dissect_mountstat3(pd, offset, fd, tree, hf_mount_status, &status);
405         switch (status) {
406                 case 0:
407                         offset = dissect_nfs_fh3(pd,offset,fd,tree,"fhandle");
408                         if (!BYTES_ARE_IN_FRAME(offset,4)) return offset;
409                         auth_flavors = EXTRACT_UINT(pd,offset+0);
410                         proto_tree_add_item(tree,hf_mount_flavors,
411                                 offset, 4, auth_flavors);
412                         offset += 4;
413                         for (auth_flavor_i = 0 ; auth_flavor_i < hf_mount_flavors ; auth_flavor_i++) {
414                                 if (!BYTES_ARE_IN_FRAME(offset,4)) return offset;
415                                 auth_flavor = EXTRACT_UINT(pd,offset+0);
416                                 proto_tree_add_item(tree,hf_mount_flavor,
417                                         offset, 4, auth_flavor);
418                                 offset += 4;
419                         }
420                 break;
421                 default:
422                         /* void */
423                 break;
424         }
425         
426         return offset;
427 }
428
429 /* Mount protocol version 3, RFC 1813 */
430 static const vsff mount3_proc[] = {
431         { 0, "NULL", NULL, NULL },
432         { MOUNTPROC_MNT, "MNT",
433                 dissect_mount_dirpath_call, dissect_mount3_mnt_reply },
434         { MOUNTPROC_DUMP, "DUMP",
435                 NULL, NULL },
436         { MOUNTPROC_UMNT, "UMNT",
437                 dissect_mount_dirpath_call, NULL },
438         { MOUNTPROC_UMNTALL, "UMNTALL",
439                 NULL, NULL },
440         { MOUNTPROC_EXPORT, "EXPORT",
441                 NULL, NULL },
442         { 0, NULL, NULL, NULL }
443 };
444 /* end of Mount protocol version 3 */
445
446
447 void
448 proto_register_mount(void)
449 {
450         static hf_register_info hf[] = {
451                 { &hf_mount_path, {
452                         "Path", "mount.path", FT_STRING, BASE_DEC,
453                         NULL, 0, "Path" }},
454                 { &hf_mount_status, {
455                         "Status", "mount.status", FT_UINT32, BASE_DEC,
456                         VALS(mount3_mountstat3), 0, "Status" }},
457                 { &hf_mount_pathconf_link_max, {
458                         "Maximum number of links to a file", "mount.pathconf.link_max",
459                         FT_UINT32, BASE_DEC,
460                         NULL, 0, "Maximum number of links allowed to a file" }},
461                 { &hf_mount_pathconf_max_canon, {
462                         "Maximum terminal input line length", "mount.pathconf.max_canon",
463                         FT_UINT16, BASE_DEC,
464                         NULL, 0, "Max tty input line length" }},
465                 { &hf_mount_pathconf_max_input, {
466                         "Terminal input buffer size", "mount.pathconf.max_input",
467                         FT_UINT16, BASE_DEC,
468                         NULL, 0, "Terminal input buffer size" }},
469                 { &hf_mount_pathconf_name_max, {
470                         "Maximum file name length", "mount.pathconf.name_max",
471                         FT_UINT16, BASE_DEC,
472                         NULL, 0, "Maximum file name length" }},
473                 { &hf_mount_pathconf_path_max, {
474                         "Maximum path name length", "mount.pathconf.path_max",
475                         FT_UINT16, BASE_DEC,
476                         NULL, 0, "Maximum path name length" }},
477                 { &hf_mount_pathconf_pipe_buf, {
478                         "Pipe buffer size", "mount.pathconf.pipe_buf",
479                         FT_UINT16, BASE_DEC,
480                         NULL, 0, "Maximum amount of data that can be written atomically to a pipe" }},
481                 { &hf_mount_pathconf_vdisable, {
482                         "VDISABLE character", "mount.pathconf.pipe_buf",
483                         FT_UINT8, BASE_HEX,
484                         NULL, 0, "Character value to disable a terminal special character" }},
485                 { &hf_mount_pathconf_mask, {
486                         "Reply error/status bits", "mount.pathconf.mask",
487                         FT_UINT16, BASE_HEX,
488                         NULL, 0, "Bit mask with error and status bits" }},
489                 { &hf_mount_pathconf_error_all, {
490                         "ERROR_ALL",    "mount.pathconf.mask.error_all",
491                         FT_BOOLEAN, 16, TFS(&tos_error_all),
492                         PC_ERROR_ALL, "" }},
493                 { &hf_mount_pathconf_error_link_max, {
494                         "ERROR_LINK_MAX", "mount.pathconf.mask.error_link_max",
495                         FT_BOOLEAN, 16, TFS(&tos_error_link_max),
496                         PC_ERROR_LINK_MAX, "" }},
497                 { &hf_mount_pathconf_error_max_canon, {
498                         "ERROR_MAX_CANON", "mount.pathconf.mask.error_max_canon",
499                         FT_BOOLEAN, 16, TFS(&tos_error_max_canon),
500                         PC_ERROR_MAX_CANON, "" }},
501                 { &hf_mount_pathconf_error_max_input, {
502                         "ERROR_MAX_INPUT", "mount.pathconf.mask.error_max_input",
503                         FT_BOOLEAN, 16, TFS(&tos_error_max_input),
504                         PC_ERROR_MAX_INPUT, "" }},
505                 { &hf_mount_pathconf_error_name_max, {
506                         "ERROR_NAME_MAX", "mount.pathconf.mask.error_name_max",
507                         FT_BOOLEAN, 16, TFS(&tos_error_name_max),
508                         PC_ERROR_NAME_MAX, "" }},
509                 { &hf_mount_pathconf_error_path_max, {
510                         "ERROR_PATH_MAX", "mount.pathconf.mask.error_path_max",
511                         FT_BOOLEAN, 16, TFS(&tos_error_path_max),
512                         PC_ERROR_PATH_MAX, "" }},
513                 { &hf_mount_pathconf_error_pipe_buf, {
514                         "ERROR_PIPE_BUF", "mount.pathconf.mask.error_pipe_buf",
515                         FT_BOOLEAN, 16, TFS(&tos_error_pipe_buf),
516                         PC_ERROR_PIPE_BUF, "" }},
517                 { &hf_mount_pathconf_chown_restricted, {
518                         "CHOWN_RESTRICTED", "mount.pathconf.mask.chown_restricted",
519                         FT_BOOLEAN, 16, TFS(&tos_chown_restricted),
520                         PC_CHOWN_RESTRICTED, "" }},
521                 { &hf_mount_pathconf_no_trunc, {
522                         "NO_TRUNC", "mount.pathconf.mask.no_trunc",
523                         FT_BOOLEAN, 16, TFS(&tos_no_trunc),
524                         PC_NO_TRUNC, "" }},
525                 { &hf_mount_pathconf_error_vdisable, {
526                         "ERROR_VDISABLE", "mount.pathconf.mask.error_vdisable",
527                         FT_BOOLEAN, 16, TFS(&tos_error_vdisable),
528                         PC_ERROR_VDISABLE, "" }},
529                 { &hf_mount_flavors, {
530                         "Flavors", "mount.flavors", FT_UINT32, BASE_DEC,
531                         NULL, 0, "Flavors" }},
532                 { &hf_mount_flavor, {
533                         "Flavor", "mount.flavor", FT_UINT32, BASE_DEC,
534                         VALS(rpc_auth_flavor), 0, "Flavor" }},
535         };
536         static gint *ett[] = {
537                 &ett_mount,
538                 &ett_mount_pathconf_mask,
539         };
540
541         proto_mount = proto_register_protocol("Mount Service", "mount");
542         proto_register_field_array(proto_mount, hf, array_length(hf));
543         proto_register_subtree_array(ett, array_length(ett));
544
545         /* Register the protocol as RPC */
546         rpc_init_prog(proto_mount, MOUNT_PROGRAM, ett_mount);
547         /* Register the procedure tables */
548         rpc_init_proc_table(MOUNT_PROGRAM, 1, mount1_proc);
549         rpc_init_proc_table(MOUNT_PROGRAM, 2, mount2_proc);
550         rpc_init_proc_table(MOUNT_PROGRAM, 3, mount3_proc);
551 }