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