]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
Remove NEED_QUOTACTL_WRAPPER cruft. I think that was needed for old Linux
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.23 2003-12-15 04:49:56 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 #ifdef ultrix
99 /*
100 * Return the block-special device name associated with the filesystem
101 * on which "file" resides.  Returns NULL on failure.
102 */
103
104 static char *
105 special( file, nfs )
106 char *file;
107 int  *nfs;
108 {
109     static struct fs_data       fsd;
110
111     if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
112         LOG(log_info, logtype_afpd, "special: getmnt %s: %s", file, strerror(errno) );
113         return( NULL );
114     }
115
116     /* XXX: does this really detect an nfs mounted fs? */
117     if (strchr(fsd.fd_req.devname, ':'))
118         *nfs = 1;
119     return( fsd.fd_req.devname );
120 }
121
122 #else /* ultrix */
123 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
124
125 static char *
126 special( file, nfs )
127 char    *file;
128 int         *nfs;
129 {
130     static struct statfs        sfs;
131
132     if ( statfs( file, &sfs ) < 0 ) {
133         return( NULL );
134     }
135
136 #ifdef TRU64
137     /* Digital UNIX: The struct sfs contains a field sfs.f_type,
138      * the MOUNT_* constants are defined in <sys/mount.h> */
139     if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
140 #else /* TRU64 */
141     /* XXX: make sure this really detects an nfs mounted fs */
142     if (strchr(sfs.f_mntfromname, ':'))
143 #endif /* TRU64 */
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_afpd, "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_afpd, "getquota: malloc: %s", strerror(errno) );
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_afpd, "open %s: %s", buf, strerror(errno) );
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_afpd, "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_afpd, "getquota: malloc: %s", strerror(errno) );
342             return AFPERR_MISC;
343         }
344         strcpy( vol->v_gvs, p );
345     }
346 #endif
347
348 #ifdef TRU64
349     /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
350        either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
351     if (vol->v_nfs) {
352         char *hostpath;
353         char pathstring[MNAMELEN];
354         /* MNAMELEN ist defined in <sys/mount.h> */
355         int result;
356         
357         if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
358             /* convert 'path@hostname' to 'hostname:path',
359              * call getnfsquota(),
360              * convert 'hostname:path' back to 'path@hostname' */
361             *hostpath = '\0';
362             sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
363             strcpy(vol->v_gvs,pathstring);
364             
365             result = getnfsquota(vol, uuid, bsize, dq);
366             
367             hostpath = strchr(vol->v_gvs,':');
368             *hostpath = '\0';
369             sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
370             strcpy(vol->v_gvs,pathstring);
371             
372             return result;
373         }
374         else
375             /* vol->v_gvs is of the form 'hostname:path' */
376             return getnfsquota(vol, uuid, bsize, dq);
377     } else
378         /* local filesystem */
379         return getfsquota(vol, uuid, dq);
380            
381 #else /* TRU64 */
382     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
383            getfsquota(vol, uuid, dq);
384 #endif /* TRU64 */
385 }
386
387 static int overquota( dqblk )
388 struct dqblk    *dqblk;
389 {
390     struct timeval      tv;
391
392     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
393         return( 0 );
394     }
395 #ifdef ultrix
396     if ( dqblk->dqb_bwarn ) {
397         return( 0 );
398     }
399 #else /* ultrix */
400     if ( gettimeofday( &tv, 0 ) < 0 ) {
401         LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
402         return( AFPERR_PARAM );
403     }
404     if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
405         return( 0 );
406     }
407 #endif /* ultrix */
408     return( 1 );
409 }
410
411 /*
412  * This next bit is basically for linux -- everything is fine
413  * if you use 1k blocks... but if you try (for example) to mount
414  * a volume via nfs from a netapp (which might use 4k blocks) everything
415  * gets reported improperly.  I have no idea about dbtob on other
416  * platforms.
417  */
418
419 #ifdef HAVE_BROKEN_DBTOB
420 #undef dbtob
421 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
422 #define HAVE_2ARG_DBTOB
423 #endif
424
425 #ifndef dbtob
426 #define dbtob(a)       ((a) << 10)
427 #endif
428
429 /* i do the cast to VolSpace here to make sure that 64-bit shifts
430    work */
431 #ifdef HAVE_2ARG_DBTOB
432 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
433 #else 
434 #define tobytes(a, b)  dbtob((VolSpace) (a))
435 #endif
436 int uquota_getvolspace( vol, bfree, btotal, bsize)
437 const struct vol        *vol;
438 VolSpace        *bfree, *btotal;
439 const u_int32_t bsize;
440 {
441     struct dqblk        dqblk;
442
443     if (getquota( vol, &dqblk, bsize) != 0 ) {
444         return( AFPERR_PARAM );
445     }
446
447     /* no limit set for this user. it might be set in the future. */
448     if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
449         *btotal = *bfree = ~((VolSpace) 0);
450     } else if ( overquota( &dqblk )) {
451         if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
452             *btotal = tobytes( dqblk.dqb_curblocks, bsize );
453             *bfree = 0;
454         }
455         else {
456             *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
457             *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
458                      tobytes( dqblk.dqb_curblocks, bsize );
459         }
460     } else {
461         *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
462         *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize  ) -
463                  tobytes( dqblk.dqb_curblocks, bsize );
464     }
465
466     return( AFP_OK );
467 }
468 #endif