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