2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1990, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
54 #include <sys/param.h>
58 #include <ufs/ufs/quota.h>
70 #include "pathnames.h"
72 char *qfname = QUOTAFILENAME;
73 char *qfextension[] = INITQFNAMES;
74 char *quotagroup = QUOTAGROUP;
75 char tmpfil[] = _PATH_TMP;
78 struct quotause *next;
81 char fsname[MAXPATHLEN + 1];
82 char qfname[1]; /* actually longer */
86 int alldigits __P((char *s));
87 int cvtatos __P((time_t, char *, time_t *));
88 char *cvtstoa __P((time_t));
89 int editit __P((char *));
90 void freeprivs __P((struct quotause *));
91 int getentry __P((char *, int));
92 struct quotause *getprivs __P((long, int));
93 int hasquota __P((struct fstab *, int, char **));
94 void putprivs __P((long, int, struct quotause *));
95 int readprivs __P((struct quotause *, char *));
96 int readtimes __P((struct quotause *, char *));
97 static void usage __P((void));
98 int writetimes __P((struct quotause *, int, int));
99 int writeprivs __P((struct quotause *, int, char *, int));
103 register char **argv;
106 register struct quotause *qup, *protoprivs, *curprivs;
107 register long id, protoid;
108 register int quotatype, tmpfd;
109 register uid_t startuid, enduid;
110 char *protoname, *cp, ch;
111 int tflag = 0, pflag = 0;
117 errx(1, "permission denied");
118 quotatype = USRQUOTA;
119 while ((ch = getopt(argc, argv, "ugtp:")) != -1) {
126 quotatype = GRPQUOTA;
129 quotatype = USRQUOTA;
141 if ((protoid = getentry(protoname, quotatype)) == -1)
143 protoprivs = getprivs(protoid, quotatype);
144 for (qup = protoprivs; qup; qup = qup->next) {
145 qup->dqblk.dqb_btime = 0;
146 qup->dqblk.dqb_itime = 0;
149 if (isdigit(*argv[0]) &&
150 (cp = strchr(*argv, '-')) != NULL) {
152 startuid = atoi(*argv);
154 if (enduid < startuid)
156 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
158 for ( ; startuid <= enduid; startuid++) {
159 snprintf(buf, sizeof(buf), "%d",
161 if ((id = getentry(buf, quotatype)) < 0)
163 putprivs(id, quotatype, protoprivs);
167 if ((id = getentry(*argv++, quotatype)) < 0)
169 putprivs(id, quotatype, protoprivs);
173 tmpfd = mkstemp(tmpfil);
174 fchown(tmpfd, getuid(), getgid());
176 protoprivs = getprivs(0, quotatype);
177 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
179 if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
180 putprivs(0, quotatype, protoprivs);
181 freeprivs(protoprivs);
186 for ( ; argc > 0; argc--, argv++) {
187 if ((id = getentry(*argv, quotatype)) == -1)
189 curprivs = getprivs(id, quotatype);
190 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
192 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
193 putprivs(id, quotatype, curprivs);
204 fprintf(stderr, "%s\n%s\n%s\n%s\n",
205 "usage: edquota [-u] [-p username] username ...",
206 " edquota -g [-p groupname] groupname ...",
213 * This routine converts a name for a particular quota type to
214 * an identifier. This routine must agree with the kernel routine
215 * getinoquota as to the interpretation of quota types.
218 getentry(name, quotatype)
229 if ((pw = getpwnam(name)))
231 warnx("%s: no such user", name);
234 if ((gr = getgrnam(name)))
236 warnx("%s: no such group", name);
239 warnx("%d: unknown quota type", quotatype);
247 * Collect the requested quota information.
250 getprivs(id, quotatype)
254 register struct fstab *fs;
255 register struct quotause *qup, *quptail;
256 struct quotause *quphead;
257 int qcmd, qupsize, fd;
259 static int warned = 0;
262 quphead = (struct quotause *)0;
263 qcmd = QCMD(Q_GETQUOTA, quotatype);
264 while ((fs = getfsent())) {
265 if (strcmp(fs->fs_vfstype, "ufs"))
267 if (!hasquota(fs, quotatype, &qfpathname))
269 qupsize = sizeof(*qup) + strlen(qfpathname);
270 if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
271 errx(2, "out of memory");
272 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
273 if (errno == EOPNOTSUPP && !warned) {
275 warnx("warning: quotas are not compiled into this kernel");
278 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
279 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
280 if (fd < 0 && errno != ENOENT) {
281 warn("%s", qfpathname);
285 warnx("creating quota file %s", qfpathname);
287 (void) fchown(fd, getuid(),
288 getentry(quotagroup, GRPQUOTA));
289 (void) fchmod(fd, 0640);
291 lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
292 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
295 * Convert implicit 0 quota (EOF)
296 * into an explicit one (zero'ed dqblk)
298 bzero((caddr_t)&qup->dqblk,
299 sizeof(struct dqblk));
302 case sizeof(struct dqblk): /* OK */
306 warn("read error in %s", qfpathname);
313 strcpy(qup->qfname, qfpathname);
314 strcpy(qup->fsname, fs->fs_file);
327 * Store the requested quota information.
330 putprivs(id, quotatype, quplist)
333 struct quotause *quplist;
335 register struct quotause *qup;
338 qcmd = QCMD(Q_SETQUOTA, quotatype);
339 for (qup = quplist; qup; qup = qup->next) {
340 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
342 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
343 warn("%s", qup->qfname);
345 lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
346 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
347 sizeof (struct dqblk)) {
348 warn("%s", qup->qfname);
356 * Take a list of priviledges and get it edited.
365 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
367 if ((pid = fork()) < 0) {
369 if (errno == EPROCLIM) {
370 warnx("you have too many processes");
373 if (errno == EAGAIN) {
386 if ((ed = getenv("EDITOR")) == (char *)0)
388 execlp(ed, ed, tmpfile, 0);
391 waitpid(pid, &stat, 0);
393 if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
399 * Convert a quotause list to an ASCII file.
402 writeprivs(quplist, outfd, name, quotatype)
403 struct quotause *quplist;
408 register struct quotause *qup;
412 lseek(outfd, 0, L_SET);
413 if ((fd = fdopen(dup(outfd), "w")) == NULL)
414 err(1, "%s", tmpfil);
415 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
416 for (qup = quplist; qup; qup = qup->next) {
417 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
418 qup->fsname, "blocks in use:",
419 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
420 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
421 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
422 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
423 "\tinodes in use:", qup->dqblk.dqb_curinodes,
424 qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
431 * Merge changes to an ASCII file into a quotause list.
434 readprivs(quplist, inname)
435 struct quotause *quplist;
438 register struct quotause *qup;
443 char *fsp, line1[BUFSIZ], line2[BUFSIZ];
445 fd = fopen(inname, "r");
447 warnx("can't re-read temp file!!");
451 * Discard title line, then read pairs of lines to process.
453 (void) fgets(line1, sizeof (line1), fd);
454 while (fgets(line1, sizeof (line1), fd) != NULL &&
455 fgets(line2, sizeof (line2), fd) != NULL) {
456 if ((fsp = strtok(line1, " \t:")) == NULL) {
457 warnx("%s: bad format", line1);
460 if ((cp = strtok((char *)0, "\n")) == NULL) {
461 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
465 " blocks in use: %lu, limits (soft = %lu, hard = %lu)",
466 &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
467 &dqblk.dqb_bhardlimit);
469 warnx("%s:%s: bad format", fsp, cp);
472 dqblk.dqb_curblocks = btodb((off_t)dqblk.dqb_curblocks * 1024);
473 dqblk.dqb_bsoftlimit = btodb((off_t)dqblk.dqb_bsoftlimit
475 dqblk.dqb_bhardlimit = btodb((off_t)dqblk.dqb_bhardlimit
477 if ((cp = strtok(line2, "\n")) == NULL) {
478 warnx("%s: %s: bad format", fsp, line2);
482 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
483 &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
484 &dqblk.dqb_ihardlimit);
486 warnx("%s: %s: bad format", fsp, line2);
489 for (qup = quplist; qup; qup = qup->next) {
490 if (strcmp(fsp, qup->fsname))
493 * Cause time limit to be reset when the quota
494 * is next used if previously had no soft limit
495 * or were under it, but now have a soft limit
498 if (dqblk.dqb_bsoftlimit &&
499 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
500 (qup->dqblk.dqb_bsoftlimit == 0 ||
501 qup->dqblk.dqb_curblocks <
502 qup->dqblk.dqb_bsoftlimit))
503 qup->dqblk.dqb_btime = 0;
504 if (dqblk.dqb_isoftlimit &&
505 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
506 (qup->dqblk.dqb_isoftlimit == 0 ||
507 qup->dqblk.dqb_curinodes <
508 qup->dqblk.dqb_isoftlimit))
509 qup->dqblk.dqb_itime = 0;
510 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
511 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
512 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
513 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
515 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
516 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
518 warnx("%s: cannot change current allocation", fsp);
524 * Disable quotas for any filesystems that have not been found.
526 for (qup = quplist; qup; qup = qup->next) {
527 if (qup->flags & FOUND) {
528 qup->flags &= ~FOUND;
531 qup->dqblk.dqb_bsoftlimit = 0;
532 qup->dqblk.dqb_bhardlimit = 0;
533 qup->dqblk.dqb_isoftlimit = 0;
534 qup->dqblk.dqb_ihardlimit = 0;
540 * Convert a quotause list to an ASCII file of grace times.
543 writetimes(quplist, outfd, quotatype)
544 struct quotause *quplist;
548 register struct quotause *qup;
552 lseek(outfd, 0, L_SET);
553 if ((fd = fdopen(dup(outfd), "w")) == NULL)
554 err(1, "%s", tmpfil);
555 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
556 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
557 qfextension[quotatype]);
558 for (qup = quplist; qup; qup = qup->next) {
559 fprintf(fd, "%s: block grace period: %s, ",
560 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
561 fprintf(fd, "file grace period: %s\n",
562 cvtstoa(qup->dqblk.dqb_itime));
569 * Merge changes of grace times in an ASCII file into a quotause list.
572 readtimes(quplist, inname)
573 struct quotause *quplist;
576 register struct quotause *qup;
580 time_t itime, btime, iseconds, bseconds;
581 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
583 fd = fopen(inname, "r");
585 warnx("can't re-read temp file!!");
589 * Discard two title lines, then read lines to process.
591 (void) fgets(line1, sizeof (line1), fd);
592 (void) fgets(line1, sizeof (line1), fd);
593 while (fgets(line1, sizeof (line1), fd) != NULL) {
594 if ((fsp = strtok(line1, " \t:")) == NULL) {
595 warnx("%s: bad format", line1);
598 if ((cp = strtok((char *)0, "\n")) == NULL) {
599 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
603 " block grace period: %ld %s file grace period: %ld %s",
604 &btime, bunits, &itime, iunits);
606 warnx("%s:%s: bad format", fsp, cp);
609 if (cvtatos(btime, bunits, &bseconds) == 0)
611 if (cvtatos(itime, iunits, &iseconds) == 0)
613 for (qup = quplist; qup; qup = qup->next) {
614 if (strcmp(fsp, qup->fsname))
616 qup->dqblk.dqb_btime = bseconds;
617 qup->dqblk.dqb_itime = iseconds;
624 * reset default grace periods for any filesystems
625 * that have not been found.
627 for (qup = quplist; qup; qup = qup->next) {
628 if (qup->flags & FOUND) {
629 qup->flags &= ~FOUND;
632 qup->dqblk.dqb_btime = 0;
633 qup->dqblk.dqb_itime = 0;
639 * Convert seconds to ASCII times.
647 if (time % (24 * 60 * 60) == 0) {
648 time /= 24 * 60 * 60;
649 sprintf(buf, "%ld day%s", time, time == 1 ? "" : "s");
650 } else if (time % (60 * 60) == 0) {
652 sprintf(buf, "%ld hour%s", time, time == 1 ? "" : "s");
653 } else if (time % 60 == 0) {
655 sprintf(buf, "%ld minute%s", time, time == 1 ? "" : "s");
657 sprintf(buf, "%ld second%s", time, time == 1 ? "" : "s");
662 * Convert ASCII input times to seconds.
665 cvtatos(time, units, seconds)
671 if (bcmp(units, "second", 6) == 0)
673 else if (bcmp(units, "minute", 6) == 0)
674 *seconds = time * 60;
675 else if (bcmp(units, "hour", 4) == 0)
676 *seconds = time * 60 * 60;
677 else if (bcmp(units, "day", 3) == 0)
678 *seconds = time * 24 * 60 * 60;
680 printf("%s: bad units, specify %s\n", units,
681 "days, hours, minutes, or seconds");
688 * Free a list of quotause structures.
692 struct quotause *quplist;
694 register struct quotause *qup, *nextqup;
696 for (qup = quplist; qup; qup = nextqup) {
703 * Check whether a string is completely composed of digits.
715 } while ((c = *s++));
720 * Check to see if a particular quota is to be enabled.
723 hasquota(fs, type, qfnamep)
724 register struct fstab *fs;
730 static char initname, usrname[100], grpname[100];
731 static char buf[BUFSIZ];
734 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
735 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
738 strcpy(buf, fs->fs_mntops);
739 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
740 if ((cp = index(opt, '=')))
742 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
744 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
753 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);