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