1/*
2 * Copyright (c) 1980, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if 0
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1990, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)quotaon.c	8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD$");
46
47/*
48 * Turn quota on/off for a filesystem.
49 */
50#include <sys/param.h>
51#include <sys/file.h>
52#include <sys/mount.h>
53#include <ufs/ufs/quota.h>
54#include <err.h>
55#include <fstab.h>
56#include <libutil.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62static const char *qfextension[] = INITQFNAMES;
63
64static int	aflag;		/* all filesystems */
65static int	gflag;		/* operate on group quotas */
66static int	uflag;		/* operate on user quotas */
67static int	vflag;		/* verbose */
68
69static int oneof(char *, char *[], int);
70static int quotaonoff(struct fstab *fs, int, int);
71static void usage(void);
72
73int
74main(int argc, char **argv)
75{
76	struct fstab *fs;
77	const char *whoami;
78	long argnum, done = 0;
79	int ch, i, offmode = 0, errs = 0;
80
81	whoami = getprogname();
82	if (strcmp(whoami, "quotaoff") == 0)
83		offmode++;
84	else if (strcmp(whoami, "quotaon") != 0)
85		errx(1, "name must be quotaon or quotaoff");
86	while ((ch = getopt(argc, argv, "avug")) != -1) {
87		switch(ch) {
88		case 'a':
89			aflag++;
90			break;
91		case 'g':
92			gflag++;
93			break;
94		case 'u':
95			uflag++;
96			break;
97		case 'v':
98			vflag++;
99			break;
100		default:
101			usage();
102		}
103	}
104	argc -= optind;
105	argv += optind;
106	if (argc <= 0 && !aflag)
107		usage();
108	if (!gflag && !uflag) {
109		gflag++;
110		uflag++;
111	}
112	setfsent();
113	while ((fs = getfsent()) != NULL) {
114		if (strcmp(fs->fs_vfstype, "ufs") ||
115		    strcmp(fs->fs_type, FSTAB_RW))
116			continue;
117		if (aflag) {
118			if (gflag)
119				errs += quotaonoff(fs, offmode, GRPQUOTA);
120			if (uflag)
121				errs += quotaonoff(fs, offmode, USRQUOTA);
122			continue;
123		}
124		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
125		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
126			done |= 1 << argnum;
127			if (gflag)
128				errs += quotaonoff(fs, offmode, GRPQUOTA);
129			if (uflag)
130				errs += quotaonoff(fs, offmode, USRQUOTA);
131		}
132	}
133	endfsent();
134	for (i = 0; i < argc; i++)
135		if ((done & (1 << i)) == 0)
136			warnx("%s not found in fstab", argv[i]);
137	exit(errs);
138}
139
140static void
141usage(void)
142{
143
144	fprintf(stderr, "%s\n%s\n%s\n%s\n",
145		"usage: quotaon [-g] [-u] [-v] -a",
146		"       quotaon [-g] [-u] [-v] filesystem ...",
147		"       quotaoff [-g] [-u] [-v] -a",
148		"       quotaoff [-g] [-u] [-v] filesystem ...");
149	exit(1);
150}
151
152static int
153quotaonoff(struct fstab *fs, int offmode, int type)
154{
155	struct quotafile *qf;
156
157	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL)
158		return (0);
159	if (offmode) {
160		if (quota_off(qf) != 0) {
161			warn("%s", quota_fsname(qf));
162			return (1);
163		}
164		if (vflag)
165			printf("%s: quotas turned off\n", quota_fsname(qf));
166		quota_close(qf);
167		return(0);
168	}
169	if (quota_on(qf) != 0) {
170		warn("using %s on %s", quota_qfname(qf), quota_fsname(qf));
171		return (1);
172	}
173	if (vflag)
174		printf("%s: %s quotas turned on with data file %s\n",
175		    quota_fsname(qf), qfextension[type], quota_qfname(qf));
176	quota_close(qf);
177	return(0);
178}
179
180/*
181 * Check to see if target appears in list of size cnt.
182 */
183static int
184oneof(char *target, char *list[], int cnt)
185{
186	int i;
187
188	for (i = 0; i < cnt; i++)
189		if (strcmp(target, list[i]) == 0)
190			return (i);
191	return (-1);
192}
193