Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.21.6.1
      1       1.1      cgd /*
      2       1.5  mycroft  * Copyright (c) 1980, 1990, 1993
      3       1.5  mycroft  *	The Regents of the University of California.  All rights reserved.
      4       1.1      cgd  *
      5       1.1      cgd  * This code is derived from software contributed to Berkeley by
      6       1.1      cgd  * Robert Elz at The University of Melbourne.
      7       1.1      cgd  *
      8       1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9       1.1      cgd  * modification, are permitted provided that the following conditions
     10       1.1      cgd  * are met:
     11       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17       1.1      cgd  *    must display the following acknowledgement:
     18       1.1      cgd  *	This product includes software developed by the University of
     19       1.1      cgd  *	California, Berkeley and its contributors.
     20       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21       1.1      cgd  *    may be used to endorse or promote products derived from this software
     22       1.1      cgd  *    without specific prior written permission.
     23       1.1      cgd  *
     24       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.1      cgd  * SUCH DAMAGE.
     35       1.1      cgd  */
     36       1.1      cgd 
     37      1.15    lukem #include <sys/cdefs.h>
     38       1.1      cgd #ifndef lint
     39      1.15    lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
     40      1.15    lukem 	The Regents of the University of California.  All rights reserved.\n");
     41       1.1      cgd #endif /* not lint */
     42       1.1      cgd 
     43       1.1      cgd #ifndef lint
     44      1.15    lukem #if 0
     45      1.15    lukem static char sccsid[] = "from: @(#)edquota.c	8.3 (Berkeley) 4/27/95";
     46      1.15    lukem #else
     47  1.21.6.1      jmc __RCSID("$NetBSD: edquota.c,v 1.21.6.1 2003/01/26 09:02:18 jmc Exp $");
     48      1.15    lukem #endif
     49       1.1      cgd #endif /* not lint */
     50       1.1      cgd 
     51       1.1      cgd /*
     52       1.1      cgd  * Disk quota editor.
     53       1.1      cgd  */
     54       1.1      cgd #include <sys/param.h>
     55       1.1      cgd #include <sys/stat.h>
     56       1.1      cgd #include <sys/file.h>
     57       1.1      cgd #include <sys/wait.h>
     58      1.12    mikel #include <sys/queue.h>
     59       1.4      cgd #include <ufs/ufs/quota.h>
     60      1.15    lukem #include <err.h>
     61       1.1      cgd #include <errno.h>
     62       1.1      cgd #include <fstab.h>
     63       1.1      cgd #include <pwd.h>
     64       1.1      cgd #include <grp.h>
     65       1.1      cgd #include <ctype.h>
     66      1.15    lukem #include <signal.h>
     67       1.1      cgd #include <stdio.h>
     68       1.9      cgd #include <stdlib.h>
     69       1.1      cgd #include <string.h>
     70       1.5  mycroft #include <unistd.h>
     71       1.1      cgd #include "pathnames.h"
     72       1.1      cgd 
     73       1.1      cgd char *qfname = QUOTAFILENAME;
     74       1.1      cgd char *qfextension[] = INITQFNAMES;
     75       1.1      cgd char *quotagroup = QUOTAGROUP;
     76       1.1      cgd char tmpfil[] = _PATH_TMP;
     77       1.1      cgd 
     78       1.1      cgd struct quotause {
     79       1.1      cgd 	struct	quotause *next;
     80       1.1      cgd 	long	flags;
     81       1.1      cgd 	struct	dqblk dqblk;
     82       1.1      cgd 	char	fsname[MAXPATHLEN + 1];
     83       1.1      cgd 	char	qfname[1];	/* actually longer */
     84      1.15    lukem };
     85       1.1      cgd #define	FOUND	0x01
     86      1.13    mikel 
     87      1.15    lukem int	main __P((int, char **));
     88      1.13    mikel void	usage __P((void));
     89      1.13    mikel int	getentry __P((char *, int));
     90      1.13    mikel struct quotause *
     91  1.21.6.1      jmc 	getprivs __P((long, int, char *));
     92      1.13    mikel void	putprivs __P((long, int, struct quotause *));
     93      1.13    mikel int	editit __P((char *));
     94      1.13    mikel int	writeprivs __P((struct quotause *, int, char *, int));
     95      1.13    mikel int	readprivs __P((struct quotause *, int));
     96      1.13    mikel int	writetimes __P((struct quotause *, int, int));
     97      1.13    mikel int	readtimes __P((struct quotause *, int));
     98      1.13    mikel char *	cvtstoa __P((time_t));
     99      1.13    mikel int	cvtatos __P((time_t, char *, time_t *));
    100      1.13    mikel void	freeprivs __P((struct quotause *));
    101      1.13    mikel int	alldigits __P((char *));
    102      1.13    mikel int	hasquota __P((struct fstab *, int, char **));
    103       1.1      cgd 
    104      1.12    mikel int
    105       1.1      cgd main(argc, argv)
    106       1.1      cgd 	int argc;
    107      1.15    lukem 	char **argv;
    108       1.1      cgd {
    109      1.15    lukem 	struct quotause *qup, *protoprivs, *curprivs;
    110      1.15    lukem 	long id, protoid;
    111      1.15    lukem 	int quotatype, tmpfd;
    112      1.11     mark 	char *protoname;
    113  1.21.6.1      jmc 	char *soft = NULL, *hard = NULL;
    114  1.21.6.1      jmc 	char *fs = NULL;
    115      1.11     mark 	int ch;
    116       1.1      cgd 	int tflag = 0, pflag = 0;
    117       1.1      cgd 
    118       1.1      cgd 	if (argc < 2)
    119       1.1      cgd 		usage();
    120      1.15    lukem 	if (getuid())
    121      1.15    lukem 		errx(1, "permission denied");
    122      1.15    lukem 	protoname = NULL;
    123       1.1      cgd 	quotatype = USRQUOTA;
    124  1.21.6.1      jmc 	while ((ch = getopt(argc, argv, "ugtp:s:h:f:")) != -1) {
    125       1.1      cgd 		switch(ch) {
    126       1.1      cgd 		case 'p':
    127       1.1      cgd 			protoname = optarg;
    128       1.1      cgd 			pflag++;
    129       1.1      cgd 			break;
    130       1.1      cgd 		case 'g':
    131       1.1      cgd 			quotatype = GRPQUOTA;
    132       1.1      cgd 			break;
    133       1.1      cgd 		case 'u':
    134       1.1      cgd 			quotatype = USRQUOTA;
    135       1.1      cgd 			break;
    136       1.1      cgd 		case 't':
    137       1.1      cgd 			tflag++;
    138       1.1      cgd 			break;
    139  1.21.6.1      jmc 		case 's':
    140  1.21.6.1      jmc 			soft = optarg;
    141  1.21.6.1      jmc 			break;
    142  1.21.6.1      jmc 		case 'h':
    143  1.21.6.1      jmc 			hard = optarg;
    144  1.21.6.1      jmc 			break;
    145  1.21.6.1      jmc 		case 'f':
    146  1.21.6.1      jmc 			fs = optarg;
    147  1.21.6.1      jmc 			break;
    148       1.1      cgd 		default:
    149       1.1      cgd 			usage();
    150       1.1      cgd 		}
    151       1.1      cgd 	}
    152       1.1      cgd 	argc -= optind;
    153       1.1      cgd 	argv += optind;
    154       1.1      cgd 	if (pflag) {
    155  1.21.6.1      jmc 		if (soft || hard)
    156  1.21.6.1      jmc 			usage();
    157       1.1      cgd 		if ((protoid = getentry(protoname, quotatype)) == -1)
    158       1.1      cgd 			exit(1);
    159  1.21.6.1      jmc 		protoprivs = getprivs(protoid, quotatype, fs);
    160       1.1      cgd 		for (qup = protoprivs; qup; qup = qup->next) {
    161       1.1      cgd 			qup->dqblk.dqb_btime = 0;
    162       1.1      cgd 			qup->dqblk.dqb_itime = 0;
    163       1.1      cgd 		}
    164       1.1      cgd 		while (argc-- > 0) {
    165       1.1      cgd 			if ((id = getentry(*argv++, quotatype)) < 0)
    166       1.1      cgd 				continue;
    167       1.1      cgd 			putprivs(id, quotatype, protoprivs);
    168       1.1      cgd 		}
    169       1.1      cgd 		exit(0);
    170       1.1      cgd 	}
    171  1.21.6.1      jmc 	if (soft || hard) {
    172  1.21.6.1      jmc 		struct quotause *qup;
    173  1.21.6.1      jmc 		u_int32_t softb, hardb, softi, hardi;
    174  1.21.6.1      jmc 		if (tflag)
    175  1.21.6.1      jmc 			usage();
    176  1.21.6.1      jmc 		if (soft) {
    177  1.21.6.1      jmc 			if (sscanf(soft, "%d/%d", &softb, &softi) != 2)
    178  1.21.6.1      jmc 				usage();
    179  1.21.6.1      jmc 			softb = btodb((u_quad_t)softb * 1024);
    180  1.21.6.1      jmc 		}
    181  1.21.6.1      jmc 		if (hard) {
    182  1.21.6.1      jmc 			if (sscanf(hard, "%d/%d", &hardb, &hardi) != 2)
    183  1.21.6.1      jmc 				usage();
    184  1.21.6.1      jmc 			hardb = btodb((u_quad_t)hardb * 1024);
    185  1.21.6.1      jmc 		}
    186  1.21.6.1      jmc 		for ( ; argc > 0; argc--, argv++) {
    187  1.21.6.1      jmc 			if ((id = getentry(*argv, quotatype)) == -1)
    188  1.21.6.1      jmc 				continue;
    189  1.21.6.1      jmc 			curprivs = getprivs(id, quotatype, fs);
    190  1.21.6.1      jmc 			for (qup = curprivs; qup; qup = qup->next) {
    191  1.21.6.1      jmc 				if (soft) {
    192  1.21.6.1      jmc 					if (softb &&
    193  1.21.6.1      jmc 					    qup->dqblk.dqb_curblocks >= softb &&
    194  1.21.6.1      jmc 					    (qup->dqblk.dqb_bsoftlimit == 0 ||
    195  1.21.6.1      jmc 					    qup->dqblk.dqb_curblocks <
    196  1.21.6.1      jmc 					    qup->dqblk.dqb_bsoftlimit))
    197  1.21.6.1      jmc 						qup->dqblk.dqb_btime = 0;
    198  1.21.6.1      jmc 					if (softi &&
    199  1.21.6.1      jmc 					    qup->dqblk.dqb_curinodes >= softi &&
    200  1.21.6.1      jmc 					    (qup->dqblk.dqb_isoftlimit == 0 ||
    201  1.21.6.1      jmc 					    qup->dqblk.dqb_curinodes <
    202  1.21.6.1      jmc 					    qup->dqblk.dqb_isoftlimit))
    203  1.21.6.1      jmc 						qup->dqblk.dqb_itime = 0;
    204  1.21.6.1      jmc 					qup->dqblk.dqb_bsoftlimit = softb;
    205  1.21.6.1      jmc 					qup->dqblk.dqb_isoftlimit = softi;
    206  1.21.6.1      jmc 				}
    207  1.21.6.1      jmc 				if (hard) {
    208  1.21.6.1      jmc 					qup->dqblk.dqb_bhardlimit = hardb;
    209  1.21.6.1      jmc 					qup->dqblk.dqb_ihardlimit = hardi;
    210  1.21.6.1      jmc 				}
    211  1.21.6.1      jmc 			}
    212  1.21.6.1      jmc 			putprivs(id, quotatype, curprivs);
    213  1.21.6.1      jmc 			freeprivs(curprivs);
    214  1.21.6.1      jmc 		}
    215  1.21.6.1      jmc 		exit(0);
    216  1.21.6.1      jmc 	}
    217       1.1      cgd 	tmpfd = mkstemp(tmpfil);
    218       1.1      cgd 	fchown(tmpfd, getuid(), getgid());
    219       1.1      cgd 	if (tflag) {
    220  1.21.6.1      jmc 		if (soft || hard)
    221  1.21.6.1      jmc 			usage();
    222  1.21.6.1      jmc 		protoprivs = getprivs(0, quotatype, fs);
    223       1.1      cgd 		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
    224       1.1      cgd 			exit(1);
    225       1.1      cgd 		if (editit(tmpfil) && readtimes(protoprivs, tmpfd))
    226       1.1      cgd 			putprivs(0, quotatype, protoprivs);
    227       1.1      cgd 		freeprivs(protoprivs);
    228       1.1      cgd 		exit(0);
    229       1.1      cgd 	}
    230       1.1      cgd 	for ( ; argc > 0; argc--, argv++) {
    231       1.1      cgd 		if ((id = getentry(*argv, quotatype)) == -1)
    232       1.1      cgd 			continue;
    233  1.21.6.1      jmc 		curprivs = getprivs(id, quotatype, fs);
    234       1.1      cgd 		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
    235       1.1      cgd 			continue;
    236       1.1      cgd 		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
    237       1.1      cgd 			putprivs(id, quotatype, curprivs);
    238       1.1      cgd 		freeprivs(curprivs);
    239       1.1      cgd 	}
    240       1.1      cgd 	close(tmpfd);
    241       1.1      cgd 	unlink(tmpfil);
    242       1.1      cgd 	exit(0);
    243       1.1      cgd }
    244       1.1      cgd 
    245      1.12    mikel void
    246       1.1      cgd usage()
    247       1.1      cgd {
    248  1.21.6.1      jmc 	fprintf(stderr,
    249  1.21.6.1      jmc 	    "Usage: edquota [-u] [-p username] [-f filesystem] username ...\n"
    250  1.21.6.1      jmc 	    "\tedquota -g [-p groupname] [-f filesystem] groupname ...\n"
    251  1.21.6.1      jmc 	    "\tedquota [-u] [-f filesystem] [-s b#/i#] [-h b#/i#] username ...\n"
    252  1.21.6.1      jmc 	    "\tedquota -g [-f filesystem] [-s b#/i#] [-h b#/i#] groupname ...\n"
    253  1.21.6.1      jmc 	    "\tedquota [-u] [-f filesystem] -t\n"
    254  1.21.6.1      jmc 	    "\tedquota -g [-f filesystem] -t\n"
    255  1.21.6.1      jmc 	    );
    256       1.1      cgd 	exit(1);
    257       1.1      cgd }
    258       1.1      cgd 
    259       1.1      cgd /*
    260       1.1      cgd  * This routine converts a name for a particular quota type to
    261       1.1      cgd  * an identifier. This routine must agree with the kernel routine
    262       1.1      cgd  * getinoquota as to the interpretation of quota types.
    263       1.1      cgd  */
    264      1.12    mikel int
    265       1.1      cgd getentry(name, quotatype)
    266       1.1      cgd 	char *name;
    267       1.1      cgd 	int quotatype;
    268       1.1      cgd {
    269       1.1      cgd 	struct passwd *pw;
    270       1.1      cgd 	struct group *gr;
    271       1.1      cgd 
    272       1.1      cgd 	if (alldigits(name))
    273       1.1      cgd 		return (atoi(name));
    274       1.1      cgd 	switch(quotatype) {
    275       1.1      cgd 	case USRQUOTA:
    276      1.15    lukem 		if ((pw = getpwnam(name)) != NULL)
    277       1.1      cgd 			return (pw->pw_uid);
    278      1.15    lukem 		warnx("%s: no such user", name);
    279       1.1      cgd 		break;
    280       1.1      cgd 	case GRPQUOTA:
    281      1.15    lukem 		if ((gr = getgrnam(name)) != NULL)
    282       1.1      cgd 			return (gr->gr_gid);
    283      1.15    lukem 		warnx("%s: no such group", name);
    284       1.1      cgd 		break;
    285       1.1      cgd 	default:
    286      1.15    lukem 		warnx("%d: unknown quota type", quotatype);
    287       1.1      cgd 		break;
    288       1.1      cgd 	}
    289       1.1      cgd 	sleep(1);
    290       1.1      cgd 	return (-1);
    291       1.1      cgd }
    292       1.1      cgd 
    293       1.1      cgd /*
    294       1.1      cgd  * Collect the requested quota information.
    295       1.1      cgd  */
    296       1.1      cgd struct quotause *
    297  1.21.6.1      jmc getprivs(id, quotatype, filesys)
    298      1.15    lukem 	long id;
    299       1.1      cgd 	int quotatype;
    300  1.21.6.1      jmc 	char *filesys;
    301       1.1      cgd {
    302      1.15    lukem 	struct fstab *fs;
    303      1.15    lukem 	struct quotause *qup, *quptail;
    304       1.1      cgd 	struct quotause *quphead;
    305       1.1      cgd 	int qcmd, qupsize, fd;
    306       1.1      cgd 	char *qfpathname;
    307       1.1      cgd 	static int warned = 0;
    308       1.1      cgd 
    309       1.1      cgd 	setfsent();
    310      1.15    lukem 	quptail = NULL;
    311       1.1      cgd 	quphead = (struct quotause *)0;
    312       1.1      cgd 	qcmd = QCMD(Q_GETQUOTA, quotatype);
    313      1.15    lukem 	while ((fs = getfsent()) != NULL) {
    314      1.10      jtc 		if (strcmp(fs->fs_vfstype, "ffs"))
    315  1.21.6.1      jmc 			continue;
    316  1.21.6.1      jmc 		if (filesys && strcmp(fs->fs_spec, filesys) != 0 &&
    317  1.21.6.1      jmc 		    strcmp(fs->fs_file, filesys) != 0)
    318       1.1      cgd 			continue;
    319       1.1      cgd 		if (!hasquota(fs, quotatype, &qfpathname))
    320       1.1      cgd 			continue;
    321       1.1      cgd 		qupsize = sizeof(*qup) + strlen(qfpathname);
    322      1.15    lukem 		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
    323      1.15    lukem 			errx(2, "out of memory");
    324       1.1      cgd 		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
    325       1.1      cgd 	    		if (errno == EOPNOTSUPP && !warned) {
    326       1.1      cgd 				warned++;
    327      1.15    lukem 				warnx(
    328       1.1      cgd 				    "Quotas are not compiled into this kernel");
    329       1.1      cgd 				sleep(3);
    330       1.1      cgd 			}
    331       1.1      cgd 			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    332       1.1      cgd 				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    333       1.1      cgd 				if (fd < 0 && errno != ENOENT) {
    334      1.15    lukem 					warnx("open `%s'", qfpathname);
    335       1.1      cgd 					free(qup);
    336       1.1      cgd 					continue;
    337       1.1      cgd 				}
    338      1.15    lukem 				warnx("Creating quota file %s", qfpathname);
    339       1.1      cgd 				sleep(3);
    340       1.1      cgd 				(void) fchown(fd, getuid(),
    341       1.1      cgd 				    getentry(quotagroup, GRPQUOTA));
    342       1.1      cgd 				(void) fchmod(fd, 0640);
    343       1.1      cgd 			}
    344      1.14   kleink 			(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    345      1.14   kleink 			    SEEK_SET);
    346       1.1      cgd 			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
    347       1.1      cgd 			case 0:			/* EOF */
    348       1.1      cgd 				/*
    349       1.1      cgd 				 * Convert implicit 0 quota (EOF)
    350       1.1      cgd 				 * into an explicit one (zero'ed dqblk)
    351       1.1      cgd 				 */
    352      1.15    lukem 				memset((caddr_t)&qup->dqblk, 0,
    353       1.1      cgd 				    sizeof(struct dqblk));
    354       1.1      cgd 				break;
    355       1.1      cgd 
    356       1.1      cgd 			case sizeof(struct dqblk):	/* OK */
    357       1.1      cgd 				break;
    358       1.1      cgd 
    359       1.1      cgd 			default:		/* ERROR */
    360      1.15    lukem 				warn("read error in `%s'", qfpathname);
    361       1.1      cgd 				close(fd);
    362       1.1      cgd 				free(qup);
    363       1.1      cgd 				continue;
    364       1.1      cgd 			}
    365       1.1      cgd 			close(fd);
    366       1.1      cgd 		}
    367       1.1      cgd 		strcpy(qup->qfname, qfpathname);
    368       1.1      cgd 		strcpy(qup->fsname, fs->fs_file);
    369       1.1      cgd 		if (quphead == NULL)
    370       1.1      cgd 			quphead = qup;
    371       1.1      cgd 		else
    372       1.1      cgd 			quptail->next = qup;
    373       1.1      cgd 		quptail = qup;
    374       1.1      cgd 		qup->next = 0;
    375       1.1      cgd 	}
    376       1.1      cgd 	endfsent();
    377       1.1      cgd 	return (quphead);
    378       1.1      cgd }
    379       1.1      cgd 
    380       1.1      cgd /*
    381       1.1      cgd  * Store the requested quota information.
    382       1.1      cgd  */
    383      1.12    mikel void
    384       1.1      cgd putprivs(id, quotatype, quplist)
    385       1.1      cgd 	long id;
    386       1.1      cgd 	int quotatype;
    387       1.1      cgd 	struct quotause *quplist;
    388       1.1      cgd {
    389      1.15    lukem 	struct quotause *qup;
    390       1.1      cgd 	int qcmd, fd;
    391       1.1      cgd 
    392       1.1      cgd 	qcmd = QCMD(Q_SETQUOTA, quotatype);
    393       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    394       1.1      cgd 		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
    395       1.1      cgd 			continue;
    396       1.1      cgd 		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    397      1.15    lukem 			warnx("open `%s'", qup->qfname);
    398       1.1      cgd 		} else {
    399      1.14   kleink 			(void)lseek(fd,
    400      1.14   kleink 			    (off_t)(id * (long)sizeof (struct dqblk)),
    401      1.14   kleink 			    SEEK_SET);
    402       1.1      cgd 			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
    403      1.15    lukem 			    sizeof (struct dqblk))
    404      1.15    lukem 				warnx("writing `%s'", qup->qfname);
    405       1.1      cgd 			close(fd);
    406       1.1      cgd 		}
    407       1.1      cgd 	}
    408       1.1      cgd }
    409       1.1      cgd 
    410       1.1      cgd /*
    411      1.18   simonb  * Take a list of privileges and get it edited.
    412       1.1      cgd  */
    413      1.12    mikel int
    414       1.1      cgd editit(tmpfile)
    415       1.1      cgd 	char *tmpfile;
    416       1.1      cgd {
    417       1.1      cgd 	long omask;
    418       1.1      cgd 	int pid, stat;
    419       1.1      cgd 
    420       1.1      cgd 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
    421       1.1      cgd  top:
    422       1.1      cgd 	if ((pid = fork()) < 0) {
    423       1.1      cgd 
    424       1.1      cgd 		if (errno == EPROCLIM) {
    425      1.15    lukem 			warnx("You have too many processes");
    426       1.1      cgd 			return(0);
    427       1.1      cgd 		}
    428       1.1      cgd 		if (errno == EAGAIN) {
    429       1.1      cgd 			sleep(1);
    430       1.1      cgd 			goto top;
    431       1.1      cgd 		}
    432      1.15    lukem 		warn("fork");
    433       1.1      cgd 		return (0);
    434       1.1      cgd 	}
    435       1.1      cgd 	if (pid == 0) {
    436      1.15    lukem 		char *ed;
    437       1.1      cgd 
    438       1.1      cgd 		sigsetmask(omask);
    439       1.1      cgd 		setgid(getgid());
    440       1.1      cgd 		setuid(getuid());
    441       1.1      cgd 		if ((ed = getenv("EDITOR")) == (char *)0)
    442       1.1      cgd 			ed = _PATH_VI;
    443       1.1      cgd 		execlp(ed, ed, tmpfile, 0);
    444      1.15    lukem 		err(1, "%s", ed);
    445       1.1      cgd 	}
    446       1.1      cgd 	waitpid(pid, &stat, 0);
    447       1.1      cgd 	sigsetmask(omask);
    448       1.1      cgd 	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
    449       1.1      cgd 		return (0);
    450       1.1      cgd 	return (1);
    451       1.1      cgd }
    452       1.1      cgd 
    453       1.1      cgd /*
    454       1.1      cgd  * Convert a quotause list to an ASCII file.
    455       1.1      cgd  */
    456      1.12    mikel int
    457       1.1      cgd writeprivs(quplist, outfd, name, quotatype)
    458       1.1      cgd 	struct quotause *quplist;
    459       1.1      cgd 	int outfd;
    460       1.1      cgd 	char *name;
    461       1.1      cgd 	int quotatype;
    462       1.1      cgd {
    463      1.15    lukem 	struct quotause *qup;
    464       1.1      cgd 	FILE *fd;
    465       1.1      cgd 
    466       1.1      cgd 	ftruncate(outfd, 0);
    467      1.14   kleink 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    468      1.15    lukem 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    469      1.15    lukem 		errx(1, "fdopen `%s'", tmpfil);
    470       1.1      cgd 	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
    471       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    472       1.1      cgd 		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
    473       1.1      cgd 		    qup->fsname, "blocks in use:",
    474      1.19   bouyer 		    (int)(dbtob((u_quad_t)qup->dqblk.dqb_curblocks) / 1024),
    475      1.19   bouyer 		    (int)(dbtob((u_quad_t)qup->dqblk.dqb_bsoftlimit) / 1024),
    476      1.19   bouyer 		    (int)(dbtob((u_quad_t)qup->dqblk.dqb_bhardlimit) / 1024));
    477       1.1      cgd 		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
    478       1.1      cgd 		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
    479       1.1      cgd 		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
    480       1.1      cgd 	}
    481       1.1      cgd 	fclose(fd);
    482       1.1      cgd 	return (1);
    483       1.1      cgd }
    484       1.1      cgd 
    485       1.1      cgd /*
    486       1.1      cgd  * Merge changes to an ASCII file into a quotause list.
    487       1.1      cgd  */
    488      1.12    mikel int
    489       1.1      cgd readprivs(quplist, infd)
    490       1.1      cgd 	struct quotause *quplist;
    491       1.1      cgd 	int infd;
    492       1.1      cgd {
    493      1.15    lukem 	struct quotause *qup;
    494       1.1      cgd 	FILE *fd;
    495       1.1      cgd 	int cnt;
    496      1.15    lukem 	char *cp;
    497       1.1      cgd 	struct dqblk dqblk;
    498       1.1      cgd 	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
    499       1.1      cgd 
    500      1.14   kleink 	(void)lseek(infd, (off_t)0, SEEK_SET);
    501       1.1      cgd 	fd = fdopen(dup(infd), "r");
    502       1.1      cgd 	if (fd == NULL) {
    503      1.15    lukem 		warn("Can't re-read temp file");
    504       1.1      cgd 		return (0);
    505       1.1      cgd 	}
    506       1.1      cgd 	/*
    507       1.1      cgd 	 * Discard title line, then read pairs of lines to process.
    508       1.1      cgd 	 */
    509       1.1      cgd 	(void) fgets(line1, sizeof (line1), fd);
    510       1.1      cgd 	while (fgets(line1, sizeof (line1), fd) != NULL &&
    511       1.1      cgd 	       fgets(line2, sizeof (line2), fd) != NULL) {
    512       1.1      cgd 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    513      1.15    lukem 			warnx("%s: bad format", line1);
    514       1.1      cgd 			return (0);
    515       1.1      cgd 		}
    516       1.1      cgd 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    517      1.15    lukem 			warnx("%s: %s: bad format", fsp,
    518       1.1      cgd 			    &fsp[strlen(fsp) + 1]);
    519       1.1      cgd 			return (0);
    520       1.1      cgd 		}
    521       1.1      cgd 		cnt = sscanf(cp,
    522       1.1      cgd 		    " blocks in use: %d, limits (soft = %d, hard = %d)",
    523       1.1      cgd 		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
    524       1.1      cgd 		    &dqblk.dqb_bhardlimit);
    525       1.1      cgd 		if (cnt != 3) {
    526      1.15    lukem 			warnx("%s:%s: bad format", fsp, cp);
    527       1.1      cgd 			return (0);
    528       1.1      cgd 		}
    529      1.19   bouyer 		dqblk.dqb_curblocks = btodb((u_quad_t)
    530      1.19   bouyer 		    dqblk.dqb_curblocks * 1024);
    531      1.19   bouyer 		dqblk.dqb_bsoftlimit = btodb((u_quad_t)
    532      1.19   bouyer 		    dqblk.dqb_bsoftlimit * 1024);
    533      1.19   bouyer 		dqblk.dqb_bhardlimit = btodb((u_quad_t)
    534      1.19   bouyer 		    dqblk.dqb_bhardlimit * 1024);
    535       1.1      cgd 		if ((cp = strtok(line2, "\n")) == NULL) {
    536      1.15    lukem 			warnx("%s: %s: bad format", fsp, line2);
    537       1.1      cgd 			return (0);
    538       1.1      cgd 		}
    539       1.1      cgd 		cnt = sscanf(cp,
    540       1.1      cgd 		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
    541       1.1      cgd 		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
    542       1.1      cgd 		    &dqblk.dqb_ihardlimit);
    543       1.1      cgd 		if (cnt != 3) {
    544      1.15    lukem 			warnx("%s: %s: bad format", fsp, line2);
    545       1.1      cgd 			return (0);
    546       1.1      cgd 		}
    547       1.1      cgd 		for (qup = quplist; qup; qup = qup->next) {
    548       1.1      cgd 			if (strcmp(fsp, qup->fsname))
    549       1.1      cgd 				continue;
    550       1.1      cgd 			/*
    551       1.1      cgd 			 * Cause time limit to be reset when the quota
    552       1.1      cgd 			 * is next used if previously had no soft limit
    553       1.1      cgd 			 * or were under it, but now have a soft limit
    554       1.1      cgd 			 * and are over it.
    555       1.1      cgd 			 */
    556       1.1      cgd 			if (dqblk.dqb_bsoftlimit &&
    557       1.1      cgd 			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
    558       1.1      cgd 			    (qup->dqblk.dqb_bsoftlimit == 0 ||
    559       1.1      cgd 			     qup->dqblk.dqb_curblocks <
    560       1.1      cgd 			     qup->dqblk.dqb_bsoftlimit))
    561       1.1      cgd 				qup->dqblk.dqb_btime = 0;
    562       1.1      cgd 			if (dqblk.dqb_isoftlimit &&
    563       1.1      cgd 			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
    564       1.1      cgd 			    (qup->dqblk.dqb_isoftlimit == 0 ||
    565       1.1      cgd 			     qup->dqblk.dqb_curinodes <
    566       1.1      cgd 			     qup->dqblk.dqb_isoftlimit))
    567       1.1      cgd 				qup->dqblk.dqb_itime = 0;
    568       1.1      cgd 			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
    569       1.1      cgd 			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
    570       1.1      cgd 			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
    571       1.1      cgd 			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
    572       1.1      cgd 			qup->flags |= FOUND;
    573       1.1      cgd 			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
    574       1.1      cgd 			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
    575       1.1      cgd 				break;
    576      1.15    lukem 			warnx("%s: cannot change current allocation", fsp);
    577       1.1      cgd 			break;
    578       1.1      cgd 		}
    579       1.1      cgd 	}
    580       1.1      cgd 	fclose(fd);
    581       1.1      cgd 	/*
    582       1.1      cgd 	 * Disable quotas for any filesystems that have not been found.
    583       1.1      cgd 	 */
    584       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    585       1.1      cgd 		if (qup->flags & FOUND) {
    586       1.1      cgd 			qup->flags &= ~FOUND;
    587       1.1      cgd 			continue;
    588       1.1      cgd 		}
    589       1.1      cgd 		qup->dqblk.dqb_bsoftlimit = 0;
    590       1.1      cgd 		qup->dqblk.dqb_bhardlimit = 0;
    591       1.1      cgd 		qup->dqblk.dqb_isoftlimit = 0;
    592       1.1      cgd 		qup->dqblk.dqb_ihardlimit = 0;
    593       1.1      cgd 	}
    594       1.1      cgd 	return (1);
    595       1.1      cgd }
    596       1.1      cgd 
    597       1.1      cgd /*
    598       1.1      cgd  * Convert a quotause list to an ASCII file of grace times.
    599       1.1      cgd  */
    600      1.12    mikel int
    601       1.1      cgd writetimes(quplist, outfd, quotatype)
    602       1.1      cgd 	struct quotause *quplist;
    603       1.1      cgd 	int outfd;
    604       1.1      cgd 	int quotatype;
    605       1.1      cgd {
    606      1.15    lukem 	struct quotause *qup;
    607       1.1      cgd 	FILE *fd;
    608       1.1      cgd 
    609       1.1      cgd 	ftruncate(outfd, 0);
    610      1.14   kleink 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    611      1.15    lukem 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    612      1.15    lukem 		err(1, "fdopen `%s'", tmpfil);
    613       1.1      cgd 	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
    614       1.1      cgd 	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
    615       1.1      cgd 	    qfextension[quotatype]);
    616       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    617       1.1      cgd 		fprintf(fd, "%s: block grace period: %s, ",
    618       1.1      cgd 		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
    619       1.1      cgd 		fprintf(fd, "file grace period: %s\n",
    620       1.1      cgd 		    cvtstoa(qup->dqblk.dqb_itime));
    621       1.1      cgd 	}
    622       1.1      cgd 	fclose(fd);
    623       1.1      cgd 	return (1);
    624       1.1      cgd }
    625       1.1      cgd 
    626       1.1      cgd /*
    627       1.1      cgd  * Merge changes of grace times in an ASCII file into a quotause list.
    628       1.1      cgd  */
    629      1.12    mikel int
    630       1.1      cgd readtimes(quplist, infd)
    631       1.1      cgd 	struct quotause *quplist;
    632       1.1      cgd 	int infd;
    633       1.1      cgd {
    634      1.15    lukem 	struct quotause *qup;
    635       1.1      cgd 	FILE *fd;
    636       1.1      cgd 	int cnt;
    637      1.15    lukem 	char *cp;
    638      1.15    lukem 	long litime, lbtime;
    639       1.1      cgd 	time_t itime, btime, iseconds, bseconds;
    640       1.1      cgd 	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
    641       1.1      cgd 
    642      1.14   kleink 	(void)lseek(infd, (off_t)0, SEEK_SET);
    643       1.1      cgd 	fd = fdopen(dup(infd), "r");
    644       1.1      cgd 	if (fd == NULL) {
    645      1.15    lukem 		warnx("Can't re-read temp file!!");
    646       1.1      cgd 		return (0);
    647       1.1      cgd 	}
    648       1.1      cgd 	/*
    649       1.1      cgd 	 * Discard two title lines, then read lines to process.
    650       1.1      cgd 	 */
    651       1.1      cgd 	(void) fgets(line1, sizeof (line1), fd);
    652       1.1      cgd 	(void) fgets(line1, sizeof (line1), fd);
    653       1.1      cgd 	while (fgets(line1, sizeof (line1), fd) != NULL) {
    654       1.1      cgd 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    655      1.15    lukem 			warnx("%s: bad format", line1);
    656       1.1      cgd 			return (0);
    657       1.1      cgd 		}
    658       1.1      cgd 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    659      1.15    lukem 			warnx("%s: %s: bad format", fsp,
    660       1.1      cgd 			    &fsp[strlen(fsp) + 1]);
    661       1.1      cgd 			return (0);
    662       1.1      cgd 		}
    663       1.1      cgd 		cnt = sscanf(cp,
    664      1.15    lukem 		    " block grace period: %ld %s file grace period: %ld %s",
    665      1.15    lukem 		    &lbtime, bunits, &litime, iunits);
    666       1.1      cgd 		if (cnt != 4) {
    667      1.15    lukem 			warnx("%s:%s: bad format", fsp, cp);
    668       1.1      cgd 			return (0);
    669       1.1      cgd 		}
    670      1.15    lukem 		itime = (time_t)litime;
    671      1.15    lukem 		btime = (time_t)lbtime;
    672       1.1      cgd 		if (cvtatos(btime, bunits, &bseconds) == 0)
    673       1.1      cgd 			return (0);
    674       1.1      cgd 		if (cvtatos(itime, iunits, &iseconds) == 0)
    675       1.1      cgd 			return (0);
    676       1.1      cgd 		for (qup = quplist; qup; qup = qup->next) {
    677       1.1      cgd 			if (strcmp(fsp, qup->fsname))
    678       1.1      cgd 				continue;
    679       1.1      cgd 			qup->dqblk.dqb_btime = bseconds;
    680       1.1      cgd 			qup->dqblk.dqb_itime = iseconds;
    681       1.1      cgd 			qup->flags |= FOUND;
    682       1.1      cgd 			break;
    683       1.1      cgd 		}
    684       1.1      cgd 	}
    685       1.1      cgd 	fclose(fd);
    686       1.1      cgd 	/*
    687       1.1      cgd 	 * reset default grace periods for any filesystems
    688       1.1      cgd 	 * that have not been found.
    689       1.1      cgd 	 */
    690       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    691       1.1      cgd 		if (qup->flags & FOUND) {
    692       1.1      cgd 			qup->flags &= ~FOUND;
    693       1.1      cgd 			continue;
    694       1.1      cgd 		}
    695       1.1      cgd 		qup->dqblk.dqb_btime = 0;
    696       1.1      cgd 		qup->dqblk.dqb_itime = 0;
    697       1.1      cgd 	}
    698       1.1      cgd 	return (1);
    699       1.1      cgd }
    700       1.1      cgd 
    701       1.1      cgd /*
    702       1.1      cgd  * Convert seconds to ASCII times.
    703       1.1      cgd  */
    704       1.1      cgd char *
    705       1.1      cgd cvtstoa(time)
    706       1.1      cgd 	time_t time;
    707       1.1      cgd {
    708       1.1      cgd 	static char buf[20];
    709       1.1      cgd 
    710       1.1      cgd 	if (time % (24 * 60 * 60) == 0) {
    711       1.1      cgd 		time /= 24 * 60 * 60;
    712      1.15    lukem 		snprintf(buf, sizeof buf, "%ld day%s", (long)time,
    713      1.15    lukem 		    time == 1 ? "" : "s");
    714       1.1      cgd 	} else if (time % (60 * 60) == 0) {
    715       1.1      cgd 		time /= 60 * 60;
    716      1.15    lukem 		sprintf(buf, "%ld hour%s", (long)time, time == 1 ? "" : "s");
    717       1.1      cgd 	} else if (time % 60 == 0) {
    718       1.1      cgd 		time /= 60;
    719      1.15    lukem 		sprintf(buf, "%ld minute%s", (long)time, time == 1 ? "" : "s");
    720       1.1      cgd 	} else
    721      1.15    lukem 		sprintf(buf, "%ld second%s", (long)time, time == 1 ? "" : "s");
    722       1.1      cgd 	return (buf);
    723       1.1      cgd }
    724       1.1      cgd 
    725       1.1      cgd /*
    726       1.1      cgd  * Convert ASCII input times to seconds.
    727       1.1      cgd  */
    728      1.12    mikel int
    729       1.1      cgd cvtatos(time, units, seconds)
    730       1.1      cgd 	time_t time;
    731       1.1      cgd 	char *units;
    732       1.1      cgd 	time_t *seconds;
    733       1.1      cgd {
    734       1.1      cgd 
    735      1.15    lukem 	if (memcmp(units, "second", 6) == 0)
    736       1.1      cgd 		*seconds = time;
    737      1.15    lukem 	else if (memcmp(units, "minute", 6) == 0)
    738       1.1      cgd 		*seconds = time * 60;
    739      1.15    lukem 	else if (memcmp(units, "hour", 4) == 0)
    740       1.1      cgd 		*seconds = time * 60 * 60;
    741      1.15    lukem 	else if (memcmp(units, "day", 3) == 0)
    742       1.1      cgd 		*seconds = time * 24 * 60 * 60;
    743       1.1      cgd 	else {
    744       1.1      cgd 		printf("%s: bad units, specify %s\n", units,
    745       1.1      cgd 		    "days, hours, minutes, or seconds");
    746       1.1      cgd 		return (0);
    747       1.1      cgd 	}
    748       1.1      cgd 	return (1);
    749       1.1      cgd }
    750       1.1      cgd 
    751       1.1      cgd /*
    752       1.1      cgd  * Free a list of quotause structures.
    753       1.1      cgd  */
    754      1.12    mikel void
    755       1.1      cgd freeprivs(quplist)
    756       1.1      cgd 	struct quotause *quplist;
    757       1.1      cgd {
    758      1.15    lukem 	struct quotause *qup, *nextqup;
    759       1.1      cgd 
    760       1.1      cgd 	for (qup = quplist; qup; qup = nextqup) {
    761       1.1      cgd 		nextqup = qup->next;
    762       1.1      cgd 		free(qup);
    763       1.1      cgd 	}
    764       1.1      cgd }
    765       1.1      cgd 
    766       1.1      cgd /*
    767       1.1      cgd  * Check whether a string is completely composed of digits.
    768       1.1      cgd  */
    769      1.12    mikel int
    770       1.1      cgd alldigits(s)
    771      1.15    lukem 	char *s;
    772       1.1      cgd {
    773      1.15    lukem 	int c;
    774       1.1      cgd 
    775       1.1      cgd 	c = *s++;
    776       1.1      cgd 	do {
    777       1.1      cgd 		if (!isdigit(c))
    778       1.1      cgd 			return (0);
    779      1.15    lukem 	} while ((c = *s++) != 0);
    780       1.1      cgd 	return (1);
    781       1.1      cgd }
    782       1.1      cgd 
    783       1.1      cgd /*
    784       1.1      cgd  * Check to see if a particular quota is to be enabled.
    785       1.1      cgd  */
    786      1.12    mikel int
    787       1.1      cgd hasquota(fs, type, qfnamep)
    788      1.15    lukem 	struct fstab *fs;
    789       1.1      cgd 	int type;
    790       1.1      cgd 	char **qfnamep;
    791       1.1      cgd {
    792      1.15    lukem 	char *opt;
    793      1.15    lukem 	char *cp;
    794       1.1      cgd 	static char initname, usrname[100], grpname[100];
    795       1.1      cgd 	static char buf[BUFSIZ];
    796       1.1      cgd 
    797       1.1      cgd 	if (!initname) {
    798       1.1      cgd 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    799       1.1      cgd 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    800       1.1      cgd 		initname = 1;
    801       1.1      cgd 	}
    802       1.1      cgd 	strcpy(buf, fs->fs_mntops);
    803      1.16      mrg 	cp = NULL;
    804       1.1      cgd 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    805      1.15    lukem 		if ((cp = strchr(opt, '=')) != NULL)
    806       1.1      cgd 			*cp++ = '\0';
    807       1.1      cgd 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    808       1.1      cgd 			break;
    809       1.1      cgd 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    810       1.1      cgd 			break;
    811       1.1      cgd 	}
    812       1.1      cgd 	if (!opt)
    813       1.1      cgd 		return (0);
    814       1.1      cgd 	if (cp) {
    815       1.1      cgd 		*qfnamep = cp;
    816       1.1      cgd 		return (1);
    817       1.1      cgd 	}
    818       1.1      cgd 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    819       1.1      cgd 	*qfnamep = buf;
    820       1.1      cgd 	return (1);
    821       1.1      cgd }
    822