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