ifnet: Rework if_ringmap_match
[dragonfly.git] / usr.bin / quota / quota.c
blob26d100a76b5e77a3df699909ada7fd96aceb6378
1 /*
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
10 * are met:
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. 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
30 * SUCH DAMAGE.
32 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)quota.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/quota/quota.c,v 1.11.2.5 2002/11/30 23:54:21 iedowse Exp $
38 * Disk quota reporting program.
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/mount.h>
46 #include <sys/socket.h>
48 #include <rpc/rpc.h>
49 #include <rpc/pmap_prot.h>
50 #include <rpcsvc/rquota.h>
52 #include <vfs/ufs/quota.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <fstab.h>
57 #include <grp.h>
58 #include <netdb.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
65 const char *qfname = QUOTAFILENAME;
66 const char *qfextension[] = INITQFNAMES;
68 struct quotause {
69 struct quotause *next;
70 long flags;
71 struct ufs_dqblk dqblk;
72 char fsname[MAXPATHLEN + 1];
74 #define FOUND 0x01
76 static const char *timeprt(time_t);
77 static struct quotause *getprivs(long, int);
78 static void usage(void);
79 static void showuid(u_long);
80 static void showgid(u_long);
81 static int alldigits(char *);
82 static void showusrname(char *);
83 static void showgrpname(char *);
84 static void showquotas(int, u_long, const char *);
85 static void heading(int, u_long, const char *, const char *);
86 static int ufshasquota(struct fstab *, int, char **);
87 static int getufsquota(struct fstab *, struct quotause *, long, int);
88 static int getnfsquota(struct statfs *, struct quotause *, long, int);
89 static int callaurpc(char *, int, int, int, xdrproc_t, char *, xdrproc_t,
90 char *);
92 int lflag;
93 int qflag;
94 int vflag;
96 int
97 main(int argc, char *argv[])
99 int ngroups;
100 gid_t mygid, gidset[NGROUPS];
101 int i, gflag = 0, uflag = 0;
102 char ch;
104 while ((ch = getopt(argc, argv, "glquv")) != -1) {
105 switch(ch) {
106 case 'g':
107 gflag++;
108 break;
109 case 'l':
110 lflag++;
111 break;
112 case 'q':
113 qflag++;
114 break;
115 case 'u':
116 uflag++;
117 break;
118 case 'v':
119 vflag++;
120 break;
121 default:
122 usage();
125 argc -= optind;
126 argv += optind;
127 if (!uflag && !gflag)
128 uflag++;
129 if (argc == 0) {
130 if (uflag)
131 showuid(getuid());
132 if (gflag) {
133 mygid = getgid();
134 ngroups = getgroups(NGROUPS, gidset);
135 if (ngroups < 0)
136 err(1, "getgroups");
137 showgid(mygid);
138 for (i = 0; i < ngroups; i++)
139 if (gidset[i] != mygid)
140 showgid(gidset[i]);
142 return(0);
144 if (uflag && gflag)
145 usage();
146 if (uflag) {
147 for (; argc > 0; argc--, argv++) {
148 if (alldigits(*argv))
149 showuid(atoi(*argv));
150 else
151 showusrname(*argv);
153 return(0);
155 if (gflag) {
156 for (; argc > 0; argc--, argv++) {
157 if (alldigits(*argv))
158 showgid(atoi(*argv));
159 else
160 showgrpname(*argv);
163 return(0);
166 static void
167 usage(void)
170 fprintf(stderr, "%s\n%s\n%s\n",
171 "usage: quota [-glu] [-v | -q]",
172 " quota [-lu] [-v | -q] user ...",
173 " quota -g [-l] [-v | -q] group ...");
174 exit(1);
178 * Print out quotas for a specified user identifier.
180 static void
181 showuid(u_long uid)
183 struct passwd *pwd = getpwuid(uid);
184 u_long myuid;
185 const char *name;
187 if (pwd == NULL)
188 name = "(no account)";
189 else
190 name = pwd->pw_name;
191 myuid = getuid();
192 if (uid != myuid && myuid != 0) {
193 printf("quota: %s (uid %lu): permission denied\n", name, uid);
194 return;
196 showquotas(USRQUOTA, uid, name);
200 * Print out quotas for a specifed user name.
202 static void
203 showusrname(char *name)
205 struct passwd *pwd = getpwnam(name);
206 u_long myuid;
208 if (pwd == NULL) {
209 warnx("%s: unknown user", name);
210 return;
212 myuid = getuid();
213 if (pwd->pw_uid != myuid && myuid != 0) {
214 warnx("%s (uid %u): permission denied", name, pwd->pw_uid);
215 return;
217 showquotas(USRQUOTA, pwd->pw_uid, name);
221 * Print out quotas for a specified group identifier.
223 static void
224 showgid(u_long gid)
226 struct group *grp = getgrgid(gid);
227 int ngroups;
228 gid_t mygid, gidset[NGROUPS];
229 int i;
230 const char *name;
232 if (grp == NULL)
233 name = "(no entry)";
234 else
235 name = grp->gr_name;
236 mygid = getgid();
237 ngroups = getgroups(NGROUPS, gidset);
238 if (ngroups < 0) {
239 warn("getgroups");
240 return;
242 if (gid != mygid) {
243 for (i = 0; i < ngroups; i++)
244 if (gid == gidset[i])
245 break;
246 if (i >= ngroups && getuid() != 0) {
247 warnx("%s (gid %lu): permission denied", name, gid);
248 return;
251 showquotas(GRPQUOTA, gid, name);
255 * Print out quotas for a specifed group name.
257 static void
258 showgrpname(char *name)
260 struct group *grp = getgrnam(name);
261 int ngroups;
262 gid_t mygid, gidset[NGROUPS];
263 int i;
265 if (grp == NULL) {
266 warnx("%s: unknown group", name);
267 return;
269 mygid = getgid();
270 ngroups = getgroups(NGROUPS, gidset);
271 if (ngroups < 0) {
272 warn("getgroups");
273 return;
275 if (grp->gr_gid != mygid) {
276 for (i = 0; i < ngroups; i++)
277 if (grp->gr_gid == gidset[i])
278 break;
279 if (i >= ngroups && getuid() != 0) {
280 warnx("%s (gid %u): permission denied", name,
281 grp->gr_gid);
282 return;
285 showquotas(GRPQUOTA, grp->gr_gid, name);
288 static void
289 showquotas(int type, u_long id, const char *name)
291 struct quotause *qup;
292 struct quotause *quplist;
293 const char *msgi, *msgb;
294 const char *nam;
295 int lines = 0;
296 static time_t now;
298 if (now == 0)
299 time(&now);
300 quplist = getprivs(id, type);
301 for (qup = quplist; qup; qup = qup->next) {
302 if (!vflag &&
303 qup->dqblk.dqb_isoftlimit == 0 &&
304 qup->dqblk.dqb_ihardlimit == 0 &&
305 qup->dqblk.dqb_bsoftlimit == 0 &&
306 qup->dqblk.dqb_bhardlimit == 0)
307 continue;
308 msgi = NULL;
309 if (qup->dqblk.dqb_ihardlimit &&
310 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
311 msgi = "File limit reached on";
312 else if (qup->dqblk.dqb_isoftlimit &&
313 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
314 if (qup->dqblk.dqb_itime > now)
315 msgi = "In file grace period on";
316 else
317 msgi = "Over file quota on";
319 msgb = NULL;
320 if (qup->dqblk.dqb_bhardlimit &&
321 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
322 msgb = "Block limit reached on";
323 else if (qup->dqblk.dqb_bsoftlimit &&
324 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
325 if (qup->dqblk.dqb_btime > now)
326 msgb = "In block grace period on";
327 else
328 msgb = "Over block quota on";
330 if (qflag) {
331 if ((msgi != NULL || msgb != NULL) &&
332 lines++ == 0)
333 heading(type, id, name, "");
334 if (msgi != NULL)
335 printf("\t%s %s\n", msgi, qup->fsname);
336 if (msgb != NULL)
337 printf("\t%s %s\n", msgb, qup->fsname);
338 continue;
340 if (vflag ||
341 qup->dqblk.dqb_curblocks ||
342 qup->dqblk.dqb_curinodes) {
343 if (lines++ == 0)
344 heading(type, id, name, "");
345 nam = qup->fsname;
346 if (strlen(qup->fsname) > 15) {
347 printf("%s\n", qup->fsname);
348 nam = "";
350 printf("%15s%8lu%c%7lu%8lu%8s"
351 , nam
352 , (u_long) (dbtob(qup->dqblk.dqb_curblocks)
353 / 1024)
354 , (msgb == NULL) ? ' ' : '*'
355 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit)
356 / 1024)
357 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit)
358 / 1024)
359 , (msgb == NULL) ? ""
360 :timeprt(qup->dqblk.dqb_btime));
361 printf("%8lu%c%7lu%8lu%8s\n"
362 , (u_long)qup->dqblk.dqb_curinodes
363 , (msgi == NULL) ? ' ' : '*'
364 , (u_long)qup->dqblk.dqb_isoftlimit
365 , (u_long)qup->dqblk.dqb_ihardlimit
366 , (msgi == NULL) ? ""
367 : timeprt(qup->dqblk.dqb_itime)
369 continue;
372 if (!qflag && lines == 0)
373 heading(type, id, name, "none");
376 static void
377 heading(int type, u_long id, const char *name, const char *tag)
380 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
381 name, *qfextension[type], id, tag);
382 if (!qflag && tag[0] == '\0') {
383 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
384 , "Filesystem"
385 , "usage"
386 , "quota"
387 , "limit"
388 , "grace"
389 , "files"
390 , "quota"
391 , "limit"
392 , "grace"
398 * Calculate the grace period and return a printable string for it.
400 static const char *
401 timeprt(time_t seconds)
403 time_t hours, minutes;
404 static char buf[20];
405 static time_t now;
407 if (now == 0)
408 time(&now);
409 if (now > seconds)
410 return ("none");
411 seconds -= now;
412 minutes = (seconds + 30) / 60;
413 hours = (minutes + 30) / 60;
414 if (hours >= 36) {
415 sprintf(buf, "%lddays", ((long)hours + 12) / 24);
416 return (buf);
418 if (minutes >= 60) {
419 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
420 (long)minutes % 60);
421 return (buf);
423 sprintf(buf, "%2ld", (long)minutes);
424 return (buf);
428 * Collect the requested quota information.
430 static struct quotause *
431 getprivs(long id, int quotatype)
433 struct quotause *qup, *quptail = NULL;
434 struct fstab *fs;
435 struct quotause *quphead;
436 struct statfs *fst;
437 int nfst, i;
439 qup = quphead = NULL;
441 nfst = getmntinfo(&fst, MNT_NOWAIT);
442 if (nfst == 0)
443 errx(2, "no filesystems mounted!");
444 setfsent();
445 for (i=0; i<nfst; i++) {
446 if (qup == NULL) {
447 if ((qup = (struct quotause *)malloc(sizeof *qup))
448 == NULL)
449 errx(2, "out of memory");
451 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
452 if (lflag)
453 continue;
454 if (getnfsquota(&fst[i], qup, id, quotatype)
455 == 0)
456 continue;
457 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
459 * XXX
460 * UFS filesystems must be in /etc/fstab, and must
461 * indicate that they have quotas on (?!) This is quite
462 * unlike SunOS where quotas can be enabled/disabled
463 * on a filesystem independent of /etc/fstab, and it
464 * will still print quotas for them.
466 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
467 continue;
468 if (getufsquota(fs, qup, id, quotatype) == 0)
469 continue;
470 } else
471 continue;
472 strcpy(qup->fsname, fst[i].f_mntonname);
473 if (quphead == NULL)
474 quphead = qup;
475 else
476 quptail->next = qup;
477 quptail = qup;
478 quptail->next = NULL;
479 qup = NULL;
481 if (qup)
482 free(qup);
483 endfsent();
484 return (quphead);
488 * Check to see if a particular quota is to be enabled.
490 static int
491 ufshasquota(struct fstab *fs, int type, char **qfnamep)
493 static char initname, usrname[100], grpname[100];
494 static char buf[BUFSIZ];
495 char *opt, *cp;
497 if (!initname) {
498 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
499 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
500 initname = 1;
502 strcpy(buf, fs->fs_mntops);
503 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
504 if ((cp = strchr(opt, '=')))
505 *cp++ = '\0';
506 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
507 break;
508 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
509 break;
511 if (!opt)
512 return (0);
513 if (cp) {
514 *qfnamep = cp;
515 return (1);
517 sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
518 *qfnamep = buf;
519 return (1);
522 static int
523 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
525 char *qfpathname;
526 int fd, qcmd;
528 qcmd = QCMD(Q_GETQUOTA, quotatype);
529 if (!ufshasquota(fs, quotatype, &qfpathname))
530 return (0);
532 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
533 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
534 warn("%s", qfpathname);
535 return (0);
537 lseek(fd, (off_t)(id * sizeof(struct ufs_dqblk)), L_SET);
538 switch (read(fd, &qup->dqblk, sizeof(struct ufs_dqblk))) {
539 case 0: /* EOF */
541 * Convert implicit 0 quota (EOF)
542 * into an explicit one (zero'ed dqblk)
544 bzero(&qup->dqblk, sizeof(struct ufs_dqblk));
545 break;
546 case sizeof(struct ufs_dqblk): /* OK */
547 break;
548 default: /* ERROR */
549 warn("read error: %s", qfpathname);
550 close(fd);
551 return (0);
553 close(fd);
555 return (1);
558 static int
559 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
561 struct getquota_args gq_args;
562 struct getquota_rslt gq_rslt;
563 struct ufs_dqblk *dqp = &qup->dqblk;
564 struct timeval tv;
565 char *cp;
567 if (fst->f_flags & MNT_LOCAL)
568 return (0);
571 * rpc.rquotad does not support group quotas
573 if (quotatype != USRQUOTA)
574 return (0);
577 * must be some form of "hostname:/path"
579 cp = strchr(fst->f_mntfromname, ':');
580 if (cp == NULL) {
581 warnx("cannot find hostname for %s", fst->f_mntfromname);
582 return (0);
585 *cp = '\0';
586 if (*(cp+1) != '/') {
587 *cp = ':';
588 return (0);
591 gq_args.gqa_pathp = cp + 1;
592 gq_args.gqa_uid = id;
593 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
594 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
595 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
596 *cp = ':';
597 return (0);
600 switch (gq_rslt.status) {
601 case Q_NOQUOTA:
602 break;
603 case Q_EPERM:
604 warnx("quota permission error, host: %s",
605 fst->f_mntfromname);
606 break;
607 case Q_OK:
608 gettimeofday(&tv, NULL);
609 /* blocks*/
610 dqp->dqb_bhardlimit =
611 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
612 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
613 dqp->dqb_bsoftlimit =
614 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
615 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
616 dqp->dqb_curblocks =
617 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
618 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
619 /* inodes */
620 dqp->dqb_ihardlimit =
621 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
622 dqp->dqb_isoftlimit =
623 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
624 dqp->dqb_curinodes =
625 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
626 /* grace times */
627 dqp->dqb_btime =
628 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
629 dqp->dqb_itime =
630 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
631 *cp = ':';
632 return (1);
633 default:
634 warnx("bad rpc result, host: %s", fst->f_mntfromname);
635 break;
637 *cp = ':';
638 return (0);
641 static int
642 callaurpc(char *host, int prognum, int versnum, int procnum,
643 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
645 struct sockaddr_in server_addr;
646 enum clnt_stat clnt_stat;
647 struct hostent *hp;
648 struct timeval timeout, tottimeout;
650 CLIENT *client = NULL;
651 int sock = RPC_ANYSOCK;
653 if ((hp = gethostbyname(host)) == NULL)
654 return ((int) RPC_UNKNOWNHOST);
655 timeout.tv_usec = 0;
656 timeout.tv_sec = 6;
657 bcopy(hp->h_addr, &server_addr.sin_addr,
658 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
659 server_addr.sin_family = AF_INET;
660 server_addr.sin_port = 0;
662 if ((client = clntudp_create(&server_addr, prognum,
663 versnum, timeout, &sock)) == NULL)
664 return ((int) rpc_createerr.cf_stat);
666 client->cl_auth = authunix_create_default();
667 tottimeout.tv_sec = 25;
668 tottimeout.tv_usec = 0;
669 clnt_stat = clnt_call(client, procnum, inproc, in,
670 outproc, out, tottimeout);
672 return ((int) clnt_stat);
675 static int
676 alldigits(char *s)
678 int c;
680 c = *s++;
681 do {
682 if (!isdigit(c))
683 return (0);
684 } while ((c = *s++));
685 return (1);