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