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 #if 0 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1980, 1990, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif /* not lint */ 43 44 #ifndef lint 45 static char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93"; 46 #endif /* not lint */ 47 #endif 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 51 /* 52 * Disk quota editor. 53 */ 54 #include <sys/param.h> 55 #include <sys/stat.h> 56 #include <sys/file.h> 57 #include <sys/wait.h> 58 #include <ufs/ufs/quota.h> 59 #include <ctype.h> 60 #include <err.h> 61 #include <errno.h> 62 #include <fstab.h> 63 #include <grp.h> 64 #include <pwd.h> 65 #include <signal.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 #include "pathnames.h" 71 72 const char *qfname = QUOTAFILENAME; 73 const char *qfextension[] = INITQFNAMES; 74 const char *quotagroup = QUOTAGROUP; 75 char tmpfil[] = _PATH_TMP; 76 77 struct quotause { 78 struct quotause *next; 79 long flags; 80 struct dqblk dqblk; 81 char fsname[MAXPATHLEN + 1]; 82 char qfname[1]; /* actually longer */ 83 }; 84 #define FOUND 0x01 85 86 int alldigits(const char *s); 87 int cvtatos(time_t, char *, time_t *); 88 char *cvtstoa(time_t); 89 int editit(char *); 90 void freeprivs(struct quotause *); 91 int getentry(const char *, int); 92 struct quotause *getprivs(long, int, char *); 93 int hasquota(struct fstab *, int, char **); 94 void putprivs(long, int, struct quotause *); 95 int readprivs(struct quotause *, char *); 96 int readtimes(struct quotause *, char *); 97 static void usage(void); 98 int writetimes(struct quotause *, int, int); 99 int writeprivs(struct quotause *, int, char *, int); 100 101 int 102 main(int argc, char **argv) 103 { 104 struct quotause *qup, *protoprivs, *curprivs; 105 long id, protoid; 106 long long lim; 107 int i, quotatype, range, tmpfd; 108 uid_t startuid, enduid; 109 u_int32_t *limp; 110 char *protoname, *cp, *oldoptarg; 111 int eflag = 0, tflag = 0, pflag = 0, ch; 112 char *fspath = NULL; 113 char buf[MAXLOGNAME]; 114 115 if (argc < 2) 116 usage(); 117 if (getuid()) 118 errx(1, "permission denied"); 119 quotatype = USRQUOTA; 120 protoprivs = NULL; 121 while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) { 122 switch(ch) { 123 case 'f': 124 fspath = optarg; 125 break; 126 case 'p': 127 protoname = optarg; 128 pflag++; 129 break; 130 case 'g': 131 quotatype = GRPQUOTA; 132 break; 133 case 'u': 134 quotatype = USRQUOTA; 135 break; 136 case 't': 137 tflag++; 138 break; 139 case 'e': 140 if ((qup = malloc(sizeof(*qup))) == NULL) 141 errx(2, "out of memory"); 142 bzero(qup, sizeof(*qup)); 143 i = 0; 144 oldoptarg = optarg; 145 for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL; 146 i++) { 147 if (cp != oldoptarg) 148 *(cp - 1) = ':'; 149 limp = NULL; 150 switch (i) { 151 case 0: 152 strlcpy(qup->fsname, cp, 153 sizeof(qup->fsname)); 154 break; 155 case 1: 156 limp = &qup->dqblk.dqb_bsoftlimit; 157 break; 158 case 2: 159 limp = &qup->dqblk.dqb_bhardlimit; 160 break; 161 case 3: 162 limp = &qup->dqblk.dqb_isoftlimit; 163 break; 164 case 4: 165 limp = &qup->dqblk.dqb_ihardlimit; 166 break; 167 default: 168 warnx("incorrect quota specification: " 169 "%s", oldoptarg); 170 usage(); 171 break; /* XXX: report an error */ 172 } 173 if (limp != NULL) { 174 lim = strtoll(cp, NULL, 10); 175 if (lim < 0 || lim > UINT_MAX) 176 errx(1, "invalid limit value: " 177 "%lld", lim); 178 *limp = (u_int32_t)lim; 179 } 180 } 181 qup->dqblk.dqb_bsoftlimit = 182 btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024); 183 qup->dqblk.dqb_bhardlimit = 184 btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024); 185 if (protoprivs == NULL) { 186 protoprivs = curprivs = qup; 187 } else { 188 curprivs->next = qup; 189 curprivs = qup; 190 } 191 eflag++; 192 pflag++; 193 break; 194 default: 195 usage(); 196 } 197 } 198 argc -= optind; 199 argv += optind; 200 if (pflag) { 201 if (protoprivs == NULL) { 202 if ((protoid = getentry(protoname, quotatype)) == -1) 203 exit(1); 204 protoprivs = getprivs(protoid, quotatype, fspath); 205 for (qup = protoprivs; qup; qup = qup->next) { 206 qup->dqblk.dqb_btime = 0; 207 qup->dqblk.dqb_itime = 0; 208 } 209 } 210 for (; argc-- > 0; argv++) { 211 if (strspn(*argv, "0123456789-") == strlen(*argv) && 212 (cp = strchr(*argv, '-')) != NULL) { 213 *cp++ = '\0'; 214 startuid = atoi(*argv); 215 enduid = atoi(cp); 216 if (enduid < startuid) 217 errx(1, 218 "ending uid (%d) must be >= starting uid (%d) when using uid ranges", 219 enduid, startuid); 220 range = 1; 221 } else { 222 startuid = enduid = 0; 223 range = 0; 224 } 225 for ( ; startuid <= enduid; startuid++) { 226 if (range) 227 snprintf(buf, sizeof(buf), "%d", 228 startuid); 229 else 230 snprintf(buf, sizeof(buf), "%s", 231 *argv); 232 if ((id = getentry(buf, quotatype)) < 0) 233 continue; 234 if (eflag) { 235 for (qup = protoprivs; qup; 236 qup = qup->next) { 237 curprivs = getprivs(id, 238 quotatype, qup->fsname); 239 if (curprivs == NULL) 240 continue; 241 strcpy(qup->qfname, 242 curprivs->qfname); 243 strcpy(qup->fsname, 244 curprivs->fsname); 245 } 246 } 247 putprivs(id, quotatype, protoprivs); 248 } 249 } 250 exit(0); 251 } 252 tmpfd = mkstemp(tmpfil); 253 fchown(tmpfd, getuid(), getgid()); 254 if (tflag) { 255 protoprivs = getprivs(0, quotatype, fspath); 256 if (writetimes(protoprivs, tmpfd, quotatype) == 0) 257 exit(1); 258 if (editit(tmpfil) && readtimes(protoprivs, tmpfil)) 259 putprivs(0, quotatype, protoprivs); 260 freeprivs(protoprivs); 261 close(tmpfd); 262 unlink(tmpfil); 263 exit(0); 264 } 265 for ( ; argc > 0; argc--, argv++) { 266 if ((id = getentry(*argv, quotatype)) == -1) 267 continue; 268 curprivs = getprivs(id, quotatype, fspath); 269 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) 270 continue; 271 if (editit(tmpfil) && readprivs(curprivs, tmpfil)) 272 putprivs(id, quotatype, curprivs); 273 freeprivs(curprivs); 274 } 275 close(tmpfd); 276 unlink(tmpfil); 277 exit(0); 278 } 279 280 static void 281 usage() 282 { 283 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 284 "usage: edquota [-u] [-f fspath] [-p username] username ...", 285 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]", 286 " username ...", 287 " edquota -g [-f fspath] [-p groupname] groupname ...", 288 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]", 289 " groupname ...", 290 " edquota [-u] -t [-f fspath]", 291 " edquota -g -t [-f fspath]"); 292 exit(1); 293 } 294 295 /* 296 * This routine converts a name for a particular quota type to 297 * an identifier. This routine must agree with the kernel routine 298 * getinoquota as to the interpretation of quota types. 299 */ 300 int 301 getentry(name, quotatype) 302 const char *name; 303 int quotatype; 304 { 305 struct passwd *pw; 306 struct group *gr; 307 308 if (alldigits(name)) 309 return (atoi(name)); 310 switch(quotatype) { 311 case USRQUOTA: 312 if ((pw = getpwnam(name))) 313 return (pw->pw_uid); 314 warnx("%s: no such user", name); 315 break; 316 case GRPQUOTA: 317 if ((gr = getgrnam(name))) 318 return (gr->gr_gid); 319 warnx("%s: no such group", name); 320 break; 321 default: 322 warnx("%d: unknown quota type", quotatype); 323 break; 324 } 325 sleep(1); 326 return (-1); 327 } 328 329 /* 330 * Collect the requested quota information. 331 */ 332 struct quotause * 333 getprivs(id, quotatype, fspath) 334 register long id; 335 int quotatype; 336 char *fspath; 337 { 338 register struct fstab *fs; 339 register struct quotause *qup, *quptail; 340 struct quotause *quphead; 341 int qcmd, qupsize, fd; 342 char *qfpathname; 343 static int warned = 0; 344 345 setfsent(); 346 quphead = (struct quotause *)0; 347 qcmd = QCMD(Q_GETQUOTA, quotatype); 348 while ((fs = getfsent())) { 349 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) && 350 strcmp(fspath, fs->fs_file)) 351 continue; 352 if (strcmp(fs->fs_vfstype, "ufs")) 353 continue; 354 if (!hasquota(fs, quotatype, &qfpathname)) 355 continue; 356 qupsize = sizeof(*qup) + strlen(qfpathname); 357 if ((qup = (struct quotause *)malloc(qupsize)) == NULL) 358 errx(2, "out of memory"); 359 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) { 360 if (errno == EOPNOTSUPP && !warned) { 361 warned++; 362 warnx("warning: quotas are not compiled into this kernel"); 363 sleep(3); 364 } 365 if ((fd = open(qfpathname, O_RDONLY)) < 0) { 366 fd = open(qfpathname, O_RDWR|O_CREAT, 0640); 367 if (fd < 0 && errno != ENOENT) { 368 warn("%s", qfpathname); 369 free(qup); 370 continue; 371 } 372 warnx("creating quota file %s", qfpathname); 373 sleep(3); 374 (void) fchown(fd, getuid(), 375 getentry(quotagroup, GRPQUOTA)); 376 (void) fchmod(fd, 0640); 377 } 378 lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET); 379 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) { 380 case 0: /* EOF */ 381 /* 382 * Convert implicit 0 quota (EOF) 383 * into an explicit one (zero'ed dqblk) 384 */ 385 bzero((caddr_t)&qup->dqblk, 386 sizeof(struct dqblk)); 387 break; 388 389 case sizeof(struct dqblk): /* OK */ 390 break; 391 392 default: /* ERROR */ 393 warn("read error in %s", qfpathname); 394 close(fd); 395 free(qup); 396 continue; 397 } 398 close(fd); 399 } 400 strcpy(qup->qfname, qfpathname); 401 strcpy(qup->fsname, fs->fs_file); 402 if (quphead == NULL) 403 quphead = qup; 404 else 405 quptail->next = qup; 406 quptail = qup; 407 qup->next = 0; 408 } 409 endfsent(); 410 return (quphead); 411 } 412 413 /* 414 * Store the requested quota information. 415 */ 416 void 417 putprivs(id, quotatype, quplist) 418 long id; 419 int quotatype; 420 struct quotause *quplist; 421 { 422 register struct quotause *qup; 423 int qcmd, fd; 424 425 qcmd = QCMD(Q_SETQUOTA, quotatype); 426 for (qup = quplist; qup; qup = qup->next) { 427 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0) 428 continue; 429 if ((fd = open(qup->qfname, O_WRONLY)) < 0) { 430 warn("%s", qup->qfname); 431 } else { 432 lseek(fd, (long)id * (long)sizeof (struct dqblk), 0); 433 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) != 434 sizeof (struct dqblk)) { 435 warn("%s", qup->qfname); 436 } 437 close(fd); 438 } 439 } 440 } 441 442 /* 443 * Take a list of priviledges and get it edited. 444 */ 445 int 446 editit(tmpf) 447 char *tmpf; 448 { 449 long omask; 450 int pid, status; 451 452 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 453 top: 454 if ((pid = fork()) < 0) { 455 456 if (errno == EPROCLIM) { 457 warnx("you have too many processes"); 458 return(0); 459 } 460 if (errno == EAGAIN) { 461 sleep(1); 462 goto top; 463 } 464 warn("fork"); 465 return (0); 466 } 467 if (pid == 0) { 468 register const char *ed; 469 470 sigsetmask(omask); 471 setgid(getgid()); 472 setuid(getuid()); 473 if ((ed = getenv("EDITOR")) == (char *)0) 474 ed = _PATH_VI; 475 execlp(ed, ed, tmpf, (char *)0); 476 err(1, "%s", ed); 477 } 478 waitpid(pid, &status, 0); 479 sigsetmask(omask); 480 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 481 return (0); 482 return (1); 483 } 484 485 /* 486 * Convert a quotause list to an ASCII file. 487 */ 488 int 489 writeprivs(quplist, outfd, name, quotatype) 490 struct quotause *quplist; 491 int outfd; 492 char *name; 493 int quotatype; 494 { 495 register struct quotause *qup; 496 FILE *fd; 497 498 ftruncate(outfd, 0); 499 lseek(outfd, 0, L_SET); 500 if ((fd = fdopen(dup(outfd), "w")) == NULL) 501 err(1, "%s", tmpfil); 502 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name); 503 for (qup = quplist; qup; qup = qup->next) { 504 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n", 505 qup->fsname, "kbytes in use:", 506 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024), 507 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024), 508 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024)); 509 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n", 510 "\tinodes in use:", 511 (unsigned long)qup->dqblk.dqb_curinodes, 512 (unsigned long)qup->dqblk.dqb_isoftlimit, 513 (unsigned long)qup->dqblk.dqb_ihardlimit); 514 } 515 fclose(fd); 516 return (1); 517 } 518 519 /* 520 * Merge changes to an ASCII file into a quotause list. 521 */ 522 int 523 readprivs(quplist, inname) 524 struct quotause *quplist; 525 char *inname; 526 { 527 register struct quotause *qup; 528 FILE *fd; 529 unsigned long bhardlimit, bsoftlimit, curblocks; 530 unsigned long ihardlimit, isoftlimit, curinodes; 531 int cnt; 532 register char *cp; 533 struct dqblk dqblk; 534 char *fsp, line1[BUFSIZ], line2[BUFSIZ]; 535 536 fd = fopen(inname, "r"); 537 if (fd == NULL) { 538 warnx("can't re-read temp file!!"); 539 return (0); 540 } 541 /* 542 * Discard title line, then read pairs of lines to process. 543 */ 544 (void) fgets(line1, sizeof (line1), fd); 545 while (fgets(line1, sizeof (line1), fd) != NULL && 546 fgets(line2, sizeof (line2), fd) != NULL) { 547 if ((fsp = strtok(line1, " \t:")) == NULL) { 548 warnx("%s: bad format", line1); 549 return (0); 550 } 551 if ((cp = strtok((char *)0, "\n")) == NULL) { 552 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]); 553 return (0); 554 } 555 cnt = sscanf(cp, 556 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)", 557 &curblocks, &bsoftlimit, &bhardlimit); 558 if (cnt != 3) { 559 warnx("%s:%s: bad format", fsp, cp); 560 return (0); 561 } 562 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024); 563 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024); 564 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024); 565 if ((cp = strtok(line2, "\n")) == NULL) { 566 warnx("%s: %s: bad format", fsp, line2); 567 return (0); 568 } 569 cnt = sscanf(cp, 570 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)", 571 &curinodes, &isoftlimit, &ihardlimit); 572 if (cnt != 3) { 573 warnx("%s: %s: bad format", fsp, line2); 574 return (0); 575 } 576 dqblk.dqb_curinodes = curinodes; 577 dqblk.dqb_isoftlimit = isoftlimit; 578 dqblk.dqb_ihardlimit = ihardlimit; 579 for (qup = quplist; qup; qup = qup->next) { 580 if (strcmp(fsp, qup->fsname)) 581 continue; 582 /* 583 * Cause time limit to be reset when the quota 584 * is next used if previously had no soft limit 585 * or were under it, but now have a soft limit 586 * and are over it. 587 */ 588 if (dqblk.dqb_bsoftlimit && 589 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit && 590 (qup->dqblk.dqb_bsoftlimit == 0 || 591 qup->dqblk.dqb_curblocks < 592 qup->dqblk.dqb_bsoftlimit)) 593 qup->dqblk.dqb_btime = 0; 594 if (dqblk.dqb_isoftlimit && 595 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit && 596 (qup->dqblk.dqb_isoftlimit == 0 || 597 qup->dqblk.dqb_curinodes < 598 qup->dqblk.dqb_isoftlimit)) 599 qup->dqblk.dqb_itime = 0; 600 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit; 601 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit; 602 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit; 603 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit; 604 qup->flags |= FOUND; 605 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks && 606 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes) 607 break; 608 warnx("%s: cannot change current allocation", fsp); 609 break; 610 } 611 } 612 fclose(fd); 613 /* 614 * Disable quotas for any filesystems that have not been found. 615 */ 616 for (qup = quplist; qup; qup = qup->next) { 617 if (qup->flags & FOUND) { 618 qup->flags &= ~FOUND; 619 continue; 620 } 621 qup->dqblk.dqb_bsoftlimit = 0; 622 qup->dqblk.dqb_bhardlimit = 0; 623 qup->dqblk.dqb_isoftlimit = 0; 624 qup->dqblk.dqb_ihardlimit = 0; 625 } 626 return (1); 627 } 628 629 /* 630 * Convert a quotause list to an ASCII file of grace times. 631 */ 632 int 633 writetimes(quplist, outfd, quotatype) 634 struct quotause *quplist; 635 int outfd; 636 int quotatype; 637 { 638 register struct quotause *qup; 639 FILE *fd; 640 641 ftruncate(outfd, 0); 642 lseek(outfd, 0, L_SET); 643 if ((fd = fdopen(dup(outfd), "w")) == NULL) 644 err(1, "%s", tmpfil); 645 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n"); 646 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n", 647 qfextension[quotatype]); 648 for (qup = quplist; qup; qup = qup->next) { 649 fprintf(fd, "%s: block grace period: %s, ", 650 qup->fsname, cvtstoa(qup->dqblk.dqb_btime)); 651 fprintf(fd, "file grace period: %s\n", 652 cvtstoa(qup->dqblk.dqb_itime)); 653 } 654 fclose(fd); 655 return (1); 656 } 657 658 /* 659 * Merge changes of grace times in an ASCII file into a quotause list. 660 */ 661 int 662 readtimes(quplist, inname) 663 struct quotause *quplist; 664 char *inname; 665 { 666 register struct quotause *qup; 667 FILE *fd; 668 int cnt; 669 register char *cp; 670 time_t itime, btime, iseconds, bseconds; 671 long l_itime, l_btime; 672 char *fsp, bunits[10], iunits[10], line1[BUFSIZ]; 673 674 fd = fopen(inname, "r"); 675 if (fd == NULL) { 676 warnx("can't re-read temp file!!"); 677 return (0); 678 } 679 /* 680 * Discard two title lines, then read lines to process. 681 */ 682 (void) fgets(line1, sizeof (line1), fd); 683 (void) fgets(line1, sizeof (line1), fd); 684 while (fgets(line1, sizeof (line1), fd) != NULL) { 685 if ((fsp = strtok(line1, " \t:")) == NULL) { 686 warnx("%s: bad format", line1); 687 return (0); 688 } 689 if ((cp = strtok((char *)0, "\n")) == NULL) { 690 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]); 691 return (0); 692 } 693 cnt = sscanf(cp, 694 " block grace period: %ld %s file grace period: %ld %s", 695 &l_btime, bunits, &l_itime, iunits); 696 if (cnt != 4) { 697 warnx("%s:%s: bad format", fsp, cp); 698 return (0); 699 } 700 btime = l_btime; 701 itime = l_itime; 702 if (cvtatos(btime, bunits, &bseconds) == 0) 703 return (0); 704 if (cvtatos(itime, iunits, &iseconds) == 0) 705 return (0); 706 for (qup = quplist; qup; qup = qup->next) { 707 if (strcmp(fsp, qup->fsname)) 708 continue; 709 qup->dqblk.dqb_btime = bseconds; 710 qup->dqblk.dqb_itime = iseconds; 711 qup->flags |= FOUND; 712 break; 713 } 714 } 715 fclose(fd); 716 /* 717 * reset default grace periods for any filesystems 718 * that have not been found. 719 */ 720 for (qup = quplist; qup; qup = qup->next) { 721 if (qup->flags & FOUND) { 722 qup->flags &= ~FOUND; 723 continue; 724 } 725 qup->dqblk.dqb_btime = 0; 726 qup->dqblk.dqb_itime = 0; 727 } 728 return (1); 729 } 730 731 /* 732 * Convert seconds to ASCII times. 733 */ 734 char * 735 cvtstoa(secs) 736 time_t secs; 737 { 738 static char buf[20]; 739 740 if (secs % (24 * 60 * 60) == 0) { 741 secs /= 24 * 60 * 60; 742 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s"); 743 } else if (secs % (60 * 60) == 0) { 744 secs /= 60 * 60; 745 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s"); 746 } else if (secs % 60 == 0) { 747 secs /= 60; 748 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s"); 749 } else 750 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s"); 751 return (buf); 752 } 753 754 /* 755 * Convert ASCII input times to seconds. 756 */ 757 int 758 cvtatos(period, units, seconds) 759 time_t period; 760 char *units; 761 time_t *seconds; 762 { 763 764 if (bcmp(units, "second", 6) == 0) 765 *seconds = period; 766 else if (bcmp(units, "minute", 6) == 0) 767 *seconds = period * 60; 768 else if (bcmp(units, "hour", 4) == 0) 769 *seconds = period * 60 * 60; 770 else if (bcmp(units, "day", 3) == 0) 771 *seconds = period * 24 * 60 * 60; 772 else { 773 printf("%s: bad units, specify %s\n", units, 774 "days, hours, minutes, or seconds"); 775 return (0); 776 } 777 return (1); 778 } 779 780 /* 781 * Free a list of quotause structures. 782 */ 783 void 784 freeprivs(quplist) 785 struct quotause *quplist; 786 { 787 register struct quotause *qup, *nextqup; 788 789 for (qup = quplist; qup; qup = nextqup) { 790 nextqup = qup->next; 791 free(qup); 792 } 793 } 794 795 /* 796 * Check whether a string is completely composed of digits. 797 */ 798 int 799 alldigits(s) 800 register const char *s; 801 { 802 register int c; 803 804 c = *s++; 805 do { 806 if (!isdigit(c)) 807 return (0); 808 } while ((c = *s++)); 809 return (1); 810 } 811 812 /* 813 * Check to see if a particular quota is to be enabled. 814 */ 815 int 816 hasquota(fs, type, qfnamep) 817 register struct fstab *fs; 818 int type; 819 char **qfnamep; 820 { 821 register char *opt; 822 char *cp; 823 static char initname, usrname[100], grpname[100]; 824 static char buf[BUFSIZ]; 825 826 if (!initname) { 827 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 828 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 829 initname = 1; 830 } 831 strcpy(buf, fs->fs_mntops); 832 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 833 if ((cp = index(opt, '='))) 834 *cp++ = '\0'; 835 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 836 break; 837 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 838 break; 839 } 840 if (!opt) 841 return (0); 842 if (cp) { 843 *qfnamep = cp; 844 return (1); 845 } 846 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 847 *qfnamep = buf; 848 return (1); 849 } 850