1 | /* |
---|
2 | * Disk quota reporting program. |
---|
3 | */ |
---|
4 | #include <stdio.h> |
---|
5 | #ifdef SOLARIS |
---|
6 | #include <sys/mntent.h> |
---|
7 | #include <rpcsvc/rquota.h> |
---|
8 | #include <netinet/in.h> |
---|
9 | #else |
---|
10 | #include <mntent.h> |
---|
11 | #endif |
---|
12 | #include <string.h> |
---|
13 | |
---|
14 | #include <sys/param.h> |
---|
15 | #include <sys/file.h> |
---|
16 | #include <sys/stat.h> |
---|
17 | #ifdef SOLARIS |
---|
18 | #include <sys/fs/ufs_quota.h> |
---|
19 | #else |
---|
20 | #include <ufs/quota.h> |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <rpc/rpc.h> |
---|
24 | #include <rpc/pmap_prot.h> |
---|
25 | #include <sys/socket.h> |
---|
26 | #include <netdb.h> |
---|
27 | #include <rpcsvc/rquota.h> |
---|
28 | #include <sys/time.h> |
---|
29 | |
---|
30 | #include "xquota.h" |
---|
31 | |
---|
32 | static int callaurpc(); |
---|
33 | |
---|
34 | /************************************************************ |
---|
35 | * |
---|
36 | * stolen from ucb's quota. (Modified somewhat) |
---|
37 | * |
---|
38 | ************************************************************/ |
---|
39 | |
---|
40 | int |
---|
41 | getnfsquota(host, path, uid, dqp) |
---|
42 | char *host, *path; |
---|
43 | int uid; |
---|
44 | struct dqblk *dqp; |
---|
45 | { |
---|
46 | struct getquota_args gq_args; |
---|
47 | struct getquota_rslt gq_rslt; |
---|
48 | |
---|
49 | gq_args.gqa_pathp = path; |
---|
50 | gq_args.gqa_uid = uid; |
---|
51 | if (callaurpc(host, RQUOTAPROG, RQUOTAVERS, RQUOTAPROC_GETQUOTA, |
---|
52 | xdr_getquota_args, (char *) &gq_args, xdr_getquota_rslt, |
---|
53 | (char *) &gq_rslt) != 0) { |
---|
54 | return (QUOTA_ERROR); |
---|
55 | } |
---|
56 | #ifdef SOLARIS |
---|
57 | switch (gq_rslt.status) { |
---|
58 | #else |
---|
59 | switch (gq_rslt.gqr_status) { |
---|
60 | #endif |
---|
61 | case Q_OK: |
---|
62 | { |
---|
63 | struct timeval tv; |
---|
64 | |
---|
65 | gettimeofday(&tv, NULL); |
---|
66 | #ifdef SOLARIS |
---|
67 | dqp->dqb_bhardlimit = |
---|
68 | gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit * |
---|
69 | gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE; |
---|
70 | dqp->dqb_bsoftlimit = |
---|
71 | gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit * |
---|
72 | gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE; |
---|
73 | dqp->dqb_curblocks = |
---|
74 | gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks * |
---|
75 | gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE; |
---|
76 | dqp->dqb_fhardlimit = gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit; |
---|
77 | dqp->dqb_fsoftlimit = gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit; |
---|
78 | dqp->dqb_curfiles = gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles; |
---|
79 | dqp->dqb_btimelimit = |
---|
80 | tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft; |
---|
81 | dqp->dqb_ftimelimit = |
---|
82 | tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft; |
---|
83 | #else |
---|
84 | dqp->dqb_bhardlimit = |
---|
85 | gq_rslt.gqr_rquota.rq_bhardlimit * |
---|
86 | gq_rslt.gqr_rquota.rq_bsize / DEV_BSIZE; |
---|
87 | dqp->dqb_bsoftlimit = |
---|
88 | gq_rslt.gqr_rquota.rq_bsoftlimit * |
---|
89 | gq_rslt.gqr_rquota.rq_bsize / DEV_BSIZE; |
---|
90 | dqp->dqb_curblocks = |
---|
91 | gq_rslt.gqr_rquota.rq_curblocks * |
---|
92 | gq_rslt.gqr_rquota.rq_bsize / DEV_BSIZE; |
---|
93 | dqp->dqb_fhardlimit = gq_rslt.gqr_rquota.rq_fhardlimit; |
---|
94 | dqp->dqb_fsoftlimit = gq_rslt.gqr_rquota.rq_fsoftlimit; |
---|
95 | dqp->dqb_curfiles = gq_rslt.gqr_rquota.rq_curfiles; |
---|
96 | dqp->dqb_btimelimit = |
---|
97 | tv.tv_sec + gq_rslt.gqr_rquota.rq_btimeleft; |
---|
98 | dqp->dqb_ftimelimit = |
---|
99 | tv.tv_sec + gq_rslt.gqr_rquota.rq_ftimeleft; |
---|
100 | #endif |
---|
101 | return (QUOTA_OK); |
---|
102 | } |
---|
103 | |
---|
104 | case Q_NOQUOTA: |
---|
105 | return(QUOTA_NONE); |
---|
106 | break; |
---|
107 | |
---|
108 | case Q_EPERM: |
---|
109 | fprintf(stderr, "quota permission error, host: %s\n", host); |
---|
110 | return(QUOTA_PERMISSION); |
---|
111 | break; |
---|
112 | |
---|
113 | default: |
---|
114 | fprintf(stderr, "bad rpc result, host: %s\n", host); |
---|
115 | break; |
---|
116 | } |
---|
117 | return (QUOTA_ERROR); |
---|
118 | } |
---|
119 | |
---|
120 | static int |
---|
121 | callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out) |
---|
122 | char *host; |
---|
123 | xdrproc_t inproc, outproc; |
---|
124 | char *in, *out; |
---|
125 | { |
---|
126 | struct sockaddr_in server_addr; |
---|
127 | enum clnt_stat clnt_stat; |
---|
128 | struct hostent *hp; |
---|
129 | struct timeval timeout, tottimeout; |
---|
130 | |
---|
131 | static CLIENT *client = NULL; |
---|
132 | static int socket = RPC_ANYSOCK; |
---|
133 | static int valid = 0; |
---|
134 | static int oldprognum, oldversnum; |
---|
135 | static char oldhost[256]; |
---|
136 | |
---|
137 | if (valid && oldprognum == prognum && oldversnum == versnum |
---|
138 | && strcmp(oldhost, host) == 0) { |
---|
139 | /* reuse old client */ |
---|
140 | } |
---|
141 | else { |
---|
142 | valid = 0; |
---|
143 | close(socket); |
---|
144 | socket = RPC_ANYSOCK; |
---|
145 | if (client) { |
---|
146 | clnt_destroy(client); |
---|
147 | client = NULL; |
---|
148 | } |
---|
149 | if ((hp = gethostbyname(host)) == NULL) |
---|
150 | return ((int) RPC_UNKNOWNHOST); |
---|
151 | timeout.tv_usec = 0; |
---|
152 | timeout.tv_sec = 6; |
---|
153 | memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length); |
---|
154 | server_addr.sin_family = AF_INET; |
---|
155 | /* ping the remote end via tcp to see if it is up */ |
---|
156 | server_addr.sin_port = htons(PMAPPORT); |
---|
157 | if ((client = clnttcp_create(&server_addr, PMAPPROG, |
---|
158 | PMAPVERS, &socket, 0, 0)) == NULL) { |
---|
159 | return ((int) rpc_createerr.cf_stat); |
---|
160 | } else { |
---|
161 | /* the fact we succeeded means the machine is up */ |
---|
162 | close(socket); |
---|
163 | socket = RPC_ANYSOCK; |
---|
164 | clnt_destroy(client); |
---|
165 | client = NULL; |
---|
166 | } |
---|
167 | /* now really create a udp client handle */ |
---|
168 | server_addr.sin_port = 0; |
---|
169 | if ((client = clntudp_create(&server_addr, prognum, |
---|
170 | versnum, timeout, &socket)) == NULL) |
---|
171 | return ((int) rpc_createerr.cf_stat); |
---|
172 | client->cl_auth = authunix_create_default(); |
---|
173 | valid = 1; |
---|
174 | oldprognum = prognum; |
---|
175 | oldversnum = versnum; |
---|
176 | strcpy(oldhost, host); |
---|
177 | } |
---|
178 | tottimeout.tv_sec = 25; |
---|
179 | tottimeout.tv_usec = 0; |
---|
180 | clnt_stat = clnt_call(client, procnum, inproc, in, |
---|
181 | outproc, out, tottimeout); |
---|
182 | /* |
---|
183 | * if call failed, empty cache |
---|
184 | */ |
---|
185 | if (clnt_stat != RPC_SUCCESS) |
---|
186 | valid = 0; |
---|
187 | return ((int) clnt_stat); |
---|
188 | } |
---|
189 | |
---|
190 | #ifdef SOLARIS |
---|
191 | bool_t |
---|
192 | xdr_rquota(xdrs, rqp) |
---|
193 | XDR *xdrs; |
---|
194 | struct rquota *rqp; |
---|
195 | { |
---|
196 | |
---|
197 | return (xdr_int(xdrs, &rqp->rq_bsize) && |
---|
198 | xdr_bool(xdrs, &rqp->rq_active) && |
---|
199 | xdr_u_long(xdrs, &rqp->rq_bhardlimit) && |
---|
200 | xdr_u_long(xdrs, &rqp->rq_bsoftlimit) && |
---|
201 | xdr_u_long(xdrs, &rqp->rq_curblocks) && |
---|
202 | xdr_u_long(xdrs, &rqp->rq_fhardlimit) && |
---|
203 | xdr_u_long(xdrs, &rqp->rq_fsoftlimit) && |
---|
204 | xdr_u_long(xdrs, &rqp->rq_curfiles) && |
---|
205 | xdr_u_long(xdrs, &rqp->rq_btimeleft) && |
---|
206 | xdr_u_long(xdrs, &rqp->rq_ftimeleft) ); |
---|
207 | } |
---|
208 | bool_t |
---|
209 | xdr_path(xdrs, pathp) |
---|
210 | XDR *xdrs; |
---|
211 | char **pathp; |
---|
212 | { |
---|
213 | if (xdr_string(xdrs, pathp, 1024)) { |
---|
214 | return(TRUE); |
---|
215 | } |
---|
216 | return(FALSE); |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | struct xdr_discrim gqr_arms[2] = { |
---|
221 | { (int)Q_OK, xdr_rquota }, |
---|
222 | { __dontcare__, NULL } |
---|
223 | }; |
---|
224 | |
---|
225 | bool_t |
---|
226 | xdr_getquota_args(xdrs, gq_argsp) |
---|
227 | XDR *xdrs; |
---|
228 | struct getquota_args *gq_argsp; |
---|
229 | { |
---|
230 | extern bool_t xdr_path(); |
---|
231 | |
---|
232 | return (xdr_path(xdrs, &gq_argsp->gqa_pathp) && |
---|
233 | xdr_int(xdrs, &gq_argsp->gqa_uid)); |
---|
234 | } |
---|
235 | |
---|
236 | bool_t |
---|
237 | xdr_getquota_rslt(xdrs, gq_rsltp) |
---|
238 | XDR *xdrs; |
---|
239 | struct getquota_rslt *gq_rsltp; |
---|
240 | { |
---|
241 | |
---|
242 | return (xdr_union(xdrs, |
---|
243 | &gq_rsltp->status, &gq_rsltp->getquota_rslt_u.gqr_rquota, |
---|
244 | gqr_arms, xdr_void)); |
---|
245 | } |
---|
246 | #endif |
---|