File: rquota_server.c

package info (click to toggle)
nfs-utils 1%3A1.0.10-6
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 4,228 kB
  • ctags: 2,554
  • sloc: ansic: 24,689; sh: 9,398; makefile: 667
file content (283 lines) | stat: -rw-r--r-- 8,745 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * QUOTA    An implementation of the diskquota system for the LINUX
 *          operating system. QUOTA is implemented using the BSD systemcall
 *          interface as the means of communication with the user level.
 *          Should work for all filesystems because of integration into the
 *          VFS layer of the operating system.
 *          This is based on the Melbourne quota system wich uses both user and
 *          group quota files.
 *
 *          This part does the lookup of the info.
 *
 * Version: $Id: rquota_server.c,v 2.9 1996/11/17 16:59:46 mvw Exp mvw $
 *
 * Author:  Marco van Wieringen <mvw@planets.elm.net>
 *
 *          This program is free software; you can redistribute it and/or
 *          modify it under the terms of the GNU General Public License
 *          as published by the Free Software Foundation; either version
 *          2 of the License, or (at your option) any later version.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <rpc/rpc.h>
#include "rquota.h"
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/param.h>
/* Unfortunately we cannot trust sys/quota.h to have
 * what we need, either the old interface could be missing
 * (SLES9) or the new (SLES8 and others).
 * So we will just put it explicitly below
 */
#if 0
#include <sys/quota.h>
#endif
#include <sys/mount.h>
#include <dirent.h>
#include <paths.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mntent.h"
#include "xmalloc.h"

#define TYPE_EXTENDED	0x01
#define ACTIVE		0x02

#ifndef MNTTYPE_AUTOFS
#define MNTTYPE_AUTOFS	"autofs"
#endif

#ifndef BLOCK_SIZE
#define BLOCK_SIZE 1024
#endif

#define MAXQUOTAS 2
#define USRQUOTA  0		/* element used for user quotas */
#define GRPQUOTA  1		/* element used for group quotas */

struct dqblk {
	u_int32_t dqb_bhardlimit;   /* absolute limit on disk blks alloc */
	u_int32_t dqb_bsoftlimit;   /* preferred limit on disk blks */
	u_int32_t dqb_curblocks;    /* current block count */
	u_int32_t dqb_ihardlimit;   /* maximum # allocated inodes */
	u_int32_t dqb_isoftlimit;   /* preferred inode limit */
	u_int32_t dqb_curinodes;    /* current # allocated inodes */
	time_t dqb_btime;           /* time limit for excessive disk use */
	time_t dqb_itime;           /* time limit for excessive files */
};

struct if_dqblk {
        u_int64_t dqb_bhardlimit;
        u_int64_t dqb_bsoftlimit;
        u_int64_t dqb_curspace;
        u_int64_t dqb_ihardlimit;
        u_int64_t dqb_isoftlimit;
        u_int64_t dqb_curinodes;
        u_int64_t dqb_btime;
        u_int64_t dqb_itime;
        u_int32_t dqb_valid;
};

#define SUBCMDMASK  0x00ff
#define SUBCMDSHIFT 8
#define QCMD(cmd, type)  (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))

#define Q_GETQUOTA 0x0300	/* get limits and usage */
#define Q_SETQUOTA 0x0400	/* set limits and usage */

#define Q_GETFMT   0x800004     /* get quota format used on given filesystem */
#define Q_GETQUOTA_NEW 0x800007 /* get user quota structure */
#define Q_SETQUOTA_NEW 0x800008 /* set user quota structure */
#define dqoff(UID)      ((loff_t)((UID) * sizeof (struct dqblk)))

extern int quotactl (int __cmd, const char *__special, int __id,
		     caddr_t __addr) __THROW;
/*
 * Global unix authentication credentials.
 */
extern struct authunix_parms *unix_cred;

int in_group (gid_t *gids, u_int len, gid_t gid)
{
   int cnt = 0;

   while (cnt < len) {
      if (gids[cnt] == gid)
         return 1;
      cnt++;
   }
   return 0;
}

