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.5 2005/03/18 00:57:10 joerg 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 seconds); 82 static struct quotause *getprivs(long id, int quotatype); 83 static void usage(void); 84 static void showuid(u_long uid); 85 static void showgid(u_long gid); 86 static int alldigits(char *s); 87 static void showusrname(char *name); 88 static void showgrpname(char *name); 89 static void showquotas(int type, u_long id, const char *name); 90 static void heading(int type, u_long id, const char *name, const char *tag); 91 static int ufshasquota(struct fstab *fs, int type, char **qfnamep); 92 static int getufsquota(struct fstab *fs, struct quotause *qup, long id, 93 int quotatype); 94 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id, 95 int quotatype); 96 static int callaurpc(char *host, int prognum, int versnum, int procnum, 97 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out); 98 99 int lflag; 100 int qflag; 101 int vflag; 102 103 int 104 main(int argc, char *argv[]) 105 { 106 int ngroups; 107 gid_t mygid, gidset[NGROUPS]; 108 int i, gflag = 0, uflag = 0; 109 char ch; 110 111 while ((ch = getopt(argc, argv, "glquv")) != -1) { 112 switch(ch) { 113 case 'g': 114 gflag++; 115 break; 116 case 'l': 117 lflag++; 118 break; 119 case 'q': 120 qflag++; 121 break; 122 case 'u': 123 uflag++; 124 break; 125 case 'v': 126 vflag++; 127 break; 128 default: 129 usage(); 130 } 131 } 132 argc -= optind; 133 argv += optind; 134 if (!uflag && !gflag) 135 uflag++; 136 if (argc == 0) { 137 if (uflag) 138 showuid(getuid()); 139 if (gflag) { 140 mygid = getgid(); 141 ngroups = getgroups(NGROUPS, gidset); 142 if (ngroups < 0) 143 err(1, "getgroups"); 144 showgid(mygid); 145 for (i = 0; i < ngroups; i++) 146 if (gidset[i] != mygid) 147 showgid(gidset[i]); 148 } 149 return(0); 150 } 151 if (uflag && gflag) 152 usage(); 153 if (uflag) { 154 for (; argc > 0; argc--, argv++) { 155 if (alldigits(*argv)) 156 showuid(atoi(*argv)); 157 else 158 showusrname(*argv); 159 } 160 return(0); 161 } 162 if (gflag) { 163 for (; argc > 0; argc--, argv++) { 164 if (alldigits(*argv)) 165 showgid(atoi(*argv)); 166 else 167 showgrpname(*argv); 168 } 169 } 170 return(0); 171 } 172 173 static void 174 usage(void) 175 { 176 177 fprintf(stderr, "%s\n%s\n%s\n", 178 "usage: quota [-glu] [-v | -q]", 179 " quota [-lu] [-v | -q] user ...", 180 " quota -g [-l] [-v | -q] group ..."); 181 exit(1); 182 } 183 184 /* 185 * Print out quotas for a specified user identifier. 186 */ 187 static void 188 showuid(u_long uid) 189 { 190 struct passwd *pwd = getpwuid(uid); 191 u_long myuid; 192 const char *name; 193 194 if (pwd == NULL) 195 name = "(no account)"; 196 else 197 name = pwd->pw_name; 198 myuid = getuid(); 199 if (uid != myuid && myuid != 0) { 200 printf("quota: %s (uid %lu): permission denied\n", name, uid); 201 return; 202 } 203 showquotas(USRQUOTA, uid, name); 204 } 205 206 /* 207 * Print out quotas for a specifed user name. 208 */ 209 static void 210 showusrname(char *name) 211 { 212 struct passwd *pwd = getpwnam(name); 213 u_long myuid; 214 215 if (pwd == NULL) { 216 warnx("%s: unknown user", name); 217 return; 218 } 219 myuid = getuid(); 220 if (pwd->pw_uid != myuid && myuid != 0) { 221 warnx("%s (uid %u): permission denied", name, pwd->pw_uid); 222 return; 223 } 224 showquotas(USRQUOTA, pwd->pw_uid, name); 225 } 226 227 /* 228 * Print out quotas for a specified group identifier. 229 */ 230 static void 231 showgid(u_long gid) 232 { 233 struct group *grp = getgrgid(gid); 234 int ngroups; 235 gid_t mygid, gidset[NGROUPS]; 236 int i; 237 const char *name; 238 239 if (grp == NULL) 240 name = "(no entry)"; 241 else 242 name = grp->gr_name; 243 mygid = getgid(); 244 ngroups = getgroups(NGROUPS, gidset); 245 if (ngroups < 0) { 246 warn("getgroups"); 247 return; 248 } 249 if (gid != mygid) { 250 for (i = 0; i < ngroups; i++) 251 if (gid == gidset[i]) 252 break; 253 if (i >= ngroups && getuid() != 0) { 254 warnx("%s (gid %lu): permission denied", name, gid); 255 return; 256 } 257 } 258 showquotas(GRPQUOTA, gid, name); 259 } 260 261 /* 262 * Print out quotas for a specifed group name. 263 */ 264 static void 265 showgrpname(char *name) 266 { 267 struct group *grp = getgrnam(name); 268 int ngroups; 269 gid_t mygid, gidset[NGROUPS]; 270 int i; 271 272 if (grp == NULL) { 273 warnx("%s: unknown group", name); 274 return; 275 } 276 mygid = getgid(); 277 ngroups = getgroups(NGROUPS, gidset); 278 if (ngroups < 0) { 279 warn("getgroups"); 280 return; 281 } 282 if (grp->gr_gid != mygid) { 283 for (i = 0; i < ngroups; i++) 284 if (grp->gr_gid == gidset[i]) 285 break; 286 if (i >= ngroups && getuid() != 0) { 287 warnx("%s (gid %u): permission denied", name, 288 grp->gr_gid); 289 return; 290 } 291 } 292 showquotas(GRPQUOTA, grp->gr_gid, name); 293 } 294 295 static void 296 showquotas(int type, u_long id, const char *name) 297 { 298 struct quotause *qup; 299 struct quotause *quplist; 300 const char *msgi, *msgb; 301 const char *nam; 302 int lines = 0; 303 static time_t now; 304 305 if (now == 0) 306 time(&now); 307 quplist = getprivs(id, type); 308 for (qup = quplist; qup; qup = qup->next) { 309 if (!vflag && 310 qup->dqblk.dqb_isoftlimit == 0 && 311 qup->dqblk.dqb_ihardlimit == 0 && 312 qup->dqblk.dqb_bsoftlimit == 0 && 313 qup->dqblk.dqb_bhardlimit == 0) 314 continue; 315 msgi = (char *)0; 316 if (qup->dqblk.dqb_ihardlimit && 317 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) 318 msgi = "File limit reached on"; 319 else if (qup->dqblk.dqb_isoftlimit && 320 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) { 321 if (qup->dqblk.dqb_itime > now) 322 msgi = "In file grace period on"; 323 else 324 msgi = "Over file quota on"; 325 } 326 msgb = (char *)0; 327 if (qup->dqblk.dqb_bhardlimit && 328 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) 329 msgb = "Block limit reached on"; 330 else if (qup->dqblk.dqb_bsoftlimit && 331 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) { 332 if (qup->dqblk.dqb_btime > now) 333 msgb = "In block grace period on"; 334 else 335 msgb = "Over block quota on"; 336 } 337 if (qflag) { 338 if ((msgi != (char *)0 || msgb != (char *)0) && 339 lines++ == 0) 340 heading(type, id, name, ""); 341 if (msgi != (char *)0) 342 printf("\t%s %s\n", msgi, qup->fsname); 343 if (msgb != (char *)0) 344 printf("\t%s %s\n", msgb, qup->fsname); 345 continue; 346 } 347 if (vflag || 348 qup->dqblk.dqb_curblocks || 349 qup->dqblk.dqb_curinodes) { 350 if (lines++ == 0) 351 heading(type, id, name, ""); 352 nam = qup->fsname; 353 if (strlen(qup->fsname) > 15) { 354 printf("%s\n", qup->fsname); 355 nam = ""; 356 } 357 printf("%15s%8lu%c%7lu%8lu%8s" 358 , nam 359 , (u_long) (dbtob(qup->dqblk.dqb_curblocks) 360 / 1024) 361 , (msgb == (char *)0) ? ' ' : '*' 362 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit) 363 / 1024) 364 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit) 365 / 1024) 366 , (msgb == (char *)0) ? "" 367 :timeprt(qup->dqblk.dqb_btime)); 368 printf("%8lu%c%7lu%8lu%8s\n" 369 , (u_long)qup->dqblk.dqb_curinodes 370 , (msgi == (char *)0) ? ' ' : '*' 371 , (u_long)qup->dqblk.dqb_isoftlimit 372 , (u_long)qup->dqblk.dqb_ihardlimit 373 , (msgi == (char *)0) ? "" 374 : timeprt(qup->dqblk.dqb_itime) 375 ); 376 continue; 377 } 378 } 379 if (!qflag && lines == 0) 380 heading(type, id, name, "none"); 381 } 382 383 static void 384 heading(int type, u_long id, const char *name, const char *tag) 385 { 386 387 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type], 388 name, *qfextension[type], id, tag); 389 if (!qflag && tag[0] == '\0') { 390 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n" 391 , "Filesystem" 392 , "usage" 393 , "quota" 394 , "limit" 395 , "grace" 396 , "files" 397 , "quota" 398 , "limit" 399 , "grace" 400 ); 401 } 402 } 403 404 /* 405 * Calculate the grace period and return a printable string for it. 406 */ 407 static const char * 408 timeprt(time_t seconds) 409 { 410 time_t hours, minutes; 411 static char buf[20]; 412 static time_t now; 413 414 if (now == 0) 415 time(&now); 416 if (now > seconds) 417 return ("none"); 418 seconds -= now; 419 minutes = (seconds + 30) / 60; 420 hours = (minutes + 30) / 60; 421 if (hours >= 36) { 422 sprintf(buf, "%lddays", ((long)hours + 12) / 24); 423 return (buf); 424 } 425 if (minutes >= 60) { 426 sprintf(buf, "%2ld:%ld", (long)minutes / 60, 427 (long)minutes % 60); 428 return (buf); 429 } 430 sprintf(buf, "%2ld", (long)minutes); 431 return (buf); 432 } 433 434 /* 435 * Collect the requested quota information. 436 */ 437 static struct quotause * 438 getprivs(long id, int quotatype) 439 { 440 struct quotause *qup, *quptail = NULL; 441 struct fstab *fs; 442 struct quotause *quphead; 443 struct statfs *fst; 444 int nfst, i; 445 446 qup = quphead = (struct quotause *)0; 447 448 nfst = getmntinfo(&fst, MNT_NOWAIT); 449 if (nfst == 0) 450 errx(2, "no filesystems mounted!"); 451 setfsent(); 452 for (i=0; i<nfst; i++) { 453 if (qup == NULL) { 454 if ((qup = (struct quotause *)malloc(sizeof *qup)) 455 == NULL) 456 errx(2, "out of memory"); 457 } 458 if (strcmp(fst[i].f_fstypename, "nfs") == 0) { 459 if (lflag) 460 continue; 461 if (getnfsquota(&fst[i], qup, id, quotatype) 462 == 0) 463 continue; 464 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) { 465 /* 466 * XXX 467 * UFS filesystems must be in /etc/fstab, and must 468 * indicate that they have quotas on (?!) This is quite 469 * unlike SunOS where quotas can be enabled/disabled 470 * on a filesystem independent of /etc/fstab, and it 471 * will still print quotas for them. 472 */ 473 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL) 474 continue; 475 if (getufsquota(fs, qup, id, quotatype) == 0) 476 continue; 477 } else 478 continue; 479 strcpy(qup->fsname, fst[i].f_mntonname); 480 if (quphead == NULL) 481 quphead = qup; 482 else 483 quptail->next = qup; 484 quptail = qup; 485 quptail->next = 0; 486 qup = NULL; 487 } 488 if (qup) 489 free(qup); 490 endfsent(); 491 return (quphead); 492 } 493 494 /* 495 * Check to see if a particular quota is to be enabled. 496 */ 497 static int 498 ufshasquota(struct fstab *fs, int type, char **qfnamep) 499 { 500 static char initname, usrname[100], grpname[100]; 501 static char buf[BUFSIZ]; 502 char *opt, *cp; 503 504 if (!initname) { 505 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 506 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 507 initname = 1; 508 } 509 strcpy(buf, fs->fs_mntops); 510 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 511 if ((cp = strchr(opt, '='))) 512 *cp++ = '\0'; 513 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 514 break; 515 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 516 break; 517 } 518 if (!opt) 519 return (0); 520 if (cp) { 521 *qfnamep = cp; 522 return (1); 523 } 524 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 525 *qfnamep = buf; 526 return (1); 527 } 528 529 static int 530 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype) 531 { 532 char *qfpathname; 533 int fd, qcmd; 534 535 qcmd = QCMD(Q_GETQUOTA, quotatype); 536 if (!ufshasquota(fs, quotatype, &qfpathname)) 537 return (0); 538 539 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) { 540 if ((fd = open(qfpathname, O_RDONLY)) < 0) { 541 warn("%s", qfpathname); 542 return (0); 543 } 544 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET); 545 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) { 546 case 0: /* EOF */ 547 /* 548 * Convert implicit 0 quota (EOF) 549 * into an explicit one (zero'ed dqblk) 550 */ 551 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk)); 552 break; 553 case sizeof(struct dqblk): /* OK */ 554 break; 555 default: /* ERROR */ 556 warn("read error: %s", qfpathname); 557 close(fd); 558 return (0); 559 } 560 close(fd); 561 } 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