From Chris Wilson:
[obnox/wireshark/wip.git] / packet-pcnfsd.c
1 /* packet-pcnfsd.c
2  * Routines for PCNFSD dissection
3  *
4  * $Id: packet-pcnfsd.c,v 1.12 2002/11/01 00:48:38 sahlberg Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-ypbind.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
28 /*
29 Protocol information comes from the book
30         "NFS Illustrated" by Brent Callaghan, ISBN 0-201-32570-5
31 */
32
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <string.h>
39
40 #include "packet-rpc.h"
41 #include "packet-pcnfsd.h"
42
43 static int proto_pcnfsd = -1;
44 static int hf_pcnfsd_procedure_v1 = -1;
45 static int hf_pcnfsd_procedure_v2 = -1;
46 static int hf_pcnfsd_auth_client = -1;
47 static int hf_pcnfsd_auth_ident_obscure = -1;
48 static int hf_pcnfsd_auth_ident_clear = -1;
49 static int hf_pcnfsd_auth_password_obscure = -1;
50 static int hf_pcnfsd_auth_password_clear = -1;
51 static int hf_pcnfsd_comment = -1;
52 static int hf_pcnfsd_status = -1;
53 static int hf_pcnfsd_uid = -1;
54 static int hf_pcnfsd_gid = -1;
55 static int hf_pcnfsd_gids_count = -1;
56 static int hf_pcnfsd_homedir = -1;
57 static int hf_pcnfsd_def_umask = -1;
58 static int hf_pcnfsd_username = -1;
59
60
61 static gint ett_pcnfsd = -1;
62 static gint ett_pcnfsd_auth_ident = -1;
63 static gint ett_pcnfsd_auth_password = -1;
64 static gint ett_pcnfsd_gids = -1;
65
66 int
67 dissect_pcnfsd_username(tvbuff_t *tvb, int offset, proto_tree *tree)
68 {
69         return dissect_rpc_string(tvb, tree, hf_pcnfsd_username, offset, NULL);
70 }
71
72 #define MAP_REQ_UID             0
73 #define MAP_REQ_GID             1
74 #define MAP_REQ_UNAME   2
75 #define MAP_REQ_GNAME   3
76
77 static const value_string names_mapreq[] =
78 {
79         {       MAP_REQ_UID,    "MAP_REQ_UID"   },
80         {       MAP_REQ_GID,    "MAP_REQ_GID"   },
81         {       MAP_REQ_UNAME,  "MAP_REQ_UNAME" },
82         {       MAP_REQ_GNAME,  "MAP_REQ_GNAME" },
83         {       0,      NULL    }
84 };
85
86 int
87 dissect_pcnfsd_mapreq(tvbuff_t *tvb, int offset, proto_tree *tree)
88 {
89         guint32 mapreq;
90
91         mapreq = tvb_get_ntohl(tvb, offset + 0);
92
93         if (tree)
94                 proto_tree_add_text(tree, tvb, offset, 4, "Request: %s (%u)",
95                         val_to_str(mapreq, names_mapreq, "%u"), mapreq);
96
97         offset += 4;
98
99         return offset;
100 }
101
102 int
103 dissect_pcnfsd2_dissect_mapreq_arg_item(tvbuff_t *tvb, int offset,
104         packet_info *pinfo _U_, proto_tree *tree)
105 {
106         offset = dissect_pcnfsd_mapreq(tvb, offset, tree);
107
108         offset = dissect_rpc_uint32(tvb, tree, hf_pcnfsd_uid, offset);
109
110         offset = dissect_pcnfsd_username(tvb, offset, tree);
111
112         return offset;
113 }
114
115 int
116 dissect_pcnfsd2_mapid_call(tvbuff_t *tvb, int offset, packet_info *pinfo,
117         proto_tree *tree)
118 {
119         offset = dissect_rpc_string(tvb, tree, hf_pcnfsd_comment, offset, NULL);
120
121         offset = dissect_rpc_list(tvb, pinfo, tree, offset,
122                 dissect_pcnfsd2_dissect_mapreq_arg_item);
123
124         return offset;
125 }
126
127 #define MAP_RES_OK              0
128 #define MAP_RES_UNKNOWN 1
129 #define MAP_RES_DENIED  2
130
131 static const value_string names_maprstat[] =
132 {
133         {       MAP_RES_OK,     "MAP_RES_OK"    },
134         {       MAP_RES_UNKNOWN,        "MAP_RES_UNKNOWN"       },
135         {       MAP_RES_DENIED, "MAP_RES_DENIED"        },
136         {       0,      NULL    }
137 };
138
139 int
140 dissect_pcnfsd2_dissect_mapreq_res_item(tvbuff_t *tvb, int offset,
141         packet_info *pinfo _U_, proto_tree *tree)
142 {
143         guint32 maprstat;
144
145         offset = dissect_pcnfsd_mapreq(tvb, offset, tree);
146
147         maprstat = tvb_get_ntohl(tvb, offset + 0);
148
149         if (tree)
150                 proto_tree_add_text(tree, tvb, offset, 4, "Status: %s (%u)",
151                         val_to_str(maprstat, names_maprstat, "%u"), maprstat);
152
153         offset += 4;
154
155         offset = dissect_rpc_uint32(tvb, tree, hf_pcnfsd_uid, offset);
156
157         offset = dissect_pcnfsd_username(tvb, offset, tree);
158
159         return offset;
160 }
161
162 int
163 dissect_pcnfsd2_mapid_reply(tvbuff_t *tvb, int offset, packet_info *pinfo,
164         proto_tree *tree)
165 {
166         offset = dissect_rpc_string(tvb, tree, hf_pcnfsd_comment, offset, NULL);
167
168         offset = dissect_rpc_list(tvb, pinfo, tree, offset,
169                 dissect_pcnfsd2_dissect_mapreq_res_item);
170
171         return offset;
172 }
173
174 /* "NFS Illustrated 14.7.13 */
175 static void
176 pcnfsd_decode_obscure(char* data, int len)
177 {
178         for ( ; len>0 ; len--, data++) {
179                 *data = (*data ^ 0x5b) & 0x7f;
180         }
181 }
182
183
184 /* "NFS Illustrated" 14.7.13 */
185 static int
186 dissect_pcnfsd2_auth_call(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
187         proto_tree *tree)
188 {
189         int     newoffset;
190         char    *ident = NULL;
191         proto_item      *ident_item = NULL;
192         proto_tree      *ident_tree = NULL;
193         char    *password = NULL;
194         proto_item      *password_item = NULL;
195         proto_tree      *password_tree = NULL;
196
197         offset = dissect_rpc_string(tvb, tree,
198                 hf_pcnfsd_auth_client, offset, NULL);
199
200         if (tree) {
201                 ident_item = proto_tree_add_text(tree, tvb,
202                                 offset, -1, "Authentication Ident");
203                 if (ident_item)
204                         ident_tree = proto_item_add_subtree(
205                                 ident_item, ett_pcnfsd_auth_ident);
206         }
207         newoffset = dissect_rpc_string(tvb, ident_tree,
208                 hf_pcnfsd_auth_ident_obscure, offset, &ident);
209         if (ident_item) {
210                 proto_item_set_len(ident_item, newoffset-offset);
211         }
212
213         if (ident) {
214                 pcnfsd_decode_obscure(ident, strlen(ident));
215                 if (ident_tree)
216                         proto_tree_add_string(ident_tree,
217                                 hf_pcnfsd_auth_ident_clear,
218                                 tvb, offset+4, strlen(ident), ident);
219         }
220         if (ident_item) {
221                 proto_item_set_text(ident_item, "Authentication Ident: %s",
222                         ident);
223         }
224         if (ident) {
225                 g_free(ident);
226                 ident = NULL;
227         }
228
229         offset = newoffset;
230
231         if (tree) {
232                 password_item = proto_tree_add_text(tree, tvb,
233                                 offset, -1, "Authentication Password");
234                 if (password_item)
235                         password_tree = proto_item_add_subtree(
236                                 password_item, ett_pcnfsd_auth_password);
237         }
238         newoffset = dissect_rpc_string(tvb, password_tree,
239                 hf_pcnfsd_auth_password_obscure, offset, &password);
240         if (password_item) {
241                 proto_item_set_len(password_item, newoffset-offset);
242         }
243
244         if (password) {
245                 pcnfsd_decode_obscure(password, strlen(password));
246                 if (password_tree)
247                         proto_tree_add_string(password_tree,
248                                 hf_pcnfsd_auth_password_clear,
249                                 tvb, offset+4, strlen(password), password);
250         }
251         if (password_item) {
252                 proto_item_set_text(password_item, "Authentication Password: %s",
253                         password);
254         }
255         if (password) {
256                 g_free(password);
257                 password = NULL;
258         }
259
260         offset = newoffset;
261
262         offset = dissect_rpc_string(tvb, tree,
263                 hf_pcnfsd_comment, offset, NULL);
264
265         return offset;
266 }
267
268
269 /* "NFS Illustrated" 14.7.13 */
270 static int
271 dissect_pcnfsd2_auth_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
272         proto_tree *tree)
273 {
274         int     gids_count;
275         proto_item      *gitem = NULL;
276         proto_tree      *gtree = NULL;
277         int     gids_i;
278
279         offset = dissect_rpc_uint32(tvb, tree, hf_pcnfsd_status, offset);
280         offset = dissect_rpc_uint32(tvb, tree, hf_pcnfsd_uid, offset);
281         offset = dissect_rpc_uint32(tvb, tree, hf_pcnfsd_gid, offset);
282         gids_count = tvb_get_ntohl(tvb,offset+0);
283         if (tree) {
284                 gitem = proto_tree_add_text(tree, tvb,
285                         offset, 4+gids_count*4, "Group IDs: %d", gids_count);
286                 gtree = proto_item_add_subtree(gitem, ett_pcnfsd_gids);
287         }
288         if (gtree) {
289                 proto_tree_add_item(gtree, hf_pcnfsd_gids_count, tvb, offset, 4, FALSE);
290         }
291         offset += 4;
292         for (gids_i = 0 ; gids_i < gids_count ; gids_i++) {
293                 offset = dissect_rpc_uint32(tvb, gtree,
294                                 hf_pcnfsd_gid, offset);
295         }
296         offset = dissect_rpc_string(tvb, tree,
297                 hf_pcnfsd_homedir, offset, NULL);
298         /* should be signed int32 */
299         offset = dissect_rpc_uint32(tvb, tree, hf_pcnfsd_def_umask, offset);
300         offset = dissect_rpc_string(tvb, tree,
301                 hf_pcnfsd_comment, offset, NULL);
302
303         return offset;
304 }
305
306
307 /* "NFS Illustrated", 14.6 */
308 /* proc number, "proc name", dissect_request, dissect_reply */
309 /* NULL as function pointer means: type of arguments is "void". */
310 static const vsff pcnfsd1_proc[] = {
311         { 0,    "NULL",         NULL,                           NULL },
312         { 1,    "AUTH",         NULL,                           NULL },
313         { 2,    "PR_INIT",      NULL,                           NULL },
314         { 3,    "PR_START",     NULL,                           NULL },
315         { 0,    NULL,           NULL,                           NULL }
316 };
317 static const value_string pcnfsd1_proc_vals[] = {
318         { 0,    "NULL" },
319         { 1,    "AUTH" },
320         { 2,    "PR_INIT" },
321         { 3,    "PR_START" },
322         { 0,    NULL }
323 };
324 /* end of PCNFS version 1 */
325
326
327 /* "NFS Illustrated", 14.7 */
328 static const vsff pcnfsd2_proc[] = {
329         { 0,    "NULL",         NULL,                           NULL },
330         { 1,    "INFO",         NULL,                           NULL },
331         { 2,    "PR_INIT",      NULL,                           NULL },
332         { 3,    "PR_START",     NULL,                           NULL },
333         { 4,    "PR_LIST",      NULL,                           NULL },
334         { 5,    "PR_QUEUE",     NULL,                           NULL },
335         { 6,    "PR_STATUS",    NULL,                           NULL },
336         { 7,    "PR_CANCEL",    NULL,                           NULL },
337         { 8,    "PR_ADMIN",     NULL,                           NULL },
338         { 9,    "PR_REQUEUE",   NULL,                           NULL },
339         { 10,   "PR_HOLD",      NULL,                           NULL },
340         { 11,   "PR_RELEASE",   NULL,                           NULL },
341         { 12,   "MAPID",
342         dissect_pcnfsd2_mapid_call, dissect_pcnfsd2_mapid_reply },
343         { 13,   "AUTH",
344         dissect_pcnfsd2_auth_call,      dissect_pcnfsd2_auth_reply },
345         { 14,   "ALERT",        NULL,                           NULL },
346         { 0,    NULL,           NULL,                           NULL }
347 };
348 static const value_string pcnfsd2_proc_vals[] = {
349         { 0,    "NULL" },
350         { 1,    "INFO" },
351         { 2,    "PR_INIT" },
352         { 3,    "PR_START" },
353         { 4,    "PR_LIST" },
354         { 5,    "PR_QUEUE" },
355         { 6,    "PR_STATUS" },
356         { 7,    "PR_CANCEL" },
357         { 8,    "PR_ADMIN" },
358         { 9,    "PR_REQUEUE" },
359         { 10,   "PR_HOLD" },
360         { 11,   "PR_RELEASE" },
361         { 12,   "MAPID" },
362         { 13,   "AUTH" },
363         { 14,   "ALERT" },
364         { 0,    NULL }
365 };
366 /* end of PCNFS version 2 */
367
368
369 void
370 proto_register_pcnfsd(void)
371 {
372         static hf_register_info hf[] = {
373                 { &hf_pcnfsd_procedure_v1, {
374                         "V1 Procedure", "pcnfsd.procedure_v1", FT_UINT32, BASE_DEC,
375                         VALS(pcnfsd1_proc_vals), 0, "V1 Procedure", HFILL }},
376                 { &hf_pcnfsd_procedure_v2, {
377                         "V2 Procedure", "pcnfsd.procedure_v2", FT_UINT32, BASE_DEC,
378                         VALS(pcnfsd2_proc_vals), 0, "V2 Procedure", HFILL }},
379                 { &hf_pcnfsd_auth_client, {
380                         "Authentication Client", "pcnfsd.auth.client", FT_STRING, BASE_DEC,
381                         NULL, 0, "Authentication Client", HFILL }},
382                 { &hf_pcnfsd_auth_ident_obscure, {
383                         "Obscure Ident", "pcnfsd.auth.ident.obscure", FT_STRING, BASE_DEC,
384                         NULL, 0, "Athentication Obscure Ident", HFILL }},
385                 { &hf_pcnfsd_auth_ident_clear, {
386                         "Clear Ident", "pcnfsd.auth.ident.clear", FT_STRING, BASE_DEC,
387                         NULL, 0, "Authentication Clear Ident", HFILL }},
388                 { &hf_pcnfsd_auth_password_obscure, {
389                         "Obscure Password", "pcnfsd.auth.password.obscure", FT_STRING, BASE_DEC,
390                         NULL, 0, "Athentication Obscure Password", HFILL }},
391                 { &hf_pcnfsd_auth_password_clear, {
392                         "Clear Password", "pcnfsd.auth.password.clear", FT_STRING, BASE_DEC,
393                         NULL, 0, "Authentication Clear Password", HFILL }},
394                 { &hf_pcnfsd_comment, {
395                         "Comment", "pcnfsd.comment", FT_STRING, BASE_DEC,
396                         NULL, 0, "Comment", HFILL }},
397                 { &hf_pcnfsd_status, {
398                         "Reply Status", "pcnfsd.status", FT_UINT32, BASE_DEC,
399                         NULL, 0, "Status", HFILL }},
400                 { &hf_pcnfsd_uid, {
401                         "User ID", "pcnfsd.uid", FT_UINT32, BASE_DEC,
402                         NULL, 0, "User ID", HFILL }},
403                 { &hf_pcnfsd_gid, {
404                         "Group ID", "pcnfsd.gid", FT_UINT32, BASE_DEC,
405                         NULL, 0, "Group ID", HFILL }},
406                 { &hf_pcnfsd_gids_count, {
407                         "Group ID Count", "pcnfsd.gids.count", FT_UINT32, BASE_DEC,
408                         NULL, 0, "Group ID Count", HFILL }},
409                 { &hf_pcnfsd_homedir, {
410                         "Home Directory", "pcnfsd.homedir", FT_STRING, BASE_DEC,
411                         NULL, 0, "Home Directory", HFILL }},
412                 { &hf_pcnfsd_def_umask, {
413                         "def_umask", "pcnfsd.def_umask", FT_INT32, BASE_OCT,
414                         NULL, 0, "def_umask", HFILL }},
415                 { &hf_pcnfsd_username, {
416                         "User name", "pcnfsd.username", FT_STRING, BASE_DEC,
417                         NULL, 0, "pcnfsd.username", HFILL }},
418         };
419
420         static gint *ett[] = {
421                 &ett_pcnfsd,
422                 &ett_pcnfsd_auth_ident,
423                 &ett_pcnfsd_auth_password,
424                 &ett_pcnfsd_gids
425         };
426
427         proto_pcnfsd = proto_register_protocol("PC NFS",
428             "PCNFSD", "pcnfsd");
429         proto_register_field_array(proto_pcnfsd, hf, array_length(hf));
430         proto_register_subtree_array(ett, array_length(ett));
431 }
432
433 void
434 proto_reg_handoff_pcnfsd(void)
435 {
436         /* Register the protocol as RPC */
437         rpc_init_prog(proto_pcnfsd, PCNFSD_PROGRAM, ett_pcnfsd);
438         /* Register the procedure tables */
439         rpc_init_proc_table(PCNFSD_PROGRAM, 1, pcnfsd1_proc, hf_pcnfsd_procedure_v1);
440         rpc_init_proc_table(PCNFSD_PROGRAM, 2, pcnfsd2_proc, hf_pcnfsd_procedure_v2);
441 }
442