s3:sysquota_nfs: fix build on Tru64 et alii
[Samba/gebeck_regimport.git] / source3 / lib / sysquotas_nfs.c
blob6f8713725dfec423e9cfd859b09a588c5f646866
1 /*
2 Unix SMB/CIFS implementation.
3 System QUOTA function wrappers for NFS
4 Copyright (C) Michael Adam 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_QUOTA
26 #ifndef HAVE_SYS_QUOTAS
27 #ifdef HAVE_NFS_QUOTAS
28 #undef HAVE_NFS_QUOTAS
29 #endif
30 #endif
32 #ifdef HAVE_NFS_QUOTAS
35 * nfs quota support
36 * This is based on the FreeBSD / SUNOS5 section of quotas.c
39 #include <rpc/rpc.h>
40 #include <rpc/types.h>
41 #include <rpcsvc/rquota.h>
42 #ifdef HAVE_RPC_NETTYPE_H
43 #include <rpc/nettype.h>
44 #endif
45 #include <rpc/xdr.h>
47 #ifdef HAVE_GETQUOTA_RSLT_GETQUOTA_RSLT_U
48 #define GQR_RQUOTA getquota_rslt_u.gqr_rquota
49 #define GQR_STATUS status
50 #else
51 #define GQR_RQUOTA gqr_rquota
52 #define GQR_STATUS gqr_status
53 #endif
55 static int my_xdr_getquota_args(XDR *xdrsp, struct getquota_args *args)
57 if (!xdr_string(xdrsp, &args->gqa_pathp, RQ_PATHLEN ))
58 return(0);
59 if (!xdr_int(xdrsp, &args->gqa_uid))
60 return(0);
61 return (1);
64 static int my_xdr_getquota_rslt(XDR *xdrsp, struct getquota_rslt *gqr)
66 int quotastat;
68 if (!xdr_int(xdrsp, &quotastat)) {
69 DEBUG(6,("nfs_quotas: Status bad or zero\n"));
70 return 0;
72 gqr->status = quotastat;
74 if (!xdr_int(xdrsp, &gqr->GQR_RQUOTA.rq_bsize)) {
75 DEBUG(6,("nfs_quotas: Block size bad or zero\n"));
76 return 0;
78 if (!xdr_bool(xdrsp, &gqr->GQR_RQUOTA.rq_active)) {
79 DEBUG(6,("nfs_quotas: Active bad or zero\n"));
80 return 0;
82 if (!xdr_int(xdrsp, (int *)&gqr->GQR_RQUOTA.rq_bhardlimit)) {
83 DEBUG(6,("nfs_quotas: Hardlimit bad or zero\n"));
84 return 0;
86 if (!xdr_int(xdrsp, (int *)&gqr->GQR_RQUOTA.rq_bsoftlimit)) {
87 DEBUG(6,("nfs_quotas: Softlimit bad or zero\n"));
88 return 0;
90 if (!xdr_int(xdrsp, (int *)&gqr->GQR_RQUOTA.rq_curblocks)) {
91 DEBUG(6,("nfs_quotas: Currentblocks bad or zero\n"));
92 return 0;
94 return (1);
98 int sys_get_nfs_quota(const char *path, const char *bdev,
99 enum SMB_QUOTA_TYPE qtype,
100 unid_t id, SMB_DISK_QUOTA *dp)
102 CLIENT *clnt = NULL;
103 struct getquota_rslt gq_rslt;
104 struct getquota_args gq_args;
105 const char *mnttype;
106 char *cutstr, *pathname, *host, *testpath;
107 int len;
108 static struct timeval timeout = {2,0};
109 enum clnt_stat clnt_stat;
111 int ret = -1;
112 uint32 qflags = 0;
114 if (!path || !bdev || !dp) {
115 smb_panic("sys_get_nfs_quota: called with NULL pointer");
118 DEBUG(10, ("sys_get_nfs_quota: path[%s] bdev[%s] qtype[%d]\n",
119 path, bdev, qtype));
121 ZERO_STRUCT(*dp);
123 dp->qtype = qtype;
125 if (qtype != SMB_USER_QUOTA_TYPE) {
126 DEBUG(3, ("sys_get_nfs_quota: got unsupported quota type '%d', "
127 "only supported type is '%d' (SMB_USER_QUOTA_TYPE)\n",
128 qtype, SMB_USER_QUOTA_TYPE));
129 errno = ENOSYS;
130 return -1;
133 mnttype = bdev;
134 len = strcspn(mnttype, ":");
135 pathname = strstr(mnttype, ":");
136 cutstr = (char *) SMB_MALLOC(len+1);
137 if (cutstr == NULL) {
138 errno = ENOMEM;
139 return -1;
142 memset(cutstr, '\0', len+1);
143 host = strncat(cutstr, mnttype, sizeof(char) * len);
144 testpath = strchr_m(mnttype, ':');
145 if (testpath == NULL) {
146 errno = EINVAL;
147 goto out;
149 testpath++;
150 gq_args.gqa_pathp = testpath;
151 gq_args.gqa_uid = id.uid;
153 DEBUG(10, ("sys_get_nfs_quotas: Asking for quota of path '%s' on "
154 "host '%s', rpcprog '%i', rpcvers '%i', network '%s'\n",
155 host, testpath+1, RQUOTAPROG, RQUOTAVERS, "udp"));
157 clnt = clnt_create(host, RQUOTAPROG, RQUOTAVERS, "udp");
158 if (clnt == NULL) {
159 ret = -1;
160 goto out;
163 clnt->cl_auth = authunix_create_default();
164 if (clnt->cl_auth == NULL) {
165 DEBUG(3, ("sys_get_nfs_quotas: authunix_create_default "
166 "failed\n"));
167 ret = -1;
168 goto out;
171 clnt_stat = clnt_call(clnt,
172 RQUOTAPROC_GETQUOTA,
173 (const xdrproc_t) my_xdr_getquota_args,
174 (caddr_t)&gq_args,
175 (const xdrproc_t) my_xdr_getquota_rslt,
176 (caddr_t)&gq_rslt,
177 timeout);
179 if (clnt_stat != RPC_SUCCESS) {
180 DEBUG(3, ("sys_get_nfs_quotas: clnt_call failed\n"));
181 ret = -1;
182 goto out;
185 DEBUG(10, ("sys_get_nfs_quotas: getquota_rslt:\n"
186 "status : '%i'\n"
187 "bsize : '%i'\n"
188 "active : '%s'\n"
189 "bhardlimit : '%u'\n"
190 "bsoftlimit : '%u'\n"
191 "curblocks : '%u'\n"
192 "fhardlimit : '%u'\n"
193 "fsoftlimit : '%u'\n"
194 "curfiles : '%u'\n"
195 "btimeleft : '%u'\n"
196 "ftimeleft : '%u'\n",
197 gq_rslt.GQR_STATUS,
198 gq_rslt.GQR_RQUOTA.rq_bsize,
199 gq_rslt.GQR_RQUOTA.rq_active?"yes":"no",
200 gq_rslt.GQR_RQUOTA.rq_bhardlimit,
201 gq_rslt.GQR_RQUOTA.rq_bsoftlimit,
202 gq_rslt.GQR_RQUOTA.rq_curblocks,
203 gq_rslt.GQR_RQUOTA.rq_fhardlimit,
204 gq_rslt.GQR_RQUOTA.rq_fsoftlimit,
205 gq_rslt.GQR_RQUOTA.rq_curfiles,
206 gq_rslt.GQR_RQUOTA.rq_btimeleft,
207 gq_rslt.GQR_RQUOTA.rq_ftimeleft));
210 * gqr.status returns
211 * 0 if the rpc call fails,
212 * 1 if quotas exist,
213 * 2 if there is no quota set, and
214 * 3 if no permission to get the quota.
217 switch (gq_rslt.GQR_STATUS) {
218 case 0:
219 DEBUG(3, ("sys_get_nfs_quotas: Remote Quotas Failed! "
220 "Error '%i'\n", gq_rslt.GQR_STATUS));
221 ret = -1;
222 goto out;
224 case 1:
225 DEBUG(10, ("sys_get_nfs_quotas: Good quota data\n"));
226 dp->bsize = (uint64_t)gq_rslt.GQR_RQUOTA.rq_bsize;
227 dp->softlimit = gq_rslt.GQR_RQUOTA.rq_bsoftlimit;
228 dp->hardlimit = gq_rslt.GQR_RQUOTA.rq_bhardlimit;
229 dp->curblocks = gq_rslt.GQR_RQUOTA.rq_curblocks;
230 break;
232 case 2:
233 DEBUG(5, ("sys_get_nfs_quotas: No quota set\n"));
234 SMB_QUOTAS_SET_NO_LIMIT(dp);
235 break;
237 case 3:
238 DEBUG(3, ("sys_get_nfs_quotas: no permission to get quota\n"));
239 errno = EPERM;
240 ret = -1;
241 goto out;
243 default:
244 DEBUG(5, ("sys_get_nfs_quotas: Unknown remote quota status "
245 "code '%i'\n", gq_rslt.GQR_STATUS));
246 ret = -1;
247 goto out;
248 break;
251 dp->qflags = qflags;
253 ret = 0;
255 out:
256 if (clnt) {
257 if (clnt->cl_auth) {
258 auth_destroy(clnt->cl_auth);
260 clnt_destroy(clnt);
263 SAFE_FREE(cutstr);
265 DEBUG(10, ("sys_get_nfs_quotas: finished\n" ));
266 return ret;
269 int sys_set_nfs_quota(const char *path, const char *bdev,
270 enum SMB_QUOTA_TYPE qtype,
271 unid_t id, SMB_DISK_QUOTA *dp)
273 DEBUG(1, ("sys_set_nfs_quota : not supported\n"));
274 errno = ENOSYS;
275 return -1;
278 #else /* HAVE_NFS_QUOTAS */
280 void dummy_sysquotas_nfs(void);
281 void dummy_sysquotas_nfs(void) {}
283 #endif /* HAVE_NFS_QUOTAS */