quota.c revision 1.35 1 1.35 christos /* $NetBSD: quota.c,v 1.35 2011/03/06 20:47:59 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.35 christos __RCSID("$NetBSD: quota.c,v 1.35 2011/03/06 20:47:59 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.34 bouyer #include <printquota.h>
80 1.34 bouyer #include <getvfsquota.h>
81 1.1 cgd
82 1.1 cgd struct quotause {
83 1.1 cgd struct quotause *next;
84 1.1 cgd long flags;
85 1.34 bouyer struct quota2_entry q2e;
86 1.1 cgd char fsname[MAXPATHLEN + 1];
87 1.6 deraadt };
88 1.1 cgd #define FOUND 0x01
89 1.34 bouyer #define QUOTA2 0x02
90 1.1 cgd
91 1.35 christos static int alldigits(const char *);
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.1 cgd if (!vflag &&
339 1.34 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit == UQUAD_MAX &&
340 1.34 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit == UQUAD_MAX &&
341 1.34 bouyer qup->q2e.q2e_val[QL_FILE].q2v_softlimit == UQUAD_MAX &&
342 1.34 bouyer qup->q2e.q2e_val[QL_FILE].q2v_hardlimit == UQUAD_MAX)
343 1.1 cgd continue;
344 1.34 bouyer ql_stat = quota2_check_limit(&qup->q2e.q2e_val[QL_FILE],
345 1.34 bouyer 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.34 bouyer ql_stat = quota2_check_limit(&qup->q2e.q2e_val[QL_BLOCK],
360 1.34 bouyer 1, now);
361 1.34 bouyer switch(QL_STATUS(ql_stat)) {
362 1.34 bouyer case QL_S_DENY_HARD:
363 1.1 cgd msgb = "Block limit reached on";
364 1.34 bouyer break;
365 1.34 bouyer case QL_S_DENY_GRACE:
366 1.34 bouyer msgb = "Over block quota on";
367 1.34 bouyer break;
368 1.34 bouyer case QL_S_ALLOW_SOFT:
369 1.34 bouyer msgb = "In block grace period on";
370 1.34 bouyer break;
371 1.34 bouyer default:
372 1.34 bouyer msgb = NULL;
373 1.21 ross }
374 1.1 cgd if (qflag) {
375 1.33 lukem if ((msgi != NULL || msgb != NULL) &&
376 1.1 cgd lines++ == 0)
377 1.1 cgd heading(type, id, name, "");
378 1.33 lukem if (msgi != NULL)
379 1.1 cgd printf("\t%s %s\n", msgi, qup->fsname);
380 1.33 lukem if (msgb != NULL)
381 1.1 cgd printf("\t%s %s\n", msgb, qup->fsname);
382 1.1 cgd continue;
383 1.1 cgd }
384 1.34 bouyer if (vflag || dflag || msgi || msgb ||
385 1.34 bouyer qup->q2e.q2e_val[QL_BLOCK].q2v_cur ||
386 1.34 bouyer qup->q2e.q2e_val[QL_FILE].q2v_cur) {
387 1.1 cgd if (lines++ == 0)
388 1.1 cgd heading(type, id, name, "");
389 1.6 deraadt nam = qup->fsname;
390 1.34 bouyer if (strlen(qup->fsname) > 4) {
391 1.6 deraadt printf("%s\n", qup->fsname);
392 1.6 deraadt nam = "";
393 1.6 deraadt }
394 1.34 bouyer if (msgb)
395 1.35 christos timemsg = timeprt(b0, 9, now,
396 1.35 christos qup->q2e.q2e_val[QL_BLOCK].q2v_time);
397 1.34 bouyer else if ((qup->flags & QUOTA2) != 0 && vflag)
398 1.35 christos timemsg = timeprt(b0, 9, 0,
399 1.35 christos qup->q2e.q2e_val[QL_BLOCK].q2v_grace);
400 1.34 bouyer else
401 1.34 bouyer timemsg = "";
402 1.34 bouyer
403 1.35 christos printf("%12s%9s%c%8s%9s%8s",
404 1.35 christos nam,
405 1.35 christos intprt(b1, 9,
406 1.35 christos qup->q2e.q2e_val[QL_BLOCK].q2v_cur,
407 1.35 christos HN_B, hflag),
408 1.35 christos (msgb == NULL) ? ' ' : '*',
409 1.35 christos intprt(b2, 9,
410 1.35 christos qup->q2e.q2e_val[QL_BLOCK].q2v_softlimit,
411 1.35 christos HN_B, hflag),
412 1.35 christos intprt(b3, 9,
413 1.35 christos qup->q2e.q2e_val[QL_BLOCK].q2v_hardlimit,
414 1.35 christos HN_B, hflag),
415 1.35 christos timemsg);
416 1.34 bouyer
417 1.34 bouyer if (msgi)
418 1.35 christos timemsg = timeprt(b0, 9, now,
419 1.35 christos qup->q2e.q2e_val[QL_FILE].q2v_time);
420 1.34 bouyer else if ((qup->flags & QUOTA2) != 0 && vflag)
421 1.35 christos timemsg = timeprt(b0, 9, 0,
422 1.35 christos qup->q2e.q2e_val[QL_FILE].q2v_grace);
423 1.34 bouyer else
424 1.34 bouyer timemsg = "";
425 1.34 bouyer
426 1.35 christos printf("%8s%c%7s%8s%8s\n",
427 1.35 christos intprt(b1, 8,
428 1.35 christos qup->q2e.q2e_val[QL_FILE].q2v_cur, 0, hflag),
429 1.35 christos (msgi == NULL) ? ' ' : '*',
430 1.35 christos intprt(b2, 8,
431 1.35 christos qup->q2e.q2e_val[QL_FILE].q2v_softlimit,
432 1.35 christos 0, hflag),
433 1.35 christos intprt(b3, 8,
434 1.35 christos qup->q2e.q2e_val[QL_FILE].q2v_hardlimit,
435 1.35 christos 0, hflag),
436 1.35 christos timemsg);
437 1.1 cgd continue;
438 1.1 cgd }
439 1.1 cgd }
440 1.1 cgd if (!qflag && lines == 0)
441 1.1 cgd heading(type, id, name, "none");
442 1.1 cgd }
443 1.1 cgd
444 1.35 christos static void
445 1.35 christos heading(int type, uint32_t id, const char *name, const char *tag)
446 1.1 cgd {
447 1.34 bouyer if (dflag)
448 1.34 bouyer printf("Default %s disk quotas: %s\n",
449 1.34 bouyer qfextension[type], tag);
450 1.34 bouyer else
451 1.35 christos printf("Disk quotas for %s %s (%cid %u): %s\n",
452 1.34 bouyer qfextension[type], name, *qfextension[type],
453 1.35 christos id, tag);
454 1.1 cgd
455 1.1 cgd if (!qflag && tag[0] == '\0') {
456 1.22 bouyer printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
457 1.22 bouyer , "Filesystem"
458 1.22 bouyer , "blocks"
459 1.22 bouyer , "quota"
460 1.22 bouyer , "limit"
461 1.22 bouyer , "grace"
462 1.22 bouyer , "files"
463 1.22 bouyer , "quota"
464 1.22 bouyer , "limit"
465 1.22 bouyer , "grace"
466 1.1 cgd );
467 1.1 cgd }
468 1.1 cgd }
469 1.1 cgd
470 1.1 cgd /*
471 1.1 cgd * Collect the requested quota information.
472 1.1 cgd */
473 1.35 christos static struct quotause *
474 1.35 christos getprivs(uint32_t id, int quotatype)
475 1.1 cgd {
476 1.16 lukem struct quotause *qup, *quptail;
477 1.1 cgd struct quotause *quphead;
478 1.29 christos struct statvfs *fst;
479 1.6 deraadt int nfst, i;
480 1.34 bouyer int8_t version;
481 1.6 deraadt
482 1.16 lukem qup = quphead = quptail = NULL;
483 1.1 cgd
484 1.6 deraadt nfst = getmntinfo(&fst, MNT_WAIT);
485 1.16 lukem if (nfst == 0)
486 1.16 lukem errx(2, "no filesystems mounted!");
487 1.19 mrg for (i = 0; i < nfst; i++) {
488 1.6 deraadt if (qup == NULL) {
489 1.35 christos if ((qup = malloc(sizeof *qup)) == NULL)
490 1.35 christos err(1, "out of memory");
491 1.1 cgd }
492 1.31 christos if (strncmp(fst[i].f_fstypename, "nfs",
493 1.31 christos sizeof(fst[i].f_fstypename)) == 0) {
494 1.34 bouyer version = 0;
495 1.35 christos if (getnfsquota(&fst[i], qup, id, quotatype) == 0)
496 1.6 deraadt continue;
497 1.34 bouyer } else if ((fst[i].f_flag & ST_QUOTA) != 0) {
498 1.34 bouyer if (getvfsquota(fst[i].f_mntonname, &qup->q2e, &version,
499 1.34 bouyer id, quotatype, dflag, Dflag) == 0)
500 1.1 cgd continue;
501 1.6 deraadt } else
502 1.6 deraadt continue;
503 1.13 mrg (void)strncpy(qup->fsname, fst[i].f_mntonname,
504 1.13 mrg sizeof(qup->fsname) - 1);
505 1.34 bouyer if (version == 2)
506 1.34 bouyer qup->flags |= QUOTA2;
507 1.1 cgd if (quphead == NULL)
508 1.1 cgd quphead = qup;
509 1.1 cgd else
510 1.1 cgd quptail->next = qup;
511 1.1 cgd quptail = qup;
512 1.6 deraadt quptail->next = 0;
513 1.6 deraadt qup = NULL;
514 1.1 cgd }
515 1.35 christos free(qup);
516 1.35 christos return quphead;
517 1.1 cgd }
518 1.1 cgd
519 1.35 christos static int
520 1.35 christos getnfsquota(struct statvfs *fst, struct quotause *qup,
521 1.35 christos uint32_t id, int quotatype)
522 1.6 deraadt {
523 1.6 deraadt struct getquota_args gq_args;
524 1.25 bouyer struct ext_getquota_args ext_gq_args;
525 1.6 deraadt struct getquota_rslt gq_rslt;
526 1.34 bouyer struct quota2_entry *q2e = &qup->q2e;
527 1.34 bouyer struct dqblk dqblk;
528 1.6 deraadt struct timeval tv;
529 1.6 deraadt char *cp;
530 1.25 bouyer int ret;
531 1.6 deraadt
532 1.29 christos if (fst->f_flag & MNT_LOCAL)
533 1.35 christos return 0;
534 1.6 deraadt
535 1.6 deraadt /*
536 1.6 deraadt * must be some form of "hostname:/path"
537 1.6 deraadt */
538 1.6 deraadt cp = strchr(fst->f_mntfromname, ':');
539 1.6 deraadt if (cp == NULL) {
540 1.16 lukem warnx("cannot find hostname for %s", fst->f_mntfromname);
541 1.35 christos return 0;
542 1.6 deraadt }
543 1.6 deraadt
544 1.6 deraadt *cp = '\0';
545 1.6 deraadt if (*(cp+1) != '/') {
546 1.6 deraadt *cp = ':';
547 1.35 christos return 0;
548 1.6 deraadt }
549 1.6 deraadt
550 1.25 bouyer ext_gq_args.gqa_pathp = cp + 1;
551 1.25 bouyer ext_gq_args.gqa_id = id;
552 1.25 bouyer ext_gq_args.gqa_type =
553 1.25 bouyer (quotatype == USRQUOTA) ? RQUOTA_USRQUOTA : RQUOTA_GRPQUOTA;
554 1.25 bouyer ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, EXT_RQUOTAVERS,
555 1.25 bouyer RQUOTAPROC_GETQUOTA, xdr_ext_getquota_args, &ext_gq_args,
556 1.25 bouyer xdr_getquota_rslt, &gq_rslt);
557 1.27 bouyer if (ret == RPC_PROGVERSMISMATCH) {
558 1.25 bouyer if (quotatype != USRQUOTA) {
559 1.25 bouyer *cp = ':';
560 1.35 christos return 0;
561 1.25 bouyer }
562 1.25 bouyer /* try RQUOTAVERS */
563 1.25 bouyer gq_args.gqa_pathp = cp + 1;
564 1.25 bouyer gq_args.gqa_uid = id;
565 1.25 bouyer ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
566 1.25 bouyer RQUOTAPROC_GETQUOTA, xdr_getquota_args, &gq_args,
567 1.35 christos xdr_getquota_rslt, &gq_rslt);
568 1.25 bouyer }
569 1.25 bouyer if (ret != RPC_SUCCESS) {
570 1.6 deraadt *cp = ':';
571 1.35 christos return 0;
572 1.6 deraadt }
573 1.6 deraadt
574 1.6 deraadt switch (gq_rslt.status) {
575 1.6 deraadt case Q_NOQUOTA:
576 1.6 deraadt break;
577 1.6 deraadt case Q_EPERM:
578 1.16 lukem warnx("quota permission error, host: %s", fst->f_mntfromname);
579 1.6 deraadt break;
580 1.6 deraadt case Q_OK:
581 1.6 deraadt gettimeofday(&tv, NULL);
582 1.35 christos
583 1.35 christos /* blocks*/
584 1.34 bouyer q2e->q2e_val[QL_BLOCK].q2v_hardlimit =
585 1.34 bouyer dqblk.dqb_bhardlimit =
586 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
587 1.23 cgd (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
588 1.34 bouyer dqblk.dqb_bsoftlimit =
589 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
590 1.23 cgd (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
591 1.34 bouyer dqblk.dqb_curblocks =
592 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
593 1.23 cgd (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
594 1.35 christos
595 1.35 christos /* inodes */
596 1.34 bouyer dqblk.dqb_ihardlimit =
597 1.35 christos gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
598 1.34 bouyer dqblk.dqb_isoftlimit =
599 1.35 christos gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
600 1.34 bouyer dqblk.dqb_curinodes =
601 1.35 christos gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
602 1.35 christos
603 1.35 christos /* grace times */
604 1.35 christos dqblk.dqb_btime = (int)(tv.tv_sec +
605 1.35 christos gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft);
606 1.35 christos dqblk.dqb_itime = (int)(tv.tv_sec +
607 1.35 christos gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft);
608 1.34 bouyer dqblk2q2e(&dqblk, q2e);
609 1.6 deraadt *cp = ':';
610 1.35 christos return 1;
611 1.6 deraadt default:
612 1.16 lukem warnx("bad rpc result, host: %s", fst->f_mntfromname);
613 1.6 deraadt break;
614 1.6 deraadt }
615 1.6 deraadt *cp = ':';
616 1.35 christos return 0;
617 1.6 deraadt }
618 1.6 deraadt
619 1.35 christos static int
620 1.35 christos callaurpc(const char *host, rpcprog_t prognum, rpcvers_t versnum,
621 1.35 christos rpcproc_t procnum, xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
622 1.6 deraadt {
623 1.6 deraadt struct sockaddr_in server_addr;
624 1.6 deraadt enum clnt_stat clnt_stat;
625 1.6 deraadt struct hostent *hp;
626 1.6 deraadt struct timeval timeout, tottimeout;
627 1.6 deraadt
628 1.6 deraadt CLIENT *client = NULL;
629 1.33 lukem int sock = RPC_ANYSOCK;
630 1.6 deraadt
631 1.6 deraadt if ((hp = gethostbyname(host)) == NULL)
632 1.35 christos return (int) RPC_UNKNOWNHOST;
633 1.6 deraadt timeout.tv_usec = 0;
634 1.6 deraadt timeout.tv_sec = 6;
635 1.16 lukem memmove(&server_addr.sin_addr, hp->h_addr, hp->h_length);
636 1.6 deraadt server_addr.sin_family = AF_INET;
637 1.6 deraadt server_addr.sin_port = 0;
638 1.6 deraadt
639 1.6 deraadt if ((client = clntudp_create(&server_addr, prognum,
640 1.33 lukem versnum, timeout, &sock)) == NULL)
641 1.35 christos return (int) rpc_createerr.cf_stat;
642 1.6 deraadt
643 1.6 deraadt client->cl_auth = authunix_create_default();
644 1.6 deraadt tottimeout.tv_sec = 25;
645 1.6 deraadt tottimeout.tv_usec = 0;
646 1.6 deraadt clnt_stat = clnt_call(client, procnum, inproc, in,
647 1.6 deraadt outproc, out, tottimeout);
648 1.6 deraadt
649 1.35 christos return (int) clnt_stat;
650 1.1 cgd }
651 1.1 cgd
652 1.35 christos static int
653 1.35 christos alldigits(const char *s)
654 1.1 cgd {
655 1.35 christos unsigned char c;
656 1.1 cgd
657 1.1 cgd c = *s++;
658 1.1 cgd do {
659 1.1 cgd if (!isdigit(c))
660 1.35 christos return 0;
661 1.16 lukem } while ((c = *s++) != 0);
662 1.35 christos return 1;
663 1.1 cgd }
664