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