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