]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/quota.c
Add support for new NetBSD quota subsystem, Bug ID 3249879.
[netatalk.git] / etc / afpd / quota.c
1 /*
2  * $Id: quota.c,v 1.35 2010-04-03 07:11:35 franklahm 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 #ifndef NO_QUOTA_SUPPORT
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 /* STDC check */
19 #if STDC_HEADERS
20 #include <string.h>
21 #else /* STDC_HEADERS */
22 #ifndef HAVE_STRCHR
23 #define strchr index
24 #define strrchr index
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
27 #ifndef HAVE_MEMCPY
28 #define memcpy(d,s,n) bcopy ((s), (d), (n))
29 #define memmove(d,s,n) bcopy ((s), (d), (n))
30 #endif /* ! HAVE_MEMCPY */
31 #endif /* STDC_HEADERS */
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
42 #include <atalk/logger.h>
43 #include <atalk/afp.h>
44 #include <atalk/compat.h>
45
46 #include "auth.h"
47 #include "volume.h"
48 #include "unix.h"
49
50 #ifdef HAVE_LIBQUOTA
51 #include <quota/quota.h>
52
53 static int
54 getfreespace(struct vol *vol, VolSpace *bfree, VolSpace *btotal,
55     uid_t uid, const char *classq)
56 {
57         int retq;
58         struct ufs_quota_entry ufsq[QUOTA_NLIMITS];
59         time_t now;
60
61         if (time(&now) == -1) {
62                 LOG(log_info, logtype_afpd, "time(): %s",
63                     strerror(errno));
64                 return -1;
65         }
66
67         if ( seteuid( getuid() ) != 0 )  {
68                 LOG(log_info, logtype_afpd, "seteuid(): %s",
69                     strerror(errno));
70                 return -1;
71         }
72         if ((retq = getfsquota(vol->v_path, ufsq, uid, classq)) < 0) {
73                 LOG(log_info, logtype_afpd, "getfsquota(%s, %s): %s",
74                     vol->v_path, classq, strerror(errno));
75         }
76         seteuid( uid );
77         if (retq < 1)
78                 return retq;
79
80         switch(QL_STATUS(quota_check_limit(ufsq[QUOTA_LIMIT_BLOCK].ufsqe_cur, 1,
81             ufsq[QUOTA_LIMIT_BLOCK].ufsqe_softlimit,
82             ufsq[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit,
83             ufsq[QUOTA_LIMIT_BLOCK].ufsqe_time, now))) {
84         case QL_S_DENY_HARD:
85         case QL_S_DENY_GRACE:
86                 *bfree = 0;
87                 *btotal = dbtob(ufsq[QUOTA_LIMIT_BLOCK].ufsqe_cur);
88                 break;
89         default:
90                 *bfree = dbtob(ufsq[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit -
91                     ufsq[QUOTA_LIMIT_BLOCK].ufsqe_cur);
92                 *btotal = dbtob(ufsq[QUOTA_LIMIT_BLOCK].ufsqe_hardlimit);
93                 break;
94         }
95         return 1;
96 }
97
98 int uquota_getvolspace( struct vol *vol, VolSpace *bfree, VolSpace *btotal, const u_int32_t bsize)
99 {
100         int uretq, gretq;
101         VolSpace ubfree, ubtotal;
102         VolSpace gbfree, gbtotal;
103
104         uretq = getfreespace(vol, &ubfree, &ubtotal,
105             uuid, QUOTADICT_CLASS_USER);
106         LOG(log_info, logtype_afpd, "getfsquota(%s): %d %d",
107             vol->v_path, (int)ubfree, (int)ubtotal);
108         if (ngroups >= 1) {
109                 gretq = getfreespace(vol, &ubfree, &ubtotal,
110                     groups[0], QUOTADICT_CLASS_GROUP);
111         } else
112                 gretq = -1;
113         if (uretq < 1 && gretq < 1) { /* no quota for this fs */
114                 return AFPERR_PARAM;
115         }
116         if (uretq < 1) {
117                 /* use group quotas */
118                 *bfree = gbfree;
119                 *btotal = gbtotal;
120         } else if (gretq < 1) {
121                 /* use user quotas */
122                 *bfree = ubfree;
123                 *btotal = ubtotal;
124         } else {
125                 /* return smallest remaining space of user and group */
126                 if (ubfree < gbfree) {
127                         *bfree = ubfree;
128                         *btotal = ubtotal;
129                 } else {
130                         *bfree = gbfree;
131                         *btotal = gbtotal;
132                 }
133         }
134         return AFP_OK;
135
136 }
137
138 #else /* HAVE_LIBQUOTA */
139
140 /*
141 #define DEBUG_QUOTA 0
142 */
143
144 #define WANT_USER_QUOTA 0
145 #define WANT_GROUP_QUOTA 1
146
147 #ifdef NEED_QUOTACTL_WRAPPER
148 int quotactl(int cmd, const char *special, int id, caddr_t addr)
149 {
150     return syscall(__NR_quotactl, cmd, special, id, addr);
151 }
152 #endif /* NEED_QUOTACTL_WRAPPER */
153
154 static int overquota( struct dqblk *);
155
156 #ifdef linux
157
158 #ifdef HAVE_LINUX_XQM_H
159 #include <linux/xqm.h>
160 #else
161 #ifdef HAVE_XFS_XQM_H
162 #include <xfs/xqm.h>
163 #define HAVE_LINUX_XQM_H
164 #else
165 #ifdef  HAVE_LINUX_DQBLK_XFS_H
166 #include <linux/dqblk_xfs.h>
167 #define HAVE_LINUX_XQM_H
168 #endif /* HAVE_LINUX_DQBLK_XFS_H */
169 #endif /* HAVE_XFS_XQM_H */
170 #endif /* HAVE_LINUX_XQM_H */
171
172 #include <linux/unistd.h>
173
174 static int is_xfs = 0;
175
176 static int get_linux_xfs_quota(int, char*, uid_t, struct dqblk *);
177 static int get_linux_fs_quota(int, char*, uid_t, struct dqblk *);
178
179 /* format supported by current kernel */
180 static int kernel_iface = IFACE_UNSET;
181
182 /*
183 **  Check kernel quota version
184 **  Taken from quota-tools 3.08 by Jan Kara <jack@suse.cz>
185 */
186 static void linuxquota_get_api( void )
187 {
188 #ifndef LINUX_API_VERSION
189     struct stat st;
190
191     if (stat("/proc/sys/fs/quota", &st) == 0) {
192         kernel_iface = IFACE_GENERIC;
193     }
194     else {
195         struct dqstats_v2 v2_stats;
196         struct sigaction  sig;
197         struct sigaction  oldsig;
198
199         /* This signal handling is needed because old kernels send us SIGSEGV as they try to resolve the device */
200         sig.sa_handler   = SIG_IGN;
201         sig.sa_sigaction = NULL;
202         sig.sa_flags     = 0;
203         sigemptyset(&sig.sa_mask);
204         if (sigaction(SIGSEGV, &sig, &oldsig) < 0) {
205             LOG( log_error, logtype_afpd, "cannot set SEGV signal handler: %s", strerror(errno));
206             goto failure;
207         }
208         if (quotactl(QCMD(Q_V2_GETSTATS, 0), NULL, 0, (void *)&v2_stats) >= 0) {
209             kernel_iface = IFACE_VFSV0;
210         }
211         else if (errno != ENOSYS && errno != ENOTSUP) {
212             /* RedHat 7.1 (2.4.2-2) newquota check 
213              * Q_V2_GETSTATS in it's old place, Q_GETQUOTA in the new place
214              * (they haven't moved Q_GETSTATS to its new value) */
215             int err_stat = 0;
216             int err_quota = 0;
217             char tmp[1024];         /* Just temporary buffer */
218
219             if (quotactl(QCMD(Q_V1_GETSTATS, 0), NULL, 0, tmp))
220                 err_stat = errno;
221             if (quotactl(QCMD(Q_V1_GETQUOTA, 0), "/dev/null", 0, tmp))
222                 err_quota = errno;
223
224             /* On a RedHat 2.4.2-2      we expect 0, EINVAL
225              * On a 2.4.x               we expect 0, ENOENT
226              * On a 2.4.x-ac    we wont get here */
227             if (err_stat == 0 && err_quota == EINVAL) {
228                 kernel_iface = IFACE_VFSV0;
229             }
230             else {
231                 kernel_iface = IFACE_VFSOLD;
232             }
233         }
234         else {
235             /* This branch is *not* in quota-tools 3.08
236             ** but without it quota version is not correctly
237             ** identified for the original SuSE 8.0 kernel */
238             unsigned int vers_no;
239             FILE * qf;
240
241             if ((qf = fopen("/proc/fs/quota", "r"))) {
242                 if (fscanf(qf, "Version %u", &vers_no) == 1) {
243                     if ( (vers_no == (6*10000 + 5*100 + 0)) ||
244                          (vers_no == (6*10000 + 5*100 + 1)) ) {
245                         kernel_iface = IFACE_VFSV0;
246                     }
247                 }
248                 fclose(qf);
249             }
250         }
251         if (sigaction(SIGSEGV, &oldsig, NULL) < 0) {
252             LOG(log_error, logtype_afpd, "cannot reset signal handler: %s", strerror(errno));
253             goto failure;
254         }
255     }
256
257 failure:
258     if (kernel_iface == IFACE_UNSET)
259        kernel_iface = IFACE_VFSOLD;
260
261 #else /* defined LINUX_API_VERSION */
262     kernel_iface = LINUX_API_VERSION;
263 #endif
264 }
265
266 /****************************************************************************/
267
268 static int get_linux_quota(int what, char *path, uid_t euser_id, struct dqblk *dp)
269 {
270         int r; /* result */
271
272         if ( is_xfs )
273                 r=get_linux_xfs_quota(what, path, euser_id, dp);
274         else
275                 r=get_linux_fs_quota(what, path, euser_id, dp);
276     
277         return r;
278 }
279
280 /****************************************************************************
281  Abstract out the XFS Quota Manager quota get call.
282 ****************************************************************************/
283
284 static int get_linux_xfs_quota(int what, char *path, uid_t euser_id, struct dqblk *dqb)
285 {
286         int ret = -1;
287 #ifdef HAVE_LINUX_XQM_H
288         struct fs_disk_quota D;
289         
290         memset (&D, 0, sizeof(D));
291
292         if ((ret = quotactl(QCMD(Q_XGETQUOTA,(what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t)&D)))
293                return ret;
294
295         dqb->bsize = (u_int64_t)512;
296         dqb->dqb_bsoftlimit  = (u_int64_t)D.d_blk_softlimit;
297         dqb->dqb_bhardlimit  = (u_int64_t)D.d_blk_hardlimit;
298         dqb->dqb_ihardlimit  = (u_int64_t)D.d_ino_hardlimit;
299         dqb->dqb_isoftlimit  = (u_int64_t)D.d_ino_softlimit;
300         dqb->dqb_curinodes   = (u_int64_t)D.d_icount;
301         dqb->dqb_curblocks   = (u_int64_t)D.d_bcount; 
302 #endif
303        return ret;
304 }
305
306 /*
307 ** Wrapper for the quotactl(GETQUOTA) call.
308 ** For API v2 the results are copied back into a v1 structure.
309 ** Taken from quota-1.4.8 perl module
310 */
311 static int get_linux_fs_quota(int what, char *path, uid_t euser_id, struct dqblk *dqb)
312 {
313         int ret;
314
315         if (kernel_iface == IFACE_UNSET)
316                 linuxquota_get_api();
317
318         if (kernel_iface == IFACE_GENERIC)
319         {
320                 struct dqblk_v3 dqb3;
321
322                 ret = quotactl(QCMD(Q_V3_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb3);
323                 if (ret == 0)
324                 {
325                         dqb->dqb_bhardlimit = dqb3.dqb_bhardlimit;
326                         dqb->dqb_bsoftlimit = dqb3.dqb_bsoftlimit;
327                         dqb->dqb_curblocks  = dqb3.dqb_curspace / DEV_QBSIZE;
328                         dqb->dqb_ihardlimit = dqb3.dqb_ihardlimit;
329                         dqb->dqb_isoftlimit = dqb3.dqb_isoftlimit;
330                         dqb->dqb_curinodes  = dqb3.dqb_curinodes;
331                         dqb->dqb_btime      = dqb3.dqb_btime;
332                         dqb->dqb_itime      = dqb3.dqb_itime;
333                         dqb->bsize          = DEV_QBSIZE;
334                 }
335         }
336         else if (kernel_iface == IFACE_VFSV0)
337         {
338                 struct dqblk_v2 dqb2;
339
340                 ret = quotactl(QCMD(Q_V2_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb2);
341                 if (ret == 0)
342                 {
343                         dqb->dqb_bhardlimit = dqb2.dqb_bhardlimit;
344                         dqb->dqb_bsoftlimit = dqb2.dqb_bsoftlimit;
345                         dqb->dqb_curblocks  = dqb2.dqb_curspace / DEV_QBSIZE;
346                         dqb->dqb_ihardlimit = dqb2.dqb_ihardlimit;
347                         dqb->dqb_isoftlimit = dqb2.dqb_isoftlimit;
348                         dqb->dqb_curinodes  = dqb2.dqb_curinodes;
349                         dqb->dqb_btime      = dqb2.dqb_btime;
350                         dqb->dqb_itime      = dqb2.dqb_itime;
351                         dqb->bsize          = DEV_QBSIZE;
352                 }
353         }
354         else /* if (kernel_iface == IFACE_VFSOLD) */
355         {
356                 struct dqblk_v1 dqb1;
357
358                 ret = quotactl(QCMD(Q_V1_GETQUOTA, (what ? GRPQUOTA : USRQUOTA)), path, euser_id, (caddr_t) &dqb1);
359                 if (ret == 0)
360                 {
361                         dqb->dqb_bhardlimit = dqb1.dqb_bhardlimit;
362                         dqb->dqb_bsoftlimit = dqb1.dqb_bsoftlimit;
363                         dqb->dqb_curblocks  = dqb1.dqb_curblocks;
364                         dqb->dqb_ihardlimit = dqb1.dqb_ihardlimit;
365                         dqb->dqb_isoftlimit = dqb1.dqb_isoftlimit;
366                         dqb->dqb_curinodes  = dqb1.dqb_curinodes;
367                         dqb->dqb_btime      = dqb1.dqb_btime;
368                         dqb->dqb_itime      = dqb1.dqb_itime;
369                         dqb->bsize          = DEV_QBSIZE;
370                 }
371         }
372         return ret;
373 }
374
375 #endif /* linux */
376
377 #if defined(HAVE_SYS_MNTTAB_H) || defined(__svr4__)
378 /*
379  * Return the mount point associated with the filesystem
380  * on which "file" resides.  Returns NULL on failure.
381  */
382 static char *
383 mountp( char *file, int *nfs)
384 {
385     struct stat                 sb;
386     FILE                        *mtab;
387     dev_t                       devno;
388     static struct mnttab        mnt;
389
390     if ( lstat( file, &sb ) < 0 ) {
391         return( NULL );
392     }
393     devno = sb.st_dev;
394
395     if (( mtab = fopen( "/etc/mnttab", "r" )) == NULL ) {
396         return( NULL );
397     }
398
399     while ( getmntent( mtab, &mnt ) == 0 ) {
400         /* local fs */
401         if ( (lstat( mnt.mnt_special, &sb ) == 0) && (devno == sb.st_rdev)) {
402             fclose( mtab );
403             return mnt.mnt_mountp;
404         }
405
406         /* check for nfs. we probably should use
407          * strcmp(mnt.mnt_fstype, MNTTYPE_NFS), but that's not as fast. */
408         if ((lstat(mnt.mnt_mountp, &sb) == 0) && (devno == sb.st_dev) &&
409                 strchr(mnt.mnt_special, ':')) {
410             *nfs = 1;
411             fclose( mtab );
412             return mnt.mnt_special;
413         }
414     }
415
416     fclose( mtab );
417     return( NULL );
418 }
419
420 #else /* __svr4__ */
421 #ifdef ultrix
422 /*
423 * Return the block-special device name associated with the filesystem
424 * on which "file" resides.  Returns NULL on failure.
425 */
426
427 static char *
428 special( char *file, int *nfs)
429 {
430     static struct fs_data       fsd;
431
432     if ( getmnt(0, &fsd, 0, STAT_ONE, file ) < 0 ) {
433         LOG(log_info, logtype_afpd, "special: getmnt %s: %s", file, strerror(errno) );
434         return( NULL );
435     }
436
437     /* XXX: does this really detect an nfs mounted fs? */
438     if (strchr(fsd.fd_req.devname, ':'))
439         *nfs = 1;
440     return( fsd.fd_req.devname );
441 }
442
443 #else /* ultrix */
444 #if (defined(HAVE_SYS_MOUNT_H) && !defined(__linux__)) || defined(BSD4_4) || defined(_IBMR2)
445
446 static char *
447 special(char *file, int *nfs)
448 {
449     static struct statfs        sfs;
450
451     if ( statfs( file, &sfs ) < 0 ) {
452         return( NULL );
453     }
454
455 #ifdef TRU64
456     /* Digital UNIX: The struct sfs contains a field sfs.f_type,
457      * the MOUNT_* constants are defined in <sys/mount.h> */
458     if ((sfs.f_type == MOUNT_NFS)||(sfs.f_type == MOUNT_NFS3))
459 #else /* TRU64 */
460     /* XXX: make sure this really detects an nfs mounted fs */
461     if (strchr(sfs.f_mntfromname, ':'))
462 #endif /* TRU64 */
463         *nfs = 1;
464     return( sfs.f_mntfromname );
465 }
466
467 #else /* BSD4_4 */
468
469 static char *
470 special(char *file, int *nfs)
471 {
472     struct stat         sb;
473     FILE                *mtab;
474     dev_t               devno;
475     struct mntent       *mnt;
476     int                 found=0;
477
478     if ( lstat( file, &sb ) < 0 ) {
479         return( NULL );
480     }
481     devno = sb.st_dev;
482
483     if (( mtab = setmntent( "/etc/mtab", "r" )) == NULL ) {
484         return( NULL );
485     }
486
487     while (( mnt = getmntent( mtab )) != NULL ) {
488         /* check for local fs */
489         if ( (lstat( mnt->mnt_fsname, &sb ) == 0) && devno == sb.st_rdev) {
490             found = 1;
491             break;
492         }
493
494         /* check for an nfs mount entry. the alternative is to use
495         * strcmp(mnt->mnt_type, MNTTYPE_NFS) instead of the strchr. */
496         if ((lstat(mnt->mnt_dir, &sb) == 0) && (devno == sb.st_dev) &&
497                 strchr(mnt->mnt_fsname, ':')) {
498             *nfs = 1;
499             found = 1;
500             break;
501         }
502     }
503
504     endmntent( mtab );
505
506     if (!found)
507         return (NULL);
508 #ifdef linux
509     if (strcmp(mnt->mnt_type, "xfs") == 0)
510         is_xfs = 1;
511 #endif
512         
513     return( mnt->mnt_fsname );
514 }
515
516 #endif /* BSD4_4 */
517 #endif /* ultrix */
518 #endif /* __svr4__ */
519
520
521 static int getfsquota(struct vol *vol, const int uid, struct dqblk *dq)
522
523 {
524         struct dqblk dqg;
525
526 #ifdef __svr4__
527     struct quotctl      qc;
528 #endif
529
530     memset(&dqg, 0, sizeof(dqg));
531         
532 #ifdef __svr4__
533     qc.op = Q_GETQUOTA;
534     qc.uid = uid;
535     qc.addr = (caddr_t)dq;
536     if ( ioctl( vol->v_qfd, Q_QUOTACTL, &qc ) < 0 ) {
537         return( AFPERR_PARAM );
538     }
539
540 #else /* __svr4__ */
541 #ifdef ultrix
542     if ( quota( Q_GETDLIM, uid, vol->v_gvs, dq ) != 0 ) {
543         return( AFPERR_PARAM );
544     }
545 #else /* ultrix */
546
547 #ifndef USRQUOTA
548 #define USRQUOTA   0
549 #endif
550
551 #ifndef QCMD
552 #define QCMD(a,b)  (a)
553 #endif
554
555 #ifndef TRU64
556     /* for group quotas. we only use these if the user belongs
557     * to one group. */
558 #endif /* TRU64 */
559
560 #ifdef BSD4_4
561     if ( seteuid( getuid() ) == 0 ) {
562         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA,USRQUOTA),
563                        uid, (char *)dq ) != 0 ) {
564             /* try group quotas */
565             if (ngroups >= 1) {
566                 if ( quotactl(vol->v_path, QCMD(Q_GETQUOTA, GRPQUOTA),
567                               groups[0], (char *) &dqg) != 0 ) {
568                     seteuid( uid );
569                     return( AFPERR_PARAM );
570                 }
571             }
572         }
573         seteuid( uid );
574     }
575
576 #elif defined(TRU64)
577     if ( seteuid( getuid() ) == 0 ) {
578         if ( quotactl( vol->v_path, QCMD(Q_GETQUOTA, USRQUOTA),
579                        uid, (char *)dq ) != 0 ) {
580             seteuid( uid );
581             return ( AFPERR_PARAM );
582         }
583         seteuid( uid );
584     }
585
586 #else /* BSD4_4 */
587     if (get_linux_quota (WANT_USER_QUOTA, vol->v_gvs, uid, dq) !=0) {
588         return( AFPERR_PARAM );
589     }
590
591     if (get_linux_quota(WANT_GROUP_QUOTA, vol->v_gvs, getegid(),  &dqg) != 0) {
592 #ifdef DEBUG_QUOTA
593         LOG(log_debug, logtype_afpd, "group quota did not work!" );
594 #endif /* DEBUG_QUOTA */
595
596         return AFP_OK; /* no need to check user vs group quota */
597     }
598 #endif  /* BSD4_4 */
599
600
601 #ifndef TRU64
602     /* return either the group quota entry or user quota entry,
603        whichever has the least amount of space remaining
604     */
605
606     /* if user space remaining > group space remaining */
607     if( 
608         /* if overquota, free space is 0 otherwise hard-current */
609         ( overquota( dq ) ? 0 : ( dq->dqb_bhardlimit ? dq->dqb_bhardlimit - 
610                                   dq->dqb_curblocks : ~((u_int64_t) 0) ) )
611
612       >
613         
614         ( overquota( &dqg ) ? 0 : ( dqg.dqb_bhardlimit ? dqg.dqb_bhardlimit - 
615                                     dqg.dqb_curblocks : ~((u_int64_t) 0) ) )
616
617       ) /* if */
618     {
619         /* use group quota limits rather than user limits */
620         dq->dqb_curblocks = dqg.dqb_curblocks;
621         dq->dqb_bhardlimit = dqg.dqb_bhardlimit;
622         dq->dqb_bsoftlimit = dqg.dqb_bsoftlimit;
623         dq->dqb_btimelimit = dqg.dqb_btimelimit;
624     } /* if */
625
626 #endif /* TRU64 */
627
628 #endif /* ultrix */
629 #endif /* __svr4__ */
630
631     return AFP_OK;
632 }
633
634
635 static int getquota( struct vol *vol, struct dqblk *dq, const u_int32_t bsize)
636 {
637     char *p;
638
639 #ifdef __svr4__
640     char                buf[ MAXPATHLEN + 1];
641
642     if ( vol->v_qfd == -1 && vol->v_gvs == NULL) {
643         if (( p = mountp( vol->v_path, &vol->v_nfs)) == NULL ) {
644             LOG(log_info, logtype_afpd, "getquota: mountp %s fails", vol->v_path );
645             return( AFPERR_PARAM );
646         }
647
648         if (vol->v_nfs) {
649             if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
650                 LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
651                 return AFPERR_MISC;
652             }
653             strcpy( vol->v_gvs, p );
654
655         } else {
656             sprintf( buf, "%s/quotas", p );
657             if (( vol->v_qfd = open( buf, O_RDONLY, 0 )) < 0 ) {
658                 LOG(log_info, logtype_afpd, "open %s: %s", buf, strerror(errno) );
659                 return( AFPERR_PARAM );
660             }
661         }
662
663     }
664 #else
665     if ( vol->v_gvs == NULL ) {
666         if (( p = special( vol->v_path, &vol->v_nfs )) == NULL ) {
667             LOG(log_info, logtype_afpd, "getquota: special %s fails", vol->v_path );
668             return( AFPERR_PARAM );
669         }
670
671         if (( vol->v_gvs = (char *)malloc( strlen( p ) + 1 )) == NULL ) {
672             LOG(log_error, logtype_afpd, "getquota: malloc: %s", strerror(errno) );
673             return AFPERR_MISC;
674         }
675         strcpy( vol->v_gvs, p );
676     }
677 #endif
678
679 #ifdef TRU64
680     /* Digital UNIX: Two forms of specifying an NFS filesystem are possible,
681        either 'hostname:path' or 'path@hostname' (Ultrix heritage) */
682     if (vol->v_nfs) {
683         char *hostpath;
684         char pathstring[MNAMELEN];
685         /* MNAMELEN ist defined in <sys/mount.h> */
686         int result;
687         
688         if ((hostpath = strchr(vol->v_gvs,'@')) != NULL ) {
689             /* convert 'path@hostname' to 'hostname:path',
690              * call getnfsquota(),
691              * convert 'hostname:path' back to 'path@hostname' */
692             *hostpath = '\0';
693             sprintf(pathstring,"%s:%s",hostpath+1,vol->v_gvs);
694             strcpy(vol->v_gvs,pathstring);
695             
696             result = getnfsquota(vol, uuid, bsize, dq);
697             
698             hostpath = strchr(vol->v_gvs,':');
699             *hostpath = '\0';
700             sprintf(pathstring,"%s@%s",hostpath+1,vol->v_gvs);
701             strcpy(vol->v_gvs,pathstring);
702             
703             return result;
704         }
705         else
706             /* vol->v_gvs is of the form 'hostname:path' */
707             return getnfsquota(vol, uuid, bsize, dq);
708     } else
709         /* local filesystem */
710         return getfsquota(vol, uuid, dq);
711            
712 #else /* TRU64 */
713     return vol->v_nfs ? getnfsquota(vol, uuid, bsize, dq) :
714            getfsquota(vol, uuid, dq);
715 #endif /* TRU64 */
716 }
717
718 static int overquota( struct dqblk *dqblk)
719 {
720     struct timeval      tv;
721
722     if ( dqblk->dqb_curblocks > dqblk->dqb_bhardlimit &&
723          dqblk->dqb_bhardlimit != 0 ) {
724         return( 1 );
725     }
726
727     if ( dqblk->dqb_curblocks < dqblk->dqb_bsoftlimit ||
728          dqblk->dqb_bsoftlimit == 0 ) {
729         return( 0 );
730     }
731 #ifdef ultrix
732     if ( dqblk->dqb_bwarn ) {
733         return( 0 );
734     }
735 #else /* ultrix */
736     if ( gettimeofday( &tv, NULL ) < 0 ) {
737         LOG(log_error, logtype_afpd, "overquota: gettimeofday: %s", strerror(errno) );
738         return( AFPERR_PARAM );
739     }
740     if ( dqblk->dqb_btimelimit && dqblk->dqb_btimelimit > tv.tv_sec ) {
741         return( 0 );
742     }
743 #endif /* ultrix */
744     return( 1 );
745 }
746
747 /*
748  * This next bit is basically for linux -- everything is fine
749  * if you use 1k blocks... but if you try (for example) to mount
750  * a volume via nfs from a netapp (which might use 4k blocks) everything
751  * gets reported improperly.  I have no idea about dbtob on other
752  * platforms.
753  */
754
755 #ifdef HAVE_BROKEN_DBTOB
756 #undef dbtob
757 #define dbtob(a, b)     ((VolSpace)((VolSpace)(a) * (VolSpace)(b)))
758 #define HAVE_2ARG_DBTOB
759 #endif
760
761 #ifndef dbtob
762 #define dbtob(a)       ((a) << 10)
763 #endif
764
765 /* i do the cast to VolSpace here to make sure that 64-bit shifts
766    work */
767 #ifdef HAVE_2ARG_DBTOB
768 #define tobytes(a, b)  dbtob((VolSpace) (a), (VolSpace) (b))
769 #else 
770 #define tobytes(a, b)  dbtob((VolSpace) (a))
771 #endif
772
773 int uquota_getvolspace( struct vol *vol, VolSpace *bfree, VolSpace *btotal, const u_int32_t bsize)
774 {
775         u_int64_t this_bsize;
776         struct dqblk dqblk;
777
778         this_bsize = bsize;
779                         
780         if (getquota( vol, &dqblk, bsize) != 0 ) {
781                 return( AFPERR_PARAM );
782         }
783
784 #ifdef linux
785         this_bsize = dqblk.bsize;
786 #endif
787
788 #ifdef DEBUG_QUOTA
789         LOG(log_debug, logtype_afpd, "after calling getquota in uquota_getvolspace!" );
790         LOG(log_debug, logtype_afpd, "dqb_ihardlimit: %u", dqblk.dqb_ihardlimit );
791         LOG(log_debug, logtype_afpd, "dqb_isoftlimit: %u", dqblk.dqb_isoftlimit );
792         LOG(log_debug, logtype_afpd, "dqb_curinodes : %u", dqblk.dqb_curinodes );
793         LOG(log_debug, logtype_afpd, "dqb_bhardlimit: %u", dqblk.dqb_bhardlimit );
794         LOG(log_debug, logtype_afpd, "dqb_bsoftlimit: %u", dqblk.dqb_bsoftlimit );
795         LOG(log_debug, logtype_afpd, "dqb_curblocks : %u", dqblk.dqb_curblocks );
796         LOG(log_debug, logtype_afpd, "dqb_btime     : %u", dqblk.dqb_btime );
797         LOG(log_debug, logtype_afpd, "dqb_itime     : %u", dqblk.dqb_itime );
798         LOG(log_debug, logtype_afpd, "bsize/this_bsize : %u/%u", bsize, this_bsize );
799         LOG(log_debug, logtype_afpd, "dqblk.dqb_bhardlimit size: %u", tobytes( dqblk.dqb_bhardlimit, this_bsize ));
800         LOG(log_debug, logtype_afpd, "dqblk.dqb_bsoftlimit size: %u", tobytes( dqblk.dqb_bsoftlimit, this_bsize ));
801         LOG(log_debug, logtype_afpd, "dqblk.dqb_curblocks  size: %u", tobytes( dqblk.dqb_curblocks, this_bsize ));
802 #endif /* DEBUG_QUOTA */ 
803
804         /* no limit set for this user. it might be set in the future. */
805         if (dqblk.dqb_bsoftlimit == 0 && dqblk.dqb_bhardlimit == 0) {
806                 *btotal = *bfree = ~((VolSpace) 0);
807         } else if ( overquota( &dqblk )) {
808                 if ( tobytes( dqblk.dqb_curblocks, this_bsize ) > tobytes( dqblk.dqb_bsoftlimit, this_bsize ) ) {
809                         *btotal = tobytes( dqblk.dqb_curblocks, this_bsize );
810                         *bfree = 0;
811                 }
812                 else {
813                         *btotal = tobytes( dqblk.dqb_bsoftlimit, this_bsize );
814                         *bfree  = tobytes( dqblk.dqb_bsoftlimit, this_bsize ) -
815                                   tobytes( dqblk.dqb_curblocks, this_bsize );
816                 }
817         } else {
818                 *btotal = tobytes( dqblk.dqb_bhardlimit, this_bsize );
819                 *bfree  = tobytes( dqblk.dqb_bhardlimit, this_bsize  ) -
820                           tobytes( dqblk.dqb_curblocks, this_bsize );
821         }
822
823 #ifdef DEBUG_QUOTA
824         LOG(log_debug, logtype_afpd, "bfree          : %u", *bfree );
825         LOG(log_debug, logtype_afpd, "btotal         : %u", *btotal );
826         LOG(log_debug, logtype_afpd, "bfree          : %uKB", *bfree/1024 );
827         LOG(log_debug, logtype_afpd, "btotal         : %uKB", *btotal/1024 );
828 #endif
829
830         return( AFP_OK );
831 }
832 #endif /* HAVE_LIBQUOTA */
833 #endif