Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.37
      1 /*      $NetBSD: edquota.c,v 1.37 2011/11/13 15:41:34 dholland Exp $ */
      2 /*
      3  * Copyright (c) 1980, 1990, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Robert Elz at The University of Melbourne.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
     37  The Regents of the University of California.  All rights reserved.");
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "from: @(#)edquota.c	8.3 (Berkeley) 4/27/95";
     43 #else
     44 __RCSID("$NetBSD: edquota.c,v 1.37 2011/11/13 15:41:34 dholland Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  * Disk quota editor.
     50  */
     51 #include <sys/param.h>
     52 #include <sys/stat.h>
     53 #include <sys/file.h>
     54 #include <sys/wait.h>
     55 #include <sys/queue.h>
     56 #include <sys/types.h>
     57 #include <sys/statvfs.h>
     58 
     59 #include <quota/quotaprop.h>
     60 #include <quota/quota.h>
     61 #include <ufs/ufs/quota1.h>
     62 #include <sys/quota.h>
     63 
     64 #include <assert.h>
     65 #include <err.h>
     66 #include <errno.h>
     67 #include <fstab.h>
     68 #include <pwd.h>
     69 #include <grp.h>
     70 #include <ctype.h>
     71 #include <signal.h>
     72 #include <stdbool.h>
     73 #include <stdio.h>
     74 #include <stdlib.h>
     75 #include <string.h>
     76 #include <unistd.h>
     77 
     78 #include "printquota.h"
     79 #include "getvfsquota.h"
     80 #include "quotautil.h"
     81 
     82 #include "pathnames.h"
     83 
     84 static const char *quotagroup = QUOTAGROUP;
     85 static char tmpfil[] = _PATH_TMPFILE;
     86 
     87 #define MAX_TMPSTR	(100+MAXPATHLEN)
     88 
     89 /* flags for quotause */
     90 #define	FOUND	0x01
     91 #define	QUOTA2	0x02
     92 #define	DEFAULT	0x04
     93 
     94 struct quotause {
     95 	struct	quotause *next;
     96 	long	flags;
     97 	struct	ufs_quota_entry qe[QUOTA_NLIMITS];
     98 	char	fsname[MAXPATHLEN + 1];
     99 	char	*qfname;
    100 };
    101 
    102 struct quotalist {
    103 	struct quotause *head;
    104 	struct quotause *tail;
    105 };
    106 
    107 static void	usage(void) __dead;
    108 
    109 static int Hflag = 0;
    110 static int Dflag = 0;
    111 static int dflag = 0;
    112 
    113 /* more compact form of constants */
    114 #define QL_BLK QUOTA_LIMIT_BLOCK
    115 #define QL_FL  QUOTA_LIMIT_FILE
    116 
    117 ////////////////////////////////////////////////////////////
    118 // support code
    119 
    120 /*
    121  * This routine converts a name for a particular quota class to
    122  * an identifier. This routine must agree with the kernel routine
    123  * getinoquota as to the interpretation of quota classes.
    124  */
    125 static int
    126 getidbyname(const char *name, int quotaclass)
    127 {
    128 	struct passwd *pw;
    129 	struct group *gr;
    130 
    131 	if (alldigits(name))
    132 		return atoi(name);
    133 	switch(quotaclass) {
    134 	case QUOTA_CLASS_USER:
    135 		if ((pw = getpwnam(name)) != NULL)
    136 			return pw->pw_uid;
    137 		warnx("%s: no such user", name);
    138 		break;
    139 	case QUOTA_CLASS_GROUP:
    140 		if ((gr = getgrnam(name)) != NULL)
    141 			return gr->gr_gid;
    142 		warnx("%s: no such group", name);
    143 		break;
    144 	default:
    145 		warnx("%d: unknown quota type", quotaclass);
    146 		break;
    147 	}
    148 	sleep(1);
    149 	return -1;
    150 }
    151 
    152 ////////////////////////////////////////////////////////////
    153 // quotause operations
    154 
    155 /*
    156  * Create an empty quotause structure.
    157  */
    158 static struct quotause *
    159 quotause_create(void)
    160 {
    161 	struct quotause *qup;
    162 
    163 	qup = malloc(sizeof(*qup));
    164 	if (qup == NULL) {
    165 		err(1, "malloc");
    166 	}
    167 
    168 	qup->next = NULL;
    169 	qup->flags = 0;
    170 	memset(qup->qe, 0, sizeof(qup->qe));
    171 	qup->fsname[0] = '\0';
    172 	qup->qfname = NULL;
    173 
    174 	return qup;
    175 }
    176 
    177 /*
    178  * Free a quotause structure.
    179  */
    180 static void
    181 quotause_destroy(struct quotause *qup)
    182 {
    183 	free(qup->qfname);
    184 	free(qup);
    185 }
    186 
    187 ////////////////////////////////////////////////////////////
    188 // quotalist operations
    189 
    190 /*
    191  * Create a quotause list.
    192  */
    193 static struct quotalist *
    194 quotalist_create(void)
    195 {
    196 	struct quotalist *qlist;
    197 
    198 	qlist = malloc(sizeof(*qlist));
    199 	if (qlist == NULL) {
    200 		err(1, "malloc");
    201 	}
    202 
    203 	qlist->head = NULL;
    204 	qlist->tail = NULL;
    205 
    206 	return qlist;
    207 }
    208 
    209 /*
    210  * Free a list of quotause structures.
    211  */
    212 static void
    213 quotalist_destroy(struct quotalist *qlist)
    214 {
    215 	struct quotause *qup, *nextqup;
    216 
    217 	for (qup = qlist->head; qup; qup = nextqup) {
    218 		nextqup = qup->next;
    219 		quotause_destroy(qup);
    220 	}
    221 	free(qlist);
    222 }
    223 
    224 static bool
    225 quotalist_empty(struct quotalist *qlist)
    226 {
    227 	return qlist->head == NULL;
    228 }
    229 
    230 static void
    231 quotalist_append(struct quotalist *qlist, struct quotause *qup)
    232 {
    233 	/* should not already be on a list */
    234 	assert(qup->next == NULL);
    235 
    236 	if (qlist->head == NULL) {
    237 		qlist->head = qup;
    238 	} else {
    239 		qlist->tail->next = qup;
    240 	}
    241 	qlist->tail = qup;
    242 }
    243 
    244 ////////////////////////////////////////////////////////////
    245 // ffs quota v1
    246 
    247 static void
    248 putprivs1(uint32_t id, int quotaclass, struct quotause *qup)
    249 {
    250 	struct dqblk dqblk;
    251 	int fd;
    252 
    253 	ufsqe2dqblk(qup->qe, &dqblk);
    254 	assert((qup->flags & DEFAULT) == 0);
    255 
    256 	if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    257 		warnx("open `%s'", qup->qfname);
    258 	} else {
    259 		(void)lseek(fd,
    260 		    (off_t)(id * (long)sizeof (struct dqblk)),
    261 		    SEEK_SET);
    262 		if (write(fd, &dqblk, sizeof (struct dqblk)) !=
    263 		    sizeof (struct dqblk))
    264 			warnx("writing `%s'", qup->qfname);
    265 		close(fd);
    266 	}
    267 }
    268 
    269 static struct quotause *
    270 getprivs1(long id, int quotaclass, const char *filesys)
    271 {
    272 	struct fstab *fs;
    273 	char qfpathname[MAXPATHLEN];
    274 	struct quotause *qup;
    275 	struct dqblk dqblk;
    276 	int fd;
    277 
    278 	setfsent();
    279 	while ((fs = getfsent()) != NULL) {
    280 		if (strcmp(fs->fs_vfstype, "ffs"))
    281 			continue;
    282 		if (strcmp(fs->fs_spec, filesys) == 0 ||
    283 		    strcmp(fs->fs_file, filesys) == 0)
    284 			break;
    285 	}
    286 	if (fs == NULL)
    287 		return NULL;
    288 
    289 	if (!hasquota(qfpathname, sizeof(qfpathname), fs,
    290 	    ufsclass2qtype(quotaclass)))
    291 		return NULL;
    292 
    293 	qup = quotause_create();
    294 	strcpy(qup->fsname, fs->fs_file);
    295 	if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    296 		fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    297 		if (fd < 0 && errno != ENOENT) {
    298 			warnx("open `%s'", qfpathname);
    299 			quotause_destroy(qup);
    300 			return NULL;
    301 		}
    302 		warnx("Creating quota file %s", qfpathname);
    303 		sleep(3);
    304 		(void)fchown(fd, getuid(),
    305 		    getidbyname(quotagroup, QUOTA_CLASS_GROUP));
    306 		(void)fchmod(fd, 0640);
    307 	}
    308 	(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    309 	    SEEK_SET);
    310 	switch (read(fd, &dqblk, sizeof(struct dqblk))) {
    311 	case 0:			/* EOF */
    312 		/*
    313 		 * Convert implicit 0 quota (EOF)
    314 		 * into an explicit one (zero'ed dqblk)
    315 		 */
    316 		memset(&dqblk, 0, sizeof(struct dqblk));
    317 		break;
    318 
    319 	case sizeof(struct dqblk):	/* OK */
    320 		break;
    321 
    322 	default:		/* ERROR */
    323 		warn("read error in `%s'", qfpathname);
    324 		close(fd);
    325 		quotause_destroy(qup);
    326 		return NULL;
    327 	}
    328 	close(fd);
    329 	qup->qfname = qfpathname;
    330 	endfsent();
    331 	dqblk2ufsqe(&dqblk, qup->qe);
    332 	return qup;
    333 }
    334 
    335 ////////////////////////////////////////////////////////////
    336 // ffs quota v2
    337 
    338 static struct quotause *
    339 getprivs2(long id, int quotaclass, const char *filesys, int defaultq)
    340 {
    341 	struct quotause *qup;
    342 	int8_t version;
    343 
    344 	qup = quotause_create();
    345 	strcpy(qup->fsname, filesys);
    346 	if (defaultq)
    347 		qup->flags |= DEFAULT;
    348 	if (!getvfsquota(filesys, qup->qe, &version,
    349 	    id, quotaclass, defaultq, Dflag)) {
    350 		/* no entry, get default entry */
    351 		if (!getvfsquota(filesys, qup->qe, &version,
    352 		    id, quotaclass, 1, Dflag)) {
    353 			free(qup);
    354 			return NULL;
    355 		}
    356 	}
    357 	if (version == 2)
    358 		qup->flags |= QUOTA2;
    359 	return qup;
    360 }
    361 
    362 static void
    363 putprivs2(uint32_t id, int quotaclass, struct quotause *qup)
    364 {
    365 
    366 	prop_dictionary_t dict, data, cmd;
    367 	prop_array_t cmds, datas;
    368 	struct plistref pref;
    369 	int8_t error8;
    370 	uint64_t *valuesp[QUOTA_NLIMITS];
    371 
    372 	valuesp[QL_BLK] =
    373 	    &qup->qe[QL_BLK].ufsqe_hardlimit;
    374 	valuesp[QL_FL] =
    375 	    &qup->qe[QL_FL].ufsqe_hardlimit;
    376 
    377 	data = quota64toprop(id, (qup->flags & DEFAULT) ? 1 : 0,
    378 	    valuesp, ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    379 	    ufs_quota_limit_names, QUOTA_NLIMITS);
    380 
    381 	if (data == NULL)
    382 		err(1, "quota64toprop(id)");
    383 
    384 	dict = quota_prop_create();
    385 	cmds = prop_array_create();
    386 	datas = prop_array_create();
    387 
    388 	if (dict == NULL || cmds == NULL || datas == NULL) {
    389 		errx(1, "can't allocate proplist");
    390 	}
    391 
    392 	if (!prop_array_add_and_rel(datas, data))
    393 		err(1, "prop_array_add(data)");
    394 
    395 	if (!quota_prop_add_command(cmds, "set",
    396 	    ufs_quota_class_names[quotaclass], datas))
    397 		err(1, "prop_add_command");
    398 	if (!prop_dictionary_set(dict, "commands", cmds))
    399 		err(1, "prop_dictionary_set(command)");
    400 	if (Dflag)
    401 		printf("message to kernel:\n%s\n",
    402 		    prop_dictionary_externalize(dict));
    403 
    404 	if (prop_dictionary_send_syscall(dict, &pref) != 0)
    405 		err(1, "prop_dictionary_send_syscall");
    406 	prop_object_release(dict);
    407 
    408 	if (quotactl(qup->fsname, &pref) != 0)
    409 		err(1, "quotactl");
    410 
    411 	if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
    412 		err(1, "prop_dictionary_recv_syscall");
    413 	}
    414 
    415 	if (Dflag)
    416 		printf("reply from kernel:\n%s\n",
    417 		    prop_dictionary_externalize(dict));
    418 
    419 	if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
    420 		err(1, "quota_get_cmds");
    421 	}
    422 	/* only one command, no need to iter */
    423 	cmd = prop_array_get(cmds, 0);
    424 	if (cmd == NULL)
    425 		err(1, "prop_array_get(cmd)");
    426 
    427 	if (!prop_dictionary_get_int8(cmd, "return", &error8))
    428 		err(1, "prop_get(return)");
    429 
    430 	if (error8) {
    431 		errno = error8;
    432 		if (qup->flags & DEFAULT)
    433 			warn("set default %s quota",
    434 			    ufs_quota_class_names[quotaclass]);
    435 		else
    436 			warn("set %s quota for %u",
    437 			    ufs_quota_class_names[quotaclass], id);
    438 	}
    439 	prop_object_release(dict);
    440 }
    441 
    442 ////////////////////////////////////////////////////////////
    443 // quota format switch
    444 
    445 /*
    446  * Collect the requested quota information.
    447  */
    448 static struct quotalist *
    449 getprivs(long id, int quotaclass, const char *filesys, int defaultq)
    450 {
    451 	struct statvfs *fst;
    452 	int nfst, i;
    453 	struct quotalist *qlist;
    454 	struct quotause *qup;
    455 
    456 	qlist = quotalist_create();
    457 
    458 	nfst = getmntinfo(&fst, MNT_WAIT);
    459 	if (nfst == 0)
    460 		errx(1, "no filesystems mounted!");
    461 
    462 	for (i = 0; i < nfst; i++) {
    463 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    464 			continue;
    465 		if (filesys &&
    466 		    strcmp(fst[i].f_mntonname, filesys) != 0 &&
    467 		    strcmp(fst[i].f_mntfromname, filesys) != 0)
    468 			continue;
    469 		qup = getprivs2(id, quotaclass, fst[i].f_mntonname, defaultq);
    470 		if (qup == NULL) {
    471 			/*
    472 			 * XXX: returning NULL is totally wrong. On
    473 			 * serious error, abort; on minor error, warn
    474 			 * and continue.
    475 			 *
    476 			 * Note: we cannot warn unconditionally here
    477 			 * because this case apparently includes "no
    478 			 * quota entry on this volume" and that causes
    479 			 * the atf tests to fail. Bletch.
    480 			 */
    481 			/*return NULL;*/
    482 			/*warnx("getprivs2 failed");*/
    483 			continue;
    484 		}
    485 
    486 		quotalist_append(qlist, qup);
    487 	}
    488 
    489 	if (filesys && quotalist_empty(qlist)) {
    490 		if (defaultq)
    491 			errx(1, "no default quota for version 1");
    492 		/* if we get there, filesys is not mounted. try the old way */
    493 		qup = getprivs1(id, quotaclass, filesys);
    494 		if (qup == NULL) {
    495 			/* XXX. see above */
    496 			/*return NULL;*/
    497 			/*warnx("getprivs1 failed");*/
    498 			return qlist;
    499 		}
    500 		quotalist_append(qlist, qup);
    501 	}
    502 	return qlist;
    503 }
    504 
    505 /*
    506  * Store the requested quota information.
    507  */
    508 static void
    509 putprivs(uint32_t id, int quotaclass, struct quotalist *qlist)
    510 {
    511 	struct quotause *qup;
    512 
    513         for (qup = qlist->head; qup; qup = qup->next) {
    514 		if (qup->qfname == NULL)
    515 			putprivs2(id, quotaclass, qup);
    516 		else
    517 			putprivs1(id, quotaclass, qup);
    518 	}
    519 }
    520 
    521 static void
    522 clearpriv(int argc, char **argv, const char *filesys, int quotaclass)
    523 {
    524 	prop_array_t cmds, datas;
    525 	prop_dictionary_t protodict, dict, data, cmd;
    526 	struct plistref pref;
    527 	bool ret;
    528 	struct statvfs *fst;
    529 	int nfst, i;
    530 	int8_t error8;
    531 	int id;
    532 
    533 	/* build a generic command */
    534 	protodict = quota_prop_create();
    535 	cmds = prop_array_create();
    536 	datas = prop_array_create();
    537 	if (protodict == NULL || cmds == NULL || datas == NULL) {
    538 		errx(1, "can't allocate proplist");
    539 	}
    540 
    541 	for ( ; argc > 0; argc--, argv++) {
    542 		if ((id = getidbyname(*argv, quotaclass)) == -1)
    543 			continue;
    544 		data = prop_dictionary_create();
    545 		if (data == NULL)
    546 			errx(1, "can't allocate proplist");
    547 
    548 		ret = prop_dictionary_set_uint32(data, "id", id);
    549 		if (!ret)
    550 			err(1, "prop_dictionary_set(id)");
    551 		if (!prop_array_add_and_rel(datas, data))
    552 			err(1, "prop_array_add(data)");
    553 	}
    554 	if (!quota_prop_add_command(cmds, "clear",
    555 	    ufs_quota_class_names[quotaclass], datas))
    556 		err(1, "prop_add_command");
    557 
    558 	if (!prop_dictionary_set(protodict, "commands", cmds))
    559 		err(1, "prop_dictionary_set(command)");
    560 
    561 	/* now loop over quota-enabled filesystems */
    562 	nfst = getmntinfo(&fst, MNT_WAIT);
    563 	if (nfst == 0)
    564 		errx(1, "no filesystems mounted!");
    565 
    566 	for (i = 0; i < nfst; i++) {
    567 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    568 			continue;
    569 		if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
    570 		    strcmp(fst[i].f_mntfromname, filesys) != 0)
    571 			continue;
    572 		if (Dflag) {
    573 			fprintf(stderr, "message to kernel for %s:\n%s\n",
    574 			    fst[i].f_mntonname,
    575 			    prop_dictionary_externalize(protodict));
    576 		}
    577 
    578 		if (prop_dictionary_send_syscall(protodict, &pref) != 0)
    579 			err(1, "prop_dictionary_send_syscall");
    580 		if (quotactl(fst[i].f_mntonname, &pref) != 0)
    581 			err(1, "quotactl");
    582 
    583 		if (prop_dictionary_recv_syscall(&pref, &dict) != 0) {
    584 			err(1, "prop_dictionary_recv_syscall");
    585 		}
    586 
    587 		if (Dflag) {
    588 			fprintf(stderr, "reply from kernel for %s:\n%s\n",
    589 			    fst[i].f_mntonname,
    590 			    prop_dictionary_externalize(dict));
    591 		}
    592 		if ((errno = quota_get_cmds(dict, &cmds)) != 0) {
    593 			err(1, "quota_get_cmds");
    594 		}
    595 		/* only one command, no need to iter */
    596 		cmd = prop_array_get(cmds, 0);
    597 		if (cmd == NULL)
    598 			err(1, "prop_array_get(cmd)");
    599 
    600 		if (!prop_dictionary_get_int8(cmd, "return", &error8))
    601 			err(1, "prop_get(return)");
    602 		if (error8) {
    603 			errno = error8;
    604 			warn("clear %s quota entries on %s",
    605 			    ufs_quota_class_names[quotaclass],
    606 			    fst[i].f_mntonname);
    607 		}
    608 		prop_object_release(dict);
    609 	}
    610 	prop_object_release(protodict);
    611 }
    612 
    613 ////////////////////////////////////////////////////////////
    614 // editor
    615 
    616 /*
    617  * Take a list of privileges and get it edited.
    618  */
    619 static int
    620 editit(const char *ltmpfile)
    621 {
    622 	pid_t pid;
    623 	int lst;
    624 	char p[MAX_TMPSTR];
    625 	const char *ed;
    626 	sigset_t s, os;
    627 
    628 	sigemptyset(&s);
    629 	sigaddset(&s, SIGINT);
    630 	sigaddset(&s, SIGQUIT);
    631 	sigaddset(&s, SIGHUP);
    632 	if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
    633 		err(1, "sigprocmask");
    634 top:
    635 	switch ((pid = fork())) {
    636 	case -1:
    637 		if (errno == EPROCLIM) {
    638 			warnx("You have too many processes");
    639 			return 0;
    640 		}
    641 		if (errno == EAGAIN) {
    642 			sleep(1);
    643 			goto top;
    644 		}
    645 		warn("fork");
    646 		return 0;
    647 	case 0:
    648 		if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
    649 			err(1, "sigprocmask");
    650 		setgid(getgid());
    651 		setuid(getuid());
    652 		if ((ed = getenv("EDITOR")) == (char *)0)
    653 			ed = _PATH_VI;
    654 		if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
    655 			errx(1, "%s", "editor or filename too long");
    656 		}
    657 		snprintf(p, sizeof(p), "%s %s", ed, ltmpfile);
    658 		execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
    659 		err(1, "%s", ed);
    660 	default:
    661 		if (waitpid(pid, &lst, 0) == -1)
    662 			err(1, "waitpid");
    663 		if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
    664 			err(1, "sigprocmask");
    665 		if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
    666 			return 0;
    667 		return 1;
    668 	}
    669 }
    670 
    671 /*
    672  * Convert a quotause list to an ASCII file.
    673  */
    674 static int
    675 writeprivs(struct quotalist *qlist, int outfd, const char *name,
    676     int quotaclass)
    677 {
    678 	struct quotause *qup;
    679 	FILE *fd;
    680 	char b0[32], b1[32], b2[32], b3[32];
    681 
    682 	(void)ftruncate(outfd, 0);
    683 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    684 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    685 		errx(1, "fdopen `%s'", tmpfil);
    686 	if (dflag) {
    687 		fprintf(fd, "Default %s quotas:\n",
    688 		    ufs_quota_class_names[quotaclass]);
    689 	} else {
    690 		fprintf(fd, "Quotas for %s %s:\n",
    691 		    ufs_quota_class_names[quotaclass], name);
    692 	}
    693 	for (qup = qlist->head; qup; qup = qup->next) {
    694 		struct ufs_quota_entry *q = qup->qe;
    695 		fprintf(fd, "%s (version %d):\n",
    696 		     qup->fsname, (qup->flags & QUOTA2) ? 2 : 1);
    697 		if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
    698 			fprintf(fd, "\tblocks in use: %s, "
    699 			    "limits (soft = %s, hard = %s",
    700 			    intprt(b1, 21, q[QL_BLK].ufsqe_cur,
    701 			    HN_NOSPACE | HN_B, Hflag),
    702 			    intprt(b2, 21, q[QL_BLK].ufsqe_softlimit,
    703 			    HN_NOSPACE | HN_B, Hflag),
    704 			    intprt(b3, 21, q[QL_BLK].ufsqe_hardlimit,
    705 				HN_NOSPACE | HN_B, Hflag));
    706 			if (qup->flags & QUOTA2)
    707 				fprintf(fd, ", ");
    708 		} else
    709 			fprintf(fd, "\tblocks: (");
    710 
    711 		if (qup->flags & (QUOTA2|DEFAULT)) {
    712 		    fprintf(fd, "grace = %s",
    713 			timepprt(b0, 21, q[QL_BLK].ufsqe_grace, Hflag));
    714 		}
    715 		fprintf(fd, ")\n");
    716 		if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
    717 			fprintf(fd, "\tinodes in use: %s, "
    718 			    "limits (soft = %s, hard = %s",
    719 			    intprt(b1, 21, q[QL_FL].ufsqe_cur,
    720 			    HN_NOSPACE, Hflag),
    721 			    intprt(b2, 21, q[QL_FL].ufsqe_softlimit,
    722 			    HN_NOSPACE, Hflag),
    723 			    intprt(b3, 21, q[QL_FL].ufsqe_hardlimit,
    724 			     HN_NOSPACE, Hflag));
    725 			if (qup->flags & QUOTA2)
    726 				fprintf(fd, ", ");
    727 		} else
    728 			fprintf(fd, "\tinodes: (");
    729 
    730 		if (qup->flags & (QUOTA2|DEFAULT)) {
    731 		    fprintf(fd, "grace = %s",
    732 			timepprt(b0, 21, q[QL_FL].ufsqe_grace, Hflag));
    733 		}
    734 		fprintf(fd, ")\n");
    735 	}
    736 	fclose(fd);
    737 	return 1;
    738 }
    739 
    740 /*
    741  * Merge changes to an ASCII file into a quotause list.
    742  */
    743 static int
    744 readprivs(struct quotalist *qlist, int infd)
    745 {
    746 	struct quotause *qup;
    747 	FILE *fd;
    748 	int cnt;
    749 	char fsp[BUFSIZ];
    750 	static char line0[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
    751 	static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
    752 	static char stime[BUFSIZ];
    753 	uint64_t softb, hardb, softi, hardi;
    754 	time_t graceb = -1, gracei = -1;
    755 	int version;
    756 
    757 	(void)lseek(infd, (off_t)0, SEEK_SET);
    758 	fd = fdopen(dup(infd), "r");
    759 	if (fd == NULL) {
    760 		warn("Can't re-read temp file");
    761 		return 0;
    762 	}
    763 	/*
    764 	 * Discard title line, then read pairs of lines to process.
    765 	 */
    766 	(void) fgets(line1, sizeof (line1), fd);
    767 	while (fgets(line0, sizeof (line0), fd) != NULL &&
    768 	       fgets(line1, sizeof (line2), fd) != NULL &&
    769 	       fgets(line2, sizeof (line2), fd) != NULL) {
    770 		if (sscanf(line0, "%s (version %d):\n", fsp, &version) != 2) {
    771 			warnx("%s: bad format", line0);
    772 			goto out;
    773 		}
    774 #define last_char(str) ((str)[strlen(str) - 1])
    775 		if (last_char(line1) != '\n') {
    776 			warnx("%s:%s: bad format", fsp, line1);
    777 			goto out;
    778 		}
    779 		last_char(line1) = '\0';
    780 		if (last_char(line2) != '\n') {
    781 			warnx("%s:%s: bad format", fsp, line2);
    782 			goto out;
    783 		}
    784 		last_char(line2) = '\0';
    785 		if (dflag && version == 1) {
    786 			if (sscanf(line1,
    787 			    "\tblocks: (grace = %s\n", stime) != 1) {
    788 				warnx("%s:%s: bad format", fsp, line1);
    789 				goto out;
    790 			}
    791 			if (last_char(stime) != ')') {
    792 				warnx("%s:%s: bad format", fsp, line1);
    793 				goto out;
    794 			}
    795 			last_char(stime) = '\0';
    796 			if (timeprd(stime, &graceb) != 0) {
    797 				warnx("%s:%s: bad number", fsp, stime);
    798 				goto out;
    799 			}
    800 			if (sscanf(line2,
    801 			    "\tinodes: (grace = %s\n", stime) != 1) {
    802 				warnx("%s:%s: bad format", fsp, line2);
    803 				goto out;
    804 			}
    805 			if (last_char(stime) != ')') {
    806 				warnx("%s:%s: bad format", fsp, line2);
    807 				goto out;
    808 			}
    809 			last_char(stime) = '\0';
    810 			if (timeprd(stime, &gracei) != 0) {
    811 				warnx("%s:%s: bad number", fsp, stime);
    812 				goto out;
    813 			}
    814 		} else {
    815 			cnt = sscanf(line1,
    816 			    "\tblocks in use: %s limits (soft = %s hard = %s "
    817 			    "grace = %s", scurb, ssoft, shard, stime);
    818 			if (cnt == 3) {
    819 				if (version != 1 ||
    820 				    last_char(scurb) != ',' ||
    821 				    last_char(ssoft) != ',' ||
    822 				    last_char(shard) != ')') {
    823 					warnx("%s:%s: bad format %d",
    824 					    fsp, line1, cnt);
    825 					goto out;
    826 				}
    827 				stime[0] = '\0';
    828 			} else if (cnt == 4) {
    829 				if (version < 2 ||
    830 				    last_char(scurb) != ',' ||
    831 				    last_char(ssoft) != ',' ||
    832 				    last_char(shard) != ',' ||
    833 				    last_char(stime) != ')') {
    834 					warnx("%s:%s: bad format %d",
    835 					    fsp, line1, cnt);
    836 					goto out;
    837 				}
    838 			} else {
    839 				warnx("%s: %s: bad format cnt %d", fsp, line1,
    840 				    cnt);
    841 				goto out;
    842 			}
    843 			/* drop last char which is ',' or ')' */
    844 			last_char(scurb) = '\0';
    845 			last_char(ssoft) = '\0';
    846 			last_char(shard) = '\0';
    847 			last_char(stime) = '\0';
    848 
    849 			if (intrd(ssoft, &softb, HN_B) != 0) {
    850 				warnx("%s:%s: bad number", fsp, ssoft);
    851 				goto out;
    852 			}
    853 			if (intrd(shard, &hardb, HN_B) != 0) {
    854 				warnx("%s:%s: bad number", fsp, shard);
    855 				goto out;
    856 			}
    857 			if (cnt == 4) {
    858 				if (timeprd(stime, &graceb) != 0) {
    859 					warnx("%s:%s: bad number", fsp, stime);
    860 					goto out;
    861 				}
    862 			}
    863 
    864 			cnt = sscanf(line2,
    865 			    "\tinodes in use: %s limits (soft = %s hard = %s "
    866 			    "grace = %s", scuri, ssoft, shard, stime);
    867 			if (cnt == 3) {
    868 				if (version != 1 ||
    869 				    last_char(scuri) != ',' ||
    870 				    last_char(ssoft) != ',' ||
    871 				    last_char(shard) != ')') {
    872 					warnx("%s:%s: bad format %d",
    873 					    fsp, line2, cnt);
    874 					goto out;
    875 				}
    876 				stime[0] = '\0';
    877 			} else if (cnt == 4) {
    878 				if (version < 2 ||
    879 				    last_char(scuri) != ',' ||
    880 				    last_char(ssoft) != ',' ||
    881 				    last_char(shard) != ',' ||
    882 				    last_char(stime) != ')') {
    883 					warnx("%s:%s: bad format %d",
    884 					    fsp, line2, cnt);
    885 					goto out;
    886 				}
    887 			} else {
    888 				warnx("%s: %s: bad format", fsp, line2);
    889 				goto out;
    890 			}
    891 			/* drop last char which is ',' or ')' */
    892 			last_char(scuri) = '\0';
    893 			last_char(ssoft) = '\0';
    894 			last_char(shard) = '\0';
    895 			last_char(stime) = '\0';
    896 			if (intrd(ssoft, &softi, 0) != 0) {
    897 				warnx("%s:%s: bad number", fsp, ssoft);
    898 				goto out;
    899 			}
    900 			if (intrd(shard, &hardi, 0) != 0) {
    901 				warnx("%s:%s: bad number", fsp, shard);
    902 				goto out;
    903 			}
    904 			if (cnt == 4) {
    905 				if (timeprd(stime, &gracei) != 0) {
    906 					warnx("%s:%s: bad number", fsp, stime);
    907 					goto out;
    908 				}
    909 			}
    910 		}
    911 		for (qup = qlist->head; qup; qup = qup->next) {
    912 			struct ufs_quota_entry *q = qup->qe;
    913 			char b1[32], b2[32];
    914 			if (strcmp(fsp, qup->fsname))
    915 				continue;
    916 			if (version == 1 && dflag) {
    917 				q[QL_BLK].ufsqe_grace = graceb;
    918 				q[QL_FL].ufsqe_grace = gracei;
    919 				qup->flags |= FOUND;
    920 				continue;
    921 			}
    922 
    923 			if (strcmp(intprt(b1, 21, q[QL_BLK].ufsqe_cur,
    924 			    HN_NOSPACE | HN_B, Hflag),
    925 			    scurb) != 0 ||
    926 			    strcmp(intprt(b2, 21, q[QL_FL].ufsqe_cur,
    927 			    HN_NOSPACE, Hflag),
    928 			    scuri) != 0) {
    929 				warnx("%s: cannot change current allocation",
    930 				    fsp);
    931 				break;
    932 			}
    933 			/*
    934 			 * Cause time limit to be reset when the quota
    935 			 * is next used if previously had no soft limit
    936 			 * or were under it, but now have a soft limit
    937 			 * and are over it.
    938 			 */
    939 			if (q[QL_BLK].ufsqe_cur &&
    940 			    q[QL_BLK].ufsqe_cur >= softb &&
    941 			    (q[QL_BLK].ufsqe_softlimit == 0 ||
    942 			     q[QL_BLK].ufsqe_cur < q[QL_BLK].ufsqe_softlimit))
    943 				q[QL_BLK].ufsqe_time = 0;
    944 			if (q[QL_FL].ufsqe_cur &&
    945 			    q[QL_FL].ufsqe_cur >= softi &&
    946 			    (q[QL_FL].ufsqe_softlimit == 0 ||
    947 			     q[QL_FL].ufsqe_cur < q[QL_FL].ufsqe_softlimit))
    948 				q[QL_FL].ufsqe_time = 0;
    949 			q[QL_BLK].ufsqe_softlimit = softb;
    950 			q[QL_BLK].ufsqe_hardlimit = hardb;
    951 			if (version == 2)
    952 				q[QL_BLK].ufsqe_grace = graceb;
    953 			q[QL_FL].ufsqe_softlimit  = softi;
    954 			q[QL_FL].ufsqe_hardlimit  = hardi;
    955 			if (version == 2)
    956 				q[QL_FL].ufsqe_grace = gracei;
    957 			qup->flags |= FOUND;
    958 		}
    959 	}
    960 out:
    961 	fclose(fd);
    962 	/*
    963 	 * Disable quotas for any filesystems that have not been found.
    964 	 */
    965 	for (qup = qlist->head; qup; qup = qup->next) {
    966 		struct ufs_quota_entry *q = qup->qe;
    967 		if (qup->flags & FOUND) {
    968 			qup->flags &= ~FOUND;
    969 			continue;
    970 		}
    971 		q[QL_BLK].ufsqe_softlimit = UQUAD_MAX;
    972 		q[QL_BLK].ufsqe_hardlimit = UQUAD_MAX;
    973 		q[QL_BLK].ufsqe_grace = 0;
    974 		q[QL_FL].ufsqe_softlimit = UQUAD_MAX;
    975 		q[QL_FL].ufsqe_hardlimit = UQUAD_MAX;
    976 		q[QL_FL].ufsqe_grace = 0;
    977 	}
    978 	return 1;
    979 }
    980 
    981 ////////////////////////////////////////////////////////////
    982 // main
    983 
    984 static void
    985 usage(void)
    986 {
    987 	const char *p = getprogname();
    988 	fprintf(stderr,
    989 	    "Usage: %s [-D] [-H] [-u] [-p <username>] [-f <filesystem>] "
    990 		"-d | <username> ...\n"
    991 	    "\t%s [-D] [-H] -g [-p <groupname>] [-f <filesystem>] "
    992 		"-d | <groupname> ...\n"
    993 	    "\t%s [-D] [-u] [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
    994 		"-d | <username> ...\n"
    995 	    "\t%s [-D] -g [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
    996 		"-d | <groupname> ...\n"
    997 	    "\t%s [-D] [-H] [-u] -c [-f <filesystem>] username ...\n"
    998 	    "\t%s [-D] [-H] -g -c [-f <filesystem>] groupname ...\n",
    999 	    p, p, p, p, p, p);
   1000 	exit(1);
   1001 }
   1002 
   1003 int
   1004 main(int argc, char *argv[])
   1005 {
   1006 	struct quotause *qup;
   1007 	struct quotalist *protoprivs, *curprivs;
   1008 	long id, protoid;
   1009 	int quotaclass, tmpfd;
   1010 	char *protoname;
   1011 	char *soft = NULL, *hard = NULL, *grace = NULL;
   1012 	char *fs = NULL;
   1013 	int ch;
   1014 	int pflag = 0;
   1015 	int cflag = 0;
   1016 
   1017 	if (argc < 2)
   1018 		usage();
   1019 	if (getuid())
   1020 		errx(1, "permission denied");
   1021 	protoname = NULL;
   1022 	quotaclass = QUOTA_CLASS_USER;
   1023 	while ((ch = getopt(argc, argv, "DHcdugp:s:h:t:f:")) != -1) {
   1024 		switch(ch) {
   1025 		case 'D':
   1026 			Dflag++;
   1027 			break;
   1028 		case 'H':
   1029 			Hflag++;
   1030 			break;
   1031 		case 'c':
   1032 			cflag++;
   1033 			break;
   1034 		case 'd':
   1035 			dflag++;
   1036 			break;
   1037 		case 'p':
   1038 			protoname = optarg;
   1039 			pflag++;
   1040 			break;
   1041 		case 'g':
   1042 			quotaclass = QUOTA_CLASS_GROUP;
   1043 			break;
   1044 		case 'u':
   1045 			quotaclass = QUOTA_CLASS_USER;
   1046 			break;
   1047 		case 's':
   1048 			soft = optarg;
   1049 			break;
   1050 		case 'h':
   1051 			hard = optarg;
   1052 			break;
   1053 		case 't':
   1054 			grace = optarg;
   1055 			break;
   1056 		case 'f':
   1057 			fs = optarg;
   1058 			break;
   1059 		default:
   1060 			usage();
   1061 		}
   1062 	}
   1063 	argc -= optind;
   1064 	argv += optind;
   1065 
   1066 	if (pflag) {
   1067 		if (soft || hard || grace || dflag || cflag)
   1068 			usage();
   1069 		if ((protoid = getidbyname(protoname, quotaclass)) == -1)
   1070 			return 1;
   1071 		protoprivs = getprivs(protoid, quotaclass, fs, 0);
   1072 		for (qup = protoprivs->head; qup; qup = qup->next) {
   1073 			qup->qe[QL_BLK].ufsqe_time = 0;
   1074 			qup->qe[QL_FL].ufsqe_time = 0;
   1075 		}
   1076 		while (argc-- > 0) {
   1077 			if ((id = getidbyname(*argv++, quotaclass)) < 0)
   1078 				continue;
   1079 			putprivs(id, quotaclass, protoprivs);
   1080 		}
   1081 		/* XXX */
   1082 		/* quotalist_destroy(protoprivs); */
   1083 		return 0;
   1084 	}
   1085 	if (soft || hard || grace) {
   1086 		struct quotause *lqup;
   1087 		u_int64_t softb, hardb, softi, hardi;
   1088 		time_t  graceb, gracei;
   1089 		char *str;
   1090 
   1091 		if (cflag)
   1092 			usage();
   1093 		if (soft) {
   1094 			str = strsep(&soft, "/");
   1095 			if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
   1096 				usage();
   1097 
   1098 			if (intrd(str, &softb, HN_B) != 0)
   1099 				errx(1, "%s: bad number", str);
   1100 			if (intrd(soft, &softi, 0) != 0)
   1101 				errx(1, "%s: bad number", soft);
   1102 		}
   1103 		if (hard) {
   1104 			str = strsep(&hard, "/");
   1105 			if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
   1106 				usage();
   1107 
   1108 			if (intrd(str, &hardb, HN_B) != 0)
   1109 				errx(1, "%s: bad number", str);
   1110 			if (intrd(hard, &hardi, 0) != 0)
   1111 				errx(1, "%s: bad number", hard);
   1112 		}
   1113 		if (grace) {
   1114 			str = strsep(&grace, "/");
   1115 			if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
   1116 				usage();
   1117 
   1118 			if (timeprd(str, &graceb) != 0)
   1119 				errx(1, "%s: bad number", str);
   1120 			if (timeprd(grace, &gracei) != 0)
   1121 				errx(1, "%s: bad number", grace);
   1122 		}
   1123 		if (dflag) {
   1124 			curprivs = getprivs(0, quotaclass, fs, 1);
   1125 			for (lqup = curprivs->head; lqup; lqup = lqup->next) {
   1126 				struct ufs_quota_entry *q = lqup->qe;
   1127 				if (soft) {
   1128 					q[QL_BLK].ufsqe_softlimit = softb;
   1129 					q[QL_FL].ufsqe_softlimit = softi;
   1130 				}
   1131 				if (hard) {
   1132 					q[QL_BLK].ufsqe_hardlimit = hardb;
   1133 					q[QL_FL].ufsqe_hardlimit = hardi;
   1134 				}
   1135 				if (grace) {
   1136 					q[QL_BLK].ufsqe_grace = graceb;
   1137 					q[QL_FL].ufsqe_grace = gracei;
   1138 				}
   1139 			}
   1140 			putprivs(0, quotaclass, curprivs);
   1141 			quotalist_destroy(curprivs);
   1142 			return 0;
   1143 		}
   1144 		for ( ; argc > 0; argc--, argv++) {
   1145 			if ((id = getidbyname(*argv, quotaclass)) == -1)
   1146 				continue;
   1147 			curprivs = getprivs(id, quotaclass, fs, 0);
   1148 			for (lqup = curprivs->head; lqup; lqup = lqup->next) {
   1149 				struct ufs_quota_entry *q = lqup->qe;
   1150 				if (soft) {
   1151 					if (softb &&
   1152 					    q[QL_BLK].ufsqe_cur >= softb &&
   1153 					    (q[QL_BLK].ufsqe_softlimit == 0 ||
   1154 					    q[QL_BLK].ufsqe_cur <
   1155 					    q[QL_BLK].ufsqe_softlimit))
   1156 						q[QL_BLK].ufsqe_time = 0;
   1157 					if (softi &&
   1158 					    q[QL_FL].ufsqe_cur >= softb &&
   1159 					    (q[QL_FL].ufsqe_softlimit == 0 ||
   1160 					    q[QL_FL].ufsqe_cur <
   1161 					    q[QL_FL].ufsqe_softlimit))
   1162 						q[QL_FL].ufsqe_time = 0;
   1163 					q[QL_BLK].ufsqe_softlimit = softb;
   1164 					q[QL_FL].ufsqe_softlimit = softi;
   1165 				}
   1166 				if (hard) {
   1167 					q[QL_BLK].ufsqe_hardlimit = hardb;
   1168 					q[QL_FL].ufsqe_hardlimit = hardi;
   1169 				}
   1170 				if (grace) {
   1171 					q[QL_BLK].ufsqe_grace = graceb;
   1172 					q[QL_FL].ufsqe_grace = gracei;
   1173 				}
   1174 			}
   1175 			putprivs(id, quotaclass, curprivs);
   1176 			quotalist_destroy(curprivs);
   1177 		}
   1178 		return 0;
   1179 	}
   1180 	if (cflag) {
   1181 		if (dflag)
   1182 			usage();
   1183 		clearpriv(argc, argv, fs, quotaclass);
   1184 		return 0;
   1185 	}
   1186 	tmpfd = mkstemp(tmpfil);
   1187 	fchown(tmpfd, getuid(), getgid());
   1188 	if (dflag) {
   1189 		curprivs = getprivs(0, quotaclass, fs, 1);
   1190 		if (writeprivs(curprivs, tmpfd, NULL, quotaclass) &&
   1191 		    editit(tmpfil) && readprivs(curprivs, tmpfd))
   1192 			putprivs(0, quotaclass, curprivs);
   1193 		quotalist_destroy(curprivs);
   1194 	}
   1195 	for ( ; argc > 0; argc--, argv++) {
   1196 		if ((id = getidbyname(*argv, quotaclass)) == -1)
   1197 			continue;
   1198 		curprivs = getprivs(id, quotaclass, fs, 0);
   1199 		if (writeprivs(curprivs, tmpfd, *argv, quotaclass) == 0)
   1200 			continue;
   1201 		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
   1202 			putprivs(id, quotaclass, curprivs);
   1203 		quotalist_destroy(curprivs);
   1204 	}
   1205 	close(tmpfd);
   1206 	unlink(tmpfil);
   1207 	return 0;
   1208 }
   1209