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