quota.c revision 1.38 1 /* $NetBSD: quota.c,v 1.38 2011/11/25 16:55:05 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95";
44 #else
45 __RCSID("$NetBSD: quota.c,v 1.38 2011/11/25 16:55:05 dholland Exp $");
46 #endif
47 #endif /* not lint */
48
49 /*
50 * Disk quota reporting program.
51 */
52 #include <sys/param.h>
53 #include <sys/types.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <sys/mount.h>
57 #include <sys/socket.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <fstab.h>
63 #include <grp.h>
64 #include <netdb.h>
65 #include <pwd.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <time.h>
70 #include <unistd.h>
71
72 #include <quota/quotaprop.h>
73 #include <quota/quota.h>
74
75 #include "printquota.h"
76 #include "getvfsquota.h"
77
78 struct quotause {
79 struct quotause *next;
80 long flags;
81 uid_t id;
82 struct quotaval qv[QUOTA_NLIMITS];
83 char fsname[MAXPATHLEN + 1];
84 };
85 #define FOUND 0x01
86 #define QUOTA2 0x02
87
88 static struct quotause *getprivs(uint32_t, int);
89 static void heading(int, uint32_t, const char *, const char *);
90 static void showgid(gid_t);
91 static void showgrpname(const char *);
92 static void showquotas(int, uint32_t, const char *);
93 static void showuid(uid_t);
94 static void showusrname(const char *);
95 static void usage(void) __attribute__((__noreturn__));
96
97 static int qflag = 0;
98 static int vflag = 0;
99 static int hflag = 0;
100 static int dflag = 0;
101 static int Dflag = 0;
102 static uid_t myuid;
103
104 int
105 main(int argc, char *argv[])
106 {
107 int ngroups;
108 gid_t mygid, gidset[NGROUPS];
109 int i, gflag = 0, uflag = 0;
110 int ch;
111
112 myuid = getuid();
113 while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
114 switch(ch) {
115 case 'g':
116 gflag++;
117 break;
118 case 'u':
119 uflag++;
120 break;
121 case 'v':
122 vflag++;
123 break;
124 case 'q':
125 qflag++;
126 break;
127 case 'h':
128 hflag++;
129 break;
130 case 'd':
131 dflag++;
132 break;
133 case 'D':
134 Dflag++;
135 break;
136 default:
137 usage();
138 }
139 }
140 argc -= optind;
141 argv += optind;
142 if (!uflag && !gflag)
143 uflag++;
144 if (dflag) {
145 #if 0
146 if (myuid != 0)
147 errx(1, "-d: permission denied");
148 #endif
149 if (uflag)
150 showquotas(QUOTA_CLASS_USER, 0, "");
151 if (gflag)
152 showquotas(QUOTA_CLASS_GROUP, 0, "");
153 return 0;
154 }
155 if (argc == 0) {
156 if (uflag)
157 showuid(myuid);
158 if (gflag) {
159 if (dflag)
160 showgid(0);
161 else {
162 mygid = getgid();
163 ngroups = getgroups(NGROUPS, gidset);
164 if (ngroups < 0)
165 err(1, "getgroups");
166 showgid(mygid);
167 for (i = 0; i < ngroups; i++)
168 if (gidset[i] != mygid)
169 showgid(gidset[i]);
170 }
171 }
172 return 0;
173 }
174 if (uflag && gflag)
175 usage();
176 if (uflag) {
177 for (; argc > 0; argc--, argv++) {
178 if (alldigits(*argv))
179 showuid((uid_t)atoi(*argv));
180 else
181 showusrname(*argv);
182 }
183 return 0;
184 }
185 if (gflag) {
186 for (; argc > 0; argc--, argv++) {
187 if (alldigits(*argv))
188 showgid((gid_t)atoi(*argv));
189 else
190 showgrpname(*argv);
191 }
192 return 0;
193 }
194 /* NOTREACHED */
195 return 0;
196 }
197
198 static void
199 usage(void)
200 {
201 const char *p = getprogname();
202 fprintf(stderr, "Usage: %s [-Dhguqv]\n"
203 "\t%s [-Dhqv] -u username ...\n"
204 "\t%s [-Dhqv] -g groupname ...\n"
205 "\t%s -d [-Dhguqv]\n", p, p, p, p);
206 exit(1);
207 }
208
209 /*
210 * Print out quotas for a specified user identifier.
211 */
212 static void
213 showuid(uid_t uid)
214 {
215 struct passwd *pwd = getpwuid(uid);
216 const char *name;
217
218 if (pwd == NULL)
219 name = "(no account)";
220 else
221 name = pwd->pw_name;
222 if (uid != myuid && myuid != 0) {
223 warnx("%s (uid %d): permission denied", name, uid);
224 return;
225 }
226 showquotas(QUOTA_CLASS_USER, uid, name);
227 }
228
229 /*
230 * Print out quotas for a specified user name.
231 */
232 static void
233 showusrname(const char *name)
234 {
235 struct passwd *pwd = getpwnam(name);
236
237 if (pwd == NULL) {
238 warnx("%s: unknown user", name);
239 return;
240 }
241 if (pwd->pw_uid != myuid && myuid != 0) {
242 warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
243 return;
244 }
245 showquotas(QUOTA_CLASS_USER, pwd->pw_uid, name);
246 }
247
248 /*
249 * Print out quotas for a specified group identifier.
250 */
251 static void
252 showgid(gid_t gid)
253 {
254 struct group *grp = getgrgid(gid);
255 int ngroups;
256 gid_t mygid, gidset[NGROUPS];
257 int i;
258 const char *name;
259
260 if (grp == NULL)
261 name = "(no entry)";
262 else
263 name = grp->gr_name;
264 mygid = getgid();
265 ngroups = getgroups(NGROUPS, gidset);
266 if (ngroups < 0) {
267 warn("getgroups");
268 return;
269 }
270 if (gid != mygid) {
271 for (i = 0; i < ngroups; i++)
272 if (gid == gidset[i])
273 break;
274 if (i >= ngroups && myuid != 0) {
275 warnx("%s (gid %d): permission denied", name, gid);
276 return;
277 }
278 }
279 showquotas(QUOTA_CLASS_GROUP, gid, name);
280 }
281
282 /*
283 * Print out quotas for a specified group name.
284 */
285 static void
286 showgrpname(const char *name)
287 {
288 struct group *grp = getgrnam(name);
289 int ngroups;
290 gid_t mygid, gidset[NGROUPS];
291 int i;
292
293 if (grp == NULL) {
294 warnx("%s: unknown group", name);
295 return;
296 }
297 mygid = getgid();
298 ngroups = getgroups(NGROUPS, gidset);
299 if (ngroups < 0) {
300 warn("getgroups");
301 return;
302 }
303 if (grp->gr_gid != mygid) {
304 for (i = 0; i < ngroups; i++)
305 if (grp->gr_gid == gidset[i])
306 break;
307 if (i >= ngroups && myuid != 0) {
308 warnx("%s (gid %d): permission denied",
309 name, grp->gr_gid);
310 return;
311 }
312 }
313 showquotas(QUOTA_CLASS_GROUP, grp->gr_gid, name);
314 }
315
316 static void
317 showquotas(int type, uint32_t id, const char *name)
318 {
319 struct quotause *qup;
320 struct quotause *quplist;
321 const char *msgi, *msgb, *nam, *timemsg;
322 int lines = 0;
323 static time_t now;
324 char b0[20], b1[20], b2[20], b3[20];
325
326 if (now == 0)
327 time(&now);
328 quplist = getprivs(id, type);
329 for (qup = quplist; qup; qup = qup->next) {
330 int ql_stat;
331 struct quotaval *q = qup->qv;
332 if (!vflag &&
333 q[QUOTA_LIMIT_BLOCK].qv_softlimit == UQUAD_MAX &&
334 q[QUOTA_LIMIT_BLOCK].qv_hardlimit == UQUAD_MAX &&
335 q[QUOTA_LIMIT_FILE].qv_softlimit == UQUAD_MAX &&
336 q[QUOTA_LIMIT_FILE].qv_hardlimit == UQUAD_MAX)
337 continue;
338 ql_stat = quota_check_limit(q[QUOTA_LIMIT_FILE].qv_usage, 1,
339 q[QUOTA_LIMIT_FILE].qv_softlimit,
340 q[QUOTA_LIMIT_FILE].qv_hardlimit,
341 q[QUOTA_LIMIT_FILE].qv_expiretime, now);
342 switch(QL_STATUS(ql_stat)) {
343 case QL_S_DENY_HARD:
344 msgi = "File limit reached on";
345 break;
346 case QL_S_DENY_GRACE:
347 msgi = "Over file quota on";
348 break;
349 case QL_S_ALLOW_SOFT:
350 msgi = "In file grace period on";
351 break;
352 default:
353 msgi = NULL;
354 }
355 ql_stat = quota_check_limit(q[QUOTA_LIMIT_BLOCK].qv_usage, 1,
356 q[QUOTA_LIMIT_BLOCK].qv_softlimit,
357 q[QUOTA_LIMIT_BLOCK].qv_hardlimit,
358 q[QUOTA_LIMIT_BLOCK].qv_expiretime, now);
359 switch(QL_STATUS(ql_stat)) {
360 case QL_S_DENY_HARD:
361 msgb = "Block limit reached on";
362 break;
363 case QL_S_DENY_GRACE:
364 msgb = "Over block quota on";
365 break;
366 case QL_S_ALLOW_SOFT:
367 msgb = "In block grace period on";
368 break;
369 default:
370 msgb = NULL;
371 }
372 if (qflag) {
373 if ((msgi != NULL || msgb != NULL) &&
374 lines++ == 0)
375 heading(type, id, name, "");
376 if (msgi != NULL)
377 printf("\t%s %s\n", msgi, qup->fsname);
378 if (msgb != NULL)
379 printf("\t%s %s\n", msgb, qup->fsname);
380 continue;
381 }
382 if (vflag || dflag || msgi || msgb ||
383 q[QUOTA_LIMIT_BLOCK].qv_usage ||
384 q[QUOTA_LIMIT_FILE].qv_usage) {
385 if (lines++ == 0)
386 heading(type, id, name, "");
387 nam = qup->fsname;
388 if (strlen(qup->fsname) > 4) {
389 printf("%s\n", qup->fsname);
390 nam = "";
391 }
392 if (msgb)
393 timemsg = timeprt(b0, 9, now,
394 q[QUOTA_LIMIT_BLOCK].qv_expiretime);
395 else if ((qup->flags & QUOTA2) != 0 && vflag)
396 timemsg = timeprt(b0, 9, 0,
397 q[QUOTA_LIMIT_BLOCK].qv_grace);
398 else
399 timemsg = "";
400
401 printf("%12s%9s%c%8s%9s%8s",
402 nam,
403 intprt(b1, 9, q[QUOTA_LIMIT_BLOCK].qv_usage,
404 HN_B, hflag),
405 (msgb == NULL) ? ' ' : '*',
406 intprt(b2, 9, q[QUOTA_LIMIT_BLOCK].qv_softlimit,
407 HN_B, hflag),
408 intprt(b3, 9, q[QUOTA_LIMIT_BLOCK].qv_hardlimit,
409 HN_B, hflag),
410 timemsg);
411
412 if (msgi)
413 timemsg = timeprt(b0, 9, now,
414 q[QUOTA_LIMIT_FILE].qv_expiretime);
415 else if ((qup->flags & QUOTA2) != 0 && vflag)
416 timemsg = timeprt(b0, 9, 0,
417 q[QUOTA_LIMIT_FILE].qv_grace);
418 else
419 timemsg = "";
420
421 printf("%8s%c%7s%8s%8s\n",
422 intprt(b1, 8, q[QUOTA_LIMIT_FILE].qv_usage, 0,
423 hflag),
424 (msgi == NULL) ? ' ' : '*',
425 intprt(b2, 8, q[QUOTA_LIMIT_FILE].qv_softlimit,
426 0, hflag),
427 intprt(b3, 8, q[QUOTA_LIMIT_FILE].qv_hardlimit,
428 0, hflag),
429 timemsg);
430 continue;
431 }
432 }
433 if (!qflag && lines == 0)
434 heading(type, id, name, "none");
435 }
436
437 static void
438 heading(int type, uint32_t id, const char *name, const char *tag)
439 {
440 if (dflag)
441 printf("Default %s disk quotas: %s\n",
442 ufs_quota_class_names[type], tag);
443 else
444 printf("Disk quotas for %s %s (%cid %u): %s\n",
445 ufs_quota_class_names[type], name,
446 *ufs_quota_class_names[type], id, tag);
447
448 if (!qflag && tag[0] == '\0') {
449 printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
450 , "Filesystem"
451 , "blocks"
452 , "quota"
453 , "limit"
454 , "grace"
455 , "files"
456 , "quota"
457 , "limit"
458 , "grace"
459 );
460 }
461 }
462
463 /*
464 * Collect the requested quota information.
465 */
466 static struct quotause *
467 getprivs(uint32_t id, int quotatype)
468 {
469 struct quotause *qup, *quptail;
470 struct quotause *quphead;
471 struct statvfs *fst;
472 int nfst, i;
473 int8_t version;
474
475 qup = quphead = quptail = NULL;
476
477 nfst = getmntinfo(&fst, MNT_WAIT);
478 if (nfst == 0)
479 errx(2, "no filesystems mounted!");
480 for (i = 0; i < nfst; i++) {
481 if (qup == NULL) {
482 if ((qup = malloc(sizeof *qup)) == NULL)
483 err(1, "out of memory");
484 }
485 if (strncmp(fst[i].f_fstypename, "nfs",
486 sizeof(fst[i].f_fstypename)) == 0) {
487 version = 0;
488 if (getnfsquota(fst[i].f_mntfromname,
489 qup->qv, id, ufs_quota_class_names[quotatype]) != 1)
490 continue;
491 } else if ((fst[i].f_flag & ST_QUOTA) != 0) {
492 if (getvfsquota(fst[i].f_mntonname, qup->qv, &version,
493 id, quotatype, dflag, Dflag) != 1)
494 continue;
495 } else
496 continue;
497 (void)strncpy(qup->fsname, fst[i].f_mntonname,
498 sizeof(qup->fsname) - 1);
499 if (version == 2)
500 qup->flags |= QUOTA2;
501 if (quphead == NULL)
502 quphead = qup;
503 else
504 quptail->next = qup;
505 quptail = qup;
506 quptail->next = 0;
507 qup = NULL;
508 }
509 free(qup);
510 return quphead;
511 }
512