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. 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.
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
32 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)edquota.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/edquota/edquota.c,v 1.9.2.6 2002/10/31 22:38:43 iedowse Exp $
40 #include <sys/param.h>
44 #include <vfs/ufs/quota.h>
56 #include "pathnames.h"
58 const char *qfname = QUOTAFILENAME;
59 const char *qfextension[] = INITQFNAMES;
60 const char *quotagroup = QUOTAGROUP;
61 char tmpfil[] = _PATH_TMP;
64 struct quotause *next;
66 struct ufs_dqblk dqblk;
67 char fsname[MAXPATHLEN + 1];
68 char qfname[1]; /* actually longer */
72 int alldigits(const char *s);
73 int cvtatos(time_t, char *, time_t *);
74 char *cvtstoa(time_t);
76 void freeprivs(struct quotause *);
77 int getentry(const char *, int);
78 struct quotause *getprivs(long, int, char *);
79 int hasquota(struct fstab *, int, char **);
80 void putprivs(long, int, struct quotause *);
81 int readprivs(struct quotause *, char *);
82 int readtimes(struct quotause *, char *);
83 static void usage(void);
84 int writetimes(struct quotause *, int, int);
85 int writeprivs(struct quotause *, int, char *, int);
88 main(int argc, char **argv)
90 struct quotause *qup, *protoprivs, *curprivs;
93 int i, quotatype, range, tmpfd;
94 uid_t startuid, enduid;
96 char *protoname, *cp, *oldoptarg, ch;
97 int eflag = 0, tflag = 0, pflag = 0;
104 errx(1, "permission denied");
105 quotatype = USRQUOTA;
109 while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
119 quotatype = GRPQUOTA;
122 quotatype = USRQUOTA;
128 if ((qup = malloc(sizeof(*qup))) == NULL)
129 errx(2, "out of memory");
130 bzero(qup, sizeof(*qup));
133 for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
140 strlcpy(qup->fsname, cp,
141 sizeof(qup->fsname));
144 limp = &qup->dqblk.dqb_bsoftlimit;
147 limp = &qup->dqblk.dqb_bhardlimit;
150 limp = &qup->dqblk.dqb_isoftlimit;
153 limp = &qup->dqblk.dqb_ihardlimit;
156 warnx("incorrect quota specification: "
159 break; /* XXX: report an error */
162 lim = strtoll(cp, NULL, 10);
163 if (lim < 0 || lim > UINT_MAX)
164 errx(1, "invalid limit value: "
166 *limp = (u_int32_t)lim;
169 qup->dqblk.dqb_bsoftlimit =
170 btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
171 qup->dqblk.dqb_bhardlimit =
172 btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
173 if (protoprivs == NULL) {
174 protoprivs = curprivs = qup;
176 curprivs->next = qup;
189 if (protoprivs == NULL) {
190 if ((protoid = getentry(protoname, quotatype)) == -1)
192 protoprivs = getprivs(protoid, quotatype, fspath);
193 for (qup = protoprivs; qup; qup = qup->next) {
194 qup->dqblk.dqb_btime = 0;
195 qup->dqblk.dqb_itime = 0;
198 for (; argc-- > 0; argv++) {
199 if (strspn(*argv, "0123456789-") == strlen(*argv) &&
200 (cp = strchr(*argv, '-')) != NULL) {
202 startuid = atoi(*argv);
204 if (enduid < startuid)
206 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
210 startuid = enduid = 0;
213 for ( ; startuid <= enduid; startuid++) {
215 snprintf(buf, sizeof(buf), "%d",
218 snprintf(buf, sizeof(buf), "%s",
220 if ((id = getentry(buf, quotatype)) < 0)
223 for (qup = protoprivs; qup;
225 curprivs = getprivs(id,
226 quotatype, qup->fsname);
227 if (curprivs == NULL)
235 putprivs(id, quotatype, protoprivs);
240 tmpfd = mkstemp(tmpfil);
241 fchown(tmpfd, getuid(), getgid());
243 protoprivs = getprivs(0, quotatype, fspath);
244 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
246 if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
247 putprivs(0, quotatype, protoprivs);
248 freeprivs(protoprivs);
253 for ( ; argc > 0; argc--, argv++) {
254 if ((id = getentry(*argv, quotatype)) == -1)
256 curprivs = getprivs(id, quotatype, fspath);
257 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
259 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
260 putprivs(id, quotatype, curprivs);
271 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
272 "usage: edquota [-u] [-f fspath] [-p username] username ...",
273 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
275 " edquota -g [-f fspath] [-p groupname] groupname ...",
276 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
278 " edquota [-u] -t [-f fspath]",
279 " edquota -g -t [-f fspath]");
284 * This routine converts a name for a particular quota type to
285 * an identifier. This routine must agree with the kernel routine
286 * getinoquota as to the interpretation of quota types.
289 getentry(const char *name, int quotatype)
298 if ((pw = getpwnam(name)))
300 warnx("%s: no such user", name);
303 if ((gr = getgrnam(name)))
305 warnx("%s: no such group", name);
308 warnx("%d: unknown quota type", quotatype);
316 * Collect the requested quota information.
319 getprivs(long id, int quotatype, char *fspath)
322 struct quotause *qup, *quptail;
323 struct quotause *quphead;
324 int qcmd, qupsize, fd;
326 static int warned = 0;
329 quphead = quptail = NULL;
330 qcmd = QCMD(Q_GETQUOTA, quotatype);
331 while ((fs = getfsent())) {
332 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
333 strcmp(fspath, fs->fs_file))
335 if (strcmp(fs->fs_vfstype, "ufs"))
337 if (!hasquota(fs, quotatype, &qfpathname))
339 qupsize = sizeof(*qup) + strlen(qfpathname);
340 if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
341 errx(2, "out of memory");
342 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
343 if (errno == EOPNOTSUPP && !warned) {
345 warnx("warning: quotas are not compiled into this kernel");
348 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
349 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
350 if (fd < 0 && errno != ENOENT) {
351 warn("%s", qfpathname);
355 warnx("creating quota file %s", qfpathname);
358 getentry(quotagroup, GRPQUOTA));
361 lseek(fd, (long)(id * sizeof(struct ufs_dqblk)), L_SET);
362 switch (read(fd, &qup->dqblk, sizeof(struct ufs_dqblk))) {
365 * Convert implicit 0 quota (EOF)
366 * into an explicit one (zero'ed dqblk)
368 bzero((caddr_t)&qup->dqblk,
369 sizeof(struct ufs_dqblk));
372 case sizeof(struct ufs_dqblk): /* OK */
376 warn("read error in %s", qfpathname);
383 strcpy(qup->qfname, qfpathname);
384 strcpy(qup->fsname, fs->fs_file);
397 * Store the requested quota information.
400 putprivs(long id, int quotatype, struct quotause *quplist)
402 struct quotause *qup;
405 qcmd = QCMD(Q_SETQUOTA, quotatype);
406 for (qup = quplist; qup; qup = qup->next) {
407 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
409 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
410 warn("%s", qup->qfname);
412 lseek(fd, (long)id * (long)sizeof(struct ufs_dqblk), 0);
413 if (write(fd, &qup->dqblk, sizeof(struct ufs_dqblk)) !=
414 sizeof(struct ufs_dqblk)) {
415 warn("%s", qup->qfname);
423 * Take a list of priviledges and get it edited.
431 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
433 if ((pid = fork()) < 0) {
435 if (errno == EPROCLIM) {
436 warnx("you have too many processes");
439 if (errno == EAGAIN) {
452 if ((ed = getenv("EDITOR")) == NULL)
454 execlp(ed, ed, tmpf, NULL);
457 waitpid(pid, &status, 0);
459 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
465 * Convert a quotause list to an ASCII file.
468 writeprivs(struct quotause *quplist, int outfd, char *name, int quotatype)
470 struct quotause *qup;
474 lseek(outfd, 0, L_SET);
475 if ((fd = fdopen(dup(outfd), "w")) == NULL)
476 err(1, "%s", tmpfil);
477 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
478 for (qup = quplist; qup; qup = qup->next) {
479 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
480 qup->fsname, "kbytes in use:",
481 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
482 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
483 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
484 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
486 (unsigned long)qup->dqblk.dqb_curinodes,
487 (unsigned long)qup->dqblk.dqb_isoftlimit,
488 (unsigned long)qup->dqblk.dqb_ihardlimit);
495 * Merge changes to an ASCII file into a quotause list.
498 readprivs(struct quotause *quplist, char *inname)
500 struct quotause *qup;
502 unsigned long bhardlimit, bsoftlimit, curblocks;
503 unsigned long ihardlimit, isoftlimit, curinodes;
506 struct ufs_dqblk dqblk;
507 char *fsp, line1[BUFSIZ], line2[BUFSIZ];
509 fd = fopen(inname, "r");
511 warnx("can't re-read temp file!!");
515 * Discard title line, then read pairs of lines to process.
517 fgets(line1, sizeof(line1), fd);
518 while (fgets(line1, sizeof(line1), fd) != NULL &&
519 fgets(line2, sizeof(line2), fd) != NULL) {
520 if ((fsp = strtok(line1, " \t:")) == NULL) {
521 warnx("%s: bad format", line1);
524 if ((cp = strtok(NULL, "\n")) == NULL) {
525 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
529 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
530 &curblocks, &bsoftlimit, &bhardlimit);
532 warnx("%s:%s: bad format", fsp, cp);
535 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
536 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
537 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
538 if ((cp = strtok(line2, "\n")) == NULL) {
539 warnx("%s: %s: bad format", fsp, line2);
543 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
544 &curinodes, &isoftlimit, &ihardlimit);
546 warnx("%s: %s: bad format", fsp, line2);
549 dqblk.dqb_curinodes = curinodes;
550 dqblk.dqb_isoftlimit = isoftlimit;
551 dqblk.dqb_ihardlimit = ihardlimit;
552 for (qup = quplist; qup; qup = qup->next) {
553 if (strcmp(fsp, qup->fsname))
556 * Cause time limit to be reset when the quota
557 * is next used if previously had no soft limit
558 * or were under it, but now have a soft limit
561 if (dqblk.dqb_bsoftlimit &&
562 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
563 (qup->dqblk.dqb_bsoftlimit == 0 ||
564 qup->dqblk.dqb_curblocks <
565 qup->dqblk.dqb_bsoftlimit))
566 qup->dqblk.dqb_btime = 0;
567 if (dqblk.dqb_isoftlimit &&
568 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
569 (qup->dqblk.dqb_isoftlimit == 0 ||
570 qup->dqblk.dqb_curinodes <
571 qup->dqblk.dqb_isoftlimit))
572 qup->dqblk.dqb_itime = 0;
573 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
574 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
575 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
576 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
578 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
579 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
581 warnx("%s: cannot change current allocation", fsp);
587 * Disable quotas for any filesystems that have not been found.
589 for (qup = quplist; qup; qup = qup->next) {
590 if (qup->flags & FOUND) {
591 qup->flags &= ~FOUND;
594 qup->dqblk.dqb_bsoftlimit = 0;
595 qup->dqblk.dqb_bhardlimit = 0;
596 qup->dqblk.dqb_isoftlimit = 0;
597 qup->dqblk.dqb_ihardlimit = 0;
603 * Convert a quotause list to an ASCII file of grace times.
606 writetimes(struct quotause *quplist, int outfd, int quotatype)
608 struct quotause *qup;
612 lseek(outfd, 0, L_SET);
613 if ((fd = fdopen(dup(outfd), "w")) == NULL)
614 err(1, "%s", tmpfil);
615 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
616 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
617 qfextension[quotatype]);
618 for (qup = quplist; qup; qup = qup->next) {
619 fprintf(fd, "%s: block grace period: %s, ",
620 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
621 fprintf(fd, "file grace period: %s\n",
622 cvtstoa(qup->dqblk.dqb_itime));
629 * Merge changes of grace times in an ASCII file into a quotause list.
632 readtimes(struct quotause *quplist, char *inname)
634 struct quotause *qup;
638 time_t itime, btime, iseconds, bseconds;
639 long l_itime, l_btime;
640 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
642 fd = fopen(inname, "r");
644 warnx("can't re-read temp file!!");
648 * Discard two title lines, then read lines to process.
650 fgets(line1, sizeof(line1), fd);
651 fgets(line1, sizeof(line1), fd);
652 while (fgets(line1, sizeof(line1), fd) != NULL) {
653 if ((fsp = strtok(line1, " \t:")) == NULL) {
654 warnx("%s: bad format", line1);
657 if ((cp = strtok(NULL, "\n")) == NULL) {
658 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
662 " block grace period: %ld %s file grace period: %ld %s",
663 &l_btime, bunits, &l_itime, iunits);
665 warnx("%s:%s: bad format", fsp, cp);
670 if (cvtatos(btime, bunits, &bseconds) == 0)
672 if (cvtatos(itime, iunits, &iseconds) == 0)
674 for (qup = quplist; qup; qup = qup->next) {
675 if (strcmp(fsp, qup->fsname))
677 qup->dqblk.dqb_btime = bseconds;
678 qup->dqblk.dqb_itime = iseconds;
685 * reset default grace periods for any filesystems
686 * that have not been found.
688 for (qup = quplist; qup; qup = qup->next) {
689 if (qup->flags & FOUND) {
690 qup->flags &= ~FOUND;
693 qup->dqblk.dqb_btime = 0;
694 qup->dqblk.dqb_itime = 0;
700 * Convert seconds to ASCII times.
707 if (secs % (24 * 60 * 60) == 0) {
708 secs /= 24 * 60 * 60;
709 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
710 } else if (secs % (60 * 60) == 0) {
712 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
713 } else if (secs % 60 == 0) {
715 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
717 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
722 * Convert ASCII input times to seconds.
725 cvtatos(time_t period, char *units, time_t *seconds)
728 if (bcmp(units, "second", 6) == 0)
730 else if (bcmp(units, "minute", 6) == 0)
731 *seconds = period * 60;
732 else if (bcmp(units, "hour", 4) == 0)
733 *seconds = period * 60 * 60;
734 else if (bcmp(units, "day", 3) == 0)
735 *seconds = period * 24 * 60 * 60;
737 printf("%s: bad units, specify %s\n", units,
738 "days, hours, minutes, or seconds");
745 * Free a list of quotause structures.
748 freeprivs(struct quotause *quplist)
750 struct quotause *qup, *nextqup;
752 for (qup = quplist; qup; qup = nextqup) {
759 * Check whether a string is completely composed of digits.
762 alldigits(const char *s)
770 } while ((c = *s++));
775 * Check to see if a particular quota is to be enabled.
778 hasquota(struct fstab *fs, int type, char **qfnamep)
782 static char initname, usrname[100], grpname[100];
783 static char buf[BUFSIZ];
786 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
787 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
790 strcpy(buf, fs->fs_mntops);
791 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
792 if ((cp = strchr(opt, '=')))
794 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
796 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
805 sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);