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