edquota.c revision 1553
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1980, 1990, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * This code is derived from software contributed to Berkeley by
61553Srgrimes * Robert Elz at The University of Melbourne.
71553Srgrimes *
81553Srgrimes * Redistribution and use in source and binary forms, with or without
91553Srgrimes * modification, are permitted provided that the following conditions
101553Srgrimes * are met:
111553Srgrimes * 1. Redistributions of source code must retain the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer.
131553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141553Srgrimes *    notice, this list of conditions and the following disclaimer in the
151553Srgrimes *    documentation and/or other materials provided with the distribution.
161553Srgrimes * 3. All advertising materials mentioning features or use of this software
171553Srgrimes *    must display the following acknowledgement:
181553Srgrimes *	This product includes software developed by the University of
191553Srgrimes *	California, Berkeley and its contributors.
201553Srgrimes * 4. Neither the name of the University nor the names of its contributors
211553Srgrimes *    may be used to endorse or promote products derived from this software
221553Srgrimes *    without specific prior written permission.
231553Srgrimes *
241553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341553Srgrimes * SUCH DAMAGE.
351553Srgrimes */
361553Srgrimes
371553Srgrimes#ifndef lint
381553Srgrimesstatic char copyright[] =
391553Srgrimes"@(#) Copyright (c) 1980, 1990, 1993\n\
401553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411553Srgrimes#endif /* not lint */
421553Srgrimes
431553Srgrimes#ifndef lint
441553Srgrimesstatic char sccsid[] = "@(#)edquota.c	8.1 (Berkeley) 6/6/93";
451553Srgrimes#endif /* not lint */
461553Srgrimes
471553Srgrimes/*
481553Srgrimes * Disk quota editor.
491553Srgrimes */
501553Srgrimes#include <sys/param.h>
511553Srgrimes#include <sys/stat.h>
521553Srgrimes#include <sys/file.h>
531553Srgrimes#include <sys/wait.h>
541553Srgrimes#include <ufs/ufs/quota.h>
551553Srgrimes#include <errno.h>
561553Srgrimes#include <fstab.h>
571553Srgrimes#include <pwd.h>
581553Srgrimes#include <grp.h>
591553Srgrimes#include <ctype.h>
601553Srgrimes#include <stdio.h>
611553Srgrimes#include <string.h>
621553Srgrimes#include <unistd.h>
631553Srgrimes#include "pathnames.h"
641553Srgrimes
651553Srgrimeschar *qfname = QUOTAFILENAME;
661553Srgrimeschar *qfextension[] = INITQFNAMES;
671553Srgrimeschar *quotagroup = QUOTAGROUP;
681553Srgrimeschar tmpfil[] = _PATH_TMP;
691553Srgrimes
701553Srgrimesstruct quotause {
711553Srgrimes	struct	quotause *next;
721553Srgrimes	long	flags;
731553Srgrimes	struct	dqblk dqblk;
741553Srgrimes	char	fsname[MAXPATHLEN + 1];
751553Srgrimes	char	qfname[1];	/* actually longer */
761553Srgrimes} *getprivs();
771553Srgrimes#define	FOUND	0x01
781553Srgrimes
791553Srgrimesmain(argc, argv)
801553Srgrimes	register char **argv;
811553Srgrimes	int argc;
821553Srgrimes{
831553Srgrimes	register struct quotause *qup, *protoprivs, *curprivs;
841553Srgrimes	extern char *optarg;
851553Srgrimes	extern int optind;
861553Srgrimes	register long id, protoid;
871553Srgrimes	register int quotatype, tmpfd;
881553Srgrimes	char *protoname, ch;
891553Srgrimes	int tflag = 0, pflag = 0;
901553Srgrimes
911553Srgrimes	if (argc < 2)
921553Srgrimes		usage();
931553Srgrimes	if (getuid()) {
941553Srgrimes		fprintf(stderr, "edquota: permission denied\n");
951553Srgrimes		exit(1);
961553Srgrimes	}
971553Srgrimes	quotatype = USRQUOTA;
981553Srgrimes	while ((ch = getopt(argc, argv, "ugtp:")) != EOF) {
991553Srgrimes		switch(ch) {
1001553Srgrimes		case 'p':
1011553Srgrimes			protoname = optarg;
1021553Srgrimes			pflag++;
1031553Srgrimes			break;
1041553Srgrimes		case 'g':
1051553Srgrimes			quotatype = GRPQUOTA;
1061553Srgrimes			break;
1071553Srgrimes		case 'u':
1081553Srgrimes			quotatype = USRQUOTA;
1091553Srgrimes			break;
1101553Srgrimes		case 't':
1111553Srgrimes			tflag++;
1121553Srgrimes			break;
1131553Srgrimes		default:
1141553Srgrimes			usage();
1151553Srgrimes		}
1161553Srgrimes	}
1171553Srgrimes	argc -= optind;
1181553Srgrimes	argv += optind;
1191553Srgrimes	if (pflag) {
1201553Srgrimes		if ((protoid = getentry(protoname, quotatype)) == -1)
1211553Srgrimes			exit(1);
1221553Srgrimes		protoprivs = getprivs(protoid, quotatype);
1231553Srgrimes		for (qup = protoprivs; qup; qup = qup->next) {
1241553Srgrimes			qup->dqblk.dqb_btime = 0;
1251553Srgrimes			qup->dqblk.dqb_itime = 0;
1261553Srgrimes		}
1271553Srgrimes		while (argc-- > 0) {
1281553Srgrimes			if ((id = getentry(*argv++, quotatype)) < 0)
1291553Srgrimes				continue;
1301553Srgrimes			putprivs(id, quotatype, protoprivs);
1311553Srgrimes		}
1321553Srgrimes		exit(0);
1331553Srgrimes	}
1341553Srgrimes	tmpfd = mkstemp(tmpfil);
1351553Srgrimes	fchown(tmpfd, getuid(), getgid());
1361553Srgrimes	if (tflag) {
1371553Srgrimes		protoprivs = getprivs(0, quotatype);
1381553Srgrimes		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
1391553Srgrimes			exit(1);
1401553Srgrimes		if (editit(tmpfil) && readtimes(protoprivs, tmpfd))
1411553Srgrimes			putprivs(0, quotatype, protoprivs);
1421553Srgrimes		freeprivs(protoprivs);
1431553Srgrimes		exit(0);
1441553Srgrimes	}
1451553Srgrimes	for ( ; argc > 0; argc--, argv++) {
1461553Srgrimes		if ((id = getentry(*argv, quotatype)) == -1)
1471553Srgrimes			continue;
1481553Srgrimes		curprivs = getprivs(id, quotatype);
1491553Srgrimes		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
1501553Srgrimes			continue;
1511553Srgrimes		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
1521553Srgrimes			putprivs(id, quotatype, curprivs);
1531553Srgrimes		freeprivs(curprivs);
1541553Srgrimes	}
1551553Srgrimes	close(tmpfd);
1561553Srgrimes	unlink(tmpfil);
1571553Srgrimes	exit(0);
1581553Srgrimes}
1591553Srgrimes
1601553Srgrimesusage()
1611553Srgrimes{
1621553Srgrimes	fprintf(stderr, "%s%s%s%s",
1631553Srgrimes		"Usage: edquota [-u] [-p username] username ...\n",
1641553Srgrimes		"\tedquota -g [-p groupname] groupname ...\n",
1651553Srgrimes		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
1661553Srgrimes	exit(1);
1671553Srgrimes}
1681553Srgrimes
1691553Srgrimes/*
1701553Srgrimes * This routine converts a name for a particular quota type to
1711553Srgrimes * an identifier. This routine must agree with the kernel routine
1721553Srgrimes * getinoquota as to the interpretation of quota types.
1731553Srgrimes */
1741553Srgrimesgetentry(name, quotatype)
1751553Srgrimes	char *name;
1761553Srgrimes	int quotatype;
1771553Srgrimes{
1781553Srgrimes	struct passwd *pw;
1791553Srgrimes	struct group *gr;
1801553Srgrimes
1811553Srgrimes	if (alldigits(name))
1821553Srgrimes		return (atoi(name));
1831553Srgrimes	switch(quotatype) {
1841553Srgrimes	case USRQUOTA:
1851553Srgrimes		if (pw = getpwnam(name))
1861553Srgrimes			return (pw->pw_uid);
1871553Srgrimes		fprintf(stderr, "%s: no such user\n", name);
1881553Srgrimes		break;
1891553Srgrimes	case GRPQUOTA:
1901553Srgrimes		if (gr = getgrnam(name))
1911553Srgrimes			return (gr->gr_gid);
1921553Srgrimes		fprintf(stderr, "%s: no such group\n", name);
1931553Srgrimes		break;
1941553Srgrimes	default:
1951553Srgrimes		fprintf(stderr, "%d: unknown quota type\n", quotatype);
1961553Srgrimes		break;
1971553Srgrimes	}
1981553Srgrimes	sleep(1);
1991553Srgrimes	return (-1);
2001553Srgrimes}
2011553Srgrimes
2021553Srgrimes/*
2031553Srgrimes * Collect the requested quota information.
2041553Srgrimes */
2051553Srgrimesstruct quotause *
2061553Srgrimesgetprivs(id, quotatype)
2071553Srgrimes	register long id;
2081553Srgrimes	int quotatype;
2091553Srgrimes{
2101553Srgrimes	register struct fstab *fs;
2111553Srgrimes	register struct quotause *qup, *quptail;
2121553Srgrimes	struct quotause *quphead;
2131553Srgrimes	int qcmd, qupsize, fd;
2141553Srgrimes	char *qfpathname;
2151553Srgrimes	static int warned = 0;
2161553Srgrimes	extern int errno;
2171553Srgrimes
2181553Srgrimes	setfsent();
2191553Srgrimes	quphead = (struct quotause *)0;
2201553Srgrimes	qcmd = QCMD(Q_GETQUOTA, quotatype);
2211553Srgrimes	while (fs = getfsent()) {
2221553Srgrimes		if (strcmp(fs->fs_vfstype, "ufs"))
2231553Srgrimes			continue;
2241553Srgrimes		if (!hasquota(fs, quotatype, &qfpathname))
2251553Srgrimes			continue;
2261553Srgrimes		qupsize = sizeof(*qup) + strlen(qfpathname);
2271553Srgrimes		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
2281553Srgrimes			fprintf(stderr, "edquota: out of memory\n");
2291553Srgrimes			exit(2);
2301553Srgrimes		}
2311553Srgrimes		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
2321553Srgrimes	    		if (errno == EOPNOTSUPP && !warned) {
2331553Srgrimes				warned++;
2341553Srgrimes				fprintf(stderr, "Warning: %s\n",
2351553Srgrimes				    "Quotas are not compiled into this kernel");
2361553Srgrimes				sleep(3);
2371553Srgrimes			}
2381553Srgrimes			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
2391553Srgrimes				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
2401553Srgrimes				if (fd < 0 && errno != ENOENT) {
2411553Srgrimes					perror(qfpathname);
2421553Srgrimes					free(qup);
2431553Srgrimes					continue;
2441553Srgrimes				}
2451553Srgrimes				fprintf(stderr, "Creating quota file %s\n",
2461553Srgrimes				    qfpathname);
2471553Srgrimes				sleep(3);
2481553Srgrimes				(void) fchown(fd, getuid(),
2491553Srgrimes				    getentry(quotagroup, GRPQUOTA));
2501553Srgrimes				(void) fchmod(fd, 0640);
2511553Srgrimes			}
2521553Srgrimes			lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
2531553Srgrimes			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
2541553Srgrimes			case 0:			/* EOF */
2551553Srgrimes				/*
2561553Srgrimes				 * Convert implicit 0 quota (EOF)
2571553Srgrimes				 * into an explicit one (zero'ed dqblk)
2581553Srgrimes				 */
2591553Srgrimes				bzero((caddr_t)&qup->dqblk,
2601553Srgrimes				    sizeof(struct dqblk));
2611553Srgrimes				break;
2621553Srgrimes
2631553Srgrimes			case sizeof(struct dqblk):	/* OK */
2641553Srgrimes				break;
2651553Srgrimes
2661553Srgrimes			default:		/* ERROR */
2671553Srgrimes				fprintf(stderr, "edquota: read error in ");
2681553Srgrimes				perror(qfpathname);
2691553Srgrimes				close(fd);
2701553Srgrimes				free(qup);
2711553Srgrimes				continue;
2721553Srgrimes			}
2731553Srgrimes			close(fd);
2741553Srgrimes		}
2751553Srgrimes		strcpy(qup->qfname, qfpathname);
2761553Srgrimes		strcpy(qup->fsname, fs->fs_file);
2771553Srgrimes		if (quphead == NULL)
2781553Srgrimes			quphead = qup;
2791553Srgrimes		else
2801553Srgrimes			quptail->next = qup;
2811553Srgrimes		quptail = qup;
2821553Srgrimes		qup->next = 0;
2831553Srgrimes	}
2841553Srgrimes	endfsent();
2851553Srgrimes	return (quphead);
2861553Srgrimes}
2871553Srgrimes
2881553Srgrimes/*
2891553Srgrimes * Store the requested quota information.
2901553Srgrimes */
2911553Srgrimesputprivs(id, quotatype, quplist)
2921553Srgrimes	long id;
2931553Srgrimes	int quotatype;
2941553Srgrimes	struct quotause *quplist;
2951553Srgrimes{
2961553Srgrimes	register struct quotause *qup;
2971553Srgrimes	int qcmd, fd;
2981553Srgrimes
2991553Srgrimes	qcmd = QCMD(Q_SETQUOTA, quotatype);
3001553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
3011553Srgrimes		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
3021553Srgrimes			continue;
3031553Srgrimes		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
3041553Srgrimes			perror(qup->qfname);
3051553Srgrimes		} else {
3061553Srgrimes			lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
3071553Srgrimes			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
3081553Srgrimes			    sizeof (struct dqblk)) {
3091553Srgrimes				fprintf(stderr, "edquota: ");
3101553Srgrimes				perror(qup->qfname);
3111553Srgrimes			}
3121553Srgrimes			close(fd);
3131553Srgrimes		}
3141553Srgrimes	}
3151553Srgrimes}
3161553Srgrimes
3171553Srgrimes/*
3181553Srgrimes * Take a list of priviledges and get it edited.
3191553Srgrimes */
3201553Srgrimeseditit(tmpfile)
3211553Srgrimes	char *tmpfile;
3221553Srgrimes{
3231553Srgrimes	long omask;
3241553Srgrimes	int pid, stat;
3251553Srgrimes	extern char *getenv();
3261553Srgrimes
3271553Srgrimes	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
3281553Srgrimes top:
3291553Srgrimes	if ((pid = fork()) < 0) {
3301553Srgrimes		extern errno;
3311553Srgrimes
3321553Srgrimes		if (errno == EPROCLIM) {
3331553Srgrimes			fprintf(stderr, "You have too many processes\n");
3341553Srgrimes			return(0);
3351553Srgrimes		}
3361553Srgrimes		if (errno == EAGAIN) {
3371553Srgrimes			sleep(1);
3381553Srgrimes			goto top;
3391553Srgrimes		}
3401553Srgrimes		perror("fork");
3411553Srgrimes		return (0);
3421553Srgrimes	}
3431553Srgrimes	if (pid == 0) {
3441553Srgrimes		register char *ed;
3451553Srgrimes
3461553Srgrimes		sigsetmask(omask);
3471553Srgrimes		setgid(getgid());
3481553Srgrimes		setuid(getuid());
3491553Srgrimes		if ((ed = getenv("EDITOR")) == (char *)0)
3501553Srgrimes			ed = _PATH_VI;
3511553Srgrimes		execlp(ed, ed, tmpfile, 0);
3521553Srgrimes		perror(ed);
3531553Srgrimes		exit(1);
3541553Srgrimes	}
3551553Srgrimes	waitpid(pid, &stat, 0);
3561553Srgrimes	sigsetmask(omask);
3571553Srgrimes	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
3581553Srgrimes		return (0);
3591553Srgrimes	return (1);
3601553Srgrimes}
3611553Srgrimes
3621553Srgrimes/*
3631553Srgrimes * Convert a quotause list to an ASCII file.
3641553Srgrimes */
3651553Srgrimeswriteprivs(quplist, outfd, name, quotatype)
3661553Srgrimes	struct quotause *quplist;
3671553Srgrimes	int outfd;
3681553Srgrimes	char *name;
3691553Srgrimes	int quotatype;
3701553Srgrimes{
3711553Srgrimes	register struct quotause *qup;
3721553Srgrimes	FILE *fd;
3731553Srgrimes
3741553Srgrimes	ftruncate(outfd, 0);
3751553Srgrimes	lseek(outfd, 0, L_SET);
3761553Srgrimes	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
3771553Srgrimes		fprintf(stderr, "edquota: ");
3781553Srgrimes		perror(tmpfil);
3791553Srgrimes		exit(1);
3801553Srgrimes	}
3811553Srgrimes	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
3821553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
3831553Srgrimes		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
3841553Srgrimes		    qup->fsname, "blocks in use:",
3851553Srgrimes		    dbtob(qup->dqblk.dqb_curblocks) / 1024,
3861553Srgrimes		    dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
3871553Srgrimes		    dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
3881553Srgrimes		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
3891553Srgrimes		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
3901553Srgrimes		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
3911553Srgrimes	}
3921553Srgrimes	fclose(fd);
3931553Srgrimes	return (1);
3941553Srgrimes}
3951553Srgrimes
3961553Srgrimes/*
3971553Srgrimes * Merge changes to an ASCII file into a quotause list.
3981553Srgrimes */
3991553Srgrimesreadprivs(quplist, infd)
4001553Srgrimes	struct quotause *quplist;
4011553Srgrimes	int infd;
4021553Srgrimes{
4031553Srgrimes	register struct quotause *qup;
4041553Srgrimes	FILE *fd;
4051553Srgrimes	int cnt;
4061553Srgrimes	register char *cp;
4071553Srgrimes	struct dqblk dqblk;
4081553Srgrimes	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
4091553Srgrimes
4101553Srgrimes	lseek(infd, 0, L_SET);
4111553Srgrimes	fd = fdopen(dup(infd), "r");
4121553Srgrimes	if (fd == NULL) {
4131553Srgrimes		fprintf(stderr, "Can't re-read temp file!!\n");
4141553Srgrimes		return (0);
4151553Srgrimes	}
4161553Srgrimes	/*
4171553Srgrimes	 * Discard title line, then read pairs of lines to process.
4181553Srgrimes	 */
4191553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
4201553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL &&
4211553Srgrimes	       fgets(line2, sizeof (line2), fd) != NULL) {
4221553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
4231553Srgrimes			fprintf(stderr, "%s: bad format\n", line1);
4241553Srgrimes			return (0);
4251553Srgrimes		}
4261553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
4271553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp,
4281553Srgrimes			    &fsp[strlen(fsp) + 1]);
4291553Srgrimes			return (0);
4301553Srgrimes		}
4311553Srgrimes		cnt = sscanf(cp,
4321553Srgrimes		    " blocks in use: %d, limits (soft = %d, hard = %d)",
4331553Srgrimes		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
4341553Srgrimes		    &dqblk.dqb_bhardlimit);
4351553Srgrimes		if (cnt != 3) {
4361553Srgrimes			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
4371553Srgrimes			return (0);
4381553Srgrimes		}
4391553Srgrimes		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
4401553Srgrimes		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
4411553Srgrimes		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
4421553Srgrimes		if ((cp = strtok(line2, "\n")) == NULL) {
4431553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
4441553Srgrimes			return (0);
4451553Srgrimes		}
4461553Srgrimes		cnt = sscanf(cp,
4471553Srgrimes		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
4481553Srgrimes		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
4491553Srgrimes		    &dqblk.dqb_ihardlimit);
4501553Srgrimes		if (cnt != 3) {
4511553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
4521553Srgrimes			return (0);
4531553Srgrimes		}
4541553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
4551553Srgrimes			if (strcmp(fsp, qup->fsname))
4561553Srgrimes				continue;
4571553Srgrimes			/*
4581553Srgrimes			 * Cause time limit to be reset when the quota
4591553Srgrimes			 * is next used if previously had no soft limit
4601553Srgrimes			 * or were under it, but now have a soft limit
4611553Srgrimes			 * and are over it.
4621553Srgrimes			 */
4631553Srgrimes			if (dqblk.dqb_bsoftlimit &&
4641553Srgrimes			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
4651553Srgrimes			    (qup->dqblk.dqb_bsoftlimit == 0 ||
4661553Srgrimes			     qup->dqblk.dqb_curblocks <
4671553Srgrimes			     qup->dqblk.dqb_bsoftlimit))
4681553Srgrimes				qup->dqblk.dqb_btime = 0;
4691553Srgrimes			if (dqblk.dqb_isoftlimit &&
4701553Srgrimes			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
4711553Srgrimes			    (qup->dqblk.dqb_isoftlimit == 0 ||
4721553Srgrimes			     qup->dqblk.dqb_curinodes <
4731553Srgrimes			     qup->dqblk.dqb_isoftlimit))
4741553Srgrimes				qup->dqblk.dqb_itime = 0;
4751553Srgrimes			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
4761553Srgrimes			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
4771553Srgrimes			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
4781553Srgrimes			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
4791553Srgrimes			qup->flags |= FOUND;
4801553Srgrimes			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
4811553Srgrimes			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
4821553Srgrimes				break;
4831553Srgrimes			fprintf(stderr,
4841553Srgrimes			    "%s: cannot change current allocation\n", fsp);
4851553Srgrimes			break;
4861553Srgrimes		}
4871553Srgrimes	}
4881553Srgrimes	fclose(fd);
4891553Srgrimes	/*
4901553Srgrimes	 * Disable quotas for any filesystems that have not been found.
4911553Srgrimes	 */
4921553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
4931553Srgrimes		if (qup->flags & FOUND) {
4941553Srgrimes			qup->flags &= ~FOUND;
4951553Srgrimes			continue;
4961553Srgrimes		}
4971553Srgrimes		qup->dqblk.dqb_bsoftlimit = 0;
4981553Srgrimes		qup->dqblk.dqb_bhardlimit = 0;
4991553Srgrimes		qup->dqblk.dqb_isoftlimit = 0;
5001553Srgrimes		qup->dqblk.dqb_ihardlimit = 0;
5011553Srgrimes	}
5021553Srgrimes	return (1);
5031553Srgrimes}
5041553Srgrimes
5051553Srgrimes/*
5061553Srgrimes * Convert a quotause list to an ASCII file of grace times.
5071553Srgrimes */
5081553Srgrimeswritetimes(quplist, outfd, quotatype)
5091553Srgrimes	struct quotause *quplist;
5101553Srgrimes	int outfd;
5111553Srgrimes	int quotatype;
5121553Srgrimes{
5131553Srgrimes	register struct quotause *qup;
5141553Srgrimes	char *cvtstoa();
5151553Srgrimes	FILE *fd;
5161553Srgrimes
5171553Srgrimes	ftruncate(outfd, 0);
5181553Srgrimes	lseek(outfd, 0, L_SET);
5191553Srgrimes	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
5201553Srgrimes		fprintf(stderr, "edquota: ");
5211553Srgrimes		perror(tmpfil);
5221553Srgrimes		exit(1);
5231553Srgrimes	}
5241553Srgrimes	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
5251553Srgrimes	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
5261553Srgrimes	    qfextension[quotatype]);
5271553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5281553Srgrimes		fprintf(fd, "%s: block grace period: %s, ",
5291553Srgrimes		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
5301553Srgrimes		fprintf(fd, "file grace period: %s\n",
5311553Srgrimes		    cvtstoa(qup->dqblk.dqb_itime));
5321553Srgrimes	}
5331553Srgrimes	fclose(fd);
5341553Srgrimes	return (1);
5351553Srgrimes}
5361553Srgrimes
5371553Srgrimes/*
5381553Srgrimes * Merge changes of grace times in an ASCII file into a quotause list.
5391553Srgrimes */
5401553Srgrimesreadtimes(quplist, infd)
5411553Srgrimes	struct quotause *quplist;
5421553Srgrimes	int infd;
5431553Srgrimes{
5441553Srgrimes	register struct quotause *qup;
5451553Srgrimes	FILE *fd;
5461553Srgrimes	int cnt;
5471553Srgrimes	register char *cp;
5481553Srgrimes	time_t itime, btime, iseconds, bseconds;
5491553Srgrimes	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
5501553Srgrimes
5511553Srgrimes	lseek(infd, 0, L_SET);
5521553Srgrimes	fd = fdopen(dup(infd), "r");
5531553Srgrimes	if (fd == NULL) {
5541553Srgrimes		fprintf(stderr, "Can't re-read temp file!!\n");
5551553Srgrimes		return (0);
5561553Srgrimes	}
5571553Srgrimes	/*
5581553Srgrimes	 * Discard two title lines, then read lines to process.
5591553Srgrimes	 */
5601553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5611553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5621553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL) {
5631553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
5641553Srgrimes			fprintf(stderr, "%s: bad format\n", line1);
5651553Srgrimes			return (0);
5661553Srgrimes		}
5671553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
5681553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp,
5691553Srgrimes			    &fsp[strlen(fsp) + 1]);
5701553Srgrimes			return (0);
5711553Srgrimes		}
5721553Srgrimes		cnt = sscanf(cp,
5731553Srgrimes		    " block grace period: %d %s file grace period: %d %s",
5741553Srgrimes		    &btime, bunits, &itime, iunits);
5751553Srgrimes		if (cnt != 4) {
5761553Srgrimes			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
5771553Srgrimes			return (0);
5781553Srgrimes		}
5791553Srgrimes		if (cvtatos(btime, bunits, &bseconds) == 0)
5801553Srgrimes			return (0);
5811553Srgrimes		if (cvtatos(itime, iunits, &iseconds) == 0)
5821553Srgrimes			return (0);
5831553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
5841553Srgrimes			if (strcmp(fsp, qup->fsname))
5851553Srgrimes				continue;
5861553Srgrimes			qup->dqblk.dqb_btime = bseconds;
5871553Srgrimes			qup->dqblk.dqb_itime = iseconds;
5881553Srgrimes			qup->flags |= FOUND;
5891553Srgrimes			break;
5901553Srgrimes		}
5911553Srgrimes	}
5921553Srgrimes	fclose(fd);
5931553Srgrimes	/*
5941553Srgrimes	 * reset default grace periods for any filesystems
5951553Srgrimes	 * that have not been found.
5961553Srgrimes	 */
5971553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5981553Srgrimes		if (qup->flags & FOUND) {
5991553Srgrimes			qup->flags &= ~FOUND;
6001553Srgrimes			continue;
6011553Srgrimes		}
6021553Srgrimes		qup->dqblk.dqb_btime = 0;
6031553Srgrimes		qup->dqblk.dqb_itime = 0;
6041553Srgrimes	}
6051553Srgrimes	return (1);
6061553Srgrimes}
6071553Srgrimes
6081553Srgrimes/*
6091553Srgrimes * Convert seconds to ASCII times.
6101553Srgrimes */
6111553Srgrimeschar *
6121553Srgrimescvtstoa(time)
6131553Srgrimes	time_t time;
6141553Srgrimes{
6151553Srgrimes	static char buf[20];
6161553Srgrimes
6171553Srgrimes	if (time % (24 * 60 * 60) == 0) {
6181553Srgrimes		time /= 24 * 60 * 60;
6191553Srgrimes		sprintf(buf, "%d day%s", time, time == 1 ? "" : "s");
6201553Srgrimes	} else if (time % (60 * 60) == 0) {
6211553Srgrimes		time /= 60 * 60;
6221553Srgrimes		sprintf(buf, "%d hour%s", time, time == 1 ? "" : "s");
6231553Srgrimes	} else if (time % 60 == 0) {
6241553Srgrimes		time /= 60;
6251553Srgrimes		sprintf(buf, "%d minute%s", time, time == 1 ? "" : "s");
6261553Srgrimes	} else
6271553Srgrimes		sprintf(buf, "%d second%s", time, time == 1 ? "" : "s");
6281553Srgrimes	return (buf);
6291553Srgrimes}
6301553Srgrimes
6311553Srgrimes/*
6321553Srgrimes * Convert ASCII input times to seconds.
6331553Srgrimes */
6341553Srgrimescvtatos(time, units, seconds)
6351553Srgrimes	time_t time;
6361553Srgrimes	char *units;
6371553Srgrimes	time_t *seconds;
6381553Srgrimes{
6391553Srgrimes
6401553Srgrimes	if (bcmp(units, "second", 6) == 0)
6411553Srgrimes		*seconds = time;
6421553Srgrimes	else if (bcmp(units, "minute", 6) == 0)
6431553Srgrimes		*seconds = time * 60;
6441553Srgrimes	else if (bcmp(units, "hour", 4) == 0)
6451553Srgrimes		*seconds = time * 60 * 60;
6461553Srgrimes	else if (bcmp(units, "day", 3) == 0)
6471553Srgrimes		*seconds = time * 24 * 60 * 60;
6481553Srgrimes	else {
6491553Srgrimes		printf("%s: bad units, specify %s\n", units,
6501553Srgrimes		    "days, hours, minutes, or seconds");
6511553Srgrimes		return (0);
6521553Srgrimes	}
6531553Srgrimes	return (1);
6541553Srgrimes}
6551553Srgrimes
6561553Srgrimes/*
6571553Srgrimes * Free a list of quotause structures.
6581553Srgrimes */
6591553Srgrimesfreeprivs(quplist)
6601553Srgrimes	struct quotause *quplist;
6611553Srgrimes{
6621553Srgrimes	register struct quotause *qup, *nextqup;
6631553Srgrimes
6641553Srgrimes	for (qup = quplist; qup; qup = nextqup) {
6651553Srgrimes		nextqup = qup->next;
6661553Srgrimes		free(qup);
6671553Srgrimes	}
6681553Srgrimes}
6691553Srgrimes
6701553Srgrimes/*
6711553Srgrimes * Check whether a string is completely composed of digits.
6721553Srgrimes */
6731553Srgrimesalldigits(s)
6741553Srgrimes	register char *s;
6751553Srgrimes{
6761553Srgrimes	register c;
6771553Srgrimes
6781553Srgrimes	c = *s++;
6791553Srgrimes	do {
6801553Srgrimes		if (!isdigit(c))
6811553Srgrimes			return (0);
6821553Srgrimes	} while (c = *s++);
6831553Srgrimes	return (1);
6841553Srgrimes}
6851553Srgrimes
6861553Srgrimes/*
6871553Srgrimes * Check to see if a particular quota is to be enabled.
6881553Srgrimes */
6891553Srgrimeshasquota(fs, type, qfnamep)
6901553Srgrimes	register struct fstab *fs;
6911553Srgrimes	int type;
6921553Srgrimes	char **qfnamep;
6931553Srgrimes{
6941553Srgrimes	register char *opt;
6951553Srgrimes	char *cp, *index(), *strtok();
6961553Srgrimes	static char initname, usrname[100], grpname[100];
6971553Srgrimes	static char buf[BUFSIZ];
6981553Srgrimes
6991553Srgrimes	if (!initname) {
7001553Srgrimes		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
7011553Srgrimes		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
7021553Srgrimes		initname = 1;
7031553Srgrimes	}
7041553Srgrimes	strcpy(buf, fs->fs_mntops);
7051553Srgrimes	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
7061553Srgrimes		if (cp = index(opt, '='))
7071553Srgrimes			*cp++ = '\0';
7081553Srgrimes		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
7091553Srgrimes			break;
7101553Srgrimes		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
7111553Srgrimes			break;
7121553Srgrimes	}
7131553Srgrimes	if (!opt)
7141553Srgrimes		return (0);
7151553Srgrimes	if (cp) {
7161553Srgrimes		*qfnamep = cp;
7171553Srgrimes		return (1);
7181553Srgrimes	}
7191553Srgrimes	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
7201553Srgrimes	*qfnamep = buf;
7211553Srgrimes	return (1);
7221553Srgrimes}
723