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