4216aa1199fc85d498142117126593c5c877dcef
[kai/samba.git] / source3 / smbd / quotas.c
1 #ifdef QUOTAS
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    support for quotas
6    Copyright (C) Andrew Tridgell 1992-1995
7    
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.
12    
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.
17    
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.
21 */
22
23
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 :-(
28  */
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34 #ifdef LINUX
35
36 #ifdef __KERNEL__
37 # undef __KERNEL__
38 # include <sys/quota.h>
39 # define __KERNEL__
40 #else
41 # include <sys/quota.h>
42 #endif
43
44 #include <mntent.h>
45
46 /****************************************************************************
47 try to get the disk space from disk quotas (LINUX version)
48 ****************************************************************************/
49 /*
50 If you didn't make the symlink to the quota package, too bad :(
51 */
52 #include "quota/quotactl.c"
53 #include "quota/hasquota.c"
54 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
55 {
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;
65   
66   /* find the block device file */
67   
68   if ( stat(path, &S) == -1 )
69     return(False) ;
70
71   devno = S.st_dev ;
72   
73   fp = setmntent(MOUNTED,"r");
74   found = False ;
75   
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 ;
82     }
83   }
84   endmntent(fp) ;
85   
86   if ( ! found )
87     return(False) ;
88   
89   qcmd = QCMD(Q_GETQUOTA, USRQUOTA);
90   
91   if (hasmntopt(mnt, MNTOPT_NOAUTO) || hasmntopt(mnt, MNTOPT_NOQUOTA))
92     return(False) ;
93   
94   if (!hasquota(mnt, USRQUOTA, &qfpathname))
95     return(False) ;
96   
97   euser_id = geteuid();
98   seteuid(0);
99   
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);
104     }
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);
116     }
117   }
118   seteuid(euser_id);
119   *bsize=1024;
120
121   if (D.dqb_bsoftlimit==0)
122     return(False);
123   if ((D.dqb_curblocks>D.dqb_bsoftlimit)||(D.dqb_curinodes>D.dqb_isoftlimit))
124     {
125       *dfree = 0;
126       *dsize = D.dqb_curblocks;
127     }
128   else {
129     *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
130     *dsize = D.dqb_bsoftlimit;
131   }
132   return (True);
133 }
134
135 #elif defined(CRAY)
136
137 #include <sys/quota.h>
138 #include <mntent.h>
139
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)
144 {
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 ;
155   
156   if ( stat(path,&sbuf) == -1 )
157     return(False) ;
158   
159   devno = sbuf.st_dev ;
160   
161   if ( devno != devno_cached ) {
162     
163     devno_cached = devno ;
164     
165     if ((fd = setmntent(KMTAB)) == NULL)
166       return(False) ;
167     
168     found = False ;
169     
170     while ((mnt = getmntent(fd)) != NULL) {
171       
172       if ( stat(mnt->mnt_dir,&sbuf) == -1 )
173         continue ;
174       
175       if (sbuf.st_dev == devno) {
176         
177         found = True ;
178         break ;
179         
180       }
181       
182     }
183     
184     strcpy(name,mnt->mnt_dir) ;
185     endmntent(fd) ;
186     
187     if ( ! found )
188       return(False) ;
189   }
190   
191   request.qf_magic = QF_MAGIC ;
192   request.qf_entry.id = geteuid() ;
193   
194   if (quotactl(name, Q_GETQUOTA, &request) == -1)
195     return(False) ;
196   
197   if ( ! request.user )
198     return(False) ;
199   
200   if ( request.qf_entry.user_q.f_quota == QFV_DEFAULT ) {
201     
202     if ( ! quota_default ) {
203       
204       if ( quotactl(name, Q_GETHEADER, &header) == -1 )
205         return(False) ;
206       else
207         quota_default = header.user_h.def_fq ;
208     }
209     
210     *dfree = quota_default ;
211     
212   }else if ( request.qf_entry.user_q.f_quota == QFV_PREVENT ) {
213     
214     *dfree = 0 ;
215     
216   }else{
217     
218     *dfree = request.qf_entry.user_q.f_quota ;
219     
220   }
221   
222   *dsize = request.qf_entry.user_q.f_use ;
223   
224   if ( *dfree )
225     *dfree -= *dsize ;
226   
227   if ( *dfree < 0 )
228     *dfree = 0 ;
229   
230   *bsize = 4096 ;  /* Cray blocksize */
231   
232   return(True) ;
233   
234 }
235
236
237 #elif defined(SUNOS5) || defined(SUNOS4)
238
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
247
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)
253 {
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 ;
271   
272   if ( stat(path,&sbuf) == -1 )
273     return(False) ;
274   
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) ;
282     
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", mnt.mnt_mountp,sbuf.st_dev));
288       if (sbuf.st_dev == devno) {
289         found = True ;
290         break ;
291       }
292     }
293     
294     strcpy(name,mnt.mnt_mountp) ;
295     strcat(name,"/quotas") ;
296     fclose(fd) ;
297 #else
298     if ((fd = setmntent(MOUNTED, "r")) == NULL)
299       return(False) ;
300     
301     found = False ;
302     while ((mnt = getmntent(fd)) != NULL) {
303       if ( stat(mnt->mnt_dir,&sbuf) == -1 )
304         continue ;
305 DEBUG(5,("disk_quotas: testing \"%s\" devno=%o\n", mnt->mnt_dir,sbuf.st_dev));
306       if (sbuf.st_dev == devno) {
307         found = True ;
308         break ;
309       }
310     }
311     
312     strcpy(name,mnt->mnt_fsname) ;
313     endmntent(fd) ;
314 #endif
315     
316     if ( ! found )
317       return(False) ;
318   }
319
320   euser_id = geteuid();
321   user_id = getuid();
322
323   setuid(0);  /* Solaris seems to want to give info only to super-user */
324   seteuid(0);
325
326 #if defined(SUNOS5)
327 DEBUG(5,("disk_quotas: looking for quotas file \"%s\"\n", name));
328   if((file=open(name, O_RDONLY))<0) {
329     setuid(user_id);  /* Restore the original UID status */
330     seteuid(euser_id);
331     return(False);
332   }
333   command.op = Q_GETQUOTA;
334   command.uid = euser_id;
335   command.addr = (caddr_t) &D;
336   ret = ioctl(file, Q_QUOTACTL, &command);
337   close(file);
338 #else
339 DEBUG(5,("disk_quotas: trying quotactl on device \"%s\"\n", name));
340   ret = quotactl(Q_GETQUOTA, name, euser_id, &D);
341 #endif
342
343   setuid(user_id);  /* Restore the original UID status */
344   seteuid(euser_id);
345
346   if (ret < 0) {
347     DEBUG(2,("disk_quotas ioctl (Solaris) failed\n"));
348     return(False);
349   }
350
351
352   /* Use softlimit to determine disk space. A user exceeding the quota is told
353    * that there's no space left. Writes might actually work for a bit if the
354    * hardlimit is set higher than softlimit. Effectively the disk becomes
355    * made of rubber latex and begins to expand to accommodate the user :-)
356    */
357
358   if (D.dqb_bsoftlimit==0)
359     return(False);
360   *bsize = 512;
361   *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
362   *dsize = D.dqb_bsoftlimit;
363   if(*dfree < 0)
364     {
365      *dfree = 0;
366      *dsize = D.dqb_curblocks;
367     }
368       
369 DEBUG(5,("disk_quotas for path \"%s\" returning  bsize %d, dfree %d, dsize %d\n",
370          path,*bsize,*dfree,*dsize));
371
372       return(True);
373 }
374
375 #else
376
377 #include <sys/quota.h>
378 #include <devnm.h>
379
380 /****************************************************************************
381 try to get the disk space from disk quotas - default version
382 ****************************************************************************/
383 BOOL disk_quotas(char *path, int *bsize, int *dfree, int *dsize)
384 {
385   uid_t user_id, euser_id;
386   int r;
387   char dev_disk[256];
388   struct dqblk D;
389   struct stat S;
390   /* find the block device file */
391   if ((stat(path, &S)<0) ||
392       (devnm(S_IFBLK, S.st_dev, dev_disk, 256, 0)<0)) return (False);
393
394   euser_id = geteuid();
395
396 #ifdef USE_SETRES
397   /* for HPUX, real uid must be same as euid to execute quotactl for euid */
398   user_id = getuid();
399   setresuid(euser_id,-1,-1);
400 #endif
401   r=quotactl(Q_GETQUOTA, dev_disk, euser_id, &D);
402   #ifdef USE_SETRES
403   if (setresuid(user_id,-1,-1))
404     DEBUG(5,("Unable to reset uid to %d\n", user_id));
405   #endif
406   /* Use softlimit to determine disk space, except when it has been exceeded */
407   *bsize = 1024;
408   if (r)
409     {
410       if (errno == EDQUOT) 
411         {
412           *dfree =0;
413           *dsize =D.dqb_curblocks;
414           return (True);
415         }
416       else return(False);
417     }
418   if (D.dqb_bsoftlimit==0)
419     return(False);
420   /* Use softlimit to determine disk space, except when it has been exceeded */
421   if ((D.dqb_curblocks>D.dqb_bsoftlimit)||(D.dqb_curfiles>D.dqb_fsoftlimit)) 
422     {
423       *dfree = 0;
424       *dsize = D.dqb_curblocks;
425     }
426   else {
427     *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
428     *dsize = D.dqb_bsoftlimit;
429   }
430   return (True);
431 }
432
433 #endif
434
435 #else
436 /* this keeps fussy compilers happy */
437  void quotas_dummy(void) {}
438 #endif /* QUOTAS */
439