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