1 /* $OpenBSD: parsenfsfh.c,v 1.15 2023/09/06 05:54:07 jsg 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 #include <sys/types.h>
46 #include <sys/time.h>
47
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <string.h>
51
52 #include "interface.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
78 #define make_uint32(msb,b,c,lsb)\
79 ((lsb) + ((c)<<8) + ((b)<<16) + ((msb)<<24))
80
81 #define make_uint24(msb,b, lsb)\
82 ((lsb) + ((b)<<8) + ((msb)<<16))
83
84 #define make_uint16(msb,lsb)\
85 ((lsb) + ((msb)<<8))
86
87 #ifdef __alpha
88 /* or other 64-bit systems */
89 #define make_uint48(msb,b,c,d,e,lsb)\
90 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
91 #else
92 /* on 32-bit systems ignore high-order bits */
93 #define make_uint48(msb,b,c,d,e,lsb)\
94 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
95 #endif
96
97 static int is_UCX(unsigned char *);
98
99 void
Parse_fh(caddr_t * fh,my_fsid * fsidp,ino_t * inop,char ** osnamep,char ** fsnamep)100 Parse_fh(caddr_t *fh, my_fsid *fsidp, ino_t *inop,
101 char **osnamep, /* if non-NULL, return OS name here */
102 char **fsnamep) /* if non-NULL, return server fs name here (for VMS) */
103 {
104 unsigned char *fhp = (unsigned char *)fh;
105 u_int32_t temp;
106 int fhtype = FHT_UNKNOWN;
107
108 /*
109 * This is basically a big decision tree
110 */
111 if ((fhp[0] == 0) && (fhp[1] == 0)) {
112 /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
113 /* probably rules out HP-UX, AIX unless they allow major=0 */
114 if ((fhp[2] == 0) && (fhp[3] == 0)) {
115 /* bytes[2,3] == (0,0); must be Auspex */
116 /* XXX or could be Ultrix+MASSBUS "hp" disk? */
117 fhtype = FHT_AUSPEX;
118 }
119 else {
120 /*
121 * bytes[2,3] != (0,0); rules out Auspex, could be
122 * DECOSF, SUNOS4, or IRIX4
123 */
124 if ((fhp[4] != 0) && (fhp[5] == 0) &&
125 (fhp[8] == 12) && (fhp[9] == 0)) {
126 /* seems to be DECOSF, with minor == 0 */
127 fhtype = FHT_DECOSF;
128 }
129 else {
130 /* could be SUNOS4 or IRIX4 */
131 /* XXX the test of fhp[5] == 8 could be wrong */
132 if ((fhp[4] == 0) && (fhp[5] == 8) && (fhp[6] == 0) &&
133 (fhp[7] == 0)) {
134 /* looks like a length, not a file system typecode */
135 fhtype = FHT_IRIX4;
136 }
137 else {
138 /* by elimination */
139 fhtype = FHT_SUNOS4;
140 }
141 }
142 }
143 }
144 else {
145 /*
146 * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
147 * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
148 * could be AIX, HP-UX
149 */
150 if ((fhp[2] == 0) && (fhp[3] == 0)) {
151 /*
152 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
153 * (unless the exported device name is just one letter!),
154 * could be Ultrix, IRIX5, AIX, or SUNOS5
155 * might be HP-UX (depends on their values for minor devs)
156 */
157 /*XXX we probably only need to test of these two bytes */
158 if ((fhp[21] == 0) && (fhp[23] == 0)) {
159 fhtype = FHT_ULTRIX;
160 }
161 else {
162 /* Could be SUNOS5/IRIX5, maybe AIX */
163 /* XXX no obvious difference between SUNOS5 and IRIX5 */
164 if (fhp[9] == 10)
165 fhtype = FHT_SUNOS5;
166 /* XXX what about AIX? */
167 }
168 }
169 else {
170 /*
171 * bytes[2,3] != (0,0); rules out Ultrix, could be
172 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
173 */
174 if ((fhp[8] == 12) && (fhp[9] == 0)) {
175 fhtype = FHT_DECOSF;
176 }
177 else if ((fhp[8] == 0) && (fhp[9] == 10)) {
178 /* could be SUNOS5/IRIX5, AIX, HP-UX */
179 if ((fhp[7] == 0) && (fhp[6] == 0) &&
180 (fhp[5] == 0) && (fhp[4] == 0)) {
181 /* XXX is this always true of HP-UX? */
182 fhtype = FHT_HPUX9;
183 }
184 else if (fhp[7] == 2) {
185 /* This would be MNT_NFS on AIX, which is impossible */
186 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
187 }
188 else {
189 /*
190 * XXX Could be SUNOS5/IRIX5 or AIX. I don't
191 * XXX see any way to disambiguate these, so
192 * XXX I'm going with the more likely guess.
193 * XXX Sorry, Big Blue.
194 */
195 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
196 }
197 }
198 else {
199 if (is_UCX(fhp)) {
200 fhtype = FHT_VMSUCX;
201 }
202 else {
203 fhtype = FHT_UNKNOWN;
204 }
205 }
206 }
207 }
208
209 /* XXX still needs to handle SUNOS3 */
210
211 switch (fhtype) {
212 case FHT_AUSPEX:
213 fsidp->Fsid_dev.Minor = fhp[7];
214 fsidp->Fsid_dev.Major = fhp[6];
215 fsidp->fsid_code = 0;
216
217 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
218 *inop = temp;
219
220 if (osnamep)
221 *osnamep = "Auspex";
222 break;
223
224 case FHT_DECOSF:
225 fsidp->fsid_code = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
226 /* XXX could ignore 3 high-order bytes */
227
228 temp = make_uint32(fhp[3], fhp[2], fhp[1], fhp[0]);
229 fsidp->Fsid_dev.Minor = temp & 0xFFFFF;
230 fsidp->Fsid_dev.Major = (temp>>20) & 0xFFF;
231
232 temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
233 *inop = temp;
234 if (osnamep)
235 *osnamep = "OSF";
236 break;
237
238 case FHT_IRIX4:
239 fsidp->Fsid_dev.Minor = fhp[3];
240 fsidp->Fsid_dev.Major = fhp[2];
241 fsidp->fsid_code = 0;
242
243 temp = make_uint32(fhp[8], fhp[9], fhp[10], fhp[11]);
244 *inop = temp;
245
246 if (osnamep)
247 *osnamep = "IRIX4";
248 break;
249
250 case FHT_IRIX5:
251 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
252 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
253 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
254
255 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
256 *inop = temp;
257
258 if (osnamep)
259 *osnamep = "IRIX5";
260 break;
261
262 case FHT_SUNOS3:
263 if (osnamep)
264 *osnamep = "SUNOS3";
265 break;
266
267 case FHT_SUNOS4:
268 fsidp->Fsid_dev.Minor = fhp[3];
269 fsidp->Fsid_dev.Major = fhp[2];
270 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
271
272 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
273 *inop = temp;
274
275 if (osnamep)
276 *osnamep = "SUNOS4";
277 break;
278
279 case FHT_SUNOS5:
280 temp = make_uint16(fhp[0], fhp[1]);
281 fsidp->Fsid_dev.Major = (temp>>2) & 0x3FFF;
282 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
283 fsidp->Fsid_dev.Minor = temp & 0x3FFFF;
284 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
285
286 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
287 *inop = temp;
288
289 if (osnamep)
290 *osnamep = "SUNOS5";
291 break;
292
293 case FHT_ULTRIX:
294 fsidp->fsid_code = 0;
295 fsidp->Fsid_dev.Minor = fhp[0];
296 fsidp->Fsid_dev.Major = fhp[1];
297
298 temp = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
299 *inop = temp;
300 if (osnamep)
301 *osnamep = "Ultrix";
302 break;
303
304 case FHT_VMSUCX:
305 /* No numeric file system ID, so hash on the device-name */
306 if (sizeof(*fsidp) >= 14) {
307 if (sizeof(*fsidp) > 14)
308 memset((char *)fsidp, 0, sizeof(*fsidp));
309 /* just use the whole thing */
310 memcpy((char *)fsidp, (char *)fh, 14);
311 }
312 else {
313 u_int32_t tempa[4]; /* at least 16 bytes, maybe more */
314
315 memset((char *)tempa, 0, sizeof(tempa));
316 memcpy((char *)tempa, (char *)fh, 14); /* ensure alignment */
317 fsidp->Fsid_dev.Minor = tempa[0] + (tempa[1]<<1);
318 fsidp->Fsid_dev.Major = tempa[2] + (tempa[3]<<1);
319 fsidp->fsid_code = 0;
320 }
321
322 /* VMS file ID is: (RVN, FidHi, FidLo) */
323 *inop = make_uint32(fhp[26], fhp[27], fhp[23], fhp[22]);
324
325 /* Caller must save (and null-terminate?) this value */
326 if (fsnamep)
327 *fsnamep = (char *)&(fhp[1]);
328
329 if (osnamep)
330 *osnamep = "VMS";
331 break;
332
333 case FHT_AIX32:
334 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
335 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
336 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
337
338 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
339 *inop = temp;
340
341 if (osnamep)
342 *osnamep = "AIX32";
343 break;
344
345 case FHT_HPUX9:
346 fsidp->Fsid_dev.Major = fhp[0];
347 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
348 fsidp->Fsid_dev.Minor = temp;
349 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
350
351 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
352 *inop = temp;
353
354 if (osnamep)
355 *osnamep = "HPUX9";
356 break;
357
358 case FHT_UNKNOWN:
359 #ifdef DEBUG
360 {
361 int i;
362 for (i = 0; i < 32; i++)
363 (void)fprintf(stderr, "%x.", fhp[i]);
364 (void)fprintf(stderr, "\n");
365 }
366 #endif
367 /* XXX for now, give "bogus" values to aid debugging */
368 fsidp->fsid_code = 0;
369 fsidp->Fsid_dev.Minor = 257;
370 fsidp->Fsid_dev.Major = 257;
371 *inop = 1;
372
373 /* display will show this string instead of (257,257) */
374 if (fsnamep)
375 *fsnamep = "Unknown";
376
377 if (osnamep)
378 *osnamep = "Unknown";
379 break;
380
381 }
382 }
383
384 /*
385 * Is this a VMS UCX file handle?
386 * Check for:
387 * (1) leading code byte [XXX not yet]
388 * (2) followed by string of printing chars & spaces
389 * (3) followed by string of nulls
390 */
391 static int
is_UCX(unsigned char * fhp)392 is_UCX(unsigned char *fhp)
393 {
394 int i;
395 int seen_null = 0;
396
397 for (i = 1; i < 14; i++) {
398 if (isprint(fhp[i])) {
399 if (seen_null)
400 return(0);
401 else
402 continue;
403 }
404 else if (fhp[i] == 0) {
405 seen_null = 1;
406 continue;
407 }
408 else
409 return(0);
410 }
411
412 return(1);
413 }
414