edquota.c revision 101546
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
3829529Scharnierstatic const 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
4429529Scharnier#if 0
451553Srgrimesstatic char sccsid[] = "@(#)edquota.c	8.1 (Berkeley) 6/6/93";
4629529Scharnier#endif
4729529Scharnierstatic const char rcsid[] =
4850479Speter  "$FreeBSD: head/usr.sbin/edquota/edquota.c 101546 2002-08-09 01:21:03Z iedowse $";
491553Srgrimes#endif /* not lint */
501553Srgrimes
511553Srgrimes/*
521553Srgrimes * Disk quota editor.
531553Srgrimes */
541553Srgrimes#include <sys/param.h>
551553Srgrimes#include <sys/stat.h>
561553Srgrimes#include <sys/file.h>
571553Srgrimes#include <sys/wait.h>
581553Srgrimes#include <ufs/ufs/quota.h>
5929529Scharnier#include <ctype.h>
6029529Scharnier#include <err.h>
611553Srgrimes#include <errno.h>
621553Srgrimes#include <fstab.h>
6329529Scharnier#include <grp.h>
641553Srgrimes#include <pwd.h>
6529529Scharnier#include <signal.h>
661553Srgrimes#include <stdio.h>
6729529Scharnier#include <stdlib.h>
681553Srgrimes#include <string.h>
691553Srgrimes#include <unistd.h>
701553Srgrimes#include "pathnames.h"
711553Srgrimes
7287596Smikehconst char *qfname = QUOTAFILENAME;
7387596Smikehconst char *qfextension[] = INITQFNAMES;
7487596Smikehconst char *quotagroup = QUOTAGROUP;
751553Srgrimeschar tmpfil[] = _PATH_TMP;
761553Srgrimes
771553Srgrimesstruct quotause {
781553Srgrimes	struct	quotause *next;
791553Srgrimes	long	flags;
801553Srgrimes	struct	dqblk dqblk;
811553Srgrimes	char	fsname[MAXPATHLEN + 1];
821553Srgrimes	char	qfname[1];	/* actually longer */
8329529Scharnier};
841553Srgrimes#define	FOUND	0x01
851553Srgrimes
8699800Salfredint alldigits(const char *s);
8799800Salfredint cvtatos(time_t, char *, time_t *);
8899800Salfredchar *cvtstoa(time_t);
8999800Salfredint editit(char *);
9099800Salfredvoid freeprivs(struct quotause *);
9199800Salfredint getentry(const char *, int);
9299800Salfredstruct quotause *getprivs(long, int, char *);
9399800Salfredint hasquota(struct fstab *, int, char **);
9499800Salfredvoid putprivs(long, int, struct quotause *);
9599800Salfredint readprivs(struct quotause *, char *);
9699800Salfredint readtimes(struct quotause *, char *);
9799800Salfredstatic void usage(void);
9899800Salfredint writetimes(struct quotause *, int, int);
9999800Salfredint writeprivs(struct quotause *, int, char *, int);
10029529Scharnier
10129529Scharnierint
10299820Salfredmain(int argc, char **argv)
1031553Srgrimes{
1041553Srgrimes	register struct quotause *qup, *protoprivs, *curprivs;
1051553Srgrimes	register long id, protoid;
1061553Srgrimes	register int quotatype, tmpfd;
10714961Smpp	register uid_t startuid, enduid;
10814961Smpp	char *protoname, *cp, ch;
1091553Srgrimes	int tflag = 0, pflag = 0;
11084081Syar	char *fspath = NULL;
11114961Smpp	char buf[30];
1121553Srgrimes
1131553Srgrimes	if (argc < 2)
1141553Srgrimes		usage();
11529529Scharnier	if (getuid())
11629529Scharnier		errx(1, "permission denied");
1171553Srgrimes	quotatype = USRQUOTA;
11884081Syar	while ((ch = getopt(argc, argv, "ugtf:p:")) != -1) {
1191553Srgrimes		switch(ch) {
12084081Syar		case 'f':
12184081Syar			fspath = optarg;
12284081Syar			break;
1231553Srgrimes		case 'p':
1241553Srgrimes			protoname = optarg;
1251553Srgrimes			pflag++;
1261553Srgrimes			break;
1271553Srgrimes		case 'g':
1281553Srgrimes			quotatype = GRPQUOTA;
1291553Srgrimes			break;
1301553Srgrimes		case 'u':
1311553Srgrimes			quotatype = USRQUOTA;
1321553Srgrimes			break;
1331553Srgrimes		case 't':
1341553Srgrimes			tflag++;
1351553Srgrimes			break;
1361553Srgrimes		default:
1371553Srgrimes			usage();
1381553Srgrimes		}
1391553Srgrimes	}
1401553Srgrimes	argc -= optind;
1411553Srgrimes	argv += optind;
1421553Srgrimes	if (pflag) {
1431553Srgrimes		if ((protoid = getentry(protoname, quotatype)) == -1)
1441553Srgrimes			exit(1);
14584081Syar		protoprivs = getprivs(protoid, quotatype, fspath);
1461553Srgrimes		for (qup = protoprivs; qup; qup = qup->next) {
1471553Srgrimes			qup->dqblk.dqb_btime = 0;
1481553Srgrimes			qup->dqblk.dqb_itime = 0;
1491553Srgrimes		}
150101546Siedowse		for (; argc-- > 0; argv++) {
151101546Siedowse			if (strspn(*argv, "0123456789-") == strlen(*argv) &&
15214961Smpp			    (cp = strchr(*argv, '-')) != NULL) {
15314961Smpp				*cp++ = '\0';
15414961Smpp				startuid = atoi(*argv);
15514961Smpp				enduid = atoi(cp);
15629529Scharnier				if (enduid < startuid)
15729529Scharnier					errx(1,
15829529Scharnier	"ending uid (%d) must be >= starting uid (%d) when using uid ranges",
15914961Smpp						enduid, startuid);
16014961Smpp				for ( ; startuid <= enduid; startuid++) {
16114961Smpp					snprintf(buf, sizeof(buf), "%d",
16214961Smpp					    startuid);
16314961Smpp					if ((id = getentry(buf, quotatype)) < 0)
16414961Smpp						continue;
16514961Smpp					putprivs(id, quotatype, protoprivs);
16614961Smpp				}
16714961Smpp				continue;
16814961Smpp			}
169101546Siedowse			if ((id = getentry(*argv, quotatype)) < 0)
1701553Srgrimes				continue;
1711553Srgrimes			putprivs(id, quotatype, protoprivs);
1721553Srgrimes		}
1731553Srgrimes		exit(0);
1741553Srgrimes	}
1751553Srgrimes	tmpfd = mkstemp(tmpfil);
1761553Srgrimes	fchown(tmpfd, getuid(), getgid());
1771553Srgrimes	if (tflag) {
17884081Syar		protoprivs = getprivs(0, quotatype, fspath);
1791553Srgrimes		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
1801553Srgrimes			exit(1);
1814782Sache		if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
1821553Srgrimes			putprivs(0, quotatype, protoprivs);
1831553Srgrimes		freeprivs(protoprivs);
18428431Sjlemon		close(tmpfd);
18528431Sjlemon		unlink(tmpfil);
1861553Srgrimes		exit(0);
1871553Srgrimes	}
1881553Srgrimes	for ( ; argc > 0; argc--, argv++) {
1891553Srgrimes		if ((id = getentry(*argv, quotatype)) == -1)
1901553Srgrimes			continue;
19184081Syar		curprivs = getprivs(id, quotatype, fspath);
1921553Srgrimes		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
1931553Srgrimes			continue;
1944782Sache		if (editit(tmpfil) && readprivs(curprivs, tmpfil))
1951553Srgrimes			putprivs(id, quotatype, curprivs);
1961553Srgrimes		freeprivs(curprivs);
1971553Srgrimes	}
1981553Srgrimes	close(tmpfd);
1991553Srgrimes	unlink(tmpfil);
2001553Srgrimes	exit(0);
2011553Srgrimes}
2021553Srgrimes
20329529Scharnierstatic void
2041553Srgrimesusage()
2051553Srgrimes{
20629529Scharnier	fprintf(stderr, "%s\n%s\n%s\n%s\n",
20784081Syar		"usage: edquota [-u] [-f fspath] [-p username] username ...",
20884081Syar		"       edquota -g [-f fspath] [-p groupname] groupname ...",
20984081Syar		"       edquota [-u] -t [-f fspath]",
21084081Syar		"       edquota -g -t [-f fspath]");
2111553Srgrimes	exit(1);
2121553Srgrimes}
2131553Srgrimes
2141553Srgrimes/*
2151553Srgrimes * This routine converts a name for a particular quota type to
2161553Srgrimes * an identifier. This routine must agree with the kernel routine
2171553Srgrimes * getinoquota as to the interpretation of quota types.
2181553Srgrimes */
21929529Scharnierint
2201553Srgrimesgetentry(name, quotatype)
22187596Smikeh	const char *name;
2221553Srgrimes	int quotatype;
2231553Srgrimes{
2241553Srgrimes	struct passwd *pw;
2251553Srgrimes	struct group *gr;
2261553Srgrimes
2271553Srgrimes	if (alldigits(name))
2281553Srgrimes		return (atoi(name));
2291553Srgrimes	switch(quotatype) {
2301553Srgrimes	case USRQUOTA:
23129529Scharnier		if ((pw = getpwnam(name)))
2321553Srgrimes			return (pw->pw_uid);
23329529Scharnier		warnx("%s: no such user", name);
2341553Srgrimes		break;
2351553Srgrimes	case GRPQUOTA:
23629529Scharnier		if ((gr = getgrnam(name)))
2371553Srgrimes			return (gr->gr_gid);
23829529Scharnier		warnx("%s: no such group", name);
2391553Srgrimes		break;
2401553Srgrimes	default:
24129529Scharnier		warnx("%d: unknown quota type", quotatype);
2421553Srgrimes		break;
2431553Srgrimes	}
2441553Srgrimes	sleep(1);
2451553Srgrimes	return (-1);
2461553Srgrimes}
2471553Srgrimes
2481553Srgrimes/*
2491553Srgrimes * Collect the requested quota information.
2501553Srgrimes */
2511553Srgrimesstruct quotause *
25284081Syargetprivs(id, quotatype, fspath)
2531553Srgrimes	register long id;
2541553Srgrimes	int quotatype;
25584081Syar	char *fspath;
2561553Srgrimes{
2571553Srgrimes	register struct fstab *fs;
2581553Srgrimes	register struct quotause *qup, *quptail;
2591553Srgrimes	struct quotause *quphead;
2601553Srgrimes	int qcmd, qupsize, fd;
2611553Srgrimes	char *qfpathname;
2621553Srgrimes	static int warned = 0;
2631553Srgrimes
2641553Srgrimes	setfsent();
2651553Srgrimes	quphead = (struct quotause *)0;
2661553Srgrimes	qcmd = QCMD(Q_GETQUOTA, quotatype);
26729529Scharnier	while ((fs = getfsent())) {
26884081Syar		if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
26984081Syar		    strcmp(fspath, fs->fs_file))
27084081Syar			continue;
2711553Srgrimes		if (strcmp(fs->fs_vfstype, "ufs"))
2721553Srgrimes			continue;
2731553Srgrimes		if (!hasquota(fs, quotatype, &qfpathname))
2741553Srgrimes			continue;
2751553Srgrimes		qupsize = sizeof(*qup) + strlen(qfpathname);
27629529Scharnier		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
27729529Scharnier			errx(2, "out of memory");
2781553Srgrimes		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
2791553Srgrimes	    		if (errno == EOPNOTSUPP && !warned) {
2801553Srgrimes				warned++;
28129529Scharnier		warnx("warning: quotas are not compiled into this kernel");
2821553Srgrimes				sleep(3);
2831553Srgrimes			}
2841553Srgrimes			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
2851553Srgrimes				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
2861553Srgrimes				if (fd < 0 && errno != ENOENT) {
28729529Scharnier					warn("%s", qfpathname);
2881553Srgrimes					free(qup);
2891553Srgrimes					continue;
2901553Srgrimes				}
29129529Scharnier				warnx("creating quota file %s", qfpathname);
2921553Srgrimes				sleep(3);
2931553Srgrimes				(void) fchown(fd, getuid(),
2941553Srgrimes				    getentry(quotagroup, GRPQUOTA));
2951553Srgrimes				(void) fchmod(fd, 0640);
2961553Srgrimes			}
2971553Srgrimes			lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
2981553Srgrimes			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
2991553Srgrimes			case 0:			/* EOF */
3001553Srgrimes				/*
3011553Srgrimes				 * Convert implicit 0 quota (EOF)
3021553Srgrimes				 * into an explicit one (zero'ed dqblk)
3031553Srgrimes				 */
3041553Srgrimes				bzero((caddr_t)&qup->dqblk,
3051553Srgrimes				    sizeof(struct dqblk));
3061553Srgrimes				break;
3071553Srgrimes
3081553Srgrimes			case sizeof(struct dqblk):	/* OK */
3091553Srgrimes				break;
3101553Srgrimes
3111553Srgrimes			default:		/* ERROR */
31229529Scharnier				warn("read error in %s", qfpathname);
3131553Srgrimes				close(fd);
3141553Srgrimes				free(qup);
3151553Srgrimes				continue;
3161553Srgrimes			}
3171553Srgrimes			close(fd);
3181553Srgrimes		}
3191553Srgrimes		strcpy(qup->qfname, qfpathname);
3201553Srgrimes		strcpy(qup->fsname, fs->fs_file);
3211553Srgrimes		if (quphead == NULL)
3221553Srgrimes			quphead = qup;
3231553Srgrimes		else
3241553Srgrimes			quptail->next = qup;
3251553Srgrimes		quptail = qup;
3261553Srgrimes		qup->next = 0;
3271553Srgrimes	}
3281553Srgrimes	endfsent();
3291553Srgrimes	return (quphead);
3301553Srgrimes}
3311553Srgrimes
3321553Srgrimes/*
3331553Srgrimes * Store the requested quota information.
3341553Srgrimes */
33529529Scharniervoid
3361553Srgrimesputprivs(id, quotatype, quplist)
3371553Srgrimes	long id;
3381553Srgrimes	int quotatype;
3391553Srgrimes	struct quotause *quplist;
3401553Srgrimes{
3411553Srgrimes	register struct quotause *qup;
3421553Srgrimes	int qcmd, fd;
3431553Srgrimes
3441553Srgrimes	qcmd = QCMD(Q_SETQUOTA, quotatype);
3451553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
3461553Srgrimes		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
3471553Srgrimes			continue;
3481553Srgrimes		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
34929529Scharnier			warn("%s", qup->qfname);
3501553Srgrimes		} else {
3511553Srgrimes			lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
3521553Srgrimes			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
3531553Srgrimes			    sizeof (struct dqblk)) {
35429529Scharnier				warn("%s", qup->qfname);
3551553Srgrimes			}
3561553Srgrimes			close(fd);
3571553Srgrimes		}
3581553Srgrimes	}
3591553Srgrimes}
3601553Srgrimes
3611553Srgrimes/*
3621553Srgrimes * Take a list of priviledges and get it edited.
3631553Srgrimes */
36429529Scharnierint
36587596Smikeheditit(tmpf)
36687596Smikeh	char *tmpf;
3671553Srgrimes{
3681553Srgrimes	long omask;
36987596Smikeh	int pid, status;
3701553Srgrimes
3711553Srgrimes	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
3721553Srgrimes top:
3731553Srgrimes	if ((pid = fork()) < 0) {
3741553Srgrimes
3751553Srgrimes		if (errno == EPROCLIM) {
37629529Scharnier			warnx("you have too many processes");
3771553Srgrimes			return(0);
3781553Srgrimes		}
3791553Srgrimes		if (errno == EAGAIN) {
3801553Srgrimes			sleep(1);
3811553Srgrimes			goto top;
3821553Srgrimes		}
38329529Scharnier		warn("fork");
3841553Srgrimes		return (0);
3851553Srgrimes	}
3861553Srgrimes	if (pid == 0) {
38787596Smikeh		register const char *ed;
3881553Srgrimes
3891553Srgrimes		sigsetmask(omask);
3901553Srgrimes		setgid(getgid());
3911553Srgrimes		setuid(getuid());
3921553Srgrimes		if ((ed = getenv("EDITOR")) == (char *)0)
3931553Srgrimes			ed = _PATH_VI;
39487596Smikeh		execlp(ed, ed, tmpf, (char *)0);
39529529Scharnier		err(1, "%s", ed);
3961553Srgrimes	}
39787596Smikeh	waitpid(pid, &status, 0);
3981553Srgrimes	sigsetmask(omask);
39987596Smikeh	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
4001553Srgrimes		return (0);
4011553Srgrimes	return (1);
4021553Srgrimes}
4031553Srgrimes
4041553Srgrimes/*
4051553Srgrimes * Convert a quotause list to an ASCII file.
4061553Srgrimes */
40729529Scharnierint
4081553Srgrimeswriteprivs(quplist, outfd, name, quotatype)
4091553Srgrimes	struct quotause *quplist;
4101553Srgrimes	int outfd;
4111553Srgrimes	char *name;
4121553Srgrimes	int quotatype;
4131553Srgrimes{
4141553Srgrimes	register struct quotause *qup;
4151553Srgrimes	FILE *fd;
4161553Srgrimes
4171553Srgrimes	ftruncate(outfd, 0);
4181553Srgrimes	lseek(outfd, 0, L_SET);
41929529Scharnier	if ((fd = fdopen(dup(outfd), "w")) == NULL)
42029529Scharnier		err(1, "%s", tmpfil);
4211553Srgrimes	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
4221553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
42367794Sgallatin		fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
4241553Srgrimes		    qup->fsname, "blocks in use:",
4258325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
4268325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
4278325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
42867794Sgallatin		fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
42967794Sgallatin		    "\tinodes in use:",
43067794Sgallatin		    (unsigned long)qup->dqblk.dqb_curinodes,
43167794Sgallatin		    (unsigned long)qup->dqblk.dqb_isoftlimit,
43267794Sgallatin		    (unsigned long)qup->dqblk.dqb_ihardlimit);
4331553Srgrimes	}
4341553Srgrimes	fclose(fd);
4351553Srgrimes	return (1);
4361553Srgrimes}
4371553Srgrimes
4381553Srgrimes/*
4391553Srgrimes * Merge changes to an ASCII file into a quotause list.
4401553Srgrimes */
44129529Scharnierint
4424782Sachereadprivs(quplist, inname)
4431553Srgrimes	struct quotause *quplist;
4444782Sache	char *inname;
4451553Srgrimes{
4461553Srgrimes	register struct quotause *qup;
4471553Srgrimes	FILE *fd;
44867794Sgallatin	unsigned long bhardlimit, bsoftlimit, curblocks;
44967794Sgallatin	unsigned long ihardlimit, isoftlimit, curinodes;
4501553Srgrimes	int cnt;
4511553Srgrimes	register char *cp;
4521553Srgrimes	struct dqblk dqblk;
4531553Srgrimes	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
4541553Srgrimes
4554782Sache	fd = fopen(inname, "r");
4561553Srgrimes	if (fd == NULL) {
45729529Scharnier		warnx("can't re-read temp file!!");
4581553Srgrimes		return (0);
4591553Srgrimes	}
4601553Srgrimes	/*
4611553Srgrimes	 * Discard title line, then read pairs of lines to process.
4621553Srgrimes	 */
4631553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
4641553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL &&
4651553Srgrimes	       fgets(line2, sizeof (line2), fd) != NULL) {
4661553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
46729529Scharnier			warnx("%s: bad format", line1);
4681553Srgrimes			return (0);
4691553Srgrimes		}
4701553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
47129529Scharnier			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
4721553Srgrimes			return (0);
4731553Srgrimes		}
4741553Srgrimes		cnt = sscanf(cp,
47567794Sgallatin		    " blocks in use: %lu, limits (soft = %lu, hard = %lu)",
47667794Sgallatin		    &curblocks, &bsoftlimit, &bhardlimit);
4771553Srgrimes		if (cnt != 3) {
47829529Scharnier			warnx("%s:%s: bad format", fsp, cp);
4791553Srgrimes			return (0);
4801553Srgrimes		}
48167794Sgallatin		dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
48267794Sgallatin		dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
48367794Sgallatin		dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
4841553Srgrimes		if ((cp = strtok(line2, "\n")) == NULL) {
48529529Scharnier			warnx("%s: %s: bad format", fsp, line2);
4861553Srgrimes			return (0);
4871553Srgrimes		}
4881553Srgrimes		cnt = sscanf(cp,
48967794Sgallatin		    "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
49067794Sgallatin		    &curinodes, &isoftlimit, &ihardlimit);
4911553Srgrimes		if (cnt != 3) {
49229529Scharnier			warnx("%s: %s: bad format", fsp, line2);
4931553Srgrimes			return (0);
4941553Srgrimes		}
49567794Sgallatin		dqblk.dqb_curinodes = curinodes;
49667794Sgallatin		dqblk.dqb_isoftlimit = isoftlimit;
49767794Sgallatin		dqblk.dqb_ihardlimit = ihardlimit;
4981553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
4991553Srgrimes			if (strcmp(fsp, qup->fsname))
5001553Srgrimes				continue;
5011553Srgrimes			/*
5021553Srgrimes			 * Cause time limit to be reset when the quota
5031553Srgrimes			 * is next used if previously had no soft limit
5041553Srgrimes			 * or were under it, but now have a soft limit
5051553Srgrimes			 * and are over it.
5061553Srgrimes			 */
5071553Srgrimes			if (dqblk.dqb_bsoftlimit &&
5081553Srgrimes			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
5091553Srgrimes			    (qup->dqblk.dqb_bsoftlimit == 0 ||
5101553Srgrimes			     qup->dqblk.dqb_curblocks <
5111553Srgrimes			     qup->dqblk.dqb_bsoftlimit))
5121553Srgrimes				qup->dqblk.dqb_btime = 0;
5131553Srgrimes			if (dqblk.dqb_isoftlimit &&
5141553Srgrimes			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
5151553Srgrimes			    (qup->dqblk.dqb_isoftlimit == 0 ||
5161553Srgrimes			     qup->dqblk.dqb_curinodes <
5171553Srgrimes			     qup->dqblk.dqb_isoftlimit))
5181553Srgrimes				qup->dqblk.dqb_itime = 0;
5191553Srgrimes			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
5201553Srgrimes			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
5211553Srgrimes			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
5221553Srgrimes			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
5231553Srgrimes			qup->flags |= FOUND;
5241553Srgrimes			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
5251553Srgrimes			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
5261553Srgrimes				break;
52729529Scharnier			warnx("%s: cannot change current allocation", fsp);
5281553Srgrimes			break;
5291553Srgrimes		}
5301553Srgrimes	}
5311553Srgrimes	fclose(fd);
5321553Srgrimes	/*
5331553Srgrimes	 * Disable quotas for any filesystems that have not been found.
5341553Srgrimes	 */
5351553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5361553Srgrimes		if (qup->flags & FOUND) {
5371553Srgrimes			qup->flags &= ~FOUND;
5381553Srgrimes			continue;
5391553Srgrimes		}
5401553Srgrimes		qup->dqblk.dqb_bsoftlimit = 0;
5411553Srgrimes		qup->dqblk.dqb_bhardlimit = 0;
5421553Srgrimes		qup->dqblk.dqb_isoftlimit = 0;
5431553Srgrimes		qup->dqblk.dqb_ihardlimit = 0;
5441553Srgrimes	}
5451553Srgrimes	return (1);
5461553Srgrimes}
5471553Srgrimes
5481553Srgrimes/*
5491553Srgrimes * Convert a quotause list to an ASCII file of grace times.
5501553Srgrimes */
55129529Scharnierint
5521553Srgrimeswritetimes(quplist, outfd, quotatype)
5531553Srgrimes	struct quotause *quplist;
5541553Srgrimes	int outfd;
5551553Srgrimes	int quotatype;
5561553Srgrimes{
5571553Srgrimes	register struct quotause *qup;
5581553Srgrimes	FILE *fd;
5591553Srgrimes
5601553Srgrimes	ftruncate(outfd, 0);
5611553Srgrimes	lseek(outfd, 0, L_SET);
56229529Scharnier	if ((fd = fdopen(dup(outfd), "w")) == NULL)
56329529Scharnier		err(1, "%s", tmpfil);
5641553Srgrimes	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
5651553Srgrimes	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
5661553Srgrimes	    qfextension[quotatype]);
5671553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
5681553Srgrimes		fprintf(fd, "%s: block grace period: %s, ",
5691553Srgrimes		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
5701553Srgrimes		fprintf(fd, "file grace period: %s\n",
5711553Srgrimes		    cvtstoa(qup->dqblk.dqb_itime));
5721553Srgrimes	}
5731553Srgrimes	fclose(fd);
5741553Srgrimes	return (1);
5751553Srgrimes}
5761553Srgrimes
5771553Srgrimes/*
5781553Srgrimes * Merge changes of grace times in an ASCII file into a quotause list.
5791553Srgrimes */
58029529Scharnierint
5814782Sachereadtimes(quplist, inname)
5821553Srgrimes	struct quotause *quplist;
5834782Sache	char *inname;
5841553Srgrimes{
5851553Srgrimes	register struct quotause *qup;
5861553Srgrimes	FILE *fd;
5871553Srgrimes	int cnt;
5881553Srgrimes	register char *cp;
5891553Srgrimes	time_t itime, btime, iseconds, bseconds;
59067794Sgallatin	long l_itime, l_btime;
5911553Srgrimes	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
5921553Srgrimes
5934782Sache	fd = fopen(inname, "r");
5941553Srgrimes	if (fd == NULL) {
59529529Scharnier		warnx("can't re-read temp file!!");
5961553Srgrimes		return (0);
5971553Srgrimes	}
5981553Srgrimes	/*
5991553Srgrimes	 * Discard two title lines, then read lines to process.
6001553Srgrimes	 */
6011553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
6021553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
6031553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL) {
6041553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
60529529Scharnier			warnx("%s: bad format", line1);
6061553Srgrimes			return (0);
6071553Srgrimes		}
6081553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
60929529Scharnier			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
6101553Srgrimes			return (0);
6111553Srgrimes		}
6121553Srgrimes		cnt = sscanf(cp,
6138325Sbde		    " block grace period: %ld %s file grace period: %ld %s",
61467794Sgallatin		    &l_btime, bunits, &l_itime, iunits);
6151553Srgrimes		if (cnt != 4) {
61629529Scharnier			warnx("%s:%s: bad format", fsp, cp);
6171553Srgrimes			return (0);
6181553Srgrimes		}
61967794Sgallatin		btime = l_btime;
62067794Sgallatin		itime = l_itime;
6211553Srgrimes		if (cvtatos(btime, bunits, &bseconds) == 0)
6221553Srgrimes			return (0);
6231553Srgrimes		if (cvtatos(itime, iunits, &iseconds) == 0)
6241553Srgrimes			return (0);
6251553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
6261553Srgrimes			if (strcmp(fsp, qup->fsname))
6271553Srgrimes				continue;
6281553Srgrimes			qup->dqblk.dqb_btime = bseconds;
6291553Srgrimes			qup->dqblk.dqb_itime = iseconds;
6301553Srgrimes			qup->flags |= FOUND;
6311553Srgrimes			break;
6321553Srgrimes		}
6331553Srgrimes	}
6341553Srgrimes	fclose(fd);
6351553Srgrimes	/*
6361553Srgrimes	 * reset default grace periods for any filesystems
6371553Srgrimes	 * that have not been found.
6381553Srgrimes	 */
6391553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
6401553Srgrimes		if (qup->flags & FOUND) {
6411553Srgrimes			qup->flags &= ~FOUND;
6421553Srgrimes			continue;
6431553Srgrimes		}
6441553Srgrimes		qup->dqblk.dqb_btime = 0;
6451553Srgrimes		qup->dqblk.dqb_itime = 0;
6461553Srgrimes	}
6471553Srgrimes	return (1);
6481553Srgrimes}
6491553Srgrimes
6501553Srgrimes/*
6511553Srgrimes * Convert seconds to ASCII times.
6521553Srgrimes */
6531553Srgrimeschar *
65487596Smikehcvtstoa(secs)
65587596Smikeh	time_t secs;
6561553Srgrimes{
6571553Srgrimes	static char buf[20];
6581553Srgrimes
65987596Smikeh	if (secs % (24 * 60 * 60) == 0) {
66087596Smikeh		secs /= 24 * 60 * 60;
66187596Smikeh		sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
66287596Smikeh	} else if (secs % (60 * 60) == 0) {
66387596Smikeh		secs /= 60 * 60;
66487596Smikeh		sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
66587596Smikeh	} else if (secs % 60 == 0) {
66687596Smikeh		secs /= 60;
66787596Smikeh		sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
6681553Srgrimes	} else
66987596Smikeh		sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
6701553Srgrimes	return (buf);
6711553Srgrimes}
6721553Srgrimes
6731553Srgrimes/*
6741553Srgrimes * Convert ASCII input times to seconds.
6751553Srgrimes */
67629529Scharnierint
67787596Smikehcvtatos(period, units, seconds)
67887596Smikeh	time_t period;
6791553Srgrimes	char *units;
6801553Srgrimes	time_t *seconds;
6811553Srgrimes{
6821553Srgrimes
6831553Srgrimes	if (bcmp(units, "second", 6) == 0)
68487596Smikeh		*seconds = period;
6851553Srgrimes	else if (bcmp(units, "minute", 6) == 0)
68687596Smikeh		*seconds = period * 60;
6871553Srgrimes	else if (bcmp(units, "hour", 4) == 0)
68887596Smikeh		*seconds = period * 60 * 60;
6891553Srgrimes	else if (bcmp(units, "day", 3) == 0)
69087596Smikeh		*seconds = period * 24 * 60 * 60;
6911553Srgrimes	else {
6921553Srgrimes		printf("%s: bad units, specify %s\n", units,
6931553Srgrimes		    "days, hours, minutes, or seconds");
6941553Srgrimes		return (0);
6951553Srgrimes	}
6961553Srgrimes	return (1);
6971553Srgrimes}
6981553Srgrimes
6991553Srgrimes/*
7001553Srgrimes * Free a list of quotause structures.
7011553Srgrimes */
70229529Scharniervoid
7031553Srgrimesfreeprivs(quplist)
7041553Srgrimes	struct quotause *quplist;
7051553Srgrimes{
7061553Srgrimes	register struct quotause *qup, *nextqup;
7071553Srgrimes
7081553Srgrimes	for (qup = quplist; qup; qup = nextqup) {
7091553Srgrimes		nextqup = qup->next;
7101553Srgrimes		free(qup);
7111553Srgrimes	}
7121553Srgrimes}
7131553Srgrimes
7141553Srgrimes/*
7151553Srgrimes * Check whether a string is completely composed of digits.
7161553Srgrimes */
71729529Scharnierint
7181553Srgrimesalldigits(s)
71987596Smikeh	register const char *s;
7201553Srgrimes{
72187596Smikeh	register int c;
7221553Srgrimes
7231553Srgrimes	c = *s++;
7241553Srgrimes	do {
7251553Srgrimes		if (!isdigit(c))
7261553Srgrimes			return (0);
72729529Scharnier	} while ((c = *s++));
7281553Srgrimes	return (1);
7291553Srgrimes}
7301553Srgrimes
7311553Srgrimes/*
7321553Srgrimes * Check to see if a particular quota is to be enabled.
7331553Srgrimes */
73429529Scharnierint
7351553Srgrimeshasquota(fs, type, qfnamep)
7361553Srgrimes	register struct fstab *fs;
7371553Srgrimes	int type;
7381553Srgrimes	char **qfnamep;
7391553Srgrimes{
7401553Srgrimes	register char *opt;
74129529Scharnier	char *cp;
7421553Srgrimes	static char initname, usrname[100], grpname[100];
7431553Srgrimes	static char buf[BUFSIZ];
7441553Srgrimes
7451553Srgrimes	if (!initname) {
7461553Srgrimes		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
7471553Srgrimes		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
7481553Srgrimes		initname = 1;
7491553Srgrimes	}
7501553Srgrimes	strcpy(buf, fs->fs_mntops);
7511553Srgrimes	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
75229529Scharnier		if ((cp = index(opt, '=')))
7531553Srgrimes			*cp++ = '\0';
7541553Srgrimes		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
7551553Srgrimes			break;
7561553Srgrimes		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
7571553Srgrimes			break;
7581553Srgrimes	}
7591553Srgrimes	if (!opt)
7601553Srgrimes		return (0);
7611553Srgrimes	if (cp) {
7621553Srgrimes		*qfnamep = cp;
7631553Srgrimes		return (1);
7641553Srgrimes	}
7651553Srgrimes	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
7661553Srgrimes	*qfnamep = buf;
7671553Srgrimes	return (1);
7681553Srgrimes}
769