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 * 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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
54 #include <ufs/ufs/quota.h>
66 #include "pathnames.h"
68 const char *qfname = QUOTAFILENAME;
69 const char *qfextension[] = INITQFNAMES;
70 const char *quotagroup = QUOTAGROUP;
71 char tmpfil[] = _PATH_TMP;
74 struct quotause *next;
77 char fsname[MAXPATHLEN + 1];
78 char qfname[1]; /* actually longer */
82 int alldigits(const char *s);
83 int cvtatos(time_t, char *, time_t *);
84 char *cvtstoa(time_t);
86 void freeprivs(struct quotause *);
87 int getentry(const char *, int);
88 struct quotause *getprivs(long, int, char *);
89 int hasquota(struct fstab *, int, char **);
90 void putprivs(long, int, struct quotause *);
91 int readprivs(struct quotause *, char *);
92 int readtimes(struct quotause *, char *);
93 static void usage(void);
94 int writetimes(struct quotause *, int, int);
95 int writeprivs(struct quotause *, int, char *, int);
98 main(int argc, char **argv)
100 struct quotause *qup, *protoprivs, *curprivs;
103 int i, quotatype, range, tmpfd;
104 uid_t startuid, enduid;
106 char *protoname, *cp, *oldoptarg;
107 int eflag = 0, tflag = 0, pflag = 0, ch;
109 char buf[MAXLOGNAME];
114 errx(1, "permission denied");
115 quotatype = USRQUOTA;
117 while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
127 quotatype = GRPQUOTA;
130 quotatype = USRQUOTA;
136 if ((qup = malloc(sizeof(*qup))) == NULL)
137 errx(2, "out of memory");
138 bzero(qup, sizeof(*qup));
141 for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
148 strlcpy(qup->fsname, cp,
149 sizeof(qup->fsname));
152 limp = &qup->dqblk.dqb_bsoftlimit;
155 limp = &qup->dqblk.dqb_bhardlimit;
158 limp = &qup->dqblk.dqb_isoftlimit;
161 limp = &qup->dqblk.dqb_ihardlimit;
164 warnx("incorrect quota specification: "
167 break; /* XXX: report an error */
170 lim = strtoll(cp, NULL, 10);
171 if (lim < 0 || lim > UINT_MAX)
172 errx(1, "invalid limit value: "
174 *limp = (u_int32_t)lim;
177 qup->dqblk.dqb_bsoftlimit =
178 btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
179 qup->dqblk.dqb_bhardlimit =
180 btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
181 if (protoprivs == NULL) {
182 protoprivs = curprivs = qup;
184 curprivs->next = qup;
197 if (protoprivs == NULL) {
198 if ((protoid = getentry(protoname, quotatype)) == -1)
200 protoprivs = getprivs(protoid, quotatype, fspath);
201 for (qup = protoprivs; qup; qup = qup->next) {
202 qup->dqblk.dqb_btime = 0;
203 qup->dqblk.dqb_itime = 0;
206 for (; argc-- > 0; argv++) {
207 if (strspn(*argv, "0123456789-") == strlen(*argv) &&
208 (cp = strchr(*argv, '-')) != NULL) {
210 startuid = atoi(*argv);
212 if (enduid < startuid)
214 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
218 startuid = enduid = 0;
221 for ( ; startuid <= enduid; startuid++) {
223 snprintf(buf, sizeof(buf), "%d",
226 snprintf(buf, sizeof(buf), "%s",
228 if ((id = getentry(buf, quotatype)) < 0)
231 for (qup = protoprivs; qup;
233 curprivs = getprivs(id,
234 quotatype, qup->fsname);
235 if (curprivs == NULL)
243 putprivs(id, quotatype, protoprivs);
248 tmpfd = mkstemp(tmpfil);
249 fchown(tmpfd, getuid(), getgid());
251 protoprivs = getprivs(0, quotatype, fspath);
252 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
254 if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
255 putprivs(0, quotatype, protoprivs);
256 freeprivs(protoprivs);
261 for ( ; argc > 0; argc--, argv++) {
262 if ((id = getentry(*argv, quotatype)) == -1)
264 curprivs = getprivs(id, quotatype, fspath);
265 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
267 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
268 putprivs(id, quotatype, curprivs);
279 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
280 "usage: edquota [-u] [-f fspath] [-p username] username ...",
281 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
283 " edquota -g [-f fspath] [-p groupname] groupname ...",
284 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
286 " edquota [-u] -t [-f fspath]",
287 " edquota -g -t [-f fspath]");
292 * This routine converts a name for a particular quota type to
293 * an identifier. This routine must agree with the kernel routine
294 * getinoquota as to the interpretation of quota types.
297 getentry(name, quotatype)
308 if ((pw = getpwnam(name)))
310 warnx("%s: no such user", name);
313 if ((gr = getgrnam(name)))
315 warnx("%s: no such group", name);
318 warnx("%d: unknown quota type", quotatype);
326 * Collect the requested quota information.
329 getprivs(id, quotatype, fspath)
334 register struct fstab *fs;
335 register struct quotause *qup, *quptail;
336 struct quotause *quphead;
337 int qcmd, qupsize, fd;
339 static int warned = 0;
342 quphead = (struct quotause *)0;
343 qcmd = QCMD(Q_GETQUOTA, quotatype);
344 while ((fs = getfsent())) {
345 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
346 strcmp(fspath, fs->fs_file))
348 if (strcmp(fs->fs_vfstype, "ufs"))
350 if (!hasquota(fs, quotatype, &qfpathname))
352 qupsize = sizeof(*qup) + strlen(qfpathname);
353 if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
354 errx(2, "out of memory");
355 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
356 if (errno == EOPNOTSUPP && !warned) {
358 warnx("warning: quotas are not compiled into this kernel");
361 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
362 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
363 if (fd < 0 && errno != ENOENT) {
364 warn("%s", qfpathname);
368 warnx("creating quota file %s", qfpathname);
370 (void) fchown(fd, getuid(),
371 getentry(quotagroup, GRPQUOTA));
372 (void) fchmod(fd, 0640);
374 lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
375 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
378 * Convert implicit 0 quota (EOF)
379 * into an explicit one (zero'ed dqblk)
381 bzero((caddr_t)&qup->dqblk,
382 sizeof(struct dqblk));
385 case sizeof(struct dqblk): /* OK */
389 warn("read error in %s", qfpathname);
396 strcpy(qup->qfname, qfpathname);
397 strcpy(qup->fsname, fs->fs_file);
410 * Store the requested quota information.
413 putprivs(id, quotatype, quplist)
416 struct quotause *quplist;
418 register struct quotause *qup;
421 qcmd = QCMD(Q_SETQUOTA, quotatype);
422 for (qup = quplist; qup; qup = qup->next) {
423 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
425 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
426 warn("%s", qup->qfname);
428 lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
429 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
430 sizeof (struct dqblk)) {
431 warn("%s", qup->qfname);
439 * Take a list of priviledges and get it edited.
448 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
450 if ((pid = fork()) < 0) {
452 if (errno == EPROCLIM) {
453 warnx("you have too many processes");
456 if (errno == EAGAIN) {
464 register const char *ed;
469 if ((ed = getenv("EDITOR")) == (char *)0)
471 execlp(ed, ed, tmpf, (char *)0);
474 waitpid(pid, &status, 0);
476 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
482 * Convert a quotause list to an ASCII file.
485 writeprivs(quplist, outfd, name, quotatype)
486 struct quotause *quplist;
491 register struct quotause *qup;
495 lseek(outfd, 0, L_SET);
496 if ((fd = fdopen(dup(outfd), "w")) == NULL)
497 err(1, "%s", tmpfil);
498 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
499 for (qup = quplist; qup; qup = qup->next) {
500 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
501 qup->fsname, "kbytes in use:",
502 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
503 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
504 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
505 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
507 (unsigned long)qup->dqblk.dqb_curinodes,
508 (unsigned long)qup->dqblk.dqb_isoftlimit,
509 (unsigned long)qup->dqblk.dqb_ihardlimit);
516 * Merge changes to an ASCII file into a quotause list.
519 readprivs(quplist, inname)
520 struct quotause *quplist;
523 register struct quotause *qup;
525 unsigned long bhardlimit, bsoftlimit, curblocks;
526 unsigned long ihardlimit, isoftlimit, curinodes;
530 char *fsp, line1[BUFSIZ], line2[BUFSIZ];
532 fd = fopen(inname, "r");
534 warnx("can't re-read temp file!!");
538 * Discard title line, then read pairs of lines to process.
540 (void) fgets(line1, sizeof (line1), fd);
541 while (fgets(line1, sizeof (line1), fd) != NULL &&
542 fgets(line2, sizeof (line2), fd) != NULL) {
543 if ((fsp = strtok(line1, " \t:")) == NULL) {
544 warnx("%s: bad format", line1);
547 if ((cp = strtok((char *)0, "\n")) == NULL) {
548 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
552 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
553 &curblocks, &bsoftlimit, &bhardlimit);
555 warnx("%s:%s: bad format", fsp, cp);
558 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
559 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
560 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
561 if ((cp = strtok(line2, "\n")) == NULL) {
562 warnx("%s: %s: bad format", fsp, line2);
566 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
567 &curinodes, &isoftlimit, &ihardlimit);
569 warnx("%s: %s: bad format", fsp, line2);
572 dqblk.dqb_curinodes = curinodes;
573 dqblk.dqb_isoftlimit = isoftlimit;
574 dqblk.dqb_ihardlimit = ihardlimit;
575 for (qup = quplist; qup; qup = qup->next) {
576 if (strcmp(fsp, qup->fsname))
579 * Cause time limit to be reset when the quota
580 * is next used if previously had no soft limit
581 * or were under it, but now have a soft limit
584 if (dqblk.dqb_bsoftlimit &&
585 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
586 (qup->dqblk.dqb_bsoftlimit == 0 ||
587 qup->dqblk.dqb_curblocks <
588 qup->dqblk.dqb_bsoftlimit))
589 qup->dqblk.dqb_btime = 0;
590 if (dqblk.dqb_isoftlimit &&
591 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
592 (qup->dqblk.dqb_isoftlimit == 0 ||
593 qup->dqblk.dqb_curinodes <
594 qup->dqblk.dqb_isoftlimit))
595 qup->dqblk.dqb_itime = 0;
596 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
597 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
598 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
599 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
601 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
602 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
604 warnx("%s: cannot change current allocation", fsp);
610 * Disable quotas for any filesystems that have not been found.
612 for (qup = quplist; qup; qup = qup->next) {
613 if (qup->flags & FOUND) {
614 qup->flags &= ~FOUND;
617 qup->dqblk.dqb_bsoftlimit = 0;
618 qup->dqblk.dqb_bhardlimit = 0;
619 qup->dqblk.dqb_isoftlimit = 0;
620 qup->dqblk.dqb_ihardlimit = 0;
626 * Convert a quotause list to an ASCII file of grace times.
629 writetimes(quplist, outfd, quotatype)
630 struct quotause *quplist;
634 register struct quotause *qup;
638 lseek(outfd, 0, L_SET);
639 if ((fd = fdopen(dup(outfd), "w")) == NULL)
640 err(1, "%s", tmpfil);
641 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
642 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
643 qfextension[quotatype]);
644 for (qup = quplist; qup; qup = qup->next) {
645 fprintf(fd, "%s: block grace period: %s, ",
646 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
647 fprintf(fd, "file grace period: %s\n",
648 cvtstoa(qup->dqblk.dqb_itime));
655 * Merge changes of grace times in an ASCII file into a quotause list.
658 readtimes(quplist, inname)
659 struct quotause *quplist;
662 register struct quotause *qup;
666 time_t itime, btime, iseconds, bseconds;
667 long l_itime, l_btime;
668 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
670 fd = fopen(inname, "r");
672 warnx("can't re-read temp file!!");
676 * Discard two title lines, then read lines to process.
678 (void) fgets(line1, sizeof (line1), fd);
679 (void) fgets(line1, sizeof (line1), fd);
680 while (fgets(line1, sizeof (line1), fd) != NULL) {
681 if ((fsp = strtok(line1, " \t:")) == NULL) {
682 warnx("%s: bad format", line1);
685 if ((cp = strtok((char *)0, "\n")) == NULL) {
686 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
690 " block grace period: %ld %s file grace period: %ld %s",
691 &l_btime, bunits, &l_itime, iunits);
693 warnx("%s:%s: bad format", fsp, cp);
698 if (cvtatos(btime, bunits, &bseconds) == 0)
700 if (cvtatos(itime, iunits, &iseconds) == 0)
702 for (qup = quplist; qup; qup = qup->next) {
703 if (strcmp(fsp, qup->fsname))
705 qup->dqblk.dqb_btime = bseconds;
706 qup->dqblk.dqb_itime = iseconds;
713 * reset default grace periods for any filesystems
714 * that have not been found.
716 for (qup = quplist; qup; qup = qup->next) {
717 if (qup->flags & FOUND) {
718 qup->flags &= ~FOUND;
721 qup->dqblk.dqb_btime = 0;
722 qup->dqblk.dqb_itime = 0;
728 * Convert seconds to ASCII times.
736 if (secs % (24 * 60 * 60) == 0) {
737 secs /= 24 * 60 * 60;
738 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
739 } else if (secs % (60 * 60) == 0) {
741 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
742 } else if (secs % 60 == 0) {
744 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
746 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
751 * Convert ASCII input times to seconds.
754 cvtatos(period, units, seconds)
760 if (bcmp(units, "second", 6) == 0)
762 else if (bcmp(units, "minute", 6) == 0)
763 *seconds = period * 60;
764 else if (bcmp(units, "hour", 4) == 0)
765 *seconds = period * 60 * 60;
766 else if (bcmp(units, "day", 3) == 0)
767 *seconds = period * 24 * 60 * 60;
769 printf("%s: bad units, specify %s\n", units,
770 "days, hours, minutes, or seconds");
777 * Free a list of quotause structures.
781 struct quotause *quplist;
783 register struct quotause *qup, *nextqup;
785 for (qup = quplist; qup; qup = nextqup) {
792 * Check whether a string is completely composed of digits.
796 register const char *s;
804 } while ((c = *s++));
809 * Check to see if a particular quota is to be enabled.
812 hasquota(fs, type, qfnamep)
813 register struct fstab *fs;
819 static char initname, usrname[100], grpname[100];
820 static char buf[BUFSIZ];
823 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
824 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
827 strcpy(buf, fs->fs_mntops);
828 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
829 if ((cp = index(opt, '=')))
831 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
833 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
842 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);