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