cliquota: factor out parsing of a quota record buffer
[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 cli_get_user_quota(struct cli_state *cli, int quota_fnum,
172                             SMB_NTQUOTA_STRUCT *pqt)
173 {
174         uint16_t setup[1];
175         uint8_t params[16];
176         unsigned int data_len;
177         uint8_t data[SID_MAX_SIZE+8];
178         uint8_t *rparam, *rdata;
179         uint32_t rparam_count, rdata_count;
180         unsigned int sid_len;
181         unsigned int offset;
182         NTSTATUS status;
183
184         if (!cli||!pqt) {
185                 smb_panic("cli_get_user_quota() called with NULL Pointer!");
186         }
187
188         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
189                 return cli_smb2_get_user_quota(cli, quota_fnum, pqt);
190         }
191
192         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
193
194         SSVAL(params, 0,quota_fnum);
195         SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
196         SIVAL(params, 4,0x00000024);
197         SIVAL(params, 8,0x00000000);
198         SIVAL(params,12,0x00000024);
199
200         sid_len = ndr_size_dom_sid(&pqt->sid, 0);
201         data_len = sid_len+8;
202         SIVAL(data, 0, 0x00000000);
203         SIVAL(data, 4, sid_len);
204         sid_linearize(data+8, sid_len, &pqt->sid);
205
206         status = cli_trans(talloc_tos(), cli, SMBnttrans,
207                            NULL, -1, /* name, fid */
208                            NT_TRANSACT_GET_USER_QUOTA, 0,
209                            setup, 1, 0, /* setup */
210                            params, 16, 4, /* params */
211                            data, data_len, 112, /* data */
212                            NULL,                /* recv_flags2 */
213                            NULL, 0, NULL,       /* rsetup */
214                            &rparam, 4, &rparam_count,
215                            &rdata, 8, &rdata_count);
216         if (!NT_STATUS_IS_OK(status)) {
217                 DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
218                           nt_errstr(status)));
219                 return status;
220         }
221
222         if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
223                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
224                 DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
225         }
226
227         TALLOC_FREE(rparam);
228         TALLOC_FREE(rdata);
229         return status;
230 }
231
232 NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
233                             SMB_NTQUOTA_STRUCT *pqt)
234 {
235         uint16_t setup[1];
236         uint8_t params[2];
237         uint8_t data[112];
238         unsigned int sid_len;   
239         NTSTATUS status;
240
241         memset(data,'\0',112);
242
243         if (!cli||!pqt) {
244                 smb_panic("cli_set_user_quota() called with NULL Pointer!");
245         }
246
247         SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
248
249         SSVAL(params,0,quota_fnum);
250
251         sid_len = ndr_size_dom_sid(&pqt->sid, 0);
252         SIVAL(data,0,0);
253         SIVAL(data,4,sid_len);
254         SBIG_UINT(data, 8,(uint64_t)0);
255         SBIG_UINT(data,16,pqt->usedspace);
256         SBIG_UINT(data,24,pqt->softlim);
257         SBIG_UINT(data,32,pqt->hardlim);
258         sid_linearize(data+40, sid_len, &pqt->sid);
259
260         status = cli_trans(talloc_tos(), cli, SMBnttrans,
261                            NULL, -1, /* name, fid */
262                            NT_TRANSACT_SET_USER_QUOTA, 0,
263                            setup, 1, 0, /* setup */
264                            params, 2, 0, /* params */
265                            data, 112, 0, /* data */
266                            NULL,                /* recv_flags2 */
267                            NULL, 0, NULL,       /* rsetup */
268                            NULL, 0, NULL,       /* rparams */
269                            NULL, 0, NULL);      /* rdata */
270
271         if (!NT_STATUS_IS_OK(status)) {
272                 DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
273                           nt_errstr(status)));
274         }
275
276         return status;
277 }
278
279 static NTSTATUS cli_list_user_quota_step(struct cli_state *cli,
280                                          TALLOC_CTX *mem_ctx,
281                                          int quota_fnum,
282                                          SMB_NTQUOTA_LIST **pqt_list,
283                                          bool first)
284 {
285         uint16_t setup[1];
286         uint8_t params[16];
287         uint8_t *rparam=NULL, *rdata=NULL;
288         uint32_t rparam_count=0, rdata_count=0;
289         NTSTATUS status;
290         uint16_t op = first ? TRANSACT_GET_USER_QUOTA_LIST_START
291                             : TRANSACT_GET_USER_QUOTA_LIST_CONTINUE;
292
293         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
294
295         SSVAL(params, 0,quota_fnum);
296         SSVAL(params, 2, op);
297         SIVAL(params, 4,0x00000000);
298         SIVAL(params, 8,0x00000000);
299         SIVAL(params,12,0x00000000);
300
301         status = cli_trans(talloc_tos(), cli, SMBnttrans,
302                            NULL, -1, /* name, fid */
303                            NT_TRANSACT_GET_USER_QUOTA, 0,
304                            setup, 1, 0, /* setup */
305                            params, 16, 4, /* params */
306                            NULL, 0, 2048, /* data */
307                            NULL,                /* recv_flags2 */
308                            NULL, 0, NULL,       /* rsetup */
309                            &rparam, 0, &rparam_count,
310                            &rdata, 0, &rdata_count);
311
312         /* compat. with smbd + safeguard against
313          * endless loop
314          */
315         if (NT_STATUS_IS_OK(status) && rdata_count == 0) {
316                 status = NT_STATUS_NO_MORE_ENTRIES;
317         }
318
319         if (!NT_STATUS_IS_OK(status)) {
320                 goto cleanup;
321         }
322
323         status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
324
325 cleanup:
326         TALLOC_FREE(rparam);
327         TALLOC_FREE(rdata);
328
329         return status;
330 }
331
332 NTSTATUS cli_list_user_quota(struct cli_state *cli,
333                              int quota_fnum,
334                              SMB_NTQUOTA_LIST **pqt_list)
335 {
336         NTSTATUS status;
337         TALLOC_CTX *mem_ctx = NULL;
338         bool first = true;
339
340         if (!cli || !pqt_list) {
341                 smb_panic("cli_list_user_quota() called with NULL Pointer!");
342         }
343
344         *pqt_list = NULL;
345
346         if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
347                 return NT_STATUS_NO_MEMORY;
348         }
349
350         do {
351                 status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
352                                                   pqt_list, first);
353                 first = false;
354         } while (NT_STATUS_IS_OK(status));
355
356         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
357                 status = NT_STATUS_OK;
358         }
359
360         if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
361                 TALLOC_FREE(mem_ctx);
362         }
363
364         return status;
365 }
366
367 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
368                                SMB_NTQUOTA_STRUCT *pqt)
369 {
370         uint16_t setup[1];
371         uint8_t param[2];
372         uint8_t *rdata=NULL;
373         uint32_t rdata_count=0;
374         SMB_NTQUOTA_STRUCT qt;
375         NTSTATUS status;
376
377         ZERO_STRUCT(qt);
378
379         if (!cli||!pqt) {
380                 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
381         }
382
383         SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
384
385         SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
386
387         status = cli_trans(talloc_tos(), cli, SMBtrans2,
388                            NULL, -1, /* name, fid */
389                            0, 0,     /* function, flags */
390                            setup, 1, 0, /* setup */
391                            param, 2, 0, /* param */
392                            NULL, 0, 560, /* data */
393                            NULL,         /* recv_flags2 */
394                            NULL, 0, NULL, /* rsetup */
395                            NULL, 0, NULL, /* rparam */
396                            &rdata, 48, &rdata_count);
397
398         if (!NT_STATUS_IS_OK(status)) {
399                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
400                           nt_errstr(status)));
401                 return status;
402         }
403
404         /* unknown_1 24 NULL bytes in pdata*/
405
406         /* the soft quotas 8 bytes (uint64_t)*/
407         qt.softlim = BVAL(rdata,24);
408
409         /* the hard quotas 8 bytes (uint64_t)*/
410         qt.hardlim = BVAL(rdata,32);
411
412         /* quota_flags 2 bytes **/
413         qt.qflags = SVAL(rdata,40);
414
415         qt.qtype = SMB_USER_FS_QUOTA_TYPE;
416
417         *pqt = qt;
418
419         TALLOC_FREE(rdata);
420         return status;
421 }
422
423 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
424                                SMB_NTQUOTA_STRUCT *pqt)
425 {
426         uint16_t setup[1];
427         uint8_t param[4];
428         uint8_t data[48];
429         SMB_NTQUOTA_STRUCT qt;
430         NTSTATUS status;
431         ZERO_STRUCT(qt);
432         memset(data,'\0',48);
433
434         if (!cli||!pqt) {
435                 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
436         }
437
438         SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
439
440         SSVAL(param,0,quota_fnum);
441         SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
442
443         /* Unknown1 24 NULL bytes*/
444
445         /* Default Soft Quota 8 bytes */
446         SBIG_UINT(data,24,pqt->softlim);
447
448         /* Default Hard Quota 8 bytes */
449         SBIG_UINT(data,32,pqt->hardlim);
450
451         /* Quota flag 2 bytes */
452         SSVAL(data,40,pqt->qflags);
453
454         /* Unknown3 6 NULL bytes */
455
456         status = cli_trans(talloc_tos(), cli, SMBtrans2,
457                            NULL, -1, /* name, fid */
458                            0, 0,     /* function, flags */
459                            setup, 1, 0, /* setup */
460                            param, 4, 0, /* param */
461                            data, 48, 0, /* data */
462                            NULL,         /* recv_flags2 */
463                            NULL, 0, NULL, /* rsetup */
464                            NULL, 0, NULL, /* rparam */
465                            NULL, 0, NULL); /* rdata */
466
467         if (!NT_STATUS_IS_OK(status)) {
468                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
469                           nt_errstr(status)));
470         }
471
472         return status;
473 }