edquota.c revision 8325
1187262Snwhitehorn/*
2187262Snwhitehorn * Copyright (c) 1980, 1990, 1993
3187262Snwhitehorn *	The Regents of the University of California.  All rights reserved.
4187262Snwhitehorn *
5187262Snwhitehorn * This code is derived from software contributed to Berkeley by
6187262Snwhitehorn * Robert Elz at The University of Melbourne.
7187262Snwhitehorn *
8187262Snwhitehorn * Redistribution and use in source and binary forms, with or without
9187262Snwhitehorn * modification, are permitted provided that the following conditions
10187262Snwhitehorn * are met:
11187262Snwhitehorn * 1. Redistributions of source code must retain the above copyright
12187262Snwhitehorn *    notice, this list of conditions and the following disclaimer.
13187262Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
14187262Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
15187262Snwhitehorn *    documentation and/or other materials provided with the distribution.
16187262Snwhitehorn * 3. All advertising materials mentioning features or use of this software
17187262Snwhitehorn *    must display the following acknowledgement:
18187262Snwhitehorn *	This product includes software developed by the University of
19187262Snwhitehorn *	California, Berkeley and its contributors.
20187262Snwhitehorn * 4. Neither the name of the University nor the names of its contributors
21187262Snwhitehorn *    may be used to endorse or promote products derived from this software
22187262Snwhitehorn *    without specific prior written permission.
23187262Snwhitehorn *
24187262Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25187262Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26187262Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27187262Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28187262Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29187262Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30187262Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31187262Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32187262Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33187262Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34187262Snwhitehorn * SUCH DAMAGE.
35187262Snwhitehorn */
36187262Snwhitehorn
37187262Snwhitehorn#ifndef lint
38187262Snwhitehornstatic char copyright[] =
39187262Snwhitehorn"@(#) Copyright (c) 1980, 1990, 1993\n\
40187262Snwhitehorn	The Regents of the University of California.  All rights reserved.\n";
41187262Snwhitehorn#endif /* not lint */
42187262Snwhitehorn
43187262Snwhitehorn#ifndef lint
44187262Snwhitehornstatic char sccsid[] = "@(#)edquota.c	8.1 (Berkeley) 6/6/93";
45187262Snwhitehorn#endif /* not lint */
46187262Snwhitehorn
47187262Snwhitehorn/*
48187262Snwhitehorn * Disk quota editor.
49187262Snwhitehorn */
50187262Snwhitehorn#include <sys/param.h>
51187262Snwhitehorn#include <sys/stat.h>
52187262Snwhitehorn#include <sys/file.h>
53187262Snwhitehorn#include <sys/wait.h>
54187262Snwhitehorn#include <ufs/ufs/quota.h>
55187262Snwhitehorn#include <errno.h>
56187262Snwhitehorn#include <fstab.h>
57187262Snwhitehorn#include <pwd.h>
58187262Snwhitehorn#include <grp.h>
59187262Snwhitehorn#include <ctype.h>
60187262Snwhitehorn#include <stdio.h>
61187262Snwhitehorn#include <string.h>
62187262Snwhitehorn#include <unistd.h>
63187262Snwhitehorn#include "pathnames.h"
64187262Snwhitehorn
65187262Snwhitehornchar *qfname = QUOTAFILENAME;
66187262Snwhitehornchar *qfextension[] = INITQFNAMES;
67187262Snwhitehornchar *quotagroup = QUOTAGROUP;
68187262Snwhitehornchar tmpfil[] = _PATH_TMP;
69187262Snwhitehorn
70187262Snwhitehornstruct quotause {
71187262Snwhitehorn	struct	quotause *next;
72187262Snwhitehorn	long	flags;
73187262Snwhitehorn	struct	dqblk dqblk;
74187262Snwhitehorn	char	fsname[MAXPATHLEN + 1];
75187262Snwhitehorn	char	qfname[1];	/* actually longer */
76187262Snwhitehorn} *getprivs();
77187262Snwhitehorn#define	FOUND	0x01
78187262Snwhitehorn
79187262Snwhitehornmain(argc, argv)
80187262Snwhitehorn	register char **argv;
81187262Snwhitehorn	int argc;
82187262Snwhitehorn{
83187262Snwhitehorn	register struct quotause *qup, *protoprivs, *curprivs;
84187262Snwhitehorn	extern char *optarg;
85187262Snwhitehorn	extern int optind;
86187262Snwhitehorn	register long id, protoid;
87187262Snwhitehorn	register int quotatype, tmpfd;
88187262Snwhitehorn	char *protoname, ch;
89187262Snwhitehorn	int tflag = 0, pflag = 0;
90187262Snwhitehorn
91187262Snwhitehorn	if (argc < 2)
92187262Snwhitehorn		usage();
93187262Snwhitehorn	if (getuid()) {
94187262Snwhitehorn		fprintf(stderr, "edquota: permission denied\n");
95187262Snwhitehorn		exit(1);
96187262Snwhitehorn	}
97187262Snwhitehorn	quotatype = USRQUOTA;
98187262Snwhitehorn	while ((ch = getopt(argc, argv, "ugtp:")) != EOF) {
99187262Snwhitehorn		switch(ch) {
100187262Snwhitehorn		case 'p':
101187262Snwhitehorn			protoname = optarg;
102187262Snwhitehorn			pflag++;
103187262Snwhitehorn			break;
104187262Snwhitehorn		case 'g':
105187262Snwhitehorn			quotatype = GRPQUOTA;
106187262Snwhitehorn			break;
107187262Snwhitehorn		case 'u':
108187262Snwhitehorn			quotatype = USRQUOTA;
109187262Snwhitehorn			break;
110187262Snwhitehorn		case 't':
111187262Snwhitehorn			tflag++;
112187262Snwhitehorn			break;
113187262Snwhitehorn		default:
114187262Snwhitehorn			usage();
115187262Snwhitehorn		}
116187262Snwhitehorn	}
117187262Snwhitehorn	argc -= optind;
118187262Snwhitehorn	argv += optind;
119187262Snwhitehorn	if (pflag) {
120187262Snwhitehorn		if ((protoid = getentry(protoname, quotatype)) == -1)
121187262Snwhitehorn			exit(1);
122187262Snwhitehorn		protoprivs = getprivs(protoid, quotatype);
123187262Snwhitehorn		for (qup = protoprivs; qup; qup = qup->next) {
124187262Snwhitehorn			qup->dqblk.dqb_btime = 0;
125187262Snwhitehorn			qup->dqblk.dqb_itime = 0;
126187262Snwhitehorn		}
127187262Snwhitehorn		while (argc-- > 0) {
128187262Snwhitehorn			if ((id = getentry(*argv++, quotatype)) < 0)
129187262Snwhitehorn				continue;
130187262Snwhitehorn			putprivs(id, quotatype, protoprivs);
131187262Snwhitehorn		}
132187262Snwhitehorn		exit(0);
133187262Snwhitehorn	}
134187262Snwhitehorn	tmpfd = mkstemp(tmpfil);
135187262Snwhitehorn	fchown(tmpfd, getuid(), getgid());
136187262Snwhitehorn	if (tflag) {
137187262Snwhitehorn		protoprivs = getprivs(0, quotatype);
138187262Snwhitehorn		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
139187262Snwhitehorn			exit(1);
140187262Snwhitehorn		if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
141187262Snwhitehorn			putprivs(0, quotatype, protoprivs);
142187262Snwhitehorn		freeprivs(protoprivs);
143187262Snwhitehorn		exit(0);
144187262Snwhitehorn	}
145187262Snwhitehorn	for ( ; argc > 0; argc--, argv++) {
146187262Snwhitehorn		if ((id = getentry(*argv, quotatype)) == -1)
147187262Snwhitehorn			continue;
148187262Snwhitehorn		curprivs = getprivs(id, quotatype);
149187262Snwhitehorn		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
150187262Snwhitehorn			continue;
151187262Snwhitehorn		if (editit(tmpfil) && readprivs(curprivs, tmpfil))
152187262Snwhitehorn			putprivs(id, quotatype, curprivs);
153187262Snwhitehorn		freeprivs(curprivs);
154187262Snwhitehorn	}
155187262Snwhitehorn	close(tmpfd);
156187262Snwhitehorn	unlink(tmpfil);
157187262Snwhitehorn	exit(0);
158187262Snwhitehorn}
159187262Snwhitehorn
160187262Snwhitehornusage()
161187262Snwhitehorn{
162187262Snwhitehorn	fprintf(stderr, "%s%s%s%s",
163187262Snwhitehorn		"Usage: edquota [-u] [-p username] username ...\n",
164187262Snwhitehorn		"\tedquota -g [-p groupname] groupname ...\n",
165187262Snwhitehorn		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
166187262Snwhitehorn	exit(1);
167187262Snwhitehorn}
168187262Snwhitehorn
169187262Snwhitehorn/*
170187262Snwhitehorn * This routine converts a name for a particular quota type to
171187262Snwhitehorn * an identifier. This routine must agree with the kernel routine
172187262Snwhitehorn * getinoquota as to the interpretation of quota types.
173187262Snwhitehorn */
174187262Snwhitehorngetentry(name, quotatype)
175187262Snwhitehorn	char *name;
176187262Snwhitehorn	int quotatype;
177187262Snwhitehorn{
178187262Snwhitehorn	struct passwd *pw;
179187262Snwhitehorn	struct group *gr;
180187262Snwhitehorn
181187262Snwhitehorn	if (alldigits(name))
182187262Snwhitehorn		return (atoi(name));
183187262Snwhitehorn	switch(quotatype) {
184187262Snwhitehorn	case USRQUOTA:
185187262Snwhitehorn		if (pw = getpwnam(name))
186187262Snwhitehorn			return (pw->pw_uid);
187187262Snwhitehorn		fprintf(stderr, "%s: no such user\n", name);
188187262Snwhitehorn		break;
189187262Snwhitehorn	case GRPQUOTA:
190187262Snwhitehorn		if (gr = getgrnam(name))
191187262Snwhitehorn			return (gr->gr_gid);
192187262Snwhitehorn		fprintf(stderr, "%s: no such group\n", name);
193187262Snwhitehorn		break;
194187262Snwhitehorn	default:
195187262Snwhitehorn		fprintf(stderr, "%d: unknown quota type\n", quotatype);
196187262Snwhitehorn		break;
197187262Snwhitehorn	}
198187262Snwhitehorn	sleep(1);
199187262Snwhitehorn	return (-1);
200187262Snwhitehorn}
201187262Snwhitehorn
202187262Snwhitehorn/*
203187262Snwhitehorn * Collect the requested quota information.
204187262Snwhitehorn */
205187262Snwhitehornstruct quotause *
206187262Snwhitehorngetprivs(id, quotatype)
207187262Snwhitehorn	register long id;
208187262Snwhitehorn	int quotatype;
209187262Snwhitehorn{
210187262Snwhitehorn	register struct fstab *fs;
211187262Snwhitehorn	register struct quotause *qup, *quptail;
212187262Snwhitehorn	struct quotause *quphead;
213187262Snwhitehorn	int qcmd, qupsize, fd;
214187262Snwhitehorn	char *qfpathname;
215187262Snwhitehorn	static int warned = 0;
216187262Snwhitehorn	extern int errno;
217187262Snwhitehorn
218187262Snwhitehorn	setfsent();
219187262Snwhitehorn	quphead = (struct quotause *)0;
220187262Snwhitehorn	qcmd = QCMD(Q_GETQUOTA, quotatype);
221187262Snwhitehorn	while (fs = getfsent()) {
222187262Snwhitehorn		if (strcmp(fs->fs_vfstype, "ufs"))
223187262Snwhitehorn			continue;
224187262Snwhitehorn		if (!hasquota(fs, quotatype, &qfpathname))
225187262Snwhitehorn			continue;
226187262Snwhitehorn		qupsize = sizeof(*qup) + strlen(qfpathname);
227187262Snwhitehorn		if ((qup = (struct quotause *)malloc(qupsize)) == NULL) {
228187262Snwhitehorn			fprintf(stderr, "edquota: out of memory\n");
229187262Snwhitehorn			exit(2);
230187262Snwhitehorn		}
231187262Snwhitehorn		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
232187262Snwhitehorn	    		if (errno == EOPNOTSUPP && !warned) {
233187262Snwhitehorn				warned++;
234187262Snwhitehorn				fprintf(stderr, "Warning: %s\n",
235187262Snwhitehorn				    "Quotas are not compiled into this kernel");
236187262Snwhitehorn				sleep(3);
237187262Snwhitehorn			}
238187262Snwhitehorn			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
239187262Snwhitehorn				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
240187262Snwhitehorn				if (fd < 0 && errno != ENOENT) {
241187262Snwhitehorn					perror(qfpathname);
242187262Snwhitehorn					free(qup);
243187262Snwhitehorn					continue;
244187262Snwhitehorn				}
245187262Snwhitehorn				fprintf(stderr, "Creating quota file %s\n",
246187262Snwhitehorn				    qfpathname);
247187262Snwhitehorn				sleep(3);
248187262Snwhitehorn				(void) fchown(fd, getuid(),
249187262Snwhitehorn				    getentry(quotagroup, GRPQUOTA));
250187262Snwhitehorn				(void) fchmod(fd, 0640);
251187262Snwhitehorn			}
252187262Snwhitehorn			lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
253187262Snwhitehorn			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
254187262Snwhitehorn			case 0:			/* EOF */
255187262Snwhitehorn				/*
256187262Snwhitehorn				 * Convert implicit 0 quota (EOF)
257187262Snwhitehorn				 * into an explicit one (zero'ed dqblk)
258187262Snwhitehorn				 */
259187262Snwhitehorn				bzero((caddr_t)&qup->dqblk,
260187262Snwhitehorn				    sizeof(struct dqblk));
261187262Snwhitehorn				break;
262187262Snwhitehorn
263187262Snwhitehorn			case sizeof(struct dqblk):	/* OK */
264187262Snwhitehorn				break;
265187262Snwhitehorn
266187262Snwhitehorn			default:		/* ERROR */
267187262Snwhitehorn				fprintf(stderr, "edquota: read error in ");
268187262Snwhitehorn				perror(qfpathname);
269187262Snwhitehorn				close(fd);
270187262Snwhitehorn				free(qup);
271187262Snwhitehorn				continue;
272187262Snwhitehorn			}
273187262Snwhitehorn			close(fd);
274187262Snwhitehorn		}
275187262Snwhitehorn		strcpy(qup->qfname, qfpathname);
276187262Snwhitehorn		strcpy(qup->fsname, fs->fs_file);
277187262Snwhitehorn		if (quphead == NULL)
278187262Snwhitehorn			quphead = qup;
279187262Snwhitehorn		else
280187262Snwhitehorn			quptail->next = qup;
281187262Snwhitehorn		quptail = qup;
282187262Snwhitehorn		qup->next = 0;
283187262Snwhitehorn	}
284187262Snwhitehorn	endfsent();
285187262Snwhitehorn	return (quphead);
286187262Snwhitehorn}
287187262Snwhitehorn
288187262Snwhitehorn/*
289187262Snwhitehorn * Store the requested quota information.
290187262Snwhitehorn */
291187262Snwhitehornputprivs(id, quotatype, quplist)
292187262Snwhitehorn	long id;
293187262Snwhitehorn	int quotatype;
294187262Snwhitehorn	struct quotause *quplist;
295187262Snwhitehorn{
296187262Snwhitehorn	register struct quotause *qup;
297187262Snwhitehorn	int qcmd, fd;
298187262Snwhitehorn
299187262Snwhitehorn	qcmd = QCMD(Q_SETQUOTA, quotatype);
300187262Snwhitehorn	for (qup = quplist; qup; qup = qup->next) {
301187262Snwhitehorn		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
302187262Snwhitehorn			continue;
303187262Snwhitehorn		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
304187262Snwhitehorn			perror(qup->qfname);
305187262Snwhitehorn		} else {
306187262Snwhitehorn			lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
307187262Snwhitehorn			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
308187262Snwhitehorn			    sizeof (struct dqblk)) {
309187262Snwhitehorn				fprintf(stderr, "edquota: ");
310187262Snwhitehorn				perror(qup->qfname);
311187262Snwhitehorn			}
312187262Snwhitehorn			close(fd);
313187262Snwhitehorn		}
314187262Snwhitehorn	}
315187262Snwhitehorn}
316187262Snwhitehorn
317187262Snwhitehorn/*
318187262Snwhitehorn * Take a list of priviledges and get it edited.
319187262Snwhitehorn */
320187262Snwhitehorneditit(tmpfile)
321187262Snwhitehorn	char *tmpfile;
322187262Snwhitehorn{
323187262Snwhitehorn	long omask;
324187262Snwhitehorn	int pid, stat;
325187262Snwhitehorn	extern char *getenv();
326187262Snwhitehorn
327187262Snwhitehorn	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
328187262Snwhitehorn top:
329187262Snwhitehorn	if ((pid = fork()) < 0) {
330187262Snwhitehorn		extern errno;
331187262Snwhitehorn
332187262Snwhitehorn		if (errno == EPROCLIM) {
333187262Snwhitehorn			fprintf(stderr, "You have too many processes\n");
334187262Snwhitehorn			return(0);
335187262Snwhitehorn		}
336187262Snwhitehorn		if (errno == EAGAIN) {
337187262Snwhitehorn			sleep(1);
338187262Snwhitehorn			goto top;
339187262Snwhitehorn		}
340187262Snwhitehorn		perror("fork");
341187262Snwhitehorn		return (0);
342187262Snwhitehorn	}
343187262Snwhitehorn	if (pid == 0) {
344187262Snwhitehorn		register char *ed;
345187262Snwhitehorn
346187262Snwhitehorn		sigsetmask(omask);
347187262Snwhitehorn		setgid(getgid());
348187262Snwhitehorn		setuid(getuid());
349187262Snwhitehorn		if ((ed = getenv("EDITOR")) == (char *)0)
350187262Snwhitehorn			ed = _PATH_VI;
351187262Snwhitehorn		execlp(ed, ed, tmpfile, 0);
352187262Snwhitehorn		perror(ed);
353187262Snwhitehorn		exit(1);
354187262Snwhitehorn	}
355187262Snwhitehorn	waitpid(pid, &stat, 0);
356187262Snwhitehorn	sigsetmask(omask);
357187262Snwhitehorn	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
358187262Snwhitehorn		return (0);
359187262Snwhitehorn	return (1);
360187262Snwhitehorn}
361187262Snwhitehorn
362187262Snwhitehorn/*
363187262Snwhitehorn * Convert a quotause list to an ASCII file.
364187262Snwhitehorn */
365187262Snwhitehornwriteprivs(quplist, outfd, name, quotatype)
366187262Snwhitehorn	struct quotause *quplist;
367187262Snwhitehorn	int outfd;
368187262Snwhitehorn	char *name;
369187262Snwhitehorn	int quotatype;
370187262Snwhitehorn{
371187262Snwhitehorn	register struct quotause *qup;
372187262Snwhitehorn	FILE *fd;
373187262Snwhitehorn
374187262Snwhitehorn	ftruncate(outfd, 0);
375187262Snwhitehorn	lseek(outfd, 0, L_SET);
376187262Snwhitehorn	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
377187262Snwhitehorn		fprintf(stderr, "edquota: ");
378187262Snwhitehorn		perror(tmpfil);
379187262Snwhitehorn		exit(1);
380187262Snwhitehorn	}
381187262Snwhitehorn	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
382187262Snwhitehorn	for (qup = quplist; qup; qup = qup->next) {
383187262Snwhitehorn		fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
384187262Snwhitehorn		    qup->fsname, "blocks in use:",
385187262Snwhitehorn		    (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
386187262Snwhitehorn		    (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
387187262Snwhitehorn		    (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
388187262Snwhitehorn		fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
389187262Snwhitehorn		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
390187262Snwhitehorn		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
391187262Snwhitehorn	}
392	fclose(fd);
393	return (1);
394}
395
396/*
397 * Merge changes to an ASCII file into a quotause list.
398 */
399readprivs(quplist, inname)
400	struct quotause *quplist;
401	char *inname;
402{
403	register struct quotause *qup;
404	FILE *fd;
405	int cnt;
406	register char *cp;
407	struct dqblk dqblk;
408	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
409
410	fd = fopen(inname, "r");
411	if (fd == NULL) {
412		fprintf(stderr, "Can't re-read temp file!!\n");
413		return (0);
414	}
415	/*
416	 * Discard title line, then read pairs of lines to process.
417	 */
418	(void) fgets(line1, sizeof (line1), fd);
419	while (fgets(line1, sizeof (line1), fd) != NULL &&
420	       fgets(line2, sizeof (line2), fd) != NULL) {
421		if ((fsp = strtok(line1, " \t:")) == NULL) {
422			fprintf(stderr, "%s: bad format\n", line1);
423			return (0);
424		}
425		if ((cp = strtok((char *)0, "\n")) == NULL) {
426			fprintf(stderr, "%s: %s: bad format\n", fsp,
427			    &fsp[strlen(fsp) + 1]);
428			return (0);
429		}
430		cnt = sscanf(cp,
431		    " blocks in use: %lu, limits (soft = %lu, hard = %lu)",
432		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
433		    &dqblk.dqb_bhardlimit);
434		if (cnt != 3) {
435			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
436			return (0);
437		}
438		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
439		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
440		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
441		if ((cp = strtok(line2, "\n")) == NULL) {
442			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
443			return (0);
444		}
445		cnt = sscanf(cp,
446		    "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
447		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
448		    &dqblk.dqb_ihardlimit);
449		if (cnt != 3) {
450			fprintf(stderr, "%s: %s: bad format\n", fsp, line2);
451			return (0);
452		}
453		for (qup = quplist; qup; qup = qup->next) {
454			if (strcmp(fsp, qup->fsname))
455				continue;
456			/*
457			 * Cause time limit to be reset when the quota
458			 * is next used if previously had no soft limit
459			 * or were under it, but now have a soft limit
460			 * and are over it.
461			 */
462			if (dqblk.dqb_bsoftlimit &&
463			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
464			    (qup->dqblk.dqb_bsoftlimit == 0 ||
465			     qup->dqblk.dqb_curblocks <
466			     qup->dqblk.dqb_bsoftlimit))
467				qup->dqblk.dqb_btime = 0;
468			if (dqblk.dqb_isoftlimit &&
469			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
470			    (qup->dqblk.dqb_isoftlimit == 0 ||
471			     qup->dqblk.dqb_curinodes <
472			     qup->dqblk.dqb_isoftlimit))
473				qup->dqblk.dqb_itime = 0;
474			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
475			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
476			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
477			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
478			qup->flags |= FOUND;
479			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
480			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
481				break;
482			fprintf(stderr,
483			    "%s: cannot change current allocation\n", fsp);
484			break;
485		}
486	}
487	fclose(fd);
488	/*
489	 * Disable quotas for any filesystems that have not been found.
490	 */
491	for (qup = quplist; qup; qup = qup->next) {
492		if (qup->flags & FOUND) {
493			qup->flags &= ~FOUND;
494			continue;
495		}
496		qup->dqblk.dqb_bsoftlimit = 0;
497		qup->dqblk.dqb_bhardlimit = 0;
498		qup->dqblk.dqb_isoftlimit = 0;
499		qup->dqblk.dqb_ihardlimit = 0;
500	}
501	return (1);
502}
503
504/*
505 * Convert a quotause list to an ASCII file of grace times.
506 */
507writetimes(quplist, outfd, quotatype)
508	struct quotause *quplist;
509	int outfd;
510	int quotatype;
511{
512	register struct quotause *qup;
513	char *cvtstoa();
514	FILE *fd;
515
516	ftruncate(outfd, 0);
517	lseek(outfd, 0, L_SET);
518	if ((fd = fdopen(dup(outfd), "w")) == NULL) {
519		fprintf(stderr, "edquota: ");
520		perror(tmpfil);
521		exit(1);
522	}
523	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
524	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
525	    qfextension[quotatype]);
526	for (qup = quplist; qup; qup = qup->next) {
527		fprintf(fd, "%s: block grace period: %s, ",
528		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
529		fprintf(fd, "file grace period: %s\n",
530		    cvtstoa(qup->dqblk.dqb_itime));
531	}
532	fclose(fd);
533	return (1);
534}
535
536/*
537 * Merge changes of grace times in an ASCII file into a quotause list.
538 */
539readtimes(quplist, inname)
540	struct quotause *quplist;
541	char *inname;
542{
543	register struct quotause *qup;
544	FILE *fd;
545	int cnt;
546	register char *cp;
547	time_t itime, btime, iseconds, bseconds;
548	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
549
550	fd = fopen(inname, "r");
551	if (fd == NULL) {
552		fprintf(stderr, "Can't re-read temp file!!\n");
553		return (0);
554	}
555	/*
556	 * Discard two title lines, then read lines to process.
557	 */
558	(void) fgets(line1, sizeof (line1), fd);
559	(void) fgets(line1, sizeof (line1), fd);
560	while (fgets(line1, sizeof (line1), fd) != NULL) {
561		if ((fsp = strtok(line1, " \t:")) == NULL) {
562			fprintf(stderr, "%s: bad format\n", line1);
563			return (0);
564		}
565		if ((cp = strtok((char *)0, "\n")) == NULL) {
566			fprintf(stderr, "%s: %s: bad format\n", fsp,
567			    &fsp[strlen(fsp) + 1]);
568			return (0);
569		}
570		cnt = sscanf(cp,
571		    " block grace period: %ld %s file grace period: %ld %s",
572		    &btime, bunits, &itime, iunits);
573		if (cnt != 4) {
574			fprintf(stderr, "%s:%s: bad format\n", fsp, cp);
575			return (0);
576		}
577		if (cvtatos(btime, bunits, &bseconds) == 0)
578			return (0);
579		if (cvtatos(itime, iunits, &iseconds) == 0)
580			return (0);
581		for (qup = quplist; qup; qup = qup->next) {
582			if (strcmp(fsp, qup->fsname))
583				continue;
584			qup->dqblk.dqb_btime = bseconds;
585			qup->dqblk.dqb_itime = iseconds;
586			qup->flags |= FOUND;
587			break;
588		}
589	}
590	fclose(fd);
591	/*
592	 * reset default grace periods for any filesystems
593	 * that have not been found.
594	 */
595	for (qup = quplist; qup; qup = qup->next) {
596		if (qup->flags & FOUND) {
597			qup->flags &= ~FOUND;
598			continue;
599		}
600		qup->dqblk.dqb_btime = 0;
601		qup->dqblk.dqb_itime = 0;
602	}
603	return (1);
604}
605
606/*
607 * Convert seconds to ASCII times.
608 */
609char *
610cvtstoa(time)
611	time_t time;
612{
613	static char buf[20];
614
615	if (time % (24 * 60 * 60) == 0) {
616		time /= 24 * 60 * 60;
617		sprintf(buf, "%ld day%s", time, time == 1 ? "" : "s");
618	} else if (time % (60 * 60) == 0) {
619		time /= 60 * 60;
620		sprintf(buf, "%ld hour%s", time, time == 1 ? "" : "s");
621	} else if (time % 60 == 0) {
622		time /= 60;
623		sprintf(buf, "%ld minute%s", time, time == 1 ? "" : "s");
624	} else
625		sprintf(buf, "%ld second%s", time, time == 1 ? "" : "s");
626	return (buf);
627}
628
629/*
630 * Convert ASCII input times to seconds.
631 */
632cvtatos(time, units, seconds)
633	time_t time;
634	char *units;
635	time_t *seconds;
636{
637
638	if (bcmp(units, "second", 6) == 0)
639		*seconds = time;
640	else if (bcmp(units, "minute", 6) == 0)
641		*seconds = time * 60;
642	else if (bcmp(units, "hour", 4) == 0)
643		*seconds = time * 60 * 60;
644	else if (bcmp(units, "day", 3) == 0)
645		*seconds = time * 24 * 60 * 60;
646	else {
647		printf("%s: bad units, specify %s\n", units,
648		    "days, hours, minutes, or seconds");
649		return (0);
650	}
651	return (1);
652}
653
654/*
655 * Free a list of quotause structures.
656 */
657freeprivs(quplist)
658	struct quotause *quplist;
659{
660	register struct quotause *qup, *nextqup;
661
662	for (qup = quplist; qup; qup = nextqup) {
663		nextqup = qup->next;
664		free(qup);
665	}
666}
667
668/*
669 * Check whether a string is completely composed of digits.
670 */
671alldigits(s)
672	register char *s;
673{
674	register c;
675
676	c = *s++;
677	do {
678		if (!isdigit(c))
679			return (0);
680	} while (c = *s++);
681	return (1);
682}
683
684/*
685 * Check to see if a particular quota is to be enabled.
686 */
687hasquota(fs, type, qfnamep)
688	register struct fstab *fs;
689	int type;
690	char **qfnamep;
691{
692	register char *opt;
693	char *cp, *index(), *strtok();
694	static char initname, usrname[100], grpname[100];
695	static char buf[BUFSIZ];
696
697	if (!initname) {
698		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
699		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
700		initname = 1;
701	}
702	strcpy(buf, fs->fs_mntops);
703	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
704		if (cp = index(opt, '='))
705			*cp++ = '\0';
706		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
707			break;
708		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
709			break;
710	}
711	if (!opt)
712		return (0);
713	if (cp) {
714		*qfnamep = cp;
715		return (1);
716	}
717	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
718	*qfnamep = buf;
719	return (1);
720}
721