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