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