]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/rquotad/rquota_server.c
2832974c735c174097074319682342a5242f5dfb
[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
80    /*
81     * First check authentication.
82     */
83    if (flags & TYPE_EXTENDED) {
84       arguments.ext_args = (ext_getquota_args *)argp;
85       id = arguments.ext_args->gqa_id;
86       type = arguments.ext_args->gqa_type;
87       pathname = arguments.ext_args->gqa_pathp;
88
89       if (type == USRQUOTA && unix_cred->aup_uid && unix_cred->aup_uid != id) {
90          result.status = Q_EPERM;
91          return(&result);
92       }
93
94       if (type == GRPQUOTA && unix_cred->aup_uid && unix_cred->aup_gid != id &&
95           !in_group((gid_t *)unix_cred->aup_gids, unix_cred->aup_len, id)) {
96          result.status = Q_EPERM;
97          return(&result);
98       }
99    } else {
100       arguments.args = (getquota_args *)argp;
101       id = arguments.args->gqa_uid;
102       type = USRQUOTA;
103       pathname = arguments.args->gqa_pathp;
104
105       if (unix_cred->aup_uid && unix_cred->aup_uid != id) {
106          result.status = Q_EPERM;
107          return(&result);
108       }
109    }
110
111    fp = setmntent(MNTTAB, "r");
112    while ((mnt = getmntent(fp)) != (struct mntent *)0) {
113       if (stat(mnt->mnt_dir, &stm) == -1)
114           continue;
115
116       if (stat(pathname, &stn) == -1)
117           break;
118       else if (stm.st_dev != stn.st_dev)
119           continue;
120
121       if (mnt->mnt_fsname [0] != '/'
122           || strcasecmp (mnt->mnt_type, MNTTYPE_NFS) == 0
123           || strcasecmp (mnt->mnt_type, MNTTYPE_AUTOFS) == 0
124           || strcasecmp (mnt->mnt_type, MNTTYPE_SWAP) == 0
125           || strcasecmp (mnt->mnt_type, MNTTYPE_IGNORE) == 0)
126          break;
127
128       /* All blocks reported are in BLOCK_SIZE. */
129       result.getquota_rslt_u.gqr_rquota.rq_bsize = BLOCK_SIZE;
130
131       if (hasquota(mnt, type, &qfpathname)) {
132          if ((err = quotactl(QCMD(Q_GETQUOTA, type), mnt->mnt_fsname,
133                              id, (caddr_t)&dq_dqb)) == -1
134              && !(flags & ACTIVE)) {
135             if ((fd = open(qfpathname, O_RDONLY)) < 0)
136             {
137                free(qfpathname);
138                continue;
139             }
140             free(qfpathname);
141             lseek(fd, (long) dqoff(id), L_SET);
142             switch (read(fd, &dq_dqb, sizeof(struct dqblk))) {
143                case 0:/* EOF */
144                   /*
145                    * Convert implicit 0 quota (EOF) into an
146                    * explicit one (zero'ed dqblk)
147                    */
148                   memset((caddr_t)&dq_dqb, 0, sizeof(struct dqblk));
149                   break;
150                case sizeof(struct dqblk):   /* OK */
151                   break;
152                default:   /* ERROR */
153                   close(fd);
154                   continue;
155             }
156             close(fd);
157          }
158          endmntent(fp);
159
160          if (err && (flags & ACTIVE)) {
161             result.status = Q_NOQUOTA;   
162             return(&result);
163          }
164
165          result.status = Q_OK;   
166          result.getquota_rslt_u.gqr_rquota.rq_active = (err == 0) ? TRUE : FALSE;
167          /*
168           * Make a copy of the info into the last part of the remote quota
169           * struct which is exactly the same.
170           */
171          memcpy((caddr_t *)&result.getquota_rslt_u.gqr_rquota.rq_bhardlimit,
172                 (caddr_t *)&dq_dqb, sizeof(struct dqblk));
173
174          return(&result);
175       }
176    }
177    endmntent(fp);
178
179    result.status = Q_NOQUOTA;   
180    return(&result);
181 }
182
183 getquota_rslt *rquotaproc_getquota_1(getquota_args *argp, struct svc_req *rqstp)
184 {
185    return(getquotainfo(0, (caddr_t *)argp, rqstp));
186 }
187
188 getquota_rslt *rquotaproc_getactivequota_1(getquota_args *argp, struct svc_req *rqstp)
189 {
190    return(getquotainfo(ACTIVE, (caddr_t *)argp, rqstp));
191 }
192
193 getquota_rslt *rquotaproc_getquota_2(ext_getquota_args *argp, struct svc_req *rqstp)
194 {
195    return(getquotainfo(TYPE_EXTENDED, (caddr_t *)argp, rqstp));
196 }
197
198 getquota_rslt *rquotaproc_getactivequota_2(ext_getquota_args *argp, struct svc_req *rqstp)
199 {
200    return(getquotainfo(TYPE_EXTENDED | ACTIVE, (caddr_t *)argp, rqstp));
201 }