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