Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.36.2.2
      1  1.36.2.2      yamt /*      $NetBSD: edquota.c,v 1.36.2.2 2012/10/30 19:00:32 yamt 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.2.2      yamt __RCSID("$NetBSD: edquota.c,v 1.36.2.2 2012/10/30 19:00:32 yamt 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.36.2.1      yamt #include <quota.h>
     60      1.30    bouyer 
     61      1.30    bouyer #include <assert.h>
     62      1.15     lukem #include <err.h>
     63       1.1       cgd #include <errno.h>
     64       1.1       cgd #include <fstab.h>
     65       1.1       cgd #include <pwd.h>
     66       1.1       cgd #include <grp.h>
     67       1.1       cgd #include <ctype.h>
     68      1.15     lukem #include <signal.h>
     69  1.36.2.1      yamt #include <stdbool.h>
     70       1.1       cgd #include <stdio.h>
     71       1.9       cgd #include <stdlib.h>
     72       1.1       cgd #include <string.h>
     73       1.5   mycroft #include <unistd.h>
     74  1.36.2.1      yamt #include <util.h>
     75      1.30    bouyer 
     76      1.31  christos #include "printquota.h"
     77      1.30    bouyer 
     78       1.1       cgd #include "pathnames.h"
     79       1.1       cgd 
     80  1.36.2.1      yamt /*
     81  1.36.2.1      yamt  * XXX. Ideally we shouldn't compile this in, but it'll take some
     82  1.36.2.1      yamt  * reworking to avoid it and it'll be ok for now.
     83  1.36.2.1      yamt  */
     84  1.36.2.1      yamt #define EDQUOTA_NUMOBJTYPES	2
     85  1.36.2.1      yamt 
     86  1.36.2.1      yamt #if 0
     87      1.31  christos static const char *quotagroup = QUOTAGROUP;
     88  1.36.2.1      yamt #endif
     89  1.36.2.1      yamt 
     90  1.36.2.1      yamt #define MAX_TMPSTR	(100+MAXPATHLEN)
     91  1.36.2.1      yamt 
     92  1.36.2.2      yamt enum sources {
     93  1.36.2.2      yamt 	SRC_EDITED,	/* values came from user */
     94  1.36.2.2      yamt 	SRC_QUOTA,	/* values came from a specific quota entry */
     95  1.36.2.2      yamt 	SRC_DEFAULT,	/* values were copied from the default quota entry */
     96  1.36.2.2      yamt 	SRC_CLEAR,	/* values arose by zeroing out a quota entry */
     97  1.36.2.2      yamt };
     98       1.1       cgd 
     99       1.1       cgd struct quotause {
    100       1.1       cgd 	struct	quotause *next;
    101  1.36.2.2      yamt 	unsigned found:1,	/* found after running editor */
    102  1.36.2.2      yamt 		xgrace:1,	/* grace periods are per-id */
    103  1.36.2.2      yamt 		isdefault:1;
    104  1.36.2.2      yamt 
    105  1.36.2.1      yamt 	struct	quotaval qv[EDQUOTA_NUMOBJTYPES];
    106  1.36.2.2      yamt 	enum sources source[EDQUOTA_NUMOBJTYPES];
    107       1.1       cgd 	char	fsname[MAXPATHLEN + 1];
    108  1.36.2.1      yamt 	char	implementation[32];
    109      1.15     lukem };
    110      1.13     mikel 
    111  1.36.2.1      yamt struct quotalist {
    112  1.36.2.1      yamt 	struct quotause *head;
    113  1.36.2.1      yamt 	struct quotause *tail;
    114  1.36.2.1      yamt 	char *idtypename;
    115  1.36.2.1      yamt };
    116      1.23      jonb 
    117      1.33  dholland static void	usage(void) __dead;
    118      1.30    bouyer 
    119      1.31  christos static int Hflag = 0;
    120       1.1       cgd 
    121      1.32    bouyer /* more compact form of constants */
    122  1.36.2.1      yamt #define QO_BLK QUOTA_OBJTYPE_BLOCKS
    123  1.36.2.1      yamt #define QO_FL  QUOTA_OBJTYPE_FILES
    124      1.32    bouyer 
    125      1.34  dholland ////////////////////////////////////////////////////////////
    126      1.34  dholland // support code
    127       1.1       cgd 
    128       1.1       cgd /*
    129      1.35  dholland  * This routine converts a name for a particular quota class to
    130       1.1       cgd  * an identifier. This routine must agree with the kernel routine
    131      1.35  dholland  * getinoquota as to the interpretation of quota classes.
    132       1.1       cgd  */
    133      1.31  christos static int
    134  1.36.2.1      yamt getidbyname(const char *name, int idtype)
    135       1.1       cgd {
    136       1.1       cgd 	struct passwd *pw;
    137       1.1       cgd 	struct group *gr;
    138       1.1       cgd 
    139       1.1       cgd 	if (alldigits(name))
    140      1.31  christos 		return atoi(name);
    141  1.36.2.1      yamt 	switch (idtype) {
    142  1.36.2.1      yamt 	case QUOTA_IDTYPE_USER:
    143      1.15     lukem 		if ((pw = getpwnam(name)) != NULL)
    144      1.31  christos 			return pw->pw_uid;
    145      1.15     lukem 		warnx("%s: no such user", name);
    146       1.1       cgd 		break;
    147  1.36.2.1      yamt 	case QUOTA_IDTYPE_GROUP:
    148      1.15     lukem 		if ((gr = getgrnam(name)) != NULL)
    149      1.31  christos 			return gr->gr_gid;
    150      1.15     lukem 		warnx("%s: no such group", name);
    151       1.1       cgd 		break;
    152       1.1       cgd 	default:
    153  1.36.2.1      yamt 		warnx("%d: unknown quota type", idtype);
    154       1.1       cgd 		break;
    155       1.1       cgd 	}
    156       1.1       cgd 	sleep(1);
    157      1.31  christos 	return -1;
    158       1.1       cgd }
    159       1.1       cgd 
    160  1.36.2.2      yamt /*
    161  1.36.2.2      yamt  * check if a source is "real" (reflects actual data) or not
    162  1.36.2.2      yamt  */
    163  1.36.2.2      yamt static bool
    164  1.36.2.2      yamt source_is_real(enum sources source)
    165  1.36.2.2      yamt {
    166  1.36.2.2      yamt 	switch (source) {
    167  1.36.2.2      yamt 	    case SRC_EDITED:
    168  1.36.2.2      yamt 	    case SRC_QUOTA:
    169  1.36.2.2      yamt 		return true;
    170  1.36.2.2      yamt 	    case SRC_DEFAULT:
    171  1.36.2.2      yamt 	    case SRC_CLEAR:
    172  1.36.2.2      yamt 		return false;
    173  1.36.2.2      yamt 	}
    174  1.36.2.2      yamt 	assert(!"encountered invalid source");
    175  1.36.2.2      yamt 	return false;
    176  1.36.2.2      yamt }
    177  1.36.2.2      yamt 
    178  1.36.2.2      yamt /*
    179  1.36.2.2      yamt  * some simple string tools
    180  1.36.2.2      yamt  */
    181  1.36.2.2      yamt 
    182  1.36.2.2      yamt static /*const*/ char *
    183  1.36.2.2      yamt skipws(/*const*/ char *s)
    184  1.36.2.2      yamt {
    185  1.36.2.2      yamt 	while (*s == ' ' || *s == '\t') {
    186  1.36.2.2      yamt 		s++;
    187  1.36.2.2      yamt 	}
    188  1.36.2.2      yamt 	return s;
    189  1.36.2.2      yamt }
    190  1.36.2.2      yamt 
    191  1.36.2.2      yamt static /*const*/ char *
    192  1.36.2.2      yamt skipword(/*const*/ char *s)
    193  1.36.2.2      yamt {
    194  1.36.2.2      yamt 	while (*s != '\0' && *s != '\n' && *s != ' ' && *s != '\t') {
    195  1.36.2.2      yamt 		s++;
    196  1.36.2.2      yamt 	}
    197  1.36.2.2      yamt 	return s;
    198  1.36.2.2      yamt }
    199  1.36.2.2      yamt 
    200      1.34  dholland ////////////////////////////////////////////////////////////
    201      1.34  dholland // quotause operations
    202      1.34  dholland 
    203      1.34  dholland /*
    204  1.36.2.1      yamt  * Create an empty quotause structure.
    205  1.36.2.1      yamt  */
    206  1.36.2.1      yamt static struct quotause *
    207  1.36.2.1      yamt quotause_create(void)
    208  1.36.2.1      yamt {
    209  1.36.2.1      yamt 	struct quotause *qup;
    210  1.36.2.2      yamt 	unsigned i;
    211  1.36.2.1      yamt 
    212  1.36.2.1      yamt 	qup = malloc(sizeof(*qup));
    213  1.36.2.1      yamt 	if (qup == NULL) {
    214  1.36.2.1      yamt 		err(1, "malloc");
    215  1.36.2.1      yamt 	}
    216  1.36.2.1      yamt 
    217  1.36.2.1      yamt 	qup->next = NULL;
    218  1.36.2.2      yamt 	qup->found = 0;
    219  1.36.2.2      yamt 	qup->xgrace = 0;
    220  1.36.2.2      yamt 	qup->isdefault = 0;
    221  1.36.2.2      yamt 	for (i=0; i<EDQUOTA_NUMOBJTYPES; i++) {
    222  1.36.2.2      yamt 		quotaval_clear(&qup->qv[i]);
    223  1.36.2.2      yamt 		qup->source[i] = SRC_CLEAR;
    224  1.36.2.2      yamt 	}
    225  1.36.2.1      yamt 	qup->fsname[0] = '\0';
    226  1.36.2.1      yamt 
    227  1.36.2.1      yamt 	return qup;
    228  1.36.2.1      yamt }
    229  1.36.2.1      yamt 
    230  1.36.2.1      yamt /*
    231      1.34  dholland  * Free a quotause structure.
    232      1.34  dholland  */
    233      1.34  dholland static void
    234      1.35  dholland quotause_destroy(struct quotause *qup)
    235      1.34  dholland {
    236      1.34  dholland 	free(qup);
    237      1.34  dholland }
    238      1.34  dholland 
    239  1.36.2.1      yamt ////////////////////////////////////////////////////////////
    240  1.36.2.1      yamt // quotalist operations
    241  1.36.2.1      yamt 
    242  1.36.2.1      yamt /*
    243  1.36.2.1      yamt  * Create a quotause list.
    244  1.36.2.1      yamt  */
    245  1.36.2.1      yamt static struct quotalist *
    246  1.36.2.1      yamt quotalist_create(void)
    247  1.36.2.1      yamt {
    248  1.36.2.1      yamt 	struct quotalist *qlist;
    249  1.36.2.1      yamt 
    250  1.36.2.1      yamt 	qlist = malloc(sizeof(*qlist));
    251  1.36.2.1      yamt 	if (qlist == NULL) {
    252  1.36.2.1      yamt 		err(1, "malloc");
    253  1.36.2.1      yamt 	}
    254  1.36.2.1      yamt 
    255  1.36.2.1      yamt 	qlist->head = NULL;
    256  1.36.2.1      yamt 	qlist->tail = NULL;
    257  1.36.2.1      yamt 	qlist->idtypename = NULL;
    258  1.36.2.1      yamt 
    259  1.36.2.1      yamt 	return qlist;
    260  1.36.2.1      yamt }
    261  1.36.2.1      yamt 
    262       1.1       cgd /*
    263      1.34  dholland  * Free a list of quotause structures.
    264       1.1       cgd  */
    265      1.34  dholland static void
    266  1.36.2.1      yamt quotalist_destroy(struct quotalist *qlist)
    267       1.1       cgd {
    268      1.34  dholland 	struct quotause *qup, *nextqup;
    269      1.34  dholland 
    270  1.36.2.1      yamt 	for (qup = qlist->head; qup; qup = nextqup) {
    271      1.34  dholland 		nextqup = qup->next;
    272      1.35  dholland 		quotause_destroy(qup);
    273      1.34  dholland 	}
    274  1.36.2.1      yamt 	free(qlist->idtypename);
    275  1.36.2.1      yamt 	free(qlist);
    276  1.36.2.1      yamt }
    277  1.36.2.1      yamt 
    278  1.36.2.1      yamt #if 0
    279  1.36.2.1      yamt static bool
    280  1.36.2.1      yamt quotalist_empty(struct quotalist *qlist)
    281  1.36.2.1      yamt {
    282  1.36.2.1      yamt 	return qlist->head == NULL;
    283  1.36.2.1      yamt }
    284  1.36.2.1      yamt #endif
    285  1.36.2.1      yamt 
    286  1.36.2.1      yamt static void
    287  1.36.2.1      yamt quotalist_append(struct quotalist *qlist, struct quotause *qup)
    288  1.36.2.1      yamt {
    289  1.36.2.1      yamt 	/* should not already be on a list */
    290  1.36.2.1      yamt 	assert(qup->next == NULL);
    291  1.36.2.1      yamt 
    292  1.36.2.1      yamt 	if (qlist->head == NULL) {
    293  1.36.2.1      yamt 		qlist->head = qup;
    294  1.36.2.1      yamt 	} else {
    295  1.36.2.1      yamt 		qlist->tail->next = qup;
    296  1.36.2.1      yamt 	}
    297  1.36.2.1      yamt 	qlist->tail = qup;
    298      1.34  dholland }
    299      1.30    bouyer 
    300      1.34  dholland ////////////////////////////////////////////////////////////
    301      1.34  dholland // ffs quota v1
    302       1.1       cgd 
    303  1.36.2.1      yamt #if 0
    304      1.34  dholland static void
    305  1.36.2.1      yamt putprivs1(uint32_t id, int idtype, struct quotause *qup)
    306      1.34  dholland {
    307      1.34  dholland 	struct dqblk dqblk;
    308      1.34  dholland 	int fd;
    309       1.1       cgd 
    310  1.36.2.1      yamt 	quotavals_to_dqblk(&qup->qv[QUOTA_LIMIT_BLOCK],
    311  1.36.2.1      yamt 			   &qup->qv[QUOTA_LIMIT_FILE],
    312  1.36.2.1      yamt 			   &dqblk);
    313      1.34  dholland 	assert((qup->flags & DEFAULT) == 0);
    314      1.30    bouyer 
    315      1.34  dholland 	if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    316      1.34  dholland 		warnx("open `%s'", qup->qfname);
    317      1.34  dholland 	} else {
    318      1.34  dholland 		(void)lseek(fd,
    319      1.34  dholland 		    (off_t)(id * (long)sizeof (struct dqblk)),
    320      1.34  dholland 		    SEEK_SET);
    321      1.34  dholland 		if (write(fd, &dqblk, sizeof (struct dqblk)) !=
    322      1.34  dholland 		    sizeof (struct dqblk))
    323      1.34  dholland 			warnx("writing `%s'", qup->qfname);
    324      1.34  dholland 		close(fd);
    325      1.30    bouyer 	}
    326      1.30    bouyer }
    327      1.30    bouyer 
    328      1.31  christos static struct quotause *
    329  1.36.2.1      yamt getprivs1(long id, int idtype, const char *filesys)
    330      1.30    bouyer {
    331      1.30    bouyer 	struct fstab *fs;
    332  1.36.2.1      yamt 	char qfpathname[MAXPATHLEN], xbuf[MAXPATHLEN];
    333  1.36.2.1      yamt 	const char *fsspec;
    334      1.30    bouyer 	struct quotause *qup;
    335      1.30    bouyer 	struct dqblk dqblk;
    336      1.30    bouyer 	int fd;
    337      1.30    bouyer 
    338      1.30    bouyer 	setfsent();
    339      1.30    bouyer 	while ((fs = getfsent()) != NULL) {
    340      1.30    bouyer 		if (strcmp(fs->fs_vfstype, "ffs"))
    341      1.30    bouyer 			continue;
    342  1.36.2.1      yamt 		fsspec = getfsspecname(xbuf, sizeof(xbuf), fs->fs_spec);
    343  1.36.2.1      yamt 		if (fsspec == NULL) {
    344  1.36.2.1      yamt 			warn("%s", xbuf);
    345  1.36.2.1      yamt 			continue;
    346  1.36.2.1      yamt 		}
    347  1.36.2.1      yamt 		if (strcmp(fsspec, filesys) == 0 ||
    348      1.30    bouyer 		    strcmp(fs->fs_file, filesys) == 0)
    349      1.30    bouyer 			break;
    350      1.30    bouyer 	}
    351      1.30    bouyer 	if (fs == NULL)
    352      1.30    bouyer 		return NULL;
    353      1.30    bouyer 
    354      1.32    bouyer 	if (!hasquota(qfpathname, sizeof(qfpathname), fs,
    355  1.36.2.1      yamt 	    quota_idtype_to_ufs(idtype)))
    356      1.30    bouyer 		return NULL;
    357  1.36.2.1      yamt 
    358  1.36.2.1      yamt 	qup = quotause_create();
    359      1.30    bouyer 	strcpy(qup->fsname, fs->fs_file);
    360      1.30    bouyer 	if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    361      1.30    bouyer 		fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    362      1.30    bouyer 		if (fd < 0 && errno != ENOENT) {
    363      1.30    bouyer 			warnx("open `%s'", qfpathname);
    364      1.35  dholland 			quotause_destroy(qup);
    365      1.30    bouyer 			return NULL;
    366      1.30    bouyer 		}
    367      1.30    bouyer 		warnx("Creating quota file %s", qfpathname);
    368      1.30    bouyer 		sleep(3);
    369      1.32    bouyer 		(void)fchown(fd, getuid(),
    370      1.35  dholland 		    getidbyname(quotagroup, QUOTA_CLASS_GROUP));
    371      1.31  christos 		(void)fchmod(fd, 0640);
    372      1.30    bouyer 	}
    373      1.30    bouyer 	(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    374      1.30    bouyer 	    SEEK_SET);
    375      1.30    bouyer 	switch (read(fd, &dqblk, sizeof(struct dqblk))) {
    376      1.30    bouyer 	case 0:			/* EOF */
    377      1.30    bouyer 		/*
    378      1.30    bouyer 		 * Convert implicit 0 quota (EOF)
    379      1.30    bouyer 		 * into an explicit one (zero'ed dqblk)
    380      1.30    bouyer 		 */
    381      1.31  christos 		memset(&dqblk, 0, sizeof(struct dqblk));
    382      1.30    bouyer 		break;
    383      1.30    bouyer 
    384      1.30    bouyer 	case sizeof(struct dqblk):	/* OK */
    385      1.30    bouyer 		break;
    386      1.30    bouyer 
    387      1.30    bouyer 	default:		/* ERROR */
    388      1.30    bouyer 		warn("read error in `%s'", qfpathname);
    389      1.30    bouyer 		close(fd);
    390      1.35  dholland 		quotause_destroy(qup);
    391      1.30    bouyer 		return NULL;
    392      1.30    bouyer 	}
    393      1.30    bouyer 	close(fd);
    394      1.30    bouyer 	qup->qfname = qfpathname;
    395       1.1       cgd 	endfsent();
    396  1.36.2.1      yamt 	dqblk_to_quotavals(&dqblk,
    397  1.36.2.1      yamt 			   &qup->qv[QUOTA_LIMIT_BLOCK],
    398  1.36.2.1      yamt 			   &qup->qv[QUOTA_LIMIT_FILE]);
    399      1.31  christos 	return qup;
    400       1.1       cgd }
    401  1.36.2.1      yamt #endif
    402       1.1       cgd 
    403      1.34  dholland ////////////////////////////////////////////////////////////
    404  1.36.2.2      yamt // generic quota interface
    405      1.34  dholland 
    406  1.36.2.1      yamt static int
    407  1.36.2.1      yamt dogetprivs2(struct quotahandle *qh, int idtype, id_t id, int defaultq,
    408  1.36.2.1      yamt 	    int objtype, struct quotause *qup)
    409  1.36.2.1      yamt {
    410  1.36.2.1      yamt 	struct quotakey qk;
    411  1.36.2.1      yamt 
    412  1.36.2.1      yamt 	qk.qk_idtype = idtype;
    413  1.36.2.1      yamt 	qk.qk_id = defaultq ? QUOTA_DEFAULTID : id;
    414  1.36.2.1      yamt 	qk.qk_objtype = objtype;
    415  1.36.2.2      yamt 	if (quota_get(qh, &qk, &qup->qv[objtype]) == 0) {
    416  1.36.2.2      yamt 		/* succeeded */
    417  1.36.2.2      yamt 		qup->source[objtype] = SRC_QUOTA;
    418  1.36.2.2      yamt 		return 0;
    419  1.36.2.2      yamt 	}
    420  1.36.2.2      yamt 	if (errno != ENOENT) {
    421  1.36.2.2      yamt 		/* serious failure */
    422  1.36.2.2      yamt 		return -1;
    423  1.36.2.2      yamt 	}
    424  1.36.2.2      yamt 
    425  1.36.2.2      yamt 	/* no entry, get default entry */
    426  1.36.2.2      yamt 	qk.qk_id = QUOTA_DEFAULTID;
    427  1.36.2.2      yamt 	if (quota_get(qh, &qk, &qup->qv[objtype]) == 0) {
    428  1.36.2.2      yamt 		/* succeeded */
    429  1.36.2.2      yamt 		qup->source[objtype] = SRC_DEFAULT;
    430  1.36.2.2      yamt 		return 0;
    431  1.36.2.1      yamt 	}
    432  1.36.2.2      yamt 	if (errno != ENOENT) {
    433  1.36.2.2      yamt 		return -1;
    434  1.36.2.2      yamt 	}
    435  1.36.2.2      yamt 
    436  1.36.2.2      yamt 	/* use a zeroed-out entry */
    437  1.36.2.2      yamt 	quotaval_clear(&qup->qv[objtype]);
    438  1.36.2.2      yamt 	qup->source[objtype] = SRC_CLEAR;
    439  1.36.2.1      yamt 	return 0;
    440  1.36.2.1      yamt }
    441  1.36.2.1      yamt 
    442      1.34  dholland static struct quotause *
    443  1.36.2.1      yamt getprivs2(long id, int idtype, const char *filesys, int defaultq,
    444  1.36.2.1      yamt 	  char **idtypename_p)
    445       1.1       cgd {
    446      1.15     lukem 	struct quotause *qup;
    447  1.36.2.1      yamt 	struct quotahandle *qh;
    448  1.36.2.1      yamt 	const char *impl;
    449  1.36.2.1      yamt 	unsigned restrictions;
    450  1.36.2.1      yamt 	const char *idtypename;
    451  1.36.2.2      yamt 	int serrno;
    452       1.1       cgd 
    453  1.36.2.1      yamt 	qup = quotause_create();
    454      1.34  dholland 	strcpy(qup->fsname, filesys);
    455      1.34  dholland 	if (defaultq)
    456  1.36.2.2      yamt 		qup->isdefault = 1;
    457  1.36.2.1      yamt 
    458  1.36.2.1      yamt 	qh = quota_open(filesys);
    459  1.36.2.1      yamt 	if (qh == NULL) {
    460  1.36.2.2      yamt 		serrno = errno;
    461  1.36.2.1      yamt 		quotause_destroy(qup);
    462  1.36.2.2      yamt 		errno = serrno;
    463  1.36.2.1      yamt 		return NULL;
    464  1.36.2.1      yamt 	}
    465  1.36.2.1      yamt 
    466  1.36.2.1      yamt 	impl = quota_getimplname(qh);
    467  1.36.2.1      yamt 	if (impl == NULL) {
    468  1.36.2.1      yamt 		impl = "???";
    469  1.36.2.1      yamt 	}
    470  1.36.2.1      yamt 	strlcpy(qup->implementation, impl, sizeof(qup->implementation));
    471  1.36.2.1      yamt 
    472  1.36.2.1      yamt 	restrictions = quota_getrestrictions(qh);
    473  1.36.2.1      yamt 	if ((restrictions & QUOTA_RESTRICT_UNIFORMGRACE) == 0) {
    474  1.36.2.2      yamt 		qup->xgrace = 1;
    475  1.36.2.1      yamt 	}
    476  1.36.2.1      yamt 
    477  1.36.2.1      yamt 	if (*idtypename_p == NULL) {
    478  1.36.2.1      yamt 		idtypename = quota_idtype_getname(qh, idtype);
    479  1.36.2.1      yamt 		*idtypename_p = strdup(idtypename);
    480  1.36.2.1      yamt 		if (*idtypename_p == NULL) {
    481  1.36.2.1      yamt 			errx(1, "Out of memory");
    482      1.34  dholland 		}
    483      1.30    bouyer 	}
    484  1.36.2.1      yamt 
    485  1.36.2.1      yamt 	if (dogetprivs2(qh, idtype, id, defaultq, QUOTA_OBJTYPE_BLOCKS, qup)) {
    486  1.36.2.2      yamt 		serrno = errno;
    487  1.36.2.1      yamt 		quota_close(qh);
    488  1.36.2.1      yamt 		quotause_destroy(qup);
    489  1.36.2.2      yamt 		errno = serrno;
    490  1.36.2.1      yamt 		return NULL;
    491  1.36.2.1      yamt 	}
    492  1.36.2.1      yamt 
    493  1.36.2.1      yamt 	if (dogetprivs2(qh, idtype, id, defaultq, QUOTA_OBJTYPE_FILES, qup)) {
    494  1.36.2.2      yamt 		serrno = errno;
    495  1.36.2.1      yamt 		quota_close(qh);
    496  1.36.2.1      yamt 		quotause_destroy(qup);
    497  1.36.2.2      yamt 		errno = serrno;
    498  1.36.2.1      yamt 		return NULL;
    499  1.36.2.1      yamt 	}
    500  1.36.2.1      yamt 
    501  1.36.2.1      yamt 	quota_close(qh);
    502  1.36.2.1      yamt 
    503      1.34  dholland 	return qup;
    504      1.30    bouyer }
    505      1.30    bouyer 
    506      1.31  christos static void
    507  1.36.2.1      yamt putprivs2(uint32_t id, int idtype, struct quotause *qup)
    508      1.30    bouyer {
    509  1.36.2.1      yamt 	struct quotahandle *qh;
    510  1.36.2.1      yamt 	struct quotakey qk;
    511  1.36.2.1      yamt 	char idname[32];
    512  1.36.2.1      yamt 
    513  1.36.2.2      yamt 	if (qup->isdefault) {
    514  1.36.2.1      yamt 		snprintf(idname, sizeof(idname), "%s default",
    515  1.36.2.1      yamt 			 idtype == QUOTA_IDTYPE_USER ? "user" : "group");
    516  1.36.2.1      yamt 		id = QUOTA_DEFAULTID;
    517  1.36.2.1      yamt 	} else {
    518  1.36.2.1      yamt 		snprintf(idname, sizeof(idname), "%s %u",
    519  1.36.2.1      yamt 			 idtype == QUOTA_IDTYPE_USER ? "uid" : "gid", id);
    520  1.36.2.1      yamt 	}
    521      1.30    bouyer 
    522  1.36.2.1      yamt 	qh = quota_open(qup->fsname);
    523  1.36.2.1      yamt 	if (qh == NULL) {
    524  1.36.2.1      yamt 		err(1, "%s: quota_open", qup->fsname);
    525      1.30    bouyer 	}
    526  1.36.2.1      yamt 
    527  1.36.2.2      yamt 	if (source_is_real(qup->source[QUOTA_OBJTYPE_BLOCKS])) {
    528  1.36.2.2      yamt 		qk.qk_idtype = idtype;
    529  1.36.2.2      yamt 		qk.qk_id = id;
    530  1.36.2.2      yamt 		qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    531  1.36.2.2      yamt 		if (quota_put(qh, &qk, &qup->qv[QO_BLK])) {
    532  1.36.2.2      yamt 			err(1, "%s: quota_put (%s blocks)", qup->fsname,
    533  1.36.2.2      yamt 			    idname);
    534  1.36.2.2      yamt 		}
    535  1.36.2.1      yamt 	}
    536  1.36.2.1      yamt 
    537  1.36.2.2      yamt 	if (source_is_real(qup->source[QUOTA_OBJTYPE_FILES])) {
    538  1.36.2.2      yamt 		qk.qk_idtype = idtype;
    539  1.36.2.2      yamt 		qk.qk_id = id;
    540  1.36.2.2      yamt 		qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    541  1.36.2.2      yamt 		if (quota_put(qh, &qk, &qup->qv[QO_FL])) {
    542  1.36.2.2      yamt 			err(1, "%s: quota_put (%s files)", qup->fsname,
    543  1.36.2.2      yamt 			    idname);
    544  1.36.2.2      yamt 		}
    545  1.36.2.1      yamt 	}
    546  1.36.2.1      yamt 
    547  1.36.2.1      yamt 	quota_close(qh);
    548      1.30    bouyer }
    549      1.30    bouyer 
    550      1.34  dholland ////////////////////////////////////////////////////////////
    551      1.34  dholland // quota format switch
    552      1.34  dholland 
    553      1.34  dholland /*
    554      1.34  dholland  * Collect the requested quota information.
    555      1.34  dholland  */
    556  1.36.2.1      yamt static struct quotalist *
    557  1.36.2.1      yamt getprivs(long id, int defaultq, int idtype, const char *filesys)
    558      1.30    bouyer {
    559      1.34  dholland 	struct statvfs *fst;
    560      1.34  dholland 	int nfst, i;
    561  1.36.2.1      yamt 	struct quotalist *qlist;
    562  1.36.2.1      yamt 	struct quotause *qup;
    563  1.36.2.2      yamt 	int seenany = 0;
    564  1.36.2.1      yamt 
    565  1.36.2.1      yamt 	qlist = quotalist_create();
    566      1.34  dholland 
    567      1.34  dholland 	nfst = getmntinfo(&fst, MNT_WAIT);
    568      1.34  dholland 	if (nfst == 0)
    569      1.34  dholland 		errx(1, "no filesystems mounted!");
    570      1.30    bouyer 
    571      1.34  dholland 	for (i = 0; i < nfst; i++) {
    572      1.34  dholland 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    573      1.34  dholland 			continue;
    574  1.36.2.2      yamt 		seenany = 1;
    575  1.36.2.1      yamt 		if (filesys &&
    576  1.36.2.1      yamt 		    strcmp(fst[i].f_mntonname, filesys) != 0 &&
    577      1.34  dholland 		    strcmp(fst[i].f_mntfromname, filesys) != 0)
    578      1.34  dholland 			continue;
    579  1.36.2.1      yamt 		qup = getprivs2(id, idtype, fst[i].f_mntonname, defaultq,
    580  1.36.2.1      yamt 				&qlist->idtypename);
    581  1.36.2.1      yamt 		if (qup == NULL) {
    582  1.36.2.2      yamt 			/* XXX the atf tests demand failing silently */
    583  1.36.2.2      yamt 			/*warn("Reading quotas failed for id %ld", id);*/
    584  1.36.2.1      yamt 			continue;
    585  1.36.2.1      yamt 		}
    586  1.36.2.1      yamt 
    587  1.36.2.1      yamt 		quotalist_append(qlist, qup);
    588      1.34  dholland 	}
    589      1.30    bouyer 
    590  1.36.2.2      yamt 	if (!seenany) {
    591  1.36.2.2      yamt 		errx(1, "No mounted filesystems have quota support");
    592  1.36.2.2      yamt 	}
    593  1.36.2.2      yamt 
    594  1.36.2.1      yamt #if 0
    595  1.36.2.1      yamt 	if (filesys && quotalist_empty(qlist)) {
    596      1.34  dholland 		if (defaultq)
    597      1.34  dholland 			errx(1, "no default quota for version 1");
    598      1.34  dholland 		/* if we get there, filesys is not mounted. try the old way */
    599  1.36.2.1      yamt 		qup = getprivs1(id, idtype, filesys);
    600  1.36.2.1      yamt 		if (qup == NULL) {
    601  1.36.2.2      yamt 			warnx("getprivs1 failed");
    602  1.36.2.1      yamt 			return qlist;
    603  1.36.2.1      yamt 		}
    604  1.36.2.1      yamt 		quotalist_append(qlist, qup);
    605       1.1       cgd 	}
    606  1.36.2.1      yamt #endif
    607  1.36.2.1      yamt 	return qlist;
    608       1.1       cgd }
    609       1.1       cgd 
    610       1.1       cgd /*
    611      1.34  dholland  * Store the requested quota information.
    612       1.1       cgd  */
    613  1.36.2.1      yamt static void
    614  1.36.2.1      yamt putprivs(uint32_t id, int idtype, struct quotalist *qlist)
    615       1.1       cgd {
    616      1.34  dholland 	struct quotause *qup;
    617       1.1       cgd 
    618  1.36.2.1      yamt         for (qup = qlist->head; qup; qup = qup->next) {
    619  1.36.2.1      yamt #if 0
    620  1.36.2.2      yamt 		if (qup->qfname != NULL)
    621  1.36.2.1      yamt 			putprivs1(id, idtype, qup);
    622  1.36.2.2      yamt 		else
    623  1.36.2.1      yamt #endif
    624  1.36.2.2      yamt 			putprivs2(id, idtype, qup);
    625       1.1       cgd 	}
    626       1.1       cgd }
    627       1.1       cgd 
    628      1.34  dholland static void
    629  1.36.2.1      yamt clearpriv(int argc, char **argv, const char *filesys, int idtype)
    630       1.1       cgd {
    631      1.34  dholland 	struct statvfs *fst;
    632      1.34  dholland 	int nfst, i;
    633      1.34  dholland 	int id;
    634  1.36.2.1      yamt 	id_t *ids;
    635  1.36.2.1      yamt 	unsigned nids, maxids, j;
    636  1.36.2.1      yamt 	struct quotahandle *qh;
    637  1.36.2.1      yamt 	struct quotakey qk;
    638  1.36.2.1      yamt 	char idname[32];
    639  1.36.2.1      yamt 
    640  1.36.2.1      yamt 	maxids = 4;
    641  1.36.2.1      yamt 	nids = 0;
    642  1.36.2.1      yamt 	ids = malloc(maxids * sizeof(ids[0]));
    643  1.36.2.1      yamt 	if (ids == NULL) {
    644  1.36.2.1      yamt 		err(1, "malloc");
    645      1.34  dholland 	}
    646      1.34  dholland 
    647      1.34  dholland 	for ( ; argc > 0; argc--, argv++) {
    648  1.36.2.1      yamt 		if ((id = getidbyname(*argv, idtype)) == -1)
    649      1.34  dholland 			continue;
    650      1.34  dholland 
    651  1.36.2.1      yamt 		if (nids + 1 > maxids) {
    652  1.36.2.1      yamt 			maxids *= 2;
    653  1.36.2.1      yamt 			ids = realloc(ids, maxids * sizeof(ids[0]));
    654  1.36.2.1      yamt 			if (ids == NULL) {
    655  1.36.2.1      yamt 				err(1, "realloc");
    656  1.36.2.1      yamt 			}
    657  1.36.2.1      yamt 		}
    658  1.36.2.1      yamt 		ids[nids++] = id;
    659  1.36.2.1      yamt 	}
    660      1.34  dholland 
    661      1.34  dholland 	/* now loop over quota-enabled filesystems */
    662      1.34  dholland 	nfst = getmntinfo(&fst, MNT_WAIT);
    663      1.34  dholland 	if (nfst == 0)
    664      1.34  dholland 		errx(1, "no filesystems mounted!");
    665      1.34  dholland 
    666      1.34  dholland 	for (i = 0; i < nfst; i++) {
    667      1.34  dholland 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    668      1.34  dholland 			continue;
    669      1.34  dholland 		if (filesys && strcmp(fst[i].f_mntonname, filesys) != 0 &&
    670      1.34  dholland 		    strcmp(fst[i].f_mntfromname, filesys) != 0)
    671      1.34  dholland 			continue;
    672      1.34  dholland 
    673  1.36.2.1      yamt 		qh = quota_open(fst[i].f_mntonname);
    674  1.36.2.1      yamt 		if (qh == NULL) {
    675  1.36.2.1      yamt 			err(1, "%s: quota_open", fst[i].f_mntonname);
    676  1.36.2.1      yamt 		}
    677  1.36.2.1      yamt 
    678  1.36.2.1      yamt 		for (j = 0; j < nids; j++) {
    679  1.36.2.1      yamt 			snprintf(idname, sizeof(idname), "%s %u",
    680  1.36.2.1      yamt 				 idtype == QUOTA_IDTYPE_USER ?
    681  1.36.2.1      yamt 				 "uid" : "gid", ids[j]);
    682  1.36.2.1      yamt 
    683  1.36.2.1      yamt 			qk.qk_idtype = idtype;
    684  1.36.2.1      yamt 			qk.qk_id = ids[j];
    685  1.36.2.1      yamt 			qk.qk_objtype = QUOTA_OBJTYPE_BLOCKS;
    686  1.36.2.1      yamt 			if (quota_delete(qh, &qk)) {
    687  1.36.2.1      yamt 				err(1, "%s: quota_delete (%s blocks)",
    688  1.36.2.1      yamt 				    fst[i].f_mntonname, idname);
    689  1.36.2.1      yamt 			}
    690      1.34  dholland 
    691  1.36.2.1      yamt 			qk.qk_idtype = idtype;
    692  1.36.2.1      yamt 			qk.qk_id = ids[j];
    693  1.36.2.1      yamt 			qk.qk_objtype = QUOTA_OBJTYPE_FILES;
    694  1.36.2.1      yamt 			if (quota_delete(qh, &qk)) {
    695  1.36.2.1      yamt 				if (errno == ENOENT) {
    696  1.36.2.1      yamt  					/*
    697  1.36.2.1      yamt 					 * XXX ignore this case; due
    698  1.36.2.1      yamt 					 * to a weakness in the quota
    699  1.36.2.1      yamt 					 * proplib interface it can
    700  1.36.2.1      yamt 					 * appear spuriously.
    701  1.36.2.1      yamt 					 */
    702  1.36.2.1      yamt 				} else {
    703  1.36.2.1      yamt 					err(1, "%s: quota_delete (%s files)",
    704  1.36.2.1      yamt 					    fst[i].f_mntonname, idname);
    705  1.36.2.1      yamt 				}
    706  1.36.2.1      yamt 			}
    707      1.34  dholland 		}
    708      1.34  dholland 
    709  1.36.2.1      yamt 		quota_close(qh);
    710      1.34  dholland 	}
    711  1.36.2.1      yamt 
    712  1.36.2.1      yamt 	free(ids);
    713      1.34  dholland }
    714      1.34  dholland 
    715      1.34  dholland ////////////////////////////////////////////////////////////
    716      1.34  dholland // editor
    717      1.34  dholland 
    718      1.34  dholland /*
    719      1.34  dholland  * Take a list of privileges and get it edited.
    720      1.34  dholland  */
    721      1.34  dholland static int
    722      1.34  dholland editit(const char *ltmpfile)
    723      1.34  dholland {
    724      1.34  dholland 	pid_t pid;
    725      1.34  dholland 	int lst;
    726      1.34  dholland 	char p[MAX_TMPSTR];
    727      1.34  dholland 	const char *ed;
    728      1.34  dholland 	sigset_t s, os;
    729      1.34  dholland 
    730      1.34  dholland 	sigemptyset(&s);
    731      1.34  dholland 	sigaddset(&s, SIGINT);
    732      1.34  dholland 	sigaddset(&s, SIGQUIT);
    733      1.34  dholland 	sigaddset(&s, SIGHUP);
    734      1.34  dholland 	if (sigprocmask(SIG_BLOCK, &s, &os) == -1)
    735      1.34  dholland 		err(1, "sigprocmask");
    736      1.34  dholland top:
    737      1.34  dholland 	switch ((pid = fork())) {
    738      1.34  dholland 	case -1:
    739      1.34  dholland 		if (errno == EPROCLIM) {
    740      1.34  dholland 			warnx("You have too many processes");
    741      1.34  dholland 			return 0;
    742      1.34  dholland 		}
    743      1.34  dholland 		if (errno == EAGAIN) {
    744      1.34  dholland 			sleep(1);
    745      1.34  dholland 			goto top;
    746      1.34  dholland 		}
    747      1.34  dholland 		warn("fork");
    748      1.34  dholland 		return 0;
    749      1.34  dholland 	case 0:
    750      1.34  dholland 		if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
    751      1.34  dholland 			err(1, "sigprocmask");
    752      1.34  dholland 		setgid(getgid());
    753      1.34  dholland 		setuid(getuid());
    754      1.34  dholland 		if ((ed = getenv("EDITOR")) == (char *)0)
    755      1.34  dholland 			ed = _PATH_VI;
    756      1.34  dholland 		if (strlen(ed) + strlen(ltmpfile) + 2 >= MAX_TMPSTR) {
    757      1.34  dholland 			errx(1, "%s", "editor or filename too long");
    758      1.34  dholland 		}
    759      1.34  dholland 		snprintf(p, sizeof(p), "%s %s", ed, ltmpfile);
    760      1.34  dholland 		execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", p, NULL);
    761      1.34  dholland 		err(1, "%s", ed);
    762      1.34  dholland 	default:
    763      1.34  dholland 		if (waitpid(pid, &lst, 0) == -1)
    764      1.34  dholland 			err(1, "waitpid");
    765      1.34  dholland 		if (sigprocmask(SIG_SETMASK, &os, NULL) == -1)
    766      1.34  dholland 			err(1, "sigprocmask");
    767      1.34  dholland 		if (!WIFEXITED(lst) || WEXITSTATUS(lst) != 0)
    768      1.34  dholland 			return 0;
    769      1.34  dholland 		return 1;
    770      1.34  dholland 	}
    771      1.34  dholland }
    772      1.34  dholland 
    773      1.34  dholland /*
    774      1.34  dholland  * Convert a quotause list to an ASCII file.
    775      1.34  dholland  */
    776      1.34  dholland static int
    777  1.36.2.1      yamt writeprivs(struct quotalist *qlist, int outfd, const char *name,
    778  1.36.2.1      yamt     int idtype, const char *idtypename)
    779      1.34  dholland {
    780      1.34  dholland 	struct quotause *qup;
    781      1.34  dholland 	FILE *fd;
    782      1.34  dholland 	char b0[32], b1[32], b2[32], b3[32];
    783  1.36.2.2      yamt 	const char *comm;
    784      1.34  dholland 
    785      1.34  dholland 	(void)ftruncate(outfd, 0);
    786      1.34  dholland 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    787      1.34  dholland 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    788  1.36.2.1      yamt 		errx(1, "fdopen");
    789  1.36.2.1      yamt 	if (name == NULL) {
    790  1.36.2.1      yamt 		fprintf(fd, "Default %s quotas:\n", idtypename);
    791      1.34  dholland 	} else {
    792  1.36.2.1      yamt 		fprintf(fd, "Quotas for %s %s:\n", idtypename, name);
    793      1.30    bouyer 	}
    794  1.36.2.1      yamt 	for (qup = qlist->head; qup; qup = qup->next) {
    795  1.36.2.1      yamt 		struct quotaval *q = qup->qv;
    796  1.36.2.2      yamt 
    797  1.36.2.2      yamt 		fprintf(fd, "%s (%s):\n", qup->fsname, qup->implementation);
    798  1.36.2.2      yamt 
    799  1.36.2.2      yamt 		comm = source_is_real(qup->source[QO_BLK]) ? "" : "#";
    800  1.36.2.2      yamt 		fprintf(fd, "\tblocks:%s\n",
    801  1.36.2.2      yamt 			Hflag ? "" : " (sizes in 1K-blocks)");
    802  1.36.2.2      yamt 		fprintf(fd, "\t\t%susage: %s\n", comm,
    803  1.36.2.2      yamt 			intprt(b1, 21, q[QO_BLK].qv_usage,
    804  1.36.2.2      yamt 			       HN_NOSPACE | HN_B, Hflag));
    805  1.36.2.2      yamt 		fprintf(fd, "\t\t%slimits: soft %s, hard %s\n", comm,
    806  1.36.2.2      yamt 			intprt(b2, 21, q[QO_BLK].qv_softlimit,
    807  1.36.2.2      yamt 			       HN_NOSPACE | HN_B, Hflag),
    808  1.36.2.2      yamt 			intprt(b3, 21, q[QO_BLK].qv_hardlimit,
    809  1.36.2.2      yamt 			       HN_NOSPACE | HN_B, Hflag));
    810  1.36.2.2      yamt 		if (qup->xgrace || qup->isdefault) {
    811  1.36.2.2      yamt 			fprintf(fd, "\t\t%sgrace: %s\n", comm,
    812  1.36.2.2      yamt 				timepprt(b0, 21, q[QO_BLK].qv_grace, Hflag));
    813  1.36.2.2      yamt 		}
    814  1.36.2.2      yamt 
    815  1.36.2.2      yamt 		comm = source_is_real(qup->source[QO_FL]) ? "" : "#";
    816  1.36.2.2      yamt 		fprintf(fd, "\tinodes:\n");
    817  1.36.2.2      yamt 		fprintf(fd, "\t\t%susage: %s\n", comm,
    818  1.36.2.2      yamt 			intprt(b1, 21, q[QO_FL].qv_usage,
    819  1.36.2.2      yamt 			       HN_NOSPACE, Hflag));
    820  1.36.2.2      yamt 		fprintf(fd, "\t\t%slimits: soft %s, hard %s\n", comm,
    821  1.36.2.2      yamt 			intprt(b2, 21, q[QO_FL].qv_softlimit,
    822  1.36.2.2      yamt 			       HN_NOSPACE, Hflag),
    823  1.36.2.2      yamt 			intprt(b3, 21, q[QO_FL].qv_hardlimit,
    824  1.36.2.2      yamt 			       HN_NOSPACE, Hflag));
    825  1.36.2.2      yamt 		if (qup->xgrace || qup->isdefault) {
    826  1.36.2.2      yamt 			fprintf(fd, "\t\t%sgrace: %s\n", comm,
    827  1.36.2.2      yamt 				timepprt(b0, 21, q[QO_FL].qv_grace, Hflag));
    828      1.30    bouyer 		}
    829       1.1       cgd 	}
    830       1.1       cgd 	fclose(fd);
    831      1.31  christos 	return 1;
    832       1.1       cgd }
    833       1.1       cgd 
    834       1.1       cgd /*
    835       1.1       cgd  * Merge changes to an ASCII file into a quotause list.
    836       1.1       cgd  */
    837      1.31  christos static int
    838  1.36.2.1      yamt readprivs(struct quotalist *qlist, int infd, int dflag)
    839       1.1       cgd {
    840  1.36.2.2      yamt 	FILE *fd;			/* file */
    841  1.36.2.2      yamt 	unsigned lineno;		/* line number in file */
    842  1.36.2.2      yamt 	char line[BUFSIZ];		/* input buffer */
    843  1.36.2.2      yamt 	size_t len;			/* length of input buffer */
    844  1.36.2.2      yamt 	bool seenheader;		/* true if past the file header */
    845  1.36.2.2      yamt 	struct quotause *qup;		/* current filesystem */
    846  1.36.2.2      yamt 	bool haveobjtype;		/* true if objtype is valid */
    847  1.36.2.2      yamt 	unsigned objtype;		/* current object type */
    848  1.36.2.2      yamt 	int objtypeflags;		/* humanize flags */
    849  1.36.2.2      yamt 	/* XXX make following const later (requires non-local cleanup) */
    850  1.36.2.2      yamt 	/*const*/ char *text, *text2, *t; /* scratch values */
    851  1.36.2.2      yamt 	char b0[32], b1[32];
    852  1.36.2.2      yamt 	uint64_t soft, hard, current;
    853  1.36.2.2      yamt 	time_t grace;
    854  1.36.2.2      yamt 	struct quotaval *qv;
    855  1.36.2.2      yamt 
    856  1.36.2.2      yamt 	lineno = 0;
    857  1.36.2.2      yamt 	seenheader = false;
    858  1.36.2.2      yamt 	qup = NULL;
    859  1.36.2.2      yamt 	haveobjtype = false;
    860  1.36.2.2      yamt 	objtype = QUOTA_OBJTYPE_BLOCKS; /* for gcc 4.5 */
    861  1.36.2.2      yamt 	objtypeflags = HN_B;
    862       1.1       cgd 
    863      1.14    kleink 	(void)lseek(infd, (off_t)0, SEEK_SET);
    864       1.1       cgd 	fd = fdopen(dup(infd), "r");
    865       1.1       cgd 	if (fd == NULL) {
    866      1.15     lukem 		warn("Can't re-read temp file");
    867  1.36.2.2      yamt 		return -1;
    868       1.1       cgd 	}
    869  1.36.2.2      yamt 
    870       1.1       cgd 	/*
    871  1.36.2.2      yamt 	 * Read back the same format we wrote.
    872       1.1       cgd 	 */
    873  1.36.2.2      yamt 
    874  1.36.2.2      yamt 	while (fgets(line, sizeof(line), fd)) {
    875  1.36.2.2      yamt 		lineno++;
    876  1.36.2.2      yamt 		if (!seenheader) {
    877  1.36.2.2      yamt 			if ((!strncmp(line, "Default ", 8) && dflag) ||
    878  1.36.2.2      yamt 			    (!strncmp(line, "Quotas for ", 11) && !dflag)) {
    879  1.36.2.2      yamt 				/* ok. */
    880  1.36.2.2      yamt 				seenheader = 1;
    881  1.36.2.2      yamt 				continue;
    882      1.30    bouyer 			}
    883  1.36.2.2      yamt 			warnx("Header line missing");
    884  1.36.2.2      yamt 			goto fail;
    885  1.36.2.2      yamt 		}
    886  1.36.2.2      yamt 		len = strlen(line);
    887  1.36.2.2      yamt 		if (len == 0) {
    888  1.36.2.2      yamt 			/* ? */
    889  1.36.2.2      yamt 			continue;
    890  1.36.2.2      yamt 		}
    891  1.36.2.2      yamt 		if (line[len - 1] != '\n') {
    892  1.36.2.2      yamt 			warnx("Line %u too long", lineno);
    893  1.36.2.2      yamt 			goto fail;
    894  1.36.2.2      yamt 		}
    895  1.36.2.2      yamt 		line[--len] = '\0';
    896  1.36.2.2      yamt 		if (line[len - 1] == '\r') {
    897  1.36.2.2      yamt 			line[--len] = '\0';
    898  1.36.2.2      yamt 		}
    899  1.36.2.2      yamt 		if (len == 0) {
    900  1.36.2.2      yamt 			/* blank line */
    901  1.36.2.2      yamt 			continue;
    902  1.36.2.2      yamt 		}
    903  1.36.2.2      yamt 
    904  1.36.2.2      yamt 		/*
    905  1.36.2.2      yamt 		 * If the line has:     it is:
    906  1.36.2.2      yamt                  *      two tabs        values
    907  1.36.2.2      yamt 		 *      one tab         the next objtype
    908  1.36.2.2      yamt 		 *      no tabs         the next filesystem
    909  1.36.2.2      yamt 		 */
    910  1.36.2.2      yamt 		if (line[0] == '\t' && line[1] == '\t') {
    911  1.36.2.2      yamt 			if (qup == NULL) {
    912  1.36.2.2      yamt 				warnx("Line %u: values with no filesystem",
    913  1.36.2.2      yamt 				      lineno);
    914  1.36.2.2      yamt 				goto fail;
    915  1.36.2.2      yamt 			}
    916  1.36.2.2      yamt 			if (!haveobjtype) {
    917  1.36.2.2      yamt 				warnx("Line %u: values with no object type",
    918  1.36.2.2      yamt 				      lineno);
    919  1.36.2.2      yamt 				goto fail;
    920  1.36.2.2      yamt 			}
    921  1.36.2.2      yamt 			qv = &qup->qv[objtype];
    922  1.36.2.2      yamt 
    923  1.36.2.2      yamt 			text = line + 2;
    924  1.36.2.2      yamt 			if (*text == '#') {
    925  1.36.2.2      yamt 				/* commented out; ignore */
    926  1.36.2.2      yamt 				continue;
    927      1.30    bouyer 			}
    928  1.36.2.2      yamt 			else if (!strncmp(text, "usage:", 6)) {
    929  1.36.2.2      yamt 
    930  1.36.2.2      yamt 				/* usage: %llu */
    931  1.36.2.2      yamt 				text += 6;
    932  1.36.2.2      yamt 				t = skipws(text);
    933  1.36.2.2      yamt 				if (intrd(t, &current, objtypeflags) != 0) {
    934  1.36.2.2      yamt 					warnx("Line %u: Bad number %s",
    935  1.36.2.2      yamt 					      lineno, t);
    936  1.36.2.2      yamt 					goto fail;
    937      1.30    bouyer 				}
    938  1.36.2.2      yamt 
    939  1.36.2.2      yamt 				/*
    940  1.36.2.2      yamt 				 * Because the humanization can lead
    941  1.36.2.2      yamt 				 * to roundoff, check if the two
    942  1.36.2.2      yamt 				 * values produce the same humanized
    943  1.36.2.2      yamt 				 * string, rather than if they're the
    944  1.36.2.2      yamt 				 * same number. Sigh.
    945  1.36.2.2      yamt 				 */
    946  1.36.2.2      yamt 				intprt(b0, 21, current,
    947  1.36.2.2      yamt 				       HN_NOSPACE | objtypeflags, Hflag);
    948  1.36.2.2      yamt 				intprt(b1, 21, qv->qv_usage,
    949  1.36.2.2      yamt 				       HN_NOSPACE | objtypeflags, Hflag);
    950  1.36.2.2      yamt 				if (strcmp(b0, b1)) {
    951  1.36.2.2      yamt 					warnx("Line %u: cannot change usage",
    952  1.36.2.2      yamt 					      lineno);
    953      1.30    bouyer 				}
    954  1.36.2.2      yamt 				continue;
    955  1.36.2.2      yamt 
    956  1.36.2.2      yamt 			} else if (!strncmp(text, "limits:", 7)) {
    957  1.36.2.2      yamt 
    958  1.36.2.2      yamt 				/* limits: soft %llu, hard %llu */
    959  1.36.2.2      yamt 				text += 7;
    960  1.36.2.2      yamt 				text2 = strchr(text, ',');
    961  1.36.2.2      yamt 				if (text2 == NULL) {
    962  1.36.2.2      yamt 					warnx("Line %u: expected comma",
    963  1.36.2.2      yamt 					      lineno);
    964  1.36.2.2      yamt 					goto fail;
    965      1.30    bouyer 				}
    966  1.36.2.2      yamt 				*text2 = '\0';
    967  1.36.2.2      yamt 				text2++;
    968  1.36.2.2      yamt 
    969  1.36.2.2      yamt 				t = skipws(text);
    970  1.36.2.2      yamt 				t = skipword(t);
    971  1.36.2.2      yamt 				t = skipws(t);
    972  1.36.2.2      yamt 				if (intrd(t, &soft, objtypeflags) != 0) {
    973  1.36.2.2      yamt 					warnx("Line %u: Bad number %s",
    974  1.36.2.2      yamt 					      lineno, t);
    975  1.36.2.2      yamt 					goto fail;
    976  1.36.2.2      yamt 				}
    977  1.36.2.2      yamt 				t = skipws(text2);
    978  1.36.2.2      yamt 				t = skipword(t);
    979  1.36.2.2      yamt 				t = skipws(t);
    980  1.36.2.2      yamt 				if (intrd(t, &hard, objtypeflags) != 0) {
    981  1.36.2.2      yamt 					warnx("Line %u: Bad number %s",
    982  1.36.2.2      yamt 					      lineno, t);
    983  1.36.2.2      yamt 					goto fail;
    984  1.36.2.2      yamt 				}
    985  1.36.2.2      yamt 
    986  1.36.2.2      yamt 				/*
    987  1.36.2.2      yamt 				 * Cause time limit to be reset when the quota
    988  1.36.2.2      yamt 				 * is next used if previously had no soft limit
    989  1.36.2.2      yamt 				 * or were under it, but now have a soft limit
    990  1.36.2.2      yamt 				 * and are over it.
    991  1.36.2.2      yamt 				 */
    992  1.36.2.2      yamt 				if (qv->qv_usage && qv->qv_usage >= soft &&
    993  1.36.2.2      yamt 				    (qv->qv_softlimit == 0 ||
    994  1.36.2.2      yamt 				     qv->qv_usage < qv->qv_softlimit)) {
    995  1.36.2.2      yamt 					qv->qv_expiretime = 0;
    996  1.36.2.2      yamt 				}
    997  1.36.2.2      yamt 				if (soft != qv->qv_softlimit ||
    998  1.36.2.2      yamt 				    hard != qv->qv_hardlimit) {
    999  1.36.2.2      yamt 					qv->qv_softlimit = soft;
   1000  1.36.2.2      yamt 					qv->qv_hardlimit = hard;
   1001  1.36.2.2      yamt 					qup->source[objtype] = SRC_EDITED;
   1002  1.36.2.2      yamt 				}
   1003  1.36.2.2      yamt 				qup->found = 1;
   1004  1.36.2.2      yamt 
   1005  1.36.2.2      yamt 			} else if (!strncmp(text, "grace:", 6)) {
   1006      1.30    bouyer 
   1007  1.36.2.2      yamt 				text += 6;
   1008  1.36.2.2      yamt 				/* grace: %llu */
   1009  1.36.2.2      yamt 				t = skipws(text);
   1010  1.36.2.2      yamt 				if (timeprd(t, &grace) != 0) {
   1011  1.36.2.2      yamt 					warnx("Line %u: Bad number %s",
   1012  1.36.2.2      yamt 					      lineno, t);
   1013  1.36.2.2      yamt 					goto fail;
   1014      1.30    bouyer 				}
   1015  1.36.2.2      yamt 				if (qup->isdefault || qup->xgrace) {
   1016  1.36.2.2      yamt 					if (grace != qv->qv_grace) {
   1017  1.36.2.2      yamt 						qv->qv_grace = grace;
   1018  1.36.2.2      yamt 						qup->source[objtype] =
   1019  1.36.2.2      yamt 							SRC_EDITED;
   1020  1.36.2.2      yamt 					}
   1021  1.36.2.2      yamt 					qup->found = 1;
   1022  1.36.2.2      yamt 				} else {
   1023  1.36.2.2      yamt 					warnx("Line %u: Cannot set individual "
   1024  1.36.2.2      yamt 					      "grace time on this filesystem",
   1025  1.36.2.2      yamt 					      lineno);
   1026  1.36.2.2      yamt 					goto fail;
   1027      1.30    bouyer 				}
   1028  1.36.2.2      yamt 
   1029      1.30    bouyer 			} else {
   1030  1.36.2.2      yamt 				warnx("Line %u: Unknown/unexpected value line",
   1031  1.36.2.2      yamt 				      lineno);
   1032  1.36.2.2      yamt 				goto fail;
   1033  1.36.2.2      yamt 			}
   1034  1.36.2.2      yamt 		} else if (line[0] == '\t') {
   1035  1.36.2.2      yamt 			text = line + 1;
   1036  1.36.2.2      yamt 			if (*text == '#') {
   1037  1.36.2.2      yamt 				/* commented out; ignore */
   1038  1.36.2.2      yamt 				continue;
   1039      1.30    bouyer 			}
   1040  1.36.2.2      yamt 			else if (!strncmp(text, "blocks:", 7)) {
   1041  1.36.2.2      yamt 				objtype = QUOTA_OBJTYPE_BLOCKS;
   1042  1.36.2.2      yamt 				objtypeflags = HN_B;
   1043  1.36.2.2      yamt 				haveobjtype = true;
   1044  1.36.2.2      yamt 			} else if (!strncmp(text, "inodes:", 7)) {
   1045  1.36.2.2      yamt 				objtype = QUOTA_OBJTYPE_FILES;
   1046  1.36.2.2      yamt 				objtypeflags = 0;
   1047  1.36.2.2      yamt 				haveobjtype = true;
   1048  1.36.2.2      yamt 			} else {
   1049  1.36.2.2      yamt 				warnx("Line %u: Unknown/unexpected object "
   1050  1.36.2.2      yamt 				      "type (%s)", lineno, text);
   1051  1.36.2.2      yamt 				goto fail;
   1052      1.30    bouyer 			}
   1053  1.36.2.2      yamt 		} else {
   1054  1.36.2.2      yamt 			t = strchr(line, ' ');
   1055  1.36.2.2      yamt 			if (t == NULL) {
   1056  1.36.2.2      yamt 				t = strchr(line, ':');
   1057  1.36.2.2      yamt 				if (t == NULL) {
   1058  1.36.2.2      yamt 					t = line + len;
   1059      1.30    bouyer 				}
   1060      1.30    bouyer 			}
   1061  1.36.2.2      yamt 			*t = '\0';
   1062  1.36.2.2      yamt 
   1063  1.36.2.2      yamt 			if (*line == '#') {
   1064  1.36.2.2      yamt 				/* commented out; ignore */
   1065      1.30    bouyer 				continue;
   1066      1.30    bouyer 			}
   1067      1.30    bouyer 
   1068  1.36.2.2      yamt 			for (qup = qlist->head; qup; qup = qup->next) {
   1069  1.36.2.2      yamt 				if (!strcmp(line, qup->fsname))
   1070  1.36.2.2      yamt 					break;
   1071  1.36.2.2      yamt 			}
   1072  1.36.2.2      yamt 			if (qup == NULL) {
   1073  1.36.2.2      yamt 				warnx("Line %u: Filesystem %s invalid or has "
   1074  1.36.2.2      yamt 				      "no quota support", lineno, line);
   1075  1.36.2.2      yamt 				goto fail;
   1076      1.30    bouyer 			}
   1077  1.36.2.2      yamt 			haveobjtype = false;
   1078       1.1       cgd 		}
   1079       1.1       cgd 	}
   1080  1.36.2.2      yamt 
   1081       1.1       cgd 	fclose(fd);
   1082  1.36.2.2      yamt 
   1083       1.1       cgd 	/*
   1084  1.36.2.2      yamt 	 * Disable quotas for any filesystems that we didn't see,
   1085  1.36.2.2      yamt 	 * because they must have been deleted in the editor.
   1086  1.36.2.2      yamt 	 *
   1087  1.36.2.2      yamt 	 * XXX this should be improved so it results in
   1088  1.36.2.2      yamt 	 * quota_delete(), not just writing out a blank quotaval.
   1089       1.1       cgd 	 */
   1090  1.36.2.1      yamt 	for (qup = qlist->head; qup; qup = qup->next) {
   1091  1.36.2.2      yamt 		if (qup->found) {
   1092  1.36.2.2      yamt 			qup->found = 0;
   1093       1.1       cgd 			continue;
   1094       1.1       cgd 		}
   1095  1.36.2.2      yamt 
   1096  1.36.2.2      yamt 		if (source_is_real(qup->source[QUOTA_OBJTYPE_BLOCKS])) {
   1097  1.36.2.2      yamt 			quotaval_clear(&qup->qv[QUOTA_OBJTYPE_BLOCKS]);
   1098  1.36.2.2      yamt 			qup->source[QUOTA_OBJTYPE_BLOCKS] = SRC_EDITED;
   1099  1.36.2.2      yamt 		}
   1100  1.36.2.2      yamt 
   1101  1.36.2.2      yamt 		if (source_is_real(qup->source[QUOTA_OBJTYPE_FILES])) {
   1102  1.36.2.2      yamt 			quotaval_clear(&qup->qv[QUOTA_OBJTYPE_FILES]);
   1103  1.36.2.2      yamt 			qup->source[QUOTA_OBJTYPE_FILES] = SRC_EDITED;
   1104  1.36.2.2      yamt 		}
   1105       1.1       cgd 	}
   1106  1.36.2.2      yamt 	return 0;
   1107  1.36.2.2      yamt 
   1108  1.36.2.2      yamt fail:
   1109  1.36.2.2      yamt 	sleep(3);
   1110  1.36.2.2      yamt 	fclose(fd);
   1111  1.36.2.2      yamt 	return -1;
   1112       1.1       cgd }
   1113       1.1       cgd 
   1114      1.34  dholland ////////////////////////////////////////////////////////////
   1115  1.36.2.1      yamt // actions
   1116  1.36.2.1      yamt 
   1117  1.36.2.1      yamt static void
   1118  1.36.2.1      yamt replicate(const char *fs, int idtype, const char *protoname,
   1119  1.36.2.1      yamt 	  char **names, int numnames)
   1120  1.36.2.1      yamt {
   1121  1.36.2.1      yamt 	long protoid, id;
   1122  1.36.2.1      yamt 	struct quotalist *protoprivs;
   1123  1.36.2.1      yamt 	struct quotause *qup;
   1124  1.36.2.1      yamt 	int i;
   1125  1.36.2.1      yamt 
   1126  1.36.2.1      yamt 	if ((protoid = getidbyname(protoname, idtype)) == -1)
   1127  1.36.2.1      yamt 		exit(1);
   1128  1.36.2.1      yamt 	protoprivs = getprivs(protoid, 0, idtype, fs);
   1129  1.36.2.1      yamt 	for (qup = protoprivs->head; qup; qup = qup->next) {
   1130  1.36.2.1      yamt 		qup->qv[QO_BLK].qv_expiretime = 0;
   1131  1.36.2.1      yamt 		qup->qv[QO_FL].qv_expiretime = 0;
   1132  1.36.2.2      yamt 		qup->source[QO_BLK] = SRC_EDITED;
   1133  1.36.2.2      yamt 		qup->source[QO_FL] = SRC_EDITED;
   1134  1.36.2.1      yamt 	}
   1135  1.36.2.1      yamt 	for (i=0; i<numnames; i++) {
   1136  1.36.2.1      yamt 		id = getidbyname(names[i], idtype);
   1137  1.36.2.1      yamt 		if (id == -1)
   1138  1.36.2.1      yamt 			continue;
   1139  1.36.2.1      yamt 		putprivs(id, idtype, protoprivs);
   1140  1.36.2.1      yamt 	}
   1141  1.36.2.1      yamt 	/* XXX */
   1142  1.36.2.1      yamt 	/* quotalist_destroy(protoprivs); */
   1143  1.36.2.1      yamt }
   1144  1.36.2.1      yamt 
   1145  1.36.2.1      yamt static void
   1146  1.36.2.1      yamt assign(const char *fs, int idtype,
   1147  1.36.2.1      yamt        char *soft, char *hard, char *grace,
   1148  1.36.2.1      yamt        char **names, int numnames)
   1149  1.36.2.1      yamt {
   1150  1.36.2.1      yamt 	struct quotalist *curprivs;
   1151  1.36.2.1      yamt 	struct quotause *lqup;
   1152  1.36.2.1      yamt 	u_int64_t softb, hardb, softi, hardi;
   1153  1.36.2.1      yamt 	time_t  graceb, gracei;
   1154  1.36.2.1      yamt 	char *str;
   1155  1.36.2.1      yamt 	long id;
   1156  1.36.2.1      yamt 	int dflag;
   1157  1.36.2.1      yamt 	int i;
   1158  1.36.2.1      yamt 
   1159  1.36.2.1      yamt 	if (soft) {
   1160  1.36.2.1      yamt 		str = strsep(&soft, "/");
   1161  1.36.2.1      yamt 		if (str[0] == '\0' || soft == NULL || soft[0] == '\0')
   1162  1.36.2.1      yamt 			usage();
   1163  1.36.2.1      yamt 
   1164  1.36.2.1      yamt 		if (intrd(str, &softb, HN_B) != 0)
   1165  1.36.2.1      yamt 			errx(1, "%s: bad number", str);
   1166  1.36.2.1      yamt 		if (intrd(soft, &softi, 0) != 0)
   1167  1.36.2.1      yamt 			errx(1, "%s: bad number", soft);
   1168  1.36.2.1      yamt 	}
   1169  1.36.2.1      yamt 	if (hard) {
   1170  1.36.2.1      yamt 		str = strsep(&hard, "/");
   1171  1.36.2.1      yamt 		if (str[0] == '\0' || hard == NULL || hard[0] == '\0')
   1172  1.36.2.1      yamt 			usage();
   1173  1.36.2.1      yamt 
   1174  1.36.2.1      yamt 		if (intrd(str, &hardb, HN_B) != 0)
   1175  1.36.2.1      yamt 			errx(1, "%s: bad number", str);
   1176  1.36.2.1      yamt 		if (intrd(hard, &hardi, 0) != 0)
   1177  1.36.2.1      yamt 			errx(1, "%s: bad number", hard);
   1178  1.36.2.1      yamt 	}
   1179  1.36.2.1      yamt 	if (grace) {
   1180  1.36.2.1      yamt 		str = strsep(&grace, "/");
   1181  1.36.2.1      yamt 		if (str[0] == '\0' || grace == NULL || grace[0] == '\0')
   1182  1.36.2.1      yamt 			usage();
   1183  1.36.2.1      yamt 
   1184  1.36.2.1      yamt 		if (timeprd(str, &graceb) != 0)
   1185  1.36.2.1      yamt 			errx(1, "%s: bad number", str);
   1186  1.36.2.1      yamt 		if (timeprd(grace, &gracei) != 0)
   1187  1.36.2.1      yamt 			errx(1, "%s: bad number", grace);
   1188  1.36.2.1      yamt 	}
   1189  1.36.2.1      yamt 	for (i=0; i<numnames; i++) {
   1190  1.36.2.1      yamt 		if (names[i] == NULL) {
   1191  1.36.2.1      yamt 			id = 0;
   1192  1.36.2.1      yamt 			dflag = 1;
   1193  1.36.2.1      yamt 		} else {
   1194  1.36.2.1      yamt 			id = getidbyname(names[i], idtype);
   1195  1.36.2.1      yamt 			if (id == -1)
   1196  1.36.2.1      yamt 				continue;
   1197  1.36.2.1      yamt 			dflag = 0;
   1198  1.36.2.1      yamt 		}
   1199  1.36.2.1      yamt 
   1200  1.36.2.1      yamt 		curprivs = getprivs(id, dflag, idtype, fs);
   1201  1.36.2.1      yamt 		for (lqup = curprivs->head; lqup; lqup = lqup->next) {
   1202  1.36.2.1      yamt 			struct quotaval *q = lqup->qv;
   1203  1.36.2.1      yamt 			if (soft) {
   1204  1.36.2.1      yamt 				if (!dflag && softb &&
   1205  1.36.2.1      yamt 				    q[QO_BLK].qv_usage >= softb &&
   1206  1.36.2.1      yamt 				    (q[QO_BLK].qv_softlimit == 0 ||
   1207  1.36.2.1      yamt 				     q[QO_BLK].qv_usage <
   1208  1.36.2.1      yamt 				     q[QO_BLK].qv_softlimit))
   1209  1.36.2.1      yamt 					q[QO_BLK].qv_expiretime = 0;
   1210  1.36.2.1      yamt 				if (!dflag && softi &&
   1211  1.36.2.1      yamt 				    q[QO_FL].qv_usage >= softb &&
   1212  1.36.2.1      yamt 				    (q[QO_FL].qv_softlimit == 0 ||
   1213  1.36.2.1      yamt 				     q[QO_FL].qv_usage <
   1214  1.36.2.1      yamt 				     q[QO_FL].qv_softlimit))
   1215  1.36.2.1      yamt 					q[QO_FL].qv_expiretime = 0;
   1216  1.36.2.1      yamt 				q[QO_BLK].qv_softlimit = softb;
   1217  1.36.2.1      yamt 				q[QO_FL].qv_softlimit = softi;
   1218  1.36.2.1      yamt 			}
   1219  1.36.2.1      yamt 			if (hard) {
   1220  1.36.2.1      yamt 				q[QO_BLK].qv_hardlimit = hardb;
   1221  1.36.2.1      yamt 				q[QO_FL].qv_hardlimit = hardi;
   1222  1.36.2.1      yamt 			}
   1223  1.36.2.1      yamt 			if (grace) {
   1224  1.36.2.1      yamt 				q[QO_BLK].qv_grace = graceb;
   1225  1.36.2.1      yamt 				q[QO_FL].qv_grace = gracei;
   1226  1.36.2.1      yamt 			}
   1227  1.36.2.2      yamt 			lqup->source[QO_BLK] = SRC_EDITED;
   1228  1.36.2.2      yamt 			lqup->source[QO_FL] = SRC_EDITED;
   1229  1.36.2.1      yamt 		}
   1230  1.36.2.1      yamt 		putprivs(id, idtype, curprivs);
   1231  1.36.2.1      yamt 		quotalist_destroy(curprivs);
   1232  1.36.2.1      yamt 	}
   1233  1.36.2.1      yamt }
   1234  1.36.2.1      yamt 
   1235  1.36.2.1      yamt static void
   1236  1.36.2.1      yamt clear(const char *fs, int idtype, char **names, int numnames)
   1237  1.36.2.1      yamt {
   1238  1.36.2.1      yamt 	clearpriv(numnames, names, fs, idtype);
   1239  1.36.2.1      yamt }
   1240  1.36.2.1      yamt 
   1241  1.36.2.1      yamt static void
   1242  1.36.2.1      yamt editone(const char *fs, int idtype, const char *name,
   1243  1.36.2.1      yamt 	int tmpfd, const char *tmppath)
   1244  1.36.2.1      yamt {
   1245  1.36.2.1      yamt 	struct quotalist *curprivs;
   1246  1.36.2.1      yamt 	long id;
   1247  1.36.2.1      yamt 	int dflag;
   1248  1.36.2.1      yamt 
   1249  1.36.2.1      yamt 	if (name == NULL) {
   1250  1.36.2.1      yamt 		id = 0;
   1251  1.36.2.1      yamt 		dflag = 1;
   1252  1.36.2.1      yamt 	} else {
   1253  1.36.2.1      yamt 		id = getidbyname(name, idtype);
   1254  1.36.2.1      yamt 		if (id == -1)
   1255  1.36.2.1      yamt 			return;
   1256  1.36.2.1      yamt 		dflag = 0;
   1257  1.36.2.1      yamt 	}
   1258  1.36.2.1      yamt 	curprivs = getprivs(id, dflag, idtype, fs);
   1259  1.36.2.1      yamt 
   1260  1.36.2.1      yamt 	if (writeprivs(curprivs, tmpfd, name, idtype,
   1261  1.36.2.1      yamt 		       curprivs->idtypename) == 0)
   1262  1.36.2.1      yamt 		goto fail;
   1263  1.36.2.1      yamt 
   1264  1.36.2.1      yamt 	if (editit(tmppath) == 0)
   1265  1.36.2.1      yamt 		goto fail;
   1266  1.36.2.1      yamt 
   1267  1.36.2.2      yamt 	if (readprivs(curprivs, tmpfd, dflag) < 0)
   1268  1.36.2.1      yamt 		goto fail;
   1269  1.36.2.1      yamt 
   1270  1.36.2.1      yamt 	putprivs(id, idtype, curprivs);
   1271  1.36.2.1      yamt fail:
   1272  1.36.2.1      yamt 	quotalist_destroy(curprivs);
   1273  1.36.2.1      yamt }
   1274  1.36.2.1      yamt 
   1275  1.36.2.1      yamt static void
   1276  1.36.2.1      yamt edit(const char *fs, int idtype, char **names, int numnames)
   1277  1.36.2.1      yamt {
   1278  1.36.2.1      yamt 	char tmppath[] = _PATH_TMPFILE;
   1279  1.36.2.1      yamt 	int tmpfd, i;
   1280  1.36.2.1      yamt 
   1281  1.36.2.1      yamt 	tmpfd = mkstemp(tmppath);
   1282  1.36.2.1      yamt 	fchown(tmpfd, getuid(), getgid());
   1283  1.36.2.1      yamt 
   1284  1.36.2.1      yamt 	for (i=0; i<numnames; i++) {
   1285  1.36.2.1      yamt 		editone(fs, idtype, names[i], tmpfd, tmppath);
   1286  1.36.2.1      yamt 	}
   1287  1.36.2.1      yamt 
   1288  1.36.2.1      yamt 	close(tmpfd);
   1289  1.36.2.1      yamt 	unlink(tmppath);
   1290  1.36.2.1      yamt }
   1291  1.36.2.1      yamt 
   1292  1.36.2.1      yamt ////////////////////////////////////////////////////////////
   1293      1.34  dholland // main
   1294       1.1       cgd 
   1295      1.31  christos static void
   1296      1.34  dholland usage(void)
   1297       1.1       cgd {
   1298      1.34  dholland 	const char *p = getprogname();
   1299      1.34  dholland 	fprintf(stderr,
   1300      1.34  dholland 	    "Usage: %s [-D] [-H] [-u] [-p <username>] [-f <filesystem>] "
   1301      1.34  dholland 		"-d | <username> ...\n"
   1302      1.34  dholland 	    "\t%s [-D] [-H] -g [-p <groupname>] [-f <filesystem>] "
   1303      1.34  dholland 		"-d | <groupname> ...\n"
   1304      1.34  dholland 	    "\t%s [-D] [-u] [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
   1305      1.34  dholland 		"-d | <username> ...\n"
   1306      1.34  dholland 	    "\t%s [-D] -g [-f <filesystem>] [-s b#/i#] [-h b#/i#] [-t t#/t#] "
   1307      1.34  dholland 		"-d | <groupname> ...\n"
   1308      1.34  dholland 	    "\t%s [-D] [-H] [-u] -c [-f <filesystem>] username ...\n"
   1309      1.34  dholland 	    "\t%s [-D] [-H] -g -c [-f <filesystem>] groupname ...\n",
   1310      1.34  dholland 	    p, p, p, p, p, p);
   1311      1.34  dholland 	exit(1);
   1312       1.1       cgd }
   1313       1.1       cgd 
   1314      1.34  dholland int
   1315      1.34  dholland main(int argc, char *argv[])
   1316      1.30    bouyer {
   1317  1.36.2.1      yamt 	int idtype;
   1318      1.34  dholland 	char *protoname;
   1319      1.34  dholland 	char *soft = NULL, *hard = NULL, *grace = NULL;
   1320      1.34  dholland 	char *fs = NULL;
   1321      1.34  dholland 	int ch;
   1322      1.34  dholland 	int pflag = 0;
   1323      1.34  dholland 	int cflag = 0;
   1324  1.36.2.1      yamt 	int dflag = 0;
   1325      1.30    bouyer 
   1326      1.34  dholland 	if (argc < 2)
   1327      1.34  dholland 		usage();
   1328      1.34  dholland 	if (getuid())
   1329      1.34  dholland 		errx(1, "permission denied");
   1330      1.34  dholland 	protoname = NULL;
   1331  1.36.2.1      yamt 	idtype = QUOTA_IDTYPE_USER;
   1332  1.36.2.1      yamt 	while ((ch = getopt(argc, argv, "Hcdugp:s:h:t:f:")) != -1) {
   1333      1.34  dholland 		switch(ch) {
   1334      1.34  dholland 		case 'H':
   1335      1.34  dholland 			Hflag++;
   1336      1.34  dholland 			break;
   1337      1.34  dholland 		case 'c':
   1338      1.34  dholland 			cflag++;
   1339      1.34  dholland 			break;
   1340      1.34  dholland 		case 'd':
   1341      1.34  dholland 			dflag++;
   1342      1.34  dholland 			break;
   1343      1.34  dholland 		case 'p':
   1344      1.34  dholland 			protoname = optarg;
   1345      1.34  dholland 			pflag++;
   1346      1.34  dholland 			break;
   1347      1.34  dholland 		case 'g':
   1348  1.36.2.1      yamt 			idtype = QUOTA_IDTYPE_GROUP;
   1349      1.34  dholland 			break;
   1350      1.34  dholland 		case 'u':
   1351  1.36.2.1      yamt 			idtype = QUOTA_IDTYPE_USER;
   1352      1.34  dholland 			break;
   1353      1.34  dholland 		case 's':
   1354      1.34  dholland 			soft = optarg;
   1355      1.34  dholland 			break;
   1356      1.34  dholland 		case 'h':
   1357      1.34  dholland 			hard = optarg;
   1358      1.34  dholland 			break;
   1359      1.34  dholland 		case 't':
   1360      1.34  dholland 			grace = optarg;
   1361      1.34  dholland 			break;
   1362      1.34  dholland 		case 'f':
   1363      1.34  dholland 			fs = optarg;
   1364      1.34  dholland 			break;
   1365      1.34  dholland 		default:
   1366      1.34  dholland 			usage();
   1367      1.34  dholland 		}
   1368      1.30    bouyer 	}
   1369      1.34  dholland 	argc -= optind;
   1370      1.34  dholland 	argv += optind;
   1371      1.30    bouyer 
   1372      1.34  dholland 	if (pflag) {
   1373      1.34  dholland 		if (soft || hard || grace || dflag || cflag)
   1374      1.34  dholland 			usage();
   1375  1.36.2.1      yamt 		replicate(fs, idtype, protoname, argv, argc);
   1376  1.36.2.1      yamt 	} else if (soft || hard || grace) {
   1377      1.34  dholland 		if (cflag)
   1378      1.34  dholland 			usage();
   1379      1.34  dholland 		if (dflag) {
   1380  1.36.2.1      yamt 			/* use argv[argc], which is null, to mean 'default' */
   1381  1.36.2.1      yamt 			argc++;
   1382      1.30    bouyer 		}
   1383  1.36.2.1      yamt 		assign(fs, idtype, soft, hard, grace, argv, argc);
   1384  1.36.2.1      yamt 	} else if (cflag) {
   1385      1.34  dholland 		if (dflag)
   1386      1.34  dholland 			usage();
   1387  1.36.2.1      yamt 		clear(fs, idtype, argv, argc);
   1388  1.36.2.1      yamt 	} else {
   1389  1.36.2.1      yamt 		if (dflag) {
   1390  1.36.2.1      yamt 			/* use argv[argc], which is null, to mean 'default' */
   1391  1.36.2.1      yamt 			argc++;
   1392  1.36.2.1      yamt 		}
   1393  1.36.2.1      yamt 		edit(fs, idtype, argv, argc);
   1394      1.30    bouyer 	}
   1395      1.34  dholland 	return 0;
   1396      1.30    bouyer }
   1397