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