s3: Add debug when a message is registered
[samba.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 static int my_xdr_getquota_args(XDR *xdrsp, struct getquota_args *args)
48 {
49         if (!xdr_string(xdrsp, &args->gqa_pathp, RQ_PATHLEN ))
50                 return(0);
51         if (!xdr_int(xdrsp, &args->gqa_uid))
52                 return(0);
53         return (1);
54 }
55
56 static int my_xdr_getquota_rslt(XDR *xdrsp, struct getquota_rslt *gqr)
57 {
58         int quotastat;
59
60         if (!xdr_int(xdrsp, &quotastat)) {
61                 DEBUG(6,("nfs_quotas: Status bad or zero\n"));
62                 return 0;
63         }
64         gqr->status = quotastat;
65
66         if (!xdr_int(xdrsp, &gqr->getquota_rslt_u.gqr_rquota.rq_bsize)) {
67                 DEBUG(6,("nfs_quotas: Block size bad or zero\n"));
68                 return 0;
69         }
70         if (!xdr_bool(xdrsp, &gqr->getquota_rslt_u.gqr_rquota.rq_active)) {
71                 DEBUG(6,("nfs_quotas: Active bad or zero\n"));
72                 return 0;
73         }
74         if (!xdr_int(xdrsp, (int *)&gqr->getquota_rslt_u.gqr_rquota.rq_bhardlimit)) {
75                 DEBUG(6,("nfs_quotas: Hardlimit bad or zero\n"));
76                 return 0;
77         }
78         if (!xdr_int(xdrsp, (int *)&gqr->getquota_rslt_u.gqr_rquota.rq_bsoftlimit)) {
79                 DEBUG(6,("nfs_quotas: Softlimit bad or zero\n"));
80                 return 0;
81         }
82         if (!xdr_int(xdrsp, (int *)&gqr->getquota_rslt_u.gqr_rquota.rq_curblocks)) {
83                 DEBUG(6,("nfs_quotas: Currentblocks bad or zero\n"));
84                 return 0;
85         }
86         return (1);
87 }
88
89
90 int sys_get_nfs_quota(const char *path, const char *bdev,
91                       enum SMB_QUOTA_TYPE qtype,
92                       unid_t id, SMB_DISK_QUOTA *dp)
93 {
94         CLIENT *clnt = NULL;
95         struct getquota_rslt gq_rslt;
96         struct getquota_args gq_args;
97         const char *mnttype;
98         char *cutstr, *pathname, *host, *testpath;
99         int len;
100         static struct timeval timeout = {2,0};
101         enum clnt_stat clnt_stat;
102
103         int ret = -1;
104         uint32 qflags = 0;
105
106         if (!path || !bdev || !dp) {
107                 smb_panic("sys_get_nfs_quota: called with NULL pointer");
108         }
109
110         DEBUG(10, ("sys_get_nfs_quota: path[%s] bdev[%s] qtype[%d]\n",
111                    path, bdev, qtype));
112
113         ZERO_STRUCT(*dp);
114
115         dp->qtype = qtype;
116
117         if (qtype != SMB_USER_QUOTA_TYPE) {
118                 DEBUG(3, ("sys_get_nfs_quota: got unsupported quota type '%d', "
119                           "only supported type is '%d' (SMB_USER_QUOTA_TYPE)\n",
120                           qtype, SMB_USER_QUOTA_TYPE));
121                 errno = ENOSYS;
122                 return -1;
123         }
124
125         mnttype = bdev;
126         len = strcspn(mnttype, ":");
127         pathname = strstr(mnttype, ":");
128         cutstr = (char *) SMB_MALLOC(len+1);
129         if (cutstr == NULL) {
130                 errno = ENOMEM;
131                 return -1;
132         }
133
134         memset(cutstr, '\0', len+1);
135         host = strncat(cutstr, mnttype, sizeof(char) * len);
136         testpath = strchr_m(mnttype, ':');
137         if (testpath == NULL) {
138                 errno = EINVAL;
139                 goto out;
140         }
141         testpath++;
142         gq_args.gqa_pathp = testpath;
143         gq_args.gqa_uid = id.uid;
144
145         DEBUG(10, ("sys_get_nfs_quotas: Asking for quota of path '%s' on "
146                    "host '%s', rpcprog '%i', rpcvers '%i', network '%s'\n",
147                     host, testpath+1, RQUOTAPROG, RQUOTAVERS, "udp"));
148
149         clnt = clnt_create(host, RQUOTAPROG, RQUOTAVERS, "udp");
150         if (clnt == NULL) {
151                 ret = -1;
152                 goto out;
153         }
154
155         clnt->cl_auth = authunix_create_default();
156         if (clnt->cl_auth == NULL) {
157                 DEBUG(3, ("sys_get_nfs_quotas: authunix_create_default "
158                           "failed\n"));
159                 ret = -1;
160                 goto out;
161         }
162
163         clnt_stat = clnt_call(clnt,
164                               RQUOTAPROC_GETQUOTA,
165                               (const xdrproc_t) my_xdr_getquota_args,
166                               (caddr_t)&gq_args,
167                               (const xdrproc_t) my_xdr_getquota_rslt,
168                               (caddr_t)&gq_rslt,
169                               timeout);
170
171         if (clnt_stat != RPC_SUCCESS) {
172                 DEBUG(3, ("sys_get_nfs_quotas: clnt_call failed\n"));
173                 ret = -1;
174                 goto out;
175         }
176
177         DEBUG(10, ("sys_get_nfs_quotas: getquota_rslt:\n"
178                    "status       : '%i'\n"
179                    "bsize        : '%i'\n"
180                    "active       : '%s'\n"
181                    "bhardlimit   : '%u'\n"
182                    "bsoftlimit   : '%u'\n"
183                    "curblocks    : '%u'\n"
184                    "fhardlimit   : '%u'\n"
185                    "fsoftlimit   : '%u'\n"
186                    "curfiles     : '%u'\n"
187                    "btimeleft    : '%u'\n"
188                    "ftimeleft    : '%u'\n",
189                    gq_rslt.status,
190                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize,
191                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_active?"yes":"no",
192                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit,
193                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit,
194                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks,
195                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit,
196                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit,
197                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles,
198                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft,
199                    gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft));
200
201         /*
202          * gqr.status returns
203          *   0 if the rpc call fails,
204          *   1 if quotas exist,
205          *   2 if there is no quota set, and
206          *   3 if no permission to get the quota.
207          */
208
209         switch (gq_rslt.status) {
210         case 0:
211                 DEBUG(3, ("sys_get_nfs_quotas: Remote Quotas Failed! "
212                           "Error '%i'\n", gq_rslt.status));
213                 ret = -1;
214                 goto out;
215
216         case 1:
217                 DEBUG(10, ("sys_get_nfs_quotas: Good quota data\n"));
218                 dp->bsize = (uint64_t)gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize;
219                 dp->softlimit = gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit;
220                 dp->hardlimit = gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit;
221                 dp->curblocks = gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks;
222                 break;
223
224         case 2:
225                 DEBUG(5, ("sys_get_nfs_quotas: No quota set\n"));
226                 SMB_QUOTAS_SET_NO_LIMIT(dp);
227                 break;
228
229         case 3:
230                 DEBUG(3, ("sys_get_nfs_quotas: no permission to get quota\n"));
231                 errno = EPERM;
232                 ret = -1;
233                 goto out;
234
235         default:
236                 DEBUG(5, ("sys_get_nfs_quotas: Unknown remote quota status "
237                           "code '%i'\n", gq_rslt.status));
238                 ret = -1;
239                 goto out;
240                 break;
241         }
242
243         dp->qflags = qflags;
244
245         ret = 0;
246
247 out:
248         if (clnt) {
249                 if (clnt->cl_auth) {
250                         auth_destroy(clnt->cl_auth);
251                 }
252                 clnt_destroy(clnt);
253         }
254
255         SAFE_FREE(cutstr);
256
257         DEBUG(10, ("sys_get_nfs_quotas: finished\n" ));
258         return ret;
259 }
260
261 int sys_set_nfs_quota(const char *path, const char *bdev,
262                       enum SMB_QUOTA_TYPE qtype,
263                       unid_t id, SMB_DISK_QUOTA *dp)
264 {
265         DEBUG(1, ("sys_set_nfs_quota : not supported\n"));
266         errno = ENOSYS;
267         return -1;
268 }
269
270 #else /* HAVE_NFS_QUOTAS */
271
272 void dummy_sysquotas_nfs(void);
273 void dummy_sysquotas_nfs(void) {}
274
275 #endif /* HAVE_NFS_QUOTAS */