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