xref: /freebsd/usr.sbin/edquota/edquota.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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()
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(name, quotatype)
303 	const char *name;
304 	int quotatype;
305 {
306 	struct passwd *pw;
307 	struct group *gr;
308 
309 	if (alldigits(name))
310 		return (atoi(name));
311 	switch(quotatype) {
312 	case USRQUOTA:
313 		if ((pw = getpwnam(name)))
314 			return (pw->pw_uid);
315 		warnx("%s: no such user", name);
316 		break;
317 	case GRPQUOTA:
318 		if ((gr = getgrnam(name)))
319 			return (gr->gr_gid);
320 		warnx("%s: no such group", name);
321 		break;
322 	default:
323 		warnx("%d: unknown quota type", quotatype);
324 		break;
325 	}
326 	sleep(1);
327 	return (-1);
328 }
329 
330 /*
331  * Collect the requested quota information.
332  */
333 struct quotause *
334 getprivs(id, quotatype, fspath)
335 	register long id;
336 	int quotatype;
337 	char *fspath;
338 {
339 	register struct fstab *fs;
340 	register struct quotause *qup, *quptail;
341 	struct quotause *quphead;
342 	int qcmd, qupsize, fd;
343 	char *qfpathname;
344 	static int warned = 0;
345 
346 	setfsent();
347 	quphead = quptail = NULL;
348 	qcmd = QCMD(Q_GETQUOTA, quotatype);
349 	while ((fs = getfsent())) {
350 		if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
351 		    strcmp(fspath, fs->fs_file))
352 			continue;
353 		if (strcmp(fs->fs_vfstype, "ufs"))
354 			continue;
355 		if (!hasquota(fs, quotatype, &qfpathname))
356 			continue;
357 		qupsize = sizeof(*qup) + strlen(qfpathname);
358 		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
359 			errx(2, "out of memory");
360 		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
361 	    		if (errno == EOPNOTSUPP && !warned) {
362 				warned++;
363 		warnx("warning: quotas are not compiled into this kernel");
364 				sleep(3);
365 			}
366 			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
367 				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
368 				if (fd < 0 && errno != ENOENT) {
369 					warn("%s", qfpathname);
370 					free(qup);
371 					continue;
372 				}
373 				warnx("creating quota file %s", qfpathname);
374 				sleep(3);
375 				(void) fchown(fd, getuid(),
376 				    getentry(quotagroup, GRPQUOTA));
377 				(void) fchmod(fd, 0640);
378 			}
379 			if (lseek(fd, (off_t)id * sizeof(struct dqblk),
380 			    L_SET) < 0) {
381 				warn("seek error on %s", qfpathname);
382 				close(fd);
383 				free(qup);
384 				continue;
385 			}
386 			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
387 			case 0:			/* EOF */
388 				/*
389 				 * Convert implicit 0 quota (EOF)
390 				 * into an explicit one (zero'ed dqblk)
391 				 */
392 				bzero((caddr_t)&qup->dqblk,
393 				    sizeof(struct dqblk));
394 				break;
395 
396 			case sizeof(struct dqblk):	/* OK */
397 				break;
398 
399 			default:		/* ERROR */
400 				warn("read error in %s", qfpathname);
401 				close(fd);
402 				free(qup);
403 				continue;
404 			}
405 			close(fd);
406 		}
407 		strcpy(qup->qfname, qfpathname);
408 		strcpy(qup->fsname, fs->fs_file);
409 		if (quphead == NULL)
410 			quphead = qup;
411 		else
412 			quptail->next = qup;
413 		quptail = qup;
414 		qup->next = 0;
415 	}
416 	endfsent();
417 	return (quphead);
418 }
419 
420 /*
421  * Store the requested quota information.
422  */
423 void
424 putprivs(id, quotatype, quplist)
425 	long id;
426 	int quotatype;
427 	struct quotause *quplist;
428 {
429 	register struct quotause *qup;
430 	int qcmd, fd;
431 	struct dqblk dqbuf;
432 
433 	qcmd = QCMD(Q_SETQUOTA, quotatype);
434 	for (qup = quplist; qup; qup = qup->next) {
435 		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
436 			continue;
437 		if ((fd = open(qup->qfname, O_RDWR)) < 0) {
438 			warn("%s", qup->qfname);
439 			continue;
440 		}
441 		if (lseek(fd, (off_t)id * sizeof(struct dqblk), L_SET) < 0) {
442 			warn("seek error on %s", qup->qfname);
443 			close(fd);
444 			continue;
445 		}
446 		switch (read(fd, &dqbuf, sizeof(struct dqblk))) {
447 		case 0:			/* EOF */
448 			/*
449 			 * Convert implicit 0 quota (EOF)
450 			 * into an explicit one (zero'ed dqblk)
451 			 */
452 			bzero(&dqbuf, sizeof(struct dqblk));
453 			break;
454 
455 		case sizeof(struct dqblk):	/* OK */
456 			break;
457 
458 		default:		/* ERROR */
459 			warn("read error in %s", qup->qfname);
460 			close(fd);
461 			continue;
462 		}
463 		/*
464 		 * Reset time limit if have a soft limit and were
465 		 * previously under it, but are now over it
466 		 * or if there previously was no soft limit, but
467 		 * now have one and are over it.
468 		 */
469 		if (dqbuf.dqb_bsoftlimit && id != 0 &&
470 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
471 		    dqbuf.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit)
472 			qup->dqblk.dqb_btime = 0;
473 		if (dqbuf.dqb_bsoftlimit == 0 && id != 0 &&
474 		    dqbuf.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit)
475 			qup->dqblk.dqb_btime = 0;
476 		if (dqbuf.dqb_isoftlimit && id != 0 &&
477 		    dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
478 		    dqbuf.dqb_curinodes >= qup->dqblk.dqb_isoftlimit)
479 			qup->dqblk.dqb_itime = 0;
480 		if (dqbuf.dqb_isoftlimit == 0 && id !=0 &&
481 		    dqbuf.dqb_curinodes >= qup->dqblk.dqb_isoftlimit)
482 			qup->dqblk.dqb_itime = 0;
483 		qup->dqblk.dqb_curinodes = dqbuf.dqb_curinodes;
484 		qup->dqblk.dqb_curblocks = dqbuf.dqb_curblocks;
485 		if (lseek(fd, (off_t)id * sizeof(struct dqblk), L_SET) < 0) {
486 			warn("seek error on %s", qup->qfname);
487 			close(fd);
488 			continue;
489 		}
490 		if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
491 		    sizeof (struct dqblk)) {
492 			warn("%s", qup->qfname);
493 			}
494 		close(fd);
495 	}
496 }
497 
498 /*
499  * Take a list of priviledges and get it edited.
500  */
501 int
502 editit(tmpf)
503 	char *tmpf;
504 {
505 	long omask;
506 	int pid, status;
507 
508 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
509  top:
510 	if ((pid = fork()) < 0) {
511 
512 		if (errno == EPROCLIM) {
513 			warnx("you have too many processes");
514 			return(0);
515 		}
516 		if (errno == EAGAIN) {
517 			sleep(1);
518 			goto top;
519 		}
520 		warn("fork");
521 		return (0);
522 	}
523 	if (pid == 0) {
524 		register const char *ed;
525 
526 		sigsetmask(omask);
527 		setgid(getgid());
528 		setuid(getuid());
529 		if ((ed = getenv("EDITOR")) == (char *)0)
530 			ed = _PATH_VI;
531 		execlp(ed, ed, tmpf, (char *)0);
532 		err(1, "%s", ed);
533 	}
534 	waitpid(pid, &status, 0);
535 	sigsetmask(omask);
536 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
537 		return (0);
538 	return (1);
539 }
540 
541 /*
542  * Convert a quotause list to an ASCII file.
543  */
544 int
545 writeprivs(quplist, outfd, name, quotatype)
546 	struct quotause *quplist;
547 	int outfd;
548 	char *name;
549 	int quotatype;
550 {
551 	register struct quotause *qup;
552 	FILE *fd;
553 
554 	ftruncate(outfd, 0);
555 	lseek(outfd, 0, L_SET);
556 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
557 		err(1, "%s", tmpfil);
558 	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
559 	for (qup = quplist; qup; qup = qup->next) {
560 		fprintf(fd, "%s: %s %lu, limits (soft = %lu, hard = %lu)\n",
561 		    qup->fsname, "kbytes in use:",
562 		    (unsigned long)(dbtob(qup->dqblk.dqb_curblocks) / 1024),
563 		    (unsigned long)(dbtob(qup->dqblk.dqb_bsoftlimit) / 1024),
564 		    (unsigned long)(dbtob(qup->dqblk.dqb_bhardlimit) / 1024));
565 		fprintf(fd, "%s %lu, limits (soft = %lu, hard = %lu)\n",
566 		    "\tinodes in use:",
567 		    (unsigned long)qup->dqblk.dqb_curinodes,
568 		    (unsigned long)qup->dqblk.dqb_isoftlimit,
569 		    (unsigned long)qup->dqblk.dqb_ihardlimit);
570 	}
571 	fclose(fd);
572 	return (1);
573 }
574 
575 /*
576  * Merge changes to an ASCII file into a quotause list.
577  */
578 int
579 readprivs(quplist, inname)
580 	struct quotause *quplist;
581 	char *inname;
582 {
583 	register struct quotause *qup;
584 	FILE *fd;
585 	unsigned long bhardlimit, bsoftlimit, curblocks;
586 	unsigned long ihardlimit, isoftlimit, curinodes;
587 	int cnt;
588 	register char *cp;
589 	struct dqblk dqblk;
590 	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
591 
592 	fd = fopen(inname, "r");
593 	if (fd == NULL) {
594 		warnx("can't re-read temp file!!");
595 		return (0);
596 	}
597 	/*
598 	 * Discard title line, then read pairs of lines to process.
599 	 */
600 	(void) fgets(line1, sizeof (line1), fd);
601 	while (fgets(line1, sizeof (line1), fd) != NULL &&
602 	       fgets(line2, sizeof (line2), fd) != NULL) {
603 		if ((fsp = strtok(line1, " \t:")) == NULL) {
604 			warnx("%s: bad format", line1);
605 			return (0);
606 		}
607 		if ((cp = strtok((char *)0, "\n")) == NULL) {
608 			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
609 			return (0);
610 		}
611 		cnt = sscanf(cp,
612 		    " kbytes in use: %lu, limits (soft = %lu, hard = %lu)",
613 		    &curblocks, &bsoftlimit, &bhardlimit);
614 		if (cnt != 3) {
615 			warnx("%s:%s: bad format", fsp, cp);
616 			return (0);
617 		}
618 		dqblk.dqb_curblocks = btodb((off_t)curblocks * 1024);
619 		dqblk.dqb_bsoftlimit = btodb((off_t)bsoftlimit * 1024);
620 		dqblk.dqb_bhardlimit = btodb((off_t)bhardlimit * 1024);
621 		if ((cp = strtok(line2, "\n")) == NULL) {
622 			warnx("%s: %s: bad format", fsp, line2);
623 			return (0);
624 		}
625 		cnt = sscanf(cp,
626 		    "\tinodes in use: %lu, limits (soft = %lu, hard = %lu)",
627 		    &curinodes, &isoftlimit, &ihardlimit);
628 		if (cnt != 3) {
629 			warnx("%s: %s: bad format", fsp, line2);
630 			return (0);
631 		}
632 		dqblk.dqb_curinodes = curinodes;
633 		dqblk.dqb_isoftlimit = isoftlimit;
634 		dqblk.dqb_ihardlimit = ihardlimit;
635 		for (qup = quplist; qup; qup = qup->next) {
636 			if (strcmp(fsp, qup->fsname))
637 				continue;
638 			/*
639 			 * Cause time limit to be reset when the quota
640 			 * is next used if previously had no soft limit
641 			 * or were under it, but now have a soft limit
642 			 * and are over it.
643 			 */
644 			if (dqblk.dqb_bsoftlimit &&
645 			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
646 			    (qup->dqblk.dqb_bsoftlimit == 0 ||
647 			     qup->dqblk.dqb_curblocks <
648 			     qup->dqblk.dqb_bsoftlimit))
649 				qup->dqblk.dqb_btime = 0;
650 			if (dqblk.dqb_isoftlimit &&
651 			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
652 			    (qup->dqblk.dqb_isoftlimit == 0 ||
653 			     qup->dqblk.dqb_curinodes <
654 			     qup->dqblk.dqb_isoftlimit))
655 				qup->dqblk.dqb_itime = 0;
656 			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
657 			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
658 			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
659 			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
660 			qup->flags |= FOUND;
661 			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
662 			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
663 				break;
664 			warnx("%s: cannot change current allocation", fsp);
665 			break;
666 		}
667 	}
668 	fclose(fd);
669 	/*
670 	 * Disable quotas for any filesystems that have not been found.
671 	 */
672 	for (qup = quplist; qup; qup = qup->next) {
673 		if (qup->flags & FOUND) {
674 			qup->flags &= ~FOUND;
675 			continue;
676 		}
677 		qup->dqblk.dqb_bsoftlimit = 0;
678 		qup->dqblk.dqb_bhardlimit = 0;
679 		qup->dqblk.dqb_isoftlimit = 0;
680 		qup->dqblk.dqb_ihardlimit = 0;
681 	}
682 	return (1);
683 }
684 
685 /*
686  * Convert a quotause list to an ASCII file of grace times.
687  */
688 int
689 writetimes(quplist, outfd, quotatype)
690 	struct quotause *quplist;
691 	int outfd;
692 	int quotatype;
693 {
694 	register struct quotause *qup;
695 	FILE *fd;
696 
697 	ftruncate(outfd, 0);
698 	lseek(outfd, 0, L_SET);
699 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
700 		err(1, "%s", tmpfil);
701 	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
702 	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
703 	    qfextension[quotatype]);
704 	for (qup = quplist; qup; qup = qup->next) {
705 		fprintf(fd, "%s: block grace period: %s, ",
706 		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
707 		fprintf(fd, "file grace period: %s\n",
708 		    cvtstoa(qup->dqblk.dqb_itime));
709 	}
710 	fclose(fd);
711 	return (1);
712 }
713 
714 /*
715  * Merge changes of grace times in an ASCII file into a quotause list.
716  */
717 int
718 readtimes(quplist, inname)
719 	struct quotause *quplist;
720 	char *inname;
721 {
722 	register struct quotause *qup;
723 	FILE *fd;
724 	int cnt;
725 	register char *cp;
726 	time_t itime, btime, iseconds, bseconds;
727 	long l_itime, l_btime;
728 	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
729 
730 	fd = fopen(inname, "r");
731 	if (fd == NULL) {
732 		warnx("can't re-read temp file!!");
733 		return (0);
734 	}
735 	/*
736 	 * Discard two title lines, then read lines to process.
737 	 */
738 	(void) fgets(line1, sizeof (line1), fd);
739 	(void) fgets(line1, sizeof (line1), fd);
740 	while (fgets(line1, sizeof (line1), fd) != NULL) {
741 		if ((fsp = strtok(line1, " \t:")) == NULL) {
742 			warnx("%s: bad format", line1);
743 			return (0);
744 		}
745 		if ((cp = strtok((char *)0, "\n")) == NULL) {
746 			warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
747 			return (0);
748 		}
749 		cnt = sscanf(cp,
750 		    " block grace period: %ld %s file grace period: %ld %s",
751 		    &l_btime, bunits, &l_itime, iunits);
752 		if (cnt != 4) {
753 			warnx("%s:%s: bad format", fsp, cp);
754 			return (0);
755 		}
756 		btime = l_btime;
757 		itime = l_itime;
758 		if (cvtatos(btime, bunits, &bseconds) == 0)
759 			return (0);
760 		if (cvtatos(itime, iunits, &iseconds) == 0)
761 			return (0);
762 		for (qup = quplist; qup; qup = qup->next) {
763 			if (strcmp(fsp, qup->fsname))
764 				continue;
765 			qup->dqblk.dqb_btime = bseconds;
766 			qup->dqblk.dqb_itime = iseconds;
767 			qup->flags |= FOUND;
768 			break;
769 		}
770 	}
771 	fclose(fd);
772 	/*
773 	 * reset default grace periods for any filesystems
774 	 * that have not been found.
775 	 */
776 	for (qup = quplist; qup; qup = qup->next) {
777 		if (qup->flags & FOUND) {
778 			qup->flags &= ~FOUND;
779 			continue;
780 		}
781 		qup->dqblk.dqb_btime = 0;
782 		qup->dqblk.dqb_itime = 0;
783 	}
784 	return (1);
785 }
786 
787 /*
788  * Convert seconds to ASCII times.
789  */
790 char *
791 cvtstoa(secs)
792 	time_t secs;
793 {
794 	static char buf[20];
795 
796 	if (secs % (24 * 60 * 60) == 0) {
797 		secs /= 24 * 60 * 60;
798 		sprintf(buf, "%ld day%s", (long)secs, secs == 1 ? "" : "s");
799 	} else if (secs % (60 * 60) == 0) {
800 		secs /= 60 * 60;
801 		sprintf(buf, "%ld hour%s", (long)secs, secs == 1 ? "" : "s");
802 	} else if (secs % 60 == 0) {
803 		secs /= 60;
804 		sprintf(buf, "%ld minute%s", (long)secs, secs == 1 ? "" : "s");
805 	} else
806 		sprintf(buf, "%ld second%s", (long)secs, secs == 1 ? "" : "s");
807 	return (buf);
808 }
809 
810 /*
811  * Convert ASCII input times to seconds.
812  */
813 int
814 cvtatos(period, units, seconds)
815 	time_t period;
816 	char *units;
817 	time_t *seconds;
818 {
819 
820 	if (bcmp(units, "second", 6) == 0)
821 		*seconds = period;
822 	else if (bcmp(units, "minute", 6) == 0)
823 		*seconds = period * 60;
824 	else if (bcmp(units, "hour", 4) == 0)
825 		*seconds = period * 60 * 60;
826 	else if (bcmp(units, "day", 3) == 0)
827 		*seconds = period * 24 * 60 * 60;
828 	else {
829 		printf("%s: bad units, specify %s\n", units,
830 		    "days, hours, minutes, or seconds");
831 		return (0);
832 	}
833 	return (1);
834 }
835 
836 /*
837  * Free a list of quotause structures.
838  */
839 void
840 freeprivs(quplist)
841 	struct quotause *quplist;
842 {
843 	register struct quotause *qup, *nextqup;
844 
845 	for (qup = quplist; qup; qup = nextqup) {
846 		nextqup = qup->next;
847 		free(qup);
848 	}
849 }
850 
851 /*
852  * Check whether a string is completely composed of digits.
853  */
854 int
855 alldigits(s)
856 	register const char *s;
857 {
858 	register int c;
859 
860 	c = *s++;
861 	do {
862 		if (!isdigit(c))
863 			return (0);
864 	} while ((c = *s++));
865 	return (1);
866 }
867 
868 /*
869  * Check to see if a particular quota is to be enabled.
870  */
871 int
872 hasquota(fs, type, qfnamep)
873 	struct fstab *fs;
874 	int type;
875 	char **qfnamep;
876 {
877 	char *opt;
878 	char *cp;
879 	struct statfs sfb;
880 	static char initname, usrname[100], grpname[100];
881 	static char buf[BUFSIZ];
882 
883 	if (!initname) {
884 		(void)snprintf(usrname, sizeof(usrname), "%s%s",
885 		    qfextension[USRQUOTA], qfname);
886 		(void)snprintf(grpname, sizeof(grpname), "%s%s",
887 		    qfextension[GRPQUOTA], qfname);
888 		initname = 1;
889 	}
890 	strcpy(buf, fs->fs_mntops);
891 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
892 		if ((cp = index(opt, '=')))
893 			*cp++ = '\0';
894 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
895 			break;
896 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
897 			break;
898 	}
899 	if (!opt)
900 		return (0);
901 	if (cp)
902 		*qfnamep = cp;
903 	else {
904 		(void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
905 		    qfname, qfextension[type]);
906 		*qfnamep = buf;
907 	}
908 	if (statfs(fs->fs_file, &sfb) != 0) {
909 		warn("cannot statfs mount point %s", fs->fs_file);
910 		return (0);
911 	}
912 	if (strcmp(fs->fs_file, sfb.f_mntonname)) {
913 		warnx("%s not mounted for %s quotas", fs->fs_file,
914 		    type == USRQUOTA ? "user" : "group");
915 		sleep(3);
916 		return (0);
917 	}
918 	return (1);
919 }
920