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