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