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[] =
48 "$FreeBSD: src/usr.sbin/edquota/edquota.c,v 1.9.2.6 2002/10/31 22:38:43 iedowse Exp $";
54 #include <sys/param.h>
58 #include <ufs/ufs/quota.h>
70 #include "pathnames.h"
72 const char *qfname = QUOTAFILENAME;
73 const char *qfextension[] = INITQFNAMES;
74 const 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(const char *s);
87 int cvtatos(time_t, char *, time_t *);
88 char *cvtstoa(time_t);
90 void freeprivs(struct quotause *);
91 int getentry(const char *, int);
92 struct quotause *getprivs(long, int, char *);
93 int hasquota(struct fstab *, int, char **);
94 void putprivs(long, int, struct quotause *);
95 int readprivs(struct quotause *, char *);
96 int readtimes(struct quotause *, char *);
97 static void usage(void);
98 int writetimes(struct quotause *, int, int);
99 int writeprivs(struct quotause *, int, char *, int);
102 main(int argc, char **argv)
104 struct quotause *qup, *protoprivs, *curprivs;
107 int i, quotatype, range, tmpfd;
108 uid_t startuid, enduid;
110 char *protoname, *cp, *oldoptarg, ch;
111 int eflag = 0, tflag = 0, pflag = 0;
118 errx(1, "permission denied");
119 quotatype = USRQUOTA;
121 while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
131 quotatype = GRPQUOTA;
134 quotatype = USRQUOTA;
140 if ((qup = malloc(sizeof(*qup))) == NULL)
141 errx(2, "out of memory");
142 bzero(qup, sizeof(*qup));
145 for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
152 strlcpy(qup->fsname, cp,
153 sizeof(qup->fsname));
156 limp = &qup->dqblk.dqb_bsoftlimit;
159 limp = &qup->dqblk.dqb_bhardlimit;
162 limp = &qup->dqblk.dqb_isoftlimit;
165 limp = &qup->dqblk.dqb_ihardlimit;
168 warnx("incorrect quota specification: "
171 break; /* XXX: report an error */
174 lim = strtoll(cp, NULL, 10);
175 if (lim < 0 || lim > UINT_MAX)
176 errx(1, "invalid limit value: "
178 *limp = (u_int32_t)lim;
181 qup->dqblk.dqb_bsoftlimit =
182 btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
183 qup->dqblk.dqb_bhardlimit =
184 btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
185 if (protoprivs == NULL) {
186 protoprivs = curprivs = qup;
188 curprivs->next = qup;
201 if (protoprivs == NULL) {
202 if ((protoid = getentry(protoname, quotatype)) == -1)
204 protoprivs = getprivs(protoid, quotatype, fspath);
205 for (qup = protoprivs; qup; qup = qup->next) {
206 qup->dqblk.dqb_btime = 0;
207 qup->dqblk.dqb_itime = 0;
210 for (; argc-- > 0; argv++) {
211 if (strspn(*argv, "0123456789-") == strlen(*argv) &&
212 (cp = strchr(*argv, '-')) != NULL) {
214 startuid = atoi(*argv);
216 if (enduid < startuid)
218 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
222 startuid = enduid = 0;
225 for ( ; startuid <= enduid; startuid++) {
227 snprintf(buf, sizeof(buf), "%d",
230 snprintf(buf, sizeof(buf), "%s",
232 if ((id = getentry(buf, quotatype)) < 0)
235 for (qup = protoprivs; qup;
237 curprivs = getprivs(id,
238 quotatype, qup->fsname);
239 if (curprivs == NULL)
247 putprivs(id, quotatype, protoprivs);
252 tmpfd = mkstemp(tmpfil);
253 fchown(tmpfd, getuid(), getgid());
255 protoprivs = getprivs(0, quotatype, fspath);
256 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
258 if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
259 putprivs(0, quotatype, protoprivs);
260 freeprivs(protoprivs);
265 for ( ; argc > 0; argc--, argv++) {
266 if ((id = getentry(*argv, quotatype)) == -1)
268 curprivs = getprivs(id, quotatype, fspath);
269 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
271 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
272 putprivs(id, quotatype, curprivs);
283 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
284 "usage: edquota [-u] [-f fspath] [-p username] username ...",
285 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
287 " edquota -g [-f fspath] [-p groupname] groupname ...",
288 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
290 " edquota [-u] -t [-f fspath]",
291 " edquota -g -t [-f fspath]");
296 * This routine converts a name for a particular quota type to
297 * an identifier. This routine must agree with the kernel routine
298 * getinoquota as to the interpretation of quota types.
301 getentry(name, quotatype)
312 if ((pw = getpwnam(name)))
314 warnx("%s: no such user", name);
317 if ((gr = getgrnam(name)))
319 warnx("%s: no such group", name);
322 warnx("%d: unknown quota type", quotatype);
330 * Collect the requested quota information.
333 getprivs(id, quotatype, fspath)
338 register struct fstab *fs;
339 register struct quotause *qup, *quptail;
340 struct quotause *quphead;
341 int qcmd, qupsize, fd;
343 static int warned = 0;
346 quphead = (struct quotause *)0;
347 qcmd = QCMD(Q_GETQUOTA, quotatype);
348 while ((fs = getfsent())) {
349 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
350 strcmp(fspath, fs->fs_file))
352 if (strcmp(fs->fs_vfstype, "ufs"))
354 if (!hasquota(fs, quotatype, &qfpathname))
356 qupsize = sizeof(*qup) + strlen(qfpathname);
357 if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
358 errx(2, "out of memory");
359 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
360 if (errno == EOPNOTSUPP && !warned) {
362 warnx("warning: quotas are not compiled into this kernel");
365 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
366 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
367 if (fd < 0 && errno != ENOENT) {
368 warn("%s", qfpathname);
372 warnx("creating quota file %s", qfpathname);
374 (void) fchown(fd, getuid(),
375 getentry(quotagroup, GRPQUOTA));
376 (void) fchmod(fd, 0640);
378 lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
379 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
382 * Convert implicit 0 quota (EOF)
383 * into an explicit one (zero'ed dqblk)
385 bzero((caddr_t)&qup->dqblk,
386 sizeof(struct dqblk));
389 case sizeof(struct dqblk): /* OK */
393 warn("read error in %s", qfpathname);
400 strcpy(qup->qfname, qfpathname);
401 strcpy(qup->fsname, fs->fs_file);
414 * Store the requested quota information.
417 putprivs(id, quotatype, quplist)
420 struct quotause *quplist;
422 register struct quotause *qup;
425 qcmd = QCMD(Q_SETQUOTA, quotatype);
426 for (qup = quplist; qup; qup = qup->next) {
427 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
429 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
430 warn("%s", qup->qfname);
432 lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
433 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
434 sizeof (struct dqblk)) {
435 warn("%s", qup->qfname);
443 * Take a list of priviledges and get it edited.
452 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
454 if ((pid = fork()) < 0) {
456 if (errno == EPROCLIM) {
457 warnx("you have too many processes");
460 if (errno == EAGAIN) {
468 register const char *ed;
473 if ((ed = getenv("EDITOR")) == (char *)0)
475 execlp(ed, ed, tmpf, (char *)0);
478 waitpid(pid, &status, 0);
480 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
486 * Convert a quotause list to an ASCII file.
489 writeprivs(quplist, outfd, name, quotatype)
490 struct quotause *quplist;
495 register struct quotause *qup;
499 lseek(outfd, 0, L_SET);
500 if ((fd = fdopen(dup(outfd), "w")) == NULL)
501 err(1, "%s", tmpfil);
502 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
503 for (qup = quplist; qup; qup = qup->next) {
504 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
505 qup->fsname, "kbytes in use:",
506 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
507 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
508 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
509 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
511 (unsigned long)qup->dqblk.dqb_curinodes,
512 (unsigned long)qup->dqblk.dqb_isoftlimit,
513 (unsigned long)qup->dqblk.dqb_ihardlimit);
520 * Merge changes to an ASCII file into a quotause list.
523 readprivs(quplist, inname)
524 struct quotause *quplist;
527 register struct quotause *qup;
529 unsigned long bhardlimit, bsoftlimit, curblocks;
530 unsigned long ihardlimit, isoftlimit, curinodes;
534 char *fsp, line1[BUFSIZ], line2[BUFSIZ];
536 fd = fopen(inname, "r");
538 warnx("can't re-read temp file!!");
542 * Discard title line, then read pairs of lines to process.
544 (void) fgets(line1, sizeof (line1), fd);
545 while (fgets(line1, sizeof (line1), fd) != NULL &&
546 fgets(line2, sizeof (line2), fd) != NULL) {
547 if ((fsp = strtok(line1, " \t:")) == NULL) {
548 warnx("%s: bad format", line1);
551 if ((cp = strtok((char *)0, "\n")) == NULL) {
552 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
556 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
557 &curblocks, &bsoftlimit, &bhardlimit);
559 warnx("%s:%s: bad format", fsp, cp);
562 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
563 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
564 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
565 if ((cp = strtok(line2, "\n")) == NULL) {
566 warnx("%s: %s: bad format", fsp, line2);
570 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
571 &curinodes, &isoftlimit, &ihardlimit);
573 warnx("%s: %s: bad format", fsp, line2);
576 dqblk.dqb_curinodes = curinodes;
577 dqblk.dqb_isoftlimit = isoftlimit;
578 dqblk.dqb_ihardlimit = ihardlimit;
579 for (qup = quplist; qup; qup = qup->next) {
580 if (strcmp(fsp, qup->fsname))
583 * Cause time limit to be reset when the quota
584 * is next used if previously had no soft limit
585 * or were under it, but now have a soft limit
588 if (dqblk.dqb_bsoftlimit &&
589 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
590 (qup->dqblk.dqb_bsoftlimit == 0 ||
591 qup->dqblk.dqb_curblocks <
592 qup->dqblk.dqb_bsoftlimit))
593 qup->dqblk.dqb_btime = 0;
594 if (dqblk.dqb_isoftlimit &&
595 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
596 (qup->dqblk.dqb_isoftlimit == 0 ||
597 qup->dqblk.dqb_curinodes <
598 qup->dqblk.dqb_isoftlimit))
599 qup->dqblk.dqb_itime = 0;
600 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
601 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
602 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
603 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
605 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
606 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
608 warnx("%s: cannot change current allocation", fsp);
614 * Disable quotas for any filesystems that have not been found.
616 for (qup = quplist; qup; qup = qup->next) {
617 if (qup->flags & FOUND) {
618 qup->flags &= ~FOUND;
621 qup->dqblk.dqb_bsoftlimit = 0;
622 qup->dqblk.dqb_bhardlimit = 0;
623 qup->dqblk.dqb_isoftlimit = 0;
624 qup->dqblk.dqb_ihardlimit = 0;
630 * Convert a quotause list to an ASCII file of grace times.
633 writetimes(quplist, outfd, quotatype)
634 struct quotause *quplist;
638 register struct quotause *qup;
642 lseek(outfd, 0, L_SET);
643 if ((fd = fdopen(dup(outfd), "w")) == NULL)
644 err(1, "%s", tmpfil);
645 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
646 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
647 qfextension[quotatype]);
648 for (qup = quplist; qup; qup = qup->next) {
649 fprintf(fd, "%s: block grace period: %s, ",
650 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
651 fprintf(fd, "file grace period: %s\n",
652 cvtstoa(qup->dqblk.dqb_itime));
659 * Merge changes of grace times in an ASCII file into a quotause list.
662 readtimes(quplist, inname)
663 struct quotause *quplist;
666 register struct quotause *qup;
670 time_t itime, btime, iseconds, bseconds;
671 long l_itime, l_btime;
672 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
674 fd = fopen(inname, "r");
676 warnx("can't re-read temp file!!");
680 * Discard two title lines, then read lines to process.
682 (void) fgets(line1, sizeof (line1), fd);
683 (void) fgets(line1, sizeof (line1), fd);
684 while (fgets(line1, sizeof (line1), fd) != NULL) {
685 if ((fsp = strtok(line1, " \t:")) == NULL) {
686 warnx("%s: bad format", line1);
689 if ((cp = strtok((char *)0, "\n")) == NULL) {
690 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
694 " block grace period: %ld %s file grace period: %ld %s",
695 &l_btime, bunits, &l_itime, iunits);
697 warnx("%s:%s: bad format", fsp, cp);
702 if (cvtatos(btime, bunits, &bseconds) == 0)
704 if (cvtatos(itime, iunits, &iseconds) == 0)
706 for (qup = quplist; qup; qup = qup->next) {
707 if (strcmp(fsp, qup->fsname))
709 qup->dqblk.dqb_btime = bseconds;
710 qup->dqblk.dqb_itime = iseconds;
717 * reset default grace periods for any filesystems
718 * that have not been found.
720 for (qup = quplist; qup; qup = qup->next) {
721 if (qup->flags & FOUND) {
722 qup->flags &= ~FOUND;
725 qup->dqblk.dqb_btime = 0;
726 qup->dqblk.dqb_itime = 0;
732 * Convert seconds to ASCII times.
740 if (secs % (24 * 60 * 60) == 0) {
741 secs /= 24 * 60 * 60;
742 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
743 } else if (secs % (60 * 60) == 0) {
745 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
746 } else if (secs % 60 == 0) {
748 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
750 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
755 * Convert ASCII input times to seconds.
758 cvtatos(period, units, seconds)
764 if (bcmp(units, "second", 6) == 0)
766 else if (bcmp(units, "minute", 6) == 0)
767 *seconds = period * 60;
768 else if (bcmp(units, "hour", 4) == 0)
769 *seconds = period * 60 * 60;
770 else if (bcmp(units, "day", 3) == 0)
771 *seconds = period * 24 * 60 * 60;
773 printf("%s: bad units, specify %s\n", units,
774 "days, hours, minutes, or seconds");
781 * Free a list of quotause structures.
785 struct quotause *quplist;
787 register struct quotause *qup, *nextqup;
789 for (qup = quplist; qup; qup = nextqup) {
796 * Check whether a string is completely composed of digits.
800 register const char *s;
808 } while ((c = *s++));
813 * Check to see if a particular quota is to be enabled.
816 hasquota(fs, type, qfnamep)
817 register struct fstab *fs;
823 static char initname, usrname[100], grpname[100];
824 static char buf[BUFSIZ];
827 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
828 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
831 strcpy(buf, fs->fs_mntops);
832 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
833 if ((cp = index(opt, '=')))
835 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
837 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
846 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);