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