]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
more Tru64 fixes from Burkhard Schmidt <bs@cpfs.mpg.de>
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.6 2001-05-22 19:13:36 rufustfirefly 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
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 #include <sys/param.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <syslog.h>
21
22 #include <atalk/afp.h>
23
24 #include "auth.h"
25 #include "volume.h"
26 #include "unix.h"
27
28 #ifndef NO_QUOTA_SUPPORT
29 #ifdef NEED_QUOTACTL_WRAPPER
30 int quotactl(int cmd, const char *special, int id, caddr_t addr)
31 {
32   return syscall(__NR_quotactl, cmd, special, id, addr);
33 }
34 #endif
35
36
37 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
38 /*
39  * Return the mount point associated with the filesystem
40  * on which "file" resides.  Returns NULL on failure.
41  */
42 static char *
43 mountp( file, nfs)
44     char        *file;
45     int         *nfs;
46 {
47     struct stat                 sb;
48     FILE                        *mtab;
49     dev_t                       devno;
50     static struct mnttab        mnt;
51
52     if ( stat( file, &sb ) < 0 ) {
53         return( NULL );
54     }
55     devno = sb.st_dev;
56
57     if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
58         return( NULL );
59     }
60
61     while ( getmntent( mtab, &mnt ) == 0 ) {
62       /* local fs */
63       if ( (stat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
64         fclose( mtab );
65         return mnt.mnt_mountp;
66       }
67           
68       /* check for nfs. we probably should use 
69        * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
70       if ((stat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
71           strchr(mnt.mnt_special, ':')) {
72         *nfs = 1;
73         fclose( mtab );
74         return mnt.mnt_special;
75       }
76     }
77
78     fclose( mtab );
79     return( NULL );
80 }
81
82 #else /* __svr4__ */
83 #ifdef ultrix
84 /*
85  * Return the block-special device name associated with the filesystem
86  * on which "file" resides.  Returns NULL on failure.
87  */
88
89 static char *
90 special( file, nfs )
91     char *file;
92     int  *nfs;
93 {
94     static struct fs_data       fsd;
95
96     if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
97         syslog(LOG_INFO, "special: getmnt %s: %m", file );
98         return( NULL );
99     }
100
101     /* XXX: does this really detect an nfs mounted fs? */
102     if (strchr(fsd.fd_req.devname, ':'))
103       *nfs = 1;
104     return( fsd.fd_req.devname );
105 }
106
107 #else /* ultrix */
108 #if defined(HAVE_SYS_MOUNT_H) || defined(BSD4_4) || defined(_IBMR2)
109
110 static char *
111 special( file, nfs )
112     char        *file;
113     int         *nfs;
114 {
115     static struct statfs        sfs;
116
117     if ( statfs( file, &sfs ) < 0 ) {
118         return( NULL );
119     }
120
121     /* XXX: make sure this really detects an nfs mounted fs */
122     if (strchr(sfs.f_mntfromname, ':'))
123       *nfs = 1;
124     return( sfs.f_mntfromname );
125 }
126
127 #else /* BSD4_4 */
128
129 static char *
130 special( file, nfs )
131     char *file;
132     int *nfs;
133 {
134     struct stat         sb;
135     FILE                *mtab;
136     dev_t               devno;
137     struct mntent       *mnt;
138
139     if ( stat( file, &sb ) < 0 ) {
140         return( NULL );
141     }
142     devno = sb.st_dev;
143
144     if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
145         return( NULL );
146     }
147
148     while (( mnt = getmntent( mtab )) != NULL ) {
149       /* check for local fs */
150       if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
151         endmntent( mtab );
152         return( mnt->mnt_fsname );
153       }
154       
155       /* check for an nfs mount entry. the alternative is to use
156        * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
157       if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
158           strchr(mnt->mnt_fsname, ':')) {
159         *nfs = 1;
160         endmntent( mtab );
161         return( mnt->mnt_fsname );
162       }
163     }
164
165     endmntent( mtab );
166     return( NULL );
167 }
168
169 #endif /* BSD4_4 */
170 #endif /* ultrix */
171 #endif /* __svr4__ */
172
173
174 static int getfsquota(vol, uid, dq)
175     struct vol          *vol;
176     const int           uid;
177     struct dqblk        *dq;
178 {
179 #ifdef __svr4__
180     struct quotctl      qc;
181
182     qc.op = Q_GETQUOTA;
183     qc.uid = uid;
184     qc.addr = (caddr_t)dq;
185     if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
186         return( AFPERR_PARAM );
187     }
188
189 #else /* __svr4__ */
190 #ifdef ultrix
191     if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
192         return( AFPERR_PARAM );
193     }
194 #else ultrix
195
196 #ifndef USRQUOTA
197 #define USRQUOTA   0
198 #endif
199
200 #ifndef QCMD
201 #define QCMD(a,b)  (a)
202 #endif
203
204 #ifndef TRU64
205     /* for group quotas. we only use these if the user belongs
206      * to one group. */
207     struct dqblk        dqg;
208
209     memset(&dqg, 0, sizeof(dqg));
210 #endif /* TRU64 */
211
212 #ifdef BSD4_4
213     if ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA), 
214                    uid, (char *)dq ) != 0 ) {
215         return( AFPERR_PARAM );
216     }
217
218     if (ngroups == 1) 
219       quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
220                    groups[0], (char *) &dqg);
221       
222 #elif defined(TRU64)
223         if ( seteuid( getuid() ) == 0 ) {
224                 if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
225                                 uid, (char *)dq ) != 0 ) {
226                         seteuid( uid );
227                         return ( AFPERR_PARAM );
228                 }
229                 seteuid( uid );
230         }
231
232 #else /* BSD4_4 */
233     if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
234                   (caddr_t) dq ) != 0 ) {
235         return( AFPERR_PARAM );
236     }
237
238     if (ngroups == 1) 
239       quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs, 
240                groups[0], (char *) &dqg);
241 #endif  /* BSD4_4 */
242
243 #ifndef TRU64
244     /* set stuff up for group quotas if necessary */
245
246     /* max(user blocks, group blocks) */
247     if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
248       dq->dqb_curblocks = dqg.dqb_curblocks;
249     
250     /* min(user limit, group limit) */
251     if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
252          (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
253       dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
254
255     /* ditto */
256     if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
257          (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
258       dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
259
260     /* ditto */
261     if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
262          (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
263       dq->dqb_btimelimit = dqg.dqb_btimelimit;
264
265 #endif /* TRU64 */
266
267 #endif /* ultrix */
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             syslog( LOG_INFO, "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             syslog( LOG_ERR, "getquota: malloc: %m" );
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             syslog( LOG_INFO, "open %s: %m", buf );
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             syslog( LOG_INFO, "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             syslog( LOG_ERR, "getquota: malloc: %m" );
315             return AFPERR_MISC;
316         }
317         strcpy( vol->v_gvs, p );
318     }
319 #endif
320
321     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) : 
322       getfsquota(vol, uuid, dq);
323 }
324
325 static int overquota( dqblk )
326     struct dqblk        *dqblk;
327 {
328     struct timeval      tv;
329
330     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
331         return( 0 );
332     }
333 #ifdef ultrix
334     if ( dqblk->dqb_bwarn ) {
335         return( 0 );
336     }
337 #else /* ultrix */
338     if ( gettimeofday( &tv, 0 ) < 0 ) {
339         syslog( LOG_ERR, "overquota: gettimeofday: %m" );
340         return( AFPERR_PARAM );
341     }
342     if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
343         return( 0 );
344     }
345 #endif /* ultrix */
346     return( 1 );
347 }
348
349 /*
350  * This next bit is basically for linux -- everything is fine
351  * if you use 1k blocks... but if you try (for example) to mount
352  * a volume via nfs from a netapp (which might use 4k blocks) everything
353  * gets reported improperly.  I have no idea about dbtob on other
354  * platforms.
355  */
356
357 #ifdef HAVE_BROKEN_DBTOB
358 #undef dbtob
359 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
360 #define HAVE_2ARG_DBTOB
361 #endif
362
363 #ifndef dbtob
364 #define dbtob(a)       ((a) << 10)
365 #endif
366
367 /* i do the cast to VolSpace here to make sure that 64-bit shifts 
368    work */
369 #ifdef HAVE_2ARG_DBTOB
370 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
371 #else 
372 #define tobytes(a, b)  dbtob((VolSpace) (a))
373 #endif
374 int uquota_getvolspace( vol, bfree, btotal, bsize)
375     const struct vol    *vol;
376     VolSpace    *bfree, *btotal;
377     const u_int32_t bsize;
378 {
379     struct dqblk        dqblk;
380
381     if (getquota( vol, &dqblk, bsize) != 0 ) {
382       return( AFPERR_PARAM );
383     }
384
385     /* no limit set for this user. it might be set in the future. */
386     if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
387        *btotal = *bfree = ~((VolSpace) 0);
388     } else if ( overquota( &dqblk )) {
389         *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
390         *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) - 
391           tobytes( dqblk.dqb_curblocks, bsize );
392     } else {
393         *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
394         *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize  ) - 
395           tobytes( dqblk.dqb_curblocks, bsize );
396     }
397
398     return( AFP_OK );
399 }
400 #endif