1/*	$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by Todd C. Miller.
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#ifndef lint
36static const char rcsid[] =
37  "$FreeBSD$";
38#endif /* not lint */
39
40#include <sys/param.h>
41#include <sys/resource.h>
42
43#include <ufs/ufs/dinode.h>
44#include <ufs/ffs/fs.h>
45
46#include <err.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <libufs.h>
50#include <stdio.h>
51#include <stdint.h>
52#include <stdlib.h>
53#include <string.h>
54#include <time.h>
55#include <unistd.h>
56
57static void usage(void) __dead2;
58int fsirand(char *);
59
60static int printonly = 0, force = 0, ignorelabel = 0;
61
62int
63main(int argc, 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	/* Increase our data size to the max */
87	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
88		rl.rlim_cur = rl.rlim_max;
89		if (setrlimit(RLIMIT_DATA, &rl) < 0)
90			warn("can't get resource limit to max data size");
91	} else
92		warn("can't get resource limit for data size");
93
94	for (n = optind; n < argc; n++) {
95		if (argc - optind != 1)
96			(void)puts(argv[n]);
97		ex += fsirand(argv[n]);
98		if (n < argc - 1)
99			putchar('\n');
100	}
101
102	exit(ex);
103}
104
105int
106fsirand(char *device)
107{
108	struct ufs1_dinode *dp1;
109	struct ufs2_dinode *dp2;
110	caddr_t inodebuf;
111	ssize_t ibufsize;
112	struct fs *sblock;
113	ino_t inumber;
114	ufs2_daddr_t dblk;
115	int devfd, n, cg, ret;
116	u_int32_t bsize = DEV_BSIZE;
117
118	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
119		warn("can't open %s", device);
120		return (1);
121	}
122
123	dp1 = NULL;
124	dp2 = NULL;
125
126	/* Read in master superblock */
127	if ((ret = sbget(devfd, &sblock, STDSB)) != 0) {
128		switch (ret) {
129		case ENOENT:
130			warn("Cannot find file system superblock");
131			return (1);
132		default:
133			warn("Unable to read file system superblock");
134			return (1);
135		}
136	}
137	/*
138	 * Check for unclean filesystem.
139	 */
140	if (sblock->fs_clean == 0 ||
141	    (sblock->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0)
142		errx(1, "%s is not clean - run fsck.\n", device);
143
144	if (sblock->fs_magic == FS_UFS1_MAGIC &&
145	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
146		warnx("file system format is too old, sorry");
147		return (1);
148	}
149	if (!force && !printonly && sblock->fs_clean != 1) {
150		warnx("file system is not clean, fsck %s first", device);
151		return (1);
152	}
153
154	/* XXX - should really cap buffer at 512kb or so */
155	if (sblock->fs_magic == FS_UFS1_MAGIC)
156		ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
157	else
158		ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
159	if ((inodebuf = malloc(ibufsize)) == NULL)
160		errx(1, "can't allocate memory for inode buffer");
161
162	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
163		if (sblock->fs_id[0])
164			(void)printf("%s was randomized on %s", device,
165			    ctime((void *)&(sblock->fs_id[0])));
166		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
167			    sblock->fs_id[1]);
168	}
169
170	/* Randomize fs_id unless old 4.2BSD file system */
171	if (!printonly) {
172		/* Randomize fs_id and write out new sblock and backups */
173		sblock->fs_id[0] = (u_int32_t)time(NULL);
174		sblock->fs_id[1] = arc4random();
175		if (sbput(devfd, sblock, sblock->fs_ncg) != 0) {
176			warn("could not write updated superblock");
177			return (1);
178		}
179	}
180
181	/* For each cylinder group, randomize inodes and update backup sblock */
182	for (cg = 0, inumber = UFS_ROOTINO; cg < (int)sblock->fs_ncg; cg++) {
183		/* Read in inodes, then print or randomize generation nums */
184		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
185		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
186			warn("can't seek to %jd", (intmax_t)dblk * bsize);
187			return (1);
188		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
189			warnx("can't read inodes: %s",
190			     (n < ibufsize) ? "short read" : strerror(errno));
191			return (1);
192		}
193
194		dp1 = (struct ufs1_dinode *)(void *)inodebuf;
195		dp2 = (struct ufs2_dinode *)(void *)inodebuf;
196		for (n = cg > 0 ? 0 : UFS_ROOTINO;
197		     n < (int)sblock->fs_ipg;
198		     n++, inumber++) {
199			if (printonly) {
200				(void)printf("ino %ju gen %08x\n",
201				    (uintmax_t)inumber,
202				    sblock->fs_magic == FS_UFS1_MAGIC ?
203				    dp1->di_gen : dp2->di_gen);
204			} else if (sblock->fs_magic == FS_UFS1_MAGIC) {
205				dp1->di_gen = arc4random();
206				dp1++;
207			} else {
208				dp2->di_gen = arc4random();
209				ffs_update_dinode_ckhash(sblock, dp2);
210				dp2++;
211			}
212		}
213
214		/* Write out modified inodes */
215		if (!printonly) {
216			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
217				warn("can't seek to %jd",
218				    (intmax_t)dblk * bsize);
219				return (1);
220			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
221				 ibufsize) {
222				warnx("can't write inodes: %s",
223				     (n != ibufsize) ? "short write" :
224				     strerror(errno));
225				return (1);
226			}
227		}
228	}
229	(void)close(devfd);
230
231	return(0);
232}
233
234static void
235usage(void)
236{
237	(void)fprintf(stderr,
238		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
239	exit(1);
240}
241