wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / lib / sysquotas_nfs.c
1 /* 
2    Unix SMB/CIFS implementation.
3    System QUOTA function wrappers for NFS
4    Copyright (C) Michael Adam 2010
5    
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.
10    
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.
15    
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/>.
18 */
19
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_QUOTA
25
26 #ifndef HAVE_SYS_QUOTAS
27 #ifdef HAVE_NFS_QUOTAS
28 #undef HAVE_NFS_QUOTAS
29 #endif
30 #endif
31
32 #ifdef HAVE_NFS_QUOTAS
33
34 /*
35  * nfs quota support
36  * This is based on the FreeBSD / SUNOS5 section of quotas.c
37  */
38
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>
46
47 #ifndef RQ_PATHLEN
48 #define RQ_PATHLEN 1024
49 #endif
50
51 #ifdef HAVE_GETQUOTA_RSLT_GETQUOTA_RSLT_U
52 #define GQR_RQUOTA getquota_rslt_u.gqr_rquota
53 #define GQR_STATUS status
54 #else
55 #define GQR_RQUOTA gqr_rquota
56 #define GQR_STATUS gqr_status
57 #endif
58
59 static int my_xdr_getquota_args(XDR *xdrsp, struct getquota_args *args)
60 {
61         if (!xdr_string(xdrsp, &args->gqa_pathp, RQ_PATHLEN ))
62                 return(0);
63         if (!xdr_int(xdrsp, &args->gqa_uid))
64                 return(0);
65         return (1);
66 }
67
68 static int my_xdr_getquota_rslt(XDR *xdrsp, struct getquota_rslt *gqr)
69 {
70         int quotastat;
71
72         if (!xdr_int(xdrsp, &quotastat)) {
73                 DEBUG(6,("nfs_quotas: Status bad or zero\n"));
74                 return 0;
75         }
76         gqr->GQR_STATUS = quotastat;
77
78         if (!xdr_int(xdrsp, &gqr->GQR_RQUOTA.rq_bsize)) {
79                 DEBUG(6,("nfs_quotas: Block size bad or zero\n"));
80                 return 0;
81         }
82         if (!xdr_bool(xdrsp, &gqr->GQR_RQUOTA.rq_active)) {
83                 DEBUG(6,("nfs_quotas: Active bad or zero\n"));
84                 return 0;
85         }
86         if (!xdr_int(xdrsp, (int *)&gqr->GQR_RQUOTA.rq_bhardlimit)) {
87                 DEBUG(6,("nfs_quotas: Hardlimit bad or zero\n"));
88                 return 0;
89         }
90         if (!xdr_int(xdrsp, (int *)&gqr->GQR_RQUOTA.rq_bsoftlimit)) {
91                 DEBUG(6,("nfs_quotas: Softlimit bad or zero\n"));
92                 return 0;
93         }
94         if (!xdr_int(xdrsp, (int *)&gqr->GQR_RQUOTA.rq_curblocks)) {
95                 DEBUG(6,("nfs_quotas: Currentblocks bad or zero\n"));
96                 return 0;
97         }
98         return (1);
99 }
100
101
102 int sys_get_nfs_quota(const char *path, const char *bdev,
103                       enum SMB_QUOTA_TYPE qtype,
104                       unid_t id, SMB_DISK_QUOTA *dp)
105 {
106         CLIENT *clnt = NULL;
107         struct getquota_rslt gq_rslt;
108         struct getquota_args gq_args;
109         const char *mnttype;
110         char *cutstr, *host, *testpath;
111         int len;
112         static struct timeval timeout = {2,0};
113         enum clnt_stat clnt_stat;
114
115         int ret = -1;
116         uint32_t qflags = 0;
117
118         if (!path || !bdev || !dp) {
119                 smb_panic("sys_get_nfs_quota: called with NULL pointer");
120         }
121
122         DEBUG(10, ("sys_get_nfs_quota: path[%s] bdev[%s] qtype[%d]\n",
123                    path, bdev, qtype));
124
125         ZERO_STRUCT(*dp);
126
127         dp->qtype = qtype;
128
129         if (qtype != SMB_USER_QUOTA_TYPE) {
130                 DEBUG(3, ("sys_get_nfs_quota: got unsupported quota type '%d', "
131                           "only supported type is '%d' (SMB_USER_QUOTA_TYPE)\n",
132                           qtype, SMB_USER_QUOTA_TYPE));
133                 errno = ENOSYS;
134                 return -1;
135         }
136
137         mnttype = bdev;
138         len = strcspn(mnttype, ":");
139         cutstr = (char *) SMB_MALLOC(len+1);
140         if (cutstr == NULL) {
141                 errno = ENOMEM;
142                 return -1;
143         }
144
145         memset(cutstr, '\0', len+1);
146         host = strncat(cutstr, mnttype, sizeof(char) * len);
147         testpath = strchr_m(mnttype, ':');
148         if (testpath == NULL) {
149                 errno = EINVAL;
150                 goto out;
151         }
152         testpath++;
153         gq_args.gqa_pathp = testpath;
154         gq_args.gqa_uid = id.uid;
155
156         DEBUG(10, ("sys_get_nfs_quotas: Asking for quota of path '%s' on "
157                    "host '%s', rpcprog '%i', rpcvers '%i', network '%s'\n",
158                    host, testpath+1, (int)RQUOTAPROG, (int)RQUOTAVERS, "udp"));
159
160         clnt = clnt_create(host, RQUOTAPROG, RQUOTAVERS, "udp");
161         if (clnt == NULL) {
162                 ret = -1;
163                 goto out;
164         }
165
166         clnt->cl_auth = authunix_create_default();
167         if (clnt->cl_auth == NULL) {
168                 DEBUG(3, ("sys_get_nfs_quotas: authunix_create_default "
169                           "failed\n"));
170                 ret = -1;
171                 goto out;
172         }
173
174         clnt_stat = clnt_call(clnt,
175                               RQUOTAPROC_GETQUOTA,
176                               (const xdrproc_t) my_xdr_getquota_args,
177                               (caddr_t)&gq_args,
178                               (const xdrproc_t) my_xdr_getquota_rslt,
179                               (caddr_t)&gq_rslt,
180                               timeout);
181
182         if (clnt_stat != RPC_SUCCESS) {
183                 if (errno == ECONNREFUSED) {
184                         /* If we cannot connect with rpc.quotad, it may
185                          * simply be because there's no quota on the remote
186                          * system
187                          */
188                         DBG_INFO("clnt_call failed with ECONNREFUSED - "
189                                  "assuming no quotas on server\n");
190                         ret = 0;
191                 } else {
192                         int save_errno = errno;
193                         DBG_NOTICE("clnt_call failed - %s\n", strerror(errno));
194                         errno = save_errno;
195                         ret = -1;
196                 }
197                 goto out;
198         }
199
200         DEBUG(10, ("sys_get_nfs_quotas: getquota_rslt:\n"
201                    "status       : '%i'\n"
202                    "bsize        : '%i'\n"
203                    "active       : '%s'\n"
204                    "bhardlimit   : '%u'\n"
205                    "bsoftlimit   : '%u'\n"
206                    "curblocks    : '%u'\n"
207                    "fhardlimit   : '%u'\n"
208                    "fsoftlimit   : '%u'\n"
209                    "curfiles     : '%u'\n"
210                    "btimeleft    : '%u'\n"
211                    "ftimeleft    : '%u'\n",
212                    gq_rslt.GQR_STATUS,
213                    gq_rslt.GQR_RQUOTA.rq_bsize,
214                    gq_rslt.GQR_RQUOTA.rq_active?"yes":"no",
215                    gq_rslt.GQR_RQUOTA.rq_bhardlimit,
216                    gq_rslt.GQR_RQUOTA.rq_bsoftlimit,
217                    gq_rslt.GQR_RQUOTA.rq_curblocks,
218                    gq_rslt.GQR_RQUOTA.rq_fhardlimit,
219                    gq_rslt.GQR_RQUOTA.rq_fsoftlimit,
220                    gq_rslt.GQR_RQUOTA.rq_curfiles,
221                    gq_rslt.GQR_RQUOTA.rq_btimeleft,
222                    gq_rslt.GQR_RQUOTA.rq_ftimeleft));
223
224         /*
225          * gqr.status returns
226          *   1 if quotas exist,
227          *   2 if there is no quota set, and
228          *   3 if no permission to get the quota.
229          */
230
231         switch (gq_rslt.GQR_STATUS) {
232         case 1:
233                 DEBUG(10, ("sys_get_nfs_quotas: Good quota data\n"));
234                 dp->bsize = (uint64_t)gq_rslt.GQR_RQUOTA.rq_bsize;
235                 dp->softlimit = gq_rslt.GQR_RQUOTA.rq_bsoftlimit;
236                 dp->hardlimit = gq_rslt.GQR_RQUOTA.rq_bhardlimit;
237                 dp->curblocks = gq_rslt.GQR_RQUOTA.rq_curblocks;
238                 break;
239
240         case 2:
241                 DEBUG(5, ("sys_get_nfs_quotas: No quota set\n"));
242                 SMB_QUOTAS_SET_NO_LIMIT(dp);
243                 break;
244
245         case 3:
246                 DEBUG(3, ("sys_get_nfs_quotas: no permission to get quota\n"));
247                 errno = EPERM;
248                 ret = -1;
249                 goto out;
250
251         default:
252                 DEBUG(5, ("sys_get_nfs_quotas: Unknown remote quota status "
253                           "code '%i'\n", gq_rslt.GQR_STATUS));
254                 ret = -1;
255                 goto out;
256                 break;
257         }
258
259         dp->qflags = qflags;
260
261         ret = 0;
262
263 out:
264         if (clnt) {
265                 if (clnt->cl_auth) {
266                         auth_destroy(clnt->cl_auth);
267                 }
268                 clnt_destroy(clnt);
269         }
270
271         SAFE_FREE(cutstr);
272
273         DEBUG(10, ("sys_get_nfs_quotas: finished\n" ));
274         return ret;
275 }
276
277 int sys_set_nfs_quota(const char *path, const char *bdev,
278                       enum SMB_QUOTA_TYPE qtype,
279                       unid_t id, SMB_DISK_QUOTA *dp)
280 {
281         DEBUG(1, ("sys_set_nfs_quota : not supported\n"));
282         errno = ENOSYS;
283         return -1;
284 }
285
286 #else /* HAVE_NFS_QUOTAS */
287
288 void dummy_sysquotas_nfs(void);
289 void dummy_sysquotas_nfs(void) {}
290
291 #endif /* HAVE_NFS_QUOTAS */