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 $
39 * $DragonFly: src/usr.sbin/edquota/edquota.c,v 1.8 2006/04/03 01:58:49 dillon Exp $
45 #include <sys/param.h>
49 #include <vfs/ufs/quota.h>
61 #include "pathnames.h"
63 const char *qfname
= QUOTAFILENAME
;
64 const char *qfextension
[] = INITQFNAMES
;
65 const char *quotagroup
= QUOTAGROUP
;
66 char tmpfil
[] = _PATH_TMP
;
69 struct quotause
*next
;
71 struct ufs_dqblk dqblk
;
72 char fsname
[MAXPATHLEN
+ 1];
73 char qfname
[1]; /* actually longer */
77 int alldigits(const char *s
);
78 int cvtatos(time_t, char *, time_t *);
79 char *cvtstoa(time_t);
81 void freeprivs(struct quotause
*);
82 int getentry(const char *, int);
83 struct quotause
*getprivs(long, int, char *);
84 int hasquota(struct fstab
*, int, char **);
85 void putprivs(long, int, struct quotause
*);
86 int readprivs(struct quotause
*, char *);
87 int readtimes(struct quotause
*, char *);
88 static void usage(void);
89 int writetimes(struct quotause
*, int, int);
90 int writeprivs(struct quotause
*, int, char *, int);
93 main(int argc
, char **argv
)
95 struct quotause
*qup
, *protoprivs
, *curprivs
;
98 int i
, quotatype
, range
, tmpfd
;
99 uid_t startuid
, enduid
;
101 char *protoname
, *cp
, *oldoptarg
, ch
;
102 int eflag
= 0, tflag
= 0, pflag
= 0;
104 char buf
[MAXLOGNAME
];
109 errx(1, "permission denied");
110 quotatype
= USRQUOTA
;
112 while ((ch
= getopt(argc
, argv
, "ugtf:p:e:")) != -1) {
122 quotatype
= GRPQUOTA
;
125 quotatype
= USRQUOTA
;
131 if ((qup
= malloc(sizeof(*qup
))) == NULL
)
132 errx(2, "out of memory");
133 bzero(qup
, sizeof(*qup
));
136 for (cp
= optarg
; (cp
= strsep(&optarg
, ":")) != NULL
;
143 strlcpy(qup
->fsname
, cp
,
144 sizeof(qup
->fsname
));
147 limp
= &qup
->dqblk
.dqb_bsoftlimit
;
150 limp
= &qup
->dqblk
.dqb_bhardlimit
;
153 limp
= &qup
->dqblk
.dqb_isoftlimit
;
156 limp
= &qup
->dqblk
.dqb_ihardlimit
;
159 warnx("incorrect quota specification: "
162 break; /* XXX: report an error */
165 lim
= strtoll(cp
, NULL
, 10);
166 if (lim
< 0 || lim
> UINT_MAX
)
167 errx(1, "invalid limit value: "
169 *limp
= (u_int32_t
)lim
;
172 qup
->dqblk
.dqb_bsoftlimit
=
173 btodb((off_t
)qup
->dqblk
.dqb_bsoftlimit
* 1024);
174 qup
->dqblk
.dqb_bhardlimit
=
175 btodb((off_t
)qup
->dqblk
.dqb_bhardlimit
* 1024);
176 if (protoprivs
== NULL
) {
177 protoprivs
= curprivs
= qup
;
179 curprivs
->next
= qup
;
192 if (protoprivs
== NULL
) {
193 if ((protoid
= getentry(protoname
, quotatype
)) == -1)
195 protoprivs
= getprivs(protoid
, quotatype
, fspath
);
196 for (qup
= protoprivs
; qup
; qup
= qup
->next
) {
197 qup
->dqblk
.dqb_btime
= 0;
198 qup
->dqblk
.dqb_itime
= 0;
201 for (; argc
-- > 0; argv
++) {
202 if (strspn(*argv
, "0123456789-") == strlen(*argv
) &&
203 (cp
= strchr(*argv
, '-')) != NULL
) {
205 startuid
= atoi(*argv
);
207 if (enduid
< startuid
)
209 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
213 startuid
= enduid
= 0;
216 for ( ; startuid
<= enduid
; startuid
++) {
218 snprintf(buf
, sizeof(buf
), "%d",
221 snprintf(buf
, sizeof(buf
), "%s",
223 if ((id
= getentry(buf
, quotatype
)) < 0)
226 for (qup
= protoprivs
; qup
;
228 curprivs
= getprivs(id
,
229 quotatype
, qup
->fsname
);
230 if (curprivs
== NULL
)
238 putprivs(id
, quotatype
, protoprivs
);
243 tmpfd
= mkstemp(tmpfil
);
244 fchown(tmpfd
, getuid(), getgid());
246 protoprivs
= getprivs(0, quotatype
, fspath
);
247 if (writetimes(protoprivs
, tmpfd
, quotatype
) == 0)
249 if (editit(tmpfil
) && readtimes(protoprivs
, tmpfil
))
250 putprivs(0, quotatype
, protoprivs
);
251 freeprivs(protoprivs
);
256 for ( ; argc
> 0; argc
--, argv
++) {
257 if ((id
= getentry(*argv
, quotatype
)) == -1)
259 curprivs
= getprivs(id
, quotatype
, fspath
);
260 if (writeprivs(curprivs
, tmpfd
, *argv
, quotatype
) == 0)
262 if (editit(tmpfil
) && readprivs(curprivs
, tmpfil
))
263 putprivs(id
, quotatype
, curprivs
);
274 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
275 "usage: edquota [-u] [-f fspath] [-p username] username ...",
276 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
278 " edquota -g [-f fspath] [-p groupname] groupname ...",
279 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
281 " edquota [-u] -t [-f fspath]",
282 " edquota -g -t [-f fspath]");
287 * This routine converts a name for a particular quota type to
288 * an identifier. This routine must agree with the kernel routine
289 * getinoquota as to the interpretation of quota types.
292 getentry(const char *name
, int quotatype
)
301 if ((pw
= getpwnam(name
)))
303 warnx("%s: no such user", name
);
306 if ((gr
= getgrnam(name
)))
308 warnx("%s: no such group", name
);
311 warnx("%d: unknown quota type", quotatype
);
319 * Collect the requested quota information.
322 getprivs(long id
, int quotatype
, char *fspath
)
325 struct quotause
*qup
, *quptail
;
326 struct quotause
*quphead
;
327 int qcmd
, qupsize
, fd
;
329 static int warned
= 0;
332 quphead
= (struct quotause
*)0;
333 qcmd
= QCMD(Q_GETQUOTA
, quotatype
);
334 while ((fs
= getfsent())) {
335 if (fspath
&& *fspath
&& strcmp(fspath
, fs
->fs_spec
) &&
336 strcmp(fspath
, fs
->fs_file
))
338 if (strcmp(fs
->fs_vfstype
, "ufs"))
340 if (!hasquota(fs
, quotatype
, &qfpathname
))
342 qupsize
= sizeof(*qup
) + strlen(qfpathname
);
343 if ((qup
= (struct quotause
*)malloc(qupsize
)) == NULL
)
344 errx(2, "out of memory");
345 if (quotactl(fs
->fs_file
, qcmd
, id
, &qup
->dqblk
) != 0) {
346 if (errno
== EOPNOTSUPP
&& !warned
) {
348 warnx("warning: quotas are not compiled into this kernel");
351 if ((fd
= open(qfpathname
, O_RDONLY
)) < 0) {
352 fd
= open(qfpathname
, O_RDWR
|O_CREAT
, 0640);
353 if (fd
< 0 && errno
!= ENOENT
) {
354 warn("%s", qfpathname
);
358 warnx("creating quota file %s", qfpathname
);
361 getentry(quotagroup
, GRPQUOTA
));
364 lseek(fd
, (long)(id
* sizeof(struct ufs_dqblk
)), L_SET
);
365 switch (read(fd
, &qup
->dqblk
, sizeof(struct ufs_dqblk
))) {
368 * Convert implicit 0 quota (EOF)
369 * into an explicit one (zero'ed dqblk)
371 bzero((caddr_t
)&qup
->dqblk
,
372 sizeof(struct ufs_dqblk
));
375 case sizeof(struct ufs_dqblk
): /* OK */
379 warn("read error in %s", qfpathname
);
386 strcpy(qup
->qfname
, qfpathname
);
387 strcpy(qup
->fsname
, fs
->fs_file
);
400 * Store the requested quota information.
403 putprivs(long id
, int quotatype
, struct quotause
*quplist
)
405 struct quotause
*qup
;
408 qcmd
= QCMD(Q_SETQUOTA
, quotatype
);
409 for (qup
= quplist
; qup
; qup
= qup
->next
) {
410 if (quotactl(qup
->fsname
, qcmd
, id
, &qup
->dqblk
) == 0)
412 if ((fd
= open(qup
->qfname
, O_WRONLY
)) < 0) {
413 warn("%s", qup
->qfname
);
415 lseek(fd
, (long)id
* (long)sizeof(struct ufs_dqblk
), 0);
416 if (write(fd
, &qup
->dqblk
, sizeof(struct ufs_dqblk
)) !=
417 sizeof(struct ufs_dqblk
)) {
418 warn("%s", qup
->qfname
);
426 * Take a list of priviledges and get it edited.
434 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
436 if ((pid
= fork()) < 0) {
438 if (errno
== EPROCLIM
) {
439 warnx("you have too many processes");
442 if (errno
== EAGAIN
) {
455 if ((ed
= getenv("EDITOR")) == (char *)0)
457 execlp(ed
, ed
, tmpf
, (char *)0);
460 waitpid(pid
, &status
, 0);
462 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
468 * Convert a quotause list to an ASCII file.
471 writeprivs(struct quotause
*quplist
, int outfd
, char *name
, int quotatype
)
473 struct quotause
*qup
;
477 lseek(outfd
, 0, L_SET
);
478 if ((fd
= fdopen(dup(outfd
), "w")) == NULL
)
479 err(1, "%s", tmpfil
);
480 fprintf(fd
, "Quotas for %s %s:\n", qfextension
[quotatype
], name
);
481 for (qup
= quplist
; qup
; qup
= qup
->next
) {
482 fprintf(fd
, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
483 qup
->fsname
, "kbytes in use:",
484 (unsigned long)(dbtob(qup
->dqblk
.dqb_curblocks
) / 1024),
485 (unsigned long)(dbtob(qup
->dqblk
.dqb_bsoftlimit
) / 1024),
486 (unsigned long)(dbtob(qup
->dqblk
.dqb_bhardlimit
) / 1024));
487 fprintf(fd
, "%s %lu, limits (soft = %lu, hard = %lu)\n",
489 (unsigned long)qup
->dqblk
.dqb_curinodes
,
490 (unsigned long)qup
->dqblk
.dqb_isoftlimit
,
491 (unsigned long)qup
->dqblk
.dqb_ihardlimit
);
498 * Merge changes to an ASCII file into a quotause list.
501 readprivs(struct quotause
*quplist
, char *inname
)
503 struct quotause
*qup
;
505 unsigned long bhardlimit
, bsoftlimit
, curblocks
;
506 unsigned long ihardlimit
, isoftlimit
, curinodes
;
509 struct ufs_dqblk dqblk
;
510 char *fsp
, line1
[BUFSIZ
], line2
[BUFSIZ
];
512 fd
= fopen(inname
, "r");
514 warnx("can't re-read temp file!!");
518 * Discard title line, then read pairs of lines to process.
520 fgets(line1
, sizeof(line1
), fd
);
521 while (fgets(line1
, sizeof(line1
), fd
) != NULL
&&
522 fgets(line2
, sizeof(line2
), fd
) != NULL
) {
523 if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
524 warnx("%s: bad format", line1
);
527 if ((cp
= strtok((char *)0, "\n")) == NULL
) {
528 warnx("%s: %s: bad format", fsp
, &fsp
[strlen(fsp
) + 1]);
532 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
533 &curblocks
, &bsoftlimit
, &bhardlimit
);
535 warnx("%s:%s: bad format", fsp
, cp
);
538 dqblk
.dqb_curblocks
= btodb((off_t
)curblocks
* 1024);
539 dqblk
.dqb_bsoftlimit
= btodb((off_t
)bsoftlimit
* 1024);
540 dqblk
.dqb_bhardlimit
= btodb((off_t
)bhardlimit
* 1024);
541 if ((cp
= strtok(line2
, "\n")) == NULL
) {
542 warnx("%s: %s: bad format", fsp
, line2
);
546 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
547 &curinodes
, &isoftlimit
, &ihardlimit
);
549 warnx("%s: %s: bad format", fsp
, line2
);
552 dqblk
.dqb_curinodes
= curinodes
;
553 dqblk
.dqb_isoftlimit
= isoftlimit
;
554 dqblk
.dqb_ihardlimit
= ihardlimit
;
555 for (qup
= quplist
; qup
; qup
= qup
->next
) {
556 if (strcmp(fsp
, qup
->fsname
))
559 * Cause time limit to be reset when the quota
560 * is next used if previously had no soft limit
561 * or were under it, but now have a soft limit
564 if (dqblk
.dqb_bsoftlimit
&&
565 qup
->dqblk
.dqb_curblocks
>= dqblk
.dqb_bsoftlimit
&&
566 (qup
->dqblk
.dqb_bsoftlimit
== 0 ||
567 qup
->dqblk
.dqb_curblocks
<
568 qup
->dqblk
.dqb_bsoftlimit
))
569 qup
->dqblk
.dqb_btime
= 0;
570 if (dqblk
.dqb_isoftlimit
&&
571 qup
->dqblk
.dqb_curinodes
>= dqblk
.dqb_isoftlimit
&&
572 (qup
->dqblk
.dqb_isoftlimit
== 0 ||
573 qup
->dqblk
.dqb_curinodes
<
574 qup
->dqblk
.dqb_isoftlimit
))
575 qup
->dqblk
.dqb_itime
= 0;
576 qup
->dqblk
.dqb_bsoftlimit
= dqblk
.dqb_bsoftlimit
;
577 qup
->dqblk
.dqb_bhardlimit
= dqblk
.dqb_bhardlimit
;
578 qup
->dqblk
.dqb_isoftlimit
= dqblk
.dqb_isoftlimit
;
579 qup
->dqblk
.dqb_ihardlimit
= dqblk
.dqb_ihardlimit
;
581 if (dqblk
.dqb_curblocks
== qup
->dqblk
.dqb_curblocks
&&
582 dqblk
.dqb_curinodes
== qup
->dqblk
.dqb_curinodes
)
584 warnx("%s: cannot change current allocation", fsp
);
590 * Disable quotas for any filesystems that have not been found.
592 for (qup
= quplist
; qup
; qup
= qup
->next
) {
593 if (qup
->flags
& FOUND
) {
594 qup
->flags
&= ~FOUND
;
597 qup
->dqblk
.dqb_bsoftlimit
= 0;
598 qup
->dqblk
.dqb_bhardlimit
= 0;
599 qup
->dqblk
.dqb_isoftlimit
= 0;
600 qup
->dqblk
.dqb_ihardlimit
= 0;
606 * Convert a quotause list to an ASCII file of grace times.
609 writetimes(struct quotause
*quplist
, int outfd
, int quotatype
)
611 struct quotause
*qup
;
615 lseek(outfd
, 0, L_SET
);
616 if ((fd
= fdopen(dup(outfd
), "w")) == NULL
)
617 err(1, "%s", tmpfil
);
618 fprintf(fd
, "Time units may be: days, hours, minutes, or seconds\n");
619 fprintf(fd
, "Grace period before enforcing soft limits for %ss:\n",
620 qfextension
[quotatype
]);
621 for (qup
= quplist
; qup
; qup
= qup
->next
) {
622 fprintf(fd
, "%s: block grace period: %s, ",
623 qup
->fsname
, cvtstoa(qup
->dqblk
.dqb_btime
));
624 fprintf(fd
, "file grace period: %s\n",
625 cvtstoa(qup
->dqblk
.dqb_itime
));
632 * Merge changes of grace times in an ASCII file into a quotause list.
635 readtimes(struct quotause
*quplist
, char *inname
)
637 struct quotause
*qup
;
641 time_t itime
, btime
, iseconds
, bseconds
;
642 long l_itime
, l_btime
;
643 char *fsp
, bunits
[10], iunits
[10], line1
[BUFSIZ
];
645 fd
= fopen(inname
, "r");
647 warnx("can't re-read temp file!!");
651 * Discard two title lines, then read lines to process.
653 fgets(line1
, sizeof(line1
), fd
);
654 fgets(line1
, sizeof(line1
), fd
);
655 while (fgets(line1
, sizeof(line1
), fd
) != NULL
) {
656 if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
657 warnx("%s: bad format", line1
);
660 if ((cp
= strtok((char *)0, "\n")) == NULL
) {
661 warnx("%s: %s: bad format", fsp
, &fsp
[strlen(fsp
) + 1]);
665 " block grace period: %ld %s file grace period: %ld %s",
666 &l_btime
, bunits
, &l_itime
, iunits
);
668 warnx("%s:%s: bad format", fsp
, cp
);
673 if (cvtatos(btime
, bunits
, &bseconds
) == 0)
675 if (cvtatos(itime
, iunits
, &iseconds
) == 0)
677 for (qup
= quplist
; qup
; qup
= qup
->next
) {
678 if (strcmp(fsp
, qup
->fsname
))
680 qup
->dqblk
.dqb_btime
= bseconds
;
681 qup
->dqblk
.dqb_itime
= iseconds
;
688 * reset default grace periods for any filesystems
689 * that have not been found.
691 for (qup
= quplist
; qup
; qup
= qup
->next
) {
692 if (qup
->flags
& FOUND
) {
693 qup
->flags
&= ~FOUND
;
696 qup
->dqblk
.dqb_btime
= 0;
697 qup
->dqblk
.dqb_itime
= 0;
703 * Convert seconds to ASCII times.
710 if (secs
% (24 * 60 * 60) == 0) {
711 secs
/= 24 * 60 * 60;
712 sprintf(buf
, "%ld day%s", (long)secs
, secs
== 1 ? "" : "s");
713 } else if (secs
% (60 * 60) == 0) {
715 sprintf(buf
, "%ld hour%s", (long)secs
, secs
== 1 ? "" : "s");
716 } else if (secs
% 60 == 0) {
718 sprintf(buf
, "%ld minute%s", (long)secs
, secs
== 1 ? "" : "s");
720 sprintf(buf
, "%ld second%s", (long)secs
, secs
== 1 ? "" : "s");
725 * Convert ASCII input times to seconds.
728 cvtatos(time_t period
, char *units
, time_t *seconds
)
731 if (bcmp(units
, "second", 6) == 0)
733 else if (bcmp(units
, "minute", 6) == 0)
734 *seconds
= period
* 60;
735 else if (bcmp(units
, "hour", 4) == 0)
736 *seconds
= period
* 60 * 60;
737 else if (bcmp(units
, "day", 3) == 0)
738 *seconds
= period
* 24 * 60 * 60;
740 printf("%s: bad units, specify %s\n", units
,
741 "days, hours, minutes, or seconds");
748 * Free a list of quotause structures.
751 freeprivs(struct quotause
*quplist
)
753 struct quotause
*qup
, *nextqup
;
755 for (qup
= quplist
; qup
; qup
= nextqup
) {
762 * Check whether a string is completely composed of digits.
765 alldigits(const char *s
)
773 } while ((c
= *s
++));
778 * Check to see if a particular quota is to be enabled.
781 hasquota(struct fstab
*fs
, int type
, char **qfnamep
)
785 static char initname
, usrname
[100], grpname
[100];
786 static char buf
[BUFSIZ
];
789 sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
790 sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
793 strcpy(buf
, fs
->fs_mntops
);
794 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
795 if ((cp
= strchr(opt
, '=')))
797 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
799 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
808 sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);