]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
Fix a problem where overquota() will report false if the user has no
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.19 2002-02-03 05:01:09 jmarcus Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16
17 /* STDC check */
18 #if STDC_HEADERS
19 #include <string.h>
20 #else /* STDC_HEADERS */
21 #ifndef HAVE_STRCHR
22 #define strchr index
23 #define strrchr index
24 #endif /* HAVE_STRCHR */
25 char *strchr (), *strrchr ();
26 #ifndef HAVE_MEMCPY
27 #define memcpy(d,s,n) bcopy ((s), (d), (n))
28 #define memmove(d,s,n) bcopy ((s), (d), (n))
29 #endif /* ! HAVE_MEMCPY */
30 #endif /* STDC_HEADERS */
31
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/param.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif /* HAVE_FCNTL_H */
41 #include <atalk/logger.h>
42
43 #include <atalk/afp.h>
44
45 #include "auth.h"
46 #include "volume.h"
47 #include "unix.h"
48
49 #ifndef NO_QUOTA_SUPPORT
50 #ifdef NEED_QUOTACTL_WRAPPER
51 int quotactl(int cmd, const char *special, int id, caddr_t addr)
52 {
53     return syscall(__NR_quotactl, cmd, special, id, addr);
54 }
55 #endif /* NEED_QUOTACTL_WRAPPER */
56
57
58 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
59 /*
60  * Return the mount point associated with the filesystem
61  * on which "file" resides.  Returns NULL on failure.
62  */
63 static char *
64 mountp( file, nfs)
65 char    *file;
66 int         *nfs;
67 {
68     struct stat                 sb;
69     FILE                        *mtab;
70     dev_t                       devno;
71     static struct mnttab        mnt;
72
73     if ( stat( file, &sb ) < 0 ) {
74         return( NULL );
75     }
76     devno = sb.st_dev;
77
78     if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
79         return( NULL );
80     }
81
82     while ( getmntent( mtab, &mnt ) == 0 ) {
83         /* local fs */
84         if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
85             fclose( mtab );
86             return mnt.mnt_mountp;
87         }
88
89         /* check for nfs. we probably should use
90          * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
91         if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
92                 strchr(mnt.mnt_special, ':')) {
93             *nfs = 1;
94             fclose( mtab );
95             return mnt.mnt_special;
96         }
97     }
98
99     fclose( mtab );
100     return( NULL );
101 }
102
103 #else /* __svr4__ */
104 #ifdef ultrix
105 /*
106 * Return the block-special device name associated with the filesystem
107 * on which "file" resides.  Returns NULL on failure.
108 */
109
110 static char *
111 special( file, nfs )
112 char *file;
113 int  *nfs;
114 {
115     static struct fs_data       fsd;
116
117     if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
118         LOG(log_info, logtype_default, "special: getmnt %s: %m", file );
119         return( NULL );
120     }
121
122     /* XXX: does this really detect an nfs mounted fs? */
123     if (strchr(fsd.fd_req.devname, ':'))
124         *nfs = 1;
125     return( fsd.fd_req.devname );
126 }
127
128 #else /* ultrix */
129 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
130
131 static char *
132 special( file, nfs )
133 char    *file;
134 int         *nfs;
135 {
136     static struct statfs        sfs;
137
138     if ( statfs( file, &sfs ) < 0 ) {
139         return( NULL );
140     }
141
142     /* XXX: make sure this really detects an nfs mounted fs */
143     if (strchr(sfs.f_mntfromname, ':'))
144         *nfs = 1;
145     return( sfs.f_mntfromname );
146 }
147
148 #else /* BSD4_4 */
149
150 static char *
151 special( file, nfs )
152 char *file;
153 int *nfs;
154 {
155     struct stat         sb;
156     FILE                *mtab;
157     dev_t               devno;
158     struct mntent       *mnt;
159
160     if ( stat( file, &sb ) < 0 ) {
161         return( NULL );
162     }
163     devno = sb.st_dev;
164
165     if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
166         return( NULL );
167     }
168
169     while (( mnt = getmntent( mtab )) != NULL ) {
170         /* check for local fs */
171         if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
172             endmntent( mtab );
173             return( mnt->mnt_fsname );
174         }
175
176         /* check for an nfs mount entry. the alternative is to use
177         * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
178         if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
179                 strchr(mnt->mnt_fsname, ':')) {
180             *nfs = 1;
181             endmntent( mtab );
182             return( mnt->mnt_fsname );
183         }
184     }
185
186     endmntent( mtab );
187     return( NULL );
188 }
189
190 #endif /* BSD4_4 */
191 #endif /* ultrix */
192 #endif /* __svr4__ */
193
194
195 static int getfsquota(vol, uid, dq)
196 struct vol          *vol;
197 const int           uid;
198 struct dqblk        *dq;
199 {
200 #ifdef __svr4__
201     struct quotctl      qc;
202
203     qc.op = Q_GETQUOTA;
204     qc.uid = uid;
205     qc.addr = (caddr_t)dq;
206     if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
207         return( AFPERR_PARAM );
208     }
209
210 #else /* __svr4__ */
211 #ifdef ultrix
212     if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
213         return( AFPERR_PARAM );
214     }
215 #else /* ultrix */
216
217 #ifndef USRQUOTA
218 #define USRQUOTA   0
219 #endif
220
221 #ifndef QCMD
222 #define QCMD(a,b)  (a)
223 #endif
224
225 #ifndef TRU64
226     /* for group quotas. we only use these if the user belongs
227     * to one group. */
228     struct dqblk        dqg;
229
230     memset(&dqg, 0, sizeof(dqg));
231 #endif /* TRU64 */
232
233 #ifdef BSD4_4
234     if ( seteuid( getuid() ) == 0 ) {
235         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
236                        uid, (char *)dq ) != 0 ) {
237             /* try group quotas */
238             if (ngroups >= 1) {
239                 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
240                               groups[0], (char *) &dqg) != 0 ) {
241                     seteuid( uid );
242                     return( AFPERR_PARAM );
243                 }
244             }
245         }
246         seteuid( uid );
247     }
248
249 #elif defined(TRU64)
250     if ( seteuid( getuid() ) == 0 ) {
251         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
252                        uid, (char *)dq ) != 0 ) {
253             seteuid( uid );
254             return ( AFPERR_PARAM );
255         }
256         seteuid( uid );
257     }
258
259 #else /* BSD4_4 */
260     if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
261                   (caddr_t) dq ) != 0 ) {
262         return( AFPERR_PARAM );
263     }
264
265     if (ngroups >= 1)
266         quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
267                  groups[0], (char *) &dqg);
268 #endif  /* BSD4_4 */
269
270 #ifndef TRU64
271     /* set stuff up for group quotas if necessary */
272
273     /* max(user blocks, group blocks) */
274     if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
275         dq->dqb_curblocks = dqg.dqb_curblocks;
276
277     /* min(user limit, group limit) */
278     if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
279                                (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
280         dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
281
282     /* ditto */
283     if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
284                                (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
285         dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
286
287     /* ditto */
288     if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
289                                (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
290         dq->dqb_btimelimit = dqg.dqb_btimelimit;
291
292 #endif /* TRU64 */
293
294 #endif /* ultrix */
295 #endif /* __svr4__ */
296
297     return AFP_OK;
298 }
299
300
301 static int getquota( vol, dq, bsize)
302 struct vol              *vol;
303 struct dqblk    *dq;
304 const u_int32_t     bsize;
305 {
306     char *p;
307
308 #ifdef __svr4__
309     char                buf[ MAXPATHLEN + 1];
310
311     if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
312         if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
313             LOG(log_info, logtype_default, "getquota: mountp %s fails", vol->v_path );
314             return( AFPERR_PARAM );
315         }
316
317         if (vol->v_nfs) {
318             if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
319                 LOG(log_error, logtype_default, "getquota: malloc: %m" );
320                 return AFPERR_MISC;
321             }
322             strcpy( vol->v_gvs, p );
323
324         } else {
325             sprintf( buf, "%s/quotas", p );
326             if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
327                 LOG(log_info, logtype_default, "open %s: %m", buf );
328                 return( AFPERR_PARAM );
329             }
330         }
331
332     }
333 #else
334     if ( vol->v_gvs == NULL ) {
335         if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
336             LOG(log_info, logtype_default, "getquota: special %s fails", vol->v_path );
337             return( AFPERR_PARAM );
338         }
339
340         if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
341             LOG(log_error, logtype_default, "getquota: malloc: %s", strerror(errno) );
342             return AFPERR_MISC;
343         }
344         strcpy( vol->v_gvs, p );
345     }
346 #endif
347
348     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
349            getfsquota(vol, uuid, dq);
350 }
351
352 static int overquota( dqblk )
353 struct dqblk    *dqblk;
354 {
355     struct timeval      tv;
356
357     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
358         return( 0 );
359     }
360 #ifdef ultrix
361     if ( dqblk->dqb_bwarn ) {
362         return( 0 );
363     }
364 #else /* ultrix */
365     if ( gettimeofday( &tv, 0 ) < 0 ) {
366         LOG(log_error, logtype_default, "overquota: gettimeofday: %s", strerror(errno) );
367         return( AFPERR_PARAM );
368     }
369     if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
370         return( 0 );
371     }
372 #endif /* ultrix */
373     return( 1 );
374 }
375
376 /*
377  * This next bit is basically for linux -- everything is fine
378  * if you use 1k blocks... but if you try (for example) to mount
379  * a volume via nfs from a netapp (which might use 4k blocks) everything
380  * gets reported improperly.  I have no idea about dbtob on other
381  * platforms.
382  */
383
384 #ifdef HAVE_BROKEN_DBTOB
385 #undef dbtob
386 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
387 #define HAVE_2ARG_DBTOB
388 #endif
389
390 #ifndef dbtob
391 #define dbtob(a)       ((a) << 10)
392 #endif
393
394 /* i do the cast to VolSpace here to make sure that 64-bit shifts
395    work */
396 #ifdef HAVE_2ARG_DBTOB
397 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
398 #else 
399 #define tobytes(a, b)  dbtob((VolSpace) (a))
400 #endif
401 int uquota_getvolspace( vol, bfree, btotal, bsize)
402 const struct vol        *vol;
403 VolSpace        *bfree, *btotal;
404 const u_int32_t bsize;
405 {
406     struct dqblk        dqblk;
407
408     if (getquota( vol, &dqblk, bsize) != 0 ) {
409         return( AFPERR_PARAM );
410     }
411
412     /* no limit set for this user. it might be set in the future. */
413     if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
414         *btotal = *bfree = ~((VolSpace) 0);
415     } else if ( overquota( &dqblk )) {
416         if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
417             *btotal = tobytes( dqblk.dqb_curblocks, bsize );
418             *bfree = 0;
419         }
420         else {
421             *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
422             *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
423                      tobytes( dqblk.dqb_curblocks, bsize );
424         }
425     } else {
426         *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
427         *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize  ) -
428                  tobytes( dqblk.dqb_curblocks, bsize );
429     }
430
431     return( AFP_OK );
432 }
433 #endif