'The mother of all checkins' :-). Jeremy Allison (jallison@whistle.com)
[Samba.git] / source / smbd / quotas.c
blobeba76d4c74b2384d2f3094a5f611b6e13649b521
1 #ifdef QUOTAS
2 /*
3 Unix SMB/Netbios implementation.
4 Version 1.9.
5 support for quotas
6 Copyright (C) Andrew Tridgell 1992-1997
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /*
25 * This is one of the most system dependent parts of Samba, and its
26 * done a litle differently. Each system has its own way of doing
27 * things :-(
30 #include "includes.h"
32 extern int DEBUGLEVEL;
34 #ifdef LINUX
36 #ifdef __KERNEL__
37 # undef __KERNEL__
38 # include <sys/quota.h>
39 # define __KERNEL__
40 #else
41 # include <sys/quota.h>
42 #endif
44 #include <mntent.h>
46 /****************************************************************************
47 try to get the disk space from disk quotas (LINUX version)
48 ****************************************************************************/
50 If you didn't make the symlink to the quota package, too bad :(
52 #include "quota/quotactl.c"
53 #include "quota/hasquota.c"
54 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
56 uid_t euser_id;
57 struct dqblk D;
58 struct stat S;
59 dev_t devno ;
60 struct mntent *mnt;
61 FILE *fp;
62 int found ;
63 int qcmd, fd ;
64 char *qfpathname;
66 /* find the block device file */
68 if ( stat(path, &S) == -1 )
69 return(False) ;
71 devno = S.st_dev ;
73 fp = setmntent(MOUNTED,"r");
74 found = False ;
76 while ((mnt = getmntent(fp)) != (struct mntent *) 0) {
77 if ( stat(mnt->mnt_dir,&S) == -1 )
78 continue ;
79 if (S.st_dev == devno) {
80 found = True ;
81 break ;
84 endmntent(fp) ;
86 if ( ! found )
87 return(False) ;
89 qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
91 if (hasmntopt(mnt, MNTOPT_NOAUTO) || hasmntopt(mnt, MNTOPT_NOQUOTA))
92 return(False) ;
94 if (!hasquota(mnt, USRQUOTA, &qfpathname))
95 return(False) ;
97 euser_id = geteuid();
98 seteuid(0);
100 if (quotactl(qcmd, mnt->mnt_fsname, euser_id, (caddr_t)&D) != 0) {
101 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
102 seteuid(euser_id);
103 return(False);
105 lseek(fd, (long) dqoff(euser_id), L_SET);
106 switch (read(fd, &D, sizeof(struct dqblk))) {
107 case 0:/* EOF */
108 memset((caddr_t)&D, 0, sizeof(struct dqblk));
109 break;
110 case sizeof(struct dqblk): /* OK */
111 break;
112 default: /* ERROR */
113 close(fd);
114 seteuid(euser_id);
115 return(False);
118 seteuid(euser_id);
119 *bsize=1024;
121 if (D.dqb_bsoftlimit==0)
122 return(False);
123 if ((D.dqb_curblocks>D.dqb_bsoftlimit)||(D.dqb_curinodes>D.dqb_isoftlimit))
125 *dfree = 0;
126 *dsize = D.dqb_curblocks;
128 else {
129 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
130 *dsize = D.dqb_bsoftlimit;
132 return (True);
135 #elif defined(CRAY)
137 #include <sys/quota.h>
138 #include <mntent.h>
140 /****************************************************************************
141 try to get the disk space from disk quotas (CRAY VERSION)
142 ****************************************************************************/
143 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
145 struct mntent *mnt;
146 FILE *fd;
147 struct stat sbuf;
148 dev_t devno ;
149 static dev_t devno_cached = 0 ;
150 static char name[MNTMAXSTR] ;
151 struct q_request request ;
152 struct qf_header header ;
153 static int quota_default = 0 ;
154 int found ;
156 if ( stat(path,&sbuf) == -1 )
157 return(False) ;
159 devno = sbuf.st_dev ;
161 if ( devno != devno_cached ) {
163 devno_cached = devno ;
165 if ((fd = setmntent(KMTAB)) == NULL)
166 return(False) ;
168 found = False ;
170 while ((mnt = getmntent(fd)) != NULL) {
172 if ( stat(mnt->mnt_dir,&sbuf) == -1 )
173 continue ;
175 if (sbuf.st_dev == devno) {
177 found = True ;
178 break ;
184 strcpy(name,mnt->mnt_dir) ;
185 endmntent(fd) ;
187 if ( ! found )
188 return(False) ;
191 request.qf_magic = QF_MAGIC ;
192 request.qf_entry.id = geteuid() ;
194 if (quotactl(name, Q_GETQUOTA, &request) == -1)
195 return(False) ;
197 if ( ! request.user )
198 return(False) ;
200 if ( request.qf_entry.user_q.f_quota == QFV_DEFAULT ) {
202 if ( ! quota_default ) {
204 if ( quotactl(name, Q_GETHEADER, &header) == -1 )
205 return(False) ;
206 else
207 quota_default = header.user_h.def_fq ;
210 *dfree = quota_default ;
212 }else if ( request.qf_entry.user_q.f_quota == QFV_PREVENT ) {
214 *dfree = 0 ;
216 }else{
218 *dfree = request.qf_entry.user_q.f_quota ;
222 *dsize = request.qf_entry.user_q.f_use ;
224 if ( *dfree )
225 *dfree -= *dsize ;
227 if ( *dfree < 0 )
228 *dfree = 0 ;
230 *bsize = 4096 ; /* Cray blocksize */
232 return(True) ;
237 #elif defined(SUNOS5) || defined(SUNOS4)
239 #include <fcntl.h>
240 #if defined(SUNOS5)
241 #include <sys/fs/ufs_quota.h>
242 #include <sys/mnttab.h>
243 #else /* defined(SUNOS4) */
244 #include <ufs/quota.h>
245 #include <mntent.h>
246 #endif
248 /****************************************************************************
249 try to get the disk space from disk quotas (solaris 2 version)
250 ****************************************************************************/
251 /* Quota code by Peter Urbanec (amiga@cse.unsw.edu.au) */
252 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
254 uid_t user_id, euser_id;
255 int ret;
256 struct dqblk D;
257 #if defined(SUNOS5)
258 struct quotctl command;
259 int file;
260 struct mnttab mnt;
261 static char name[MNT_LINE_MAX] ;
262 #else
263 struct mntent *mnt;
264 static char name[MNTMAXSTR] ;
265 #endif
266 FILE *fd;
267 struct stat sbuf;
268 dev_t devno ;
269 static dev_t devno_cached = 0 ;
270 int found ;
272 if ( stat(path,&sbuf) == -1 )
273 return(False) ;
275 devno = sbuf.st_dev ;
276 DEBUG(5,("disk_quotas: looking for path \"%s\" devno=%o\n", path,devno));
277 if ( devno != devno_cached ) {
278 devno_cached = devno ;
279 #if defined(SUNOS5)
280 if ((fd = fopen(MNTTAB, "r")) == NULL)
281 return(False) ;
283 found = False ;
284 while (getmntent(fd, &mnt) == 0) {
285 if ( stat(mnt.mnt_mountp,&sbuf) == -1 )
286 continue ;
287 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
288 mnt.mnt_mountp,sbuf.st_dev));
289 if (sbuf.st_dev == devno) {
290 found = True ;
291 break ;
295 strcpy(name,mnt.mnt_mountp) ;
296 strcat(name,"/quotas") ;
297 fclose(fd) ;
298 #else
299 if ((fd = setmntent(MOUNTED, "r")) == NULL)
300 return(False) ;
302 found = False ;
303 while ((mnt = getmntent(fd)) != NULL) {
304 if ( stat(mnt->mnt_dir,&sbuf) == -1 )
305 continue ;
306 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n",
307 mnt->mnt_dir,sbuf.st_dev));
308 if (sbuf.st_dev == devno) {
309 found = True ;
310 break ;
314 strcpy(name,mnt->mnt_fsname) ;
315 endmntent(fd) ;
316 #endif
318 if ( ! found )
319 return(False) ;
322 euser_id = geteuid();
323 user_id = getuid();
325 setuid(0); /* Solaris seems to want to give info only to super-user */
326 seteuid(0);
328 #if defined(SUNOS5)
329 DEBUG(5,("disk_quotas: looking for quotas file \"%s\"\n", name));
330 if((file=open(name, O_RDONLY))<0) {
331 setuid(user_id); /* Restore the original UID status */
332 seteuid(euser_id);
333 return(False);
335 command.op = Q_GETQUOTA;
336 command.uid = euser_id;
337 command.addr = (caddr_t) &D;
338 ret = ioctl(file, Q_QUOTACTL, &command);
339 close(file);
340 #else
341 DEBUG(5,("disk_quotas: trying quotactl on device \"%s\"\n", name));
342 ret = quotactl(Q_GETQUOTA, name, euser_id, &D);
343 #endif
345 setuid(user_id); /* Restore the original UID status */
346 seteuid(euser_id);
348 if (ret < 0) {
349 DEBUG(2,("disk_quotas ioctl (Solaris) failed\n"));
350 return(False);
354 /* Use softlimit to determine disk space. A user exceeding the quota is told
355 * that there's no space left. Writes might actually work for a bit if the
356 * hardlimit is set higher than softlimit. Effectively the disk becomes
357 * made of rubber latex and begins to expand to accommodate the user :-)
360 if (D.dqb_bsoftlimit==0)
361 return(False);
362 *bsize = 512;
363 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
364 *dsize = D.dqb_bsoftlimit;
365 if(*dfree < 0)
367 *dfree = 0;
368 *dsize = D.dqb_curblocks;
371 DEBUG(5,("disk_quotas for path \"%s\" returning bsize %d, dfree %d, dsize %d\n",
372 path,*bsize,*dfree,*dsize));
374 return(True);
377 #else
379 #ifdef __FreeBSD__
380 #include <ufs/ufs/quota.h>
381 #else
382 #include <sys/quota.h>
383 #include <devnm.h>
384 #endif
386 /****************************************************************************
387 try to get the disk space from disk quotas - default version
388 ****************************************************************************/
389 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
391 uid_t user_id, euser_id;
392 int r;
393 char dev_disk[256];
394 struct dqblk D;
395 struct stat S;
396 #ifndef __FreeBSD__
397 /* find the block device file */
398 if ((stat(path, &S)<0) ||
399 (devnm(S_IFBLK, S.st_dev, dev_disk, 256, 0)<0)) return (False);
400 #endif
402 euser_id = geteuid();
404 #ifdef USE_SETRES
405 /* for HPUX, real uid must be same as euid to execute quotactl for euid */
406 user_id = getuid();
407 setresuid(euser_id,-1,-1);
408 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
409 if (setresuid(user_id,-1,-1))
410 DEBUG(5,("Unable to reset uid to %d\n", user_id));
411 #else
412 #if defined(__FreeBSD__)
413 r= quotactl(path,Q_GETQUOTA,euser_id,(char *) &D);
414 #else
415 r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
416 #endif
417 #endif
419 /* Use softlimit to determine disk space, except when it has been exceeded */
420 *bsize = 1024;
421 if (r)
423 if (errno == EDQUOT)
425 *dfree =0;
426 *dsize =D.dqb_curblocks;
427 return (True);
429 else return(False);
431 if (D.dqb_bsoftlimit==0)
432 return(False);
433 /* Use softlimit to determine disk space, except when it has been exceeded */
434 if ((D.dqb_curblocks>D.dqb_bsoftlimit)
435 #if !defined(__FreeBSD__)
436 ||(D.dqb_curfiles>D.dqb_fsoftlimit)
437 #endif
439 *dfree = 0;
440 *dsize = D.dqb_curblocks;
442 else {
443 *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
444 *dsize = D.dqb_bsoftlimit;
446 return (True);
449 #endif
451 #else
452 /* this keeps fussy compilers happy */
453 void quotas_dummy(void) {}
454 #endif /* QUOTAS */