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