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