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