edquota.c revision 133249
1/*
2 * Copyright (c) 1980, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if 0
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1990, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)edquota.c	8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/usr.sbin/edquota/edquota.c 133249 2004-08-07 04:28:56Z imp $");
46
47/*
48 * Disk quota editor.
49 */
50#include <sys/param.h>
51#include <sys/stat.h>
52#include <sys/file.h>
53#include <sys/wait.h>
54#include <ufs/ufs/quota.h>
55#include <ctype.h>
56#include <err.h>
57#include <errno.h>
58#include <fstab.h>
59#include <grp.h>
60#include <pwd.h>
61#include <signal.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66#include "pathnames.h"
67
68const char *qfname = QUOTAFILENAME;
69const char *qfextension[] = INITQFNAMES;
70const char *quotagroup = QUOTAGROUP;
71char tmpfil[] = _PATH_TMP;
72
73struct quotause {
74	struct	quotause *next;
75	long	flags;
76	struct	dqblk dqblk;
77	char	fsname[MAXPATHLEN + 1];
78	char	qfname[1];	/* actually longer */
79};
80#define	FOUND	0x01
81
82int alldigits(const char *s);
83int cvtatos(time_t, char *, time_t *);
84char *cvtstoa(time_t);
85int editit(char *);
86void freeprivs(struct quotause *);
87int getentry(const char *, int);
88struct quotause *getprivs(long, int, char *);
89int hasquota(struct fstab *, int, char **);
90void putprivs(long, int, struct quotause *);
91int readprivs(struct quotause *, char *);
92int readtimes(struct quotause *, char *);
93static void usage(void);
94int writetimes(struct quotause *, int, int);
95int writeprivs(struct quotause *, int, char *, int);
96
97int
98main(int argc, char **argv)
99{
100	struct quotause *qup, *protoprivs, *curprivs;
101	long id, protoid;
102	long long lim;
103	int i, quotatype, range, tmpfd;
104	uid_t startuid, enduid;
105	u_int32_t *limp;
106	char *protoname, *cp, *oldoptarg;
107	int eflag = 0, tflag = 0, pflag = 0, ch;
108	char *fspath = NULL;
109	char buf[MAXLOGNAME];
110
111	if (argc < 2)
112		usage();
113	if (getuid())
114		errx(1, "permission denied");
115	quotatype = USRQUOTA;
116	protoprivs = NULL;
117	while ((ch = getopt(argc, argv, "ugtf:p:e:")) != -1) {
118		switch(ch) {
119		case 'f':
120			fspath = optarg;
121			break;
122		case 'p':
123			protoname = optarg;
124			pflag++;
125			break;
126		case 'g':
127			quotatype = GRPQUOTA;
128			break;
129		case 'u':
130			quotatype = USRQUOTA;
131			break;
132		case 't':
133			tflag++;
134			break;
135		case 'e':
136			if ((qup = malloc(sizeof(*qup))) == NULL)
137				errx(2, "out of memory");
138			bzero(qup, sizeof(*qup));
139			i = 0;
140			oldoptarg = optarg;
141			for (cp = optarg; (cp = strsep(&optarg, ":")) != NULL;
142			    i++) {
143				if (cp != oldoptarg)
144					*(cp - 1) = ':';
145				limp = NULL;
146				switch (i) {
147				case 0:
148					strlcpy(qup->fsname, cp,
149					    sizeof(qup->fsname));
150					break;
151				case 1:
152					limp = &qup->dqblk.dqb_bsoftlimit;
153					break;
154				case 2:
155					limp = &qup->dqblk.dqb_bhardlimit;
156					break;
157				case 3:
158					limp = &qup->dqblk.dqb_isoftlimit;
159					break;
160				case 4:
161					limp = &qup->dqblk.dqb_ihardlimit;
162					break;
163				default:
164					warnx("incorrect quota specification: "
165					    "%s", oldoptarg);
166					usage();
167					break; /* XXX: report an error */
168				}
169				if (limp != NULL) {
170					lim = strtoll(cp, NULL, 10);
171					if (lim < 0 || lim > UINT_MAX)
172						errx(1, "invalid limit value: "
173						    "%lld", lim);
174					*limp = (u_int32_t)lim;
175				}
176			}
177			qup->dqblk.dqb_bsoftlimit =
178			    btodb((off_t)qup->dqblk.dqb_bsoftlimit * 1024);
179			qup->dqblk.dqb_bhardlimit =
180			    btodb((off_t)qup->dqblk.dqb_bhardlimit * 1024);
181			if (protoprivs == NULL) {
182				protoprivs = curprivs = qup;
183			} else {
184				curprivs->next = qup;
185				curprivs = qup;
186			}
187			eflag++;
188			pflag++;
189			break;
190		default:
191			usage();
192		}
193	}
194	argc -= optind;
195	argv += optind;
196	if (pflag) {
197		if (protoprivs == NULL) {
198			if ((protoid = getentry(protoname, quotatype)) == -1)
199				exit(1);
200			protoprivs = getprivs(protoid, quotatype, fspath);
201			for (qup = protoprivs; qup; qup = qup->next) {
202				qup->dqblk.dqb_btime = 0;
203				qup->dqblk.dqb_itime = 0;
204			}
205		}
206		for (; argc-- > 0; argv++) {
207			if (strspn(*argv, "0123456789-") == strlen(*argv) &&
208			    (cp = strchr(*argv, '-')) != NULL) {
209				*cp++ = '\0';
210				startuid = atoi(*argv);
211				enduid = atoi(cp);
212				if (enduid < startuid)
213					errx(1,
214	"ending uid (%d) must be >= starting uid (%d) when using uid ranges",
215						enduid, startuid);
216				range = 1;
217			} else {
218				startuid = enduid = 0;
219				range = 0;
220			}
221			for ( ; startuid <= enduid; startuid++) {
222				if (range)
223					snprintf(buf, sizeof(buf), "%d",
224					    startuid);
225				else
226					snprintf(buf, sizeof(buf), "%s",
227						*argv);
228				if ((id = getentry(buf, quotatype)) < 0)
229					continue;
230				if (eflag) {
231					for (qup = protoprivs; qup;
232					    qup = qup->next) {
233						curprivs = getprivs(id,
234						    quotatype, qup->fsname);
235						if (curprivs == NULL)
236							continue;
237						strcpy(qup->qfname,
238						    curprivs->qfname);
239						strcpy(qup->fsname,
240						    curprivs->fsname);
241					}
242				}
243				putprivs(id, quotatype, protoprivs);
244			}
245		}
246		exit(0);
247	}
248	tmpfd = mkstemp(tmpfil);
249	fchown(tmpfd, getuid(), getgid());
250	if (tflag) {
251		protoprivs = getprivs(0, quotatype, fspath);
252		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
253			exit(1);
254		if (editit(tmpfil) && readtimes(protoprivs, tmpfil))
255			putprivs(0, quotatype, protoprivs);
256		freeprivs(protoprivs);
257		close(tmpfd);
258		unlink(tmpfil);
259		exit(0);
260	}
261	for ( ; argc > 0; argc--, argv++) {
262		if ((id = getentry(*argv, quotatype)) == -1)
263			continue;
264		curprivs = getprivs(id, quotatype, fspath);
265		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
266			continue;
267		if (editit(tmpfil) && readprivs(curprivs, tmpfil))
268			putprivs(id, quotatype, curprivs);
269		freeprivs(curprivs);
270	}
271	close(tmpfd);
272	unlink(tmpfil);
273	exit(0);
274}
275
276static void
277usage()
278{
279	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
280		"usage: edquota [-u] [-f fspath] [-p username] username ...",
281		"       edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
282		"               username ...",
283		"       edquota -g [-f fspath] [-p groupname] groupname ...",
284		"       edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
285		"               groupname ...",
286		"       edquota [-u] -t [-f fspath]",
287		"       edquota -g -t [-f fspath]");
288	exit(1);
289}
290
291/*
292 * This routine converts a name for a particular quota type to
293 * an identifier. This routine must agree with the kernel routine
294 * getinoquota as to the interpretation of quota types.
295 */
296int
297getentry(name, quotatype)
298	const char *name;
299	int quotatype;
300{
301	struct passwd *pw;
302	struct group *gr;
303
304	if (alldigits(name))
305		return (atoi(name));
306	switch(quotatype) {
307	case USRQUOTA:
308		if ((pw = getpwnam(name)))
309			return (pw->pw_uid);
310		warnx("%s: no such user", name);
311		break;
312	case GRPQUOTA:
313		if ((gr = getgrnam(name)))
314			return (gr->gr_gid);
315		warnx("%s: no such group", name);
316		break;
317	default:
318		warnx("%d: unknown quota type", quotatype);
319		break;
320	}
321	sleep(1);
322	return (-1);
323}
324
325/*
326 * Collect the requested quota information.
327 */
328struct quotause *
329getprivs(id, quotatype, fspath)
330	register long id;
331	int quotatype;
332	char *fspath;
333{
334	register struct fstab *fs;
335	register struct quotause *qup, *quptail;
336	struct quotause *quphead;
337	int qcmd, qupsize, fd;
338	char *qfpathname;
339	static int warned = 0;
340
341	setfsent();
342	quphead = (struct quotause *)0;
343	qcmd = QCMD(Q_GETQUOTA, quotatype);
344	while ((fs = getfsent())) {
345		if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
346		    strcmp(fspath, fs->fs_file))
347			continue;
348		if (strcmp(fs->fs_vfstype, "ufs"))
349			continue;
350		if (!hasquota(fs, quotatype, &qfpathname))
351			continue;
352		qupsize = sizeof(*qup) + strlen(qfpathname);
353		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
354			errx(2, "out of memory");
355		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
356	    		if (errno == EOPNOTSUPP && !warned) {
357				warned++;
358		warnx("warning: quotas are not compiled into this kernel");
359				sleep(3);
360			}
361			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
362				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
363				if (fd < 0 && errno != ENOENT) {
364					warn("%s", qfpathname);
365					free(qup);
366					continue;
367				}
368				warnx("creating quota file %s", qfpathname);
369				sleep(3);
370				(void) fchown(fd, getuid(),
371				    getentry(quotagroup, GRPQUOTA));
372				(void) fchmod(fd, 0640);
373			}
374			lseek(fd, (long)(id * sizeof(struct dqblk)), L_SET);
375			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
376			case 0:			/* EOF */
377				/*
378				 * Convert implicit 0 quota (EOF)
379				 * into an explicit one (zero'ed dqblk)
380				 */
381				bzero((caddr_t)&qup->dqblk,
382				    sizeof(struct dqblk));
383				break;
384
385			case sizeof(struct dqblk):	/* OK */
386				break;
387
388			default:		/* ERROR */
389				warn("read error in %s", qfpathname);
390				close(fd);
391				free(qup);
392				continue;
393			}
394			close(fd);
395		}
396		strcpy(qup->qfname, qfpathname);
397		strcpy(qup->fsname, fs->fs_file);
398		if (quphead == NULL)
399			quphead = qup;
400		else
401			quptail->next = qup;
402		quptail = qup;
403		qup->next = 0;
404	}
405	endfsent();
406	return (quphead);
407}
408
409/*
410 * Store the requested quota information.
411 */
412void
413putprivs(id, quotatype, quplist)
414	long id;
415	int quotatype;
416	struct quotause *quplist;
417{
418	register struct quotause *qup;
419	int qcmd, fd;
420
421	qcmd = QCMD(Q_SETQUOTA, quotatype);
422	for (qup = quplist; qup; qup = qup->next) {
423		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
424			continue;
425		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
426			warn("%s", qup->qfname);
427		} else {
428			lseek(fd, (long)id * (long)sizeof (struct dqblk), 0);
429			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
430			    sizeof (struct dqblk)) {
431				warn("%s", qup->qfname);
432			}
433			close(fd);
434		}
435	}
436}
437
438/*
439 * Take a list of priviledges and get it edited.
440 */
441int
442editit(tmpf)
443	char *tmpf;
444{
445	long omask;
446	int pid, status;
447
448	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
449 top:
450	if ((pid = fork()) < 0) {
451
452		if (errno == EPROCLIM) {
453			warnx("you have too many processes");
454			return(0);
455		}
456		if (errno == EAGAIN) {
457			sleep(1);
458			goto top;
459		}
460		warn("fork");
461		return (0);
462	}
463	if (pid == 0) {
464		register const char *ed;
465
466		sigsetmask(omask);
467		setgid(getgid());
468		setuid(getuid());
469		if ((ed = getenv("EDITOR")) == (char *)0)
470			ed = _PATH_VI;
471		execlp(ed, ed, tmpf, (char *)0);
472		err(1, "%s", ed);
473	}
474	waitpid(pid, &status, 0);
475	sigsetmask(omask);
476	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
477		return (0);
478	return (1);
479}
480
481/*
482 * Convert a quotause list to an ASCII file.
483 */
484int
485writeprivs(quplist, outfd, name, quotatype)
486	struct quotause *quplist;
487	int outfd;
488	char *name;
489	int quotatype;
490{
491	register struct quotause *qup;
492	FILE *fd;
493
494	ftruncate(outfd, 0);
495	lseek(outfd, 0, L_SET);
496	if ((fd = fdopen(dup(outfd), "w")) == NULL)
497		err(1, "%s", tmpfil);
498	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
499	for (qup = quplist; qup; qup = qup->next) {
500		fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
501		    qup->fsname, "kbytes in use:",
502		    (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
503		    (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
504		    (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
505		fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
506		    "\tinodes in use:",
507		    (unsigned long)qup->dqblk.dqb_curinodes,
508		    (unsigned long)qup->dqblk.dqb_isoftlimit,
509		    (unsigned long)qup->dqblk.dqb_ihardlimit);
510	}
511	fclose(fd);
512	return (1);
513}
514
515/*
516 * Merge changes to an ASCII file into a quotause list.
517 */
518int
519readprivs(quplist, inname)
520	struct quotause *quplist;
521	char *inname;
522{
523	register struct quotause *qup;
524	FILE *fd;
525	unsigned long bhardlimit, bsoftlimit, curblocks;
526	unsigned long ihardlimit, isoftlimit, curinodes;
527	int cnt;
528	register char *cp;
529	struct dqblk dqblk;
530	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
531
532	fd = fopen(inname, "r");
533	if (fd == NULL) {
534		warnx("can't re-read temp file!!");
535		return (0);
536	}
537	/*
538	 * Discard title line, then read pairs of lines to process.
539	 */
540	(void) fgets(line1, sizeof (line1), fd);
541	while (fgets(line1, sizeof (line1), fd) != NULL &&
542	       fgets(line2, sizeof (line2), fd) != NULL) {
543		if ((fsp = strtok(line1, " \t:")) == NULL) {
544			warnx("%s: bad format", line1);
545			return (0);
546		}
547		if ((cp = strtok((char *)0, "\n")) == NULL) {
548			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
549			return (0);
550		}
551		cnt = sscanf(cp,
552		    " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
553		    &curblocks, &bsoftlimit, &bhardlimit);
554		if (cnt != 3) {
555			warnx("%s:%s: bad format", fsp, cp);
556			return (0);
557		}
558		dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
559		dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
560		dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
561		if ((cp = strtok(line2, "\n")) == NULL) {
562			warnx("%s: %s: bad format", fsp, line2);
563			return (0);
564		}
565		cnt = sscanf(cp,
566		    "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
567		    &curinodes, &isoftlimit, &ihardlimit);
568		if (cnt != 3) {
569			warnx("%s: %s: bad format", fsp, line2);
570			return (0);
571		}
572		dqblk.dqb_curinodes = curinodes;
573		dqblk.dqb_isoftlimit = isoftlimit;
574		dqblk.dqb_ihardlimit = ihardlimit;
575		for (qup = quplist; qup; qup = qup->next) {
576			if (strcmp(fsp, qup->fsname))
577				continue;
578			/*
579			 * Cause time limit to be reset when the quota
580			 * is next used if previously had no soft limit
581			 * or were under it, but now have a soft limit
582			 * and are over it.
583			 */
584			if (dqblk.dqb_bsoftlimit &&
585			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
586			    (qup->dqblk.dqb_bsoftlimit == 0 ||
587			     qup->dqblk.dqb_curblocks <
588			     qup->dqblk.dqb_bsoftlimit))
589				qup->dqblk.dqb_btime = 0;
590			if (dqblk.dqb_isoftlimit &&
591			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
592			    (qup->dqblk.dqb_isoftlimit == 0 ||
593			     qup->dqblk.dqb_curinodes <
594			     qup->dqblk.dqb_isoftlimit))
595				qup->dqblk.dqb_itime = 0;
596			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
597			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
598			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
599			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
600			qup->flags |= FOUND;
601			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
602			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
603				break;
604			warnx("%s: cannot change current allocation", fsp);
605			break;
606		}
607	}
608	fclose(fd);
609	/*
610	 * Disable quotas for any filesystems that have not been found.
611	 */
612	for (qup = quplist; qup; qup = qup->next) {
613		if (qup->flags & FOUND) {
614			qup->flags &= ~FOUND;
615			continue;
616		}
617		qup->dqblk.dqb_bsoftlimit = 0;
618		qup->dqblk.dqb_bhardlimit = 0;
619		qup->dqblk.dqb_isoftlimit = 0;
620		qup->dqblk.dqb_ihardlimit = 0;
621	}
622	return (1);
623}
624
625/*
626 * Convert a quotause list to an ASCII file of grace times.
627 */
628int
629writetimes(quplist, outfd, quotatype)
630	struct quotause *quplist;
631	int outfd;
632	int quotatype;
633{
634	register struct quotause *qup;
635	FILE *fd;
636
637	ftruncate(outfd, 0);
638	lseek(outfd, 0, L_SET);
639	if ((fd = fdopen(dup(outfd), "w")) == NULL)
640		err(1, "%s", tmpfil);
641	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
642	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
643	    qfextension[quotatype]);
644	for (qup = quplist; qup; qup = qup->next) {
645		fprintf(fd, "%s: block grace period: %s, ",
646		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
647		fprintf(fd, "file grace period: %s\n",
648		    cvtstoa(qup->dqblk.dqb_itime));
649	}
650	fclose(fd);
651	return (1);
652}
653
654/*
655 * Merge changes of grace times in an ASCII file into a quotause list.
656 */
657int
658readtimes(quplist, inname)
659	struct quotause *quplist;
660	char *inname;
661{
662	register struct quotause *qup;
663	FILE *fd;
664	int cnt;
665	register char *cp;
666	time_t itime, btime, iseconds, bseconds;
667	long l_itime, l_btime;
668	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
669
670	fd = fopen(inname, "r");
671	if (fd == NULL) {
672		warnx("can't re-read temp file!!");
673		return (0);
674	}
675	/*
676	 * Discard two title lines, then read lines to process.
677	 */
678	(void) fgets(line1, sizeof (line1), fd);
679	(void) fgets(line1, sizeof (line1), fd);
680	while (fgets(line1, sizeof (line1), fd) != NULL) {
681		if ((fsp = strtok(line1, " \t:")) == NULL) {
682			warnx("%s: bad format", line1);
683			return (0);
684		}
685		if ((cp = strtok((char *)0, "\n")) == NULL) {
686			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
687			return (0);
688		}
689		cnt = sscanf(cp,
690		    " block grace period: %ld %s file grace period: %ld %s",
691		    &l_btime, bunits, &l_itime, iunits);
692		if (cnt != 4) {
693			warnx("%s:%s: bad format", fsp, cp);
694			return (0);
695		}
696		btime = l_btime;
697		itime = l_itime;
698		if (cvtatos(btime, bunits, &bseconds) == 0)
699			return (0);
700		if (cvtatos(itime, iunits, &iseconds) == 0)
701			return (0);
702		for (qup = quplist; qup; qup = qup->next) {
703			if (strcmp(fsp, qup->fsname))
704				continue;
705			qup->dqblk.dqb_btime = bseconds;
706			qup->dqblk.dqb_itime = iseconds;
707			qup->flags |= FOUND;
708			break;
709		}
710	}
711	fclose(fd);
712	/*
713	 * reset default grace periods for any filesystems
714	 * that have not been found.
715	 */
716	for (qup = quplist; qup; qup = qup->next) {
717		if (qup->flags & FOUND) {
718			qup->flags &= ~FOUND;
719			continue;
720		}
721		qup->dqblk.dqb_btime = 0;
722		qup->dqblk.dqb_itime = 0;
723	}
724	return (1);
725}
726
727/*
728 * Convert seconds to ASCII times.
729 */
730char *
731cvtstoa(secs)
732	time_t secs;
733{
734	static char buf[20];
735
736	if (secs % (24 * 60 * 60) == 0) {
737		secs /= 24 * 60 * 60;
738		sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
739	} else if (secs % (60 * 60) == 0) {
740		secs /= 60 * 60;
741		sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
742	} else if (secs % 60 == 0) {
743		secs /= 60;
744		sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
745	} else
746		sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
747	return (buf);
748}
749
750/*
751 * Convert ASCII input times to seconds.
752 */
753int
754cvtatos(period, units, seconds)
755	time_t period;
756	char *units;
757	time_t *seconds;
758{
759
760	if (bcmp(units, "second", 6) == 0)
761		*seconds = period;
762	else if (bcmp(units, "minute", 6) == 0)
763		*seconds = period * 60;
764	else if (bcmp(units, "hour", 4) == 0)
765		*seconds = period * 60 * 60;
766	else if (bcmp(units, "day", 3) == 0)
767		*seconds = period * 24 * 60 * 60;
768	else {
769		printf("%s: bad units, specify %s\n", units,
770		    "days, hours, minutes, or seconds");
771		return (0);
772	}
773	return (1);
774}
775
776/*
777 * Free a list of quotause structures.
778 */
779void
780freeprivs(quplist)
781	struct quotause *quplist;
782{
783	register struct quotause *qup, *nextqup;
784
785	for (qup = quplist; qup; qup = nextqup) {
786		nextqup = qup->next;
787		free(qup);
788	}
789}
790
791/*
792 * Check whether a string is completely composed of digits.
793 */
794int
795alldigits(s)
796	register const char *s;
797{
798	register int c;
799
800	c = *s++;
801	do {
802		if (!isdigit(c))
803			return (0);
804	} while ((c = *s++));
805	return (1);
806}
807
808/*
809 * Check to see if a particular quota is to be enabled.
810 */
811int
812hasquota(fs, type, qfnamep)
813	register struct fstab *fs;
814	int type;
815	char **qfnamep;
816{
817	register char *opt;
818	char *cp;
819	static char initname, usrname[100], grpname[100];
820	static char buf[BUFSIZ];
821
822	if (!initname) {
823		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
824		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
825		initname = 1;
826	}
827	strcpy(buf, fs->fs_mntops);
828	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
829		if ((cp = index(opt, '=')))
830			*cp++ = '\0';
831		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
832			break;
833		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
834			break;
835	}
836	if (!opt)
837		return (0);
838	if (cp) {
839		*qfnamep = cp;
840		return (1);
841	}
842	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
843	*qfnamep = buf;
844	return (1);
845}
846