quota.c revision 1.33.2.1 1 1.33.2.1 bouyer /* $NetBSD: quota.c,v 1.33.2.1 2011/01/20 14:25:05 bouyer 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.33.2.1 bouyer __RCSID("$NetBSD: quota.c,v 1.33.2.1 2011/01/20 14:25:05 bouyer 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 #include <sys/queue.h>
59 1.33.2.1 bouyer #include <prop/proplib.h>
60 1.33.2.1 bouyer #include <sys/quota.h>
61 1.15 mrg
62 1.33.2.1 bouyer #include <ufs/ufs/quota2_prop.h>
63 1.15 mrg #include <ctype.h>
64 1.16 lukem #include <err.h>
65 1.15 mrg #include <errno.h>
66 1.15 mrg #include <fstab.h>
67 1.15 mrg #include <grp.h>
68 1.15 mrg #include <netdb.h>
69 1.15 mrg #include <pwd.h>
70 1.1 cgd #include <stdio.h>
71 1.7 cgd #include <stdlib.h>
72 1.16 lukem #include <string.h>
73 1.18 kleink #include <time.h>
74 1.16 lukem #include <unistd.h>
75 1.1 cgd
76 1.6 deraadt #include <rpc/rpc.h>
77 1.6 deraadt #include <rpc/pmap_prot.h>
78 1.6 deraadt #include <rpcsvc/rquota.h>
79 1.6 deraadt
80 1.33 lukem const char *qfextension[] = INITQFNAMES;
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.33.2.1 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.1 cgd
90 1.33.2.1 bouyer int alldigits(char *);
91 1.33.2.1 bouyer int callaurpc(char *, int, int, int, xdrproc_t, void *, xdrproc_t, void *);
92 1.33.2.1 bouyer int main(int, char **);
93 1.33.2.1 bouyer int getnfsquota(struct statvfs *, struct fstab *, struct quotause *,
94 1.33.2.1 bouyer long, int);
95 1.33.2.1 bouyer struct quotause *getprivs(long id, int quotatype);
96 1.33.2.1 bouyer int getufsquota(struct statvfs *, struct quotause *, long, int);
97 1.33.2.1 bouyer void heading(int, u_long, const char *, const char *);
98 1.33.2.1 bouyer void showgid(gid_t);
99 1.33.2.1 bouyer void showgrpname(const char *);
100 1.33.2.1 bouyer void showquotas(int, u_long, const char *);
101 1.33.2.1 bouyer void showuid(uid_t);
102 1.33.2.1 bouyer void showusrname(const char *);
103 1.33.2.1 bouyer const char *intprt(uint64_t, int);
104 1.33.2.1 bouyer const char *timeprt(time_t seconds);
105 1.33.2.1 bouyer void usage(void);
106 1.33.2.1 bouyer
107 1.33.2.1 bouyer int qflag = 0;
108 1.33.2.1 bouyer int vflag = 0;
109 1.33.2.1 bouyer int hflag = 0;
110 1.33.2.1 bouyer int dflag = 0;
111 1.33.2.1 bouyer int Dflag = 0;
112 1.19 mrg uid_t myuid;
113 1.1 cgd
114 1.16 lukem int
115 1.1 cgd main(argc, argv)
116 1.16 lukem int argc;
117 1.1 cgd char *argv[];
118 1.1 cgd {
119 1.3 jtc int ngroups;
120 1.8 mycroft gid_t mygid, gidset[NGROUPS];
121 1.1 cgd int i, gflag = 0, uflag = 0;
122 1.11 mark int ch;
123 1.1 cgd
124 1.19 mrg myuid = getuid();
125 1.33.2.1 bouyer while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
126 1.1 cgd switch(ch) {
127 1.1 cgd case 'g':
128 1.1 cgd gflag++;
129 1.1 cgd break;
130 1.1 cgd case 'u':
131 1.1 cgd uflag++;
132 1.1 cgd break;
133 1.1 cgd case 'v':
134 1.1 cgd vflag++;
135 1.1 cgd break;
136 1.1 cgd case 'q':
137 1.1 cgd qflag++;
138 1.1 cgd break;
139 1.33.2.1 bouyer case 'h':
140 1.33.2.1 bouyer hflag++;
141 1.33.2.1 bouyer break;
142 1.33.2.1 bouyer case 'd':
143 1.33.2.1 bouyer dflag++;
144 1.33.2.1 bouyer break;
145 1.33.2.1 bouyer case 'D':
146 1.33.2.1 bouyer Dflag++;
147 1.33.2.1 bouyer break;
148 1.1 cgd default:
149 1.1 cgd usage();
150 1.1 cgd }
151 1.1 cgd }
152 1.1 cgd argc -= optind;
153 1.1 cgd argv += optind;
154 1.1 cgd if (!uflag && !gflag)
155 1.1 cgd uflag++;
156 1.33.2.1 bouyer if (dflag) {
157 1.33.2.1 bouyer #if 0
158 1.33.2.1 bouyer if (myuid != 0) {
159 1.33.2.1 bouyer printf("quota: -d: permission denied\n");
160 1.33.2.1 bouyer exit(1);
161 1.33.2.1 bouyer }
162 1.33.2.1 bouyer #endif
163 1.33.2.1 bouyer if (uflag)
164 1.33.2.1 bouyer showquotas(USRQUOTA, 0, "");
165 1.33.2.1 bouyer if (gflag)
166 1.33.2.1 bouyer showquotas(GRPQUOTA, 0, "");
167 1.33.2.1 bouyer exit(0);
168 1.33.2.1 bouyer }
169 1.1 cgd if (argc == 0) {
170 1.1 cgd if (uflag)
171 1.19 mrg showuid(myuid);
172 1.1 cgd if (gflag) {
173 1.33.2.1 bouyer if (dflag)
174 1.33.2.1 bouyer showgid(0);
175 1.33.2.1 bouyer else {
176 1.33.2.1 bouyer mygid = getgid();
177 1.33.2.1 bouyer ngroups = getgroups(NGROUPS, gidset);
178 1.33.2.1 bouyer if (ngroups < 0)
179 1.33.2.1 bouyer err(1, "getgroups");
180 1.33.2.1 bouyer showgid(mygid);
181 1.33.2.1 bouyer for (i = 0; i < ngroups; i++)
182 1.33.2.1 bouyer if (gidset[i] != mygid)
183 1.33.2.1 bouyer showgid(gidset[i]);
184 1.33.2.1 bouyer }
185 1.1 cgd }
186 1.1 cgd exit(0);
187 1.1 cgd }
188 1.1 cgd if (uflag && gflag)
189 1.1 cgd usage();
190 1.1 cgd if (uflag) {
191 1.1 cgd for (; argc > 0; argc--, argv++) {
192 1.1 cgd if (alldigits(*argv))
193 1.1 cgd showuid(atoi(*argv));
194 1.1 cgd else
195 1.1 cgd showusrname(*argv);
196 1.1 cgd }
197 1.1 cgd exit(0);
198 1.1 cgd }
199 1.1 cgd if (gflag) {
200 1.1 cgd for (; argc > 0; argc--, argv++) {
201 1.1 cgd if (alldigits(*argv))
202 1.1 cgd showgid(atoi(*argv));
203 1.1 cgd else
204 1.1 cgd showgrpname(*argv);
205 1.1 cgd }
206 1.1 cgd exit(0);
207 1.1 cgd }
208 1.16 lukem /* NOTREACHED */
209 1.16 lukem return (0);
210 1.1 cgd }
211 1.1 cgd
212 1.16 lukem void
213 1.1 cgd usage()
214 1.1 cgd {
215 1.1 cgd
216 1.33.2.1 bouyer fprintf(stderr, "%s\n%s\n%s\n%s\n",
217 1.33.2.1 bouyer "usage: quota [-Dhguqv]",
218 1.33.2.1 bouyer "\tquota [-Dhqv] -u username ...",
219 1.33.2.1 bouyer "\tquota [-Dhqv] -g groupname ...",
220 1.33.2.1 bouyer "\tquota -d [-Dhguqv]");
221 1.1 cgd exit(1);
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd /*
225 1.1 cgd * Print out quotas for a specified user identifier.
226 1.1 cgd */
227 1.16 lukem void
228 1.1 cgd showuid(uid)
229 1.16 lukem uid_t uid;
230 1.1 cgd {
231 1.1 cgd struct passwd *pwd = getpwuid(uid);
232 1.20 mycroft const char *name;
233 1.1 cgd
234 1.1 cgd if (pwd == NULL)
235 1.1 cgd name = "(no account)";
236 1.1 cgd else
237 1.1 cgd name = pwd->pw_name;
238 1.1 cgd if (uid != myuid && myuid != 0) {
239 1.1 cgd printf("quota: %s (uid %d): permission denied\n", name, uid);
240 1.1 cgd return;
241 1.1 cgd }
242 1.1 cgd showquotas(USRQUOTA, uid, name);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd /*
246 1.24 wiz * Print out quotas for a specified user name.
247 1.1 cgd */
248 1.16 lukem void
249 1.1 cgd showusrname(name)
250 1.20 mycroft const char *name;
251 1.1 cgd {
252 1.1 cgd struct passwd *pwd = getpwnam(name);
253 1.1 cgd
254 1.1 cgd if (pwd == NULL) {
255 1.16 lukem warnx("%s: unknown user", name);
256 1.1 cgd return;
257 1.1 cgd }
258 1.1 cgd if (pwd->pw_uid != myuid && myuid != 0) {
259 1.16 lukem warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
260 1.1 cgd return;
261 1.1 cgd }
262 1.1 cgd showquotas(USRQUOTA, pwd->pw_uid, name);
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd /*
266 1.1 cgd * Print out quotas for a specified group identifier.
267 1.1 cgd */
268 1.16 lukem void
269 1.1 cgd showgid(gid)
270 1.16 lukem gid_t gid;
271 1.1 cgd {
272 1.1 cgd struct group *grp = getgrgid(gid);
273 1.3 jtc int ngroups;
274 1.8 mycroft gid_t mygid, gidset[NGROUPS];
275 1.16 lukem int i;
276 1.20 mycroft const char *name;
277 1.1 cgd
278 1.1 cgd if (grp == NULL)
279 1.1 cgd name = "(no entry)";
280 1.1 cgd else
281 1.1 cgd name = grp->gr_name;
282 1.8 mycroft mygid = getgid();
283 1.1 cgd ngroups = getgroups(NGROUPS, gidset);
284 1.1 cgd if (ngroups < 0) {
285 1.16 lukem warn("getgroups");
286 1.1 cgd return;
287 1.1 cgd }
288 1.8 mycroft if (gid != mygid) {
289 1.8 mycroft for (i = 0; i < ngroups; i++)
290 1.8 mycroft if (gid == gidset[i])
291 1.8 mycroft break;
292 1.19 mrg if (i >= ngroups && myuid != 0) {
293 1.16 lukem warnx("%s (gid %d): permission denied", name, gid);
294 1.8 mycroft return;
295 1.8 mycroft }
296 1.1 cgd }
297 1.1 cgd showquotas(GRPQUOTA, gid, name);
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd /*
301 1.24 wiz * Print out quotas for a specified group name.
302 1.1 cgd */
303 1.16 lukem void
304 1.1 cgd showgrpname(name)
305 1.20 mycroft const char *name;
306 1.1 cgd {
307 1.1 cgd struct group *grp = getgrnam(name);
308 1.3 jtc int ngroups;
309 1.8 mycroft gid_t mygid, gidset[NGROUPS];
310 1.16 lukem int i;
311 1.1 cgd
312 1.1 cgd if (grp == NULL) {
313 1.16 lukem warnx("%s: unknown group", name);
314 1.1 cgd return;
315 1.1 cgd }
316 1.8 mycroft mygid = getgid();
317 1.1 cgd ngroups = getgroups(NGROUPS, gidset);
318 1.1 cgd if (ngroups < 0) {
319 1.16 lukem warn("getgroups");
320 1.1 cgd return;
321 1.1 cgd }
322 1.8 mycroft if (grp->gr_gid != mygid) {
323 1.8 mycroft for (i = 0; i < ngroups; i++)
324 1.8 mycroft if (grp->gr_gid == gidset[i])
325 1.8 mycroft break;
326 1.19 mrg if (i >= ngroups && myuid != 0) {
327 1.16 lukem warnx("%s (gid %d): permission denied",
328 1.8 mycroft name, grp->gr_gid);
329 1.8 mycroft return;
330 1.8 mycroft }
331 1.1 cgd }
332 1.1 cgd showquotas(GRPQUOTA, grp->gr_gid, name);
333 1.1 cgd }
334 1.1 cgd
335 1.16 lukem void
336 1.1 cgd showquotas(type, id, name)
337 1.1 cgd int type;
338 1.1 cgd u_long id;
339 1.20 mycroft const char *name;
340 1.1 cgd {
341 1.16 lukem struct quotause *qup;
342 1.6 deraadt struct quotause *quplist;
343 1.33 lukem const char *msgi, *msgb, *nam;
344 1.16 lukem int lines = 0;
345 1.1 cgd static time_t now;
346 1.1 cgd
347 1.1 cgd if (now == 0)
348 1.1 cgd time(&now);
349 1.1 cgd quplist = getprivs(id, type);
350 1.1 cgd for (qup = quplist; qup; qup = qup->next) {
351 1.1 cgd if (!vflag &&
352 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit == UQUAD_MAX &&
353 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit == UQUAD_MAX &&
354 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit == UQUAD_MAX &&
355 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit == UQUAD_MAX)
356 1.1 cgd continue;
357 1.33 lukem msgi = NULL;
358 1.33.2.1 bouyer if (qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit &&
359 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_cur >=
360 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit)
361 1.1 cgd msgi = "File limit reached on";
362 1.33.2.1 bouyer else if (qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit &&
363 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_cur >=
364 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit) {
365 1.33.2.1 bouyer if (qup->q2e.q2e_val[Q2V_FILE].q2v_time > now)
366 1.1 cgd msgi = "In file grace period on";
367 1.1 cgd else
368 1.1 cgd msgi = "Over file quota on";
369 1.21 ross }
370 1.33 lukem msgb = NULL;
371 1.33.2.1 bouyer if (qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit &&
372 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur >=
373 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit)
374 1.1 cgd msgb = "Block limit reached on";
375 1.33.2.1 bouyer else if (qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit &&
376 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur >=
377 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit) {
378 1.33.2.1 bouyer if (qup->q2e.q2e_val[Q2V_BLOCK].q2v_time > now)
379 1.33.2.1 bouyer msgb = "In block grace period on";
380 1.33.2.1 bouyer else
381 1.33.2.1 bouyer msgb = "Over block quota on";
382 1.21 ross }
383 1.1 cgd if (qflag) {
384 1.33 lukem if ((msgi != NULL || msgb != NULL) &&
385 1.1 cgd lines++ == 0)
386 1.1 cgd heading(type, id, name, "");
387 1.33 lukem if (msgi != NULL)
388 1.1 cgd printf("\t%s %s\n", msgi, qup->fsname);
389 1.33 lukem if (msgb != NULL)
390 1.1 cgd printf("\t%s %s\n", msgb, qup->fsname);
391 1.1 cgd continue;
392 1.1 cgd }
393 1.33.2.1 bouyer if (vflag || dflag ||
394 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur ||
395 1.33.2.1 bouyer qup->q2e.q2e_val[Q2V_FILE].q2v_cur) {
396 1.1 cgd if (lines++ == 0)
397 1.1 cgd heading(type, id, name, "");
398 1.6 deraadt nam = qup->fsname;
399 1.33.2.1 bouyer if (strlen(qup->fsname) > 4) {
400 1.6 deraadt printf("%s\n", qup->fsname);
401 1.6 deraadt nam = "";
402 1.6 deraadt }
403 1.33.2.1 bouyer printf("%12s%9s%c%8s%9s%8s"
404 1.22 bouyer , nam
405 1.33.2.1 bouyer , intprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_cur
406 1.33.2.1 bouyer ,HN_B)
407 1.33 lukem , (msgb == NULL) ? ' ' : '*'
408 1.33.2.1 bouyer , intprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_softlimit
409 1.33.2.1 bouyer , HN_B)
410 1.33.2.1 bouyer , intprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_hardlimit
411 1.33.2.1 bouyer , HN_B)
412 1.33 lukem , (msgb == NULL) ? ""
413 1.33.2.1 bouyer : timeprt(qup->q2e.q2e_val[Q2V_BLOCK].q2v_time));
414 1.33.2.1 bouyer printf("%8s%c%7s%8s%8s\n"
415 1.33.2.1 bouyer , intprt(qup->q2e.q2e_val[Q2V_FILE].q2v_cur, 0)
416 1.33 lukem , (msgi == NULL) ? ' ' : '*'
417 1.33.2.1 bouyer , intprt(qup->q2e.q2e_val[Q2V_FILE].q2v_softlimit
418 1.33.2.1 bouyer , 0)
419 1.33.2.1 bouyer , intprt(qup->q2e.q2e_val[Q2V_FILE].q2v_hardlimit
420 1.33.2.1 bouyer , 0)
421 1.33 lukem , (msgi == NULL) ? ""
422 1.33.2.1 bouyer : timeprt(qup->q2e.q2e_val[Q2V_FILE].q2v_time)
423 1.1 cgd );
424 1.1 cgd continue;
425 1.1 cgd }
426 1.1 cgd }
427 1.1 cgd if (!qflag && lines == 0)
428 1.1 cgd heading(type, id, name, "none");
429 1.1 cgd }
430 1.1 cgd
431 1.16 lukem void
432 1.1 cgd heading(type, id, name, tag)
433 1.1 cgd int type;
434 1.1 cgd u_long id;
435 1.20 mycroft const char *name, *tag;
436 1.1 cgd {
437 1.33.2.1 bouyer if (dflag)
438 1.33.2.1 bouyer printf("Default %s disk quotas: %s\n",
439 1.33.2.1 bouyer qfextension[type], tag);
440 1.33.2.1 bouyer else
441 1.33.2.1 bouyer printf("Disk quotas for %s %s (%cid %ld): %s\n",
442 1.33.2.1 bouyer qfextension[type], name, *qfextension[type],
443 1.33.2.1 bouyer (u_long)id, tag);
444 1.1 cgd
445 1.1 cgd if (!qflag && tag[0] == '\0') {
446 1.22 bouyer printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
447 1.22 bouyer , "Filesystem"
448 1.22 bouyer , "blocks"
449 1.22 bouyer , "quota"
450 1.22 bouyer , "limit"
451 1.22 bouyer , "grace"
452 1.22 bouyer , "files"
453 1.22 bouyer , "quota"
454 1.22 bouyer , "limit"
455 1.22 bouyer , "grace"
456 1.1 cgd );
457 1.1 cgd }
458 1.1 cgd }
459 1.1 cgd
460 1.1 cgd /*
461 1.33.2.1 bouyer * convert 64bit value to a printable string
462 1.33.2.1 bouyer */
463 1.33.2.1 bouyer const char *
464 1.33.2.1 bouyer intprt(uint64_t val, int flags)
465 1.33.2.1 bouyer {
466 1.33.2.1 bouyer static char buf[21];
467 1.33.2.1 bouyer
468 1.33.2.1 bouyer if (val == UQUAD_MAX)
469 1.33.2.1 bouyer return("-");
470 1.33.2.1 bouyer
471 1.33.2.1 bouyer if (flags & HN_B)
472 1.33.2.1 bouyer val = dbtob(val);
473 1.33.2.1 bouyer
474 1.33.2.1 bouyer if (hflag) {
475 1.33.2.1 bouyer humanize_number(buf, 6, val, "", HN_AUTOSCALE, flags);
476 1.33.2.1 bouyer return buf;
477 1.33.2.1 bouyer }
478 1.33.2.1 bouyer if (flags & HN_B) {
479 1.33.2.1 bouyer /* traditionnal display: blocks are in kilobytes */
480 1.33.2.1 bouyer val = val / 1024;
481 1.33.2.1 bouyer }
482 1.33.2.1 bouyer snprintf(buf, sizeof(buf), "%" PRIu64, val);
483 1.33.2.1 bouyer return buf;
484 1.33.2.1 bouyer }
485 1.33.2.1 bouyer
486 1.33.2.1 bouyer /*
487 1.1 cgd * Calculate the grace period and return a printable string for it.
488 1.1 cgd */
489 1.33 lukem const char *
490 1.33.2.1 bouyer timeprt(time_t seconds)
491 1.1 cgd {
492 1.1 cgd time_t hours, minutes;
493 1.1 cgd static char buf[20];
494 1.1 cgd static time_t now;
495 1.1 cgd
496 1.1 cgd if (now == 0)
497 1.1 cgd time(&now);
498 1.1 cgd if (now > seconds)
499 1.1 cgd return ("none");
500 1.1 cgd seconds -= now;
501 1.1 cgd minutes = (seconds + 30) / 60;
502 1.1 cgd hours = (minutes + 30) / 60;
503 1.1 cgd if (hours >= 36) {
504 1.16 lukem (void)snprintf(buf, sizeof buf, "%ddays",
505 1.16 lukem (int)((hours + 12) / 24));
506 1.1 cgd return (buf);
507 1.1 cgd }
508 1.1 cgd if (minutes >= 60) {
509 1.16 lukem (void)snprintf(buf, sizeof buf, "%2d:%d",
510 1.16 lukem (int)(minutes / 60), (int)(minutes % 60));
511 1.1 cgd return (buf);
512 1.1 cgd }
513 1.16 lukem (void)snprintf(buf, sizeof buf, "%2d", (int)minutes);
514 1.1 cgd return (buf);
515 1.1 cgd }
516 1.1 cgd
517 1.1 cgd /*
518 1.1 cgd * Collect the requested quota information.
519 1.1 cgd */
520 1.1 cgd struct quotause *
521 1.1 cgd getprivs(id, quotatype)
522 1.16 lukem long id;
523 1.1 cgd int quotatype;
524 1.1 cgd {
525 1.16 lukem struct quotause *qup, *quptail;
526 1.1 cgd struct quotause *quphead;
527 1.29 christos struct statvfs *fst;
528 1.6 deraadt int nfst, i;
529 1.6 deraadt
530 1.16 lukem qup = quphead = quptail = NULL;
531 1.1 cgd
532 1.6 deraadt nfst = getmntinfo(&fst, MNT_WAIT);
533 1.16 lukem if (nfst == 0)
534 1.16 lukem errx(2, "no filesystems mounted!");
535 1.1 cgd setfsent();
536 1.19 mrg for (i = 0; i < nfst; i++) {
537 1.6 deraadt if (qup == NULL) {
538 1.16 lukem if ((qup =
539 1.16 lukem (struct quotause *)malloc(sizeof *qup)) == NULL)
540 1.16 lukem errx(2, "out of memory");
541 1.1 cgd }
542 1.31 christos if (strncmp(fst[i].f_fstypename, "nfs",
543 1.31 christos sizeof(fst[i].f_fstypename)) == 0) {
544 1.6 deraadt if (getnfsquota(&fst[i], NULL, qup, id, quotatype) == 0)
545 1.6 deraadt continue;
546 1.10 jtc } else if (strncmp(fst[i].f_fstypename, "ffs",
547 1.33.2.1 bouyer sizeof(fst[i].f_fstypename)) == 0 &&
548 1.33.2.1 bouyer (fst[i].f_flag &ST_QUOTA) != 0) {
549 1.33.2.1 bouyer if (getufsquota(&fst[i], qup, id, quotatype) == 0)
550 1.1 cgd continue;
551 1.6 deraadt } else
552 1.6 deraadt continue;
553 1.13 mrg (void)strncpy(qup->fsname, fst[i].f_mntonname,
554 1.13 mrg sizeof(qup->fsname) - 1);
555 1.1 cgd if (quphead == NULL)
556 1.1 cgd quphead = qup;
557 1.1 cgd else
558 1.1 cgd quptail->next = qup;
559 1.1 cgd quptail = qup;
560 1.6 deraadt quptail->next = 0;
561 1.6 deraadt qup = NULL;
562 1.1 cgd }
563 1.6 deraadt if (qup)
564 1.6 deraadt free(qup);
565 1.1 cgd endfsent();
566 1.1 cgd return (quphead);
567 1.1 cgd }
568 1.1 cgd
569 1.6 deraadt
570 1.6 deraadt int
571 1.33.2.1 bouyer getufsquota(struct statvfs *fst, struct quotause *qup, long id, int type)
572 1.6 deraadt {
573 1.33.2.1 bouyer prop_dictionary_t dict, data, cmd;
574 1.33.2.1 bouyer prop_array_t cmds, datas;
575 1.33.2.1 bouyer struct plistref pref;
576 1.33.2.1 bouyer int error;
577 1.33.2.1 bouyer int8_t error8;
578 1.33.2.1 bouyer bool ret;
579 1.33.2.1 bouyer
580 1.33.2.1 bouyer dict = quota2_prop_create();
581 1.33.2.1 bouyer cmds = prop_array_create();
582 1.33.2.1 bouyer datas = prop_array_create();
583 1.33.2.1 bouyer data = prop_dictionary_create();
584 1.6 deraadt
585 1.33.2.1 bouyer if (dict == NULL || cmds == NULL || datas == NULL || data == NULL)
586 1.33.2.1 bouyer errx(1, "can't allocate proplist");
587 1.6 deraadt
588 1.33.2.1 bouyer if (dflag)
589 1.33.2.1 bouyer ret = prop_dictionary_set_cstring(data, "id", "default");
590 1.33.2.1 bouyer else
591 1.33.2.1 bouyer ret = prop_dictionary_set_uint32(data, "id", id);
592 1.33.2.1 bouyer if (!ret)
593 1.33.2.1 bouyer err(1, "prop_dictionary_set(id)");
594 1.33.2.1 bouyer
595 1.33.2.1 bouyer if (!prop_array_add(datas, data))
596 1.33.2.1 bouyer err(1, "prop_array_add(data)");
597 1.33.2.1 bouyer prop_object_release(data);
598 1.33.2.1 bouyer if (!quota2_prop_add_command(cmds, "get", qfextension[type], datas))
599 1.33.2.1 bouyer err(1, "prop_add_command");
600 1.33.2.1 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
601 1.33.2.1 bouyer err(1, "prop_dictionary_set(command)");
602 1.33.2.1 bouyer if (Dflag)
603 1.33.2.1 bouyer printf("message to kernel:\n%s\n",
604 1.33.2.1 bouyer prop_dictionary_externalize(dict));
605 1.33.2.1 bouyer
606 1.33.2.1 bouyer if (!prop_dictionary_send_syscall(dict, &pref))
607 1.33.2.1 bouyer err(1, "prop_dictionary_send_syscall");
608 1.33.2.1 bouyer prop_object_release(dict);
609 1.33.2.1 bouyer
610 1.33.2.1 bouyer if (quotactl(fst->f_mntonname, &pref) != 0)
611 1.33.2.1 bouyer err(1, "quotactl");
612 1.33.2.1 bouyer
613 1.33.2.1 bouyer if ((error = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
614 1.33.2.1 bouyer errx(1, "prop_dictionary_recv_syscall: %s\n",
615 1.33.2.1 bouyer strerror(error));
616 1.33.2.1 bouyer }
617 1.33.2.1 bouyer if (Dflag)
618 1.33.2.1 bouyer printf("reply from kernel:\n%s\n",
619 1.33.2.1 bouyer prop_dictionary_externalize(dict));
620 1.33.2.1 bouyer if ((error = quota2_get_cmds(dict, &cmds)) != 0) {
621 1.33.2.1 bouyer errx(1, "quota2_get_cmds: %s\n",
622 1.33.2.1 bouyer strerror(error));
623 1.33.2.1 bouyer }
624 1.33.2.1 bouyer /* only one command, no need to iter */
625 1.33.2.1 bouyer cmd = prop_array_get(cmds, 0);
626 1.33.2.1 bouyer if (cmd == NULL)
627 1.33.2.1 bouyer err(1, "prop_array_get(cmd)");
628 1.33.2.1 bouyer
629 1.33.2.1 bouyer if (!prop_dictionary_get_int8(cmd, "return", &error8))
630 1.33.2.1 bouyer err(1, "prop_get(return)");
631 1.33.2.1 bouyer
632 1.33.2.1 bouyer if (error8) {
633 1.33.2.1 bouyer if (error8 != ENOENT && error8 != ENODEV) {
634 1.33.2.1 bouyer if (dflag)
635 1.33.2.1 bouyer fprintf(stderr, "get default %s quota: %s\n",
636 1.33.2.1 bouyer qfextension[type], strerror(error8));
637 1.33.2.1 bouyer else
638 1.33.2.1 bouyer fprintf(stderr, "get %s quota for %ld: %s\n",
639 1.33.2.1 bouyer qfextension[type], id, strerror(error8));
640 1.6 deraadt }
641 1.33.2.1 bouyer prop_object_release(dict);
642 1.33.2.1 bouyer return (0);
643 1.33.2.1 bouyer }
644 1.33.2.1 bouyer datas = prop_dictionary_get(cmd, "data");
645 1.33.2.1 bouyer if (datas == NULL)
646 1.33.2.1 bouyer err(1, "prop_dict_get(datas)");
647 1.33.2.1 bouyer
648 1.33.2.1 bouyer /* only one data, no need to iter */
649 1.33.2.1 bouyer if (prop_array_count(datas) == 0) {
650 1.33.2.1 bouyer /* no quota for this user/group */
651 1.33.2.1 bouyer prop_object_release(dict);
652 1.33.2.1 bouyer return (0);
653 1.33.2.1 bouyer }
654 1.33.2.1 bouyer
655 1.33.2.1 bouyer data = prop_array_get(datas, 0);
656 1.33.2.1 bouyer if (data == NULL)
657 1.33.2.1 bouyer err(1, "prop_array_get(data)");
658 1.33.2.1 bouyer
659 1.33.2.1 bouyer error = quota2_dict_get_q2e_usage(data, &qup->q2e);
660 1.33.2.1 bouyer if (error) {
661 1.33.2.1 bouyer errx(1, "quota2_dict_get_q2e_usage: %s\n",
662 1.33.2.1 bouyer strerror(error));
663 1.6 deraadt }
664 1.6 deraadt return (1);
665 1.6 deraadt }
666 1.6 deraadt
667 1.6 deraadt int
668 1.6 deraadt getnfsquota(fst, fs, qup, id, quotatype)
669 1.29 christos struct statvfs *fst;
670 1.6 deraadt struct fstab *fs;
671 1.6 deraadt struct quotause *qup;
672 1.6 deraadt long id;
673 1.6 deraadt int quotatype;
674 1.6 deraadt {
675 1.6 deraadt struct getquota_args gq_args;
676 1.25 bouyer struct ext_getquota_args ext_gq_args;
677 1.6 deraadt struct getquota_rslt gq_rslt;
678 1.33.2.1 bouyer struct quota2_entry *q2e = &qup->q2e;
679 1.6 deraadt struct timeval tv;
680 1.6 deraadt char *cp;
681 1.25 bouyer int ret;
682 1.6 deraadt
683 1.29 christos if (fst->f_flag & MNT_LOCAL)
684 1.6 deraadt return (0);
685 1.6 deraadt
686 1.6 deraadt /*
687 1.6 deraadt * must be some form of "hostname:/path"
688 1.6 deraadt */
689 1.6 deraadt cp = strchr(fst->f_mntfromname, ':');
690 1.6 deraadt if (cp == NULL) {
691 1.16 lukem warnx("cannot find hostname for %s", fst->f_mntfromname);
692 1.6 deraadt return (0);
693 1.6 deraadt }
694 1.6 deraadt
695 1.6 deraadt *cp = '\0';
696 1.6 deraadt if (*(cp+1) != '/') {
697 1.6 deraadt *cp = ':';
698 1.6 deraadt return (0);
699 1.6 deraadt }
700 1.6 deraadt
701 1.25 bouyer ext_gq_args.gqa_pathp = cp + 1;
702 1.25 bouyer ext_gq_args.gqa_id = id;
703 1.25 bouyer ext_gq_args.gqa_type =
704 1.25 bouyer (quotatype == USRQUOTA) ? RQUOTA_USRQUOTA : RQUOTA_GRPQUOTA;
705 1.25 bouyer ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, EXT_RQUOTAVERS,
706 1.25 bouyer RQUOTAPROC_GETQUOTA, xdr_ext_getquota_args, &ext_gq_args,
707 1.25 bouyer xdr_getquota_rslt, &gq_rslt);
708 1.27 bouyer if (ret == RPC_PROGVERSMISMATCH) {
709 1.25 bouyer if (quotatype != USRQUOTA) {
710 1.25 bouyer *cp = ':';
711 1.25 bouyer return (0);
712 1.25 bouyer }
713 1.25 bouyer /* try RQUOTAVERS */
714 1.25 bouyer gq_args.gqa_pathp = cp + 1;
715 1.25 bouyer gq_args.gqa_uid = id;
716 1.25 bouyer ret = callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
717 1.25 bouyer RQUOTAPROC_GETQUOTA, xdr_getquota_args, &gq_args,
718 1.25 bouyer xdr_getquota_rslt, &gq_rslt);
719 1.25 bouyer }
720 1.25 bouyer if (ret != RPC_SUCCESS) {
721 1.6 deraadt *cp = ':';
722 1.6 deraadt return (0);
723 1.6 deraadt }
724 1.6 deraadt
725 1.6 deraadt switch (gq_rslt.status) {
726 1.6 deraadt case Q_NOQUOTA:
727 1.6 deraadt break;
728 1.6 deraadt case Q_EPERM:
729 1.16 lukem warnx("quota permission error, host: %s", fst->f_mntfromname);
730 1.6 deraadt break;
731 1.6 deraadt case Q_OK:
732 1.6 deraadt gettimeofday(&tv, NULL);
733 1.6 deraadt /* blocks*/
734 1.33.2.1 bouyer q2e->q2e_val[Q2V_BLOCK].q2v_hardlimit =
735 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
736 1.23 cgd (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
737 1.33.2.1 bouyer q2e->q2e_val[Q2V_BLOCK].q2v_softlimit =
738 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
739 1.23 cgd (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
740 1.33.2.1 bouyer q2e->q2e_val[Q2V_BLOCK].q2v_cur =
741 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
742 1.23 cgd (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
743 1.6 deraadt /* inodes */
744 1.33.2.1 bouyer q2e->q2e_val[Q2V_FILE].q2v_hardlimit =
745 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
746 1.33.2.1 bouyer q2e->q2e_val[Q2V_FILE].q2v_softlimit =
747 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
748 1.33.2.1 bouyer q2e->q2e_val[Q2V_FILE].q2v_cur =
749 1.6 deraadt gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
750 1.6 deraadt /* grace times */
751 1.33.2.1 bouyer q2e->q2e_val[Q2V_BLOCK].q2v_time =
752 1.6 deraadt tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
753 1.33.2.1 bouyer q2e->q2e_val[Q2V_FILE].q2v_time =
754 1.6 deraadt tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
755 1.6 deraadt *cp = ':';
756 1.6 deraadt return (1);
757 1.6 deraadt default:
758 1.16 lukem warnx("bad rpc result, host: %s", fst->f_mntfromname);
759 1.6 deraadt break;
760 1.6 deraadt }
761 1.6 deraadt *cp = ':';
762 1.6 deraadt return (0);
763 1.6 deraadt }
764 1.6 deraadt
765 1.6 deraadt int
766 1.6 deraadt callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
767 1.6 deraadt char *host;
768 1.16 lukem int prognum, versnum, procnum;
769 1.16 lukem xdrproc_t inproc;
770 1.16 lukem void *in;
771 1.16 lukem xdrproc_t outproc;
772 1.16 lukem void *out;
773 1.6 deraadt {
774 1.6 deraadt struct sockaddr_in server_addr;
775 1.6 deraadt enum clnt_stat clnt_stat;
776 1.6 deraadt struct hostent *hp;
777 1.6 deraadt struct timeval timeout, tottimeout;
778 1.6 deraadt
779 1.6 deraadt CLIENT *client = NULL;
780 1.33 lukem int sock = RPC_ANYSOCK;
781 1.6 deraadt
782 1.6 deraadt if ((hp = gethostbyname(host)) == NULL)
783 1.6 deraadt return ((int) RPC_UNKNOWNHOST);
784 1.6 deraadt timeout.tv_usec = 0;
785 1.6 deraadt timeout.tv_sec = 6;
786 1.16 lukem memmove(&server_addr.sin_addr, hp->h_addr, hp->h_length);
787 1.6 deraadt server_addr.sin_family = AF_INET;
788 1.6 deraadt server_addr.sin_port = 0;
789 1.6 deraadt
790 1.6 deraadt if ((client = clntudp_create(&server_addr, prognum,
791 1.33 lukem versnum, timeout, &sock)) == NULL)
792 1.6 deraadt return ((int) rpc_createerr.cf_stat);
793 1.6 deraadt
794 1.6 deraadt client->cl_auth = authunix_create_default();
795 1.6 deraadt tottimeout.tv_sec = 25;
796 1.6 deraadt tottimeout.tv_usec = 0;
797 1.6 deraadt clnt_stat = clnt_call(client, procnum, inproc, in,
798 1.6 deraadt outproc, out, tottimeout);
799 1.6 deraadt
800 1.6 deraadt return ((int) clnt_stat);
801 1.1 cgd }
802 1.1 cgd
803 1.16 lukem int
804 1.1 cgd alldigits(s)
805 1.16 lukem char *s;
806 1.1 cgd {
807 1.16 lukem int c;
808 1.1 cgd
809 1.1 cgd c = *s++;
810 1.1 cgd do {
811 1.1 cgd if (!isdigit(c))
812 1.1 cgd return (0);
813 1.16 lukem } while ((c = *s++) != 0);
814 1.1 cgd return (1);
815 1.1 cgd }
816