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