edquota.c revision 4782
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);
1404782Sache		if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
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;
1514782Sache		if (editit(tmpfil) && readprivs(curprivs, tmpfil))
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 */
3994782Sachereadprivs(quplist, inname)
4001553Srgrimes	struct quotause *quplist;
4014782Sache	char *inname;
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
4104782Sache	fd = fopen(inname, "r");
4111553Srgrimes	if (fd == NULL) {
4121553Srgrimes		fprintf(stderr, "Can't re-read temp file!!\n");
4131553Srgrimes		return (0);
4141553Srgrimes	}
4151553Srgrimes	/*
4161553Srgrimes	 * Discard title line, then read pairs of lines to process.
4171553Srgrimes	 */
4181553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
4191553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL &&
4201553Srgrimes	       fgets(line2, sizeof (line2), fd) != NULL) {
4211553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
4221553Srgrimes			fprintf(stderr, "%s: bad format\n", line1);
4231553Srgrimes			return (0);
4241553Srgrimes		}
4251553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
4261553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp,
4271553Srgrimes			    &fsp[strlen(fsp) + 1]);
4281553Srgrimes			return (0);
4291553Srgrimes		}
4301553Srgrimes		cnt = sscanf(cp,
4311553Srgrimes		    " blocks in use: %d, limits (soft = %d, hard = %d)",
4321553Srgrimes		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
4331553Srgrimes		    &dqblk.dqb_bhardlimit);
4341553Srgrimes		if (cnt != 3) {
4351553Srgrimes			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
4361553Srgrimes			return (0);
4371553Srgrimes		}
4381553Srgrimes		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
4391553Srgrimes		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
4401553Srgrimes		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
4411553Srgrimes		if ((cp = strtok(line2, "\n")) == NULL) {
4421553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
4431553Srgrimes			return (0);
4441553Srgrimes		}
4451553Srgrimes		cnt = sscanf(cp,
4461553Srgrimes		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
4471553Srgrimes		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
4481553Srgrimes		    &dqblk.dqb_ihardlimit);
4491553Srgrimes		if (cnt != 3) {
4501553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
4511553Srgrimes			return (0);
4521553Srgrimes		}
4531553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
4541553Srgrimes			if (strcmp(fsp, qup->fsname))
4551553Srgrimes				continue;
4561553Srgrimes			/*
4571553Srgrimes			 * Cause time limit to be reset when the quota
4581553Srgrimes			 * is next used if previously had no soft limit
4591553Srgrimes			 * or were under it, but now have a soft limit
4601553Srgrimes			 * and are over it.
4611553Srgrimes			 */
4621553Srgrimes			if (dqblk.dqb_bsoftlimit &&
4631553Srgrimes			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
4641553Srgrimes			    (qup->dqblk.dqb_bsoftlimit == 0 ||
4651553Srgrimes			     qup->dqblk.dqb_curblocks <
4661553Srgrimes			     qup->dqblk.dqb_bsoftlimit))
4671553Srgrimes				qup->dqblk.dqb_btime = 0;
4681553Srgrimes			if (dqblk.dqb_isoftlimit &&
4691553Srgrimes			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
4701553Srgrimes			    (qup->dqblk.dqb_isoftlimit == 0 ||
4711553Srgrimes			     qup->dqblk.dqb_curinodes <
4721553Srgrimes			     qup->dqblk.dqb_isoftlimit))
4731553Srgrimes				qup->dqblk.dqb_itime = 0;
4741553Srgrimes			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
4751553Srgrimes			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
4761553Srgrimes			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
4771553Srgrimes			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
4781553Srgrimes			qup->flags |= FOUND;
4791553Srgrimes			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
4801553Srgrimes			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
4811553Srgrimes				break;
4821553Srgrimes			fprintf(stderr,
4831553Srgrimes			    "%s: cannot change current allocation\n", fsp);
4841553Srgrimes			break;
4851553Srgrimes		}
4861553Srgrimes	}
4871553Srgrimes	fclose(fd);
4881553Srgrimes	/*
4891553Srgrimes	 * Disable quotas for any filesystems that have not been found.
4901553Srgrimes	 */
4911553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
4921553Srgrimes		if (qup->flags & FOUND) {
4931553Srgrimes			qup->flags &= ~FOUND;
4941553Srgrimes			continue;
4951553Srgrimes		}
4961553Srgrimes		qup->dqblk.dqb_bsoftlimit = 0;
4971553Srgrimes		qup->dqblk.dqb_bhardlimit = 0;
4981553Srgrimes		qup->dqblk.dqb_isoftlimit = 0;
4991553Srgrimes		qup->dqblk.dqb_ihardlimit = 0;
5001553Srgrimes	}
5011553Srgrimes	return (1);
5021553Srgrimes}
5031553Srgrimes
5041553Srgrimes/*
5051553Srgrimes * Convert a quotause list to an ASCII file of grace times.
5061553Srgrimes */
5071553Srgrimeswritetimes(quplist, outfd, quotatype)
5081553Srgrimes	struct quotause *quplist;
5091553Srgrimes	int outfd;
5101553Srgrimes	int quotatype;
5111553Srgrimes{
5121553Srgrimes	register struct quotause *qup;
5131553Srgrimes	char *cvtstoa();
5141553Srgrimes	FILE *fd;
5151553Srgrimes
5161553Srgrimes	ftruncate(outfd, 0);
5171553Srgrimes	lseek(outfd, 0, L_SET);
5181553Srgrimes	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
5191553Srgrimes		fprintf(stderr, "edquota: ");
5201553Srgrimes		perror(tmpfil);
5211553Srgrimes		exit(1);
5221553Srgrimes	}
5231553Srgrimes	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
5241553Srgrimes	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
5251553Srgrimes	    qfextension[quotatype]);
5261553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5271553Srgrimes		fprintf(fd, "%s: block grace period: %s, ",
5281553Srgrimes		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
5291553Srgrimes		fprintf(fd, "file grace period: %s\n",
5301553Srgrimes		    cvtstoa(qup->dqblk.dqb_itime));
5311553Srgrimes	}
5321553Srgrimes	fclose(fd);
5331553Srgrimes	return (1);
5341553Srgrimes}
5351553Srgrimes
5361553Srgrimes/*
5371553Srgrimes * Merge changes of grace times in an ASCII file into a quotause list.
5381553Srgrimes */
5394782Sachereadtimes(quplist, inname)
5401553Srgrimes	struct quotause *quplist;
5414782Sache	char *inname;
5421553Srgrimes{
5431553Srgrimes	register struct quotause *qup;
5441553Srgrimes	FILE *fd;
5451553Srgrimes	int cnt;
5461553Srgrimes	register char *cp;
5471553Srgrimes	time_t itime, btime, iseconds, bseconds;
5481553Srgrimes	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
5491553Srgrimes
5504782Sache	fd = fopen(inname, "r");
5511553Srgrimes	if (fd == NULL) {
5521553Srgrimes		fprintf(stderr, "Can't re-read temp file!!\n");
5531553Srgrimes		return (0);
5541553Srgrimes	}
5551553Srgrimes	/*
5561553Srgrimes	 * Discard two title lines, then read lines to process.
5571553Srgrimes	 */
5581553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5591553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5601553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL) {
5611553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
5621553Srgrimes			fprintf(stderr, "%s: bad format\n", line1);
5631553Srgrimes			return (0);
5641553Srgrimes		}
5651553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
5661553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp,
5671553Srgrimes			    &fsp[strlen(fsp) + 1]);
5681553Srgrimes			return (0);
5691553Srgrimes		}
5701553Srgrimes		cnt = sscanf(cp,
5711553Srgrimes		    " block grace period: %d %s file grace period: %d %s",
5721553Srgrimes		    &btime, bunits, &itime, iunits);
5731553Srgrimes		if (cnt != 4) {
5741553Srgrimes			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
5751553Srgrimes			return (0);
5761553Srgrimes		}
5771553Srgrimes		if (cvtatos(btime, bunits, &bseconds) == 0)
5781553Srgrimes			return (0);
5791553Srgrimes		if (cvtatos(itime, iunits, &iseconds) == 0)
5801553Srgrimes			return (0);
5811553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
5821553Srgrimes			if (strcmp(fsp, qup->fsname))
5831553Srgrimes				continue;
5841553Srgrimes			qup->dqblk.dqb_btime = bseconds;
5851553Srgrimes			qup->dqblk.dqb_itime = iseconds;
5861553Srgrimes			qup->flags |= FOUND;
5871553Srgrimes			break;
5881553Srgrimes		}
5891553Srgrimes	}
5901553Srgrimes	fclose(fd);
5911553Srgrimes	/*
5921553Srgrimes	 * reset default grace periods for any filesystems
5931553Srgrimes	 * that have not been found.
5941553Srgrimes	 */
5951553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5961553Srgrimes		if (qup->flags & FOUND) {
5971553Srgrimes			qup->flags &= ~FOUND;
5981553Srgrimes			continue;
5991553Srgrimes		}
6001553Srgrimes		qup->dqblk.dqb_btime = 0;
6011553Srgrimes		qup->dqblk.dqb_itime = 0;
6021553Srgrimes	}
6031553Srgrimes	return (1);
6041553Srgrimes}
6051553Srgrimes
6061553Srgrimes/*
6071553Srgrimes * Convert seconds to ASCII times.
6081553Srgrimes */
6091553Srgrimeschar *
6101553Srgrimescvtstoa(time)
6111553Srgrimes	time_t time;
6121553Srgrimes{
6131553Srgrimes	static char buf[20];
6141553Srgrimes
6151553Srgrimes	if (time % (24 * 60 * 60) == 0) {
6161553Srgrimes		time /= 24 * 60 * 60;
6171553Srgrimes		sprintf(buf, "%d day%s", time, time == 1 ? "" : "s");
6181553Srgrimes	} else if (time % (60 * 60) == 0) {
6191553Srgrimes		time /= 60 * 60;
6201553Srgrimes		sprintf(buf, "%d hour%s", time, time == 1 ? "" : "s");
6211553Srgrimes	} else if (time % 60 == 0) {
6221553Srgrimes		time /= 60;
6231553Srgrimes		sprintf(buf, "%d minute%s", time, time == 1 ? "" : "s");
6241553Srgrimes	} else
6251553Srgrimes		sprintf(buf, "%d second%s", time, time == 1 ? "" : "s");
6261553Srgrimes	return (buf);
6271553Srgrimes}
6281553Srgrimes
6291553Srgrimes/*
6301553Srgrimes * Convert ASCII input times to seconds.
6311553Srgrimes */
6321553Srgrimescvtatos(time, units, seconds)
6331553Srgrimes	time_t time;
6341553Srgrimes	char *units;
6351553Srgrimes	time_t *seconds;
6361553Srgrimes{
6371553Srgrimes
6381553Srgrimes	if (bcmp(units, "second", 6) == 0)
6391553Srgrimes		*seconds = time;
6401553Srgrimes	else if (bcmp(units, "minute", 6) == 0)
6411553Srgrimes		*seconds = time * 60;
6421553Srgrimes	else if (bcmp(units, "hour", 4) == 0)
6431553Srgrimes		*seconds = time * 60 * 60;
6441553Srgrimes	else if (bcmp(units, "day", 3) == 0)
6451553Srgrimes		*seconds = time * 24 * 60 * 60;
6461553Srgrimes	else {
6471553Srgrimes		printf("%s: bad units, specify %s\n", units,
6481553Srgrimes		    "days, hours, minutes, or seconds");
6491553Srgrimes		return (0);
6501553Srgrimes	}
6511553Srgrimes	return (1);
6521553Srgrimes}
6531553Srgrimes
6541553Srgrimes/*
6551553Srgrimes * Free a list of quotause structures.
6561553Srgrimes */
6571553Srgrimesfreeprivs(quplist)
6581553Srgrimes	struct quotause *quplist;
6591553Srgrimes{
6601553Srgrimes	register struct quotause *qup, *nextqup;
6611553Srgrimes
6621553Srgrimes	for (qup = quplist; qup; qup = nextqup) {
6631553Srgrimes		nextqup = qup->next;
6641553Srgrimes		free(qup);
6651553Srgrimes	}
6661553Srgrimes}
6671553Srgrimes
6681553Srgrimes/*
6691553Srgrimes * Check whether a string is completely composed of digits.
6701553Srgrimes */
6711553Srgrimesalldigits(s)
6721553Srgrimes	register char *s;
6731553Srgrimes{
6741553Srgrimes	register c;
6751553Srgrimes
6761553Srgrimes	c = *s++;
6771553Srgrimes	do {
6781553Srgrimes		if (!isdigit(c))
6791553Srgrimes			return (0);
6801553Srgrimes	} while (c = *s++);
6811553Srgrimes	return (1);
6821553Srgrimes}
6831553Srgrimes
6841553Srgrimes/*
6851553Srgrimes * Check to see if a particular quota is to be enabled.
6861553Srgrimes */
6871553Srgrimeshasquota(fs, type, qfnamep)
6881553Srgrimes	register struct fstab *fs;
6891553Srgrimes	int type;
6901553Srgrimes	char **qfnamep;
6911553Srgrimes{
6921553Srgrimes	register char *opt;
6931553Srgrimes	char *cp, *index(), *strtok();
6941553Srgrimes	static char initname, usrname[100], grpname[100];
6951553Srgrimes	static char buf[BUFSIZ];
6961553Srgrimes
6971553Srgrimes	if (!initname) {
6981553Srgrimes		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
6991553Srgrimes		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
7001553Srgrimes		initname = 1;
7011553Srgrimes	}
7021553Srgrimes	strcpy(buf, fs->fs_mntops);
7031553Srgrimes	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
7041553Srgrimes		if (cp = index(opt, '='))
7051553Srgrimes			*cp++ = '\0';
7061553Srgrimes		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
7071553Srgrimes			break;
7081553Srgrimes		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
7091553Srgrimes			break;
7101553Srgrimes	}
7111553Srgrimes	if (!opt)
7121553Srgrimes		return (0);
7131553Srgrimes	if (cp) {
7141553Srgrimes		*qfnamep = cp;
7151553Srgrimes		return (1);
7161553Srgrimes	}
7171553Srgrimes	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
7181553Srgrimes	*qfnamep = buf;
7191553Srgrimes	return (1);
7201553Srgrimes}
721