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 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 __P((const 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((const char *, int));
92 struct quotause *getprivs __P((long, int, char *));
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;
118 errx(1, "permission denied");
119 quotatype = USRQUOTA;
120 while ((ch = getopt(argc, argv, "ugtf:p:")) != -1) {
130 quotatype = GRPQUOTA;
133 quotatype = USRQUOTA;
145 if ((protoid = getentry(protoname, quotatype)) == -1)
147 protoprivs = getprivs(protoid, quotatype, fspath);
148 for (qup = protoprivs; qup; qup = qup->next) {
149 qup->dqblk.dqb_btime = 0;
150 qup->dqblk.dqb_itime = 0;
153 if (isdigit(*argv[0]) &&
154 (cp = strchr(*argv, '-')) != NULL) {
156 startuid = atoi(*argv);
158 if (enduid < startuid)
160 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
162 for ( ; startuid <= enduid; startuid++) {
163 snprintf(buf, sizeof(buf), "%d",
165 if ((id = getentry(buf, quotatype)) < 0)
167 putprivs(id, quotatype, protoprivs);
171 if ((id = getentry(*argv++, quotatype)) < 0)
173 putprivs(id, quotatype, protoprivs);
177 tmpfd = mkstemp(tmpfil);
178 fchown(tmpfd, getuid(), getgid());
180 protoprivs = getprivs(0, quotatype, fspath);
181 if (writetimes(protoprivs, tmpfd, quotatype) == 0)
183 if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
184 putprivs(0, quotatype, protoprivs);
185 freeprivs(protoprivs);
190 for ( ; argc > 0; argc--, argv++) {
191 if ((id = getentry(*argv, quotatype)) == -1)
193 curprivs = getprivs(id, quotatype, fspath);
194 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
196 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
197 putprivs(id, quotatype, curprivs);
208 fprintf(stderr, "%s\n%s\n%s\n%s\n",
209 "usage: edquota [-u] [-f fspath] [-p username] username ...",
210 " edquota -g [-f fspath] [-p groupname] groupname ...",
211 " edquota [-u] -t [-f fspath]",
212 " edquota -g -t [-f fspath]");
217 * This routine converts a name for a particular quota type to
218 * an identifier. This routine must agree with the kernel routine
219 * getinoquota as to the interpretation of quota types.
222 getentry(name, quotatype)
233 if ((pw = getpwnam(name)))
235 warnx("%s: no such user", name);
238 if ((gr = getgrnam(name)))
240 warnx("%s: no such group", name);
243 warnx("%d: unknown quota type", quotatype);
251 * Collect the requested quota information.
254 getprivs(id, quotatype, fspath)
259 register struct fstab *fs;
260 register struct quotause *qup, *quptail;
261 struct quotause *quphead;
262 int qcmd, qupsize, fd;
264 static int warned = 0;
267 quphead = (struct quotause *)0;
268 qcmd = QCMD(Q_GETQUOTA, quotatype);
269 while ((fs = getfsent())) {
270 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
271 strcmp(fspath, fs->fs_file))
273 if (strcmp(fs->fs_vfstype, "ufs"))
275 if (!hasquota(fs, quotatype, &qfpathname))
277 qupsize = sizeof(*qup) + strlen(qfpathname);
278 if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
279 errx(2, "out of memory");
280 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
281 if (errno == EOPNOTSUPP && !warned) {
283 warnx("warning: quotas are not compiled into this kernel");
286 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
287 fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
288 if (fd < 0 && errno != ENOENT) {
289 warn("%s", qfpathname);
293 warnx("creating quota file %s", qfpathname);
295 (void) fchown(fd, getuid(),
296 getentry(quotagroup, GRPQUOTA));
297 (void) fchmod(fd, 0640);
299 lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
300 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
303 * Convert implicit 0 quota (EOF)
304 * into an explicit one (zero'ed dqblk)
306 bzero((caddr_t)&qup->dqblk,
307 sizeof(struct dqblk));
310 case sizeof(struct dqblk): /* OK */
314 warn("read error in %s", qfpathname);
321 strcpy(qup->qfname, qfpathname);
322 strcpy(qup->fsname, fs->fs_file);
335 * Store the requested quota information.
338 putprivs(id, quotatype, quplist)
341 struct quotause *quplist;
343 register struct quotause *qup;
346 qcmd = QCMD(Q_SETQUOTA, quotatype);
347 for (qup = quplist; qup; qup = qup->next) {
348 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
350 if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
351 warn("%s", qup->qfname);
353 lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
354 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
355 sizeof (struct dqblk)) {
356 warn("%s", qup->qfname);
364 * Take a list of priviledges and get it edited.
373 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
375 if ((pid = fork()) < 0) {
377 if (errno == EPROCLIM) {
378 warnx("you have too many processes");
381 if (errno == EAGAIN) {
389 register const char *ed;
394 if ((ed = getenv("EDITOR")) == (char *)0)
396 execlp(ed, ed, tmpf, (char *)0);
399 waitpid(pid, &status, 0);
401 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
407 * Convert a quotause list to an ASCII file.
410 writeprivs(quplist, outfd, name, quotatype)
411 struct quotause *quplist;
416 register struct quotause *qup;
420 lseek(outfd, 0, L_SET);
421 if ((fd = fdopen(dup(outfd), "w")) == NULL)
422 err(1, "%s", tmpfil);
423 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
424 for (qup = quplist; qup; qup = qup->next) {
425 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
426 qup->fsname, "blocks in use:",
427 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
428 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
429 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
430 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
432 (unsigned long)qup->dqblk.dqb_curinodes,
433 (unsigned long)qup->dqblk.dqb_isoftlimit,
434 (unsigned long)qup->dqblk.dqb_ihardlimit);
441 * Merge changes to an ASCII file into a quotause list.
444 readprivs(quplist, inname)
445 struct quotause *quplist;
448 register struct quotause *qup;
450 unsigned long bhardlimit, bsoftlimit, curblocks;
451 unsigned long ihardlimit, isoftlimit, curinodes;
455 char *fsp, line1[BUFSIZ], line2[BUFSIZ];
457 fd = fopen(inname, "r");
459 warnx("can't re-read temp file!!");
463 * Discard title line, then read pairs of lines to process.
465 (void) fgets(line1, sizeof (line1), fd);
466 while (fgets(line1, sizeof (line1), fd) != NULL &&
467 fgets(line2, sizeof (line2), fd) != NULL) {
468 if ((fsp = strtok(line1, " \t:")) == NULL) {
469 warnx("%s: bad format", line1);
472 if ((cp = strtok((char *)0, "\n")) == NULL) {
473 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
477 " blocks in use: %lu, limits (soft = %lu, hard = %lu)",
478 &curblocks, &bsoftlimit, &bhardlimit);
480 warnx("%s:%s: bad format", fsp, cp);
483 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
484 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
485 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
486 if ((cp = strtok(line2, "\n")) == NULL) {
487 warnx("%s: %s: bad format", fsp, line2);
491 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
492 &curinodes, &isoftlimit, &ihardlimit);
494 warnx("%s: %s: bad format", fsp, line2);
497 dqblk.dqb_curinodes = curinodes;
498 dqblk.dqb_isoftlimit = isoftlimit;
499 dqblk.dqb_ihardlimit = ihardlimit;
500 for (qup = quplist; qup; qup = qup->next) {
501 if (strcmp(fsp, qup->fsname))
504 * Cause time limit to be reset when the quota
505 * is next used if previously had no soft limit
506 * or were under it, but now have a soft limit
509 if (dqblk.dqb_bsoftlimit &&
510 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
511 (qup->dqblk.dqb_bsoftlimit == 0 ||
512 qup->dqblk.dqb_curblocks <
513 qup->dqblk.dqb_bsoftlimit))
514 qup->dqblk.dqb_btime = 0;
515 if (dqblk.dqb_isoftlimit &&
516 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
517 (qup->dqblk.dqb_isoftlimit == 0 ||
518 qup->dqblk.dqb_curinodes <
519 qup->dqblk.dqb_isoftlimit))
520 qup->dqblk.dqb_itime = 0;
521 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
522 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
523 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
524 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
526 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
527 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
529 warnx("%s: cannot change current allocation", fsp);
535 * Disable quotas for any filesystems that have not been found.
537 for (qup = quplist; qup; qup = qup->next) {
538 if (qup->flags & FOUND) {
539 qup->flags &= ~FOUND;
542 qup->dqblk.dqb_bsoftlimit = 0;
543 qup->dqblk.dqb_bhardlimit = 0;
544 qup->dqblk.dqb_isoftlimit = 0;
545 qup->dqblk.dqb_ihardlimit = 0;
551 * Convert a quotause list to an ASCII file of grace times.
554 writetimes(quplist, outfd, quotatype)
555 struct quotause *quplist;
559 register struct quotause *qup;
563 lseek(outfd, 0, L_SET);
564 if ((fd = fdopen(dup(outfd), "w")) == NULL)
565 err(1, "%s", tmpfil);
566 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
567 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
568 qfextension[quotatype]);
569 for (qup = quplist; qup; qup = qup->next) {
570 fprintf(fd, "%s: block grace period: %s, ",
571 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
572 fprintf(fd, "file grace period: %s\n",
573 cvtstoa(qup->dqblk.dqb_itime));
580 * Merge changes of grace times in an ASCII file into a quotause list.
583 readtimes(quplist, inname)
584 struct quotause *quplist;
587 register struct quotause *qup;
591 time_t itime, btime, iseconds, bseconds;
592 long l_itime, l_btime;
593 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
595 fd = fopen(inname, "r");
597 warnx("can't re-read temp file!!");
601 * Discard two title lines, then read lines to process.
603 (void) fgets(line1, sizeof (line1), fd);
604 (void) fgets(line1, sizeof (line1), fd);
605 while (fgets(line1, sizeof (line1), fd) != NULL) {
606 if ((fsp = strtok(line1, " \t:")) == NULL) {
607 warnx("%s: bad format", line1);
610 if ((cp = strtok((char *)0, "\n")) == NULL) {
611 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
615 " block grace period: %ld %s file grace period: %ld %s",
616 &l_btime, bunits, &l_itime, iunits);
618 warnx("%s:%s: bad format", fsp, cp);
623 if (cvtatos(btime, bunits, &bseconds) == 0)
625 if (cvtatos(itime, iunits, &iseconds) == 0)
627 for (qup = quplist; qup; qup = qup->next) {
628 if (strcmp(fsp, qup->fsname))
630 qup->dqblk.dqb_btime = bseconds;
631 qup->dqblk.dqb_itime = iseconds;
638 * reset default grace periods for any filesystems
639 * that have not been found.
641 for (qup = quplist; qup; qup = qup->next) {
642 if (qup->flags & FOUND) {
643 qup->flags &= ~FOUND;
646 qup->dqblk.dqb_btime = 0;
647 qup->dqblk.dqb_itime = 0;
653 * Convert seconds to ASCII times.
661 if (secs % (24 * 60 * 60) == 0) {
662 secs /= 24 * 60 * 60;
663 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
664 } else if (secs % (60 * 60) == 0) {
666 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
667 } else if (secs % 60 == 0) {
669 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
671 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
676 * Convert ASCII input times to seconds.
679 cvtatos(period, units, seconds)
685 if (bcmp(units, "second", 6) == 0)
687 else if (bcmp(units, "minute", 6) == 0)
688 *seconds = period * 60;
689 else if (bcmp(units, "hour", 4) == 0)
690 *seconds = period * 60 * 60;
691 else if (bcmp(units, "day", 3) == 0)
692 *seconds = period * 24 * 60 * 60;
694 printf("%s: bad units, specify %s\n", units,
695 "days, hours, minutes, or seconds");
702 * Free a list of quotause structures.
706 struct quotause *quplist;
708 register struct quotause *qup, *nextqup;
710 for (qup = quplist; qup; qup = nextqup) {
717 * Check whether a string is completely composed of digits.
721 register const char *s;
729 } while ((c = *s++));
734 * Check to see if a particular quota is to be enabled.
737 hasquota(fs, type, qfnamep)
738 register struct fstab *fs;
744 static char initname, usrname[100], grpname[100];
745 static char buf[BUFSIZ];
748 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
749 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
752 strcpy(buf, fs->fs_mntops);
753 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
754 if ((cp = index(opt, '=')))
756 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
758 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
767 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);