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