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