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