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