]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
merged logging code into main branch. use configure option --without-logfile to...
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.14 2002-01-04 04:45:47 sibaz 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 ( quotactl( vol->v_gvs, QCMD(Q_GETQUOTA,USRQUOTA),
235                    uid, (char *)dq ) != 0 ) {
236         return( AFPERR_PARAM );
237     }
238
239     if (ngroups == 1)
240         quotactl(vol->v_gvs, QCMD(Q_GETQUOTA, GRPQUOTA),
241                  groups[0], (char *) &dqg);
242
243 #elif defined(TRU64)
244     if ( seteuid( getuid() ) == 0 ) {
245         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
246                        uid, (char *)dq ) != 0 ) {
247             seteuid( uid );
248             return ( AFPERR_PARAM );
249         }
250         seteuid( uid );
251     }
252
253 #else /* BSD4_4 */
254     if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
255                   (caddr_t) dq ) != 0 ) {
256         return( AFPERR_PARAM );
257     }
258
259     if (ngroups == 1)
260         quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
261                  groups[0], (char *) &dqg);
262 #endif  /* BSD4_4 */
263
264 #ifndef TRU64
265     /* set stuff up for group quotas if necessary */
266
267     /* max(user blocks, group blocks) */
268     if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
269         dq->dqb_curblocks = dqg.dqb_curblocks;
270
271     /* min(user limit, group limit) */
272     if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
273                                (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
274         dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
275
276     /* ditto */
277     if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
278                                (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
279         dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
280
281     /* ditto */
282     if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
283                                (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
284         dq->dqb_btimelimit = dqg.dqb_btimelimit;
285
286 #endif /* TRU64 */
287
288 #endif /* ultrix */
289 #endif /* __svr4__ */
290
291     return AFP_OK;
292 }
293
294
295 static int getquota( vol, dq, bsize)
296 struct vol              *vol;
297 struct dqblk    *dq;
298 const u_int32_t     bsize;
299 {
300     char *p;
301
302 #ifdef __svr4__
303     char                buf[ MAXPATHLEN + 1];
304
305     if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
306         if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
307             LOG(log_info, logtype_default, "getquota: mountp %s fails", vol->v_path );
308             return( AFPERR_PARAM );
309         }
310
311         if (vol->v_nfs) {
312             if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
313                 LOG(log_error, logtype_default, "getquota: malloc: %m" );
314                 return AFPERR_MISC;
315             }
316             strcpy( vol->v_gvs, p );
317
318         } else {
319             sprintf( buf, "%s/quotas", p );
320             if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
321                 LOG(log_info, logtype_default, "open %s: %m", buf );
322                 return( AFPERR_PARAM );
323             }
324         }
325
326     }
327 #else
328     if ( vol->v_gvs == NULL ) {
329         if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
330             LOG(log_info, logtype_default, "getquota: special %s fails", vol->v_path );
331             return( AFPERR_PARAM );
332         }
333
334         if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
335             LOG(log_error, logtype_default, "getquota: malloc: %s", strerror(errno) );
336             return AFPERR_MISC;
337         }
338         strcpy( vol->v_gvs, p );
339     }
340 #endif
341
342     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
343            getfsquota(vol, uuid, dq);
344 }
345
346 static int overquota( dqblk )
347 struct dqblk    *dqblk;
348 {
349     struct timeval      tv;
350
351     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
352         return( 0 );
353     }
354 #ifdef ultrix
355     if ( dqblk->dqb_bwarn ) {
356         return( 0 );
357     }
358 #else /* ultrix */
359     if ( gettimeofday( &tv, 0 ) < 0 ) {
360         LOG(log_error, logtype_default, "overquota: gettimeofday: %s", strerror(errno) );
361         return( AFPERR_PARAM );
362     }
363     if ( !dqblk->dqb_btimelimit || dqblk->dqb_btimelimit > tv.tv_sec ) {
364         return( 0 );
365     }
366 #endif /* ultrix */
367     return( 1 );
368 }
369
370 /*
371  * This next bit is basically for linux -- everything is fine
372  * if you use 1k blocks... but if you try (for example) to mount
373  * a volume via nfs from a netapp (which might use 4k blocks) everything
374  * gets reported improperly.  I have no idea about dbtob on other
375  * platforms.
376  */
377
378 #ifdef HAVE_BROKEN_DBTOB
379 #undef dbtob
380 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
381 #define HAVE_2ARG_DBTOB
382 #endif
383
384 #ifndef dbtob
385 #define dbtob(a)       ((a) << 10)
386 #endif
387
388 /* i do the cast to VolSpace here to make sure that 64-bit shifts
389    work */
390 #ifdef HAVE_2ARG_DBTOB
391 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
392 #else 
393 #define tobytes(a, b)  dbtob((VolSpace) (a))
394 #endif
395 int uquota_getvolspace( vol, bfree, btotal, bsize)
396 const struct vol        *vol;
397 VolSpace        *bfree, *btotal;
398 const u_int32_t bsize;
399 {
400     struct dqblk        dqblk;
401
402     if (getquota( vol, &dqblk, bsize) != 0 ) {
403         return( AFPERR_PARAM );
404     }
405
406     /* no limit set for this user. it might be set in the future. */
407     if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
408         *btotal = *bfree = ~((VolSpace) 0);
409     } else if ( overquota( &dqblk )) {
410         *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
411         *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
412                  tobytes( dqblk.dqb_curblocks, bsize );
413     } else {
414         *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
415         *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize  ) -
416                  tobytes( dqblk.dqb_curblocks, bsize );
417     }
418
419     return( AFP_OK );
420 }
421 #endif