]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
MFS: Fix NFS quotas on Tru64.
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.22 2002-08-29 17:22:06 jmarcus 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_afpd, "special: getmnt %s: %s", file, strerror(errno) );
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 #ifdef TRU64
143     /* Digital UNIX: The struct sfs contains a field sfs.f_type,
144      * the MOUNT_* constants are defined in <sys/mount.h> */
145     if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
146 #else /* TRU64 */
147     /* XXX: make sure this really detects an nfs mounted fs */
148     if (strchr(sfs.f_mntfromname, ':'))
149 #endif /* TRU64 */
150         *nfs = 1;
151     return( sfs.f_mntfromname );
152 }
153
154 #else /* BSD4_4 */
155
156 static char *
157 special( file, nfs )
158 char *file;
159 int *nfs;
160 {
161     struct stat         sb;
162     FILE                *mtab;
163     dev_t               devno;
164     struct mntent       *mnt;
165
166     if ( stat( file, &sb ) < 0 ) {
167         return( NULL );
168     }
169     devno = sb.st_dev;
170
171     if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
172         return( NULL );
173     }
174
175     while (( mnt = getmntent( mtab )) != NULL ) {
176         /* check for local fs */
177         if ( (stat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
178             endmntent( mtab );
179             return( mnt->mnt_fsname );
180         }
181
182         /* check for an nfs mount entry. the alternative is to use
183         * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
184         if ((stat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
185                 strchr(mnt->mnt_fsname, ':')) {
186             *nfs = 1;
187             endmntent( mtab );
188             return( mnt->mnt_fsname );
189         }
190     }
191
192     endmntent( mtab );
193     return( NULL );
194 }
195
196 #endif /* BSD4_4 */
197 #endif /* ultrix */
198 #endif /* __svr4__ */
199
200
201 static int getfsquota(vol, uid, dq)
202 struct vol          *vol;
203 const int           uid;
204 struct dqblk        *dq;
205 {
206 #ifdef __svr4__
207     struct quotctl      qc;
208
209     qc.op = Q_GETQUOTA;
210     qc.uid = uid;
211     qc.addr = (caddr_t)dq;
212     if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
213         return( AFPERR_PARAM );
214     }
215
216 #else /* __svr4__ */
217 #ifdef ultrix
218     if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
219         return( AFPERR_PARAM );
220     }
221 #else /* ultrix */
222
223 #ifndef USRQUOTA
224 #define USRQUOTA   0
225 #endif
226
227 #ifndef QCMD
228 #define QCMD(a,b)  (a)
229 #endif
230
231 #ifndef TRU64
232     /* for group quotas. we only use these if the user belongs
233     * to one group. */
234     struct dqblk        dqg;
235
236     memset(&dqg, 0, sizeof(dqg));
237 #endif /* TRU64 */
238
239 #ifdef BSD4_4
240     if ( seteuid( getuid() ) == 0 ) {
241         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
242                        uid, (char *)dq ) != 0 ) {
243             /* try group quotas */
244             if (ngroups >= 1) {
245                 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
246                               groups[0], (char *) &dqg) != 0 ) {
247                     seteuid( uid );
248                     return( AFPERR_PARAM );
249                 }
250             }
251         }
252         seteuid( uid );
253     }
254
255 #elif defined(TRU64)
256     if ( seteuid( getuid() ) == 0 ) {
257         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
258                        uid, (char *)dq ) != 0 ) {
259             seteuid( uid );
260             return ( AFPERR_PARAM );
261         }
262         seteuid( uid );
263     }
264
265 #else /* BSD4_4 */
266     if ( quotactl(QCMD(Q_GETQUOTA, USRQUOTA), vol->v_gvs, uid,
267                   (caddr_t) dq ) != 0 ) {
268         return( AFPERR_PARAM );
269     }
270
271     if (ngroups >= 1)
272         quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), vol->v_gvs,
273                  groups[0], (char *) &dqg);
274 #endif  /* BSD4_4 */
275
276 #ifndef TRU64
277     /* set stuff up for group quotas if necessary */
278
279     /* max(user blocks, group blocks) */
280     if (dqg.dqb_curblocks && (dq->dqb_curblocks < dqg.dqb_curblocks))
281         dq->dqb_curblocks = dqg.dqb_curblocks;
282
283     /* min(user limit, group limit) */
284     if (dqg.dqb_bhardlimit && (!dq->dqb_bhardlimit ||
285                                (dq->dqb_bhardlimit > dqg.dqb_bhardlimit)))
286         dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
287
288     /* ditto */
289     if (dqg.dqb_bsoftlimit && (!dq->dqb_bsoftlimit ||
290                                (dq->dqb_bsoftlimit > dqg.dqb_bsoftlimit)))
291         dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
292
293     /* ditto */
294     if (dqg.dqb_btimelimit && (!dq->dqb_btimelimit ||
295                                (dq->dqb_btimelimit > dqg.dqb_btimelimit)))
296         dq->dqb_btimelimit = dqg.dqb_btimelimit;
297
298 #endif /* TRU64 */
299
300 #endif /* ultrix */
301 #endif /* __svr4__ */
302
303     return AFP_OK;
304 }
305
306
307 static int getquota( vol, dq, bsize)
308 struct vol              *vol;
309 struct dqblk    *dq;
310 const u_int32_t     bsize;
311 {
312     char *p;
313
314 #ifdef __svr4__
315     char                buf[ MAXPATHLEN + 1];
316
317     if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
318         if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
319             LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
320             return( AFPERR_PARAM );
321         }
322
323         if (vol->v_nfs) {
324             if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
325                 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
326                 return AFPERR_MISC;
327             }
328             strcpy( vol->v_gvs, p );
329
330         } else {
331             sprintf( buf, "%s/quotas", p );
332             if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
333                 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
334                 return( AFPERR_PARAM );
335             }
336         }
337
338     }
339 #else
340     if ( vol->v_gvs == NULL ) {
341         if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
342             LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
343             return( AFPERR_PARAM );
344         }
345
346         if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
347             LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
348             return AFPERR_MISC;
349         }
350         strcpy( vol->v_gvs, p );
351     }
352 #endif
353
354 #ifdef TRU64
355     /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
356        either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
357     if (vol->v_nfs) {
358         char *hostpath;
359         char pathstring[MNAMELEN];
360         /* MNAMELEN ist defined in <sys/mount.h> */
361         int result;
362         
363         if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
364             /* convert 'path@hostname' to 'hostname:path',
365              * call getnfsquota(),
366              * convert 'hostname:path' back to 'path@hostname' */
367             *hostpath = '\0';
368             sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
369             strcpy(vol->v_gvs,pathstring);
370             
371             result = getnfsquota(vol, uuid, bsize, dq);
372             
373             hostpath = strchr(vol->v_gvs,':');
374             *hostpath = '\0';
375             sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
376             strcpy(vol->v_gvs,pathstring);
377             
378             return result;
379         }
380         else
381             /* vol->v_gvs is of the form 'hostname:path' */
382             return getnfsquota(vol, uuid, bsize, dq);
383     } else
384         /* local filesystem */
385         return getfsquota(vol, uuid, dq);
386            
387 #else /* TRU64 */
388     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
389            getfsquota(vol, uuid, dq);
390 #endif /* TRU64 */
391 }
392
393 static int overquota( dqblk )
394 struct dqblk    *dqblk;
395 {
396     struct timeval      tv;
397
398     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ) {
399         return( 0 );
400     }
401 #ifdef ultrix
402     if ( dqblk->dqb_bwarn ) {
403         return( 0 );
404     }
405 #else /* ultrix */
406     if ( gettimeofday( &tv, 0 ) < 0 ) {
407         LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
408         return( AFPERR_PARAM );
409     }
410     if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
411         return( 0 );
412     }
413 #endif /* ultrix */
414     return( 1 );
415 }
416
417 /*
418  * This next bit is basically for linux -- everything is fine
419  * if you use 1k blocks... but if you try (for example) to mount
420  * a volume via nfs from a netapp (which might use 4k blocks) everything
421  * gets reported improperly.  I have no idea about dbtob on other
422  * platforms.
423  */
424
425 #ifdef HAVE_BROKEN_DBTOB
426 #undef dbtob
427 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
428 #define HAVE_2ARG_DBTOB
429 #endif
430
431 #ifndef dbtob
432 #define dbtob(a)       ((a) << 10)
433 #endif
434
435 /* i do the cast to VolSpace here to make sure that 64-bit shifts
436    work */
437 #ifdef HAVE_2ARG_DBTOB
438 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
439 #else 
440 #define tobytes(a, b)  dbtob((VolSpace) (a))
441 #endif
442 int uquota_getvolspace( vol, bfree, btotal, bsize)
443 const struct vol        *vol;
444 VolSpace        *bfree, *btotal;
445 const u_int32_t bsize;
446 {
447     struct dqblk        dqblk;
448
449     if (getquota( vol, &dqblk, bsize) != 0 ) {
450         return( AFPERR_PARAM );
451     }
452
453     /* no limit set for this user. it might be set in the future. */
454     if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
455         *btotal = *bfree = ~((VolSpace) 0);
456     } else if ( overquota( &dqblk )) {
457         if ( tobytes( dqblk.dqb_curblocks, bsize ) > tobytes( dqblk.dqb_bhardlimit, bsize ) ) {
458             *btotal = tobytes( dqblk.dqb_curblocks, bsize );
459             *bfree = 0;
460         }
461         else {
462             *btotal = tobytes( dqblk.dqb_bhardlimit, bsize );
463             *bfree = tobytes( dqblk.dqb_bhardlimit, bsize ) -
464                      tobytes( dqblk.dqb_curblocks, bsize );
465         }
466     } else {
467         *btotal = tobytes( dqblk.dqb_bsoftlimit, bsize );
468         *bfree = tobytes( dqblk.dqb_bsoftlimit, bsize  ) -
469                  tobytes( dqblk.dqb_curblocks, bsize );
470     }
471
472     return( AFP_OK );
473 }
474 #endif