3e22e26ba3445d12a8a6f21028e7532aa9f63048
[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", 
288                mnt.mnt_mountp,sbuf.st_dev));
289       if (sbuf.st_dev == devno) {
290         found = True ;
291         break ;
292       }
293     }
294     
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) ;
301     
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 ;
311       }
312     }
313     
314     strcpy(name,mnt->mnt_fsname) ;
315     endmntent(fd) ;
316 #endif
317     
318     if ( ! found )
319       return(False) ;
320   }
321
322   euser_id = geteuid();
323   user_id = getuid();
324
325   setuid(0);  /* Solaris seems to want to give info only to super-user */
326   seteuid(0);
327
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);
334   }
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
344
345   setuid(user_id);  /* Restore the original UID status */
346   seteuid(euser_id);
347
348   if (ret < 0) {
349     DEBUG(2,("disk_quotas ioctl (Solaris) failed\n"));
350     return(False);
351   }
352
353
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 :-)
358    */
359
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)
366     {
367      *dfree = 0;
368      *dsize = D.dqb_curblocks;
369     }
370       
371 DEBUG(5,("disk_quotas for path \"%s\" returning  bsize %d, dfree %d, dsize %d\n",
372          path,*bsize,*dfree,*dsize));
373
374       return(True);
375 }
376
377 #else
378
379 #ifdef        __FreeBSD__
380 #include <ufs/ufs/quota.h>
381 #else
382 #include <sys/quota.h>
383 #include <devnm.h>
384 #endif
385
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)
390 {
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
401
402   euser_id = geteuid();
403
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
418
419   /* Use softlimit to determine disk space, except when it has been exceeded */
420   *bsize = 1024;
421   if (r)
422     {
423       if (errno == EDQUOT) 
424         {
425           *dfree =0;
426           *dsize =D.dqb_curblocks;
427           return (True);
428         }
429       else return(False);
430     }
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
438     ) {
439       *dfree = 0;
440       *dsize = D.dqb_curblocks;
441     }
442   else {
443     *dfree = D.dqb_bsoftlimit - D.dqb_curblocks;
444     *dsize = D.dqb_bsoftlimit;
445   }
446   return (True);
447 }
448
449 #endif
450
451 #else
452 /* this keeps fussy compilers happy */
453  void quotas_dummy(void) {}
454 #endif /* QUOTAS */
455