edquota.c revision 166480
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 * 4. 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 33#if 0 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1980, 1990, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93"; 42#endif /* not lint */ 43#endif 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: head/usr.sbin/edquota/edquota.c 166480 2007-02-04 01:41:32Z mpp $"); 47 48/* 49 * Disk quota editor. 50 */ 51 52#include <sys/param.h> 53#include <sys/stat.h> 54#include <sys/file.h> 55#include <sys/wait.h> 56#include <ufs/ufs/quota.h> 57#include <ctype.h> 58#include <err.h> 59#include <errno.h> 60#include <fstab.h> 61#include <grp.h> 62#include <pwd.h> 63#include <signal.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <string.h> 67#include <unistd.h> 68#include "pathnames.h" 69 70const char *qfname = QUOTAFILENAME; 71const char *qfextension[] = INITQFNAMES; 72const char *quotagroup = QUOTAGROUP; 73char tmpfil[] = _PATH_TMP; 74 75struct quotause { 76 struct quotause *next; 77 long flags; 78 struct dqblk dqblk; 79 char fsname[MAXPATHLEN + 1]; 80 char qfname[1]; /* actually longer */ 81}; 82#define FOUND 0x01 83 84int alldigits(const char *s); 85int cvtatos(time_t, char *, time_t *); 86char *cvtstoa(time_t); 87int editit(char *); 88void freeprivs(struct quotause *); 89int getentry(const char *, int); 90struct quotause *getprivs(long, int, char *); 91int hasquota(struct fstab *, int, char **); 92void putprivs(long, int, struct quotause *); 93int readprivs(struct quotause *, char *); 94int readtimes(struct quotause *, char *); 95static void usage(void); 96int writetimes(struct quotause *, int, int); 97int writeprivs(struct quotause *, int, char *, int); 98 99int 100main(int argc, char **argv) 101{ 102 struct quotause *qup, *protoprivs, *curprivs; 103 long id, protoid; 104 long long lim; 105 int i, quotatype, range, tmpfd; 106 uid_t startuid, enduid; 107 u_int32_t *limp; 108 char *protoname, *cp, *oldoptarg; 109 int eflag = 0, tflag = 0, pflag = 0, ch; 110 char *fspath = NULL; 111 char buf[MAXLOGNAME]; 112 113 if (argc < 2) 114 usage(); 115 if (getuid()) 116 errx(1, "permission denied"); 117 quotatype = USRQUOTA; 118 protoprivs = NULL; 119 curprivs = NULL; 120 protoname = 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(0L, 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 280static void 281usage() 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 */ 300int 301getentry(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 */ 332struct quotause * 333getprivs(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 = quptail = NULL; 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 if (lseek(fd, (off_t)id * sizeof(struct dqblk), 379 L_SET) < 0) { 380 warn("seek error on %s", qfpathname); 381 close(fd); 382 free(qup); 383 continue; 384 } 385 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) { 386 case 0: /* EOF */ 387 /* 388 * Convert implicit 0 quota (EOF) 389 * into an explicit one (zero'ed dqblk) 390 */ 391 bzero((caddr_t)&qup->dqblk, 392 sizeof(struct dqblk)); 393 break; 394 395 case sizeof(struct dqblk): /* OK */ 396 break; 397 398 default: /* ERROR */ 399 warn("read error in %s", qfpathname); 400 close(fd); 401 free(qup); 402 continue; 403 } 404 close(fd); 405 } 406 strcpy(qup->qfname, qfpathname); 407 strcpy(qup->fsname, fs->fs_file); 408 if (quphead == NULL) 409 quphead = qup; 410 else 411 quptail->next = qup; 412 quptail = qup; 413 qup->next = 0; 414 } 415 endfsent(); 416 return (quphead); 417} 418 419/* 420 * Store the requested quota information. 421 */ 422void 423putprivs(id, quotatype, quplist) 424 long id; 425 int quotatype; 426 struct quotause *quplist; 427{ 428 register struct quotause *qup; 429 int qcmd, fd; 430 struct dqblk dqbuf; 431 432 qcmd = QCMD(Q_SETQUOTA, quotatype); 433 for (qup = quplist; qup; qup = qup->next) { 434 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0) 435 continue; 436 if ((fd = open(qup->qfname, O_RDWR)) < 0) { 437 warn("%s", qup->qfname); 438 continue; 439 } 440 if (lseek(fd, (off_t)id * sizeof(struct dqblk), L_SET) < 0) { 441 warn("seek error on %s", qup->qfname); 442 close(fd); 443 continue; 444 } 445 switch (read(fd, &dqbuf, sizeof(struct dqblk))) { 446 case 0: /* EOF */ 447 /* 448 * Convert implicit 0 quota (EOF) 449 * into an explicit one (zero'ed dqblk) 450 */ 451 bzero(&dqbuf, sizeof(struct dqblk)); 452 break; 453 454 case sizeof(struct dqblk): /* OK */ 455 break; 456 457 default: /* ERROR */ 458 warn("read error in %s", qup->qfname); 459 close(fd); 460 continue; 461 } 462 /* 463 * Reset time limit if have a soft limit and were 464 * previously under it, but are now over it 465 * or if there previously was no soft limit, but 466 * now have one and are over it. 467 */ 468 if (dqbuf.dqb_bsoftlimit && id != 0 && 469 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 470 dqbuf.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) 471 qup->dqblk.dqb_btime = 0; 472 if (dqbuf.dqb_bsoftlimit == 0 && id != 0 && 473 dqbuf.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) 474 qup->dqblk.dqb_btime = 0; 475 if (dqbuf.dqb_isoftlimit && id != 0 && 476 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && 477 dqbuf.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) 478 qup->dqblk.dqb_itime = 0; 479 if (dqbuf.dqb_isoftlimit == 0 && id !=0 && 480 dqbuf.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) 481 qup->dqblk.dqb_itime = 0; 482 qup->dqblk.dqb_curinodes = dqbuf.dqb_curinodes; 483 qup->dqblk.dqb_curblocks = dqbuf.dqb_curblocks; 484 if (lseek(fd, (off_t)id * sizeof(struct dqblk), L_SET) < 0) { 485 warn("seek error on %s", qup->qfname); 486 close(fd); 487 continue; 488 } 489 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) != 490 sizeof (struct dqblk)) { 491 warn("%s", qup->qfname); 492 } 493 close(fd); 494 } 495} 496 497/* 498 * Take a list of priviledges and get it edited. 499 */ 500int 501editit(tmpf) 502 char *tmpf; 503{ 504 long omask; 505 int pid, status; 506 507 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 508 top: 509 if ((pid = fork()) < 0) { 510 511 if (errno == EPROCLIM) { 512 warnx("you have too many processes"); 513 return(0); 514 } 515 if (errno == EAGAIN) { 516 sleep(1); 517 goto top; 518 } 519 warn("fork"); 520 return (0); 521 } 522 if (pid == 0) { 523 register const char *ed; 524 525 sigsetmask(omask); 526 setgid(getgid()); 527 setuid(getuid()); 528 if ((ed = getenv("EDITOR")) == (char *)0) 529 ed = _PATH_VI; 530 execlp(ed, ed, tmpf, (char *)0); 531 err(1, "%s", ed); 532 } 533 waitpid(pid, &status, 0); 534 sigsetmask(omask); 535 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 536 return (0); 537 return (1); 538} 539 540/* 541 * Convert a quotause list to an ASCII file. 542 */ 543int 544writeprivs(quplist, outfd, name, quotatype) 545 struct quotause *quplist; 546 int outfd; 547 char *name; 548 int quotatype; 549{ 550 register struct quotause *qup; 551 FILE *fd; 552 553 ftruncate(outfd, 0); 554 lseek(outfd, 0, L_SET); 555 if ((fd = fdopen(dup(outfd), "w")) == NULL) 556 err(1, "%s", tmpfil); 557 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name); 558 for (qup = quplist; qup; qup = qup->next) { 559 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n", 560 qup->fsname, "kbytes in use:", 561 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024), 562 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024), 563 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024)); 564 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n", 565 "\tinodes in use:", 566 (unsigned long)qup->dqblk.dqb_curinodes, 567 (unsigned long)qup->dqblk.dqb_isoftlimit, 568 (unsigned long)qup->dqblk.dqb_ihardlimit); 569 } 570 fclose(fd); 571 return (1); 572} 573 574/* 575 * Merge changes to an ASCII file into a quotause list. 576 */ 577int 578readprivs(quplist, inname) 579 struct quotause *quplist; 580 char *inname; 581{ 582 register struct quotause *qup; 583 FILE *fd; 584 unsigned long bhardlimit, bsoftlimit, curblocks; 585 unsigned long ihardlimit, isoftlimit, curinodes; 586 int cnt; 587 register char *cp; 588 struct dqblk dqblk; 589 char *fsp, line1[BUFSIZ], line2[BUFSIZ]; 590 591 fd = fopen(inname, "r"); 592 if (fd == NULL) { 593 warnx("can't re-read temp file!!"); 594 return (0); 595 } 596 /* 597 * Discard title line, then read pairs of lines to process. 598 */ 599 (void) fgets(line1, sizeof (line1), fd); 600 while (fgets(line1, sizeof (line1), fd) != NULL && 601 fgets(line2, sizeof (line2), fd) != NULL) { 602 if ((fsp = strtok(line1, " \t:")) == NULL) { 603 warnx("%s: bad format", line1); 604 return (0); 605 } 606 if ((cp = strtok((char *)0, "\n")) == NULL) { 607 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]); 608 return (0); 609 } 610 cnt = sscanf(cp, 611 " kbytes in use: %lu, limits (soft = %lu, hard = %lu)", 612 &curblocks, &bsoftlimit, &bhardlimit); 613 if (cnt != 3) { 614 warnx("%s:%s: bad format", fsp, cp); 615 return (0); 616 } 617 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024); 618 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024); 619 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024); 620 if ((cp = strtok(line2, "\n")) == NULL) { 621 warnx("%s: %s: bad format", fsp, line2); 622 return (0); 623 } 624 cnt = sscanf(cp, 625 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)", 626 &curinodes, &isoftlimit, &ihardlimit); 627 if (cnt != 3) { 628 warnx("%s: %s: bad format", fsp, line2); 629 return (0); 630 } 631 dqblk.dqb_curinodes = curinodes; 632 dqblk.dqb_isoftlimit = isoftlimit; 633 dqblk.dqb_ihardlimit = ihardlimit; 634 for (qup = quplist; qup; qup = qup->next) { 635 if (strcmp(fsp, qup->fsname)) 636 continue; 637 /* 638 * Cause time limit to be reset when the quota 639 * is next used if previously had no soft limit 640 * or were under it, but now have a soft limit 641 * and are over it. 642 */ 643 if (dqblk.dqb_bsoftlimit && 644 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit && 645 (qup->dqblk.dqb_bsoftlimit == 0 || 646 qup->dqblk.dqb_curblocks < 647 qup->dqblk.dqb_bsoftlimit)) 648 qup->dqblk.dqb_btime = 0; 649 if (dqblk.dqb_isoftlimit && 650 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit && 651 (qup->dqblk.dqb_isoftlimit == 0 || 652 qup->dqblk.dqb_curinodes < 653 qup->dqblk.dqb_isoftlimit)) 654 qup->dqblk.dqb_itime = 0; 655 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit; 656 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit; 657 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit; 658 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit; 659 qup->flags |= FOUND; 660 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks && 661 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes) 662 break; 663 warnx("%s: cannot change current allocation", fsp); 664 break; 665 } 666 } 667 fclose(fd); 668 /* 669 * Disable quotas for any filesystems that have not been found. 670 */ 671 for (qup = quplist; qup; qup = qup->next) { 672 if (qup->flags & FOUND) { 673 qup->flags &= ~FOUND; 674 continue; 675 } 676 qup->dqblk.dqb_bsoftlimit = 0; 677 qup->dqblk.dqb_bhardlimit = 0; 678 qup->dqblk.dqb_isoftlimit = 0; 679 qup->dqblk.dqb_ihardlimit = 0; 680 } 681 return (1); 682} 683 684/* 685 * Convert a quotause list to an ASCII file of grace times. 686 */ 687int 688writetimes(quplist, outfd, quotatype) 689 struct quotause *quplist; 690 int outfd; 691 int quotatype; 692{ 693 register struct quotause *qup; 694 FILE *fd; 695 696 ftruncate(outfd, 0); 697 lseek(outfd, 0, L_SET); 698 if ((fd = fdopen(dup(outfd), "w")) == NULL) 699 err(1, "%s", tmpfil); 700 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n"); 701 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n", 702 qfextension[quotatype]); 703 for (qup = quplist; qup; qup = qup->next) { 704 fprintf(fd, "%s: block grace period: %s, ", 705 qup->fsname, cvtstoa(qup->dqblk.dqb_btime)); 706 fprintf(fd, "file grace period: %s\n", 707 cvtstoa(qup->dqblk.dqb_itime)); 708 } 709 fclose(fd); 710 return (1); 711} 712 713/* 714 * Merge changes of grace times in an ASCII file into a quotause list. 715 */ 716int 717readtimes(quplist, inname) 718 struct quotause *quplist; 719 char *inname; 720{ 721 register struct quotause *qup; 722 FILE *fd; 723 int cnt; 724 register char *cp; 725 time_t itime, btime, iseconds, bseconds; 726 long l_itime, l_btime; 727 char *fsp, bunits[10], iunits[10], line1[BUFSIZ]; 728 729 fd = fopen(inname, "r"); 730 if (fd == NULL) { 731 warnx("can't re-read temp file!!"); 732 return (0); 733 } 734 /* 735 * Discard two title lines, then read lines to process. 736 */ 737 (void) fgets(line1, sizeof (line1), fd); 738 (void) fgets(line1, sizeof (line1), fd); 739 while (fgets(line1, sizeof (line1), fd) != NULL) { 740 if ((fsp = strtok(line1, " \t:")) == NULL) { 741 warnx("%s: bad format", line1); 742 return (0); 743 } 744 if ((cp = strtok((char *)0, "\n")) == NULL) { 745 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]); 746 return (0); 747 } 748 cnt = sscanf(cp, 749 " block grace period: %ld %s file grace period: %ld %s", 750 &l_btime, bunits, &l_itime, iunits); 751 if (cnt != 4) { 752 warnx("%s:%s: bad format", fsp, cp); 753 return (0); 754 } 755 btime = l_btime; 756 itime = l_itime; 757 if (cvtatos(btime, bunits, &bseconds) == 0) 758 return (0); 759 if (cvtatos(itime, iunits, &iseconds) == 0) 760 return (0); 761 for (qup = quplist; qup; qup = qup->next) { 762 if (strcmp(fsp, qup->fsname)) 763 continue; 764 qup->dqblk.dqb_btime = bseconds; 765 qup->dqblk.dqb_itime = iseconds; 766 qup->flags |= FOUND; 767 break; 768 } 769 } 770 fclose(fd); 771 /* 772 * reset default grace periods for any filesystems 773 * that have not been found. 774 */ 775 for (qup = quplist; qup; qup = qup->next) { 776 if (qup->flags & FOUND) { 777 qup->flags &= ~FOUND; 778 continue; 779 } 780 qup->dqblk.dqb_btime = 0; 781 qup->dqblk.dqb_itime = 0; 782 } 783 return (1); 784} 785 786/* 787 * Convert seconds to ASCII times. 788 */ 789char * 790cvtstoa(secs) 791 time_t secs; 792{ 793 static char buf[20]; 794 795 if (secs % (24 * 60 * 60) == 0) { 796 secs /= 24 * 60 * 60; 797 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s"); 798 } else if (secs % (60 * 60) == 0) { 799 secs /= 60 * 60; 800 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s"); 801 } else if (secs % 60 == 0) { 802 secs /= 60; 803 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s"); 804 } else 805 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s"); 806 return (buf); 807} 808 809/* 810 * Convert ASCII input times to seconds. 811 */ 812int 813cvtatos(period, units, seconds) 814 time_t period; 815 char *units; 816 time_t *seconds; 817{ 818 819 if (bcmp(units, "second", 6) == 0) 820 *seconds = period; 821 else if (bcmp(units, "minute", 6) == 0) 822 *seconds = period * 60; 823 else if (bcmp(units, "hour", 4) == 0) 824 *seconds = period * 60 * 60; 825 else if (bcmp(units, "day", 3) == 0) 826 *seconds = period * 24 * 60 * 60; 827 else { 828 printf("%s: bad units, specify %s\n", units, 829 "days, hours, minutes, or seconds"); 830 return (0); 831 } 832 return (1); 833} 834 835/* 836 * Free a list of quotause structures. 837 */ 838void 839freeprivs(quplist) 840 struct quotause *quplist; 841{ 842 register struct quotause *qup, *nextqup; 843 844 for (qup = quplist; qup; qup = nextqup) { 845 nextqup = qup->next; 846 free(qup); 847 } 848} 849 850/* 851 * Check whether a string is completely composed of digits. 852 */ 853int 854alldigits(s) 855 register const char *s; 856{ 857 register int c; 858 859 c = *s++; 860 do { 861 if (!isdigit(c)) 862 return (0); 863 } while ((c = *s++)); 864 return (1); 865} 866 867/* 868 * Check to see if a particular quota is to be enabled. 869 */ 870int 871hasquota(fs, type, qfnamep) 872 register struct fstab *fs; 873 int type; 874 char **qfnamep; 875{ 876 register char *opt; 877 char *cp; 878 static char initname, usrname[100], grpname[100]; 879 static char buf[BUFSIZ]; 880 881 if (!initname) { 882 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 883 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 884 initname = 1; 885 } 886 strcpy(buf, fs->fs_mntops); 887 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 888 if ((cp = index(opt, '='))) 889 *cp++ = '\0'; 890 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 891 break; 892 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 893 break; 894 } 895 if (!opt) 896 return (0); 897 if (cp) { 898 *qfnamep = cp; 899 return (1); 900 } 901 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 902 *qfnamep = buf; 903 return (1); 904} 905