edquota.c revision 114601
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
37114601Sobrien#if 0
381553Srgrimes#ifndef lint
3929529Scharnierstatic const char copyright[] =
401553Srgrimes"@(#) Copyright (c) 1980, 1990, 1993\n\
411553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
421553Srgrimes#endif /* not lint */
431553Srgrimes
441553Srgrimes#ifndef lint
451553Srgrimesstatic char sccsid[] = "@(#)edquota.c	8.1 (Berkeley) 6/6/93";
46114601Sobrien#endif /* not lint */
4729529Scharnier#endif
48114601Sobrien#include <sys/cdefs.h>
49114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/edquota/edquota.c 114601 2003-05-03 21:06:42Z obrien $");
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{
104103071Ssobomax	struct quotause *qup, *protoprivs, *curprivs;
105103071Ssobomax	long id, protoid;
106103071Ssobomax	long long lim;
107103071Ssobomax	int i, quotatype, range, tmpfd;
108103071Ssobomax	uid_t startuid, enduid;
109103071Ssobomax	u_int32_t *limp;
110103071Ssobomax	char *protoname, *cp, *oldoptarg, ch;
111103071Ssobomax	int eflag = 0, tflag = 0, pflag = 0;
11284081Syar	char *fspath = NULL;
11314961Smpp	char buf[30];
1141553Srgrimes
1151553Srgrimes	if (argc < 2)
1161553Srgrimes		usage();
11729529Scharnier	if (getuid())
11829529Scharnier		errx(1, "permission denied");
1191553Srgrimes	quotatype = USRQUOTA;
120103071Ssobomax	protoprivs = NULL;
121103071Ssobomax	while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
1221553Srgrimes		switch(ch) {
12384081Syar		case 'f':
12484081Syar			fspath = optarg;
12584081Syar			break;
1261553Srgrimes		case 'p':
1271553Srgrimes			protoname = optarg;
1281553Srgrimes			pflag++;
1291553Srgrimes			break;
1301553Srgrimes		case 'g':
1311553Srgrimes			quotatype = GRPQUOTA;
1321553Srgrimes			break;
1331553Srgrimes		case 'u':
1341553Srgrimes			quotatype = USRQUOTA;
1351553Srgrimes			break;
1361553Srgrimes		case 't':
1371553Srgrimes			tflag++;
1381553Srgrimes			break;
139103071Ssobomax		case 'e':
140103071Ssobomax			if ((qup = malloc(sizeof(*qup))) == NULL)
141103071Ssobomax				errx(2, "out of memory");
142103071Ssobomax			bzero(qup, sizeof(*qup));
143103071Ssobomax			i = 0;
144103071Ssobomax			oldoptarg = optarg;
145103071Ssobomax			for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
146103071Ssobomax			    i++) {
147103071Ssobomax				if (cp != oldoptarg)
148103071Ssobomax					*(cp - 1) = ':';
149103071Ssobomax				limp = NULL;
150103071Ssobomax				switch (i) {
151103071Ssobomax				case 0:
152103071Ssobomax					strlcpy(qup->fsname, cp,
153103071Ssobomax					    sizeof(qup->fsname));
154103071Ssobomax					break;
155103071Ssobomax				case 1:
156103071Ssobomax					limp = &qup->dqblk.dqb_bsoftlimit;
157103071Ssobomax					break;
158103071Ssobomax				case 2:
159103071Ssobomax					limp = &qup->dqblk.dqb_bhardlimit;
160103071Ssobomax					break;
161103071Ssobomax				case 3:
162103071Ssobomax					limp = &qup->dqblk.dqb_isoftlimit;
163103071Ssobomax					break;
164103071Ssobomax				case 4:
165103071Ssobomax					limp = &qup->dqblk.dqb_ihardlimit;
166103071Ssobomax					break;
167103071Ssobomax				default:
168103071Ssobomax					warnx("incorrect quota specification: "
169103071Ssobomax					    "%s", oldoptarg);
170103071Ssobomax					usage();
171103071Ssobomax					break; /* XXX: report an error */
172103071Ssobomax				}
173103071Ssobomax				if (limp != NULL) {
174103071Ssobomax					lim = strtoll(cp, NULL, 10);
175103071Ssobomax					if (lim < 0 || lim > UINT_MAX)
176103071Ssobomax						errx(1, "invalid limit value: "
177103071Ssobomax						    "%lld", lim);
178103071Ssobomax					*limp = (u_int32_t)lim;
179103071Ssobomax				}
180103071Ssobomax			}
181103071Ssobomax			qup->dqblk.dqb_bsoftlimit =
182103071Ssobomax			    btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
183103071Ssobomax			qup->dqblk.dqb_bhardlimit =
184103071Ssobomax			    btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
185103071Ssobomax			if (protoprivs == NULL) {
186103071Ssobomax				protoprivs = curprivs = qup;
187103071Ssobomax			} else {
188103071Ssobomax				curprivs->next = qup;
189103071Ssobomax				curprivs = qup;
190103071Ssobomax			}
191103071Ssobomax			eflag++;
192103071Ssobomax			pflag++;
193103071Ssobomax			break;
1941553Srgrimes		default:
1951553Srgrimes			usage();
1961553Srgrimes		}
1971553Srgrimes	}
1981553Srgrimes	argc -= optind;
1991553Srgrimes	argv += optind;
2001553Srgrimes	if (pflag) {
201103071Ssobomax		if (protoprivs == NULL) {
202103071Ssobomax			if ((protoid = getentry(protoname, quotatype)) == -1)
203103071Ssobomax				exit(1);
204103071Ssobomax			protoprivs = getprivs(protoid, quotatype, fspath);
205103071Ssobomax			for (qup = protoprivs; qup; qup = qup->next) {
206103071Ssobomax				qup->dqblk.dqb_btime = 0;
207103071Ssobomax				qup->dqblk.dqb_itime = 0;
208103071Ssobomax			}
2091553Srgrimes		}
210101546Siedowse		for (; argc-- > 0; argv++) {
211101546Siedowse			if (strspn(*argv, "0123456789-") == strlen(*argv) &&
21214961Smpp			    (cp = strchr(*argv, '-')) != NULL) {
21314961Smpp				*cp++ = '\0';
21414961Smpp				startuid = atoi(*argv);
21514961Smpp				enduid = atoi(cp);
21629529Scharnier				if (enduid < startuid)
21729529Scharnier					errx(1,
21829529Scharnier	"ending uid (%d) must be >= starting uid (%d) when using uid ranges",
21914961Smpp						enduid, startuid);
220103071Ssobomax				range = 1;
221103071Ssobomax			} else {
222103071Ssobomax				startuid = enduid = 0;
223103071Ssobomax				range = 0;
224103071Ssobomax			}
225103071Ssobomax			for ( ; startuid <= enduid; startuid++) {
226103071Ssobomax				if (range)
227103071Ssobomax					snprintf(buf, sizeof(buf), "%d",
22814961Smpp					    startuid);
229103071Ssobomax				else
230103071Ssobomax					snprintf(buf, sizeof(buf), "%s",
231103071Ssobomax						*argv);
232103071Ssobomax				if ((id = getentry(buf, quotatype)) < 0)
233103071Ssobomax					continue;
234103071Ssobomax				if (eflag) {
235103071Ssobomax					for (qup = protoprivs; qup;
236103071Ssobomax					    qup = qup->next) {
237103071Ssobomax						curprivs = getprivs(id,
238103071Ssobomax						    quotatype, qup->fsname);
239103071Ssobomax						if (curprivs == NULL)
240103071Ssobomax							continue;
241103071Ssobomax						strcpy(qup->qfname,
242103071Ssobomax						    curprivs->qfname);
243103071Ssobomax						strcpy(qup->fsname,
244103071Ssobomax						    curprivs->fsname);
245103071Ssobomax					}
24614961Smpp				}
247103071Ssobomax				putprivs(id, quotatype, protoprivs);
24814961Smpp			}
2491553Srgrimes		}
2501553Srgrimes		exit(0);
2511553Srgrimes	}
2521553Srgrimes	tmpfd = mkstemp(tmpfil);
2531553Srgrimes	fchown(tmpfd, getuid(), getgid());
2541553Srgrimes	if (tflag) {
25584081Syar		protoprivs = getprivs(0, quotatype, fspath);
2561553Srgrimes		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
2571553Srgrimes			exit(1);
2584782Sache		if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
2591553Srgrimes			putprivs(0, quotatype, protoprivs);
2601553Srgrimes		freeprivs(protoprivs);
26128431Sjlemon		close(tmpfd);
26228431Sjlemon		unlink(tmpfil);
2631553Srgrimes		exit(0);
2641553Srgrimes	}
2651553Srgrimes	for ( ; argc > 0; argc--, argv++) {
2661553Srgrimes		if ((id = getentry(*argv, quotatype)) == -1)
2671553Srgrimes			continue;
26884081Syar		curprivs = getprivs(id, quotatype, fspath);
2691553Srgrimes		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
2701553Srgrimes			continue;
2714782Sache		if (editit(tmpfil) && readprivs(curprivs, tmpfil))
2721553Srgrimes			putprivs(id, quotatype, curprivs);
2731553Srgrimes		freeprivs(curprivs);
2741553Srgrimes	}
2751553Srgrimes	close(tmpfd);
2761553Srgrimes	unlink(tmpfil);
2771553Srgrimes	exit(0);
2781553Srgrimes}
2791553Srgrimes
28029529Scharnierstatic void
2811553Srgrimesusage()
2821553Srgrimes{
283103071Ssobomax	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
28484081Syar		"usage: edquota [-u] [-f fspath] [-p username] username ...",
285103071Ssobomax		"       edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
286103071Ssobomax		"               username ...",
28784081Syar		"       edquota -g [-f fspath] [-p groupname] groupname ...",
288103071Ssobomax		"       edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
289103071Ssobomax		"               groupname ...",
29084081Syar		"       edquota [-u] -t [-f fspath]",
29184081Syar		"       edquota -g -t [-f fspath]");
2921553Srgrimes	exit(1);
2931553Srgrimes}
2941553Srgrimes
2951553Srgrimes/*
2961553Srgrimes * This routine converts a name for a particular quota type to
2971553Srgrimes * an identifier. This routine must agree with the kernel routine
2981553Srgrimes * getinoquota as to the interpretation of quota types.
2991553Srgrimes */
30029529Scharnierint
3011553Srgrimesgetentry(name, quotatype)
30287596Smikeh	const char *name;
3031553Srgrimes	int quotatype;
3041553Srgrimes{
3051553Srgrimes	struct passwd *pw;
3061553Srgrimes	struct group *gr;
3071553Srgrimes
3081553Srgrimes	if (alldigits(name))
3091553Srgrimes		return (atoi(name));
3101553Srgrimes	switch(quotatype) {
3111553Srgrimes	case USRQUOTA:
31229529Scharnier		if ((pw = getpwnam(name)))
3131553Srgrimes			return (pw->pw_uid);
31429529Scharnier		warnx("%s: no such user", name);
3151553Srgrimes		break;
3161553Srgrimes	case GRPQUOTA:
31729529Scharnier		if ((gr = getgrnam(name)))
3181553Srgrimes			return (gr->gr_gid);
31929529Scharnier		warnx("%s: no such group", name);
3201553Srgrimes		break;
3211553Srgrimes	default:
32229529Scharnier		warnx("%d: unknown quota type", quotatype);
3231553Srgrimes		break;
3241553Srgrimes	}
3251553Srgrimes	sleep(1);
3261553Srgrimes	return (-1);
3271553Srgrimes}
3281553Srgrimes
3291553Srgrimes/*
3301553Srgrimes * Collect the requested quota information.
3311553Srgrimes */
3321553Srgrimesstruct quotause *
33384081Syargetprivs(id, quotatype, fspath)
3341553Srgrimes	register long id;
3351553Srgrimes	int quotatype;
33684081Syar	char *fspath;
3371553Srgrimes{
3381553Srgrimes	register struct fstab *fs;
3391553Srgrimes	register struct quotause *qup, *quptail;
3401553Srgrimes	struct quotause *quphead;
3411553Srgrimes	int qcmd, qupsize, fd;
3421553Srgrimes	char *qfpathname;
3431553Srgrimes	static int warned = 0;
3441553Srgrimes
3451553Srgrimes	setfsent();
3461553Srgrimes	quphead = (struct quotause *)0;
3471553Srgrimes	qcmd = QCMD(Q_GETQUOTA, quotatype);
34829529Scharnier	while ((fs = getfsent())) {
34984081Syar		if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
35084081Syar		    strcmp(fspath, fs->fs_file))
35184081Syar			continue;
3521553Srgrimes		if (strcmp(fs->fs_vfstype, "ufs"))
3531553Srgrimes			continue;
3541553Srgrimes		if (!hasquota(fs, quotatype, &qfpathname))
3551553Srgrimes			continue;
3561553Srgrimes		qupsize = sizeof(*qup) + strlen(qfpathname);
35729529Scharnier		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
35829529Scharnier			errx(2, "out of memory");
3591553Srgrimes		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
3601553Srgrimes	    		if (errno == EOPNOTSUPP && !warned) {
3611553Srgrimes				warned++;
36229529Scharnier		warnx("warning: quotas are not compiled into this kernel");
3631553Srgrimes				sleep(3);
3641553Srgrimes			}
3651553Srgrimes			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
3661553Srgrimes				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
3671553Srgrimes				if (fd < 0 && errno != ENOENT) {
36829529Scharnier					warn("%s", qfpathname);
3691553Srgrimes					free(qup);
3701553Srgrimes					continue;
3711553Srgrimes				}
37229529Scharnier				warnx("creating quota file %s", qfpathname);
3731553Srgrimes				sleep(3);
3741553Srgrimes				(void) fchown(fd, getuid(),
3751553Srgrimes				    getentry(quotagroup, GRPQUOTA));
3761553Srgrimes				(void) fchmod(fd, 0640);
3771553Srgrimes			}
3781553Srgrimes			lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
3791553Srgrimes			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
3801553Srgrimes			case 0:			/* EOF */
3811553Srgrimes				/*
3821553Srgrimes				 * Convert implicit 0 quota (EOF)
3831553Srgrimes				 * into an explicit one (zero'ed dqblk)
3841553Srgrimes				 */
3851553Srgrimes				bzero((caddr_t)&qup->dqblk,
3861553Srgrimes				    sizeof(struct dqblk));
3871553Srgrimes				break;
3881553Srgrimes
3891553Srgrimes			case sizeof(struct dqblk):	/* OK */
3901553Srgrimes				break;
3911553Srgrimes
3921553Srgrimes			default:		/* ERROR */
39329529Scharnier				warn("read error in %s", qfpathname);
3941553Srgrimes				close(fd);
3951553Srgrimes				free(qup);
3961553Srgrimes				continue;
3971553Srgrimes			}
3981553Srgrimes			close(fd);
3991553Srgrimes		}
4001553Srgrimes		strcpy(qup->qfname, qfpathname);
4011553Srgrimes		strcpy(qup->fsname, fs->fs_file);
4021553Srgrimes		if (quphead == NULL)
4031553Srgrimes			quphead = qup;
4041553Srgrimes		else
4051553Srgrimes			quptail->next = qup;
4061553Srgrimes		quptail = qup;
4071553Srgrimes		qup->next = 0;
4081553Srgrimes	}
4091553Srgrimes	endfsent();
4101553Srgrimes	return (quphead);
4111553Srgrimes}
4121553Srgrimes
4131553Srgrimes/*
4141553Srgrimes * Store the requested quota information.
4151553Srgrimes */
41629529Scharniervoid
4171553Srgrimesputprivs(id, quotatype, quplist)
4181553Srgrimes	long id;
4191553Srgrimes	int quotatype;
4201553Srgrimes	struct quotause *quplist;
4211553Srgrimes{
4221553Srgrimes	register struct quotause *qup;
4231553Srgrimes	int qcmd, fd;
4241553Srgrimes
4251553Srgrimes	qcmd = QCMD(Q_SETQUOTA, quotatype);
4261553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
4271553Srgrimes		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
4281553Srgrimes			continue;
4291553Srgrimes		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
43029529Scharnier			warn("%s", qup->qfname);
4311553Srgrimes		} else {
4321553Srgrimes			lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
4331553Srgrimes			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
4341553Srgrimes			    sizeof (struct dqblk)) {
43529529Scharnier				warn("%s", qup->qfname);
4361553Srgrimes			}
4371553Srgrimes			close(fd);
4381553Srgrimes		}
4391553Srgrimes	}
4401553Srgrimes}
4411553Srgrimes
4421553Srgrimes/*
4431553Srgrimes * Take a list of priviledges and get it edited.
4441553Srgrimes */
44529529Scharnierint
44687596Smikeheditit(tmpf)
44787596Smikeh	char *tmpf;
4481553Srgrimes{
4491553Srgrimes	long omask;
45087596Smikeh	int pid, status;
4511553Srgrimes
4521553Srgrimes	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
4531553Srgrimes top:
4541553Srgrimes	if ((pid = fork()) < 0) {
4551553Srgrimes
4561553Srgrimes		if (errno == EPROCLIM) {
45729529Scharnier			warnx("you have too many processes");
4581553Srgrimes			return(0);
4591553Srgrimes		}
4601553Srgrimes		if (errno == EAGAIN) {
4611553Srgrimes			sleep(1);
4621553Srgrimes			goto top;
4631553Srgrimes		}
46429529Scharnier		warn("fork");
4651553Srgrimes		return (0);
4661553Srgrimes	}
4671553Srgrimes	if (pid == 0) {
46887596Smikeh		register const char *ed;
4691553Srgrimes
4701553Srgrimes		sigsetmask(omask);
4711553Srgrimes		setgid(getgid());
4721553Srgrimes		setuid(getuid());
4731553Srgrimes		if ((ed = getenv("EDITOR")) == (char *)0)
4741553Srgrimes			ed = _PATH_VI;
47587596Smikeh		execlp(ed, ed, tmpf, (char *)0);
47629529Scharnier		err(1, "%s", ed);
4771553Srgrimes	}
47887596Smikeh	waitpid(pid, &status, 0);
4791553Srgrimes	sigsetmask(omask);
48087596Smikeh	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
4811553Srgrimes		return (0);
4821553Srgrimes	return (1);
4831553Srgrimes}
4841553Srgrimes
4851553Srgrimes/*
4861553Srgrimes * Convert a quotause list to an ASCII file.
4871553Srgrimes */
48829529Scharnierint
4891553Srgrimeswriteprivs(quplist, outfd, name, quotatype)
4901553Srgrimes	struct quotause *quplist;
4911553Srgrimes	int outfd;
4921553Srgrimes	char *name;
4931553Srgrimes	int quotatype;
4941553Srgrimes{
4951553Srgrimes	register struct quotause *qup;
4961553Srgrimes	FILE *fd;
4971553Srgrimes
4981553Srgrimes	ftruncate(outfd, 0);
4991553Srgrimes	lseek(outfd, 0, L_SET);
50029529Scharnier	if ((fd = fdopen(dup(outfd), "w")) == NULL)
50129529Scharnier		err(1, "%s", tmpfil);
5021553Srgrimes	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
5031553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
50467794Sgallatin		fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
505102362Sschweikh		    qup->fsname, "kbytes in use:",
5068325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
5078325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
5088325Sbde		    (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
50967794Sgallatin		fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
51067794Sgallatin		    "\tinodes in use:",
51167794Sgallatin		    (unsigned long)qup->dqblk.dqb_curinodes,
51267794Sgallatin		    (unsigned long)qup->dqblk.dqb_isoftlimit,
51367794Sgallatin		    (unsigned long)qup->dqblk.dqb_ihardlimit);
5141553Srgrimes	}
5151553Srgrimes	fclose(fd);
5161553Srgrimes	return (1);
5171553Srgrimes}
5181553Srgrimes
5191553Srgrimes/*
5201553Srgrimes * Merge changes to an ASCII file into a quotause list.
5211553Srgrimes */
52229529Scharnierint
5234782Sachereadprivs(quplist, inname)
5241553Srgrimes	struct quotause *quplist;
5254782Sache	char *inname;
5261553Srgrimes{
5271553Srgrimes	register struct quotause *qup;
5281553Srgrimes	FILE *fd;
52967794Sgallatin	unsigned long bhardlimit, bsoftlimit, curblocks;
53067794Sgallatin	unsigned long ihardlimit, isoftlimit, curinodes;
5311553Srgrimes	int cnt;
5321553Srgrimes	register char *cp;
5331553Srgrimes	struct dqblk dqblk;
5341553Srgrimes	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
5351553Srgrimes
5364782Sache	fd = fopen(inname, "r");
5371553Srgrimes	if (fd == NULL) {
53829529Scharnier		warnx("can't re-read temp file!!");
5391553Srgrimes		return (0);
5401553Srgrimes	}
5411553Srgrimes	/*
5421553Srgrimes	 * Discard title line, then read pairs of lines to process.
5431553Srgrimes	 */
5441553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
5451553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL &&
5461553Srgrimes	       fgets(line2, sizeof (line2), fd) != NULL) {
5471553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
54829529Scharnier			warnx("%s: bad format", line1);
5491553Srgrimes			return (0);
5501553Srgrimes		}
5511553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
55229529Scharnier			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
5531553Srgrimes			return (0);
5541553Srgrimes		}
5551553Srgrimes		cnt = sscanf(cp,
556102362Sschweikh		    " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
55767794Sgallatin		    &curblocks, &bsoftlimit, &bhardlimit);
5581553Srgrimes		if (cnt != 3) {
55929529Scharnier			warnx("%s:%s: bad format", fsp, cp);
5601553Srgrimes			return (0);
5611553Srgrimes		}
56267794Sgallatin		dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
56367794Sgallatin		dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
56467794Sgallatin		dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
5651553Srgrimes		if ((cp = strtok(line2, "\n")) == NULL) {
56629529Scharnier			warnx("%s: %s: bad format", fsp, line2);
5671553Srgrimes			return (0);
5681553Srgrimes		}
5691553Srgrimes		cnt = sscanf(cp,
57067794Sgallatin		    "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
57167794Sgallatin		    &curinodes, &isoftlimit, &ihardlimit);
5721553Srgrimes		if (cnt != 3) {
57329529Scharnier			warnx("%s: %s: bad format", fsp, line2);
5741553Srgrimes			return (0);
5751553Srgrimes		}
57667794Sgallatin		dqblk.dqb_curinodes = curinodes;
57767794Sgallatin		dqblk.dqb_isoftlimit = isoftlimit;
57867794Sgallatin		dqblk.dqb_ihardlimit = ihardlimit;
5791553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
5801553Srgrimes			if (strcmp(fsp, qup->fsname))
5811553Srgrimes				continue;
5821553Srgrimes			/*
5831553Srgrimes			 * Cause time limit to be reset when the quota
5841553Srgrimes			 * is next used if previously had no soft limit
5851553Srgrimes			 * or were under it, but now have a soft limit
5861553Srgrimes			 * and are over it.
5871553Srgrimes			 */
5881553Srgrimes			if (dqblk.dqb_bsoftlimit &&
5891553Srgrimes			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
5901553Srgrimes			    (qup->dqblk.dqb_bsoftlimit == 0 ||
5911553Srgrimes			     qup->dqblk.dqb_curblocks <
5921553Srgrimes			     qup->dqblk.dqb_bsoftlimit))
5931553Srgrimes				qup->dqblk.dqb_btime = 0;
5941553Srgrimes			if (dqblk.dqb_isoftlimit &&
5951553Srgrimes			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
5961553Srgrimes			    (qup->dqblk.dqb_isoftlimit == 0 ||
5971553Srgrimes			     qup->dqblk.dqb_curinodes <
5981553Srgrimes			     qup->dqblk.dqb_isoftlimit))
5991553Srgrimes				qup->dqblk.dqb_itime = 0;
6001553Srgrimes			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
6011553Srgrimes			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
6021553Srgrimes			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
6031553Srgrimes			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
6041553Srgrimes			qup->flags |= FOUND;
6051553Srgrimes			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
6061553Srgrimes			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
6071553Srgrimes				break;
60829529Scharnier			warnx("%s: cannot change current allocation", fsp);
6091553Srgrimes			break;
6101553Srgrimes		}
6111553Srgrimes	}
6121553Srgrimes	fclose(fd);
6131553Srgrimes	/*
6141553Srgrimes	 * Disable quotas for any filesystems that have not been found.
6151553Srgrimes	 */
6161553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
6171553Srgrimes		if (qup->flags & FOUND) {
6181553Srgrimes			qup->flags &= ~FOUND;
6191553Srgrimes			continue;
6201553Srgrimes		}
6211553Srgrimes		qup->dqblk.dqb_bsoftlimit = 0;
6221553Srgrimes		qup->dqblk.dqb_bhardlimit = 0;
6231553Srgrimes		qup->dqblk.dqb_isoftlimit = 0;
6241553Srgrimes		qup->dqblk.dqb_ihardlimit = 0;
6251553Srgrimes	}
6261553Srgrimes	return (1);
6271553Srgrimes}
6281553Srgrimes
6291553Srgrimes/*
6301553Srgrimes * Convert a quotause list to an ASCII file of grace times.
6311553Srgrimes */
63229529Scharnierint
6331553Srgrimeswritetimes(quplist, outfd, quotatype)
6341553Srgrimes	struct quotause *quplist;
6351553Srgrimes	int outfd;
6361553Srgrimes	int quotatype;
6371553Srgrimes{
6381553Srgrimes	register struct quotause *qup;
6391553Srgrimes	FILE *fd;
6401553Srgrimes
6411553Srgrimes	ftruncate(outfd, 0);
6421553Srgrimes	lseek(outfd, 0, L_SET);
64329529Scharnier	if ((fd = fdopen(dup(outfd), "w")) == NULL)
64429529Scharnier		err(1, "%s", tmpfil);
6451553Srgrimes	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
6461553Srgrimes	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
6471553Srgrimes	    qfextension[quotatype]);
6481553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
6491553Srgrimes		fprintf(fd, "%s: block grace period: %s, ",
6501553Srgrimes		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
6511553Srgrimes		fprintf(fd, "file grace period: %s\n",
6521553Srgrimes		    cvtstoa(qup->dqblk.dqb_itime));
6531553Srgrimes	}
6541553Srgrimes	fclose(fd);
6551553Srgrimes	return (1);
6561553Srgrimes}
6571553Srgrimes
6581553Srgrimes/*
6591553Srgrimes * Merge changes of grace times in an ASCII file into a quotause list.
6601553Srgrimes */
66129529Scharnierint
6624782Sachereadtimes(quplist, inname)
6631553Srgrimes	struct quotause *quplist;
6644782Sache	char *inname;
6651553Srgrimes{
6661553Srgrimes	register struct quotause *qup;
6671553Srgrimes	FILE *fd;
6681553Srgrimes	int cnt;
6691553Srgrimes	register char *cp;
6701553Srgrimes	time_t itime, btime, iseconds, bseconds;
67167794Sgallatin	long l_itime, l_btime;
6721553Srgrimes	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
6731553Srgrimes
6744782Sache	fd = fopen(inname, "r");
6751553Srgrimes	if (fd == NULL) {
67629529Scharnier		warnx("can't re-read temp file!!");
6771553Srgrimes		return (0);
6781553Srgrimes	}
6791553Srgrimes	/*
6801553Srgrimes	 * Discard two title lines, then read lines to process.
6811553Srgrimes	 */
6821553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
6831553Srgrimes	(void) fgets(line1, sizeof (line1), fd);
6841553Srgrimes	while (fgets(line1, sizeof (line1), fd) != NULL) {
6851553Srgrimes		if ((fsp = strtok(line1, " \t:")) == NULL) {
68629529Scharnier			warnx("%s: bad format", line1);
6871553Srgrimes			return (0);
6881553Srgrimes		}
6891553Srgrimes		if ((cp = strtok((char *)0, "\n")) == NULL) {
69029529Scharnier			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
6911553Srgrimes			return (0);
6921553Srgrimes		}
6931553Srgrimes		cnt = sscanf(cp,
6948325Sbde		    " block grace period: %ld %s file grace period: %ld %s",
69567794Sgallatin		    &l_btime, bunits, &l_itime, iunits);
6961553Srgrimes		if (cnt != 4) {
69729529Scharnier			warnx("%s:%s: bad format", fsp, cp);
6981553Srgrimes			return (0);
6991553Srgrimes		}
70067794Sgallatin		btime = l_btime;
70167794Sgallatin		itime = l_itime;
7021553Srgrimes		if (cvtatos(btime, bunits, &bseconds) == 0)
7031553Srgrimes			return (0);
7041553Srgrimes		if (cvtatos(itime, iunits, &iseconds) == 0)
7051553Srgrimes			return (0);
7061553Srgrimes		for (qup = quplist; qup; qup = qup->next) {
7071553Srgrimes			if (strcmp(fsp, qup->fsname))
7081553Srgrimes				continue;
7091553Srgrimes			qup->dqblk.dqb_btime = bseconds;
7101553Srgrimes			qup->dqblk.dqb_itime = iseconds;
7111553Srgrimes			qup->flags |= FOUND;
7121553Srgrimes			break;
7131553Srgrimes		}
7141553Srgrimes	}
7151553Srgrimes	fclose(fd);
7161553Srgrimes	/*
7171553Srgrimes	 * reset default grace periods for any filesystems
7181553Srgrimes	 * that have not been found.
7191553Srgrimes	 */
7201553Srgrimes	for (qup = quplist; qup; qup = qup->next) {
7211553Srgrimes		if (qup->flags & FOUND) {
7221553Srgrimes			qup->flags &= ~FOUND;
7231553Srgrimes			continue;
7241553Srgrimes		}
7251553Srgrimes		qup->dqblk.dqb_btime = 0;
7261553Srgrimes		qup->dqblk.dqb_itime = 0;
7271553Srgrimes	}
7281553Srgrimes	return (1);
7291553Srgrimes}
7301553Srgrimes
7311553Srgrimes/*
7321553Srgrimes * Convert seconds to ASCII times.
7331553Srgrimes */
7341553Srgrimeschar *
73587596Smikehcvtstoa(secs)
73687596Smikeh	time_t secs;
7371553Srgrimes{
7381553Srgrimes	static char buf[20];
7391553Srgrimes
74087596Smikeh	if (secs % (24 * 60 * 60) == 0) {
74187596Smikeh		secs /= 24 * 60 * 60;
74287596Smikeh		sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
74387596Smikeh	} else if (secs % (60 * 60) == 0) {
74487596Smikeh		secs /= 60 * 60;
74587596Smikeh		sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
74687596Smikeh	} else if (secs % 60 == 0) {
74787596Smikeh		secs /= 60;
74887596Smikeh		sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
7491553Srgrimes	} else
75087596Smikeh		sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
7511553Srgrimes	return (buf);
7521553Srgrimes}
7531553Srgrimes
7541553Srgrimes/*
7551553Srgrimes * Convert ASCII input times to seconds.
7561553Srgrimes */
75729529Scharnierint
75887596Smikehcvtatos(period, units, seconds)
75987596Smikeh	time_t period;
7601553Srgrimes	char *units;
7611553Srgrimes	time_t *seconds;
7621553Srgrimes{
7631553Srgrimes
7641553Srgrimes	if (bcmp(units, "second", 6) == 0)
76587596Smikeh		*seconds = period;
7661553Srgrimes	else if (bcmp(units, "minute", 6) == 0)
76787596Smikeh		*seconds = period * 60;
7681553Srgrimes	else if (bcmp(units, "hour", 4) == 0)
76987596Smikeh		*seconds = period * 60 * 60;
7701553Srgrimes	else if (bcmp(units, "day", 3) == 0)
77187596Smikeh		*seconds = period * 24 * 60 * 60;
7721553Srgrimes	else {
7731553Srgrimes		printf("%s: bad units, specify %s\n", units,
7741553Srgrimes		    "days, hours, minutes, or seconds");
7751553Srgrimes		return (0);
7761553Srgrimes	}
7771553Srgrimes	return (1);
7781553Srgrimes}
7791553Srgrimes
7801553Srgrimes/*
7811553Srgrimes * Free a list of quotause structures.
7821553Srgrimes */
78329529Scharniervoid
7841553Srgrimesfreeprivs(quplist)
7851553Srgrimes	struct quotause *quplist;
7861553Srgrimes{
7871553Srgrimes	register struct quotause *qup, *nextqup;
7881553Srgrimes
7891553Srgrimes	for (qup = quplist; qup; qup = nextqup) {
7901553Srgrimes		nextqup = qup->next;
7911553Srgrimes		free(qup);
7921553Srgrimes	}
7931553Srgrimes}
7941553Srgrimes
7951553Srgrimes/*
7961553Srgrimes * Check whether a string is completely composed of digits.
7971553Srgrimes */
79829529Scharnierint
7991553Srgrimesalldigits(s)
80087596Smikeh	register const char *s;
8011553Srgrimes{
80287596Smikeh	register int c;
8031553Srgrimes
8041553Srgrimes	c = *s++;
8051553Srgrimes	do {
8061553Srgrimes		if (!isdigit(c))
8071553Srgrimes			return (0);
80829529Scharnier	} while ((c = *s++));
8091553Srgrimes	return (1);
8101553Srgrimes}
8111553Srgrimes
8121553Srgrimes/*
8131553Srgrimes * Check to see if a particular quota is to be enabled.
8141553Srgrimes */
81529529Scharnierint
8161553Srgrimeshasquota(fs, type, qfnamep)
8171553Srgrimes	register struct fstab *fs;
8181553Srgrimes	int type;
8191553Srgrimes	char **qfnamep;
8201553Srgrimes{
8211553Srgrimes	register char *opt;
82229529Scharnier	char *cp;
8231553Srgrimes	static char initname, usrname[100], grpname[100];
8241553Srgrimes	static char buf[BUFSIZ];
8251553Srgrimes
8261553Srgrimes	if (!initname) {
8271553Srgrimes		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
8281553Srgrimes		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
8291553Srgrimes		initname = 1;
8301553Srgrimes	}
8311553Srgrimes	strcpy(buf, fs->fs_mntops);
8321553Srgrimes	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
83329529Scharnier		if ((cp = index(opt, '=')))
8341553Srgrimes			*cp++ = '\0';
8351553Srgrimes		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
8361553Srgrimes			break;
8371553Srgrimes		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
8381553Srgrimes			break;
8391553Srgrimes	}
8401553Srgrimes	if (!opt)
8411553Srgrimes		return (0);
8421553Srgrimes	if (cp) {
8431553Srgrimes		*qfnamep = cp;
8441553Srgrimes		return (1);
8451553Srgrimes	}
8461553Srgrimes	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
8471553Srgrimes	*qfnamep = buf;
8481553Srgrimes	return (1);
8491553Srgrimes}
850