Home | History | Annotate | Line # | Download | only in quota
quota.c revision 1.36
      1  1.36  christos /*	$NetBSD: quota.c,v 1.36 2011/03/06 22:36:07 christos 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.32     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
     38  1.32     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42  1.15       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.36  christos __RCSID("$NetBSD: quota.c,v 1.36 2011/03/06 22:36:07 christos 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 
     59  1.15       mrg #include <ctype.h>
     60  1.16     lukem #include <err.h>
     61  1.15       mrg #include <errno.h>
     62  1.15       mrg #include <fstab.h>
     63  1.15       mrg #include <grp.h>
     64  1.15       mrg #include <netdb.h>
     65  1.15       mrg #include <pwd.h>
     66   1.1       cgd #include <stdio.h>
     67   1.7       cgd #include <stdlib.h>
     68  1.16     lukem #include <string.h>
     69  1.18    kleink #include <time.h>
     70  1.16     lukem #include <unistd.h>
     71   1.1       cgd 
     72  1.34    bouyer #include <ufs/ufs/quota2.h>
     73  1.34    bouyer #include <ufs/ufs/quota1.h>
     74  1.34    bouyer 
     75   1.6   deraadt #include <rpc/rpc.h>
     76   1.6   deraadt #include <rpc/pmap_prot.h>
     77   1.6   deraadt #include <rpcsvc/rquota.h>
     78   1.6   deraadt 
     79  1.36  christos #include "printquota.h"
     80  1.36  christos #include "quotautil.h"
     81  1.36  christos #include "getvfsquota.h"
     82   1.1       cgd 
     83   1.1       cgd struct quotause {
     84   1.1       cgd 	struct	quotause *next;
     85   1.1       cgd 	long	flags;
     86  1.34    bouyer 	struct	quota2_entry q2e;
     87   1.1       cgd 	char	fsname[MAXPATHLEN + 1];
     88   1.6   deraadt };
     89   1.1       cgd #define	FOUND	0x01
     90  1.34    bouyer #define	QUOTA2	0x02
     91   1.1       cgd 
     92  1.35  christos static int	callaurpc(const char *, rpcprog_t, rpcvers_t, rpcproc_t,
     93  1.35  christos     xdrproc_t, void *, xdrproc_t, void *);
     94  1.35  christos static int	getnfsquota(struct statvfs *, struct quotause *, uint32_t, int);
     95  1.35  christos static struct quotause	*getprivs(uint32_t, int);
     96  1.35  christos static void	heading(int, uint32_t, const char *, const char *);
     97  1.35  christos static void	showgid(gid_t);
     98  1.35  christos static void	showgrpname(const char *);
     99  1.35  christos static void	showquotas(int, uint32_t, const char *);
    100  1.35  christos static void	showuid(uid_t);
    101  1.35  christos static void	showusrname(const char *);
    102  1.35  christos static void	usage(void) __attribute__((__noreturn__));
    103  1.35  christos 
    104  1.35  christos static int	qflag = 0;
    105  1.35  christos static int	vflag = 0;
    106  1.35  christos static int	hflag = 0;
    107  1.35  christos static int	dflag = 0;
    108  1.35  christos static int	Dflag = 0;
    109  1.35  christos static uid_t	myuid;
    110   1.1       cgd 
    111  1.16     lukem int
    112  1.35  christos main(int argc, char *argv[])
    113   1.1       cgd {
    114   1.3       jtc 	int ngroups;
    115   1.8   mycroft 	gid_t mygid, gidset[NGROUPS];
    116   1.1       cgd 	int i, gflag = 0, uflag = 0;
    117  1.11      mark 	int ch;
    118   1.1       cgd 
    119  1.19       mrg 	myuid = getuid();
    120  1.34    bouyer 	while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
    121   1.1       cgd 		switch(ch) {
    122   1.1       cgd 		case 'g':
    123   1.1       cgd 			gflag++;
    124   1.1       cgd 			break;
    125   1.1       cgd 		case 'u':
    126   1.1       cgd 			uflag++;
    127   1.1       cgd 			break;
    128   1.1       cgd 		case 'v':
    129   1.1       cgd 			vflag++;
    130   1.1       cgd 			break;
    131   1.1       cgd 		case 'q':
    132   1.1       cgd 			qflag++;
    133   1.1       cgd 			break;
    134  1.34    bouyer 		case 'h':
    135  1.34    bouyer 			hflag++;
    136  1.34    bouyer 			break;
    137  1.34    bouyer 		case 'd':
    138  1.34    bouyer 			dflag++;
    139  1.34    bouyer 			break;
    140  1.34    bouyer 		case 'D':
    141  1.34    bouyer 			Dflag++;
    142  1.34    bouyer 			break;
    143   1.1       cgd 		default:
    144   1.1       cgd 			usage();
    145   1.1       cgd 		}
    146   1.1       cgd 	}
    147   1.1       cgd 	argc -= optind;
    148   1.1       cgd 	argv += optind;
    149   1.1       cgd 	if (!uflag && !gflag)
    150   1.1       cgd 		uflag++;
    151  1.34    bouyer 	if (dflag) {
    152  1.34    bouyer #if 0
    153  1.35  christos 		if (myuid != 0)
    154  1.35  christos 			errx(1, "-d: permission denied");
    155  1.34    bouyer #endif
    156  1.34    bouyer 		if (uflag)
    157  1.34    bouyer 			showquotas(USRQUOTA, 0, "");
    158  1.34    bouyer 		if (gflag)
    159  1.34    bouyer 			showquotas(GRPQUOTA, 0, "");
    160  1.35  christos 		return 0;
    161  1.34    bouyer 	}
    162   1.1       cgd 	if (argc == 0) {
    163   1.1       cgd 		if (uflag)
    164  1.19       mrg 			showuid(myuid);
    165   1.1       cgd 		if (gflag) {
    166  1.34    bouyer 			if (dflag)
    167  1.34    bouyer 				showgid(0);
    168  1.34    bouyer 			else {
    169  1.34    bouyer 				mygid = getgid();
    170  1.34    bouyer 				ngroups = getgroups(NGROUPS, gidset);
    171  1.34    bouyer 				if (ngroups < 0)
    172  1.34    bouyer 					err(1, "getgroups");
    173  1.34    bouyer 				showgid(mygid);
    174  1.34    bouyer 				for (i = 0; i < ngroups; i++)
    175  1.34    bouyer 					if (gidset[i] != mygid)
    176  1.34    bouyer 						showgid(gidset[i]);
    177  1.34    bouyer 			}
    178   1.1       cgd 		}
    179  1.35  christos 		return 0;
    180   1.1       cgd 	}
    181   1.1       cgd 	if (uflag && gflag)
    182   1.1       cgd 		usage();
    183   1.1       cgd 	if (uflag) {
    184   1.1       cgd 		for (; argc > 0; argc--, argv++) {
    185   1.1       cgd 			if (alldigits(*argv))
    186  1.35  christos 				showuid((uid_t)atoi(*argv));
    187   1.1       cgd 			else
    188   1.1       cgd 				showusrname(*argv);
    189   1.1       cgd 		}
    190  1.35  christos 		return 0;
    191   1.1       cgd 	}
    192   1.1       cgd 	if (gflag) {
    193   1.1       cgd 		for (; argc > 0; argc--, argv++) {
    194   1.1       cgd 			if (alldigits(*argv))
    195  1.35  christos 				showgid((gid_t)atoi(*argv));
    196   1.1       cgd 			else
    197   1.1       cgd 				showgrpname(*argv);
    198   1.1       cgd 		}
    199  1.35  christos 		return 0;
    200   1.1       cgd 	}
    201  1.16     lukem 	/* NOTREACHED */
    202  1.35  christos 	return 0;
    203   1.1       cgd }
    204   1.1       cgd 
    205  1.35  christos static void
    206  1.35  christos usage(void)
    207   1.1       cgd {
    208  1.35  christos 	const char *p = getprogname();
    209  1.35  christos 	fprintf(stderr, "Usage: %s [-Dhguqv]\n"
    210  1.35  christos 	    "\t%s [-Dhqv] -u username ...\n"
    211  1.35  christos 	    "\t%s [-Dhqv] -g groupname ...\n"
    212  1.35  christos 	    "\t%s -d [-Dhguqv]\n", p, p, p, p);
    213   1.1       cgd 	exit(1);
    214   1.1       cgd }
    215   1.1       cgd 
    216   1.1       cgd /*
    217   1.1       cgd  * Print out quotas for a specified user identifier.
    218   1.1       cgd  */
    219  1.35  christos static void
    220  1.35  christos showuid(uid_t uid)
    221   1.1       cgd {
    222   1.1       cgd 	struct passwd *pwd = getpwuid(uid);
    223  1.20   mycroft 	const char *name;
    224   1.1       cgd 
    225   1.1       cgd 	if (pwd == NULL)
    226   1.1       cgd 		name = "(no account)";
    227   1.1       cgd 	else
    228   1.1       cgd 		name = pwd->pw_name;
    229   1.1       cgd 	if (uid != myuid && myuid != 0) {
    230  1.35  christos 		warnx("%s (uid %d): permission denied", name, uid);
    231   1.1       cgd 		return;
    232   1.1       cgd 	}
    233   1.1       cgd 	showquotas(USRQUOTA, uid, name);
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.1       cgd /*
    237  1.24       wiz  * Print out quotas for a specified user name.
    238   1.1       cgd  */
    239  1.35  christos static void
    240  1.35  christos showusrname(const char *name)
    241   1.1       cgd {
    242   1.1       cgd 	struct passwd *pwd = getpwnam(name);
    243   1.1       cgd 
    244   1.1       cgd 	if (pwd == NULL) {
    245  1.16     lukem 		warnx("%s: unknown user", name);
    246   1.1       cgd 		return;
    247   1.1       cgd 	}
    248   1.1       cgd 	if (pwd->pw_uid != myuid && myuid != 0) {
    249  1.16     lukem 		warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
    250   1.1       cgd 		return;
    251   1.1       cgd 	}
    252   1.1       cgd 	showquotas(USRQUOTA, pwd->pw_uid, name);
    253   1.1       cgd }
    254   1.1       cgd 
    255   1.1       cgd /*
    256   1.1       cgd  * Print out quotas for a specified group identifier.
    257   1.1       cgd  */
    258  1.35  christos static void
    259  1.35  christos showgid(gid_t gid)
    260   1.1       cgd {
    261   1.1       cgd 	struct group *grp = getgrgid(gid);
    262   1.3       jtc 	int ngroups;
    263   1.8   mycroft 	gid_t mygid, gidset[NGROUPS];
    264  1.16     lukem 	int i;
    265  1.20   mycroft 	const char *name;
    266   1.1       cgd 
    267   1.1       cgd 	if (grp == NULL)
    268   1.1       cgd 		name = "(no entry)";
    269   1.1       cgd 	else
    270   1.1       cgd 		name = grp->gr_name;
    271   1.8   mycroft 	mygid = getgid();
    272   1.1       cgd 	ngroups = getgroups(NGROUPS, gidset);
    273   1.1       cgd 	if (ngroups < 0) {
    274  1.16     lukem 		warn("getgroups");
    275   1.1       cgd 		return;
    276   1.1       cgd 	}
    277   1.8   mycroft 	if (gid != mygid) {
    278   1.8   mycroft 		for (i = 0; i < ngroups; i++)
    279   1.8   mycroft 			if (gid == gidset[i])
    280   1.8   mycroft 				break;
    281  1.19       mrg 		if (i >= ngroups && myuid != 0) {
    282  1.16     lukem 			warnx("%s (gid %d): permission denied", name, gid);
    283   1.8   mycroft 			return;
    284   1.8   mycroft 		}
    285   1.1       cgd 	}
    286   1.1       cgd 	showquotas(GRPQUOTA, gid, name);
    287   1.1       cgd }
    288   1.1       cgd 
    289   1.1       cgd /*
    290  1.24       wiz  * Print out quotas for a specified group name.
    291   1.1       cgd  */
    292  1.35  christos static void
    293  1.35  christos showgrpname(const char *name)
    294   1.1       cgd {
    295   1.1       cgd 	struct group *grp = getgrnam(name);
    296   1.3       jtc 	int ngroups;
    297   1.8   mycroft 	gid_t mygid, gidset[NGROUPS];
    298  1.16     lukem 	int i;
    299   1.1       cgd 
    300   1.1       cgd 	if (grp == NULL) {
    301  1.16     lukem 		warnx("%s: unknown group", name);
    302   1.1       cgd 		return;
    303   1.1       cgd 	}
    304   1.8   mycroft 	mygid = getgid();
    305   1.1       cgd 	ngroups = getgroups(NGROUPS, gidset);
    306   1.1       cgd 	if (ngroups < 0) {
    307  1.16     lukem 		warn("getgroups");
    308   1.1       cgd 		return;
    309   1.1       cgd 	}
    310   1.8   mycroft 	if (grp->gr_gid != mygid) {
    311   1.8   mycroft 		for (i = 0; i < ngroups; i++)
    312   1.8   mycroft 			if (grp->gr_gid == gidset[i])
    313   1.8   mycroft 				break;
    314  1.19       mrg 		if (i >= ngroups && myuid != 0) {
    315  1.16     lukem 			warnx("%s (gid %d): permission denied",
    316   1.8   mycroft 			    name, grp->gr_gid);
    317   1.8   mycroft 			return;
    318   1.8   mycroft 		}
    319   1.1       cgd 	}
    320   1.1       cgd 	showquotas(GRPQUOTA, grp->gr_gid, name);
    321   1.1       cgd }
    322   1.1       cgd 
    323  1.35  christos static void
    324  1.35  christos showquotas(int type, uint32_t id, const char *name)
    325   1.1       cgd {
    326  1.16     lukem 	struct quotause *qup;
    327   1.6   deraadt 	struct quotause *quplist;
    328  1.34    bouyer 	const char *msgi, *msgb, *nam, *timemsg;
    329  1.16     lukem 	int lines = 0;
    330   1.1       cgd 	static time_t now;
    331  1.35  christos 	char b0[20], b1[20], b2[20], b3[20];
    332   1.1       cgd 
    333   1.1       cgd 	if (now == 0)
    334   1.1       cgd 		time(&now);
    335   1.1       cgd 	quplist = getprivs(id, type);
    336   1.1       cgd 	for (qup = quplist; qup; qup = qup->next) {
    337  1.34    bouyer 		int ql_stat;
    338  1.36  christos 		struct quota2_val *q = qup->q2e.q2e_val;
    339   1.1       cgd 		if (!vflag &&
    340  1.36  christos 		    q[QL_BLOCK].q2v_softlimit == UQUAD_MAX &&
    341  1.36  christos 		    q[QL_BLOCK].q2v_hardlimit == UQUAD_MAX &&
    342  1.36  christos 		    q[QL_FILE].q2v_softlimit == UQUAD_MAX &&
    343  1.36  christos 		    q[QL_FILE].q2v_hardlimit == UQUAD_MAX)
    344   1.1       cgd 			continue;
    345  1.36  christos 		ql_stat = quota2_check_limit(&q[QL_FILE], 1, now);
    346  1.34    bouyer 		switch(QL_STATUS(ql_stat)) {
    347  1.34    bouyer 		case QL_S_DENY_HARD:
    348   1.1       cgd 			msgi = "File limit reached on";
    349  1.34    bouyer 			break;
    350  1.34    bouyer 		case QL_S_DENY_GRACE:
    351  1.34    bouyer 			msgi = "Over file quota on";
    352  1.34    bouyer 			break;
    353  1.34    bouyer 		case QL_S_ALLOW_SOFT:
    354  1.34    bouyer 			msgi = "In file grace period on";
    355  1.34    bouyer 			break;
    356  1.34    bouyer 		default:
    357  1.34    bouyer 			msgi = NULL;
    358  1.21      ross 		}
    359  1.36  christos 		ql_stat = quota2_check_limit(&q[QL_BLOCK], 1, now);
    360  1.34    bouyer 		switch(QL_STATUS(ql_stat)) {
    361  1.34    bouyer 		case QL_S_DENY_HARD:
    362   1.1       cgd 			msgb = "Block limit reached on";
    363  1.34    bouyer 			break;
    364  1.34    bouyer 		case QL_S_DENY_GRACE:
    365  1.34    bouyer 			msgb = "Over block quota on";
    366  1.34    bouyer 			break;
    367  1.34    bouyer 		case QL_S_ALLOW_SOFT:
    368  1.34    bouyer 			msgb = "In block grace period on";
    369  1.34    bouyer 			break;
    370  1.34    bouyer 		default:
    371  1.34    bouyer 			msgb = NULL;
    372  1.21      ross 		}
    373   1.1       cgd 		if (qflag) {
    374  1.33     lukem 			if ((msgi != NULL || msgb != NULL) &&
    375   1.1       cgd 			    lines++ == 0)
    376   1.1       cgd 				heading(type, id, name, "");
    377  1.33     lukem 			if (msgi != NULL)
    378   1.1       cgd 				printf("\t%s %s\n", msgi, qup->fsname);
    379  1.33     lukem 			if (msgb != NULL)
    380   1.1       cgd 				printf("\t%s %s\n", msgb, qup->fsname);
    381   1.1       cgd 			continue;
    382   1.1       cgd 		}
    383  1.36  christos 		if (vflag || dflag || msgi || msgb || q[QL_BLOCK].q2v_cur ||
    384  1.36  christos 		    q[QL_FILE].q2v_cur) {
    385   1.1       cgd 			if (lines++ == 0)
    386   1.1       cgd 				heading(type, id, name, "");
    387   1.6   deraadt 			nam = qup->fsname;
    388  1.34    bouyer 			if (strlen(qup->fsname) > 4) {
    389   1.6   deraadt 				printf("%s\n", qup->fsname);
    390   1.6   deraadt 				nam = "";
    391   1.6   deraadt 			}
    392  1.34    bouyer 			if (msgb)
    393  1.35  christos 				timemsg = timeprt(b0, 9, now,
    394  1.36  christos 				    q[QL_BLOCK].q2v_time);
    395  1.34    bouyer 			else if ((qup->flags & QUOTA2) != 0 && vflag)
    396  1.35  christos 				timemsg = timeprt(b0, 9, 0,
    397  1.36  christos 				    q[QL_BLOCK].q2v_grace);
    398  1.34    bouyer 			else
    399  1.34    bouyer 				timemsg = "";
    400  1.34    bouyer 
    401  1.35  christos 			printf("%12s%9s%c%8s%9s%8s",
    402  1.35  christos 			    nam,
    403  1.36  christos 			    intprt(b1, 9, q[QL_BLOCK].q2v_cur,
    404  1.35  christos 			    HN_B, hflag),
    405  1.35  christos 			    (msgb == NULL) ? ' ' : '*',
    406  1.36  christos 			    intprt(b2, 9, q[QL_BLOCK].q2v_softlimit,
    407  1.35  christos 			    HN_B, hflag),
    408  1.36  christos 			    intprt(b3, 9, q[QL_BLOCK].q2v_hardlimit,
    409  1.35  christos 			    HN_B, hflag),
    410  1.35  christos 			    timemsg);
    411  1.34    bouyer 
    412  1.34    bouyer 			if (msgi)
    413  1.35  christos 				timemsg = timeprt(b0, 9, now,
    414  1.36  christos 				    q[QL_FILE].q2v_time);
    415  1.34    bouyer 			else if ((qup->flags & QUOTA2) != 0 && vflag)
    416  1.35  christos 				timemsg = timeprt(b0, 9, 0,
    417  1.36  christos 				    q[QL_FILE].q2v_grace);
    418  1.34    bouyer 			else
    419  1.34    bouyer 				timemsg = "";
    420  1.34    bouyer 
    421  1.35  christos 			printf("%8s%c%7s%8s%8s\n",
    422  1.36  christos 			    intprt(b1, 8, q[QL_FILE].q2v_cur, 0, hflag),
    423  1.35  christos 			    (msgi == NULL) ? ' ' : '*',
    424  1.36  christos 			    intprt(b2, 8, q[QL_FILE].q2v_softlimit, 0, hflag),
    425  1.36  christos 			    intprt(b3, 8, q[QL_FILE].q2v_hardlimit, 0, hflag),
    426  1.35  christos 			    timemsg);
    427   1.1       cgd 			continue;
    428   1.1       cgd 		}
    429   1.1       cgd 	}
    430   1.1       cgd 	if (!qflag && lines == 0)
    431   1.1       cgd 		heading(type, id, name, "none");
    432   1.1       cgd }
    433   1.1       cgd 
    434  1.35  christos static void
    435  1.35  christos heading(int type, uint32_t id, const char *name, const char *tag)
    436   1.1       cgd {
    437  1.34    bouyer 	if (dflag)
    438  1.34    bouyer 		printf("Default %s disk quotas: %s\n",
    439  1.34    bouyer 		    qfextension[type], tag);
    440  1.34    bouyer 	else
    441  1.35  christos 		printf("Disk quotas for %s %s (%cid %u): %s\n",
    442  1.34    bouyer 		    qfextension[type], name, *qfextension[type],
    443  1.35  christos 		    id, tag);
    444   1.1       cgd 
    445   1.1       cgd 	if (!qflag && tag[0] == '\0') {
    446  1.22    bouyer 		printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
    447  1.22    bouyer 		    , "Filesystem"
    448  1.22    bouyer 		    , "blocks"
    449  1.22    bouyer 		    , "quota"
    450  1.22    bouyer 		    , "limit"
    451  1.22    bouyer 		    , "grace"
    452  1.22    bouyer 		    , "files"
    453  1.22    bouyer 		    , "quota"
    454  1.22    bouyer 		    , "limit"
    455  1.22    bouyer 		    , "grace"
    456   1.1       cgd 		);
    457   1.1       cgd 	}
    458   1.1       cgd }
    459   1.1       cgd 
    460   1.1       cgd /*
    461   1.1       cgd  * Collect the requested quota information.
    462   1.1       cgd  */
    463  1.35  christos static struct quotause *
    464  1.35  christos getprivs(uint32_t id, int quotatype)
    465   1.1       cgd {
    466  1.16     lukem 	struct quotause *qup, *quptail;
    467   1.1       cgd 	struct quotause *quphead;
    468  1.29  christos 	struct statvfs *fst;
    469   1.6   deraadt 	int nfst, i;
    470  1.34    bouyer 	int8_t version;
    471   1.6   deraadt 
    472  1.16     lukem 	qup = quphead = quptail = NULL;
    473   1.1       cgd 
    474   1.6   deraadt 	nfst = getmntinfo(&fst, MNT_WAIT);
    475  1.16     lukem 	if (nfst == 0)
    476  1.16     lukem 		errx(2, "no filesystems mounted!");
    477  1.19       mrg 	for (i = 0; i < nfst; i++) {
    478   1.6   deraadt 		if (qup == NULL) {
    479  1.35  christos 			if ((qup = malloc(sizeof *qup)) == NULL)
    480  1.35  christos 				err(1, "out of memory");
    481   1.1       cgd 		}
    482  1.31  christos 		if (strncmp(fst[i].f_fstypename, "nfs",
    483  1.31  christos 		    sizeof(fst[i].f_fstypename)) == 0) {
    484  1.34    bouyer 			version = 0;
    485  1.35  christos 			if (getnfsquota(&fst[i], qup, id, quotatype) == 0)
    486   1.6   deraadt 				continue;
    487  1.34    bouyer 		} else if ((fst[i].f_flag & ST_QUOTA) != 0) {
    488  1.34    bouyer 			if (getvfsquota(fst[i].f_mntonname, &qup->q2e, &version,
    489  1.34    bouyer 			    id, quotatype, dflag, Dflag) == 0)
    490   1.1       cgd 				continue;
    491   1.6   deraadt 		} else
    492   1.6   deraadt 			continue;
    493  1.13       mrg 		(void)strncpy(qup->fsname, fst[i].f_mntonname,
    494  1.13       mrg 		    sizeof(qup->fsname) - 1);
    495  1.34    bouyer 		if (version == 2)
    496  1.34    bouyer 			qup->flags |= QUOTA2;
    497   1.1       cgd 		if (quphead == NULL)
    498   1.1       cgd 			quphead = qup;
    499   1.1       cgd 		else
    500   1.1       cgd 			quptail->next = qup;
    501   1.1       cgd 		quptail = qup;
    502   1.6   deraadt 		quptail->next = 0;
    503   1.6   deraadt 		qup = NULL;
    504   1.1       cgd 	}
    505  1.35  christos 	free(qup);
    506  1.35  christos 	return quphead;
    507   1.1       cgd }
    508   1.1       cgd 
    509  1.35  christos static int
    510  1.35  christos getnfsquota(struct statvfs *fst, struct quotause *qup,
    511  1.35  christos     uint32_t id, int quotatype)
    512   1.6   deraadt {
    513   1.6   deraadt 	struct getquota_args gq_args;
    514  1.25    bouyer 	struct ext_getquota_args ext_gq_args;
    515   1.6   deraadt 	struct getquota_rslt gq_rslt;
    516  1.34    bouyer 	struct quota2_entry *q2e = &qup->q2e;
    517  1.34    bouyer 	struct dqblk dqblk;
    518   1.6   deraadt 	struct timeval tv;
    519   1.6   deraadt 	char *cp;
    520  1.25    bouyer 	int ret;
    521   1.6   deraadt 
    522  1.29  christos 	if (fst->f_flag & MNT_LOCAL)
    523  1.35  christos 		return 0;
    524   1.6   deraadt 
    525   1.6   deraadt 	/*
    526   1.6   deraadt 	 * must be some form of "hostname:/path"
    527   1.6   deraadt 	 */
    528   1.6   deraadt 	cp = strchr(fst->f_mntfromname, ':');
    529   1.6   deraadt 	if (cp == NULL) {
    530  1.16     lukem 		warnx("cannot find hostname for %s", fst->f_mntfromname);
    531  1.35  christos 		return 0;
    532   1.6   deraadt 	}
    533   1.6   deraadt 
    534   1.6   deraadt 	*cp = '\0';
    535   1.6   deraadt 	if (*(cp+1) != '/') {
    536   1.6   deraadt 		*cp = ':';
    537  1.35  christos 		return 0;
    538   1.6   deraadt 	}
    539   1.6   deraadt 
    540  1.25    bouyer 	ext_gq_args.gqa_pathp = cp + 1;
    541  1.25    bouyer 	ext_gq_args.gqa_id = id;
    542  1.25    bouyer 	ext_gq_args.gqa_type =
    543  1.25    bouyer 	    (quotatype == USRQUOTA) ? RQUOTA_USRQUOTA : RQUOTA_GRPQUOTA;
    544  1.25    bouyer 	ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, EXT_RQUOTAVERS,
    545  1.25    bouyer 	    RQUOTAPROC_GETQUOTA, xdr_ext_getquota_args, &ext_gq_args,
    546  1.25    bouyer 	    xdr_getquota_rslt, &gq_rslt);
    547  1.27    bouyer 	if (ret == RPC_PROGVERSMISMATCH) {
    548  1.25    bouyer 		if (quotatype != USRQUOTA) {
    549  1.25    bouyer 			*cp = ':';
    550  1.35  christos 			return 0;
    551  1.25    bouyer 		}
    552  1.25    bouyer 		/* try RQUOTAVERS */
    553  1.25    bouyer 		gq_args.gqa_pathp = cp + 1;
    554  1.25    bouyer 		gq_args.gqa_uid = id;
    555  1.25    bouyer 		ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
    556  1.25    bouyer 		    RQUOTAPROC_GETQUOTA, xdr_getquota_args, &gq_args,
    557  1.35  christos 		    xdr_getquota_rslt, &gq_rslt);
    558  1.25    bouyer 	}
    559  1.25    bouyer 	if (ret != RPC_SUCCESS) {
    560   1.6   deraadt 		*cp = ':';
    561  1.35  christos 		return 0;
    562   1.6   deraadt 	}
    563   1.6   deraadt 
    564   1.6   deraadt 	switch (gq_rslt.status) {
    565   1.6   deraadt 	case Q_NOQUOTA:
    566   1.6   deraadt 		break;
    567   1.6   deraadt 	case Q_EPERM:
    568  1.16     lukem 		warnx("quota permission error, host: %s", fst->f_mntfromname);
    569   1.6   deraadt 		break;
    570   1.6   deraadt 	case Q_OK:
    571   1.6   deraadt 		gettimeofday(&tv, NULL);
    572  1.35  christos 
    573  1.35  christos 		/* blocks*/
    574  1.34    bouyer 		q2e->q2e_val[QL_BLOCK].q2v_hardlimit =
    575  1.34    bouyer 		dqblk.dqb_bhardlimit =
    576   1.6   deraadt 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
    577  1.23       cgd 		    (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
    578  1.34    bouyer 		dqblk.dqb_bsoftlimit =
    579   1.6   deraadt 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
    580  1.23       cgd 		    (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
    581  1.34    bouyer 		dqblk.dqb_curblocks =
    582   1.6   deraadt 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
    583  1.23       cgd 		    (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
    584  1.35  christos 
    585  1.35  christos 		/* inodes */
    586  1.34    bouyer 		dqblk.dqb_ihardlimit =
    587  1.35  christos 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
    588  1.34    bouyer 		dqblk.dqb_isoftlimit =
    589  1.35  christos 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
    590  1.34    bouyer 		dqblk.dqb_curinodes =
    591  1.35  christos 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
    592  1.35  christos 
    593  1.35  christos 		/* grace times */
    594  1.35  christos 		dqblk.dqb_btime = (int)(tv.tv_sec +
    595  1.35  christos 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft);
    596  1.35  christos 		dqblk.dqb_itime = (int)(tv.tv_sec +
    597  1.35  christos 		    gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft);
    598  1.34    bouyer 		dqblk2q2e(&dqblk, q2e);
    599   1.6   deraadt 		*cp = ':';
    600  1.35  christos 		return 1;
    601   1.6   deraadt 	default:
    602  1.16     lukem 		warnx("bad rpc result, host: %s", fst->f_mntfromname);
    603   1.6   deraadt 		break;
    604   1.6   deraadt 	}
    605   1.6   deraadt 	*cp = ':';
    606  1.35  christos 	return 0;
    607   1.6   deraadt }
    608   1.6   deraadt 
    609  1.35  christos static int
    610  1.35  christos callaurpc(const char *host, rpcprog_t prognum, rpcvers_t versnum,
    611  1.35  christos     rpcproc_t procnum, xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
    612   1.6   deraadt {
    613   1.6   deraadt 	struct sockaddr_in server_addr;
    614   1.6   deraadt 	enum clnt_stat clnt_stat;
    615   1.6   deraadt 	struct hostent *hp;
    616   1.6   deraadt 	struct timeval timeout, tottimeout;
    617   1.6   deraadt 
    618   1.6   deraadt 	CLIENT *client = NULL;
    619  1.33     lukem 	int sock = RPC_ANYSOCK;
    620   1.6   deraadt 
    621   1.6   deraadt 	if ((hp = gethostbyname(host)) == NULL)
    622  1.35  christos 		return (int) RPC_UNKNOWNHOST;
    623   1.6   deraadt 	timeout.tv_usec = 0;
    624   1.6   deraadt 	timeout.tv_sec = 6;
    625  1.16     lukem 	memmove(&server_addr.sin_addr, hp->h_addr, hp->h_length);
    626   1.6   deraadt 	server_addr.sin_family = AF_INET;
    627   1.6   deraadt 	server_addr.sin_port =  0;
    628   1.6   deraadt 
    629   1.6   deraadt 	if ((client = clntudp_create(&server_addr, prognum,
    630  1.33     lukem 	    versnum, timeout, &sock)) == NULL)
    631  1.35  christos 		return (int) rpc_createerr.cf_stat;
    632   1.6   deraadt 
    633   1.6   deraadt 	client->cl_auth = authunix_create_default();
    634   1.6   deraadt 	tottimeout.tv_sec = 25;
    635   1.6   deraadt 	tottimeout.tv_usec = 0;
    636   1.6   deraadt 	clnt_stat = clnt_call(client, procnum, inproc, in,
    637   1.6   deraadt 	    outproc, out, tottimeout);
    638   1.6   deraadt 
    639  1.35  christos 	return (int) clnt_stat;
    640   1.1       cgd }
    641