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