ntquotas: support "freeing" an empty quota list
[metze/samba/wip.git] / source3 / libsmb / cliquota.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client quota functions
4    Copyright (C) Stefan (metze) Metzmacher      2003
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 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "../librpc/gen_ndr/ndr_security.h"
23 #include "fake_file.h"
24 #include "../libcli/security/security.h"
25 #include "trans2.h"
26
27 NTSTATUS cli_get_quota_handle(struct cli_state *cli, uint16_t *quota_fnum)
28 {
29         return cli_ntcreate(cli, FAKE_FILE_NAME_QUOTA_WIN32,
30                  0x00000016, DESIRED_ACCESS_PIPE,
31                  0x00000000, FILE_SHARE_READ|FILE_SHARE_WRITE,
32                  FILE_OPEN, 0x00000000, 0x03, quota_fnum, NULL);
33 }
34
35 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
36 {
37         if (!qt_list || !*qt_list) {
38                 return;
39         }
40
41         if ((*qt_list)->mem_ctx)
42                 talloc_destroy((*qt_list)->mem_ctx);
43
44         (*qt_list) = NULL;
45
46         return; 
47 }
48
49 static bool parse_user_quota_record(const uint8_t *rdata,
50                                     unsigned int rdata_count,
51                                     unsigned int *offset,
52                                     SMB_NTQUOTA_STRUCT *pqt)
53 {
54         int sid_len;
55         SMB_NTQUOTA_STRUCT qt;
56
57         ZERO_STRUCT(qt);
58
59         if (!rdata||!offset||!pqt) {
60                 smb_panic("parse_quota_record: called with NULL POINTER!");
61         }
62
63         if (rdata_count < 40) {
64                 return False;
65         }
66
67         /* offset to next quota record.
68          * 4 bytes IVAL(rdata,0)
69          * unused here...
70          */
71         *offset = IVAL(rdata,0);
72
73         /* sid len */
74         sid_len = IVAL(rdata,4);
75
76         if (rdata_count < 40+sid_len) {
77                 return False;           
78         }
79
80         /* unknown 8 bytes in pdata 
81          * maybe its the change time in NTTIME
82          */
83
84         /* the used space 8 bytes (uint64_t)*/
85         qt.usedspace = BVAL(rdata,16);
86
87         /* the soft quotas 8 bytes (uint64_t)*/
88         qt.softlim = BVAL(rdata,24);
89
90         /* the hard quotas 8 bytes (uint64_t)*/
91         qt.hardlim = BVAL(rdata,32);
92
93         if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
94                 return false;
95         }
96
97         qt.qtype = SMB_USER_QUOTA_TYPE;
98
99         *pqt = qt;
100
101         return True;
102 }
103
104 NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
105                             SMB_NTQUOTA_STRUCT *pqt)
106 {
107         uint16_t setup[1];
108         uint8_t params[16];
109         unsigned int data_len;
110         uint8_t data[SID_MAX_SIZE+8];
111         uint8_t *rparam, *rdata;
112         uint32_t rparam_count, rdata_count;
113         unsigned int sid_len;
114         unsigned int offset;
115         NTSTATUS status;
116
117         if (!cli||!pqt) {
118                 smb_panic("cli_get_user_quota() called with NULL Pointer!");
119         }
120
121         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
122
123         SSVAL(params, 0,quota_fnum);
124         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
125         SIVAL(params, 4,0x00000024);
126         SIVAL(params, 8,0x00000000);
127         SIVAL(params,12,0x00000024);
128
129         sid_len = ndr_size_dom_sid(&pqt->sid, 0);
130         data_len = sid_len+8;
131         SIVAL(data, 0, 0x00000000);
132         SIVAL(data, 4, sid_len);
133         sid_linearize(data+8, sid_len, &pqt->sid);
134
135         status = cli_trans(talloc_tos(), cli, SMBnttrans,
136                            NULL, -1, /* name, fid */
137                            NT_TRANSACT_GET_USER_QUOTA, 0,
138                            setup, 1, 0, /* setup */
139                            params, 16, 4, /* params */
140                            data, data_len, 112, /* data */
141                            NULL,                /* recv_flags2 */
142                            NULL, 0, NULL,       /* rsetup */
143                            &rparam, 4, &rparam_count,
144                            &rdata, 8, &rdata_count);
145         if (!NT_STATUS_IS_OK(status)) {
146                 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
147                           nt_errstr(status)));
148                 return status;
149         }
150
151         if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
152                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
153                 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
154         }
155
156         TALLOC_FREE(rparam);
157         TALLOC_FREE(rdata);
158         return status;
159 }
160
161 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
162                             SMB_NTQUOTA_STRUCT *pqt)
163 {
164         uint16_t setup[1];
165         uint8_t params[2];
166         uint8_t data[112];
167         unsigned int sid_len;   
168         NTSTATUS status;
169
170         memset(data,'\0',112);
171
172         if (!cli||!pqt) {
173                 smb_panic("cli_set_user_quota() called with NULL Pointer!");
174         }
175
176         SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
177
178         SSVAL(params,0,quota_fnum);
179
180         sid_len = ndr_size_dom_sid(&pqt->sid, 0);
181         SIVAL(data,0,0);
182         SIVAL(data,4,sid_len);
183         SBIG_UINT(data, 8,(uint64_t)0);
184         SBIG_UINT(data,16,pqt->usedspace);
185         SBIG_UINT(data,24,pqt->softlim);
186         SBIG_UINT(data,32,pqt->hardlim);
187         sid_linearize(data+40, sid_len, &pqt->sid);
188
189         status = cli_trans(talloc_tos(), cli, SMBnttrans,
190                            NULL, -1, /* name, fid */
191                            NT_TRANSACT_SET_USER_QUOTA, 0,
192                            setup, 1, 0, /* setup */
193                            params, 2, 0, /* params */
194                            data, 112, 0, /* data */
195                            NULL,                /* recv_flags2 */
196                            NULL, 0, NULL,       /* rsetup */
197                            NULL, 0, NULL,       /* rparams */
198                            NULL, 0, NULL);      /* rdata */
199
200         if (!NT_STATUS_IS_OK(status)) {
201                 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
202                           nt_errstr(status)));
203         }
204
205         return status;
206 }
207
208 NTSTATUS cli_list_user_quota(struct cli_state *cli, int quota_fnum,
209                              SMB_NTQUOTA_LIST **pqt_list)
210 {
211         uint16_t setup[1];
212         uint8_t params[16];
213         uint8_t *rparam=NULL, *rdata=NULL;
214         uint32_t rparam_count=0, rdata_count=0;
215         unsigned int offset;
216         const uint8_t *curdata = NULL;
217         unsigned int curdata_count = 0;
218         TALLOC_CTX *mem_ctx = NULL;
219         SMB_NTQUOTA_STRUCT qt;
220         SMB_NTQUOTA_LIST *tmp_list_ent;
221         NTSTATUS status;
222
223         if (!cli||!pqt_list) {
224                 smb_panic("cli_list_user_quota() called with NULL Pointer!");
225         }
226
227         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
228
229         SSVAL(params, 0,quota_fnum);
230         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_START);
231         SIVAL(params, 4,0x00000000);
232         SIVAL(params, 8,0x00000000);
233         SIVAL(params,12,0x00000000);
234
235         status = cli_trans(talloc_tos(), cli, SMBnttrans,
236                            NULL, -1, /* name, fid */
237                            NT_TRANSACT_GET_USER_QUOTA, 0,
238                            setup, 1, 0, /* setup */
239                            params, 16, 4, /* params */
240                            NULL, 0, 2048, /* data */
241                            NULL,                /* recv_flags2 */
242                            NULL, 0, NULL,       /* rsetup */
243                            &rparam, 0, &rparam_count,
244                            &rdata, 0, &rdata_count);
245
246         if (!NT_STATUS_IS_OK(status) &&
247             !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
248                 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
249                           nt_errstr(status)));
250                 goto cleanup;
251         }
252
253         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) ||
254             rdata_count == 0) {
255                 *pqt_list = NULL;
256                 return NT_STATUS_OK;
257         }
258
259         if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
260                 DEBUG(0,("talloc_init() failed\n"));
261                 return NT_STATUS_NO_MEMORY;
262         }
263
264         offset = 1;
265         for (curdata=rdata,curdata_count=rdata_count;
266                 ((curdata)&&(curdata_count>=8)&&(offset>0));
267                 curdata +=offset,curdata_count -= offset) {
268                 ZERO_STRUCT(qt);
269                 if (!parse_user_quota_record((const uint8_t *)curdata, curdata_count,
270                                              &offset, &qt)) {
271                         DEBUG(1,("Failed to parse the quota record\n"));
272                         goto cleanup;
273                 }
274
275                 if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
276                         DEBUG(0,("TALLOC_ZERO() failed\n"));
277                         talloc_destroy(mem_ctx);
278                         return NT_STATUS_NO_MEMORY;
279                 }
280
281                 if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
282                         DEBUG(0,("TALLOC_ZERO() failed\n"));
283                         talloc_destroy(mem_ctx);
284                         return NT_STATUS_NO_MEMORY;
285                 }
286
287                 memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
288                 tmp_list_ent->mem_ctx = mem_ctx;                
289
290                 DLIST_ADD((*pqt_list),tmp_list_ent);
291         }
292
293         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE); 
294         while(1) {
295
296                 TALLOC_FREE(rparam);
297                 TALLOC_FREE(rdata);
298
299                 status = cli_trans(talloc_tos(), cli, SMBnttrans,
300                                    NULL, -1, /* name, fid */
301                                    NT_TRANSACT_GET_USER_QUOTA, 0,
302                                    setup, 1, 0, /* setup */
303                                    params, 16, 4, /* params */
304                                    NULL, 0, 2048, /* data */
305                                    NULL,                /* recv_flags2 */
306                                    NULL, 0, NULL,       /* rsetup */
307                                    &rparam, 0, &rparam_count,
308                                    &rdata, 0, &rdata_count);
309
310                 if (!NT_STATUS_IS_OK(status) &&
311                     !NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
312                         DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
313                                   nt_errstr(status)));
314                         goto cleanup;
315                 }
316
317                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES) ||
318                     rdata_count == 0) {
319                         status = NT_STATUS_OK;
320                         break;
321                 }
322
323                 offset = 1;
324                 for (curdata=rdata,curdata_count=rdata_count;
325                         ((curdata)&&(curdata_count>=8)&&(offset>0));
326                         curdata +=offset,curdata_count -= offset) {
327                         ZERO_STRUCT(qt);
328                         if (!parse_user_quota_record((const uint8_t *)curdata,
329                                                      curdata_count, &offset,
330                                                      &qt)) {
331                                 DEBUG(1,("Failed to parse the quota record\n"));
332                                 goto cleanup;
333                         }
334
335                         if ((tmp_list_ent=talloc_zero(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
336                                 DEBUG(0,("TALLOC_ZERO() failed\n"));
337                                 talloc_destroy(mem_ctx);
338                                 goto cleanup;
339                         }
340
341                         if ((tmp_list_ent->quotas=talloc_zero(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
342                                 DEBUG(0,("TALLOC_ZERO() failed\n"));
343                                 talloc_destroy(mem_ctx);
344                                 goto cleanup;
345                         }
346
347                         memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
348                         tmp_list_ent->mem_ctx = mem_ctx;                
349
350                         DLIST_ADD((*pqt_list),tmp_list_ent);
351                 }
352         }
353
354  cleanup:
355         TALLOC_FREE(rparam);
356         TALLOC_FREE(rdata);
357
358         return status;
359 }
360
361 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
362                                SMB_NTQUOTA_STRUCT *pqt)
363 {
364         uint16_t setup[1];
365         uint8_t param[2];
366         uint8_t *rdata=NULL;
367         uint32_t rdata_count=0;
368         SMB_NTQUOTA_STRUCT qt;
369         NTSTATUS status;
370
371         ZERO_STRUCT(qt);
372
373         if (!cli||!pqt) {
374                 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
375         }
376
377         SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
378
379         SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
380
381         status = cli_trans(talloc_tos(), cli, SMBtrans2,
382                            NULL, -1, /* name, fid */
383                            0, 0,     /* function, flags */
384                            setup, 1, 0, /* setup */
385                            param, 2, 0, /* param */
386                            NULL, 0, 560, /* data */
387                            NULL,         /* recv_flags2 */
388                            NULL, 0, NULL, /* rsetup */
389                            NULL, 0, NULL, /* rparam */
390                            &rdata, 48, &rdata_count);
391
392         if (!NT_STATUS_IS_OK(status)) {
393                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
394                           nt_errstr(status)));
395                 return status;
396         }
397
398         /* unknown_1 24 NULL bytes in pdata*/
399
400         /* the soft quotas 8 bytes (uint64_t)*/
401         qt.softlim = BVAL(rdata,24);
402
403         /* the hard quotas 8 bytes (uint64_t)*/
404         qt.hardlim = BVAL(rdata,32);
405
406         /* quota_flags 2 bytes **/
407         qt.qflags = SVAL(rdata,40);
408
409         qt.qtype = SMB_USER_FS_QUOTA_TYPE;
410
411         *pqt = qt;
412
413         TALLOC_FREE(rdata);
414         return status;
415 }
416
417 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
418                                SMB_NTQUOTA_STRUCT *pqt)
419 {
420         uint16_t setup[1];
421         uint8_t param[4];
422         uint8_t data[48];
423         SMB_NTQUOTA_STRUCT qt;
424         NTSTATUS status;
425         ZERO_STRUCT(qt);
426         memset(data,'\0',48);
427
428         if (!cli||!pqt) {
429                 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
430         }
431
432         SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
433
434         SSVAL(param,0,quota_fnum);
435         SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
436
437         /* Unknown1 24 NULL bytes*/
438
439         /* Default Soft Quota 8 bytes */
440         SBIG_UINT(data,24,pqt->softlim);
441
442         /* Default Hard Quota 8 bytes */
443         SBIG_UINT(data,32,pqt->hardlim);
444
445         /* Quota flag 2 bytes */
446         SSVAL(data,40,pqt->qflags);
447
448         /* Unknown3 6 NULL bytes */
449
450         status = cli_trans(talloc_tos(), cli, SMBtrans2,
451                            NULL, -1, /* name, fid */
452                            0, 0,     /* function, flags */
453                            setup, 1, 0, /* setup */
454                            param, 8, 0, /* param */
455                            data, 48, 0, /* data */
456                            NULL,         /* recv_flags2 */
457                            NULL, 0, NULL, /* rsetup */
458                            NULL, 0, NULL, /* rparam */
459                            NULL, 0, NULL); /* rdata */
460
461         if (!NT_STATUS_IS_OK(status)) {
462                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
463                           nt_errstr(status)));
464         }
465
466         return status;
467 }