Home | History | Annotate | Line # | Download | only in edquota
edquota.c revision 1.15.2.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.15.2.1      mrg __RCSID("$NetBSD: edquota.c,v 1.15.2.1 1997/10/21 07:58:01 mrg 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.13    mikel 	getprivs __P((long, int));
     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.1      cgd 	extern char *optarg;
    111       1.1      cgd 	extern int optind;
    112      1.15    lukem 	long id, protoid;
    113      1.15    lukem 	int quotatype, tmpfd;
    114      1.11     mark 	char *protoname;
    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.11     mark 	while ((ch = getopt(argc, argv, "ugtp:")) != -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.1      cgd 		default:
    140       1.1      cgd 			usage();
    141       1.1      cgd 		}
    142       1.1      cgd 	}
    143       1.1      cgd 	argc -= optind;
    144       1.1      cgd 	argv += optind;
    145       1.1      cgd 	if (pflag) {
    146       1.1      cgd 		if ((protoid = getentry(protoname, quotatype)) == -1)
    147       1.1      cgd 			exit(1);
    148       1.1      cgd 		protoprivs = getprivs(protoid, quotatype);
    149       1.1      cgd 		for (qup = protoprivs; qup; qup = qup->next) {
    150       1.1      cgd 			qup->dqblk.dqb_btime = 0;
    151       1.1      cgd 			qup->dqblk.dqb_itime = 0;
    152       1.1      cgd 		}
    153       1.1      cgd 		while (argc-- > 0) {
    154       1.1      cgd 			if ((id = getentry(*argv++, quotatype)) < 0)
    155       1.1      cgd 				continue;
    156       1.1      cgd 			putprivs(id, quotatype, protoprivs);
    157       1.1      cgd 		}
    158       1.1      cgd 		exit(0);
    159       1.1      cgd 	}
    160       1.1      cgd 	tmpfd = mkstemp(tmpfil);
    161       1.1      cgd 	fchown(tmpfd, getuid(), getgid());
    162       1.1      cgd 	if (tflag) {
    163       1.1      cgd 		protoprivs = getprivs(0, quotatype);
    164       1.1      cgd 		if (writetimes(protoprivs, tmpfd, quotatype) == 0)
    165       1.1      cgd 			exit(1);
    166       1.1      cgd 		if (editit(tmpfil) && readtimes(protoprivs, tmpfd))
    167       1.1      cgd 			putprivs(0, quotatype, protoprivs);
    168       1.1      cgd 		freeprivs(protoprivs);
    169       1.1      cgd 		exit(0);
    170       1.1      cgd 	}
    171       1.1      cgd 	for ( ; argc > 0; argc--, argv++) {
    172       1.1      cgd 		if ((id = getentry(*argv, quotatype)) == -1)
    173       1.1      cgd 			continue;
    174       1.1      cgd 		curprivs = getprivs(id, quotatype);
    175       1.1      cgd 		if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
    176       1.1      cgd 			continue;
    177       1.1      cgd 		if (editit(tmpfil) && readprivs(curprivs, tmpfd))
    178       1.1      cgd 			putprivs(id, quotatype, curprivs);
    179       1.1      cgd 		freeprivs(curprivs);
    180       1.1      cgd 	}
    181       1.1      cgd 	close(tmpfd);
    182       1.1      cgd 	unlink(tmpfil);
    183       1.1      cgd 	exit(0);
    184       1.1      cgd }
    185       1.1      cgd 
    186      1.12    mikel void
    187       1.1      cgd usage()
    188       1.1      cgd {
    189       1.1      cgd 	fprintf(stderr, "%s%s%s%s",
    190       1.1      cgd 		"Usage: edquota [-u] [-p username] username ...\n",
    191       1.1      cgd 		"\tedquota -g [-p groupname] groupname ...\n",
    192       1.1      cgd 		"\tedquota [-u] -t\n", "\tedquota -g -t\n");
    193       1.1      cgd 	exit(1);
    194       1.1      cgd }
    195       1.1      cgd 
    196       1.1      cgd /*
    197       1.1      cgd  * This routine converts a name for a particular quota type to
    198       1.1      cgd  * an identifier. This routine must agree with the kernel routine
    199       1.1      cgd  * getinoquota as to the interpretation of quota types.
    200       1.1      cgd  */
    201      1.12    mikel int
    202       1.1      cgd getentry(name, quotatype)
    203       1.1      cgd 	char *name;
    204       1.1      cgd 	int quotatype;
    205       1.1      cgd {
    206       1.1      cgd 	struct passwd *pw;
    207       1.1      cgd 	struct group *gr;
    208       1.1      cgd 
    209       1.1      cgd 	if (alldigits(name))
    210       1.1      cgd 		return (atoi(name));
    211       1.1      cgd 	switch(quotatype) {
    212       1.1      cgd 	case USRQUOTA:
    213      1.15    lukem 		if ((pw = getpwnam(name)) != NULL)
    214       1.1      cgd 			return (pw->pw_uid);
    215      1.15    lukem 		warnx("%s: no such user", name);
    216       1.1      cgd 		break;
    217       1.1      cgd 	case GRPQUOTA:
    218      1.15    lukem 		if ((gr = getgrnam(name)) != NULL)
    219       1.1      cgd 			return (gr->gr_gid);
    220      1.15    lukem 		warnx("%s: no such group", name);
    221       1.1      cgd 		break;
    222       1.1      cgd 	default:
    223      1.15    lukem 		warnx("%d: unknown quota type", quotatype);
    224       1.1      cgd 		break;
    225       1.1      cgd 	}
    226       1.1      cgd 	sleep(1);
    227       1.1      cgd 	return (-1);
    228       1.1      cgd }
    229       1.1      cgd 
    230       1.1      cgd /*
    231       1.1      cgd  * Collect the requested quota information.
    232       1.1      cgd  */
    233       1.1      cgd struct quotause *
    234       1.1      cgd getprivs(id, quotatype)
    235      1.15    lukem 	long id;
    236       1.1      cgd 	int quotatype;
    237       1.1      cgd {
    238      1.15    lukem 	struct fstab *fs;
    239      1.15    lukem 	struct quotause *qup, *quptail;
    240       1.1      cgd 	struct quotause *quphead;
    241       1.1      cgd 	int qcmd, qupsize, fd;
    242       1.1      cgd 	char *qfpathname;
    243       1.1      cgd 	static int warned = 0;
    244       1.1      cgd 	extern int errno;
    245       1.1      cgd 
    246       1.1      cgd 	setfsent();
    247      1.15    lukem 	quptail = NULL;
    248       1.1      cgd 	quphead = (struct quotause *)0;
    249       1.1      cgd 	qcmd = QCMD(Q_GETQUOTA, quotatype);
    250      1.15    lukem 	while ((fs = getfsent()) != NULL) {
    251      1.10      jtc 		if (strcmp(fs->fs_vfstype, "ffs"))
    252       1.1      cgd 			continue;
    253       1.1      cgd 		if (!hasquota(fs, quotatype, &qfpathname))
    254       1.1      cgd 			continue;
    255       1.1      cgd 		qupsize = sizeof(*qup) + strlen(qfpathname);
    256      1.15    lukem 		if ((qup = (struct quotause *)malloc(qupsize)) == NULL)
    257      1.15    lukem 			errx(2, "out of memory");
    258       1.1      cgd 		if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
    259       1.1      cgd 	    		if (errno == EOPNOTSUPP && !warned) {
    260       1.1      cgd 				warned++;
    261      1.15    lukem 				warnx(
    262       1.1      cgd 				    "Quotas are not compiled into this kernel");
    263       1.1      cgd 				sleep(3);
    264       1.1      cgd 			}
    265       1.1      cgd 			if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    266       1.1      cgd 				fd = open(qfpathname, O_RDWR|O_CREAT, 0640);
    267       1.1      cgd 				if (fd < 0 && errno != ENOENT) {
    268      1.15    lukem 					warnx("open `%s'", qfpathname);
    269       1.1      cgd 					free(qup);
    270       1.1      cgd 					continue;
    271       1.1      cgd 				}
    272      1.15    lukem 				warnx("Creating quota file %s", qfpathname);
    273       1.1      cgd 				sleep(3);
    274       1.1      cgd 				(void) fchown(fd, getuid(),
    275       1.1      cgd 				    getentry(quotagroup, GRPQUOTA));
    276       1.1      cgd 				(void) fchmod(fd, 0640);
    277       1.1      cgd 			}
    278      1.14   kleink 			(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)),
    279      1.14   kleink 			    SEEK_SET);
    280       1.1      cgd 			switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
    281       1.1      cgd 			case 0:			/* EOF */
    282       1.1      cgd 				/*
    283       1.1      cgd 				 * Convert implicit 0 quota (EOF)
    284       1.1      cgd 				 * into an explicit one (zero'ed dqblk)
    285       1.1      cgd 				 */
    286      1.15    lukem 				memset((caddr_t)&qup->dqblk, 0,
    287       1.1      cgd 				    sizeof(struct dqblk));
    288       1.1      cgd 				break;
    289       1.1      cgd 
    290       1.1      cgd 			case sizeof(struct dqblk):	/* OK */
    291       1.1      cgd 				break;
    292       1.1      cgd 
    293       1.1      cgd 			default:		/* ERROR */
    294      1.15    lukem 				warn("read error in `%s'", qfpathname);
    295       1.1      cgd 				close(fd);
    296       1.1      cgd 				free(qup);
    297       1.1      cgd 				continue;
    298       1.1      cgd 			}
    299       1.1      cgd 			close(fd);
    300       1.1      cgd 		}
    301       1.1      cgd 		strcpy(qup->qfname, qfpathname);
    302       1.1      cgd 		strcpy(qup->fsname, fs->fs_file);
    303       1.1      cgd 		if (quphead == NULL)
    304       1.1      cgd 			quphead = qup;
    305       1.1      cgd 		else
    306       1.1      cgd 			quptail->next = qup;
    307       1.1      cgd 		quptail = qup;
    308       1.1      cgd 		qup->next = 0;
    309       1.1      cgd 	}
    310       1.1      cgd 	endfsent();
    311       1.1      cgd 	return (quphead);
    312       1.1      cgd }
    313       1.1      cgd 
    314       1.1      cgd /*
    315       1.1      cgd  * Store the requested quota information.
    316       1.1      cgd  */
    317      1.12    mikel void
    318       1.1      cgd putprivs(id, quotatype, quplist)
    319       1.1      cgd 	long id;
    320       1.1      cgd 	int quotatype;
    321       1.1      cgd 	struct quotause *quplist;
    322       1.1      cgd {
    323      1.15    lukem 	struct quotause *qup;
    324       1.1      cgd 	int qcmd, fd;
    325       1.1      cgd 
    326       1.1      cgd 	qcmd = QCMD(Q_SETQUOTA, quotatype);
    327       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    328       1.1      cgd 		if (quotactl(qup->fsname, qcmd, id, &qup->dqblk) == 0)
    329       1.1      cgd 			continue;
    330       1.1      cgd 		if ((fd = open(qup->qfname, O_WRONLY)) < 0) {
    331      1.15    lukem 			warnx("open `%s'", qup->qfname);
    332       1.1      cgd 		} else {
    333      1.14   kleink 			(void)lseek(fd,
    334      1.14   kleink 			    (off_t)(id * (long)sizeof (struct dqblk)),
    335      1.14   kleink 			    SEEK_SET);
    336       1.1      cgd 			if (write(fd, &qup->dqblk, sizeof (struct dqblk)) !=
    337      1.15    lukem 			    sizeof (struct dqblk))
    338      1.15    lukem 				warnx("writing `%s'", qup->qfname);
    339       1.1      cgd 			close(fd);
    340       1.1      cgd 		}
    341       1.1      cgd 	}
    342       1.1      cgd }
    343       1.1      cgd 
    344       1.1      cgd /*
    345       1.1      cgd  * Take a list of priviledges and get it edited.
    346       1.1      cgd  */
    347      1.12    mikel int
    348       1.1      cgd editit(tmpfile)
    349       1.1      cgd 	char *tmpfile;
    350       1.1      cgd {
    351       1.1      cgd 	long omask;
    352       1.1      cgd 	int pid, stat;
    353       1.1      cgd 
    354       1.1      cgd 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
    355       1.1      cgd  top:
    356       1.1      cgd 	if ((pid = fork()) < 0) {
    357       1.1      cgd 		extern errno;
    358       1.1      cgd 
    359       1.1      cgd 		if (errno == EPROCLIM) {
    360      1.15    lukem 			warnx("You have too many processes");
    361       1.1      cgd 			return(0);
    362       1.1      cgd 		}
    363       1.1      cgd 		if (errno == EAGAIN) {
    364       1.1      cgd 			sleep(1);
    365       1.1      cgd 			goto top;
    366       1.1      cgd 		}
    367      1.15    lukem 		warn("fork");
    368       1.1      cgd 		return (0);
    369       1.1      cgd 	}
    370       1.1      cgd 	if (pid == 0) {
    371      1.15    lukem 		char *ed;
    372       1.1      cgd 
    373       1.1      cgd 		sigsetmask(omask);
    374       1.1      cgd 		setgid(getgid());
    375       1.1      cgd 		setuid(getuid());
    376       1.1      cgd 		if ((ed = getenv("EDITOR")) == (char *)0)
    377       1.1      cgd 			ed = _PATH_VI;
    378       1.1      cgd 		execlp(ed, ed, tmpfile, 0);
    379      1.15    lukem 		err(1, "%s", ed);
    380       1.1      cgd 	}
    381       1.1      cgd 	waitpid(pid, &stat, 0);
    382       1.1      cgd 	sigsetmask(omask);
    383       1.1      cgd 	if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
    384       1.1      cgd 		return (0);
    385       1.1      cgd 	return (1);
    386       1.1      cgd }
    387       1.1      cgd 
    388       1.1      cgd /*
    389       1.1      cgd  * Convert a quotause list to an ASCII file.
    390       1.1      cgd  */
    391      1.12    mikel int
    392       1.1      cgd writeprivs(quplist, outfd, name, quotatype)
    393       1.1      cgd 	struct quotause *quplist;
    394       1.1      cgd 	int outfd;
    395       1.1      cgd 	char *name;
    396       1.1      cgd 	int quotatype;
    397       1.1      cgd {
    398      1.15    lukem 	struct quotause *qup;
    399       1.1      cgd 	FILE *fd;
    400       1.1      cgd 
    401       1.1      cgd 	ftruncate(outfd, 0);
    402      1.14   kleink 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    403      1.15    lukem 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    404      1.15    lukem 		errx(1, "fdopen `%s'", tmpfil);
    405       1.1      cgd 	fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
    406       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    407       1.1      cgd 		fprintf(fd, "%s: %s %d, limits (soft = %d, hard = %d)\n",
    408       1.1      cgd 		    qup->fsname, "blocks in use:",
    409       1.1      cgd 		    dbtob(qup->dqblk.dqb_curblocks) / 1024,
    410       1.1      cgd 		    dbtob(qup->dqblk.dqb_bsoftlimit) / 1024,
    411       1.1      cgd 		    dbtob(qup->dqblk.dqb_bhardlimit) / 1024);
    412       1.1      cgd 		fprintf(fd, "%s %d, limits (soft = %d, hard = %d)\n",
    413       1.1      cgd 		    "\tinodes in use:", qup->dqblk.dqb_curinodes,
    414       1.1      cgd 		    qup->dqblk.dqb_isoftlimit, qup->dqblk.dqb_ihardlimit);
    415       1.1      cgd 	}
    416       1.1      cgd 	fclose(fd);
    417       1.1      cgd 	return (1);
    418       1.1      cgd }
    419       1.1      cgd 
    420       1.1      cgd /*
    421       1.1      cgd  * Merge changes to an ASCII file into a quotause list.
    422       1.1      cgd  */
    423      1.12    mikel int
    424       1.1      cgd readprivs(quplist, infd)
    425       1.1      cgd 	struct quotause *quplist;
    426       1.1      cgd 	int infd;
    427       1.1      cgd {
    428      1.15    lukem 	struct quotause *qup;
    429       1.1      cgd 	FILE *fd;
    430       1.1      cgd 	int cnt;
    431      1.15    lukem 	char *cp;
    432       1.1      cgd 	struct dqblk dqblk;
    433       1.1      cgd 	char *fsp, line1[BUFSIZ], line2[BUFSIZ];
    434       1.1      cgd 
    435      1.14   kleink 	(void)lseek(infd, (off_t)0, SEEK_SET);
    436       1.1      cgd 	fd = fdopen(dup(infd), "r");
    437       1.1      cgd 	if (fd == NULL) {
    438      1.15    lukem 		warn("Can't re-read temp file");
    439       1.1      cgd 		return (0);
    440       1.1      cgd 	}
    441       1.1      cgd 	/*
    442       1.1      cgd 	 * Discard title line, then read pairs of lines to process.
    443       1.1      cgd 	 */
    444       1.1      cgd 	(void) fgets(line1, sizeof (line1), fd);
    445       1.1      cgd 	while (fgets(line1, sizeof (line1), fd) != NULL &&
    446       1.1      cgd 	       fgets(line2, sizeof (line2), fd) != NULL) {
    447       1.1      cgd 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    448      1.15    lukem 			warnx("%s: bad format", line1);
    449       1.1      cgd 			return (0);
    450       1.1      cgd 		}
    451       1.1      cgd 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    452      1.15    lukem 			warnx("%s: %s: bad format", fsp,
    453       1.1      cgd 			    &fsp[strlen(fsp) + 1]);
    454       1.1      cgd 			return (0);
    455       1.1      cgd 		}
    456       1.1      cgd 		cnt = sscanf(cp,
    457       1.1      cgd 		    " blocks in use: %d, limits (soft = %d, hard = %d)",
    458       1.1      cgd 		    &dqblk.dqb_curblocks, &dqblk.dqb_bsoftlimit,
    459       1.1      cgd 		    &dqblk.dqb_bhardlimit);
    460       1.1      cgd 		if (cnt != 3) {
    461      1.15    lukem 			warnx("%s:%s: bad format", fsp, cp);
    462       1.1      cgd 			return (0);
    463       1.1      cgd 		}
    464       1.1      cgd 		dqblk.dqb_curblocks = btodb(dqblk.dqb_curblocks * 1024);
    465       1.1      cgd 		dqblk.dqb_bsoftlimit = btodb(dqblk.dqb_bsoftlimit * 1024);
    466       1.1      cgd 		dqblk.dqb_bhardlimit = btodb(dqblk.dqb_bhardlimit * 1024);
    467       1.1      cgd 		if ((cp = strtok(line2, "\n")) == NULL) {
    468      1.15    lukem 			warnx("%s: %s: bad format", fsp, line2);
    469       1.1      cgd 			return (0);
    470       1.1      cgd 		}
    471       1.1      cgd 		cnt = sscanf(cp,
    472       1.1      cgd 		    "\tinodes in use: %d, limits (soft = %d, hard = %d)",
    473       1.1      cgd 		    &dqblk.dqb_curinodes, &dqblk.dqb_isoftlimit,
    474       1.1      cgd 		    &dqblk.dqb_ihardlimit);
    475       1.1      cgd 		if (cnt != 3) {
    476      1.15    lukem 			warnx("%s: %s: bad format", fsp, line2);
    477       1.1      cgd 			return (0);
    478       1.1      cgd 		}
    479       1.1      cgd 		for (qup = quplist; qup; qup = qup->next) {
    480       1.1      cgd 			if (strcmp(fsp, qup->fsname))
    481       1.1      cgd 				continue;
    482       1.1      cgd 			/*
    483       1.1      cgd 			 * Cause time limit to be reset when the quota
    484       1.1      cgd 			 * is next used if previously had no soft limit
    485       1.1      cgd 			 * or were under it, but now have a soft limit
    486       1.1      cgd 			 * and are over it.
    487       1.1      cgd 			 */
    488       1.1      cgd 			if (dqblk.dqb_bsoftlimit &&
    489       1.1      cgd 			    qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
    490       1.1      cgd 			    (qup->dqblk.dqb_bsoftlimit == 0 ||
    491       1.1      cgd 			     qup->dqblk.dqb_curblocks <
    492       1.1      cgd 			     qup->dqblk.dqb_bsoftlimit))
    493       1.1      cgd 				qup->dqblk.dqb_btime = 0;
    494       1.1      cgd 			if (dqblk.dqb_isoftlimit &&
    495       1.1      cgd 			    qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
    496       1.1      cgd 			    (qup->dqblk.dqb_isoftlimit == 0 ||
    497       1.1      cgd 			     qup->dqblk.dqb_curinodes <
    498       1.1      cgd 			     qup->dqblk.dqb_isoftlimit))
    499       1.1      cgd 				qup->dqblk.dqb_itime = 0;
    500       1.1      cgd 			qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
    501       1.1      cgd 			qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
    502       1.1      cgd 			qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
    503       1.1      cgd 			qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
    504       1.1      cgd 			qup->flags |= FOUND;
    505       1.1      cgd 			if (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
    506       1.1      cgd 			    dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes)
    507       1.1      cgd 				break;
    508      1.15    lukem 			warnx("%s: cannot change current allocation", fsp);
    509       1.1      cgd 			break;
    510       1.1      cgd 		}
    511       1.1      cgd 	}
    512       1.1      cgd 	fclose(fd);
    513       1.1      cgd 	/*
    514       1.1      cgd 	 * Disable quotas for any filesystems that have not been found.
    515       1.1      cgd 	 */
    516       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    517       1.1      cgd 		if (qup->flags & FOUND) {
    518       1.1      cgd 			qup->flags &= ~FOUND;
    519       1.1      cgd 			continue;
    520       1.1      cgd 		}
    521       1.1      cgd 		qup->dqblk.dqb_bsoftlimit = 0;
    522       1.1      cgd 		qup->dqblk.dqb_bhardlimit = 0;
    523       1.1      cgd 		qup->dqblk.dqb_isoftlimit = 0;
    524       1.1      cgd 		qup->dqblk.dqb_ihardlimit = 0;
    525       1.1      cgd 	}
    526       1.1      cgd 	return (1);
    527       1.1      cgd }
    528       1.1      cgd 
    529       1.1      cgd /*
    530       1.1      cgd  * Convert a quotause list to an ASCII file of grace times.
    531       1.1      cgd  */
    532      1.12    mikel int
    533       1.1      cgd writetimes(quplist, outfd, quotatype)
    534       1.1      cgd 	struct quotause *quplist;
    535       1.1      cgd 	int outfd;
    536       1.1      cgd 	int quotatype;
    537       1.1      cgd {
    538      1.15    lukem 	struct quotause *qup;
    539       1.1      cgd 	FILE *fd;
    540       1.1      cgd 
    541       1.1      cgd 	ftruncate(outfd, 0);
    542      1.14   kleink 	(void)lseek(outfd, (off_t)0, SEEK_SET);
    543      1.15    lukem 	if ((fd = fdopen(dup(outfd), "w")) == NULL)
    544      1.15    lukem 		err(1, "fdopen `%s'", tmpfil);
    545       1.1      cgd 	fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
    546       1.1      cgd 	fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
    547       1.1      cgd 	    qfextension[quotatype]);
    548       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    549       1.1      cgd 		fprintf(fd, "%s: block grace period: %s, ",
    550       1.1      cgd 		    qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
    551       1.1      cgd 		fprintf(fd, "file grace period: %s\n",
    552       1.1      cgd 		    cvtstoa(qup->dqblk.dqb_itime));
    553       1.1      cgd 	}
    554       1.1      cgd 	fclose(fd);
    555       1.1      cgd 	return (1);
    556       1.1      cgd }
    557       1.1      cgd 
    558       1.1      cgd /*
    559       1.1      cgd  * Merge changes of grace times in an ASCII file into a quotause list.
    560       1.1      cgd  */
    561      1.12    mikel int
    562       1.1      cgd readtimes(quplist, infd)
    563       1.1      cgd 	struct quotause *quplist;
    564       1.1      cgd 	int infd;
    565       1.1      cgd {
    566      1.15    lukem 	struct quotause *qup;
    567       1.1      cgd 	FILE *fd;
    568       1.1      cgd 	int cnt;
    569      1.15    lukem 	char *cp;
    570      1.15    lukem 	long litime, lbtime;
    571       1.1      cgd 	time_t itime, btime, iseconds, bseconds;
    572       1.1      cgd 	char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
    573       1.1      cgd 
    574      1.14   kleink 	(void)lseek(infd, (off_t)0, SEEK_SET);
    575       1.1      cgd 	fd = fdopen(dup(infd), "r");
    576       1.1      cgd 	if (fd == NULL) {
    577      1.15    lukem 		warnx("Can't re-read temp file!!");
    578       1.1      cgd 		return (0);
    579       1.1      cgd 	}
    580       1.1      cgd 	/*
    581       1.1      cgd 	 * Discard two title lines, then read lines to process.
    582       1.1      cgd 	 */
    583       1.1      cgd 	(void) fgets(line1, sizeof (line1), fd);
    584       1.1      cgd 	(void) fgets(line1, sizeof (line1), fd);
    585       1.1      cgd 	while (fgets(line1, sizeof (line1), fd) != NULL) {
    586       1.1      cgd 		if ((fsp = strtok(line1, " \t:")) == NULL) {
    587      1.15    lukem 			warnx("%s: bad format", line1);
    588       1.1      cgd 			return (0);
    589       1.1      cgd 		}
    590       1.1      cgd 		if ((cp = strtok((char *)0, "\n")) == NULL) {
    591      1.15    lukem 			warnx("%s: %s: bad format", fsp,
    592       1.1      cgd 			    &fsp[strlen(fsp) + 1]);
    593       1.1      cgd 			return (0);
    594       1.1      cgd 		}
    595       1.1      cgd 		cnt = sscanf(cp,
    596      1.15    lukem 		    " block grace period: %ld %s file grace period: %ld %s",
    597      1.15    lukem 		    &lbtime, bunits, &litime, iunits);
    598       1.1      cgd 		if (cnt != 4) {
    599      1.15    lukem 			warnx("%s:%s: bad format", fsp, cp);
    600       1.1      cgd 			return (0);
    601       1.1      cgd 		}
    602      1.15    lukem 		itime = (time_t)litime;
    603      1.15    lukem 		btime = (time_t)lbtime;
    604       1.1      cgd 		if (cvtatos(btime, bunits, &bseconds) == 0)
    605       1.1      cgd 			return (0);
    606       1.1      cgd 		if (cvtatos(itime, iunits, &iseconds) == 0)
    607       1.1      cgd 			return (0);
    608       1.1      cgd 		for (qup = quplist; qup; qup = qup->next) {
    609       1.1      cgd 			if (strcmp(fsp, qup->fsname))
    610       1.1      cgd 				continue;
    611       1.1      cgd 			qup->dqblk.dqb_btime = bseconds;
    612       1.1      cgd 			qup->dqblk.dqb_itime = iseconds;
    613       1.1      cgd 			qup->flags |= FOUND;
    614       1.1      cgd 			break;
    615       1.1      cgd 		}
    616       1.1      cgd 	}
    617       1.1      cgd 	fclose(fd);
    618       1.1      cgd 	/*
    619       1.1      cgd 	 * reset default grace periods for any filesystems
    620       1.1      cgd 	 * that have not been found.
    621       1.1      cgd 	 */
    622       1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    623       1.1      cgd 		if (qup->flags & FOUND) {
    624       1.1      cgd 			qup->flags &= ~FOUND;
    625       1.1      cgd 			continue;
    626       1.1      cgd 		}
    627       1.1      cgd 		qup->dqblk.dqb_btime = 0;
    628       1.1      cgd 		qup->dqblk.dqb_itime = 0;
    629       1.1      cgd 	}
    630       1.1      cgd 	return (1);
    631       1.1      cgd }
    632       1.1      cgd 
    633       1.1      cgd /*
    634       1.1      cgd  * Convert seconds to ASCII times.
    635       1.1      cgd  */
    636       1.1      cgd char *
    637       1.1      cgd cvtstoa(time)
    638       1.1      cgd 	time_t time;
    639       1.1      cgd {
    640       1.1      cgd 	static char buf[20];
    641       1.1      cgd 
    642       1.1      cgd 	if (time % (24 * 60 * 60) == 0) {
    643       1.1      cgd 		time /= 24 * 60 * 60;
    644      1.15    lukem 		snprintf(buf, sizeof buf, "%ld day%s", (long)time,
    645      1.15    lukem 		    time == 1 ? "" : "s");
    646       1.1      cgd 	} else if (time % (60 * 60) == 0) {
    647       1.1      cgd 		time /= 60 * 60;
    648      1.15    lukem 		sprintf(buf, "%ld hour%s", (long)time, time == 1 ? "" : "s");
    649       1.1      cgd 	} else if (time % 60 == 0) {
    650       1.1      cgd 		time /= 60;
    651      1.15    lukem 		sprintf(buf, "%ld minute%s", (long)time, time == 1 ? "" : "s");
    652       1.1      cgd 	} else
    653      1.15    lukem 		sprintf(buf, "%ld second%s", (long)time, time == 1 ? "" : "s");
    654       1.1      cgd 	return (buf);
    655       1.1      cgd }
    656       1.1      cgd 
    657       1.1      cgd /*
    658       1.1      cgd  * Convert ASCII input times to seconds.
    659       1.1      cgd  */
    660      1.12    mikel int
    661       1.1      cgd cvtatos(time, units, seconds)
    662       1.1      cgd 	time_t time;
    663       1.1      cgd 	char *units;
    664       1.1      cgd 	time_t *seconds;
    665       1.1      cgd {
    666       1.1      cgd 
    667      1.15    lukem 	if (memcmp(units, "second", 6) == 0)
    668       1.1      cgd 		*seconds = time;
    669      1.15    lukem 	else if (memcmp(units, "minute", 6) == 0)
    670       1.1      cgd 		*seconds = time * 60;
    671      1.15    lukem 	else if (memcmp(units, "hour", 4) == 0)
    672       1.1      cgd 		*seconds = time * 60 * 60;
    673      1.15    lukem 	else if (memcmp(units, "day", 3) == 0)
    674       1.1      cgd 		*seconds = time * 24 * 60 * 60;
    675       1.1      cgd 	else {
    676       1.1      cgd 		printf("%s: bad units, specify %s\n", units,
    677       1.1      cgd 		    "days, hours, minutes, or seconds");
    678       1.1      cgd 		return (0);
    679       1.1      cgd 	}
    680       1.1      cgd 	return (1);
    681       1.1      cgd }
    682       1.1      cgd 
    683       1.1      cgd /*
    684       1.1      cgd  * Free a list of quotause structures.
    685       1.1      cgd  */
    686      1.12    mikel void
    687       1.1      cgd freeprivs(quplist)
    688       1.1      cgd 	struct quotause *quplist;
    689       1.1      cgd {
    690      1.15    lukem 	struct quotause *qup, *nextqup;
    691       1.1      cgd 
    692       1.1      cgd 	for (qup = quplist; qup; qup = nextqup) {
    693       1.1      cgd 		nextqup = qup->next;
    694       1.1      cgd 		free(qup);
    695       1.1      cgd 	}
    696       1.1      cgd }
    697       1.1      cgd 
    698       1.1      cgd /*
    699       1.1      cgd  * Check whether a string is completely composed of digits.
    700       1.1      cgd  */
    701      1.12    mikel int
    702       1.1      cgd alldigits(s)
    703      1.15    lukem 	char *s;
    704       1.1      cgd {
    705      1.15    lukem 	int c;
    706       1.1      cgd 
    707       1.1      cgd 	c = *s++;
    708       1.1      cgd 	do {
    709       1.1      cgd 		if (!isdigit(c))
    710       1.1      cgd 			return (0);
    711      1.15    lukem 	} while ((c = *s++) != 0);
    712       1.1      cgd 	return (1);
    713       1.1      cgd }
    714       1.1      cgd 
    715       1.1      cgd /*
    716       1.1      cgd  * Check to see if a particular quota is to be enabled.
    717       1.1      cgd  */
    718      1.12    mikel int
    719       1.1      cgd hasquota(fs, type, qfnamep)
    720      1.15    lukem 	struct fstab *fs;
    721       1.1      cgd 	int type;
    722       1.1      cgd 	char **qfnamep;
    723       1.1      cgd {
    724      1.15    lukem 	char *opt;
    725      1.15    lukem 	char *cp;
    726       1.1      cgd 	static char initname, usrname[100], grpname[100];
    727       1.1      cgd 	static char buf[BUFSIZ];
    728       1.1      cgd 
    729       1.1      cgd 	if (!initname) {
    730       1.1      cgd 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    731       1.1      cgd 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    732       1.1      cgd 		initname = 1;
    733       1.1      cgd 	}
    734       1.1      cgd 	strcpy(buf, fs->fs_mntops);
    735  1.15.2.1      mrg 	cp = NULL;
    736       1.1      cgd 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    737      1.15    lukem 		if ((cp = strchr(opt, '=')) != NULL)
    738       1.1      cgd 			*cp++ = '\0';
    739       1.1      cgd 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    740       1.1      cgd 			break;
    741       1.1      cgd 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    742       1.1      cgd 			break;
    743       1.1      cgd 	}
    744       1.1      cgd 	if (!opt)
    745       1.1      cgd 		return (0);
    746       1.1      cgd 	if (cp) {
    747       1.1      cgd 		*qfnamep = cp;
    748       1.1      cgd 		return (1);
    749       1.1      cgd 	}
    750       1.1      cgd 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    751       1.1      cgd 	*qfnamep = buf;
    752       1.1      cgd 	return (1);
    753       1.1      cgd }
    754