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