fsirand.c revision 37275
1/*	$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $	*/
2
3/*
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Todd C. Miller.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
24 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char rcsid[] =
35	"$Id$";
36#endif /* not lint */
37
38#include <sys/types.h>
39#include <sys/disklabel.h>
40#include <sys/param.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43
44#include <ufs/ffs/fs.h>
45#include <ufs/ufs/dinode.h>
46
47#include <err.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54
55static void usage __P((void));
56int fsirand __P((char *));
57
58int printonly = 0, force = 0, ignorelabel = 0;
59
60int
61main(argc, argv)
62	int	argc;
63	char	*argv[];
64{
65	int n, ex = 0;
66	struct rlimit rl;
67
68	while ((n = getopt(argc, argv, "bfp")) != -1) {
69		switch (n) {
70		case 'b':
71			ignorelabel++;
72			break;
73		case 'p':
74			printonly++;
75			break;
76		case 'f':
77			force++;
78			break;
79		default:
80			usage();
81		}
82	}
83	if (argc - optind < 1)
84		usage();
85
86	srandomdev();
87
88	/* Increase our data size to the max */
89	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
90		rl.rlim_cur = rl.rlim_max;
91		if (setrlimit(RLIMIT_DATA, &rl) < 0)
92			warn("can't get resource limit to max data size");
93	} else
94		warn("can't get resource limit for data size");
95
96	for (n = optind; n < argc; n++) {
97		if (argc - optind != 1)
98			(void)puts(argv[n]);
99		ex += fsirand(argv[n]);
100		if (n < argc - 1)
101			putchar('\n');
102	}
103
104	exit(ex);
105}
106
107int
108fsirand(device)
109	char *device;
110{
111	static struct dinode *inodebuf;
112	static size_t oldibufsize;
113	size_t ibufsize;
114	struct fs *sblock;
115	ino_t inumber, maxino;
116	daddr_t dblk;
117	char sbuf[SBSIZE], sbuftmp[SBSIZE];
118	int devfd, n, cg;
119	u_int32_t bsize = DEV_BSIZE;
120	struct disklabel label;
121
122	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
123		warn("can't open %s", device);
124		return (1);
125	}
126
127	/* Get block size (usually 512) from disklabel if possible */
128	if (!ignorelabel) {
129		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
130			warn("can't read disklabel, using sector size of %d",
131			    bsize);
132		else
133			bsize = label.d_secsize;
134	}
135
136	/* Read in master superblock */
137	(void)memset(&sbuf, 0, sizeof(sbuf));
138	sblock = (struct fs *)&sbuf;
139	if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
140		warn("can't seek to superblock (%qd) on %s", SBOFF, device);
141		return (1);
142	}
143	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
144		warnx("can't read superblock on %s: %s", device,
145		    (n < SBSIZE) ? "short read" : strerror(errno));
146		return (1);
147	}
148	maxino = sblock->fs_ncg * sblock->fs_ipg;
149
150	/* Simple sanity checks on the superblock */
151	if (sblock->fs_magic != FS_MAGIC) {
152		warnx("bad magic number in superblock");
153		return (1);
154	}
155	if (sblock->fs_sbsize > SBSIZE) {
156		warnx("superblock size is preposterous");
157		return (1);
158	}
159	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
160		warnx("filesystem format is too old, sorry");
161		return (1);
162	}
163	if (!force && !printonly && sblock->fs_clean != 1) {
164		warnx("filesystem is not clean, fsck %s first", device);
165		return (1);
166	}
167
168	/* Make sure backup superblocks are sane. */
169	sblock = (struct fs *)&sbuftmp;
170	for (cg = 0; cg < sblock->fs_ncg; cg++) {
171		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
172		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
173			warn("can't seek to %qd", (off_t)dblk * bsize);
174			return (1);
175		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
176			warn("can't read backup superblock %d on %s: %s",
177			    cg + 1, device, (n < SBSIZE) ? "short write"
178			    : strerror(errno));
179			return (1);
180		}
181		if (sblock->fs_magic != FS_MAGIC) {
182			warnx("bad magic number in backup superblock %d on %s",
183			    cg + 1, device);
184			return (1);
185		}
186		if (sblock->fs_sbsize > SBSIZE) {
187			warnx("size of backup superblock %d on %s is preposterous",
188			    cg + 1, device);
189			return (1);
190		}
191	}
192	sblock = (struct fs *)&sbuf;
193
194	/* XXX - should really cap buffer at 512kb or so */
195	ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
196	if (oldibufsize < ibufsize) {
197		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
198			errx(1, "can't allocate memory for inode buffer");
199		oldibufsize = ibufsize;
200	}
201
202	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
203		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
204			(void)printf("%s was randomized on %s", device,
205			    ctime((const time_t *)&(sblock->fs_id[0])));
206		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
207			    sblock->fs_id[1]);
208	}
209
210	/* Randomize fs_id unless old 4.2BSD filesystem */
211	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
212		/* Randomize fs_id and write out new sblock and backups */
213		sblock->fs_id[0] = (u_int32_t)time(NULL);
214		sblock->fs_id[1] = random();
215
216		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
217			warn("can't seek to superblock (%qd) on %s", SBOFF,
218			    device);
219			return (1);
220		}
221		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
222			warn("can't read superblock on %s: %s", device,
223			    (n < SBSIZE) ? "short write" : strerror(errno));
224			return (1);
225		}
226	}
227
228	/* For each cylinder group, randomize inodes and update backup sblock */
229	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
230		/* Update superblock if appropriate */
231		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
232			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
233			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
234				warn("can't seek to %qd", (off_t)dblk * bsize);
235				return (1);
236			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
237				warn("can't read backup superblock %d on %s: %s",
238				    cg + 1, device, (n < SBSIZE) ? "short write"
239				    : strerror(errno));
240				return (1);
241			}
242		}
243
244		/* Read in inodes, then print or randomize generation nums */
245		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
246		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
247			warn("can't seek to %qd", (off_t)dblk * bsize);
248			return (1);
249		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
250			warnx("can't read inodes: %s",
251			     (n < ibufsize) ? "short read" : strerror(errno));
252			return (1);
253		}
254
255		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
256			if (inumber >= ROOTINO) {
257				if (printonly)
258					(void)printf("ino %d gen %x\n", inumber,
259						     inodebuf[n].di_gen);
260				else
261					inodebuf[n].di_gen = random();
262			}
263		}
264
265		/* Write out modified inodes */
266		if (!printonly) {
267			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
268				warn("can't seek to %qd",
269				    (off_t)dblk * bsize);
270				return (1);
271			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
272				 ibufsize) {
273				warnx("can't write inodes: %s",
274				     (n != ibufsize) ? "short write" :
275				     strerror(errno));
276				return (1);
277			}
278		}
279	}
280	(void)close(devfd);
281
282	return(0);
283}
284
285static void
286usage()
287{
288	(void)fprintf(stderr,
289		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
290	exit(1);
291}
292