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