]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/nfsquota.c
Initial revision
[netatalk.git] / etc / afpd / nfsquota.c
1 /* parts of this are lifted from the bsd quota program and are
2  * therefore under the following copyright:
3  *
4  * Copyright (c) 1980, 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Robert Elz at The University of Melbourne.
9  *
10  * Ported for AIX (jfs) by Joerg Schumacher (J.Schumacher@tu-bs.de) at the
11  * Technische Universitaet Braunschweig, FRG
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <sys/param.h> /* for DEV_BSIZE */
19 #include <sys/time.h>  /* <rpc/rpc.h> on ultrix doesn't include this */
20 #include <syslog.h>
21
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <rpc/rpc.h>
25 #include <rpc/pmap_prot.h>
26 #include <rpcsvc/rquota.h>
27
28 #include <atalk/afp.h>
29
30 #include "unix.h"
31
32 #ifndef NO_QUOTA_SUPPORT
33 /* lifted (with modifications) from the bsd quota program */
34 static int 
35 callaurpc(vol, prognum, versnum, procnum, inproc, in, outproc, out)
36         struct vol *vol;
37         u_long prognum, versnum, procnum;
38         xdrproc_t inproc, outproc;
39         char *in, *out;
40 {
41         enum clnt_stat clnt_stat;
42         struct timeval tottimeout;
43  
44         if (!vol->v_nfsclient) {
45           struct hostent *hp;
46           struct sockaddr_in server_addr;
47           struct timeval timeout;
48           int socket = RPC_ANYSOCK;
49           
50           if ((hp = gethostbyname(vol->v_gvs)) == NULL)
51             return ((int) RPC_UNKNOWNHOST);
52           timeout.tv_usec = 0;
53           timeout.tv_sec = 6;
54           memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
55           server_addr.sin_family = AF_INET;
56           server_addr.sin_port =  0;
57           
58           if ((vol->v_nfsclient = (void *) 
59                clntudp_create(&server_addr, prognum, versnum, 
60                               timeout, &socket)) == NULL)
61             return ((int) rpc_createerr.cf_stat);
62           
63           ((CLIENT *) vol->v_nfsclient)->cl_auth = authunix_create_default();
64         }
65
66         tottimeout.tv_sec = 10;
67         tottimeout.tv_usec = 0;
68         clnt_stat = clnt_call((CLIENT *) vol->v_nfsclient, procnum,
69                               inproc, in, outproc, out, tottimeout);
70         return ((int) clnt_stat);
71 }
72
73
74 /* sunos 4 machines structure things a little differently. */
75 #ifdef USE_OLD_RQUOTA
76 #define GQR_STATUS gqr_status
77 #define GQR_RQUOTA gqr_rquota
78 #else
79 #define GQR_STATUS status
80 #define GQR_RQUOTA getquota_rslt_u.gqr_rquota
81 #endif
82
83 int getnfsquota(const struct vol *vol, const int uid, const u_int32_t bsize,
84                 struct dqblk *dqp)
85 {
86
87   struct getquota_args gq_args;
88   struct getquota_rslt gq_rslt;
89   struct timeval tv;
90   char *hostpath;
91  
92   /* figure out the host and path */
93   if ((hostpath = strchr(vol->v_gvs, ':')) == NULL) {
94     syslog(LOG_ERR, "can't find hostname for %s", vol->v_gvs);
95     return AFPERR_PARAM;
96   }
97  
98   if (*(hostpath + 1) != '/') 
99     return AFPERR_PARAM;
100
101   /* separate host from hostpath */
102   *hostpath = '\0';
103   
104   gq_args.gqa_pathp = hostpath + 1;
105   gq_args.gqa_uid = uid;
106
107   if(callaurpc(vol, RQUOTAPROG, RQUOTAVERS, RQUOTAPROC_GETQUOTA, 
108                (xdrproc_t) xdr_getquota_args, (char *) &gq_args,
109                (xdrproc_t) xdr_getquota_rslt, (char *) &gq_rslt) != 0) {
110     syslog(LOG_INFO, "nfsquota: can't retrieve nfs quota information. \
111 make sure that rpc.rquotad is running on %s.", vol->v_gvs);
112     *hostpath = ':';
113     return AFPERR_PARAM;
114   }
115  
116   switch (gq_rslt.GQR_STATUS) {
117   case Q_NOQUOTA:
118     break;
119
120   case Q_EPERM:
121     syslog(LOG_ERR, "nfsquota: quota permission error, host: %s\n",
122            vol->v_gvs);
123     break;
124
125   case Q_OK: /* we only copy the bits that we need. */
126     gettimeofday(&tv, NULL);
127
128 #ifdef __svr4__
129     /* why doesn't using bsize work? */
130 #define NFS_BSIZE (gq_rslt.GQR_RQUOTA.rq_bsize / DEV_BSIZE)
131 #else
132     /* NOTE: linux' rquotad program doesn't currently report the
133      * correct rq_bsize. */
134 #define NFS_BSIZE (gq_rslt.GQR_RQUOTA.rq_bsize / bsize)
135 #endif
136
137     dqp->dqb_bhardlimit =
138       gq_rslt.GQR_RQUOTA.rq_bhardlimit*NFS_BSIZE;
139     dqp->dqb_bsoftlimit =
140       gq_rslt.GQR_RQUOTA.rq_bsoftlimit*NFS_BSIZE;
141     dqp->dqb_curblocks =
142       gq_rslt.GQR_RQUOTA.rq_curblocks*NFS_BSIZE;
143
144 #ifdef ultrix
145     dqp->dqb_bwarn = gq_rslt.GQR_RQUOTA.rq_btimeleft;
146 #else
147     dqp->dqb_btimelimit =
148       tv.tv_sec + gq_rslt.GQR_RQUOTA.rq_btimeleft;
149 #endif
150
151     *hostpath = ':';
152     return AFP_OK;
153     break;
154
155   default:
156     syslog(LOG_INFO, "bad rpc result, host: %s\n", vol->v_gvs);
157     break;
158   }
159
160   *hostpath = ':';
161   return AFPERR_PARAM;
162 }
163 #endif