]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/rquotad/rquota_server.c
Copy dq_dqb into rquota structure more carefully
[nfs-utils.git] / utils / rquotad / rquota_server.c
1 /*
2  * QUOTA    An implementation of the diskquota system for the LINUX
3  *          operating system. QUOTA is implemented using the BSD systemcall
4  *          interface as the means of communication with the user level.
5  *          Should work for all filesystems because of integration into the
6  *          VFS layer of the operating system.
7  *          This is based on the Melbourne quota system wich uses both user and
8  *          group quota files.
9  *
10  *          This part does the lookup of the info.
11  *
12  * Version: $Id: rquota_server.c,v 2.9 1996/11/17 16:59:46 mvw Exp mvw $
13  *
14  * Author:  Marco van Wieringen <mvw@planets.elm.net>
15  *
16  *          This program is free software; you can redistribute it and/or
17  *          modify it under the terms of the GNU General Public License
18  *          as published by the Free Software Foundation; either version
19  *          2 of the License, or (at your option) any later version.
20  */
21 #include "config.h"
22
23 #include <rpc/rpc.h>
24 #include "rquota.h"
25 #include <sys/file.h>
26 #include <sys/stat.h>
27 #include <sys/param.h>
28 #include <sys/quota.h>
29 #include <sys/mount.h>
30 #include <dirent.h>
31 #include <paths.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include "mntent.h"
36 #include "xmalloc.h"
37
38 #define TYPE_EXTENDED   0x01
39 #define ACTIVE          0x02
40
41 #ifndef MNTTYPE_AUTOFS
42 #define MNTTYPE_AUTOFS  "autofs"
43 #endif
44
45 #ifndef BLOCK_SIZE
46 #define BLOCK_SIZE 1024
47 #endif
48
49 /*
50  * Global unix authentication credentials.
51  */
52 extern struct authunix_parms *unix_cred;
53
54 int in_group (gid_t *gids, u_int len, gid_t gid)
55 {
56    int cnt = 0;
57
58    while (cnt < len) {
59       if (gids[cnt] == gid)
60          return 1;
61       cnt++;
62    }
63    return 0;
64 }
65
66 getquota_rslt *getquotainfo(int flags, caddr_t *argp, struct svc_req *rqstp)
67 {
68    static getquota_rslt result;
69    union {
70       getquota_args *args;
71       ext_getquota_args *ext_args;
72    } arguments;
73    FILE *fp;
74    struct dqblk dq_dqb;
75    struct mntent *mnt;
76    char *pathname, *qfpathname;
77    int fd, err, id, type;
78    struct stat stm, stn;
79    struct rquota *rquota;
80
81    /*
82     * First check authentication.
83     */
84    if (flags & TYPE_EXTENDED) {
85       arguments.ext_args = (ext_getquota_args *)argp;
86       id = arguments.ext_args->gqa_id;
87       type = arguments.ext_args->gqa_type;
88       pathname = arguments.ext_args->gqa_pathp;
89
90       if (type == USRQUOTA && unix_cred->aup_uid && unix_cred->aup_uid != id) {
91          result.status = Q_EPERM;
92          return(&result);
93       }
94
95       if (type == GRPQUOTA && unix_cred->aup_uid && unix_cred->aup_gid != id &&
96           !in_group((gid_t *)unix_cred->aup_gids, unix_cred->aup_len, id)) {
97          result.status = Q_EPERM;
98          return(&result);
99       }
100    } else {
101       arguments.args = (getquota_args *)argp;
102       id = arguments.args->gqa_uid;
103       type = USRQUOTA;
104       pathname = arguments.args->gqa_pathp;
105
106       if (unix_cred->aup_uid && unix_cred->aup_uid != id) {
107          result.status = Q_EPERM;
108          return(&result);
109       }
110    }
111
112    fp = setmntent(MNTTAB, "r");
113    while ((mnt = getmntent(fp)) != (struct mntent *)0) {
114       if (stat(mnt->mnt_dir, &stm) == -1)
115           continue;
116
117       if (stat(pathname, &stn) == -1)
118           break;
119       else if (stm.st_dev != stn.st_dev)
120           continue;
121
122       if (mnt->mnt_fsname [0] != '/'
123           || strcasecmp (mnt->mnt_type, MNTTYPE_NFS) == 0
124           || strcasecmp (mnt->mnt_type, MNTTYPE_AUTOFS) == 0
125           || strcasecmp (mnt->mnt_type, MNTTYPE_SWAP) == 0
126           || strcasecmp (mnt->mnt_type, MNTTYPE_IGNORE) == 0)
127          break;
128
129       /* All blocks reported are in BLOCK_SIZE. */
130       result.getquota_rslt_u.gqr_rquota.rq_bsize = BLOCK_SIZE;
131
132       if (hasquota(mnt, type, &qfpathname)) {
133          if ((err = quotactl(QCMD(Q_GETQUOTA, type), mnt->mnt_fsname,
134                              id, (caddr_t)&dq_dqb)) == -1
135              && !(flags & ACTIVE)) {
136             if ((fd = open(qfpathname, O_RDONLY)) < 0)
137             {
138                free(qfpathname);
139                continue;
140             }
141             free(qfpathname);
142             lseek(fd, (long) dqoff(id), L_SET);
143             switch (read(fd, &dq_dqb, sizeof(struct dqblk))) {
144                case 0:/* EOF */
145                   /*
146                    * Convert implicit 0 quota (EOF) into an
147                    * explicit one (zero'ed dqblk)
148                    */
149                   memset((caddr_t)&dq_dqb, 0, sizeof(struct dqblk));
150                   break;
151                case sizeof(struct dqblk):   /* OK */
152                   break;
153                default:   /* ERROR */
154                   close(fd);
155                   continue;
156             }
157             close(fd);
158          }
159          endmntent(fp);
160
161          if (err && (flags & ACTIVE)) {
162             result.status = Q_NOQUOTA;   
163             return(&result);
164          }
165
166          result.status = Q_OK;   
167          result.getquota_rslt_u.gqr_rquota.rq_active = (err == 0) ? TRUE : FALSE;
168          /*
169           * Make a copy of the info into the last part of the remote quota
170           * struct might not be exactly the same on all architectures...
171           */
172
173          rquota = &result.getquota_rslt_u.gqr_rquota;
174          rquota->rq_bhardlimit = dq_dqb.dqb_bhardlimit;
175          rquota->rq_bsoftlimit = dq_dqb.dqb_bsoftlimit;;
176          rquota->rq_curblocks = dq_dqb.dqb_curblocks;
177          rquota->rq_fhardlimit = dq_dqb.dqb_ihardlimit;
178          rquota->rq_fsoftlimit = dq_dqb.dqb_isoftlimit;
179          rquota->rq_curfiles = dq_dqb.dqb_curinodes;
180          rquota->rq_btimeleft = dq_dqb.dqb_btime;
181          rquota->rq_ftimeleft = dq_dqb.dqb_itime;
182
183          return(&result);
184       }
185    }
186    endmntent(fp);
187
188    result.status = Q_NOQUOTA;   
189    return(&result);
190 }
191
192 getquota_rslt *rquotaproc_getquota_1(getquota_args *argp, struct svc_req *rqstp)
193 {
194    return(getquotainfo(0, (caddr_t *)argp, rqstp));
195 }
196
197 getquota_rslt *rquotaproc_getactivequota_1(getquota_args *argp, struct svc_req *rqstp)
198 {
199    return(getquotainfo(ACTIVE, (caddr_t *)argp, rqstp));
200 }
201
202 getquota_rslt *rquotaproc_getquota_2(ext_getquota_args *argp, struct svc_req *rqstp)
203 {
204    return(getquotainfo(TYPE_EXTENDED, (caddr_t *)argp, rqstp));
205 }
206
207 getquota_rslt *rquotaproc_getactivequota_2(ext_getquota_args *argp, struct svc_req *rqstp)
208 {
209    return(getquotainfo(TYPE_EXTENDED | ACTIVE, (caddr_t *)argp, rqstp));
210 }