quota.c revision 1.33.2.7 1 /* $NetBSD: quota.c,v 1.33.2.7 2011/02/03 15:56:15 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.7 2011/02/03 15:56:15 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 int ql_stat;
349 if (!vflag &&
350 qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == UQUAD_MAX &&
351 qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit == UQUAD_MAX &&
352 qup->q2e.q2e_val[QL_FILE].q2v_softlimit == UQUAD_MAX &&
353 qup->q2e.q2e_val[QL_FILE].q2v_hardlimit == UQUAD_MAX)
354 continue;
355 ql_stat = quota2_check_limit(&qup->q2e.q2e_val[QL_FILE],
356 1, now);
357 switch(QL_STATUS(ql_stat)) {
358 case QL_S_DENY_HARD:
359 msgi = "File limit reached on";
360 break;
361 case QL_S_DENY_GRACE:
362 msgi = "Over file quota on";
363 break;
364 case QL_S_ALLOW_SOFT:
365 msgi = "In file grace period on";
366 break;
367 default:
368 msgi = NULL;
369 }
370 ql_stat = quota2_check_limit(&qup->q2e.q2e_val[QL_BLOCK],
371 1, now);
372 switch(QL_STATUS(ql_stat)) {
373 case QL_S_DENY_HARD:
374 msgb = "Block limit reached on";
375 break;
376 case QL_S_DENY_GRACE:
377 msgb = "Over block quota on";
378 break;
379 case QL_S_ALLOW_SOFT:
380 msgb = "In block grace period on";
381 break;
382 default:
383 msgb = NULL;
384 }
385 if (qflag) {
386 if ((msgi != NULL || msgb != NULL) &&
387 lines++ == 0)
388 heading(type, id, name, "");
389 if (msgi != NULL)
390 printf("\t%s %s\n", msgi, qup->fsname);
391 if (msgb != NULL)
392 printf("\t%s %s\n", msgb, qup->fsname);
393 continue;
394 }
395 if (vflag || dflag ||
396 qup->q2e.q2e_val[QL_BLOCK].q2v_cur ||
397 qup->q2e.q2e_val[QL_FILE].q2v_cur) {
398 if (lines++ == 0)
399 heading(type, id, name, "");
400 nam = qup->fsname;
401 if (strlen(qup->fsname) > 4) {
402 printf("%s\n", qup->fsname);
403 nam = "";
404 }
405 if (msgb)
406 timemsg = timeprt(now,
407 qup->q2e.q2e_val[QL_BLOCK].q2v_time, 8);
408 else if ((qup->flags & QUOTA2) != 0 && vflag)
409 timemsg = timeprt(0,
410 qup->q2e.q2e_val[QL_BLOCK].q2v_grace, 8);
411 else
412 timemsg = "";
413
414 printf("%12s%9s%c%8s%9s%8s"
415 , nam
416 , intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_cur
417 ,HN_B, hflag, 8)
418 , (msgb == NULL) ? ' ' : '*'
419 , intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit
420 , HN_B, hflag, 8)
421 , intprt(qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit
422 , HN_B, hflag, 8)
423 , timemsg);
424
425 if (msgi)
426 timemsg = timeprt(now,
427 qup->q2e.q2e_val[QL_FILE].q2v_time, 8);
428 else if ((qup->flags & QUOTA2) != 0 && vflag)
429 timemsg = timeprt(0,
430 qup->q2e.q2e_val[QL_FILE].q2v_grace, 8);
431 else
432 timemsg = "";
433
434 printf("%8s%c%7s%8s%8s\n"
435 , intprt(qup->q2e.q2e_val[QL_FILE].q2v_cur
436 , 0, hflag, 7)
437 , (msgi == NULL) ? ' ' : '*'
438 , intprt(qup->q2e.q2e_val[QL_FILE].q2v_softlimit
439 , 0, hflag, 7)
440 , intprt(qup->q2e.q2e_val[QL_FILE].q2v_hardlimit
441 , 0, hflag, 7)
442 , timemsg);
443 continue;
444 }
445 }
446 if (!qflag && lines == 0)
447 heading(type, id, name, "none");
448 }
449
450 void
451 heading(type, id, name, tag)
452 int type;
453 u_long id;
454 const char *name, *tag;
455 {
456 if (dflag)
457 printf("Default %s disk quotas: %s\n",
458 qfextension[type], tag);
459 else
460 printf("Disk quotas for %s %s (%cid %ld): %s\n",
461 qfextension[type], name, *qfextension[type],
462 (u_long)id, tag);
463
464 if (!qflag && tag[0] == '\0') {
465 printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
466 , "Filesystem"
467 , "blocks"
468 , "quota"
469 , "limit"
470 , "grace"
471 , "files"
472 , "quota"
473 , "limit"
474 , "grace"
475 );
476 }
477 }
478
479 /*
480 * Collect the requested quota information.
481 */
482 struct quotause *
483 getprivs(id, quotatype)
484 long id;
485 int quotatype;
486 {
487 struct quotause *qup, *quptail;
488 struct quotause *quphead;
489 struct statvfs *fst;
490 int nfst, i;
491 int8_t version;
492
493 qup = quphead = quptail = NULL;
494
495 nfst = getmntinfo(&fst, MNT_WAIT);
496 if (nfst == 0)
497 errx(2, "no filesystems mounted!");
498 for (i = 0; i < nfst; i++) {
499 if (qup == NULL) {
500 if ((qup =
501 (struct quotause *)malloc(sizeof *qup)) == NULL)
502 errx(2, "out of memory");
503 }
504 if (strncmp(fst[i].f_fstypename, "nfs",
505 sizeof(fst[i].f_fstypename)) == 0) {
506 version = 0;
507 if (getnfsquota(&fst[i], NULL, qup, id, quotatype) == 0)
508 continue;
509 } else if (strncmp(fst[i].f_fstypename, "ffs",
510 sizeof(fst[i].f_fstypename)) == 0 &&
511 (fst[i].f_flag & ST_QUOTA) != 0) {
512 if (getvfsquota(fst[i].f_mntonname, &qup->q2e, &version,
513 id, quotatype, dflag, Dflag) == 0)
514 continue;
515 } else
516 continue;
517 (void)strncpy(qup->fsname, fst[i].f_mntonname,
518 sizeof(qup->fsname) - 1);
519 if (version == 2)
520 qup->flags |= QUOTA2;
521 if (quphead == NULL)
522 quphead = qup;
523 else
524 quptail->next = qup;
525 quptail = qup;
526 quptail->next = 0;
527 qup = NULL;
528 }
529 if (qup)
530 free(qup);
531 return (quphead);
532 }
533
534 int
535 getnfsquota(fst, fs, qup, id, quotatype)
536 struct statvfs *fst;
537 struct fstab *fs;
538 struct quotause *qup;
539 long id;
540 int quotatype;
541 {
542 struct getquota_args gq_args;
543 struct ext_getquota_args ext_gq_args;
544 struct getquota_rslt gq_rslt;
545 struct quota2_entry *q2e = &qup->q2e;
546 struct timeval tv;
547 char *cp;
548 int ret;
549
550 if (fst->f_flag & MNT_LOCAL)
551 return (0);
552
553 /*
554 * must be some form of "hostname:/path"
555 */
556 cp = strchr(fst->f_mntfromname, ':');
557 if (cp == NULL) {
558 warnx("cannot find hostname for %s", fst->f_mntfromname);
559 return (0);
560 }
561
562 *cp = '\0';
563 if (*(cp+1) != '/') {
564 *cp = ':';
565 return (0);
566 }
567
568 ext_gq_args.gqa_pathp = cp + 1;
569 ext_gq_args.gqa_id = id;
570 ext_gq_args.gqa_type =
571 (quotatype == USRQUOTA) ? RQUOTA_USRQUOTA : RQUOTA_GRPQUOTA;
572 ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, EXT_RQUOTAVERS,
573 RQUOTAPROC_GETQUOTA, xdr_ext_getquota_args, &ext_gq_args,
574 xdr_getquota_rslt, &gq_rslt);
575 if (ret == RPC_PROGVERSMISMATCH) {
576 if (quotatype != USRQUOTA) {
577 *cp = ':';
578 return (0);
579 }
580 /* try RQUOTAVERS */
581 gq_args.gqa_pathp = cp + 1;
582 gq_args.gqa_uid = id;
583 ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
584 RQUOTAPROC_GETQUOTA, xdr_getquota_args, &gq_args,
585 xdr_getquota_rslt, &gq_rslt);
586 }
587 if (ret != RPC_SUCCESS) {
588 *cp = ':';
589 return (0);
590 }
591
592 switch (gq_rslt.status) {
593 case Q_NOQUOTA:
594 break;
595 case Q_EPERM:
596 warnx("quota permission error, host: %s", fst->f_mntfromname);
597 break;
598 case Q_OK:
599 gettimeofday(&tv, NULL);
600 /* blocks*/
601 q2e->q2e_val[QL_BLOCK].q2v_hardlimit =
602 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
603 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
604 q2e->q2e_val[QL_BLOCK].q2v_softlimit =
605 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
606 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
607 q2e->q2e_val[QL_BLOCK].q2v_cur =
608 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
609 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
610 /* inodes */
611 q2e->q2e_val[QL_FILE].q2v_hardlimit =
612 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
613 q2e->q2e_val[QL_FILE].q2v_softlimit =
614 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
615 q2e->q2e_val[QL_FILE].q2v_cur =
616 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
617 /* grace times */
618 q2e->q2e_val[QL_BLOCK].q2v_time =
619 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
620 q2e->q2e_val[QL_FILE].q2v_time =
621 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
622 *cp = ':';
623 return (1);
624 default:
625 warnx("bad rpc result, host: %s", fst->f_mntfromname);
626 break;
627 }
628 *cp = ':';
629 return (0);
630 }
631
632 int
633 callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
634 char *host;
635 int prognum, versnum, procnum;
636 xdrproc_t inproc;
637 void *in;
638 xdrproc_t outproc;
639 void *out;
640 {
641 struct sockaddr_in server_addr;
642 enum clnt_stat clnt_stat;
643 struct hostent *hp;
644 struct timeval timeout, tottimeout;
645
646 CLIENT *client = NULL;
647 int sock = RPC_ANYSOCK;
648
649 if ((hp = gethostbyname(host)) == NULL)
650 return ((int) RPC_UNKNOWNHOST);
651 timeout.tv_usec = 0;
652 timeout.tv_sec = 6;
653 memmove(&server_addr.sin_addr, hp->h_addr, hp->h_length);
654 server_addr.sin_family = AF_INET;
655 server_addr.sin_port = 0;
656
657 if ((client = clntudp_create(&server_addr, prognum,
658 versnum, timeout, &sock)) == NULL)
659 return ((int) rpc_createerr.cf_stat);
660
661 client->cl_auth = authunix_create_default();
662 tottimeout.tv_sec = 25;
663 tottimeout.tv_usec = 0;
664 clnt_stat = clnt_call(client, procnum, inproc, in,
665 outproc, out, tottimeout);
666
667 return ((int) clnt_stat);
668 }
669
670 int
671 alldigits(s)
672 char *s;
673 {
674 int c;
675
676 c = *s++;
677 do {
678 if (!isdigit(c))
679 return (0);
680 } while ((c = *s++) != 0);
681 return (1);
682 }
683