1/*
2 * Copyright (c) 1993, 1994 Jeffrey C. Mogul, Digital Equipment Corporation,
3 * Western Research Laboratory. All rights reserved.
4 * Copyright (c) 2001 Compaq Computer Corporation. All rights reserved.
5 *
6 *  Permission to use, copy, and modify this software and its
7 *  documentation is hereby granted only under the following terms and
8 *  conditions.  Both the above copyright notice and this permission
9 *  notice must appear in all copies of the software, derivative works
10 *  or modified versions, and any portions thereof, and both notices
11 *  must appear in supporting documentation.
12 *
13 *  Redistribution and use in source and binary forms, with or without
14 *  modification, are permitted provided that the following conditions
15 *  are met:
16 *    1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 *    2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in
20 *    the documentation and/or other materials provided with the
21 *    distribution.
22 *
23 *  THE SOFTWARE IS PROVIDED "AS IS" AND COMPAQ COMPUTER CORPORATION
24 *  DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25 *  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.   IN NO
26 *  EVENT SHALL COMPAQ COMPUTER CORPORATION BE LIABLE FOR ANY
27 *  SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29 *  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
30 *  OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31 *  SOFTWARE.
32 */
33
34/*
35 * parsenfsfh.c - portable parser for NFS file handles
36 *			uses all sorts of heuristics
37 *
38 * Jeffrey C. Mogul
39 * Digital Equipment Corporation
40 * Western Research Laboratory
41 */
42
43#ifdef HAVE_CONFIG_H
44#include "config.h"
45#endif
46
47#include <netdissect-stdinc.h>
48
49#include <stdio.h>
50#include <string.h>
51
52#include "netdissect.h"
53#include "nfsfh.h"
54
55/*
56 * This routine attempts to parse a file handle (in network byte order),
57 * using heuristics to guess what kind of format it is in.  See the
58 * file "fhandle_layouts" for a detailed description of the various
59 * patterns we know about.
60 *
61 * The file handle is parsed into our internal representation of a
62 * file-system id, and an internal representation of an inode-number.
63 */
64
65#define	FHT_UNKNOWN	0
66#define	FHT_AUSPEX	1
67#define	FHT_DECOSF	2
68#define	FHT_IRIX4	3
69#define	FHT_IRIX5	4
70#define	FHT_SUNOS3	5
71#define	FHT_SUNOS4	6
72#define	FHT_ULTRIX	7
73#define	FHT_VMSUCX	8
74#define	FHT_SUNOS5	9
75#define	FHT_AIX32	10
76#define	FHT_HPUX9	11
77#define	FHT_BSD44	12
78
79#ifdef	ultrix
80/* Nasty hack to keep the Ultrix C compiler from emitting bogus warnings */
81#define	XFF(x)	((uint32_t)(x))
82#else
83#define	XFF(x)	(x)
84#endif
85
86#define	make_uint32(msb,b,c,lsb)\
87	(XFF(lsb) + (XFF(c)<<8) + (XFF(b)<<16) + (XFF(msb)<<24))
88
89#define	make_uint24(msb,b, lsb)\
90	(XFF(lsb) + (XFF(b)<<8) + (XFF(msb)<<16))
91
92#define	make_uint16(msb,lsb)\
93	(XFF(lsb) + (XFF(msb)<<8))
94
95#ifdef	__alpha
96	/* or other 64-bit systems */
97#define	make_uint48(msb,b,c,d,e,lsb)\
98	((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
99#else
100	/* on 32-bit systems ignore high-order bits */
101#define	make_uint48(msb,b,c,d,e,lsb)\
102	((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
103#endif
104
105static int is_UCX(const unsigned char *, u_int);
106
107void
108Parse_fh(register const unsigned char *fh, u_int len, my_fsid *fsidp,
109	 uint32_t *inop,
110	 const char **osnamep, /* if non-NULL, return OS name here */
111	 const char **fsnamep, /* if non-NULL, return server fs name here (for VMS) */
112	 int ourself)	/* true if file handle was generated on this host */
113{
114	register const unsigned char *fhp = fh;
115	uint32_t temp;
116	int fhtype = FHT_UNKNOWN;
117	u_int i;
118
119	/*
120	 * Require at least 16 bytes of file handle; it's variable-length
121	 * in NFSv3.  "len" is in units of 32-bit words, not bytes.
122	 */
123	if (len < 16/4)
124		fhtype = FHT_UNKNOWN;
125	else {
126		if (ourself) {
127		    /* File handle generated on this host, no need for guessing */
128#if	defined(IRIX40)
129		    fhtype = FHT_IRIX4;
130#endif
131#if	defined(IRIX50)
132		    fhtype = FHT_IRIX5;
133#endif
134#if	defined(IRIX51)
135		    fhtype = FHT_IRIX5;
136#endif
137#if	defined(SUNOS4)
138		    fhtype = FHT_SUNOS4;
139#endif
140#if	defined(SUNOS5)
141		    fhtype = FHT_SUNOS5;
142#endif
143#if	defined(ultrix)
144		    fhtype = FHT_ULTRIX;
145#endif
146#if	defined(__osf__)
147		    fhtype = FHT_DECOSF;
148#endif
149#if	defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) \
150     || defined(__OpenBSD__)
151		    fhtype = FHT_BSD44;
152#endif
153		}
154		/*
155		 * This is basically a big decision tree
156		 */
157		else if ((fhp[0] == 0) && (fhp[1] == 0)) {
158		    /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
159		    /* probably rules out HP-UX, AIX unless they allow major=0 */
160		    if ((fhp[2] == 0) && (fhp[3] == 0)) {
161			/* bytes[2,3] == (0,0); must be Auspex */
162			/* XXX or could be Ultrix+MASSBUS "hp" disk? */
163			fhtype = FHT_AUSPEX;
164		    }
165		    else {
166			/*
167			 * bytes[2,3] != (0,0); rules out Auspex, could be
168			 * DECOSF, SUNOS4, or IRIX4
169			 */
170			if ((fhp[4] != 0) && (fhp[5] == 0) &&
171				(fhp[8] == 12) && (fhp[9] == 0)) {
172			    /* seems to be DECOSF, with minor == 0 */
173			    fhtype = FHT_DECOSF;
174			}
175			else {
176			    /* could be SUNOS4 or IRIX4 */
177			    /* XXX the test of fhp[5] == 8 could be wrong */
178			    if ((fhp[4] == 0) && (fhp[5] == 8) && (fhp[6] == 0) &&
179			        (fhp[7] == 0)) {
180				/* looks like a length, not a file system typecode */
181				fhtype = FHT_IRIX4;
182			    }
183			    else {
184				/* by elimination */
185				fhtype = FHT_SUNOS4;
186			    }
187			}
188		    }
189		}
190		else {
191		    /*
192		     * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
193		     * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
194		     * could be AIX, HP-UX
195		     */
196		    if ((fhp[2] == 0) && (fhp[3] == 0)) {
197			/*
198			 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
199			 * (unless the exported device name is just one letter!),
200			 * could be Ultrix, IRIX5, AIX, or SUNOS5
201			 * might be HP-UX (depends on their values for minor devs)
202			 */
203			if ((fhp[6] == 0) && (fhp[7] == 0)) {
204			    fhtype = FHT_BSD44;
205			}
206			/*XXX we probably only need to test of these two bytes */
207			else if ((len >= 24/4) && (fhp[21] == 0) && (fhp[23] == 0)) {
208			    fhtype = FHT_ULTRIX;
209			}
210			else {
211			    /* Could be SUNOS5/IRIX5, maybe AIX */
212			    /* XXX no obvious difference between SUNOS5 and IRIX5 */
213			    if (fhp[9] == 10)
214				fhtype = FHT_SUNOS5;
215			    /* XXX what about AIX? */
216			}
217		    }
218		    else {
219			/*
220			 * bytes[2,3] != (0,0); rules out Ultrix, could be
221			 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
222			 */
223			if ((fhp[8] == 12) && (fhp[9] == 0)) {
224			    fhtype = FHT_DECOSF;
225			}
226			else if ((fhp[8] == 0) && (fhp[9] == 10)) {
227			    /* could be SUNOS5/IRIX5, AIX, HP-UX */
228			    if ((fhp[7] == 0) && (fhp[6] == 0) &&
229				(fhp[5] == 0) && (fhp[4] == 0)) {
230				/* XXX is this always true of HP-UX? */
231				fhtype = FHT_HPUX9;
232			    }
233			    else if (fhp[7] == 2) {
234				/* This would be MNT_NFS on AIX, which is impossible */
235				fhtype = FHT_SUNOS5;	/* or maybe IRIX5 */
236			    }
237			    else {
238				/*
239				 * XXX Could be SUNOS5/IRIX5 or AIX.  I don't
240				 * XXX see any way to disambiguate these, so
241				 * XXX I'm going with the more likely guess.
242				 * XXX Sorry, Big Blue.
243				 */
244				fhtype = FHT_SUNOS5;	/* or maybe IRIX5 */
245			    }
246		        }
247			else {
248			    if (is_UCX(fhp, len)) {
249				fhtype = FHT_VMSUCX;
250			    }
251			    else {
252				fhtype = FHT_UNKNOWN;
253			    }
254			}
255		    }
256		}
257	}
258
259	/* XXX still needs to handle SUNOS3 */
260
261	switch (fhtype) {
262	case FHT_AUSPEX:
263	    fsidp->Fsid_dev.Minor = fhp[7];
264	    fsidp->Fsid_dev.Major = fhp[6];
265	    fsidp->fsid_code = 0;
266
267	    *inop = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
268
269	    if (osnamep)
270		*osnamep = "Auspex";
271	    break;
272
273	case FHT_BSD44:
274	    fsidp->Fsid_dev.Minor = fhp[0];
275	    fsidp->Fsid_dev.Major = fhp[1];
276	    fsidp->fsid_code = 0;
277
278	    *inop = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
279
280	    if (osnamep)
281		*osnamep = "BSD 4.4";
282	    break;
283
284	case FHT_DECOSF:
285	    fsidp->fsid_code = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
286			/* XXX could ignore 3 high-order bytes */
287
288	    temp = make_uint32(fhp[3], fhp[2], fhp[1], fhp[0]);
289	    fsidp->Fsid_dev.Minor = temp & 0xFFFFF;
290	    fsidp->Fsid_dev.Major = (temp>>20) & 0xFFF;
291
292	    *inop = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
293	    if (osnamep)
294		*osnamep = "OSF";
295	    break;
296
297	case FHT_IRIX4:
298	    fsidp->Fsid_dev.Minor = fhp[3];
299	    fsidp->Fsid_dev.Major = fhp[2];
300	    fsidp->fsid_code = 0;
301
302	    *inop = make_uint32(fhp[8], fhp[9], fhp[10], fhp[11]);
303
304	    if (osnamep)
305		*osnamep = "IRIX4";
306	    break;
307
308	case FHT_IRIX5:
309	    fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
310	    fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
311	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
312
313	    *inop = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
314
315	    if (osnamep)
316		*osnamep = "IRIX5";
317	    break;
318
319#ifdef notdef
320	case FHT_SUNOS3:
321	    /*
322	     * XXX - none of the heuristics above return this.
323	     * Are there any SunOS 3.x systems around to care about?
324	     */
325	    if (osnamep)
326		*osnamep = "SUNOS3";
327	    break;
328#endif
329
330	case FHT_SUNOS4:
331	    fsidp->Fsid_dev.Minor = fhp[3];
332	    fsidp->Fsid_dev.Major = fhp[2];
333	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
334
335	    *inop = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
336
337	    if (osnamep)
338		*osnamep = "SUNOS4";
339	    break;
340
341	case FHT_SUNOS5:
342	    temp = make_uint16(fhp[0], fhp[1]);
343	    fsidp->Fsid_dev.Major = (temp>>2) &  0x3FFF;
344	    temp = make_uint24(fhp[1], fhp[2], fhp[3]);
345	    fsidp->Fsid_dev.Minor = temp & 0x3FFFF;
346	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
347
348	    *inop = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
349
350	    if (osnamep)
351		*osnamep = "SUNOS5";
352	    break;
353
354	case FHT_ULTRIX:
355	    fsidp->fsid_code = 0;
356	    fsidp->Fsid_dev.Minor = fhp[0];
357	    fsidp->Fsid_dev.Major = fhp[1];
358
359	    temp = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
360	    *inop = temp;
361	    if (osnamep)
362		*osnamep = "Ultrix";
363	    break;
364
365	case FHT_VMSUCX:
366	    /* No numeric file system ID, so hash on the device-name */
367	    if (sizeof(*fsidp) >= 14) {
368		if (sizeof(*fsidp) > 14)
369		    memset((char *)fsidp, 0, sizeof(*fsidp));
370		/* just use the whole thing */
371		memcpy((char *)fsidp, (const char *)fh, 14);
372	    }
373	    else {
374		uint32_t tempa[4];	/* at least 16 bytes, maybe more */
375
376		memset((char *)tempa, 0, sizeof(tempa));
377		memcpy((char *)tempa, (const char *)fh, 14); /* ensure alignment */
378		fsidp->Fsid_dev.Minor = tempa[0] + (tempa[1]<<1);
379		fsidp->Fsid_dev.Major = tempa[2] + (tempa[3]<<1);
380		fsidp->fsid_code = 0;
381	    }
382
383	    /* VMS file ID is: (RVN, FidHi, FidLo) */
384	    *inop = make_uint32(fhp[26], fhp[27], fhp[23], fhp[22]);
385
386	    /* Caller must save (and null-terminate?) this value */
387	    if (fsnamep)
388		*fsnamep = (const char *)&(fhp[1]);
389
390	    if (osnamep)
391		*osnamep = "VMS";
392	    break;
393
394	case FHT_AIX32:
395	    fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
396	    fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
397	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
398
399	    *inop = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
400
401	    if (osnamep)
402		*osnamep = "AIX32";
403	    break;
404
405	case FHT_HPUX9:
406	    fsidp->Fsid_dev.Major = fhp[0];
407	    temp = make_uint24(fhp[1], fhp[2], fhp[3]);
408	    fsidp->Fsid_dev.Minor = temp;
409	    fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
410
411	    *inop = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
412
413	    if (osnamep)
414		*osnamep = "HPUX9";
415	    break;
416
417	case FHT_UNKNOWN:
418#ifdef DEBUG
419	    /* XXX debugging */
420	    for (i = 0; i < len*4; i++)
421		(void)fprintf(stderr, "%x.", fhp[i]);
422	    (void)fprintf(stderr, "\n");
423#endif
424	    /* Save the actual handle, so it can be display with -u */
425	    for (i = 0; i < len*4 && i*2 < sizeof(fsidp->Opaque_Handle) - 1; i++)
426	    	(void)snprintf(&(fsidp->Opaque_Handle[i*2]), 3, "%.2X", fhp[i]);
427	    fsidp->Opaque_Handle[i*2] = '\0';
428
429	    /* XXX for now, give "bogus" values to aid debugging */
430	    fsidp->fsid_code = 0;
431	    fsidp->Fsid_dev.Minor = 257;
432	    fsidp->Fsid_dev.Major = 257;
433	    *inop = 1;
434
435	    /* display will show this string instead of (257,257) */
436	    if (fsnamep)
437		*fsnamep = "Unknown";
438
439	    if (osnamep)
440		*osnamep = "Unknown";
441	    break;
442
443	}
444}
445
446/*
447 * Is this a VMS UCX file handle?
448 *	Check for:
449 *	(1) leading code byte	[XXX not yet]
450 *	(2) followed by string of printing chars & spaces
451 *	(3) followed by string of nulls
452 */
453static int
454is_UCX(const unsigned char *fhp, u_int len)
455{
456	register u_int i;
457	int seen_null = 0;
458
459	/*
460	 * Require at least 28 bytes of file handle; it's variable-length
461	 * in NFSv3.  "len" is in units of 32-bit words, not bytes.
462	 */
463	if (len < 28/4)
464		return(0);
465
466	for (i = 1; i < 14; i++) {
467	    if (ND_ISPRINT(fhp[i])) {
468		if (seen_null)
469		   return(0);
470		else
471		   continue;
472	    }
473	    else if (fhp[i] == 0) {
474		seen_null = 1;
475		continue;
476	    }
477	    else
478		return(0);
479	}
480
481	return(1);
482}
483