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