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