Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.31
      1 /*      $NetBSD: edquota.c,v 1.31 2011/03/06 22:34:57 christos 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.31 2011/03/06 22:34:57 christos 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 <ufs/ufs/quota2_prop.h>
     60 #include <ufs/ufs/quota1.h>
     61 #include <sys/quota.h>
     62 
     63 #include <assert.h>
     64 #include <err.h>
     65 #include <errno.h>
     66 #include <fstab.h>
     67 #include <pwd.h>
     68 #include <grp.h>
     69 #include <ctype.h>
     70 #include <signal.h>
     71 #include <stdio.h>
     72 #include <stdlib.h>
     73 #include <string.h>
     74 #include <unistd.h>
     75 
     76 #include "printquota.h"
     77 #include "getvfsquota.h"
     78 #include "quotautil.h"
     79 
     80 #include "pathnames.h"
     81 
     82 static const char *quotagroup = QUOTAGROUP;
     83 static char tmpfil[] = _PATH_TMP;
     84 
     85 struct quotause {
     86 	struct	quotause *next;
     87 	long	flags;
     88 	struct	quota2_entry q2e;
     89 	char	fsname[MAXPATHLEN + 1];
     90 	char	*qfname;
     91 };
     92 #define	FOUND	0x01
     93 #define	QUOTA2	0x02
     94 #define	DEFAULT	0x04
     95 
     96 #define MAX_TMPSTR	(100+MAXPATHLEN)
     97 
     98 static void	usage(void) __attribute__((__noreturn__));
     99 static int	getentry(const char *, int);
    100 static struct quotause * getprivs(long, int, const char *, int);
    101 static struct quotause * getprivs2(long, int, const char *, int);
    102 static struct quotause * getprivs1(long, int, const char *);
    103 static void	putprivs(uint32_t, int, struct quotause *);
    104 static void	putprivs2(uint32_t, int, struct quotause *);
    105 static void	putprivs1(uint32_t, int, struct quotause *);
    106 static int	editit(const char *);
    107 static int	writeprivs(struct quotause *, int, const char *, int);
    108 static int	readprivs(struct quotause *, int);
    109 static void	freeq(struct quotause *);
    110 static void	freeprivs(struct quotause *);
    111 static void clearpriv(int, char **, const char *, int);
    112 
    113 static int Hflag = 0;
    114 static int Dflag = 0;
    115 static int dflag = 0;
    116 
    117 int
    118 main(int argc, char *argv[])
    119 {
    120 	struct quotause *qup, *protoprivs, *curprivs;
    121 	long id, protoid;
    122 	int quotatype, tmpfd;
    123 	char *protoname;
    124 	char *soft = NULL, *hard = NULL, *grace = NULL;
    125 	char *fs = NULL;
    126 	int ch;
    127 	int pflag = 0;
    128 	int cflag = 0;
    129 
    130 	if (argc < 2)
    131 		usage();
    132 	if (getuid())
    133 		errx(1, "permission denied");
    134 	protoname = NULL;
    135 	quotatype = USRQUOTA;
    136 	while ((ch = getopt(argc, argv, "DHcdugp:s:h:t:f:")) != -1) {
    137 		switch(ch) {
    138 		case 'D':
    139 			Dflag++;
    140 			break;
    141 		case 'H':
    142 			Hflag++;
    143 			break;
    144 		case 'c':
    145 			cflag++;
    146 			break;
    147 		case 'd':
    148 			dflag++;
    149 			break;
    150 		case 'p':
    151 			protoname = optarg;
    152 			pflag++;
    153 			break;
    154 		case 'g':
    155 			quotatype = GRPQUOTA;
    156 			break;
    157 		case 'u':
    158 			quotatype = USRQUOTA;
    159 			break;
    160 		case 's':
    161 			soft = optarg;
    162 			break;
    163 		case 'h':
    164 			hard = optarg;
    165 			break;
    166 		case 't':
    167 			grace = optarg;
    168 			break;
    169 		case 'f':
    170 			fs = optarg;
    171 			break;
    172 		default:
    173 			usage();
    174 		}
    175 	}
    176 	argc -= optind;
    177 	argv += optind;
    178 
    179 	if (pflag) {
    180 		if (soft || hard || grace || dflag || cflag)
    181 			usage();
    182 		if ((protoid = getentry(protoname, quotatype)) == -1)
    183 			return 1;
    184 		protoprivs = getprivs(protoid, quotatype, fs, 0);
    185 		for (qup = protoprivs; qup; qup = qup->next) {
    186 			qup->q2e.q2e_val[QL_BLOCK].q2v_time = 0;
    187 			qup->q2e.q2e_val[QL_FILE].q2v_time = 0;
    188 		}
    189 		while (argc-- > 0) {
    190 			if ((id = getentry(*argv++, quotatype)) < 0)
    191 				continue;
    192 			putprivs(id, quotatype, protoprivs);
    193 		}
    194 		return 0;
    195 	}
    196 	if (soft || hard || grace) {
    197 		struct quotause *lqup;
    198 		u_int64_t softb, hardb, softi, hardi;
    199 		time_t  graceb, gracei;
    200 		char *str;
    201 
    202 		if (cflag)
    203 			usage();
    204 		if (soft) {
    205 			str = strsep(&soft, "/");
    206 			if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
    207 				usage();
    208 
    209 			if (intrd(str, &softb, HN_B) != 0)
    210 				errx(1, "%s: bad number", str);
    211 			if (intrd(soft, &softi, 0) != 0)
    212 				errx(1, "%s: bad number", soft);
    213 		}
    214 		if (hard) {
    215 			str = strsep(&hard, "/");
    216 			if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
    217 				usage();
    218 
    219 			if (intrd(str, &hardb, HN_B) != 0)
    220 				errx(1, "%s: bad number", str);
    221 			if (intrd(hard, &hardi, 0) != 0)
    222 				errx(1, "%s: bad number", hard);
    223 		}
    224 		if (grace) {
    225 			str = strsep(&grace, "/");
    226 			if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
    227 				usage();
    228 
    229 			if (timeprd(str, &graceb) != 0)
    230 				errx(1, "%s: bad number", str);
    231 			if (timeprd(grace, &gracei) != 0)
    232 				errx(1, "%s: bad number", grace);
    233 		}
    234 		if (dflag) {
    235 			curprivs = getprivs(0, quotatype, fs, 1);
    236 			for (lqup = curprivs; lqup; lqup = lqup->next) {
    237 				struct quota2_val *q = lqup->q2e.q2e_val;
    238 				if (soft) {
    239 					q[QL_BLOCK].q2v_softlimit = softb;
    240 					q[QL_FILE].q2v_softlimit = softi;
    241 				}
    242 				if (hard) {
    243 					q[QL_BLOCK].q2v_hardlimit = hardb;
    244 					q[QL_FILE].q2v_hardlimit = hardi;
    245 				}
    246 				if (grace) {
    247 					q[QL_BLOCK].q2v_grace = graceb;
    248 					q[QL_FILE].q2v_grace = gracei;
    249 				}
    250 			}
    251 			putprivs(0, quotatype, curprivs);
    252 			freeprivs(curprivs);
    253 			return 0;
    254 		}
    255 		for ( ; argc > 0; argc--, argv++) {
    256 			if ((id = getentry(*argv, quotatype)) == -1)
    257 				continue;
    258 			curprivs = getprivs(id, quotatype, fs, 0);
    259 			for (lqup = curprivs; lqup; lqup = lqup->next) {
    260 				struct quota2_val *q = lqup->q2e.q2e_val;
    261 				if (soft) {
    262 					if (softb &&
    263 					    q[QL_BLOCK].q2v_cur >= softb &&
    264 					    (q[QL_BLOCK].q2v_softlimit == 0 ||
    265 					    q[QL_BLOCK].q2v_cur <
    266 					    q[QL_BLOCK].q2v_softlimit))
    267 						q[QL_BLOCK].q2v_time = 0;
    268 					if (softi &&
    269 					    q[QL_FILE].q2v_cur >= softb &&
    270 					    (q[QL_FILE].q2v_softlimit == 0 ||
    271 					    q[QL_FILE].q2v_cur <
    272 					    q[QL_FILE].q2v_softlimit))
    273 						q[QL_FILE].q2v_time = 0;
    274 					q[QL_BLOCK].q2v_softlimit = softb;
    275 					q[QL_FILE].q2v_softlimit = softi;
    276 				}
    277 				if (hard) {
    278 					q[QL_BLOCK].q2v_hardlimit = hardb;
    279 					q[QL_FILE].q2v_hardlimit = hardi;
    280 				}
    281 				if (grace) {
    282 					q[QL_BLOCK].q2v_grace = graceb;
    283 					q[QL_FILE].q2v_grace = gracei;
    284 				}
    285 			}
    286 			putprivs(id, quotatype, curprivs);
    287 			freeprivs(curprivs);
    288 		}
    289 		return 0;
    290 	}
    291 	if (cflag) {
    292 		if (dflag)
    293 			usage();
    294 		clearpriv(argc, argv, fs, quotatype);
    295 		return 0;
    296 	}
    297 	tmpfd = mkstemp(tmpfil);
    298 	fchown(tmpfd, getuid(), getgid());
    299 	if (dflag) {
    300 		curprivs = getprivs(0, quotatype, fs, 1);
    301 		if (writeprivs(curprivs, tmpfd, NULL, quotatype) &&
    302 		    editit(tmpfil) && readprivs(curprivs, tmpfd))
    303 			putprivs(0, quotatype, curprivs);
    304 		freeprivs(curprivs);
    305 	}
    306 	for ( ; argc > 0; argc--, argv++) {
    307 		if ((id = getentry(*argv, quotatype)) == -1)
    308 			continue;
    309 		curprivs = getprivs(id, quotatype, fs, 0);
    310 		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
    311 			continue;
    312 		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
    313 			putprivs(id, quotatype, curprivs);
    314 		freeprivs(curprivs);
    315 	}
    316 	close(tmpfd);
    317 	unlink(tmpfil);
    318 	return 0;
    319 }
    320 
    321 static void
    322 usage(void)
    323 {
    324 	const char *p = getprogname();
    325 	fprintf(stderr,
    326 	    "Usage: %s [-D] [-H] [-u] [-p <username>] [-f <filesystem>] "
    327 		"-d | <username> ...\n"
    328 	    "\t%s [-D] [-H] -g [-p <groupname>] [-f <filesystem>] "
    329 		"-d | <groupname> ...\n"
    330 	    "\t%s [-D] [-u] [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
    331 		"-d | <username> ...\n"
    332 	    "\t%s [-D] -g [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
    333 		"-d | <groupname> ...\n"
    334 	    "\t%s [-D] [-H] [-u] -c [-f <filesystem>] username ...\n"
    335 	    "\t%s [-D] [-H] -g -c [-f <filesystem>] groupname ...\n",
    336 	    p, p, p, p, p, p);
    337 	exit(1);
    338 }
    339 
    340 /*
    341  * This routine converts a name for a particular quota type to
    342  * an identifier. This routine must agree with the kernel routine
    343  * getinoquota as to the interpretation of quota types.
    344  */
    345 static int
    346 getentry(const char *name, int quotatype)
    347 {
    348 	struct passwd *pw;
    349 	struct group *gr;
    350 
    351 	if (alldigits(name))
    352 		return atoi(name);
    353 	switch(quotatype) {
    354 	case USRQUOTA:
    355 		if ((pw = getpwnam(name)) != NULL)
    356 			return pw->pw_uid;
    357 		warnx("%s: no such user", name);
    358 		break;
    359 	case GRPQUOTA:
    360 		if ((gr = getgrnam(name)) != NULL)
    361 			return gr->gr_gid;
    362 		warnx("%s: no such group", name);
    363 		break;
    364 	default:
    365 		warnx("%d: unknown quota type", quotatype);
    366 		break;
    367 	}
    368 	sleep(1);
    369 	return -1;
    370 }
    371 
    372 /*
    373  * Collect the requested quota information.
    374  */
    375 static struct quotause *
    376 getprivs(long id, int quotatype, const char *filesys, int defaultq)
    377 {
    378 	struct statvfs *fst;
    379 	int nfst, i;
    380 	struct quotause *qup, *quptail = NULL;
    381 	struct quotause *quphead = NULL;
    382 
    383 	nfst = getmntinfo(&fst, MNT_WAIT);
    384 	if (nfst == 0)
    385 		errx(1, "no filesystems mounted!");
    386 
    387 	for (i = 0; i < nfst; i++) {
    388 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    389 			continue;
    390 		if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
    391 		    strcmp(fst[i].f_mntfromname, filesys) != 0)
    392 			continue;
    393 		qup = getprivs2(id, quotatype, fst[i].f_mntonname, defaultq);
    394 		if (qup == NULL)
    395 			return NULL;
    396 		if (quphead == NULL)
    397 			quphead = qup;
    398 		else
    399 			quptail->next = qup;
    400 		quptail = qup;
    401 		qup->next = 0;
    402 	}
    403 
    404 	if (filesys && quphead == NULL) {
    405 		if (defaultq)
    406 			errx(1, "no default quota for version 1");
    407 		/* if we get there, filesys is not mounted. try the old way */
    408 		qup = getprivs1(id, quotatype, filesys);
    409 		if (qup == NULL)
    410 			return NULL;
    411 		if (quphead == NULL)
    412 			quphead = qup;
    413 		else
    414 			quptail->next = qup;
    415 		quptail = qup;
    416 		qup->next = 0;
    417 	}
    418 	return quphead;
    419 }
    420 
    421 static struct quotause *
    422 getprivs2(long id, int quotatype, const char *filesys, int defaultq)
    423 {
    424 	struct quotause *qup;
    425 	int8_t version;
    426 
    427 	if ((qup = malloc(sizeof(*qup))) == NULL)
    428 		err(1, "out of memory");
    429 	memset(qup, 0, sizeof(*qup));
    430 	strcpy(qup->fsname, filesys);
    431 	if (defaultq)
    432 		qup->flags |= DEFAULT;
    433 	if (!getvfsquota(filesys, &qup->q2e, &version,
    434 	    id, quotatype, defaultq, Dflag)) {
    435 		/* no entry, get default entry */
    436 		if (!getvfsquota(filesys, &qup->q2e, &version,
    437 		    id, quotatype, 1, Dflag)) {
    438 			free(qup);
    439 			return NULL;
    440 		}
    441 	}
    442 	if (version == 2)
    443 		qup->flags |= QUOTA2;
    444 	qup->q2e.q2e_uid = id;
    445 	return qup;
    446 }
    447 
    448 static struct quotause *
    449 getprivs1(long id, int quotatype, const char *filesys)
    450 {
    451 	struct fstab *fs;
    452 	char qfpathname[MAXPATHLEN];
    453 	struct quotause *qup;
    454 	struct dqblk dqblk;
    455 	int fd;
    456 
    457 	setfsent();
    458 	while ((fs = getfsent()) != NULL) {
    459 		if (strcmp(fs->fs_vfstype, "ffs"))
    460 			continue;
    461 		if (strcmp(fs->fs_spec, filesys) == 0 ||
    462 		    strcmp(fs->fs_file, filesys) == 0)
    463 			break;
    464 	}
    465 	if (fs == NULL)
    466 		return NULL;
    467 
    468 	if (!hasquota(qfpathname, sizeof(qfpathname), fs, quotatype))
    469 		return NULL;
    470 	if ((qup = malloc(sizeof(*qup))) == NULL)
    471 		err(1, "out of memory");
    472 	strcpy(qup->fsname, fs->fs_file);
    473 	if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    474 		fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    475 		if (fd < 0 && errno != ENOENT) {
    476 			warnx("open `%s'", qfpathname);
    477 			freeq(qup);
    478 			return NULL;
    479 		}
    480 		warnx("Creating quota file %s", qfpathname);
    481 		sleep(3);
    482 		(void)fchown(fd, getuid(), getentry(quotagroup, GRPQUOTA));
    483 		(void)fchmod(fd, 0640);
    484 	}
    485 	(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    486 	    SEEK_SET);
    487 	switch (read(fd, &dqblk, sizeof(struct dqblk))) {
    488 	case 0:			/* EOF */
    489 		/*
    490 		 * Convert implicit 0 quota (EOF)
    491 		 * into an explicit one (zero'ed dqblk)
    492 		 */
    493 		memset(&dqblk, 0, sizeof(struct dqblk));
    494 		break;
    495 
    496 	case sizeof(struct dqblk):	/* OK */
    497 		break;
    498 
    499 	default:		/* ERROR */
    500 		warn("read error in `%s'", qfpathname);
    501 		close(fd);
    502 		freeq(qup);
    503 		return NULL;
    504 	}
    505 	close(fd);
    506 	qup->qfname = qfpathname;
    507 	endfsent();
    508 	dqblk2q2e(&dqblk, &qup->q2e);
    509 	return qup;
    510 }
    511 
    512 /*
    513  * Store the requested quota information.
    514  */
    515 void
    516 putprivs(uint32_t id, int quotatype, struct quotause *quplist)
    517 {
    518 	struct quotause *qup;
    519 
    520         for (qup = quplist; qup; qup = qup->next) {
    521 		if (qup->qfname == NULL)
    522 			putprivs2(id, quotatype, qup);
    523 		else
    524 			putprivs1(id, quotatype, qup);
    525 	}
    526 }
    527 
    528 static void
    529 putprivs2(uint32_t id, int quotatype, struct quotause *qup)
    530 {
    531 
    532 	prop_dictionary_t dict, data, cmd;
    533 	prop_array_t cmds, datas;
    534 	struct plistref pref;
    535 	int8_t error8;
    536 
    537 	qup->q2e.q2e_uid = id;
    538 	data = q2etoprop(&qup->q2e, (qup->flags & DEFAULT) ? 1 : 0);
    539 
    540 	if (data == NULL)
    541 		err(1, "q2etoprop(id)");
    542 
    543 	dict = quota2_prop_create();
    544 	cmds = prop_array_create();
    545 	datas = prop_array_create();
    546 
    547 	if (dict == NULL || cmds == NULL || datas == NULL) {
    548 		errx(1, "can't allocate proplist");
    549 	}
    550 
    551 	if (!prop_array_add_and_rel(datas, data))
    552 		err(1, "prop_array_add(data)");
    553 
    554 	if (!quota2_prop_add_command(cmds, "set",
    555 	    qfextension[quotatype], datas))
    556 		err(1, "prop_add_command");
    557 	if (!prop_dictionary_set(dict, "commands", cmds))
    558 		err(1, "prop_dictionary_set(command)");
    559 	if (Dflag)
    560 		printf("message to kernel:\n%s\n",
    561 		    prop_dictionary_externalize(dict));
    562 
    563 	if (!prop_dictionary_send_syscall(dict, &pref))
    564 		err(1, "prop_dictionary_send_syscall");
    565 	prop_object_release(dict);
    566 
    567 	if (quotactl(qup->fsname, &pref) != 0)
    568 		err(1, "quotactl");
    569 
    570 	if ((errno = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
    571 		err(1, "prop_dictionary_recv_syscall");
    572 	}
    573 
    574 	if (Dflag)
    575 		printf("reply from kernel:\n%s\n",
    576 		    prop_dictionary_externalize(dict));
    577 
    578 	if ((errno = quota2_get_cmds(dict, &cmds)) != 0) {
    579 		err(1, "quota2_get_cmds");
    580 	}
    581 	/* only one command, no need to iter */
    582 	cmd = prop_array_get(cmds, 0);
    583 	if (cmd == NULL)
    584 		err(1, "prop_array_get(cmd)");
    585 
    586 	if (!prop_dictionary_get_int8(cmd, "return", &error8))
    587 		err(1, "prop_get(return)");
    588 
    589 	if (error8) {
    590 		errno = error8;
    591 		if (qup->flags & DEFAULT)
    592 			warn("set default %s quota", qfextension[quotatype]);
    593 		else
    594 			warn("set %s quota for %u", qfextension[quotatype], id);
    595 	}
    596 	prop_object_release(dict);
    597 }
    598 
    599 static void
    600 putprivs1(uint32_t id, int quotatype, struct quotause *qup)
    601 {
    602 	struct dqblk dqblk;
    603 	int fd;
    604 
    605 	q2e2dqblk(&qup->q2e, &dqblk);
    606 	assert((qup->flags & DEFAULT) == 0);
    607 
    608 	if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    609 		warnx("open `%s'", qup->qfname);
    610 	} else {
    611 		(void)lseek(fd,
    612 		    (off_t)(id * (long)sizeof (struct dqblk)),
    613 		    SEEK_SET);
    614 		if (write(fd, &dqblk, sizeof (struct dqblk)) !=
    615 		    sizeof (struct dqblk))
    616 			warnx("writing `%s'", qup->qfname);
    617 		close(fd);
    618 	}
    619 }
    620 
    621 /*
    622  * Take a list of privileges and get it edited.
    623  */
    624 static int
    625 editit(const char *ltmpfile)
    626 {
    627 	pid_t pid;
    628 	int lst;
    629 	char p[MAX_TMPSTR];
    630 	const char *ed;
    631 	sigset_t s, os;
    632 
    633 	sigemptyset(&s);
    634 	sigaddset(&s, SIGINT);
    635 	sigaddset(&s, SIGQUIT);
    636 	sigaddset(&s, SIGHUP);
    637 	if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
    638 		err(1, "sigprocmask");
    639 top:
    640 	switch ((pid = fork())) {
    641 	case -1:
    642 		if (errno == EPROCLIM) {
    643 			warnx("You have too many processes");
    644 			return 0;
    645 		}
    646 		if (errno == EAGAIN) {
    647 			sleep(1);
    648 			goto top;
    649 		}
    650 		warn("fork");
    651 		return 0;
    652 	case 0:
    653 		if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
    654 			err(1, "sigprocmask");
    655 		setgid(getgid());
    656 		setuid(getuid());
    657 		if ((ed = getenv("EDITOR")) == (char *)0)
    658 			ed = _PATH_VI;
    659 		if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
    660 			errx(1, "%s", "editor or filename too long");
    661 		}
    662 		snprintf(p, sizeof(p), "%s %s", ed, ltmpfile);
    663 		execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
    664 		err(1, "%s", ed);
    665 	default:
    666 		if (waitpid(pid, &lst, 0) == -1)
    667 			err(1, "waitpid");
    668 		if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
    669 			err(1, "sigprocmask");
    670 		if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
    671 			return 0;
    672 		return 1;
    673 	}
    674 }
    675 
    676 /*
    677  * Convert a quotause list to an ASCII file.
    678  */
    679 static int
    680 writeprivs(struct quotause *quplist, int outfd, const char *name,
    681     int quotatype)
    682 {
    683 	struct quotause *qup;
    684 	FILE *fd;
    685 	char b0[32], b1[32], b2[32], b3[32];
    686 
    687 	(void)ftruncate(outfd, 0);
    688 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    689 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    690 		errx(1, "fdopen `%s'", tmpfil);
    691 	if (dflag) {
    692 		fprintf(fd, "Default %s quotas:\n", qfextension[quotatype]);
    693 	} else {
    694 		fprintf(fd, "Quotas for %s %s:\n",
    695 		    qfextension[quotatype], name);
    696 	}
    697 	for (qup = quplist; qup; qup = qup->next) {
    698 		struct quota2_val *q = qup->q2e.q2e_val;
    699 		fprintf(fd, "%s (version %d):\n",
    700 		     qup->fsname, (qup->flags & QUOTA2) ? 2 : 1);
    701 		if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
    702 			fprintf(fd, "\tblocks in use: %s, "
    703 			    "limits (soft = %s, hard = %s",
    704 			    intprt(b1, 21, q[QL_BLOCK].q2v_cur,
    705 			    HN_NOSPACE | HN_B, Hflag),
    706 			    intprt(b2, 21, q[QL_BLOCK].q2v_softlimit,
    707 			    HN_NOSPACE | HN_B, Hflag),
    708 			    intprt(b3, 21, q[QL_BLOCK].q2v_hardlimit,
    709 				HN_NOSPACE | HN_B, Hflag));
    710 			if (qup->flags & QUOTA2)
    711 				fprintf(fd, ", ");
    712 		} else
    713 			fprintf(fd, "\tblocks: (");
    714 
    715 		if (qup->flags & (QUOTA2|DEFAULT)) {
    716 		    fprintf(fd, "grace = %s",
    717 			timepprt(b0, 21, q[QL_BLOCK].q2v_grace, Hflag));
    718 		}
    719 		fprintf(fd, ")\n");
    720 		if ((qup->flags & DEFAULT) == 0 || (qup->flags & QUOTA2) != 0) {
    721 			fprintf(fd, "\tinodes in use: %s, "
    722 			    "limits (soft = %s, hard = %s",
    723 			    intprt(b1, 21, q[QL_FILE].q2v_cur,
    724 			    HN_NOSPACE, Hflag),
    725 			    intprt(b2, 21, q[QL_FILE].q2v_softlimit,
    726 			    HN_NOSPACE, Hflag),
    727 			    intprt(b3, 21, q[QL_FILE].q2v_hardlimit,
    728 			     HN_NOSPACE, Hflag));
    729 			if (qup->flags & QUOTA2)
    730 				fprintf(fd, ", ");
    731 		} else
    732 			fprintf(fd, "\tinodes: (");
    733 
    734 		if (qup->flags & (QUOTA2|DEFAULT)) {
    735 		    fprintf(fd, "grace = %s",
    736 			timepprt(b0, 21, q[QL_FILE].q2v_grace, Hflag));
    737 		}
    738 		fprintf(fd, ")\n");
    739 	}
    740 	fclose(fd);
    741 	return 1;
    742 }
    743 
    744 /*
    745  * Merge changes to an ASCII file into a quotause list.
    746  */
    747 static int
    748 readprivs(struct quotause *quplist, int infd)
    749 {
    750 	struct quotause *qup;
    751 	FILE *fd;
    752 	int cnt;
    753 	char fsp[BUFSIZ];
    754 	static char line0[BUFSIZ], line1[BUFSIZ], line2[BUFSIZ];
    755 	static char scurb[BUFSIZ], scuri[BUFSIZ], ssoft[BUFSIZ], shard[BUFSIZ];
    756 	static char stime[BUFSIZ];
    757 	uint64_t softb, hardb, softi, hardi;
    758 	time_t graceb = -1, gracei = -1;
    759 	int version;
    760 
    761 	(void)lseek(infd, (off_t)0, SEEK_SET);
    762 	fd = fdopen(dup(infd), "r");
    763 	if (fd == NULL) {
    764 		warn("Can't re-read temp file");
    765 		return 0;
    766 	}
    767 	/*
    768 	 * Discard title line, then read pairs of lines to process.
    769 	 */
    770 	(void) fgets(line1, sizeof (line1), fd);
    771 	while (fgets(line0, sizeof (line0), fd) != NULL &&
    772 	       fgets(line1, sizeof (line2), fd) != NULL &&
    773 	       fgets(line2, sizeof (line2), fd) != NULL) {
    774 		if (sscanf(line0, "%s (version %d):\n", fsp, &version) != 2) {
    775 			warnx("%s: bad format", line0);
    776 			goto out;
    777 		}
    778 #define last_char(str) ((str)[strlen(str) - 1])
    779 		if (last_char(line1) != '\n') {
    780 			warnx("%s:%s: bad format", fsp, line1);
    781 			goto out;
    782 		}
    783 		last_char(line1) = '\0';
    784 		if (last_char(line2) != '\n') {
    785 			warnx("%s:%s: bad format", fsp, line2);
    786 			goto out;
    787 		}
    788 		last_char(line2) = '\0';
    789 		if (dflag && version == 1) {
    790 			if (sscanf(line1,
    791 			    "\tblocks: (grace = %s\n", stime) != 1) {
    792 				warnx("%s:%s: bad format", fsp, line1);
    793 				goto out;
    794 			}
    795 			if (last_char(stime) != ')') {
    796 				warnx("%s:%s: bad format", fsp, line1);
    797 				goto out;
    798 			}
    799 			last_char(stime) = '\0';
    800 			if (timeprd(stime, &graceb) != 0) {
    801 				warnx("%s:%s: bad number", fsp, stime);
    802 				goto out;
    803 			}
    804 			if (sscanf(line2,
    805 			    "\tinodes: (grace = %s\n", stime) != 1) {
    806 				warnx("%s:%s: bad format", fsp, line2);
    807 				goto out;
    808 			}
    809 			if (last_char(stime) != ')') {
    810 				warnx("%s:%s: bad format", fsp, line2);
    811 				goto out;
    812 			}
    813 			last_char(stime) = '\0';
    814 			if (timeprd(stime, &gracei) != 0) {
    815 				warnx("%s:%s: bad number", fsp, stime);
    816 				goto out;
    817 			}
    818 		} else {
    819 			cnt = sscanf(line1,
    820 			    "\tblocks in use: %s limits (soft = %s hard = %s "
    821 			    "grace = %s", scurb, ssoft, shard, stime);
    822 			if (cnt == 3) {
    823 				if (version != 1 ||
    824 				    last_char(scurb) != ',' ||
    825 				    last_char(ssoft) != ',' ||
    826 				    last_char(shard) != ')') {
    827 					warnx("%s:%s: bad format %d",
    828 					    fsp, line1, cnt);
    829 					goto out;
    830 				}
    831 				stime[0] = '\0';
    832 			} else if (cnt == 4) {
    833 				if (version < 2 ||
    834 				    last_char(scurb) != ',' ||
    835 				    last_char(ssoft) != ',' ||
    836 				    last_char(shard) != ',' ||
    837 				    last_char(stime) != ')') {
    838 					warnx("%s:%s: bad format %d",
    839 					    fsp, line1, cnt);
    840 					goto out;
    841 				}
    842 			} else {
    843 				warnx("%s: %s: bad format cnt %d", fsp, line1,
    844 				    cnt);
    845 				goto out;
    846 			}
    847 			/* drop last char which is ',' or ')' */
    848 			last_char(scurb) = '\0';
    849 			last_char(ssoft) = '\0';
    850 			last_char(shard) = '\0';
    851 			last_char(stime) = '\0';
    852 
    853 			if (intrd(ssoft, &softb, HN_B) != 0) {
    854 				warnx("%s:%s: bad number", fsp, ssoft);
    855 				goto out;
    856 			}
    857 			if (intrd(shard, &hardb, HN_B) != 0) {
    858 				warnx("%s:%s: bad number", fsp, shard);
    859 				goto out;
    860 			}
    861 			if (cnt == 4) {
    862 				if (timeprd(stime, &graceb) != 0) {
    863 					warnx("%s:%s: bad number", fsp, stime);
    864 					goto out;
    865 				}
    866 			}
    867 
    868 			cnt = sscanf(line2,
    869 			    "\tinodes in use: %s limits (soft = %s hard = %s "
    870 			    "grace = %s", scuri, ssoft, shard, stime);
    871 			if (cnt == 3) {
    872 				if (version != 1 ||
    873 				    last_char(scuri) != ',' ||
    874 				    last_char(ssoft) != ',' ||
    875 				    last_char(shard) != ')') {
    876 					warnx("%s:%s: bad format %d",
    877 					    fsp, line2, cnt);
    878 					goto out;
    879 				}
    880 				stime[0] = '\0';
    881 			} else if (cnt == 4) {
    882 				if (version < 2 ||
    883 				    last_char(scuri) != ',' ||
    884 				    last_char(ssoft) != ',' ||
    885 				    last_char(shard) != ',' ||
    886 				    last_char(stime) != ')') {
    887 					warnx("%s:%s: bad format %d",
    888 					    fsp, line2, cnt);
    889 					goto out;
    890 				}
    891 			} else {
    892 				warnx("%s: %s: bad format", fsp, line2);
    893 				goto out;
    894 			}
    895 			/* drop last char which is ',' or ')' */
    896 			last_char(scuri) = '\0';
    897 			last_char(ssoft) = '\0';
    898 			last_char(shard) = '\0';
    899 			last_char(stime) = '\0';
    900 			if (intrd(ssoft, &softi, 0) != 0) {
    901 				warnx("%s:%s: bad number", fsp, ssoft);
    902 				goto out;
    903 			}
    904 			if (intrd(shard, &hardi, 0) != 0) {
    905 				warnx("%s:%s: bad number", fsp, shard);
    906 				goto out;
    907 			}
    908 			if (cnt == 4) {
    909 				if (timeprd(stime, &gracei) != 0) {
    910 					warnx("%s:%s: bad number", fsp, stime);
    911 					goto out;
    912 				}
    913 			}
    914 		}
    915 		for (qup = quplist; qup; qup = qup->next) {
    916 			struct quota2_val *q = qup->q2e.q2e_val;
    917 			char b1[32], b2[32];
    918 			if (strcmp(fsp, qup->fsname))
    919 				continue;
    920 			if (version == 1 && dflag) {
    921 				q[QL_BLOCK].q2v_grace = graceb;
    922 				q[QL_FILE].q2v_grace = gracei;
    923 				qup->flags |= FOUND;
    924 				continue;
    925 			}
    926 
    927 			if (strcmp(intprt(b1, 21, q[QL_BLOCK].q2v_cur,
    928 			    HN_NOSPACE | HN_B, Hflag),
    929 			    scurb) != 0 ||
    930 			    strcmp(intprt(b2, 21, q[QL_FILE].q2v_cur,
    931 			    HN_NOSPACE, Hflag),
    932 			    scuri) != 0) {
    933 				warnx("%s: cannot change current allocation",
    934 				    fsp);
    935 				break;
    936 			}
    937 			/*
    938 			 * Cause time limit to be reset when the quota
    939 			 * is next used if previously had no soft limit
    940 			 * or were under it, but now have a soft limit
    941 			 * and are over it.
    942 			 */
    943 			if (q[QL_BLOCK].q2v_cur &&
    944 			    q[QL_BLOCK].q2v_cur >= softb &&
    945 			    (q[QL_BLOCK].q2v_softlimit == 0 ||
    946 			     q[QL_BLOCK].q2v_cur < q[QL_BLOCK].q2v_softlimit))
    947 				q[QL_BLOCK].q2v_time = 0;
    948 			if (q[QL_FILE].q2v_cur &&
    949 			    q[QL_FILE].q2v_cur >= softi &&
    950 			    (q[QL_FILE].q2v_softlimit == 0 ||
    951 			     q[QL_FILE].q2v_cur < q[QL_FILE].q2v_softlimit))
    952 				q[QL_FILE].q2v_time = 0;
    953 			q[QL_BLOCK].q2v_softlimit = softb;
    954 			q[QL_BLOCK].q2v_hardlimit = hardb;
    955 			if (version == 2)
    956 				q[QL_BLOCK].q2v_grace = graceb;
    957 			q[QL_FILE].q2v_softlimit  = softi;
    958 			q[QL_FILE].q2v_hardlimit  = hardi;
    959 			if (version == 2)
    960 				q[QL_FILE].q2v_grace = gracei;
    961 			qup->flags |= FOUND;
    962 		}
    963 	}
    964 out:
    965 	fclose(fd);
    966 	/*
    967 	 * Disable quotas for any filesystems that have not been found.
    968 	 */
    969 	for (qup = quplist; qup; qup = qup->next) {
    970 		struct quota2_val *q = qup->q2e.q2e_val;
    971 		if (qup->flags & FOUND) {
    972 			qup->flags &= ~FOUND;
    973 			continue;
    974 		}
    975 		q[QL_BLOCK].q2v_softlimit = UQUAD_MAX;
    976 		q[QL_BLOCK].q2v_hardlimit = UQUAD_MAX;
    977 		q[QL_BLOCK].q2v_grace = 0;
    978 		q[QL_FILE].q2v_softlimit = UQUAD_MAX;
    979 		q[QL_FILE].q2v_hardlimit = UQUAD_MAX;
    980 		q[QL_FILE].q2v_grace = 0;
    981 	}
    982 	return 1;
    983 }
    984 
    985 /*
    986  * Free a quotause structure.
    987  */
    988 static void
    989 freeq(struct quotause *qup)
    990 {
    991 	free(qup->qfname);
    992 	free(qup);
    993 }
    994 
    995 /*
    996  * Free a list of quotause structures.
    997  */
    998 static void
    999 freeprivs(struct quotause *quplist)
   1000 {
   1001 	struct quotause *qup, *nextqup;
   1002 
   1003 	for (qup = quplist; qup; qup = nextqup) {
   1004 		nextqup = qup->next;
   1005 		freeq(qup);
   1006 	}
   1007 }
   1008 
   1009 static void
   1010 clearpriv(int argc, char **argv, const char *filesys, int quotatype)
   1011 {
   1012 	prop_array_t cmds, datas;
   1013 	prop_dictionary_t protodict, dict, data, cmd;
   1014 	struct plistref pref;
   1015 	bool ret;
   1016 	struct statvfs *fst;
   1017 	int nfst, i;
   1018 	int8_t error8;
   1019 	int id;
   1020 
   1021 	/* build a generic command */
   1022 	protodict = quota2_prop_create();
   1023 	cmds = prop_array_create();
   1024 	datas = prop_array_create();
   1025 	if (protodict == NULL || cmds == NULL || datas == NULL) {
   1026 		errx(1, "can't allocate proplist");
   1027 	}
   1028 
   1029 	for ( ; argc > 0; argc--, argv++) {
   1030 		if ((id = getentry(*argv, quotatype)) == -1)
   1031 			continue;
   1032 		data = prop_dictionary_create();
   1033 		if (data == NULL)
   1034 			errx(1, "can't allocate proplist");
   1035 
   1036 		ret = prop_dictionary_set_uint32(data, "id", id);
   1037 		if (!ret)
   1038 			err(1, "prop_dictionary_set(id)");
   1039 		if (!prop_array_add_and_rel(datas, data))
   1040 			err(1, "prop_array_add(data)");
   1041 	}
   1042 	if (!quota2_prop_add_command(cmds, "clear", qfextension[quotatype],
   1043 	    datas))
   1044 		err(1, "prop_add_command");
   1045 
   1046 	if (!prop_dictionary_set(protodict, "commands", cmds))
   1047 		err(1, "prop_dictionary_set(command)");
   1048 
   1049 	/* now loop over quota-enabled filesystems */
   1050 	nfst = getmntinfo(&fst, MNT_WAIT);
   1051 	if (nfst == 0)
   1052 		errx(1, "no filesystems mounted!");
   1053 
   1054 	for (i = 0; i < nfst; i++) {
   1055 		if ((fst[i].f_flag & ST_QUOTA) == 0)
   1056 			continue;
   1057 		if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
   1058 		    strcmp(fst[i].f_mntfromname, filesys) != 0)
   1059 			continue;
   1060 		if (Dflag) {
   1061 			fprintf(stderr, "message to kernel for %s:\n%s\n",
   1062 			    fst[i].f_mntonname,
   1063 			    prop_dictionary_externalize(protodict));
   1064 		}
   1065 
   1066 		if (!prop_dictionary_send_syscall(protodict, &pref))
   1067 			err(1, "prop_dictionary_send_syscall");
   1068 		if (quotactl(fst[i].f_mntonname, &pref) != 0)
   1069 			err(1, "quotactl");
   1070 
   1071 		if ((errno = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
   1072 			err(1, "prop_dictionary_recv_syscall");
   1073 		}
   1074 
   1075 		if (Dflag) {
   1076 			fprintf(stderr, "reply from kernel for %s:\n%s\n",
   1077 			    fst[i].f_mntonname,
   1078 			    prop_dictionary_externalize(dict));
   1079 		}
   1080 		if ((errno = quota2_get_cmds(dict, &cmds)) != 0) {
   1081 			err(1, "quota2_get_cmds");
   1082 		}
   1083 		/* only one command, no need to iter */
   1084 		cmd = prop_array_get(cmds, 0);
   1085 		if (cmd == NULL)
   1086 			err(1, "prop_array_get(cmd)");
   1087 
   1088 		if (!prop_dictionary_get_int8(cmd, "return", &error8))
   1089 			err(1, "prop_get(return)");
   1090 		if (error8) {
   1091 			errno = error8;
   1092 			warn("clear %s quota entries on %s",
   1093 			    qfextension[quotatype], fst[i].f_mntonname);
   1094 		}
   1095 		prop_object_release(dict);
   1096 	}
   1097 	prop_object_release(protodict);
   1098 }
   1099