]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
Linux 2.6 quota interface. [Bjoern]
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.24 2003-12-28 13:51:12 srittau 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
51
52 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
53 /*
54  * Return the mount point associated with the filesystem
55  * on which "file" resides.  Returns NULL on failure.
56  */
57 static char *
58 mountp( file, nfs)
59 char    *file;
60 int         *nfs;
61 {
62     struct stat                 sb;
63     FILE                        *mtab;
64     dev_t                       devno;
65     static struct mnttab        mnt;
66
67     if ( stat( file, &sb ) < 0 ) {
68         return( NULL );
69     }
70     devno = sb.st_dev;
71
72     if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
73         return( NULL );
74     }
75
76     while ( getmntent( mtab, &mnt ) == 0 ) {
77         /* local fs */
78         if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
79             fclose( mtab );
80             return mnt.mnt_mountp;
81         }
82
83         /* check for nfs. we probably should use
84          * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
85         if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
86                 strchr(mnt.mnt_special, ':')) {
87             *nfs = 1;
88             fclose( mtab );
89             return mnt.mnt_special;
90         }
91     }
92
93     fclose( mtab );
94     return( NULL );
95 }
96
97 #else /* __svr4__ */
98 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
99
100 static char *
101 special( file, nfs )
102 char    *file;
103 int         *nfs;
104 {
105     static struct statfs        sfs;
106
107     if ( statfs( file, &sfs ) < 0 ) {
108         return( NULL );
109     }
110
111 #ifdef TRU64
112     /* Digital UNIX: The struct sfs contains a field sfs.f_type,
113      * the MOUNT_* constants are defined in <sys/mount.h> */
114     if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
115 #else /* TRU64 */
116     /* XXX: make sure this really detects an nfs mounted fs */
117     if (strchr(sfs.f_mntfromname, ':'))
118 #endif /* TRU64 */
119         *nfs = 1;
120     return( sfs.f_mntfromname );
121 }
122
123 #else /* BSD4_4 */
124
125 static char *
126 special( file, nfs )
127 char *file;
128 int *nfs;
129 {
130     struct stat         sb;
131     FILE                *mtab;
132     dev_t               devno;
133     struct mntent       *mnt;
134
135     if ( stat( file, &sb ) < 0 ) {
136         return( NULL );
137     }
138     devno = sb.st_dev;
139
140     if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
141         return( NULL );
142     }
143
144     while (( mnt = getmntent( mtab )) != NULL ) {
145         /* check for local fs */
146         if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
147             endmntent( mtab );
148             return( mnt->mnt_fsname );
149         }
150
151         /* check for an nfs mount entry. the alternative is to use
152         * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
153         if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
154                 strchr(mnt->mnt_fsname, ':')) {
155             *nfs = 1;
156             endmntent( mtab );
157             return( mnt->mnt_fsname );
158         }
159     }
160
161     endmntent( mtab );
162     return( NULL );
163 }
164
165 #endif /* BSD4_4 */
166 #endif /* __svr4__ */
167
168
169 static int getfsquota(vol, uid, dq)
170 struct vol          *vol;
171 const int           uid;
172 struct dqblk        *dq;
173 {
174 #ifdef __svr4__
175     struct quotctl      qc;
176
177     qc.op = Q_GETQUOTA;
178     qc.uid = uid;
179     qc.addr = (caddr_t)dq;
180     if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
181         return( AFPERR_PARAM );
182     }
183
184 #else /* __svr4__ */
185
186 #ifndef USRQUOTA
187 #define USRQUOTA   0
188 #endif
189
190 #ifndef QCMD
191 #define QCMD(a,b)  (a)
192 #endif
193
194 #ifndef TRU64
195     /* for group quotas. we only use these if the user belongs
196     * to one group. */
197     struct dqblk        dqg;
198
199     memset(&dqg, 0, sizeof(dqg));
200 #endif /* TRU64 */
201
202 #ifdef BSD4_4
203     if ( seteuid( getuid() ) == 0 ) {
204         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
205                        uid, (char *)dq ) != 0 ) {
206             /* try group quotas */
207             if (ngroups >= 1) {
208                 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
209                               groups[0], (char *) &dqg) != 0 ) {
210                     seteuid( uid );
211                     return( AFPERR_PARAM );
212                 }
213             }
214         }
215         seteuid( uid );
216     }
217
218 #elif defined(TRU64)
219     if ( seteuid( getuid() ) == 0 ) {
220         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
221                        uid, (char *)dq ) != 0 ) {
222             seteuid( uid );
223             return ( AFPERR_PARAM );
224         }
225         seteuid( uid );
226     }
227
228 #else /* BSD4_4 */
229     if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
230                   (caddr_t) dq ) != 0 ) {
231         return( AFPERR_PARAM );
232     }
233
234     if (ngroups >= 1)
235         quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
236                  groups[0], (char *) &dqg);
237 #endif  /* BSD4_4 */
238
239 #ifndef TRU64
240     /* set stuff up for group quotas if necessary */
241
242     /* max(user blocks, group blocks) */
243 #ifdef HAVE_STRUCT_IF_DQBLK
244     if (dqg.dqb_curspace && (dq->dqb_curspace < dqg.dqb_curspace))
245         dq->dqb_curspace = dqg.dqb_curspace;
246 #else
247     if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
248         dq->dqb_curblocks = dqg.dqb_curblocks;
249 #endif
250
251     /* min(user limit, group limit) */
252     if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
253                                (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
254         dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
255
256     /* ditto */
257     if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
258                                (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
259         dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
260
261     /* ditto */
262     if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
263                                (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
264         dq->dqb_btimelimit = dqg.dqb_btimelimit;
265
266 #endif /* TRU64 */
267
268 #endif /* __svr4__ */
269
270     return AFP_OK;
271 }
272
273
274 static int getquota( vol, dq, bsize)
275 struct vol              *vol;
276 struct dqblk    *dq;
277 const u_int32_t     bsize;
278 {
279     char *p;
280
281 #ifdef __svr4__
282     char                buf[ MAXPATHLEN + 1];
283
284     if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
285         if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
286             LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
287             return( AFPERR_PARAM );
288         }
289
290         if (vol->v_nfs) {
291             if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
292                 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
293                 return AFPERR_MISC;
294             }
295             strcpy( vol->v_gvs, p );
296
297         } else {
298             sprintf( buf, "%s/quotas", p );
299             if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
300                 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
301                 return( AFPERR_PARAM );
302             }
303         }
304
305     }
306 #else
307     if ( vol->v_gvs == NULL ) {
308         if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
309             LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
310             return( AFPERR_PARAM );
311         }
312
313         if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
314             LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
315             return AFPERR_MISC;
316         }
317         strcpy( vol->v_gvs, p );
318     }
319 #endif
320
321 #ifdef TRU64
322     /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
323        either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
324     if (vol->v_nfs) {
325         char *hostpath;
326         char pathstring[MNAMELEN];
327         /* MNAMELEN ist defined in <sys/mount.h> */
328         int result;
329         
330         if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
331             /* convert 'path@hostname' to 'hostname:path',
332              * call getnfsquota(),
333              * convert 'hostname:path' back to 'path@hostname' */
334             *hostpath = '\0';
335             sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
336             strcpy(vol->v_gvs,pathstring);
337             
338             result = getnfsquota(vol, uuid, bsize, dq);
339             
340             hostpath = strchr(vol->v_gvs,':');
341             *hostpath = '\0';
342             sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
343             strcpy(vol->v_gvs,pathstring);
344             
345             return result;
346         }
347         else
348             /* vol->v_gvs is of the form 'hostname:path' */
349             return getnfsquota(vol, uuid, bsize, dq);
350     } else
351         /* local filesystem */
352         return getfsquota(vol, uuid, dq);
353            
354 #else /* TRU64 */
355     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
356            getfsquota(vol, uuid, dq);
357 #endif /* TRU64 */
358 }
359
360 static int overquota( dqblk )
361 struct dqblk    *dqblk;
362 {
363     struct timeval      tv;
364
365 #ifdef HAVE_STRUCT_IF_DQBLK
366     if ( dqblk->dqb_curspace < dqblk->dqb_bsoftlimit ) {
367 #else
368     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
369 #endif
370         return 0 ;
371     }
372     if ( gettimeofday( &tv, 0 ) < 0 ) {
373         LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
374         return( AFPERR_PARAM );
375     }
376     if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
377         return( 0 );
378     }
379     return( 1 );
380 }
381
382 /*
383  * This next bit is basically for linux -- everything is fine
384  * if you use 1k blocks... but if you try (for example) to mount
385  * a volume via nfs from a netapp (which might use 4k blocks) everything
386  * gets reported improperly.  I have no idea about dbtob on other
387  * platforms.
388  */
389
390 #ifdef HAVE_BROKEN_DBTOB
391 #undef dbtob
392 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
393 #define HAVE_2ARG_DBTOB
394 #endif
395
396 #ifndef dbtob
397 #define dbtob(a)       ((a) << 10)
398 #endif
399
400 /* i do the cast to VolSpace here to make sure that 64-bit shifts
401    work */
402 #ifdef HAVE_2ARG_DBTOB
403 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
404 #else 
405 #define tobytes(a, b)  dbtob((VolSpace) (a))
406 #endif
407 int uquota_getvolspace( vol, bfree, btotal, bsize)
408 const struct vol        *vol;
409 VolSpace        *bfree, *btotal;
410 const u_int32_t bsize;
411 {
412     struct dqblk        dqblk;
413
414     if (getquota( vol, &dqblk, bsize) != 0 ) {
415         return( AFPERR_PARAM );
416     }
417
418     /* no limit set for this user. it might be set in the future. */
419     if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
420         *btotal = *bfree = ~((VolSpace) 0);
421     } else if ( overquota( &dqblk )) {
422 #ifdef HAVE_STRUCT_IF_DQBLK
423         if ( tobytes( dqblk.dqb_curspace, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
424             *btotal = tobytes( dqblk.dqb_curspace, bsize );
425 #else
426         if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
427             *btotal = tobytes( dqblk.dqb_curblocks, bsize );
428 #endif
429             *bfree = 0;
430         }
431         else {
432             *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
433             *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
434 #ifdef HAVE_STRUCT_IF_DQBLK
435                      tobytes( dqblk.dqb_curspace, bsize );
436 #else
437                      tobytes( dqblk.dqb_curblocks, bsize );
438 #endif
439         }
440     } else {
441         *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
442         *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize  ) -
443                  tobytes( dqblk.dqb_curblocks, bsize );
444     }
445
446     return( AFP_OK );
447 }
448 #endif