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