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