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