quota.c revision 1.33.2.4 1 /* $NetBSD: quota.c,v 1.33.2.4 2011/01/30 19:38:45 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95";
44 #else
45 __RCSID("$NetBSD: quota.c,v 1.33.2.4 2011/01/30 19:38:45 bouyer Exp $");
46 #endif
47 #endif /* not lint */
48
49 /*
50 * Disk quota reporting program.
51 */
52 #include <sys/param.h>
53 #include <sys/types.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/mount.h>
57 #include <sys/socket.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <fstab.h>
63 #include <grp.h>
64 #include <netdb.h>
65 #include <pwd.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <time.h>
70 #include <unistd.h>
71
72 #include <ufs/ufs/quota2.h>
73
74 #include <rpc/rpc.h>
75 #include <rpc/pmap_prot.h>
76 #include <rpcsvc/rquota.h>
77
78 #include <printquota.h>
79 #include <getvfsquota.h>
80
81 struct quotause {
82 struct quotause *next;
83 long flags;
84 struct quota2_entry q2e;
85 char fsname[MAXPATHLEN + 1];
86 };
87 #define FOUND 0x01
88 #define QUOTA2 0x02
89
90 int alldigits(char *);
91 int callaurpc(char *, int, int, int, xdrproc_t, void *, xdrproc_t, void *);
92 int main(int, char **);
93 int getnfsquota(struct statvfs *, struct fstab *, struct quotause *,
94 long, int);
95 struct quotause *getprivs(long id, int quotatype);
96 void heading(int, u_long, const char *, const char *);
97 void showgid(gid_t);
98 void showgrpname(const char *);
99 void showquotas(int, u_long, const char *);
100 void showuid(uid_t);
101 void showusrname(const char *);
102 void usage(void);
103
104 int qflag = 0;
105 int vflag = 0;
106 int hflag = 0;
107 int dflag = 0;
108 int Dflag = 0;
109 uid_t myuid;
110
111 int
112 main(argc, argv)
113 int argc;
114 char *argv[];
115 {
116 int ngroups;
117 gid_t mygid, gidset[NGROUPS];
118 int i, gflag = 0, uflag = 0;
119 int ch;
120
121 myuid = getuid();
122 while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
123 switch(ch) {
124 case 'g':
125 gflag++;
126 break;
127 case 'u':
128 uflag++;
129 break;
130 case 'v':
131 vflag++;
132 break;
133 case 'q':
134 qflag++;
135 break;
136 case 'h':
137 hflag++;
138 break;
139 case 'd':
140 dflag++;
141 break;
142 case 'D':
143 Dflag++;
144 break;
145 default:
146 usage();
147 }
148 }
149 argc -= optind;
150 argv += optind;
151 if (!uflag && !gflag)
152 uflag++;
153 if (dflag) {
154 #if 0
155 if (myuid != 0) {
156 printf("quota: -d: permission denied\n");
157 exit(1);
158 }
159 #endif
160 if (uflag)
161 showquotas(USRQUOTA, 0, "");
162 if (gflag)
163 showquotas(GRPQUOTA, 0, "");
164 exit(0);
165 }
166 if (argc == 0) {
167 if (uflag)
168 showuid(myuid);
169 if (gflag) {
170 if (dflag)
171 showgid(0);
172 else {
173 mygid = getgid();
174 ngroups = getgroups(NGROUPS, gidset);
175 if (ngroups < 0)
176 err(1, "getgroups");
177 showgid(mygid);
178 for (i = 0; i < ngroups; i++)
179 if (gidset[i] != mygid)
180 showgid(gidset[i]);
181 }
182 }
183 exit(0);
184 }
185 if (uflag && gflag)
186 usage();
187 if (uflag) {
188 for (; argc > 0; argc--, argv++) {
189 if (alldigits(*argv))
190 showuid(atoi(*argv));
191 else
192 showusrname(*argv);
193 }
194 exit(0);
195 }
196 if (gflag) {
197 for (; argc > 0; argc--, argv++) {
198 if (alldigits(*argv))
199 showgid(atoi(*argv));
200 else
201 showgrpname(*argv);
202 }
203 exit(0);
204 }
205 /* NOTREACHED */
206 return (0);
207 }
208
209 void
210 usage()
211 {
212
213 fprintf(stderr, "%s\n%s\n%s\n%s\n",
214 "usage: quota [-Dhguqv]",
215 "\tquota [-Dhqv] -u username ...",
216 "\tquota [-Dhqv] -g groupname ...",
217 "\tquota -d [-Dhguqv]");
218 exit(1);
219 }
220
221 /*
222 * Print out quotas for a specified user identifier.
223 */
224 void
225 showuid(uid)
226 uid_t uid;
227 {
228 struct passwd *pwd = getpwuid(uid);
229 const char *name;
230
231 if (pwd == NULL)
232 name = "(no account)";
233 else
234 name = pwd->pw_name;
235 if (uid != myuid && myuid != 0) {
236 printf("quota: %s (uid %d): permission denied\n", name, uid);
237 return;
238 }
239 showquotas(USRQUOTA, uid, name);
240 }
241
242 /*
243 * Print out quotas for a specified user name.
244 */
245 void
246 showusrname(name)
247 const char *name;
248 {
249 struct passwd *pwd = getpwnam(name);
250
251 if (pwd == NULL) {
252 warnx("%s: unknown user", name);
253 return;
254 }
255 if (pwd->pw_uid != myuid && myuid != 0) {
256 warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
257 return;
258 }
259 showquotas(USRQUOTA, pwd->pw_uid, name);
260 }
261
262 /*
263 * Print out quotas for a specified group identifier.
264 */
265 void
266 showgid(gid)
267 gid_t gid;
268 {
269 struct group *grp = getgrgid(gid);
270 int ngroups;
271 gid_t mygid, gidset[NGROUPS];
272 int i;
273 const char *name;
274
275 if (grp == NULL)
276 name = "(no entry)";
277 else
278 name = grp->gr_name;
279 mygid = getgid();
280 ngroups = getgroups(NGROUPS, gidset);
281 if (ngroups < 0) {
282 warn("getgroups");
283 return;
284 }
285 if (gid != mygid) {
286 for (i = 0; i < ngroups; i++)
287 if (gid == gidset[i])
288 break;
289 if (i >= ngroups && myuid != 0) {
290 warnx("%s (gid %d): permission denied", name, gid);
291 return;
292 }
293 }
294 showquotas(GRPQUOTA, gid, name);
295 }
296
297 /*
298 * Print out quotas for a specified group name.
299 */
300 void
301 showgrpname(name)
302 const char *name;
303 {
304 struct group *grp = getgrnam(name);
305 int ngroups;
306 gid_t mygid, gidset[NGROUPS];
307 int i;
308
309 if (grp == NULL) {
310 warnx("%s: unknown group", name);
311 return;
312 }
313 mygid = getgid();
314 ngroups = getgroups(NGROUPS, gidset);
315 if (ngroups < 0) {
316 warn("getgroups");
317 return;
318 }
319 if (grp->gr_gid != mygid) {
320 for (i = 0; i < ngroups; i++)
321 if (grp->gr_gid == gidset[i])
322 break;
323 if (i >= ngroups && myuid != 0) {
324 warnx("%s (gid %d): permission denied",
325 name, grp->gr_gid);
326 return;
327 }
328 }
329 showquotas(GRPQUOTA, grp->gr_gid, name);
330 }
331
332 void
333 showquotas(type, id, name)
334 int type;
335 u_long id;
336 const char *name;
337 {
338 struct quotause *qup;
339 struct quotause *quplist;
340 const char *msgi, *msgb, *nam, *timemsg;
341 int lines = 0;
342 static time_t now;
343
344 if (now == 0)
345 time(&now);
346 quplist = getprivs(id, type);
347 for (qup = quplist; qup; qup = qup->next) {
348 if (!vflag &&
349 qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit == UQUAD_MAX &&
350 qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit == UQUAD_MAX &&
351 qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit == UQUAD_MAX &&
352 qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit == UQUAD_MAX)
353 continue;
354 msgi = NULL;
355 if (qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit &&
356 qup->q2e.q2e_val[Q2V_FILE].q2v_cur >=
357 qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit)
358 msgi = "File limit reached on";
359 else if (qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit &&
360 qup->q2e.q2e_val[Q2V_FILE].q2v_cur >=
361 qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit) {
362 if (qup->q2e.q2e_val[Q2V_FILE].q2v_time > now)
363 msgi = "In file grace period on";
364 else
365 msgi = "Over file quota on";
366 }
367 msgb = NULL;
368 if (qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit &&
369 qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur >=
370 qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit)
371 msgb = "Block limit reached on";
372 else if (qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit &&
373 qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur >=
374 qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit) {
375 if (qup->q2e.q2e_val[Q2V_BLOCK].q2v_time > now)
376 msgb = "In block grace period on";
377 else
378 msgb = "Over block quota on";
379 }
380 if (qflag) {
381 if ((msgi != NULL || msgb != NULL) &&
382 lines++ == 0)
383 heading(type, id, name, "");
384 if (msgi != NULL)
385 printf("\t%s %s\n", msgi, qup->fsname);
386 if (msgb != NULL)
387 printf("\t%s %s\n", msgb, qup->fsname);
388 continue;
389 }
390 if (vflag || dflag ||
391 qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur ||
392 qup->q2e.q2e_val[Q2V_FILE].q2v_cur) {
393 if (lines++ == 0)
394 heading(type, id, name, "");
395 nam = qup->fsname;
396 if (strlen(qup->fsname) > 4) {
397 printf("%s\n", qup->fsname);
398 nam = "";
399 }
400 if (msgb)
401 timemsg = timeprt(now,
402 qup->q2e.q2e_val[Q2V_BLOCK].q2v_time);
403 else if ((qup->flags & QUOTA2) != 0 && vflag)
404 timemsg = timeprt(0,
405 qup->q2e.q2e_val[Q2V_BLOCK].q2v_grace);
406 else
407 timemsg = NULL;
408
409 printf("%12s%9s%c%8s%9s%8s"
410 , nam
411 , intprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur
412 ,HN_B, hflag)
413 , (msgb == NULL) ? ' ' : '*'
414 , intprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit
415 , HN_B, hflag)
416 , intprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit
417 , HN_B, hflag)
418 , timemsg);
419
420 if (msgi)
421 timemsg = timeprt(now,
422 qup->q2e.q2e_val[Q2V_FILE].q2v_time);
423 else if ((qup->flags & QUOTA2) != 0 && vflag)
424 timemsg = timeprt(0,
425 qup->q2e.q2e_val[Q2V_FILE].q2v_grace);
426 else
427 timemsg = NULL;
428
429 printf("%8s%c%7s%8s%8s\n"
430 , intprt(qup->q2e.q2e_val[Q2V_FILE].q2v_cur
431 , 0, hflag)
432 , (msgi == NULL) ? ' ' : '*'
433 , intprt(qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit
434 , 0, hflag)
435 , intprt(qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit
436 , 0, hflag)
437 , timemsg);
438 continue;
439 }
440 }
441 if (!qflag && lines == 0)
442 heading(type, id, name, "none");
443 }
444
445 void
446 heading(type, id, name, tag)
447 int type;
448 u_long id;
449 const char *name, *tag;
450 {
451 if (dflag)
452 printf("Default %s disk quotas: %s\n",
453 qfextension[type], tag);
454 else
455 printf("Disk quotas for %s %s (%cid %ld): %s\n",
456 qfextension[type], name, *qfextension[type],
457 (u_long)id, tag);
458
459 if (!qflag && tag[0] == '\0') {
460 printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
461 , "Filesystem"
462 , "blocks"
463 , "quota"
464 , "limit"
465 , "grace"
466 , "files"
467 , "quota"
468 , "limit"
469 , "grace"
470 );
471 }
472 }
473
474 /*
475 * Collect the requested quota information.
476 */
477 struct quotause *
478 getprivs(id, quotatype)
479 long id;
480 int quotatype;
481 {
482 struct quotause *qup, *quptail;
483 struct quotause *quphead;
484 struct statvfs *fst;
485 int nfst, i;
486 int8_t version;
487
488 qup = quphead = quptail = NULL;
489
490 nfst = getmntinfo(&fst, MNT_WAIT);
491 if (nfst == 0)
492 errx(2, "no filesystems mounted!");
493 for (i = 0; i < nfst; i++) {
494 if (qup == NULL) {
495 if ((qup =
496 (struct quotause *)malloc(sizeof *qup)) == NULL)
497 errx(2, "out of memory");
498 }
499 if (strncmp(fst[i].f_fstypename, "nfs",
500 sizeof(fst[i].f_fstypename)) == 0) {
501 version = 0;
502 if (getnfsquota(&fst[i], NULL, qup, id, quotatype) == 0)
503 continue;
504 } else if (strncmp(fst[i].f_fstypename, "ffs",
505 sizeof(fst[i].f_fstypename)) == 0 &&
506 (fst[i].f_flag & ST_QUOTA) != 0) {
507 if (getvfsquota(fst[i].f_mntonname, &qup->q2e, &version,
508 id, quotatype, dflag, Dflag) == 0)
509 continue;
510 } else
511 continue;
512 (void)strncpy(qup->fsname, fst[i].f_mntonname,
513 sizeof(qup->fsname) - 1);
514 if (version == 2)
515 qup->flags |= QUOTA2;
516 if (quphead == NULL)
517 quphead = qup;
518 else
519 quptail->next = qup;
520 quptail = qup;
521 quptail->next = 0;
522 qup = NULL;
523 }
524 if (qup)
525 free(qup);
526 return (quphead);
527 }
528
529 int
530 getnfsquota(fst, fs, qup, id, quotatype)
531 struct statvfs *fst;
532 struct fstab *fs;
533 struct quotause *qup;
534 long id;
535 int quotatype;
536 {
537 struct getquota_args gq_args;
538 struct ext_getquota_args ext_gq_args;
539 struct getquota_rslt gq_rslt;
540 struct quota2_entry *q2e = &qup->q2e;
541 struct timeval tv;
542 char *cp;
543 int ret;
544
545 if (fst->f_flag & MNT_LOCAL)
546 return (0);
547
548 /*
549 * must be some form of "hostname:/path"
550 */
551 cp = strchr(fst->f_mntfromname, ':');
552 if (cp == NULL) {
553 warnx("cannot find hostname for %s", fst->f_mntfromname);
554 return (0);
555 }
556
557 *cp = '\0';
558 if (*(cp+1) != '/') {
559 *cp = ':';
560 return (0);
561 }
562
563 ext_gq_args.gqa_pathp = cp + 1;
564 ext_gq_args.gqa_id = id;
565 ext_gq_args.gqa_type =
566 (quotatype == USRQUOTA) ? RQUOTA_USRQUOTA : RQUOTA_GRPQUOTA;
567 ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, EXT_RQUOTAVERS,
568 RQUOTAPROC_GETQUOTA, xdr_ext_getquota_args, &ext_gq_args,
569 xdr_getquota_rslt, &gq_rslt);
570 if (ret == RPC_PROGVERSMISMATCH) {
571 if (quotatype != USRQUOTA) {
572 *cp = ':';
573 return (0);
574 }
575 /* try RQUOTAVERS */
576 gq_args.gqa_pathp = cp + 1;
577 gq_args.gqa_uid = id;
578 ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
579 RQUOTAPROC_GETQUOTA, xdr_getquota_args, &gq_args,
580 xdr_getquota_rslt, &gq_rslt);
581 }
582 if (ret != RPC_SUCCESS) {
583 *cp = ':';
584 return (0);
585 }
586
587 switch (gq_rslt.status) {
588 case Q_NOQUOTA:
589 break;
590 case Q_EPERM:
591 warnx("quota permission error, host: %s", fst->f_mntfromname);
592 break;
593 case Q_OK:
594 gettimeofday(&tv, NULL);
595 /* blocks*/
596 q2e->q2e_val[Q2V_BLOCK].q2v_hardlimit =
597 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
598 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
599 q2e->q2e_val[Q2V_BLOCK].q2v_softlimit =
600 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
601 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
602 q2e->q2e_val[Q2V_BLOCK].q2v_cur =
603 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
604 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
605 /* inodes */
606 q2e->q2e_val[Q2V_FILE].q2v_hardlimit =
607 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
608 q2e->q2e_val[Q2V_FILE].q2v_softlimit =
609 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
610 q2e->q2e_val[Q2V_FILE].q2v_cur =
611 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
612 /* grace times */
613 q2e->q2e_val[Q2V_BLOCK].q2v_time =
614 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
615 q2e->q2e_val[Q2V_FILE].q2v_time =
616 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
617 *cp = ':';
618 return (1);
619 default:
620 warnx("bad rpc result, host: %s", fst->f_mntfromname);
621 break;
622 }
623 *cp = ':';
624 return (0);
625 }
626
627 int
628 callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
629 char *host;
630 int prognum, versnum, procnum;
631 xdrproc_t inproc;
632 void *in;
633 xdrproc_t outproc;
634 void *out;
635 {
636 struct sockaddr_in server_addr;
637 enum clnt_stat clnt_stat;
638 struct hostent *hp;
639 struct timeval timeout, tottimeout;
640
641 CLIENT *client = NULL;
642 int sock = RPC_ANYSOCK;
643
644 if ((hp = gethostbyname(host)) == NULL)
645 return ((int) RPC_UNKNOWNHOST);
646 timeout.tv_usec = 0;
647 timeout.tv_sec = 6;
648 memmove(&server_addr.sin_addr, hp->h_addr, hp->h_length);
649 server_addr.sin_family = AF_INET;
650 server_addr.sin_port = 0;
651
652 if ((client = clntudp_create(&server_addr, prognum,
653 versnum, timeout, &sock)) == NULL)
654 return ((int) rpc_createerr.cf_stat);
655
656 client->cl_auth = authunix_create_default();
657 tottimeout.tv_sec = 25;
658 tottimeout.tv_usec = 0;
659 clnt_stat = clnt_call(client, procnum, inproc, in,
660 outproc, out, tottimeout);
661
662 return ((int) clnt_stat);
663 }
664
665 int
666 alldigits(s)
667 char *s;
668 {
669 int c;
670
671 c = *s++;
672 do {
673 if (!isdigit(c))
674 return (0);
675 } while ((c = *s++) != 0);
676 return (1);
677 }
678