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