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  "$FreeBSD$";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/disklabel.h>
40#include <sys/resource.h>
41
42#include <ufs/ufs/dinode.h>
43#include <ufs/ffs/fs.h>
44
45#include <err.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <stdio.h>
49#include <stdint.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include <unistd.h>
54
55static void usage(void) __dead2;
56int fsirand(char *);
57
58/*
59 * Possible superblock locations ordered from most to least likely.
60 */
61static int sblock_try[] = SBLOCKSEARCH;
62
63static int printonly = 0, force = 0, ignorelabel = 0;
64
65int
66main(int argc, char *argv[])
67{
68	int n, ex = 0;
69	struct rlimit rl;
70
71	while ((n = getopt(argc, argv, "bfp")) != -1) {
72		switch (n) {
73		case 'b':
74			ignorelabel++;
75			break;
76		case 'p':
77			printonly++;
78			break;
79		case 'f':
80			force++;
81			break;
82		default:
83			usage();
84		}
85	}
86	if (argc - optind < 1)
87		usage();
88
89	srandomdev();
90
91	/* Increase our data size to the max */
92	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
93		rl.rlim_cur = rl.rlim_max;
94		if (setrlimit(RLIMIT_DATA, &rl) < 0)
95			warn("can't get resource limit to max data size");
96	} else
97		warn("can't get resource limit for data size");
98
99	for (n = optind; n < argc; n++) {
100		if (argc - optind != 1)
101			(void)puts(argv[n]);
102		ex += fsirand(argv[n]);
103		if (n < argc - 1)
104			putchar('\n');
105	}
106
107	exit(ex);
108}
109
110int
111fsirand(char *device)
112{
113	struct ufs1_dinode *dp1;
114	struct ufs2_dinode *dp2;
115	caddr_t inodebuf;
116	ssize_t ibufsize;
117	struct fs *sblock;
118	ino_t inumber;
119	ufs2_daddr_t sblockloc, dblk;
120	char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
121	int i, devfd, n, cg;
122	u_int32_t bsize = DEV_BSIZE;
123	struct disklabel label;
124
125	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
126		warn("can't open %s", device);
127		return (1);
128	}
129
130	/* Get block size (usually 512) from disklabel if possible */
131	if (!ignorelabel) {
132		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
133			warn("can't read disklabel, using sector size of %d",
134			    bsize);
135		else
136			bsize = label.d_secsize;
137	}
138
139	dp1 = NULL;
140	dp2 = NULL;
141
142	/* Read in master superblock */
143	(void)memset(&sbuf, 0, sizeof(sbuf));
144	sblock = (struct fs *)&sbuf;
145	for (i = 0; sblock_try[i] != -1; i++) {
146		sblockloc = sblock_try[i];
147		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
148			warn("can't seek to superblock (%jd) on %s",
149			    (intmax_t)sblockloc, device);
150			return (1);
151		}
152		if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) {
153			warnx("can't read superblock on %s: %s", device,
154			    (n < SBLOCKSIZE) ? "short read" : strerror(errno));
155			return (1);
156		}
157		if ((sblock->fs_magic == FS_UFS1_MAGIC ||
158		     (sblock->fs_magic == FS_UFS2_MAGIC &&
159		      sblock->fs_sblockloc == sblock_try[i])) &&
160		    sblock->fs_bsize <= MAXBSIZE &&
161		    sblock->fs_bsize >= (ssize_t)sizeof(struct fs))
162			break;
163	}
164	if (sblock_try[i] == -1) {
165		fprintf(stderr, "Cannot find file system superblock\n");
166		return (1);
167	}
168
169	if (sblock->fs_magic == FS_UFS1_MAGIC &&
170	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
171		warnx("file system format is too old, sorry");
172		return (1);
173	}
174	if (!force && !printonly && sblock->fs_clean != 1) {
175		warnx("file system is not clean, fsck %s first", device);
176		return (1);
177	}
178
179	/* Make sure backup superblocks are sane. */
180	sblock = (struct fs *)&sbuftmp;
181	for (cg = 0; cg < (int)sblock->fs_ncg; cg++) {
182		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
183		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
184			warn("can't seek to %jd", (intmax_t)dblk * bsize);
185			return (1);
186		} else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) {
187			warn("can't read backup superblock %d on %s: %s",
188			    cg + 1, device, (n < SBLOCKSIZE) ? "short write"
189			    : strerror(errno));
190			return (1);
191		}
192		if (sblock->fs_magic != FS_UFS1_MAGIC &&
193		    sblock->fs_magic != FS_UFS2_MAGIC) {
194			warnx("bad magic number in backup superblock %d on %s",
195			    cg + 1, device);
196			return (1);
197		}
198		if (sblock->fs_sbsize > SBLOCKSIZE) {
199			warnx("size of backup superblock %d on %s is preposterous",
200			    cg + 1, device);
201			return (1);
202		}
203	}
204	sblock = (struct fs *)&sbuf;
205
206	/* XXX - should really cap buffer at 512kb or so */
207	if (sblock->fs_magic == FS_UFS1_MAGIC)
208		ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
209	else
210		ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
211	if ((inodebuf = malloc(ibufsize)) == NULL)
212		errx(1, "can't allocate memory for inode buffer");
213
214	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
215		if (sblock->fs_id[0])
216			(void)printf("%s was randomized on %s", device,
217			    ctime((void *)&(sblock->fs_id[0])));
218		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
219			    sblock->fs_id[1]);
220	}
221
222	/* Randomize fs_id unless old 4.2BSD file system */
223	if (!printonly) {
224		/* Randomize fs_id and write out new sblock and backups */
225		sblock->fs_id[0] = (u_int32_t)time(NULL);
226		sblock->fs_id[1] = random();
227
228		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
229			warn("can't seek to superblock (%jd) on %s",
230			    (intmax_t)sblockloc, device);
231			return (1);
232		}
233		if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
234		    SBLOCKSIZE) {
235			warn("can't write superblock on %s: %s", device,
236			    (n < SBLOCKSIZE) ? "short write" : strerror(errno));
237			return (1);
238		}
239	}
240
241	/* For each cylinder group, randomize inodes and update backup sblock */
242	for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) {
243		/* Update superblock if appropriate */
244		if (!printonly) {
245			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
246			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
247				warn("can't seek to %jd",
248				    (intmax_t)dblk * bsize);
249				return (1);
250			} else if ((n = write(devfd, (void *)sblock,
251			    SBLOCKSIZE)) != SBLOCKSIZE) {
252			      warn("can't write backup superblock %d on %s: %s",
253				    cg + 1, device, (n < SBLOCKSIZE) ?
254				    "short write" : strerror(errno));
255				return (1);
256			}
257		}
258
259		/* Read in inodes, then print or randomize generation nums */
260		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
261		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
262			warn("can't seek to %jd", (intmax_t)dblk * bsize);
263			return (1);
264		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
265			warnx("can't read inodes: %s",
266			     (n < ibufsize) ? "short read" : strerror(errno));
267			return (1);
268		}
269
270		for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) {
271			if (sblock->fs_magic == FS_UFS1_MAGIC)
272				dp1 = &((struct ufs1_dinode *)inodebuf)[n];
273			else
274				dp2 = &((struct ufs2_dinode *)inodebuf)[n];
275			if (inumber >= ROOTINO) {
276				if (printonly)
277					(void)printf("ino %ju gen %08x\n",
278					    (uintmax_t)inumber,
279					    sblock->fs_magic == FS_UFS1_MAGIC ?
280					    dp1->di_gen : dp2->di_gen);
281				else if (sblock->fs_magic == FS_UFS1_MAGIC)
282					dp1->di_gen = random();
283				else
284					dp2->di_gen = random();
285			}
286		}
287
288		/* Write out modified inodes */
289		if (!printonly) {
290			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
291				warn("can't seek to %jd",
292				    (intmax_t)dblk * bsize);
293				return (1);
294			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
295				 ibufsize) {
296				warnx("can't write inodes: %s",
297				     (n != ibufsize) ? "short write" :
298				     strerror(errno));
299				return (1);
300			}
301		}
302	}
303	(void)close(devfd);
304
305	return(0);
306}
307
308static void
309usage(void)
310{
311	(void)fprintf(stderr,
312		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
313	exit(1);
314}
315