Home | History | Annotate | Line # | Download | only in quota
quota.c revision 1.26
      1  1.26      agc /*	$NetBSD: quota.c,v 1.26 2003/08/07 11:15:34 agc Exp $	*/
      2  1.12      tls 
      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.26      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.16    lukem #include <sys/cdefs.h>
     36   1.1      cgd #ifndef lint
     37  1.16    lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
     38  1.16    lukem 	The Regents of the University of California.  All rights reserved.\n");
     39   1.1      cgd #endif /* not lint */
     40   1.1      cgd 
     41   1.1      cgd #ifndef lint
     42  1.15      mrg #if 0
     43  1.15      mrg static char sccsid[] = "@(#)quota.c	8.4 (Berkeley) 4/28/95";
     44  1.15      mrg #else
     45  1.26      agc __RCSID("$NetBSD: quota.c,v 1.26 2003/08/07 11:15:34 agc Exp $");
     46  1.15      mrg #endif
     47   1.1      cgd #endif /* not lint */
     48   1.1      cgd 
     49   1.1      cgd /*
     50   1.1      cgd  * Disk quota reporting program.
     51   1.1      cgd  */
     52   1.1      cgd #include <sys/param.h>
     53   1.6  deraadt #include <sys/types.h>
     54   1.1      cgd #include <sys/file.h>
     55   1.1      cgd #include <sys/stat.h>
     56   1.6  deraadt #include <sys/mount.h>
     57   1.6  deraadt #include <sys/socket.h>
     58  1.15      mrg #include <sys/queue.h>
     59  1.15      mrg 
     60   1.5  mycroft #include <ufs/ufs/quota.h>
     61  1.15      mrg #include <ctype.h>
     62  1.16    lukem #include <err.h>
     63  1.15      mrg #include <errno.h>
     64  1.15      mrg #include <fstab.h>
     65  1.15      mrg #include <grp.h>
     66  1.15      mrg #include <netdb.h>
     67  1.15      mrg #include <pwd.h>
     68   1.1      cgd #include <stdio.h>
     69   1.7      cgd #include <stdlib.h>
     70  1.16    lukem #include <string.h>
     71  1.18   kleink #include <time.h>
     72  1.16    lukem #include <unistd.h>
     73   1.1      cgd 
     74   1.6  deraadt #include <rpc/rpc.h>
     75   1.6  deraadt #include <rpc/pmap_prot.h>
     76   1.6  deraadt #include <rpcsvc/rquota.h>
     77   1.6  deraadt 
     78   1.1      cgd char *qfname = QUOTAFILENAME;
     79   1.1      cgd char *qfextension[] = INITQFNAMES;
     80   1.1      cgd 
     81   1.1      cgd struct quotause {
     82   1.1      cgd 	struct	quotause *next;
     83   1.1      cgd 	long	flags;
     84   1.1      cgd 	struct	dqblk dqblk;
     85   1.1      cgd 	char	fsname[MAXPATHLEN + 1];
     86   1.6  deraadt };
     87   1.1      cgd #define	FOUND	0x01
     88   1.1      cgd 
     89  1.16    lukem int	alldigits __P((char *));
     90  1.16    lukem int	callaurpc __P((char *, int, int, int, xdrproc_t, void *,
     91  1.16    lukem 	    xdrproc_t, void *));
     92  1.16    lukem int	main __P((int, char **));
     93  1.16    lukem int	getnfsquota __P((struct statfs *, struct fstab *, struct quotause *,
     94  1.16    lukem 	    long, int));
     95  1.16    lukem struct quotause	*getprivs __P((long id, int quotatype));
     96  1.16    lukem int	getufsquota __P((struct statfs *, struct fstab *, struct quotause *,
     97  1.16    lukem 	    long, int));
     98  1.20  mycroft void	heading __P((int, u_long, const char *, const char *));
     99  1.16    lukem void	showgid __P((gid_t));
    100  1.20  mycroft void	showgrpname __P((const char *));
    101  1.20  mycroft void	showquotas __P((int, u_long, const char *));
    102  1.16    lukem void	showuid __P((uid_t));
    103  1.20  mycroft void	showusrname __P((const char *));
    104  1.16    lukem char   *timeprt __P((time_t seconds));
    105  1.16    lukem int	ufshasquota __P((struct fstab *, int, char **));
    106  1.16    lukem void	usage __P((void));
    107   1.6  deraadt 
    108   1.1      cgd int	qflag;
    109   1.1      cgd int	vflag;
    110  1.19      mrg uid_t	myuid;
    111   1.1      cgd 
    112  1.16    lukem int
    113   1.1      cgd main(argc, argv)
    114  1.16    lukem 	int argc;
    115   1.1      cgd 	char *argv[];
    116   1.1      cgd {
    117   1.3      jtc 	int ngroups;
    118   1.8  mycroft 	gid_t mygid, gidset[NGROUPS];
    119   1.1      cgd 	int i, gflag = 0, uflag = 0;
    120  1.11     mark 	int ch;
    121   1.1      cgd 
    122  1.19      mrg 	myuid = getuid();
    123  1.11     mark 	while ((ch = getopt(argc, argv, "ugvq")) != -1) {
    124   1.1      cgd 		switch(ch) {
    125   1.1      cgd 		case 'g':
    126   1.1      cgd 			gflag++;
    127   1.1      cgd 			break;
    128   1.1      cgd 		case 'u':
    129   1.1      cgd 			uflag++;
    130   1.1      cgd 			break;
    131   1.1      cgd 		case 'v':
    132   1.1      cgd 			vflag++;
    133   1.1      cgd 			break;
    134   1.1      cgd 		case 'q':
    135   1.1      cgd 			qflag++;
    136   1.1      cgd 			break;
    137   1.1      cgd 		default:
    138   1.1      cgd 			usage();
    139   1.1      cgd 		}
    140   1.1      cgd 	}
    141   1.1      cgd 	argc -= optind;
    142   1.1      cgd 	argv += optind;
    143   1.1      cgd 	if (!uflag && !gflag)
    144   1.1      cgd 		uflag++;
    145   1.1      cgd 	if (argc == 0) {
    146   1.1      cgd 		if (uflag)
    147  1.19      mrg 			showuid(myuid);
    148   1.1      cgd 		if (gflag) {
    149   1.8  mycroft 			mygid = getgid();
    150   1.1      cgd 			ngroups = getgroups(NGROUPS, gidset);
    151  1.16    lukem 			if (ngroups < 0)
    152  1.16    lukem 				err(1, "getgroups");
    153   1.8  mycroft 			showgid(mygid);
    154   1.8  mycroft 			for (i = 0; i < ngroups; i++)
    155   1.8  mycroft 				if (gidset[i] != mygid)
    156   1.8  mycroft 					showgid(gidset[i]);
    157   1.1      cgd 		}
    158   1.1      cgd 		exit(0);
    159   1.1      cgd 	}
    160   1.1      cgd 	if (uflag && gflag)
    161   1.1      cgd 		usage();
    162   1.1      cgd 	if (uflag) {
    163   1.1      cgd 		for (; argc > 0; argc--, argv++) {
    164   1.1      cgd 			if (alldigits(*argv))
    165   1.1      cgd 				showuid(atoi(*argv));
    166   1.1      cgd 			else
    167   1.1      cgd 				showusrname(*argv);
    168   1.1      cgd 		}
    169   1.1      cgd 		exit(0);
    170   1.1      cgd 	}
    171   1.1      cgd 	if (gflag) {
    172   1.1      cgd 		for (; argc > 0; argc--, argv++) {
    173   1.1      cgd 			if (alldigits(*argv))
    174   1.1      cgd 				showgid(atoi(*argv));
    175   1.1      cgd 			else
    176   1.1      cgd 				showgrpname(*argv);
    177   1.1      cgd 		}
    178   1.1      cgd 		exit(0);
    179   1.1      cgd 	}
    180  1.16    lukem 	/* NOTREACHED */
    181  1.16    lukem 	return (0);
    182   1.1      cgd }
    183   1.1      cgd 
    184  1.16    lukem void
    185   1.1      cgd usage()
    186   1.1      cgd {
    187   1.1      cgd 
    188   1.1      cgd 	fprintf(stderr, "%s\n%s\n%s\n",
    189  1.22   bouyer 	    "Usage: quota [-guqv]",
    190  1.22   bouyer 	    "\tquota [-qv] -u username ...",
    191  1.22   bouyer 	    "\tquota [-qv] -g groupname ...");
    192   1.1      cgd 	exit(1);
    193   1.1      cgd }
    194   1.1      cgd 
    195   1.1      cgd /*
    196   1.1      cgd  * Print out quotas for a specified user identifier.
    197   1.1      cgd  */
    198  1.16    lukem void
    199   1.1      cgd showuid(uid)
    200  1.16    lukem 	uid_t uid;
    201   1.1      cgd {
    202   1.1      cgd 	struct passwd *pwd = getpwuid(uid);
    203  1.20  mycroft 	const char *name;
    204   1.1      cgd 
    205   1.1      cgd 	if (pwd == NULL)
    206   1.1      cgd 		name = "(no account)";
    207   1.1      cgd 	else
    208   1.1      cgd 		name = pwd->pw_name;
    209   1.1      cgd 	if (uid != myuid && myuid != 0) {
    210   1.1      cgd 		printf("quota: %s (uid %d): permission denied\n", name, uid);
    211   1.1      cgd 		return;
    212   1.1      cgd 	}
    213   1.1      cgd 	showquotas(USRQUOTA, uid, name);
    214   1.1      cgd }
    215   1.1      cgd 
    216   1.1      cgd /*
    217  1.24      wiz  * Print out quotas for a specified user name.
    218   1.1      cgd  */
    219  1.16    lukem void
    220   1.1      cgd showusrname(name)
    221  1.20  mycroft 	const char *name;
    222   1.1      cgd {
    223   1.1      cgd 	struct passwd *pwd = getpwnam(name);
    224   1.1      cgd 
    225   1.1      cgd 	if (pwd == NULL) {
    226  1.16    lukem 		warnx("%s: unknown user", name);
    227   1.1      cgd 		return;
    228   1.1      cgd 	}
    229   1.1      cgd 	if (pwd->pw_uid != myuid && myuid != 0) {
    230  1.16    lukem 		warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
    231   1.1      cgd 		return;
    232   1.1      cgd 	}
    233   1.1      cgd 	showquotas(USRQUOTA, pwd->pw_uid, name);
    234   1.1      cgd }
    235   1.1      cgd 
    236   1.1      cgd /*
    237   1.1      cgd  * Print out quotas for a specified group identifier.
    238   1.1      cgd  */
    239  1.16    lukem void
    240   1.1      cgd showgid(gid)
    241  1.16    lukem 	gid_t gid;
    242   1.1      cgd {
    243   1.1      cgd 	struct group *grp = getgrgid(gid);
    244   1.3      jtc 	int ngroups;
    245   1.8  mycroft 	gid_t mygid, gidset[NGROUPS];
    246  1.16    lukem 	int i;
    247  1.20  mycroft 	const char *name;
    248   1.1      cgd 
    249   1.1      cgd 	if (grp == NULL)
    250   1.1      cgd 		name = "(no entry)";
    251   1.1      cgd 	else
    252   1.1      cgd 		name = grp->gr_name;
    253   1.8  mycroft 	mygid = getgid();
    254   1.1      cgd 	ngroups = getgroups(NGROUPS, gidset);
    255   1.1      cgd 	if (ngroups < 0) {
    256  1.16    lukem 		warn("getgroups");
    257   1.1      cgd 		return;
    258   1.1      cgd 	}
    259   1.8  mycroft 	if (gid != mygid) {
    260   1.8  mycroft 		for (i = 0; i < ngroups; i++)
    261   1.8  mycroft 			if (gid == gidset[i])
    262   1.8  mycroft 				break;
    263  1.19      mrg 		if (i >= ngroups && myuid != 0) {
    264  1.16    lukem 			warnx("%s (gid %d): permission denied", name, gid);
    265   1.8  mycroft 			return;
    266   1.8  mycroft 		}
    267   1.1      cgd 	}
    268   1.1      cgd 	showquotas(GRPQUOTA, gid, name);
    269   1.1      cgd }
    270   1.1      cgd 
    271   1.1      cgd /*
    272  1.24      wiz  * Print out quotas for a specified group name.
    273   1.1      cgd  */
    274  1.16    lukem void
    275   1.1      cgd showgrpname(name)
    276  1.20  mycroft 	const char *name;
    277   1.1      cgd {
    278   1.1      cgd 	struct group *grp = getgrnam(name);
    279   1.3      jtc 	int ngroups;
    280   1.8  mycroft 	gid_t mygid, gidset[NGROUPS];
    281  1.16    lukem 	int i;
    282   1.1      cgd 
    283   1.1      cgd 	if (grp == NULL) {
    284  1.16    lukem 		warnx("%s: unknown group", name);
    285   1.1      cgd 		return;
    286   1.1      cgd 	}
    287   1.8  mycroft 	mygid = getgid();
    288   1.1      cgd 	ngroups = getgroups(NGROUPS, gidset);
    289   1.1      cgd 	if (ngroups < 0) {
    290  1.16    lukem 		warn("getgroups");
    291   1.1      cgd 		return;
    292   1.1      cgd 	}
    293   1.8  mycroft 	if (grp->gr_gid != mygid) {
    294   1.8  mycroft 		for (i = 0; i < ngroups; i++)
    295   1.8  mycroft 			if (grp->gr_gid == gidset[i])
    296   1.8  mycroft 				break;
    297  1.19      mrg 		if (i >= ngroups && myuid != 0) {
    298  1.16    lukem 			warnx("%s (gid %d): permission denied",
    299   1.8  mycroft 			    name, grp->gr_gid);
    300   1.8  mycroft 			return;
    301   1.8  mycroft 		}
    302   1.1      cgd 	}
    303   1.1      cgd 	showquotas(GRPQUOTA, grp->gr_gid, name);
    304   1.1      cgd }
    305   1.1      cgd 
    306  1.16    lukem void
    307   1.1      cgd showquotas(type, id, name)
    308   1.1      cgd 	int type;
    309   1.1      cgd 	u_long id;
    310  1.20  mycroft 	const char *name;
    311   1.1      cgd {
    312  1.16    lukem 	struct quotause *qup;
    313   1.6  deraadt 	struct quotause *quplist;
    314   1.6  deraadt 	char *msgi, *msgb, *nam;
    315  1.16    lukem 	int lines = 0;
    316   1.1      cgd 	static time_t now;
    317   1.1      cgd 
    318   1.1      cgd 	if (now == 0)
    319   1.1      cgd 		time(&now);
    320   1.1      cgd 	quplist = getprivs(id, type);
    321   1.1      cgd 	for (qup = quplist; qup; qup = qup->next) {
    322   1.1      cgd 		if (!vflag &&
    323   1.1      cgd 		    qup->dqblk.dqb_isoftlimit == 0 &&
    324   1.1      cgd 		    qup->dqblk.dqb_ihardlimit == 0 &&
    325   1.1      cgd 		    qup->dqblk.dqb_bsoftlimit == 0 &&
    326   1.1      cgd 		    qup->dqblk.dqb_bhardlimit == 0)
    327   1.1      cgd 			continue;
    328   1.1      cgd 		msgi = (char *)0;
    329   1.1      cgd 		if (qup->dqblk.dqb_ihardlimit &&
    330   1.1      cgd 		    qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
    331   1.1      cgd 			msgi = "File limit reached on";
    332   1.1      cgd 		else if (qup->dqblk.dqb_isoftlimit &&
    333  1.21     ross 		    qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
    334   1.1      cgd 			if (qup->dqblk.dqb_itime > now)
    335   1.1      cgd 				msgi = "In file grace period on";
    336   1.1      cgd 			else
    337   1.1      cgd 				msgi = "Over file quota on";
    338  1.21     ross 		}
    339   1.1      cgd 		msgb = (char *)0;
    340   1.1      cgd 		if (qup->dqblk.dqb_bhardlimit &&
    341   1.1      cgd 		    qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
    342   1.1      cgd 			msgb = "Block limit reached on";
    343  1.21     ross 		else {
    344  1.21     ross 			if (qup->dqblk.dqb_bsoftlimit
    345  1.21     ross 			&&  qup->dqblk.dqb_curblocks
    346  1.21     ross 			    >= qup->dqblk.dqb_bsoftlimit) {
    347  1.21     ross 				if (qup->dqblk.dqb_btime > now)
    348  1.21     ross 					msgb = "In block grace period on";
    349  1.22   bouyer 				else
    350  1.22   bouyer 					msgb = "Over block quota on";
    351  1.21     ross 			}
    352  1.21     ross 		}
    353   1.1      cgd 		if (qflag) {
    354   1.1      cgd 			if ((msgi != (char *)0 || msgb != (char *)0) &&
    355   1.1      cgd 			    lines++ == 0)
    356   1.1      cgd 				heading(type, id, name, "");
    357   1.1      cgd 			if (msgi != (char *)0)
    358   1.1      cgd 				printf("\t%s %s\n", msgi, qup->fsname);
    359   1.1      cgd 			if (msgb != (char *)0)
    360   1.1      cgd 				printf("\t%s %s\n", msgb, qup->fsname);
    361   1.1      cgd 			continue;
    362   1.1      cgd 		}
    363   1.1      cgd 		if (vflag ||
    364   1.1      cgd 		    qup->dqblk.dqb_curblocks ||
    365   1.1      cgd 		    qup->dqblk.dqb_curinodes) {
    366   1.1      cgd 			if (lines++ == 0)
    367   1.1      cgd 				heading(type, id, name, "");
    368   1.6  deraadt 			nam = qup->fsname;
    369   1.6  deraadt 			if (strlen(qup->fsname) > 15) {
    370   1.6  deraadt 				printf("%s\n", qup->fsname);
    371   1.6  deraadt 				nam = "";
    372   1.6  deraadt 			}
    373  1.22   bouyer 			printf("%12s%9d%c%8d%9d%8s"
    374  1.22   bouyer 			    , nam
    375  1.22   bouyer 			    , (int)(dbtob((u_quad_t)qup->dqblk.dqb_curblocks)
    376  1.22   bouyer 				/ 1024)
    377  1.22   bouyer 			    , (msgb == (char *)0) ? ' ' : '*'
    378  1.22   bouyer 			    , (int)(dbtob((u_quad_t)qup->dqblk.dqb_bsoftlimit)
    379  1.22   bouyer 				/ 1024)
    380  1.22   bouyer 			    , (int)(dbtob((u_quad_t)qup->dqblk.dqb_bhardlimit)
    381  1.22   bouyer 				/ 1024)
    382  1.22   bouyer 			    , (msgb == (char *)0) ? ""
    383  1.22   bouyer 			        : timeprt(qup->dqblk.dqb_btime));
    384   1.1      cgd 			printf("%8d%c%7d%8d%8s\n"
    385  1.22   bouyer 			    , qup->dqblk.dqb_curinodes
    386  1.22   bouyer 			    , (msgi == (char *)0) ? ' ' : '*'
    387  1.22   bouyer 			    , qup->dqblk.dqb_isoftlimit
    388  1.22   bouyer 			    , qup->dqblk.dqb_ihardlimit
    389  1.22   bouyer 			    , (msgi == (char *)0) ? ""
    390  1.22   bouyer 			        : timeprt(qup->dqblk.dqb_itime)
    391   1.1      cgd 			);
    392   1.1      cgd 			continue;
    393   1.1      cgd 		}
    394   1.1      cgd 	}
    395   1.1      cgd 	if (!qflag && lines == 0)
    396   1.1      cgd 		heading(type, id, name, "none");
    397   1.1      cgd }
    398   1.1      cgd 
    399  1.16    lukem void
    400   1.1      cgd heading(type, id, name, tag)
    401   1.1      cgd 	int type;
    402   1.1      cgd 	u_long id;
    403  1.20  mycroft 	const char *name, *tag;
    404   1.1      cgd {
    405   1.1      cgd 
    406  1.16    lukem 	printf("Disk quotas for %s %s (%cid %ld): %s\n", qfextension[type],
    407  1.16    lukem 	    name, *qfextension[type], (u_long)id, tag);
    408   1.1      cgd 	if (!qflag && tag[0] == '\0') {
    409  1.22   bouyer 		printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
    410  1.22   bouyer 		    , "Filesystem"
    411  1.22   bouyer 		    , "blocks"
    412  1.22   bouyer 		    , "quota"
    413  1.22   bouyer 		    , "limit"
    414  1.22   bouyer 		    , "grace"
    415  1.22   bouyer 		    , "files"
    416  1.22   bouyer 		    , "quota"
    417  1.22   bouyer 		    , "limit"
    418  1.22   bouyer 		    , "grace"
    419   1.1      cgd 		);
    420   1.1      cgd 	}
    421   1.1      cgd }
    422   1.1      cgd 
    423   1.1      cgd /*
    424   1.1      cgd  * Calculate the grace period and return a printable string for it.
    425   1.1      cgd  */
    426   1.1      cgd char *
    427   1.1      cgd timeprt(seconds)
    428   1.1      cgd 	time_t seconds;
    429   1.1      cgd {
    430   1.1      cgd 	time_t hours, minutes;
    431   1.1      cgd 	static char buf[20];
    432   1.1      cgd 	static time_t now;
    433   1.1      cgd 
    434   1.1      cgd 	if (now == 0)
    435   1.1      cgd 		time(&now);
    436   1.1      cgd 	if (now > seconds)
    437   1.1      cgd 		return ("none");
    438   1.1      cgd 	seconds -= now;
    439   1.1      cgd 	minutes = (seconds + 30) / 60;
    440   1.1      cgd 	hours = (minutes + 30) / 60;
    441   1.1      cgd 	if (hours >= 36) {
    442  1.16    lukem 		(void)snprintf(buf, sizeof buf, "%ddays",
    443  1.16    lukem 		    (int)((hours + 12) / 24));
    444   1.1      cgd 		return (buf);
    445   1.1      cgd 	}
    446   1.1      cgd 	if (minutes >= 60) {
    447  1.16    lukem 		(void)snprintf(buf, sizeof buf, "%2d:%d",
    448  1.16    lukem 		    (int)(minutes / 60), (int)(minutes % 60));
    449   1.1      cgd 		return (buf);
    450   1.1      cgd 	}
    451  1.16    lukem 	(void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
    452   1.1      cgd 	return (buf);
    453   1.1      cgd }
    454   1.1      cgd 
    455   1.1      cgd /*
    456   1.1      cgd  * Collect the requested quota information.
    457   1.1      cgd  */
    458   1.1      cgd struct quotause *
    459   1.1      cgd getprivs(id, quotatype)
    460  1.16    lukem 	long id;
    461   1.1      cgd 	int quotatype;
    462   1.1      cgd {
    463  1.16    lukem 	struct quotause *qup, *quptail;
    464  1.16    lukem 	struct fstab *fs;
    465   1.1      cgd 	struct quotause *quphead;
    466   1.6  deraadt 	struct statfs *fst;
    467   1.6  deraadt 	int nfst, i;
    468   1.6  deraadt 
    469  1.16    lukem 	qup = quphead = quptail = NULL;
    470   1.1      cgd 
    471   1.6  deraadt 	nfst = getmntinfo(&fst, MNT_WAIT);
    472  1.16    lukem 	if (nfst == 0)
    473  1.16    lukem 		errx(2, "no filesystems mounted!");
    474   1.1      cgd 	setfsent();
    475  1.19      mrg 	for (i = 0; i < nfst; i++) {
    476   1.6  deraadt 		if (qup == NULL) {
    477  1.16    lukem 			if ((qup =
    478  1.16    lukem 			    (struct quotause *)malloc(sizeof *qup)) == NULL)
    479  1.16    lukem 				errx(2, "out of memory");
    480   1.1      cgd 		}
    481   1.9      cgd 		if (strncmp(fst[i].f_fstypename, "nfs", MFSNAMELEN) == 0) {
    482   1.6  deraadt 			if (getnfsquota(&fst[i], NULL, qup, id, quotatype) == 0)
    483   1.6  deraadt 				continue;
    484  1.10      jtc 		} else if (strncmp(fst[i].f_fstypename, "ffs",
    485   1.9      cgd 		    MFSNAMELEN) == 0) {
    486   1.6  deraadt 			/*
    487   1.6  deraadt 			 * XXX
    488   1.6  deraadt 			 * UFS filesystems must be in /etc/fstab, and must
    489   1.6  deraadt 			 * indicate that they have quotas on (?!) This is quite
    490   1.6  deraadt 			 * unlike SunOS where quotas can be enabled/disabled
    491   1.6  deraadt 			 * on a filesystem independent of /etc/fstab, and it
    492   1.6  deraadt 			 * will still print quotas for them.
    493   1.6  deraadt 			 */
    494   1.6  deraadt 			if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
    495   1.1      cgd 				continue;
    496   1.6  deraadt 			if (getufsquota(&fst[i], fs, qup, id, quotatype) == 0)
    497   1.1      cgd 				continue;
    498   1.6  deraadt 		} else
    499   1.6  deraadt 			continue;
    500  1.13      mrg 		(void)strncpy(qup->fsname, fst[i].f_mntonname,
    501  1.13      mrg 		    sizeof(qup->fsname) - 1);
    502   1.1      cgd 		if (quphead == NULL)
    503   1.1      cgd 			quphead = qup;
    504   1.1      cgd 		else
    505   1.1      cgd 			quptail->next = qup;
    506   1.1      cgd 		quptail = qup;
    507   1.6  deraadt 		quptail->next = 0;
    508   1.6  deraadt 		qup = NULL;
    509   1.1      cgd 	}
    510   1.6  deraadt 	if (qup)
    511   1.6  deraadt 		free(qup);
    512   1.1      cgd 	endfsent();
    513   1.1      cgd 	return (quphead);
    514   1.1      cgd }
    515   1.1      cgd 
    516   1.1      cgd /*
    517   1.1      cgd  * Check to see if a particular quota is to be enabled.
    518   1.1      cgd  */
    519  1.16    lukem int
    520   1.6  deraadt ufshasquota(fs, type, qfnamep)
    521  1.16    lukem 	struct fstab *fs;
    522   1.1      cgd 	int type;
    523   1.1      cgd 	char **qfnamep;
    524   1.1      cgd {
    525   1.1      cgd 	static char initname, usrname[100], grpname[100];
    526   1.1      cgd 	static char buf[BUFSIZ];
    527   1.6  deraadt 	char *opt, *cp;
    528   1.1      cgd 
    529  1.17      mrg 	cp = NULL;
    530   1.1      cgd 	if (!initname) {
    531  1.13      mrg 		(void)snprintf(usrname, sizeof usrname, "%s%s",
    532  1.13      mrg 		    qfextension[USRQUOTA], qfname);
    533  1.13      mrg 		(void)snprintf(grpname, sizeof grpname, "%s%s",
    534  1.13      mrg 		    qfextension[GRPQUOTA], qfname);
    535   1.1      cgd 		initname = 1;
    536   1.1      cgd 	}
    537  1.13      mrg 	(void)strncpy(buf, fs->fs_mntops, sizeof(buf) - 1);
    538  1.19      mrg 	buf[sizeof(buf) - 1] = '\0';
    539   1.1      cgd 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    540  1.16    lukem 		if ((cp = strchr(opt, '=')) != NULL)
    541   1.1      cgd 			*cp++ = '\0';
    542   1.1      cgd 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    543   1.1      cgd 			break;
    544   1.1      cgd 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    545   1.1      cgd 			break;
    546   1.1      cgd 	}
    547   1.1      cgd 	if (!opt)
    548   1.1      cgd 		return (0);
    549   1.1      cgd 	if (cp) {
    550   1.1      cgd 		*qfnamep = cp;
    551   1.1      cgd 		return (1);
    552   1.1      cgd 	}
    553  1.13      mrg 	(void)snprintf(buf, sizeof buf, "%s/%s.%s", fs->fs_file, qfname,
    554  1.13      mrg 	    qfextension[type]);
    555   1.1      cgd 	*qfnamep = buf;
    556   1.1      cgd 	return (1);
    557   1.6  deraadt }
    558   1.6  deraadt 
    559   1.6  deraadt int
    560   1.6  deraadt getufsquota(fst, fs, qup, id, quotatype)
    561   1.6  deraadt 	struct statfs *fst;
    562   1.6  deraadt 	struct fstab *fs;
    563   1.6  deraadt 	struct quotause *qup;
    564   1.6  deraadt 	long id;
    565   1.6  deraadt 	int quotatype;
    566   1.6  deraadt {
    567   1.6  deraadt 	char *qfpathname;
    568   1.6  deraadt 	int fd, qcmd;
    569   1.6  deraadt 
    570   1.6  deraadt 	qcmd = QCMD(Q_GETQUOTA, quotatype);
    571   1.6  deraadt 	if (!ufshasquota(fs, quotatype, &qfpathname))
    572   1.6  deraadt 		return (0);
    573   1.6  deraadt 
    574   1.6  deraadt 	if (quotactl(fs->fs_file, qcmd, id, &qup->dqblk) != 0) {
    575   1.6  deraadt 		if ((fd = open(qfpathname, O_RDONLY)) < 0) {
    576  1.16    lukem 			warn("%s", qfpathname);
    577   1.6  deraadt 			return (0);
    578   1.6  deraadt 		}
    579  1.14   kleink 		(void)lseek(fd, (off_t)(id * sizeof(struct dqblk)), SEEK_SET);
    580   1.6  deraadt 		switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
    581   1.6  deraadt 		case 0:				/* EOF */
    582   1.6  deraadt 			/*
    583   1.6  deraadt 			 * Convert implicit 0 quota (EOF)
    584   1.6  deraadt 			 * into an explicit one (zero'ed dqblk)
    585   1.6  deraadt 			 */
    586  1.16    lukem 			memset((caddr_t)&qup->dqblk, 0, sizeof(struct dqblk));
    587   1.6  deraadt 			break;
    588   1.6  deraadt 		case sizeof(struct dqblk):	/* OK */
    589   1.6  deraadt 			break;
    590   1.6  deraadt 		default:		/* ERROR */
    591  1.16    lukem 			warn("read error `%s'", qfpathname);
    592   1.6  deraadt 			close(fd);
    593   1.6  deraadt 			return (0);
    594   1.6  deraadt 		}
    595   1.6  deraadt 		close(fd);
    596   1.6  deraadt 	}
    597   1.6  deraadt 	return (1);
    598   1.6  deraadt }
    599   1.6  deraadt 
    600   1.6  deraadt int
    601   1.6  deraadt getnfsquota(fst, fs, qup, id, quotatype)
    602   1.6  deraadt 	struct statfs *fst;
    603   1.6  deraadt 	struct fstab *fs;
    604   1.6  deraadt 	struct quotause *qup;
    605   1.6  deraadt 	long id;
    606   1.6  deraadt 	int quotatype;
    607   1.6  deraadt {
    608   1.6  deraadt 	struct getquota_args gq_args;
    609  1.25   bouyer 	struct ext_getquota_args ext_gq_args;
    610   1.6  deraadt 	struct getquota_rslt gq_rslt;
    611   1.6  deraadt 	struct dqblk *dqp = &qup->dqblk;
    612   1.6  deraadt 	struct timeval tv;
    613   1.6  deraadt 	char *cp;
    614  1.25   bouyer 	int ret;
    615   1.6  deraadt 
    616   1.6  deraadt 	if (fst->f_flags & MNT_LOCAL)
    617   1.6  deraadt 		return (0);
    618   1.6  deraadt 
    619   1.6  deraadt 	/*
    620   1.6  deraadt 	 * must be some form of "hostname:/path"
    621   1.6  deraadt 	 */
    622   1.6  deraadt 	cp = strchr(fst->f_mntfromname, ':');
    623   1.6  deraadt 	if (cp == NULL) {
    624  1.16    lukem 		warnx("cannot find hostname for %s", fst->f_mntfromname);
    625   1.6  deraadt 		return (0);
    626   1.6  deraadt 	}
    627   1.6  deraadt 
    628   1.6  deraadt 	*cp = '\0';
    629   1.6  deraadt 	if (*(cp+1) != '/') {
    630   1.6  deraadt 		*cp = ':';
    631   1.6  deraadt 		return (0);
    632   1.6  deraadt 	}
    633   1.6  deraadt 
    634  1.25   bouyer 	ext_gq_args.gqa_pathp = cp + 1;
    635  1.25   bouyer 	ext_gq_args.gqa_id = id;
    636  1.25   bouyer 	ext_gq_args.gqa_type =
    637  1.25   bouyer 	    (quotatype == USRQUOTA) ? RQUOTA_USRQUOTA : RQUOTA_GRPQUOTA;
    638  1.25   bouyer 	ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, EXT_RQUOTAVERS,
    639  1.25   bouyer 	    RQUOTAPROC_GETQUOTA, xdr_ext_getquota_args, &ext_gq_args,
    640  1.25   bouyer 	    xdr_getquota_rslt, &gq_rslt);
    641  1.25   bouyer 	if (ret == RPC_VERSMISMATCH) {
    642  1.25   bouyer 		if (quotatype != USRQUOTA) {
    643  1.25   bouyer 			*cp = ':';
    644  1.25   bouyer 			return (0);
    645  1.25   bouyer 		}
    646  1.25   bouyer 		/* try RQUOTAVERS */
    647  1.25   bouyer 		gq_args.gqa_pathp = cp + 1;
    648  1.25   bouyer 		gq_args.gqa_uid = id;
    649  1.25   bouyer 		ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
    650  1.25   bouyer 		    RQUOTAPROC_GETQUOTA, xdr_getquota_args, &gq_args,
    651  1.25   bouyer 			    xdr_getquota_rslt, &gq_rslt);
    652  1.25   bouyer 	}
    653  1.25   bouyer 	if (ret != RPC_SUCCESS) {
    654   1.6  deraadt 		*cp = ':';
    655   1.6  deraadt 		return (0);
    656   1.6  deraadt 	}
    657   1.6  deraadt 
    658   1.6  deraadt 	switch (gq_rslt.status) {
    659   1.6  deraadt 	case Q_NOQUOTA:
    660   1.6  deraadt 		break;
    661   1.6  deraadt 	case Q_EPERM:
    662  1.16    lukem 		warnx("quota permission error, host: %s", fst->f_mntfromname);
    663   1.6  deraadt 		break;
    664   1.6  deraadt 	case Q_OK:
    665   1.6  deraadt 		gettimeofday(&tv, NULL);
    666   1.6  deraadt 			/* blocks*/
    667   1.6  deraadt 		dqp->dqb_bhardlimit =
    668   1.6  deraadt 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
    669  1.23      cgd 		    (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
    670   1.6  deraadt 		dqp->dqb_bsoftlimit =
    671   1.6  deraadt 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
    672  1.23      cgd 		    (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
    673   1.6  deraadt 		dqp->dqb_curblocks =
    674   1.6  deraadt 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
    675  1.23      cgd 		    (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
    676   1.6  deraadt 			/* inodes */
    677   1.6  deraadt 		dqp->dqb_ihardlimit =
    678   1.6  deraadt 			gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
    679   1.6  deraadt 		dqp->dqb_isoftlimit =
    680   1.6  deraadt 			gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
    681   1.6  deraadt 		dqp->dqb_curinodes =
    682   1.6  deraadt 			gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
    683   1.6  deraadt 			/* grace times */
    684   1.6  deraadt 		dqp->dqb_btime =
    685   1.6  deraadt 		    tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
    686   1.6  deraadt 		dqp->dqb_itime =
    687   1.6  deraadt 		    tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
    688   1.6  deraadt 		*cp = ':';
    689   1.6  deraadt 		return (1);
    690   1.6  deraadt 	default:
    691  1.16    lukem 		warnx("bad rpc result, host: %s", fst->f_mntfromname);
    692   1.6  deraadt 		break;
    693   1.6  deraadt 	}
    694   1.6  deraadt 	*cp = ':';
    695   1.6  deraadt 	return (0);
    696   1.6  deraadt }
    697   1.6  deraadt 
    698   1.6  deraadt int
    699   1.6  deraadt callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
    700   1.6  deraadt 	char *host;
    701  1.16    lukem 	int prognum, versnum, procnum;
    702  1.16    lukem 	xdrproc_t inproc;
    703  1.16    lukem 	void *in;
    704  1.16    lukem 	xdrproc_t outproc;
    705  1.16    lukem 	void *out;
    706   1.6  deraadt {
    707   1.6  deraadt 	struct sockaddr_in server_addr;
    708   1.6  deraadt 	enum clnt_stat clnt_stat;
    709   1.6  deraadt 	struct hostent *hp;
    710   1.6  deraadt 	struct timeval timeout, tottimeout;
    711   1.6  deraadt 
    712   1.6  deraadt 	CLIENT *client = NULL;
    713   1.6  deraadt 	int socket = RPC_ANYSOCK;
    714   1.6  deraadt 
    715   1.6  deraadt 	if ((hp = gethostbyname(host)) == NULL)
    716   1.6  deraadt 		return ((int) RPC_UNKNOWNHOST);
    717   1.6  deraadt 	timeout.tv_usec = 0;
    718   1.6  deraadt 	timeout.tv_sec = 6;
    719  1.16    lukem 	memmove(&server_addr.sin_addr, hp->h_addr, hp->h_length);
    720   1.6  deraadt 	server_addr.sin_family = AF_INET;
    721   1.6  deraadt 	server_addr.sin_port =  0;
    722   1.6  deraadt 
    723   1.6  deraadt 	if ((client = clntudp_create(&server_addr, prognum,
    724   1.6  deraadt 	    versnum, timeout, &socket)) == NULL)
    725   1.6  deraadt 		return ((int) rpc_createerr.cf_stat);
    726   1.6  deraadt 
    727   1.6  deraadt 	client->cl_auth = authunix_create_default();
    728   1.6  deraadt 	tottimeout.tv_sec = 25;
    729   1.6  deraadt 	tottimeout.tv_usec = 0;
    730   1.6  deraadt 	clnt_stat = clnt_call(client, procnum, inproc, in,
    731   1.6  deraadt 	    outproc, out, tottimeout);
    732   1.6  deraadt 
    733   1.6  deraadt 	return ((int) clnt_stat);
    734   1.1      cgd }
    735   1.1      cgd 
    736  1.16    lukem int
    737   1.1      cgd alldigits(s)
    738  1.16    lukem 	char *s;
    739   1.1      cgd {
    740  1.16    lukem 	int c;
    741   1.1      cgd 
    742   1.1      cgd 	c = *s++;
    743   1.1      cgd 	do {
    744   1.1      cgd 		if (!isdigit(c))
    745   1.1      cgd 			return (0);
    746  1.16    lukem 	} while ((c = *s++) != 0);
    747   1.1      cgd 	return (1);
    748   1.1      cgd }
    749