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