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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved. 37 * @(#)quotaon.c 8.1 (Berkeley) 6/6/93 38 * $FreeBSD: src/usr.sbin/quotaon/quotaon.c,v 1.4.2.1 2001/07/19 05:17:06 kris Exp $ 39 * $DragonFly: src/usr.sbin/quotaon/quotaon.c,v 1.2 2003/06/17 04:30:02 dillon Exp $ 40 */ 41 42 /* 43 * Turn quota on/off for a filesystem. 44 */ 45 #include <sys/param.h> 46 #include <sys/file.h> 47 #include <sys/mount.h> 48 #include <ufs/ufs/quota.h> 49 #include <err.h> 50 #include <fstab.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 char *qfname = QUOTAFILENAME; 57 char *qfextension[] = INITQFNAMES; 58 59 int aflag; /* all file systems */ 60 int gflag; /* operate on group quotas */ 61 int uflag; /* operate on user quotas */ 62 int vflag; /* verbose */ 63 64 int hasquota __P((struct fstab *, int, char **)); 65 int oneof __P((char *, char *[], int)); 66 int quotaonoff __P((struct fstab *fs, int, int, char *)); 67 int readonly __P((struct fstab *)); 68 static void usage __P((void)); 69 70 int 71 main(argc, argv) 72 int argc; 73 char **argv; 74 { 75 register struct fstab *fs; 76 char ch, *qfnp, *whoami; 77 long argnum, done = 0; 78 int i, offmode = 0, errs = 0; 79 80 whoami = rindex(*argv, '/') + 1; 81 if (whoami == (char *)1) 82 whoami = *argv; 83 if (strcmp(whoami, "quotaoff") == 0) 84 offmode++; 85 else if (strcmp(whoami, "quotaon") != 0) 86 errx(1, "name must be quotaon or quotaoff"); 87 while ((ch = getopt(argc, argv, "avug")) != -1) { 88 switch(ch) { 89 case 'a': 90 aflag++; 91 break; 92 case 'g': 93 gflag++; 94 break; 95 case 'u': 96 uflag++; 97 break; 98 case 'v': 99 vflag++; 100 break; 101 default: 102 usage(); 103 } 104 } 105 argc -= optind; 106 argv += optind; 107 if (argc <= 0 && !aflag) 108 usage(); 109 if (!gflag && !uflag) { 110 gflag++; 111 uflag++; 112 } 113 setfsent(); 114 while ((fs = getfsent()) != NULL) { 115 if (strcmp(fs->fs_vfstype, "ufs") || 116 strcmp(fs->fs_type, FSTAB_RW)) 117 continue; 118 if (aflag) { 119 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 120 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 121 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 122 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 123 continue; 124 } 125 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 126 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 127 done |= 1 << argnum; 128 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 129 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 130 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 131 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 132 } 133 } 134 endfsent(); 135 for (i = 0; i < argc; i++) 136 if ((done & (1 << i)) == 0) 137 warnx("%s not found in fstab", argv[i]); 138 exit(errs); 139 } 140 141 static void 142 usage() 143 { 144 145 fprintf(stderr, "%s\n%s\n%s\n%s\n", 146 "usage: quotaon [-g] [-u] [-v] -a", 147 " quotaon [-g] [-u] [-v] filesystem ...", 148 " quotaoff [-g] [-u] [-v] -a", 149 " quotaoff [-g] [-u] [-v] filesystem ..."); 150 exit(1); 151 } 152 153 int 154 quotaonoff(fs, offmode, type, qfpathname) 155 register struct fstab *fs; 156 int offmode, type; 157 char *qfpathname; 158 { 159 160 if (strcmp(fs->fs_file, "/") && readonly(fs)) 161 return (1); 162 if (offmode) { 163 if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) { 164 warn("%s", fs->fs_file); 165 return (1); 166 } 167 if (vflag) 168 printf("%s: quotas turned off\n", fs->fs_file); 169 return (0); 170 } 171 if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) { 172 warnx("using %s on", qfpathname); 173 warn("%s", fs->fs_file); 174 return (1); 175 } 176 if (vflag) 177 printf("%s: %s quotas turned on\n", fs->fs_file, 178 qfextension[type]); 179 return (0); 180 } 181 182 /* 183 * Check to see if target appears in list of size cnt. 184 */ 185 int 186 oneof(target, list, cnt) 187 register char *target, *list[]; 188 int cnt; 189 { 190 register int i; 191 192 for (i = 0; i < cnt; i++) 193 if (strcmp(target, list[i]) == 0) 194 return (i); 195 return (-1); 196 } 197 198 /* 199 * Check to see if a particular quota is to be enabled. 200 */ 201 int 202 hasquota(fs, type, qfnamep) 203 register struct fstab *fs; 204 int type; 205 char **qfnamep; 206 { 207 register char *opt; 208 char *cp; 209 static char initname, usrname[100], grpname[100]; 210 static char buf[BUFSIZ]; 211 212 if (!initname) { 213 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 214 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 215 initname = 1; 216 } 217 strcpy(buf, fs->fs_mntops); 218 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 219 if ((cp = index(opt, '='))) 220 *cp++ = '\0'; 221 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 222 break; 223 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 224 break; 225 } 226 if (!opt) 227 return (0); 228 if (cp) { 229 *qfnamep = cp; 230 return (1); 231 } 232 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 233 *qfnamep = buf; 234 return (1); 235 } 236 237 /* 238 * Verify file system is mounted and not readonly. 239 */ 240 int 241 readonly(fs) 242 register struct fstab *fs; 243 { 244 struct statfs fsbuf; 245 246 if (statfs(fs->fs_file, &fsbuf) < 0 || 247 strcmp(fsbuf.f_mntonname, fs->fs_file) || 248 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { 249 printf("%s: not mounted\n", fs->fs_file); 250 return (1); 251 } 252 if (fsbuf.f_flags & MNT_RDONLY) { 253 printf("%s: mounted read-only\n", fs->fs_file); 254 return (1); 255 } 256 return (0); 257 } 258