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