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