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 * @(#)quota.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/quota/quota.c,v 1.11.2.5 2002/11/30 23:54:21 iedowse Exp $
39 * $DragonFly: src/usr.bin/quota/quota.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
43 * Disk quota reporting program.
46 #include <sys/param.h>
47 #include <sys/types.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
54 #include <rpc/pmap_prot.h>
55 #include <rpcsvc/rquota.h>
57 #include <ufs/ufs/quota.h>
70 const char *qfname = QUOTAFILENAME;
71 const char *qfextension[] = INITQFNAMES;
74 struct quotause *next;
77 char fsname[MAXPATHLEN + 1];
81 static const char *timeprt(time_t seconds);
82 static struct quotause *getprivs(long id, int quotatype);
83 static void usage(void);
84 static void showuid(u_long uid);
85 static void showgid(u_long gid);
86 static int alldigits(char *s);
87 static void showusrname(char *name);
88 static void showgrpname(char *name);
89 static void showquotas(int type, u_long id, const char *name);
90 static void heading(int type, u_long id, const char *name, const char *tag);
91 static struct quotause *getprivs(long id, int quotatype);
92 static int ufshasquota(struct fstab *fs, int type, char **qfnamep);
93 static int getufsquota(struct fstab *fs, struct quotause *qup, long id,
95 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id,
97 static int callaurpc(char *host, int prognum, int versnum, int procnum,
98 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
99 static int alldigits(char *s);
106 main(int argc, char *argv[])
109 gid_t mygid, gidset[NGROUPS];
110 int i, gflag = 0, uflag = 0;
113 while ((ch = getopt(argc, argv, "glquv")) != -1) {
136 if (!uflag && !gflag)
143 ngroups = getgroups(NGROUPS, gidset);
147 for (i = 0; i < ngroups; i++)
148 if (gidset[i] != mygid)
156 for (; argc > 0; argc--, argv++) {
157 if (alldigits(*argv))
158 showuid(atoi(*argv));
165 for (; argc > 0; argc--, argv++) {
166 if (alldigits(*argv))
167 showgid(atoi(*argv));
179 fprintf(stderr, "%s\n%s\n%s\n",
180 "usage: quota [-glu] [-v | -q]",
181 " quota [-lu] [-v | -q] user ...",
182 " quota -g [-l] [-v | -q] group ...");
187 * Print out quotas for a specified user identifier.
192 struct passwd *pwd = getpwuid(uid);
197 name = "(no account)";
201 if (uid != myuid && myuid != 0) {
202 printf("quota: %s (uid %lu): permission denied\n", name, uid);
205 showquotas(USRQUOTA, uid, name);
209 * Print out quotas for a specifed user name.
212 showusrname(char *name)
214 struct passwd *pwd = getpwnam(name);
218 warnx("%s: unknown user", name);
222 if (pwd->pw_uid != myuid && myuid != 0) {
223 warnx("%s (uid %u): permission denied", name, pwd->pw_uid);
226 showquotas(USRQUOTA, pwd->pw_uid, name);
230 * Print out quotas for a specified group identifier.
235 struct group *grp = getgrgid(gid);
237 gid_t mygid, gidset[NGROUPS];
246 ngroups = getgroups(NGROUPS, gidset);
252 for (i = 0; i < ngroups; i++)
253 if (gid == gidset[i])
255 if (i >= ngroups && getuid() != 0) {
256 warnx("%s (gid %lu): permission denied", name, gid);
260 showquotas(GRPQUOTA, gid, name);
264 * Print out quotas for a specifed group name.
267 showgrpname(char *name)
269 struct group *grp = getgrnam(name);
271 gid_t mygid, gidset[NGROUPS];
275 warnx("%s: unknown group", name);
279 ngroups = getgroups(NGROUPS, gidset);
284 if (grp->gr_gid != mygid) {
285 for (i = 0; i < ngroups; i++)
286 if (grp->gr_gid == gidset[i])
288 if (i >= ngroups && getuid() != 0) {
289 warnx("%s (gid %u): permission denied", name,
294 showquotas(GRPQUOTA, grp->gr_gid, name);
298 showquotas(int type, u_long id, const char *name)
300 struct quotause *qup;
301 struct quotause *quplist;
302 const char *msgi, *msgb;
309 quplist = getprivs(id, type);
310 for (qup = quplist; qup; qup = qup->next) {
312 qup->dqblk.dqb_isoftlimit == 0 &&
313 qup->dqblk.dqb_ihardlimit == 0 &&
314 qup->dqblk.dqb_bsoftlimit == 0 &&
315 qup->dqblk.dqb_bhardlimit == 0)
318 if (qup->dqblk.dqb_ihardlimit &&
319 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
320 msgi = "File limit reached on";
321 else if (qup->dqblk.dqb_isoftlimit &&
322 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
323 if (qup->dqblk.dqb_itime > now)
324 msgi = "In file grace period on";
326 msgi = "Over file quota on";
329 if (qup->dqblk.dqb_bhardlimit &&
330 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
331 msgb = "Block limit reached on";
332 else if (qup->dqblk.dqb_bsoftlimit &&
333 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
334 if (qup->dqblk.dqb_btime > now)
335 msgb = "In block grace period on";
337 msgb = "Over block quota on";
340 if ((msgi != (char *)0 || msgb != (char *)0) &&
342 heading(type, id, name, "");
343 if (msgi != (char *)0)
344 printf("\t%s %s\n", msgi, qup->fsname);
345 if (msgb != (char *)0)
346 printf("\t%s %s\n", msgb, qup->fsname);
350 qup->dqblk.dqb_curblocks ||
351 qup->dqblk.dqb_curinodes) {
353 heading(type, id, name, "");
355 if (strlen(qup->fsname) > 15) {
356 printf("%s\n", qup->fsname);
359 printf("%15s%8lu%c%7lu%8lu%8s"
361 , (u_long) (dbtob(qup->dqblk.dqb_curblocks)
363 , (msgb == (char *)0) ? ' ' : '*'
364 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit)
366 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit)
368 , (msgb == (char *)0) ? ""
369 :timeprt(qup->dqblk.dqb_btime));
370 printf("%8lu%c%7lu%8lu%8s\n"
371 , (u_long)qup->dqblk.dqb_curinodes
372 , (msgi == (char *)0) ? ' ' : '*'
373 , (u_long)qup->dqblk.dqb_isoftlimit
374 , (u_long)qup->dqblk.dqb_ihardlimit
375 , (msgi == (char *)0) ? ""
376 : timeprt(qup->dqblk.dqb_itime)
381 if (!qflag && lines == 0)
382 heading(type, id, name, "none");
386 heading(int type, u_long id, const char *name, const char *tag)
389 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
390 name, *qfextension[type], id, tag);
391 if (!qflag && tag[0] == '\0') {
392 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
407 * Calculate the grace period and return a printable string for it.
410 timeprt(time_t seconds)
412 time_t hours, minutes;
421 minutes = (seconds + 30) / 60;
422 hours = (minutes + 30) / 60;
424 sprintf(buf, "%lddays", ((long)hours + 12) / 24);
428 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
432 sprintf(buf, "%2ld", (long)minutes);
437 * Collect the requested quota information.
439 static struct quotause *
440 getprivs(long id, int quotatype)
442 struct quotause *qup, *quptail = NULL;
444 struct quotause *quphead;
448 qup = quphead = (struct quotause *)0;
450 nfst = getmntinfo(&fst, MNT_NOWAIT);
452 errx(2, "no filesystems mounted!");
454 for (i=0; i<nfst; i++) {
456 if ((qup = (struct quotause *)malloc(sizeof *qup))
458 errx(2, "out of memory");
460 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
463 if (getnfsquota(&fst[i], qup, id, quotatype)
466 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
469 * UFS filesystems must be in /etc/fstab, and must
470 * indicate that they have quotas on (?!) This is quite
471 * unlike SunOS where quotas can be enabled/disabled
472 * on a filesystem independent of /etc/fstab, and it
473 * will still print quotas for them.
475 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
477 if (getufsquota(fs, qup, id, quotatype) == 0)
481 strcpy(qup->fsname, fst[i].f_mntonname);
497 * Check to see if a particular quota is to be enabled.
500 ufshasquota(struct fstab *fs, int type, char **qfnamep)
502 static char initname, usrname[100], grpname[100];
503 static char buf[BUFSIZ];
507 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
508 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
511 strcpy(buf, fs->fs_mntops);
512 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
513 if ((cp = index(opt, '=')))
515 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
517 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
526 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
532 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
537 qcmd = QCMD(Q_GETQUOTA, quotatype);
538 if (!ufshasquota(fs, quotatype, &qfpathname))
541 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
542 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
543 warn("%s", qfpathname);
546 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
547 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
550 * Convert implicit 0 quota (EOF)
551 * into an explicit one (zero'ed dqblk)
553 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk));
555 case sizeof(struct dqblk): /* OK */
558 warn("read error: %s", qfpathname);
568 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
570 struct getquota_args gq_args;
571 struct getquota_rslt gq_rslt;
572 struct dqblk *dqp = &qup->dqblk;
576 if (fst->f_flags & MNT_LOCAL)
580 * rpc.rquotad does not support group quotas
582 if (quotatype != USRQUOTA)
586 * must be some form of "hostname:/path"
588 cp = strchr(fst->f_mntfromname, ':');
590 warnx("cannot find hostname for %s", fst->f_mntfromname);
595 if (*(cp+1) != '/') {
600 /* Avoid attempting the RPC for special amd(8) filesystems. */
601 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
602 strchr(fst->f_mntfromname, '@') != NULL) {
607 gq_args.gqa_pathp = cp + 1;
608 gq_args.gqa_uid = id;
609 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
610 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
611 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
616 switch (gq_rslt.status) {
620 warnx("quota permission error, host: %s",
624 gettimeofday(&tv, NULL);
626 dqp->dqb_bhardlimit =
627 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
628 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
629 dqp->dqb_bsoftlimit =
630 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
631 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
633 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
634 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
636 dqp->dqb_ihardlimit =
637 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
638 dqp->dqb_isoftlimit =
639 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
641 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
644 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
646 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
650 warnx("bad rpc result, host: %s", fst->f_mntfromname);
658 callaurpc(char *host, int prognum, int versnum, int procnum,
659 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
661 struct sockaddr_in server_addr;
662 enum clnt_stat clnt_stat;
664 struct timeval timeout, tottimeout;
666 CLIENT *client = NULL;
667 int sock = RPC_ANYSOCK;
669 if ((hp = gethostbyname(host)) == NULL)
670 return ((int) RPC_UNKNOWNHOST);
673 bcopy(hp->h_addr, &server_addr.sin_addr,
674 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
675 server_addr.sin_family = AF_INET;
676 server_addr.sin_port = 0;
678 if ((client = clntudp_create(&server_addr, prognum,
679 versnum, timeout, &sock)) == NULL)
680 return ((int) rpc_createerr.cf_stat);
682 client->cl_auth = authunix_create_default();
683 tottimeout.tv_sec = 25;
684 tottimeout.tv_usec = 0;
685 clnt_stat = clnt_call(client, procnum, inproc, in,
686 outproc, out, tottimeout);
688 return ((int) clnt_stat);
700 } while ((c = *s++));