xref: /onnv-gate/usr/src/cmd/fs.d/ufs/fsirand/fsirand.c (revision 757:f4be52b91377)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23634Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
38634Sdp  * fsirand installs random inode generation numbers on all the inodes on
39634Sdp  * device <special>, and also installs a file system ID in the superblock.
40634Sdp  * This helps increase the security of  file systems exported by NFS.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <fcntl.h>
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <errno.h>
46634Sdp #include <strings.h>
47634Sdp #include <unistd.h>
48634Sdp #include <stdlib.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <sys/types.h>
510Sstevel@tonic-gate #include <sys/time.h>
520Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
530Sstevel@tonic-gate #include <sys/vnode.h>
540Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
550Sstevel@tonic-gate 
56634Sdp long fsbuf[(SBSIZE / sizeof (long))];
570Sstevel@tonic-gate struct dinode dibuf[8192/sizeof (struct dinode)];
580Sstevel@tonic-gate 
59634Sdp int
main(int argc,char * argv[])60634Sdp main(int argc, char *argv[])
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	struct fs *fs;
630Sstevel@tonic-gate 	int fd;
640Sstevel@tonic-gate 	char *dev;
650Sstevel@tonic-gate 	int bno;
660Sstevel@tonic-gate 	struct dinode *dip;
670Sstevel@tonic-gate 	int inum, imax;
680Sstevel@tonic-gate 	int i, n;
690Sstevel@tonic-gate 	offset_t seekaddr;
700Sstevel@tonic-gate 	int bsize;
710Sstevel@tonic-gate 	int pflag = 0;
720Sstevel@tonic-gate 	struct timeval timeval;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	argv++;
750Sstevel@tonic-gate 	argc--;
760Sstevel@tonic-gate 	if (argc > 0 && strcmp(*argv, "-p") == 0) {
770Sstevel@tonic-gate 		pflag++;
780Sstevel@tonic-gate 		argv++;
790Sstevel@tonic-gate 		argc--;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	if (argc <= 0) {
820Sstevel@tonic-gate 		(void) fprintf(stderr, "Usage: fsirand [-p] special\n");
830Sstevel@tonic-gate 		exit(1);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	dev = *argv;
860Sstevel@tonic-gate 	fd = open64(dev, pflag ? O_RDONLY : O_RDWR);
870Sstevel@tonic-gate 	if (fd == -1) {
880Sstevel@tonic-gate 		(void) fprintf(stderr, "fsirand: Cannot open %s: %s\n", dev,
890Sstevel@tonic-gate 		    strerror(errno));
900Sstevel@tonic-gate 		exit(1);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1) {
930Sstevel@tonic-gate 		(void) fprintf(stderr,
940Sstevel@tonic-gate 		    "fsirand: Seek to superblock failed: %s\n",
950Sstevel@tonic-gate 		    strerror(errno));
960Sstevel@tonic-gate 		exit(1);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	fs = (struct fs *)fsbuf;
990Sstevel@tonic-gate 	if ((n = read(fd, (char *)fs, SBSIZE)) != SBSIZE) {
1000Sstevel@tonic-gate 		(void) fprintf(stderr,
1010Sstevel@tonic-gate 		    "fsirand: Read of superblock failed: %s\n",
1020Sstevel@tonic-gate 		    n == -1 ? strerror(errno) : "Short read");
1030Sstevel@tonic-gate 		exit(1);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	if ((fs->fs_magic != FS_MAGIC) &&
1060Sstevel@tonic-gate 	    (fs->fs_magic != MTB_UFS_MAGIC)) {
1070Sstevel@tonic-gate 		(void) fprintf(stderr,
1080Sstevel@tonic-gate 	"fsirand: Not a file system (bad magic number in superblock)\n");
1090Sstevel@tonic-gate 		exit(1);
1100Sstevel@tonic-gate 	}
111*757Svsakar 	if (fs->fs_magic == FS_MAGIC &&
112*757Svsakar 	    (fs->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
113*757Svsakar 	    fs->fs_version != UFS_VERSION_MIN)) {
114*757Svsakar 		(void) fprintf(stderr,
115*757Svsakar 	"fsirand: Unrecognized UFS format version number %d (in superblock)\n",
116*757Svsakar 		    fs->fs_version);
117*757Svsakar 		exit(1);
118*757Svsakar 	}
1190Sstevel@tonic-gate 	if (fs->fs_magic == MTB_UFS_MAGIC &&
1200Sstevel@tonic-gate 	    (fs->fs_version > MTB_UFS_VERSION_1 ||
1210Sstevel@tonic-gate 	    fs->fs_version < MTB_UFS_VERSION_MIN)) {
1220Sstevel@tonic-gate 		(void) fprintf(stderr,
1230Sstevel@tonic-gate 	"fsirand: Unrecognized UFS format version number %d (in superblock)\n",
1240Sstevel@tonic-gate 		    fs->fs_version);
1250Sstevel@tonic-gate 		exit(1);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	if (pflag) {
1280Sstevel@tonic-gate 		(void) printf("fsid: %x %x\n", fs->fs_id[0], fs->fs_id[1]);
1290Sstevel@tonic-gate 	} else {
1300Sstevel@tonic-gate 		n = getpid();
1310Sstevel@tonic-gate 		(void) gettimeofday(&timeval, (struct timezone *)NULL);
1320Sstevel@tonic-gate 		srand48((long)(timeval.tv_sec + timeval.tv_usec + n));
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 	bsize = INOPB(fs) * sizeof (struct dinode);
1350Sstevel@tonic-gate 	inum = 0;
1360Sstevel@tonic-gate 	imax = fs->fs_ipg * fs->fs_ncg;
1370Sstevel@tonic-gate 	while (inum < imax) {
1380Sstevel@tonic-gate 		bno = itod(fs, inum);
1390Sstevel@tonic-gate 		seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE;
1400Sstevel@tonic-gate 		if (llseek(fd, seekaddr, 0) == -1) {
1410Sstevel@tonic-gate 			(void) fprintf(stderr,
1420Sstevel@tonic-gate 			    "fsirand: Seek to %ld %ld failed: %s\n",
1430Sstevel@tonic-gate 			    ((off_t *)&seekaddr)[0], ((off_t *)&seekaddr)[1],
1440Sstevel@tonic-gate 			    strerror(errno));
1450Sstevel@tonic-gate 			exit(1);
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 		n = read(fd, (char *)dibuf, bsize);
1480Sstevel@tonic-gate 		if (n != bsize) {
1490Sstevel@tonic-gate 			(void) fprintf(stderr,
1500Sstevel@tonic-gate 			    "fsirand: Read of ilist block failed: %s\n",
1510Sstevel@tonic-gate 			    n == -1 ? strerror(errno) : "Short read");
1520Sstevel@tonic-gate 			exit(1);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) {
1550Sstevel@tonic-gate 			if (pflag) {
1560Sstevel@tonic-gate 				(void) printf("ino %d gen %x\n", inum,
1570Sstevel@tonic-gate 				    dip->di_gen);
1580Sstevel@tonic-gate 			} else {
1590Sstevel@tonic-gate 				dip->di_gen = lrand48();
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 			inum++;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 		if (!pflag) {
1640Sstevel@tonic-gate 			if (llseek(fd, seekaddr, 0) == -1) {
1650Sstevel@tonic-gate 				(void) fprintf(stderr,
1660Sstevel@tonic-gate 				    "fsirand: Seek to %ld %ld failed: %s\n",
1670Sstevel@tonic-gate 				    ((off_t *)&seekaddr)[0],
1680Sstevel@tonic-gate 				    ((off_t *)&seekaddr)[1],
169634Sdp 				    strerror(errno));
1700Sstevel@tonic-gate 				exit(1);
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			n = write(fd, (char *)dibuf, bsize);
1730Sstevel@tonic-gate 			if (n != bsize) {
1740Sstevel@tonic-gate 				(void) fprintf(stderr,
1750Sstevel@tonic-gate 				"fsirand: Write of ilist block failed: %s\n",
1760Sstevel@tonic-gate 				    n == -1 ? strerror(errno) : "Short write");
1770Sstevel@tonic-gate 				exit(1);
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	if (!pflag) {
1820Sstevel@tonic-gate 		(void) gettimeofday(&timeval, (struct timezone *)NULL);
1830Sstevel@tonic-gate 		fs->fs_id[0] = timeval.tv_sec;
1840Sstevel@tonic-gate 		fs->fs_id[1] = timeval.tv_usec + getpid();
1850Sstevel@tonic-gate 		if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1) {
1860Sstevel@tonic-gate 			(void) fprintf(stderr,
1870Sstevel@tonic-gate 			    "fsirand: Seek to superblock failed: %s\n",
1880Sstevel@tonic-gate 			    strerror(errno));
1890Sstevel@tonic-gate 			exit(1);
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 		if ((n = write(fd, (char *)fs, SBSIZE)) != SBSIZE) {
1920Sstevel@tonic-gate 			(void) fprintf(stderr,
1930Sstevel@tonic-gate 			    "fsirand: Write of superblock failed: %s\n",
1940Sstevel@tonic-gate 			    n == -1 ? strerror(errno) : "Short write");
1950Sstevel@tonic-gate 			exit(1);
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	for (i = 0; i < fs->fs_ncg; i++) {
1990Sstevel@tonic-gate 		seekaddr = (offset_t)fsbtodb(fs, cgsblock(fs, i)) * DEV_BSIZE;
2000Sstevel@tonic-gate 		if (llseek(fd,  seekaddr, 0) == -1) {
2010Sstevel@tonic-gate 			(void) fprintf(stderr,
2020Sstevel@tonic-gate 			"fsirand: Seek to alternate superblock failed: %s\n",
2030Sstevel@tonic-gate 			    strerror(errno));
2040Sstevel@tonic-gate 			exit(1);
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		if (pflag) {
2070Sstevel@tonic-gate 			if ((n = read(fd, (char *)fs, SBSIZE)) != SBSIZE) {
2080Sstevel@tonic-gate 				(void) fprintf(stderr,
2090Sstevel@tonic-gate 			"fsirand: Read of alternate superblock failed: %s\n",
2100Sstevel@tonic-gate 				    n == -1 ? strerror(errno) : "Short read");
2110Sstevel@tonic-gate 				exit(1);
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 			if ((fs->fs_magic != FS_MAGIC) &&
2140Sstevel@tonic-gate 			    (fs->fs_magic != MTB_UFS_MAGIC)) {
2150Sstevel@tonic-gate 				(void) fprintf(stderr,
2160Sstevel@tonic-gate 				    "fsirand: Not a valid file system (bad "
2170Sstevel@tonic-gate 				    "magic number in alternate superblock)\n");
2180Sstevel@tonic-gate 				exit(1);
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			if ((n = write(fd, (char *)fs, SBSIZE)) != SBSIZE) {
2220Sstevel@tonic-gate 				(void) fprintf(stderr,
2230Sstevel@tonic-gate 			"fsirand: Write of alternate superblock failed: %s\n",
2240Sstevel@tonic-gate 				    n == -1 ? strerror(errno) : "Short write");
2250Sstevel@tonic-gate 				exit(1);
2260Sstevel@tonic-gate 			}
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 	}
229634Sdp 	return (0);
2300Sstevel@tonic-gate }
231