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
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)edquota.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.sbin/edquota/edquota.c,v 1.9.2.6 2002/10/31 22:38:43 iedowse Exp $
44 #include <sys/param.h>
48 #include <vfs/ufs/quota.h>
60 #include "pathnames.h"
62 const char *qfname
= QUOTAFILENAME
;
63 const char *qfextension
[] = INITQFNAMES
;
64 const char *quotagroup
= QUOTAGROUP
;
65 char tmpfil
[] = _PATH_TMP
;
68 struct quotause
*next
;
70 struct ufs_dqblk dqblk
;
71 char fsname
[MAXPATHLEN
+ 1];
72 char qfname
[1]; /* actually longer */
76 int alldigits(const char *s
);
77 int cvtatos(time_t, char *, time_t *);
78 char *cvtstoa(time_t);
80 void freeprivs(struct quotause
*);
81 int getentry(const char *, int);
82 struct quotause
*getprivs(long, int, char *);
83 int hasquota(struct fstab
*, int, char **);
84 void putprivs(long, int, struct quotause
*);
85 int readprivs(struct quotause
*, char *);
86 int readtimes(struct quotause
*, char *);
87 static void usage(void);
88 int writetimes(struct quotause
*, int, int);
89 int writeprivs(struct quotause
*, int, char *, int);
92 main(int argc
, char **argv
)
94 struct quotause
*qup
, *protoprivs
, *curprivs
;
97 int i
, quotatype
, range
, tmpfd
;
98 uid_t startuid
, enduid
;
100 char *protoname
, *cp
, *oldoptarg
, ch
;
101 int eflag
= 0, tflag
= 0, pflag
= 0;
103 char buf
[MAXLOGNAME
];
108 errx(1, "permission denied");
109 quotatype
= USRQUOTA
;
113 while ((ch
= getopt(argc
, argv
, "ugtf:p:e:")) != -1) {
123 quotatype
= GRPQUOTA
;
126 quotatype
= USRQUOTA
;
132 if ((qup
= malloc(sizeof(*qup
))) == NULL
)
133 errx(2, "out of memory");
134 bzero(qup
, sizeof(*qup
));
137 for (cp
= optarg
; (cp
= strsep(&optarg
, ":")) != NULL
;
144 strlcpy(qup
->fsname
, cp
,
145 sizeof(qup
->fsname
));
148 limp
= &qup
->dqblk
.dqb_bsoftlimit
;
151 limp
= &qup
->dqblk
.dqb_bhardlimit
;
154 limp
= &qup
->dqblk
.dqb_isoftlimit
;
157 limp
= &qup
->dqblk
.dqb_ihardlimit
;
160 warnx("incorrect quota specification: "
163 break; /* XXX: report an error */
166 lim
= strtoll(cp
, NULL
, 10);
167 if (lim
< 0 || lim
> UINT_MAX
)
168 errx(1, "invalid limit value: "
170 *limp
= (u_int32_t
)lim
;
173 qup
->dqblk
.dqb_bsoftlimit
=
174 btodb((off_t
)qup
->dqblk
.dqb_bsoftlimit
* 1024);
175 qup
->dqblk
.dqb_bhardlimit
=
176 btodb((off_t
)qup
->dqblk
.dqb_bhardlimit
* 1024);
177 if (protoprivs
== NULL
) {
178 protoprivs
= curprivs
= qup
;
180 curprivs
->next
= qup
;
193 if (protoprivs
== NULL
) {
194 if ((protoid
= getentry(protoname
, quotatype
)) == -1)
196 protoprivs
= getprivs(protoid
, quotatype
, fspath
);
197 for (qup
= protoprivs
; qup
; qup
= qup
->next
) {
198 qup
->dqblk
.dqb_btime
= 0;
199 qup
->dqblk
.dqb_itime
= 0;
202 for (; argc
-- > 0; argv
++) {
203 if (strspn(*argv
, "0123456789-") == strlen(*argv
) &&
204 (cp
= strchr(*argv
, '-')) != NULL
) {
206 startuid
= atoi(*argv
);
208 if (enduid
< startuid
)
210 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
214 startuid
= enduid
= 0;
217 for ( ; startuid
<= enduid
; startuid
++) {
219 snprintf(buf
, sizeof(buf
), "%d",
222 snprintf(buf
, sizeof(buf
), "%s",
224 if ((id
= getentry(buf
, quotatype
)) < 0)
227 for (qup
= protoprivs
; qup
;
229 curprivs
= getprivs(id
,
230 quotatype
, qup
->fsname
);
231 if (curprivs
== NULL
)
239 putprivs(id
, quotatype
, protoprivs
);
244 tmpfd
= mkstemp(tmpfil
);
245 fchown(tmpfd
, getuid(), getgid());
247 protoprivs
= getprivs(0, quotatype
, fspath
);
248 if (writetimes(protoprivs
, tmpfd
, quotatype
) == 0)
250 if (editit(tmpfil
) && readtimes(protoprivs
, tmpfil
))
251 putprivs(0, quotatype
, protoprivs
);
252 freeprivs(protoprivs
);
257 for ( ; argc
> 0; argc
--, argv
++) {
258 if ((id
= getentry(*argv
, quotatype
)) == -1)
260 curprivs
= getprivs(id
, quotatype
, fspath
);
261 if (writeprivs(curprivs
, tmpfd
, *argv
, quotatype
) == 0)
263 if (editit(tmpfil
) && readprivs(curprivs
, tmpfil
))
264 putprivs(id
, quotatype
, curprivs
);
275 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
276 "usage: edquota [-u] [-f fspath] [-p username] username ...",
277 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
279 " edquota -g [-f fspath] [-p groupname] groupname ...",
280 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
282 " edquota [-u] -t [-f fspath]",
283 " edquota -g -t [-f fspath]");
288 * This routine converts a name for a particular quota type to
289 * an identifier. This routine must agree with the kernel routine
290 * getinoquota as to the interpretation of quota types.
293 getentry(const char *name
, int quotatype
)
302 if ((pw
= getpwnam(name
)))
304 warnx("%s: no such user", name
);
307 if ((gr
= getgrnam(name
)))
309 warnx("%s: no such group", name
);
312 warnx("%d: unknown quota type", quotatype
);
320 * Collect the requested quota information.
323 getprivs(long id
, int quotatype
, char *fspath
)
326 struct quotause
*qup
, *quptail
;
327 struct quotause
*quphead
;
328 int qcmd
, qupsize
, fd
;
330 static int warned
= 0;
333 quphead
= quptail
= NULL
;
334 qcmd
= QCMD(Q_GETQUOTA
, quotatype
);
335 while ((fs
= getfsent())) {
336 if (fspath
&& *fspath
&& strcmp(fspath
, fs
->fs_spec
) &&
337 strcmp(fspath
, fs
->fs_file
))
339 if (strcmp(fs
->fs_vfstype
, "ufs"))
341 if (!hasquota(fs
, quotatype
, &qfpathname
))
343 qupsize
= sizeof(*qup
) + strlen(qfpathname
);
344 if ((qup
= (struct quotause
*)malloc(qupsize
)) == NULL
)
345 errx(2, "out of memory");
346 if (quotactl(fs
->fs_file
, qcmd
, id
, &qup
->dqblk
) != 0) {
347 if (errno
== EOPNOTSUPP
&& !warned
) {
349 warnx("warning: quotas are not compiled into this kernel");
352 if ((fd
= open(qfpathname
, O_RDONLY
)) < 0) {
353 fd
= open(qfpathname
, O_RDWR
|O_CREAT
, 0640);
354 if (fd
< 0 && errno
!= ENOENT
) {
355 warn("%s", qfpathname
);
359 warnx("creating quota file %s", qfpathname
);
362 getentry(quotagroup
, GRPQUOTA
));
365 lseek(fd
, (long)(id
* sizeof(struct ufs_dqblk
)), L_SET
);
366 switch (read(fd
, &qup
->dqblk
, sizeof(struct ufs_dqblk
))) {
369 * Convert implicit 0 quota (EOF)
370 * into an explicit one (zero'ed dqblk)
372 bzero((caddr_t
)&qup
->dqblk
,
373 sizeof(struct ufs_dqblk
));
376 case sizeof(struct ufs_dqblk
): /* OK */
380 warn("read error in %s", qfpathname
);
387 strcpy(qup
->qfname
, qfpathname
);
388 strcpy(qup
->fsname
, fs
->fs_file
);
401 * Store the requested quota information.
404 putprivs(long id
, int quotatype
, struct quotause
*quplist
)
406 struct quotause
*qup
;
409 qcmd
= QCMD(Q_SETQUOTA
, quotatype
);
410 for (qup
= quplist
; qup
; qup
= qup
->next
) {
411 if (quotactl(qup
->fsname
, qcmd
, id
, &qup
->dqblk
) == 0)
413 if ((fd
= open(qup
->qfname
, O_WRONLY
)) < 0) {
414 warn("%s", qup
->qfname
);
416 lseek(fd
, (long)id
* (long)sizeof(struct ufs_dqblk
), 0);
417 if (write(fd
, &qup
->dqblk
, sizeof(struct ufs_dqblk
)) !=
418 sizeof(struct ufs_dqblk
)) {
419 warn("%s", qup
->qfname
);
427 * Take a list of priviledges and get it edited.
435 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
437 if ((pid
= fork()) < 0) {
439 if (errno
== EPROCLIM
) {
440 warnx("you have too many processes");
443 if (errno
== EAGAIN
) {
456 if ((ed
= getenv("EDITOR")) == NULL
)
458 execlp(ed
, ed
, tmpf
, NULL
);
461 waitpid(pid
, &status
, 0);
463 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
469 * Convert a quotause list to an ASCII file.
472 writeprivs(struct quotause
*quplist
, int outfd
, char *name
, int quotatype
)
474 struct quotause
*qup
;
478 lseek(outfd
, 0, L_SET
);
479 if ((fd
= fdopen(dup(outfd
), "w")) == NULL
)
480 err(1, "%s", tmpfil
);
481 fprintf(fd
, "Quotas for %s %s:\n", qfextension
[quotatype
], name
);
482 for (qup
= quplist
; qup
; qup
= qup
->next
) {
483 fprintf(fd
, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
484 qup
->fsname
, "kbytes in use:",
485 (unsigned long)(dbtob(qup
->dqblk
.dqb_curblocks
) / 1024),
486 (unsigned long)(dbtob(qup
->dqblk
.dqb_bsoftlimit
) / 1024),
487 (unsigned long)(dbtob(qup
->dqblk
.dqb_bhardlimit
) / 1024));
488 fprintf(fd
, "%s %lu, limits (soft = %lu, hard = %lu)\n",
490 (unsigned long)qup
->dqblk
.dqb_curinodes
,
491 (unsigned long)qup
->dqblk
.dqb_isoftlimit
,
492 (unsigned long)qup
->dqblk
.dqb_ihardlimit
);
499 * Merge changes to an ASCII file into a quotause list.
502 readprivs(struct quotause
*quplist
, char *inname
)
504 struct quotause
*qup
;
506 unsigned long bhardlimit
, bsoftlimit
, curblocks
;
507 unsigned long ihardlimit
, isoftlimit
, curinodes
;
510 struct ufs_dqblk dqblk
;
511 char *fsp
, line1
[BUFSIZ
], line2
[BUFSIZ
];
513 fd
= fopen(inname
, "r");
515 warnx("can't re-read temp file!!");
519 * Discard title line, then read pairs of lines to process.
521 fgets(line1
, sizeof(line1
), fd
);
522 while (fgets(line1
, sizeof(line1
), fd
) != NULL
&&
523 fgets(line2
, sizeof(line2
), fd
) != NULL
) {
524 if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
525 warnx("%s: bad format", line1
);
528 if ((cp
= strtok(NULL
, "\n")) == NULL
) {
529 warnx("%s: %s: bad format", fsp
, &fsp
[strlen(fsp
) + 1]);
533 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
534 &curblocks
, &bsoftlimit
, &bhardlimit
);
536 warnx("%s:%s: bad format", fsp
, cp
);
539 dqblk
.dqb_curblocks
= btodb((off_t
)curblocks
* 1024);
540 dqblk
.dqb_bsoftlimit
= btodb((off_t
)bsoftlimit
* 1024);
541 dqblk
.dqb_bhardlimit
= btodb((off_t
)bhardlimit
* 1024);
542 if ((cp
= strtok(line2
, "\n")) == NULL
) {
543 warnx("%s: %s: bad format", fsp
, line2
);
547 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
548 &curinodes
, &isoftlimit
, &ihardlimit
);
550 warnx("%s: %s: bad format", fsp
, line2
);
553 dqblk
.dqb_curinodes
= curinodes
;
554 dqblk
.dqb_isoftlimit
= isoftlimit
;
555 dqblk
.dqb_ihardlimit
= ihardlimit
;
556 for (qup
= quplist
; qup
; qup
= qup
->next
) {
557 if (strcmp(fsp
, qup
->fsname
))
560 * Cause time limit to be reset when the quota
561 * is next used if previously had no soft limit
562 * or were under it, but now have a soft limit
565 if (dqblk
.dqb_bsoftlimit
&&
566 qup
->dqblk
.dqb_curblocks
>= dqblk
.dqb_bsoftlimit
&&
567 (qup
->dqblk
.dqb_bsoftlimit
== 0 ||
568 qup
->dqblk
.dqb_curblocks
<
569 qup
->dqblk
.dqb_bsoftlimit
))
570 qup
->dqblk
.dqb_btime
= 0;
571 if (dqblk
.dqb_isoftlimit
&&
572 qup
->dqblk
.dqb_curinodes
>= dqblk
.dqb_isoftlimit
&&
573 (qup
->dqblk
.dqb_isoftlimit
== 0 ||
574 qup
->dqblk
.dqb_curinodes
<
575 qup
->dqblk
.dqb_isoftlimit
))
576 qup
->dqblk
.dqb_itime
= 0;
577 qup
->dqblk
.dqb_bsoftlimit
= dqblk
.dqb_bsoftlimit
;
578 qup
->dqblk
.dqb_bhardlimit
= dqblk
.dqb_bhardlimit
;
579 qup
->dqblk
.dqb_isoftlimit
= dqblk
.dqb_isoftlimit
;
580 qup
->dqblk
.dqb_ihardlimit
= dqblk
.dqb_ihardlimit
;
582 if (dqblk
.dqb_curblocks
== qup
->dqblk
.dqb_curblocks
&&
583 dqblk
.dqb_curinodes
== qup
->dqblk
.dqb_curinodes
)
585 warnx("%s: cannot change current allocation", fsp
);
591 * Disable quotas for any filesystems that have not been found.
593 for (qup
= quplist
; qup
; qup
= qup
->next
) {
594 if (qup
->flags
& FOUND
) {
595 qup
->flags
&= ~FOUND
;
598 qup
->dqblk
.dqb_bsoftlimit
= 0;
599 qup
->dqblk
.dqb_bhardlimit
= 0;
600 qup
->dqblk
.dqb_isoftlimit
= 0;
601 qup
->dqblk
.dqb_ihardlimit
= 0;
607 * Convert a quotause list to an ASCII file of grace times.
610 writetimes(struct quotause
*quplist
, int outfd
, int quotatype
)
612 struct quotause
*qup
;
616 lseek(outfd
, 0, L_SET
);
617 if ((fd
= fdopen(dup(outfd
), "w")) == NULL
)
618 err(1, "%s", tmpfil
);
619 fprintf(fd
, "Time units may be: days, hours, minutes, or seconds\n");
620 fprintf(fd
, "Grace period before enforcing soft limits for %ss:\n",
621 qfextension
[quotatype
]);
622 for (qup
= quplist
; qup
; qup
= qup
->next
) {
623 fprintf(fd
, "%s: block grace period: %s, ",
624 qup
->fsname
, cvtstoa(qup
->dqblk
.dqb_btime
));
625 fprintf(fd
, "file grace period: %s\n",
626 cvtstoa(qup
->dqblk
.dqb_itime
));
633 * Merge changes of grace times in an ASCII file into a quotause list.
636 readtimes(struct quotause
*quplist
, char *inname
)
638 struct quotause
*qup
;
642 time_t itime
, btime
, iseconds
, bseconds
;
643 long l_itime
, l_btime
;
644 char *fsp
, bunits
[10], iunits
[10], line1
[BUFSIZ
];
646 fd
= fopen(inname
, "r");
648 warnx("can't re-read temp file!!");
652 * Discard two title lines, then read lines to process.
654 fgets(line1
, sizeof(line1
), fd
);
655 fgets(line1
, sizeof(line1
), fd
);
656 while (fgets(line1
, sizeof(line1
), fd
) != NULL
) {
657 if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
658 warnx("%s: bad format", line1
);
661 if ((cp
= strtok(NULL
, "\n")) == NULL
) {
662 warnx("%s: %s: bad format", fsp
, &fsp
[strlen(fsp
) + 1]);
666 " block grace period: %ld %s file grace period: %ld %s",
667 &l_btime
, bunits
, &l_itime
, iunits
);
669 warnx("%s:%s: bad format", fsp
, cp
);
674 if (cvtatos(btime
, bunits
, &bseconds
) == 0)
676 if (cvtatos(itime
, iunits
, &iseconds
) == 0)
678 for (qup
= quplist
; qup
; qup
= qup
->next
) {
679 if (strcmp(fsp
, qup
->fsname
))
681 qup
->dqblk
.dqb_btime
= bseconds
;
682 qup
->dqblk
.dqb_itime
= iseconds
;
689 * reset default grace periods for any filesystems
690 * that have not been found.
692 for (qup
= quplist
; qup
; qup
= qup
->next
) {
693 if (qup
->flags
& FOUND
) {
694 qup
->flags
&= ~FOUND
;
697 qup
->dqblk
.dqb_btime
= 0;
698 qup
->dqblk
.dqb_itime
= 0;
704 * Convert seconds to ASCII times.
711 if (secs
% (24 * 60 * 60) == 0) {
712 secs
/= 24 * 60 * 60;
713 sprintf(buf
, "%ld day%s", (long)secs
, secs
== 1 ? "" : "s");
714 } else if (secs
% (60 * 60) == 0) {
716 sprintf(buf
, "%ld hour%s", (long)secs
, secs
== 1 ? "" : "s");
717 } else if (secs
% 60 == 0) {
719 sprintf(buf
, "%ld minute%s", (long)secs
, secs
== 1 ? "" : "s");
721 sprintf(buf
, "%ld second%s", (long)secs
, secs
== 1 ? "" : "s");
726 * Convert ASCII input times to seconds.
729 cvtatos(time_t period
, char *units
, time_t *seconds
)
732 if (bcmp(units
, "second", 6) == 0)
734 else if (bcmp(units
, "minute", 6) == 0)
735 *seconds
= period
* 60;
736 else if (bcmp(units
, "hour", 4) == 0)
737 *seconds
= period
* 60 * 60;
738 else if (bcmp(units
, "day", 3) == 0)
739 *seconds
= period
* 24 * 60 * 60;
741 printf("%s: bad units, specify %s\n", units
,
742 "days, hours, minutes, or seconds");
749 * Free a list of quotause structures.
752 freeprivs(struct quotause
*quplist
)
754 struct quotause
*qup
, *nextqup
;
756 for (qup
= quplist
; qup
; qup
= nextqup
) {
763 * Check whether a string is completely composed of digits.
766 alldigits(const char *s
)
774 } while ((c
= *s
++));
779 * Check to see if a particular quota is to be enabled.
782 hasquota(struct fstab
*fs
, int type
, char **qfnamep
)
786 static char initname
, usrname
[100], grpname
[100];
787 static char buf
[BUFSIZ
];
790 sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
791 sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
794 strcpy(buf
, fs
->fs_mntops
);
795 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
796 if ((cp
= strchr(opt
, '=')))
798 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
800 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
809 sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);