getquota_rslt *getquotainfo(int flags, caddr_t *argp, struct svc_req *rqstp)
{
   static getquota_rslt result;
   union {
      getquota_args *args;
      ext_getquota_args *ext_args;
   } arguments;
   FILE *fp;
   struct mntent *mnt;
   char *pathname, *qfpathname;
   int fd, err, id, type;
   struct stat stm, stn;
   struct rquota *rquota;
   struct if_dqblk dqb;

   /*
    * First check authentication.
    */
   if (flags & TYPE_EXTENDED) {
      arguments.ext_args = (ext_getquota_args *)argp;
      id = arguments.ext_args->gqa_id;
      type = arguments.ext_args->gqa_type;
      pathname = arguments.ext_args->gqa_pathp;

      if (type == USRQUOTA && unix_cred->aup_uid && unix_cred->aup_uid != id) {
         result.status = Q_EPERM;
         return(&result);
      }

      if (type == GRPQUOTA && unix_cred->aup_uid && unix_cred->aup_gid != id &&
          !in_group((gid_t *)unix_cred->aup_gids, unix_cred->aup_len, id)) {
         result.status = Q_EPERM;
         return(&result);
      }
   } else {
      arguments.args = (getquota_args *)argp;
      id = arguments.args->gqa_uid;
      type = USRQUOTA;
      pathname = arguments.args->gqa_pathp;

      if (unix_cred->aup_uid && unix_cred->aup_uid != id) {
         result.status = Q_EPERM;
         return(&result);
      }
   }

   fp = setmntent(MNTTAB, "r");
   while ((mnt = getmntent(fp)) != (struct mntent *)0) {
      if (stat(mnt->mnt_dir, &stm) == -1)
	  continue;

      if (stat(pathname, &stn) == -1)
	  break;
      else if (stm.st_dev != stn.st_dev)
	  continue;

      if (mnt->mnt_fsname [0] != '/'
	  || strcasecmp (mnt->mnt_type, MNTTYPE_NFS) == 0
	  || strcasecmp (mnt->mnt_type, MNTTYPE_AUTOFS) == 0
	  || strcasecmp (mnt->mnt_type, MNTTYPE_SWAP) == 0
	  || strcasecmp (mnt->mnt_type, MNTTYPE_IGNORE) == 0)
         break;

      /* All blocks reported are in BLOCK_SIZE. */
      result.getquota_rslt_u.gqr_rquota.rq_bsize = BLOCK_SIZE;

      if (hasquota(mnt, type, &qfpathname)) {
	      int fmt;
	      if (quotactl(QCMD(Q_GETFMT, type), mnt->mnt_fsname, 0, (caddr_t)&fmt)==0) {
		      /* new style interface
		       * Don't bother trying to read from the file
		       */
		      err = quotactl(QCMD(Q_GETQUOTA_NEW, type),
				     mnt->mnt_fsname, id, (caddr_t)&dqb);
		      if (err) memset(&dqb, 0, sizeof(dqb));
	      } else {
		      /* old style */
		      struct dqblk dq_dqb;

		      if ((err = quotactl(QCMD(Q_GETQUOTA, type), mnt->mnt_fsname,
					  id, (caddr_t)&dq_dqb)) == -1
			  && !(flags & ACTIVE)) {
			      if ((fd = open(qfpathname, O_RDONLY)) < 0)
			      {
				      free(qfpathname);
				      continue;
			      }
			      lseek(fd, (long) dqoff(id), L_SET);
			      switch (read(fd, &dq_dqb, sizeof(struct dqblk))) {
			      case 0:/* EOF */
				      /*
				       * Convert implicit 0 quota (EOF) into an
				       * explicit one (zero'ed dqblk)
				       */
				      memset((caddr_t)&dq_dqb, 0, sizeof(struct dqblk));
				      break;
			      case sizeof(struct dqblk):   /* OK */
				      break;
			      default:   /* ERROR */
				      close(fd);
				      free(qfpathname);
				      continue;
			      }
			      close(fd);
		      }
		      dqb.dqb_bhardlimit = dq_dqb.dqb_bhardlimit;
		      dqb.dqb_bsoftlimit = dq_dqb.dqb_bsoftlimit;
		      dqb.dqb_curspace = dq_dqb.dqb_curblocks * 1024;
		      dqb.dqb_ihardlimit = dq_dqb.dqb_ihardlimit;
		      dqb.dqb_isoftlimit = dq_dqb.dqb_isoftlimit;
		      dqb.dqb_curinodes = dq_dqb.dqb_curinodes;
		      dqb.dqb_btime = dq_dqb.dqb_btime;
		      dqb.dqb_itime = dq_dqb.dqb_itime;
	      }
	      free(qfpathname);
         endmntent(fp);

         if (err && (flags & ACTIVE)) {
            result.status = Q_NOQUOTA;   
            return(&result);
         }

         result.status = Q_OK;   
         result.getquota_rslt_u.gqr_rquota.rq_active = (err == 0) ? TRUE : FALSE;
         /*
          * Make a copy of the info into the last part of the remote quota
          * struct might not be exactly the same on all architectures...
          */

         rquota = &result.getquota_rslt_u.gqr_rquota;
         rquota->rq_bhardlimit = dqb.dqb_bhardlimit;
         rquota->rq_bsoftlimit = dqb.dqb_bsoftlimit;;
         rquota->rq_curblocks = dqb.dqb_curspace/1024;
         rquota->rq_fhardlimit = dqb.dqb_ihardlimit;
         rquota->rq_fsoftlimit = dqb.dqb_isoftlimit;
         rquota->rq_curfiles = dqb.dqb_curinodes;
         rquota->rq_btimeleft = dqb.dqb_btime;
         rquota->rq_ftimeleft = dqb.dqb_itime;

         return(&result);
      }
   }
   endmntent(fp);

   result.status = Q_NOQUOTA;   
   return(&result);
}

getquota_rslt *rquotaproc_getquota_1_svc(getquota_args *argp, struct svc_req *rqstp)
{
   return(getquotainfo(0, (caddr_t *)argp, rqstp));
}

getquota_rslt *rquotaproc_getactivequota_1_svc(getquota_args *argp, struct svc_req *rqstp)
{
   return(getquotainfo(ACTIVE, (caddr_t *)argp, rqstp));
}

getquota_rslt *rquotaproc_getquota_2_svc(ext_getquota_args *argp, struct svc_req *rqstp)
{
   return(getquotainfo(TYPE_EXTENDED, (caddr_t *)argp, rqstp));
}

getquota_rslt *rquotaproc_getactivequota_2_svc(ext_getquota_args *argp, struct svc_req *rqstp)
{
   return(getquotainfo(TYPE_EXTENDED | ACTIVE, (caddr_t *)argp, rqstp));
}