ff7e6e6c89acd054de32f81553623c29945b6cac
[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         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
294                 return cli_smb2_list_user_quota_step(cli, mem_ctx, quota_fnum,
295                                                      pqt_list, first);
296         }
297
298         SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
299
300         SSVAL(params, 0,quota_fnum);
301         SSVAL(params, 2, op);
302         SIVAL(params, 4,0x00000000);
303         SIVAL(params, 8,0x00000000);
304         SIVAL(params,12,0x00000000);
305
306         status = cli_trans(talloc_tos(), cli, SMBnttrans,
307                            NULL, -1, /* name, fid */
308                            NT_TRANSACT_GET_USER_QUOTA, 0,
309                            setup, 1, 0, /* setup */
310                            params, 16, 4, /* params */
311                            NULL, 0, 2048, /* data */
312                            NULL,                /* recv_flags2 */
313                            NULL, 0, NULL,       /* rsetup */
314                            &rparam, 0, &rparam_count,
315                            &rdata, 0, &rdata_count);
316
317         /* compat. with smbd + safeguard against
318          * endless loop
319          */
320         if (NT_STATUS_IS_OK(status) && rdata_count == 0) {
321                 status = NT_STATUS_NO_MORE_ENTRIES;
322         }
323
324         if (!NT_STATUS_IS_OK(status)) {
325                 goto cleanup;
326         }
327
328         status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
329
330 cleanup:
331         TALLOC_FREE(rparam);
332         TALLOC_FREE(rdata);
333
334         return status;
335 }
336
337 NTSTATUS cli_list_user_quota(struct cli_state *cli,
338                              int quota_fnum,
339                              SMB_NTQUOTA_LIST **pqt_list)
340 {
341         NTSTATUS status;
342         TALLOC_CTX *mem_ctx = NULL;
343         bool first = true;
344
345         if (!cli || !pqt_list) {
346                 smb_panic("cli_list_user_quota() called with NULL Pointer!");
347         }
348
349         *pqt_list = NULL;
350
351         if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
352                 return NT_STATUS_NO_MEMORY;
353         }
354
355         do {
356                 status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
357                                                   pqt_list, first);
358                 first = false;
359         } while (NT_STATUS_IS_OK(status));
360
361         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
362                 status = NT_STATUS_OK;
363         }
364
365         if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
366                 TALLOC_FREE(mem_ctx);
367         }
368
369         return status;
370 }
371
372 NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
373                                SMB_NTQUOTA_STRUCT *pqt)
374 {
375         uint16_t setup[1];
376         uint8_t param[2];
377         uint8_t *rdata=NULL;
378         uint32_t rdata_count=0;
379         SMB_NTQUOTA_STRUCT qt;
380         NTSTATUS status;
381
382         ZERO_STRUCT(qt);
383
384         if (!cli||!pqt) {
385                 smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
386         }
387
388         SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
389
390         SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
391
392         status = cli_trans(talloc_tos(), cli, SMBtrans2,
393                            NULL, -1, /* name, fid */
394                            0, 0,     /* function, flags */
395                            setup, 1, 0, /* setup */
396                            param, 2, 0, /* param */
397                            NULL, 0, 560, /* data */
398                            NULL,         /* recv_flags2 */
399                            NULL, 0, NULL, /* rsetup */
400                            NULL, 0, NULL, /* rparam */
401                            &rdata, 48, &rdata_count);
402
403         if (!NT_STATUS_IS_OK(status)) {
404                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
405                           nt_errstr(status)));
406                 return status;
407         }
408
409         /* unknown_1 24 NULL bytes in pdata*/
410
411         /* the soft quotas 8 bytes (uint64_t)*/
412         qt.softlim = BVAL(rdata,24);
413
414         /* the hard quotas 8 bytes (uint64_t)*/
415         qt.hardlim = BVAL(rdata,32);
416
417         /* quota_flags 2 bytes **/
418         qt.qflags = SVAL(rdata,40);
419
420         qt.qtype = SMB_USER_FS_QUOTA_TYPE;
421
422         *pqt = qt;
423
424         TALLOC_FREE(rdata);
425         return status;
426 }
427
428 NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
429                                SMB_NTQUOTA_STRUCT *pqt)
430 {
431         uint16_t setup[1];
432         uint8_t param[4];
433         uint8_t data[48];
434         SMB_NTQUOTA_STRUCT qt;
435         NTSTATUS status;
436         ZERO_STRUCT(qt);
437         memset(data,'\0',48);
438
439         if (!cli||!pqt) {
440                 smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
441         }
442
443         SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
444
445         SSVAL(param,0,quota_fnum);
446         SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
447
448         /* Unknown1 24 NULL bytes*/
449
450         /* Default Soft Quota 8 bytes */
451         SBIG_UINT(data,24,pqt->softlim);
452
453         /* Default Hard Quota 8 bytes */
454         SBIG_UINT(data,32,pqt->hardlim);
455
456         /* Quota flag 2 bytes */
457         SSVAL(data,40,pqt->qflags);
458
459         /* Unknown3 6 NULL bytes */
460
461         status = cli_trans(talloc_tos(), cli, SMBtrans2,
462                            NULL, -1, /* name, fid */
463                            0, 0,     /* function, flags */
464                            setup, 1, 0, /* setup */
465                            param, 4, 0, /* param */
466                            data, 48, 0, /* data */
467                            NULL,         /* recv_flags2 */
468                            NULL, 0, NULL, /* rsetup */
469                            NULL, 0, NULL, /* rparam */
470                            NULL, 0, NULL); /* rdata */
471
472         if (!NT_STATUS_IS_OK(status)) {
473                 DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
474                           nt_errstr(status)));
475         }
476
477         return status;
478 }