edquota.c revision 28431
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;
8814961Smpp	register uid_t startuid, enduid;
8914961Smpp	char *protoname, *cp, ch;
901553Srgrimes	int tflag = 0, pflag = 0;
9114961Smpp	char buf[30];
921553Srgrimes
931553Srgrimes	if (argc < 2)
941553Srgrimes		usage();
951553Srgrimes	if (getuid()) {
961553Srgrimes		fprintf(stderr, "edquota: permission denied\n");
971553Srgrimes		exit(1);
981553Srgrimes	}
991553Srgrimes	quotatype = USRQUOTA;
10024428Simp	while ((ch = getopt(argc, argv, "ugtp:")) != -1) {
1011553Srgrimes		switch(ch) {
1021553Srgrimes		case 'p':
1031553Srgrimes			protoname = optarg;
1041553Srgrimes			pflag++;
1051553Srgrimes			break;
1061553Srgrimes		case 'g':
1071553Srgrimes			quotatype = GRPQUOTA;
1081553Srgrimes			break;
1091553Srgrimes		case 'u':
1101553Srgrimes			quotatype = USRQUOTA;
1111553Srgrimes			break;
1121553Srgrimes		case 't':
1131553Srgrimes			tflag++;
1141553Srgrimes			break;
1151553Srgrimes		default:
1161553Srgrimes			usage();
1171553Srgrimes		}
1181553Srgrimes	}
1191553Srgrimes	argc -= optind;
1201553Srgrimes	argv += optind;
1211553Srgrimes	if (pflag) {
1221553Srgrimes		if ((protoid = getentry(protoname, quotatype)) == -1)
1231553Srgrimes			exit(1);
1241553Srgrimes		protoprivs = getprivs(protoid, quotatype);
1251553Srgrimes		for (qup = protoprivs; qup; qup = qup->next) {
1261553Srgrimes			qup->dqblk.dqb_btime = 0;
1271553Srgrimes			qup->dqblk.dqb_itime = 0;
1281553Srgrimes		}
1291553Srgrimes		while (argc-- > 0) {
13014961Smpp			if (isdigit(*argv[0]) &&
13114961Smpp			    (cp = strchr(*argv, '-')) != NULL) {
13214961Smpp				*cp++ = '\0';
13314961Smpp				startuid = atoi(*argv);
13414961Smpp				enduid = atoi(cp);
13514961Smpp				if (enduid < startuid) {
13614961Smpp					fprintf(stderr, "edquota: ending uid (%d) must be >= starting uid (%d) when using uid ranges\n",
13714961Smpp						enduid, startuid);
13814961Smpp					exit(1);
13914961Smpp				}
14014961Smpp				for ( ; startuid <= enduid; startuid++) {
14114961Smpp					snprintf(buf, sizeof(buf), "%d",
14214961Smpp					    startuid);
14314961Smpp					if ((id = getentry(buf, quotatype)) < 0)
14414961Smpp						continue;
14514961Smpp					putprivs(id, quotatype, protoprivs);
14614961Smpp				}
14714961Smpp				continue;
14814961Smpp			}
1491553Srgrimes			if ((id = getentry(*argv++, quotatype)) < 0)
1501553Srgrimes				continue;
1511553Srgrimes			putprivs(id, quotatype, protoprivs);
1521553Srgrimes		}
1531553Srgrimes		exit(0);
1541553Srgrimes	}
1551553Srgrimes	tmpfd = mkstemp(tmpfil);
1561553Srgrimes	fchown(tmpfd, getuid(), getgid());
1571553Srgrimes	if (tflag) {
1581553Srgrimes		protoprivs = getprivs(0, quotatype);
1591553Srgrimes		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
1601553Srgrimes			exit(1);
1614782Sache		if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
1621553Srgrimes			putprivs(0, quotatype, protoprivs);
1631553Srgrimes		freeprivs(protoprivs);
16428431Sjlemon		close(tmpfd);
16528431Sjlemon		unlink(tmpfil);
1661553Srgrimes		exit(0);
1671553Srgrimes	}
1681553Srgrimes	for ( ; argc > 0; argc--, argv++) {
1691553Srgrimes		if ((id = getentry(*argv, quotatype)) == -1)
1701553Srgrimes			continue;
1711553Srgrimes		curprivs = getprivs(id, quotatype);
1721553Srgrimes		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
1731553Srgrimes			continue;
1744782Sache		if (editit(tmpfil) && readprivs(curprivs, tmpfil))
1751553Srgrimes			putprivs(id, quotatype, curprivs);
1761553Srgrimes		freeprivs(curprivs);
1771553Srgrimes	}
1781553Srgrimes	close(tmpfd);
1791553Srgrimes	unlink(tmpfil);
1801553Srgrimes	exit(0);
1811553Srgrimes}
1821553Srgrimes
1831553Srgrimesusage()
1841553Srgrimes{
1851553Srgrimes	fprintf(stderr, "%s%s%s%s",
1861553Srgrimes		"Usage: edquota [-u] [-p username] username ...\n",
1871553Srgrimes		"\tedquota -g [-p groupname] groupname ...\n",
1881553Srgrimes		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
1891553Srgrimes	exit(1);
1901553Srgrimes}
1911553Srgrimes
1921553Srgrimes/*
1931553Srgrimes * This routine converts a name for a particular quota type to
1941553Srgrimes * an identifier. This routine must agree with the kernel routine
1951553Srgrimes * getinoquota as to the interpretation of quota types.
1961553Srgrimes */
1971553Srgrimesgetentry(name, quotatype)
1981553Srgrimes	char *name;
1991553Srgrimes	int quotatype;
2001553Srgrimes{
2011553Srgrimes	struct passwd *pw;
2021553Srgrimes	struct group *gr;
2031553Srgrimes
2041553Srgrimes	if (alldigits(name))
2051553Srgrimes		return (atoi(name));
2061553Srgrimes	switch(quotatype) {
2071553Srgrimes	case USRQUOTA:
2081553Srgrimes		if (pw = getpwnam(name))
2091553Srgrimes			return (pw->pw_uid);
2101553Srgrimes		fprintf(stderr, "%s: no such user\n", name);
2111553Srgrimes		break;
2121553Srgrimes	case GRPQUOTA:
2131553Srgrimes		if (gr = getgrnam(name))
2141553Srgrimes			return (gr->gr_gid);
2151553Srgrimes		fprintf(stderr, "%s: no such group\n", name);
2161553Srgrimes		break;
2171553Srgrimes	default:
2181553Srgrimes		fprintf(stderr, "%d: unknown quota type\n", quotatype);
2191553Srgrimes		break;
2201553Srgrimes	}
2211553Srgrimes	sleep(1);
2221553Srgrimes	return (-1);
2231553Srgrimes}
2241553Srgrimes
2251553Srgrimes/*
2261553Srgrimes * Collect the requested quota information.
2271553Srgrimes */
2281553Srgrimesstruct quotause *
2291553Srgrimesgetprivs(id, quotatype)
2301553Srgrimes	register long id;
2311553Srgrimes	int quotatype;
2321553Srgrimes{
2331553Srgrimes	register struct fstab *fs;
2341553Srgrimes	register struct quotause *qup, *quptail;
2351553Srgrimes	struct quotause *quphead;
2361553Srgrimes	int qcmd, qupsize, fd;
2371553Srgrimes	char *qfpathname;
2381553Srgrimes	static int warned = 0;
2391553Srgrimes	extern int errno;
2401553Srgrimes
2411553Srgrimes	setfsent();
2421553Srgrimes	quphead = (struct quotause *)0;
2431553Srgrimes	qcmd = QCMD(Q_GETQUOTA, quotatype);
2441553Srgrimes	while (fs = getfsent()) {
2451553Srgrimes		if (strcmp(fs->fs_vfstype, "ufs"))
2461553Srgrimes			continue;
2471553Srgrimes		if (!hasquota(fs, quotatype, &qfpathname))
2481553Srgrimes			continue;
2491553Srgrimes		qupsize = sizeof(*qup) + strlen(qfpathname);
2501553Srgrimes		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
2511553Srgrimes			fprintf(stderr, "edquota: out of memory\n");
2521553Srgrimes			exit(2);
2531553Srgrimes		}
2541553Srgrimes		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
2551553Srgrimes	    		if (errno == EOPNOTSUPP && !warned) {
2561553Srgrimes				warned++;
2571553Srgrimes				fprintf(stderr, "Warning: %s\n",
2581553Srgrimes				    "Quotas are not compiled into this kernel");
2591553Srgrimes				sleep(3);
2601553Srgrimes			}
2611553Srgrimes			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
2621553Srgrimes				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
2631553Srgrimes				if (fd < 0 && errno != ENOENT) {
2641553Srgrimes					perror(qfpathname);
2651553Srgrimes					free(qup);
2661553Srgrimes					continue;
2671553Srgrimes				}
2681553Srgrimes				fprintf(stderr, "Creating quota file %s\n",
2691553Srgrimes				    qfpathname);
2701553Srgrimes				sleep(3);
2711553Srgrimes				(void) fchown(fd, getuid(),
2721553Srgrimes				    getentry(quotagroup, GRPQUOTA));
2731553Srgrimes				(void) fchmod(fd, 0640);
2741553Srgrimes			}
2751553Srgrimes			lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
2761553Srgrimes			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
2771553Srgrimes			case 0:			/* EOF */
2781553Srgrimes				/*
2791553Srgrimes				 * Convert implicit 0 quota (EOF)
2801553Srgrimes				 * into an explicit one (zero'ed dqblk)
2811553Srgrimes				 */
2821553Srgrimes				bzero((caddr_t)&qup->dqblk,
2831553Srgrimes				    sizeof(struct dqblk));
2841553Srgrimes				break;
2851553Srgrimes
2861553Srgrimes			case sizeof(struct dqblk):	/* OK */
2871553Srgrimes				break;
2881553Srgrimes
2891553Srgrimes			default:		/* ERROR */
2901553Srgrimes				fprintf(stderr, "edquota: read error in ");
2911553Srgrimes				perror(qfpathname);
2921553Srgrimes				close(fd);
2931553Srgrimes				free(qup);
2941553Srgrimes				continue;
2951553Srgrimes			}
2961553Srgrimes			close(fd);
2971553Srgrimes		}
2981553Srgrimes		strcpy(qup->qfname, qfpathname);
2991553Srgrimes		strcpy(qup->fsname, fs->fs_file);
3001553Srgrimes		if (quphead == NULL)
3011553Srgrimes			quphead = qup;
3021553Srgrimes		else
3031553Srgrimes			quptail->next = qup;
3041553Srgrimes		quptail = qup;
3051553Srgrimes		qup->next = 0;
3061553Srgrimes	}
3071553Srgrimes	endfsent();
3081553Srgrimes	return (quphead);
3091553Srgrimes}
3101553Srgrimes
3111553Srgrimes/*
3121553Srgrimes * Store the requested quota information.
3131553Srgrimes */
3141553Srgrimesputprivs(id, quotatype, quplist)
3151553Srgrimes	long id;
3161553Srgrimes	int quotatype;
3171553Srgrimes	struct quotause *quplist;
3181553Srgrimes{
3191553Srgrimes	register struct quotause *qup;
3201553Srgrimes	int qcmd, fd;
3211553Srgrimes
3221553Srgrimes	qcmd = QCMD(Q_SETQUOTA, quotatype);
3231553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
3241553Srgrimes		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
3251553Srgrimes			continue;
3261553Srgrimes		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
3271553Srgrimes			perror(qup->qfname);
3281553Srgrimes		} else {
3291553Srgrimes			lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
3301553Srgrimes			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
3311553Srgrimes			    sizeof (struct dqblk)) {
3321553Srgrimes				fprintf(stderr, "edquota: ");
3331553Srgrimes				perror(qup->qfname);
3341553Srgrimes			}
3351553Srgrimes			close(fd);
3361553Srgrimes		}
3371553Srgrimes	}
3381553Srgrimes}
3391553Srgrimes
3401553Srgrimes/*
3411553Srgrimes * Take a list of priviledges and get it edited.
3421553Srgrimes */
3431553Srgrimeseditit(tmpfile)
3441553Srgrimes	char *tmpfile;
3451553Srgrimes{
3461553Srgrimes	long omask;
3471553Srgrimes	int pid, stat;
3481553Srgrimes	extern char *getenv();
3491553Srgrimes
3501553Srgrimes	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
3511553Srgrimes top:
3521553Srgrimes	if ((pid = fork()) < 0) {
3531553Srgrimes		extern errno;
3541553Srgrimes
3551553Srgrimes		if (errno == EPROCLIM) {
3561553Srgrimes			fprintf(stderr, "You have too many processes\n");
3571553Srgrimes			return(0);
3581553Srgrimes		}
3591553Srgrimes		if (errno == EAGAIN) {
3601553Srgrimes			sleep(1);
3611553Srgrimes			goto top;
3621553Srgrimes		}
3631553Srgrimes		perror("fork");
3641553Srgrimes		return (0);
3651553Srgrimes	}
3661553Srgrimes	if (pid == 0) {
3671553Srgrimes		register char *ed;
3681553Srgrimes
3691553Srgrimes		sigsetmask(omask);
3701553Srgrimes		setgid(getgid());
3711553Srgrimes		setuid(getuid());
3721553Srgrimes		if ((ed = getenv("EDITOR")) == (char *)0)
3731553Srgrimes			ed = _PATH_VI;
3741553Srgrimes		execlp(ed, ed, tmpfile, 0);
3751553Srgrimes		perror(ed);
3761553Srgrimes		exit(1);
3771553Srgrimes	}
3781553Srgrimes	waitpid(pid, &stat, 0);
3791553Srgrimes	sigsetmask(omask);
3801553Srgrimes	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
3811553Srgrimes		return (0);
3821553Srgrimes	return (1);
3831553Srgrimes}
3841553Srgrimes
3851553Srgrimes/*
3861553Srgrimes * Convert a quotause list to an ASCII file.
3871553Srgrimes */
3881553Srgrimeswriteprivs(quplist, outfd, name, quotatype)
3891553Srgrimes	struct quotause *quplist;
3901553Srgrimes	int outfd;
3911553Srgrimes	char *name;
3921553Srgrimes	int quotatype;
3931553Srgrimes{
3941553Srgrimes	register struct quotause *qup;
3951553Srgrimes	FILE *fd;
3961553Srgrimes
3971553Srgrimes	ftruncate(outfd, 0);
3981553Srgrimes	lseek(outfd, 0, L_SET);
3991553Srgrimes	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
4001553Srgrimes		fprintf(stderr, "edquota: ");
4011553Srgrimes		perror(tmpfil);
4021553Srgrimes		exit(1);
4031553Srgrimes	}
4041553Srgrimes	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
4051553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
4068325Sbde		fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
4071553Srgrimes		    qup->fsname, "blocks in use:",
4088325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
4098325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
4108325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
4118325Sbde		fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
4121553Srgrimes		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
4131553Srgrimes		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
4141553Srgrimes	}
4151553Srgrimes	fclose(fd);
4161553Srgrimes	return (1);
4171553Srgrimes}
4181553Srgrimes
4191553Srgrimes/*
4201553Srgrimes * Merge changes to an ASCII file into a quotause list.
4211553Srgrimes */
4224782Sachereadprivs(quplist, inname)
4231553Srgrimes	struct quotause *quplist;
4244782Sache	char *inname;
4251553Srgrimes{
4261553Srgrimes	register struct quotause *qup;
4271553Srgrimes	FILE *fd;
4281553Srgrimes	int cnt;
4291553Srgrimes	register char *cp;
4301553Srgrimes	struct dqblk dqblk;
4311553Srgrimes	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
4321553Srgrimes
4334782Sache	fd = fopen(inname, "r");
4341553Srgrimes	if (fd == NULL) {
4351553Srgrimes		fprintf(stderr, "Can't re-read temp file!!\n");
4361553Srgrimes		return (0);
4371553Srgrimes	}
4381553Srgrimes	/*
4391553Srgrimes	 * Discard title line, then read pairs of lines to process.
4401553Srgrimes	 */
4411553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
4421553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL &&
4431553Srgrimes	       fgets(line2, sizeof (line2), fd) != NULL) {
4441553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
4451553Srgrimes			fprintf(stderr, "%s: bad format\n", line1);
4461553Srgrimes			return (0);
4471553Srgrimes		}
4481553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
4491553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp,
4501553Srgrimes			    &fsp[strlen(fsp) + 1]);
4511553Srgrimes			return (0);
4521553Srgrimes		}
4531553Srgrimes		cnt = sscanf(cp,
4548325Sbde		    " blocks in use: %lu, limits (soft = %lu, hard = %lu)",
4551553Srgrimes		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
4561553Srgrimes		    &dqblk.dqb_bhardlimit);
4571553Srgrimes		if (cnt != 3) {
4581553Srgrimes			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
4591553Srgrimes			return (0);
4601553Srgrimes		}
4611553Srgrimes		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
4621553Srgrimes		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
4631553Srgrimes		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
4641553Srgrimes		if ((cp = strtok(line2, "\n")) == NULL) {
4651553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
4661553Srgrimes			return (0);
4671553Srgrimes		}
4681553Srgrimes		cnt = sscanf(cp,
4698325Sbde		    "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
4701553Srgrimes		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
4711553Srgrimes		    &dqblk.dqb_ihardlimit);
4721553Srgrimes		if (cnt != 3) {
4731553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
4741553Srgrimes			return (0);
4751553Srgrimes		}
4761553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
4771553Srgrimes			if (strcmp(fsp, qup->fsname))
4781553Srgrimes				continue;
4791553Srgrimes			/*
4801553Srgrimes			 * Cause time limit to be reset when the quota
4811553Srgrimes			 * is next used if previously had no soft limit
4821553Srgrimes			 * or were under it, but now have a soft limit
4831553Srgrimes			 * and are over it.
4841553Srgrimes			 */
4851553Srgrimes			if (dqblk.dqb_bsoftlimit &&
4861553Srgrimes			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
4871553Srgrimes			    (qup->dqblk.dqb_bsoftlimit == 0 ||
4881553Srgrimes			     qup->dqblk.dqb_curblocks <
4891553Srgrimes			     qup->dqblk.dqb_bsoftlimit))
4901553Srgrimes				qup->dqblk.dqb_btime = 0;
4911553Srgrimes			if (dqblk.dqb_isoftlimit &&
4921553Srgrimes			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
4931553Srgrimes			    (qup->dqblk.dqb_isoftlimit == 0 ||
4941553Srgrimes			     qup->dqblk.dqb_curinodes <
4951553Srgrimes			     qup->dqblk.dqb_isoftlimit))
4961553Srgrimes				qup->dqblk.dqb_itime = 0;
4971553Srgrimes			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
4981553Srgrimes			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
4991553Srgrimes			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
5001553Srgrimes			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
5011553Srgrimes			qup->flags |= FOUND;
5021553Srgrimes			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
5031553Srgrimes			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
5041553Srgrimes				break;
5051553Srgrimes			fprintf(stderr,
5061553Srgrimes			    "%s: cannot change current allocation\n", fsp);
5071553Srgrimes			break;
5081553Srgrimes		}
5091553Srgrimes	}
5101553Srgrimes	fclose(fd);
5111553Srgrimes	/*
5121553Srgrimes	 * Disable quotas for any filesystems that have not been found.
5131553Srgrimes	 */
5141553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5151553Srgrimes		if (qup->flags & FOUND) {
5161553Srgrimes			qup->flags &= ~FOUND;
5171553Srgrimes			continue;
5181553Srgrimes		}
5191553Srgrimes		qup->dqblk.dqb_bsoftlimit = 0;
5201553Srgrimes		qup->dqblk.dqb_bhardlimit = 0;
5211553Srgrimes		qup->dqblk.dqb_isoftlimit = 0;
5221553Srgrimes		qup->dqblk.dqb_ihardlimit = 0;
5231553Srgrimes	}
5241553Srgrimes	return (1);
5251553Srgrimes}
5261553Srgrimes
5271553Srgrimes/*
5281553Srgrimes * Convert a quotause list to an ASCII file of grace times.
5291553Srgrimes */
5301553Srgrimeswritetimes(quplist, outfd, quotatype)
5311553Srgrimes	struct quotause *quplist;
5321553Srgrimes	int outfd;
5331553Srgrimes	int quotatype;
5341553Srgrimes{
5351553Srgrimes	register struct quotause *qup;
5361553Srgrimes	char *cvtstoa();
5371553Srgrimes	FILE *fd;
5381553Srgrimes
5391553Srgrimes	ftruncate(outfd, 0);
5401553Srgrimes	lseek(outfd, 0, L_SET);
5411553Srgrimes	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
5421553Srgrimes		fprintf(stderr, "edquota: ");
5431553Srgrimes		perror(tmpfil);
5441553Srgrimes		exit(1);
5451553Srgrimes	}
5461553Srgrimes	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
5471553Srgrimes	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
5481553Srgrimes	    qfextension[quotatype]);
5491553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5501553Srgrimes		fprintf(fd, "%s: block grace period: %s, ",
5511553Srgrimes		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
5521553Srgrimes		fprintf(fd, "file grace period: %s\n",
5531553Srgrimes		    cvtstoa(qup->dqblk.dqb_itime));
5541553Srgrimes	}
5551553Srgrimes	fclose(fd);
5561553Srgrimes	return (1);
5571553Srgrimes}
5581553Srgrimes
5591553Srgrimes/*
5601553Srgrimes * Merge changes of grace times in an ASCII file into a quotause list.
5611553Srgrimes */
5624782Sachereadtimes(quplist, inname)
5631553Srgrimes	struct quotause *quplist;
5644782Sache	char *inname;
5651553Srgrimes{
5661553Srgrimes	register struct quotause *qup;
5671553Srgrimes	FILE *fd;
5681553Srgrimes	int cnt;
5691553Srgrimes	register char *cp;
5701553Srgrimes	time_t itime, btime, iseconds, bseconds;
5711553Srgrimes	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
5721553Srgrimes
5734782Sache	fd = fopen(inname, "r");
5741553Srgrimes	if (fd == NULL) {
5751553Srgrimes		fprintf(stderr, "Can't re-read temp file!!\n");
5761553Srgrimes		return (0);
5771553Srgrimes	}
5781553Srgrimes	/*
5791553Srgrimes	 * Discard two title lines, then read lines to process.
5801553Srgrimes	 */
5811553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5821553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5831553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL) {
5841553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
5851553Srgrimes			fprintf(stderr, "%s: bad format\n", line1);
5861553Srgrimes			return (0);
5871553Srgrimes		}
5881553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
5891553Srgrimes			fprintf(stderr, "%s: %s: bad format\n", fsp,
5901553Srgrimes			    &fsp[strlen(fsp) + 1]);
5911553Srgrimes			return (0);
5921553Srgrimes		}
5931553Srgrimes		cnt = sscanf(cp,
5948325Sbde		    " block grace period: %ld %s file grace period: %ld %s",
5951553Srgrimes		    &btime, bunits, &itime, iunits);
5961553Srgrimes		if (cnt != 4) {
5971553Srgrimes			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
5981553Srgrimes			return (0);
5991553Srgrimes		}
6001553Srgrimes		if (cvtatos(btime, bunits, &bseconds) == 0)
6011553Srgrimes			return (0);
6021553Srgrimes		if (cvtatos(itime, iunits, &iseconds) == 0)
6031553Srgrimes			return (0);
6041553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
6051553Srgrimes			if (strcmp(fsp, qup->fsname))
6061553Srgrimes				continue;
6071553Srgrimes			qup->dqblk.dqb_btime = bseconds;
6081553Srgrimes			qup->dqblk.dqb_itime = iseconds;
6091553Srgrimes			qup->flags |= FOUND;
6101553Srgrimes			break;
6111553Srgrimes		}
6121553Srgrimes	}
6131553Srgrimes	fclose(fd);
6141553Srgrimes	/*
6151553Srgrimes	 * reset default grace periods for any filesystems
6161553Srgrimes	 * that have not been found.
6171553Srgrimes	 */
6181553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
6191553Srgrimes		if (qup->flags & FOUND) {
6201553Srgrimes			qup->flags &= ~FOUND;
6211553Srgrimes			continue;
6221553Srgrimes		}
6231553Srgrimes		qup->dqblk.dqb_btime = 0;
6241553Srgrimes		qup->dqblk.dqb_itime = 0;
6251553Srgrimes	}
6261553Srgrimes	return (1);
6271553Srgrimes}
6281553Srgrimes
6291553Srgrimes/*
6301553Srgrimes * Convert seconds to ASCII times.
6311553Srgrimes */
6321553Srgrimeschar *
6331553Srgrimescvtstoa(time)
6341553Srgrimes	time_t time;
6351553Srgrimes{
6361553Srgrimes	static char buf[20];
6371553Srgrimes
6381553Srgrimes	if (time % (24 * 60 * 60) == 0) {
6391553Srgrimes		time /= 24 * 60 * 60;
6408325Sbde		sprintf(buf, "%ld day%s", time, time == 1 ? "" : "s");
6411553Srgrimes	} else if (time % (60 * 60) == 0) {
6421553Srgrimes		time /= 60 * 60;
6438325Sbde		sprintf(buf, "%ld hour%s", time, time == 1 ? "" : "s");
6441553Srgrimes	} else if (time % 60 == 0) {
6451553Srgrimes		time /= 60;
6468325Sbde		sprintf(buf, "%ld minute%s", time, time == 1 ? "" : "s");
6471553Srgrimes	} else
6488325Sbde		sprintf(buf, "%ld second%s", time, time == 1 ? "" : "s");
6491553Srgrimes	return (buf);
6501553Srgrimes}
6511553Srgrimes
6521553Srgrimes/*
6531553Srgrimes * Convert ASCII input times to seconds.
6541553Srgrimes */
6551553Srgrimescvtatos(time, units, seconds)
6561553Srgrimes	time_t time;
6571553Srgrimes	char *units;
6581553Srgrimes	time_t *seconds;
6591553Srgrimes{
6601553Srgrimes
6611553Srgrimes	if (bcmp(units, "second", 6) == 0)
6621553Srgrimes		*seconds = time;
6631553Srgrimes	else if (bcmp(units, "minute", 6) == 0)
6641553Srgrimes		*seconds = time * 60;
6651553Srgrimes	else if (bcmp(units, "hour", 4) == 0)
6661553Srgrimes		*seconds = time * 60 * 60;
6671553Srgrimes	else if (bcmp(units, "day", 3) == 0)
6681553Srgrimes		*seconds = time * 24 * 60 * 60;
6691553Srgrimes	else {
6701553Srgrimes		printf("%s: bad units, specify %s\n", units,
6711553Srgrimes		    "days, hours, minutes, or seconds");
6721553Srgrimes		return (0);
6731553Srgrimes	}
6741553Srgrimes	return (1);
6751553Srgrimes}
6761553Srgrimes
6771553Srgrimes/*
6781553Srgrimes * Free a list of quotause structures.
6791553Srgrimes */
6801553Srgrimesfreeprivs(quplist)
6811553Srgrimes	struct quotause *quplist;
6821553Srgrimes{
6831553Srgrimes	register struct quotause *qup, *nextqup;
6841553Srgrimes
6851553Srgrimes	for (qup = quplist; qup; qup = nextqup) {
6861553Srgrimes		nextqup = qup->next;
6871553Srgrimes		free(qup);
6881553Srgrimes	}
6891553Srgrimes}
6901553Srgrimes
6911553Srgrimes/*
6921553Srgrimes * Check whether a string is completely composed of digits.
6931553Srgrimes */
6941553Srgrimesalldigits(s)
6951553Srgrimes	register char *s;
6961553Srgrimes{
6971553Srgrimes	register c;
6981553Srgrimes
6991553Srgrimes	c = *s++;
7001553Srgrimes	do {
7011553Srgrimes		if (!isdigit(c))
7021553Srgrimes			return (0);
7031553Srgrimes	} while (c = *s++);
7041553Srgrimes	return (1);
7051553Srgrimes}
7061553Srgrimes
7071553Srgrimes/*
7081553Srgrimes * Check to see if a particular quota is to be enabled.
7091553Srgrimes */
7101553Srgrimeshasquota(fs, type, qfnamep)
7111553Srgrimes	register struct fstab *fs;
7121553Srgrimes	int type;
7131553Srgrimes	char **qfnamep;
7141553Srgrimes{
7151553Srgrimes	register char *opt;
7161553Srgrimes	char *cp, *index(), *strtok();
7171553Srgrimes	static char initname, usrname[100], grpname[100];
7181553Srgrimes	static char buf[BUFSIZ];
7191553Srgrimes
7201553Srgrimes	if (!initname) {
7211553Srgrimes		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
7221553Srgrimes		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
7231553Srgrimes		initname = 1;
7241553Srgrimes	}
7251553Srgrimes	strcpy(buf, fs->fs_mntops);
7261553Srgrimes	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
7271553Srgrimes		if (cp = index(opt, '='))
7281553Srgrimes			*cp++ = '\0';
7291553Srgrimes		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
7301553Srgrimes			break;
7311553Srgrimes		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
7321553Srgrimes			break;
7331553Srgrimes	}
7341553Srgrimes	if (!opt)
7351553Srgrimes		return (0);
7361553Srgrimes	if (cp) {
7371553Srgrimes		*qfnamep = cp;
7381553Srgrimes		return (1);
7391553Srgrimes	}
7401553Srgrimes	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
7411553Srgrimes	*qfnamep = buf;
7421553Srgrimes	return (1);
7431553Srgrimes}
744