edquota.c revision 101546
1168054Sflz/* 2168054Sflz * Copyright (c) 1980, 1990, 1993 3168266Sgabor * The Regents of the University of California. All rights reserved. 4168266Sgabor * 5168266Sgabor * This code is derived from software contributed to Berkeley by 6168266Sgabor * Robert Elz at The University of Melbourne. 7168266Sgabor * 8168266Sgabor * Redistribution and use in source and binary forms, with or without 9168266Sgabor * modification, are permitted provided that the following conditions 10168266Sgabor * are met: 11168054Sflz * 1. Redistributions of source code must retain the above copyright 12168054Sflz * notice, this list of conditions and the following disclaimer. 13168064Sflz * 2. Redistributions in binary form must reproduce the above copyright 14168064Sflz * notice, this list of conditions and the following disclaimer in the 15168064Sflz * documentation and/or other materials provided with the distribution. 16168064Sflz * 3. All advertising materials mentioning features or use of this software 17168064Sflz * must display the following acknowledgement: 18168064Sflz * This product includes software developed by the University of 19168064Sflz * California, Berkeley and its contributors. 20168064Sflz * 4. Neither the name of the University nor the names of its contributors 21168064Sflz * may be used to endorse or promote products derived from this software 22168064Sflz * without specific prior written permission. 23168064Sflz * 24168064Sflz * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25168064Sflz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26168064Sflz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27168054Sflz * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28168054Sflz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29168064Sflz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30168054Sflz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31168064Sflz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32168939Stmclaugh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33168131Sbmah * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34168113Smarcus * SUCH DAMAGE. 35168123Snetchild */ 36168939Stmclaugh 37168064Sflz#ifndef lint 38168054Sflzstatic const char copyright[] = 39168054Sflz"@(#) Copyright (c) 1980, 1990, 1993\n\ 40168054Sflz The Regents of the University of California. All rights reserved.\n"; 41168054Sflz#endif /* not lint */ 42168261Sache 43168077Sflz#ifndef lint 44168077Sflz#if 0 45168126Salestatic char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93"; 46168069Sgarga#endif 47168472Snovelstatic const char rcsid[] = 48168274Ssem "$FreeBSD: head/usr.sbin/edquota/edquota.c 101546 2002-08-09 01:21:03Z iedowse $"; 49168667Sstefan#endif /* not lint */ 50168274Ssem 51168113Smarcus/* 52168098Skrion * Disk quota editor. 53168123Snetchild */ 54168082Sgarga#include <sys/param.h> 55168116Sclsung#include <sys/stat.h> 56168937Scperciva#include <sys/file.h> 57168177Sgabor#include <sys/wait.h> 58168354Sdanfe#include <ufs/ufs/quota.h> 59168072Sehaupt#include <ctype.h> 60168108Srafan#include <err.h> 61168186Smat#include <errno.h> 62168210Sitetcu#include <fstab.h> 63168068Serwin#include <grp.h> 64168072Sehaupt#include <pwd.h> 65168113Smarcus#include <signal.h> 66168059Sgabor#include <stdio.h> 67168542Smiwi#include <stdlib.h> 68168098Skrion#include <string.h> 69168054Sflz#include <unistd.h> 70168059Sgabor#include "pathnames.h" 71168054Sflz 72168256Sijliaoconst char *qfname = QUOTAFILENAME; 73168209Sitetcuconst char *qfextension[] = INITQFNAMES; 74168076Sjmeloconst char *quotagroup = QUOTAGROUP; 75168123Snetchildchar tmpfil[] = _PATH_TMP; 76168054Sflz 77168055Spavstruct quotause { 78168055Spav struct quotause *next; 79168536Skevlo long flags; 80168177Sgabor struct dqblk dqblk; 81168098Skrion char fsname[MAXPATHLEN + 1]; 82168055Spav char qfname[1]; /* actually longer */ 83168161Sphilip}; 84168054Sflz#define FOUND 0x01 85168208Sitetcu 86168295Sleeymint alldigits(const char *s); 87168295Sleeymint cvtatos(time_t, char *, time_t *); 88168939Stmclaughchar *cvtstoa(time_t); 89168068Serwinint editit(char *); 90168337Slwhsuvoid freeprivs(struct quotause *); 91168177Sgaborint getentry(const char *, int); 92168113Smarcusstruct quotause *getprivs(long, int, char *); 93168918Sbruefferint hasquota(struct fstab *, int, char **); 94168186Smatvoid putprivs(long, int, struct quotause *); 95168055Spavint readprivs(struct quotause *, char *); 96168098Skrionint readtimes(struct quotause *, char *); 97168359Smmstatic void usage(void); 98168100Smnagint writetimes(struct quotause *, int, int); 99168123Snetchildint writeprivs(struct quotause *, int, char *, int); 100168177Sgabor 101168134Snorkint 102168084Sehauptmain(int argc, char **argv) 103168542Smiwi{ 104168939Stmclaugh register struct quotause *qup, *protoprivs, *curprivs; 105168054Sflz register long id, protoid; 106168098Skrion register int quotatype, tmpfd; 107168108Srafan register uid_t startuid, enduid; 108168098Skrion char *protoname, *cp, ch; 109168098Skrion int tflag = 0, pflag = 0; 110168098Skrion char *fspath = NULL; 111168055Spav char buf[30]; 112168068Serwin 113168939Stmclaugh if (argc < 2) 114168290Ssem usage(); 115168667Sstefan if (getuid()) 116168125Stdb errx(1, "permission denied"); 117169018Strasz quotatype = USRQUOTA; 118168225Strhodes while ((ch = getopt(argc, argv, "ugtf:p:")) != -1) { 119168186Smat switch(ch) { 120168061Sahze case 'f': 121168069Sgarga fspath = optarg; 122168935Stmclaugh break; 123168054Sflz case 'p': 124168054Sflz protoname = optarg; 125168064Sflz pflag++; 126168064Sflz break; 127168054Sflz case 'g': 128168055Spav quotatype = GRPQUOTA; 129168055Spav break; 130168055Spav case 'u': 131168055Spav quotatype = USRQUOTA; 132168055Spav break; 133168055Spav case 't': 134168057Sahze tflag++; 135168055Spav break; 136168919Sbrueffer default: 137168667Sstefan usage(); 138168667Sstefan } 139168939Stmclaugh } 140168939Stmclaugh argc -= optind; 141168939Stmclaugh argv += optind; 142168125Stdb if (pflag) { 143168208Sitetcu if ((protoid = getentry(protoname, quotatype)) == -1) 144168125Stdb exit(1); 145168337Slwhsu protoprivs = getprivs(protoid, quotatype, fspath); 146168337Slwhsu for (qup = protoprivs; qup; qup = qup->next) { 147168108Srafan qup->dqblk.dqb_btime = 0; 148168108Srafan qup->dqblk.dqb_itime = 0; 149168186Smat } 150168186Smat for (; argc-- > 0; argv++) { 151168938Scperciva if (strspn(*argv, "0123456789-") == strlen(*argv) && 152168068Serwin (cp = strchr(*argv, '-')) != NULL) { 153168068Serwin *cp++ = '\0'; 154168072Sehaupt startuid = atoi(*argv); 155168072Sehaupt enduid = atoi(cp); 156168274Ssem if (enduid < startuid) 157168225Strhodes errx(1, 158168225Strhodes "ending uid (%d) must be >= starting uid (%d) when using uid ranges", 159168068Serwin enduid, startuid); 160168059Sgabor for ( ; startuid <= enduid; startuid++) { 161168068Serwin snprintf(buf, sizeof(buf), "%d", 162168068Serwin startuid); 163168068Serwin if ((id = getentry(buf, quotatype)) < 0) 164168059Sgabor continue; 165168354Sdanfe putprivs(id, quotatype, protoprivs); 166168098Skrion } 167168098Skrion continue; 168168054Sflz } 169168054Sflz if ((id = getentry(*argv, quotatype)) < 0) 170168054Sflz continue; 171168054Sflz putprivs(id, quotatype, protoprivs); 172168069Sgarga } 173168069Sgarga exit(0); 174168359Smm } 175168069Sgarga tmpfd = mkstemp(tmpfil); 176168935Stmclaugh fchown(tmpfd, getuid(), getgid()); 177168069Sgarga if (tflag) { 178168295Sleeym protoprivs = getprivs(0, quotatype, fspath); 179168295Sleeym if (writetimes(protoprivs, tmpfd, quotatype) == 0) 180168210Sitetcu exit(1); 181168210Sitetcu if (editit(tmpfil) && readtimes(protoprivs, tmpfil)) 182168123Snetchild putprivs(0, quotatype, protoprivs); 183168123Snetchild freeprivs(protoprivs); 184168134Snork close(tmpfd); 185168134Snork unlink(tmpfil); 186168134Snork exit(0); 187168134Snork } 188168134Snork for ( ; argc > 0; argc--, argv++) { 189168098Skrion if ((id = getentry(*argv, quotatype)) == -1) 190168098Skrion continue; 191168098Skrion curprivs = getprivs(id, quotatype, fspath); 192168098Skrion if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0) 193168098Skrion continue; 194168098Skrion if (editit(tmpfil) && readprivs(curprivs, tmpfil)) 195168098Skrion putprivs(id, quotatype, curprivs); 196168098Skrion freeprivs(curprivs); 197168209Sitetcu } 198168209Sitetcu close(tmpfd); 199168295Sleeym unlink(tmpfil); 200168295Sleeym exit(0); 201168939Stmclaugh} 202168939Stmclaugh 203168687Sahzestatic void 204168113Smarcususage() 205168113Smarcus{ 206168113Smarcus fprintf(stderr, "%s\n%s\n%s\n%s\n", 207168113Smarcus "usage: edquota [-u] [-f fspath] [-p username] username ...", 208168186Smat " edquota -g [-f fspath] [-p groupname] groupname ...", 209168186Smat " edquota [-u] -t [-f fspath]", 210168936Stmclaugh " edquota -g -t [-f fspath]"); 211168936Stmclaugh exit(1); 212168542Smiwi} 213168542Smiwi 214169018Strasz/* 215168542Smiwi * This routine converts a name for a particular quota type to 216168076Sjmelo * an identifier. This routine must agree with the kernel routine 217168076Sjmelo * getinoquota as to the interpretation of quota types. 218168123Snetchild */ 219168123Snetchildint 220168126Salegetentry(name, quotatype) 221168126Sale const char *name; 222168472Snovel int quotatype; 223168084Sehaupt{ 224168084Sehaupt struct passwd *pw; 225168939Stmclaugh struct group *gr; 226168939Stmclaugh 227168687Sahze if (alldigits(name)) 228168054Sflz return (atoi(name)); 229168055Spav switch(quotatype) { 230168055Spav case USRQUOTA: 231168055Spav if ((pw = getpwnam(name))) 232168054Sflz return (pw->pw_uid); 233168161Sphilip warnx("%s: no such user", name); 234168161Sphilip break; 235168274Ssem case GRPQUOTA: 236168274Ssem if ((gr = getgrnam(name))) 237168108Srafan return (gr->gr_gid); 238168274Ssem warnx("%s: no such group", name); 239168108Srafan break; 240168939Stmclaugh default: 241168939Stmclaugh warnx("%d: unknown quota type", quotatype); 242168123Snetchild break; 243168123Snetchild } 244168209Sitetcu sleep(1); 245168935Stmclaugh return (-1); 246168209Sitetcu} 247168939Stmclaugh 248168939Stmclaugh/* 249168054Sflz * Collect the requested quota information. 250 */ 251struct quotause * 252getprivs(id, quotatype, fspath) 253 register long id; 254 int quotatype; 255 char *fspath; 256{ 257 register struct fstab *fs; 258 register struct quotause *qup, *quptail; 259 struct quotause *quphead; 260 int qcmd, qupsize, fd; 261 char *qfpathname; 262 static int warned = 0; 263 264 setfsent(); 265 quphead = (struct quotause *)0; 266 qcmd = QCMD(Q_GETQUOTA, quotatype); 267 while ((fs = getfsent())) { 268 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) && 269 strcmp(fspath, fs->fs_file)) 270 continue; 271 if (strcmp(fs->fs_vfstype, "ufs")) 272 continue; 273 if (!hasquota(fs, quotatype, &qfpathname)) 274 continue; 275 qupsize = sizeof(*qup) + strlen(qfpathname); 276 if ((qup = (struct quotause *)malloc(qupsize)) == NULL) 277 errx(2, "out of memory"); 278 if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) { 279 if (errno == EOPNOTSUPP && !warned) { 280 warned++; 281 warnx("warning: quotas are not compiled into this kernel"); 282 sleep(3); 283 } 284 if ((fd = open(qfpathname, O_RDONLY)) < 0) { 285 fd = open(qfpathname, O_RDWR|O_CREAT, 0640); 286 if (fd < 0 && errno != ENOENT) { 287 warn("%s", qfpathname); 288 free(qup); 289 continue; 290 } 291 warnx("creating quota file %s", qfpathname); 292 sleep(3); 293 (void) fchown(fd, getuid(), 294 getentry(quotagroup, GRPQUOTA)); 295 (void) fchmod(fd, 0640); 296 } 297 lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET); 298 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) { 299 case 0: /* EOF */ 300 /* 301 * Convert implicit 0 quota (EOF) 302 * into an explicit one (zero'ed dqblk) 303 */ 304 bzero((caddr_t)&qup->dqblk, 305 sizeof(struct dqblk)); 306 break; 307 308 case sizeof(struct dqblk): /* OK */ 309 break; 310 311 default: /* ERROR */ 312 warn("read error in %s", qfpathname); 313 close(fd); 314 free(qup); 315 continue; 316 } 317 close(fd); 318 } 319 strcpy(qup->qfname, qfpathname); 320 strcpy(qup->fsname, fs->fs_file); 321 if (quphead == NULL) 322 quphead = qup; 323 else 324 quptail->next = qup; 325 quptail = qup; 326 qup->next = 0; 327 } 328 endfsent(); 329 return (quphead); 330} 331 332/* 333 * Store the requested quota information. 334 */ 335void 336putprivs(id, quotatype, quplist) 337 long id; 338 int quotatype; 339 struct quotause *quplist; 340{ 341 register struct quotause *qup; 342 int qcmd, fd; 343 344 qcmd = QCMD(Q_SETQUOTA, quotatype); 345 for (qup = quplist; qup; qup = qup->next) { 346 if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0) 347 continue; 348 if ((fd = open(qup->qfname, O_WRONLY)) < 0) { 349 warn("%s", qup->qfname); 350 } else { 351 lseek(fd, (long)id * (long)sizeof (struct dqblk), 0); 352 if (write(fd, &qup->dqblk, sizeof (struct dqblk)) != 353 sizeof (struct dqblk)) { 354 warn("%s", qup->qfname); 355 } 356 close(fd); 357 } 358 } 359} 360 361/* 362 * Take a list of priviledges and get it edited. 363 */ 364int 365editit(tmpf) 366 char *tmpf; 367{ 368 long omask; 369 int pid, status; 370 371 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 372 top: 373 if ((pid = fork()) < 0) { 374 375 if (errno == EPROCLIM) { 376 warnx("you have too many processes"); 377 return(0); 378 } 379 if (errno == EAGAIN) { 380 sleep(1); 381 goto top; 382 } 383 warn("fork"); 384 return (0); 385 } 386 if (pid == 0) { 387 register const char *ed; 388 389 sigsetmask(omask); 390 setgid(getgid()); 391 setuid(getuid()); 392 if ((ed = getenv("EDITOR")) == (char *)0) 393 ed = _PATH_VI; 394 execlp(ed, ed, tmpf, (char *)0); 395 err(1, "%s", ed); 396 } 397 waitpid(pid, &status, 0); 398 sigsetmask(omask); 399 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) 400 return (0); 401 return (1); 402} 403 404/* 405 * Convert a quotause list to an ASCII file. 406 */ 407int 408writeprivs(quplist, outfd, name, quotatype) 409 struct quotause *quplist; 410 int outfd; 411 char *name; 412 int quotatype; 413{ 414 register struct quotause *qup; 415 FILE *fd; 416 417 ftruncate(outfd, 0); 418 lseek(outfd, 0, L_SET); 419 if ((fd = fdopen(dup(outfd), "w")) == NULL) 420 err(1, "%s", tmpfil); 421 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name); 422 for (qup = quplist; qup; qup = qup->next) { 423 fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n", 424 qup->fsname, "blocks in use:", 425 (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024), 426 (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024), 427 (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024)); 428 fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n", 429 "\tinodes in use:", 430 (unsigned long)qup->dqblk.dqb_curinodes, 431 (unsigned long)qup->dqblk.dqb_isoftlimit, 432 (unsigned long)qup->dqblk.dqb_ihardlimit); 433 } 434 fclose(fd); 435 return (1); 436} 437 438/* 439 * Merge changes to an ASCII file into a quotause list. 440 */ 441int 442readprivs(quplist, inname) 443 struct quotause *quplist; 444 char *inname; 445{ 446 register struct quotause *qup; 447 FILE *fd; 448 unsigned long bhardlimit, bsoftlimit, curblocks; 449 unsigned long ihardlimit, isoftlimit, curinodes; 450 int cnt; 451 register char *cp; 452 struct dqblk dqblk; 453 char *fsp, line1[BUFSIZ], line2[BUFSIZ]; 454 455 fd = fopen(inname, "r"); 456 if (fd == NULL) { 457 warnx("can't re-read temp file!!"); 458 return (0); 459 } 460 /* 461 * Discard title line, then read pairs of lines to process. 462 */ 463 (void) fgets(line1, sizeof (line1), fd); 464 while (fgets(line1, sizeof (line1), fd) != NULL && 465 fgets(line2, sizeof (line2), fd) != NULL) { 466 if ((fsp = strtok(line1, " \t:")) == NULL) { 467 warnx("%s: bad format", line1); 468 return (0); 469 } 470 if ((cp = strtok((char *)0, "\n")) == NULL) { 471 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]); 472 return (0); 473 } 474 cnt = sscanf(cp, 475 " blocks in use: %lu, limits (soft = %lu, hard = %lu)", 476 &curblocks, &bsoftlimit, &bhardlimit); 477 if (cnt != 3) { 478 warnx("%s:%s: bad format", fsp, cp); 479 return (0); 480 } 481 dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024); 482 dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024); 483 dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024); 484 if ((cp = strtok(line2, "\n")) == NULL) { 485 warnx("%s: %s: bad format", fsp, line2); 486 return (0); 487 } 488 cnt = sscanf(cp, 489 "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)", 490 &curinodes, &isoftlimit, &ihardlimit); 491 if (cnt != 3) { 492 warnx("%s: %s: bad format", fsp, line2); 493 return (0); 494 } 495 dqblk.dqb_curinodes = curinodes; 496 dqblk.dqb_isoftlimit = isoftlimit; 497 dqblk.dqb_ihardlimit = ihardlimit; 498 for (qup = quplist; qup; qup = qup->next) { 499 if (strcmp(fsp, qup->fsname)) 500 continue; 501 /* 502 * Cause time limit to be reset when the quota 503 * is next used if previously had no soft limit 504 * or were under it, but now have a soft limit 505 * and are over it. 506 */ 507 if (dqblk.dqb_bsoftlimit && 508 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit && 509 (qup->dqblk.dqb_bsoftlimit == 0 || 510 qup->dqblk.dqb_curblocks < 511 qup->dqblk.dqb_bsoftlimit)) 512 qup->dqblk.dqb_btime = 0; 513 if (dqblk.dqb_isoftlimit && 514 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit && 515 (qup->dqblk.dqb_isoftlimit == 0 || 516 qup->dqblk.dqb_curinodes < 517 qup->dqblk.dqb_isoftlimit)) 518 qup->dqblk.dqb_itime = 0; 519 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit; 520 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit; 521 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit; 522 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit; 523 qup->flags |= FOUND; 524 if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks && 525 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes) 526 break; 527 warnx("%s: cannot change current allocation", fsp); 528 break; 529 } 530 } 531 fclose(fd); 532 /* 533 * Disable quotas for any filesystems that have not been found. 534 */ 535 for (qup = quplist; qup; qup = qup->next) { 536 if (qup->flags & FOUND) { 537 qup->flags &= ~FOUND; 538 continue; 539 } 540 qup->dqblk.dqb_bsoftlimit = 0; 541 qup->dqblk.dqb_bhardlimit = 0; 542 qup->dqblk.dqb_isoftlimit = 0; 543 qup->dqblk.dqb_ihardlimit = 0; 544 } 545 return (1); 546} 547 548/* 549 * Convert a quotause list to an ASCII file of grace times. 550 */ 551int 552writetimes(quplist, outfd, quotatype) 553 struct quotause *quplist; 554 int outfd; 555 int quotatype; 556{ 557 register struct quotause *qup; 558 FILE *fd; 559 560 ftruncate(outfd, 0); 561 lseek(outfd, 0, L_SET); 562 if ((fd = fdopen(dup(outfd), "w")) == NULL) 563 err(1, "%s", tmpfil); 564 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n"); 565 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n", 566 qfextension[quotatype]); 567 for (qup = quplist; qup; qup = qup->next) { 568 fprintf(fd, "%s: block grace period: %s, ", 569 qup->fsname, cvtstoa(qup->dqblk.dqb_btime)); 570 fprintf(fd, "file grace period: %s\n", 571 cvtstoa(qup->dqblk.dqb_itime)); 572 } 573 fclose(fd); 574 return (1); 575} 576 577/* 578 * Merge changes of grace times in an ASCII file into a quotause list. 579 */ 580int 581readtimes(quplist, inname) 582 struct quotause *quplist; 583 char *inname; 584{ 585 register struct quotause *qup; 586 FILE *fd; 587 int cnt; 588 register char *cp; 589 time_t itime, btime, iseconds, bseconds; 590 long l_itime, l_btime; 591 char *fsp, bunits[10], iunits[10], line1[BUFSIZ]; 592 593 fd = fopen(inname, "r"); 594 if (fd == NULL) { 595 warnx("can't re-read temp file!!"); 596 return (0); 597 } 598 /* 599 * Discard two title lines, then read lines to process. 600 */ 601 (void) fgets(line1, sizeof (line1), fd); 602 (void) fgets(line1, sizeof (line1), fd); 603 while (fgets(line1, sizeof (line1), fd) != NULL) { 604 if ((fsp = strtok(line1, " \t:")) == NULL) { 605 warnx("%s: bad format", line1); 606 return (0); 607 } 608 if ((cp = strtok((char *)0, "\n")) == NULL) { 609 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]); 610 return (0); 611 } 612 cnt = sscanf(cp, 613 " block grace period: %ld %s file grace period: %ld %s", 614 &l_btime, bunits, &l_itime, iunits); 615 if (cnt != 4) { 616 warnx("%s:%s: bad format", fsp, cp); 617 return (0); 618 } 619 btime = l_btime; 620 itime = l_itime; 621 if (cvtatos(btime, bunits, &bseconds) == 0) 622 return (0); 623 if (cvtatos(itime, iunits, &iseconds) == 0) 624 return (0); 625 for (qup = quplist; qup; qup = qup->next) { 626 if (strcmp(fsp, qup->fsname)) 627 continue; 628 qup->dqblk.dqb_btime = bseconds; 629 qup->dqblk.dqb_itime = iseconds; 630 qup->flags |= FOUND; 631 break; 632 } 633 } 634 fclose(fd); 635 /* 636 * reset default grace periods for any filesystems 637 * that have not been found. 638 */ 639 for (qup = quplist; qup; qup = qup->next) { 640 if (qup->flags & FOUND) { 641 qup->flags &= ~FOUND; 642 continue; 643 } 644 qup->dqblk.dqb_btime = 0; 645 qup->dqblk.dqb_itime = 0; 646 } 647 return (1); 648} 649 650/* 651 * Convert seconds to ASCII times. 652 */ 653char * 654cvtstoa(secs) 655 time_t secs; 656{ 657 static char buf[20]; 658 659 if (secs % (24 * 60 * 60) == 0) { 660 secs /= 24 * 60 * 60; 661 sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s"); 662 } else if (secs % (60 * 60) == 0) { 663 secs /= 60 * 60; 664 sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s"); 665 } else if (secs % 60 == 0) { 666 secs /= 60; 667 sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s"); 668 } else 669 sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s"); 670 return (buf); 671} 672 673/* 674 * Convert ASCII input times to seconds. 675 */ 676int 677cvtatos(period, units, seconds) 678 time_t period; 679 char *units; 680 time_t *seconds; 681{ 682 683 if (bcmp(units, "second", 6) == 0) 684 *seconds = period; 685 else if (bcmp(units, "minute", 6) == 0) 686 *seconds = period * 60; 687 else if (bcmp(units, "hour", 4) == 0) 688 *seconds = period * 60 * 60; 689 else if (bcmp(units, "day", 3) == 0) 690 *seconds = period * 24 * 60 * 60; 691 else { 692 printf("%s: bad units, specify %s\n", units, 693 "days, hours, minutes, or seconds"); 694 return (0); 695 } 696 return (1); 697} 698 699/* 700 * Free a list of quotause structures. 701 */ 702void 703freeprivs(quplist) 704 struct quotause *quplist; 705{ 706 register struct quotause *qup, *nextqup; 707 708 for (qup = quplist; qup; qup = nextqup) { 709 nextqup = qup->next; 710 free(qup); 711 } 712} 713 714/* 715 * Check whether a string is completely composed of digits. 716 */ 717int 718alldigits(s) 719 register const char *s; 720{ 721 register int c; 722 723 c = *s++; 724 do { 725 if (!isdigit(c)) 726 return (0); 727 } while ((c = *s++)); 728 return (1); 729} 730 731/* 732 * Check to see if a particular quota is to be enabled. 733 */ 734int 735hasquota(fs, type, qfnamep) 736 register struct fstab *fs; 737 int type; 738 char **qfnamep; 739{ 740 register char *opt; 741 char *cp; 742 static char initname, usrname[100], grpname[100]; 743 static char buf[BUFSIZ]; 744 745 if (!initname) { 746 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 747 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 748 initname = 1; 749 } 750 strcpy(buf, fs->fs_mntops); 751 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 752 if ((cp = index(opt, '='))) 753 *cp++ = '\0'; 754 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 755 break; 756 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 757 break; 758 } 759 if (!opt) 760 return (0); 761 if (cp) { 762 *qfnamep = cp; 763 return (1); 764 } 765 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 766 *qfnamep = buf; 767 return (1); 768} 769